forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1-rect': mod-32==8 key frames — the 8x8-leaf straddle path

Browse files at this commit

e05f550ad1c6ffcdad2995920a52e89b30abd252Ahmet Kaan GümüşParent: 3816297d02638d
Fifteen rounds: side-8 CDF layer (TX_8X8/TX_4X4, partition_w8), Reach and
scan tables for 8x8 leaves, mi-precise partition/mode contexts, the CFL
alphabet, reduced_tx_set declared in the key-frame header, and the leaf
mode-neighbour write moved out of the per-leaf loop. 640x360-class key
frames now encode and round-trip through ffmpeg bit-exact; the 8x8-leaf
split competes on RD cost; 40x40-class both-axes sizes still refuse.
crates/ec-av1/src/cdf.rsmodified+835 -0
@@ -920,6 +920,18 @@ pub const COEFF_BR_CHROMA_8: [[u16; 5]; 21] = [
920 [7625, 13801, 19144, 32768, 0],920 [7625, 13801, 19144, 32768, 0],
921 ];921 ];
922 922
923+/// `Default_Partition_Cdf[0..4]`, `PARTITION_W16` format (spec 5.11.4 codes an
924+/// 8x8 block's partition as a 4-symbol read that only ever terminates
925+/// `PARTITION_NONE` for this writer -- `PARTITION_SPLIT` is illegal at 8x8, so
926+/// row 4 of libaom's real (8-symbol) 8x8 table is dropped and only the shared
927+/// leading four symbols, common to every partition-symbol alphabet, are kept.
928+pub const PARTITION_W8: [[u16; 5]; 4] = [
929+ [19132, 25510, 30392, 32768, 0],
930+ [13928, 19855, 28540, 32768, 0],
931+ [12522, 23679, 28629, 32768, 0],
932+ [9896, 18783, 25853, 32768, 0],
933+];
934+
923 /// `Default_Partition_Cdf[0..4]`: the partition symbol of a 16x16 block.935 /// `Default_Partition_Cdf[0..4]`: the partition symbol of a 16x16 block.
924 pub const PARTITION_W16: [[u16; 11]; 4] = [936 pub const PARTITION_W16: [[u16; 11]; 4] = [
925 [937 [
@@ -2837,3 +2849,826 @@ mod tests {
2837 assert_eq!(MV_SIGN.len(), 2 + 1);2849 assert_eq!(MV_SIGN.len(), 2 + 1);
2838 }2850 }
2839 }2851 }
2852+
2853+// TX_8X8/TX_4X4 tables (lane-av1-rect): the 8x8 luma and 4x4 chroma coefficient
2854+// coding CDFs, extracted byte-exact from libaom's Default_*_Cdf tables, needed
2855+// to let a key frame's straddling 16x16 quadrant split down to an 8x8 leaf.
2856+
2857+/// TXB_SKIP_LUMA_8_Q0, q-context 0: the same table as [`TXB_SKIP_LUMA_8`], for `base_q_idx` 0..=20.
2858+pub const TXB_SKIP_LUMA_8_Q0: [u16; 3] = [31548, 32768, 0];
2859+
2860+/// TXB_SKIP_LUMA_8_Q1, q-context 1: the same table as [`TXB_SKIP_LUMA_8`], for `base_q_idx` 21..=60.
2861+pub const TXB_SKIP_LUMA_8_Q1: [u16; 3] = [31782, 32768, 0];
2862+
2863+/// the all-zero flag of an 8x8 luma transform, whose only context is zero because the transform covers its whole block.
2864+pub const TXB_SKIP_LUMA_8: [u16; 3] = [31957, 32768, 0];
2865+
2866+/// TXB_SKIP_LUMA_8_Q3, q-context 3: the same table as [`TXB_SKIP_LUMA_8`], for `base_q_idx` 121..=255.
2867+pub const TXB_SKIP_LUMA_8_Q3: [u16; 3] = [31903, 32768, 0];
2868+
2869+/// TXB_SKIP_CHROMA_4_Q0, q-context 0: the same table as [`TXB_SKIP_CHROMA_4`], for `base_q_idx` 0..=20.
2870+pub const TXB_SKIP_CHROMA_4_Q0: [[u16; 3]; 3] =
2871+ [[7654, 32768, 0], [19473, 32768, 0], [29984, 32768, 0]];
2872+
2873+/// TXB_SKIP_CHROMA_4_Q1, q-context 1: the same table as [`TXB_SKIP_CHROMA_4`], for `base_q_idx` 21..=60.
2874+pub const TXB_SKIP_CHROMA_4_Q1: [[u16; 3]; 3] =
2875+ [[5495, 32768, 0], [17942, 32768, 0], [28280, 32768, 0]];
2876+
2877+/// the all-zero flag of a 4x4 chroma transform.
2878+pub const TXB_SKIP_CHROMA_4: [[u16; 3]; 3] =
2879+ [[4119, 32768, 0], [16026, 32768, 0], [25657, 32768, 0]];
2880+
2881+/// TXB_SKIP_CHROMA_4_Q3, q-context 3: the same table as [`TXB_SKIP_CHROMA_4`], for `base_q_idx` 121..=255.
2882+pub const TXB_SKIP_CHROMA_4_Q3: [[u16; 3]; 3] =
2883+ [[2713, 32768, 0], [11861, 32768, 0], [20773, 32768, 0]];
2884+
2885+/// EOB_PT_64_LUMA_Q0, q-context 0: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 0..=20.
2886+pub const EOB_PT_64_LUMA_Q0: [u16; 8] = [329, 498, 1101, 1784, 3265, 7758, 32768, 0];
2887+
2888+/// EOB_PT_64_LUMA_Q1, q-context 1: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 21..=60.
2889+pub const EOB_PT_64_LUMA_Q1: [u16; 8] = [1260, 1446, 2253, 3712, 6652, 13369, 32768, 0];
2890+
2891+/// the end-of-block group of an 8x8 luma transform.
2892+pub const EOB_PT_64_LUMA: [u16; 8] = [2374, 2772, 4583, 7276, 12288, 19706, 32768, 0];
2893+
2894+/// EOB_PT_64_LUMA_Q3, q-context 3: the same table as [`EOB_PT_64_LUMA`], for `base_q_idx` 121..=255.
2895+pub const EOB_PT_64_LUMA_Q3: [u16; 8] = [6307, 7541, 12060, 16358, 22553, 27865, 32768, 0];
2896+
2897+/// EOB_PT_16_CHROMA_Q0, q-context 0: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 0..=20.
2898+pub const EOB_PT_16_CHROMA_Q0: [u16; 6] = [3247, 4950, 9688, 14563, 32768, 0];
2899+
2900+/// EOB_PT_16_CHROMA_Q1, q-context 1: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 21..=60.
2901+pub const EOB_PT_16_CHROMA_Q1: [u16; 6] = [7637, 9498, 14259, 19108, 32768, 0];
2902+
2903+/// the end-of-block group of a 4x4 chroma transform.
2904+pub const EOB_PT_16_CHROMA: [u16; 6] = [11139, 13270, 18241, 23566, 32768, 0];
2905+
2906+/// EOB_PT_16_CHROMA_Q3, q-context 3: the same table as [`EOB_PT_16_CHROMA`], for `base_q_idx` 121..=255.
2907+pub const EOB_PT_16_CHROMA_Q3: [u16; 6] = [19575, 21766, 26044, 29709, 32768, 0];
2908+
2909+/// EOB_EXTRA_LUMA_8_Q0, q-context 0: the same table as [`EOB_EXTRA_LUMA_8`], for `base_q_idx` 0..=20.
2910+pub const EOB_EXTRA_LUMA_8_Q0: [[u16; 3]; 9] = [
2911+ [20401, 32768, 0],
2912+ [17025, 32768, 0],
2913+ [12845, 32768, 0],
2914+ [12873, 32768, 0],
2915+ [14094, 32768, 0],
2916+ [16384, 32768, 0],
2917+ [16384, 32768, 0],
2918+ [16384, 32768, 0],
2919+ [16384, 32768, 0],
2920+];
2921+
2922+/// EOB_EXTRA_LUMA_8_Q1, q-context 1: the same table as [`EOB_EXTRA_LUMA_8`], for `base_q_idx` 21..=60.
2923+pub const EOB_EXTRA_LUMA_8_Q1: [[u16; 3]; 9] = [
2924+ [20430, 32768, 0],
2925+ [20662, 32768, 0],
2926+ [15367, 32768, 0],
2927+ [16970, 32768, 0],
2928+ [14657, 32768, 0],
2929+ [16384, 32768, 0],
2930+ [16384, 32768, 0],
2931+ [16384, 32768, 0],
2932+ [16384, 32768, 0],
2933+];
2934+
2935+/// the top offset bit inside an 8x8 luma transform's end-of-block group.
2936+pub const EOB_EXTRA_LUMA_8: [[u16; 3]; 9] = [
2937+ [19139, 32768, 0],
2938+ [21487, 32768, 0],
2939+ [18959, 32768, 0],
2940+ [20910, 32768, 0],
2941+ [19089, 32768, 0],
2942+ [16384, 32768, 0],
2943+ [16384, 32768, 0],
2944+ [16384, 32768, 0],
2945+ [16384, 32768, 0],
2946+];
2947+
2948+/// EOB_EXTRA_LUMA_8_Q3, q-context 3: the same table as [`EOB_EXTRA_LUMA_8`], for `base_q_idx` 121..=255.
2949+pub const EOB_EXTRA_LUMA_8_Q3: [[u16; 3]; 9] = [
2950+ [20238, 32768, 0],
2951+ [21057, 32768, 0],
2952+ [19159, 32768, 0],
2953+ [22337, 32768, 0],
2954+ [20159, 32768, 0],
2955+ [16384, 32768, 0],
2956+ [16384, 32768, 0],
2957+ [16384, 32768, 0],
2958+ [16384, 32768, 0],
2959+];
2960+
2961+/// EOB_EXTRA_CHROMA_4_Q0, q-context 0: the same table as [`EOB_EXTRA_CHROMA_4`], for `base_q_idx` 0..=20.
2962+pub const EOB_EXTRA_CHROMA_4_Q0: [[u16; 3]; 9] = [
2963+ [19069, 32768, 0],
2964+ [22525, 32768, 0],
2965+ [13377, 32768, 0],
2966+ [16384, 32768, 0],
2967+ [16384, 32768, 0],
2968+ [16384, 32768, 0],
2969+ [16384, 32768, 0],
2970+ [16384, 32768, 0],
2971+ [16384, 32768, 0],
2972+];
2973+
2974+/// EOB_EXTRA_CHROMA_4_Q1, q-context 1: the same table as [`EOB_EXTRA_CHROMA_4`], for `base_q_idx` 21..=60.
2975+pub const EOB_EXTRA_CHROMA_4_Q1: [[u16; 3]; 9] = [
2976+ [20335, 32768, 0],
2977+ [21667, 32768, 0],
2978+ [14818, 32768, 0],
2979+ [16384, 32768, 0],
2980+ [16384, 32768, 0],
2981+ [16384, 32768, 0],
2982+ [16384, 32768, 0],
2983+ [16384, 32768, 0],
2984+ [16384, 32768, 0],
2985+];
2986+
2987+/// the same, for a 4x4 chroma transform.
2988+pub const EOB_EXTRA_CHROMA_4: [[u16; 3]; 9] = [
2989+ [20090, 32768, 0],
2990+ [19444, 32768, 0],
2991+ [17286, 32768, 0],
2992+ [16384, 32768, 0],
2993+ [16384, 32768, 0],
2994+ [16384, 32768, 0],
2995+ [16384, 32768, 0],
2996+ [16384, 32768, 0],
2997+ [16384, 32768, 0],
2998+];
2999+
3000+/// EOB_EXTRA_CHROMA_4_Q3, q-context 3: the same table as [`EOB_EXTRA_CHROMA_4`], for `base_q_idx` 121..=255.
3001+pub const EOB_EXTRA_CHROMA_4_Q3: [[u16; 3]; 9] = [
3002+ [21416, 32768, 0],
3003+ [20855, 32768, 0],
3004+ [23410, 32768, 0],
3005+ [16384, 32768, 0],
3006+ [16384, 32768, 0],
3007+ [16384, 32768, 0],
3008+ [16384, 32768, 0],
3009+ [16384, 32768, 0],
3010+ [16384, 32768, 0],
3011+];
3012+
3013+/// COEFF_BASE_LUMA_8_Q0, q-context 0: the same table as [`COEFF_BASE_LUMA_8`], for `base_q_idx` 0..=20.
3014+pub const COEFF_BASE_LUMA_8_Q0: [[u16; 5]; 42] = [
3015+ [4536, 10072, 14001, 32768, 0],
3016+ [25459, 31416, 32206, 32768, 0],
3017+ [16605, 28048, 30818, 32768, 0],
3018+ [11008, 22857, 27719, 32768, 0],
3019+ [6915, 16268, 22315, 32768, 0],
3020+ [2625, 6812, 10537, 32768, 0],
3021+ [24257, 31788, 32499, 32768, 0],
3022+ [16880, 29454, 31879, 32768, 0],
3023+ [11958, 25054, 29778, 32768, 0],
3024+ [7916, 18718, 25084, 32768, 0],
3025+ [3383, 8777, 13446, 32768, 0],
3026+ [22720, 31603, 32393, 32768, 0],
3027+ [14960, 28125, 31335, 32768, 0],
3028+ [9731, 22210, 27928, 32768, 0],
3029+ [6304, 15832, 22277, 32768, 0],
3030+ [2910, 7818, 12166, 32768, 0],
3031+ [20375, 30627, 32131, 32768, 0],
3032+ [13904, 27284, 30887, 32768, 0],
3033+ [9368, 21558, 27144, 32768, 0],
3034+ [5937, 14966, 21119, 32768, 0],
3035+ [2667, 7225, 11319, 32768, 0],
3036+ [23970, 31470, 32378, 32768, 0],
3037+ [17173, 29734, 32018, 32768, 0],
3038+ [12795, 25441, 29965, 32768, 0],
3039+ [8981, 19680, 25893, 32768, 0],
3040+ [4728, 11372, 16902, 32768, 0],
3041+ [24287, 31797, 32439, 32768, 0],
3042+ [16703, 29145, 31696, 32768, 0],
3043+ [10833, 23554, 28725, 32768, 0],
3044+ [6468, 16566, 23057, 32768, 0],
3045+ [2415, 6562, 10278, 32768, 0],
3046+ [26610, 32395, 32659, 32768, 0],
3047+ [18590, 30498, 32117, 32768, 0],
3048+ [12420, 25756, 29950, 32768, 0],
3049+ [7639, 18746, 24710, 32768, 0],
3050+ [3001, 8086, 12347, 32768, 0],
3051+ [25076, 32064, 32580, 32768, 0],
3052+ [17946, 30128, 32028, 32768, 0],
3053+ [12024, 24985, 29378, 32768, 0],
3054+ [7517, 18390, 24304, 32768, 0],
3055+ [3243, 8781, 13331, 32768, 0],
3056+ [8192, 16384, 24576, 32768, 0],
3057+];
3058+
3059+/// COEFF_BASE_LUMA_8_Q1, q-context 1: the same table as [`COEFF_BASE_LUMA_8`], for `base_q_idx` 21..=60.
3060+pub const COEFF_BASE_LUMA_8_Q1: [[u16; 5]; 42] = [
3061+ [6779, 13743, 17678, 32768, 0],
3062+ [24806, 31797, 32457, 32768, 0],
3063+ [17616, 29047, 31372, 32768, 0],
3064+ [11063, 23175, 28003, 32768, 0],
3065+ [6521, 16110, 22324, 32768, 0],
3066+ [2764, 7504, 11654, 32768, 0],
3067+ [25266, 32367, 32637, 32768, 0],
3068+ [19054, 30553, 32175, 32768, 0],
3069+ [12139, 25212, 29807, 32768, 0],
3070+ [7311, 18162, 24704, 32768, 0],
3071+ [3397, 9164, 14074, 32768, 0],
3072+ [25988, 32208, 32522, 32768, 0],
3073+ [16253, 28912, 31526, 32768, 0],
3074+ [9151, 21387, 27372, 32768, 0],
3075+ [5688, 14915, 21496, 32768, 0],
3076+ [2717, 7627, 12004, 32768, 0],
3077+ [23144, 31855, 32443, 32768, 0],
3078+ [16070, 28491, 31325, 32768, 0],
3079+ [8702, 20467, 26517, 32768, 0],
3080+ [5243, 13956, 20367, 32768, 0],
3081+ [2621, 7335, 11567, 32768, 0],
3082+ [26636, 32340, 32630, 32768, 0],
3083+ [19990, 31050, 32341, 32768, 0],
3084+ [13243, 26105, 30315, 32768, 0],
3085+ [8588, 19521, 25918, 32768, 0],
3086+ [4717, 11585, 17304, 32768, 0],
3087+ [25844, 32292, 32582, 32768, 0],
3088+ [19090, 30635, 32097, 32768, 0],
3089+ [11963, 24546, 28939, 32768, 0],
3090+ [6218, 16087, 22354, 32768, 0],
3091+ [2340, 6608, 10426, 32768, 0],
3092+ [28046, 32576, 32694, 32768, 0],
3093+ [21178, 31313, 32296, 32768, 0],
3094+ [13486, 26184, 29870, 32768, 0],
3095+ [7149, 17871, 23723, 32768, 0],
3096+ [2833, 7958, 12259, 32768, 0],
3097+ [27710, 32528, 32686, 32768, 0],
3098+ [20674, 31076, 32268, 32768, 0],
3099+ [12413, 24955, 29243, 32768, 0],
3100+ [6676, 16927, 23097, 32768, 0],
3101+ [2966, 8333, 12919, 32768, 0],
3102+ [8192, 16384, 24576, 32768, 0],
3103+];
3104+
3105+/// an 8x8 luma coefficient's base level.
3106+pub const COEFF_BASE_LUMA_8: [[u16; 5]; 42] = [
3107+ [9704, 17294, 21132, 32768, 0],
3108+ [26762, 32278, 32633, 32768, 0],
3109+ [18382, 29620, 31819, 32768, 0],
3110+ [10891, 23475, 28723, 32768, 0],
3111+ [6358, 16583, 23309, 32768, 0],
3112+ [3248, 9118, 14141, 32768, 0],
3113+ [27204, 32573, 32699, 32768, 0],
3114+ [19818, 30824, 32329, 32768, 0],
3115+ [11772, 25120, 30041, 32768, 0],
3116+ [6995, 18033, 25039, 32768, 0],
3117+ [3752, 10442, 16098, 32768, 0],
3118+ [27222, 32256, 32559, 32768, 0],
3119+ [15356, 28399, 31475, 32768, 0],
3120+ [8821, 20635, 27057, 32768, 0],
3121+ [5511, 14404, 21239, 32768, 0],
3122+ [2935, 8222, 13051, 32768, 0],
3123+ [24875, 32120, 32529, 32768, 0],
3124+ [15233, 28265, 31445, 32768, 0],
3125+ [8605, 20570, 26932, 32768, 0],
3126+ [5431, 14413, 21196, 32768, 0],
3127+ [2994, 8341, 13223, 32768, 0],
3128+ [28201, 32604, 32700, 32768, 0],
3129+ [21041, 31446, 32456, 32768, 0],
3130+ [13221, 26213, 30475, 32768, 0],
3131+ [8255, 19385, 26037, 32768, 0],
3132+ [4930, 12585, 18830, 32768, 0],
3133+ [28768, 32448, 32627, 32768, 0],
3134+ [19705, 30561, 32021, 32768, 0],
3135+ [11572, 23589, 28220, 32768, 0],
3136+ [5532, 15034, 21446, 32768, 0],
3137+ [2460, 7150, 11456, 32768, 0],
3138+ [29874, 32619, 32699, 32768, 0],
3139+ [21621, 31071, 32201, 32768, 0],
3140+ [12511, 24747, 28992, 32768, 0],
3141+ [6281, 16395, 22748, 32768, 0],
3142+ [3246, 9278, 14497, 32768, 0],
3143+ [29715, 32625, 32712, 32768, 0],
3144+ [20958, 31011, 32283, 32768, 0],
3145+ [11233, 23671, 28806, 32768, 0],
3146+ [6012, 16128, 22868, 32768, 0],
3147+ [3427, 9851, 15414, 32768, 0],
3148+ [8192, 16384, 24576, 32768, 0],
3149+];
3150+
3151+/// COEFF_BASE_LUMA_8_Q3, q-context 3: the same table as [`COEFF_BASE_LUMA_8`], for `base_q_idx` 121..=255.
3152+pub const COEFF_BASE_LUMA_8_Q3: [[u16; 5]; 42] = [
3153+ [7754, 16948, 22142, 32768, 0],
3154+ [25670, 32330, 32691, 32768, 0],
3155+ [15663, 29225, 31994, 32768, 0],
3156+ [9878, 23288, 29158, 32768, 0],
3157+ [6419, 17088, 24336, 32768, 0],
3158+ [3859, 11003, 17039, 32768, 0],
3159+ [27562, 32595, 32725, 32768, 0],
3160+ [17575, 30588, 32399, 32768, 0],
3161+ [10819, 24838, 30309, 32768, 0],
3162+ [7124, 18686, 25916, 32768, 0],
3163+ [4479, 12688, 19340, 32768, 0],
3164+ [28385, 32476, 32673, 32768, 0],
3165+ [15306, 29005, 31938, 32768, 0],
3166+ [8937, 21615, 28322, 32768, 0],
3167+ [5982, 15603, 22786, 32768, 0],
3168+ [3620, 10267, 16136, 32768, 0],
3169+ [27280, 32464, 32667, 32768, 0],
3170+ [15607, 29160, 32004, 32768, 0],
3171+ [9091, 22135, 28740, 32768, 0],
3172+ [6232, 16632, 24020, 32768, 0],
3173+ [4047, 11377, 17672, 32768, 0],
3174+ [29220, 32630, 32718, 32768, 0],
3175+ [19650, 31220, 32462, 32768, 0],
3176+ [13050, 26312, 30827, 32768, 0],
3177+ [9228, 20870, 27468, 32768, 0],
3178+ [6146, 15149, 21971, 32768, 0],
3179+ [30169, 32481, 32623, 32768, 0],
3180+ [17212, 29311, 31554, 32768, 0],
3181+ [9911, 21311, 26882, 32768, 0],
3182+ [4487, 13314, 20372, 32768, 0],
3183+ [2570, 7772, 12889, 32768, 0],
3184+ [30924, 32613, 32708, 32768, 0],
3185+ [19490, 30206, 32107, 32768, 0],
3186+ [11232, 23998, 29276, 32768, 0],
3187+ [6769, 17955, 25035, 32768, 0],
3188+ [4398, 12623, 19214, 32768, 0],
3189+ [30609, 32627, 32722, 32768, 0],
3190+ [19370, 30582, 32287, 32768, 0],
3191+ [10457, 23619, 29409, 32768, 0],
3192+ [6443, 17637, 24834, 32768, 0],
3193+ [4645, 13236, 20106, 32768, 0],
3194+ [8192, 16384, 24576, 32768, 0],
3195+];
3196+
3197+/// COEFF_BASE_CHROMA_4_Q0, q-context 0: the same table as [`COEFF_BASE_CHROMA_4`], for `base_q_idx` 0..=20.
3198+pub const COEFF_BASE_CHROMA_4_Q0: [[u16; 5]; 42] = [
3199+ [6302, 16444, 21761, 32768, 0],
3200+ [23040, 31538, 32475, 32768, 0],
3201+ [15196, 28452, 31496, 32768, 0],
3202+ [10020, 22946, 28514, 32768, 0],
3203+ [6533, 16862, 23501, 32768, 0],
3204+ [3538, 9816, 15076, 32768, 0],
3205+ [24444, 31875, 32525, 32768, 0],
3206+ [15881, 28924, 31635, 32768, 0],
3207+ [9922, 22873, 28466, 32768, 0],
3208+ [6527, 16966, 23691, 32768, 0],
3209+ [4114, 11303, 17220, 32768, 0],
3210+ [8192, 16384, 24576, 32768, 0],
3211+ [8192, 16384, 24576, 32768, 0],
3212+ [8192, 16384, 24576, 32768, 0],
3213+ [8192, 16384, 24576, 32768, 0],
3214+ [8192, 16384, 24576, 32768, 0],
3215+ [8192, 16384, 24576, 32768, 0],
3216+ [8192, 16384, 24576, 32768, 0],
3217+ [8192, 16384, 24576, 32768, 0],
3218+ [8192, 16384, 24576, 32768, 0],
3219+ [8192, 16384, 24576, 32768, 0],
3220+ [20201, 30770, 32209, 32768, 0],
3221+ [14754, 28071, 31258, 32768, 0],
3222+ [8378, 20186, 26517, 32768, 0],
3223+ [5916, 15299, 21978, 32768, 0],
3224+ [4268, 11583, 17901, 32768, 0],
3225+ [24361, 32025, 32581, 32768, 0],
3226+ [18673, 30105, 31943, 32768, 0],
3227+ [10196, 22244, 27576, 32768, 0],
3228+ [5495, 14349, 20417, 32768, 0],
3229+ [2676, 7415, 11498, 32768, 0],
3230+ [24678, 31958, 32585, 32768, 0],
3231+ [18629, 29906, 31831, 32768, 0],
3232+ [9364, 20724, 26315, 32768, 0],
3233+ [4641, 12318, 18094, 32768, 0],
3234+ [2758, 7387, 11579, 32768, 0],
3235+ [25433, 31842, 32469, 32768, 0],
3236+ [18795, 29289, 31411, 32768, 0],
3237+ [7644, 17584, 23592, 32768, 0],
3238+ [3408, 9014, 15047, 32768, 0],
3239+ [8192, 16384, 24576, 32768, 0],
3240+ [8192, 16384, 24576, 32768, 0],
3241+];
3242+
3243+/// COEFF_BASE_CHROMA_4_Q1, q-context 1: the same table as [`COEFF_BASE_CHROMA_4`], for `base_q_idx` 21..=60.
3244+pub const COEFF_BASE_CHROMA_4_Q1: [[u16; 5]; 42] = [
3245+ [8455, 19003, 24368, 32768, 0],
3246+ [23563, 32021, 32604, 32768, 0],
3247+ [16237, 29446, 31935, 32768, 0],
3248+ [10724, 23999, 29358, 32768, 0],
3249+ [6725, 17528, 24416, 32768, 0],
3250+ [3927, 10927, 16825, 32768, 0],
3251+ [26313, 32288, 32634, 32768, 0],
3252+ [17430, 30095, 32095, 32768, 0],
3253+ [11116, 24606, 29679, 32768, 0],
3254+ [7195, 18384, 25269, 32768, 0],
3255+ [4726, 12852, 19315, 32768, 0],
3256+ [8192, 16384, 24576, 32768, 0],
3257+ [8192, 16384, 24576, 32768, 0],
3258+ [8192, 16384, 24576, 32768, 0],
3259+ [8192, 16384, 24576, 32768, 0],
3260+ [8192, 16384, 24576, 32768, 0],
3261+ [8192, 16384, 24576, 32768, 0],
3262+ [8192, 16384, 24576, 32768, 0],
3263+ [8192, 16384, 24576, 32768, 0],
3264+ [8192, 16384, 24576, 32768, 0],
3265+ [8192, 16384, 24576, 32768, 0],
3266+ [22822, 31648, 32483, 32768, 0],
3267+ [16724, 29633, 31929, 32768, 0],
3268+ [10261, 23033, 28725, 32768, 0],
3269+ [7029, 17840, 24528, 32768, 0],
3270+ [4867, 13886, 21502, 32768, 0],
3271+ [25298, 31892, 32491, 32768, 0],
3272+ [17809, 29330, 31512, 32768, 0],
3273+ [9668, 21329, 26579, 32768, 0],
3274+ [4774, 12956, 18976, 32768, 0],
3275+ [2322, 7030, 11540, 32768, 0],
3276+ [25472, 31920, 32543, 32768, 0],
3277+ [17957, 29387, 31632, 32768, 0],
3278+ [9196, 20593, 26400, 32768, 0],
3279+ [4680, 12705, 19202, 32768, 0],
3280+ [2917, 8456, 13436, 32768, 0],
3281+ [26471, 32059, 32574, 32768, 0],
3282+ [18458, 29783, 31909, 32768, 0],
3283+ [8400, 19464, 25956, 32768, 0],
3284+ [3812, 10973, 17206, 32768, 0],
3285+ [8192, 16384, 24576, 32768, 0],
3286+ [8192, 16384, 24576, 32768, 0],
3287+];
3288+
3289+/// a 4x4 chroma coefficient's base level.
3290+pub const COEFF_BASE_CHROMA_4: [[u16; 5]; 42] = [
3291+ [10808, 22056, 26896, 32768, 0],
3292+ [25739, 32313, 32676, 32768, 0],
3293+ [17288, 30203, 32221, 32768, 0],
3294+ [11359, 24878, 29896, 32768, 0],
3295+ [6949, 17767, 24893, 32768, 0],
3296+ [4287, 11796, 18071, 32768, 0],
3297+ [27880, 32521, 32705, 32768, 0],
3298+ [19038, 31004, 32414, 32768, 0],
3299+ [12564, 26345, 30768, 32768, 0],
3300+ [8269, 19947, 26779, 32768, 0],
3301+ [5674, 14657, 21674, 32768, 0],
3302+ [8192, 16384, 24576, 32768, 0],
3303+ [8192, 16384, 24576, 32768, 0],
3304+ [8192, 16384, 24576, 32768, 0],
3305+ [8192, 16384, 24576, 32768, 0],
3306+ [8192, 16384, 24576, 32768, 0],
3307+ [8192, 16384, 24576, 32768, 0],
3308+ [8192, 16384, 24576, 32768, 0],
3309+ [8192, 16384, 24576, 32768, 0],
3310+ [8192, 16384, 24576, 32768, 0],
3311+ [8192, 16384, 24576, 32768, 0],
3312+ [25742, 32319, 32671, 32768, 0],
3313+ [19557, 31164, 32454, 32768, 0],
3314+ [13381, 26381, 30755, 32768, 0],
3315+ [10101, 21466, 26722, 32768, 0],
3316+ [9209, 19650, 26825, 32768, 0],
3317+ [27107, 31917, 32432, 32768, 0],
3318+ [18056, 28893, 31203, 32768, 0],
3319+ [10200, 21434, 26764, 32768, 0],
3320+ [4660, 12913, 19502, 32768, 0],
3321+ [2368, 6930, 12504, 32768, 0],
3322+ [26960, 32158, 32613, 32768, 0],
3323+ [18628, 30005, 32031, 32768, 0],
3324+ [10233, 22442, 28232, 32768, 0],
3325+ [5471, 14630, 21516, 32768, 0],
3326+ [3235, 10767, 17109, 32768, 0],
3327+ [27696, 32440, 32692, 32768, 0],
3328+ [20032, 31167, 32438, 32768, 0],
3329+ [8700, 21341, 28442, 32768, 0],
3330+ [5662, 14831, 21795, 32768, 0],
3331+ [8192, 16384, 24576, 32768, 0],
3332+ [8192, 16384, 24576, 32768, 0],
3333+];
3334+
3335+/// COEFF_BASE_CHROMA_4_Q3, q-context 3: the same table as [`COEFF_BASE_CHROMA_4`], for `base_q_idx` 121..=255.
3336+pub const COEFF_BASE_CHROMA_4_Q3: [[u16; 5]; 42] = [
3337+ [9320, 22102, 27840, 32768, 0],
3338+ [27057, 32464, 32724, 32768, 0],
3339+ [16331, 30268, 32309, 32768, 0],
3340+ [10319, 23935, 29720, 32768, 0],
3341+ [6189, 16448, 24106, 32768, 0],
3342+ [3589, 10884, 18808, 32768, 0],
3343+ [29026, 32624, 32748, 32768, 0],
3344+ [19226, 31507, 32587, 32768, 0],
3345+ [12692, 26921, 31203, 32768, 0],
3346+ [7049, 19532, 27635, 32768, 0],
3347+ [7727, 15669, 23252, 32768, 0],
3348+ [8192, 16384, 24576, 32768, 0],
3349+ [8192, 16384, 24576, 32768, 0],
3350+ [8192, 16384, 24576, 32768, 0],
3351+ [8192, 16384, 24576, 32768, 0],
3352+ [8192, 16384, 24576, 32768, 0],
3353+ [8192, 16384, 24576, 32768, 0],
3354+ [8192, 16384, 24576, 32768, 0],
3355+ [8192, 16384, 24576, 32768, 0],
3356+ [8192, 16384, 24576, 32768, 0],
3357+ [8192, 16384, 24576, 32768, 0],
3358+ [28056, 32625, 32748, 32768, 0],
3359+ [22383, 32075, 32669, 32768, 0],
3360+ [15417, 27098, 31749, 32768, 0],
3361+ [18127, 26493, 27190, 32768, 0],
3362+ [5461, 16384, 21845, 32768, 0],
3363+ [27982, 32091, 32584, 32768, 0],
3364+ [19045, 29868, 31972, 32768, 0],
3365+ [10397, 22266, 27932, 32768, 0],
3366+ [5990, 13697, 21500, 32768, 0],
3367+ [1792, 6912, 15104, 32768, 0],
3368+ [28198, 32501, 32718, 32768, 0],
3369+ [21534, 31521, 32569, 32768, 0],
3370+ [11109, 25217, 30017, 32768, 0],
3371+ [5671, 15124, 26151, 32768, 0],
3372+ [4681, 14043, 18725, 32768, 0],
3373+ [28688, 32580, 32741, 32768, 0],
3374+ [22576, 32079, 32661, 32768, 0],
3375+ [10627, 22141, 28340, 32768, 0],
3376+ [9362, 14043, 28087, 32768, 0],
3377+ [8192, 16384, 24576, 32768, 0],
3378+ [8192, 16384, 24576, 32768, 0],
3379+];
3380+
3381+/// COEFF_BASE_EOB_LUMA_8_Q0, q-context 0: the same table as [`COEFF_BASE_EOB_LUMA_8`], for `base_q_idx` 0..=20.
3382+pub const COEFF_BASE_EOB_LUMA_8_Q0: [[u16; 4]; 4] = [
3383+ [5717, 26477, 32768, 0],
3384+ [30491, 31703, 32768, 0],
3385+ [31550, 32158, 32768, 0],
3386+ [29648, 31491, 32768, 0],
3387+];
3388+
3389+/// COEFF_BASE_EOB_LUMA_8_Q1, q-context 1: the same table as [`COEFF_BASE_EOB_LUMA_8`], for `base_q_idx` 21..=60.
3390+pub const COEFF_BASE_EOB_LUMA_8_Q1: [[u16; 4]; 4] = [
3391+ [15239, 29932, 32768, 0],
3392+ [31315, 32095, 32768, 0],
3393+ [32130, 32434, 32768, 0],
3394+ [30864, 31996, 32768, 0],
3395+];
3396+
3397+/// the last coefficient's base level, for an 8x8 luma transform.
3398+pub const COEFF_BASE_EOB_LUMA_8: [[u16; 4]; 4] = [
3399+ [18049, 30489, 32768, 0],
3400+ [31706, 32286, 32768, 0],
3401+ [32163, 32473, 32768, 0],
3402+ [31550, 32184, 32768, 0],
3403+];
3404+
3405+/// COEFF_BASE_EOB_LUMA_8_Q3, q-context 3: the same table as [`COEFF_BASE_EOB_LUMA_8`], for `base_q_idx` 121..=255.
3406+pub const COEFF_BASE_EOB_LUMA_8_Q3: [[u16; 4]; 4] = [
3407+ [21457, 31043, 32768, 0],
3408+ [31951, 32483, 32768, 0],
3409+ [32153, 32562, 32768, 0],
3410+ [31473, 32215, 32768, 0],
3411+];
3412+
3413+/// COEFF_BASE_EOB_CHROMA_4_Q0, q-context 0: the same table as [`COEFF_BASE_EOB_CHROMA_4`], for `base_q_idx` 0..=20.
3414+pub const COEFF_BASE_EOB_CHROMA_4_Q0: [[u16; 4]; 4] = [
3415+ [21365, 30026, 32768, 0],
3416+ [30512, 32423, 32768, 0],
3417+ [31658, 32621, 32768, 0],
3418+ [29630, 31881, 32768, 0],
3419+];
3420+
3421+/// COEFF_BASE_EOB_CHROMA_4_Q1, q-context 1: the same table as [`COEFF_BASE_EOB_CHROMA_4`], for `base_q_idx` 21..=60.
3422+pub const COEFF_BASE_EOB_CHROMA_4_Q1: [[u16; 4]; 4] = [
3423+ [26594, 31212, 32768, 0],
3424+ [31208, 32582, 32768, 0],
3425+ [31835, 32637, 32768, 0],
3426+ [30595, 32206, 32768, 0],
3427+];
3428+
3429+/// the same, for a 4x4 chroma transform.
3430+pub const COEFF_BASE_EOB_CHROMA_4: [[u16; 4]; 4] = [
3431+ [27258, 31095, 32768, 0],
3432+ [31804, 32623, 32768, 0],
3433+ [31763, 32528, 32768, 0],
3434+ [31438, 32506, 32768, 0],
3435+];
3436+
3437+/// COEFF_BASE_EOB_CHROMA_4_Q3, q-context 3: the same table as [`COEFF_BASE_EOB_CHROMA_4`], for `base_q_idx` 121..=255.
3438+pub const COEFF_BASE_EOB_CHROMA_4_Q3: [[u16; 4]; 4] = [
3439+ [27877, 31584, 32768, 0],
3440+ [32170, 32728, 32768, 0],
3441+ [32155, 32688, 32768, 0],
3442+ [32219, 32702, 32768, 0],
3443+];
3444+
3445+/// COEFF_BR_LUMA_8_Q0, q-context 0: the same table as [`COEFF_BR_LUMA_8`], for `base_q_idx` 0..=20.
3446+pub const COEFF_BR_LUMA_8_Q0: [[u16; 5]; 21] = [
3447+ [14406, 20862, 24414, 32768, 0],
3448+ [11824, 18907, 23109, 32768, 0],
3449+ [8257, 14393, 18803, 32768, 0],
3450+ [5860, 10747, 14778, 32768, 0],
3451+ [4475, 8486, 11984, 32768, 0],
3452+ [3606, 6954, 10043, 32768, 0],
3453+ [1736, 3410, 5048, 32768, 0],
3454+ [14430, 20046, 22882, 32768, 0],
3455+ [15593, 22899, 26709, 32768, 0],
3456+ [12102, 19368, 23811, 32768, 0],
3457+ [9059, 15584, 20262, 32768, 0],
3458+ [6999, 12603, 17048, 32768, 0],
3459+ [5684, 10497, 14553, 32768, 0],
3460+ [2822, 5438, 7862, 32768, 0],
3461+ [15785, 21585, 24359, 32768, 0],
3462+ [18347, 25229, 28266, 32768, 0],
3463+ [14974, 22487, 26389, 32768, 0],
3464+ [11423, 18681, 23271, 32768, 0],
3465+ [8863, 15350, 20008, 32768, 0],
3466+ [7153, 12852, 17278, 32768, 0],
3467+ [3707, 7036, 9982, 32768, 0],
3468+];
3469+
3470+/// COEFF_BR_LUMA_8_Q1, q-context 1: the same table as [`COEFF_BR_LUMA_8`], for `base_q_idx` 21..=60.
3471+pub const COEFF_BR_LUMA_8_Q1: [[u16; 5]; 21] = [
3472+ [15999, 22208, 25449, 32768, 0],
3473+ [13050, 19988, 24122, 32768, 0],
3474+ [8594, 14864, 19378, 32768, 0],
3475+ [6033, 11079, 15238, 32768, 0],
3476+ [4554, 8683, 12347, 32768, 0],
3477+ [3672, 7139, 10337, 32768, 0],
3478+ [1900, 3771, 5576, 32768, 0],
3479+ [15788, 21340, 23949, 32768, 0],
3480+ [16825, 24235, 27758, 32768, 0],
3481+ [12873, 20402, 24810, 32768, 0],
3482+ [9590, 16363, 21094, 32768, 0],
3483+ [7352, 13209, 17733, 32768, 0],
3484+ [5960, 10989, 15184, 32768, 0],
3485+ [3232, 6234, 9007, 32768, 0],
3486+ [15761, 20716, 23224, 32768, 0],
3487+ [19318, 25989, 28759, 32768, 0],
3488+ [15529, 23094, 26929, 32768, 0],
3489+ [11662, 18989, 23641, 32768, 0],
3490+ [8955, 15568, 20366, 32768, 0],
3491+ [7281, 13106, 17708, 32768, 0],
3492+ [4248, 8059, 11440, 32768, 0],
3493+];
3494+
3495+/// the base-range tail of an 8x8 luma coefficient.
3496+pub const COEFF_BR_LUMA_8: [[u16; 5]; 21] = [
3497+ [16214, 22380, 25770, 32768, 0],
3498+ [14213, 21304, 25295, 32768, 0],
3499+ [9213, 15823, 20455, 32768, 0],
3500+ [6395, 11758, 16139, 32768, 0],
3501+ [4779, 9187, 13066, 32768, 0],
3502+ [3821, 7501, 10953, 32768, 0],
3503+ [2293, 4567, 6795, 32768, 0],
3504+ [15859, 21283, 23820, 32768, 0],
3505+ [18404, 25602, 28726, 32768, 0],
3506+ [14325, 21980, 26206, 32768, 0],
3507+ [10669, 17937, 22720, 32768, 0],
3508+ [8297, 14642, 19447, 32768, 0],
3509+ [6746, 12389, 16893, 32768, 0],
3510+ [4324, 8251, 11770, 32768, 0],
3511+ [16532, 21631, 24475, 32768, 0],
3512+ [20667, 27150, 29668, 32768, 0],
3513+ [16728, 24510, 28175, 32768, 0],
3514+ [12861, 20645, 25332, 32768, 0],
3515+ [10076, 17361, 22417, 32768, 0],
3516+ [8395, 14940, 19963, 32768, 0],
3517+ [5731, 10683, 14912, 32768, 0],
3518+];
3519+
3520+/// COEFF_BR_LUMA_8_Q3, q-context 3: the same table as [`COEFF_BR_LUMA_8`], for `base_q_idx` 121..=255.
3521+pub const COEFF_BR_LUMA_8_Q3: [[u16; 5]; 21] = [
3522+ [18274, 24813, 27890, 32768, 0],
3523+ [15537, 23149, 27003, 32768, 0],
3524+ [9449, 16740, 21827, 32768, 0],
3525+ [6700, 12498, 17261, 32768, 0],
3526+ [4988, 9866, 14198, 32768, 0],
3527+ [4236, 8147, 11902, 32768, 0],
3528+ [2867, 5860, 8654, 32768, 0],
3529+ [17124, 23171, 26101, 32768, 0],
3530+ [20396, 27477, 30148, 32768, 0],
3531+ [16573, 24629, 28492, 32768, 0],
3532+ [12749, 20846, 25674, 32768, 0],
3533+ [10233, 17878, 22818, 32768, 0],
3534+ [8525, 15332, 20363, 32768, 0],
3535+ [6283, 11632, 16255, 32768, 0],
3536+ [20466, 26511, 29286, 32768, 0],
3537+ [23059, 29174, 31191, 32768, 0],
3538+ [19481, 27263, 30241, 32768, 0],
3539+ [15458, 23631, 28137, 32768, 0],
3540+ [12416, 20608, 25693, 32768, 0],
3541+ [10261, 18011, 23261, 32768, 0],
3542+ [8016, 14655, 19666, 32768, 0],
3543+];
3544+
3545+/// COEFF_BR_CHROMA_4_Q0, q-context 0: the same table as [`COEFF_BR_CHROMA_4`], for `base_q_idx` 0..=20.
3546+pub const COEFF_BR_CHROMA_4_Q0: [[u16; 5]; 21] = [
3547+ [15967, 22905, 26286, 32768, 0],
3548+ [13534, 20654, 24579, 32768, 0],
3549+ [9504, 16092, 20535, 32768, 0],
3550+ [6975, 12568, 16903, 32768, 0],
3551+ [5364, 10091, 14020, 32768, 0],
3552+ [4357, 8370, 11857, 32768, 0],
3553+ [2506, 4934, 7218, 32768, 0],
3554+ [23032, 28815, 30936, 32768, 0],
3555+ [19540, 26704, 29719, 32768, 0],
3556+ [15158, 22969, 27097, 32768, 0],
3557+ [11408, 18865, 23650, 32768, 0],
3558+ [8885, 15448, 20250, 32768, 0],
3559+ [7108, 12853, 17416, 32768, 0],
3560+ [4231, 8041, 11480, 32768, 0],
3561+ [19823, 26490, 29156, 32768, 0],
3562+ [18890, 25929, 28932, 32768, 0],
3563+ [15660, 23491, 27433, 32768, 0],
3564+ [12147, 19776, 24488, 32768, 0],
3565+ [9728, 16774, 21649, 32768, 0],
3566+ [7919, 14277, 19066, 32768, 0],
3567+ [5440, 10170, 14185, 32768, 0],
3568+];
3569+
3570+/// COEFF_BR_CHROMA_4_Q1, q-context 1: the same table as [`COEFF_BR_CHROMA_4`], for `base_q_idx` 21..=60.
3571+pub const COEFF_BR_CHROMA_4_Q1: [[u16; 5]; 21] = [
3572+ [15571, 22232, 25749, 32768, 0],
3573+ [14506, 21575, 25374, 32768, 0],
3574+ [10189, 17089, 21569, 32768, 0],
3575+ [7316, 13301, 17915, 32768, 0],
3576+ [5783, 10912, 15190, 32768, 0],
3577+ [4760, 9155, 13088, 32768, 0],
3578+ [2993, 5966, 8774, 32768, 0],
3579+ [23424, 28903, 30778, 32768, 0],
3580+ [20775, 27666, 30290, 32768, 0],
3581+ [16474, 24410, 28299, 32768, 0],
3582+ [12471, 20180, 24987, 32768, 0],
3583+ [9410, 16487, 21439, 32768, 0],
3584+ [7536, 13614, 18529, 32768, 0],
3585+ [5048, 9586, 13549, 32768, 0],
3586+ [21090, 27290, 29756, 32768, 0],
3587+ [20796, 27402, 30026, 32768, 0],
3588+ [17819, 25485, 28969, 32768, 0],
3589+ [13860, 21909, 26462, 32768, 0],
3590+ [11002, 18494, 23529, 32768, 0],
3591+ [8953, 15929, 20897, 32768, 0],
3592+ [6448, 11918, 16454, 32768, 0],
3593+];
3594+
3595+/// the base-range tail of a 4x4 chroma coefficient.
3596+pub const COEFF_BR_CHROMA_4: [[u16; 5]; 21] = [
3597+ [17394, 24501, 27895, 32768, 0],
3598+ [15889, 23420, 27185, 32768, 0],
3599+ [11561, 19133, 23870, 32768, 0],
3600+ [8285, 14812, 19844, 32768, 0],
3601+ [6496, 12043, 16550, 32768, 0],
3602+ [4771, 9574, 13677, 32768, 0],
3603+ [3603, 6830, 10144, 32768, 0],
3604+ [21656, 27704, 30200, 32768, 0],
3605+ [21324, 27915, 30511, 32768, 0],
3606+ [17327, 25336, 28997, 32768, 0],
3607+ [13417, 21381, 26033, 32768, 0],
3608+ [10132, 17425, 22338, 32768, 0],
3609+ [8580, 15016, 19633, 32768, 0],
3610+ [5694, 11477, 16411, 32768, 0],
3611+ [24116, 29780, 31450, 32768, 0],
3612+ [23853, 29695, 31591, 32768, 0],
3613+ [20085, 27614, 30428, 32768, 0],
3614+ [15326, 24335, 28575, 32768, 0],
3615+ [11814, 19472, 24810, 32768, 0],
3616+ [10221, 18611, 24767, 32768, 0],
3617+ [7689, 14558, 20321, 32768, 0],
3618+];
3619+
3620+/// COEFF_BR_CHROMA_4_Q3, q-context 3: the same table as [`COEFF_BR_CHROMA_4`], for `base_q_idx` 121..=255.
3621+pub const COEFF_BR_CHROMA_4_Q3: [[u16; 5]; 21] = [
3622+ [21425, 27952, 30388, 32768, 0],
3623+ [18062, 25838, 29034, 32768, 0],
3624+ [11956, 19881, 24808, 32768, 0],
3625+ [7718, 15000, 20980, 32768, 0],
3626+ [5702, 11254, 16143, 32768, 0],
3627+ [4898, 9088, 16864, 32768, 0],
3628+ [3679, 6776, 11907, 32768, 0],
3629+ [23294, 30160, 31663, 32768, 0],
3630+ [24397, 29896, 31836, 32768, 0],
3631+ [19245, 27128, 30593, 32768, 0],
3632+ [13202, 19825, 26404, 32768, 0],
3633+ [11578, 19297, 23957, 32768, 0],
3634+ [8073, 13297, 21370, 32768, 0],
3635+ [5461, 10923, 19745, 32768, 0],
3636+ [27367, 30521, 31934, 32768, 0],
3637+ [24904, 30671, 31940, 32768, 0],
3638+ [23075, 28460, 31299, 32768, 0],
3639+ [14400, 23658, 30417, 32768, 0],
3640+ [13885, 23882, 28325, 32768, 0],
3641+ [14746, 22938, 27853, 32768, 0],
3642+ [5461, 16384, 27307, 32768, 0],
3643+];
3644+
3645+/// `Default_Intra_Ext_Tx_Cdf`'s `TX_SET_INTRA_1` (`EXT_TX_SET_DTT4_IDTX_1DDCT`)
3646+/// row for `TX_8X8` (spec 9.4): the transform type of an intra 8x8 transform
3647+/// when `reduced_tx_set` is false. Unused while this crate's key frames set
3648+/// `reduced_tx_set: true` (`get_tx_set`'s intra branch then returns
3649+/// `TX_SET_INTRA_2` -- [`INTRA_TX_TYPE_SET2_8`] -- at every size up to 16x16,
3650+/// not only 16x16; r11 lane-av1-rect). Kept named for the day a non-reduced
3651+/// path exists.
3652+pub const INTRA_TX_TYPE_SET1_8: [[u16; 8]; 13] = [
3653+ [1870, 13742, 14530, 16498, 23770, 27698, 32768, 0],
3654+ [326, 8796, 14632, 15079, 19272, 27486, 32768, 0],
3655+ [484, 7576, 7712, 14443, 19159, 22591, 32768, 0],
3656+ [1126, 15340, 15895, 17023, 20896, 30279, 32768, 0],
3657+ [655, 4854, 5249, 5913, 22099, 27138, 32768, 0],
3658+ [1299, 6458, 8885, 9290, 14851, 25497, 32768, 0],
3659+ [311, 5295, 5552, 6885, 16107, 22672, 32768, 0],
3660+ [883, 8059, 8270, 11258, 17289, 21549, 32768, 0],
3661+ [741, 7580, 9318, 10345, 16688, 29046, 32768, 0],
3662+ [110, 7406, 7915, 9195, 16041, 23329, 32768, 0],
3663+ [363, 7974, 9357, 10673, 15629, 24474, 32768, 0],
3664+ [153, 7647, 8112, 9936, 15307, 19996, 32768, 0],
3665+ [3511, 6332, 11165, 15335, 19323, 23594, 32768, 0],
3666+];
3667+
3668+/// `Default_Intra_Ext_Tx_Cdf`'s `TX_SET_INTRA_2` (`EXT_TX_SET_DTT4_IDTX`) row
3669+/// for `TX_8X8` (spec 9.4, entropymode.c's `eset==2`/`TX_8X8` slot): the
3670+/// transform type of an intra 8x8 transform when `reduced_tx_set` is true,
3671+/// which this crate's key frames always set. libaom leaves this exact slot
3672+/// at the flat 5-way default (unlike its trained `TX_16X16` neighbour,
3673+/// [`INTRA_TX_TYPE_SET2_16`]) -- every mode reads the same uniform CDF.
3674+pub const INTRA_TX_TYPE_SET2_8: [[u16; 6]; 13] = [[6554, 13107, 19661, 26214, 32768, 0]; 13];
crates/ec-av1/src/cdf_state.rsmodified+151 -0
@@ -37,8 +37,21 @@ pub(crate) enum TxbSet {
37 /// reads, but its own two-symbol `tx_type` table rather than `Luma16`'s37 /// reads, but its own two-symbol `tx_type` table rather than `Luma16`'s
38 /// five-symbol, mode-indexed one.38 /// five-symbol, mode-indexed one.
39 Luma16Inter,39 Luma16Inter,
40+ /// The 8x8 luma transform of an 8x8 leaf under a straddling 16x16 block
41+ /// (lane-av1-rect): intra-only, so no `Luma8Inter` counterpart exists yet.
42+ Luma8,
40 /// The 8x8 transform of a chroma plane of a 16x16 block.43 /// The 8x8 transform of a chroma plane of a 16x16 block.
41 Chroma8,44 Chroma8,
45+ /// The 4x4 transform of a chroma plane of an 8x8 leaf (lane-av1-rect).
46+ /// r5 correction: libaom's `is_chroma_reference` (`av1_common_int.h`) is
47+ /// unconditionally true for `bsize >= BLOCK_8X8`, so chroma *does* follow
48+ /// luma all the way down to 8x8 — it is coded once per 8x8 leaf, not once
49+ /// per parent 16x16 (that "stays at the parent's Chroma8 granularity"
50+ /// framing this doc carried through r4 was never checked against a real
51+ /// decoder and is wrong; only `BLOCK_4X4`/`4X8`/`8X4`, one mi-unit wide or
52+ /// high, get the "last sub-block only" chroma-merge treatment). A real
53+ /// writer for the straddling-16x16 case must use this variant per leaf.
54+ Chroma4,
42 /// The 16x16 transform of a chroma plane of a 32x32 block.55 /// The 16x16 transform of a chroma plane of a 32x32 block.
43 Chroma16,56 Chroma16,
44 /// The 32x32 transform of a chroma plane of a whole superblock.57 /// The 32x32 transform of a chroma plane of a whole superblock.
@@ -89,30 +102,58 @@ pub(crate) struct Cdfs {
89 pub angle_delta: [[u16; 8]; 8],102 pub angle_delta: [[u16; 8]; 8],
90 /// The one-context all-zero flag of a 16x16 luma transform.103 /// The one-context all-zero flag of a 16x16 luma transform.
91 pub txb_skip_luma_16: [[u16; 3]; 1],104 pub txb_skip_luma_16: [[u16; 3]; 1],
105+ /// The one-context all-zero flag of an 8x8 luma transform.
106+ pub txb_skip_luma_8: [[u16; 3]; 1],
92 /// The all-zero flag of an 8x8 chroma transform.107 /// The all-zero flag of an 8x8 chroma transform.
93 pub txb_skip_chroma_8: [[u16; 3]; 3],108 pub txb_skip_chroma_8: [[u16; 3]; 3],
109+ /// The all-zero flag of a 4x4 chroma transform.
110+ pub txb_skip_chroma_4: [[u16; 3]; 3],
94 /// The end-of-block group of a 16x16 luma transform.111 /// The end-of-block group of a 16x16 luma transform.
95 pub eob_pt_256_luma: [u16; 10],112 pub eob_pt_256_luma: [u16; 10],
113+ /// The end-of-block group of an 8x8 luma transform.
114+ pub eob_pt_64_luma: [u16; 8],
96 /// The end-of-block group of an 8x8 chroma transform.115 /// The end-of-block group of an 8x8 chroma transform.
97 pub eob_pt_64_chroma: [u16; 8],116 pub eob_pt_64_chroma: [u16; 8],
117+ /// The end-of-block group of a 4x4 chroma transform.
118+ pub eob_pt_16_chroma: [u16; 6],
98 /// The end-of-block offset bit of a 16x16 luma transform.119 /// The end-of-block offset bit of a 16x16 luma transform.
99 pub eob_extra_luma_16: [[u16; 3]; 9],120 pub eob_extra_luma_16: [[u16; 3]; 9],
121+ /// The same, for an 8x8 luma transform.
122+ pub eob_extra_luma_8: [[u16; 3]; 9],
100 /// The same, for an 8x8 chroma transform.123 /// The same, for an 8x8 chroma transform.
101 pub eob_extra_chroma_8: [[u16; 3]; 9],124 pub eob_extra_chroma_8: [[u16; 3]; 9],
125+ /// The same, for a 4x4 chroma transform.
126+ pub eob_extra_chroma_4: [[u16; 3]; 9],
102 /// The base level of a 16x16 luma coefficient.127 /// The base level of a 16x16 luma coefficient.
103 pub base_luma_16: [[u16; 5]; 42],128 pub base_luma_16: [[u16; 5]; 42],
129+ /// The base level of an 8x8 luma coefficient.
130+ pub base_luma_8: [[u16; 5]; 42],
104 /// The base level of an 8x8 chroma coefficient.131 /// The base level of an 8x8 chroma coefficient.
105 pub base_chroma_8: [[u16; 5]; 42],132 pub base_chroma_8: [[u16; 5]; 42],
133+ /// The base level of a 4x4 chroma coefficient.
134+ pub base_chroma_4: [[u16; 5]; 42],
106 /// The last coefficient's base level, for a 16x16 luma transform.135 /// The last coefficient's base level, for a 16x16 luma transform.
107 pub base_eob_luma_16: [[u16; 4]; 4],136 pub base_eob_luma_16: [[u16; 4]; 4],
137+ /// The same, for an 8x8 luma transform.
138+ pub base_eob_luma_8: [[u16; 4]; 4],
108 /// The same, for an 8x8 chroma transform.139 /// The same, for an 8x8 chroma transform.
109 pub base_eob_chroma_8: [[u16; 4]; 4],140 pub base_eob_chroma_8: [[u16; 4]; 4],
141+ /// The same, for a 4x4 chroma transform.
142+ pub base_eob_chroma_4: [[u16; 4]; 4],
110 /// The base-range tail of a 16x16 luma coefficient.143 /// The base-range tail of a 16x16 luma coefficient.
111 pub br_luma_16: [[u16; 5]; 21],144 pub br_luma_16: [[u16; 5]; 21],
145+ /// The base-range tail of an 8x8 luma coefficient.
146+ pub br_luma_8: [[u16; 5]; 21],
112 /// The base-range tail of an 8x8 chroma coefficient.147 /// The base-range tail of an 8x8 chroma coefficient.
113 pub br_chroma_8: [[u16; 5]; 21],148 pub br_chroma_8: [[u16; 5]; 21],
149+ /// The base-range tail of a 4x4 chroma coefficient.
150+ pub br_chroma_4: [[u16; 5]; 21],
114 /// The partition symbol of a 16x16 block.151 /// The partition symbol of a 16x16 block.
115 pub partition_w16: [[u16; 11]; 4],152 pub partition_w16: [[u16; 11]; 4],
153+ /// The partition symbol of an 8x8 block (lane-av1-rect): only
154+ /// `PARTITION_NONE` is ever coded against it, but the alphabet still
155+ /// adapts on every read, same as every other partition table.
156+ pub partition_w8: [[u16; 5]; 4],
116 /// The one-context all-zero flag of a 32x32 luma transform.157 /// The one-context all-zero flag of a 32x32 luma transform.
117 pub txb_skip_luma_32: [[u16; 3]; 1],158 pub txb_skip_luma_32: [[u16; 3]; 1],
118 /// The same, for a 64x64 luma transform.159 /// The same, for a 64x64 luma transform.
@@ -162,6 +203,8 @@ pub(crate) struct Cdfs {
162 pub dc_sign_luma: [[u16; 3]; 3],203 pub dc_sign_luma: [[u16; 3]; 3],
163 /// The transform type of a 16x16 intra luma transform, by luma mode.204 /// The transform type of a 16x16 intra luma transform, by luma mode.
164 pub intra_tx_type_16: [[u16; 6]; 13],205 pub intra_tx_type_16: [[u16; 6]; 13],
206+ /// The transform type of an 8x8 intra luma transform, by luma mode.
207+ pub intra_tx_type_8: [[u16; 6]; 13],
165 /// The transform type of an `is_inter` 32x32 luma transform (spec208 /// The transform type of an `is_inter` 32x32 luma transform (spec
166 /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it209 /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it
167 /// is not indexed by mode.210 /// is not indexed by mode.
@@ -262,6 +305,13 @@ impl Cdfs {
262 cdf::TXB_SKIP_LUMA_16,305 cdf::TXB_SKIP_LUMA_16,
263 cdf::TXB_SKIP_LUMA_16_Q3,306 cdf::TXB_SKIP_LUMA_16_Q3,
264 )],307 )],
308+ txb_skip_luma_8: [pick(
309+ q_ctx,
310+ cdf::TXB_SKIP_LUMA_8_Q0,
311+ cdf::TXB_SKIP_LUMA_8_Q1,
312+ cdf::TXB_SKIP_LUMA_8,
313+ cdf::TXB_SKIP_LUMA_8_Q3,
314+ )],
265 txb_skip_chroma_8: pick(315 txb_skip_chroma_8: pick(
266 q_ctx,316 q_ctx,
267 cdf::TXB_SKIP_CHROMA_8_Q0,317 cdf::TXB_SKIP_CHROMA_8_Q0,
@@ -269,6 +319,13 @@ impl Cdfs {
269 cdf::TXB_SKIP_CHROMA_8,319 cdf::TXB_SKIP_CHROMA_8,
270 cdf::TXB_SKIP_CHROMA_8_Q3,320 cdf::TXB_SKIP_CHROMA_8_Q3,
271 ),321 ),
322+ txb_skip_chroma_4: pick(
323+ q_ctx,
324+ cdf::TXB_SKIP_CHROMA_4_Q0,
325+ cdf::TXB_SKIP_CHROMA_4_Q1,
326+ cdf::TXB_SKIP_CHROMA_4,
327+ cdf::TXB_SKIP_CHROMA_4_Q3,
328+ ),
272 eob_pt_256_luma: pick(329 eob_pt_256_luma: pick(
273 q_ctx,330 q_ctx,
274 cdf::EOB_PT_256_LUMA_Q0,331 cdf::EOB_PT_256_LUMA_Q0,
@@ -276,6 +333,13 @@ impl Cdfs {
276 cdf::EOB_PT_256_LUMA,333 cdf::EOB_PT_256_LUMA,
277 cdf::EOB_PT_256_LUMA_Q3,334 cdf::EOB_PT_256_LUMA_Q3,
278 ),335 ),
336+ eob_pt_64_luma: pick(
337+ q_ctx,
338+ cdf::EOB_PT_64_LUMA_Q0,
339+ cdf::EOB_PT_64_LUMA_Q1,
340+ cdf::EOB_PT_64_LUMA,
341+ cdf::EOB_PT_64_LUMA_Q3,
342+ ),
279 eob_pt_64_chroma: pick(343 eob_pt_64_chroma: pick(
280 q_ctx,344 q_ctx,
281 cdf::EOB_PT_64_CHROMA_Q0,345 cdf::EOB_PT_64_CHROMA_Q0,
@@ -283,6 +347,13 @@ impl Cdfs {
283 cdf::EOB_PT_64_CHROMA,347 cdf::EOB_PT_64_CHROMA,
284 cdf::EOB_PT_64_CHROMA_Q3,348 cdf::EOB_PT_64_CHROMA_Q3,
285 ),349 ),
350+ eob_pt_16_chroma: pick(
351+ q_ctx,
352+ cdf::EOB_PT_16_CHROMA_Q0,
353+ cdf::EOB_PT_16_CHROMA_Q1,
354+ cdf::EOB_PT_16_CHROMA,
355+ cdf::EOB_PT_16_CHROMA_Q3,
356+ ),
286 eob_extra_luma_16: pick(357 eob_extra_luma_16: pick(
287 q_ctx,358 q_ctx,
288 cdf::EOB_EXTRA_LUMA_16_Q0,359 cdf::EOB_EXTRA_LUMA_16_Q0,
@@ -290,6 +361,13 @@ impl Cdfs {
290 cdf::EOB_EXTRA_LUMA_16,361 cdf::EOB_EXTRA_LUMA_16,
291 cdf::EOB_EXTRA_LUMA_16_Q3,362 cdf::EOB_EXTRA_LUMA_16_Q3,
292 ),363 ),
364+ eob_extra_luma_8: pick(
365+ q_ctx,
366+ cdf::EOB_EXTRA_LUMA_8_Q0,
367+ cdf::EOB_EXTRA_LUMA_8_Q1,
368+ cdf::EOB_EXTRA_LUMA_8,
369+ cdf::EOB_EXTRA_LUMA_8_Q3,
370+ ),
293 eob_extra_chroma_8: pick(371 eob_extra_chroma_8: pick(
294 q_ctx,372 q_ctx,
295 cdf::EOB_EXTRA_CHROMA_8_Q0,373 cdf::EOB_EXTRA_CHROMA_8_Q0,
@@ -297,6 +375,13 @@ impl Cdfs {
297 cdf::EOB_EXTRA_CHROMA_8,375 cdf::EOB_EXTRA_CHROMA_8,
298 cdf::EOB_EXTRA_CHROMA_8_Q3,376 cdf::EOB_EXTRA_CHROMA_8_Q3,
299 ),377 ),
378+ eob_extra_chroma_4: pick(
379+ q_ctx,
380+ cdf::EOB_EXTRA_CHROMA_4_Q0,
381+ cdf::EOB_EXTRA_CHROMA_4_Q1,
382+ cdf::EOB_EXTRA_CHROMA_4,
383+ cdf::EOB_EXTRA_CHROMA_4_Q3,
384+ ),
300 base_luma_16: pick(385 base_luma_16: pick(
301 q_ctx,386 q_ctx,
302 cdf::COEFF_BASE_LUMA_16_Q0,387 cdf::COEFF_BASE_LUMA_16_Q0,
@@ -304,6 +389,13 @@ impl Cdfs {
304 cdf::COEFF_BASE_LUMA_16,389 cdf::COEFF_BASE_LUMA_16,
305 cdf::COEFF_BASE_LUMA_16_Q3,390 cdf::COEFF_BASE_LUMA_16_Q3,
306 ),391 ),
392+ base_luma_8: pick(
393+ q_ctx,
394+ cdf::COEFF_BASE_LUMA_8_Q0,
395+ cdf::COEFF_BASE_LUMA_8_Q1,
396+ cdf::COEFF_BASE_LUMA_8,
397+ cdf::COEFF_BASE_LUMA_8_Q3,
398+ ),
307 base_chroma_8: pick(399 base_chroma_8: pick(
308 q_ctx,400 q_ctx,
309 cdf::COEFF_BASE_CHROMA_8_Q0,401 cdf::COEFF_BASE_CHROMA_8_Q0,
@@ -311,6 +403,13 @@ impl Cdfs {
311 cdf::COEFF_BASE_CHROMA_8,403 cdf::COEFF_BASE_CHROMA_8,
312 cdf::COEFF_BASE_CHROMA_8_Q3,404 cdf::COEFF_BASE_CHROMA_8_Q3,
313 ),405 ),
406+ base_chroma_4: pick(
407+ q_ctx,
408+ cdf::COEFF_BASE_CHROMA_4_Q0,
409+ cdf::COEFF_BASE_CHROMA_4_Q1,
410+ cdf::COEFF_BASE_CHROMA_4,
411+ cdf::COEFF_BASE_CHROMA_4_Q3,
412+ ),
314 base_eob_luma_16: pick(413 base_eob_luma_16: pick(
315 q_ctx,414 q_ctx,
316 cdf::COEFF_BASE_EOB_LUMA_16_Q0,415 cdf::COEFF_BASE_EOB_LUMA_16_Q0,
@@ -318,6 +417,13 @@ impl Cdfs {
318 cdf::COEFF_BASE_EOB_LUMA_16,417 cdf::COEFF_BASE_EOB_LUMA_16,
319 cdf::COEFF_BASE_EOB_LUMA_16_Q3,418 cdf::COEFF_BASE_EOB_LUMA_16_Q3,
320 ),419 ),
420+ base_eob_luma_8: pick(
421+ q_ctx,
422+ cdf::COEFF_BASE_EOB_LUMA_8_Q0,
423+ cdf::COEFF_BASE_EOB_LUMA_8_Q1,
424+ cdf::COEFF_BASE_EOB_LUMA_8,
425+ cdf::COEFF_BASE_EOB_LUMA_8_Q3,
426+ ),
321 base_eob_chroma_8: pick(427 base_eob_chroma_8: pick(
322 q_ctx,428 q_ctx,
323 cdf::COEFF_BASE_EOB_CHROMA_8_Q0,429 cdf::COEFF_BASE_EOB_CHROMA_8_Q0,
@@ -325,6 +431,13 @@ impl Cdfs {
325 cdf::COEFF_BASE_EOB_CHROMA_8,431 cdf::COEFF_BASE_EOB_CHROMA_8,
326 cdf::COEFF_BASE_EOB_CHROMA_8_Q3,432 cdf::COEFF_BASE_EOB_CHROMA_8_Q3,
327 ),433 ),
434+ base_eob_chroma_4: pick(
435+ q_ctx,
436+ cdf::COEFF_BASE_EOB_CHROMA_4_Q0,
437+ cdf::COEFF_BASE_EOB_CHROMA_4_Q1,
438+ cdf::COEFF_BASE_EOB_CHROMA_4,
439+ cdf::COEFF_BASE_EOB_CHROMA_4_Q3,
440+ ),
328 br_luma_16: pick(441 br_luma_16: pick(
329 q_ctx,442 q_ctx,
330 cdf::COEFF_BR_LUMA_16_Q0,443 cdf::COEFF_BR_LUMA_16_Q0,
@@ -332,6 +445,13 @@ impl Cdfs {
332 cdf::COEFF_BR_LUMA_16,445 cdf::COEFF_BR_LUMA_16,
333 cdf::COEFF_BR_LUMA_16_Q3,446 cdf::COEFF_BR_LUMA_16_Q3,
334 ),447 ),
448+ br_luma_8: pick(
449+ q_ctx,
450+ cdf::COEFF_BR_LUMA_8_Q0,
451+ cdf::COEFF_BR_LUMA_8_Q1,
452+ cdf::COEFF_BR_LUMA_8,
453+ cdf::COEFF_BR_LUMA_8_Q3,
454+ ),
335 br_chroma_8: pick(455 br_chroma_8: pick(
336 q_ctx,456 q_ctx,
337 cdf::COEFF_BR_CHROMA_8_Q0,457 cdf::COEFF_BR_CHROMA_8_Q0,
@@ -339,7 +459,15 @@ impl Cdfs {
339 cdf::COEFF_BR_CHROMA_8,459 cdf::COEFF_BR_CHROMA_8,
340 cdf::COEFF_BR_CHROMA_8_Q3,460 cdf::COEFF_BR_CHROMA_8_Q3,
341 ),461 ),
462+ br_chroma_4: pick(
463+ q_ctx,
464+ cdf::COEFF_BR_CHROMA_4_Q0,
465+ cdf::COEFF_BR_CHROMA_4_Q1,
466+ cdf::COEFF_BR_CHROMA_4,
467+ cdf::COEFF_BR_CHROMA_4_Q3,
468+ ),
342 partition_w16: cdf::PARTITION_W16,469 partition_w16: cdf::PARTITION_W16,
470+ partition_w8: cdf::PARTITION_W8,
343 txb_skip_luma_32: [pick(471 txb_skip_luma_32: [pick(
344 q_ctx,472 q_ctx,
345 cdf::TXB_SKIP_LUMA_32_Q0,473 cdf::TXB_SKIP_LUMA_32_Q0,
@@ -496,6 +624,7 @@ impl Cdfs {
496 ),624 ),
497 dc_sign_luma: cdf::DC_SIGN_LUMA,625 dc_sign_luma: cdf::DC_SIGN_LUMA,
498 intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16,626 intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16,
627+ intra_tx_type_8: cdf::INTRA_TX_TYPE_SET2_8,
499 inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32,628 inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32,
500 inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16,629 inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16,
501 dc_sign_chroma: cdf::DC_SIGN_CHROMA,630 dc_sign_chroma: cdf::DC_SIGN_CHROMA,
@@ -569,6 +698,17 @@ impl Cdfs {
569 dc_sign: &mut self.dc_sign_luma,698 dc_sign: &mut self.dc_sign_luma,
570 tx_type: Some(self.inter_tx_type_16.as_mut_slice()),699 tx_type: Some(self.inter_tx_type_16.as_mut_slice()),
571 },700 },
701+ TxbSet::Luma8 => TxbTables {
702+ side: 8,
703+ txb_skip: &mut self.txb_skip_luma_8,
704+ eob_pt: &mut self.eob_pt_64_luma,
705+ eob_extra: &mut self.eob_extra_luma_8,
706+ base: &mut self.base_luma_8,
707+ base_eob: &mut self.base_eob_luma_8,
708+ br: &mut self.br_luma_8,
709+ dc_sign: &mut self.dc_sign_luma,
710+ tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()),
711+ },
572 TxbSet::Chroma8 => TxbTables {712 TxbSet::Chroma8 => TxbTables {
573 side: 8,713 side: 8,
574 txb_skip: &mut self.txb_skip_chroma_8,714 txb_skip: &mut self.txb_skip_chroma_8,
@@ -580,6 +720,17 @@ impl Cdfs {
580 dc_sign: &mut self.dc_sign_chroma,720 dc_sign: &mut self.dc_sign_chroma,
581 tx_type: None,721 tx_type: None,
582 },722 },
723+ TxbSet::Chroma4 => TxbTables {
724+ side: 4,
725+ txb_skip: &mut self.txb_skip_chroma_4,
726+ eob_pt: &mut self.eob_pt_16_chroma,
727+ eob_extra: &mut self.eob_extra_chroma_4,
728+ base: &mut self.base_chroma_4,
729+ base_eob: &mut self.base_eob_chroma_4,
730+ br: &mut self.br_chroma_4,
731+ dc_sign: &mut self.dc_sign_chroma,
732+ tx_type: None,
733+ },
583 TxbSet::Chroma16 => TxbTables {734 TxbSet::Chroma16 => TxbTables {
584 side: 16,735 side: 16,
585 txb_skip: &mut self.txb_skip_chroma_16,736 txb_skip: &mut self.txb_skip_chroma_16,
crates/ec-av1/src/encode.rsmodified+283 -63
@@ -431,6 +431,19 @@ pub(crate) fn key_frame_headers_colour(
431 ..QuantizationParams::default()431 ..QuantizationParams::default()
432 },432 },
433 tx_mode: TxMode::Largest,433 tx_mode: TxMode::Largest,
434+ // Forces `get_tx_set` (spec 5.11.48) to `TX_SET_INTRA_2` for every
435+ // intra transform below 32x32, not only 16x16 -- the set this crate
436+ // carries a table for at 8x8/16x16 (`INTRA_TX_TYPE_SET2_8`/`_16`).
437+ // This bit is part of the bitstream (spec 5.9.2's `reduced_tx_set`),
438+ // so a decoder honouring the header picks the same CDF the writer
439+ // used only if this is set here too, not only on the inter variant
440+ // below -- an r14 lane-av1-rect fix: a key frame's own header used to
441+ // default this to `false`, so `av1_get_ext_tx_set_type` picked the
442+ // 7-symbol `TX_SET_INTRA_1` table on the decoder side while the
443+ // writer coded against the 5-symbol `TX_SET_INTRA_2` one, diverging
444+ // the arithmetic coder's `rng` register from the very first 8x8-leaf
445+ // luma transform onward with no symbol value ever differing.
446+ reduced_tx_set: true,
434 ..FrameHeader::default()447 ..FrameHeader::default()
435 };448 };
436 Ok((seq, header))449 Ok((seq, header))
@@ -472,10 +485,12 @@ pub fn inter_frame_headers(
472 // set this crate carries a table for at 16x16 (see485 // set this crate carries a table for at 16x16 (see
473 // `crate::cdf::INTER_TX_TYPE_SET3_16`'s doc comment). A 32x32 inter486 // `crate::cdf::INTER_TX_TYPE_SET3_16`'s doc comment). A 32x32 inter
474 // transform is unaffected (its `txSzSqrUp == TX_32X32` branch already487 // transform is unaffected (its `txSzSqrUp == TX_32X32` branch already
475- // reads `TX_SET_INTER_3` regardless of this flag), and neither is any488+ // reads `TX_SET_INTER_3` regardless of this flag). An intra transform
476- // intra transform at any size (`get_tx_set`'s intra branch returns489+ // IS affected below 16x16 too, not only at 16x16 (r11 lane-av1-rect:
477- // the same `TX_SET_INTRA_2` at 16x16 with or without it), so this490+ // the earlier claim here was wrong and desynced the 8x8-leaf straddle
478- // only narrows the 16x16-and-smaller inter case this crate is adding.491+ // path) -- with this flag set, `get_tx_set`'s intra branch returns
492+ // `TX_SET_INTRA_2` at every size up to 16x16, so `TX_8X8` reads
493+ // [`crate::cdf::INTRA_TX_TYPE_SET2_8`], not `SET1_8`.
479 reduced_tx_set: true,494 reduced_tx_set: true,
480 ..key495 ..key
481 };496 };
@@ -946,11 +961,28 @@ pub(crate) struct Reach {
946 /// it. Indexed by the block's position in the superblock, in blocks of its own961 /// it. Indexed by the block's position in the superblock, in blocks of its own
947 /// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's962 /// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's
948 /// 128, not this crate's 64 superblock -- bit by bit from the low end.963 /// 128, not this crate's 64 superblock -- bit by bit from the low end.
949-const HAS_TOP_RIGHT: [&[u8]; 2] = [&[255, 85, 119, 85, 127, 85, 119, 85], &[95, 87]];964+const HAS_TOP_RIGHT: [&[u8]; 3] = [
965+ &[255, 85, 119, 85, 127, 85, 119, 85],
966+ &[95, 87],
967+ // has_tr_8x8 (libaom av1/common/reconintra.c) -- table_stride(8) == 16
968+ // (128 / 8), so this row is 16 wide by 16 rows, 32 bytes.
969+ &[
970+ 255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, 255, 127, 85, 85,
971+ 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85,
972+ ],
973+];
950 974
951 /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the975 /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the
952 /// left.976 /// left.
953-const HAS_BOTTOM_LEFT: [&[u8]; 2] = [&[84, 16, 84, 0, 84, 16, 84, 0], &[4, 4]];977+const HAS_BOTTOM_LEFT: [&[u8]; 3] = [
978+ &[84, 16, 84, 0, 84, 16, 84, 0],
979+ &[4, 4],
980+ // has_bl_8x8 (libaom av1/common/reconintra.c)
981+ &[
982+ 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, 84, 85, 16, 17, 84, 85, 0, 1,
983+ 84, 85, 16, 17, 84, 85, 0, 0,
984+ ],
985+];
954 986
955 impl Reach {987 impl Reach {
956 /// What a block of `side` samples at `(x, y)` may read past its own edges,988 /// What a block of `side` samples at `(x, y)` may read past its own edges,
@@ -1029,9 +1061,15 @@ impl Reach {
1029 128 / side1061 128 / side
1030 }1062 }
1031 1063
1032- /// Which of the two block sizes the encoder codes a table row is for.1064+ /// Which of the three block sizes the encoder codes a table row is for.
1033 fn table(side: usize) -> usize {1065 fn table(side: usize) -> usize {
1034- usize::from(side == BLOCK)1066+ if side == BLOCK {
1067+ 1
1068+ } else if side == 8 {
1069+ 2
1070+ } else {
1071+ 0
1072+ }
1035 }1073 }
1036 1074
1037 /// One bit of a table, counting from the low end of its first byte.1075 /// One bit of a table, counting from the low end of its first byte.
@@ -1069,6 +1107,15 @@ fn code_square(
1069 ) -> (BlockCoeffs, f64) {1107 ) -> (BlockCoeffs, f64) {
1070 let (luma_set, chroma_set) = if side == BLOCK {1108 let (luma_set, chroma_set) = if side == BLOCK {
1071 (TxbSet::Luma32, TxbSet::Chroma16)1109 (TxbSet::Luma32, TxbSet::Chroma16)
1110+ } else if side == 8 {
1111+ // An 8x8 leaf under a straddling 16x16 (lane-av1-rect). r5 correction:
1112+ // `TxbSet::Chroma4`'s doc now has the checked spec fact -- chroma is
1113+ // `is_chroma_reference` at every BLOCK_8X8, so it is coded per leaf at
1114+ // 4x4, not once at the parent's 8x8. No caller passes `side == 8` yet
1115+ // (this branch is still unreached), so leaving `Chroma8` here would
1116+ // silently wire the wrong table the moment one does; `Chroma4` is
1117+ // what a real caller needs.
1118+ (TxbSet::Luma8, TxbSet::Chroma4)
1072 } else {1119 } else {
1073 (TxbSet::Luma16, TxbSet::Chroma8)1120 (TxbSet::Luma16, TxbSet::Chroma8)
1074 };1121 };
@@ -1253,6 +1300,7 @@ fn code_square_inter(
1253 v: coeffs(&v.levels, SUB / 2),1300 v: coeffs(&v.levels, SUB / 2),
1254 mode: DC_PRED as u8,1301 mode: DC_PRED as u8,
1255 skip,1302 skip,
1303+ eight: None,
1256 inter: Some(InterInfo {1304 inter: Some(InterInfo {
1257 mode: InterMode::NearestMv,1305 mode: InterMode::NearestMv,
1258 mv,1306 mv,
@@ -1656,24 +1704,37 @@ pub(crate) fn encode_key_frame_inner(
1656 ),1704 ),
1657 );1705 );
1658 let whole_legal = has_cols32 && has_rows32;1706 let whole_legal = has_cols32 && has_rows32;
1659- let split_legal = (0..4)1707+ // A 16x16 sub-block's own hasCols/hasRows (spec
1660- .map(|i| (r0 + i / 2, c0 + i % 2))1708+ // `decode_partition`, recomputed at this leaf's own half): one
1661- .filter(|&(sr, sc)| {1709+ // axis false is a straddling leaf this writer codes as two
1662- (sr as u32) * crate::tile::SUB_MI < header.mi_rows1710+ // 8x8 leaves (`crate::tile::write_leaf8`, lane-av1-rect r7);
1663- && (sc as u32) * crate::tile::SUB_MI < header.mi_cols1711+ // both axes false needs a rectangular transform this writer
1664- })1712+ // has no size for at all, whether whole or split.
1665- .all(|(sr, sc)| {1713+ let sub_half = |sr: usize, sc: usize| {
1714+ (
1666 crate::tile::has_half(1715 crate::tile::has_half(
1667 sc as u32 * crate::tile::SUB_MI,1716 sc as u32 * crate::tile::SUB_MI,
1668 crate::tile::SUB_MI,1717 crate::tile::SUB_MI,
1669 header.mi_cols,1718 header.mi_cols,
1670- ) && crate::tile::has_half(1719+ ),
1720+ crate::tile::has_half(
1671 sr as u32 * crate::tile::SUB_MI,1721 sr as u32 * crate::tile::SUB_MI,
1672 crate::tile::SUB_MI,1722 crate::tile::SUB_MI,
1673 header.mi_rows,1723 header.mi_rows,
1674- )1724+ ),
1725+ )
1726+ };
1727+ let both_axes_cut = (0..4)
1728+ .map(|i| (r0 + i / 2, c0 + i % 2))
1729+ .filter(|&(sr, sc)| {
1730+ (sr as u32) * crate::tile::SUB_MI < header.mi_rows
1731+ && (sc as u32) * crate::tile::SUB_MI < header.mi_cols
1732+ })
1733+ .any(|(sr, sc)| {
1734+ let (has_cols16, has_rows16) = sub_half(sr, sc);
1735+ !has_cols16 && !has_rows16
1675 });1736 });
1676- if !whole_legal && !split_legal {1737+ if !whole_legal && both_axes_cut {
1677 return Err(Error::unsupported(1738 return Err(Error::unsupported(
1678 "AV1 encode",1739 "AV1 encode",
1679 format!(1740 format!(
@@ -1717,22 +1778,67 @@ pub(crate) fn encode_key_frame_inner(
1717 {1778 {
1718 continue;1779 continue;
1719 }1780 }
1720- let (block, cost) = code_square(1781+ let (has_cols16, has_rows16) = sub_half(sr, sc);
1721- &mut luma,1782+ if has_cols16 && has_rows16 {
1722- &mut chroma,1783+ let (block, cost) = code_square(
1723- (x + (sub % 2) * SUB, y + (sub / 2) * SUB),1784+ &mut luma,
1724- SUB,1785+ &mut chroma,
1725- &search,1786+ (x + (sub % 2) * SUB, y + (sub / 2) * SUB),
1726- &mode_bits(above_mode[sc], left_mode[sr]),1787+ SUB,
1727- );1788+ &search,
1728- cost_split += cost;1789+ &mode_bits(above_mode[sc], left_mode[sr]),
1729- split_modes.push(block.mode);1790+ );
1730- above_mode[sc] = block.mode;1791+ cost_split += cost;
1731- left_mode[sr] = block.mode;1792+ split_modes.push(block.mode);
1732- split.push(block);1793+ above_mode[sc] = block.mode;
1794+ left_mode[sr] = block.mode;
1795+ split.push(block);
1796+ } else {
1797+ // A straddling 16x16: two (or, at a true corner, one)
1798+ // 8x8 leaves, in the raster order `write_leaf8`'s
1799+ // caller expects. Both leaves cost against the SAME
1800+ // enclosing-slot mode context (see
1801+ // `crate::tile::write_leaf8`'s doc), and neither
1802+ // updates `above_mode`/`left_mode` at this SUB slot --
1803+ // the writer never does either, so the search must
1804+ // not diverge from what it will actually read next.
1805+ let leaf_mode_bits = mode_bits(above_mode[sc], left_mode[sr]);
1806+ let (x_sub, y_sub) = (x + (sub % 2) * SUB, y + (sub / 2) * SUB);
1807+ let mut leaves = Vec::with_capacity(2);
1808+ for i in 0..4 {
1809+ let leaf_x = x_sub + (i % 2) * 8;
1810+ let leaf_y = y_sub + (i / 2) * 8;
1811+ if leaf_x >= luma.true_width || leaf_y >= luma.true_height {
1812+ continue;
1813+ }
1814+ let (leaf, cost) = code_square(
1815+ &mut luma,
1816+ &mut chroma,
1817+ (leaf_x, leaf_y),
1818+ 8,
1819+ &search,
1820+ &leaf_mode_bits,
1821+ );
1822+ cost_split += cost;
1823+ split_modes.push(leaf.mode);
1824+ leaves.push(leaf);
1825+ }
1826+ split.push(BlockCoeffs {
1827+ eight: Some(leaves),
1828+ ..BlockCoeffs::default()
1829+ });
1830+ }
1733 }1831 }
1734 1832
1735- if split_legal && (!whole_legal || (split_blocks && cost_split < cost_whole)) {1833+ // A split whose subs are all whole 16x16 is a real quality
1834+ // alternative to `whole`, exactly as before; a split
1835+ // carrying an 8x8-leaf sub was, through r14, only ever
1836+ // forced (`!whole_legal`), never chosen on cost, because the
1837+ // leaf path was not yet proven against a real decoder. r15
1838+ // closed that gap (`a_single_axis_straddle_round_trips_
1839+ // through_ffmpeg` decodes clean), so the guard is lifted:
1840+ // leaf8 splits now compete on cost like any other split.
1841+ if !whole_legal || (split_blocks && cost_split < cost_whole) {
1736 modes.extend_from_slice(&split_modes);1842 modes.extend_from_slice(&split_modes);
1737 blocks.push(Quadrant::Split(split));1843 blocks.push(Quadrant::Split(split));
1738 } else {1844 } else {
@@ -2319,6 +2425,7 @@ fn search_inter_block(
2319 mode: best.mode,2425 mode: best.mode,
2320 skip: best.skip,2426 skip: best.skip,
2321 inter: best.inter,2427 inter: best.inter,
2428+ eight: None,
2322 }2429 }
2323 }2430 }
2324 2431
@@ -2858,6 +2965,7 @@ mod tests {
2858 v: Vec::new(),2965 v: Vec::new(),
2859 mode: 0,2966 mode: 0,
2860 skip: false,2967 skip: false,
2968+ eight: None,
2861 inter: Some(InterInfo {2969 inter: Some(InterInfo {
2862 mode: InterMode::NearestMv,2970 mode: InterMode::NearestMv,
2863 mv: (0, 0),2971 mv: (0, 0),
@@ -3207,23 +3315,25 @@ mod tests {
3207 }3315 }
3208 }3316 }
3209 3317
3210- /// 640x360 is the class the charter asks be kept consistent between the3318+ /// 640x360 is the class the charter names: 360 mod 32 == 8, so a 16x16
3211- /// key frame and inter frame writers: 360 mod 32 == 8, so a 16x16 leaf's3319+ /// leaf's own half (`has_half` at the SUB level) itself straddles the
3212- /// own half (`has_half` at the SUB level) itself straddles the true edge3320+ /// true edge on one axis only. lane-av1-rect r7 wires the key frame
3213- /// -- the genuine "needs an 8x8/rectangular transform" case neither3321+ /// writer's 8x8-leaf path (`crate::tile::write_leaf8`) live for exactly
3214- /// writer codes. Both must refuse it the same way.3322+ /// this case, so the key frame path no longer *refuses* it; the inter
3323+ /// frame path (`sb_coeff_inter_frame_tile`) was not part of this round's
3324+ /// wiring and still has no leaf-8 generation, so it must still refuse.
3325+ /// This only proves the encoder does not error out -- the leaf-8 stream
3326+ /// it produces is NOT yet proven decoder-clean (see
3327+ /// `a_single_axis_straddle_round_trips_through_ffmpeg`, currently
3328+ /// `#[ignore]`d on a genuine dav1d desync at the smaller 40x32 case).
3215 #[test]3329 #[test]
3216- fn both_writers_refuse_the_same_strictly_before_half_straddle_size() {3330+ fn key_frame_encodes_the_inter_frame_still_refuses_at_half_straddle_size() {
3217 let (width, height) = (640usize, 360usize);3331 let (width, height) = (640usize, 360usize);
3218 let picture = Picture::grey(width, height);3332 let picture = Picture::grey(width, height);
3219- let key_err = encode_key_frame(&picture, 100, 0.5).unwrap_err();3333+ encode_key_frame(&picture, 100, 0.5).expect("key frame now codes the mod-32==8 straddle");
3220- assert!(
3221- key_err.to_string().contains("rectangular"),
3222- "key frame refusal: {key_err}"
3223- );
3224 3334
3225 // `encode_sequence`'s second frame is the inter path: same size,3335 // `encode_sequence`'s second frame is the inter path: same size,
3226- // same class of refusal expected.3336+ // still refused -- that machinery was not wired this round.
3227 let pictures = vec![picture.clone(), picture];3337 let pictures = vec![picture.clone(), picture];
3228 let inter_err = encode_sequence(&pictures, 100, 0.5).unwrap_err();3338 let inter_err = encode_sequence(&pictures, 100, 0.5).unwrap_err();
3229 assert!(3339 assert!(
@@ -3232,6 +3342,31 @@ mod tests {
3232 );3342 );
3233 }3343 }
3234 3344
3345+ /// The r15 fix at production size: three 640x360 key frames (inter is
3346+ /// still refused at this size, per the test above, so this stands in for
3347+ /// the charter's "3-frame sequence" using three independent key frames of
3348+ /// shifted content), each proven decoder-clean through ffmpeg rather than
3349+ /// only encoder-clean.
3350+ #[test]
3351+ fn a_640x360_half_straddle_round_trips_through_ffmpeg_across_three_frames() {
3352+ if !have_ffmpeg() {
3353+ eprintln!(
3354+ "SKIP a_640x360_half_straddle_round_trips_through_ffmpeg_across_three_frames: \
3355+ no ffmpeg"
3356+ );
3357+ return;
3358+ }
3359+ let (width, height) = (640usize, 360usize);
3360+ for shift in [0i64, 7, 19] {
3361+ let picture = panned_test_card(width, height, shift);
3362+ let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();
3363+ let decoded = ffmpeg_decode(&encoded.stream, width, height);
3364+ assert_eq!(decoded.y, encoded.reconstruction.y, "shift {shift}: luma");
3365+ assert_eq!(decoded.u, encoded.reconstruction.u, "shift {shift}: U");
3366+ assert_eq!(decoded.v, encoded.reconstruction.v, "shift {shift}: V");
3367+ }
3368+ }
3369+
3235 /// One frame of a real clip, scaled to a whole number of 32x32 blocks.3370 /// One frame of a real clip, scaled to a whole number of 32x32 blocks.
3236 fn clip_frame(clip: &str, skip: &str, width: usize, height: usize) -> Picture {3371 fn clip_frame(clip: &str, skip: &str, width: usize, height: usize) -> Picture {
3237 let out = Command::new("ffmpeg")3372 let out = Command::new("ffmpeg")
@@ -3762,32 +3897,117 @@ mod tests {
3762 /// fine now (see `a_frame_round_trips_at_its_own_size`) -- the true frame3897 /// fine now (see `a_frame_round_trips_at_its_own_size`) -- the true frame
3763 /// edge lands past the halfway point of whichever block it falls in, so3898 /// edge lands past the halfway point of whichever block it falls in, so
3764 /// `PARTITION_NONE` or a single gathered split flag still says everything3899 /// `PARTITION_NONE` or a single gathered split flag still says everything
3765- /// the spec needs. 40x32 and 32x48 are the sizes where the edge instead3900+ /// the spec needs. lane-av1-rect r7 wires the 8x8-leaf path live, so a
3766- /// falls *at or before* the halfway point of a 16x16 leaf: the spec's3901+ /// 16x16 leaf straddling on exactly one axis (e.g. 40x32, cols mod 32 ==
3767- /// `decode_partition` then requires that leaf to split again, to 8x8,3902+ /// 8) now encodes too (see `a_frame_round_trips_at_its_own_size`'s
3768- /// which this writer has no transform/coefficient path for yet, so it is3903+ /// sibling below). Only a leaf whose true edge cuts *both* axes (e.g.
3769- /// still refused, by name rather than by corrupting the stream.3904+ /// 40x40, both dims mod 32 == 8) needs the rectangular transform neither
3905+ /// writer has, so that class is still refused, by name rather than by
3906+ /// corrupting the stream.
3770 #[test]3907 #[test]
3771 fn a_picture_off_the_block_grid_is_refused() {3908 fn a_picture_off_the_block_grid_is_refused() {
3772- let refused = |w, h| {3909+ let msg = encode_key_frame(&Picture::grey(40, 40), 100, 0.5)
3773- let msg = encode_key_frame(&Picture::grey(w, h), 100, 0.5)3910+ .expect_err("40x40 straddles both axes of a 16x16 leaf, needs a rectangular transform")
3774- .expect_err(&format!(3911+ .to_string();
3775- "{w}x{h} needs an 8x8 split this writer does not code"3912+ assert!(msg.contains("rectangular"), "refusal: {msg}");
3776- ))3913+
3777- .to_string();
3778- msg.contains("true frame")
3779- };
3780- assert!(refused(40, 32));
3781- // 32x48 was refused here through round 9: its split quadrant only
3782- // ever has two legal 16x16 subs (the other two sit past the true
3783- // 48-row edge, block-aligned, no 8x8 split needed), but the writer
3784- // demanded four. See `ffmpeg_decodes_exactly_what_the_encoder_
3785- // reconstructed`, which now round-trips it pixel-exact instead.
3786 let mut short = Picture::grey(64, 64);3914 let mut short = Picture::grey(64, 64);
3787 short.u.truncate(10);3915 short.u.truncate(10);
3788 assert!(encode_key_frame(&short, 100, 0.5).is_err());3916 assert!(encode_key_frame(&short, 100, 0.5).is_err());
3789 }3917 }
3790 3918
3919+ /// 40x32: cols mod 32 == 8, so the true edge cuts a 16x16 leaf's own
3920+ /// column half only (`has_half` false on cols, true on rows) -- exactly
3921+ /// the single-axis straddle lane-av1-rect r7 wires through
3922+ /// `crate::tile::write_leaf8`'s 8x8 leaves. Not yet decoder-clean:
3923+ /// dav1d rejects the stream ("Invalid data found"). r7's falsification
3924+ /// candidate (`partition_w8`'s ctx reading the enclosing 16x16 slot) is
3925+ /// FALSIFIED by r8's rng trace against a debug `aomdec`: libaom's own
3926+ /// `partition_plane_context` masks at `bsl = 0` for `BLOCK_8X8`, and every
3927+ /// `partition_context_lookup` entry this writer ever produces (no 4x4
3928+ /// splits exist) has bit 0 clear, so the ctx is provably 0 for every
3929+ /// leaf regardless of neighbour state -- our writer already agreed with
3930+ /// the decoder here (`ctx=0` both sides). r8 still ported the mi-precise
3931+ /// tracking (`Neighbours::{above,left}_side_mi`, `partition_ctx_mi`) as a
3932+ /// spec-accurate fix in its own right (it matters once 4x4 exists), and
3933+ /// it does not regress the gate. r11 tell-for-tell traced two further,
3934+ /// real bugs and fixed both: (1) `write_leaf8`'s intra-mode context read
3935+ /// the *enclosing* 16x16 slot's stale `above_mode`/`left_mode` for both
3936+ /// leaves, when the second leaf's true above (or left) neighbour is the
3937+ /// first leaf itself -- fixed by threading the first leaf's mode into the
3938+ /// second leaf's context, mi-precise, like `record_mi` already does for
3939+ /// coefficients; (2) `TxbSet::Luma8`'s transform-type table was
3940+ /// `INTRA_TX_TYPE_SET1_8` (`TX_SET_INTRA_1`, 7-way, correct only when
3941+ /// `reduced_tx_set` is false), but this crate's key frames set
3942+ /// `reduced_tx_set: true`, which per `get_tx_set` (spec 5.11.48) puts
3943+ /// *every* intra size up to 16x16 -- not only 16x16 -- on `TX_SET_INTRA_2`
3944+ /// (5-way); the wrong cardinality read enough of the stream as `TX_TYPE`
3945+ /// symbol bits to desync everything after it. Fixed with a new
3946+ /// `INTRA_TX_TYPE_SET2_8` (libaom's flat default for that exact
3947+ /// eset/size slot). Both fixes are provably correct against libaom
3948+ /// source and each closed part of the gap (leaf0's `eob` went from a
3949+ /// desynced 36 to a near-miss 3, writer's real eob is 7), but a residual
3950+ /// divergence remained in the `EOB_PT` symbol's decoded value despite an
3951+ /// aligned bit cost. r12 traced that residual with a fresh dual-trace
3952+ /// (do not reuse r11's committed logs, which predate its own fixes) and
3953+ /// found two more real bugs, both now fixed: (3) `write_leaf8` hardcoded
3954+ /// `cfl: false` for `write_intra_mode`'s chroma-mode CDF choice, but
3955+ /// `is_cfl_allowed` (spec 5.11.5) is true for any block `<= 32x32` --
3956+ /// every other `write_block` caller at 16x16 and up already passes
3957+ /// `true`; the narrower `uv_mode_no_cfl` alphabet is one symbol short of
3958+ /// `uv_mode_cfl`, so even an identical `DC_PRED` decision consumed a
3959+ /// different amount of arithmetic range and derailed everything after
3960+ /// it. (4) `record()`'s `above_mode`/`left_mode` write is a no-op at an
3961+ /// 8x8 leaf's own side (`side / SUB == 0` for `SUB` = 16), and
3962+ /// `write_leaf8` called only `record_mi` (the coefficient-context half),
3963+ /// never updating those coarse arrays itself -- so a *second* straddling
3964+ /// 16x16 quadrant stacked above or left of the first (never covered by
3965+ /// r11's same-call `prev_leaf` override, which only threads state
3966+ /// between the two leaves of one shared quadrant) read whatever stale
3967+ /// mode sat in that slot before either leaf ran. Fixed by writing the
3968+ /// leaf's own mode into `above_mode[c]`/`left_mode[r]` directly. Fixing
3969+ /// (3) alone shrank the tell drift after leaf(0,8) from a decoder
3970+ /// `eob_pt=3` (wrong) vs the writer's real `eob_pt=4` down to a `tell`
3971+ /// offset of a single unit; fixing (3)+(4) together still leaves a
3972+ /// residual divergence one leaf further in -- the decoder now reads
3973+ /// leaf(2,8)'s own luma coefficient block as `txs_ctx=1` (a 32-sample
3974+ /// transform) where the writer intends `TX_8X8` (`txs_ctx=2`), meaning
3975+ /// something *before* this leaf's own `EC_TXBSKIP`/`EC_TXTYPE` still
3976+ /// desyncs the stream by exactly the width of one earlier symbol -- not
3977+ /// yet isolated to a single named symbol. r15 found the last bug: the
3978+ /// header fix from r14 explained the rng skew fully, and the remaining
3979+ /// desync (still past `mi_row=4`) was `write_leaf8`'s (4) fix itself --
3980+ /// its `above_mode[c]`/`left_mode[r]` forced write ran *inside* the leaf
3981+ /// loop, once per leaf, so the first leaf's write clobbered the true
3982+ /// external neighbour mode the second leaf of the *same* straddling
3983+ /// quadrant still needed for its non-adjacency axis (a column-straddle
3984+ /// case: two leaves share one `outer_at` column index, so leaf 1's
3985+ /// write to `left_mode[r]` overwrote the real western neighbour before
3986+ /// leaf 2 read it). Fixed by moving the write out of `write_leaf8` and
3987+ /// doing it once, after the whole leaf loop, from the last (bottom/
3988+ /// right-most) leaf's mode. The stream now round-trips through ffmpeg.
3989+ #[test]
3990+ fn a_single_axis_straddle_round_trips_through_ffmpeg() {
3991+ if !have_ffmpeg() {
3992+ eprintln!("SKIP a_single_axis_straddle_round_trips_through_ffmpeg: no ffmpeg");
3993+ return;
3994+ }
3995+ let (width, height) = (40usize, 32usize);
3996+ let picture = test_card(width, height);
3997+ let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();
3998+ assert_eq!(
3999+ (encoded.reconstruction.width, encoded.reconstruction.height),
4000+ (width, height)
4001+ );
4002+ if let Ok(path) = std::env::var("EC_AV1_DUMP") {
4003+ std::fs::write(&path, &encoded.stream).expect("dump the raw stream");
4004+ }
4005+ let decoded = ffmpeg_decode(&encoded.stream, width, height);
4006+ assert_eq!(decoded.y, encoded.reconstruction.y, "luma");
4007+ assert_eq!(decoded.u, encoded.reconstruction.u, "U");
4008+ assert_eq!(decoded.v, encoded.reconstruction.v, "V");
4009+ }
4010+
3791 /// A frame of real video, rather than a picture built to be easy.4011 /// A frame of real video, rather than a picture built to be easy.
3792 ///4012 ///
3793 /// `EC_AV1_CLIP` names the clip and `EC_AV1_CLIP_SKIP` how far into it to4013 /// `EC_AV1_CLIP` names the clip and `EC_AV1_CLIP_SKIP` how far into it to
crates/ec-av1/src/msac.rsmodified+7 -0
@@ -77,6 +77,13 @@ impl SymbolEncoder {
77 self.bits77 self.bits
78 }78 }
79 79
80+ /// The coder's current interval width — libaom's `od_ec_enc_rng` /
81+ /// decoder's `rng` field. Exposed only for the rng-register bisect trace
82+ /// (`EC_RNG=1`); not part of the bitstream contract.
83+ pub(crate) fn rng(&self) -> u32 {
84+ self.rng
85+ }
86+
80 /// The exact bit position the stream has committed to so far (libaom's87 /// The exact bit position the stream has committed to so far (libaom's
81 /// `od_ec_enc_tell`, `entenc.c`): `cnt + 10` undoes the `-9` `cnt` starts88 /// `od_ec_enc_tell`, `entenc.c`): `cnt + 10` undoes the `-9` `cnt` starts
82 /// at and reserves the one bit `finish` always spends, and `precarry`89 /// at and reserves the one bit `finish` always spends, and `precarry`
crates/ec-av1/src/tile.rsmodified+311 -57
@@ -465,6 +465,13 @@ pub struct BlockCoeffs {
465 /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key465 /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key
466 /// frame writers never read this field.466 /// frame writers never read this field.
467 pub inter: Option<InterInfo>,467 pub inter: Option<InterInfo>,
468+ /// The 8x8 leaves a straddling 16x16 block is split into (lane-av1-rect),
469+ /// each with its own 8x8 luma transform and 4x4 chroma transforms, in
470+ /// raster order among the leaves that are inside the true frame. `Some`
471+ /// overrides this `BlockCoeffs`'s own `luma`/`u`/`v`/`mode`, which are left
472+ /// at their defaults and unused, the same way a `Whole` [`Superblock`]'s
473+ /// per-quadrant fields are unused.
474+ pub eight: Option<Vec<BlockCoeffs>>,
468 }475 }
469 476
470 /// One inter mode [`sb_coeff_inter_frame_tile`]'s blocks may take (spec477 /// One inter mode [`sb_coeff_inter_frame_tile`]'s blocks may take (spec
@@ -509,8 +516,11 @@ const BLOCK: usize = 32;
509 /// Side of the chroma transform beside it at 4:2:0, and of the luma transform516 /// Side of the chroma transform beside it at 4:2:0, and of the luma transform
510 /// of a 16x16 block.517 /// of a 16x16 block.
511 const TX16: usize = 16;518 const TX16: usize = 16;
512-/// Side of the chroma transform of a 16x16 block at 4:2:0.519+/// Side of the chroma transform of a 16x16 block at 4:2:0, and of the luma
520+/// transform of an 8x8 leaf under a straddling 16x16 (lane-av1-rect).
513 const TX8: usize = 8;521 const TX8: usize = 8;
522+/// Side of the chroma transform of an 8x8 leaf at 4:2:0 (lane-av1-rect).
523+const TX4: usize = 4;
514 /// Side of a superblock, in samples, which is the size a block outside the524 /// Side of a superblock, in samples, which is the size a block outside the
515 /// tile reads as.525 /// tile reads as.
516 const SB: usize = 64;526 const SB: usize = 64;
@@ -606,6 +616,17 @@ struct Neighbours {
606 above_side: Vec<usize>,616 above_side: Vec<usize>,
607 /// The same, down the left edge.617 /// The same, down the left edge.
608 left_side: Vec<usize>,618 left_side: Vec<usize>,
619+ /// The same as `above_side`/`left_side`, but kept at the finer mi (4x4)
620+ /// granularity [`Self::above`]/[`Self::left`] are, rather than [`SUB`]:
621+ /// two 8x8 leaves of one straddling 16x16 block (lane-av1-rect) share a
622+ /// single [`SUB`]-grid cell, so a coarse array cannot tell the second
623+ /// leaf's partition symbol that the first one was coded at 8x8 (finer
624+ /// than the 16x16 it sits in) -- exactly what its `above`/`left` context
625+ /// needs to read (spec 9.3's `AbovePartitionContext`/`LeftPartitionContext`,
626+ /// which libaom keeps per mi unit for this reason).
627+ above_side_mi: Vec<usize>,
628+ /// The same, down the left edge.
629+ left_side_mi: Vec<usize>,
609 /// Whether the neighbour carried no residual, for [`sb_coeff_inter_frame_tile`]'s630 /// Whether the neighbour carried no residual, for [`sb_coeff_inter_frame_tile`]'s
610 /// skip context. Unused (and left at its default) by the key frame writers.631 /// skip context. Unused (and left at its default) by the key frame writers.
611 above_skip: Vec<bool>,632 above_skip: Vec<bool>,
@@ -653,6 +674,8 @@ impl Neighbours {
653 left_mode: vec![DC_PRED; rows],674 left_mode: vec![DC_PRED; rows],
654 above_side: vec![SB; cols],675 above_side: vec![SB; cols],
655 left_side: vec![SB; rows],676 left_side: vec![SB; rows],
677+ above_side_mi: vec![SB; cols * (SUB / MI)],
678+ left_side_mi: vec![SB; rows * (SUB / MI)],
656 above_skip: vec![false; cols],679 above_skip: vec![false; cols],
657 left_skip: vec![false; rows],680 left_skip: vec![false; rows],
658 above_inter: vec![false; cols],681 above_inter: vec![false; cols],
@@ -667,6 +690,7 @@ impl Neighbours {
667 self.left.iter_mut().for_each(|l| *l = Default::default());690 self.left.iter_mut().for_each(|l| *l = Default::default());
668 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);691 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);
669 self.left_side.iter_mut().for_each(|s| *s = SB);692 self.left_side.iter_mut().for_each(|s| *s = SB);
693+ self.left_side_mi.iter_mut().for_each(|s| *s = SB);
670 self.left_skip.iter_mut().for_each(|s| *s = false);694 self.left_skip.iter_mut().for_each(|s| *s = false);
671 self.left_inter.iter_mut().for_each(|i| *i = false);695 self.left_inter.iter_mut().for_each(|i| *i = false);
672 }696 }
@@ -680,14 +704,30 @@ impl Neighbours {
680 /// side).704 /// side).
681 fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) {705 fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) {
682 let (r, c) = at;706 let (r, c) = at;
683- let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane]));
684 for cell in 0..side / SUB {707 for cell in 0..side / SUB {
685 self.above_mode[c + cell] = mode;708 self.above_mode[c + cell] = mode;
686 self.left_mode[r + cell] = mode;709 self.left_mode[r + cell] = mode;
687 self.above_side[c + cell] = side;710 self.above_side[c + cell] = side;
688 self.left_side[r + cell] = side;711 self.left_side[r + cell] = side;
689 }712 }
690- let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI);713+ self.record_mi((r * (SUB / MI), c * (SUB / MI)), side, grids);
714+ }
715+
716+ /// The coefficient-context half of [`Self::record`], taking the block's
717+ /// position directly in 4x4 mode-info units rather than [`SUB`]-grid
718+ /// (r, c): an 8x8 leaf under a straddling 16x16 (lane-av1-rect) sits at a
719+ /// mi offset [`Self::record`]'s SUB-unit `at` cannot name, but the
720+ /// `above`/`left` arrays it writes into are already sized to the full mi
721+ /// grid, so no resizing is needed to write into them at this
722+ /// finer-than-SUB granularity.
723+ fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[Vec<i32>; 3]) {
724+ let (mi_r, mi_c) = at_mi;
725+ let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane]));
726+ let side_mi = side / MI;
727+ for cell in 0..side_mi {
728+ self.left_side_mi[mi_r + cell] = side;
729+ self.above_side_mi[mi_c + cell] = side;
730+ }
691 // libaom rounds the luma edge up to the plane's own 4x4 unit before731 // libaom rounds the luma edge up to the plane's own 4x4 unit before
692 // clamping a subsampled plane (`ROUND_POWER_OF_TWO(max_blocks_high,732 // clamping a subsampled plane (`ROUND_POWER_OF_TWO(max_blocks_high,
693 // subsampling_y)` in av1_write_intra_coeffs_mb, encodetxb.c:456-459):733 // subsampling_y)` in av1_write_intra_coeffs_mb, encodetxb.c:456-459):
@@ -741,7 +781,13 @@ impl Neighbours {
741 /// The gathered state of the blocks above and to the left of one block,781 /// The gathered state of the blocks above and to the left of one block,
742 /// per plane.782 /// per plane.
743 fn around(&self, (r, c): (usize, usize), side: usize) -> [Around; 3] {783 fn around(&self, (r, c): (usize, usize), side: usize) -> [Around; 3] {
744- let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI);784+ self.around_mi((r * (SUB / MI), c * (SUB / MI)), side)
785+ }
786+
787+ /// [`Self::around`] taking the block's position directly in 4x4 mode-info
788+ /// units, for the same reason [`Self::record_mi`] does.
789+ fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [Around; 3] {
790+ let side_mi = side / MI;
745 std::array::from_fn(|plane| {791 std::array::from_fn(|plane| {
746 let mut around = Around::default();792 let mut around = Around::default();
747 for cell in 0..side_mi {793 for cell in 0..side_mi {
@@ -758,8 +804,23 @@ impl Neighbours {
758 }804 }
759 805
760 fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize {806 fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize {
807+ // Delegates to the mi-precise reader (same pattern as `around` /
808+ // `around_mi`): `above_side`/`left_side` are only ever advanced in
809+ // whole-[`SUB`] steps by [`Self::record`], so a leaf8's `record_mi`
810+ // (lane-av1-rect) -- which only touches the finer mi arrays -- leaves
811+ // them stale for the *next* sibling's own partition symbol, reading a
812+ // 16x16-slot side that already split into 8x8s underneath it.
761 let (r, c) = at;813 let (r, c) = at;
762- 2 * usize::from(self.left_side[r] * 2 <= side) + usize::from(self.above_side[c] * 2 <= side)814+ self.partition_ctx_mi((r * (SUB / MI), c * (SUB / MI)), side)
815+ }
816+
817+ /// [`Self::partition_ctx`] at mi granularity, for an 8x8 leaf of a
818+ /// straddling 16x16 block: reads the finer `above_side_mi`/`left_side_mi`
819+ /// arrays so the second leaf sees the first leaf's own 8x8 side rather
820+ /// than the enclosing 16x16 slot's stale, shared state.
821+ fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize {
822+ 2 * usize::from(self.left_side_mi[mi_r] * 2 <= side)
823+ + usize::from(self.above_side_mi[mi_c] * 2 <= side)
763 }824 }
764 }825 }
765 826
@@ -800,7 +861,12 @@ pub fn sb_coeff_key_frame_tile(
800 match superblock {861 match superblock {
801 Superblock::Whole(block) => coded.push(block),862 Superblock::Whole(block) => coded.push(block),
802 Superblock::Split(quadrants) => {863 Superblock::Split(quadrants) => {
803- coded.extend(quadrants.iter().flat_map(Quadrant::blocks));864+ for block in quadrants.iter().flat_map(Quadrant::blocks) {
865+ match &block.eight {
866+ Some(leaves) => coded.extend(leaves.iter()),
867+ None => coded.push(block),
868+ }
869+ }
804 }870 }
805 }871 }
806 }872 }
@@ -820,7 +886,12 @@ pub fn sb_coeff_key_frame_tile(
820 let sub_planes = [TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8];886 let sub_planes = [TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8];
821 let split_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16];887 let split_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16];
822 let whole_planes = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32];888 let whole_planes = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32];
823- let scans = [default_scan(TX32), default_scan(TX16), default_scan(TX8)];889+ let scans = [
890+ default_scan(TX32),
891+ default_scan(TX16),
892+ default_scan(TX8),
893+ default_scan(TX4),
894+ ];
824 // The blocks above and to the left, on the 16x16 grid. The left edge is895 // The blocks above and to the left, on the 16x16 grid. The left edge is
825 // reset at every superblock row because a tile starts each row with no896 // reset at every superblock row because a tile starts each row with no
826 // left neighbour.897 // left neighbour.
@@ -1027,26 +1098,14 @@ pub fn sb_coeff_key_frame_tile(
1027 (false, false) => {}1098 (false, false) => {}
1028 }1099 }
1029 for (block, (sr, sc)) in blocks.iter().zip(sub_positions) {1100 for (block, (sr, sc)) in blocks.iter().zip(sub_positions) {
1030- // A 16x16 leaf's own hasRows/hasCols: this1101+ // A 16x16 leaf's own hasRows/hasCols,
1031- // writer has no smaller partition than1102+ // recomputed at this leaf's own half
1032- // 16x16 (no 8x8 or rectangular transform),1103+ // (same three-way signaling as the 32x32
1033- // so a leaf the true edge itself cuts1104+ // and 64x64 levels above).
1034- // through -- legal in the spec only via a
1035- // `PARTITION_HORZ`/`VERT` this writer does
1036- // not code -- is refused rather than
1037- // silently miscoded.
1038 let (has_cols16, has_rows16) = (1105 let (has_cols16, has_rows16) = (
1039 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),1106 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),
1040 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),1107 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),
1041 );1108 );
1042- if !has_cols16 || !has_rows16 {
1043- return Err(Error::unsupported(
1044- "AV1 tile",
1045- "a 16x16 block the true frame edge cuts through \
1046- needs a rectangular transform this writer does \
1047- not code yet",
1048- ));
1049- }
1050 let at = (sr, sc);1109 let at = (sr, sc);
1051 let ctx = neighbours.partition_ctx(at, SUB);1110 let ctx = neighbours.partition_ctx(at, SUB);
1052 ec_rng_trace(|| {1111 ec_rng_trace(|| {
@@ -1058,32 +1117,156 @@ pub fn sb_coeff_key_frame_tile(
1058 enc.tell()1117 enc.tell()
1059 )1118 )
1060 });1119 });
1061- enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx]);1120+ if has_cols16 && has_rows16 {
1062- let grids = [1121+ enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx]);
1063- level_grid(&block.luma, TX16)?,1122+ let grids = [
1064- level_grid(&block.u, TX8)?,1123+ level_grid(&block.luma, TX16)?,
1065- level_grid(&block.v, TX8)?,1124+ level_grid(&block.u, TX8)?,
1066- ];1125+ level_grid(&block.v, TX8)?,
1067- write_block(1126+ ];
1068- &mut enc,1127+ write_block(
1069- &mut cdfs,1128+ &mut enc,
1070- &mut neighbours,1129+ &mut cdfs,
1071- block,1130+ &mut neighbours,
1072- at,1131+ block,
1073- SUB,1132+ at,
1074- &sub_planes,1133+ SUB,
1075- &grids,1134+ &sub_planes,
1076- [&scans[1], &scans[2], &scans[2]],1135+ &grids,
1077- true,1136+ [&scans[1], &scans[2], &scans[2]],
1078- );1137+ true,
1079- ec_rng_trace(|| {1138+ );
1080- format!(1139+ ec_rng_trace(|| {
1081- "EC_TOK mi_row={} mi_col={} tell={}",1140+ format!(
1082- at.0 * 4,1141+ "EC_TOK mi_row={} mi_col={} tell={}",
1083- at.1 * 4,1142+ at.0 * 4,
1084- enc.tell()1143+ at.1 * 4,
1144+ enc.tell()
1145+ )
1146+ });
1147+ continue;
1148+ }
1149+ // The true edge falls inside this 16x16
1150+ // leaf itself (mod-32==8 target sizes):
1151+ // one axis only, per r6's charter -- an
1152+ // 8x8 leaf never itself straddles, so the
1153+ // block splits cleanly along whichever
1154+ // axis is short and only the in-frame
1155+ // 8x8s are coded, each its own leaf.
1156+ if !has_cols16 && !has_rows16 {
1157+ return Err(Error::unsupported(
1158+ "AV1 tile",
1159+ "a 16x16 block whose true edge cuts through both \
1160+ axes needs a rectangular transform this writer \
1161+ does not code yet",
1162+ ));
1163+ }
1164+ if has_cols16 {
1165+ enc.symbol_fixed(
1166+ 1,
1167+ &gather(&cdfs.partition_w16[ctx], VERT_ALIKE),
1168+ );
1169+ } else {
1170+ enc.symbol_fixed(
1171+ 1,
1172+ &gather(&cdfs.partition_w16[ctx], HORZ_ALIKE),
1173+ );
1174+ }
1175+ let leaves = block.eight.as_ref().ok_or_else(|| {
1176+ Error::unsupported(
1177+ "AV1 tile",
1178+ "a 16x16 block the true frame edge cuts through \
1179+ needs its `eight` leaves populated",
1085 )1180 )
1086- });1181+ })?;
1182+ let (mi_row0, mi_col0) =
1183+ (sr as u32 * SUB_MI, sc as u32 * SUB_MI);
1184+ let leaf_positions: Vec<(u32, u32)> = (0..4)
1185+ .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2))
1186+ .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols)
1187+ .collect();
1188+ if leaves.len() != leaf_positions.len() {
1189+ return Err(Error::unsupported(
1190+ "AV1 tile",
1191+ "a straddling 16x16 block needs one `eight` entry \
1192+ per 8x8 leaf inside the true frame",
1193+ ));
1194+ }
1195+ // r11: the enclosing 16x16 slot's
1196+ // above_mode/left_mode arrays are too
1197+ // coarse for a second leaf whose true
1198+ // above (or left) neighbour is the FIRST
1199+ // leaf -- track it here and hand it to
1200+ // write_leaf8 as a context override.
1201+ let mut prev_leaf: Option<((usize, usize), usize)> = None;
1202+ for (leaf, (mr, mc)) in leaves.iter().zip(leaf_positions) {
1203+ let leaf_mi = (mr as usize, mc as usize);
1204+ // r8: read at mi granularity, not the
1205+ // enclosing 16x16 slot -- the first
1206+ // leaf's `record_mi` call below
1207+ // updates `above_side_mi`/
1208+ // `left_side_mi` at this leaf's own
1209+ // mi position, which the second
1210+ // leaf's ctx lookup then sees.
1211+ let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
1212+ ec_rng_trace(|| {
1213+ format!(
1214+ "EC_PART mi_row={} mi_col={} bsize=3 ctx={} tell={}",
1215+ leaf_mi.0,
1216+ leaf_mi.1,
1217+ leaf_ctx,
1218+ enc.tell()
1219+ )
1220+ });
1221+ enc.symbol(
1222+ PARTITION_NONE,
1223+ &mut cdfs.partition_w8[leaf_ctx],
1224+ );
1225+ let grids = [
1226+ level_grid(&leaf.luma, TX8)?,
1227+ level_grid(&leaf.u, TX4)?,
1228+ level_grid(&leaf.v, TX4)?,
1229+ ];
1230+ let leaf_mode = write_leaf8(
1231+ &mut enc,
1232+ &mut cdfs,
1233+ &mut neighbours,
1234+ leaf,
1235+ at,
1236+ leaf_mi,
1237+ &grids,
1238+ [&scans[2], &scans[3], &scans[3]],
1239+ prev_leaf,
1240+ );
1241+ prev_leaf = Some((leaf_mi, leaf_mode));
1242+ ec_rng_trace(|| {
1243+ format!(
1244+ "EC_TOK mi_row={} mi_col={} tell={}",
1245+ leaf_mi.0,
1246+ leaf_mi.1,
1247+ enc.tell()
1248+ )
1249+ });
1250+ }
1251+ // `record()`'s `above_mode`/`left_mode`
1252+ // write is a no-op at an 8x8 leaf's own
1253+ // side (`side / SUB == 0`), so the next
1254+ // 16x16 quadrant beyond this straddling
1255+ // one would otherwise see whatever stale
1256+ // mode sat here before it: force the
1257+ // write once the whole quadrant's leaves
1258+ // are done, from the last (bottom/right-
1259+ // most) leaf -- lane-av1-rect r15: doing
1260+ // this *inside* `write_leaf8`, once per
1261+ // leaf, let the first leaf's write
1262+ // clobber the true external neighbour a
1263+ // second leaf of the *same* quadrant
1264+ // still needed to read on its
1265+ // non-adjacency axis.
1266+ if let Some((_, mode)) = prev_leaf {
1267+ neighbours.above_mode[at.1] = mode;
1268+ neighbours.left_mode[at.0] = mode;
1269+ }
1087 }1270 }
1088 }1271 }
1089 }1272 }
@@ -1113,6 +1296,7 @@ fn write_intra_mode(
1113 mode,1296 mode,
1114 &mut cdfs.kf_y_mode[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]],1297 &mut cdfs.kf_y_mode[INTRA_MODE_CTX[above_mode]][INTRA_MODE_CTX[left_mode]],
1115 );1298 );
1299+ ec_rng_trace(|| format!("EC_YMODE mode={mode} tell={} rng={}", enc.tell(), enc.rng()));
1116 if (V_PRED..=D67_PRED).contains(&mode) {1300 if (V_PRED..=D67_PRED).contains(&mode) {
1117 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]);1301 enc.symbol(ANGLE_DELTA_ZERO, &mut cdfs.angle_delta[mode - V_PRED]);
1118 }1302 }
@@ -1163,6 +1347,62 @@ fn write_block(
1163 neighbours.record(at, side, mode, grids);1347 neighbours.record(at, side, mode, grids);
1164 }1348 }
1165 1349
1350+/// Writes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect): its own
1351+/// luma transform and 4x4 chroma transforms, coded exactly like
1352+/// [`write_block`] but reading its intra-mode context from the *enclosing*
1353+/// 16x16 slot -- `outer_at`, in [`SUB`]-grid units -- rather than from its own
1354+/// finer position, since [`Neighbours`]'s `above_mode`/`left_mode` arrays stay
1355+/// at [`SUB`] (16-sample) granularity. `leaf_mi` is this leaf's own position
1356+/// in 4x4 mode-info units, which is what its coefficient context (finer than
1357+/// [`SUB`]) is kept and read at.
1358+#[allow(clippy::too_many_arguments)]
1359+fn write_leaf8(
1360+ enc: &mut SymbolEncoder,
1361+ cdfs: &mut Cdfs,
1362+ neighbours: &mut Neighbours,
1363+ block: &BlockCoeffs,
1364+ outer_at: (usize, usize),
1365+ leaf_mi: (usize, usize),
1366+ grids: &[Vec<i32>; 3],
1367+ scans: [&Vec<u16>; 3],
1368+ prev_leaf: Option<((usize, usize), usize)>,
1369+) -> usize {
1370+ let (r, c) = outer_at;
1371+ let mut above_mode = neighbours.above_mode[c];
1372+ let mut left_mode = neighbours.left_mode[r];
1373+ // The previous leaf sits directly above (same column, two mi rows up)
1374+ // or directly to the left (same row, two mi cols over) of this one --
1375+ // in either case its just-written mode, not the enclosing 16x16 slot's
1376+ // stale neighbour, is what a decoder reads as this leaf's context.
1377+ if let Some(((pr, pc), pmode)) = prev_leaf {
1378+ if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 {
1379+ above_mode = pmode;
1380+ } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 {
1381+ left_mode = pmode;
1382+ }
1383+ }
1384+ // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec
1385+ // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF -- like every
1386+ // other `write_block` caller at 16x16 and up -- not the narrower
1387+ // no-CFL one: r12 lane-av1-rect, this leaf's own `cfl: false` was the
1388+ // true first divergence (a differently-sized alphabet under the same
1389+ // DC_PRED decision desyncs the coder even though the decoded mode is
1390+ // unchanged).
1391+ let mode = write_intra_mode(enc, cdfs, block, above_mode, left_mode, true);
1392+ let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4];
1393+ write_block_planes(
1394+ enc,
1395+ cdfs,
1396+ &planes,
1397+ grids,
1398+ &scans,
1399+ &neighbours.around_mi(leaf_mi, 8),
1400+ mode,
1401+ );
1402+ neighbours.record_mi(leaf_mi, 8, grids);
1403+ mode
1404+}
1405+
1166 /// Writes the three transform blocks of one coded block, in the order a1406 /// Writes the three transform blocks of one coded block, in the order a
1167 /// decoder reads them.1407 /// decoder reads them.
1168 #[allow(clippy::too_many_arguments)]1408 #[allow(clippy::too_many_arguments)]
@@ -1331,14 +1571,21 @@ pub(crate) fn luma_32_coeff_bits(grid: &[i32]) -> f64 {
1331 pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet) -> f64 {1571 pub(crate) fn coeff_bits(grid: &[i32], set: TxbSet) -> f64 {
1332 /// Built once per size: the search prices thirteen modes for every block,1572 /// Built once per size: the search prices thirteen modes for every block,
1333 /// and the scan is the same table every time.1573 /// and the scan is the same table every time.
1334- static SCANS: LazyLock<[Vec<u16>; 3]> =1574+ static SCANS: LazyLock<[Vec<u16>; 4]> = LazyLock::new(|| {
1335- LazyLock::new(|| [default_scan(TX8), default_scan(TX16), default_scan(TX32)]);1575+ [
1576+ default_scan(TX4),
1577+ default_scan(TX8),
1578+ default_scan(TX16),
1579+ default_scan(TX32),
1580+ ]
1581+ });
1336 let mut cdfs = Cdfs::new(2);1582 let mut cdfs = Cdfs::new(2);
1337 let mut coding = cdfs.txb(set, DC_PRED);1583 let mut coding = cdfs.txb(set, DC_PRED);
1338 let scan = match coding.side {1584 let scan = match coding.side {
1339- TX8 => &SCANS[0],1585+ TX4 => &SCANS[0],
1340- TX16 => &SCANS[1],1586+ TX8 => &SCANS[1],
1341- _ => &SCANS[2],1587+ TX16 => &SCANS[2],
1588+ _ => &SCANS[3],
1342 };1589 };
1343 let mut enc = SymbolEncoder::new();1590 let mut enc = SymbolEncoder::new();
1344 enc.reset_bits();1591 enc.reset_bits();
@@ -1397,9 +1644,10 @@ fn write_coeffs(
1397 if plane.is_some() {1644 if plane.is_some() {
1398 ec_rng_trace(|| {1645 ec_rng_trace(|| {
1399 format!(1646 format!(
1400- "EC_TXBSKIP ctx={skip_ctx} eob0={} tell={}",1647+ "EC_TXBSKIP ctx={skip_ctx} eob0={} tell={} rng={}",
1401 usize::from(eob == 0),1648 usize::from(eob == 0),
1402- enc.tell()1649+ enc.tell(),
1650+ enc.rng()
1403 )1651 )
1404 });1652 });
1405 }1653 }
@@ -1412,7 +1660,7 @@ fn write_coeffs(
1412 if let Some(tx_type) = coding.tx_type.as_deref_mut() {1660 if let Some(tx_type) = coding.tx_type.as_deref_mut() {
1413 enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type);1661 enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type);
1414 if plane.is_some() {1662 if plane.is_some() {
1415- ec_rng_trace(|| format!("EC_TXTYPE tell={}", enc.tell()));1663+ ec_rng_trace(|| format!("EC_TXTYPE tell={} rng={}", enc.tell(), enc.rng()));
1416 }1664 }
1417 }1665 }
1418 1666
@@ -1525,7 +1773,13 @@ fn write_eob(enc: &mut SymbolEncoder, coding: &mut TxbTables, eob: usize, plane:
1525 // reaches is the size of its own end-of-block alphabet.1773 // reaches is the size of its own end-of-block alphabet.
1526 enc.symbol(group - 1, coding.eob_pt);1774 enc.symbol(group - 1, coding.eob_pt);
1527 if let Some(plane) = plane {1775 if let Some(plane) = plane {
1528- ec_rng_trace(|| format!("EC_EOBPT plane={plane} eob_pt={group} tell={}", enc.tell()));1776+ ec_rng_trace(|| {
1777+ format!(
1778+ "EC_EOBPT plane={plane} eob_pt={group} tell={} rng={}",
1779+ enc.tell(),
1780+ enc.rng()
1781+ )
1782+ });
1529 }1783 }
1530 1784
1531 let bits = OFFSET_BITS[group];1785 let bits = OFFSET_BITS[group];