1use std::sync::Arc;
6
7use crate::decode::jobs::Slot;
8use crate::decode::picture::Plane;
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
11pub enum MbKind {
12 #[default]
13 None,
14 I4x4,
15 I8x8,
16 I16x16,
17 IPcm,
18 P,
19 B,
20 PSkip,
21 BSkip,
22}
23
24impl MbKind {
25 #[inline]
26 pub fn is_intra(self) -> bool {
27 matches!(
28 self,
29 MbKind::I4x4 | MbKind::I8x8 | MbKind::I16x16 | MbKind::IPcm
30 )
31 }
32 #[inline]
33 pub fn is_skip(self) -> bool {
34 matches!(self, MbKind::PSkip | MbKind::BSkip)
35 }
36 #[inline]
38 pub fn is_nxn(self) -> bool {
39 matches!(self, MbKind::I4x4 | MbKind::I8x8)
40 }
41}
42
43#[derive(Clone, Copy, Debug)]
47pub struct MbInfo {
48 pub slice: u32,
50 pub kind: MbKind,
51 pub b_direct16: bool,
53 pub qp: i8,
55 pub qpc: [i8; 2],
57 pub cbp: u8,
59 pub t8x8: bool,
60 pub chroma_mode: u8,
61 pub ipred: [u8; 16],
63 pub nnz: [u8; 16],
65 pub nnz_c: [[u8; 8]; 2],
67 pub cbf_dc: u8,
69 pub nz_mask: u16,
72 pub direct8: u8,
74 pub ref_idx: [[i8; 4]; 2],
75 pub ref_id: [[i32; 4]; 2],
77 pub mv: [[[i16; 2]; 16]; 2],
78 pub mvd: [[[u8; 2]; 16]; 2],
80 pub field: bool,
82}
83
84impl Default for MbInfo {
85 fn default() -> Self {
86 MbInfo {
87 slice: u32::MAX,
88 kind: MbKind::None,
89 b_direct16: false,
90 qp: 0,
91 qpc: [0; 2],
92 cbp: 0,
93 t8x8: false,
94 chroma_mode: 0,
95 ipred: [2; 16],
96 nnz: [0; 16],
97 nnz_c: [[0; 8]; 2],
98 cbf_dc: 0,
99 nz_mask: 0,
100 direct8: 0,
101 ref_idx: [[-1; 4]; 2],
102 ref_id: [[-1; 4]; 2],
103 mv: [[[0; 2]; 16]; 2],
104 mvd: [[[0; 2]; 16]; 2],
105 field: false,
106 }
107 }
108}
109
110pub struct MbSyn {
112 pub kind: MbKind,
113 pub mb_type: u32,
115 pub i16_mode: u8,
116 pub prev_flag: [bool; 16],
117 pub rem: [u8; 16],
118 pub chroma_mode: u8,
119 pub sub: [u8; 4],
120 pub ref_idx: [[i8; 4]; 2],
121 pub mvd: [[[i32; 2]; 16]; 2],
123 pub cbp: u8,
124 pub t8x8: bool,
125 pub qp_delta: i32,
126 pub pcm: Vec<u16>,
127 pub coef: [[i32; 16]; 16],
129 pub coef8: [[i32; 64]; 4],
131 pub dc_luma: [i32; 16],
133 pub dc_c: [[i32; 8]; 2],
134 pub ac_c: [[[i32; 16]; 8]; 2],
136 pub coded: u16,
138 pub coded8: u8,
140 pub coded_c: [u8; 2],
141}
142
143impl MbSyn {
144 pub fn new() -> Self {
145 MbSyn {
146 kind: MbKind::None,
147 mb_type: 0,
148 i16_mode: 0,
149 prev_flag: [false; 16],
150 rem: [0; 16],
151 chroma_mode: 0,
152 sub: [0; 4],
153 ref_idx: [[0; 4]; 2],
154 mvd: [[[0; 2]; 16]; 2],
155 cbp: 0,
156 t8x8: false,
157 qp_delta: 0,
158 pcm: vec![0; 512],
159 coef: [[0; 16]; 16],
160 coef8: [[0; 64]; 4],
161 dc_luma: [0; 16],
162 dc_c: [[0; 8]; 2],
163 ac_c: [[[0; 16]; 8]; 2],
164 coded: 0,
165 coded8: 0,
166 coded_c: [0; 2],
167 }
168 }
169
170 pub fn reset(&mut self) {
173 for b in 0..16 {
174 if self.coded & (1 << b) != 0 {
175 self.coef[b] = [0; 16];
176 }
177 }
178 for b in 0..4 {
179 if self.coded8 & (1 << b) != 0 {
180 self.coef8[b] = [0; 64];
181 }
182 }
183 self.dc_luma = [0; 16];
184 self.dc_c = [[0; 8]; 2];
185 for c in 0..2 {
186 if self.coded_c[c] != 0 {
187 self.ac_c[c] = [[0; 16]; 8];
188 }
189 }
190 self.coded = 0;
191 self.coded8 = 0;
192 self.coded_c = [0; 2];
193 self.kind = MbKind::None;
194 self.mb_type = 0;
195 self.i16_mode = 0;
196 self.prev_flag = [false; 16];
197 self.rem = [0; 16];
198 self.chroma_mode = 0;
199 self.sub = [0; 4];
200 self.ref_idx = [[0; 4]; 2];
201 self.mvd = [[[0; 2]; 16]; 2];
202 self.cbp = 0;
203 self.t8x8 = false;
204 self.qp_delta = 0;
205 }
206}
207
208impl Default for MbSyn {
209 fn default() -> Self {
210 Self::new()
211 }
212}
213
214#[derive(Clone, Copy, Debug, PartialEq, Eq)]
216pub enum Shape {
217 S16x16,
218 S16x8,
219 S8x16,
220 S8x8,
221}
222
223impl Shape {
224 pub fn parts(self) -> &'static [(usize, usize, usize, usize)] {
226 match self {
227 Shape::S16x16 => &[(0, 0, 4, 4)],
228 Shape::S16x8 => &[(0, 0, 4, 2), (0, 2, 4, 2)],
229 Shape::S8x16 => &[(0, 0, 2, 4), (2, 0, 2, 4)],
230 Shape::S8x8 => &[(0, 0, 2, 2), (2, 0, 2, 2), (0, 2, 2, 2), (2, 2, 2, 2)],
231 }
232 }
233}
234
235pub type PredFlags = u8;
237
238pub fn p_mb_layout(t: u32) -> (Shape, [PredFlags; 2]) {
240 match t {
241 0 => (Shape::S16x16, [1, 0]),
242 1 => (Shape::S16x8, [1, 1]),
243 2 => (Shape::S8x16, [1, 1]),
244 _ => (Shape::S8x8, [1, 1]),
245 }
246}
247
248pub fn b_mb_layout(t: u32) -> (Shape, [PredFlags; 2]) {
250 const T: [(Shape, [u8; 2]); 21] = [
251 (Shape::S16x16, [1, 0]),
252 (Shape::S16x16, [2, 0]),
253 (Shape::S16x16, [3, 0]),
254 (Shape::S16x8, [1, 1]),
255 (Shape::S8x16, [1, 1]),
256 (Shape::S16x8, [2, 2]),
257 (Shape::S8x16, [2, 2]),
258 (Shape::S16x8, [1, 2]),
259 (Shape::S8x16, [1, 2]),
260 (Shape::S16x8, [2, 1]),
261 (Shape::S8x16, [2, 1]),
262 (Shape::S16x8, [1, 3]),
263 (Shape::S8x16, [1, 3]),
264 (Shape::S16x8, [2, 3]),
265 (Shape::S8x16, [2, 3]),
266 (Shape::S16x8, [3, 1]),
267 (Shape::S8x16, [3, 1]),
268 (Shape::S16x8, [3, 2]),
269 (Shape::S8x16, [3, 2]),
270 (Shape::S16x8, [3, 3]),
271 (Shape::S8x16, [3, 3]),
272 ];
273 if (1..=21).contains(&t) {
274 T[t as usize - 1]
275 } else {
276 (Shape::S8x8, [0, 0])
277 }
278}
279
280pub fn p_sub_layout(t: u8) -> (PredFlags, usize, usize) {
282 match t {
283 0 => (1, 2, 2),
284 1 => (1, 2, 1),
285 2 => (1, 1, 2),
286 _ => (1, 1, 1),
287 }
288}
289
290pub fn b_sub_layout(t: u8) -> (PredFlags, usize, usize) {
292 match t {
293 0 => (0, 2, 2),
294 1 => (1, 2, 2),
295 2 => (2, 2, 2),
296 3 => (3, 2, 2),
297 4 => (1, 2, 1),
298 5 => (1, 1, 2),
299 6 => (2, 2, 1),
300 7 => (2, 1, 2),
301 8 => (3, 2, 1),
302 9 => (3, 1, 2),
303 10 => (1, 1, 1),
304 11 => (2, 1, 1),
305 _ => (3, 1, 1),
306 }
307}
308
309#[derive(Clone)]
312pub struct RefPic {
313 pub id: u32,
314 pub planes: Arc<[Plane; 3]>,
316 pub field_planes: [Option<Arc<[Plane; 3]>>; 2],
318 pub mbs: Arc<Vec<MbInfo>>,
321 pub field_mbs: [Option<Arc<Vec<MbInfo>>>; 2],
323 pub field_poc: [i32; 2],
325 pub poc: i32,
327 pub frame_num: u32,
328 pub frame_num_wrap: i32,
329 pub short: u8,
331 pub long: u8,
333 pub lt_idx: u32,
334 pub(crate) slot: Option<Arc<Slot<RefData>>>,
337}
338
339pub(crate) struct RefData {
341 pub planes: Arc<[Plane; 3]>,
342 pub mbs: Arc<Vec<MbInfo>>,
343}
344
345impl RefPic {
346 pub fn is_ref(&self) -> bool {
347 self.short | self.long != 0
348 }
349
350 pub fn field(&mut self, parity: usize) -> Arc<[Plane; 3]> {
353 if let Some(p) = &self.field_planes[parity] {
354 return p.clone();
355 }
356 let f = |p: &Plane| {
357 let h = p.height / 2;
358 let mut out = Plane::new(p.width, h, 0);
359 for y in 0..h {
360 let src = (2 * y + parity) * p.width;
361 out.data[y * p.width..(y + 1) * p.width]
362 .copy_from_slice(&p.data[src..src + p.width]);
363 }
364 out
365 };
366 let a = Arc::new([f(&self.planes[0]), f(&self.planes[1]), f(&self.planes[2])]);
367 self.field_planes[parity] = Some(a.clone());
368 a
369 }
370}
371
372#[derive(Clone, Debug)]
374pub struct RefEntry {
375 pub dpb: usize,
377 pub id: u32,
380 pub poc: i32,
381 pub long_term: bool,
382 pub structure: u8,
384 pub planes: Arc<[Plane; 3]>,
386}