1 //! Output frame reordering.
3 //! NihAV decoders output frames in the same order as they are put in.
4 //! In result if you want to have frames in display order you might need some frame reorderer.
5 //! This module provides such functionality depending on codec type: audio codecs and video codecs without B-frames do not need any reorderer and can use `NoReorderer` if the common interface is required. Codecs with B-frames should use `IPBReorderer`. For codecs with very complex reordering rules like H.264 or H.256 `PictureIDReorderer` will be added eventually.
7 //! You can find out required reorderer by quering codec properties using `nihav_core::register` module.
9 pub use crate::frame::{FrameType, NAFrameRef};
11 /// A trait for frame reorderer.
12 pub trait FrameReorderer {
13 /// Stores a newly decoded frame.
14 fn add_frame(&mut self, fref: NAFrameRef) -> bool;
15 /// Gets the next frame to be displayed (or `None` if that is not possible).
16 fn get_frame(&mut self) -> Option<NAFrameRef>;
17 /// Clears all stored frames.
19 /// Retrieves the last frames stored by the reorderer.
20 fn get_last_frames(&mut self) -> Option<NAFrameRef>;
24 pub struct NoReorderer {
25 fref: Option<NAFrameRef>,
29 /// Constructs a new instance of `NoReorderer`.
30 pub fn new() -> Self {
35 impl Default for NoReorderer {
36 fn default() -> Self {
41 impl FrameReorderer for NoReorderer {
42 fn add_frame(&mut self, fref: NAFrameRef) -> bool {
43 if self.fref.is_none() {
44 self.fref = Some(fref);
50 fn get_frame(&mut self) -> Option<NAFrameRef> {
52 swap(&mut ret, &mut self.fref);
55 fn flush(&mut self) { self.fref = None; }
56 fn get_last_frames(&mut self) -> Option<NAFrameRef> { None }
59 /// Frame reorderer for codecs with I/P/B frames.
61 pub struct IPBReorderer {
62 rframe: Option<NAFrameRef>,
63 bframe: Option<NAFrameRef>,
67 /// Constructs a new instance of `IPBReorderer`.
68 pub fn new() -> Self { Self::default() }
71 impl FrameReorderer for IPBReorderer {
72 fn add_frame(&mut self, fref: NAFrameRef) -> bool {
73 if self.rframe.is_some() && self.bframe.is_some() { return false; }
74 let is_b = fref.get_frame_type() == FrameType::B;
75 if is_b && self.bframe.is_some() { return false; }
77 self.bframe = Some(fref);
79 std::mem::swap(&mut self.bframe, &mut self.rframe);
80 self.rframe = Some(fref);
84 fn get_frame(&mut self) -> Option<NAFrameRef> {
86 if self.bframe.is_some() {
87 std::mem::swap(&mut ret, &mut self.bframe);
95 fn get_last_frames(&mut self) -> Option<NAFrameRef> {
97 if self.bframe.is_some() {
98 std::mem::swap(&mut ret, &mut self.bframe);
99 } else if self.rframe.is_some() {
100 std::mem::swap(&mut ret, &mut self.rframe);
106 /// Frame reorderer for codecs with complex I/P/B frame structure like ITU H.26x.
108 pub struct ComplexReorderer {
109 last_ref_dts: Option<u64>,
111 frames: Vec<NAFrameRef>,
114 impl ComplexReorderer {
115 /// Constructs a new instance of `IPBReorderer`.
116 pub fn new() -> Self { Self::default() }
119 impl FrameReorderer for ComplexReorderer {
120 fn add_frame(&mut self, fref: NAFrameRef) -> bool {
121 if self.frames.len() >= 64 {
124 let is_ref = fref.frame_type == FrameType::I || fref.frame_type == FrameType::P;
126 if self.frames.is_empty() || fref.get_dts().is_none() {
127 self.frames.push(fref);
128 } else if let Some(new_dts) = fref.get_dts() {
130 for (i, frm) in self.frames.iter().enumerate() {
132 if let Some(dts) = frm.get_dts() {
138 self.frames.insert(idx, fref);
141 for (i, frm) in self.frames.iter().enumerate() {
142 if frm.get_dts() == self.last_ref_dts {
143 self.ready_idx = i + 1;
146 self.last_ref_dts = fref.get_dts();
147 self.frames.push(fref);
151 fn get_frame(&mut self) -> Option<NAFrameRef> {
152 if self.ready_idx > 0 {
154 Some(self.frames.remove(0))
159 fn flush(&mut self) {
160 self.last_ref_dts = None;
164 fn get_last_frames(&mut self) -> Option<NAFrameRef> {
165 if !self.frames.is_empty() {
166 Some(self.frames.remove(0))