forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

tokens.rs208 lines, 7.7 KiBHistoryRawDownload
1//! Coefficient token decoding (spec 8.5.2 `decode_coefs` / libvpx
2//! `vp9_detokenize.c`, ported verbatim including its context bookkeeping).
3
4use crate::adapt::Counts;
5use crate::bool::BoolDecoder;
6use crate::tables::*;
7
8/// `vp9_cat6_prob_high12 + 2` (`vp9/common/vp9_entropy.c`): the CAT6 token's
9/// 16 extra bits at bit depth 10.
10const CAT6_PROB_HIGH: [u8; 16] = [
11 255, 255, 254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129,
12];
13
14/// `vp9_cat6_prob_high12` (`vp9/common/vp9_entropy.c`): the CAT6 token's 18
15/// extra bits at bit depth 12 (bit depth 10 uses `+ 2`, the 16 middle
16/// entries). The CAT6 token is the ONLY depth-dependent syntax element:
17/// `decode_coefs` picks `vp9_cat6_prob`/14 bits at 8-bit, `+ 2`/16 at 10-bit,
18/// and this/18 at 12-bit.
19const CAT6_PROB_HIGH12: [u8; 18] = [
20 255, 255, 255, 255, 254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129,
21];
22
23/// One plane's entropy contexts: a has-coefficient flag per 4x4 column
24/// above the current superblock row and per 4x4 row to the left.
25pub(crate) struct PlaneContexts {
26 pub above: Vec<u8>,
27 pub left: [u8; 32],
28}
29
30impl PlaneContexts {
31 pub(crate) fn new(above_len: usize) -> Self {
32 Self {
33 above: vec![0; above_len],
34 left: [0; 32],
35 }
36 }
37}
38
39/// `get_coef_context` (vp9_scan.h).
40#[inline]
41fn get_coef_context(nb: &[i16], token_cache: &[u8; 1024], c: usize) -> usize {
42 ((1 + token_cache[nb[2 * c] as usize] + token_cache[nb[2 * c + 1] as usize]) >> 1) as usize
43}
44
45#[inline]
46fn band_at(tx_size: usize, c: usize) -> usize {
47 if tx_size == TX_4X4 {
48 COEFBAND_TRANS_4X4[c] as usize
49 } else {
50 COEFBAND_TRANS_8X8PLUS[c] as usize
51 }
52}
53
54#[inline]
55fn read_coeff(r: &mut BoolDecoder, probs: &[u8], n: usize) -> i32 {
56 let mut val = 0i32;
57 for &p in &probs[..n] {
58 val = (val << 1) + r.read_bool(p) as i32;
59 }
60 val
61}
62
63/// One decoded transform block: dequantized coefficients + eob.
64pub(crate) struct CoeffBlock {
65 pub coeffs: Box<[i32]>,
66 pub eob: usize,
67}
68
69/// `decode_coefs`, verbatim port. `dq` is `[dc, ac]`; plane 0 is luma,
70/// chroma is 1. `is_inter` selects the coefficient-probability ref axis
71/// (libvpx `get_coef_probs`: `coef_probs[tx][plane][ref]`).
72pub(crate) fn decode_coefs(
73 r: &mut BoolDecoder,
74 plane_type: usize,
75 tx_size: usize,
76 dequant: &[i16; 2],
77 mut ctx: usize,
78 scan: &[i16],
79 nb: &[i16],
80 probs4d: &[[[[[u8; 3]; 6]; 6]; 2]; 2],
81 is_inter: bool,
82 bd: u8,
83 #[allow(unused_variables)] pos: (usize, usize),
84 #[allow(unused_variables)] plane: usize,
85 counts: &mut Counts,
86) -> CoeffBlock {
87 let max_eob = 16 << (tx_size << 1);
88 let mut coef = CoeffBlock {
89 coeffs: vec![0i32; max_eob].into_boxed_slice(),
90 eob: 0,
91 };
92 let mut token_cache = [0u8; 1024];
93 let mut c = 0usize;
94 let dq_shift = usize::from(tx_size == TX_32X32);
95 let mut dqv = dequant[0] as i32;
96 let mut band = band_at(tx_size, c);
97 let mut prob: &[u8; 3] = &probs4d[plane_type][usize::from(is_inter)][band][ctx];
98
99 while c < max_eob {
100 if crate::trace_enabled() {
101 eprintln!(
102 "TOK c={c} band={band} ctx={ctx} p0={},{},{}",
103 prob[0], prob[1], prob[2]
104 );
105 }
106 // libvpx `++eob_branch_count[band][ctx]` before the EOB decision.
107 let cell = usize::from(is_inter);
108 counts.eob_branch[tx_size][plane_type][cell][band][ctx] += 1;
109 if !r.read_bool(prob[0]) {
110 counts.coef[tx_size][plane_type][cell][band][ctx][3] += 1;
111 break;
112 }
113 while !r.read_bool(prob[1]) {
114 counts.coef[tx_size][plane_type][cell][band][ctx][0] += 1;
115 dqv = dequant[1] as i32;
116 token_cache[scan[c] as usize] = 0;
117 c += 1;
118 if c >= max_eob {
119 return coef; // zero tokens to the end (no eob token)
120 }
121 ctx = get_coef_context(nb, &token_cache, c);
122 band = band_at(tx_size, c);
123 prob = &probs4d[plane_type][usize::from(is_inter)][band][ctx];
124 }
125 let mut v: i32;
126 if r.read_bool(prob[2]) {
127 counts.coef[tx_size][plane_type][cell][band][ctx][2] += 1;
128 let base = prob[2] as usize - 1;
129 let p = &PARETO8_FULL[base * 8..base * 8 + 8];
130 if r.read_bool(p[0]) {
131 if r.read_bool(p[3]) {
132 token_cache[scan[c] as usize] = 5;
133 if r.read_bool(p[5]) {
134 if r.read_bool(p[7]) {
135 v = 67
136 + match bd {
137 8 => read_coeff(r, &CAT6_PROB, 14),
138 10 => read_coeff(r, &CAT6_PROB_HIGH, 16),
139 _ => read_coeff(r, &CAT6_PROB_HIGH12, 18),
140 };
141 } else {
142 v = 35 + read_coeff(r, &CAT5_PROB, 5);
143 }
144 } else if r.read_bool(p[6]) {
145 v = 19 + read_coeff(r, &CAT4_PROB, 4);
146 } else {
147 v = 11 + read_coeff(r, &CAT3_PROB, 3);
148 }
149 } else {
150 token_cache[scan[c] as usize] = 4;
151 if r.read_bool(p[4]) {
152 v = 7 + read_coeff(r, &CAT2_PROB, 2);
153 } else {
154 v = 5 + read_coeff(r, &CAT1_PROB, 1);
155 }
156 }
157 v = (v * dqv) >> dq_shift;
158 } else if r.read_bool(p[1]) {
159 token_cache[scan[c] as usize] = 3;
160 v = ((3 + r.read_bool(p[2]) as i32) * dqv) >> dq_shift;
161 } else {
162 token_cache[scan[c] as usize] = 2;
163 v = (2 * dqv) >> dq_shift;
164 }
165 } else {
166 counts.coef[tx_size][plane_type][cell][band][ctx][1] += 1;
167 token_cache[scan[c] as usize] = 1;
168 v = dqv >> dq_shift;
169 }
170 let sign = r.read_bool(128);
171 let pos = scan[c] as usize;
172 coef.coeffs[pos] = if sign { -v } else { v };
173 c += 1;
174 if c < max_eob {
175 ctx = get_coef_context(nb, &token_cache, c);
176 band = band_at(tx_size, c);
177 prob = &probs4d[plane_type][usize::from(is_inter)][band][ctx];
178 }
179 dqv = dequant[1] as i32;
180 }
181 coef.eob = c;
182 if crate::trace_enabled() {
183 eprintln!(
184 "EOB p={} x={} y={} tx={} eob={} c0={}",
185 plane, pos.0, pos.1, tx_size, c, coef.coeffs[0]
186 );
187 }
188 coef
189}
190
191/// Scan + neighbors per tx size and tx type (vp9_scan_orders).
192pub(crate) fn scan_for(tx_size: usize, tx_type: usize) -> (&'static [i16], &'static [i16]) {
193 match (tx_size, tx_type) {
194 (TX_4X4, DCT_DCT) => (&DEFAULT_SCAN_4X4, &DEFAULT_SCAN_4X4_NEIGHBORS),
195 (TX_4X4, ADST_DCT) => (&ROW_SCAN_4X4, &ROW_SCAN_4X4_NEIGHBORS),
196 (TX_4X4, DCT_ADST) => (&COL_SCAN_4X4, &COL_SCAN_4X4_NEIGHBORS),
197 (TX_4X4, _) => (&DEFAULT_SCAN_4X4, &DEFAULT_SCAN_4X4_NEIGHBORS),
198 (TX_8X8, DCT_DCT) => (&DEFAULT_SCAN_8X8, &DEFAULT_SCAN_8X8_NEIGHBORS),
199 (TX_8X8, ADST_DCT) => (&ROW_SCAN_8X8, &ROW_SCAN_8X8_NEIGHBORS),
200 (TX_8X8, DCT_ADST) => (&COL_SCAN_8X8, &COL_SCAN_8X8_NEIGHBORS),
201 (TX_8X8, _) => (&DEFAULT_SCAN_8X8, &DEFAULT_SCAN_8X8_NEIGHBORS),
202 (TX_16X16, DCT_DCT) => (&DEFAULT_SCAN_16X16, &DEFAULT_SCAN_16X16_NEIGHBORS),
203 (TX_16X16, ADST_DCT) => (&ROW_SCAN_16X16, &ROW_SCAN_16X16_NEIGHBORS),
204 (TX_16X16, DCT_ADST) => (&COL_SCAN_16X16, &COL_SCAN_16X16_NEIGHBORS),
205 (TX_16X16, _) => (&DEFAULT_SCAN_16X16, &DEFAULT_SCAN_16X16_NEIGHBORS),
206 (_, _) => (&DEFAULT_SCAN_32X32, &DEFAULT_SCAN_32X32_NEIGHBORS),
207 }
208}