forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB8dbd683Tahinli3 months ago856 commits

cdf_state.rs1293 lines, 54.6 KiBHistoryRawDownload
1//! The CDFs a tile adapts as it is written.
2//!
3//! AV1's symbol decoder updates every non-literal CDF it reads (spec 8.3.2),
4//! so an encoder that wants `disable_cdf_update` off has to keep the same
5//! state and update it in the same order. What lives here is that state: one
6//! owned copy of each table the tile writer touches, seeded from the defaults
7//! in [`crate::cdf`].
8//!
9//! The tables are owned once, not once per use: two coefficient table sets
10//! that share a table -- a 64x64 luma transform reads the 32x32 base-range
11//! rows, and both luma sets read the same DC sign table -- share the *adapted*
12//! table too, which is what the decoder does.
13
14use crate::cdf;
15
16/// Which of the four coefficient table sets a transform block is coded with.
17#[derive(Clone, Copy)]
18pub(crate) enum TxbSet {
19 /// The 32x32 luma transform of an intra 32x32 block. `get_tx_set` is
20 /// DCT-only at this size for an intra block, so no `tx_type` symbol is
21 /// coded; an `is_inter` 32x32 block reads a different set and uses
22 /// [`Luma32Inter`](TxbSet::Luma32Inter) instead.
23 Luma32,
24 /// The 32x32 luma transform of an `is_inter` 32x32 block: the same
25 /// coefficient tables [`Luma32`](TxbSet::Luma32) reads, but `get_tx_set`
26 /// (spec 5.11.48) returns `TX_SET_INTER_3` rather than `TX_SET_DCTONLY`
27 /// at this size for an inter block, so this variant carries the
28 /// `tx_type` symbol `Luma32` does not.
29 Luma32Inter,
30 /// The 64x64 luma transform of a whole superblock, scanned as a 32x32.
31 Luma64,
32 /// The 16x16 luma transform of a 16x16 block.
33 Luma16,
34 /// The 16x16 luma transform of an `is_inter` 16x16 block, coded under
35 /// `reduced_tx_set` (see [`crate::cdf::INTER_TX_TYPE_SET3_16`]'s doc
36 /// comment): the same coefficient tables [`Luma16`](TxbSet::Luma16)
37 /// reads, but its own two-symbol `tx_type` table rather than `Luma16`'s
38 /// five-symbol, mode-indexed one.
39 Luma16Inter,
40 /// [`Luma16Inter`](TxbSet::Luma16Inter)'s `reduced_tx_set: false`
41 /// counterpart (lane-cdffwd2, root-caused off `cdfflake-stream-seed45`):
42 /// the same coefficient tables, but the twelve-symbol
43 /// `EXT_TX_SET_DTT9_IDTX_1DDCT` `tx_type` table
44 /// ([`crate::cdf::INTER_TX_TYPE_SET2_16`]) rather than `Luma16Inter`'s
45 /// two-symbol reduced one.
46 Luma16InterSet1,
47 /// The 8x8 luma transform of an intra 8x8 leaf under a straddling 16x16
48 /// block (lane-av1-rect). An `is_inter` leaf reads
49 /// [`Luma8Inter`](TxbSet::Luma8Inter) instead.
50 Luma8,
51 /// [`Luma8`](TxbSet::Luma8)'s `reduced_tx_set: false` counterpart (round
52 /// 8): the same coefficient tables, but the seven-symbol `TX_SET_INTRA_1`
53 /// `tx_type` table rather than `Luma8`'s five-symbol reduced one.
54 Luma8Set1,
55 /// The 8x8 luma transform of an `is_inter` 8x8 leaf under a straddling
56 /// 16x16 block (lane-av1inter8): the same coefficient tables
57 /// [`Luma8`](TxbSet::Luma8) reads, but `get_tx_set` (spec 5.11.48)
58 /// returns `TX_SET_INTER_3` at this size too under this encoder's
59 /// always-on `reduced_tx_set` -- the same two-symbol alphabet
60 /// [`Luma16Inter`](TxbSet::Luma16Inter) carries, just its own CDF (libaom
61 /// `default_inter_ext_tx_cdf[EXT_TX_SET_DCT_IDTX][TX_8X8]`, a distinct
62 /// adapted table from the 16x16 and 32x32 sizes' own entries in the same
63 /// set, per `entropymode.c`).
64 Luma8Inter,
65 /// [`Luma8Inter`](TxbSet::Luma8Inter)'s `reduced_tx_set: false`
66 /// counterpart (lane-cdffwd2): the sixteen-symbol `EXT_TX_SET_ALL16`
67 /// `tx_type` table ([`crate::cdf::INTER_TX_TYPE_SET1_8`]).
68 Luma8InterSet1,
69 /// The 4x4 luma transform of a `TxMode::Select` 8x8 leaf or 16x16 block
70 /// whose `tx_depth` resolves all the way down (lane-av1tx4). An
71 /// `is_inter` block has no writer path yet, so this crate has no
72 /// `Luma4Inter` counterpart to [`Luma8Inter`](TxbSet::Luma8Inter).
73 Luma4,
74 /// [`Luma4`](TxbSet::Luma4)'s `reduced_tx_set: false` counterpart, same
75 /// split as [`Luma8Set1`](TxbSet::Luma8Set1).
76 Luma4Set1,
77 /// The 8x8 transform of a chroma plane of a 16x16 block.
78 Chroma8,
79 /// The 4x4 transform of a chroma plane of an 8x8 leaf (lane-av1-rect).
80 /// r5 correction: libaom's `is_chroma_reference` (`av1_common_int.h`) is
81 /// unconditionally true for `bsize >= BLOCK_8X8`, so chroma *does* follow
82 /// luma all the way down to 8x8 — it is coded once per 8x8 leaf, not once
83 /// per parent 16x16 (that "stays at the parent's Chroma8 granularity"
84 /// framing this doc carried through r4 was never checked against a real
85 /// decoder and is wrong; only `BLOCK_4X4`/`4X8`/`8X4`, one mi-unit wide or
86 /// high, get the "last sub-block only" chroma-merge treatment). A real
87 /// writer for the straddling-16x16 case must use this variant per leaf.
88 Chroma4,
89 /// The 16x16 transform of a chroma plane of a 32x32 block.
90 Chroma16,
91 /// The 32x32 transform of a chroma plane of a whole superblock.
92 Chroma32,
93}
94
95/// The tables one transform block is coded with, borrowed from the state for
96/// as long as that block is being written.
97pub(crate) struct TxbTables<'a> {
98 /// Side of the transform, in coefficients.
99 pub side: usize,
100 /// The all-zero flag, indexed by its own context.
101 pub txb_skip: &'a mut [[u16; 3]],
102 /// The end-of-block group, whose alphabet the transform size sets.
103 pub eob_pt: &'a mut [u16],
104 /// `eob_pt`'s `TX_CLASS_HORIZ`/`TX_CLASS_VERT` sibling table (r5): read
105 /// instead of `eob_pt` once this TU's `tx_type` is known to be
106 /// `V_DCT`/`H_DCT`. `None` for any size/set whose `tx_type` alphabet
107 /// cannot produce those two symbols (chroma, and every inter set).
108 pub eob_pt_class1: Option<&'a mut [u16]>,
109 /// The top bit of the offset inside that group.
110 pub eob_extra: &'a mut [[u16; 3]; 9],
111 /// A coefficient's base level.
112 pub base: &'a mut [[u16; 5]; 42],
113 /// The base level of the last coefficient in the scan.
114 pub base_eob: &'a mut [[u16; 4]; 4],
115 /// The base-range tail above level two.
116 pub br: &'a mut [[u16; 5]; 21],
117 /// The sign of the DC.
118 pub dc_sign: &'a mut [[u16; 3]; 3],
119 /// The transform type, for the one size whose set holds more than one --
120 /// a plain slice because the intra 16x16 table's alphabet (five types)
121 /// and the inter 32x32 table's (two, `TX_SET_INTER_3`) are different
122 /// sizes.
123 pub tx_type: Option<&'a mut [u16]>,
124}
125
126/// Every table a key frame's tile writer adapts.
127#[derive(Clone)]
128pub(crate) struct Cdfs {
129 /// The partition symbol of a 64x64 superblock.
130 pub partition_w64: [[u16; 11]; 4],
131 /// The partition symbol of a 32x32 block.
132 pub partition_w32: [[u16; 11]; 4],
133 /// Whether a block carries no residual at all.
134 pub skip: [[u16; 3]; 3],
135 /// The luma mode of a key frame's block, under the two neighbours' modes.
136 pub kf_y_mode: [[[u16; 14]; 5]; 5],
137 /// The chroma mode of a block too big to be offered chroma from luma.
138 pub uv_mode_no_cfl: [[u16; 14]; 13],
139 /// The chroma mode of a block that is offered it.
140 pub uv_mode_cfl: [[u16; 15]; 13],
141 /// The joint sign of a `UV_CFL_PRED` block's U/V alpha.
142 pub cfl_sign: [u16; 9],
143 /// A `UV_CFL_PRED` block's per-plane alpha magnitude, by joint-sign context.
144 pub cfl_alpha: [[u16; 17]; 6],
145 /// The angle a directional mode is nudged by.
146 pub angle_delta: [[u16; 8]; 8],
147 /// The all-zero flag of a 16x16 luma transform, 7 contexts (index 0 for
148 /// a lone TU whose own bsize equals the block's; 1..6 the
149 /// `skip_contexts[top][left]` table when `TxMode::Select` splits the
150 /// block's luma into several smaller transform units, spec
151 /// `get_txb_ctx_general`).
152 pub txb_skip_luma_16: [[u16; 3]; 7],
153 /// The same, for an 8x8 luma transform.
154 pub txb_skip_luma_8: [[u16; 3]; 7],
155 /// The same, for a 4x4 luma transform (lane-av1tx4).
156 pub txb_skip_luma_4: [[u16; 3]; 7],
157 /// The all-zero flag of an 8x8 chroma transform.
158 pub txb_skip_chroma_8: [[u16; 3]; 3],
159 /// The all-zero flag of a 4x4 chroma transform.
160 pub txb_skip_chroma_4: [[u16; 3]; 3],
161 /// The end-of-block group of a 16x16 luma transform.
162 pub eob_pt_256_luma: [u16; 10],
163 /// The end-of-block group of an 8x8 luma transform.
164 pub eob_pt_64_luma: [u16; 8],
165 /// The end-of-block group of an 8x8 chroma transform.
166 pub eob_pt_64_chroma: [u16; 8],
167 /// The end-of-block group of a 4x4 chroma transform.
168 pub eob_pt_16_chroma: [u16; 6],
169 /// The end-of-block group of a 4x4 luma transform (lane-av1tx4).
170 pub eob_pt_16_luma: [u16; 6],
171 /// `eob_pt_16_luma`'s `TX_CLASS_HORIZ`/`TX_CLASS_VERT` sibling, read
172 /// instead whenever the TU's `tx_type` is `V_DCT`/`H_DCT` (r5).
173 pub eob_pt_16_luma_class1: [u16; 6],
174 /// `eob_pt_64_luma`'s class-1 sibling (r5), see `eob_pt_16_luma_class1`.
175 pub eob_pt_64_luma_class1: [u16; 8],
176 /// The end-of-block offset bit of a 16x16 luma transform.
177 pub eob_extra_luma_16: [[u16; 3]; 9],
178 /// The same, for an 8x8 luma transform.
179 pub eob_extra_luma_8: [[u16; 3]; 9],
180 /// The same, for a 4x4 luma transform.
181 pub eob_extra_luma_4: [[u16; 3]; 9],
182 /// The same, for an 8x8 chroma transform.
183 pub eob_extra_chroma_8: [[u16; 3]; 9],
184 /// The same, for a 4x4 chroma transform.
185 pub eob_extra_chroma_4: [[u16; 3]; 9],
186 /// The base level of a 16x16 luma coefficient.
187 pub base_luma_16: [[u16; 5]; 42],
188 /// The base level of an 8x8 luma coefficient.
189 pub base_luma_8: [[u16; 5]; 42],
190 /// The base level of a 4x4 luma coefficient.
191 pub base_luma_4: [[u16; 5]; 42],
192 /// The base level of an 8x8 chroma coefficient.
193 pub base_chroma_8: [[u16; 5]; 42],
194 /// The base level of a 4x4 chroma coefficient.
195 pub base_chroma_4: [[u16; 5]; 42],
196 /// The last coefficient's base level, for a 16x16 luma transform.
197 pub base_eob_luma_16: [[u16; 4]; 4],
198 /// The same, for an 8x8 luma transform.
199 pub base_eob_luma_8: [[u16; 4]; 4],
200 /// The same, for a 4x4 luma transform.
201 pub base_eob_luma_4: [[u16; 4]; 4],
202 /// The same, for an 8x8 chroma transform.
203 pub base_eob_chroma_8: [[u16; 4]; 4],
204 /// The same, for a 4x4 chroma transform.
205 pub base_eob_chroma_4: [[u16; 4]; 4],
206 /// The base-range tail of a 16x16 luma coefficient.
207 pub br_luma_16: [[u16; 5]; 21],
208 /// The base-range tail of an 8x8 luma coefficient.
209 pub br_luma_8: [[u16; 5]; 21],
210 /// The base-range tail of a 4x4 luma coefficient.
211 pub br_luma_4: [[u16; 5]; 21],
212 /// The base-range tail of an 8x8 chroma coefficient.
213 pub br_chroma_8: [[u16; 5]; 21],
214 /// The base-range tail of a 4x4 chroma coefficient.
215 pub br_chroma_4: [[u16; 5]; 21],
216 /// The partition symbol of a 16x16 block.
217 pub partition_w16: [[u16; 11]; 4],
218 /// The partition symbol of an 8x8 block (lane-av1-rect): only
219 /// `PARTITION_NONE` is ever coded against it, but the alphabet still
220 /// adapts on every read, same as every other partition table.
221 pub partition_w8: [[u16; 5]; 4],
222 /// The same as `txb_skip_luma_16`'s 7 contexts, for a 32x32 luma transform.
223 pub txb_skip_luma_32: [[u16; 3]; 7],
224 /// The same, for a 64x64 luma transform.
225 pub txb_skip_luma_64: [[u16; 3]; 7],
226 /// The all-zero flag of a 16x16 chroma transform.
227 pub txb_skip_chroma_16: [[u16; 3]; 3],
228 /// The all-zero flag of a 32x32 chroma transform.
229 pub txb_skip_chroma_32: [[u16; 3]; 3],
230 /// The end-of-block group of a luma transform of 1024 positions.
231 pub eob_pt_1024_luma: [u16; 12],
232 /// The same, for chroma.
233 pub eob_pt_1024_chroma: [u16; 12],
234 /// The end-of-block group of a 16x16 chroma transform.
235 pub eob_pt_256_chroma: [u16; 10],
236 /// The end-of-block offset bit, per table set.
237 pub eob_extra_luma_32: [[u16; 3]; 9],
238 /// The same, for a 64x64 luma transform.
239 pub eob_extra_luma_64: [[u16; 3]; 9],
240 /// The same, for a 16x16 chroma transform.
241 pub eob_extra_chroma_16: [[u16; 3]; 9],
242 /// The same, for a 32x32 chroma transform.
243 pub eob_extra_chroma_32: [[u16; 3]; 9],
244 /// The base level of a 32x32 luma coefficient.
245 pub base_luma_32: [[u16; 5]; 42],
246 /// The base level of a 64x64 luma coefficient.
247 pub base_luma_64: [[u16; 5]; 42],
248 /// The base level of a 16x16 chroma coefficient.
249 pub base_chroma_16: [[u16; 5]; 42],
250 /// The base level of a 32x32 chroma coefficient.
251 pub base_chroma_32: [[u16; 5]; 42],
252 /// The last coefficient's base level, per table set.
253 pub base_eob_luma_32: [[u16; 4]; 4],
254 /// The same, for a 64x64 luma transform.
255 pub base_eob_luma_64: [[u16; 4]; 4],
256 /// The same, for a 16x16 chroma transform.
257 pub base_eob_chroma_16: [[u16; 4]; 4],
258 /// The same, for a 32x32 chroma transform.
259 pub base_eob_chroma_32: [[u16; 4]; 4],
260 /// The base-range tail of a luma coefficient, which a 64x64 transform
261 /// reads too because its index is clamped at the 32x32 size.
262 pub br_luma_32: [[u16; 5]; 21],
263 /// The base-range tail of a 16x16 chroma coefficient.
264 pub br_chroma_16: [[u16; 5]; 21],
265 /// The base-range tail of a 32x32 chroma coefficient.
266 pub br_chroma_32: [[u16; 5]; 21],
267 /// The DC sign of a luma coefficient, shared by both luma sets.
268 pub dc_sign_luma: [[u16; 3]; 3],
269 /// The transform type of a 16x16 intra luma transform, by luma mode.
270 pub intra_tx_type_16: [[u16; 6]; 13],
271 /// The transform type of an 8x8 intra luma transform, by luma mode.
272 pub intra_tx_type_8: [[u16; 6]; 13],
273 /// The transform type of an 8x8 intra luma transform, by luma mode, when
274 /// `reduced_tx_set` is false (spec `TX_SET_INTRA_1`, seven types) --
275 /// [`Self::intra_tx_type_8`]'s five-type table is only the
276 /// `reduced_tx_set` row (round 8: a real `aomenc` stream with
277 /// `reduced_tx_set` off desyncs an 8x8 TU read against this row's
278 /// wrong-length CDF).
279 pub intra_tx_type_8_set1: [[u16; 8]; 13],
280 /// The transform type of a 4x4 intra luma transform, by luma mode
281 /// (lane-av1tx4): the flat five-symbol `reduced_tx_set` row, byte-
282 /// identical to [`Self::intra_tx_type_8`]'s own table
283 /// ([`cdf::INTRA_TX_TYPE_SET2_8`]) -- libaom leaves `TX_4X4` at the same
284 /// uniform default.
285 pub intra_tx_type_4: [[u16; 6]; 13],
286 /// [`Self::intra_tx_type_4`]'s `reduced_tx_set: false` counterpart, same
287 /// split as [`Self::intra_tx_type_8_set1`].
288 pub intra_tx_type_4_set1: [[u16; 8]; 13],
289 /// The transform type of an `is_inter` 32x32 luma transform (spec
290 /// `TX_SET_INTER_3`, `TX_32X32` row) -- unlike the intra 16x16 table, it
291 /// is not indexed by mode.
292 pub inter_tx_type_32: [u16; 3],
293 /// The transform type of an `is_inter` 16x16 luma transform under
294 /// `reduced_tx_set` (spec `TX_SET_INTER_3`, `TX_16X16` row) -- not
295 /// indexed by mode, same as [`Self::inter_tx_type_32`].
296 pub inter_tx_type_16: [u16; 3],
297 /// The transform type of an `is_inter` 8x8 luma transform under
298 /// `reduced_tx_set` (spec `TX_SET_INTER_3`, `TX_8X8` row) -- not indexed
299 /// by mode, same as [`Self::inter_tx_type_32`] (lane-av1inter8).
300 pub inter_tx_type_8: [u16; 3],
301 /// [`Self::inter_tx_type_16`]'s `reduced_tx_set: false` counterpart
302 /// (lane-cdffwd2): the twelve-symbol `EXT_TX_SET_DTT9_IDTX_1DDCT` row,
303 /// not indexed by mode.
304 pub inter_tx_type_16_set2: [u16; 13],
305 /// [`Self::inter_tx_type_8`]'s `reduced_tx_set: false` counterpart
306 /// (lane-cdffwd2): the sixteen-symbol `EXT_TX_SET_ALL16` row, not
307 /// indexed by mode.
308 pub inter_tx_type_8_set1: [u16; 17],
309 /// The DC sign of a chroma coefficient, shared by both chroma sets.
310 pub dc_sign_chroma: [[u16; 3]; 3],
311 /// Whether an inter frame's block is coded as intra.
312 pub intra_inter: [[u16; 3]; 4],
313 /// The six binary decisions of an inter block's single reference frame.
314 pub single_ref: [[[u16; 3]; 6]; 3],
315 /// Whether a block reads `SINGLE_REFERENCE` or `COMPOUND_REFERENCE`
316 /// (lane-av1comp, spec 5.11.25's `comp_mode`).
317 pub comp_mode: [[u16; 3]; 5],
318 /// Whether a block reads `skip_mode` (lane-av1comp, spec 5.11.29's
319 /// `skip_mode`), indexed by `av1_get_skip_mode_context`.
320 pub skip_mode: [[u16; 3]; 3],
321 /// Which of the eight `INTER_COMPOUND_MODES` a `COMPOUND_REFERENCE`
322 /// block takes (lane-av1comp, spec 5.11.24's `compound_mode`).
323 pub inter_compound_mode: [[u16; 9]; 8],
324 /// Unidirectional vs. bidirectional compound reference pair
325 /// (lane-av1comp, spec 5.11.25's `comp_reference_type`).
326 pub comp_ref_type: [[u16; 3]; 5],
327 /// The three binary decisions of a unidirectional compound pair.
328 pub uni_comp_ref: [[[u16; 3]; 3]; 3],
329 /// The three binary decisions of a bidirectional pair's forward ref.
330 pub comp_ref: [[[u16; 3]; 3]; 3],
331 /// The two binary decisions of a bidirectional pair's backward ref.
332 pub comp_bwdref: [[[u16; 3]; 2]; 3],
333 /// Whether a compound block's `comp_group_idx` picks masked compound
334 /// (lane-av1comp, spec 5.11.25's `comp_group_idx`).
335 pub comp_group_idx: [[u16; 3]; 6],
336 /// Whether a compound block's `compound_idx` picks the simple average
337 /// over the distance-weighted blend (lane-av1comp, spec 5.11.25's
338 /// `compound_idx`).
339 pub compound_idx: [[u16; 3]; 6],
340 /// The luma mode of an inter frame's intra block, by its size group.
341 pub y_mode: [[u16; 14]; 4],
342 /// Whether an inter block codes its motion vector explicitly.
343 pub new_mv: [[u16; 3]; 6],
344 /// Whether an inter block's motion vector is the zero vector.
345 pub zero_mv: [[u16; 3]; 2],
346 /// Which reference motion vector candidate an inter block takes.
347 pub ref_mv: [[u16; 3]; 6],
348 /// The dynamic reference list index of a motion vector prediction.
349 pub drl_mode: [[u16; 3]; 3],
350 /// A motion vector's joint (which components change), shared by both
351 /// components since it is read once per vector, not once per component.
352 pub mv_joint: [u16; 5],
353 /// The two motion vector components' own tables: separate owned state,
354 /// since spec 8.3.2 adapts one component without touching the other.
355 pub mv_comp: [MvComponentCdfs; 2],
356 /// The `use_filter_intra` flag, indexed by block-size class (`[0]`=4x4,
357 /// `[1]`=8x8, `[2]`=16x16, `[3]`=32x32) -- see [`cdf::FILTER_INTRA`].
358 pub filter_intra: [[u16; 3]; 4],
359 /// Which `FILTER_INTRA_MODES` entry a `use_filter_intra` block picks.
360 pub filter_intra_mode: [u16; 6],
361 /// `TxMode::Select`'s `tx_depth` flag at an 8x8 block, by `tx_size_context`
362 /// (0..=2) -- see [`cdf::TX_SIZE_CAT0`].
363 pub tx_size_cat0: [[u16; 3]; 3],
364 /// `tx_depth` at a 16x16 block -- see [`cdf::TX_SIZE_CAT1`].
365 pub tx_size_cat1: [[u16; 4]; 3],
366 /// `tx_depth` at a 32x32 block -- see [`cdf::TX_SIZE_CAT2`].
367 pub tx_size_cat2: [[u16; 4]; 3],
368 /// `tx_depth` at a 64x64 block -- see [`cdf::TX_SIZE_CAT3`].
369 pub tx_size_cat3: [[u16; 4]; 3],
370 /// A `SWITCHABLE`-filter inter block's own `interp_filter[dir]` -- see
371 /// [`cdf::SWITCHABLE_INTERP`].
372 pub switchable_interp: [[u16; 4]; 16],
373}
374
375/// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`,
376/// held once per component so that adapting one leaves the other alone).
377#[derive(Clone)]
378pub(crate) struct MvComponentCdfs {
379 /// The magnitude class.
380 pub class: [u16; 12],
381 /// The integer bit of a small-class magnitude.
382 pub class0_bit: [u16; 3],
383 /// The fractional part of a small-class magnitude, by its integer bit.
384 pub class0_fr: [[u16; 5]; 2],
385 /// The half-pel bit of a small-class magnitude (spec `mv_class0_hp`),
386 /// read only when the frame header sets `allow_high_precision_mv` (a
387 /// real encoder's choice, not this crate's own writer's -- which always
388 /// leaves the flag off and so never needs this table, but a foreign
389 /// stream this decoder reads can set it per frame).
390 pub class0_hp: [u16; 3],
391 /// One bit of an above-small-class magnitude, by its position.
392 pub bit: [[u16; 3]; 10],
393 /// The fractional part of an above-small-class magnitude.
394 pub fr: [u16; 5],
395 /// The half-pel bit of an above-small-class magnitude (spec `mv_hp`),
396 /// same `allow_high_precision_mv` gating as [`Self::class0_hp`].
397 pub hp: [u16; 3],
398 /// The component's sign.
399 pub sign: [u16; 3],
400}
401
402impl MvComponentCdfs {
403 fn new() -> MvComponentCdfs {
404 MvComponentCdfs {
405 class: cdf::MV_CLASS,
406 class0_bit: cdf::MV_CLASS0_BIT,
407 class0_fr: cdf::MV_CLASS0_FR,
408 class0_hp: cdf::MV_CLASS0_HP,
409 bit: cdf::MV_BIT,
410 fr: cdf::MV_FR,
411 hp: cdf::MV_HP,
412 sign: cdf::MV_SIGN,
413 }
414 }
415}
416
417/// Picks one of the four q-context variants of a coefficient table
418/// (`crate::cdf`'s `_Q0`/`_Q1`/plain/`_Q3` constants), `q_ctx` already reduced
419/// to 0..=3 by the caller.
420pub(crate) fn pick<T: Copy>(q_ctx: usize, q0: T, q1: T, q2: T, q3: T) -> T {
421 match q_ctx {
422 0 => q0,
423 1 => q1,
424 2 => q2,
425 _ => q3,
426 }
427}
428
429/// `reset_cdf_symbol_counter` (spec 7.20's `save_cdfs`, libaom
430/// `av1_reset_cdf_symbol_counters`, `entropy.c`): every adapting table's last
431/// entry is a symbol counter that slows [`update_cdf`](crate::msac)'s rate
432/// down for a table's first 32 observations. A tile decoded with
433/// `disable_frame_end_update_cdf == false` saves its *adapted* table into the
434/// reference slots the frame refreshes, but the counters go back to zero
435/// first -- forwarding the raw counts (this crate's bug until it was found:
436/// the counts climbed unchecked, so every table forwarded across two or more
437/// frames adapted *faster* than the real decoder's, a divergence too small to
438/// flip any one symbol in a single hop but compounding into a visible
439/// mismatch by the third).
440fn reset1<const N: usize>(a: &mut [u16; N]) {
441 a[N - 1] = 0;
442}
443fn reset2<const N: usize, const M: usize>(a: &mut [[u16; N]; M]) {
444 a.iter_mut().for_each(reset1);
445}
446fn reset3<const N: usize, const M: usize, const K: usize>(a: &mut [[[u16; N]; M]; K]) {
447 a.iter_mut().for_each(reset2);
448}
449
450impl MvComponentCdfs {
451 fn reset_counts(&mut self) {
452 reset1(&mut self.class);
453 reset1(&mut self.class0_bit);
454 reset2(&mut self.class0_fr);
455 reset1(&mut self.class0_hp);
456 reset2(&mut self.bit);
457 reset1(&mut self.fr);
458 reset1(&mut self.hp);
459 reset1(&mut self.sign);
460 }
461}
462
463impl Cdfs {
464 /// Zeroes every table's symbol counter (its last entry), leaving the
465 /// probabilities themselves untouched -- spec 7.20's `save_cdfs`. Call
466 /// this on the tile's own end-of-tile adapted state before storing it
467 /// into a reference slot for cross-frame forwarding; never on a table
468 /// still being read within the same tile.
469 pub(crate) fn reset_counts(&mut self) {
470 reset2(&mut self.partition_w64);
471 reset2(&mut self.partition_w32);
472 reset2(&mut self.skip);
473 reset3(&mut self.kf_y_mode);
474 reset2(&mut self.uv_mode_no_cfl);
475 reset2(&mut self.uv_mode_cfl);
476 reset1(&mut self.cfl_sign);
477 reset2(&mut self.cfl_alpha);
478 reset2(&mut self.angle_delta);
479 reset2(&mut self.txb_skip_luma_16);
480 reset2(&mut self.txb_skip_luma_8);
481 reset2(&mut self.txb_skip_luma_4);
482 reset2(&mut self.txb_skip_chroma_8);
483 reset2(&mut self.txb_skip_chroma_4);
484 reset1(&mut self.eob_pt_256_luma);
485 reset1(&mut self.eob_pt_64_luma);
486 reset1(&mut self.eob_pt_64_chroma);
487 reset1(&mut self.eob_pt_16_chroma);
488 reset1(&mut self.eob_pt_16_luma);
489 reset1(&mut self.eob_pt_16_luma_class1);
490 reset1(&mut self.eob_pt_64_luma_class1);
491 reset2(&mut self.eob_extra_luma_16);
492 reset2(&mut self.eob_extra_luma_8);
493 reset2(&mut self.eob_extra_luma_4);
494 reset2(&mut self.eob_extra_chroma_8);
495 reset2(&mut self.eob_extra_chroma_4);
496 reset2(&mut self.base_luma_16);
497 reset2(&mut self.base_luma_8);
498 reset2(&mut self.base_luma_4);
499 reset2(&mut self.base_chroma_8);
500 reset2(&mut self.base_chroma_4);
501 reset2(&mut self.base_eob_luma_16);
502 reset2(&mut self.base_eob_luma_8);
503 reset2(&mut self.base_eob_luma_4);
504 reset2(&mut self.base_eob_chroma_8);
505 reset2(&mut self.base_eob_chroma_4);
506 reset2(&mut self.br_luma_16);
507 reset2(&mut self.br_luma_8);
508 reset2(&mut self.br_luma_4);
509 reset2(&mut self.br_chroma_8);
510 reset2(&mut self.br_chroma_4);
511 reset2(&mut self.partition_w16);
512 reset2(&mut self.partition_w8);
513 reset2(&mut self.txb_skip_luma_32);
514 reset2(&mut self.txb_skip_luma_64);
515 reset2(&mut self.txb_skip_chroma_16);
516 reset2(&mut self.txb_skip_chroma_32);
517 reset1(&mut self.eob_pt_1024_luma);
518 reset1(&mut self.eob_pt_1024_chroma);
519 reset1(&mut self.eob_pt_256_chroma);
520 reset2(&mut self.eob_extra_luma_32);
521 reset2(&mut self.eob_extra_luma_64);
522 reset2(&mut self.eob_extra_chroma_16);
523 reset2(&mut self.eob_extra_chroma_32);
524 reset2(&mut self.base_luma_32);
525 reset2(&mut self.base_luma_64);
526 reset2(&mut self.base_chroma_16);
527 reset2(&mut self.base_chroma_32);
528 reset2(&mut self.base_eob_luma_32);
529 reset2(&mut self.base_eob_luma_64);
530 reset2(&mut self.base_eob_chroma_16);
531 reset2(&mut self.base_eob_chroma_32);
532 reset2(&mut self.br_luma_32);
533 reset2(&mut self.br_chroma_16);
534 reset2(&mut self.br_chroma_32);
535 reset2(&mut self.dc_sign_luma);
536 reset2(&mut self.intra_tx_type_16);
537 reset2(&mut self.intra_tx_type_8);
538 reset2(&mut self.intra_tx_type_8_set1);
539 reset2(&mut self.intra_tx_type_4);
540 reset2(&mut self.intra_tx_type_4_set1);
541 reset1(&mut self.inter_tx_type_32);
542 reset1(&mut self.inter_tx_type_16);
543 reset1(&mut self.inter_tx_type_8);
544 reset1(&mut self.inter_tx_type_16_set2);
545 reset1(&mut self.inter_tx_type_8_set1);
546 reset2(&mut self.dc_sign_chroma);
547 reset2(&mut self.intra_inter);
548 reset3(&mut self.single_ref);
549 reset2(&mut self.comp_mode);
550 reset2(&mut self.skip_mode);
551 reset2(&mut self.inter_compound_mode);
552 reset2(&mut self.comp_ref_type);
553 reset3(&mut self.uni_comp_ref);
554 reset3(&mut self.comp_ref);
555 reset3(&mut self.comp_bwdref);
556 reset2(&mut self.comp_group_idx);
557 reset2(&mut self.compound_idx);
558 reset2(&mut self.y_mode);
559 reset2(&mut self.new_mv);
560 reset2(&mut self.zero_mv);
561 reset2(&mut self.ref_mv);
562 reset2(&mut self.drl_mode);
563 reset1(&mut self.mv_joint);
564 self.mv_comp
565 .iter_mut()
566 .for_each(MvComponentCdfs::reset_counts);
567 reset2(&mut self.filter_intra);
568 reset1(&mut self.filter_intra_mode);
569 reset2(&mut self.tx_size_cat0);
570 reset2(&mut self.tx_size_cat1);
571 reset2(&mut self.tx_size_cat2);
572 reset2(&mut self.tx_size_cat3);
573 }
574
575 /// The defaults a key frame starts from (spec 8.4, `init_coeff_cdfs` and
576 /// `init_non_coeff_cdfs`), for coefficient q-context `q_ctx` (0..=3, spec
577 /// 8.3.2's `Get_Qctx`). The non-coefficient tables do not vary with the
578 /// quantizer, so they are the same for every `q_ctx`.
579 pub fn new(q_ctx: usize) -> Cdfs {
580 Cdfs {
581 partition_w64: cdf::PARTITION_W64,
582 partition_w32: cdf::PARTITION_W32,
583 skip: cdf::SKIP,
584 kf_y_mode: cdf::KF_Y_MODE,
585 uv_mode_no_cfl: cdf::UV_MODE_NO_CFL,
586 uv_mode_cfl: cdf::UV_MODE_CFL,
587 cfl_sign: cdf::CFL_SIGN,
588 cfl_alpha: cdf::CFL_ALPHA,
589 angle_delta: cdf::ANGLE_DELTA,
590 txb_skip_luma_16: pick(
591 q_ctx,
592 cdf::TXB_SKIP_LUMA_16_Q0_CTX,
593 cdf::TXB_SKIP_LUMA_16_Q1_CTX,
594 cdf::TXB_SKIP_LUMA_16_CTX,
595 cdf::TXB_SKIP_LUMA_16_Q3_CTX,
596 ),
597 txb_skip_luma_8: pick(
598 q_ctx,
599 cdf::TXB_SKIP_LUMA_8_Q0_CTX,
600 cdf::TXB_SKIP_LUMA_8_Q1_CTX,
601 cdf::TXB_SKIP_LUMA_8_CTX,
602 cdf::TXB_SKIP_LUMA_8_Q3_CTX,
603 ),
604 txb_skip_luma_4: pick(
605 q_ctx,
606 cdf::TXB_SKIP_LUMA_4_Q0_CTX,
607 cdf::TXB_SKIP_LUMA_4_Q1_CTX,
608 cdf::TXB_SKIP_LUMA_4_CTX,
609 cdf::TXB_SKIP_LUMA_4_Q3_CTX,
610 ),
611 txb_skip_chroma_8: pick(
612 q_ctx,
613 cdf::TXB_SKIP_CHROMA_8_Q0,
614 cdf::TXB_SKIP_CHROMA_8_Q1,
615 cdf::TXB_SKIP_CHROMA_8,
616 cdf::TXB_SKIP_CHROMA_8_Q3,
617 ),
618 txb_skip_chroma_4: pick(
619 q_ctx,
620 cdf::TXB_SKIP_CHROMA_4_Q0,
621 cdf::TXB_SKIP_CHROMA_4_Q1,
622 cdf::TXB_SKIP_CHROMA_4,
623 cdf::TXB_SKIP_CHROMA_4_Q3,
624 ),
625 eob_pt_256_luma: pick(
626 q_ctx,
627 cdf::EOB_PT_256_LUMA_Q0,
628 cdf::EOB_PT_256_LUMA_Q1,
629 cdf::EOB_PT_256_LUMA,
630 cdf::EOB_PT_256_LUMA_Q3,
631 ),
632 eob_pt_64_luma: pick(
633 q_ctx,
634 cdf::EOB_PT_64_LUMA_Q0,
635 cdf::EOB_PT_64_LUMA_Q1,
636 cdf::EOB_PT_64_LUMA,
637 cdf::EOB_PT_64_LUMA_Q3,
638 ),
639 eob_pt_64_chroma: pick(
640 q_ctx,
641 cdf::EOB_PT_64_CHROMA_Q0,
642 cdf::EOB_PT_64_CHROMA_Q1,
643 cdf::EOB_PT_64_CHROMA,
644 cdf::EOB_PT_64_CHROMA_Q3,
645 ),
646 eob_pt_16_chroma: pick(
647 q_ctx,
648 cdf::EOB_PT_16_CHROMA_Q0,
649 cdf::EOB_PT_16_CHROMA_Q1,
650 cdf::EOB_PT_16_CHROMA,
651 cdf::EOB_PT_16_CHROMA_Q3,
652 ),
653 eob_pt_16_luma: pick(
654 q_ctx,
655 cdf::EOB_PT_16_LUMA_Q0,
656 cdf::EOB_PT_16_LUMA_Q1,
657 cdf::EOB_PT_16_LUMA,
658 cdf::EOB_PT_16_LUMA_Q3,
659 ),
660 eob_pt_16_luma_class1: pick(
661 q_ctx,
662 cdf::EOB_PT_16_LUMA_CLASS1_Q0,
663 cdf::EOB_PT_16_LUMA_CLASS1_Q1,
664 cdf::EOB_PT_16_LUMA_CLASS1,
665 cdf::EOB_PT_16_LUMA_CLASS1_Q3,
666 ),
667 eob_pt_64_luma_class1: pick(
668 q_ctx,
669 cdf::EOB_PT_64_LUMA_CLASS1_Q0,
670 cdf::EOB_PT_64_LUMA_CLASS1_Q1,
671 cdf::EOB_PT_64_LUMA_CLASS1,
672 cdf::EOB_PT_64_LUMA_CLASS1_Q3,
673 ),
674 eob_extra_luma_16: pick(
675 q_ctx,
676 cdf::EOB_EXTRA_LUMA_16_Q0,
677 cdf::EOB_EXTRA_LUMA_16_Q1,
678 cdf::EOB_EXTRA_LUMA_16,
679 cdf::EOB_EXTRA_LUMA_16_Q3,
680 ),
681 eob_extra_luma_8: pick(
682 q_ctx,
683 cdf::EOB_EXTRA_LUMA_8_Q0,
684 cdf::EOB_EXTRA_LUMA_8_Q1,
685 cdf::EOB_EXTRA_LUMA_8,
686 cdf::EOB_EXTRA_LUMA_8_Q3,
687 ),
688 eob_extra_luma_4: pick(
689 q_ctx,
690 cdf::EOB_EXTRA_LUMA_4_Q0,
691 cdf::EOB_EXTRA_LUMA_4_Q1,
692 cdf::EOB_EXTRA_LUMA_4,
693 cdf::EOB_EXTRA_LUMA_4_Q3,
694 ),
695 eob_extra_chroma_8: pick(
696 q_ctx,
697 cdf::EOB_EXTRA_CHROMA_8_Q0,
698 cdf::EOB_EXTRA_CHROMA_8_Q1,
699 cdf::EOB_EXTRA_CHROMA_8,
700 cdf::EOB_EXTRA_CHROMA_8_Q3,
701 ),
702 eob_extra_chroma_4: pick(
703 q_ctx,
704 cdf::EOB_EXTRA_CHROMA_4_Q0,
705 cdf::EOB_EXTRA_CHROMA_4_Q1,
706 cdf::EOB_EXTRA_CHROMA_4,
707 cdf::EOB_EXTRA_CHROMA_4_Q3,
708 ),
709 base_luma_16: pick(
710 q_ctx,
711 cdf::COEFF_BASE_LUMA_16_Q0,
712 cdf::COEFF_BASE_LUMA_16_Q1,
713 cdf::COEFF_BASE_LUMA_16,
714 cdf::COEFF_BASE_LUMA_16_Q3,
715 ),
716 base_luma_8: pick(
717 q_ctx,
718 cdf::COEFF_BASE_LUMA_8_Q0,
719 cdf::COEFF_BASE_LUMA_8_Q1,
720 cdf::COEFF_BASE_LUMA_8,
721 cdf::COEFF_BASE_LUMA_8_Q3,
722 ),
723 base_luma_4: pick(
724 q_ctx,
725 cdf::COEFF_BASE_LUMA_4_Q0,
726 cdf::COEFF_BASE_LUMA_4_Q1,
727 cdf::COEFF_BASE_LUMA_4,
728 cdf::COEFF_BASE_LUMA_4_Q3,
729 ),
730 base_chroma_8: pick(
731 q_ctx,
732 cdf::COEFF_BASE_CHROMA_8_Q0,
733 cdf::COEFF_BASE_CHROMA_8_Q1,
734 cdf::COEFF_BASE_CHROMA_8,
735 cdf::COEFF_BASE_CHROMA_8_Q3,
736 ),
737 base_chroma_4: pick(
738 q_ctx,
739 cdf::COEFF_BASE_CHROMA_4_Q0,
740 cdf::COEFF_BASE_CHROMA_4_Q1,
741 cdf::COEFF_BASE_CHROMA_4,
742 cdf::COEFF_BASE_CHROMA_4_Q3,
743 ),
744 base_eob_luma_16: pick(
745 q_ctx,
746 cdf::COEFF_BASE_EOB_LUMA_16_Q0,
747 cdf::COEFF_BASE_EOB_LUMA_16_Q1,
748 cdf::COEFF_BASE_EOB_LUMA_16,
749 cdf::COEFF_BASE_EOB_LUMA_16_Q3,
750 ),
751 base_eob_luma_8: pick(
752 q_ctx,
753 cdf::COEFF_BASE_EOB_LUMA_8_Q0,
754 cdf::COEFF_BASE_EOB_LUMA_8_Q1,
755 cdf::COEFF_BASE_EOB_LUMA_8,
756 cdf::COEFF_BASE_EOB_LUMA_8_Q3,
757 ),
758 base_eob_luma_4: pick(
759 q_ctx,
760 cdf::COEFF_BASE_EOB_LUMA_4_Q0,
761 cdf::COEFF_BASE_EOB_LUMA_4_Q1,
762 cdf::COEFF_BASE_EOB_LUMA_4,
763 cdf::COEFF_BASE_EOB_LUMA_4_Q3,
764 ),
765 base_eob_chroma_8: pick(
766 q_ctx,
767 cdf::COEFF_BASE_EOB_CHROMA_8_Q0,
768 cdf::COEFF_BASE_EOB_CHROMA_8_Q1,
769 cdf::COEFF_BASE_EOB_CHROMA_8,
770 cdf::COEFF_BASE_EOB_CHROMA_8_Q3,
771 ),
772 base_eob_chroma_4: pick(
773 q_ctx,
774 cdf::COEFF_BASE_EOB_CHROMA_4_Q0,
775 cdf::COEFF_BASE_EOB_CHROMA_4_Q1,
776 cdf::COEFF_BASE_EOB_CHROMA_4,
777 cdf::COEFF_BASE_EOB_CHROMA_4_Q3,
778 ),
779 br_luma_16: pick(
780 q_ctx,
781 cdf::COEFF_BR_LUMA_16_Q0,
782 cdf::COEFF_BR_LUMA_16_Q1,
783 cdf::COEFF_BR_LUMA_16,
784 cdf::COEFF_BR_LUMA_16_Q3,
785 ),
786 br_luma_8: pick(
787 q_ctx,
788 cdf::COEFF_BR_LUMA_8_Q0,
789 cdf::COEFF_BR_LUMA_8_Q1,
790 cdf::COEFF_BR_LUMA_8,
791 cdf::COEFF_BR_LUMA_8_Q3,
792 ),
793 br_luma_4: pick(
794 q_ctx,
795 cdf::COEFF_BR_LUMA_4_Q0,
796 cdf::COEFF_BR_LUMA_4_Q1,
797 cdf::COEFF_BR_LUMA_4,
798 cdf::COEFF_BR_LUMA_4_Q3,
799 ),
800 br_chroma_8: pick(
801 q_ctx,
802 cdf::COEFF_BR_CHROMA_8_Q0,
803 cdf::COEFF_BR_CHROMA_8_Q1,
804 cdf::COEFF_BR_CHROMA_8,
805 cdf::COEFF_BR_CHROMA_8_Q3,
806 ),
807 br_chroma_4: pick(
808 q_ctx,
809 cdf::COEFF_BR_CHROMA_4_Q0,
810 cdf::COEFF_BR_CHROMA_4_Q1,
811 cdf::COEFF_BR_CHROMA_4,
812 cdf::COEFF_BR_CHROMA_4_Q3,
813 ),
814 partition_w16: cdf::PARTITION_W16,
815 partition_w8: cdf::PARTITION_W8,
816 txb_skip_luma_32: pick(
817 q_ctx,
818 cdf::TXB_SKIP_LUMA_32_Q0_CTX,
819 cdf::TXB_SKIP_LUMA_32_Q1_CTX,
820 cdf::TXB_SKIP_LUMA_32_CTX,
821 cdf::TXB_SKIP_LUMA_32_Q3_CTX,
822 ),
823 txb_skip_luma_64: pick(
824 q_ctx,
825 cdf::TXB_SKIP_LUMA_64_Q0_CTX,
826 cdf::TXB_SKIP_LUMA_64_Q1_CTX,
827 cdf::TXB_SKIP_LUMA_64_CTX,
828 cdf::TXB_SKIP_LUMA_64_Q3_CTX,
829 ),
830 txb_skip_chroma_16: pick(
831 q_ctx,
832 cdf::TXB_SKIP_CHROMA_16_Q0,
833 cdf::TXB_SKIP_CHROMA_16_Q1,
834 cdf::TXB_SKIP_CHROMA_16,
835 cdf::TXB_SKIP_CHROMA_16_Q3,
836 ),
837 txb_skip_chroma_32: pick(
838 q_ctx,
839 cdf::TXB_SKIP_CHROMA_32_Q0,
840 cdf::TXB_SKIP_CHROMA_32_Q1,
841 cdf::TXB_SKIP_CHROMA_32,
842 cdf::TXB_SKIP_CHROMA_32_Q3,
843 ),
844 eob_pt_1024_luma: pick(
845 q_ctx,
846 cdf::EOB_PT_1024_LUMA_Q0,
847 cdf::EOB_PT_1024_LUMA_Q1,
848 cdf::EOB_PT_1024_LUMA,
849 cdf::EOB_PT_1024_LUMA_Q3,
850 ),
851 eob_pt_1024_chroma: pick(
852 q_ctx,
853 cdf::EOB_PT_1024_CHROMA_Q0,
854 cdf::EOB_PT_1024_CHROMA_Q1,
855 cdf::EOB_PT_1024_CHROMA,
856 cdf::EOB_PT_1024_CHROMA_Q3,
857 ),
858 eob_pt_256_chroma: pick(
859 q_ctx,
860 cdf::EOB_PT_256_CHROMA_Q0,
861 cdf::EOB_PT_256_CHROMA_Q1,
862 cdf::EOB_PT_256_CHROMA,
863 cdf::EOB_PT_256_CHROMA_Q3,
864 ),
865 eob_extra_luma_32: pick(
866 q_ctx,
867 cdf::EOB_EXTRA_LUMA_32_Q0,
868 cdf::EOB_EXTRA_LUMA_32_Q1,
869 cdf::EOB_EXTRA_LUMA_32,
870 cdf::EOB_EXTRA_LUMA_32_Q3,
871 ),
872 eob_extra_luma_64: pick(
873 q_ctx,
874 cdf::EOB_EXTRA_LUMA_64_Q0,
875 cdf::EOB_EXTRA_LUMA_64_Q1,
876 cdf::EOB_EXTRA_LUMA_64,
877 cdf::EOB_EXTRA_LUMA_64_Q3,
878 ),
879 eob_extra_chroma_16: pick(
880 q_ctx,
881 cdf::EOB_EXTRA_CHROMA_16_Q0,
882 cdf::EOB_EXTRA_CHROMA_16_Q1,
883 cdf::EOB_EXTRA_CHROMA_16,
884 cdf::EOB_EXTRA_CHROMA_16_Q3,
885 ),
886 eob_extra_chroma_32: pick(
887 q_ctx,
888 cdf::EOB_EXTRA_CHROMA_32_Q0,
889 cdf::EOB_EXTRA_CHROMA_32_Q1,
890 cdf::EOB_EXTRA_CHROMA_32,
891 cdf::EOB_EXTRA_CHROMA_32_Q3,
892 ),
893 base_luma_32: pick(
894 q_ctx,
895 cdf::COEFF_BASE_LUMA_32_Q0,
896 cdf::COEFF_BASE_LUMA_32_Q1,
897 cdf::COEFF_BASE_LUMA_32,
898 cdf::COEFF_BASE_LUMA_32_Q3,
899 ),
900 base_luma_64: pick(
901 q_ctx,
902 cdf::COEFF_BASE_LUMA_64_Q0,
903 cdf::COEFF_BASE_LUMA_64_Q1,
904 cdf::COEFF_BASE_LUMA_64,
905 cdf::COEFF_BASE_LUMA_64_Q3,
906 ),
907 base_chroma_16: pick(
908 q_ctx,
909 cdf::COEFF_BASE_CHROMA_16_Q0,
910 cdf::COEFF_BASE_CHROMA_16_Q1,
911 cdf::COEFF_BASE_CHROMA_16,
912 cdf::COEFF_BASE_CHROMA_16_Q3,
913 ),
914 base_chroma_32: pick(
915 q_ctx,
916 cdf::COEFF_BASE_CHROMA_32_Q0,
917 cdf::COEFF_BASE_CHROMA_32_Q1,
918 cdf::COEFF_BASE_CHROMA_32,
919 cdf::COEFF_BASE_CHROMA_32_Q3,
920 ),
921 base_eob_luma_32: pick(
922 q_ctx,
923 cdf::COEFF_BASE_EOB_LUMA_32_Q0,
924 cdf::COEFF_BASE_EOB_LUMA_32_Q1,
925 cdf::COEFF_BASE_EOB_LUMA_32,
926 cdf::COEFF_BASE_EOB_LUMA_32_Q3,
927 ),
928 base_eob_luma_64: pick(
929 q_ctx,
930 cdf::COEFF_BASE_EOB_LUMA_64_Q0,
931 cdf::COEFF_BASE_EOB_LUMA_64_Q1,
932 cdf::COEFF_BASE_EOB_LUMA_64,
933 cdf::COEFF_BASE_EOB_LUMA_64_Q3,
934 ),
935 base_eob_chroma_16: pick(
936 q_ctx,
937 cdf::COEFF_BASE_EOB_CHROMA_16_Q0,
938 cdf::COEFF_BASE_EOB_CHROMA_16_Q1,
939 cdf::COEFF_BASE_EOB_CHROMA_16,
940 cdf::COEFF_BASE_EOB_CHROMA_16_Q3,
941 ),
942 base_eob_chroma_32: pick(
943 q_ctx,
944 cdf::COEFF_BASE_EOB_CHROMA_32_Q0,
945 cdf::COEFF_BASE_EOB_CHROMA_32_Q1,
946 cdf::COEFF_BASE_EOB_CHROMA_32,
947 cdf::COEFF_BASE_EOB_CHROMA_32_Q3,
948 ),
949 br_luma_32: pick(
950 q_ctx,
951 cdf::COEFF_BR_LUMA_32_Q0,
952 cdf::COEFF_BR_LUMA_32_Q1,
953 cdf::COEFF_BR_LUMA_32,
954 cdf::COEFF_BR_LUMA_32_Q3,
955 ),
956 br_chroma_16: pick(
957 q_ctx,
958 cdf::COEFF_BR_CHROMA_16_Q0,
959 cdf::COEFF_BR_CHROMA_16_Q1,
960 cdf::COEFF_BR_CHROMA_16,
961 cdf::COEFF_BR_CHROMA_16_Q3,
962 ),
963 br_chroma_32: pick(
964 q_ctx,
965 cdf::COEFF_BR_CHROMA_32_Q0,
966 cdf::COEFF_BR_CHROMA_32_Q1,
967 cdf::COEFF_BR_CHROMA_32,
968 cdf::COEFF_BR_CHROMA_32_Q3,
969 ),
970 dc_sign_luma: cdf::DC_SIGN_LUMA,
971 intra_tx_type_16: cdf::INTRA_TX_TYPE_SET2_16,
972 intra_tx_type_8: cdf::INTRA_TX_TYPE_SET2_8,
973 intra_tx_type_8_set1: cdf::INTRA_TX_TYPE_SET1_8,
974 intra_tx_type_4: cdf::INTRA_TX_TYPE_SET2_8,
975 intra_tx_type_4_set1: cdf::INTRA_TX_TYPE_SET1_4,
976 inter_tx_type_32: cdf::INTER_TX_TYPE_SET3_32,
977 inter_tx_type_16: cdf::INTER_TX_TYPE_SET3_16,
978 inter_tx_type_8: cdf::INTER_TX_TYPE_SET3_8,
979 inter_tx_type_16_set2: cdf::INTER_TX_TYPE_SET2_16,
980 inter_tx_type_8_set1: cdf::INTER_TX_TYPE_SET1_8,
981 dc_sign_chroma: cdf::DC_SIGN_CHROMA,
982 intra_inter: cdf::INTRA_INTER,
983 single_ref: cdf::SINGLE_REF,
984 comp_mode: cdf::COMP_MODE,
985 inter_compound_mode: cdf::INTER_COMPOUND_MODE,
986 comp_ref_type: cdf::COMP_REF_TYPE,
987 uni_comp_ref: cdf::UNI_COMP_REF,
988 comp_ref: cdf::COMP_REF,
989 comp_bwdref: cdf::COMP_BWDREF,
990 skip_mode: cdf::SKIP_MODE,
991 comp_group_idx: cdf::COMP_GROUP_IDX,
992 compound_idx: cdf::COMPOUND_IDX,
993 y_mode: cdf::Y_MODE,
994 new_mv: cdf::NEW_MV,
995 zero_mv: cdf::ZERO_MV,
996 ref_mv: cdf::REF_MV,
997 drl_mode: cdf::DRL_MODE,
998 mv_joint: cdf::MV_JOINT,
999 mv_comp: [MvComponentCdfs::new(), MvComponentCdfs::new()],
1000 filter_intra: cdf::FILTER_INTRA,
1001 filter_intra_mode: cdf::FILTER_INTRA_MODE,
1002 tx_size_cat0: cdf::TX_SIZE_CAT0,
1003 tx_size_cat1: cdf::TX_SIZE_CAT1,
1004 tx_size_cat2: cdf::TX_SIZE_CAT2,
1005 tx_size_cat3: cdf::TX_SIZE_CAT3,
1006 switchable_interp: cdf::SWITCHABLE_INTERP,
1007 }
1008 }
1009
1010 /// The tables one table set is coded with, borrowed out of the state.
1011 pub fn txb(&mut self, set: TxbSet, mode: usize) -> TxbTables<'_> {
1012 match set {
1013 TxbSet::Luma32 => TxbTables {
1014 side: 32,
1015 txb_skip: &mut self.txb_skip_luma_32,
1016 eob_pt: &mut self.eob_pt_1024_luma,
1017 eob_extra: &mut self.eob_extra_luma_32,
1018 base: &mut self.base_luma_32,
1019 base_eob: &mut self.base_eob_luma_32,
1020 br: &mut self.br_luma_32,
1021 dc_sign: &mut self.dc_sign_luma,
1022 tx_type: None,
1023 eob_pt_class1: None,
1024 },
1025 TxbSet::Luma32Inter => TxbTables {
1026 side: 32,
1027 txb_skip: &mut self.txb_skip_luma_32,
1028 eob_pt: &mut self.eob_pt_1024_luma,
1029 eob_extra: &mut self.eob_extra_luma_32,
1030 base: &mut self.base_luma_32,
1031 base_eob: &mut self.base_eob_luma_32,
1032 br: &mut self.br_luma_32,
1033 dc_sign: &mut self.dc_sign_luma,
1034 tx_type: Some(self.inter_tx_type_32.as_mut_slice()),
1035 eob_pt_class1: None,
1036 },
1037 TxbSet::Luma64 => TxbTables {
1038 side: 32,
1039 txb_skip: &mut self.txb_skip_luma_64,
1040 eob_pt: &mut self.eob_pt_1024_luma,
1041 eob_extra: &mut self.eob_extra_luma_64,
1042 base: &mut self.base_luma_64,
1043 base_eob: &mut self.base_eob_luma_64,
1044 br: &mut self.br_luma_32,
1045 dc_sign: &mut self.dc_sign_luma,
1046 tx_type: None,
1047 eob_pt_class1: None,
1048 },
1049 TxbSet::Luma16 => TxbTables {
1050 side: 16,
1051 txb_skip: &mut self.txb_skip_luma_16,
1052 eob_pt: &mut self.eob_pt_256_luma,
1053 eob_extra: &mut self.eob_extra_luma_16,
1054 base: &mut self.base_luma_16,
1055 base_eob: &mut self.base_eob_luma_16,
1056 br: &mut self.br_luma_16,
1057 dc_sign: &mut self.dc_sign_luma,
1058 tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()),
1059 eob_pt_class1: None,
1060 },
1061 TxbSet::Luma16Inter => TxbTables {
1062 side: 16,
1063 txb_skip: &mut self.txb_skip_luma_16,
1064 eob_pt: &mut self.eob_pt_256_luma,
1065 eob_extra: &mut self.eob_extra_luma_16,
1066 base: &mut self.base_luma_16,
1067 base_eob: &mut self.base_eob_luma_16,
1068 br: &mut self.br_luma_16,
1069 dc_sign: &mut self.dc_sign_luma,
1070 tx_type: Some(self.inter_tx_type_16.as_mut_slice()),
1071 eob_pt_class1: None,
1072 },
1073 TxbSet::Luma16InterSet1 => TxbTables {
1074 side: 16,
1075 txb_skip: &mut self.txb_skip_luma_16,
1076 eob_pt: &mut self.eob_pt_256_luma,
1077 eob_extra: &mut self.eob_extra_luma_16,
1078 base: &mut self.base_luma_16,
1079 base_eob: &mut self.base_eob_luma_16,
1080 br: &mut self.br_luma_16,
1081 dc_sign: &mut self.dc_sign_luma,
1082 tx_type: Some(self.inter_tx_type_16_set2.as_mut_slice()),
1083 eob_pt_class1: None,
1084 },
1085 TxbSet::Luma8 => TxbTables {
1086 side: 8,
1087 txb_skip: &mut self.txb_skip_luma_8,
1088 eob_pt: &mut self.eob_pt_64_luma,
1089 eob_extra: &mut self.eob_extra_luma_8,
1090 base: &mut self.base_luma_8,
1091 base_eob: &mut self.base_eob_luma_8,
1092 br: &mut self.br_luma_8,
1093 dc_sign: &mut self.dc_sign_luma,
1094 tx_type: Some(self.intra_tx_type_8[mode].as_mut_slice()),
1095 eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1),
1096 },
1097 TxbSet::Luma8Set1 => TxbTables {
1098 side: 8,
1099 txb_skip: &mut self.txb_skip_luma_8,
1100 eob_pt: &mut self.eob_pt_64_luma,
1101 eob_extra: &mut self.eob_extra_luma_8,
1102 base: &mut self.base_luma_8,
1103 base_eob: &mut self.base_eob_luma_8,
1104 br: &mut self.br_luma_8,
1105 dc_sign: &mut self.dc_sign_luma,
1106 tx_type: Some(self.intra_tx_type_8_set1[mode].as_mut_slice()),
1107 eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1),
1108 },
1109 TxbSet::Luma4 => TxbTables {
1110 side: 4,
1111 txb_skip: &mut self.txb_skip_luma_4,
1112 eob_pt: &mut self.eob_pt_16_luma,
1113 eob_extra: &mut self.eob_extra_luma_4,
1114 base: &mut self.base_luma_4,
1115 base_eob: &mut self.base_eob_luma_4,
1116 br: &mut self.br_luma_4,
1117 dc_sign: &mut self.dc_sign_luma,
1118 tx_type: Some(self.intra_tx_type_4[mode].as_mut_slice()),
1119 eob_pt_class1: Some(&mut self.eob_pt_16_luma_class1),
1120 },
1121 TxbSet::Luma4Set1 => TxbTables {
1122 side: 4,
1123 txb_skip: &mut self.txb_skip_luma_4,
1124 eob_pt: &mut self.eob_pt_16_luma,
1125 eob_extra: &mut self.eob_extra_luma_4,
1126 base: &mut self.base_luma_4,
1127 base_eob: &mut self.base_eob_luma_4,
1128 br: &mut self.br_luma_4,
1129 dc_sign: &mut self.dc_sign_luma,
1130 tx_type: Some(self.intra_tx_type_4_set1[mode].as_mut_slice()),
1131 eob_pt_class1: Some(&mut self.eob_pt_16_luma_class1),
1132 },
1133 TxbSet::Luma8Inter => TxbTables {
1134 side: 8,
1135 txb_skip: &mut self.txb_skip_luma_8,
1136 eob_pt: &mut self.eob_pt_64_luma,
1137 eob_extra: &mut self.eob_extra_luma_8,
1138 base: &mut self.base_luma_8,
1139 base_eob: &mut self.base_eob_luma_8,
1140 br: &mut self.br_luma_8,
1141 dc_sign: &mut self.dc_sign_luma,
1142 tx_type: Some(self.inter_tx_type_8.as_mut_slice()),
1143 eob_pt_class1: None,
1144 },
1145 TxbSet::Luma8InterSet1 => TxbTables {
1146 side: 8,
1147 txb_skip: &mut self.txb_skip_luma_8,
1148 eob_pt: &mut self.eob_pt_64_luma,
1149 eob_extra: &mut self.eob_extra_luma_8,
1150 base: &mut self.base_luma_8,
1151 base_eob: &mut self.base_eob_luma_8,
1152 br: &mut self.br_luma_8,
1153 dc_sign: &mut self.dc_sign_luma,
1154 tx_type: Some(self.inter_tx_type_8_set1.as_mut_slice()),
1155 eob_pt_class1: Some(&mut self.eob_pt_64_luma_class1),
1156 },
1157 TxbSet::Chroma8 => TxbTables {
1158 side: 8,
1159 txb_skip: &mut self.txb_skip_chroma_8,
1160 eob_pt: &mut self.eob_pt_64_chroma,
1161 eob_extra: &mut self.eob_extra_chroma_8,
1162 base: &mut self.base_chroma_8,
1163 base_eob: &mut self.base_eob_chroma_8,
1164 br: &mut self.br_chroma_8,
1165 dc_sign: &mut self.dc_sign_chroma,
1166 tx_type: None,
1167 eob_pt_class1: None,
1168 },
1169 TxbSet::Chroma4 => TxbTables {
1170 side: 4,
1171 txb_skip: &mut self.txb_skip_chroma_4,
1172 eob_pt: &mut self.eob_pt_16_chroma,
1173 eob_extra: &mut self.eob_extra_chroma_4,
1174 base: &mut self.base_chroma_4,
1175 base_eob: &mut self.base_eob_chroma_4,
1176 br: &mut self.br_chroma_4,
1177 dc_sign: &mut self.dc_sign_chroma,
1178 tx_type: None,
1179 eob_pt_class1: None,
1180 },
1181 TxbSet::Chroma16 => TxbTables {
1182 side: 16,
1183 txb_skip: &mut self.txb_skip_chroma_16,
1184 eob_pt: &mut self.eob_pt_256_chroma,
1185 eob_extra: &mut self.eob_extra_chroma_16,
1186 base: &mut self.base_chroma_16,
1187 base_eob: &mut self.base_eob_chroma_16,
1188 br: &mut self.br_chroma_16,
1189 dc_sign: &mut self.dc_sign_chroma,
1190 tx_type: None,
1191 eob_pt_class1: None,
1192 },
1193 TxbSet::Chroma32 => TxbTables {
1194 side: 32,
1195 txb_skip: &mut self.txb_skip_chroma_32,
1196 eob_pt: &mut self.eob_pt_1024_chroma,
1197 eob_extra: &mut self.eob_extra_chroma_32,
1198 base: &mut self.base_chroma_32,
1199 base_eob: &mut self.base_eob_chroma_32,
1200 br: &mut self.br_chroma_32,
1201 dc_sign: &mut self.dc_sign_chroma,
1202 tx_type: None,
1203 eob_pt_class1: None,
1204 },
1205 }
1206 }
1207}
1208
1209#[cfg(test)]
1210mod tests {
1211 use super::*;
1212
1213 /// A 64x64 luma transform reads the 32x32 base-range rows, so the two sets
1214 /// have to adapt the same table: a decoder keeps one copy, and an encoder
1215 /// that kept two would drift away from it the first time a level above two
1216 /// was coded.
1217 #[test]
1218 fn the_two_luma_sets_share_the_base_range_table() {
1219 let mut cdfs = Cdfs::new(2);
1220 cdfs.txb(TxbSet::Luma64, 0).br[0][0] = 1;
1221 assert_eq!(cdfs.txb(TxbSet::Luma32, 0).br[0][0], 1);
1222 }
1223
1224 /// Both luma sets read one DC sign table, and both chroma sets another;
1225 /// what a luma block adapts must not reach the chroma one.
1226 #[test]
1227 fn the_sign_tables_are_shared_within_a_plane_type_and_not_across() {
1228 let mut cdfs = Cdfs::new(2);
1229 cdfs.txb(TxbSet::Luma32, 0).dc_sign[1][0] = 1;
1230 cdfs.txb(TxbSet::Chroma16, 0).dc_sign[2][0] = 2;
1231 assert_eq!(cdfs.txb(TxbSet::Luma64, 0).dc_sign[1][0], 1);
1232 assert_eq!(cdfs.txb(TxbSet::Chroma32, 0).dc_sign[2][0], 2);
1233 assert_ne!(cdfs.txb(TxbSet::Chroma32, 0).dc_sign[1][0], 1);
1234 assert_ne!(cdfs.txb(TxbSet::Luma64, 0).dc_sign[2][0], 2);
1235 }
1236
1237 /// The end-of-block table a 1024-position transform reads is shared by both
1238 /// luma sets and separate from chroma's, which is the same hazard one table
1239 /// further along.
1240 #[test]
1241 fn the_luma_end_of_block_table_is_shared_and_chromas_is_not() {
1242 let mut cdfs = Cdfs::new(2);
1243 cdfs.txb(TxbSet::Luma32, 0).eob_pt[0] = 7;
1244 assert_eq!(cdfs.txb(TxbSet::Luma64, 0).eob_pt[0], 7);
1245 assert_ne!(cdfs.txb(TxbSet::Chroma32, 0).eob_pt[0], 7);
1246 }
1247
1248 /// Every set starts from the spec's defaults, so a tile that adapts nothing
1249 /// writes exactly what a non-adapting one does.
1250 #[test]
1251 fn the_state_starts_at_the_defaults() {
1252 let mut cdfs = Cdfs::new(2);
1253 assert_eq!(
1254 cdfs.txb(TxbSet::Luma32, 0).base[3],
1255 cdf::COEFF_BASE_LUMA_32[3]
1256 );
1257 assert_eq!(
1258 cdfs.txb(TxbSet::Chroma16, 0).eob_extra[2],
1259 cdf::EOB_EXTRA_CHROMA_16[2]
1260 );
1261 assert_eq!(cdfs.partition_w64[2], cdf::PARTITION_W64[2]);
1262 assert_eq!(cdfs.intra_inter, cdf::INTRA_INTER);
1263 assert_eq!(cdfs.single_ref, cdf::SINGLE_REF);
1264 assert_eq!(cdfs.comp_mode, cdf::COMP_MODE);
1265 assert_eq!(cdfs.inter_compound_mode, cdf::INTER_COMPOUND_MODE);
1266 assert_eq!(cdfs.comp_ref_type, cdf::COMP_REF_TYPE);
1267 assert_eq!(cdfs.uni_comp_ref, cdf::UNI_COMP_REF);
1268 assert_eq!(cdfs.comp_ref, cdf::COMP_REF);
1269 assert_eq!(cdfs.comp_bwdref, cdf::COMP_BWDREF);
1270 assert_eq!(cdfs.y_mode, cdf::Y_MODE);
1271 assert_eq!(cdfs.new_mv, cdf::NEW_MV);
1272 assert_eq!(cdfs.zero_mv, cdf::ZERO_MV);
1273 assert_eq!(cdfs.ref_mv, cdf::REF_MV);
1274 assert_eq!(cdfs.drl_mode, cdf::DRL_MODE);
1275 assert_eq!(cdfs.mv_joint, cdf::MV_JOINT);
1276 assert_eq!(cdfs.mv_comp[0].class, cdf::MV_CLASS);
1277 assert_eq!(cdfs.mv_comp[1].sign, cdf::MV_SIGN);
1278 }
1279
1280 /// A motion vector's two components are separate owned tables: adapting
1281 /// one component's CDFs must not be visible through the other, the same
1282 /// hazard the coefficient sets guard against above.
1283 #[test]
1284 fn the_mv_components_are_separate_and_do_not_share_adaptation() {
1285 let mut cdfs = Cdfs::new(2);
1286 cdfs.mv_comp[0].class[0] = 1;
1287 cdfs.mv_comp[0].bit[3][0] = 2;
1288 assert_ne!(cdfs.mv_comp[1].class[0], 1);
1289 assert_ne!(cdfs.mv_comp[1].bit[3][0], 2);
1290 assert_eq!(cdfs.mv_comp[1].class[0], cdf::MV_CLASS[0]);
1291 assert_eq!(cdfs.mv_comp[1].bit[3][0], cdf::MV_BIT[3][0]);
1292 }
1293}