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

Transpose the AIC quantization matrix relative to the coefficient layout and 
restore coefficient signs after scaling and shifting their magnitudes. This 
corrects the frequency weights and negative-coefficient rounding, removing 
visible luma ringing. Update both existing AIC FATE references for the 
corrected pixels.

Validation:
- `fate-aic` and `fate-aic-oddsize` pass. Reference changes affect only CRCs; 
frame counts, dimensions, timestamps, sizes, and repeated-frame groups are 
preserved.
- Against Apple output, maximum luma error falls from 14 to 1 for the 90-frame 
progressive golden and from 13 to 1 for the 15-frame interlaced control.
- Generated 470 progressive and 94 interlaced stress frames through 
VideoToolbox, including constant extremes, edges, DCT basis sign patterns, and 
noise. All packets use quantizer 4; Apple's encoder does not support the 
quality property. The largest packed coefficient was 1,023 and the largest 
scaled magnitude was 16,384. No narrowing to 16 bits is needed for these 
samples, and the largest multiplication possible under the decoder's input 
types fits in signed 32 bits.
- Generated samples match Apple within one code value after comparison-only 
output-range clipping and, for interlaced chroma, accounting for Apple's 
even-row duplication. Those output transformations and chroma-location metadata 
are unchanged by this patch.



>From 658cf95426ff4bea8c4720f834d9d28714e65f91 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Thu, 26 Nov 2026 12:49:09 -0800
Subject: [PATCH] avcodec/aic: fix coefficient dequantization

---
 libavcodec/aic.c           |   9 ++-
 tests/ref/fate/aic         |  30 +++++-----
 tests/ref/fate/aic-oddsize | 114 ++++++++++++++++++-------------------
 3 files changed, 78 insertions(+), 75 deletions(-)

diff --git a/libavcodec/aic.c b/libavcodec/aic.c
index e12d689c478..c871134cb2c 100644
--- a/libavcodec/aic.c
+++ b/libavcodec/aic.c
@@ -302,8 +302,9 @@ static void unquant_block(int16_t *block, int q, uint8_t 
*quant_matrix)
         int val  = (uint16_t)block[i];
         int sign = val & 1;
 
-        block[i] = (((val >> 1) ^ -sign) * q * quant_matrix[i] >> 4)
-                   + sign;
+        /* Scale the magnitude before restoring the sign. */
+        val = (((val + 1) >> 1) * q * quant_matrix[i]) >> 4;
+        block[i] = (val ^ -sign) + sign;
     }
 }
 
@@ -449,8 +450,10 @@ static av_cold int aic_decode_init(AVCodecContext *avctx)
 
     ff_idctdsp_init(&ctx->idsp, avctx);
 
+    /* The matrix is stored transposed relative to the coefficient layout. */
     for (i = 0; i < 64; i++)
-        ctx->quant_matrix[ctx->idsp.idct_permutation[i]] = aic_quant_matrix[i];
+        ctx->quant_matrix[ctx->idsp.idct_permutation[i]] =
+            aic_quant_matrix[((i & 7) << 3) | (i >> 3)];
 
     ctx->mb_width  = FFALIGN(avctx->width,  16) >> 4;
     ctx->mb_height = FFALIGN(avctx->height, 16) >> 4;
diff --git a/tests/ref/fate/aic b/tests/ref/fate/aic
index 30197129f7c..9ad4d0fc65d 100644
--- a/tests/ref/fate/aic
+++ b/tests/ref/fate/aic
@@ -3,18 +3,18 @@
 #codec_id 0: rawvideo
 #dimensions 0: 1416x1062
 #sar 0: 4/3
-0,          0,          0,        1,  2255688, 0x48f3973d
-0,          1,          1,        1,  2255688, 0x48f3973d
-0,          2,          2,        1,  2255688, 0x8c9f3c83
-0,          3,          3,        1,  2255688, 0xb89c02e3
-0,          4,          4,        1,  2255688, 0x185b1a84
-0,          5,          5,        1,  2255688, 0x82167715
-0,          6,          6,        1,  2255688, 0xe675971e
-0,          7,          7,        1,  2255688, 0x623759db
-0,          8,          8,        1,  2255688, 0x910274ad
-0,          9,          9,        1,  2255688, 0x0eafce15
-0,         10,         10,        1,  2255688, 0x4457b006
-0,         11,         11,        1,  2255688, 0x48f70e0b
-0,         12,         12,        1,  2255688, 0xfd269c3d
-0,         13,         13,        1,  2255688, 0x4c6258ad
-0,         14,         14,        1,  2255688, 0xb131c4af
+0,          0,          0,        1,  2255688, 0x9e805fba
+0,          1,          1,        1,  2255688, 0x9e805fba
+0,          2,          2,        1,  2255688, 0xd6b90bc6
+0,          3,          3,        1,  2255688, 0x7f18e173
+0,          4,          4,        1,  2255688, 0x61980514
+0,          5,          5,        1,  2255688, 0x70dd641a
+0,          6,          6,        1,  2255688, 0x5a6b9018
+0,          7,          7,        1,  2255688, 0x087c55c0
+0,          8,          8,        1,  2255688, 0xeace704c
+0,          9,          9,        1,  2255688, 0xea7ac54d
+0,         10,         10,        1,  2255688, 0x8c0d9c6e
+0,         11,         11,        1,  2255688, 0x7997e9cf
+0,         12,         12,        1,  2255688, 0x080b65c4
+0,         13,         13,        1,  2255688, 0x725b10f3
+0,         14,         14,        1,  2255688, 0x07e074a2
diff --git a/tests/ref/fate/aic-oddsize b/tests/ref/fate/aic-oddsize
index be4346a2043..9ab5d8fd565 100644
--- a/tests/ref/fate/aic-oddsize
+++ b/tests/ref/fate/aic-oddsize
@@ -3,60 +3,60 @@
 #codec_id 0: rawvideo
 #dimensions 0: 481x241
 #sar 0: 0/1
-0,          0,          0,        1,   174243, 0x15ab835b
-0,          1,          1,        1,   174243, 0x12d3acd0
-0,          2,          2,        1,   174243, 0x12d3acd0
-0,          3,          3,        1,   174243, 0x12d3acd0
-0,          4,          4,        1,   174243, 0x12d3acd0
-0,          5,          5,        1,   174243, 0x12d3acd0
-0,          6,          6,        1,   174243, 0x12d3acd0
-0,          7,          7,        1,   174243, 0x12d3acd0
-0,          8,          8,        1,   174243, 0x12d3acd0
-0,          9,          9,        1,   174243, 0xb21561b4
-0,         10,         10,        1,   174243, 0x49343b5b
-0,         11,         11,        1,   174243, 0x9bd65f49
-0,         12,         12,        1,   174243, 0xed3b9960
-0,         13,         13,        1,   174243, 0x14f70294
-0,         14,         14,        1,   174243, 0x14f70294
-0,         15,         15,        1,   174243, 0x14f70294
-0,         16,         16,        1,   174243, 0x92ac2316
-0,         17,         17,        1,   174243, 0x12729ac0
-0,         18,         18,        1,   174243, 0x6db0bfbd
-0,         19,         19,        1,   174243, 0x6db0bfbd
-0,         20,         20,        1,   174243, 0x6db0bfbd
-0,         21,         21,        1,   174243, 0x6db0bfbd
-0,         22,         22,        1,   174243, 0xd66cbef7
-0,         23,         23,        1,   174243, 0xd66cbef7
-0,         24,         24,        1,   174243, 0xd66cbef7
-0,         25,         25,        1,   174243, 0xd66cbef7
-0,         26,         26,        1,   174243, 0x3314c3d3
-0,         27,         27,        1,   174243, 0x34efb358
-0,         28,         28,        1,   174243, 0x7307d8c8
-0,         29,         29,        1,   174243, 0x80b57ea1
-0,         30,         30,        1,   174243, 0xb0b51bf9
-0,         31,         31,        1,   174243, 0x60c07516
-0,         32,         32,        1,   174243, 0xcc35033f
-0,         33,         33,        1,   174243, 0x2bf12dc4
-0,         34,         34,        1,   174243, 0xf3025eb7
-0,         35,         35,        1,   174243, 0xf3025eb7
-0,         36,         36,        1,   174243, 0xf3025eb7
-0,         37,         37,        1,   174243, 0xf3025eb7
-0,         38,         38,        1,   174243, 0x06e761b3
-0,         39,         39,        1,   174243, 0xa21152fb
-0,         40,         40,        1,   174243, 0xd6dcc575
-0,         41,         41,        1,   174243, 0xd6ceb82a
-0,         42,         42,        1,   174243, 0x20efc206
-0,         43,         43,        1,   174243, 0x20efc206
-0,         44,         44,        1,   174243, 0x20efc206
-0,         45,         45,        1,   174243, 0x20efc206
-0,         46,         46,        1,   174243, 0xba03c1f5
-0,         47,         47,        1,   174243, 0xba03c1f5
-0,         48,         48,        1,   174243, 0xba03c1f5
-0,         49,         49,        1,   174243, 0xba03c1f5
-0,         50,         50,        1,   174243, 0x4cd1c214
-0,         51,         51,        1,   174243, 0x4cd1c214
-0,         52,         52,        1,   174243, 0x4cd1c214
-0,         53,         53,        1,   174243, 0xa288c1ef
-0,         54,         54,        1,   174243, 0xa288c1ef
-0,         55,         55,        1,   174243, 0xa288c1ef
-0,         56,         56,        1,   174243, 0xa288c1ef
+0,          0,          0,        1,   174243, 0xe3458153
+0,          1,          1,        1,   174243, 0x4839aacd
+0,          2,          2,        1,   174243, 0x4839aacd
+0,          3,          3,        1,   174243, 0x4839aacd
+0,          4,          4,        1,   174243, 0x4839aacd
+0,          5,          5,        1,   174243, 0x4839aacd
+0,          6,          6,        1,   174243, 0x4839aacd
+0,          7,          7,        1,   174243, 0x4839aacd
+0,          8,          8,        1,   174243, 0x4839aacd
+0,          9,          9,        1,   174243, 0xfd275cd9
+0,         10,         10,        1,   174243, 0xe6bb36a1
+0,         11,         11,        1,   174243, 0x8c855cd6
+0,         12,         12,        1,   174243, 0x1f129490
+0,         13,         13,        1,   174243, 0x27e7fc1d
+0,         14,         14,        1,   174243, 0x27e7fc1d
+0,         15,         15,        1,   174243, 0x27e7fc1d
+0,         16,         16,        1,   174243, 0x771124e3
+0,         17,         17,        1,   174243, 0x8d509cb7
+0,         18,         18,        1,   174243, 0xb4f1c3ec
+0,         19,         19,        1,   174243, 0xb4f1c3ec
+0,         20,         20,        1,   174243, 0xb4f1c3ec
+0,         21,         21,        1,   174243, 0xb4f1c3ec
+0,         22,         22,        1,   174243, 0xd88fc370
+0,         23,         23,        1,   174243, 0xd88fc370
+0,         24,         24,        1,   174243, 0xd88fc370
+0,         25,         25,        1,   174243, 0xd88fc370
+0,         26,         26,        1,   174243, 0xfc42c817
+0,         27,         27,        1,   174243, 0x967eb7d5
+0,         28,         28,        1,   174243, 0xd4d9d223
+0,         29,         29,        1,   174243, 0xc0178143
+0,         30,         30,        1,   174243, 0xe2991ab9
+0,         31,         31,        1,   174243, 0xfc417509
+0,         32,         32,        1,   174243, 0x4ca104fb
+0,         33,         33,        1,   174243, 0x460b2e0b
+0,         34,         34,        1,   174243, 0x0c7e53c8
+0,         35,         35,        1,   174243, 0x0c7e53c8
+0,         36,         36,        1,   174243, 0x0c7e53c8
+0,         37,         37,        1,   174243, 0x0c7e53c8
+0,         38,         38,        1,   174243, 0x7d1c54d8
+0,         39,         39,        1,   174243, 0x88a050c2
+0,         40,         40,        1,   174243, 0x15ecbe1f
+0,         41,         41,        1,   174243, 0x2d9aba1c
+0,         42,         42,        1,   174243, 0xabb7c12e
+0,         43,         43,        1,   174243, 0xabb7c12e
+0,         44,         44,        1,   174243, 0xabb7c12e
+0,         45,         45,        1,   174243, 0xabb7c12e
+0,         46,         46,        1,   174243, 0x98a9c167
+0,         47,         47,        1,   174243, 0x98a9c167
+0,         48,         48,        1,   174243, 0x98a9c167
+0,         49,         49,        1,   174243, 0x98a9c167
+0,         50,         50,        1,   174243, 0x70dbc181
+0,         51,         51,        1,   174243, 0x70dbc181
+0,         52,         52,        1,   174243, 0x70dbc181
+0,         53,         53,        1,   174243, 0xea25c168
+0,         54,         54,        1,   174243, 0xea25c168
+0,         55,         55,        1,   174243, 0xea25c168
+0,         56,         56,        1,   174243, 0xea25c168
-- 
2.54.1

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

Reply via email to