Skip to main content

threers_codec/decode/hevc/
transform.rs

1//! HEVC scaling and inverse transforms (§8.6.2-8.6.4).
2
3use super::tables::{DCT32, DST4};
4
5const LEVEL_SCALE: [i32; 6] = [40, 45, 51, 57, 64, 72];
6
7/// Scale `TransCoeffLevel` in place (§8.6.3). `m` is the scaling factor
8/// array for this block (raster), or `None` for the flat 16.
9pub fn dequant(c: &mut [i32], n: usize, qp: i32, bd: u32, m: Option<&[u8]>) {
10    let log2 = n.trailing_zeros() as i32;
11    let bd_shift = bd as i32 + log2 - 5;
12    let scale = LEVEL_SCALE[(qp % 6) as usize] << (qp / 6);
13    let add = 1i64 << (bd_shift - 1);
14    for (i, v) in c.iter_mut().enumerate().take(n * n) {
15        if *v != 0 {
16            let f = m.map_or(16, |m| m[i] as i64);
17            let d = ((*v as i64 * f * scale as i64) + add) >> bd_shift;
18            *v = d.clamp(-32768, 32767) as i32;
19        }
20    }
21}
22
23/// One inverse 1-D transform of length `n` over `src` (stride `ss`) into
24/// `out[..n]`. `last` is one past the last non-zero input.
25///
26/// Accumulates a whole output row per input coefficient, over a contiguous
27/// row of the matrix, so the inner loop vectorises. `i32` is exact: inputs
28/// are clipped to 16 bits and |DCT32| <= 90, so 32 terms stay under 2^27.
29#[inline(always)]
30fn idct_1d(src: &[i32], ss: usize, out: &mut [i32; 32], n: usize, last: usize) {
31    let step = 32 / n;
32    let out = &mut out[..n];
33    out.fill(0);
34    for j in 0..last {
35        let x = src[j * ss];
36        if x == 0 {
37            continue;
38        }
39        for (o, &m) in out.iter_mut().zip(&DCT32[j * step][..n]) {
40            *o += m as i32 * x;
41        }
42    }
43}
44
45/// Inverse transform of an `n × n` block of scaled coefficients (raster) into
46/// residuals, in place (§8.6.4.2).
47pub fn inverse(c: &mut [i32], n: usize, dst: bool, bd: u32) {
48    // The intermediate is sized to the block: most blocks are 4×4, and
49    // clearing a 32×32 buffer for each was a measurable part of decoding.
50    match n {
51        4 => inverse_n::<16>(c, n, dst, bd),
52        8 => inverse_n::<64>(c, n, dst, bd),
53        16 => inverse_n::<256>(c, n, dst, bd),
54        _ => inverse_n::<1024>(c, n, dst, bd),
55    }
56}
57
58fn inverse_n<const NN: usize>(c: &mut [i32], n: usize, dst: bool, bd: u32) {
59    let mut tmp = [0i32; NN];
60    let mut out = [0i32; 32];
61    // Columns first.
62    for x in 0..n {
63        let last = (0..n)
64            .rev()
65            .find(|&y| c[y * n + x] != 0)
66            .map_or(0, |y| y + 1);
67        if last == 0 {
68            continue; // `tmp` is zero there already.
69        }
70        if dst {
71            for i in 0..4 {
72                let mut acc = 0i32;
73                for j in 0..4 {
74                    acc += DST4[j][i] * c[j * 4 + x];
75                }
76                out[i] = acc;
77            }
78        } else {
79            idct_1d(&c[x..], n, &mut out, n, last);
80        }
81        for y in 0..n {
82            tmp[y * n + x] = ((out[y] + 64) >> 7).clamp(-32768, 32767);
83        }
84    }
85    // Then rows.
86    let shift = 20 - bd as i32;
87    let add = 1 << (shift - 1);
88    for y in 0..n {
89        let row = &tmp[y * n..y * n + n];
90        let last = row.iter().rposition(|&v| v != 0).map_or(0, |p| p + 1);
91        if last == 0 {
92            c[y * n..y * n + n].fill(0);
93            continue;
94        }
95        if dst {
96            for i in 0..4 {
97                let mut acc = 0i32;
98                for j in 0..4 {
99                    acc += DST4[j][i] * row[j];
100                }
101                out[i] = acc;
102            }
103        } else {
104            idct_1d(row, 1, &mut out, n, last);
105        }
106        for (d, &o) in c[y * n..y * n + n].iter_mut().zip(&out[..n]) {
107            *d = (o + add) >> shift;
108        }
109    }
110}
111
112/// Transform skip (§8.6.4.2 with `transform_skip_flag`): residual is the
113/// scaled coefficient shifted up then back down by the bit-depth shift.
114pub fn transform_skip(c: &mut [i32], n: usize, bd: u32) {
115    let shift = 20 - bd as i32;
116    let ts_shift = 5 + n.trailing_zeros() as i32;
117    let add = 1 << (shift - 1);
118    for v in c.iter_mut().take(n * n) {
119        *v = ((*v << ts_shift) + add) >> shift;
120    }
121}
122
123/// Residual DPCM (range extensions): each residual accumulates the one
124/// before it, along columns when `vertical`, else along rows.
125pub fn rdpcm(c: &mut [i32], n: usize, vertical: bool) {
126    if vertical {
127        for i in n..n * n {
128            c[i] += c[i - n];
129        }
130    } else {
131        for y in 0..n {
132            for x in 1..n {
133                c[y * n + x] += c[y * n + x - 1];
134            }
135        }
136    }
137}