use bytes::Bytes;
use crate::codec::av1::Av1Depacketizer;
use crate::codec::h264::H264Packet;
use crate::codec::h265::H265Packet;
use crate::codec::opus::OpusPacket;
use crate::codec::vp8::Vp8Packet;
use crate::codec::vp9::Vp9Packet;
use crate::packetizer::Depacketizer;
fn key<T: Depacketizer>(depacketizer: &T, payload: &[u8]) -> bool {
depacketizer.is_key_frame_start(&Bytes::copy_from_slice(payload))
}
#[test]
fn vp8_key_frame_start_is_the_first_packet_of_a_frame_with_p_clear() {
let d = Vp8Packet::default();
assert!(d.has_key_frames());
assert!(key(&d, &[0x10, 0x00, 0xAA]), "S=1, PID=0, P=0");
assert!(!key(&d, &[0x10, 0x01, 0xAA]), "P=1: an interframe");
assert!(!key(&d, &[0x00, 0x00, 0xAA]), "S=0: not the first packet");
assert!(!key(&d, &[0x11, 0x00, 0xAA]), "a later partition");
assert!(key(&d, &[0x90, 0xF0, 0x81, 0x02, 0x03, 0x04, 0x00]));
assert!(!key(&d, &[0x90, 0xF0, 0x81, 0x02, 0x03, 0x04, 0x01]));
assert!(key(&d, &[0x90, 0x80, 0x05, 0x00]));
assert!(!key(&d, &[0x90, 0x80, 0x05, 0x01]));
assert!(!key(&d, &[0x90, 0x80, 0x05]), "no payload header");
assert!(!key(&d, &[0x10]), "no payload header");
assert!(!key(&d, &[]));
}
#[test]
fn vp9_key_frame_start_is_the_start_of_a_base_layer_frame_with_p_clear() {
let d = Vp9Packet::default();
assert!(d.has_key_frames());
assert!(key(&d, &[0x08, 0xAA]), "B=1, P=0");
assert!(!key(&d, &[0x48, 0xAA]), "P=1");
assert!(!key(&d, &[0x00, 0xAA]), "B=0: a continuation");
assert!(key(&d, &[0xA8, 0x05, 0x00, 0x00, 0xAA]), "SID=0");
assert!(!key(&d, &[0xA8, 0x05, 0x02, 0x00, 0xAA]), "SID=1, P=0");
assert!(!key(&d, &[0xA8, 0x05, 0x03, 0x00, 0xAA]), "SID=1, D=1");
assert!(key(&d, &[0xA8, 0x85, 0x05, 0x00, 0x00, 0xAA]));
assert!(!key(&d, &[0xA8, 0x85, 0x05, 0x02, 0x00, 0xAA]));
assert!(!key(&d, &[]));
}
#[test]
fn av1_key_frame_start_is_the_n_bit() {
let d = Av1Depacketizer::new();
assert!(d.has_key_frames());
assert!(key(&d, &[0x18, 0x08, 0x00]), "N=1");
assert!(!key(&d, &[0x10, 0x08, 0x00]), "N=0");
assert!(!key(&d, &[0x98, 0x08, 0x00]), "Z=1: a continuation");
assert!(!key(&d, &[]));
}
#[test]
fn h264_key_frame_start_is_the_first_slice_of_an_idr_picture() {
let d = H264Packet::default();
assert!(d.has_key_frames());
assert!(key(&d, &[0x65, 0x88, 0x80]), "IDR, first_mb_in_slice 0");
assert!(
!key(&d, &[0x65, 0x08, 0x80]),
"a later slice of the picture"
);
assert!(!key(&d, &[0x41, 0x9A, 0x80]), "a non-IDR slice");
assert!(!key(&d, &[0x67, 0x42, 0x00]), "a parameter set alone");
assert!(key(&d, &[0x7C, 0x85, 0x88, 0x80]));
assert!(!key(&d, &[0x7C, 0x05, 0x88, 0x80]), "S=0: a later fragment");
assert!(
!key(&d, &[0x7C, 0x45, 0x88, 0x80]),
"E=1: the last fragment"
);
assert!(!key(&d, &[0x7C, 0x81, 0x88, 0x80]), "a fragmented non-IDR");
assert!(key(
&d,
&[
0x78, 0x00, 0x02, 0x67, 0x42, 0x00, 0x02, 0x68, 0xCE, 0x00, 0x03, 0x65, 0x88, 0x80
]
));
assert!(!key(
&d,
&[
0x78, 0x00, 0x02, 0x67, 0x42, 0x00, 0x02, 0x68, 0xCE, 0x00, 0x03, 0x41, 0x9A, 0x80
]
));
assert!(!key(
&d,
&[0x78, 0x00, 0x02, 0x41, 0x9A, 0x00, 0x03, 0x65, 0x88, 0x80]
));
assert!(!key(&d, &[0x78, 0x00, 0x09, 0x65, 0x88]));
assert!(!key(&d, &[]));
}
#[test]
fn h265_key_frame_start_is_the_first_slice_segment_of_an_irap_picture() {
let d = H265Packet::default();
assert!(d.has_key_frames());
assert!(key(&d, &[0x26, 0x01, 0x80]), "IDR_W_RADL, first segment");
assert!(key(&d, &[0x2A, 0x01, 0x80]), "CRA");
assert!(!key(&d, &[0x26, 0x01, 0x00]), "a later slice segment");
assert!(!key(&d, &[0x02, 0x01, 0x80]), "a trailing picture");
assert!(!key(&d, &[0x42, 0x01, 0x01]), "a parameter set alone");
assert!(key(&d, &[0x62, 0x01, 0x93, 0x80]));
assert!(!key(&d, &[0x62, 0x01, 0x13, 0x80]), "S=0");
assert!(
!key(&d, &[0x62, 0x01, 0x81, 0x80]),
"a fragmented trailing picture"
);
assert!(key(
&d,
&[
0x60, 0x01, 0x00, 0x03, 0x40, 0x01, 0x0C, 0x00, 0x03, 0x42, 0x01, 0x01, 0x00, 0x03,
0x26, 0x01, 0x80
]
));
assert!(!key(
&d,
&[
0x60, 0x01, 0x00, 0x03, 0x40, 0x01, 0x0C, 0x00, 0x03, 0x02, 0x01, 0x80
]
));
let mut with_donl = H265Packet::default();
with_donl.with_donl(true);
assert!(key(&with_donl, &[0x26, 0x01, 0x00, 0x07, 0x80]));
assert!(!key(&with_donl, &[0x26, 0x01, 0x80, 0x07, 0x00]));
assert!(key(&with_donl, &[0x62, 0x01, 0x93, 0x00, 0x07, 0x80]));
assert!(!key(&d, &[0x26]));
}
#[test]
fn a_format_without_key_frames_never_names_one() {
let d = OpusPacket;
assert!(!d.has_key_frames());
assert!(!key(&d, &[0x00, 0x00, 0x00]));
assert!(!key(&d, &[0x10, 0x00, 0x00]));
}