[FFmpeg-devel] [PATCH 16/57] avcodec/mpegvideo: Redo aligning mb_height for VC-1

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue Apr 30 00:13:57 EEST 2024


VC-1 can switch from between being progressive and interlaced
on a per-frame basis. In the latter case, the number of macroblocks
is aligned to two (or equivalently, the height to 32); therefore
certain buffers are allocated for the bigger mb_height
(see 950fb8acb42f4dab9b1638721992991c0584dbf5 and
017e234c204f8ffb5f85a073231247881be1ac6f).

This commit changes how this is done: Aligning these buffers is
restricted to VC-1 and it is done directly by aligning
mb_height (but not MpegEncContext.mb_height) instead of
adding something in an ad-hoc manner.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/mpegvideo.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 2ef69a5224..ce1edca95d 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -364,14 +364,8 @@ av_cold void ff_mpv_idct_init(MpegEncContext *s)
 
 static int init_duplicate_context(MpegEncContext *s)
 {
-    int y_size = s->b8_stride * (2 * s->mb_height + 1);
-    int c_size = s->mb_stride * (s->mb_height + 1);
-    int yc_size = y_size + 2 * c_size;
     int i;
 
-    if (s->mb_height & 1)
-        yc_size += 2*s->b8_stride + 2*s->mb_stride;
-
     if (s->encoding) {
         s->me.map = av_mallocz(2 * ME_MAP_SIZE * sizeof(*s->me.map));
         if (!s->me.map)
@@ -397,6 +391,11 @@ static int init_duplicate_context(MpegEncContext *s)
     }
 
     if (s->out_format == FMT_H263) {
+        int mb_height = s->msmpeg4_version == 6 /* VC-1 like */ ?
+                            FFALIGN(s->mb_height, 2) : s->mb_height;
+        int y_size = s->b8_stride * (2 * mb_height + 1);
+        int c_size = s->mb_stride * (mb_height + 1);
+        int yc_size = y_size + 2 * c_size;
         /* ac values */
         if (!FF_ALLOCZ_TYPED_ARRAY(s->ac_val_base,  yc_size))
             return AVERROR(ENOMEM);
@@ -538,17 +537,24 @@ void ff_mpv_common_defaults(MpegEncContext *s)
 int ff_mpv_init_context_frame(MpegEncContext *s)
 {
     int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
+    int mb_height;
 
     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
         s->mb_height = (s->height + 31) / 32 * 2;
     else
         s->mb_height = (s->height + 15) / 16;
 
+    /* VC-1 can change from being progressive to interlaced on a per-frame
+     * basis. We therefore allocate certain buffers so big that they work
+     * in both instances. */
+    mb_height = s->msmpeg4_version == 6 /* VC-1 like*/ ?
+                    FFALIGN(s->mb_height, 2) : s->mb_height;
+
     s->mb_width   = (s->width + 15) / 16;
     s->mb_stride  = s->mb_width + 1;
     s->b8_stride  = s->mb_width * 2 + 1;
-    mb_array_size = s->mb_height * s->mb_stride;
-    mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
+    mb_array_size = mb_height * s->mb_stride;
+    mv_table_size = (mb_height + 2) * s->mb_stride + 1;
 
     /* set default edge pos, will be overridden
      * in decode_header if needed */
@@ -564,13 +570,10 @@ int ff_mpv_init_context_frame(MpegEncContext *s)
     s->block_wrap[4] =
     s->block_wrap[5] = s->mb_stride;
 
-    y_size  = s->b8_stride * (2 * s->mb_height + 1);
-    c_size  = s->mb_stride * (s->mb_height + 1);
+    y_size  = s->b8_stride * (2 * mb_height + 1);
+    c_size  = s->mb_stride * (mb_height + 1);
     yc_size = y_size + 2   * c_size;
 
-    if (s->mb_height & 1)
-        yc_size += 2*s->b8_stride + 2*s->mb_stride;
-
     if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_index2xy, s->mb_num + 1))
         return AVERROR(ENOMEM);
     for (y = 0; y < s->mb_height; y++)
@@ -602,7 +605,7 @@ int ff_mpv_init_context_frame(MpegEncContext *s)
     }
 
     if (s->msmpeg4_version >= 3) {
-        s->coded_block_base = av_mallocz(y_size + (s->mb_height&1)*2*s->b8_stride);
+        s->coded_block_base = av_mallocz(y_size);
         if (!s->coded_block_base)
             return AVERROR(ENOMEM);
         s->coded_block = s->coded_block_base + s->b8_stride + 1;
-- 
2.40.1



More information about the ffmpeg-devel mailing list