forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB03dc6e9Tahinli3 months ago178 commits

wrap_sbr.rs108 lines, 4.3 KiBHistoryRawDownload
1//! Wraps a bare ADTS stream (the SBR patch-map probes'
2//! `scripts/aac-tables/sbrpatchmap.py` output) in an mp4 container carrying
3//! an EXPLICIT-SBR AudioSpecificConfig as its `esds` -- the instrument
4//! round-30's blocker needed: a bare `.aac` file carries no out-of-band
5//! AudioSpecificConfig at all, so a decoder that trusts one (rather than
6//! only its bare-ADTS implicit per-frame SBR detection) has nothing to arm
7//! HE-AAC output from and locks to core-rate.
8//!
9//! Matroska was tried first (`MatroskaMuxer`, dev-dep already proven to
10//! round-trip `CodecPrivate` byte-for-byte in
11//! `crates/ec-aac/tests/sbr_real_library.rs`) but the reference decoder's
12//! Matroska demuxer reads the *codec ID string* ("A_AAC" vs.
13//! "A_AAC/MPEG4/SBR") for its reported profile, not just the `CodecPrivate`
14//! bytes our `MatroskaMuxer` writes generically -- it still upgraded the
15//! reported sample rate to the extension rate (proving it DOES parse
16//! `CodecPrivate` for that), but kept reporting profile "LC". mp4's `esds`
17//! carries the same explicit-SBR AudioSpecificConfig and needs no such
18//! sibling string, matching the real `~/Music/reeln.mp4` fixture's
19//! own container exactly.
20//!
21//! `stream()`/`stream_one_band()`'s probe frames are full ADTS (7-byte
22//! header + raw_data_block); this strips the header off each frame and
23//! remuxes the raw access units into an mp4 track whose `esds` is
24//! `write_audio_specific_config` with `object_type = AOT_SBR` and an
25//! explicit `extension_sample_rate`.
26//!
27//! usage: wrap_sbr_mkv <in.aac> <core_rate> <ext_rate> <channels> <out.mp4>
28use std::fs::File;
29
30use ec_aac::{AOT_SBR, AudioSpecificConfig, sf_index_for_rate, write_audio_specific_config};
31use ec_core::{
32 AudioParameters, ChannelLayout, CodecId, CodecParameters, MediaParameters, Muxer, Packet,
33 StreamInfo, TimeBase,
34};
35use ec_mp4::Mp4Muxer;
36
37fn main() {
38 let args: Vec<String> = std::env::args().collect();
39 assert_eq!(
40 args.len(),
41 6,
42 "usage: wrap_sbr_mkv <in.aac> <core_rate> <ext_rate> <channels> <out.mkv>"
43 );
44 let data = std::fs::read(&args[1]).expect("input readable");
45 let core_rate: u32 = args[2].parse().expect("core_rate is a number");
46 let ext_rate: u32 = args[3].parse().expect("ext_rate is a number");
47 let channels: u16 = args[4].parse().expect("channels is a number");
48
49 // Strip each ADTS frame down to its raw access unit (no header, no CRC).
50 let mut aus = Vec::new();
51 let mut at = 0usize;
52 while at + 7 <= data.len() {
53 let Ok(header) = ec_aac::parse_adts(&data[at..]) else {
54 break;
55 };
56 let end = (at + header.frame_length).min(data.len());
57 aus.push(data[at + header.header_len..end].to_vec());
58 at = end;
59 }
60 assert!(!aus.is_empty(), "no ADTS frames found in {}", args[1]);
61
62 let sf_index = sf_index_for_rate(core_rate).expect("core_rate is a valid AAC rate");
63 let channel_config = channels as u8;
64 let asc = write_audio_specific_config(&AudioSpecificConfig {
65 object_type: AOT_SBR,
66 sample_rate: core_rate,
67 sf_index,
68 channels,
69 channel_config,
70 sbr_present: true,
71 ps_present: false,
72 extension_sample_rate: Some(ext_rate),
73 });
74
75 let time_base = TimeBase::from_rate(core_rate);
76 let layout = if channels == 1 {
77 ChannelLayout::Mono
78 } else {
79 ChannelLayout::Stereo
80 };
81 let mut params = CodecParameters::new(CodecId::Aac);
82 params.extradata = Some(asc.into());
83 params.media = MediaParameters::Audio(AudioParameters {
84 sample_rate: core_rate,
85 layout,
86 format: None,
87 bits_per_sample: None,
88 });
89 let mut info = StreamInfo::new(0, time_base, params);
90 info.default = true;
91
92 let out = File::create(&args[5]).expect("output creatable");
93 let mut muxer = Mp4Muxer::new(out).expect("mp4 muxer opens");
94 muxer.add_stream(info).expect("stream declared");
95 for (i, au) in aus.iter().enumerate() {
96 let pts = i as i64 * 1024;
97 let packet = Packet::new(0, time_base, au.as_slice())
98 .with_pts(pts)
99 .with_duration(1024);
100 muxer.write_packet(&packet).expect("packet written");
101 }
102 muxer.finish().expect("finished");
103 eprintln!(
104 "wrote {} ({} frames, core={core_rate}Hz ext={ext_rate}Hz ch={channels})",
105 args[5],
106 aus.len()
107 );
108}