xref: /webrtc/rtp/src/codecs/vp9/vp9_test.rs (revision 6ac0fffd)
1 use super::*;
2 
3 #[test]
4 fn test_vp9_packet_unmarshal() -> Result<()> {
5     let tests = vec![
6         (
7             "Empty",
8             Bytes::from_static(&[]),
9             Vp9Packet::default(),
10             Bytes::new(),
11             Some(Error::ErrShortPacket),
12         ),
13         (
14             "NonFlexible",
15             Bytes::from_static(&[0x00, 0xAA]),
16             Vp9Packet::default(),
17             Bytes::from_static(&[0xAA]),
18             None,
19         ),
20         (
21             "NonFlexiblePictureID",
22             Bytes::from_static(&[0x80, 0x02, 0xAA]),
23             Vp9Packet {
24                 i: true,
25                 picture_id: 0x02,
26                 ..Default::default()
27             },
28             Bytes::from_static(&[0xAA]),
29             None,
30         ),
31         (
32             "NonFlexiblePictureIDExt",
33             Bytes::from_static(&[0x80, 0x81, 0xFF, 0xAA]),
34             Vp9Packet {
35                 i: true,
36                 picture_id: 0x01FF,
37                 ..Default::default()
38             },
39             Bytes::from_static(&[0xAA]),
40             None,
41         ),
42         (
43             "NonFlexiblePictureIDExt_ShortPacket0",
44             Bytes::from_static(&[0x80, 0x81]),
45             Vp9Packet::default(),
46             Bytes::new(),
47             Some(Error::ErrShortPacket),
48         ),
49         (
50             "NonFlexiblePictureIDExt_ShortPacket1",
51             Bytes::from_static(&[0x80]),
52             Vp9Packet::default(),
53             Bytes::new(),
54             Some(Error::ErrShortPacket),
55         ),
56         (
57             "NonFlexibleLayerIndicePictureID",
58             Bytes::from_static(&[0xA0, 0x02, 0x23, 0x01, 0xAA]),
59             Vp9Packet {
60                 i: true,
61                 l: true,
62                 picture_id: 0x02,
63                 tid: 0x01,
64                 sid: 0x01,
65                 d: true,
66                 tl0picidx: 0x01,
67                 ..Default::default()
68             },
69             Bytes::from_static(&[0xAA]),
70             None,
71         ),
72         (
73             "FlexibleLayerIndicePictureID",
74             Bytes::from_static(&[0xB0, 0x02, 0x23, 0x01, 0xAA]),
75             Vp9Packet {
76                 f: true,
77                 i: true,
78                 l: true,
79                 picture_id: 0x02,
80                 tid: 0x01,
81                 sid: 0x01,
82                 d: true,
83                 ..Default::default()
84             },
85             Bytes::from_static(&[0x01, 0xAA]),
86             None,
87         ),
88         (
89             "NonFlexibleLayerIndicePictureID_ShortPacket0",
90             Bytes::from_static(&[0xA0, 0x02, 0x23]),
91             Vp9Packet::default(),
92             Bytes::new(),
93             Some(Error::ErrShortPacket),
94         ),
95         (
96             "NonFlexibleLayerIndicePictureID_ShortPacket1",
97             Bytes::from_static(&[0xA0, 0x02]),
98             Vp9Packet::default(),
99             Bytes::new(),
100             Some(Error::ErrShortPacket),
101         ),
102         (
103             "FlexiblePictureIDRefIndex",
104             Bytes::from_static(&[0xD0, 0x02, 0x03, 0x04, 0xAA]),
105             Vp9Packet {
106                 i: true,
107                 p: true,
108                 f: true,
109                 picture_id: 0x02,
110                 pdiff: vec![0x01, 0x02],
111                 ..Default::default()
112             },
113             Bytes::from_static(&[0xAA]),
114             None,
115         ),
116         (
117             "FlexiblePictureIDRefIndex_TooManyPDiff",
118             Bytes::from_static(&[0xD0, 0x02, 0x03, 0x05, 0x07, 0x09, 0x10, 0xAA]),
119             Vp9Packet::default(),
120             Bytes::new(),
121             Some(Error::ErrTooManyPDiff),
122         ),
123         (
124             "FlexiblePictureIDRefIndexNoPayload",
125             Bytes::from_static(&[0xD0, 0x02, 0x03, 0x04]),
126             Vp9Packet {
127                 i: true,
128                 p: true,
129                 f: true,
130                 picture_id: 0x02,
131                 pdiff: vec![0x01, 0x02],
132                 ..Default::default()
133             },
134             Bytes::from_static(&[]),
135             None,
136         ),
137         (
138             "FlexiblePictureIDRefIndex_ShortPacket0",
139             Bytes::from_static(&[0xD0, 0x02, 0x03]),
140             Vp9Packet::default(),
141             Bytes::new(),
142             Some(Error::ErrShortPacket),
143         ),
144         (
145             "FlexiblePictureIDRefIndex_ShortPacket1",
146             Bytes::from_static(&[0xD0, 0x02]),
147             Vp9Packet::default(),
148             Bytes::new(),
149             Some(Error::ErrShortPacket),
150         ),
151         (
152             "FlexiblePictureIDRefIndex_ShortPacket2",
153             Bytes::from_static(&[0xD0]),
154             Vp9Packet::default(),
155             Bytes::new(),
156             Some(Error::ErrShortPacket),
157         ),
158         (
159             "ScalabilityStructureResolutionsNoPayload",
160             Bytes::from_static(&[
161                 0x0A,
162                 (1 << 5) | (1 << 4), // NS:1 Y:1 G:0
163                 (640 >> 8) as u8,
164                 (640 & 0xff) as u8,
165                 (360 >> 8) as u8,
166                 (360 & 0xff) as u8,
167                 (1280 >> 8) as u8,
168                 (1280 & 0xff) as u8,
169                 (720 >> 8) as u8,
170                 (720 & 0xff) as u8,
171             ]),
172             Vp9Packet {
173                 b: true,
174                 v: true,
175                 ns: 1,
176                 y: true,
177                 g: false,
178                 ng: 0,
179                 width: vec![640, 1280],
180                 height: vec![360, 720],
181                 ..Default::default()
182             },
183             Bytes::new(),
184             None,
185         ),
186         (
187             "ScalabilityStructureNoPayload",
188             Bytes::from_static(&[
189                 0x0A,
190                 (1 << 5) | (1 << 3), // NS:1 Y:0 G:1
191                 2,
192                 (1 << 4),            // T:0 U:1 R:0 -
193                 (2 << 5) | (1 << 2), // T:2 U:0 R:1 -
194                 33,
195             ]),
196             Vp9Packet {
197                 b: true,
198                 v: true,
199                 ns: 1,
200                 y: false,
201                 g: true,
202                 ng: 2,
203                 pgtid: vec![0, 2],
204                 pgu: vec![true, false],
205                 pgpdiff: vec![vec![], vec![33]],
206                 ..Default::default()
207             },
208             Bytes::new(),
209             None,
210         ),
211     ];
212 
213     for (name, b, pkt, expected, err) in tests {
214         let mut p = Vp9Packet::default();
215 
216         if let Some(expected) = err {
217             if let Err(actual) = p.depacketize(&b) {
218                 assert_eq!(
219                     expected, actual,
220                     "{}: expected {}, but got {}",
221                     name, expected, actual
222                 );
223             } else {
224                 assert!(false, "{}: expected error, but got passed", name);
225             }
226         } else {
227             let payload = p.depacketize(&b)?;
228             assert_eq!(pkt, p, "{}: expected {:?}, but got {:?}", name, pkt, p);
229             assert_eq!(payload, expected);
230         }
231     }
232 
233     Ok(())
234 }
235 
236 #[test]
237 fn test_vp9_payloader_payload() -> Result<()> {
238     let mut r0 = 8692;
239     let mut rands = vec![];
240     for _ in 0..10 {
241         rands.push(vec![(r0 >> 8) as u8 | 0x80, (r0 & 0xFF) as u8]);
242         r0 += 1;
243     }
244 
245     let tests = vec![
246         ("NilPayload", vec![Bytes::new()], 100, vec![]),
247         ("SmallMTU", vec![Bytes::from(vec![0x00, 0x00])], 1, vec![]),
248         (
249             "NegativeMTU",
250             vec![Bytes::from(vec![0x00, 0x00])],
251             0,
252             vec![],
253         ),
254         (
255             "OnePacket",
256             vec![Bytes::from(vec![0x01, 0x02])],
257             10,
258             vec![Bytes::from(vec![
259                 0x9C,
260                 rands[0][0],
261                 rands[0][1],
262                 0x01,
263                 0x02,
264             ])],
265         ),
266         (
267             "TwoPackets",
268             vec![Bytes::from(vec![0x01, 0x02])],
269             4,
270             vec![
271                 Bytes::from(vec![0x98, rands[0][0], rands[0][1], 0x01]),
272                 Bytes::from(vec![0x94, rands[0][0], rands[0][1], 0x02]),
273             ],
274         ),
275         (
276             "ThreePackets",
277             vec![Bytes::from(vec![0x01, 0x02, 0x03])],
278             4,
279             vec![
280                 Bytes::from(vec![0x98, rands[0][0], rands[0][1], 0x01]),
281                 Bytes::from(vec![0x90, rands[0][0], rands[0][1], 0x02]),
282                 Bytes::from(vec![0x94, rands[0][0], rands[0][1], 0x03]),
283             ],
284         ),
285         (
286             "TwoFramesFourPackets",
287             vec![Bytes::from(vec![0x01, 0x02, 0x03]), Bytes::from(vec![0x04])],
288             5,
289             vec![
290                 Bytes::from(vec![0x98, rands[0][0], rands[0][1], 0x01, 0x02]),
291                 Bytes::from(vec![0x94, rands[0][0], rands[0][1], 0x03]),
292                 Bytes::from(vec![0x9C, rands[1][0], rands[1][1], 0x04]),
293             ],
294         ),
295     ];
296 
297     for (name, bs, mtu, expected) in tests {
298         let mut pck = Vp9Payloader {
299             initial_picture_id_fn: Some(Arc::new(|| -> u16 { 8692 })),
300             ..Default::default()
301         };
302 
303         let mut actual = vec![];
304         for b in &bs {
305             actual.extend(pck.payload(mtu, b)?);
306         }
307         assert_eq!(expected, actual, "{}: Payloaded packet", name);
308     }
309 
310     //"PictureIDOverflow"
311     {
312         let mut pck = Vp9Payloader {
313             initial_picture_id_fn: Some(Arc::new(|| -> u16 { 8692 })),
314             ..Default::default()
315         };
316         let mut p_prev = Vp9Packet::default();
317         for i in 0..0x8000 {
318             let res = pck.payload(4, &Bytes::from_static(&[0x01]))?;
319             let mut p = Vp9Packet::default();
320             p.depacketize(&res[0])?;
321 
322             if i > 0 {
323                 if p_prev.picture_id == 0x7FFF {
324                     assert_eq!(
325                         p.picture_id, 0,
326                         "Picture ID next to 0x7FFF must be 0, got {}",
327                         p.picture_id
328                     );
329                 } else if p_prev.picture_id + 1 != p.picture_id {
330                     assert!(
331                         false,
332                         "Picture ID next must be incremented by 1: {} -> {}",
333                         p_prev.picture_id, p.picture_id,
334                     );
335                 }
336             }
337 
338             p_prev = p;
339         }
340     }
341 
342     Ok(())
343 }
344 
345 #[test]
346 fn test_vp9_partition_head_checker_is_partition_head() -> Result<()> {
347     let vp9 = Vp9Packet::default();
348 
349     //"SmallPacket"
350     assert!(
351         !vp9.is_partition_head(&Bytes::new()),
352         "Small packet should not be the head of a new partition"
353     );
354 
355     //"NormalPacket"
356     assert!(
357         vp9.is_partition_head(&Bytes::from_static(&[0x18, 0x00, 0x00])),
358         "VP9 RTP packet with B flag should be head of a new partition"
359     );
360     assert!(
361         !vp9.is_partition_head(&Bytes::from_static(&[0x10, 0x00, 0x00])),
362         "VP9 RTP packet without B flag should not be head of a new partition"
363     );
364 
365     Ok(())
366 }
367