forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

adts_to_pcm.rs61 lines, 2.6 KiBHistoryRawDownload
1//! Decodes an ADTS file (argv[1]) to raw interleaved f32le PCM on stdout, for
2//! the patch-map probe scripts (`scripts/aac-tables/sbrpatchmap.py`) to read
3//! our own decoder's output through the exact same FFT/comb-reading code as
4//! the reference decoder's, with no format translation in between.
5use std::io::Write;
6
7fn main() {
8 let path = std::env::args()
9 .nth(1)
10 .expect("usage: adts_to_pcm <file.aac>");
11 let data = std::fs::read(&path).expect("file readable");
12 // Plain `AacDecoder::new()` never enables SBR (it only turns on from an
13 // explicit AudioSpecificConfig's `sbr_present`, per `with_config`) --
14 // the probe streams here are raw same-rate-SBR ADTS with no ASC, so
15 // build one directly rather than parsing bytes we don't have.
16 let same_rate_sbr = std::env::var("EC_AAC_SBR_SAME_RATE").is_ok();
17 let channels: u16 = std::env::var("EC_AAC_SBR_CHANNELS")
18 .ok()
19 .and_then(|s| s.parse().ok())
20 .unwrap_or(1);
21 // Round-32: built via the SAME write_audio_specific_config +
22 // with_config_bytes round trip `wrap_sbr` uses (AOT_SBR, core rate
23 // 22050 = ADTS sf_index 7's SAMPLE_RATES entry, extension 44100) so this
24 // path and the reference's wrap_sbr-wrapped mp4 read byte-identical ASC
25 // semantics -- the prior hand-built config (object_type=2/AAC-LC,
26 // sample_rate=44100 for what is actually the 22050 Hz *core*) diverged
27 // from wrap_sbr's ASC, which is why the sanity gate (ours vs
28 // build_patches) was failing independent of any patch-map bug.
29 let mut decoder = if same_rate_sbr {
30 let asc = ec_aac::write_audio_specific_config(&ec_aac::AudioSpecificConfig {
31 object_type: ec_aac::AOT_SBR,
32 sample_rate: 22050,
33 sf_index: 7,
34 channels,
35 channel_config: channels as u8,
36 sbr_present: true,
37 ps_present: false,
38 extension_sample_rate: Some(44100),
39 pce: Vec::new(),
40 });
41 ec_aac::AacDecoder::with_config_bytes(&asc).expect("asc parses")
42 } else {
43 ec_aac::AacDecoder::new()
44 };
45 let mut at = 0usize;
46 let stdout = std::io::stdout();
47 let mut out = stdout.lock();
48 while at + 7 <= data.len() {
49 let header = match ec_aac::parse_adts(&data[at..]) {
50 Ok(h) => h,
51 Err(_) => break,
52 };
53 let end = (at + header.frame_length).min(data.len());
54 if let Ok(frame) = decoder.decode(&data[at..end], None) {
55 for v in &frame.samples {
56 out.write_all(&v.to_le_bytes()).expect("stdout writable");
57 }
58 }
59 at = end;
60 }
61}