From 3b229dc34e11826e18f42c228b77596ff30c8776 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Wed, 26 Mar 2025 08:38:58 +0100
Subject: [PATCH 05/11] avcodec/mpegvideo_enc: Set b-frame chain length
 properly

Fixes a regression caused by my desire to use loop-scope
for iterators in 72bf3d3c12ae562fd408603483bc59058757b1a1.

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

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index fb3ad2e25c..febe50930b 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -1726,8 +1726,9 @@ static int set_bframe_chain_length(MPVMainEncContext *const m)
                                         s->c.linesize) + 1;
                 }
             }
-            for (int i = 0; i < m->max_b_frames + 1; i++) {
-                if (!m->input_picture[i] ||
+            for (int i = 0;; i++) {
+                if (i >= m->max_b_frames + 1 ||
+                    !m->input_picture[i] ||
                     m->input_picture[i]->b_frame_score - 1 >
                         s->c.mb_num / m->b_sensitivity) {
                     b_frames = FFMAX(0, i - 1);
-- 
2.45.2