diff mbox series

[FFmpeg-devel,5/9] lavfi/vf_qp: convert to the video_enc_params API

Message ID 20200418101415.26409-6-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,1/9] libavutil: add API for exporting video frame quantizers
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate fail Make fate failed

Commit Message

Anton Khirnov April 18, 2020, 10:14 a.m. UTC
---
 libavfilter/vf_qp.c         | 65 ++++++++++++++++++++++++-------------
 tests/fate/filter-video.mak |  6 ++--
 2 files changed, 46 insertions(+), 25 deletions(-)
diff mbox series

Patch

diff --git a/libavfilter/vf_qp.c b/libavfilter/vf_qp.c
index 33d39493bc..306e8e4594 100644
--- a/libavfilter/vf_qp.c
+++ b/libavfilter/vf_qp.c
@@ -23,6 +23,8 @@ 
 #include "libavutil/imgutils.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
+#include "libavutil/video_enc_params.h"
+
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
@@ -89,38 +91,59 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     AVFilterContext *ctx = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
     QPContext *s = ctx->priv;
-    AVBufferRef *out_qp_table_buf;
     AVFrame *out = NULL;
-    const int8_t *in_qp_table;
-    int type, stride, ret;
+    int ret;
+
+    AVFrameSideData *sd_in;
+    AVVideoEncParams *par_in = NULL;
+    int8_t in_qp_global = 0;
+
+    AVVideoEncParams *par_out;
 
     if (!s->qp_expr_str || ctx->is_disabled)
         return ff_filter_frame(outlink, in);
 
-    out_qp_table_buf = av_buffer_alloc(s->h * s->qstride);
-    if (!out_qp_table_buf) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
+    sd_in = av_frame_get_side_data(in, AV_FRAME_DATA_VIDEO_ENC_PARAMS);
+    if (sd_in && sd_in->size >= sizeof(AVVideoEncParams)) {
+        par_in = (AVVideoEncParams*)sd_in->data;
+
+        // we accept the input QP table only if it is of the MPEG2 type
+        // and contains either no blocks at all or 16x16 macroblocks
+        if (par_in->type == AV_VIDEO_ENC_PARAMS_MPEG2 &&
+            (par_in->nb_blocks == s->h * s->qstride || !par_in->nb_blocks)) {
+            in_qp_global = par_in->qp;
+            if (!par_in->nb_blocks)
+                par_in = NULL;
+        } else
+            par_in = NULL;
     }
 
     out = av_frame_clone(in);
     if (!out) {
-        av_buffer_unref(&out_qp_table_buf);
         ret = AVERROR(ENOMEM);
         goto fail;
     }
 
-    in_qp_table = av_frame_get_qp_table(in, &stride, &type);
-    av_frame_set_qp_table(out, out_qp_table_buf, s->qstride, type);
+    par_out = av_video_enc_params_create_side_data(out, AV_VIDEO_ENC_PARAMS_MPEG2,
+                                                   (s->evaluate_per_mb || sd_in) ?
+                                                   s->h * s->qstride : 0);
+    if (!par_out) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
 
+#define BLOCK_QP_DELTA(block_idx) \
+    (par_in ? av_video_enc_params_block(par_in, block_idx)->delta_qp : 0)
 
     if (s->evaluate_per_mb) {
         int y, x;
 
         for (y = 0; y < s->h; y++)
             for (x = 0; x < s->qstride; x++) {
-                int qp = in_qp_table ? in_qp_table[x + stride * y] : NAN;
-                double var_values[] = { !!in_qp_table, qp, x, y, s->qstride, s->h, 0};
+                unsigned int block_idx = y * s->qstride + x;
+                AVVideoBlockParams *b = av_video_enc_params_block(par_out, block_idx);
+                int qp = sd_in ? in_qp_global + BLOCK_QP_DELTA(block_idx) : NAN;
+                double var_values[] = { !!sd_in, qp, x, y, s->qstride, s->h, 0};
                 static const char *var_names[] = { "known", "qp", "x", "y", "w", "h", NULL };
                 double temp_val;
 
@@ -129,21 +152,19 @@  static int filter_frame(AVFilterLink *inlink, AVFrame *in)
                                             NULL, NULL, NULL, NULL, 0, 0, ctx);
                 if (ret < 0)
                     goto fail;
-                out_qp_table_buf->data[x + s->qstride * y] = lrintf(temp_val);
+                b->delta_qp = lrintf(temp_val);
             }
-    } else if (in_qp_table) {
+    } else if (sd_in) {
         int y, x;
 
         for (y = 0; y < s->h; y++)
-            for (x = 0; x < s->qstride; x++)
-                out_qp_table_buf->data[x + s->qstride * y] = s->lut[129 +
-                    ((int8_t)in_qp_table[x + stride * y])];
+            for (x = 0; x < s->qstride; x++) {
+                unsigned int block_idx = y * s->qstride + x;
+                AVVideoBlockParams *b = av_video_enc_params_block(par_out, block_idx);
+                b->delta_qp = s->lut[129 + (int8_t)(in_qp_global + BLOCK_QP_DELTA(block_idx))];
+            }
     } else {
-        int y, x, qp = s->lut[0];
-
-        for (y = 0; y < s->h; y++)
-            for (x = 0; x < s->qstride; x++)
-                out_qp_table_buf->data[x + s->qstride * y] = qp;
+        par_out->qp = s->lut[0];
     }
 
     ret = ff_filter_frame(outlink, out);
diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index 2da27f714a..3d0d4969b8 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -537,14 +537,14 @@  $(FATE_FILTER_PP): fate-vsynth1-mpeg4-qprd
 
 fate-filter-pp:  CMD = framecrc -flags bitexact -idct simple -i $(TARGET_PATH)/tests/data/fate/vsynth1-mpeg4-qprd.avi -frames:v 5 -flags +bitexact -vf "pp=be/hb/vb/tn/l5/al"
 fate-filter-pp1: CMD = video_filter "pp=fq|4/be/hb/vb/tn/l5/al"
-fate-filter-pp2: CMD = video_filter "qp=x+y,pp=be/h1/v1/lb"
-fate-filter-pp3: CMD = video_filter "qp=x+y,pp=be/ha|128|7/va/li"
+fate-filter-pp2: CMD = video_filter "qp=2*(x+y),pp=be/h1/v1/lb"
+fate-filter-pp3: CMD = video_filter "qp=2*(x+y),pp=be/ha|128|7/va/li"
 fate-filter-pp4: CMD = video_filter "pp=be/ci"
 fate-filter-pp5: CMD = video_filter "pp=md"
 fate-filter-pp6: CMD = video_filter "pp=be/fd"
 
 FATE_FILTER_VSYNTH-$(call ALLYES, QP_FILTER PP_FILTER) += fate-filter-qp
-fate-filter-qp: CMD = video_filter "qp=17,pp=be/hb/vb/tn/l5/al"
+fate-filter-qp: CMD = video_filter "qp=34,pp=be/hb/vb/tn/l5/al"
 
 FATE_FILTER_VSYNTH-$(CONFIG_SELECT_FILTER) += fate-filter-select
 fate-filter-select: CMD = framecrc -flags bitexact -idct simple -i $(SRC) -vf "select=not(eq(mod(n\,2)\,0)+eq(mod(n\,3)\,0))" -frames:v 25 -flags +bitexact