Commit 166b5d69ad for ffmpeg

commit 166b5d69ad67eb6e34ef3591ff8967d69e05ab09
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Sun Nov 15 11:08:12 2026 -0800

    avcodec/libx264: apply ROI offsets per macroblock pair when interlaced

    A field coded pair spreads both map rows over its lines, so set both rows
    of each pair a region touches; also size the map for x264's even row count.

    ROI map overread reported by Forgejo_Fairy in review of #25875; allocation
    fix by lusoris in #25909.

    Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index e5a853c10a..f83be47e0c 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -410,6 +410,10 @@ static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic,

     int mbx = (frame->width + MB_SIZE - 1) / MB_SIZE;
     int mby = (frame->height + MB_SIZE - 1) / MB_SIZE;
+    /* x264 rounds the macroblock row count up to an even number for
+     * interlaced and fake-interlaced encoding and reads quant_offsets
+     * over that grid, so size the map for it. */
+    int mby_alloc = FFALIGN(mby, 2);
     int qp_range = 51 + 6 * (x4->params.i_bitdepth - 8);
     int nb_rois;
     const AVRegionOfInterest *roi;
@@ -438,7 +442,7 @@ static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic,
     }
     nb_rois = size / roi_size;

-    qoffsets = av_calloc(mbx * mby, sizeof(*qoffsets));
+    qoffsets = av_calloc(mbx * mby_alloc, sizeof(*qoffsets));
     if (!qoffsets)
         return AVERROR(ENOMEM);

@@ -452,6 +456,14 @@ static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic,

         starty = FFMIN(mby, roi->top / MB_SIZE);
         endy   = FFMIN(mby, (roi->bottom + MB_SIZE - 1)/ MB_SIZE);
+        if (x4->params.b_interlaced && starty < endy) {
+            /* A macroblock pair may be field coded, in which case each of
+             * its two rows covers every other line of the pair's 32 lines.
+             * Apply the offset to both rows of each pair the region
+             * touches. */
+            starty &= ~1;
+            endy    = FFALIGN(endy, 2);
+        }
         startx = FFMIN(mbx, roi->left / MB_SIZE);
         endx   = FFMIN(mbx, (roi->right + MB_SIZE - 1)/ MB_SIZE);