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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! The values of the syntax elements the encoder writes: square block
//! and transform sizes, and intra prediction modes.
use super::transform::TxType;
/// A square block or transform of `2^log2` samples a side, from 4×4
/// (`log2` 2) to 64×64 (`log2` 6).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct Square {
log2: u32,
}
impl Square {
/// The 4×4 square.
pub(crate) const S4: Self = Self { log2: 2 };
/// The 8×8 square.
pub(crate) const S8: Self = Self { log2: 3 };
/// The 32×32 square.
pub(crate) const S32: Self = Self { log2: 5 };
/// The 64×64 square, the superblock.
pub(crate) const S64: Self = Self { log2: 6 };
/// The base-2 logarithm of the side in samples.
pub(crate) const fn log2(self) -> u32 {
self.log2
}
/// The side in samples.
pub(crate) const fn samples(self) -> usize {
1 << self.log2
}
/// The side in 4×4 units (`Num_4x4_Blocks_Wide`).
pub(crate) const fn units(self) -> usize {
1 << (self.log2 - 2)
}
/// `Mi_Width_Log2`: the base-2 logarithm of the side in 4×4 units.
pub(crate) const fn units_log2(self) -> u32 {
self.log2 - 2
}
/// The square transform size index (`TX_4X4` 0 to `TX_64X64` 4),
/// which is also its `txSzCtx`.
pub(crate) const fn tx_index(self) -> usize {
(self.log2 - 2) as usize
}
/// The four squares a split makes.
pub(crate) const fn half(self) -> Self {
Self {
log2: self.log2 - 1,
}
}
/// The square `ss` halvings smaller (the chroma of a block).
pub(crate) const fn subsampled(self, ss: u32) -> Self {
Self {
log2: self.log2 - ss,
}
}
/// The largest transform of the block: the block itself up to 64×64,
/// and the 32×32 transforms of 64×64 chroma blocks (`get_tx_size`).
pub(crate) fn transform(self, chroma: bool) -> Self {
Self {
log2: self.log2.min(if chroma { 5 } else { 6 }),
}
}
}
/// The intra prediction modes, with their values in the specification.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Mode {
/// `DC_PRED`: the average of the edges.
Dc = 0,
/// `V_PRED`: the row above, copied down (90°).
Vertical = 1,
/// `H_PRED`: the column on the left, copied across (180°).
Horizontal = 2,
/// `D45_PRED`: from the top right.
D45 = 3,
/// `D135_PRED`: from the top left.
D135 = 4,
/// `D113_PRED`.
D113 = 5,
/// `D157_PRED`.
D157 = 6,
/// `D203_PRED`: from the bottom left.
D203 = 7,
/// `D67_PRED`.
D67 = 8,
/// `SMOOTH_PRED`: both edges, blended quadratically.
Smooth = 9,
/// `SMOOTH_V_PRED`.
SmoothVertical = 10,
/// `SMOOTH_H_PRED`.
SmoothHorizontal = 11,
/// `PAETH_PRED`: whichever of above, left and corner is nearest the
/// gradient's prediction.
Paeth = 12,
}
impl Mode {
/// Every mode the encoder uses (all but chroma from luma).
pub(crate) const ALL: [Self; 13] = [
Self::Dc,
Self::Vertical,
Self::Horizontal,
Self::D45,
Self::D135,
Self::D113,
Self::D157,
Self::D203,
Self::D67,
Self::Smooth,
Self::SmoothVertical,
Self::SmoothHorizontal,
Self::Paeth,
];
/// The mode's value, the symbol of `intra_frame_y_mode` and `uv_mode`.
pub(crate) const fn index(self) -> usize {
self as usize
}
/// The context the mode gives the luma mode of the blocks below and
/// to the right (`Intra_Mode_Context`).
pub(crate) const fn context(self) -> usize {
match self {
Self::Dc | Self::Smooth | Self::Paeth => 0,
Self::Vertical | Self::SmoothVertical => 1,
Self::Horizontal | Self::SmoothHorizontal => 2,
Self::D45 | Self::D67 => 3,
Self::D135 | Self::D113 | Self::D157 | Self::D203 => 4,
}
}
/// The nominal angle of a directional mode, in degrees
/// (`Mode_To_Angle`); `None` for the others.
pub(crate) const fn angle(self) -> Option<i32> {
match self {
Self::Vertical => Some(90),
Self::Horizontal => Some(180),
Self::D45 => Some(45),
Self::D135 => Some(135),
Self::D113 => Some(113),
Self::D157 => Some(157),
Self::D203 => Some(203),
Self::D67 => Some(67),
Self::Dc
| Self::Smooth
| Self::SmoothVertical
| Self::SmoothHorizontal
| Self::Paeth => None,
}
}
/// The transform type of chroma blocks predicted in this mode
/// (`Mode_To_Txfm`).
pub(crate) const fn chroma_tx_type(self) -> TxType {
match self {
Self::Dc | Self::D45 => TxType::DctDct,
Self::Vertical | Self::SmoothVertical | Self::D113 | Self::D67 => TxType::AdstDct,
Self::Horizontal | Self::SmoothHorizontal | Self::D157 | Self::D203 => TxType::DctAdst,
Self::Smooth | Self::Paeth | Self::D135 => TxType::AdstAdst,
}
}
}
/// A prediction: a mode and, for directional modes, its angle delta in
/// steps of 3° (`AngleDeltaY`, `AngleDeltaUV`, from -3 to 3).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Prediction {
/// The mode.
pub(crate) mode: Mode,
/// The angle delta, 0 for modes that are not directional.
pub(crate) delta: i32,
}
impl Prediction {
/// `MAX_ANGLE_DELTA`.
pub(crate) const MAX_DELTA: i32 = 3;
/// `mode` without an angle delta.
pub(crate) const fn plain(mode: Mode) -> Self {
Self { mode, delta: 0 }
}
/// The predictions of `mode`: with each angle delta for a
/// directional mode, else the mode alone.
pub(crate) fn with_deltas(mode: Mode) -> impl Iterator<Item = Self> {
let deltas = if mode.angle().is_some() {
-Self::MAX_DELTA..=Self::MAX_DELTA
} else {
0..=0
};
deltas.map(move |delta| Self { mode, delta })
}
/// The angle of a directional prediction, in degrees (`pAngle`).
pub(crate) fn angle(self) -> Option<i32> {
self.mode.angle().map(|angle| angle + 3 * self.delta)
}
}