diff mbox series

[FFmpeg-devel,3/7] lavfi/vf_pp: convert to the video_enc_params API

Message ID 20201002180331.20416-3-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,1/7] mpegvideo: use the AVVideoEncParams API for exporting QP tables | expand

Checks

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

Commit Message

Anton Khirnov Oct. 2, 2020, 6:03 p.m. UTC
---
 libavfilter/Makefile        |  2 +-
 libavfilter/qp_table.c      | 66 +++++++++++++++++++++++++++++++++++++
 libavfilter/qp_table.h      | 32 ++++++++++++++++++
 libavfilter/vf_pp.c         | 19 ++++++++---
 tests/fate/filter-video.mak |  6 ++--
 5 files changed, 117 insertions(+), 8 deletions(-)
 create mode 100644 libavfilter/qp_table.c
 create mode 100644 libavfilter/qp_table.h

Comments

Michael Niedermayer Dec. 4, 2020, 5:03 p.m. UTC | #1
On Fri, Oct 02, 2020 at 08:03:27PM +0200, Anton Khirnov wrote:
> ---
>  libavfilter/Makefile        |  2 +-
>  libavfilter/qp_table.c      | 66 +++++++++++++++++++++++++++++++++++++
>  libavfilter/qp_table.h      | 32 ++++++++++++++++++
>  libavfilter/vf_pp.c         | 19 ++++++++---
>  tests/fate/filter-video.mak |  6 ++--

I think the qp_table changes should be in a seperate patch


[...]
> +int ff_qp_table_extract(AVFrame *frame, int8_t **table, int *table_w, int *table_h)
> +{
> +    AVFrameSideData *sd;
> +    AVVideoEncParams *par;
> +    unsigned int mb_h = (frame->height + 15) / 16;
> +    unsigned int mb_w = (frame->width + 15) / 16;
> +    unsigned int nb_mb = mb_h * mb_w;
> +    unsigned int block_idx;
> +
> +    *table = NULL;
> +
> +    sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS);
> +    if (!sd)
> +        return 0;

> +    par = (AVVideoEncParams*)sd->data;
> +    if (par->type != AV_VIDEO_ENC_PARAMS_MPEG2 ||
> +        (par->nb_blocks != 0 && par->nb_blocks != nb_mb))
> +        return 0;

This should be an error of some kind (patchwelcome or something else indicating lack of support)


> +
> +    *table = av_malloc(nb_mb);
> +    if (!*table)
> +        return AVERROR(ENOMEM);
> +    if (table_w)
> +        *table_w = mb_w;
> +    if (table_h)
> +        *table_h = mb_h;
> +
> +    if (par->nb_blocks == 0) {
> +        memset(*table, par->qp, nb_mb);
> +        return 0;
> +    }
> +
> +    for (block_idx = 0; block_idx < nb_mb; block_idx++) {
> +        AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
> +        (*table)[block_idx] = par->qp + b->delta_qp;
> +    }
> +
> +    return 0;
> +}
> +
> diff --git a/libavfilter/qp_table.h b/libavfilter/qp_table.h
> new file mode 100644
> index 0000000000..5a9ec889b6
> --- /dev/null
> +++ b/libavfilter/qp_table.h
> @@ -0,0 +1,32 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License along
> + * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + */
> +
> +#ifndef AVFILTER_QP_TABLE_H
> +#define AVFILTER_QP_TABLE_H
> +
> +#include <stdint.h>
> +
> +#include "libavutil/frame.h"
> +

> +/**
> + * Extract a "classic" libpostproc-style QP table from AVVideoEncParams side
> + * data.
> + */

This description is inadequate i think.
Few except me and you will understand what that is
I think the description should be sufficient to use the function without having
to read its implementation

thx

[...]
diff mbox series

Patch

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index e6d3c283da..d20f2937b6 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -344,7 +344,7 @@  OBJS-$(CONFIG_PHASE_FILTER)                  += vf_phase.o
 OBJS-$(CONFIG_PHOTOSENSITIVITY_FILTER)       += vf_photosensitivity.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 OBJS-$(CONFIG_PIXSCOPE_FILTER)               += vf_datascope.o
-OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
+OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o qp_table.o
 OBJS-$(CONFIG_PP7_FILTER)                    += vf_pp7.o
 OBJS-$(CONFIG_PREMULTIPLY_FILTER)            += vf_premultiply.o framesync.o
 OBJS-$(CONFIG_PREWITT_FILTER)                += vf_convolution.o
diff --git a/libavfilter/qp_table.c b/libavfilter/qp_table.c
new file mode 100644
index 0000000000..df75c6371d
--- /dev/null
+++ b/libavfilter/qp_table.c
@@ -0,0 +1,66 @@ 
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdint.h>
+
+#include "libavutil/frame.h"
+#include "libavutil/mem.h"
+#include "libavutil/video_enc_params.h"
+
+#include "qp_table.h"
+
+int ff_qp_table_extract(AVFrame *frame, int8_t **table, int *table_w, int *table_h)
+{
+    AVFrameSideData *sd;
+    AVVideoEncParams *par;
+    unsigned int mb_h = (frame->height + 15) / 16;
+    unsigned int mb_w = (frame->width + 15) / 16;
+    unsigned int nb_mb = mb_h * mb_w;
+    unsigned int block_idx;
+
+    *table = NULL;
+
+    sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS);
+    if (!sd)
+        return 0;
+    par = (AVVideoEncParams*)sd->data;
+    if (par->type != AV_VIDEO_ENC_PARAMS_MPEG2 ||
+        (par->nb_blocks != 0 && par->nb_blocks != nb_mb))
+        return 0;
+
+    *table = av_malloc(nb_mb);
+    if (!*table)
+        return AVERROR(ENOMEM);
+    if (table_w)
+        *table_w = mb_w;
+    if (table_h)
+        *table_h = mb_h;
+
+    if (par->nb_blocks == 0) {
+        memset(*table, par->qp, nb_mb);
+        return 0;
+    }
+
+    for (block_idx = 0; block_idx < nb_mb; block_idx++) {
+        AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
+        (*table)[block_idx] = par->qp + b->delta_qp;
+    }
+
+    return 0;
+}
+
diff --git a/libavfilter/qp_table.h b/libavfilter/qp_table.h
new file mode 100644
index 0000000000..5a9ec889b6
--- /dev/null
+++ b/libavfilter/qp_table.h
@@ -0,0 +1,32 @@ 
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef AVFILTER_QP_TABLE_H
+#define AVFILTER_QP_TABLE_H
+
+#include <stdint.h>
+
+#include "libavutil/frame.h"
+
+/**
+ * Extract a "classic" libpostproc-style QP table from AVVideoEncParams side
+ * data.
+ */
+int ff_qp_table_extract(AVFrame *frame, int8_t **table, int *table_w, int *table_h);
+
+#endif // AVFILTER_QP_TABLE_H
diff --git a/libavfilter/vf_pp.c b/libavfilter/vf_pp.c
index 524ef1bb0a..4471925f7b 100644
--- a/libavfilter/vf_pp.c
+++ b/libavfilter/vf_pp.c
@@ -26,7 +26,9 @@ 
 
 #include "libavutil/avassert.h"
 #include "libavutil/opt.h"
+
 #include "internal.h"
+#include "qp_table.h"
 
 #include "libpostproc/postprocess.h"
 
@@ -126,8 +128,9 @@  static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
     const int aligned_w = FFALIGN(outlink->w, 8);
     const int aligned_h = FFALIGN(outlink->h, 8);
     AVFrame *outbuf;
-    int qstride, qp_type;
-    int8_t *qp_table ;
+    int qstride = 0;
+    int8_t *qp_table = NULL;
+    int ret;
 
     outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
     if (!outbuf) {
@@ -137,7 +140,14 @@  static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
     av_frame_copy_props(outbuf, inbuf);
     outbuf->width  = inbuf->width;
     outbuf->height = inbuf->height;
-    qp_table = av_frame_get_qp_table(inbuf, &qstride, &qp_type);
+
+    ret = ff_qp_table_extract(inbuf, &qp_table, &qstride, NULL);
+    if (ret < 0) {
+        av_frame_free(&inbuf);
+        av_frame_free(&outbuf);
+        av_freep(&qp_table);
+        return ret;
+    }
 
     pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
                    outbuf->data,                 outbuf->linesize,
@@ -146,9 +156,10 @@  static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
                    qstride,
                    pp->modes[pp->mode_id],
                    pp->pp_ctx,
-                   outbuf->pict_type | (qp_type ? PP_PICT_TYPE_QP2 : 0));
+                   outbuf->pict_type | (qp_table ? PP_PICT_TYPE_QP2 : 0));
 
     av_frame_free(&inbuf);
+    av_freep(&qp_table);
     return ff_filter_frame(outlink, outbuf);
 }
 
diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index b611dccf24..98df744f16 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -558,11 +558,11 @@  fate-filter-idet: CMD = framecrc -flags bitexact -idct simple -i $(SRC) -vf idet
 FATE_FILTER_VSYNTH-$(CONFIG_PAD_FILTER) += fate-filter-pad
 fate-filter-pad: CMD = video_filter "pad=iw*1.5:ih*1.5:iw*0.3:ih*0.2"
 
-#FATE_FILTER_PP = fate-filter-pp fate-filter-pp1 fate-filter-pp2 fate-filter-pp3 fate-filter-pp4 fate-filter-pp5 fate-filter-pp6
+FATE_FILTER_PP = fate-filter-pp fate-filter-pp1 fate-filter-pp2 fate-filter-pp3 fate-filter-pp4 fate-filter-pp5 fate-filter-pp6
 FATE_FILTER_VSYNTH-$(CONFIG_PP_FILTER) += $(FATE_FILTER_PP)
 $(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-pp:  CMD = framecrc -flags bitexact -export_side_data venc_params -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=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"
@@ -582,7 +582,7 @@  FATE_FILTER_VSYNTH-$(CONFIG_CODECVIEW_FILTER) += fate-filter-codecview
 fate-filter-codecview: fate-vsynth1-mpeg4-qprd
 fate-filter-codecview: CMD = framecrc -flags bitexact -idct simple -flags2 +export_mvs -i $(TARGET_PATH)/tests/data/fate/vsynth1-mpeg4-qprd.avi -frames:v 5 -flags +bitexact -vf codecview=mv=pf+bf+bb
 
-#FATE_FILTER_VSYNTH-$(call ALLYES, QP_FILTER PP_FILTER) += fate-filter-qp
+FATE_FILTER_VSYNTH-$(call ALLYES, QP_FILTER PP_FILTER) += fate-filter-qp
 fate-filter-qp: CMD = video_filter "qp=34,pp=be/hb/vb/tn/l5/al"
 
 FATE_FILTER_VSYNTH-$(CONFIG_SELECT_FILTER) += fate-filter-select