forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB03dc6e9Tahinli3 months ago178 commits

lib.rs96 lines, 4.0 KiBHistoryRawDownload
1//! AC-3 and Enhanced AC-3 decoding, from the ATSC A/52 standard.
2//!
3//! Two entry points, depending on how much of the format the caller needs:
4//!
5//! - [`Ac3Decoder`] takes syncframes and hands back interleaved `f32` audio
6//! frames, with the coded channel layout or a §7.8 downmix.
7//! - [`syncinfo::parse`], [`bsi::parse`] and [`eac3::bsi::parse`] read the
8//! headers alone, which is what a container needs to state a track's sample
9//! rate and channel count before decoding anything.
10//!
11//! Contracts worth knowing before implementing against this crate:
12//!
13//! - **Decode only.** Nothing here encodes AC-3; the format is a decode-side
14//! concern for this family.
15//! - **Truncated input is [`Error::NeedMore`]**, a broken bit stream is
16//! [`Error::Corrupt`], and a construct this build does not implement is
17//! [`Error::Unsupported`] naming *what* and *why*. No input panics — the bit
18//! reader bounds every read and the exponent and bandwidth fields are range
19//! checked before they index anything.
20//! - **Dynamic range compression is applied by default**, because §7.7.1 says a
21//! decoder shall: `dynrng` is the program provider's compression curve and
22//! leaving it out makes explosions 20 dB louder than the mix intended. The
23//! raw words stay visible on [`FrameInfo`], and [`Options::drc_scale`] turns
24//! it down or off.
25//! - **Dialogue normalisation is surfaced, never applied.** §7.6 puts
26//! `dialnorm` in the volume control, which is not in this crate.
27//! - **The noise this format asks for is on by default**: dither for zero-bit
28//! mantissas (§7.3.4) and the spectral extension blend (§E3.6.4.2.4). Neither
29//! sequence is specified, so two conformant decoders differ by exactly that
30//! noise; [`Options::dither`] turns both off when a comparison needs to
31//! isolate everything else.
32//!
33//! Annex E coverage: the adaptive hybrid transform (with its vector and
34//! gain-adaptive quantizers) and spectral extension are decoded; enhanced
35//! coupling and dependent substreams above 5.1 are refused by name rather than
36//! decoded wrongly. On a real Dolby Digital Plus stream that uses both, the
37//! coded bins land within 0.01% of ffmpeg's energy and the whole channel within
38//! 0.999 correlation once the two decoders' independent noise is taken out of
39//! the comparison ([`Options::dither`]).
40//!
41//! No unsafe, no allocation on the block path beyond the output buffer, and no
42//! dependencies beyond the family IR and DSP crates.
43
44#![forbid(unsafe_code)]
45#![warn(missing_docs)]
46// A bit stream parser indexes several per-channel and per-band arrays from one
47// loop variable, which is the domain's own notion of position; iterator
48// rewrites of those loops obscure which array is which.
49#![allow(clippy::needless_range_loop)]
50
51mod aht;
52mod aht_tables;
53
54pub mod bitalloc;
55pub mod bsi;
56pub mod eac3;
57pub mod exps;
58pub mod mantissa;
59pub mod syncinfo;
60pub mod tables;
61pub mod transform;
62
63mod decode;
64mod decoder;
65
66pub use decode::Syntax;
67pub use decoder::{Ac3Decoder, Downmix, FrameInfo, Options};
68pub use ec_core::Error;
69
70/// Samples per channel one audio block produces.
71pub use transform::BLOCK_SAMPLES;
72
73/// Blocks in an AC-3 syncframe; Enhanced AC-3 may send 1, 2, 3 or 6.
74pub const AC3_BLOCKS_PER_FRAME: usize = 6;
75
76/// Length in bytes of the syncframe starting at `data[0]`, for either syntax.
77///
78/// This is what walking an elementary stream needs — the frame length lives in
79/// `frmsizecod` for AC-3 and `frmsiz` for E-AC-3, in different places and
80/// different units. A short buffer is [`Error::NeedMore`]; anything that is not
81/// a syncframe is [`Error::Corrupt`].
82pub fn frame_size(data: &[u8]) -> ec_core::Result<usize> {
83 if data.len() < 6 {
84 return Err(Error::NeedMore);
85 }
86 if data[0] != 0x0B || data[1] != 0x77 {
87 return Err(Error::corrupt(
88 "AC-3: no syncword at the start of the frame",
89 ));
90 }
91 if data[5] >> 3 <= 10 {
92 Ok(syncinfo::parse(data)?.frame_size)
93 } else {
94 Ok(eac3::parse(&data[2..])?.frame_size)
95 }
96}