Skip to content
File

Blob: firmware/vendor/str0m/src/packet/contiguity_vp9.rs

rust160 lines
1use super::Vp9CodecExtra;
2use super::contiguity::FrameContiguityState;
3 
4#[derive(Debug, Default)]
5pub struct Vp9Contiguity {
6 /// last picture id of layer 0 that we allowed emitting
7 last_tl0_picture_id: Option<u8>,
8 
9 /// last picture id of any layer that we allowed emitting
10 last_picture_id: Option<u16>,
11}
12 
13impl Vp9Contiguity {
14 pub fn new() -> Self {
15 Self::default()
16 }
17 
18 /// Called when depacketizing a new frame is ready to be emitted
19 ///
20 /// Returns whether we can emit such frame and whether it's decodable with contiguity
21 pub fn check(&mut self, next: &Vp9CodecExtra, contiguous_seq: bool) -> (bool, bool) {
22 let mut frame_state: FrameContiguityState = self.into();
23 let res = frame_state.next_frame(
24 Some(next.pid.into()),
25 next.tl0_picture_id.map(Into::into),
26 next.tid.map(Into::into),
27 contiguous_seq,
28 );
29 
30 *self = frame_state.into();
31 res
32 }
33}
34 
35impl From<&mut Vp9Contiguity> for FrameContiguityState {
36 fn from(value: &mut Vp9Contiguity) -> Self {
37 Self {
38 last_picture_id: value.last_picture_id.map(Into::into),
39 last_tl0_picture_id: value.last_tl0_picture_id.map(Into::into),
40 }
41 }
42}
43 
44impl From<FrameContiguityState> for Vp9Contiguity {
45 fn from(value: FrameContiguityState) -> Self {
46 Self {
47 last_picture_id: value.last_picture_id.and_then(|v| v.try_into().ok()),
48 last_tl0_picture_id: value.last_tl0_picture_id.and_then(|v| v.try_into().ok()),
49 }
50 }
51}
52 
53#[cfg(test)]
54mod test {
55 use crate::packet::Vp9CodecExtra;
56 
57 use super::Vp9Contiguity;
58 
59 const L1T3_LAYERS: &[u64] = &[0, 2, 1, 2];
60 const L1T2_LAYERS: &[u64] = &[0, 1, 0, 1];
61 
62 fn get_codec_extra(pid: u16, tid: u8, tl0_picture_id: u8) -> Vp9CodecExtra {
63 Vp9CodecExtra {
64 pid,
65 tid: Some(tid),
66 tl0_picture_id: Some(tl0_picture_id),
67 ..Default::default()
68 }
69 }
70 
71 #[test]
72 fn contiguous_l1t3() {
73 let mut contiguity = Vp9Contiguity::new();
74 
75 for i in 0..100 {
76 let next = get_codec_extra(i, L1T3_LAYERS[i as usize % 4] as u8, (i / 4) as u8);
77 
78 let res = contiguity.check(&next, true);
79 assert_eq!(res, (true, true), "Failure at picture {} {:?}", i, next);
80 }
81 }
82 
83 #[test]
84 fn contiguous_l1t3_no_l2_contig_l0() {
85 const L1T3_LAYERS: &[u64] = &[0, 2, 1, 2];
86 
87 let mut contiguity = Vp9Contiguity::new();
88 
89 for i in 0..100 {
90 let layer_index = L1T3_LAYERS[i as usize % 4] as u8;
91 if layer_index == 2 {
92 continue;
93 }
94 
95 let next = get_codec_extra(i, layer_index, (i / 4) as u8);
96 let (emit, contiguous) = contiguity.check(&next, true);
97 
98 // all layer 0 are contiguous therefore no discontinuity
99 assert_eq!(emit, next.tid == Some(0));
100 assert!(contiguous);
101 
102 if layer_index == 1 {
103 // frames on layer 1 are not emitted (could be improved tracking more deps)
104 // and we don't raise a discontinuity (stream still decodable)
105 assert!(!emit);
106 assert!(contiguous);
107 }
108 }
109 }
110 
111 #[test]
112 fn contiguous_l1t3_no_l2_discontig_l0() {
113 const L1T3_LAYERS: &[u64] = &[0, 2, 1, 2];
114 
115 let mut contiguity = Vp9Contiguity::new();
116 
117 for i in 0..100 {
118 let layer_index = L1T3_LAYERS[i as usize % 4] as u8;
119 if layer_index == 2 {
120 continue;
121 }
122 
123 let next = get_codec_extra(i, layer_index, i as u8);
124 let (emit, _) = contiguity.check(&next, true);
125 
126 assert!(emit == (next.tid == Some(0)));
127 }
128 }
129 
130 #[test]
131 fn contiguous_l1t2() {
132 let mut contiguity = Vp9Contiguity::new();
133 
134 for i in 0..100 {
135 let next = get_codec_extra(i, L1T2_LAYERS[i as usize % 4] as u8, (i / 2) as u8);
136 
137 let res = contiguity.check(&next, true);
138 assert_eq!(res, (true, true), "Failure at picture {} {:?}", i, next);
139 }
140 }
141 
142 #[test]
143 fn contiguous_l1t1_no_l1_contig_l0() {
144 let mut contiguity = Vp9Contiguity::new();
145 
146 for i in 0..100 {
147 let layer_index = L1T2_LAYERS[i as usize % 4] as u8;
148 if layer_index == 1 {
149 continue;
150 }
151 
152 let next = get_codec_extra(i, layer_index, (i / 2) as u8);
153 let (emit, _) = contiguity.check(&next, true);
154 
155 // all layer 0 are contiguous therefore can be emitted
156 assert_eq!(emit, next.tid == Some(0));
157 }
158 }
159}