1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! The CDFs of a tile: copies of the defaults (`init_non_coeff_cdfs` and
//! `init_coeff_cdfs`) that adapt as the tile is coded. Only the syntax
//! elements the encoder writes are kept.
use super::entropy::SymbolSink;
use super::tables::coeff_cdfs::{
COEFF_BASE, COEFF_BASE_EOB, COEFF_BR, DC_SIGN, EOB_EXTRA, EOB_PT_16, EOB_PT_64, EOB_PT_256,
EOB_PT_1024, TXB_SKIP,
};
use super::tables::mode_cdfs::{
ANGLE_DELTA, DELTA_Q, INTRA_FRAME_Y_MODE, INTRA_TX_TYPE_SET2, PARTITION_W8, PARTITION_W16,
PARTITION_W32, PARTITION_W64, SKIP, UV_MODE_CFL_ALLOWED, UV_MODE_CFL_NOT_ALLOWED,
};
/// The CDFs of the colour indices of palettes, by palette size and
/// colour context.
#[derive(Clone, Debug)]
pub(crate) struct ColorCdfs {
two: [[u16; 2]; 5],
three: [[u16; 3]; 5],
four: [[u16; 4]; 5],
five: [[u16; 5]; 5],
six: [[u16; 6]; 5],
seven: [[u16; 7]; 5],
eight: [[u16; 8]; 5],
}
impl ColorCdfs {
/// Codes the rank `symbol` of a colour index of a palette of `size`
/// colours, in context `ctx`.
pub(crate) fn code<S: SymbolSink>(
&mut self,
sink: &mut S,
size: usize,
ctx: usize,
symbol: usize,
) {
match size {
2 => sink.symbol(&mut self.two[ctx], symbol),
3 => sink.symbol(&mut self.three[ctx], symbol),
4 => sink.symbol(&mut self.four[ctx], symbol),
5 => sink.symbol(&mut self.five[ctx], symbol),
6 => sink.symbol(&mut self.six[ctx], symbol),
7 => sink.symbol(&mut self.seven[ctx], symbol),
_ => sink.symbol(&mut self.eight[ctx], symbol),
}
}
}
/// The palette CDFs of a tile.
#[derive(Clone, Debug)]
pub(crate) struct PaletteCdfs {
/// `has_palette_y`, by block size and neighbour context.
pub(crate) y_mode: [[[u16; 2]; 3]; 7],
/// `has_palette_uv`, by whether the block has a luma palette.
pub(crate) uv_mode: [[u16; 2]; 2],
/// `palette_size_y_minus_2`, by block size.
pub(crate) y_size: [[u16; 7]; 7],
/// `palette_size_uv_minus_2`, by block size.
pub(crate) uv_size: [[u16; 7]; 7],
/// `palette_color_idx_y`.
pub(crate) y_color: ColorCdfs,
/// `palette_color_idx_uv`.
pub(crate) uv_color: ColorCdfs,
}
impl PaletteCdfs {
/// The defaults.
fn new() -> Self {
use super::tables::palette_cdfs as p;
Self {
y_mode: p::Y_MODE,
uv_mode: p::UV_MODE,
y_size: p::Y_SIZE,
uv_size: p::UV_SIZE,
y_color: ColorCdfs {
two: p::Y_COLOR_2,
three: p::Y_COLOR_3,
four: p::Y_COLOR_4,
five: p::Y_COLOR_5,
six: p::Y_COLOR_6,
seven: p::Y_COLOR_7,
eight: p::Y_COLOR_8,
},
uv_color: ColorCdfs {
two: p::UV_COLOR_2,
three: p::UV_COLOR_3,
four: p::UV_COLOR_4,
five: p::UV_COLOR_5,
six: p::UV_COLOR_6,
seven: p::UV_COLOR_7,
eight: p::UV_COLOR_8,
},
}
}
}
/// The adaptive CDFs of one tile (the specification's `Tile...Cdf`
/// arrays), indexed as the specification indexes them.
#[derive(Clone, Debug)]
pub(crate) struct Cdfs {
/// `intra_frame_y_mode`, by above and left mode context.
pub(crate) y_mode: [[[u16; 13]; 5]; 5],
/// `uv_mode` where chroma from luma is not allowed, by luma mode.
pub(crate) uv_mode: [[u16; 13]; 13],
/// `uv_mode` where chroma from luma is allowed, by luma mode.
pub(crate) uv_mode_cfl: [[u16; 14]; 13],
/// `angle_delta_y` and `angle_delta_uv`, by directional mode.
pub(crate) angle_delta: [[u16; 7]; 8],
/// `partition` of 8×8 blocks, by context.
pub(crate) partition_8: [[u16; 4]; 4],
/// `partition` of 16×16, 32×32 and 64×64 blocks, by context.
pub(crate) partition: [[[u16; 10]; 4]; 3],
/// `skip`, by context.
pub(crate) skip: [[u16; 2]; 3],
/// `delta_q_abs`.
pub(crate) delta_q: [u16; 4],
/// Palette mode.
pub(crate) palette: PaletteCdfs,
/// `intra_tx_type` of the reduced set, by square size and luma mode.
pub(crate) tx_type: [[[u16; 5]; 13]; 3],
/// `all_zero`, by transform size and context.
pub(crate) txb_skip: [[[u16; 2]; 13]; 5],
/// `eob_pt_16`, by plane type and transform class.
pub(crate) eob_pt_16: [[[u16; 5]; 2]; 2],
/// `eob_pt_64`.
pub(crate) eob_pt_64: [[[u16; 7]; 2]; 2],
/// `eob_pt_256`.
pub(crate) eob_pt_256: [[[u16; 9]; 2]; 2],
/// `eob_pt_1024`, by plane type.
pub(crate) eob_pt_1024: [[u16; 11]; 2],
/// `eob_extra`, by transform size, plane type and end-of-block class.
pub(crate) eob_extra: [[[[u16; 2]; 9]; 2]; 5],
/// `dc_sign`, by plane type and context.
pub(crate) dc_sign: [[[u16; 2]; 3]; 2],
/// `coeff_base_eob`, by transform size, plane type and context.
pub(crate) coeff_base_eob: [[[[u16; 3]; 4]; 2]; 5],
/// `coeff_base`.
pub(crate) coeff_base: [[[[u16; 4]; 42]; 2]; 5],
/// `coeff_br`.
pub(crate) coeff_br: [[[[u16; 4]; 21]; 2]; 5],
}
impl Cdfs {
/// The default CDFs of a frame with base quantizer index `qindex`,
/// whose coefficient CDFs come from one of four quantizer contexts.
pub(crate) fn new(qindex: u8) -> Self {
let q = match qindex {
0..=20 => 0,
21..=60 => 1,
61..=120 => 2,
_ => 3,
};
Self {
y_mode: INTRA_FRAME_Y_MODE,
uv_mode: UV_MODE_CFL_NOT_ALLOWED,
uv_mode_cfl: UV_MODE_CFL_ALLOWED,
angle_delta: ANGLE_DELTA,
partition_8: PARTITION_W8,
partition: [PARTITION_W16, PARTITION_W32, PARTITION_W64],
skip: SKIP,
delta_q: DELTA_Q,
palette: PaletteCdfs::new(),
tx_type: INTRA_TX_TYPE_SET2,
txb_skip: TXB_SKIP[q],
eob_pt_16: EOB_PT_16[q],
eob_pt_64: EOB_PT_64[q],
eob_pt_256: EOB_PT_256[q],
eob_pt_1024: EOB_PT_1024[q],
eob_extra: EOB_EXTRA[q],
dc_sign: DC_SIGN[q],
coeff_base_eob: COEFF_BASE_EOB[q],
coeff_base: COEFF_BASE[q],
coeff_br: COEFF_BR[q],
}
}
}