From 61883272a086fff9550f1df3f932a438b9ed6517 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Tue, 18 Mar 2025 11:52:20 +0100
Subject: [PATCH 39/77] avcodec/mpegvideo: Move vbv_delay_pos to
 MPVMainEncContext

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpeg12enc.c     | 2 +-
 libavcodec/mpegvideo.h     | 1 -
 libavcodec/mpegvideo_enc.c | 4 ++--
 libavcodec/mpegvideoenc.h  | 3 +++
 4 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c
index 1ef88d23b8..43addcfda9 100644
--- a/libavcodec/mpeg12enc.c
+++ b/libavcodec/mpeg12enc.c
@@ -350,7 +350,7 @@ static int mpeg1_encode_picture_header(MPVMainEncContext *const m)
              (s->picture_number - mpeg12->gop_picture_number) & 0x3ff);
     put_bits(&s->pb, 3, s->pict_type);
 
-    s->vbv_delay_pos = put_bytes_count(&s->pb, 0);
+    m->vbv_delay_pos = put_bytes_count(&s->pb, 0);
     put_bits(&s->pb, 16, 0xFFFF);               /* vbv_delay */
 
     // RAL: Forward f_code also needed for B-frames
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 12cb8fc3d5..ed9224d2fe 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -401,7 +401,6 @@ typedef struct MpegEncContext {
 
     /* MPEG-1 specific */
     int last_mv_dir;         ///< last mv_dir, used for B-frame encoding
-    int vbv_delay_pos;       ///< offset of vbv_delay in the bitstream
 
     /* MPEG-2-specific - I wished not to have to support this mess. */
     int progressive_sequence;
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 8af572a69e..cb1cf972d8 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -2023,9 +2023,9 @@ vbv_retry:
             double inbits  = avctx->rc_max_rate *
                              av_q2d(avctx->time_base);
             int    minbits = m->frame_bits - 8 *
-                             (s->vbv_delay_pos - 1);
+                             (m->vbv_delay_pos - 1);
             double bits    = m->rc_context.buffer_index + minbits - inbits;
-            uint8_t *const vbv_delay_ptr = s->pb.buf + s->vbv_delay_pos;
+            uint8_t *const vbv_delay_ptr = s->pb.buf + m->vbv_delay_pos;
 
             if (bits < 0)
                 av_log(avctx, AV_LOG_ERROR,
diff --git a/libavcodec/mpegvideoenc.h b/libavcodec/mpegvideoenc.h
index f02708e7ec..18741e9c22 100644
--- a/libavcodec/mpegvideoenc.h
+++ b/libavcodec/mpegvideoenc.h
@@ -72,6 +72,9 @@ typedef struct MPVMainEncContext {
     int lmin, lmax;
     int vbv_ignore_qmax;
 
+    /* MPEG-1/2 specific */
+    int vbv_delay_pos;             ///< offset of vbv_delay in the bitstream
+
     const uint8_t *fcode_tab;      ///< smallest fcode needed for each MV
 
     /* frame skip options */
-- 
2.45.2