[FFmpeg-devel] [PATCH v2 69/69] avcodec/mpegvideo: Move picture array to MPVMainContext

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue Feb 1 15:07:06 EET 2022


Also avoid an allocation while at it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/h263dec.c       |  7 ++---
 libavcodec/mpegvideo.c     | 14 +++-------
 libavcodec/mpegvideo.h     |  5 +++-
 libavcodec/mpegvideo_dec.c | 54 +++++++++++++++++++-------------------
 libavcodec/mpegvideo_enc.c | 12 ++++-----
 5 files changed, 45 insertions(+), 47 deletions(-)

diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c
index b502878a4a..dc09d2e15c 100644
--- a/libavcodec/h263dec.c
+++ b/libavcodec/h263dec.c
@@ -431,7 +431,8 @@ int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
                          AVPacket *avpkt)
 {
     MPVMainDecContext *const m = avctx->priv_data;
-    MPVDecContext *const s = &m->s;
+    MPVMainContext *const m2 = m;
+    MPVDecContext *const s = &m2->s;
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
     int ret;
@@ -547,10 +548,10 @@ retry:
     }
 
     if (!s->current_picture_ptr || s->current_picture_ptr->f->data[0]) {
-        int i = ff_find_unused_picture(s->avctx, s->picture, 0);
+        int i = ff_find_unused_picture(s->avctx, m2->picture, 0);
         if (i < 0)
             return i;
-        s->current_picture_ptr = &s->picture[i];
+        s->current_picture_ptr = &m2->picture[i];
     }
 
     avctx->has_b_frames = !s->low_delay;
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index c8eaafcc1c..7314f1b39e 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -645,7 +645,6 @@ static void clear_context(MPVMainContext *m)
 
     s->bitstream_buffer = NULL;
     s->allocated_bitstream_buffer_size = 0;
-    s->picture          = NULL;
     for (int i = 0; i < 2; i++)
         for (int j = 0; j < 2; j++)
             s->p_field_mv_table[i][j] = NULL;
@@ -713,11 +712,9 @@ av_cold int ff_mpv_common_init(MPVMainContext *m)
     if (ret)
         return ret;
 
-    if (!FF_ALLOCZ_TYPED_ARRAY(s->picture, MAX_PICTURE_COUNT))
-        return AVERROR(ENOMEM);
     for (i = 0; i < MAX_PICTURE_COUNT; i++) {
-        s->picture[i].f = av_frame_alloc();
-        if (!s->picture[i].f)
+        m->picture[i].f = av_frame_alloc();
+        if (!m->picture[i].f)
             goto fail_nomem;
     }
 
@@ -801,11 +798,8 @@ void ff_mpv_common_end(MPVMainContext *m)
     if (!s->avctx)
         return;
 
-    if (s->picture) {
-        for (int i = 0; i < MAX_PICTURE_COUNT; i++)
-            ff_mpv_picture_free(s->avctx, &s->picture[i]);
-    }
-    av_freep(&s->picture);
+    for (int i = 0; i < MAX_PICTURE_COUNT; i++)
+        ff_mpv_picture_free(s->avctx, &m->picture[i]);
     ff_mpv_picture_free(s->avctx, &s->last_picture);
     ff_mpv_picture_free(s->avctx, &s->current_picture);
     ff_mpv_picture_free(s->avctx, &s->next_picture);
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index af1d9af2bd..ba081a9087 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -110,7 +110,6 @@ typedef struct MPVContext {
     int mb_num;                ///< number of MBs of a picture
     ptrdiff_t linesize;        ///< line size, in bytes, may be different from width
     ptrdiff_t uvlinesize;      ///< line size, for chroma in bytes, may be different from width
-    Picture *picture;          ///< main picture buffer
 
     /** bit output */
     PutBitContext pb;
@@ -498,6 +497,10 @@ typedef struct MPVContext {
 typedef struct MPVMainContext {
     MPVContext s;
 
+    /* The main picture buffer. The pictures therein are usable iff
+     * the context is initialized. */
+    Picture picture[MAX_PICTURE_COUNT];
+
     int context_initialized;
     /* flag to indicate a reinitialization is required, e.g. after
      * a frame size change */
diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c
index 137b47efa7..8f3c401aff 100644
--- a/libavcodec/mpegvideo_dec.c
+++ b/libavcodec/mpegvideo_dec.c
@@ -111,12 +111,11 @@ int ff_mpeg_update_thread_context(AVCodecContext *dst,
     m->coded_picture_number = m1->coded_picture_number;
     s->picture_number       = s1->picture_number;
 
-    av_assert0(!s->picture || s->picture != s1->picture);
-    if (s->picture)
+    if (m->context_initialized)
         for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
-            ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
-            if (s1->picture && s1->picture[i].f->buf[0] &&
-                (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
+            ff_mpeg_unref_picture(s->avctx, &m->picture[i]);
+            if (m1->context_initialized && m1->picture[i].f->buf[0] &&
+                (ret = ff_mpeg_ref_picture(s->avctx, &m->picture[i], &m1->picture[i])) < 0)
                 return ret;
         }
 
@@ -138,9 +137,9 @@ do {\
 #define REBASE_PICTURE(pic, new_ctx, old_ctx)                                 \
     ((pic) ? &(new_ctx)->picture[(pic) - (old_ctx)->picture] : NULL)
 
-    s->last_picture_ptr    = REBASE_PICTURE(s1->last_picture_ptr,    s, s1);
-    s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
-    s->next_picture_ptr    = REBASE_PICTURE(s1->next_picture_ptr,    s, s1);
+    s->last_picture_ptr    = REBASE_PICTURE(s1->last_picture_ptr,    m, m1);
+    s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, m, m1);
+    s->next_picture_ptr    = REBASE_PICTURE(s1->next_picture_ptr,    m, m1);
 
     // Error/bug resilience
     s->workaround_bugs      = s1->workaround_bugs;
@@ -208,9 +207,8 @@ int ff_mpv_common_frame_size_change(MPVMainDecContext *m)
 
     ff_mpv_free_context_frame(m2);
 
-    if (s->picture)
-        for (int i = 0; i < MAX_PICTURE_COUNT; i++)
-            s->picture[i].needs_realloc = 1;
+    for (int i = 0; i < MAX_PICTURE_COUNT; i++)
+        m2->picture[i].needs_realloc = 1;
 
     s->last_picture_ptr         =
     s->next_picture_ptr         =
@@ -283,7 +281,8 @@ static void gray_frame(AVFrame *frame)
  */
 int ff_mpv_frame_start(MPVMainDecContext *m, AVCodecContext *avctx)
 {
-    MPVDecContext *const s = &m->s;
+    MPVMainContext *const m2 = m;
+    MPVDecContext *const s = &m2->s;
     Picture *pic;
     int idx, ret;
 
@@ -304,10 +303,10 @@ int ff_mpv_frame_start(MPVMainDecContext *m, AVCodecContext *avctx)
     /* release forgotten pictures */
     /* if (MPEG-124 / H.263) */
     for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
-        if (&s->picture[i] != s->last_picture_ptr &&
-            &s->picture[i] != s->next_picture_ptr &&
-            s->picture[i].reference && !s->picture[i].needs_realloc) {
-            ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
+        if (&m2->picture[i] != s->last_picture_ptr &&
+            &m2->picture[i] != s->next_picture_ptr &&
+            m2->picture[i].reference && !m2->picture[i].needs_realloc) {
+            ff_mpeg_unref_picture(s->avctx, &m2->picture[i]);
         }
     }
 
@@ -317,8 +316,8 @@ int ff_mpv_frame_start(MPVMainDecContext *m, AVCodecContext *avctx)
 
     /* release non reference frames */
     for (int i = 0; i < MAX_PICTURE_COUNT; i++) {
-        if (!s->picture[i].reference)
-            ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
+        if (!m2->picture[i].reference)
+            ff_mpeg_unref_picture(s->avctx, &m2->picture[i]);
     }
 
     if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
@@ -326,12 +325,12 @@ int ff_mpv_frame_start(MPVMainDecContext *m, AVCodecContext *avctx)
         // (maybe it was set before reading the header)
         pic = s->current_picture_ptr;
     } else {
-        idx = ff_find_unused_picture(s->avctx, s->picture, 0);
+        idx = ff_find_unused_picture(s->avctx, m2->picture, 0);
         if (idx < 0) {
             av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
             return idx;
         }
-        pic = &s->picture[idx];
+        pic = &m2->picture[idx];
     }
 
     pic->reference = 0;
@@ -390,12 +389,12 @@ int ff_mpv_frame_start(MPVMainDecContext *m, AVCodecContext *avctx)
                    "warning: first frame is no keyframe\n");
 
         /* Allocate a dummy frame */
-        idx = ff_find_unused_picture(s->avctx, s->picture, 0);
+        idx = ff_find_unused_picture(s->avctx, m2->picture, 0);
         if (idx < 0) {
             av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
             return idx;
         }
-        s->last_picture_ptr = &s->picture[idx];
+        s->last_picture_ptr = &m2->picture[idx];
 
         s->last_picture_ptr->reference    = 3;
         s->last_picture_ptr->f->key_frame = 0;
@@ -432,12 +431,12 @@ int ff_mpv_frame_start(MPVMainDecContext *m, AVCodecContext *avctx)
     if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
         s->pict_type == AV_PICTURE_TYPE_B) {
         /* Allocate a dummy frame */
-        idx = ff_find_unused_picture(s->avctx, s->picture, 0);
+        idx = ff_find_unused_picture(s->avctx, m2->picture, 0);
         if (idx < 0) {
             av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
             return idx;
         }
-        s->next_picture_ptr = &s->picture[idx];
+        s->next_picture_ptr = &m2->picture[idx];
 
         s->next_picture_ptr->reference   = 3;
         s->next_picture_ptr->f->key_frame = 0;
@@ -562,13 +561,14 @@ void ff_mpeg_draw_horiz_band(MPVDecContext *s, int y, int h)
 void ff_mpeg_flush(AVCodecContext *avctx)
 {
     MPVMainDecContext *const m = avctx->priv_data;
-    MPVDecContext *const s = &m->s;
+    MPVMainContext *const m2 = m;
+    MPVDecContext *const s = &m2->s;
 
-    if (!s->picture)
+    if (!m2->context_initialized)
         return;
 
     for (int i = 0; i < MAX_PICTURE_COUNT; i++)
-        ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
+        ff_mpeg_unref_picture(s->avctx, &m2->picture[i]);
     s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
 
     ff_mpeg_unref_picture(s->avctx, &s->current_picture);
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index d3e52126d7..1d1cd6e3d3 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -1123,11 +1123,11 @@ static int load_input_picture(MPVMainEncContext *m, const AVFrame *pic_arg)
         ff_dlog(s->avctx, "%d %d %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"\n", pic_arg->linesize[0],
                 pic_arg->linesize[1], s->linesize, s->uvlinesize);
 
-        i = ff_find_unused_picture(s->avctx, s->picture, direct);
+        i = ff_find_unused_picture(s->avctx, com->picture, direct);
         if (i < 0)
             return i;
 
-        pic = &s->picture[i];
+        pic = &com->picture[i];
         pic->reference = 3;
 
         if (direct) {
@@ -1571,10 +1571,10 @@ no_output_pic:
             // one & ensure that the shared one is reuseable
 
             Picture *pic;
-            int i = ff_find_unused_picture(s->avctx, s->picture, 0);
+            int i = ff_find_unused_picture(s->avctx, m2->picture, 0);
             if (i < 0)
                 return i;
-            pic = &s->picture[i];
+            pic = &m2->picture[i];
 
             pic->reference = m->reordered_input_picture[0]->reference;
             if (alloc_picture(s, pic, 0) < 0) {
@@ -1967,8 +1967,8 @@ vbv_retry:
 
     /* release non-reference frames */
     for (i = 0; i < MAX_PICTURE_COUNT; i++) {
-        if (!s->picture[i].reference)
-            ff_mpeg_unref_picture(avctx, &s->picture[i]);
+        if (!m2->picture[i].reference)
+            ff_mpeg_unref_picture(avctx, &m2->picture[i]);
     }
 
     av_assert1((m->frame_bits & 7) == 0);
-- 
2.32.0



More information about the ffmpeg-devel mailing list