PR #25942 opened by kierank URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25942 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25942.patch
For qmul > 32767 the function multiplied by qmul >> 7 and shifted by one less. That is only exact when the low 7 bits of qmul are zero, which holds for flat and default scaling matrices but not for custom DC scaling factors at low and mid QP (e.g. scale 85 at QP 25 gives qmul 59840). From qmul >= 1 << 22 the shifted value also no longer fits the signed words pmaddwd multiplies. In both cases the output differs from the C code. Split qmul into its low 15 bits and the rest instead and compute both products with pmaddwd; their sum equals x * qmul modulo 2^32, like the C code. Extend the checkasm test to cover qmul > 32767; it previously only used qmul < 4096. # Summary of changes Briefly describe what this PR does and why. <!-- If this PR requires new FATE test samples, attach them to the PR and list their target paths below (relative to the fate-suite root). Attached filenames must match the sample's filename: ```fate-samples # e.g. vorbis/new-sample.ogg ``` --> >From c7c0e9f88b427440d2f3c1e526a8affb4457a5f6 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya <[email protected]> Date: Wed, 11 Nov 2026 02:38:58 +0000 Subject: [PATCH] avcodec/x86/h264_idct: Fix ff_h264_luma_dc_dequant_idct_sse2 for qmul > 32767 For qmul > 32767 the function multiplied by qmul >> 7 and shifted by one less. That is only exact when the low 7 bits of qmul are zero, which holds for flat and default scaling matrices but not for custom DC scaling factors at low and mid QP (e.g. scale 85 at QP 25 gives qmul 59840). From qmul >= 1 << 22 the shifted value also no longer fits the signed words pmaddwd multiplies. In both cases the output differs from the C code. Split qmul into its low 15 bits and the rest instead and compute both products with pmaddwd; their sum equals x * qmul modulo 2^32, like the C code. Extend the checkasm test to cover qmul > 32767; it previously only used qmul < 4096. --- libavcodec/x86/h264_idct.asm | 50 ++++++++++++++++++------------------ tests/checkasm/h264dsp.c | 22 ++++++++-------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/libavcodec/x86/h264_idct.asm b/libavcodec/x86/h264_idct.asm index 2014129e9fa..ab1c516b836 100644 --- a/libavcodec/x86/h264_idct.asm +++ b/libavcodec/x86/h264_idct.asm @@ -572,22 +572,24 @@ RET SWAP %1, %4, %3 %endmacro -; requires m5 to contain pw_1 -%macro DEQUANT 1 +; requires m5 to contain pw_1; with %2 set, m6 holds the high part of qmul +%macro DEQUANT 1-2 0 movd m4, t3d pshufd m4, m4, 0 - punpcklwd m0, m5 - punpcklwd m1, m5 - punpcklwd m2, m5 - punpcklwd m3, m5 - pmaddwd m0, m4 - pmaddwd m1, m4 - pmaddwd m2, m4 - pmaddwd m3, m4 - psrad m0, %1 - psrad m1, %1 - psrad m2, %1 - psrad m3, %1 +%assign %%i 0 +%rep 4 + punpcklwd m %+ %%i, m5 +%if %2 + pmaddwd m7, m %+ %%i, m6 + pslld m7, 15 +%endif + pmaddwd m %+ %%i, m4 +%if %2 + paddd m %+ %%i, m7 +%endif + psrad m %+ %%i, %1 +%assign %%i %%i+1 +%endrep %endmacro %macro STORE_WORDS 10 @@ -636,8 +638,8 @@ RET %endif %endmacro -%macro DEQUANT_STORE 1 - DEQUANT %1 +%macro DEQUANT_STORE 1-2 0 + DEQUANT %1, %2 STORE_WORDS m0, m1, 0, 1, 4, 5, 2, 3, 6, 7 STORE_WORDS m2, m3, 8, 9, 12, 13, 10, 11, 14, 15 %endmacro @@ -678,17 +680,15 @@ cglobal h264_luma_dc_dequant_idct, 3, 4, 6 DEQUANT_STORE 8 RET .big_qmul: - bsr t0d, t3d - WIN64_PUSH_XMM 7 + ; x * qmul = x * (qmul & 0x7fff) + (x * (qmul >> 15) << 15), modulo 2^32 like the C code + WIN64_PUSH_XMM 8 + mov t1d, t3d + shr t1d, 15 + and t3d, 0x7fff add t3d, 128 << 16 - mov t1d, 7 - cmp t0d, t1d - cmovg t0d, t1d - inc t1d - shr t3d, t0b - sub t1d, t0d movd m6, t1d - DEQUANT_STORE m6 + pshufd m6, m6, 0 + DEQUANT_STORE 8, 1 RET %macro IDCT_XMM 1 diff --git a/tests/checkasm/h264dsp.c b/tests/checkasm/h264dsp.c index 6e9792a0f6e..a63c29a2384 100644 --- a/tests/checkasm/h264dsp.c +++ b/tests/checkasm/h264dsp.c @@ -414,10 +414,12 @@ static void check_idct_dequant(void) LOCAL_ALIGNED_16(int32_t, dst0_32, [16 * 16]); LOCAL_ALIGNED_16(int32_t, dst1_32, [16 * 16]); H264DSPContext h; - int bit_depth, i, qmul; + int bit_depth, i, qmul[2]; declare_func(void, int16_t *output, int16_t *input, int qmul); - qmul = rnd() % 4096; + qmul[0] = rnd() % 4096; + /* above 32767, up to the largest 8-bit dequant4_coeff (18 * 255 << 10) */ + qmul[1] = 32768 + rnd() % ((18 * 255 << 10) - 32767); for (i = 0; i < FF_ARRAY_ELEMS(depths); i++) { bit_depth = depths[i]; @@ -437,15 +439,15 @@ static void check_idct_dequant(void) for (int j = 0; j < 16; j++) src32[j] = (rnd() % (1 << (bit_depth + 1))) - (1 << bit_depth); } - memset(dst_ref, 0, 16 * 16 * SIZEOF_COEF); - memset(dst_new, 0, 16 * 16 * SIZEOF_COEF); - if (check_func(h.luma_dc_dequant_idct, "luma_dc_dequant_idct_%d", bit_depth)) { - - call_ref(dst_ref, src, qmul); - call_new(dst_new, src, qmul); - checkasm_check_dctcoef(dst0, 16*SIZEOF_COEF, dst1, 16*SIZEOF_COEF, 16, 16, "dst"); - bench_new(dst_new, src, qmul); + for (int j = 0; j < 2; j++) { + memset(dst_ref, 0, 16 * 16 * SIZEOF_COEF); + memset(dst_new, 0, 16 * 16 * SIZEOF_COEF); + call_ref(dst_ref, src, qmul[j]); + call_new(dst_new, src, qmul[j]); + checkasm_check_dctcoef(dst0, 16*SIZEOF_COEF, dst1, 16*SIZEOF_COEF, 16, 16, "dst"); + } + bench_new(dst_new, src, qmul[0]); } } } -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
