PR #25930 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25930 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25930.patch
Two fixes for the per-macroblock maps the libx264 wrapper hands to x264 when interlaced encoding is enabled. Both were noted as pre-existing in the review of #25909. ## mb_info map size `setup_mb_info()` allocates `mb_width * mb_height` bytes for the frame's own macroblock row count. x264 rounds its row count up to an even number for interlaced and fake-interlaced encoding and reads `mb_info` over that grid, so with an odd count it reads one row past the allocation. The map is now allocated for an even row count; the extra row stays 0, that is not marked constant. ## ROI mapping with interlaced encoding `setup_roi()` maps regions to macroblock rows in frame raster order. With `interlaced=1` x264 codes macroblock pairs, each frame or field coded, and in a field coded pair each of the two map rows covers every other line of the pair's 32 lines. A region mapped to one row of a pair therefore missed half of its lines. The choice is made per pair after adaptive quantization, so the offset is now applied to both rows of every pair a region touches. Fake-interlaced encoding keeps the raster mapping, since x264 codes those frames progressively. This commit also allocates the offset map for an even row count, because the pair rounding writes into that row. That part is the same change as #25909; whichever lands second needs a rebase. ## Testing AddressSanitizer build of FFmpeg with libx264 (x264 itself not instrumented), `testsrc2` with `addroi`, at 64x48, 64x64 and 64x80, progressive, `interlaced=1` and `fake-interlaced=1`, three region positions each (27 cases), compared with master: - all 27 run without an AddressSanitizer report; - progressive and fake-interlaced output is bit-identical to master; - `interlaced=1` output changes in 7 of 9 cases. The two unchanged ones are the bottom-row region at 64x48 and 64x80, where the only newly set row is the added one. Not tested: the mb_info change at runtime (no command line path produces video hint side data), and the overreads themselves, which happen inside x264 and need an instrumented x264 to observe. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 8c95d60049a7be75bf1ed928f0e8ad74a8d7147e Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Tue, 10 Nov 2026 23:38:24 -0800 Subject: [PATCH 1/2] avcodec/libx264: size the mb_info map for interlaced encoding x264 rounds its macroblock row count up to an even number for interlaced and fake-interlaced encoding and reads mb_info over that grid. setup_mb_info() allocated the map for the frame's own row count, so with an odd count x264 read one row past the allocation. Allocate the map for an even row count. The extra row stays 0, that is not marked constant. Co-Authored-By: Claude Fable 5.2 <[email protected]> --- libavcodec/libx264.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index a432c0b50e5..e5a853c10a9 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -370,7 +370,10 @@ static int setup_mb_info(AVCodecContext *ctx, x264_picture_t *pic, mbinfo_rects = (const AVVideoRect *)av_video_hint_rects(info); nb_rects = info->nb_rects; - mbinfo = av_calloc(mb_width * mb_height, sizeof(*mbinfo)); + /* x264 rounds the macroblock row count up to an even number for + * interlaced and fake-interlaced encoding and reads mb_info over + * that grid, so size the map for it. The extra row stays 0. */ + mbinfo = av_calloc(mb_width * FFALIGN(mb_height, 2), sizeof(*mbinfo)); if (!mbinfo) return AVERROR(ENOMEM); -- 2.54.0 From 18d102b35e0b827e8a963086e097bb28206fb94c Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Tue, 10 Nov 2026 23:39:28 -0800 Subject: [PATCH 2/2] avcodec/libx264: apply ROI offsets per macroblock pair when interlaced With interlaced encoding x264 codes macroblock pairs, each either frame or field coded. In a field coded pair each of the two map rows covers every other line of the pair's 32 lines, so a region mapped in frame raster order to one row of a pair missed half of its lines. The choice is made per pair after adaptive quantization, so apply the offset to both rows of every pair a region touches. x264 also rounds its macroblock row count up to an even number for interlaced and fake-interlaced encoding and reads quant_offsets over that grid, so allocate the map for an even row count. With an odd count it read one row past the allocation. Co-Authored-By: Claude Fable 5.2 <[email protected]> --- libavcodec/libx264.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index e5a853c10a9..ffd70b83701 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) { + /* 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); -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
