Dequantization and the inverse DCT
Browse files at this commit6a838332e78e748ce3508662733eb2f44d7521bfParent: d563e2c- feat
- dequantization and the inverse DCT
5 changed files +871 -0
- crates/ec-av1/src/lib.rs +2 -0
- crates/ec-av1/src/quant.rs +215 -0
- crates/ec-av1/src/tile.rs +260 -0
- crates/ec-av1/src/transform.rs +344 -0
- lanes/av1-fdct-r1.report.md +50 -0
crates/ec-av1/src/lib.rsmodified+2 -0
| @@ -17,5 +17,7 @@ pub mod cdf; | |||
| 17 | pub mod frame; | 17 | pub mod frame; |
| 18 | pub mod msac; | 18 | pub mod msac; |
| 19 | pub mod obu; | 19 | pub mod obu; |
| 20 | +pub mod quant; | ||
| 20 | pub mod sequence; | 21 | pub mod sequence; |
| 21 | pub mod tile; | 22 | pub mod tile; |
| 23 | +pub mod transform; | ||
crates/ec-av1/src/quant.rsadded+215 -0
| @@ -0,0 +1,215 @@ | |||
| 1 | +//! Dequantization lookups (spec 7.12.2). | ||
| 2 | +//! | ||
| 3 | +//! AV1 reuses VP9's quantizer tables verbatim, and they are vendored here | ||
| 4 | +//! rather than borrowed from `ec-vp9-syntax` so that the AV1 encoder does not | ||
| 5 | +//! depend on a VP9 crate for a table its own spec prints. Both are indexed | ||
| 6 | +//! `[(BitDepth - 8) >> 1][Clip3(0, 255, b)]`. | ||
| 7 | + | ||
| 8 | +/// `dc_qlookup` (spec 8.6.1), for 8-, 10- and 12-bit. | ||
| 9 | +static DC_QLOOKUP: [[i16; 256]; 3] = [ | ||
| 10 | + [ | ||
| 11 | + 4, 8, 8, 9, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 26, | ||
| 12 | + 27, 28, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, 43, 43, 44, 45, 46, | ||
| 13 | + 47, 48, 48, 49, 50, 51, 52, 53, 53, 54, 55, 56, 57, 57, 58, 59, 60, 61, 62, 62, 63, 64, 65, | ||
| 14 | + 66, 66, 67, 68, 69, 70, 70, 71, 72, 73, 74, 74, 75, 76, 77, 78, 78, 79, 80, 81, 81, 82, 83, | ||
| 15 | + 84, 85, 85, 87, 88, 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 108, 110, 111, | ||
| 16 | + 113, 114, 116, 117, 118, 120, 121, 123, 125, 127, 129, 131, 134, 136, 138, 140, 142, 144, | ||
| 17 | + 146, 148, 150, 152, 154, 156, 158, 161, 164, 166, 169, 172, 174, 177, 180, 182, 185, 187, | ||
| 18 | + 190, 192, 195, 199, 202, 205, 208, 211, 214, 217, 220, 223, 226, 230, 233, 237, 240, 243, | ||
| 19 | + 247, 250, 253, 257, 261, 265, 269, 272, 276, 280, 284, 288, 292, 296, 300, 304, 309, 313, | ||
| 20 | + 317, 322, 326, 330, 335, 340, 344, 349, 354, 359, 364, 369, 374, 379, 384, 389, 395, 400, | ||
| 21 | + 406, 411, 417, 423, 429, 435, 441, 447, 454, 461, 467, 475, 482, 489, 497, 505, 513, 522, | ||
| 22 | + 530, 539, 549, 559, 569, 579, 590, 602, 614, 626, 640, 654, 668, 684, 700, 717, 736, 755, | ||
| 23 | + 775, 796, 819, 843, 869, 896, 925, 955, 988, 1022, 1058, 1098, 1139, 1184, 1232, 1282, | ||
| 24 | + 1336, | ||
| 25 | + ], | ||
| 26 | + [ | ||
| 27 | + 4, 9, 10, 13, 15, 17, 20, 22, 25, 28, 31, 34, 37, 40, 43, 47, 50, 53, 57, 60, 64, 68, 71, | ||
| 28 | + 75, 78, 82, 86, 90, 93, 97, 101, 105, 109, 113, 116, 120, 124, 128, 132, 136, 140, 143, | ||
| 29 | + 147, 151, 155, 159, 163, 166, 170, 174, 178, 182, 185, 189, 193, 197, 200, 204, 208, 212, | ||
| 30 | + 215, 219, 223, 226, 230, 233, 237, 241, 244, 248, 251, 255, 259, 262, 266, 269, 273, 276, | ||
| 31 | + 280, 283, 287, 290, 293, 297, 300, 304, 307, 310, 314, 317, 321, 324, 327, 331, 334, 337, | ||
| 32 | + 343, 350, 356, 362, 369, 375, 381, 387, 394, 400, 406, 412, 418, 424, 430, 436, 442, 448, | ||
| 33 | + 454, 460, 466, 472, 478, 484, 490, 499, 507, 516, 525, 533, 542, 550, 559, 567, 576, 584, | ||
| 34 | + 592, 601, 609, 617, 625, 634, 644, 655, 666, 676, 687, 698, 708, 718, 729, 739, 749, 759, | ||
| 35 | + 770, 782, 795, 807, 819, 831, 844, 856, 868, 880, 891, 906, 920, 933, 947, 961, 975, 988, | ||
| 36 | + 1001, 1015, 1030, 1045, 1061, 1076, 1090, 1105, 1120, 1137, 1153, 1170, 1186, 1202, 1218, | ||
| 37 | + 1236, 1253, 1271, 1288, 1306, 1323, 1342, 1361, 1379, 1398, 1416, 1436, 1456, 1476, 1496, | ||
| 38 | + 1516, 1537, 1559, 1580, 1601, 1624, 1647, 1670, 1692, 1717, 1741, 1766, 1791, 1817, 1844, | ||
| 39 | + 1871, 1900, 1929, 1958, 1990, 2021, 2054, 2088, 2123, 2159, 2197, 2236, 2276, 2319, 2363, | ||
| 40 | + 2410, 2458, 2508, 2561, 2616, 2675, 2737, 2802, 2871, 2944, 3020, 3102, 3188, 3280, 3375, | ||
| 41 | + 3478, 3586, 3702, 3823, 3953, 4089, 4236, 4394, 4559, 4737, 4929, 5130, 5347, | ||
| 42 | + ], | ||
| 43 | + [ | ||
| 44 | + 4, 12, 18, 25, 33, 41, 50, 60, 70, 80, 91, 103, 115, 127, 140, 153, 166, 180, 194, 208, | ||
| 45 | + 222, 237, 251, 266, 281, 296, 312, 327, 343, 358, 374, 390, 405, 421, 437, 453, 469, 484, | ||
| 46 | + 500, 516, 532, 548, 564, 580, 596, 611, 627, 643, 659, 674, 690, 706, 721, 737, 752, 768, | ||
| 47 | + 783, 798, 814, 829, 844, 859, 874, 889, 904, 919, 934, 949, 964, 978, 993, 1008, 1022, | ||
| 48 | + 1037, 1051, 1065, 1080, 1094, 1108, 1122, 1136, 1151, 1165, 1179, 1192, 1206, 1220, 1234, | ||
| 49 | + 1248, 1261, 1275, 1288, 1302, 1315, 1329, 1342, 1368, 1393, 1419, 1444, 1469, 1494, 1519, | ||
| 50 | + 1544, 1569, 1594, 1618, 1643, 1668, 1692, 1717, 1741, 1765, 1789, 1814, 1838, 1862, 1885, | ||
| 51 | + 1909, 1933, 1957, 1992, 2027, 2061, 2096, 2130, 2165, 2199, 2233, 2267, 2300, 2334, 2367, | ||
| 52 | + 2400, 2434, 2467, 2499, 2532, 2575, 2618, 2661, 2704, 2746, 2788, 2830, 2872, 2913, 2954, | ||
| 53 | + 2995, 3036, 3076, 3127, 3177, 3226, 3275, 3324, 3373, 3421, 3469, 3517, 3565, 3621, 3677, | ||
| 54 | + 3733, 3788, 3843, 3897, 3951, 4005, 4058, 4119, 4181, 4241, 4301, 4361, 4420, 4479, 4546, | ||
| 55 | + 4612, 4677, 4742, 4807, 4871, 4942, 5013, 5083, 5153, 5222, 5291, 5367, 5442, 5517, 5591, | ||
| 56 | + 5665, 5745, 5825, 5905, 5984, 6063, 6149, 6234, 6319, 6404, 6495, 6587, 6678, 6769, 6867, | ||
| 57 | + 6966, 7064, 7163, 7269, 7376, 7483, 7599, 7715, 7832, 7958, 8085, 8214, 8352, 8492, 8635, | ||
| 58 | + 8788, 8945, 9104, 9275, 9450, 9639, 9832, 10031, 10245, 10465, 10702, 10946, 11210, 11482, | ||
| 59 | + 11776, 12081, 12409, 12750, 13118, 13501, 13913, 14343, 14807, 15290, 15812, 16356, 16943, | ||
| 60 | + 17575, 18237, 18949, 19718, 20521, 21387, | ||
| 61 | + ], | ||
| 62 | +]; | ||
| 63 | + | ||
| 64 | +/// `ac_qlookup` (spec 8.6.1), for 8-, 10- and 12-bit. | ||
| 65 | +static AC_QLOOKUP: [[i16; 256]; 3] = [ | ||
| 66 | + [ | ||
| 67 | + 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, | ||
| 68 | + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, | ||
| 69 | + 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, | ||
| 70 | + 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, | ||
| 71 | + 99, 100, 101, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, | ||
| 72 | + 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 155, 158, 161, 164, 167, 170, 173, | ||
| 73 | + 176, 179, 182, 185, 188, 191, 194, 197, 200, 203, 207, 211, 215, 219, 223, 227, 231, 235, | ||
| 74 | + 239, 243, 247, 251, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300, 305, 311, 317, 323, | ||
| 75 | + 329, 335, 341, 347, 353, 359, 366, 373, 380, 387, 394, 401, 408, 416, 424, 432, 440, 448, | ||
| 76 | + 456, 465, 474, 483, 492, 501, 510, 520, 530, 540, 550, 560, 571, 582, 593, 604, 615, 627, | ||
| 77 | + 639, 651, 663, 676, 689, 702, 715, 729, 743, 757, 771, 786, 801, 816, 832, 848, 864, 881, | ||
| 78 | + 898, 915, 933, 951, 969, 988, 1007, 1026, 1046, 1066, 1087, 1108, 1129, 1151, 1173, 1196, | ||
| 79 | + 1219, 1243, 1267, 1292, 1317, 1343, 1369, 1396, 1423, 1451, 1479, 1508, 1537, 1567, 1597, | ||
| 80 | + 1628, 1660, 1692, 1725, 1759, 1793, 1828, | ||
| 81 | + ], | ||
| 82 | + [ | ||
| 83 | + 4, 9, 11, 13, 16, 18, 21, 24, 27, 30, 33, 37, 40, 44, 48, 51, 55, 59, 63, 67, 71, 75, 79, | ||
| 84 | + 83, 88, 92, 96, 100, 105, 109, 114, 118, 122, 127, 131, 136, 140, 145, 149, 154, 158, 163, | ||
| 85 | + 168, 172, 177, 181, 186, 190, 195, 199, 204, 208, 213, 217, 222, 226, 231, 235, 240, 244, | ||
| 86 | + 249, 253, 258, 262, 267, 271, 275, 280, 284, 289, 293, 297, 302, 306, 311, 315, 319, 324, | ||
| 87 | + 328, 332, 337, 341, 345, 349, 354, 358, 362, 367, 371, 375, 379, 384, 388, 392, 396, 401, | ||
| 88 | + 409, 417, 425, 433, 441, 449, 458, 466, 474, 482, 490, 498, 506, 514, 523, 531, 539, 547, | ||
| 89 | + 555, 563, 571, 579, 588, 596, 604, 616, 628, 640, 652, 664, 676, 688, 700, 713, 725, 737, | ||
| 90 | + 749, 761, 773, 785, 797, 809, 825, 841, 857, 873, 889, 905, 922, 938, 954, 970, 986, 1002, | ||
| 91 | + 1018, 1038, 1058, 1078, 1098, 1118, 1138, 1158, 1178, 1198, 1218, 1242, 1266, 1290, 1314, | ||
| 92 | + 1338, 1362, 1386, 1411, 1435, 1463, 1491, 1519, 1547, 1575, 1603, 1631, 1663, 1695, 1727, | ||
| 93 | + 1759, 1791, 1823, 1859, 1895, 1931, 1967, 2003, 2039, 2079, 2119, 2159, 2199, 2239, 2283, | ||
| 94 | + 2327, 2371, 2415, 2459, 2507, 2555, 2603, 2651, 2703, 2755, 2807, 2859, 2915, 2971, 3027, | ||
| 95 | + 3083, 3143, 3203, 3263, 3327, 3391, 3455, 3523, 3591, 3659, 3731, 3803, 3876, 3952, 4028, | ||
| 96 | + 4104, 4184, 4264, 4348, 4432, 4516, 4604, 4692, 4784, 4876, 4972, 5068, 5168, 5268, 5372, | ||
| 97 | + 5476, 5584, 5692, 5804, 5916, 6032, 6148, 6268, 6388, 6512, 6640, 6768, 6900, 7036, 7172, | ||
| 98 | + 7312, | ||
| 99 | + ], | ||
| 100 | + [ | ||
| 101 | + 4, 13, 19, 27, 35, 44, 54, 64, 75, 87, 99, 112, 126, 139, 154, 168, 183, 199, 214, 230, | ||
| 102 | + 247, 263, 280, 297, 314, 331, 349, 366, 384, 402, 420, 438, 456, 475, 493, 511, 530, 548, | ||
| 103 | + 567, 586, 604, 623, 642, 660, 679, 698, 716, 735, 753, 772, 791, 809, 828, 846, 865, 884, | ||
| 104 | + 902, 920, 939, 957, 976, 994, 1012, 1030, 1049, 1067, 1085, 1103, 1121, 1139, 1157, 1175, | ||
| 105 | + 1193, 1211, 1229, 1246, 1264, 1282, 1299, 1317, 1335, 1352, 1370, 1387, 1405, 1422, 1440, | ||
| 106 | + 1457, 1474, 1491, 1509, 1526, 1543, 1560, 1577, 1595, 1627, 1660, 1693, 1725, 1758, 1791, | ||
| 107 | + 1824, 1856, 1889, 1922, 1954, 1987, 2020, 2052, 2085, 2118, 2150, 2183, 2216, 2248, 2281, | ||
| 108 | + 2313, 2346, 2378, 2411, 2459, 2508, 2556, 2605, 2653, 2701, 2750, 2798, 2847, 2895, 2943, | ||
| 109 | + 2992, 3040, 3088, 3137, 3185, 3234, 3298, 3362, 3426, 3491, 3555, 3619, 3684, 3748, 3812, | ||
| 110 | + 3876, 3941, 4005, 4069, 4149, 4230, 4310, 4390, 4470, 4550, 4631, 4711, 4791, 4871, 4967, | ||
| 111 | + 5064, 5160, 5256, 5352, 5448, 5544, 5641, 5737, 5849, 5961, 6073, 6185, 6297, 6410, 6522, | ||
| 112 | + 6650, 6778, 6906, 7034, 7162, 7290, 7435, 7579, 7723, 7867, 8011, 8155, 8315, 8475, 8635, | ||
| 113 | + 8795, 8956, 9132, 9308, 9484, 9660, 9836, 10028, 10220, 10412, 10604, 10812, 11020, 11228, | ||
| 114 | + 11437, 11661, 11885, 12109, 12333, 12573, 12813, 13053, 13309, 13565, 13821, 14093, 14365, | ||
| 115 | + 14637, 14925, 15213, 15502, 15806, 16110, 16414, 16734, 17054, 17390, 17726, 18062, 18414, | ||
| 116 | + 18766, 19134, 19502, 19886, 20270, 20670, 21070, 21486, 21902, 22334, 22766, 23214, 23662, | ||
| 117 | + 24126, 24590, 25070, 25551, 26047, 26559, 27071, 27599, 28143, 28687, 29247, | ||
| 118 | + ], | ||
| 119 | +]; | ||
| 120 | + | ||
| 121 | +/// The bit depth's row in both tables, or `None` for a depth AV1 cannot code. | ||
| 122 | +fn depth_row(bit_depth: u8) -> Option<usize> { | ||
| 123 | + match bit_depth { | ||
| 124 | + 8 => Some(0), | ||
| 125 | + 10 => Some(1), | ||
| 126 | + 12 => Some(2), | ||
| 127 | + _ => None, | ||
| 128 | + } | ||
| 129 | +} | ||
| 130 | + | ||
| 131 | +/// `dc_q(b)` (spec 7.12.2): the DC dequantization scale for quantizer index `b`. | ||
| 132 | +/// | ||
| 133 | +/// Out-of-range indices are clamped the way the spec clamps them; an | ||
| 134 | +/// impossible bit depth falls back to the 8-bit row rather than panicking. | ||
| 135 | +pub fn dc_q(bit_depth: u8, b: i32) -> i32 { | ||
| 136 | + i32::from(DC_QLOOKUP[depth_row(bit_depth).unwrap_or(0)][b.clamp(0, 255) as usize]) | ||
| 137 | +} | ||
| 138 | + | ||
| 139 | +/// `ac_q(b)` (spec 7.12.2): the AC dequantization scale for quantizer index `b`. | ||
| 140 | +pub fn ac_q(bit_depth: u8, b: i32) -> i32 { | ||
| 141 | + i32::from(AC_QLOOKUP[depth_row(bit_depth).unwrap_or(0)][b.clamp(0, 255) as usize]) | ||
| 142 | +} | ||
| 143 | + | ||
| 144 | +/// `dqDenom` (spec 7.12.3): what the dequantized coefficient is divided by, | ||
| 145 | +/// which is driven by the transform's area rather than its side. | ||
| 146 | +pub fn dq_denom(side: usize) -> i64 { | ||
| 147 | + match side * side { | ||
| 148 | + 0..=256 => 1, | ||
| 149 | + 257..=1024 => 2, | ||
| 150 | + _ => 4, | ||
| 151 | + } | ||
| 152 | +} | ||
| 153 | + | ||
| 154 | +/// Dequantize one coefficient (spec 7.12.3 steps a-f): scale by the DC or AC | ||
| 155 | +/// quantizer, mask to 24 bits, divide by the transform's denominator toward | ||
| 156 | +/// zero, and clip to the coefficient range. | ||
| 157 | +pub fn dequant_coeff(level: i32, is_dc: bool, bit_depth: u8, q_idx: i32, side: usize) -> i32 { | ||
| 158 | + let q = i64::from(if is_dc { | ||
| 159 | + dc_q(bit_depth, q_idx) | ||
| 160 | + } else { | ||
| 161 | + ac_q(bit_depth, q_idx) | ||
| 162 | + }); | ||
| 163 | + let dq = i64::from(level) * q; | ||
| 164 | + let sign = if dq < 0 { -1 } else { 1 }; | ||
| 165 | + let dq2 = sign * ((dq.abs() & 0xFF_FFFF) / dq_denom(side)); | ||
| 166 | + let bound = 1i64 << (7 + u32::from(bit_depth)); | ||
| 167 | + dq2.clamp(-bound, bound - 1) as i32 | ||
| 168 | +} | ||
| 169 | + | ||
| 170 | +/// Dequantize a whole raster-order coefficient grid of a `side` x `side` | ||
| 171 | +/// transform. | ||
| 172 | +pub fn dequant(levels: &[i32], side: usize, bit_depth: u8, q_idx: i32) -> Vec<i32> { | ||
| 173 | + levels | ||
| 174 | + .iter() | ||
| 175 | + .enumerate() | ||
| 176 | + .map(|(i, &l)| dequant_coeff(l, i == 0, bit_depth, q_idx, side)) | ||
| 177 | + .collect() | ||
| 178 | +} | ||
| 179 | + | ||
| 180 | +#[cfg(test)] | ||
| 181 | +mod tests { | ||
| 182 | + use super::*; | ||
| 183 | + | ||
| 184 | + /// Anchors from the spec tables, and the clamping at both edges. | ||
| 185 | + #[test] | ||
| 186 | + fn table_anchors() { | ||
| 187 | + assert_eq!((dc_q(8, 0), ac_q(8, 0)), (4, 4)); | ||
| 188 | + assert_eq!((dc_q(8, 255), ac_q(8, 255)), (1336, 1828)); | ||
| 189 | + assert_eq!((dc_q(8, 100), ac_q(8, 100)), (93, 112)); | ||
| 190 | + assert_eq!(dc_q(8, -5), dc_q(8, 0)); | ||
| 191 | + assert_eq!(ac_q(8, 9999), ac_q(8, 255)); | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + /// The denominator is an area threshold, not a side one: 16x16 divides by | ||
| 195 | + /// one, 32x32 by two and 64x64 by four. | ||
| 196 | + #[test] | ||
| 197 | + fn denominator_follows_the_area() { | ||
| 198 | + assert_eq!([4, 8, 16, 32, 64].map(dq_denom), [1, 1, 1, 2, 4]); | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + /// Negative levels round toward zero, so a level and its negation | ||
| 202 | + /// dequantize to values that differ only in sign. | ||
| 203 | + #[test] | ||
| 204 | + fn dequant_is_symmetric_in_sign() { | ||
| 205 | + for side in [32, 64] { | ||
| 206 | + for level in 1..20 { | ||
| 207 | + assert_eq!( | ||
| 208 | + dequant_coeff(level, false, 8, 100, side), | ||
| 209 | + -dequant_coeff(-level, false, 8, 100, side), | ||
| 210 | + "side {side} level {level}" | ||
| 211 | + ); | ||
| 212 | + } | ||
| 213 | + } | ||
| 214 | + } | ||
| 215 | +} | ||
crates/ec-av1/src/tile.rsmodified+260 -0
| @@ -2537,4 +2537,264 @@ mod tests { | |||
| 2537 | } | 2537 | } |
| 2538 | } | 2538 | } |
| 2539 | } | 2539 | } |
| 2540 | + | ||
| 2541 | + /// The whole point of the inverse transform living in the encoder is that | ||
| 2542 | + /// it predicts the decoder without asking one. The residual a DC-only | ||
| 2543 | + /// 64x64 transform produces has already been pinned from a real decoder in | ||
| 2544 | + /// [`DECODED_AT_Q100`], so that table is the gate: every level in it, both | ||
| 2545 | + /// signs, has to come back out of `dequant_and_inverse` exactly. | ||
| 2546 | + #[test] | ||
| 2547 | + fn the_inverse_transform_reproduces_the_pinned_whole_superblock_residuals() { | ||
| 2548 | + for (level, _) in DECODED_AT_Q100 { | ||
| 2549 | + let mut levels = vec![0i32; 64 * 64]; | ||
| 2550 | + levels[0] = level; | ||
| 2551 | + let residual = crate::transform::dequant_and_inverse(&levels, 64, 8, i32::from(Q_IDX)); | ||
| 2552 | + let want = dc_residual(level); | ||
| 2553 | + assert!( | ||
| 2554 | + residual.iter().all(|&r| r == want), | ||
| 2555 | + "level {level}: want a flat {want}, got {}..{} (first {})", | ||
| 2556 | + residual.iter().min().unwrap(), | ||
| 2557 | + residual.iter().max().unwrap(), | ||
| 2558 | + residual[0] | ||
| 2559 | + ); | ||
| 2560 | + } | ||
| 2561 | + } | ||
| 2562 | + | ||
| 2563 | + /// The same gate at the other transform size the encoder codes: a split | ||
| 2564 | + /// superblock's 32x32 blocks, whose residuals are pinned in | ||
| 2565 | + /// [`SPLIT_RESIDUAL_AT_Q100`]. The two sizes divide the dequantized | ||
| 2566 | + /// coefficient by different denominators, so a size-blind dequantizer | ||
| 2567 | + /// passes one table and fails the other. | ||
| 2568 | + #[test] | ||
| 2569 | + fn the_inverse_transform_reproduces_the_pinned_split_residuals() { | ||
| 2570 | + for (level, want) in SPLIT_RESIDUAL_AT_Q100 { | ||
| 2571 | + let mut levels = vec![0i32; 32 * 32]; | ||
| 2572 | + levels[0] = level; | ||
| 2573 | + let residual = crate::transform::dequant_and_inverse(&levels, 32, 8, i32::from(Q_IDX)); | ||
| 2574 | + assert!( | ||
| 2575 | + residual.iter().all(|&r| r == want), | ||
| 2576 | + "level {level}: want a flat {want}, got {}..{} (first {})", | ||
| 2577 | + residual.iter().min().unwrap(), | ||
| 2578 | + residual.iter().max().unwrap(), | ||
| 2579 | + residual[0] | ||
| 2580 | + ); | ||
| 2581 | + } | ||
| 2582 | + } | ||
| 2583 | + | ||
| 2584 | + /// A DC-only transform is flat, which is exactly the case that cannot tell | ||
| 2585 | + /// a transposed or mis-permuted butterfly network from a correct one. An AC | ||
| 2586 | + /// coefficient in the first row must vary along the row and stay constant | ||
| 2587 | + /// down each column, and the one in the first column the other way round; | ||
| 2588 | + /// a transposed network swaps the two. | ||
| 2589 | + #[test] | ||
| 2590 | + fn a_single_ac_coefficient_varies_along_its_own_axis() { | ||
| 2591 | + for side in [4, 8, 16, 32, 64] { | ||
| 2592 | + let mut horizontal = vec![0i32; side * side]; | ||
| 2593 | + horizontal[1] = 100; | ||
| 2594 | + let h = crate::transform::dequant_and_inverse(&horizontal, side, 8, i32::from(Q_IDX)); | ||
| 2595 | + let mut vertical = vec![0i32; side * side]; | ||
| 2596 | + vertical[side] = 100; | ||
| 2597 | + let v = crate::transform::dequant_and_inverse(&vertical, side, 8, i32::from(Q_IDX)); | ||
| 2598 | + for row in 0..side { | ||
| 2599 | + for col in 0..side { | ||
| 2600 | + assert_eq!( | ||
| 2601 | + h[row * side + col], | ||
| 2602 | + h[col], | ||
| 2603 | + "{side}: a first-row coefficient is constant down column {col}" | ||
| 2604 | + ); | ||
| 2605 | + assert_eq!( | ||
| 2606 | + v[row * side + col], | ||
| 2607 | + v[row * side], | ||
| 2608 | + "{side}: a first-column coefficient is constant along row {row}" | ||
| 2609 | + ); | ||
| 2610 | + } | ||
| 2611 | + } | ||
| 2612 | + // The two are each other's transpose. Only to within a rounding | ||
| 2613 | + // step: the spec rounds halves up rather than away from zero, so a | ||
| 2614 | + // basis function and its transpose can land a count apart where | ||
| 2615 | + // the row and column passes round opposite ways. | ||
| 2616 | + assert!(h[0] != h[side - 1], "{side}: the horizontal basis varies"); | ||
| 2617 | + for row in 0..side { | ||
| 2618 | + for col in 0..side { | ||
| 2619 | + let (a, b) = (h[row * side + col], v[col * side + row]); | ||
| 2620 | + assert!( | ||
| 2621 | + (a - b).abs() <= 1, | ||
| 2622 | + "{side}: transpose at ({row},{col}): {a} vs {b}" | ||
| 2623 | + ); | ||
| 2624 | + } | ||
| 2625 | + } | ||
| 2626 | + } | ||
| 2627 | + } | ||
| 2628 | + | ||
| 2629 | + /// Negating every level negates every residual, to within the one count | ||
| 2630 | + /// the spec's round-halves-up leaves behind. A dequantizer that rounded a | ||
| 2631 | + /// negative coefficient away from zero instead of toward it would shift a | ||
| 2632 | + /// whole basis function, not a count of it, so the bound is what makes | ||
| 2633 | + /// this a test rather than a restatement. | ||
| 2634 | + #[test] | ||
| 2635 | + fn negating_the_levels_negates_the_residual() { | ||
| 2636 | + for side in [32, 64] { | ||
| 2637 | + for level in [1, 2, 3, 5, 9, 14, 40, 100] { | ||
| 2638 | + let mut levels = vec![0i32; side * side]; | ||
| 2639 | + levels[0] = level; | ||
| 2640 | + levels[1] = -level; | ||
| 2641 | + levels[side + 1] = level * 2; | ||
| 2642 | + let pos = crate::transform::dequant_and_inverse(&levels, side, 8, i32::from(Q_IDX)); | ||
| 2643 | + let negated: Vec<i32> = levels.iter().map(|&l| -l).collect(); | ||
| 2644 | + let neg = | ||
| 2645 | + crate::transform::dequant_and_inverse(&negated, side, 8, i32::from(Q_IDX)); | ||
| 2646 | + let worst = pos | ||
| 2647 | + .iter() | ||
| 2648 | + .zip(&neg) | ||
| 2649 | + .map(|(&p, &n)| (p + n).abs()) | ||
| 2650 | + .max() | ||
| 2651 | + .expect("the block is not empty"); | ||
| 2652 | + assert!(worst <= 1, "side {side} level {level}: off by {worst}"); | ||
| 2653 | + } | ||
| 2654 | + } | ||
| 2655 | + } | ||
| 2656 | + | ||
| 2657 | + /// The pinned tables above are DC-only, and a flat residual cannot tell a | ||
| 2658 | + /// correct butterfly network from one that is merely correct at DC. This | ||
| 2659 | + /// is the same claim against a real decoder and with real AC content: the | ||
| 2660 | + /// top-left block of a key frame predicts a flat mid-grey with no | ||
| 2661 | + /// neighbours to read, so what ffmpeg shows there is exactly 128 plus the | ||
| 2662 | + /// residual the encoder's own inverse transform computes. Every sample has | ||
| 2663 | + /// to agree, not most of them. | ||
| 2664 | + #[test] | ||
| 2665 | + fn the_inverse_transform_predicts_what_ffmpeg_decodes_for_a_32x32_block() { | ||
| 2666 | + if !have_ffmpeg() { | ||
| 2667 | + eprintln!( | ||
| 2668 | + "SKIP the_inverse_transform_predicts_what_ffmpeg_decodes_for_a_32x32_block: no ffmpeg" | ||
| 2669 | + ); | ||
| 2670 | + return; | ||
| 2671 | + } | ||
| 2672 | + // A DC, two low-frequency terms of each orientation, a diagonal and a | ||
| 2673 | + // far corner, with both signs among them. | ||
| 2674 | + let coeffs = [ | ||
| 2675 | + (0u8, 0u8, 9i32), | ||
| 2676 | + (0, 1, -14), | ||
| 2677 | + (1, 0, 7), | ||
| 2678 | + (0, 3, 5), | ||
| 2679 | + (3, 0, -3), | ||
| 2680 | + (2, 2, 11), | ||
| 2681 | + (7, 5, -6), | ||
| 2682 | + (31, 31, 4), | ||
| 2683 | + ]; | ||
| 2684 | + let block: BlockCoeffs = coeffs | ||
| 2685 | + .iter() | ||
| 2686 | + .map(|&(row, col, level)| Coeff { row, col, level }) | ||
| 2687 | + .collect::<Vec<_>>() | ||
| 2688 | + .into(); | ||
| 2689 | + let rows = decode_luma_at( | ||
| 2690 | + 64, | ||
| 2691 | + 64, | ||
| 2692 | + &[ | ||
| 2693 | + block, | ||
| 2694 | + BlockCoeffs::default(), | ||
| 2695 | + BlockCoeffs::default(), | ||
| 2696 | + BlockCoeffs::default(), | ||
| 2697 | + ], | ||
| 2698 | + ); | ||
| 2699 | + | ||
| 2700 | + let mut levels = vec![0i32; 32 * 32]; | ||
| 2701 | + for &(row, col, level) in &coeffs { | ||
| 2702 | + levels[usize::from(row) * 32 + usize::from(col)] = level; | ||
| 2703 | + } | ||
| 2704 | + let residual = crate::transform::dequant_and_inverse(&levels, 32, 8, i32::from(Q_IDX)); | ||
| 2705 | + for row in 0..32 { | ||
| 2706 | + for col in 0..32 { | ||
| 2707 | + let want = (128 + residual[row * 32 + col]).clamp(0, 255); | ||
| 2708 | + assert_eq!( | ||
| 2709 | + i32::from(rows[row][col]), | ||
| 2710 | + want, | ||
| 2711 | + "({row},{col}): ffmpeg decoded {} where the encoder predicted {want}", | ||
| 2712 | + rows[row][col] | ||
| 2713 | + ); | ||
| 2714 | + } | ||
| 2715 | + } | ||
| 2716 | + // The block is not flat, so the agreement is about the transform and | ||
| 2717 | + // not about a prediction both sides got trivially right. | ||
| 2718 | + let (lo, hi) = ( | ||
| 2719 | + rows[..32].iter().flat_map(|r| &r[..32]).min().unwrap(), | ||
| 2720 | + rows[..32].iter().flat_map(|r| &r[..32]).max().unwrap(), | ||
| 2721 | + ); | ||
| 2722 | + assert!(hi - lo > 4, "the block carries real detail, got {lo}..{hi}"); | ||
| 2723 | + } | ||
| 2724 | + | ||
| 2725 | + /// The same against the other transform size, the 64x64 one a whole | ||
| 2726 | + /// superblock carries. Only its top-left 32x32 can hold coefficients, and | ||
| 2727 | + /// the spec zeroes the rest before the row transform — a 64-point network | ||
| 2728 | + /// that read the missing half as anything else would disagree here. | ||
| 2729 | + #[test] | ||
| 2730 | + fn the_inverse_transform_predicts_what_ffmpeg_decodes_for_a_64x64_block() { | ||
| 2731 | + if !have_ffmpeg() { | ||
| 2732 | + eprintln!( | ||
| 2733 | + "SKIP the_inverse_transform_predicts_what_ffmpeg_decodes_for_a_64x64_block: no ffmpeg" | ||
| 2734 | + ); | ||
| 2735 | + return; | ||
| 2736 | + } | ||
| 2737 | + let coeffs = [ | ||
| 2738 | + (0u8, 0u8, 12i32), | ||
| 2739 | + (0, 2, -9), | ||
| 2740 | + (2, 0, 6), | ||
| 2741 | + (1, 1, -5), | ||
| 2742 | + (5, 9, 8), | ||
| 2743 | + (31, 0, -4), | ||
| 2744 | + (0, 31, 3), | ||
| 2745 | + ]; | ||
| 2746 | + let block: BlockCoeffs = coeffs | ||
| 2747 | + .iter() | ||
| 2748 | + .map(|&(row, col, level)| Coeff { row, col, level }) | ||
| 2749 | + .collect::<Vec<_>>() | ||
| 2750 | + .into(); | ||
| 2751 | + let rows = decode_luma_sb(64, 64, &[Superblock::Whole(block)]); | ||
| 2752 | + | ||
| 2753 | + let mut levels = vec![0i32; 64 * 64]; | ||
| 2754 | + for &(row, col, level) in &coeffs { | ||
| 2755 | + levels[usize::from(row) * 64 + usize::from(col)] = level; | ||
| 2756 | + } | ||
| 2757 | + let residual = crate::transform::dequant_and_inverse(&levels, 64, 8, i32::from(Q_IDX)); | ||
| 2758 | + for row in 0..64 { | ||
| 2759 | + for col in 0..64 { | ||
| 2760 | + let want = (128 + residual[row * 64 + col]).clamp(0, 255); | ||
| 2761 | + assert_eq!( | ||
| 2762 | + i32::from(rows[row][col]), | ||
| 2763 | + want, | ||
| 2764 | + "({row},{col}): ffmpeg decoded {} where the encoder predicted {want}", | ||
| 2765 | + rows[row][col] | ||
| 2766 | + ); | ||
| 2767 | + } | ||
| 2768 | + } | ||
| 2769 | + } | ||
| 2770 | + | ||
| 2771 | + /// The AV1 transforms are normalized so that a quantized level means the | ||
| 2772 | + /// same thing at every size: doubling the transform's side halves what a | ||
| 2773 | + /// DC level is worth, which is the job the per-size row shift and the | ||
| 2774 | + /// dequantizer's denominator split between them. The frame syntax only | ||
| 2775 | + /// codes 32x32 and 64x64 transforms, so this is what holds the four | ||
| 2776 | + /// smaller sizes' shifts to the spec's table — a shift wrong by one at any | ||
| 2777 | + /// size doubles or halves that size's step alone. | ||
| 2778 | + #[test] | ||
| 2779 | + fn a_dc_level_is_worth_half_as_much_each_time_the_transform_doubles() { | ||
| 2780 | + let mut previous: Option<i32> = None; | ||
| 2781 | + for side in [4usize, 8, 16, 32, 64] { | ||
| 2782 | + let mut levels = vec![0i32; side * side]; | ||
| 2783 | + levels[0] = 100; | ||
| 2784 | + let residual = | ||
| 2785 | + crate::transform::dequant_and_inverse(&levels, side, 8, i32::from(Q_IDX)); | ||
| 2786 | + let dc = residual[0]; | ||
| 2787 | + assert!( | ||
| 2788 | + residual.iter().all(|&r| r == dc), | ||
| 2789 | + "side {side}: a DC coefficient reconstructs flat" | ||
| 2790 | + ); | ||
| 2791 | + if let Some(previous) = previous { | ||
| 2792 | + assert!( | ||
| 2793 | + (dc * 2 - previous).abs() <= 1, | ||
| 2794 | + "side {side}: {dc} is not half of the previous size's {previous}" | ||
| 2795 | + ); | ||
| 2796 | + } | ||
| 2797 | + previous = Some(dc); | ||
| 2798 | + } | ||
| 2799 | + } | ||
| 2540 | } | 2800 | } |
crates/ec-av1/src/transform.rsadded+344 -0
| @@ -0,0 +1,344 @@ | |||
| 1 | +//! The inverse transform (spec 7.13), so the encoder can reconstruct exactly | ||
| 2 | +//! what a decoder will show. | ||
| 3 | +//! | ||
| 4 | +//! Only the inverse DCT is here, which is all this encoder's subset codes: a | ||
| 5 | +//! square `DCT_DCT` transform per block. The butterfly network is the spec's | ||
| 6 | +//! own, stage for stage — every stage's rounding and intermediate clamp is | ||
| 7 | +//! part of the bitstream contract, so a mathematically equal but differently | ||
| 8 | +//! rounded DCT would drift away from the decoder by a sample here and there. | ||
| 9 | + | ||
| 10 | +/// `Cos128_Lookup` (spec 7.13.2.1): `4096 * cos(angle * pi / 128)` rounded. | ||
| 11 | +static COS128_LOOKUP: [i32; 65] = [ | ||
| 12 | + 4096, 4095, 4091, 4085, 4076, 4065, 4052, 4036, 4017, 3996, 3973, 3948, 3920, 3889, 3857, 3822, | ||
| 13 | + 3784, 3745, 3703, 3659, 3612, 3564, 3513, 3461, 3406, 3349, 3290, 3229, 3166, 3102, 3035, 2967, | ||
| 14 | + 2896, 2824, 2751, 2675, 2598, 2520, 2440, 2359, 2276, 2191, 2106, 2019, 1931, 1842, 1751, 1660, | ||
| 15 | + 1567, 1474, 1380, 1285, 1189, 1092, 995, 897, 799, 700, 601, 501, 401, 301, 201, 101, 0, | ||
| 16 | +]; | ||
| 17 | + | ||
| 18 | +/// The fixed-point position of the cosine table. | ||
| 19 | +const COS_BITS: usize = 12; | ||
| 20 | + | ||
| 21 | +/// `cos128(angle)` (spec 7.13.2.1), folded from the quarter table. | ||
| 22 | +fn cos128(angle: i32) -> i32 { | ||
| 23 | + let angle2 = (angle & 255) as usize; | ||
| 24 | + match angle2 { | ||
| 25 | + 0..=64 => COS128_LOOKUP[angle2], | ||
| 26 | + 65..=128 => -COS128_LOOKUP[128 - angle2], | ||
| 27 | + 129..=192 => -COS128_LOOKUP[angle2 - 128], | ||
| 28 | + _ => COS128_LOOKUP[256 - angle2], | ||
| 29 | + } | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +/// `sin128(angle)` (spec 7.13.2.1). | ||
| 33 | +fn sin128(angle: i32) -> i32 { | ||
| 34 | + cos128(angle - 64) | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +/// `brev(numBits, x)` (spec 7.13.2.1): the bit reversal of the low `num_bits`. | ||
| 38 | +fn brev(num_bits: u32, x: usize) -> usize { | ||
| 39 | + (0..num_bits).fold(0, |t, i| t | (((x >> i) & 1) << (num_bits - 1 - i))) | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +/// `Round2(x, n)` (spec 4.7): shift down `n` places, rounding halves up. | ||
| 43 | +fn round2(x: i32, n: usize) -> i32 { | ||
| 44 | + if n == 0 { | ||
| 45 | + x | ||
| 46 | + } else { | ||
| 47 | + // The intermediate can exceed 32 bits before the shift brings it | ||
| 48 | + // back, which is why it is done in 64. | ||
| 49 | + ((i64::from(x) + (1i64 << (n - 1))) >> n) as i32 | ||
| 50 | + } | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +/// `Clip3` against a signed integer of `r` bits, which is what bounds every | ||
| 54 | +/// intermediate of the transform. | ||
| 55 | +fn clamp_range(x: i32, r: usize) -> i32 { | ||
| 56 | + let hi = ((1i64 << (r - 1)) - 1) as i32; | ||
| 57 | + let lo = (-(1i64 << (r - 1))) as i32; | ||
| 58 | + x.clamp(lo, hi) | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +/// `B(a, b, angle, flip, r)` (spec 7.13.2.1): a butterfly rotation, and an | ||
| 62 | +/// exchange of the two entries when `flip` is set. | ||
| 63 | +fn butterfly(t: &mut [i32], a: usize, b: usize, angle: i32, flip: bool, _r: usize) { | ||
| 64 | + let (ta, tb) = (i64::from(t[a]), i64::from(t[b])); | ||
| 65 | + let (c, s) = (i64::from(cos128(angle)), i64::from(sin128(angle))); | ||
| 66 | + let x = ta * c - tb * s; | ||
| 67 | + let y = ta * s + tb * c; | ||
| 68 | + t[a] = ((x + (1 << (COS_BITS - 1))) >> COS_BITS) as i32; | ||
| 69 | + t[b] = ((y + (1 << (COS_BITS - 1))) >> COS_BITS) as i32; | ||
| 70 | + if flip { | ||
| 71 | + t.swap(a, b); | ||
| 72 | + } | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +/// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices | ||
| 76 | +/// exchanged when `flip` is set. | ||
| 77 | +fn hadamard(t: &mut [i32], a: usize, b: usize, flip: bool, r: usize) { | ||
| 78 | + let (a, b) = if flip { (b, a) } else { (a, b) }; | ||
| 79 | + let (x, y) = (t[a], t[b]); | ||
| 80 | + t[a] = clamp_range(x.wrapping_add(y), r); | ||
| 81 | + t[b] = clamp_range(x.wrapping_sub(y), r); | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +/// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal | ||
| 85 | +/// of the first `1 << n` entries. | ||
| 86 | +fn permute(t: &mut [i32], n: u32) { | ||
| 87 | + let n0 = 1usize << n; | ||
| 88 | + let copy: Vec<i32> = t[..n0].to_vec(); | ||
| 89 | + for (i, v) in t[..n0].iter_mut().enumerate() { | ||
| 90 | + *v = copy[brev(n, i)]; | ||
| 91 | + } | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +/// The inverse DCT process (spec 7.13.2.3): an in-place inverse DCT of the | ||
| 95 | +/// first `1 << n` entries of `t`, clamping intermediates to `r` bits. | ||
| 96 | +/// | ||
| 97 | +/// The stage list is the spec's, in the spec's order; the guards on `n` are | ||
| 98 | +/// what make one network serve every transform size from 4 to 64. | ||
| 99 | +pub fn inverse_dct(t: &mut [i32], n: u32, r: usize) { | ||
| 100 | + assert!((2..=6).contains(&n), "the inverse DCT is defined for 4..64"); | ||
| 101 | + permute(t, n); | ||
| 102 | + | ||
| 103 | + if n == 6 { | ||
| 104 | + for i in 0..16 { | ||
| 105 | + butterfly(t, 32 + i, 63 - i, 63 - 4 * brev(4, i) as i32, false, r); | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + if n >= 5 { | ||
| 109 | + for i in 0..8 { | ||
| 110 | + butterfly( | ||
| 111 | + t, | ||
| 112 | + 16 + i, | ||
| 113 | + 31 - i, | ||
| 114 | + 6 + ((brev(3, 7 - i) as i32) << 3), | ||
| 115 | + false, | ||
| 116 | + r, | ||
| 117 | + ); | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + if n == 6 { | ||
| 121 | + for i in 0..16 { | ||
| 122 | + hadamard(t, 32 + i * 2, 33 + i * 2, i & 1 == 1, r); | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | + if n >= 4 { | ||
| 126 | + for i in 0..4 { | ||
| 127 | + butterfly( | ||
| 128 | + t, | ||
| 129 | + 8 + i, | ||
| 130 | + 15 - i, | ||
| 131 | + 12 + ((brev(2, 3 - i) as i32) << 4), | ||
| 132 | + false, | ||
| 133 | + r, | ||
| 134 | + ); | ||
| 135 | + } | ||
| 136 | + } | ||
| 137 | + if n >= 5 { | ||
| 138 | + for i in 0..8 { | ||
| 139 | + hadamard(t, 16 + 2 * i, 17 + 2 * i, i & 1 == 1, r); | ||
| 140 | + } | ||
| 141 | + } | ||
| 142 | + if n == 6 { | ||
| 143 | + for i in 0..4 { | ||
| 144 | + for j in 0..2 { | ||
| 145 | + let angle = 60 - 16 * brev(2, i) as i32 + 64 * j as i32; | ||
| 146 | + butterfly(t, 62 - i * 4 - j, 33 + i * 4 + j, angle, true, r); | ||
| 147 | + } | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + if n >= 3 { | ||
| 151 | + for i in 0..2 { | ||
| 152 | + butterfly(t, 4 + i, 7 - i, 56 - 32 * i as i32, false, r); | ||
| 153 | + } | ||
| 154 | + } | ||
| 155 | + if n >= 4 { | ||
| 156 | + for i in 0..4 { | ||
| 157 | + hadamard(t, 8 + 2 * i, 9 + 2 * i, i & 1 == 1, r); | ||
| 158 | + } | ||
| 159 | + } | ||
| 160 | + if n >= 5 { | ||
| 161 | + for i in 0..2 { | ||
| 162 | + for j in 0..2 { | ||
| 163 | + let angle = 24 + ((j as i32) << 6) + ((1 - i as i32) << 5); | ||
| 164 | + butterfly(t, 30 - 4 * i - j, 17 + 4 * i + j, angle, true, r); | ||
| 165 | + } | ||
| 166 | + } | ||
| 167 | + } | ||
| 168 | + if n == 6 { | ||
| 169 | + for i in 0..8 { | ||
| 170 | + for j in 0..2 { | ||
| 171 | + hadamard(t, 32 + i * 4 + j, 35 + i * 4 - j, i & 1 == 1, r); | ||
| 172 | + } | ||
| 173 | + } | ||
| 174 | + } | ||
| 175 | + for i in 0..2 { | ||
| 176 | + butterfly(t, 2 * i, 2 * i + 1, 32 + 16 * i as i32, i == 0, r); | ||
| 177 | + } | ||
| 178 | + if n >= 3 { | ||
| 179 | + for i in 0..2 { | ||
| 180 | + hadamard(t, 4 + 2 * i, 5 + 2 * i, i == 1, r); | ||
| 181 | + } | ||
| 182 | + } | ||
| 183 | + if n >= 4 { | ||
| 184 | + for i in 0..2 { | ||
| 185 | + butterfly(t, 14 - i, 9 + i, 48 + 64 * i as i32, true, r); | ||
| 186 | + } | ||
| 187 | + } | ||
| 188 | + if n >= 5 { | ||
| 189 | + for i in 0..4 { | ||
| 190 | + for j in 0..2 { | ||
| 191 | + hadamard(t, 16 + 4 * i + j, 19 + 4 * i - j, i & 1 == 1, r); | ||
| 192 | + } | ||
| 193 | + } | ||
| 194 | + } | ||
| 195 | + if n == 6 { | ||
| 196 | + for i in 0..2 { | ||
| 197 | + for j in 0..4 { | ||
| 198 | + let angle = 56 - (i as i32) * 32 + ((j as i32) >> 1) * 64; | ||
| 199 | + butterfly(t, 61 - i * 8 - j, 34 + i * 8 + j, angle, true, r); | ||
| 200 | + } | ||
| 201 | + } | ||
| 202 | + } | ||
| 203 | + for i in 0..2 { | ||
| 204 | + hadamard(t, i, 3 - i, false, r); | ||
| 205 | + } | ||
| 206 | + if n >= 3 { | ||
| 207 | + butterfly(t, 6, 5, 32, true, r); | ||
| 208 | + } | ||
| 209 | + if n >= 4 { | ||
| 210 | + for i in 0..2 { | ||
| 211 | + for j in 0..2 { | ||
| 212 | + hadamard(t, 8 + 4 * i + j, 11 + 4 * i - j, i == 1, r); | ||
| 213 | + } | ||
| 214 | + } | ||
| 215 | + } | ||
| 216 | + if n >= 5 { | ||
| 217 | + for i in 0..4 { | ||
| 218 | + butterfly(t, 29 - i, 18 + i, 48 + ((i as i32) >> 1) * 64, true, r); | ||
| 219 | + } | ||
| 220 | + } | ||
| 221 | + if n == 6 { | ||
| 222 | + for i in 0..4 { | ||
| 223 | + for j in 0..4 { | ||
| 224 | + hadamard(t, 32 + 8 * i + j, 39 + 8 * i - j, i & 1 == 1, r); | ||
| 225 | + } | ||
| 226 | + } | ||
| 227 | + } | ||
| 228 | + if n >= 3 { | ||
| 229 | + for i in 0..4 { | ||
| 230 | + hadamard(t, i, 7 - i, false, r); | ||
| 231 | + } | ||
| 232 | + } | ||
| 233 | + if n >= 4 { | ||
| 234 | + for i in 0..2 { | ||
| 235 | + butterfly(t, 13 - i, 10 + i, 32, true, r); | ||
| 236 | + } | ||
| 237 | + } | ||
| 238 | + if n >= 5 { | ||
| 239 | + for i in 0..2 { | ||
| 240 | + for j in 0..4 { | ||
| 241 | + hadamard(t, 16 + i * 8 + j, 23 + i * 8 - j, i == 1, r); | ||
| 242 | + } | ||
| 243 | + } | ||
| 244 | + } | ||
| 245 | + if n == 6 { | ||
| 246 | + for i in 0..8 { | ||
| 247 | + butterfly(t, 59 - i, 36 + i, if i < 4 { 48 } else { 112 }, true, r); | ||
| 248 | + } | ||
| 249 | + } | ||
| 250 | + if n >= 4 { | ||
| 251 | + for i in 0..8 { | ||
| 252 | + hadamard(t, i, 15 - i, false, r); | ||
| 253 | + } | ||
| 254 | + } | ||
| 255 | + if n >= 5 { | ||
| 256 | + for i in 0..4 { | ||
| 257 | + butterfly(t, 27 - i, 20 + i, 32, true, r); | ||
| 258 | + } | ||
| 259 | + } | ||
| 260 | + if n == 6 { | ||
| 261 | + for i in 0..8 { | ||
| 262 | + hadamard(t, 32 + i, 47 - i, false, r); | ||
| 263 | + hadamard(t, 48 + i, 63 - i, true, r); | ||
| 264 | + } | ||
| 265 | + } | ||
| 266 | + if n >= 5 { | ||
| 267 | + for i in 0..16 { | ||
| 268 | + hadamard(t, i, 31 - i, false, r); | ||
| 269 | + } | ||
| 270 | + } | ||
| 271 | + if n == 6 { | ||
| 272 | + for i in 0..8 { | ||
| 273 | + butterfly(t, 55 - i, 40 + i, 32, true, r); | ||
| 274 | + } | ||
| 275 | + } | ||
| 276 | + if n == 6 { | ||
| 277 | + for i in 0..32 { | ||
| 278 | + hadamard(t, i, 63 - i, false, r); | ||
| 279 | + } | ||
| 280 | + } | ||
| 281 | +} | ||
| 282 | + | ||
| 283 | +/// `Transform_Row_Shift` (spec 7.13.3) for the square transforms. | ||
| 284 | +fn row_shift(log2: u32) -> usize { | ||
| 285 | + match log2 { | ||
| 286 | + 2 => 0, | ||
| 287 | + 3 => 1, | ||
| 288 | + _ => 2, | ||
| 289 | + } | ||
| 290 | +} | ||
| 291 | + | ||
| 292 | +/// The 2D inverse transform (spec 7.13.3) for a square `DCT_DCT` block. | ||
| 293 | +/// | ||
| 294 | +/// `dequant` is the dequantized coefficient grid in raster order; the returned | ||
| 295 | +/// residual is in the same order. A 64-point transform only ever carries | ||
| 296 | +/// coefficients in its first 32 rows and columns, and the spec zeroes the rest | ||
| 297 | +/// before the row transform, which is what the `< 32` guard does. | ||
| 298 | +pub fn inverse_transform_2d(dequant: &[i32], side: usize, bit_depth: u8) -> Vec<i32> { | ||
| 299 | + let log2 = side.trailing_zeros(); | ||
| 300 | + assert_eq!(1usize << log2, side, "the transform is square"); | ||
| 301 | + assert_eq!(dequant.len(), side * side, "one coefficient per position"); | ||
| 302 | + let row_clamp = usize::from(bit_depth) + 8; | ||
| 303 | + let col_clamp = (usize::from(bit_depth) + 6).max(16); | ||
| 304 | + let mut residual = vec![0i32; side * side]; | ||
| 305 | + | ||
| 306 | + let mut t = [0i32; 64]; | ||
| 307 | + for i in 0..side { | ||
| 308 | + for j in 0..side { | ||
| 309 | + t[j] = if i < 32 && j < 32 { | ||
| 310 | + dequant[i * side + j] | ||
| 311 | + } else { | ||
| 312 | + 0 | ||
| 313 | + }; | ||
| 314 | + } | ||
| 315 | + inverse_dct(&mut t, log2, row_clamp); | ||
| 316 | + for j in 0..side { | ||
| 317 | + // The row's output is shifted, then clamped before the column | ||
| 318 | + // transform reads it back. | ||
| 319 | + residual[i * side + j] = clamp_range(round2(t[j], row_shift(log2)), col_clamp); | ||
| 320 | + } | ||
| 321 | + } | ||
| 322 | + | ||
| 323 | + for j in 0..side { | ||
| 324 | + for i in 0..side { | ||
| 325 | + t[i] = residual[i * side + j]; | ||
| 326 | + } | ||
| 327 | + inverse_dct(&mut t, log2, col_clamp); | ||
| 328 | + for i in 0..side { | ||
| 329 | + residual[i * side + j] = round2(t[i], 4); | ||
| 330 | + } | ||
| 331 | + } | ||
| 332 | + residual | ||
| 333 | +} | ||
| 334 | + | ||
| 335 | +/// Dequantize (spec 7.12.3) and inverse transform one square `DCT_DCT` block, | ||
| 336 | +/// returning its residual in raster order. | ||
| 337 | +/// | ||
| 338 | +/// This is the encoder's model of what the decoder will add to its prediction, | ||
| 339 | +/// so it follows the spec's dequantization exactly, truncation toward zero and | ||
| 340 | +/// all. | ||
| 341 | +pub fn dequant_and_inverse(levels: &[i32], side: usize, bit_depth: u8, q_idx: i32) -> Vec<i32> { | ||
| 342 | + let dq = crate::quant::dequant(levels, side, bit_depth, q_idx); | ||
| 343 | + inverse_transform_2d(&dq, side, bit_depth) | ||
| 344 | +} | ||
lanes/av1-fdct-r1.report.mdadded+50 -0
| @@ -0,0 +1,50 @@ | |||
| 1 | +# lane-av1-fdct r1 — dequantization and the inverse DCT | ||
| 2 | + | ||
| 3 | +## What landed | ||
| 4 | + | ||
| 5 | +`ec-av1` can now compute the residual a decoder will add to its prediction, | ||
| 6 | +which is the piece an encoder needs before it can choose coefficients by | ||
| 7 | +rate-distortion rather than by construction. | ||
| 8 | + | ||
| 9 | +- `crates/ec-av1/src/quant.rs` — `dc_q`/`ac_q` (spec 7.12.2) over the vendored | ||
| 10 | + 8/10/12-bit tables, `dq_denom` and the per-coefficient dequantization of spec | ||
| 11 | + 7.12.3 steps a-f (the 24-bit mask, the divide toward zero, the clip). | ||
| 12 | +- `crates/ec-av1/src/transform.rs` — `cos128`/`sin128`, `brev`, the butterfly | ||
| 13 | + `B` and Hadamard `H` of spec 7.13.2.1, the array permutation of 7.13.2.2, the | ||
| 14 | + whole inverse DCT stage list of 7.13.2.3 for `n = 2..6`, and the 2D driver of | ||
| 15 | + 7.13.3 for square `DCT_DCT`. Written from the specification text, not | ||
| 16 | + transcribed from another encoder. | ||
| 17 | + | ||
| 18 | +## Evidence | ||
| 19 | + | ||
| 20 | +`cargo test --release -p ec-av1` — 55 passed, 0 failed. | ||
| 21 | + | ||
| 22 | +The gates, in order of what they would catch: | ||
| 23 | + | ||
| 24 | +| test | claim | | ||
| 25 | +|---|---| | ||
| 26 | +| `the_inverse_transform_predicts_what_ffmpeg_decodes_for_a_32x32_block` | eight coefficients, both signs, DC to the far corner; every one of 1024 samples equals what ffmpeg decodes | | ||
| 27 | +| `..._for_a_64x64_block` | the same at 64x64, where the spec zeroes the untransmitted half before the row transform; all 4096 samples | | ||
| 28 | +| `the_inverse_transform_reproduces_the_pinned_whole_superblock_residuals` | all 28 levels of `DECODED_AT_Q100`, the table pinned from a real decoder | | ||
| 29 | +| `the_inverse_transform_reproduces_the_pinned_split_residuals` | all 28 levels of `SPLIT_RESIDUAL_AT_Q100` | | ||
| 30 | +| `a_single_ac_coefficient_varies_along_its_own_axis` | a row basis is constant down its columns and is the transpose of the column basis, at every size | | ||
| 31 | +| `a_dc_level_is_worth_half_as_much_each_time_the_transform_doubles` | holds the row shifts of the four sizes the frame syntax cannot reach | | ||
| 32 | +| `negating_the_levels_negates_the_residual` | the dequantizer rounds toward zero, not down | | ||
| 33 | + | ||
| 34 | +Mutation kills run by hand, both reverted: | ||
| 35 | + | ||
| 36 | +- one `Cos128_Lookup` entry off by one (2896 -> 2897): 2 tests fail. | ||
| 37 | +- the 8x8 row shift 1 -> 0: `a_dc_level_is_worth_half...` fails. Without that | ||
| 38 | + test the mutation survived, which is why it exists — the frame syntax only | ||
| 39 | + codes 32x32 and 64x64, so nothing else reaches the small sizes. | ||
| 40 | + | ||
| 41 | +`cargo clippy --release -p ec-av1 --all-targets`: clean. `cargo fmt -p ec-av1` | ||
| 42 | +applied. | ||
| 43 | + | ||
| 44 | +## Not in this lane | ||
| 45 | + | ||
| 46 | +- ADST, identity and WHT transforms, and the rectangular sizes. The encoder | ||
| 47 | + codes none of them; the DCT stage list is already general over `n = 2..6`. | ||
| 48 | +- Quantization matrices (`using_qmatrix`), which the frame header never sets. | ||
| 49 | +- Choosing coefficients with this: the residual model is here, the | ||
| 50 | + rate-distortion loop that would consume it is the next lane. | ||