[FFmpeg-devel] [PATCH 04/15] avcodec/mpegvideo_dec, mpeg12dec: Move MPEG1/2 code to mpeg12dec.c

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Fri Apr 5 15:41:27 EEST 2024


Up until now, ff_mpv_frame_start() offsets the data of the current
picture and doubles the linesizes of all pictures if the current
picture is field-based so that data and linesize allow to address
the current field only.

This is done based upon the current picture_structure value.
Only two mpegvideo-based decoders ever set this field: mpeg1/2
and VC-1; but the latter only does it after ff_mpv_frame_start()
(when using hardware-acceleration and in order to signal it to
the DXVA2 hwaccel) in which case no offset is applied in
ff_mpv_frame_start(). So only one decoder actually wants this
offset*; therefore move the code performing it to mpeg12dec.c.

*: VC-1 doubles linesize when using field_mode (not only the picture's
linesize, but also uvlinesize and linesize), yet it does not offset
anything. This is further proof that this should not be performed
generically.

Also move MPEG-1/2 specific setting of the top-field-first flag.
(The change here implies that the AVFrame in current_picture
may have different top-field-first flags than the AVFrame
from current_picture_ptr, but this doesn't matter as only
the latter's are used.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/mpeg12dec.c     | 15 +++++++++++++++
 libavcodec/mpegvideo_dec.c | 19 -------------------
 2 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 4ad1eb6572..fb8bba3287 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -1299,6 +1299,21 @@ static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
         if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
             return ret;
 
+        if (s->picture_structure != PICT_FRAME) {
+            s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST *
+                ((s->picture_structure == PICT_TOP_FIELD) == s->first_field);
+
+            for (int i = 0; i < 4; i++) {
+                if (s->picture_structure == PICT_BOTTOM_FIELD) {
+                    s->current_picture.f->data[i] = FF_PTR_ADD(s->current_picture.f->data[i],
+                                                               s->current_picture.f->linesize[i]);
+                }
+                s->current_picture.f->linesize[i] *= 2;
+                s->last_picture.f->linesize[i]    *= 2;
+                s->next_picture.f->linesize[i]    *= 2;
+            }
+        }
+
         ff_mpeg_er_frame_start(s);
 
         /* first check if we must repeat the frame */
diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c
index 88facfc39d..1ced9a52ed 100644
--- a/libavcodec/mpegvideo_dec.c
+++ b/libavcodec/mpegvideo_dec.c
@@ -348,14 +348,7 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
         return -1;
 
     s->current_picture_ptr = pic;
-    // FIXME use only the vars from current_pic
     s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * !!s->top_field_first;
-    if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
-        s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
-        if (s->picture_structure != PICT_FRAME)
-            s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST *
-                ((s->picture_structure == PICT_TOP_FIELD) == s->first_field);
-    }
     s->current_picture_ptr->f->flags |= AV_FRAME_FLAG_INTERLACED * (!s->progressive_frame &&
                                                                     !s->progressive_sequence);
     s->current_picture_ptr->field_picture      =  s->picture_structure != PICT_FRAME;
@@ -454,18 +447,6 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
     av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
                                                  s->last_picture_ptr->f->buf[0]));
 
-    if (s->picture_structure != PICT_FRAME) {
-        for (int i = 0; i < 4; i++) {
-            if (s->picture_structure == PICT_BOTTOM_FIELD) {
-                s->current_picture.f->data[i] = FF_PTR_ADD(s->current_picture.f->data[i],
-                                                           s->current_picture.f->linesize[i]);
-            }
-            s->current_picture.f->linesize[i] *= 2;
-            s->last_picture.f->linesize[i]    *= 2;
-            s->next_picture.f->linesize[i]    *= 2;
-        }
-    }
-
     /* set dequantizer, we can't do it during init as
      * it might change for MPEG-4 and we can't do it in the header
      * decode as init is not called for MPEG-4 there yet */
-- 
2.40.1



More information about the ffmpeg-devel mailing list