Encoding E-AC-3¶
This page and Encoding AC-3 cover the two AC-3 family encoders. AC-4's encoder is on its page and TrueHD's, which is lossless and has no bit-rate or tool choices of this kind, on TrueHD and MLP.
E-AC-3: iclforge::ac3::eac3::FrameEncoder¶
iclforge/ac3/encoder/eac3_frame.hpp. Same shape, different container. E-AC-3 is not an AC-3 variant:
no crc1, an arbitrary 11-bit frmsiz instead of a size table (so the 44.1 kHz padding
alternation disappears), exponent strategies planned per frame and written in whichever of Annex
E's two forms the plan needs (a Table E2.10 code per channel, or per-block), and a syncframe
that can carry fewer than six blocks.
// Heap-allocated: FrameEncoder carries several KB of MDCT scratch/history
// state (PREfast's C6262).
auto encoder = std::make_unique<iclforge::ac3::eac3::FrameEncoder>(iclforge::ac3::eac3::FrameConfig{
.bitrate_kbps = 192,
.acmod = iclforge::ac3::Acmod::k2_0,
});
std::vector<std::vector<float>> pcm(2, std::vector<float>(iclforge::ac3::kSamplesPerFrame));
const auto views = views_of(pcm);
std::vector<std::byte> stream;
for (int frame = 0; frame < 31; ++frame) {
fill_tones(pcm, tones, frame, 48000.0);
const auto encoded = encoder->encode_frame(views);
if (!encoded) {
return 1;
}
stream.insert(stream.end(), encoded->begin(), encoded->end());
}
FrameConfig carries nearly everything EncoderConfig does, plus the Annex E tools. Three AC-3
fields do not carry over as they are: there is no cplendf — the coupling end frequency is derived
(the top of the coded spectrum, or from spxbegf when spectral extension is on, §E3.3.1) — no
cmixlev/surmixlev, which the mixing group below replaces, and no alternate_bsi, since
Annex D is AC-3 syntax.
chbwcod now behaves exactly as AC-3's does: −1, the default, asks the encoder to choose. It
used to default to a fixed 60 — the whole 23.7 kHz at every rate — on the reasoning that Annex
E's own tools take the high band over whenever the frame cannot afford it. They do, but only
below the per-channel rates at which auto turns them on (40 kbit/s for coupling in stereo,
56 for spectral extension), so a 192 kbit/s stereo stream ran neither and still coded every one
of the 253 mantissas. See Coded bandwidth for what the
shared decision does.
One field widens instead: FrameConfig::sample_rate also accepts the three Annex E half rates —
k24000, k22050, k16000 (24/22.05/16 kHz). For those the encoder writes fscod2 in place
of numblkscod (§E2.3.1.3, the block count is then implicitly six), and the reduced rate reuses
its double-rate parent's bit-allocation tables (§E2.3.1.4). The CLI maps the plain rate numbers
onto them. Classic AC-3 has no frmsizecod row for a reduced rate, so iclforge::ac3::FrameEncoder
rejects them outright.
| Field | Default | Notes |
|---|---|---|
numblkscod |
3 |
§E2.3.1.4, Table E2.4: how many 256-sample blocks a syncframe carries — 0/1/2 give 1/2/3 blocks (5.3/10.7/16 ms), the default 3 gives the usual six (32 ms). Every substream of an access unit must agree (AccessUnitEncoder's constructor refuses a mismatch). Below 3, Table E1.3 implies expstre = 1 and ahte = 0 — the frame-level (Table E2.10) exponent-strategy form and AHT are both unwritable, so requesting either together with a short numblkscod is refused by validate() rather than silently dropped. Not available at the three reduced rates, which spend numblkscod's own bits on fscod2 instead (see below) — validate() refuses that combination too. |
spx, spxbegf |
false, -1 |
Spectral extension (§E3.6). Above the extension frequency nothing is coded: the decoder copies a lower band up, blends noise, and scales to a transmitted envelope. Cheaper and cruder than coupling, so the two stack. |
spx_atten, spxattencod |
true, -1 |
The §E3.6.4.2.3 notch across the seam. Six bits per channel per frame. |
aht, gaqmod |
false, -1 |
Adaptive hybrid transform (§E3.4): a second 6-point DCT down each bin across the frame's six blocks. Decided per channel per frame — setting the flag permits it, not forces it. |
coupling, cplbegf |
false, -1 |
§E3.3. With spx also on, §E3.3.1 derives the coupling end frequency from spxbegf. |
enhanced |
false |
§E3.5: enhanced coupling instead of standard — 22 sub-bands, amplitude/angle/chaos-quantized coordinates and a phase-restoring reconstruction built on a full DFT, rather than a single per-band scale factor. Only meaningful with coupling also set (cpl+ecpl); combines with spx the same way standard coupling does. This encoder fits real amplitude/angle coordinates per band (an exact 2-variable linear least squares, since §3.5.5.4's reconstruction is linear in the complex gain the pair expresses) and chooses chaos by searching its 8 legal codes against the decoder's own deterministic de-correlation sequence. Two different channels forced into one narrow coupling band still cost quality — a single coordinate per band has a real, structural limit on what it can separate — but it is no longer the amplitude-only fit's all-or-nothing loss. |
transient_prenoise |
false |
§3.7 (tpn): a post-IMDCT correction that overwrites the pre-echo ahead of a detected transient with a synthesized copy of the clean audio just before it. Reuses the same transient detector block switching relies on, so it only has an effect on channels/frames that also block-switch. See Decoding for the 1536-sample decoder-side latency this introduces and the flush() call it requires. |
delta_allocation |
true |
§7.2.2.6 delta bit allocation, as for AC-3: the corrections chosen per run and the second fit that weighs them. false skips both - the first level of the encoders' effort axis, measured on the ESP32-S3 page - and the stream has dbaflde clear. The CLI accepts the command-wide delta=off option; inside eac3-encode's fourth positional [tools] argument, the spelling is nodelta, not tools=nodelta. |
fast_mdct |
true |
The §7.9.4 fast N/4-FFT forward MDCT instead of the direct §8.2.3.2 evaluation — a performance choice, not a coding tool: nothing in the bitstream's syntax changes, only how the coefficients were computed (verified ~3e-12 max relative error against the direct form; 0.000 dB SNR delta against an independent oracle at 192–448 kbps). false forces the direct reference form, which stays maintained as the oracle the fast path is validated against — the CLI spells that tools=nofastmdct. All three forward transforms accelerate — the long one and both halves of a block-switched pair, each down its own independently-derived fold (iclforge/ac3/core/mdct.hpp), and FrameConfig::fast_mdct reaches all of them. |
fgaincod |
-1 | §7.2.2.4 fast gain, Table 7.11. -1 leaves Table E1.4's implied 0x4 and writes no fgaincode element; 0–7 pins the code, which opens that element in every block (132 bits a frame at 5.1 with coupling), where AC-3 carries the code on the snroffst element it sends anyway. search moves it as one of its two axes. |
dither |
true |
§7.3.4 dithflag, decided per channel per block from content as for AC-3, except that a frame using spectral extension always dithers off; false pins it at 0. |
info |
none | std::optional<meta::BsiInfo>: the infomdat group (Table E1.2), the informational fields AC-3 carries in bsi — see Bit stream information. |
search |
kNone |
Per-frame search over §7.2.2's transmitted bit-allocation parameters against iclforge::ac3::quality's decoded-domain distortion, instead of the fixed dbpbcod 3 that an earlier sweep measured its way to on average. CBR only (FrameConfig::vbr unset) - silently inert under VBR/ABR, the same documented boundary the encoder already draws around AHT streams, not a rejected configuration. kDistortion only: kPerceptual is accepted but inert too, on the same grounds Decision search already found it for AC-3. Two axes, the same pair AC-3's search moves: dbpbcod over {kAllocCodes' 3, Table E1.4's 2}, and fgaincod over iclforge::ac3::rate_adaptive_fgaincod's measured code plus §8.2.12's own default. Unlike AC-3's, the fgaincod candidates are not free - baie carries no fast gain, so a non-default code opens the per-block fgaincode element (frmfgaincode 1) and buys its masking curve out of the mantissa budget - so each candidate is scored after a refit against its own side-info cost rather than against the incumbent's. Measured on real CC0 stereo material at 96-640 kbit/s, dbpbcod alone was negligible everywhere tried, which is what this axis was added to move. CLI: search=distortion/search=perceptual/search=off. |
mixing |
none | The mixmdate group (Table E1.2). E-AC-3 dropped cmixlev/surmixlev from bsi entirely, so without this the stream carries no downmix levels at all. |
strmtyp, substreamid, chanmap, last_dependent |
independent, 0, none, false | Substream identity. Set by AccessUnitEncoder; you rarely touch these directly. |
oba_complexity_index |
none | TS 103 420 §8.3 object count in addbsi. This is the marker FFmpeg keys its "Dolby Digital Plus + Dolby Atmos" report off. |
The in-repo decoder reads every one of these tools, individually or stacked together, at every layout including 7.1.4 — see Decoding for the decode-side picture, and the verification-gap table in Validation for which tools have an external oracle.
How auto chooses¶
FrameConfig::auto_tools (the CLI's tools=auto) hands the tool set to the encoder instead of
naming it. It overrides coupling/spx/aht rather than combining with them: when it is set
they are not read at all, while cplbegf/spxbegf/gaqmod still steer the geometry of whatever
it does turn on.
It decides from two things — the per-channel bitrate, and the frame itself. The content half is measured from the MDCT coefficients the transform has already produced, so it costs a pass over the affected region and no second transform:
| Measure | What it is | What it decides |
|---|---|---|
| coupling fit | How much of the coupling region's energy survives the decoder's own reconstruction of it. §7.4.1's shared channel is the coefficient sum and the transmitted coordinate restores each band's energy, so the residual against that rank-one shape is what coupling costs — this is evaluated, not estimated. 1.0 is a perfect fit (every channel already a scalar multiple of the sum, which is what near-mono material looks like above 8 kHz); independent channels of equal level land at 2/sqrt(n) - 1, which is 0.41 for a stereo pair and slightly negative for five. |
Whether to couple, and how far above the fixed rate ceiling coupling may reach. |
| extension energy share | How much of the frame's energy sits above the extension frequency. | Whether to extend: the ceiling runs from 110 kbit/s per channel where the top end is nearly empty down to 55 where it carries real content. |
The rules that fall out of those:
- Spectral extension is on below a ceiling that moves with the energy share above. A frame whose top end is nearly empty — which is most real programme material — gets synthesis at up to 96 kbit/s per channel, because what synthesis replaces there is a band the coder was about to spend nothing on and drop. A frame whose top end carries real content loses it by 64.
- Coupling needs a fit of 0.99 or better and a region at least four sub-bands wide.
§E3.3.1 derives
cplendffromspxbegf, so wherever synthesis is in use coupling is left one or two sub-bands — 12 or 24 coefficients — which cannot repay a coordinate per band per channel plus a shared channel the allocator buys bits for. In practiceautonow couples rarely. - AHT is always permitted and decided per channel per frame by whether the six blocks look alike, which was already a content decision.
- Enhanced coupling and transient pre-noise processing are never chosen by
auto— see Whatautowill not choose below.
Under VBR there is no fixed target rate, so VbrConfig::nominal_kbps (or max_kbps, or 192)
stands in for it. The content half is unaffected: it reads the frame either way, which is most of
what makes the VBR case work at all.
What auto will not choose¶
Both of these are fully implemented, decode correctly in this project's own decoder at every
layout, and have their own CI legs. Neither is in auto's set, for different reasons.
Enhanced coupling (§E3.5) is not a quality problem. Measured on real programme material —
six 12 s excerpts of a 5.1 theatrical mix at six (layout, rate) points — it scores above
standard coupling on ViSQOL MOS-LQO at every one of them: +0.54 MOS-LQO at 96 kbit/s stereo, +0.31 at 128 and +0.18 at 192, and +0.78 / +0.55 / +0.16 at 192 / 256 / 384 kbit/s 5.1. Every SNR trend
row records it as a loss, and both are true: a phase-restoring reconstruction built on a full DFT
does not preserve the waveform, it preserves what the waveform sounded like. (Against auto's
own set the margin is smaller — once spectral extension is chosen properly it has already taken
most of the band coupling would have worked on — which is a reason to read the two coupling
reconstructions against each other rather than against the whole tool set.)
What rules it out is interoperability. FFmpeg's Annex E parser has no model of §E3.5's syntax at
all — it does not decline an enhanced-coupling stream, it misreads it and reports a corrupt
frame — and auto is the tool set a caller gets for asking for nothing in particular, so it has
to stay decodable by the decoders that exist. cpl+ecpl still asks for it explicitly, and on a
closed pipeline with a decoder known to read it, the measurements say to.
Transient pre-noise processing (§3.7) does not pay at all, and the measurement is unambiguous. It overwrites the decoded audio ahead of a transient with a copy of the audio 512 samples earlier. That substitution's error is a property of the material, not of the coder: over exactly the samples it touches it measures 20.7–22.5 dB at every bitrate tried, flat. The coder's own error over those same samples is 11.9 dB at 96 kbit/s stereo and −3.3 dB at 256 — it improves with rate, and it is already the better of the two at the lowest rate this encoder supports. So the correction lands between 6.5 and 24 dB worse than leaving the audio alone, everywhere it fires, and the gap widens as the rate rises. It is not a bit-allocation effect: outside its own footprint the two decodes are bit-identical. Perceptually it is a no-op — MOS-LQO matched the untreated encode to within 0.01 in every row measured.
These figures predate a decoder fix: the decoder then applied each correction one block (256
samples) ahead of where A/52 and Dolby's own decoder apply it, so what was measured is a
correction over the clean audio before the pre-noise rather than over the pre-noise itself. Where
it lands now, tools/ci/quality_race.py's own-decoder rows score the stereo stream at 192 kbit/s
2.1 dB closer to its source than they did (26.2 against 24.0 dB) and the 5.1 stream at 256
kbit/s 0.1 dB closer. The comparison above has not been repeated.
The mechanism is that block switching gets there first. §3.7 exists to clean up pre-echo, and this encoder gates the correction on the same transient detector that switches to short transforms — so it fires exactly where the short transforms have already confined the pre-echo, and substitutes earlier audio for audio that was not damaged. Treat it as a reference-correctness tool: it demonstrates the syntax and the §3.7.2 reconstruction, and there is no measured rate or content at which it improves the result.
Block switching (§8.2.2/§7.9) is automatic here too — no config field. A channel that switches anywhere in the frame is excluded from both coupling (same reasoning as AC-3's) and, for this generation only, from AHT for that frame: AHT's own "stationary" premise (§E3.4, the opposite of what triggered the switch) already selects against a switching channel most of the time, but the exclusion is explicit rather than relying on that correlation.
Rematrixing (§7.5.3) is automatic too, acmod 2/0 only, no config field — the same minimum-power
decision AC-3's own encoder makes (see Encoding AC-3), over the same Table 7.25
bands. Annex E §3.3's "Modifications to Previously Defined Parameters" only changes how many of
those bands are active (nrematbd, accounting for coupling, enhanced coupling and spectral
extension all separately taking over the top of the spectrum) — the boundaries and the decision
rule are untouched, so nothing here needed reinventing beyond that band count and clamping the
last active band to wherever this channel's own coding actually stops.
The bit allocation parameters are transmitted rather than inherited (bamode 1). Table E1.4's
own bamode == 0 defaults are not §8.2.12's basic-encoder set — most of the two agree, but
floorcod is 0x7 against §8.2.12's 4 — so "inherit" was never the same thing as "what AC-3
does". Sending them costs baie plus eleven bits in block 0 and one bit in each of the other
five, 17 a frame; it buys dbpbcod 3, the one departure the AC-3 encoder measured its way to
(see Encoding AC-3), which lifts the masking curve over bands quieter than
dbknee and sends their bits to bands that hold energy. floorcod stays at 0x7: it is the
lowest of the eight and never binds, which the same sweep confirmed here as it had for AC-3.
Dither substitution (§7.3.4) works exactly as it does on AC-3 — see
Encoding AC-3 for the rule — with dithflage set so the
per-channel flags are transmitted rather than defaulting to on. One narrowing is specific to this
generation: dither is held off entirely for any frame that uses spectral extension. The encoder
holds a reconstruction of what the decoder will produce there, because the extension bands are
scaled to match the copy source's own energy, and the decoder's dither values are not
reproducible from the encoder's side — the sequence a bin receives depends on how many zero-bit
bins the decoder walked before it, across every stream and block. Rather than shadow that
traversal order, the tool that would disturb it is switched off. An AHT channel is left out of
the judgement too: its zero-hebap bins reconstruct as literal zero whatever the flag says.
FrameConfig::dialnorm2 (see "Dual mono" in Encoding AC-3) works exactly
the same way here: set it alongside dialnorm when acmod is kDualMono. Dual mono is always a
lone independent substream with no dependents — 1+1 has no bed/dependent split to make — so
AccessUnitEncoder gives Ch2 its own RangeController/HeavyCompressor too, measured on the
independent substream's own two channels the same way it measures Ch1's. It has no VBR
implications either: dual mono is orthogonal to CBR/VBR, since vbr only changes how the frame's
size is decided, not how many programmes it carries.
Variable bit rate: FrameConfig::vbr¶
E-AC-3's frmsiz states the frame's word count directly rather than indexing a table, so unlike
AC-3 a frame is free to be a different size than the one before it. Setting vbr switches a
FrameConfig from CBR (size fixed from bitrate_kbps) to VBR (size follows the content, at a
chosen quality):
iclforge::ac3::eac3::FrameEncoder encoder{{
.bitrate_kbps = 192, // not read once vbr is set — see below
.acmod = iclforge::ac3::Acmod::k2_0,
.vbr = iclforge::ac3::eac3::VbrConfig{
.quality = 0.4,
.max_kbps = 320, // optional ceiling
},
}};
VbrConfig field |
Default | Notes |
|---|---|---|
quality |
0.5 |
[0, 1], linearly maps onto the encoder's own SNR-offset search space. Encoder-relative, not a perceptual scale — and not linear in bit cost: cost rises steeply in roughly the top third of the range, so a high quality with no max_kbps bound will often refuse ordinary multi-channel material outright (FrameError::kInvalidBitrate) rather than produce an oversized frame. Not read at all when abr is set — see below. |
min_kbps, max_kbps |
none, none | Optional hard bounds, same unit as bitrate_kbps. When the quality target would need more words than max_kbps allows, the encoder falls back to the same search CBR uses, budgeted against the ceiling — so a bounded VBR frame is never worse than the best CBR could do at that rate. min_kbps is a pure floor: finish_frame's own padding covers the gap. They bound each individual frame, so they compose with abr rather than competing with it. |
nominal_kbps |
none | Drives the cplbegf/spxbegf frequency defaults in place of a fixed target rate. Unset it resolves to abr->target_kbps if there is one, then max_kbps if set, else kVbrDefaultNominalKbps (192) — under ABR the contracted average is the stand-in, where max_kbps is only the ceiling one frame may peak to. A caller who wants today's CBR tool behaviour at some quality supplies the same number they would have passed as bitrate_kbps. |
abr |
none | std::optional<AbrConfig>. Holds a long-run average rate while each frame's size still moves with the content — what a streaming ladder rung or a DVB mux contracts for, and what neither CBR nor free-running VBR delivers. See below. |
bitrate_kbps itself is not read on the encode path at all once vbr is set.
Average bit rate: AbrConfig¶
Set VbrConfig::abr and the encoder holds one composite SNR offset across frames and steers it —
up while the stream is running under its target, down while it is running over — so a quiet frame
stays cheap and a busy one is allowed to cost more, with the average landing where it was asked
to. Underneath that, window_frames consecutive frames pool one budget as a hard ceiling, so no
window can overrun whatever the offset is doing.
AbrConfig field |
Default | Notes |
|---|---|---|
target_kbps |
192 | The long-run average, same unit and meaning as bitrate_kbps. |
window_frames |
32 | How many consecutive frames share one pooled budget. At 48 kHz a six-block frame is 32 ms, so the default holds the average over about a second — long enough for a bar of music or a spoken phrase to borrow from its neighbours, short enough that a mux's own buffer model still recognises the result. 1 pools nothing, which pins every frame to one frame's share and makes ABR behave as CBR; 0 is refused by validate(). |
quality is not read under ABR at all: the two are different rate controls — quality fixes the
offset, ABR's whole job is to move it — and the stream's first frame seeds the offset from its own
budget search rather than from a number a caller guessed.
AccessUnitConfig needs no separate VBR field: each substream's own FrameConfig::vbr carries
what it needs, and plan::eac3_config() shares one VbrConfig across every substream of a
plan::Plan, halving min_kbps/max_kbps/nominal_kbps/abr->target_kbps for dependents the
same way it already halves bitrate_kbps — substreams occupy one frame period, not one frame.
abr->window_frames is a count of frames rather than a rate, so it carries over unchanged.
Silent frames (build_silent_frame) and AC-3 (plan::Codec::kAc3) both reject a vbr config
outright: silence has no content to size a quality target against, and AC-3's frmsizecod has no
free word count to vary in the first place.
Wide layouts: iclforge::ac3::eac3::AccessUnitEncoder¶
Anything past 5.1 rides in dependent substreams beside a self-sufficient 5.1 bed. Every
substream codes the same 1536 samples of the same programme; a dependent contributes only its
own channels, its chanmap, and its share of the bit rate.
// The bed is self-sufficient: a decoder that reads only the independent
// substream gets a complete 5.1 programme.
iclforge::ac3::eac3::AccessUnitConfig config;
config.independent = {
.bitrate_kbps = 384,
.acmod = iclforge::ac3::Acmod::k3_2,
.lfe = true,
};
// Each dependent gets its own slice of the rate — substreams share a frame
// period, not a frame — and a Table E2.5 chanmap naming where its channels
// belong. Per §E3.8.2 the locations that collide with the bed replace it
// and the rest extend the layout.
config.dependents.push_back({
.bitrate_kbps = 192,
.acmod = iclforge::ac3::Acmod::k2_2,
.chanmap = iclforge::ac3::eac3::chanmap::k71Rear, // Ls, Rs, Lrs, Rrs
});
config.dependents.push_back({
.bitrate_kbps = 192,
.acmod = iclforge::ac3::Acmod::k2_2,
.chanmap = iclforge::ac3::eac3::chanmap::kTopQuad, // Vhl, Vhr, Lts, Rts
});
iclforge::ac3::eac3::AccessUnitEncoder encoder{config};
Channels are grouped by substream in transmission order: the independent's first in Table 5.8
order with LFE last, then each dependent's in the order its chanmap names them.
encoder.channel_count() is the total, and the span count encode_access_unit expects.
const auto unit = encoder.encode_access_unit(views);
// unit->bytes is the wire order already; substream_bytes records the
// per-substream boundaries, which crc2 is computed over.
stream.insert(stream.end(), unit->bytes.begin(), unit->bytes.end());
Full program: examples/encode_eac3.cpp.
Constraints: at most 8 dependents; a dependent may not disagree with its parent on sample
rate; and the locations a chanmap names must add up to the channels its acmod and lfeon
actually code, or you get FrameError::kInvalidChannelMap. A single dependent codes at most 5
full-bandwidth channels, which is why 7.1.4 — six channels beyond the bed — needs two.
Useful chanmap constants (iclforge/ac3/core/eac3_tables.hpp, Table E2.5):
| Constant | Channels | Gives you |
|---|---|---|
k71Rear |
Ls, Rs, Lrs, Rrs | 7.1 (the surrounds replace the bed's, the rears are new) |
k512Height |
Vhl, Vhr | 5.1.2 |
kTopQuad |
Vhl, Vhr, Lts, Rts | 5.1.4 |
k71Rear + kTopQuad |
both of the above | 7.1.4, in two dependents |
chanmap::expand(map) turns a map into a Layout you can iterate, and chanmap::name
gives each location's short name.
Latency¶
The four terms and what each one means are set out in the AC-3 page's Latency section; everything there applies here too, because E-AC-3 uses the same transform, the same default frame length and the same lookahead-free block-switch decision. What differs is one tool, one shape and one option.
transient_prenoise costs 1536 samples of decoder hold-back. §3.7's correction reaches
across frame boundaries — back up to 1528 samples from a transient that may itself lie in a later
frame — so a decoder can only realize it while it still holds that much audio, which at six blocks
a syncframe means returning frame N−1's PCM from the call that supplies frame N. That is 1536
samples whatever the syncframe length, permanently, from the first frame that actually uses the
tool onward:
iclforge::ac3::eac3::FrameConfig config{.bitrate_kbps = 448, .acmod = iclforge::ac3::Acmod::k3_2, .lfe = true};
config.transient_prenoise = true;
iclforge::ac3::eac3::FrameEncoder encoder{config};
encoder.latency_samples(); // 3328 = 1536 frame + 256 transform + 1536 hold-back
eac3::eac3_latency(config) answers the same question without building an encoder, which is
what a pipeline sizing its buffers up front actually needs. Every other Annex E tool — AHT,
coupling, enhanced coupling, spectral extension — is a different way of coding the same frame's
coefficients and adds nothing: AHT packs a channel's six blocks into block 0 rather than looking
ahead of the frame, and spx and coupling reconstruct within the block they arrive in.
The hold-back engages when the tool does, not when it is configured. This encoder reuses the
blksw decision rather than running a second detector, so a stream that never block-switches
never sets transproce and never holds anything back — Eac3Decoder::latency_samples() reports
0 until it does, and 1536 from then on.
An access unit's budget is the worst of its substreams'. Every substream codes the same 1536
samples of the same program, so the frame and transform terms are shared rather than summed; the
hold-back is per-substream, and decode_access_unit cannot assemble a program until its slowest
substream has released. AccessUnitEncoder::latency() reports that.
Short syncframes (numblkscod 0–2)¶
Annex E §E2.3.1.4 allows a syncframe to carry 1, 2 or 3 blocks instead of 6, and
FrameConfig::numblkscod selects it on the encode side as well as the decode
side. Framing is by far the biggest latency term, so this is the largest single reduction
available: a shorter syncframe is a shorter frame, and encode_frame then wants
samples_per_frame() samples per channel — 256, 512 or 768 — rather than the 1536 a six-block
frame takes. What it costs is the whole bsi/audfrm header repeated that much more often,
which at a fixed bit rate comes straight out of the mantissas, plus the Table E1.3 shortcuts the
numblkscod row in the FrameConfig table above sets out (expstre implied 1, ahte implied
0, both refused by validate() rather than silently dropped).
eac3_latency() counts it. LatencyBudget::frame_samples is samples_per_frame() — 256,
512, 768 or 1536 — so latency_samples() on a one-block configuration is 512 (256 of frame and
256 of transform), and 768, 1024 and 1792 for two, three and six blocks. The hold-back does not
shrink with the syncframe: it is 1536 samples whatever the length, six one-block syncframes where
a six-block configuration holds one (libs/ac3/tests/decoder/test_latency.cpp pins both).
More than one programme¶
Dependent substreams widen one programme. §E2.3.1.2 also allows up to eight independent substreams (I0–I7) in one elementary stream, and that is a different thing entirely: each is a self-sufficient programme with its own layout, rate and metadata, and a receiver plays one of them. Broadcast DD+ uses it for the services A/52 §5.4.2.2 names — a second language, an audio description, a commentary.
AccessUnitConfig::additional carries them. independent/dependents stay the first
programme; each entry of additional is a ProgrammeConfig with an independent substream and
dependents of its own.
iclforge::ac3::eac3::AccessUnitConfig config;
config.independent = {.bitrate_kbps = 448, .acmod = iclforge::ac3::Acmod::k3_2,
.lfe = true, .dialnorm = 27};
// I1: a mono commentary, levelled independently of the mix it plays against.
config.additional.push_back({.independent = {.bitrate_kbps = 96,
.acmod = iclforge::ac3::Acmod::k1_0,
.dialnorm = 20}});
substreamid is assigned by position — the first programme is I0, additional[0] is I1 — the
same way a dependent's id comes from its position in dependents; FrameConfig::substreamid is
not read.
encode_access_unit takes every programme's channels in one call, the first programme's first,
in the same order the substreams go on the wire. Rates add rather than divide: substreams share
a frame period, not a frame, so access_unit_words is the sum across every programme.
Each programme keeps its own §7.7 measurement — its own RangeController/HeavyCompressor,
measured on its own independent substream — because dialnorm and DRC are properties of a
programme. Sharing one measurement across two would level a commentary by the main mix. Per
programme, too: the §E3.8.2 16-channel cap, and the metadata a FrameConfig carries.
A programme with dependents (ProgrammeConfig::dependents, or the top-level dependents above)
gets a second, independent HeavyCompressor when heavy is set: §E3.8.5 gives the LAST
dependent's compr to the whole programme, so that word has to answer for every rendered
channel, not the independent substream's own five — AccessUnitEncoder measures it from the
complete rendered programme, folded the way iclforge::ac3::OutputStage's rendered-layout overload seats a
wide layout (see Decoding). The independent's own word, from its
own channels alone, still goes out too, for a receiver that decodes only the 5.1 bed.
Constraints: at most 8 programmes; every substream of every programme must agree on the sample rate, since they all code the same frame period. An Atmos EMDF container still rides in the last substream of the first programme (TS 103 420 §8.2) — the objects belong to a programme, so a later programme's substreams are never it.
The CLI authors up to seven further programmes with programme2=<file> through programme8=<file>,
each with its own programmeN-layout=, programmeN-bitrate= and the rest of the primary
programme's own metadata keys via programmeN-<field>= (dialnorm, bsmod, the whole mixmdate
group); see docs/forge/cli/metadata-options.md.
One caveat worth knowing before you ship such a stream: FFmpeg cannot read it at all, and not just the second programme — see docs/verification.md.
See also: Metadata — mix-level and DRC fields shared with AC-3, plus the
E-AC-3-only mixing group; Encoding AC-3 — the single-substream base case,
and the full latency budget.