1 use nihav_core::codecs::DecoderResult;
2 use nihav_core::frame::{FrameType, NAVideoBufferRef};
3 use nihav_codec_support::codecs::MV;
4 use super::sets::SeqParameterSet;
9 pub struct PictureInfo {
12 pub pic_type: FrameType,
13 pub buf: NAVideoBufferRef<u8>,
16 pub long_term: Option<usize>,
18 pub mv_info: FrameMV, //todo replace with refcounted index to a pool
21 #[derive(Clone,Copy,Default, Debug)]
22 pub struct FrameMBInfo {
23 pub mb_type: CompactMBType,
24 pub ref_poc: [[u16; 2]; 4],
25 pub ref_idx: [[PicRef; 2]; 4],
26 pub mv: [[MV; 2]; 16],
30 pub fn new() -> Self { Self::default() }
35 pub mbs: Vec<FrameMBInfo>,
40 pub fn new(mb_w: usize, mb_h: usize) -> Self {
42 mbs: vec![FrameMBInfo::default(); mb_w * mb_h],
48 pub struct FrameRefs {
49 pub ref_pics: Vec<PictureInfo>,
50 pub ref_list0: Vec<Option<PictureInfo>>,
51 pub ref_list1: Vec<Option<PictureInfo>>,
52 pub long_term: Vec<Option<PictureInfo>>,
57 prev_ref_poc_lsb: u16,
59 frame_num_offset: u32,
64 pub fn new() -> Self {
66 ref_pics: Vec::with_capacity(16),
67 ref_list0: Vec::with_capacity(3),
68 ref_list1: Vec::with_capacity(3),
69 long_term: Vec::new(),
80 pub fn calc_picture_num(&mut self, slice_hdr: &SliceHeader, is_idr: bool, ref_id: u8, sps: &SeqParameterSet) -> u32 {
81 self.max_frame_num = 1 << sps.log2_max_frame_num;
82 match sps.pic_order_cnt_type {
85 //self.prev_poc_msb = 0;
86 self.prev_poc_lsb = 0;
88 self.prev_poc_lsb = self.prev_ref_poc_lsb;
90 let max_poc_lsb = 1 << sps.log2_max_pic_order_cnt_lsb;
91 let half_max_poc_lsb = 1 << (sps.log2_max_pic_order_cnt_lsb - 1);
92 let cur_lsb = slice_hdr.pic_order_cnt_lsb;
93 let poc_msb = if cur_lsb < self.prev_poc_lsb && (self.prev_poc_lsb - cur_lsb >= half_max_poc_lsb) {
94 self.prev_poc_msb + max_poc_lsb
95 } else if cur_lsb > self.prev_poc_lsb && (cur_lsb - self.prev_poc_lsb > half_max_poc_lsb) {
96 self.prev_poc_msb.wrapping_sub(max_poc_lsb)
100 let poc = poc_msb + u32::from(cur_lsb);
102 self.prev_ref_poc_lsb = slice_hdr.pic_order_cnt_lsb;
103 self.prev_poc_msb = poc_msb;
108 let off = if self.prev_frame_num > slice_hdr.frame_num {
109 self.frame_num_offset + (1 << sps.log2_max_frame_num)
111 self.frame_num_offset
113 let mut anum = if sps.num_ref_frames_in_pic_order_cnt_cycle != 0 {
114 (off as i32) + i32::from(slice_hdr.frame_num)
118 if ref_id == 0 && anum > 0 {
121 let (poc_cycle_cnt, fno_in_poc_cycle) = if anum > 0 {
122 let nrf = sps.num_ref_frames_in_pic_order_cnt_cycle as i32;
123 ((anum - 1) / nrf, (anum - 1) % nrf)
127 let mut expected_delta = 0;
128 for &offset in sps.offset_for_ref_frame[..sps.num_ref_frames_in_pic_order_cnt_cycle].iter() {
129 expected_delta += offset;
131 let mut expected_poc = if anum > 0 {
132 let mut sum = poc_cycle_cnt * expected_delta;
133 for &offset in sps.offset_for_ref_frame[..=fno_in_poc_cycle as usize].iter() {
141 expected_poc += sps.offset_for_non_ref_pic;
143 let (top_id, _bottom_id) = if !slice_hdr.field_pic {
144 let top_id = expected_poc + slice_hdr.delta_pic_order_cnt[0];
145 let bot_id = top_id + sps.offset_for_top_to_bottom_field + slice_hdr.delta_pic_order_cnt[1];
147 } else if !slice_hdr.bottom_field {
148 (expected_poc + slice_hdr.delta_pic_order_cnt[0], 0)
150 (0, sps.offset_for_top_to_bottom_field + slice_hdr.delta_pic_order_cnt[1])
152 self.prev_frame_num = slice_hdr.frame_num;
153 self.frame_num_offset = off;
157 if slice_hdr.frame_num < self.prev_frame_num {
158 self.frame_num_offset += 1 << sps.log2_max_frame_num;
160 self.prev_frame_num = slice_hdr.frame_num;
161 self.frame_num_offset + u32::from(slice_hdr.frame_num)
165 pub fn apply_adaptive_marking(&mut self, marking: &AdaptiveMarking, cur_id: u16, max_id: u16) -> DecoderResult<()> {
166 let all_ref_pics = self.ref_pics.clone();
168 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) {
171 let src_id = cur_id.wrapping_sub(arg1) & (max_id - 1);
172 let mut found = false;
174 for (i, pic) in self.ref_pics.iter().enumerate() {
175 if pic.id == src_id {
182 self.ref_pics.remove(idx);
185 2 => { // mark long term picture as unused
186 let idx = arg1 as usize;
187 if idx < self.long_term.len() {
188 self.long_term[idx] = None;
192 let src_id = cur_id.wrapping_sub(arg1) & (max_id - 1);
194 let didx = arg2 as usize;
195 for pic in all_ref_pics.iter() {
196 if pic.id == src_id {
197 if didx < self.long_term.len() {
198 self.long_term[didx] = Some(pic.clone());
205 self.long_term.resize(arg1 as usize, None);
208 self.ref_pics.clear();
209 self.long_term.clear();
212 // assign an long term index to current pic - done elsewhere
219 pub fn clear_refs(&mut self) {
220 self.ref_pics.clear();
221 self.long_term.clear();
223 #[allow(clippy::cognitive_complexity)]
224 pub fn select_refs(&mut self, sps: &SeqParameterSet, slice_hdr: &SliceHeader, cur_id: u32) {
225 self.cur_id = cur_id;
226 self.ref_list0.clear();
227 self.ref_list1.clear();
228 let pic_num_mask = if sps.log2_max_frame_num == 16 {
231 (1 << sps.log2_max_frame_num) - 1
233 if !slice_hdr.slice_type.is_intra() {
234 let has_reordering = slice_hdr.ref_pic_list_reordering_l0;
236 let num_ref = slice_hdr.num_ref_idx_l0_active;
237 if slice_hdr.slice_type.is_p() {
238 if !self.ref_pics.is_empty() {
239 for pic in self.ref_pics.iter().rev().take(num_ref) {
240 self.ref_list0.push(Some(pic.clone()));
245 for (i, pic) in self.ref_pics.iter().enumerate() {
247 if pic.full_id > cur_id {
251 for pic in self.ref_pics[..pivot].iter().rev() {
252 if self.ref_list0.len() >= num_ref {
255 self.ref_list0.push(Some(pic.clone()));
257 for pic in self.ref_pics.iter().skip(pivot) {
258 if self.ref_list0.len() >= num_ref {
261 self.ref_list0.push(Some(pic.clone()));
264 if !self.long_term.is_empty() && self.ref_list0.len() < num_ref {
265 let copy_size = num_ref - self.ref_list0.len();
266 for ltpic in self.long_term.iter().take(copy_size) {
267 self.ref_list0.push(ltpic.clone());
271 form_ref_list(&mut self.ref_list0,
272 &self.ref_pics, &self.long_term,
273 &slice_hdr.reordering_list_l0,
274 slice_hdr.frame_num, pic_num_mask);
276 if slice_hdr.slice_type.is_b() {
277 let has_reordering = slice_hdr.ref_pic_list_reordering_l1;
279 let num_ref = slice_hdr.num_ref_idx_l1_active;
281 for (i, pic) in self.ref_pics.iter().enumerate() {
283 if pic.full_id > cur_id {
287 for pic in self.ref_pics.iter().skip(pivot) {
288 if self.ref_list1.len() >= num_ref {
291 self.ref_list1.push(Some(pic.clone()));
293 for pic in self.ref_pics[..pivot].iter().rev() {
294 if self.ref_list1.len() >= num_ref {
297 self.ref_list1.push(Some(pic.clone()));
299 if !self.long_term.is_empty() && self.ref_list1.len() < num_ref {
300 let copy_size = num_ref - self.ref_list1.len();
301 for ltpic in self.long_term.iter().take(copy_size) {
302 self.ref_list1.push(ltpic.clone());
305 if self.ref_list1.len() > 1 && self.ref_list0.len() == self.ref_list1.len() {
306 let mut equal = true;
307 for (pic1, pic2) in self.ref_list0.iter().zip(self.ref_list1.iter()) {
309 (Some(p1), Some(p2)) => {
310 if p1.full_id != p2.full_id {
323 self.ref_list1.swap(0, 1);
327 form_ref_list(&mut self.ref_list1,
328 &self.ref_pics, &self.long_term,
329 &slice_hdr.reordering_list_l1,
330 slice_hdr.frame_num, pic_num_mask);
335 pub fn add_short_term(&mut self, cpic: PictureInfo, num_ref_frames: usize) {
336 if !self.ref_pics.is_empty() && self.ref_pics.len() >= num_ref_frames {
337 let base_id = i32::from(cpic.id);
338 let mut min_id = base_id;
340 for (i, pic) in self.ref_pics.iter().enumerate() {
341 let mut pic_id = i32::from(pic.id);
342 if pic_id > base_id {
343 pic_id -= self.max_frame_num;
350 self.ref_pics.remove(min_idx);
352 if self.ref_pics.is_empty() || self.ref_pics.last().unwrap().full_id < cpic.full_id {
353 self.ref_pics.push(cpic);
356 for (i, pic) in self.ref_pics.iter().enumerate() {
357 if pic.full_id < cpic.full_id {
363 self.ref_pics.insert(idx + 1, cpic);
366 pub fn add_long_term(&mut self, lt_idx: usize, cpic: PictureInfo) {
367 if lt_idx < self.long_term.len() {
368 self.long_term[lt_idx] = Some(cpic);
371 pub fn select_ref_pic(&self, list_id: u8, ref_id: usize) -> Option<NAVideoBufferRef<u8>> {
372 let ref_list = if list_id == 0 { &self.ref_list0 } else { &self.ref_list1 };
373 if ref_list.len() > ref_id {
374 if let Some(ref pic) = ref_list[ref_id] {
375 Some(pic.buf.clone())
383 pub fn get_colocated_info(&self, mb_x: usize, mb_y: usize) -> (FrameMBInfo, u16, bool) {
384 if let Some(ref ref_pic) = &self.ref_list1[0] {
385 let mv_info = &ref_pic.mv_info;
386 let mb = mv_info.mbs[mb_x + mb_y * mv_info.mb_stride];
387 (mb, ref_pic.full_id as u16, ref_pic.long_term.is_some())
389 (FrameMBInfo::default(), 0, false)
392 pub fn map_ref0(&self, ref0_id: u16) -> (PicRef, bool) {
394 let mut long = false;
395 for (i, rpic0) in self.ref_list0.iter().enumerate() {
396 if let Some(ref pic) = rpic0 {
397 if (pic.full_id as u16) == ref0_id {
399 long = pic.long_term.is_some();
404 (PicRef::new(r0_idx), long)
406 pub fn map_refs(&self, ref_idx: [PicRef; 2]) -> [u16; 2] {
407 let r0 = ref_idx[0].index();
408 let r1 = ref_idx[1].index();
409 let ref0 = if r0 < self.ref_list0.len() {
410 if let Some(ref pic) = self.ref_list0[r0] {
418 let ref1 = if r1 < self.ref_list1.len() {
419 if let Some(ref pic) = self.ref_list1[r1] {
429 pub fn cmp_refs(&self, ref1: [PicRef; 2], ref2: [PicRef; 2]) -> bool {
431 self.cmp_ref(ref1[0], ref2[0], 0) && self.cmp_ref(ref1[1], ref2[1], 1)
436 fn cmp_ref(&self, ref1: PicRef, ref2: PicRef, list: u8) -> bool {
440 let idx0 = ref1.index();
441 let idx1 = ref2.index();
445 let src = if list == 0 { &self.ref_list0 } else { &self.ref_list1 };
446 if idx0 >= src.len() || idx1 >= src.len() {
447 //panic!("wrong refs");
450 if let (Some(ref pic0), Some(ref pic1)) = (&src[idx0], &src[idx1]) {
451 pic0.full_id == pic1.full_id
453 //panic!("missing pics");
460 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) {
461 let mut ref_pic_id = cur_id;
462 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) {
465 ref_pic_id = ref_pic_id.wrapping_sub(num) & pic_num_mask;
467 ref_pic_id = ref_pic_id.wrapping_add(num) & pic_num_mask;
469 let mut found = false;
470 for pic in ref_pics.iter() {
471 if pic.id == ref_pic_id {
472 ref_list.push(Some(pic.clone()));
481 let idx = num as usize;
482 if idx < long_term.len() {
483 ref_list.push(long_term[idx].clone());