diff mbox series

[FFmpeg-devel,4/7] mpegvideo: use the AVVideoEncParams API for exporting QP tables

Message ID 20200330114526.11086-4-anton@khirnov.net
State Superseded
Headers show
Series [FFmpeg-devel,1/7] libavutil: add API for exporting video frame quantizers | expand

Checks

Context Check Description
andriy/ffmpeg-patchwork fail Make fate failed

Commit Message

Anton Khirnov March 30, 2020, 11:45 a.m. UTC
Do it only when requested with the AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS
flag.

Drop previous code using the long-deprecated AV_FRAME_DATA_QP_TABLE*
API.
---
 libavcodec/h263dec.c         |  6 ++++--
 libavcodec/mpeg12dec.c       |  5 +++--
 libavcodec/mpegpicture.c     |  1 +
 libavcodec/mpegpicture.h     |  1 +
 libavcodec/mpegvideo.c       | 39 ++++++++++++++++++++++++++++--------
 libavcodec/mpegvideo.h       |  8 +++++++-
 libavcodec/rv10.c            |  5 +++--
 libavcodec/rv34.c            |  5 +++--
 libavutil/video_enc_params.h |  2 ++
 9 files changed, 55 insertions(+), 17 deletions(-)

Comments

Michael Niedermayer March 30, 2020, 9:55 p.m. UTC | #1
On Mon, Mar 30, 2020 at 01:45:23PM +0200, Anton Khirnov wrote:
> Do it only when requested with the AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS
> flag.
> 
> Drop previous code using the long-deprecated AV_FRAME_DATA_QP_TABLE*
> API.
> ---
>  libavcodec/h263dec.c         |  6 ++++--
>  libavcodec/mpeg12dec.c       |  5 +++--
>  libavcodec/mpegpicture.c     |  1 +
>  libavcodec/mpegpicture.h     |  1 +
>  libavcodec/mpegvideo.c       | 39 ++++++++++++++++++++++++++++--------
>  libavcodec/mpegvideo.h       |  8 +++++++-
>  libavcodec/rv10.c            |  5 +++--
>  libavcodec/rv34.c            |  5 +++--
>  libavutil/video_enc_params.h |  2 ++
>  9 files changed, 55 insertions(+), 17 deletions(-)

This seems to break fate-filter-pp
TEST    filter-pp
--- ./tests/ref/fate/filter-pp	2020-03-25 02:01:55.010219361 +0100
+++ tests/data/fate/filter-pp	2020-03-30 23:52:46.611521793 +0200
@@ -3,8 +3,8 @@
 #codec_id 0: rawvideo
 #dimensions 0: 352x288
 #sar 0: 1/1
-0,          1,          1,        1,   152064, 0x0af8a873
-0,          2,          2,        1,   152064, 0xaeb99897
-0,          3,          3,        1,   152064, 0x8f3712c8
-0,          4,          4,        1,   152064, 0x5bf6a64c
-0,          5,          5,        1,   152064, 0x262de352
+0,          1,          1,        1,   152064, 0x3588a88c
+0,          2,          2,        1,   152064, 0x91dd9897
+0,          3,          3,        1,   152064, 0xef9b12c2
+0,          4,          4,        1,   152064, 0x83a2a64f
+0,          5,          5,        1,   152064, 0xc4dae34e
Test filter-pp failed. Look at tests/data/fate/filter-pp.err for details.
tests/Makefile:250: recipe for target 'fate-filter-pp' failed
make: *** [fate-filter-pp] Error 1


[...]
Anton Khirnov March 31, 2020, 9:06 a.m. UTC | #2
Quoting Michael Niedermayer (2020-03-30 23:55:25)
> On Mon, Mar 30, 2020 at 01:45:23PM +0200, Anton Khirnov wrote:
> > Do it only when requested with the AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS
> > flag.
> > 
> > Drop previous code using the long-deprecated AV_FRAME_DATA_QP_TABLE*
> > API.
> > ---
> >  libavcodec/h263dec.c         |  6 ++++--
> >  libavcodec/mpeg12dec.c       |  5 +++--
> >  libavcodec/mpegpicture.c     |  1 +
> >  libavcodec/mpegpicture.h     |  1 +
> >  libavcodec/mpegvideo.c       | 39 ++++++++++++++++++++++++++++--------
> >  libavcodec/mpegvideo.h       |  8 +++++++-
> >  libavcodec/rv10.c            |  5 +++--
> >  libavcodec/rv34.c            |  5 +++--
> >  libavutil/video_enc_params.h |  2 ++
> >  9 files changed, 55 insertions(+), 17 deletions(-)
> 
> This seems to break fate-filter-pp

Right, that is to be expected since I am not converting the filters yet.
That will be done in the next iteration if nobody has any further
substantial comments on the API.
diff mbox series

Patch

diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c
index 8ee844e298..a5793ea320 100644
--- a/libavcodec/h263dec.c
+++ b/libavcodec/h263dec.c
@@ -28,6 +28,8 @@ 
 #define UNCHECKED_BITSTREAM_READER 1
 
 #include "libavutil/cpu.h"
+#include "libavutil/video_enc_params.h"
+
 #include "avcodec.h"
 #include "error_resilience.h"
 #include "flv.h"
@@ -693,12 +695,12 @@  frame_end:
         if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
             return ret;
         ff_print_debug_info(s, s->current_picture_ptr, pict);
-        ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
+        ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, AV_VIDEO_ENC_PARAMS_H263);
     } else if (s->last_picture_ptr) {
         if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
             return ret;
         ff_print_debug_info(s, s->last_picture_ptr, pict);
-        ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
+        ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, AV_VIDEO_ENC_PARAMS_H263);
     }
 
     if (s->last_picture_ptr || s->low_delay) {
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 67ce59d451..f35da4ce80 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -32,6 +32,7 @@ 
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
 #include "libavutil/stereo3d.h"
+#include "libavutil/video_enc_params.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
@@ -2070,7 +2071,7 @@  static int slice_end(AVCodecContext *avctx, AVFrame *pict)
             if (ret < 0)
                 return ret;
             ff_print_debug_info(s, s->current_picture_ptr, pict);
-            ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2);
+            ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, AV_VIDEO_ENC_PARAMS_MPEG2);
         } else {
             if (avctx->active_thread_type & FF_THREAD_FRAME)
                 s->picture_number++;
@@ -2081,7 +2082,7 @@  static int slice_end(AVCodecContext *avctx, AVFrame *pict)
                 if (ret < 0)
                     return ret;
                 ff_print_debug_info(s, s->last_picture_ptr, pict);
-                ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2);
+                ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, AV_VIDEO_ENC_PARAMS_MPEG2);
             }
         }
 
diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c
index 5fce25ec6e..6a079e795c 100644
--- a/libavcodec/mpegpicture.c
+++ b/libavcodec/mpegpicture.c
@@ -225,6 +225,7 @@  static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encodin
 
     pic->alloc_mb_width  = mb_width;
     pic->alloc_mb_height = mb_height;
+    pic->alloc_mb_stride = mb_stride;
 
     return 0;
 }
diff --git a/libavcodec/mpegpicture.h b/libavcodec/mpegpicture.h
index 2db3d6733a..4bcd666797 100644
--- a/libavcodec/mpegpicture.h
+++ b/libavcodec/mpegpicture.h
@@ -69,6 +69,7 @@  typedef struct Picture {
 
     int alloc_mb_width;         ///< mb_width used to allocate tables
     int alloc_mb_height;        ///< mb_height used to allocate tables
+    int alloc_mb_stride;        ///< mb_stride used to allocate tables
 
     AVBufferRef *mb_mean_buf;
     uint8_t *mb_mean;           ///< Table for MB luminance
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 22cab2854b..7b43c643f6 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -32,6 +32,8 @@ 
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
 #include "libavutil/motion_vector.h"
+#include "libavutil/video_enc_params.h"
+
 #include "avcodec.h"
 #include "blockdsp.h"
 #include "h264chroma.h"
@@ -1439,16 +1441,37 @@  void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
                          s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
 }
 
-int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
+int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p,
+                           enum AVVideoEncParamsType qp_type)
 {
-    AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
-    int offset = 2*s->mb_stride + 1;
-    if(!ref)
+    AVVideoEncParams *par;
+    unsigned int nb_mb = p->alloc_mb_height * p->alloc_mb_width;
+    unsigned int x, y;
+
+    if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS))
+        return 0;
+
+    par = av_video_enc_params_create_side_data(f, nb_mb);
+    if (!par)
         return AVERROR(ENOMEM);
-    av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
-    ref->size -= offset;
-    ref->data += offset;
-    return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
+
+    par->type = qp_type;
+
+    for (y = 0; y < p->alloc_mb_height; y++)
+        for (x = 0; x < p->alloc_mb_width; x++) {
+            const unsigned int block_idx = y * p->alloc_mb_width + x;
+            const unsigned int     mb_xy = y * p->alloc_mb_stride + x;
+            AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
+
+            b->src_x = x * 16;
+            b->src_y = y * 16;
+            b->w     = 16;
+            b->h     = 16;
+
+            b->delta_qp = p->qscale_table[mb_xy];
+        }
+
+    return 0;
 }
 
 static inline int hpel_motion_lowres(MpegEncContext *s,
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 29e692f245..410d015df2 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -58,6 +58,7 @@ 
 
 #include "libavutil/opt.h"
 #include "libavutil/timecode.h"
+#include "libavutil/video_enc_params.h"
 
 #define MAX_THREADS 32
 
@@ -713,7 +714,12 @@  void ff_mpeg_flush(AVCodecContext *avctx);
 
 void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict);
 
-int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type);
+/**
+ * Export the qscale table if requested through
+ * AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS.
+ */
+int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p,
+                          enum AVVideoEncParamsType qp_type);
 
 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix);
 
diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c
index 3b41d30b92..fd88dede2d 100644
--- a/libavcodec/rv10.c
+++ b/libavcodec/rv10.c
@@ -28,6 +28,7 @@ 
 #include <inttypes.h>
 
 #include "libavutil/imgutils.h"
+#include "libavutil/video_enc_params.h"
 
 #include "avcodec.h"
 #include "error_resilience.h"
@@ -772,12 +773,12 @@  static int rv10_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
             if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
                 return ret;
             ff_print_debug_info(s, s->current_picture_ptr, pict);
-            ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
+            ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, AV_VIDEO_ENC_PARAMS_H263);
         } else if (s->last_picture_ptr) {
             if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
                 return ret;
             ff_print_debug_info(s, s->last_picture_ptr, pict);
-            ff_mpv_export_qp_table(s, pict,s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
+            ff_mpv_export_qp_table(s, pict,s->last_picture_ptr, AV_VIDEO_ENC_PARAMS_H263);
         }
 
         if (s->last_picture_ptr || s->low_delay) {
diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c
index d171e6e1bd..04339d57fc 100644
--- a/libavcodec/rv34.c
+++ b/libavcodec/rv34.c
@@ -26,6 +26,7 @@ 
 
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/video_enc_params.h"
 
 #include "avcodec.h"
 #include "error_resilience.h"
@@ -1617,13 +1618,13 @@  static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
         if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
             return ret;
         ff_print_debug_info(s, s->current_picture_ptr, pict);
-        ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
+        ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, AV_VIDEO_ENC_PARAMS_H263);
         got_picture = 1;
     } else if (s->last_picture_ptr) {
         if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
             return ret;
         ff_print_debug_info(s, s->last_picture_ptr, pict);
-        ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
+        ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, AV_VIDEO_ENC_PARAMS_H263);
         got_picture = 1;
     }
 
diff --git a/libavutil/video_enc_params.h b/libavutil/video_enc_params.h
index 8f97bc7127..c1afcb3a63 100644
--- a/libavutil/video_enc_params.h
+++ b/libavutil/video_enc_params.h
@@ -27,6 +27,8 @@ 
 
 enum AVVideoEncParamsType {
     AV_VIDEO_ENC_PARAMS_NONE = -1,
+    AV_VIDEO_ENC_PARAMS_MPEG2,
+    AV_VIDEO_ENC_PARAMS_H263,
 };
 
 /**