From 6d2d34ec3b27c315938826233946d2105e3037f3 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Wed, 19 Mar 2025 21:28:00 +0100
Subject: [PATCH 72/77] avcodec/mpegvideo_enc: Defer initialization of mb-pos
 dependent vars

Only set them after mb_x and mb_y are known which happens
only after the call to ff_h261_reorder_mb_index().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegvideo_enc.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index e84f1dd467..dc165d92d2 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -3024,8 +3024,7 @@ static int encode_thread(AVCodecContext *c, void *arg){
         ff_init_block_index(&s->c);
 
         for (int mb_x = 0; mb_x < s->c.mb_width; mb_x++) {
-            int xy = mb_y*s->c.mb_stride + mb_x; // removed const, H261 needs to adjust this
-            int mb_type= s->mb_type[xy];
+            int mb_type, xy;
 //            int d;
             int dmin= INT_MAX;
             int dir;
@@ -3049,11 +3048,10 @@ static int encode_thread(AVCodecContext *c, void *arg){
             s->c.mb_y = mb_y;  // moved into loop, can get changed by H.261
             ff_update_block_index(&s->c, 8, 0, s->c.chroma_x_shift);
 
-            if(CONFIG_H261_ENCODER && s->c.codec_id == AV_CODEC_ID_H261){
+            if (CONFIG_H261_ENCODER && s->c.codec_id == AV_CODEC_ID_H261)
                 ff_h261_reorder_mb_index(s);
-                xy = s->c.mb_y*s->c.mb_stride + s->c.mb_x;
-                mb_type= s->mb_type[xy];
-            }
+            xy      = s->c.mb_y * s->c.mb_stride + s->c.mb_x;
+            mb_type = s->mb_type[xy];
 
             /* write gob / video packet header  */
             if(s->rtp_mode){
-- 
2.45.2