Channel plans & routing¶
iclforge::ac3::plan::LayoutId only ever names eight hand-picked combinations (mono through 7.1.4). The
two APIs on this page are the general machinery underneath: any Table E2.5 channel selection at
all, and any number of separate sources feeding it.
Custom channel selections: Plan::custom_locations¶
iclforge/ac3/encoder/plan.hpp and iclforge/ac3/core/eac3_tables.hpp. iclforge::ac3::eac3::chanmap::allocate partitions
an arbitrary set of Table E2.5 locations into a bed (the widest Table 5.8 acmod whose own
locations all fit) and however many dependents the remainder needs. Plan::custom_locations
is the front door onto it: set it and it overrides layout entirely.
const auto locations = iclforge::ac3::plan::parse_channels("L,C,R,Ls,Rs,LFE,Ts,Lw,Rw");
const iclforge::ac3::plan::Plan plan{
.codec = iclforge::ac3::plan::Codec::kEac3,
.custom_locations = *locations,
.bitrate_kbps = 640,
};
const auto channel_plan = iclforge::ac3::plan::resolve(plan); // the bed + dependent chanmaps
const auto config = iclforge::ac3::plan::eac3_config(plan); // AccessUnitConfig, ready to encode
iclforge::ac3::eac3::AccessUnitEncoder encoder{config};
Full program: examples/custom_layout.cpp
— 5.1 plus a top-surround channel and a front-wide pair, a layout no LayoutId names, built and
encoded the same way a caller with an unusual speaker set would.
parse_channels/format_channels round-trip a comma-separated list of Table E2.5 names
(iclforge::ac3::eac3::chanmap::name()'s own spelling, e.g. "Ls", "LFE2"); a pair location (Lc/Rc,
Lrs/Rrs, Lsd/Rsd, Lw/Rw, Vhl/Vhr, Lts/Rts) must name both members, since Table E2.5 has no bit
for one alone. allocate() fails with AllocationError::kNoBedFit if no Table 5.8 acmod's own
locations are a subset of the request, or kOrphanLfe2 if LFE2 is asked for with no
full-bandwidth channel left to share its substream.
iclforge::ac3::plan::coded_channels(plan)/coded_channel_names(plan) work over a resolved
ChannelPlan exactly the way they do over a named LayoutId — a custom selection and a named
layout go through the same reporting path.
Plan::codec can also be Codec::kAc4, for a front end that hands the plan's channels, rate and
metadata to iclforge::ac4::Encoder (AC-4); iclforge::ac3 itself encodes AC-3 and E-AC-3 only, so
eac3_config(plan) has nothing to say about it. iclforge::ac3::plan::validate(plan) holds an AC-4 plan to
what the AC-4 encoder takes from a plan: the named layouts mono, stereo and 5.1, or a
custom_locations selection that is mono, stereo, 5.0 or 5.1, at 48 or 44.1 kHz, with no vbr
(PlanError::kLayoutNotInAc4, kSampleRateNotInAc4 and kVbrNeedsEac3). forge ac4-encode
does not go through a plan: it takes the immersive layouts, and the experimental 7.X and 3.0 ones,
from the WAV file's own channels.
One source that states its speakers¶
A width is not a layout: three channels are 3/0 as FL FR FC and 2/1 as FL FR BC, and four are 2/2
as FL FR BL BR and 3/1 as FL FR FC BC. route(target, channels, ...) has only the count, so it
reads three as L R C and four as L R Ls Rs, which is right for the files that are those and wrong
for the rest. A WAVE_FORMAT_EXTENSIBLE header's dwChannelMask says which, and four functions
carry it from the file to an encoder the way forge and Forge GUI both do:
using namespace iclforge::ac3;
const auto read = io::read_wav(path); // WavData::channel_mask
const auto stated = plan::wav_mask_locations(read->channel_mask, read->channels.size());
if (stated) { // nullopt: states none, or none usable
if (const auto chosen = plan::source_layout(plan::Codec::kAc3, *stated)) {
if (chosen->layout) { p.layout = *chosen->layout; }
else { p.custom_locations = chosen->custom_locations; }
}
const auto routing = plan::route(plan::resolve(p), *stated, clev, slev);
}
wav_mask_locations returns one Table E2.5 location per channel in file order, and nothing for a
mask that is 0, names a different number of speakers than the file has channels, or names one
Table E2.5 has no location for (SPEAKER_TOP_BACK_CENTER); SPEAKER_BACK_LEFT/RIGHT are the
surrounds on their own and the rear surrounds beside the sides, as iclforge::audio::locations_of
reads them (a test holds the two to each other). source_layout answers with the named layout that
renders exactly those locations, or else a custom selection, and nothing for a set the codec cannot
carry (AC-3 has no dependent substream). The located route() takes the source as it states itself:
a 2/1 source folds to stereo by 2/1's §7.8 coefficients, a 5.1.2 file sent to 7.1 is panned to it
rather than read as 7.1 because it is as wide, and a location named twice is refused.
plan::wav_channel_mask(locations) is the inverse for a file about to be written, which is how
forge decode states the speakers of what it writes.
Multiple sources: iclforge::ac3::plan::Assignment¶
iclforge/ac3/encoder/assignment.hpp. plan::route()'s other overload places one source by
direction — the microphone-onto-5.1 case. A caller with several sources — several files,
several capture devices, or dual mono's two independent programmes — instead says exactly
where each of their channels goes, channel by channel.
const std::array<iclforge::ac3::plan::SourceShape, 2> sources{{
{.channels = 2, .label = "music.wav"},
{.channels = 1, .label = "voiceover.wav"},
}};
iclforge::ac3::plan::Assignment assignment;
assignment.set(0, 0, {.kind = iclforge::ac3::plan::DestinationKind::kLocation, .location = Location::kLeft});
assignment.set(0, 1, {.kind = iclforge::ac3::plan::DestinationKind::kLocation, .location = Location::kRight});
// -6 dB under the music, so the voiceover reads without burying it.
assignment.set(1, 0, {.kind = iclforge::ac3::plan::DestinationKind::kLocation,
.location = Location::kCentre,
.trim_db = -6.0});
const auto routing = iclforge::ac3::plan::route(target, sources, assignment);
iclforge::ac3::plan::render(*routing, source_views, coded_views, iclforge::ac3::kSamplesPerFrame);
Full program: examples/multi_source_assignment.cpp
— a stereo music bed and a separate mono voiceover, combined onto one 5.1 stream.
Destination::kind is a closed set: kUnassigned, kLocation, kObject, kObjectMono (a
channel range folded to one mono dynamic object), kProgramme1/kProgramme2 (dual mono).
trim_db is a linear-gain trim in decibels, applied wherever that channel's content reaches
the stream — a route()-built Routing gain entry for kLocation rows and for dual mono's
kProgramme1/kProgramme2 rows (dual_mono_routing() carries them the same way), or the object
plane's own gain for kObject/kObjectMono (route() contributes nothing for those: object
audio reaches the stream through the Atmos path, not Routing). set() and
parse_destination() clamp it to [-24, +24] and snap it to a tenth-of-a-dB grid — a fixed grid
rather than an arbitrary double is what lets format_destination/parse_destination
round-trip a trim exactly. A kUnassigned row always reads 0, since set() erases those rather
than storing them.
route() returns nullopt if sources is empty, if two rows target the same location, or if
the target plan cannot express a requested location at all — it does not require every
target channel to be filled; an unassigned coded channel is simply silent, which
Assignment::unassigned(sources) reports back for a caller (a GUI's warning banner) to show.
dual_mono_routing() is the same idea specialised to 1+1's two independent programmes, one
channel on each. format_assignment/parse_assignment round-trip the whole assignment through
the CLI's map= grammar (kAssignmentSyntax documents it in full).
See also: Encoding E-AC-3 — AccessUnitEncoder/AccessUnitConfig, what a
resolved ChannelPlan is built for; Header map — iclforge/ac3/encoder/plan.hpp's
automatic single-source route() overload, not covered on this page.