| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use std::path::{Path, PathBuf}; |
| 12 | |
| 13 | use ec_ac3::{Ac3Decoder, Error, eac3, frame_size}; |
| 14 | use ec_core::{Decoder, Frame, Packet, TimeBase}; |
| 15 | |
| 16 | fn fixtures() -> PathBuf { |
| 17 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/audio") |
| 18 | } |
| 19 | |
| 20 | |
| 21 | |
| 22 | fn 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 | |
| 35 | fn 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 | |
| 49 | |
| 50 | fn 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 | |
| 73 | fn 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 | |
| 84 | |
| 85 | fn 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] |
| 97 | fn 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] |
| 114 | fn 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 | |
| 130 | |
| 131 | #[test] |
| 132 | fn 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 | |
| 145 | assert_eq!(bytes.len() as u64, samples * 8 * 4, "{n} packets: 7.1 layout"); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | #[test] |
| 152 | fn 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 | |
| 167 | #[test] |
| 168 | fn 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 | |
| 188 | |
| 189 | #[test] |
| 190 | fn 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 | } |