forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli4 days ago4021 commits

packet_walk.rs203 lines, 7.0 KiBHistoryRawDownload
1//! A packet holding several syncframes decodes to those frames back to back.
2//!
3//! `decode_frame` takes one syncframe and refuses a second by name; the packet
4//! call (`decode_packet`, and `send_packet` which the shims use) walks every
5//! syncframe in the packet against one decoder state. The reference is the
6//! same bytes decoded one frame at a time on a fresh decoder, which the
7//! per-frame matrix already holds to ffmpeg, so equality here is exact.
8//!
9//! Missing fixtures skip, or fail under `EC_REQUIRE_FIXTURES=1`.
10
11use std::path::{Path, PathBuf};
12
13use ec_ac3::{Ac3Decoder, Error, eac3, frame_size};
14use ec_core::{Decoder, Frame, Packet, TimeBase};
15
16fn fixtures() -> PathBuf {
17 Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/audio")
18}
19
20/// Whether the fixture is present. A missing one skips the test, or fails it
21/// under `EC_REQUIRE_FIXTURES=1`.
22fn present(path: &Path) -> bool {
23 if path.exists() {
24 return true;
25 }
26 assert!(
27 std::env::var("EC_REQUIRE_FIXTURES").as_deref() != Ok("1"),
28 "fixture missing: {path:?}"
29 );
30 eprintln!("skipping: fixture missing: {path:?}");
31 false
32}
33
34/// Every syncframe of an AC-3 or E-AC-3 stream without dependent substreams.
35fn frames(data: &[u8]) -> Vec<&[u8]> {
36 let mut out = Vec::new();
37 let mut pos = 0;
38 while pos < data.len() {
39 let Ok(size) = frame_size(&data[pos..]) else {
40 break;
41 };
42 out.push(&data[pos..pos + size]);
43 pos += size;
44 }
45 out
46}
47
48/// Packets of an E-AC-3 stream whose independent substreams carry dependent
49/// ones: each packet runs from one independent substream to the next.
50fn packets(data: &[u8]) -> Vec<&[u8]> {
51 let mut starts = Vec::new();
52 let mut pos = 0;
53 while pos + 6 <= data.len() {
54 let Ok(size) = frame_size(&data[pos..]) else {
55 break;
56 };
57 let independent = eac3::bsi::parse(&data[pos + 2..])
58 .is_ok_and(|h| h.strmtyp == eac3::StreamType::Independent);
59 if independent {
60 starts.push(pos);
61 }
62 pos += size;
63 }
64 (0..starts.len())
65 .map(|i| {
66 let end = starts.get(i + 1).copied().unwrap_or(data.len());
67 &data[starts[i]..end]
68 })
69 .collect()
70}
71
72/// One frame at a time on one decoder: the reference the packet walk must hit.
73fn decode_each(packets: &[&[u8]]) -> Vec<u8> {
74 let mut dec = Ac3Decoder::new();
75 let mut out = Vec::new();
76 for p in packets {
77 let frame = dec.decode_frame(p).expect("single frame decodes");
78 out.extend_from_slice(&frame.data[0]);
79 }
80 out
81}
82
83/// The packet walk over the concatenation, through the trait call the shims
84/// make.
85fn decode_packet_bytes(packets: &[&[u8]]) -> (Vec<u8>, u64) {
86 let bytes: Vec<u8> = packets.concat();
87 let mut dec = Ac3Decoder::new();
88 dec.send_packet(&Packet::new(0, TimeBase::new(1, 48_000), bytes))
89 .expect("multi-syncframe packet decodes");
90 match dec.receive_frame() {
91 Ok(Frame::Audio(audio)) => (audio.data[0].to_vec(), audio.samples as u64),
92 other => panic!("expected one audio frame, got {other:?}"),
93 }
94}
95
96#[test]
97fn ac3_packets_of_two_and_three_syncframes_decode_to_their_frames() {
98 let path = fixtures().join("ac3-5.1-48000.ac3");
99 if !present(&path) {
100 return;
101 }
102 let data = std::fs::read(&path).expect("fixture");
103 let all = frames(&data);
104 assert!(all.len() >= 3, "fixture has too few frames");
105 for n in [2, 3] {
106 let group = &all[..n];
107 let (bytes, samples) = decode_packet_bytes(group);
108 assert_eq!(bytes, decode_each(group), "{n} AC-3 frames: samples");
109 assert_eq!(samples, (n * 1536) as u64, "{n} AC-3 frames: sample count");
110 }
111}
112
113#[test]
114fn eac3_packets_of_two_and_three_syncframes_decode_to_their_frames() {
115 let path = fixtures().join("eac3-5.1-48000.eac3");
116 if !present(&path) {
117 return;
118 }
119 let data = std::fs::read(&path).expect("fixture");
120 let all = frames(&data);
121 assert!(all.len() >= 3, "fixture has too few frames");
122 for n in [2, 3] {
123 let group = &all[..n];
124 let (bytes, _) = decode_packet_bytes(group);
125 assert_eq!(bytes, decode_each(group), "{n} E-AC-3 frames: samples");
126 }
127}
128
129/// Dependent substreams stay with their independent substream inside a packet
130/// that carries several (7.1 DD+ assembled as 5.1 plus a dependent).
131#[test]
132fn e_ac3_dependent_packets_inside_a_multi_syncframe_packet_keep_their_channels() {
133 let path = fixtures().join("ac3dep-7.1-48000.eac3");
134 if !present(&path) {
135 return;
136 }
137 let data = std::fs::read(&path).expect("fixture");
138 let all = packets(&data);
139 assert!(all.len() >= 3, "fixture has too few packets");
140 for n in [2, 3] {
141 let group = &all[..n];
142 let (bytes, samples) = decode_packet_bytes(group);
143 assert_eq!(bytes, decode_each(group), "{n} dependent packets");
144 // 8 channels of f32 per sample frame after the 7.1 layout is kept.
145 assert_eq!(bytes.len() as u64, samples * 8 * 4, "{n} packets: 7.1 layout");
146 }
147}
148
149/// Bytes after the last syncframe are padding, and padding decodes like the
150/// bare frames.
151#[test]
152fn padding_after_several_syncframes_is_ignored() {
153 let path = fixtures().join("ac3-5.1-48000.ac3");
154 if !present(&path) {
155 return;
156 }
157 let data = std::fs::read(&path).expect("fixture");
158 let all = frames(&data);
159 let mut packet: Vec<u8> = all[..3].concat();
160 packet.extend_from_slice(&[0u8; 7]);
161 let mut dec = Ac3Decoder::new();
162 let frame = dec.decode_packet(&packet).expect("padded packet decodes");
163 assert_eq!(frame.data[0].to_vec(), decode_each(&all[..3]));
164}
165
166/// A second frame of another channel layout cannot share one output frame.
167#[test]
168fn syncframes_of_different_layouts_in_one_packet_are_refused_by_name() {
169 let surround = fixtures().join("ac3-5.1-48000.ac3");
170 let stereo = fixtures().join("ac3-stereo-48000.ac3");
171 if !present(&surround) || !present(&stereo) {
172 return;
173 }
174 let a = std::fs::read(&surround).expect("fixture");
175 let b = std::fs::read(&stereo).expect("fixture");
176 let mut packet = frames(&a)[0].to_vec();
177 packet.extend_from_slice(frames(&b)[0]);
178 let mut dec = Ac3Decoder::new();
179 match dec.decode_packet(&packet) {
180 Err(Error::Unsupported { what, .. }) => {
181 assert!(what.contains("different channel layouts"), "{what}")
182 }
183 other => panic!("expected a named refusal, got {other:?}"),
184 }
185}
186
187/// `decode_frame` keeps its one-syncframe contract: the same packet is refused
188/// there and decoded by the packet call.
189#[test]
190fn decode_frame_still_takes_one_syncframe() {
191 let path = fixtures().join("ac3-stereo-48000.ac3");
192 if !present(&path) {
193 return;
194 }
195 let data = std::fs::read(&path).expect("fixture");
196 let all = frames(&data);
197 let packet = [all[0], all[1]].concat();
198 let mut dec = Ac3Decoder::new();
199 assert!(matches!(
200 dec.decode_frame(&packet),
201 Err(Error::Unsupported { .. })
202 ));
203}