[FFmpeg-cvslog] avcodec/mpegvideo_enc: Unify initializing PutBitContexts

Andreas Rheinhardt git at videolan.org
Wed Jun 12 14:16:32 EEST 2024


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Mon Apr 22 09:18:22 2024 +0200| [4f2719a239e2efd9902922035c9cb16eb3a7138d] | committer: Andreas Rheinhardt

avcodec/mpegvideo_enc: Unify initializing PutBitContexts

This also rids us of the requirement to preserve the PutBitContext
in ff_update_duplicate_context().

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

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

 libavcodec/mpegvideo.c     |  1 -
 libavcodec/mpegvideo_enc.c | 42 +++++++++++++++++++-----------------------
 2 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 89d19a743a..93df8a315d 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -471,7 +471,6 @@ static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
     COPY(start_mb_y);
     COPY(end_mb_y);
     COPY(me.map_generation);
-    COPY(pb);
     COPY(dct_error_sum);
     COPY(dct_count[0]);
     COPY(dct_count[1]);
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index eaaf366624..6d1dbccf5f 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -84,7 +84,7 @@
 #define QMAT_SHIFT_MMX 16
 #define QMAT_SHIFT 21
 
-static int encode_picture(MpegEncContext *s);
+static int encode_picture(MpegEncContext *s, const AVPacket *pkt);
 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
 static int sse_mb(MpegEncContext *s);
 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
@@ -1669,7 +1669,6 @@ static int select_input_picture(MpegEncContext *s)
             ff_mpv_unref_picture(&s->cur_pic);
             return ret;
         }
-        s->me.temp = s->me.scratchpad = s->sc.scratchpad_buf;
         s->picture_number = s->cur_pic.ptr->display_picture_number;
 
     }
@@ -1755,7 +1754,7 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
                           const AVFrame *pic_arg, int *got_packet)
 {
     MpegEncContext *s = avctx->priv_data;
-    int i, stuffing_count, ret;
+    int stuffing_count, ret;
     int context_count = s->slice_context_count;
 
     ff_mpv_unref_picture(&s->cur_pic);
@@ -1791,21 +1790,11 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
             s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
         }
 
-        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 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);
-        }
-
         s->pict_type = s->new_pic->pict_type;
         //emms_c();
         frame_start(s);
 vbv_retry:
-        ret = encode_picture(s);
+        ret = encode_picture(s, pkt);
         if (growing_buffer) {
             av_assert0(s->pb.buf == avctx->internal->byte_buffer);
             pkt->data = s->pb.buf;
@@ -1849,10 +1838,6 @@ vbv_retry:
                     s->time_base       = s->last_time_base;
                     s->last_non_b_time = s->time - s->pp_time;
                 }
-                for (i = 0; i < context_count; i++) {
-                    PutBitContext *pb = &s->thread_context[i]->pb;
-                    init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
-                }
                 s->vbv_ignore_qmax = 1;
                 av_log(avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
                 goto vbv_retry;
@@ -3564,7 +3549,7 @@ static void set_frame_distances(MpegEncContext * s){
     }
 }
 
-static int encode_picture(MpegEncContext *s)
+static int encode_picture(MpegEncContext *s, const AVPacket *pkt)
 {
     int i, ret;
     int bits;
@@ -3616,12 +3601,23 @@ static int encode_picture(MpegEncContext *s)
         return -1;
 
     s->mb_intra=0; //for the rate distortion & bit compare functions
-    for(i=1; i<context_count; i++){
+    for (int i = 0; i < context_count; i++) {
         MpegEncContext *const slice = s->thread_context[i];
-        ret = ff_update_duplicate_context(slice, s);
-        if (ret < 0)
-            return ret;
+        uint8_t *start, *end;
+        int h;
+
+        if (i) {
+            ret = ff_update_duplicate_context(slice, s);
+            if (ret < 0)
+                return ret;
+        }
         slice->me.temp = slice->me.scratchpad = slice->sc.scratchpad_buf;
+
+        h     = s->mb_height;
+        start = pkt->data + (size_t)(((int64_t) pkt->size) * slice->start_mb_y / h);
+        end   = pkt->data + (size_t)(((int64_t) pkt->size) * slice->  end_mb_y / h);
+
+        init_put_bits(&s->thread_context[i]->pb, start, end - start);
     }
 
     /* Estimate motion for every MB */



More information about the ffmpeg-cvslog mailing list