[FFmpeg-devel] [PATCH v2 58/69] avcodec/mpegvideo: Move slice-context ptr array to MPVMainContext

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


The slice contexts don't have sub-slice contexts, so it makes
no sense for them to have an array of pointers to slice contexts.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/mjpegenc.c      |  2 +-
 libavcodec/mpeg12dec.c     | 12 ++++++------
 libavcodec/mpegvideo.c     | 21 ++++++++++-----------
 libavcodec/mpegvideo.h     |  3 ++-
 libavcodec/mpegvideo_dec.c | 11 ++++++-----
 libavcodec/mpegvideo_enc.c | 29 +++++++++++++++--------------
 6 files changed, 40 insertions(+), 38 deletions(-)

diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c
index faf59566b1..70636ffc91 100644
--- a/libavcodec/mjpegenc.c
+++ b/libavcodec/mjpegenc.c
@@ -86,7 +86,7 @@ static void mjpeg_encode_picture_header(MPVMainEncContext *m)
 
     s->esc_pos = put_bytes_count(&s->pb, 0);
     for (int i = 1; i < s->slice_context_count; i++)
-        s->thread_context[i]->esc_pos = 0;
+        m2->thread_context[i]->esc_pos = 0;
 }
 
 void ff_mjpeg_amv_encode_picture_header(MPVMainEncContext *main)
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index ef2fa80aba..925c72e8ff 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -2503,10 +2503,10 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
                     av_assert0(avctx->thread_count > 1);
 
                     avctx->execute(avctx, slice_decode_thread,
-                                   &s2->thread_context[0], NULL,
+                                   &m2->thread_context[0], NULL,
                                    s->slice_count, sizeof(void *));
                     for (i = 0; i < s->slice_count; i++)
-                        s2->er.error_count += s2->thread_context[i]->er.error_count;
+                        s2->er.error_count += m2->thread_context[i]->er.error_count;
                 }
 
                 ret = slice_end(avctx, picture);
@@ -2575,10 +2575,10 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
                 int i;
 
                 avctx->execute(avctx, slice_decode_thread,
-                               s2->thread_context, NULL,
+                               m2->thread_context, NULL,
                                s->slice_count, sizeof(void *));
                 for (i = 0; i < s->slice_count; i++)
-                    s2->er.error_count += s2->thread_context[i]->er.error_count;
+                    s2->er.error_count += m2->thread_context[i]->er.error_count;
                 s->slice_count = 0;
             }
             if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
@@ -2774,12 +2774,12 @@ static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
                                     s2->slice_context_count;
                     av_assert0(avctx->thread_count > 1);
                     if (threshold <= mb_y) {
-                        MPVDecContext *const thread_context = s2->thread_context[s->slice_count];
+                        MPVDecContext *const thread_context = m2->thread_context[s->slice_count];
 
                         thread_context->start_mb_y = mb_y;
                         thread_context->end_mb_y   = s2->mb_height;
                         if (s->slice_count) {
-                            s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
+                            m2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
                             ret = ff_update_duplicate_context(thread_context, s2);
                             if (ret < 0)
                                 return ret;
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 072a82e6b3..220f1c39f0 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -400,14 +400,15 @@ int ff_mpv_init_duplicate_contexts(MPVMainContext *m)
      * 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, sizeof(MPVContext));
-        if (!s->thread_context[i])
+        MPVContext *const s2 = av_memdup(s, sizeof(MPVContext));
+        if (!s2)
             return AVERROR(ENOMEM);
-        if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
+        m->thread_context[i] = s2;
+        if ((ret = init_duplicate_context(s2)) < 0)
             return ret;
-        s->thread_context[i]->start_mb_y =
+        s2->start_mb_y =
             (s->mb_height * (i    ) + nb_slices / 2) / nb_slices;
-        s->thread_context[i]->end_mb_y   =
+        s2->end_mb_y   =
             (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
     }
     s->start_mb_y = 0;
@@ -441,8 +442,8 @@ static void free_duplicate_contexts(MPVMainContext *m)
     MPVContext *const s = &m->s;
 
     for (int i = 1; i < s->slice_context_count; i++) {
-        free_duplicate_context(s->thread_context[i]);
-        av_freep(&s->thread_context[i]);
+        free_duplicate_context(m->thread_context[i]);
+        av_freep(&m->thread_context[i]);
     }
     free_duplicate_context(s);
 }
@@ -621,8 +622,7 @@ static void clear_context(MPVMainContext *m)
     memset(&s->current_picture, 0, sizeof(s->current_picture));
     memset(&s->new_picture, 0, sizeof(s->new_picture));
 
-    memset(s->thread_context, 0, sizeof(s->thread_context));
-
+    s->slice_context_count = 1;
     s->me.map = NULL;
     s->me.score_map = NULL;
     s->dct_error_sum = NULL;
@@ -736,8 +736,7 @@ av_cold int ff_mpv_common_init(MPVMainContext *m)
 #endif
 
     s->context_initialized = 1;
-    memset(s->thread_context, 0, sizeof(s->thread_context));
-    s->thread_context[0]   = s;
+    m->thread_context[0]   = s;
     s->slice_context_count = nb_slices;
 
 //     if (s->width && s->height) {
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 9adadc5b02..9005dcb58a 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -120,7 +120,6 @@ typedef struct MPVContext {
 
     int start_mb_y;            ///< start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y)
     int end_mb_y;              ///< end   mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y)
-    struct MPVContext *thread_context[MAX_THREADS];
     int slice_context_count;   ///< number of used thread_contexts
 
     /**
@@ -506,6 +505,8 @@ typedef struct MPVContext {
  */
 typedef struct MPVMainContext {
     MPVContext s;
+    /* The first entry of this array points to the above MPVContext. */
+    MPVContext *thread_context[MAX_THREADS];
 } MPVMainContext;
 
 /**
diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c
index e70e306cf9..f202db33e9 100644
--- a/libavcodec/mpegvideo_dec.c
+++ b/libavcodec/mpegvideo_dec.c
@@ -197,13 +197,14 @@ do {\
 
 int ff_mpv_common_frame_size_change(MPVMainDecContext *m)
 {
+    MPVMainContext *const m2 = m;
     MPVDecContext *const s = &m->s;
     int err = 0;
 
     if (!s->context_initialized)
         return AVERROR(EINVAL);
 
-    ff_mpv_free_context_frame(m);
+    ff_mpv_free_context_frame(m2);
 
     if (s->picture)
         for (int i = 0; i < MAX_PICTURE_COUNT; i++)
@@ -233,11 +234,11 @@ int ff_mpv_common_frame_size_change(MPVMainDecContext *m)
     if ((err = ff_mpv_init_context_frame(m)))
         goto fail;
 
-    memset(s->thread_context, 0, sizeof(s->thread_context));
-    s->thread_context[0]   = s;
+    memset(m2->thread_context, 0, sizeof(m2->thread_context));
+    m2->thread_context[0] = s;
 
     if (s->width && s->height) {
-        err = ff_mpv_init_duplicate_contexts(m);
+        err = ff_mpv_init_duplicate_contexts(m2);
         if (err < 0)
             goto fail;
     }
@@ -245,7 +246,7 @@ int ff_mpv_common_frame_size_change(MPVMainDecContext *m)
 
     return 0;
  fail:
-    ff_mpv_free_context_frame(m);
+    ff_mpv_free_context_frame(m2);
     s->context_reinit = 1;
     return err;
 }
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 656aab04cf..735c9d322f 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -1778,13 +1778,13 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
         }
 
         for (i = 0; i < context_count; i++) {
-            int start_y = s->thread_context[i]->start_mb_y;
-            int   end_y = s->thread_context[i]->  end_mb_y;
+            int start_y = m2->thread_context[i]->start_mb_y;
+            int   end_y = m2->thread_context[i]->  end_mb_y;
             int h       = s->mb_height;
             uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
             uint8_t *end   = pkt->data + (size_t)(((int64_t) pkt->size) *   end_y / h);
 
-            init_put_bits(&s->thread_context[i]->pb, start, end - start);
+            init_put_bits(&m2->thread_context[i]->pb, start, end - start);
         }
 
         s->pict_type = s->new_picture.f->pict_type;
@@ -1838,7 +1838,7 @@ vbv_retry:
                     s->last_non_b_time = s->time - s->pp_time;
                 }
                 for (i = 0; i < context_count; i++) {
-                    PutBitContext *pb = &s->thread_context[i]->pb;
+                    PutBitContext *pb = &m2->thread_context[i]->pb;
                     init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
                 }
                 s->vbv_ignore_qmax = 1;
@@ -3602,7 +3602,7 @@ static int encode_picture(MPVMainEncContext *m, int picture_number)
 
     s->mb_intra=0; //for the rate distortion & bit compare functions
     for(i=1; i<context_count; i++){
-        ret = ff_update_duplicate_context(s->thread_context[i], s);
+        ret = ff_update_duplicate_context(m2->thread_context[i], s);
         if (ret < 0)
             return ret;
     }
@@ -3617,11 +3617,11 @@ static int encode_picture(MPVMainEncContext *m, int picture_number)
         if (s->pict_type != AV_PICTURE_TYPE_B) {
             if ((m->me_pre && m->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
                 m->me_pre == 2) {
-                s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
+                s->avctx->execute(s->avctx, pre_estimate_motion_thread, &m2->thread_context[0], NULL, context_count, sizeof(void*));
             }
         }
 
-        s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
+        s->avctx->execute(s->avctx, estimate_motion_thread, &m2->thread_context[0], NULL, context_count, sizeof(void*));
     }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
         /* I-Frame */
         for(i=0; i<s->mb_stride*s->mb_height; i++)
@@ -3629,11 +3629,11 @@ static int encode_picture(MPVMainEncContext *m, int picture_number)
 
         if (!m->fixed_qscale) {
             /* finding spatial complexity for I-frame rate control */
-            s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
+            s->avctx->execute(s->avctx, mb_var_thread, &m2->thread_context[0], NULL, context_count, sizeof(void*));
         }
     }
     for(i=1; i<context_count; i++){
-        merge_context_after_me(s, s->thread_context[i]);
+        merge_context_after_me(s, m2->thread_context[i]);
     }
     s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
     s->current_picture.   mb_var_sum= s->current_picture_ptr->   mb_var_sum= s->me.   mb_var_sum_temp;
@@ -3819,13 +3819,14 @@ static int encode_picture(MPVMainEncContext *m, int picture_number)
     m->header_bits = bits - s->last_bits;
 
     for(i=1; i<context_count; i++){
-        update_duplicate_context_after_me(s->thread_context[i], s);
+        update_duplicate_context_after_me(m2->thread_context[i], s);
     }
-    s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
+    s->avctx->execute(s->avctx, encode_thread, &m2->thread_context[0], NULL, context_count, sizeof(void*));
     for(i=1; i<context_count; i++){
-        if (s->pb.buf_end == s->thread_context[i]->pb.buf)
-            set_put_bits_buffer_size(&s->pb, FFMIN(s->thread_context[i]->pb.buf_end - s->pb.buf, INT_MAX/8-BUF_BITS));
-        merge_context_after_encode(s, s->thread_context[i]);
+        MPVEncContext *const slice = m2->thread_context[i];
+        if (s->pb.buf_end == slice->pb.buf)
+            set_put_bits_buffer_size(&s->pb, FFMIN(slice->pb.buf_end - s->pb.buf, INT_MAX/8-BUF_BITS));
+        merge_context_after_encode(s, slice);
     }
     emms_c();
     return 0;
-- 
2.32.0



More information about the ffmpeg-devel mailing list