aac(sbr): explicit-SBR AudioSpecificConfig writer, plus a wrap-to-mp4 probe instrument
Browse files at this commit9b44442f6adc7de3211dd0af1b4a7e2cf23bb8d6Parent: 7049a6c2 changed files +152 -0
crates/ec-aac/examples/wrap_sbr.rsadded+108 -0
| @@ -0,0 +1,108 @@ | ||
| 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> | |
| 28 | +use std::fs::File; | |
| 29 | + | |
| 30 | +use ec_aac::{AOT_SBR, AudioSpecificConfig, sf_index_for_rate, write_audio_specific_config}; | |
| 31 | +use ec_core::{ | |
| 32 | + AudioParameters, ChannelLayout, CodecId, CodecParameters, MediaParameters, Muxer, Packet, | |
| 33 | + StreamInfo, TimeBase, | |
| 34 | +}; | |
| 35 | +use ec_mp4::Mp4Muxer; | |
| 36 | + | |
| 37 | +fn 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 | +} |
crates/ec-aac/src/config.rsmodified+44 -0
| @@ -136,11 +136,25 @@ pub fn parse_audio_specific_config(data: &[u8]) -> Result<AudioSpecificConfig> { | ||
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /// Serialises the two-byte (plus SBR extension) AudioSpecificConfig. |
| 139 | +/// | |
| 140 | +/// `AOT_SBR`/`AOT_PS` are the *explicit* signalling form (§1.6.2.1's "SBR | |
| 141 | +/// config"): a core `sf_index`/`channel_config`, then the extension | |
| 142 | +/// (`SBR`-output) `sf_index` and an `extensionAudioObjectType` of AAC-LC | |
| 143 | +/// before the GASpecificConfig that form always carries -- without those | |
| 144 | +/// fields a decoder that trusts the object type (rather than only its | |
| 145 | +/// bare-ADTS implicit per-frame detection) has no extension rate to read and | |
| 146 | +/// never reports itself as HE-AAC. | |
| 139 | 147 | pub fn write_audio_specific_config(cfg: &AudioSpecificConfig) -> Vec<u8> { |
| 140 | 148 | let mut w = BitWriter::new(); |
| 141 | 149 | w.write_bits(u32::from(cfg.object_type), 5); |
| 142 | 150 | w.write_bits(u32::from(cfg.sf_index), 4); |
| 143 | 151 | w.write_bits(u32::from(cfg.channel_config), 4); |
| 152 | + if cfg.object_type == AOT_SBR || cfg.object_type == AOT_PS { | |
| 153 | + let ext_rate = cfg.extension_sample_rate.unwrap_or(cfg.sample_rate * 2); | |
| 154 | + let ext_sf_index = sf_index_for_rate(ext_rate).unwrap_or(3); | |
| 155 | + w.write_bits(u32::from(ext_sf_index), 4); | |
| 156 | + w.write_bits(u32::from(AOT_AAC_LC), 5); // extensionAudioObjectType | |
| 157 | + } | |
| 144 | 158 | w.write_bit(false); // frameLengthFlag: 1024 samples |
| 145 | 159 | w.write_bit(false); // dependsOnCoreCoder |
| 146 | 160 | w.write_bit(false); // extensionFlag |
| @@ -308,3 +322,33 @@ pub fn write_adts_header(header: &AdtsHeader) -> Vec<u8> { | ||
| 308 | 322 | w.write_bits(u32::from(header.raw_blocks.saturating_sub(1)), 2); |
| 309 | 323 | w.into_bytes() |
| 310 | 324 | } |
| 325 | + | |
| 326 | +#[cfg(test)] | |
| 327 | +mod tests { | |
| 328 | + use super::*; | |
| 329 | + | |
| 330 | + /// An explicit-SBR AudioSpecificConfig (object type 5) round-trips its | |
| 331 | + /// core AND extension sample rate through `write`/`parse` -- the write | |
| 332 | + /// side that a bare-ADTS ASC needs so a decoder trusting the object type | |
| 333 | + /// (rather than implicit per-frame detection) reports HE-AAC at the | |
| 334 | + /// doubled rate. | |
| 335 | + #[test] | |
| 336 | + fn explicit_sbr_config_round_trips() { | |
| 337 | + let cfg = AudioSpecificConfig { | |
| 338 | + object_type: AOT_SBR, | |
| 339 | + sample_rate: 22050, | |
| 340 | + sf_index: sf_index_for_rate(22050).unwrap(), | |
| 341 | + channels: 1, | |
| 342 | + channel_config: 1, | |
| 343 | + sbr_present: true, | |
| 344 | + ps_present: false, | |
| 345 | + extension_sample_rate: Some(44100), | |
| 346 | + }; | |
| 347 | + let bytes = write_audio_specific_config(&cfg); | |
| 348 | + let parsed = parse_audio_specific_config(&bytes).expect("parses"); | |
| 349 | + assert!(parsed.sbr_present, "explicit SBR must round-trip as SBR"); | |
| 350 | + assert_eq!(parsed.sample_rate, 22050); | |
| 351 | + assert_eq!(parsed.extension_sample_rate, Some(44100)); | |
| 352 | + assert_eq!(parsed.channels, 1); | |
| 353 | + } | |
| 354 | +} |