[FFmpeg-cvslog] avcodec/mpeg12dec: Don't use MPVContext.block

Andreas Rheinhardt git at videolan.org
Thu Jul 3 21:56:11 EEST 2025


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Thu Jun 19 22:28:47 2025 +0200| [f1c56c08ed3b54429d9cc52cea03297fe39466fa] | committer: Andreas Rheinhardt

avcodec/mpeg12dec: Don't use MPVContext.block

Instead add the necessary blocks directly into Mpeg12SliceContext.
This allows to completely remove MPVContext.block.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=f1c56c08ed3b54429d9cc52cea03297fe39466fa
---

 libavcodec/mpeg12dec.c     | 40 +++++++++++++++++++++-------------------
 libavcodec/mpeg4videodec.c |  1 -
 libavcodec/mpegvideo.c     | 23 ++---------------------
 libavcodec/mpegvideo.h     |  2 --
 4 files changed, 23 insertions(+), 43 deletions(-)

diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 8ca26c0d37..db6f9d66a5 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -73,6 +73,8 @@ enum Mpeg2ClosedCaptionsFormat {
 typedef struct Mpeg12SliceContext {
     MPVContext c;
     GetBitContext gb;
+
+    DECLARE_ALIGNED_32(int16_t, block)[12][64];
 } Mpeg12SliceContext;
 
 typedef struct Mpeg1Context {
@@ -472,10 +474,10 @@ static int mpeg_decode_mb(Mpeg12SliceContext *const s)
     ff_tlog(s->c.avctx, "mb_type=%x\n", mb_type);
 //    motion_type = 0; /* avoid warning */
     if (IS_INTRA(mb_type)) {
-        s->c.bdsp.clear_blocks(s->c.block[0]);
+        s->c.bdsp.clear_blocks(s->block[0]);
 
         if (!s->c.chroma_y_shift)
-            s->c.bdsp.clear_blocks(s->c.block[6]);
+            s->c.bdsp.clear_blocks(s->block[6]);
 
         /* compute DCT type */
         // FIXME: add an interlaced_dct coded var?
@@ -509,14 +511,14 @@ static int mpeg_decode_mb(Mpeg12SliceContext *const s)
 
         if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO) {
             for (i = 0; i < mb_block_count; i++)
-                if ((ret = mpeg2_decode_block_intra(s, s->c.block[i], i)) < 0)
+                if ((ret = mpeg2_decode_block_intra(s, s->block[i], i)) < 0)
                     return ret;
         } else {
             for (i = 0; i < 6; i++) {
                 ret = ff_mpeg1_decode_block_intra(&s->gb,
                                                   s->c.intra_matrix,
                                                   s->c.intra_scantable.permutated,
-                                                  s->c.last_dc, s->c.block[i],
+                                                  s->c.last_dc, s->block[i],
                                                   i, s->c.qscale);
                 if (ret < 0) {
                     av_log(s->c.avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n",
@@ -714,13 +716,13 @@ static int mpeg_decode_mb(Mpeg12SliceContext *const s)
         s->c.mb_intra = 0;
         s->c.last_dc[0] = s->c.last_dc[1] = s->c.last_dc[2] = 128 << s->c.intra_dc_precision;
         if (HAS_CBP(mb_type)) {
-            s->c.bdsp.clear_blocks(s->c.block[0]);
+            s->c.bdsp.clear_blocks(s->block[0]);
 
             cbp = get_vlc2(&s->gb, ff_mb_pat_vlc, MB_PAT_VLC_BITS, 1);
             if (mb_block_count > 6) {
                 cbp *= 1 << mb_block_count - 6;
                 cbp |= get_bits(&s->gb, mb_block_count - 6);
-                s->c.bdsp.clear_blocks(s->c.block[6]);
+                s->c.bdsp.clear_blocks(s->block[6]);
             }
             if (cbp <= 0) {
                 av_log(s->c.avctx, AV_LOG_ERROR,
@@ -733,7 +735,7 @@ static int mpeg_decode_mb(Mpeg12SliceContext *const s)
 
                 for (i = 0; i < mb_block_count; i++) {
                     if (cbp & (1 << 11)) {
-                        if ((ret = mpeg2_decode_block_non_intra(s, s->c.block[i], i)) < 0)
+                        if ((ret = mpeg2_decode_block_non_intra(s, s->block[i], i)) < 0)
                             return ret;
                     } else {
                         s->c.block_last_index[i] = -1;
@@ -743,7 +745,7 @@ static int mpeg_decode_mb(Mpeg12SliceContext *const s)
             } else {
                 for (i = 0; i < 6; i++) {
                     if (cbp & 32) {
-                        if ((ret = mpeg1_decode_block_inter(s, s->c.block[i], i)) < 0)
+                        if ((ret = mpeg1_decode_block_inter(s, s->block[i], i)) < 0)
                             return ret;
                     } else {
                         s->c.block_last_index[i] = -1;
@@ -1498,7 +1500,7 @@ static int mpeg_decode_slice(Mpeg12SliceContext *const s, int mb_y,
         s->c.dest[1] +=(16 >> lowres) >> s->c.chroma_x_shift;
         s->c.dest[2] +=(16 >> lowres) >> s->c.chroma_x_shift;
 
-        ff_mpv_reconstruct_mb(&s->c, s->c.block);
+        ff_mpv_reconstruct_mb(&s->c, s->block);
 
         if (++s->c.mb_x >= s->c.mb_width) {
             const int mb_size = 16 >> s->c.avctx->lowres;
@@ -2755,7 +2757,6 @@ typedef struct IPUContext {
     Mpeg12SliceContext m;
 
     int flags;
-    DECLARE_ALIGNED(32, int16_t, block)[6][64];
 } IPUContext;
 
 static int ipu_decode_frame(AVCodecContext *avctx, AVFrame *frame,
@@ -2764,6 +2765,7 @@ static int ipu_decode_frame(AVCodecContext *avctx, AVFrame *frame,
     IPUContext *s = avctx->priv_data;
     MPVContext *const m = &s->m.c;
     GetBitContext *const gb = &s->m.gb;
+    int16_t (*const block)[64] = s->m.block;
     int ret;
 
     // Check for minimal intra MB size (considering mb header, luma & chroma dc VLC, ac EOB VLC)
@@ -2813,17 +2815,17 @@ static int ipu_decode_frame(AVCodecContext *avctx, AVFrame *frame,
             if (intraquant)
                 m->qscale = mpeg_get_qscale(gb, m->q_scale_type);
 
-            memset(s->block, 0, sizeof(s->block));
+            memset(block, 0, 6 * sizeof(*block));
 
             for (int n = 0; n < 6; n++) {
                 if (s->flags & 0x80) {
                     ret = ff_mpeg1_decode_block_intra(gb,
                                                       m->intra_matrix,
                                                       m->intra_scantable.permutated,
-                                                      m->last_dc, s->block[n],
+                                                      m->last_dc, block[n],
                                                       n, m->qscale);
                 } else {
-                    ret = mpeg2_decode_block_intra(&s->m, s->block[n], n);
+                    ret = mpeg2_decode_block_intra(&s->m, block[n], n);
                 }
 
                 if (ret < 0)
@@ -2831,17 +2833,17 @@ static int ipu_decode_frame(AVCodecContext *avctx, AVFrame *frame,
             }
 
             m->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
-                             frame->linesize[0], s->block[0]);
+                             frame->linesize[0], block[0]);
             m->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
-                             frame->linesize[0], s->block[1]);
+                             frame->linesize[0], block[1]);
             m->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
-                             frame->linesize[0], s->block[2]);
+                             frame->linesize[0], block[2]);
             m->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
-                             frame->linesize[0], s->block[3]);
+                             frame->linesize[0], block[3]);
             m->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
-                             frame->linesize[1], s->block[4]);
+                             frame->linesize[1], block[4]);
             m->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
-                             frame->linesize[2], s->block[5]);
+                             frame->linesize[2], block[5]);
         }
     }
 
diff --git a/libavcodec/mpeg4videodec.c b/libavcodec/mpeg4videodec.c
index c6ed9d31bb..ee78e7119f 100644
--- a/libavcodec/mpeg4videodec.c
+++ b/libavcodec/mpeg4videodec.c
@@ -3830,7 +3830,6 @@ static av_cold void clear_context(MpegEncContext *s)
 
     memset(s->thread_context, 0, sizeof(s->thread_context));
 
-    s->block = NULL;
     s->ac_val_base = NULL;
     s->ac_val = NULL;
     memset(&s->sc, 0, sizeof(s->sc));
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 7ef0dacc80..9c3589752c 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -115,31 +115,15 @@ av_cold void ff_mpv_idct_init(MpegEncContext *s)
                          s->idsp.idct_permutation);
 }
 
-static av_cold int init_duplicate_context(MpegEncContext *s)
-{
-    if (!s->encoding) {
-        s->block = av_mallocz(12 * sizeof(*s->block));
-        if (!s->block)
-            return AVERROR(ENOMEM);
-    }
-
-    return 0;
-}
-
 av_cold int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
 {
-    int nb_slices = s->slice_context_count, ret;
+    const int nb_slices = s->slice_context_count;
     const size_t slice_size = s->slice_ctx_size;
 
-    /* We initialize the copies before the original so that
-     * fields allocated in init_duplicate_context are NULL after
-     * copying. This prevents double-frees upon allocation error. */
     for (int i = 1; i < nb_slices; i++) {
         s->thread_context[i] = av_memdup(s, slice_size);
         if (!s->thread_context[i])
             return AVERROR(ENOMEM);
-        if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
-            return ret;
         s->thread_context[i]->start_mb_y =
             (s->mb_height * (i    ) + nb_slices / 2) / nb_slices;
         s->thread_context[i]->end_mb_y   =
@@ -148,7 +132,7 @@ av_cold int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
     s->start_mb_y = 0;
     s->end_mb_y   = nb_slices > 1 ? (s->mb_height + nb_slices / 2) / nb_slices
                                   : s->mb_height;
-    return init_duplicate_context(s);
+    return 0;
 }
 
 static av_cold void free_duplicate_context(MpegEncContext *s)
@@ -160,8 +144,6 @@ static av_cold void free_duplicate_context(MpegEncContext *s)
     av_freep(&s->sc.scratchpad_buf);
     s->sc.obmc_scratchpad = NULL;
     s->sc.linesize = 0;
-
-    av_freep(&s->block);
 }
 
 static av_cold void free_duplicate_contexts(MpegEncContext *s)
@@ -177,7 +159,6 @@ int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src)
 {
 #define COPY(M)              \
     M(ScratchpadContext, sc) \
-    M(void*, block)          \
     M(int, start_mb_y)       \
     M(int, end_mb_y)         \
     M(int16_t*, dc_val)      \
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 4b3c7f894d..4b215bf65f 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -302,8 +302,6 @@ typedef struct MpegEncContext {
     int interlaced_dct;
     int first_field;         ///< is 1 for the first field of a field picture 0 otherwise
 
-    int16_t (*block)[64];
-
 #define SLICE_OK         0
 #define SLICE_ERROR     -1
 #define SLICE_END       -2 ///<end marker found



More information about the ffmpeg-cvslog mailing list