PR #26260 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26260
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26260.patch

With 4:4:4 input, `+psnr` corrupted the mjpeg and speedhq output, and the 
chroma Quality Stats compared the wrong columns.

- Reconstruct all 8 chroma blocks of 4:4:4 intra macroblocks. The 4:2:2 path 
wrote half of them at half stride, into input that was not yet encoded.
- Use the 4:4:4 chroma window for the stats.

`fate-ffmpeg-speedhq-444-psnr` covers both. 4:2:0 and 4:2:2 output and stats 
are unchanged.

Reported by Forgejo_Fairy on https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25368.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From 79edf1c1a29f53c4538f6a2dfe7a96cd537f263a Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sat, 21 Nov 2026 12:12:41 -0800
Subject: [PATCH 1/2] avcodec/mpegvideo_enc: reconstruct 4:4:4 intra chroma
 blocks

The 4:2:2 path wrote only four chroma blocks at the wrong stride,
corrupting not yet encoded input when +psnr was set.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavcodec/mpegvideo_enc.c             | 11 +++++++++++
 tests/fate/ffmpeg.mak                  |  3 +++
 tests/ref/fate/ffmpeg-speedhq-444-psnr |  6 ++++++
 3 files changed, 20 insertions(+)
 create mode 100644 tests/ref/fate/ffmpeg-speedhq-444-psnr

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index dd2cb487a2b..476579d1ae7 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -1200,6 +1200,17 @@ static void mpv_reconstruct_mb(MPVEncContext *const s, 
int16_t block[12][64])
                 if (s->c.chroma_y_shift) {
                     put_dct(s, block[4], 4, dest_cb, uvlinesize, 
s->c.chroma_qscale);
                     put_dct(s, block[5], 5, dest_cr, uvlinesize, 
s->c.chroma_qscale);
+                } else if (!s->c.chroma_x_shift) {
+                    const int uv_dct_linesize = uvlinesize << 
s->c.interlaced_dct;
+                    const int uv_dct_offset   = s->c.interlaced_dct ? 
uvlinesize : uvlinesize * block_size;
+                    put_dct(s, block[ 4],  4, dest_cb,                         
     uv_dct_linesize, s->c.chroma_qscale);
+                    put_dct(s, block[ 5],  5, dest_cr,                         
     uv_dct_linesize, s->c.chroma_qscale);
+                    put_dct(s, block[ 6],  6, dest_cb + block_size,            
     uv_dct_linesize, s->c.chroma_qscale);
+                    put_dct(s, block[ 7],  7, dest_cr + block_size,            
     uv_dct_linesize, s->c.chroma_qscale);
+                    put_dct(s, block[ 8],  8, dest_cb + uv_dct_offset,         
     uv_dct_linesize, s->c.chroma_qscale);
+                    put_dct(s, block[ 9],  9, dest_cr + uv_dct_offset,         
     uv_dct_linesize, s->c.chroma_qscale);
+                    put_dct(s, block[10], 10, dest_cb + uv_dct_offset + 
block_size, uv_dct_linesize, s->c.chroma_qscale);
+                    put_dct(s, block[11], 11, dest_cr + uv_dct_offset + 
block_size, uv_dct_linesize, s->c.chroma_qscale);
                 } else {
                     dct_offset   >>= 1;
                     dct_linesize >>= 1;
diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak
index e665b08bb9a..b1ca9e1caa0 100644
--- a/tests/fate/ffmpeg.mak
+++ b/tests/fate/ffmpeg.mak
@@ -19,6 +19,9 @@ fate-ffmpeg-lavfi: CMD = framecrc -lavfi color=d=1:r=5 
-fflags +bitexact
 FATE_FFMPEG-$(call FILTERFRAMECRC, TESTSRC2 FORMAT SETPARAMS SCALE, 
LAVFI_INDEV WRAPPED_AVFRAME_DECODER MPEG2VIDEO_ENCODER) += 
fate-ffmpeg-chroma-location
 fate-ffmpeg-chroma-location: CMD = framecrc -auto_conversion_filters -f lavfi 
-i testsrc2=s=64x64:r=25:d=1,format=yuv420p,setparams=chroma_location=topleft 
-sws_flags +accurate_rnd+bitexact -flags +bitexact -idct simple -dct fastint 
-threads 1 -c:v mpeg2video -qscale:v 2 -frames:v 1
 
+FATE_FFMPEG-$(call FILTERFRAMECRC, TESTSRC2 FORMAT, LAVFI_INDEV 
WRAPPED_AVFRAME_DECODER SPEEDHQ_ENCODER) += fate-ffmpeg-speedhq-444-psnr
+fate-ffmpeg-speedhq-444-psnr: CMD = framecrc -f lavfi -i 
testsrc2=s=128x64:r=25:d=1,format=yuv444p -flags +bitexact+psnr -idct simple 
-dct fastint -threads 1 -c:v speedhq -qscale:v 3 -frames:v 1
+
 FATE_FFMPEG-$(if $(CONFIG_SMALL),no,$(call ALLYES, TESTSRC2_FILTER 
SCALE_FILTER FORMAT_FILTER LAVFI_INDEV WRAPPED_AVFRAME_DECODER RAWVIDEO_ENCODER 
NULL_MUXER)) += fate-ffmpeg-print-graphs-compact
 fate-ffmpeg-print-graphs-compact: CMD = ffmpeg -f lavfi -i 
testsrc2=s=64x64:r=25:d=0.08 -filter_complex 
"[0:v]scale=32:32,format=yuv420p[v]" -map "[v]" -c:v rawvideo 
-print_graphs_file - -print_graphs_format compact -f null -
 
diff --git a/tests/ref/fate/ffmpeg-speedhq-444-psnr 
b/tests/ref/fate/ffmpeg-speedhq-444-psnr
new file mode 100644
index 00000000000..aa6b14733e2
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-speedhq-444-psnr
@@ -0,0 +1,6 @@
+#tb 0: 1/25
+#media_type 0: video
+#codec_id 0: speedhq
+#dimensions 0: 128x64
+#sar 0: 1/1
+0,          0,          0,        1,     5728, 0xc7b9b093, S=1, Quality stats, 
      32, 0x4df20471
-- 
2.54.1


From 43615fab58c027d0e6a1fa3ff9c530181ce20c47 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sat, 21 Nov 2026 12:13:01 -0800
Subject: [PATCH 2/2] avcodec/mpegvideo_enc: use the 4:4:4 chroma window for
 Quality Stats

The chroma error assumed 8 pixel wide macroblocks.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavcodec/mpegvideo_enc.c             | 9 +++++----
 tests/ref/fate/ffmpeg-speedhq-444-psnr | 2 +-
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 476579d1ae7..0ada755264e 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -2979,6 +2979,7 @@ int ff_mpv_reallocate_putbitbuffer(MPVEncContext *const 
s, size_t threshold, siz
 
 static int encode_thread(AVCodecContext *c, void *arg){
     MPVEncContext *const s = *(void**)arg;
+    int chr_w = 16 >> s->c.chroma_x_shift;
     int chr_h = 16 >> s->c.chroma_y_shift;
     int i;
     MBBackup best_s = { 0 }, backup_s;
@@ -3590,11 +3591,11 @@ static int encode_thread(AVCodecContext *c, void *arg){
                     s, s->new_pic->data[0] + s->c.mb_x*16 + 
s->c.mb_y*s->c.linesize*16,
                     s->c.dest[0], w, h, s->c.linesize);
                 s->encoding_error[1] += sse(
-                    s, s->new_pic->data[1] + s->c.mb_x*8  + 
s->c.mb_y*s->c.uvlinesize*chr_h,
-                    s->c.dest[1], w>>1, h>>s->c.chroma_y_shift, 
s->c.uvlinesize);
+                    s, s->new_pic->data[1] + s->c.mb_x*chr_w + 
s->c.mb_y*s->c.uvlinesize*chr_h,
+                    s->c.dest[1], w>>s->c.chroma_x_shift, 
h>>s->c.chroma_y_shift, s->c.uvlinesize);
                 s->encoding_error[2] += sse(
-                    s, s->new_pic->data[2] + s->c.mb_x*8  + 
s->c.mb_y*s->c.uvlinesize*chr_h,
-                    s->c.dest[2], w>>1, h>>s->c.chroma_y_shift, 
s->c.uvlinesize);
+                    s, s->new_pic->data[2] + s->c.mb_x*chr_w + 
s->c.mb_y*s->c.uvlinesize*chr_h,
+                    s->c.dest[2], w>>s->c.chroma_x_shift, 
h>>s->c.chroma_y_shift, s->c.uvlinesize);
             }
             if (s->loop_filter) {
                 if (CONFIG_H263_ENCODER && s->c.out_format == FMT_H263)
diff --git a/tests/ref/fate/ffmpeg-speedhq-444-psnr 
b/tests/ref/fate/ffmpeg-speedhq-444-psnr
index aa6b14733e2..9a20abb42d3 100644
--- a/tests/ref/fate/ffmpeg-speedhq-444-psnr
+++ b/tests/ref/fate/ffmpeg-speedhq-444-psnr
@@ -3,4 +3,4 @@
 #codec_id 0: speedhq
 #dimensions 0: 128x64
 #sar 0: 1/1
-0,          0,          0,        1,     5728, 0xc7b9b093, S=1, Quality stats, 
      32, 0x4df20471
+0,          0,          0,        1,     5728, 0xc7b9b093, S=1, Quality stats, 
      32, 0x50cf0497
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to