Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

25.5 KB
Raw Blame
//! Normative constant tables from ITU-T H.264 (scans, CABAC state machine and initialisation values,
//! CAVLC code tables, quantisation/deblocking tables).

/// 4x4 frame zig-zag scan: scan index -> raster index (y*4+x).
pub const ZIGZAG4: [usize; 16] = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15];

/// 8x8 frame zig-zag scan: scan index -> raster index (y*8+x).
pub const ZIGZAG8: [usize; 64] = [
    0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29,
    22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
];

/// Luma 4x4 block index (decoding order) -> (x, y) in 4x4 units within the macroblock.
pub const BLK4_XY: [(usize, usize); 16] =
    [(0, 0), (1, 0), (0, 1), (1, 1), (2, 0), (3, 0), (2, 1), (3, 1), (0, 2), (1, 2), (0, 3), (1, 3), (2, 2), (3, 2), (2, 3), (3, 3)];

/// Raster 4x4 position (y*4+x) -> luma 4x4 block index.
pub const RASTER_TO_BLK4: [usize; 16] = [0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15];

/// rangeTabLPS[pStateIdx][qCodIRangeIdx].
pub const RANGE_LPS: [[u8; 4]; 64] = [
    [128, 176, 208, 240],
    [128, 167, 197, 227],
    [128, 158, 187, 216],
    [123, 150, 178, 205],
    [116, 142, 169, 195],
    [111, 135, 160, 185],
    [105, 128, 152, 175],
    [100, 122, 144, 166],
    [95, 116, 137, 158],
    [90, 110, 130, 150],
    [85, 104, 123, 142],
    [81, 99, 117, 135],
    [77, 94, 111, 128],
    [73, 89, 105, 122],
    [69, 85, 100, 116],
    [66, 80, 95, 110],
    [62, 76, 90, 104],
    [59, 72, 86, 99],
    [56, 69, 81, 94],
    [53, 65, 77, 89],
    [51, 62, 73, 85],
    [48, 59, 69, 80],
    [46, 56, 66, 76],
    [43, 53, 63, 72],
    [41, 50, 59, 69],
    [39, 48, 56, 65],
    [37, 45, 54, 62],
    [35, 43, 51, 59],
    [33, 41, 48, 56],
    [32, 39, 46, 53],
    [30, 37, 43, 50],
    [29, 35, 41, 48],
    [27, 33, 39, 45],
    [26, 31, 37, 43],
    [24, 30, 35, 41],
    [23, 28, 33, 39],
    [22, 27, 32, 37],
    [21, 26, 30, 35],
    [20, 24, 29, 33],
    [19, 23, 27, 31],
    [18, 22, 26, 30],
    [17, 21, 25, 28],
    [16, 20, 23, 27],
    [15, 19, 22, 25],
    [14, 18, 21, 24],
    [14, 17, 20, 23],
    [13, 16, 19, 22],
    [12, 15, 18, 21],
    [12, 14, 17, 20],
    [11, 14, 16, 19],
    [11, 13, 15, 18],
    [10, 12, 15, 17],
    [10, 12, 14, 16],
    [9, 11, 13, 15],
    [9, 11, 12, 14],
    [8, 10, 12, 14],
    [8, 9, 11, 13],
    [7, 9, 11, 12],
    [7, 9, 10, 12],
    [7, 8, 10, 11],
    [6, 8, 9, 11],
    [6, 7, 9, 10],
    [6, 7, 8, 9],
    [2, 2, 2, 2],
];

/// transIdxLPS.
pub const TRANS_LPS: [u8; 64] = [
    0, 0, 1, 2, 2, 4, 4, 5, 6, 7, 8, 9, 9, 11, 11, 12, 13, 13, 15, 15, 16, 16, 18, 18, 19, 19, 21, 21, 22, 22, 23, 24, 24, 25, 26, 26, 27, 27, 28, 29, 29, 30,
    30, 30, 31, 32, 32, 33, 33, 33, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 63,
];

/// Number of CABAC contexts we keep (0..=459 covers every frame-coded 4:2:0 syntax element).
pub const NUM_CTX: usize = 460;

/// Context initialisation (m, n) for I slices. Field-coding contexts are left as zero (never used).
pub fn cabac_init_i() -> [(i8, i8); NUM_CTX] {
    let mut t = [(0i8, 0i8); NUM_CTX];
    set(&mut t, 0, &COMMON_0_10);
    set(&mut t, 60, &COMMON_60_69);
    set(&mut t, 70, &I_70_104);
    set(&mut t, 105, &I_105_165);
    set(&mut t, 166, &I_166_226);
    set(&mut t, 227, &I_227_275);
    set(&mut t, 399, &I_399_401);
    set(&mut t, 402, &I_402_435);
    t
}

/// Context initialisation (m, n) for P/B slices with cabac_init_idc = 0.
pub fn cabac_init_pb0() -> [(i8, i8); NUM_CTX] {
    let mut t = [(0i8, 0i8); NUM_CTX];
    set(&mut t, 0, &COMMON_0_10);
    set(&mut t, 11, &PB0_11_59);
    set(&mut t, 60, &COMMON_60_69);
    set(&mut t, 70, &PB0_70_104);
    set(&mut t, 105, &PB0_105_165);
    set(&mut t, 166, &PB0_166_226);
    set(&mut t, 227, &PB0_227_275);
    set(&mut t, 399, &PB0_399_401);
    set(&mut t, 402, &PB0_402_435);
    t
}

fn set(t: &mut [(i8, i8); NUM_CTX], start: usize, v: &[(i8, i8)]) {
    t[start..start + v.len()].copy_from_slice(v);
}

const COMMON_0_10: [(i8, i8); 11] = [(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104), (-6, 53), (-1, 54), (7, 51)];

const COMMON_60_69: [(i8, i8); 10] = [(0, 41), (0, 63), (0, 63), (0, 63), (-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62)];

const PB0_11_59: [(i8, i8); 49] = [
    // 11..23 P mb_skip / mb_type / sub_mb_type
    (23, 33),
    (23, 2),
    (21, 0),
    (1, 9),
    (0, 49),
    (-37, 118),
    (5, 57),
    (-13, 78),
    (-11, 65),
    (1, 62),
    (12, 49),
    (-4, 73),
    (17, 50),
    // 24..39 B mb_skip / mb_type / sub_mb_type
    (18, 64),
    (9, 43),
    (29, 0),
    (26, 67),
    (16, 90),
    (9, 104),
    (-46, 127),
    (-20, 104),
    (1, 67),
    (-13, 78),
    (-11, 65),
    (1, 62),
    (-6, 86),
    (-17, 95),
    (-6, 61),
    (9, 45),
    // 40..53 mvd
    (-3, 69),
    (-6, 81),
    (-11, 96),
    (6, 55),
    (7, 67),
    (-5, 86),
    (2, 88),
    (0, 58),
    (-3, 76),
    (-10, 94),
    (5, 54),
    (4, 69),
    (-3, 81),
    (0, 88),
    // 54..59 ref_idx
    (-7, 67),
    (-5, 74),
    (-4, 74),
    (-5, 80),
    (-7, 72),
    (1, 58),
];

const I_70_104: [(i8, i8); 35] = [
    (0, 11),
    (1, 55),
    (0, 69),
    (-17, 127),
    (-13, 102),
    (0, 82),
    (-7, 74),
    (-21, 107),
    (-27, 127),
    (-31, 127),
    (-24, 127),
    (-18, 95),
    (-27, 127),
    (-21, 114),
    (-30, 127),
    (-17, 123),
    (-12, 115),
    (-16, 122),
    (-11, 115),
    (-12, 63),
    (-2, 68),
    (-15, 84),
    (-13, 104),
    (-3, 70),
    (-8, 93),
    (-10, 90),
    (-30, 127),
    (-1, 74),
    (-6, 97),
    (-7, 91),
    (-20, 127),
    (-4, 56),
    (-5, 82),
    (-7, 76),
    (-22, 125),
];

const PB0_70_104: [(i8, i8); 35] = [
    (0, 45),
    (-4, 78),
    (-3, 96),
    (-27, 126),
    (-28, 98),
    (-25, 101),
    (-23, 67),
    (-28, 82),
    (-20, 94),
    (-16, 83),
    (-22, 110),
    (-21, 91),
    (-18, 102),
    (-13, 93),
    (-29, 127),
    (-7, 92),
    (-5, 89),
    (-7, 96),
    (-13, 108),
    (-3, 46),
    (-1, 65),
    (-1, 57),
    (-9, 93),
    (-3, 74),
    (-9, 92),
    (-8, 87),
    (-23, 126),
    (5, 54),
    (6, 60),
    (6, 59),
    (6, 69),
    (-1, 48),
    (0, 68),
    (-4, 69),
    (-8, 88),
];

const I_105_165: [(i8, i8); 61] = [
    (-7, 93),
    (-11, 87),
    (-3, 77),
    (-5, 71),
    (-4, 63),
    (-4, 68),
    (-12, 84),
    (-7, 62),
    (-7, 65),
    (8, 61),
    (5, 56),
    (-2, 66),
    (1, 64),
    (0, 61),
    (-2, 78),
    (1, 50),
    (7, 52),
    (10, 35),
    (0, 44),
    (11, 38),
    (1, 45),
    (0, 46),
    (5, 44),
    (31, 17),
    (1, 51),
    (7, 50),
    (28, 19),
    (16, 33),
    (14, 62),
    (-13, 108),
    (-15, 100),
    (-13, 101),
    (-13, 91),
    (-12, 94),
    (-10, 88),
    (-16, 84),
    (-10, 86),
    (-7, 83),
    (-13, 87),
    (-19, 94),
    (1, 70),
    (0, 72),
    (-5, 74),
    (18, 59),
    (-8, 102),
    (-15, 100),
    (0, 95),
    (-4, 75),
    (2, 72),
    (-11, 75),
    (-3, 71),
    (15, 46),
    (-13, 69),
    (0, 62),
    (0, 65),
    (21, 37),
    (-15, 72),
    (9, 57),
    (16, 54),
    (0, 62),
    (12, 72),
];

const PB0_105_165: [(i8, i8); 61] = [
    (-2, 85),
    (-6, 78),
    (-1, 75),
    (-7, 77),
    (2, 54),
    (5, 50),
    (-3, 68),
    (1, 50),
    (6, 42),
    (-4, 81),
    (1, 63),
    (-4, 70),
    (0, 67),
    (2, 57),
    (-2, 76),
    (11, 35),
    (4, 64),
    (1, 61),
    (11, 35),
    (18, 25),
    (12, 24),
    (13, 29),
    (13, 36),
    (-10, 93),
    (-7, 73),
    (-2, 73),
    (13, 46),
    (9, 49),
    (-7, 100),
    (9, 53),
    (2, 53),
    (5, 53),
    (-2, 61),
    (0, 56),
    (0, 56),
    (-13, 63),
    (-5, 60),
    (-1, 62),
    (4, 57),
    (-6, 69),
    (4, 57),
    (14, 39),
    (4, 51),
    (13, 68),
    (3, 64),
    (1, 61),
    (9, 63),
    (7, 50),
    (16, 39),
    (5, 44),
    (4, 52),
    (11, 48),
    (-5, 60),
    (-1, 59),
    (0, 59),
    (22, 33),
    (5, 44),
    (14, 43),
    (-1, 78),
    (0, 60),
    (9, 69),
];

const I_166_226: [(i8, i8); 61] = [
    (24, 0),
    (15, 9),
    (8, 25),
    (13, 18),
    (15, 9),
    (13, 19),
    (10, 37),
    (12, 18),
    (6, 29),
    (20, 33),
    (15, 30),
    (4, 45),
    (1, 58),
    (0, 62),
    (7, 61),
    (12, 38),
    (11, 45),
    (15, 39),
    (11, 42),
    (13, 44),
    (16, 45),
    (12, 41),
    (10, 49),
    (30, 34),
    (18, 42),
    (10, 55),
    (17, 51),
    (17, 46),
    (0, 89),
    (26, -19),
    (22, -17),
    (26, -17),
    (30, -25),
    (28, -20),
    (33, -23),
    (37, -27),
    (33, -23),
    (40, -28),
    (38, -17),
    (33, -11),
    (40, -15),
    (41, -6),
    (38, 1),
    (41, 17),
    (30, -6),
    (27, 3),
    (26, 22),
    (37, -16),
    (35, -4),
    (38, -8),
    (38, -3),
    (37, 3),
    (38, 5),
    (42, 0),
    (35, 16),
    (39, 22),
    (14, 48),
    (27, 37),
    (21, 60),
    (12, 68),
    (2, 97),
];

const PB0_166_226: [(i8, i8); 61] = [
    (11, 28),
    (2, 40),
    (3, 44),
    (0, 49),
    (0, 46),
    (2, 44),
    (2, 51),
    (0, 47),
    (4, 39),
    (2, 62),
    (6, 46),
    (0, 54),
    (3, 54),
    (2, 58),
    (4, 63),
    (6, 51),
    (6, 57),
    (7, 53),
    (6, 52),
    (6, 55),
    (11, 45),
    (14, 36),
    (8, 53),
    (-1, 82),
    (7, 55),
    (-3, 78),
    (15, 46),
    (22, 31),
    (-1, 84),
    (25, 7),
    (30, -7),
    (28, 3),
    (28, 4),
    (32, 0),
    (34, -1),
    (30, 6),
    (30, 6),
    (32, 9),
    (31, 19),
    (26, 27),
    (26, 30),
    (37, 20),
    (28, 34),
    (17, 70),
    (1, 67),
    (5, 59),
    (9, 67),
    (16, 30),
    (18, 32),
    (18, 35),
    (22, 29),
    (24, 31),
    (23, 38),
    (18, 43),
    (20, 41),
    (11, 63),
    (9, 59),
    (9, 64),
    (-1, 94),
    (-2, 89),
    (-9, 108),
];

const I_227_275: [(i8, i8); 49] = [
    (-3, 71),
    (-6, 42),
    (-5, 50),
    (-3, 54),
    (-2, 62),
    (0, 58),
    (1, 63),
    (-2, 72),
    (-1, 74),
    (-9, 91),
    (-5, 67),
    (-5, 27),
    (-3, 39),
    (-2, 44),
    (0, 46),
    (-16, 64),
    (-8, 68),
    (-10, 78),
    (-6, 77),
    (-10, 86),
    (-12, 92),
    (-15, 55),
    (-10, 60),
    (-6, 62),
    (-4, 65),
    (-12, 73),
    (-8, 76),
    (-7, 80),
    (-9, 88),
    (-17, 110),
    (-11, 97),
    (-20, 84),
    (-11, 79),
    (-6, 73),
    (-4, 74),
    (-13, 86),
    (-13, 96),
    (-11, 97),
    (-19, 117),
    (-8, 78),
    (-5, 33),
    (-4, 48),
    (-2, 53),
    (-3, 62),
    (-13, 71),
    (-10, 79),
    (-12, 86),
    (-13, 90),
    (-14, 97),
];

const PB0_227_275: [(i8, i8); 49] = [
    (-6, 76),
    (-2, 44),
    (0, 45),
    (0, 52),
    (-3, 64),
    (-2, 59),
    (-4, 70),
    (-4, 75),
    (-8, 82),
    (-17, 102),
    (-9, 77),
    (3, 24),
    (0, 42),
    (0, 48),
    (0, 55),
    (-6, 59),
    (-7, 71),
    (-12, 83),
    (-11, 87),
    (-30, 119),
    (1, 58),
    (-3, 29),
    (-1, 36),
    (1, 38),
    (2, 43),
    (-6, 55),
    (0, 58),
    (0, 64),
    (-3, 74),
    (-10, 90),
    (0, 70),
    (-4, 29),
    (5, 31),
    (7, 42),
    (1, 59),
    (-2, 58),
    (-3, 72),
    (-3, 81),
    (-11, 97),
    (0, 58),
    (8, 5),
    (10, 14),
    (14, 18),
    (13, 27),
    (2, 40),
    (0, 58),
    (-3, 70),
    (-6, 79),
    (-8, 85),
];

const I_399_401: [(i8, i8); 3] = [(31, 21), (31, 31), (25, 50)];
const PB0_399_401: [(i8, i8); 3] = [(12, 40), (11, 51), (14, 59)];

const I_402_435: [(i8, i8); 34] = [
    (-17, 120),
    (-20, 112),
    (-18, 114),
    (-11, 85),
    (-15, 92),
    (-14, 89),
    (-26, 71),
    (-15, 81),
    (-14, 80),
    (0, 68),
    (-14, 70),
    (-24, 56),
    (-23, 68),
    (-24, 50),
    (-11, 74),
    (23, -13),
    (26, -13),
    (40, -15),
    (49, -14),
    (44, 3),
    (45, 6),
    (44, 34),
    (33, 54),
    (19, 82),
    (-3, 75),
    (-1, 23),
    (1, 34),
    (1, 43),
    (0, 54),
    (-2, 55),
    (0, 61),
    (1, 64),
    (0, 68),
    (-9, 92),
];

const PB0_402_435: [(i8, i8); 34] = [
    (-4, 79),
    (-7, 71),
    (-5, 69),
    (-9, 70),
    (-8, 66),
    (-10, 68),
    (-19, 73),
    (-12, 69),
    (-16, 70),
    (-15, 67),
    (-20, 62),
    (-19, 70),
    (-16, 66),
    (-22, 65),
    (-20, 63),
    (9, -2),
    (26, -9),
    (33, -9),
    (39, -7),
    (41, -2),
    (45, 3),
    (49, 9),
    (45, 27),
    (36, 59),
    (-6, 66),
    (-7, 35),
    (-7, 42),
    (-8, 45),
    (-5, 48),
    (-12, 56),
    (-6, 60),
    (-5, 62),
    (-8, 66),
    (-8, 76),
];

/// significant_coeff_flag ctxIdxInc for 8x8 frame-coded blocks (Table 9-43).
pub const SIG8X8_FRAME: [u8; 63] = [
    0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9, 10, 9, 8, 7, 7, 6, 11, 12, 13, 11, 6, 7, 8, 9, 14, 10, 9, 8, 6, 11, 12,
    13, 11, 6, 9, 14, 10, 9, 11, 12, 13, 11, 14, 10, 12,
];

/// last_significant_coeff_flag ctxIdxInc for 8x8 blocks (Table 9-43).
pub const LAST8X8: [u8; 63] = [
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5,
    6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8,
];

// ---------------------------------------------------------------- CAVLC

/// coeff_token (length, code) indexed [table][total_coeff][trailing_ones] for nC ranges 0..2, 2..4, 4..8.
pub const COEFF_TOKEN_LEN: [[[u8; 4]; 17]; 3] = [
    [
        [1, 0, 0, 0],
        [6, 2, 0, 0],
        [8, 6, 3, 0],
        [9, 8, 7, 5],
        [10, 9, 8, 6],
        [11, 10, 9, 7],
        [13, 11, 10, 8],
        [13, 13, 11, 9],
        [13, 13, 13, 10],
        [14, 14, 13, 11],
        [14, 14, 14, 13],
        [15, 15, 14, 14],
        [15, 15, 15, 14],
        [16, 15, 15, 15],
        [16, 16, 16, 15],
        [16, 16, 16, 16],
        [16, 16, 16, 16],
    ],
    [
        [2, 0, 0, 0],
        [6, 2, 0, 0],
        [6, 5, 3, 0],
        [7, 6, 6, 4],
        [8, 6, 6, 4],
        [8, 7, 7, 5],
        [9, 8, 8, 6],
        [11, 9, 9, 6],
        [11, 11, 11, 7],
        [12, 11, 11, 9],
        [12, 12, 12, 11],
        [12, 12, 12, 11],
        [13, 13, 13, 12],
        [13, 13, 13, 13],
        [13, 14, 13, 13],
        [14, 14, 14, 13],
        [14, 14, 14, 14],
    ],
    [
        [4, 0, 0, 0],
        [6, 4, 0, 0],
        [6, 5, 4, 0],
        [6, 5, 5, 4],
        [7, 5, 5, 4],
        [7, 5, 5, 4],
        [7, 6, 6, 4],
        [7, 6, 6, 4],
        [8, 7, 7, 5],
        [8, 8, 7, 6],
        [9, 8, 8, 7],
        [9, 9, 8, 8],
        [9, 9, 9, 8],
        [10, 9, 9, 9],
        [10, 10, 10, 10],
        [10, 10, 10, 10],
        [10, 10, 10, 10],
    ],
];

pub const COEFF_TOKEN_CODE: [[[u8; 4]; 17]; 3] = [
    [
        [1, 0, 0, 0],
        [5, 1, 0, 0],
        [7, 4, 1, 0],
        [7, 6, 5, 3],
        [7, 6, 5, 3],
        [7, 6, 5, 4],
        [15, 6, 5, 4],
        [11, 14, 5, 4],
        [8, 10, 13, 4],
        [15, 14, 9, 4],
        [11, 10, 13, 12],
        [15, 14, 9, 12],
        [11, 10, 13, 8],
        [15, 1, 9, 12],
        [11, 14, 13, 8],
        [7, 10, 9, 12],
        [4, 6, 5, 8],
    ],
    [
        [3, 0, 0, 0],
        [11, 2, 0, 0],
        [7, 7, 3, 0],
        [7, 10, 9, 5],
        [7, 6, 5, 4],
        [4, 6, 5, 6],
        [7, 6, 5, 8],
        [15, 6, 5, 4],
        [11, 14, 13, 4],
        [15, 10, 9, 4],
        [11, 14, 13, 12],
        [8, 10, 9, 8],
        [15, 14, 13, 12],
        [11, 10, 9, 12],
        [7, 11, 6, 8],
        [9, 8, 10, 1],
        [7, 6, 5, 4],
    ],
    [
        [15, 0, 0, 0],
        [15, 14, 0, 0],
        [11, 15, 13, 0],
        [8, 12, 14, 12],
        [15, 10, 11, 11],
        [11, 8, 9, 10],
        [9, 14, 13, 9],
        [8, 10, 9, 8],
        [15, 14, 13, 13],
        [11, 14, 10, 12],
        [15, 10, 13, 12],
        [11, 14, 9, 12],
        [8, 10, 13, 8],
        [13, 7, 9, 12],
        [9, 12, 11, 10],
        [5, 8, 7, 6],
        [1, 4, 3, 2],
    ],
];

/// Chroma DC (nC = -1) coeff_token [total_coeff][trailing_ones].
pub const COEFF_TOKEN_CDC_LEN: [[u8; 4]; 5] = [[2, 0, 0, 0], [6, 1, 0, 0], [6, 6, 3, 0], [6, 7, 7, 6], [6, 8, 8, 7]];
pub const COEFF_TOKEN_CDC_CODE: [[u8; 4]; 5] = [[1, 0, 0, 0], [7, 1, 0, 0], [4, 6, 1, 0], [3, 3, 2, 5], [2, 3, 2, 0]];

/// total_zeros for 4x4 blocks: [total_coeff - 1][total_zeros] = (len, code).
pub const TOTAL_ZEROS: [[(u8, u8); 16]; 15] = [
    [(1, 1), (3, 3), (3, 2), (4, 3), (4, 2), (5, 3), (5, 2), (6, 3), (6, 2), (7, 3), (7, 2), (8, 3), (8, 2), (9, 3), (9, 2), (9, 1)],
    [(3, 7), (3, 6), (3, 5), (3, 4), (3, 3), (4, 5), (4, 4), (4, 3), (4, 2), (5, 3), (5, 2), (6, 3), (6, 2), (6, 1), (6, 0), (0, 0)],
    [(4, 5), (3, 7), (3, 6), (3, 5), (4, 4), (4, 3), (3, 4), (3, 3), (4, 2), (5, 3), (5, 2), (6, 1), (5, 1), (6, 0), (0, 0), (0, 0)],
    [(5, 3), (3, 7), (4, 5), (4, 4), (3, 6), (3, 5), (3, 4), (4, 3), (3, 3), (4, 2), (5, 2), (5, 1), (5, 0), (0, 0), (0, 0), (0, 0)],
    [(4, 5), (4, 4), (4, 3), (3, 7), (3, 6), (3, 5), (3, 4), (3, 3), (4, 2), (5, 1), (4, 1), (5, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(6, 1), (5, 1), (3, 7), (3, 6), (3, 5), (3, 4), (3, 3), (3, 2), (4, 1), (3, 1), (6, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(6, 1), (5, 1), (3, 5), (3, 4), (3, 3), (2, 3), (3, 2), (4, 1), (3, 1), (6, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(6, 1), (4, 1), (5, 1), (3, 3), (2, 3), (2, 2), (3, 2), (3, 1), (6, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(6, 1), (6, 0), (4, 1), (2, 3), (2, 2), (3, 1), (2, 1), (5, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(5, 1), (5, 0), (3, 1), (2, 3), (2, 2), (2, 1), (4, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(4, 0), (4, 1), (3, 1), (3, 2), (1, 1), (3, 3), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(4, 0), (4, 1), (2, 1), (1, 1), (3, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(3, 0), (3, 1), (1, 1), (2, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(2, 0), (2, 1), (1, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(1, 0), (1, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
];

/// total_zeros for 2x2 chroma DC: [total_coeff - 1][total_zeros].
pub const TOTAL_ZEROS_CDC: [[(u8, u8); 4]; 3] = [[(1, 1), (2, 1), (3, 1), (3, 0)], [(1, 1), (2, 1), (2, 0), (0, 0)], [(1, 1), (1, 0), (0, 0), (0, 0)]];

/// run_before: [min(zeros_left, 7) - 1][run_before] = (len, code).
pub const RUN_BEFORE: [[(u8, u8); 15]; 7] = [
    [(1, 1), (1, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(1, 1), (2, 1), (2, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(2, 3), (2, 2), (2, 1), (2, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(2, 3), (2, 2), (2, 1), (3, 1), (3, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(2, 3), (2, 2), (3, 3), (3, 2), (3, 1), (3, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(2, 3), (3, 0), (3, 1), (3, 3), (3, 2), (3, 5), (3, 4), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],
    [(3, 7), (3, 6), (3, 5), (3, 4), (3, 3), (3, 2), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1)],
];

/// coded_block_pattern -> codeNum for Intra_4x4/8x8 macroblocks (ChromaArrayType 1/2).
pub const CBP_INTRA_TO_CODE: [u8; 48] = invert_cbp(&CODE_TO_CBP_INTRA);
/// coded_block_pattern -> codeNum for inter macroblocks.
pub const CBP_INTER_TO_CODE: [u8; 48] = invert_cbp(&CODE_TO_CBP_INTER);

const CODE_TO_CBP_INTRA: [u8; 48] = [
    47, 31, 15, 0, 23, 27, 29, 30, 7, 11, 13, 14, 39, 43, 45, 46, 16, 3, 5, 10, 12, 19, 21, 26, 28, 35, 37, 42, 44, 1, 2, 4, 8, 17, 18, 20, 24, 6, 9, 22, 25,
    32, 33, 34, 36, 40, 38, 41,
];
const CODE_TO_CBP_INTER: [u8; 48] = [
    0, 16, 1, 2, 4, 8, 32, 3, 5, 10, 12, 15, 47, 7, 11, 13, 14, 6, 9, 31, 35, 37, 42, 44, 33, 34, 36, 40, 39, 43, 45, 46, 17, 18, 20, 24, 19, 21, 26, 28, 23,
    27, 29, 30, 22, 25, 38, 41,
];

const fn invert_cbp(t: &[u8; 48]) -> [u8; 48] {
    let mut out = [0u8; 48];
    let mut i = 0;
    while i < 48 {
        out[t[i] as usize] = i as u8;
        i += 1;
    }
    out
}

// ---------------------------------------------------------------- quantisation

/// Forward quantisation multipliers for 4x4 (position classes: even/even, odd/odd, other).
pub const QUANT4: [[i32; 3]; 6] = [[13107, 5243, 8066], [11916, 4660, 7490], [10082, 4194, 6554], [9362, 3647, 5825], [8192, 3355, 5243], [7282, 2893, 4559]];

/// normAdjust4x4 (v) values.
pub const DEQUANT4: [[i32; 3]; 6] = [[10, 16, 13], [11, 18, 14], [13, 20, 16], [14, 23, 18], [16, 25, 20], [18, 29, 23]];

/// normAdjust8x8 (v) values.
pub const DEQUANT8: [[i32; 6]; 6] = [
    [20, 18, 32, 19, 25, 24],
    [22, 19, 35, 21, 28, 26],
    [26, 23, 42, 24, 33, 31],
    [28, 25, 45, 26, 35, 33],
    [32, 28, 51, 30, 40, 38],
    [36, 32, 58, 34, 46, 43],
];

#[inline]
pub fn pos_class4(i: usize) -> usize {
    let (x, y) = (i & 3, i >> 2);
    if x % 2 == 0 && y % 2 == 0 {
        0
    } else if x % 2 == 1 && y % 2 == 1 {
        1
    } else {
        2
    }
}

#[inline]
pub fn pos_class8(i: usize) -> usize {
    let (x, y) = (i & 7, i >> 3);
    if x % 4 == 0 && y % 4 == 0 {
        0
    } else if x % 2 == 1 && y % 2 == 1 {
        1
    } else if x % 4 == 2 && y % 4 == 2 {
        2
    } else if (x % 4 == 0 && y % 2 == 1) || (x % 2 == 1 && y % 4 == 0) {
        3
    } else if (x % 4 == 0 && y % 4 == 2) || (x % 4 == 2 && y % 4 == 0) {
        4
    } else {
        5
    }
}

/// QPc as a function of qPI (Table 8-15).
pub fn chroma_qp(qpi: i32) -> u8 {
    const T: [u8; 22] = [29, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39];
    let q = qpi.clamp(0, 51);
    if q < 30 { q as u8 } else { T[(q - 30) as usize] }
}

// ---------------------------------------------------------------- deblocking

pub const ALPHA: [u8; 52] = [
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15, 17, 20, 22, 25, 28, 32, 36, 40, 45, 50, 56, 63, 71, 80, 90, 101, 113,
    127, 144, 162, 182, 203, 226, 255, 255,
];
pub const BETA: [u8; 52] = [
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16,
    16, 17, 17, 18, 18,
];
pub const TC0: [[u8; 3]; 52] = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 1],
    [0, 0, 1],
    [0, 0, 1],
    [0, 0, 1],
    [0, 1, 1],
    [0, 1, 1],
    [1, 1, 1],
    [1, 1, 1],
    [1, 1, 1],
    [1, 1, 1],
    [1, 1, 2],
    [1, 1, 2],
    [1, 1, 2],
    [1, 1, 2],
    [1, 2, 3],
    [1, 2, 3],
    [2, 2, 3],
    [2, 2, 4],
    [2, 3, 4],
    [2, 3, 4],
    [3, 3, 5],
    [3, 4, 6],
    [3, 4, 6],
    [4, 5, 7],
    [4, 5, 8],
    [4, 6, 9],
    [5, 7, 10],
    [6, 8, 11],
    [6, 8, 13],
    [7, 10, 14],
    [8, 11, 16],
    [9, 12, 18],
    [10, 13, 20],
    [11, 15, 23],
    [13, 17, 25],
];

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cbp_tables_are_permutations() {
        let mut seen = [false; 48];
        for &c in &CBP_INTRA_TO_CODE {
            seen[c as usize] = true;
        }
        assert!(seen.iter().all(|&s| s));
        let mut seen = [false; 48];
        for &c in &CBP_INTER_TO_CODE {
            seen[c as usize] = true;
        }
        assert!(seen.iter().all(|&s| s));
    }

    #[test]
    fn scans_are_permutations() {
        let mut s = ZIGZAG8;
        s.sort();
        assert!(s.iter().enumerate().all(|(i, &v)| i == v));
        let mut s = ZIGZAG4;
        s.sort();
        assert!(s.iter().enumerate().all(|(i, &v)| i == v));
        for (b, &(x, y)) in BLK4_XY.iter().enumerate() {
            assert_eq!(RASTER_TO_BLK4[y * 4 + x], b);
        }
    }

    /// Every VLC table must be prefix-free (a cheap consistency check on transcription).
    #[test]
    fn vlc_tables_prefix_free() {
        fn check(codes: &[(u8, u32)]) {
            for (i, &(la, ca)) in codes.iter().enumerate() {
                for (j, &(lb, cb)) in codes.iter().enumerate() {
                    if i == j || la == 0 || lb == 0 || la > lb {
                        continue;
                    }
                    assert!(cb >> (lb - la) != ca, "prefix clash {la}:{ca:b} vs {lb}:{cb:b}");
                }
            }
        }
        for t in 0..3 {
            let mut v = vec![];
            for tc in 0..17 {
                for t1 in 0..4 {
                    if t1 <= tc && COEFF_TOKEN_LEN[t][tc][t1] > 0 {
                        v.push((COEFF_TOKEN_LEN[t][tc][t1], COEFF_TOKEN_CODE[t][tc][t1] as u32));
                    }
                }
            }
            assert_eq!(v.len(), 62);
            check(&v);
        }
        let mut v = vec![];
        for tc in 0..5 {
            for t1 in 0..4 {
                if t1 <= tc && COEFF_TOKEN_CDC_LEN[tc][t1] > 0 {
                    v.push((COEFF_TOKEN_CDC_LEN[tc][t1], COEFF_TOKEN_CDC_CODE[tc][t1] as u32));
                }
            }
        }
        assert_eq!(v.len(), 14);
        check(&v);
        for (tc, row) in TOTAL_ZEROS.iter().enumerate() {
            let v: Vec<_> = row.iter().take(16 - tc).map(|&(l, c)| (l, c as u32)).collect();
            check(&v);
        }
        for (i, row) in RUN_BEFORE.iter().enumerate() {
            let n = if i < 6 { i + 2 } else { 15 };
            let v: Vec<_> = row.iter().take(n).map(|&(l, c)| (l, c as u32)).collect();
            check(&v);
        }
    }
}