threers-codec 0.0.10

Pure-Rust media codecs: H.264/HEVC/VP9 encoders, H.264/HEVC/VP9/AV1 decoders, AAC/MP3/Opus/Vorbis/FLAC/WAV decoders, MP4/WebM/Matroska/Ogg, GIF/APNG, captions, and VideoToolbox acceleration on macOS
Documentation
//! Context initialization for H.264 CABAC, I slices (§9.3.1.1, Table 9-12 and
//! Tables 9-17 through 9-24, the `I`/`SI` column).
//!
//! Each entry is the `(m, n)` pair the spec gives for one `ctxIdx`; the initial
//! probability state is derived from it and the slice QP, which is why these
//! cannot be folded into a single byte the way H.265 does it.
//!
//! `ctxIdx` 11..=59 belong to syntax elements that only occur in P and B slices
//! and carry `CTX_NA` in the I column. They are stored as `(0, 0)` and never
//! selected — `tests::only_i_slice_contexts_are_reachable` pins that.

/// `(m, n)` per `ctxIdx`, covering every context an I slice can reach
/// (`end_of_slice_flag` at 276 is not context-coded, so the table stops at 275).
pub const INIT_MN: [(i8, i8); 276] = [
    (20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74),
    (-28, 127), (-23, 104), (-6, 53), (-1, 54), (7, 51), (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, 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, 41), (0, 63), (0, 63), (0, 63), (-9, 83), (4, 86),
    (0, 97), (-7, 72), (13, 41), (3, 62), (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), (-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), (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), (-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),
];

/// `(m, n)` per `ctxIdx` for P slices with `cabac_init_idc = 0` (the same
/// tables, the P/B column for that index), as far as a frame-coded 4:2:0 P
/// slice reaches. Copied from the decoder's tables, which were checked
/// against a shipped libavcodec.
pub const INIT_MN_P0: [(i8, i8); 276] = [
    (20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74),
    (-28, 127), (-23, 104), (-6, 53), (-1, 54), (7, 51), (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),
    (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), (-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),
    (-7, 67), (-5, 74), (-4, 74), (-5, 80), (-7, 72), (1, 58),
    (0, 41), (0, 63), (0, 63), (0, 63), (-9, 83), (4, 86),
    (0, 97), (-7, 72), (13, 41), (3, 62), (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), (-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), (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), (-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),
];

/// Derive `(pStateIdx, valMPS)` from an `(m, n)` pair at `slice_qp` (§9.3.1.1).
pub fn init_state(m: i8, n: i8, slice_qp: i32) -> (u8, u8) {
    let qp = slice_qp.clamp(0, 51);
    let pre = (((m as i32 * qp) >> 4) + n as i32).clamp(1, 126);
    if pre <= 63 {
        ((63 - pre) as u8, 0)
    } else {
        ((pre - 64) as u8, 1)
    }
}