]>
Commit | Line | Data |
---|---|---|
1 | use nihav_core::codecs::DecoderResult; | |
2 | use nihav_core::frame::{FrameType, NAVideoBufferRef, NATimeInfo}; | |
3 | use nihav_core::refs::*; | |
4 | use nihav_codec_support::codecs::MV; | |
5 | use super::sets::SeqParameterSet; | |
6 | use super::slice::*; | |
7 | use super::types::*; | |
8 | ||
9 | #[derive(Clone)] | |
10 | pub struct PictureInfo { | |
11 | pub id: u16, | |
12 | pub full_id: u32, | |
13 | pub time: NATimeInfo, | |
14 | pub user_id: u32, | |
15 | pub pic_type: FrameType, | |
16 | pub buf: NAVideoBufferRef<u8>, | |
17 | pub cur_mb: usize, | |
18 | pub is_ref: bool, | |
19 | pub is_idr: bool, | |
20 | pub long_term: Option<usize>, | |
21 | ||
22 | pub mv_info: NABufferRef<FrameMV>, | |
23 | } | |
24 | ||
25 | #[derive(Clone,Copy,Default, Debug)] | |
26 | pub struct FrameMBInfo { | |
27 | pub mb_type: CompactMBType, | |
28 | pub ref_poc: [[u16; 2]; 4], | |
29 | pub ref_idx: [[PicRef; 2]; 4], | |
30 | pub mv: [[MV; 2]; 16], | |
31 | } | |
32 | ||
33 | impl FrameMBInfo { | |
34 | pub fn new() -> Self { Self::default() } | |
35 | } | |
36 | ||
37 | #[derive(Clone)] | |
38 | pub struct FrameMV { | |
39 | pub mbs: Vec<FrameMBInfo>, | |
40 | pub mb_stride: usize, | |
41 | } | |
42 | ||
43 | impl FrameMV { | |
44 | pub fn new(mb_w: usize, mb_h: usize) -> Self { | |
45 | Self { | |
46 | mbs: vec![FrameMBInfo::default(); mb_w * mb_h], | |
47 | mb_stride: mb_w, | |
48 | } | |
49 | } | |
50 | } | |
51 | ||
52 | #[derive(Clone)] | |
53 | pub struct SliceRefs { | |
54 | pub ref_list0: Vec<Option<PictureInfo>>, | |
55 | pub ref_list1: Vec<Option<PictureInfo>>, | |
56 | pub cur_id: u32, | |
57 | } | |
58 | ||
59 | impl SliceRefs { | |
60 | pub fn get_ref_id(&self, list_id: u8, ref_id: usize) -> Option<u32> { | |
61 | let ref_list = if list_id == 0 { &self.ref_list0 } else { &self.ref_list1 }; | |
62 | if ref_list.len() > ref_id { | |
63 | ref_list[ref_id].as_ref().map(|pic| pic.full_id) | |
64 | } else { | |
65 | None | |
66 | } | |
67 | } | |
68 | pub fn select_ref_pic(&self, list_id: u8, ref_id: usize) -> Option<NAVideoBufferRef<u8>> { | |
69 | let ref_list = if list_id == 0 { &self.ref_list0 } else { &self.ref_list1 }; | |
70 | if ref_list.len() > ref_id { | |
71 | ref_list[ref_id].as_ref().map(|pic| pic.buf.clone()) | |
72 | } else { | |
73 | None | |
74 | } | |
75 | } | |
76 | pub fn get_colocated_info(&self, mb_x: usize, mb_y: usize) -> (FrameMBInfo, u16, bool) { | |
77 | if let Some(ref ref_pic) = &self.ref_list1[0] { | |
78 | let mv_info = &ref_pic.mv_info; | |
79 | let mb = mv_info.mbs[mb_x + mb_y * mv_info.mb_stride]; | |
80 | (mb, ref_pic.full_id as u16, ref_pic.long_term.is_some()) | |
81 | } else { | |
82 | (FrameMBInfo::default(), 0, false) | |
83 | } | |
84 | } | |
85 | pub fn map_ref0(&self, ref0_id: u16) -> (PicRef, bool) { | |
86 | let mut r0_idx = 0; | |
87 | let mut long = false; | |
88 | for (i, rpic0) in self.ref_list0.iter().enumerate() { | |
89 | if let Some(ref pic) = rpic0 { | |
90 | if (pic.full_id as u16) == ref0_id { | |
91 | r0_idx = i as u8; | |
92 | long = pic.long_term.is_some(); | |
93 | break; | |
94 | } | |
95 | } | |
96 | } | |
97 | (PicRef::new(r0_idx), long) | |
98 | } | |
99 | pub fn map_refs(&self, ref_idx: [PicRef; 2]) -> [u16; 2] { | |
100 | let r0 = ref_idx[0].index(); | |
101 | let r1 = ref_idx[1].index(); | |
102 | let ref0 = if r0 < self.ref_list0.len() { | |
103 | if let Some(ref pic) = self.ref_list0[r0] { | |
104 | pic.full_id as u16 | |
105 | } else { | |
106 | MISSING_POC | |
107 | } | |
108 | } else { | |
109 | MISSING_POC | |
110 | }; | |
111 | let ref1 = if r1 < self.ref_list1.len() { | |
112 | if let Some(ref pic) = self.ref_list1[r1] { | |
113 | pic.full_id as u16 | |
114 | } else { | |
115 | MISSING_POC | |
116 | } | |
117 | } else { | |
118 | MISSING_POC | |
119 | }; | |
120 | [ref0, ref1] | |
121 | } | |
122 | pub fn cmp_refs(&self, ref1: [PicRef; 2], ref2: [PicRef; 2]) -> bool { | |
123 | if ref1 != ref2 { | |
124 | self.cmp_ref(ref1[0], ref2[0], 0) && self.cmp_ref(ref1[1], ref2[1], 1) | |
125 | } else { | |
126 | true | |
127 | } | |
128 | } | |
129 | fn cmp_ref(&self, ref1: PicRef, ref2: PicRef, list: u8) -> bool { | |
130 | if ref1 == ref2 { | |
131 | true | |
132 | } else { | |
133 | let idx0 = ref1.index(); | |
134 | let idx1 = ref2.index(); | |
135 | if idx0 == idx1 { | |
136 | return true; | |
137 | } | |
138 | let src = if list == 0 { &self.ref_list0 } else { &self.ref_list1 }; | |
139 | if idx0 >= src.len() || idx1 >= src.len() { | |
140 | //panic!("wrong refs"); | |
141 | return false; | |
142 | } | |
143 | if let (Some(ref pic0), Some(ref pic1)) = (&src[idx0], &src[idx1]) { | |
144 | pic0.full_id == pic1.full_id | |
145 | } else { | |
146 | //panic!("missing pics"); | |
147 | false | |
148 | } | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
153 | pub struct FrameRefs { | |
154 | pub ref_pics: Vec<PictureInfo>, | |
155 | pub cur_refs: SliceRefs, | |
156 | pub long_term: Vec<Option<PictureInfo>>, | |
157 | ||
158 | prev_poc_msb: u32, | |
159 | prev_poc_lsb: u16, | |
160 | prev_ref_poc_lsb: u16, | |
161 | prev_frame_num: u16, | |
162 | frame_num_offset: u32, | |
163 | max_frame_num: i32, | |
164 | } | |
165 | ||
166 | impl FrameRefs { | |
167 | pub fn new() -> Self { | |
168 | Self { | |
169 | ref_pics: Vec::with_capacity(16), | |
170 | cur_refs: SliceRefs { | |
171 | ref_list0: Vec::with_capacity(3), | |
172 | ref_list1: Vec::with_capacity(3), | |
173 | cur_id: 0, | |
174 | }, | |
175 | long_term: Vec::new(), | |
176 | ||
177 | prev_poc_msb: 0, | |
178 | prev_poc_lsb: 0, | |
179 | prev_ref_poc_lsb: 0, | |
180 | prev_frame_num: 0, | |
181 | frame_num_offset: 0, | |
182 | max_frame_num: 0, | |
183 | } | |
184 | } | |
185 | pub fn fill_ref_nums(&self, dst: &mut Vec<u32>) { | |
186 | for pic in self.ref_pics.iter() { | |
187 | if !dst.contains(&pic.full_id) { | |
188 | dst.push(pic.full_id); | |
189 | } | |
190 | } | |
191 | for pic in self.long_term.iter().flatten() { | |
192 | if !dst.contains(&pic.full_id) { | |
193 | dst.push(pic.full_id); | |
194 | } | |
195 | } | |
196 | } | |
197 | pub fn calc_picture_num(&mut self, slice_hdr: &SliceHeader, is_idr: bool, ref_id: u8, sps: &SeqParameterSet) -> u32 { | |
198 | self.max_frame_num = 1 << sps.log2_max_frame_num; | |
199 | match sps.pic_order_cnt_type { | |
200 | 0 => { | |
201 | if is_idr { | |
202 | //self.prev_poc_msb = 0; | |
203 | self.prev_poc_lsb = 0; | |
204 | } else { | |
205 | self.prev_poc_lsb = self.prev_ref_poc_lsb; | |
206 | } | |
207 | let max_poc_lsb = 1 << sps.log2_max_pic_order_cnt_lsb; | |
208 | let half_max_poc_lsb = 1 << (sps.log2_max_pic_order_cnt_lsb - 1); | |
209 | let cur_lsb = slice_hdr.pic_order_cnt_lsb; | |
210 | let poc_msb = if cur_lsb < self.prev_poc_lsb && (self.prev_poc_lsb - cur_lsb >= half_max_poc_lsb) { | |
211 | self.prev_poc_msb + max_poc_lsb | |
212 | } else if cur_lsb > self.prev_poc_lsb && (cur_lsb - self.prev_poc_lsb > half_max_poc_lsb) { | |
213 | self.prev_poc_msb.wrapping_sub(max_poc_lsb) | |
214 | } else { | |
215 | self.prev_poc_msb | |
216 | }; | |
217 | let poc = poc_msb + u32::from(cur_lsb); | |
218 | if ref_id != 0 { | |
219 | self.prev_ref_poc_lsb = slice_hdr.pic_order_cnt_lsb; | |
220 | self.prev_poc_msb = poc_msb; | |
221 | } | |
222 | poc | |
223 | }, | |
224 | 1 => { | |
225 | let off = if self.prev_frame_num > slice_hdr.frame_num { | |
226 | self.frame_num_offset + (1 << sps.log2_max_frame_num) | |
227 | } else { | |
228 | self.frame_num_offset | |
229 | }; | |
230 | let mut anum = if sps.num_ref_frames_in_pic_order_cnt_cycle != 0 { | |
231 | (off as i32) + i32::from(slice_hdr.frame_num) | |
232 | } else { | |
233 | 0 | |
234 | }; | |
235 | if ref_id == 0 && anum > 0 { | |
236 | anum -= 1; | |
237 | } | |
238 | let (poc_cycle_cnt, fno_in_poc_cycle) = if anum > 0 { | |
239 | let nrf = sps.num_ref_frames_in_pic_order_cnt_cycle as i32; | |
240 | ((anum - 1) / nrf, (anum - 1) % nrf) | |
241 | } else { | |
242 | (0, 0) | |
243 | }; | |
244 | let mut expected_delta = 0; | |
245 | for &offset in sps.offset_for_ref_frame[..sps.num_ref_frames_in_pic_order_cnt_cycle].iter() { | |
246 | expected_delta += offset; | |
247 | } | |
248 | let mut expected_poc = if anum > 0 { | |
249 | let mut sum = poc_cycle_cnt * expected_delta; | |
250 | for &offset in sps.offset_for_ref_frame[..=fno_in_poc_cycle as usize].iter() { | |
251 | sum += offset; | |
252 | } | |
253 | sum | |
254 | } else { | |
255 | 0 | |
256 | }; | |
257 | if ref_id == 0 { | |
258 | expected_poc += sps.offset_for_non_ref_pic; | |
259 | } | |
260 | let (top_id, _bottom_id) = if !slice_hdr.field_pic { | |
261 | let top_id = expected_poc + slice_hdr.delta_pic_order_cnt[0]; | |
262 | let bot_id = top_id + sps.offset_for_top_to_bottom_field + slice_hdr.delta_pic_order_cnt[1]; | |
263 | (top_id, bot_id) | |
264 | } else if !slice_hdr.bottom_field { | |
265 | (expected_poc + slice_hdr.delta_pic_order_cnt[0], 0) | |
266 | } else { | |
267 | (0, sps.offset_for_top_to_bottom_field + slice_hdr.delta_pic_order_cnt[1]) | |
268 | }; | |
269 | self.prev_frame_num = slice_hdr.frame_num; | |
270 | self.frame_num_offset = off; | |
271 | top_id as u32 | |
272 | }, | |
273 | _ => { | |
274 | if slice_hdr.frame_num < self.prev_frame_num { | |
275 | self.frame_num_offset += 1 << sps.log2_max_frame_num; | |
276 | } | |
277 | self.prev_frame_num = slice_hdr.frame_num; | |
278 | self.frame_num_offset + u32::from(slice_hdr.frame_num) | |
279 | }, | |
280 | } | |
281 | } | |
282 | pub fn apply_adaptive_marking(&mut self, marking: &AdaptiveMarking, cur_id: u16, max_id: u16) -> DecoderResult<()> { | |
283 | let all_ref_pics = self.ref_pics.clone(); | |
284 | ||
285 | for (&op, (&arg1, &arg2)) in marking.memory_management_control_op.iter().zip(marking.operation_arg.iter().zip(marking.operation_arg2.iter())).take(marking.num_ops) { | |
286 | match op { | |
287 | 1 => { | |
288 | let src_id = cur_id.wrapping_sub(arg1) & (max_id - 1); | |
289 | let mut found = false; | |
290 | let mut idx = 0; | |
291 | for (i, pic) in self.ref_pics.iter().enumerate() { | |
292 | if pic.id == src_id { | |
293 | found = true; | |
294 | idx = i; | |
295 | break; | |
296 | } | |
297 | } | |
298 | if found { | |
299 | self.ref_pics.remove(idx); | |
300 | } | |
301 | }, | |
302 | 2 => { // mark long term picture as unused | |
303 | let idx = arg1 as usize; | |
304 | if idx < self.long_term.len() { | |
305 | self.long_term[idx] = None; | |
306 | } | |
307 | }, | |
308 | 3 => { | |
309 | let src_id = cur_id.wrapping_sub(arg1) & (max_id - 1); | |
310 | ||
311 | let didx = arg2 as usize; | |
312 | for pic in all_ref_pics.iter() { | |
313 | if pic.id == src_id { | |
314 | if didx < self.long_term.len() { | |
315 | self.long_term[didx] = Some(pic.clone()); | |
316 | } | |
317 | break; | |
318 | } | |
319 | } | |
320 | }, | |
321 | 4 => { | |
322 | self.long_term.resize(arg1 as usize, None); | |
323 | }, | |
324 | 5 => { | |
325 | self.ref_pics.clear(); | |
326 | self.long_term.clear(); | |
327 | }, | |
328 | 6 => { | |
329 | // assign an long term index to current pic - done elsewhere | |
330 | }, | |
331 | _ => {}, | |
332 | }; | |
333 | } | |
334 | Ok(()) | |
335 | } | |
336 | pub fn clear_refs(&mut self) { | |
337 | self.ref_pics.clear(); | |
338 | self.long_term.clear(); | |
339 | } | |
340 | #[allow(clippy::cognitive_complexity)] | |
341 | pub fn select_refs(&mut self, sps: &SeqParameterSet, slice_hdr: &SliceHeader, cur_id: u32) { | |
342 | self.cur_refs.cur_id = cur_id; | |
343 | self.cur_refs.ref_list0.clear(); | |
344 | self.cur_refs.ref_list1.clear(); | |
345 | let pic_num_mask = if sps.log2_max_frame_num == 16 { | |
346 | 0xFFFF | |
347 | } else { | |
348 | (1 << sps.log2_max_frame_num) - 1 | |
349 | }; | |
350 | ||
351 | if !slice_hdr.slice_type.is_intra() { | |
352 | let has_reordering = slice_hdr.ref_pic_list_reordering_l0; | |
353 | if !has_reordering { | |
354 | let num_ref = slice_hdr.num_ref_idx_l0_active; | |
355 | if slice_hdr.slice_type.is_p() { | |
356 | if !self.ref_pics.is_empty() { | |
357 | for pic in self.ref_pics.iter().rev().take(num_ref) { | |
358 | self.cur_refs.ref_list0.push(Some(pic.clone())); | |
359 | } | |
360 | } | |
361 | } else { | |
362 | let mut pivot = 0; | |
363 | for (i, pic) in self.ref_pics.iter().enumerate() { | |
364 | pivot = i; | |
365 | if pic.full_id > cur_id { | |
366 | break; | |
367 | } | |
368 | } | |
369 | for pic in self.ref_pics[..pivot].iter().rev() { | |
370 | if self.cur_refs.ref_list0.len() >= num_ref { | |
371 | break; | |
372 | } | |
373 | self.cur_refs.ref_list0.push(Some(pic.clone())); | |
374 | } | |
375 | for pic in self.ref_pics.iter().skip(pivot) { | |
376 | if self.cur_refs.ref_list0.len() >= num_ref { | |
377 | break; | |
378 | } | |
379 | self.cur_refs.ref_list0.push(Some(pic.clone())); | |
380 | } | |
381 | } | |
382 | if !self.long_term.is_empty() && self.cur_refs.ref_list0.len() < num_ref { | |
383 | let copy_size = num_ref - self.cur_refs.ref_list0.len(); | |
384 | for ltpic in self.long_term.iter().take(copy_size) { | |
385 | self.cur_refs.ref_list0.push(ltpic.clone()); | |
386 | } | |
387 | } | |
388 | } else { | |
389 | form_ref_list(&mut self.cur_refs.ref_list0, | |
390 | &self.ref_pics, &self.long_term, | |
391 | &slice_hdr.reordering_list_l0, | |
392 | slice_hdr.frame_num, pic_num_mask); | |
393 | } | |
394 | if slice_hdr.slice_type.is_b() { | |
395 | let has_reordering = slice_hdr.ref_pic_list_reordering_l1; | |
396 | if !has_reordering { | |
397 | let num_ref = slice_hdr.num_ref_idx_l1_active; | |
398 | let mut pivot = 0; | |
399 | for (i, pic) in self.ref_pics.iter().enumerate() { | |
400 | pivot = i; | |
401 | if pic.full_id > cur_id { | |
402 | break; | |
403 | } | |
404 | } | |
405 | for pic in self.ref_pics.iter().skip(pivot) { | |
406 | if self.cur_refs.ref_list1.len() >= num_ref { | |
407 | break; | |
408 | } | |
409 | self.cur_refs.ref_list1.push(Some(pic.clone())); | |
410 | } | |
411 | for pic in self.ref_pics[..pivot].iter().rev() { | |
412 | if self.cur_refs.ref_list1.len() >= num_ref { | |
413 | break; | |
414 | } | |
415 | self.cur_refs.ref_list1.push(Some(pic.clone())); | |
416 | } | |
417 | if !self.long_term.is_empty() && self.cur_refs.ref_list1.len() < num_ref { | |
418 | let copy_size = num_ref - self.cur_refs.ref_list1.len(); | |
419 | for ltpic in self.long_term.iter().take(copy_size) { | |
420 | self.cur_refs.ref_list1.push(ltpic.clone()); | |
421 | } | |
422 | } | |
423 | if self.cur_refs.ref_list1.len() > 1 && self.cur_refs.ref_list0.len() == self.cur_refs.ref_list1.len() { | |
424 | let mut equal = true; | |
425 | for (pic1, pic2) in self.cur_refs.ref_list0.iter().zip(self.cur_refs.ref_list1.iter()) { | |
426 | match (pic1, pic2) { | |
427 | (Some(p1), Some(p2)) => { | |
428 | if p1.full_id != p2.full_id { | |
429 | equal = false; | |
430 | break; | |
431 | } | |
432 | }, | |
433 | (None, None) => {}, | |
434 | _ => { | |
435 | equal = false; | |
436 | break; | |
437 | }, | |
438 | }; | |
439 | } | |
440 | if equal { | |
441 | self.cur_refs.ref_list1.swap(0, 1); | |
442 | } | |
443 | } | |
444 | } else { | |
445 | form_ref_list(&mut self.cur_refs.ref_list1, | |
446 | &self.ref_pics, &self.long_term, | |
447 | &slice_hdr.reordering_list_l1, | |
448 | slice_hdr.frame_num, pic_num_mask); | |
449 | } | |
450 | } | |
451 | } | |
452 | } | |
453 | pub fn add_short_term(&mut self, cpic: PictureInfo, num_ref_frames: usize) { | |
454 | if !self.ref_pics.is_empty() && self.ref_pics.len() >= num_ref_frames { | |
455 | let base_id = i32::from(cpic.id); | |
456 | let mut min_id = base_id; | |
457 | let mut min_idx = 0; | |
458 | for (i, pic) in self.ref_pics.iter().enumerate() { | |
459 | let mut pic_id = i32::from(pic.id); | |
460 | if pic_id > base_id { | |
461 | pic_id -= self.max_frame_num; | |
462 | } | |
463 | if pic_id < min_id { | |
464 | min_id = pic_id; | |
465 | min_idx = i; | |
466 | } | |
467 | } | |
468 | self.ref_pics.remove(min_idx); | |
469 | } | |
470 | if self.ref_pics.is_empty() || self.ref_pics.last().unwrap().full_id < cpic.full_id { | |
471 | self.ref_pics.push(cpic); | |
472 | } else { | |
473 | let mut idx = 0; | |
474 | for (i, pic) in self.ref_pics.iter().enumerate() { | |
475 | if pic.full_id < cpic.full_id { | |
476 | idx = i; | |
477 | } else { | |
478 | break; | |
479 | } | |
480 | } | |
481 | self.ref_pics.insert(idx + 1, cpic); | |
482 | } | |
483 | } | |
484 | pub fn add_long_term(&mut self, lt_idx: usize, cpic: PictureInfo) { | |
485 | if lt_idx < self.long_term.len() { | |
486 | self.long_term[lt_idx] = Some(cpic); | |
487 | } | |
488 | } | |
489 | } | |
490 | ||
491 | fn form_ref_list(ref_list: &mut Vec<Option<PictureInfo>>, ref_pics: &[PictureInfo], long_term: &[Option<PictureInfo>], reord_info: &ReorderingInfo, cur_id: u16, pic_num_mask: u16) { | |
492 | let mut ref_pic_id = cur_id; | |
493 | for (&op, &num) in reord_info.reordering_of_pic_nums_idc.iter().zip(reord_info.abs_diff_or_num.iter()).take(reord_info.num_ops) { | |
494 | if op < 2 { | |
495 | if op == 0 { | |
496 | ref_pic_id = ref_pic_id.wrapping_sub(num) & pic_num_mask; | |
497 | } else { | |
498 | ref_pic_id = ref_pic_id.wrapping_add(num) & pic_num_mask; | |
499 | } | |
500 | let mut found = false; | |
501 | for pic in ref_pics.iter() { | |
502 | if pic.id == ref_pic_id { | |
503 | ref_list.push(Some(pic.clone())); | |
504 | found = true; | |
505 | break; | |
506 | } | |
507 | } | |
508 | if !found { | |
509 | ref_list.push(None); | |
510 | } | |
511 | } else { | |
512 | let idx = num as usize; | |
513 | if idx < long_term.len() { | |
514 | ref_list.push(long_term[idx].clone()); | |
515 | } else { | |
516 | ref_list.push(None); | |
517 | } | |
518 | } | |
519 | } | |
520 | } |