From d35f6abe7a01dafed59b0df39d3761cf55edf380 Mon Sep 17 00:00:00 2001 From: TheM14 Date: Tue, 3 Nov 2026 01:34:32 +0800 Subject: [PATCH] Sync the kernel copy with the OAMD warp and trim controls The kernel copy follows the new kernel: the OAMD parser reads the trim element, the speaker renderer applies warp, per-object trim and the front-back balance, and the ADM writer carries them into the BWF metadata. joc_kernel.vcxproj lists the new speaker_trim translation unit and header, and the copied joc_version.h reports kernel 1.2.0 in the session log. The component keeps its playback settings, its speaker layout table and the side/rear channel swap; the stream interface is unchanged, so the decoder wrapper needs no edit. A stream whose OAMD asks for warp 2/3 now stops at the speaker renderer with JOC_ERR_NOT_SUPPORTED, as the reference refuses it. Builds Release-Static|x64 and |Win32. layout_table_test and channel_swap_test still pass; the render harness renders a warp-1 stream and reports the unimplemented mode for a warp-3 stream. --- kernel/include/eac3joc_core.h | 42 ++++- kernel/include/joc_version.h | 4 +- kernel/joc_kernel.vcxproj | 2 + kernel/src/adm/adm_metadata.cpp | 109 +++++++++++- kernel/src/adm/adm_metadata.h | 11 +- kernel/src/oamd/oamd_parser.cpp | 166 +++++++++++++++++++ kernel/src/oamd/oamd_parser.h | 44 +++++ kernel/src/speaker/speaker_layout_lookup.cpp | 28 ++-- kernel/src/speaker/speaker_layout_lookup.h | 7 + kernel/src/speaker/speaker_renderer.cpp | 101 +++++++++-- kernel/src/speaker/speaker_step.cpp | 23 ++- kernel/src/speaker/speaker_step.h | 5 + kernel/src/speaker/speaker_trim.cpp | 155 +++++++++++++++++ kernel/src/speaker/speaker_trim.h | 49 ++++++ kernel/src/stream/stream.cpp | 2 + kernel/src/task/task.cpp | 116 ++++++++++++- 16 files changed, 824 insertions(+), 40 deletions(-) create mode 100644 kernel/src/speaker/speaker_trim.cpp create mode 100644 kernel/src/speaker/speaker_trim.h diff --git a/kernel/include/eac3joc_core.h b/kernel/include/eac3joc_core.h index 811274e..44c6d9b 100644 --- a/kernel/include/eac3joc_core.h +++ b/kernel/include/eac3joc_core.h @@ -23,7 +23,7 @@ extern "C" { #endif enum { - EJOC_ABI_VERSION = 2, + EJOC_ABI_VERSION = 3, EJOC_FRAME_SAMPLES = 1536, EJOC_TIMESLOTS = 24, EJOC_SUBBANDS = 64, @@ -35,6 +35,7 @@ enum { EJOC_MAX_PARAMETER_BANDS = 23, EJOC_SPEAKER_BLOCK_SAMPLES = 32, EJOC_SPEAKER_COORDINATES = 3, + EJOC_SPEAKER_CONTROLS_VERSION = 1, EJOC_BINAURAL_BLOCK_SAMPLES = 512, EJOC_BINAURAL_INPUT_CHANNELS = 16, EJOC_BINAURAL_OUTPUT_CHANNELS = 2, @@ -123,6 +124,44 @@ metadata_offsets must be nondecreasing and <= sample_count. sample_count must be a multiple of EJOC_SPEAKER_BLOCK_SAMPLES. State and unfinished ramps are preserved across calls. */ + +/* +Per-object speaker controls taken from OAMD metadata: warp mode, trim mode, +the three trim levels and the two front-back balances. trim_bypass is indexed +by object slot (1..15); slot 0 carries the LFE and is unused. + +warp_mode 0 leaves the floor Y coordinate unchanged; mode 1 maps it through +clamp(2Y, 0, 1). warp modes 2 and 3 have no speaker implementation and are +rejected by the caller that builds this structure. + +trim_mode selects where the gain comes from: automatic applies the layout +position compensation, disabled applies no trim, and explicit applies the +position-dependent trim from centre_db / surround_db / height_db. A bypassed +object keeps its warp but loses the balance and the trim gain. + +Controls may change between calls; each call's metadata events use the +controls passed with that call. A null pointer means warp 0, no trim and no +balance. +*/ +typedef enum ejoc_speaker_trim_mode { + EJOC_SPEAKER_TRIM_AUTOMATIC = 0, + EJOC_SPEAKER_TRIM_DISABLED = 1, + EJOC_SPEAKER_TRIM_EXPLICIT = 2 +} ejoc_speaker_trim_mode; + +typedef struct ejoc_speaker_controls { + uint32_t struct_size; + uint32_t struct_version; + uint32_t warp_mode; + uint32_t trim_mode; /* ejoc_speaker_trim_mode */ + double centre_db; + double surround_db; /* positive values are treated as zero */ + double height_db; /* positive values are treated as zero */ + double listener_balance; /* [-1, 1] */ + double overhead_balance; /* [-1, 1] */ + uint8_t trim_bypass[EJOC_MAX_OBJECTS + 1]; /* per object slot, slot 0 unused */ +} ejoc_speaker_controls; + EJOC_API uint32_t EJOC_CALL ejoc_speaker_layout_channel_count(uint32_t speaker_bitfield); EJOC_API ejoc_speaker_renderer_handle EJOC_CALL ejoc_speaker_renderer_create(uint32_t speaker_bitfield); EJOC_API void EJOC_CALL ejoc_speaker_renderer_destroy(ejoc_speaker_renderer_handle handle); @@ -139,6 +178,7 @@ EJOC_API int EJOC_CALL ejoc_speaker_renderer_process( const uint8_t* region_indices, const uint8_t* height_enabled, const double* object_gains, + const ejoc_speaker_controls* controls, double* output_interleaved); EJOC_API ejoc_binaural_renderer_handle EJOC_CALL ejoc_binaural_renderer_create(void); diff --git a/kernel/include/joc_version.h b/kernel/include/joc_version.h index 5f32fae..dc1143c 100644 --- a/kernel/include/joc_version.h +++ b/kernel/include/joc_version.h @@ -9,8 +9,8 @@ #define JOC_VERSION_H #define JOC_VERSION_MAJOR 1 -#define JOC_VERSION_MINOR 1 +#define JOC_VERSION_MINOR 2 #define JOC_VERSION_PATCH 0 -#define JOC_VERSION_STRING "1.1.0" +#define JOC_VERSION_STRING "1.2.0" #endif /* JOC_VERSION_H */ diff --git a/kernel/joc_kernel.vcxproj b/kernel/joc_kernel.vcxproj index 96443f5..976786d 100644 --- a/kernel/joc_kernel.vcxproj +++ b/kernel/joc_kernel.vcxproj @@ -180,6 +180,7 @@ + @@ -260,6 +261,7 @@ + diff --git a/kernel/src/adm/adm_metadata.cpp b/kernel/src/adm/adm_metadata.cpp index 4240162..fb724a9 100644 --- a/kernel/src/adm/adm_metadata.cpp +++ b/kernel/src/adm/adm_metadata.cpp @@ -54,6 +54,67 @@ std::uint8_t checksum(const std::string& segment) { return static_cast((~sum + 1) & 0xFF); } +// OAMD trim element warp mode as the dbmd, DAMF and AXML warp field carry it: +// 0 normal, 1 warping, 2 downmix_pl2x, 3 downmix_loro. +std::uint32_t trim_warp_mode(const oamd::OamdTrim* trim) { + if (trim == nullptr) { + return 0; + } + return std::min(trim->warp_mode, 3u); +} + +// Trim records carry the balance as a sign bit plus a 7-bit magnitude on a +// 1/127 grid. +std::uint8_t balance_byte(double value) { + const long long magnitude = + std::min(pynum::py_round(std::fabs(value) * 127.0), 127); + return static_cast(static_cast(magnitude) | + (value < 0.0 ? 0x80u : 0x00u)); +} + +void append_trim_records(const oamd::OamdTrim& trim, std::string* out) { + out->append(""); + for (int index = 0; index < oamd::kTrimConfigs; ++index) { + const oamd::TrimConfig& record = trim.configs[index]; + if (record.kind == oamd::TrimConfigKind::kAbsent) { + continue; + } + out->append(""); + out->append(std::to_string(index)); + out->append(""); + if (record.kind == oamd::TrimConfigKind::kDefault) { + out->append(""); + continue; + } + const bool explicit_record = record.kind == oamd::TrimConfigKind::kExplicit; + const double centre = explicit_record ? record.centre : 0.0; + const double surround = explicit_record ? record.surround : 0.0; + const double height = explicit_record ? record.height : 0.0; + const double overhead = explicit_record ? record.balance_overhead : 0.0; + const double listener = explicit_record ? record.balance_listener : 0.0; + out->append("" + pynum::format_fixed(centre, 6) + ""); + out->append("" + pynum::format_fixed(surround, 6) + ""); + out->append("" + pynum::format_fixed(height, 6) + ""); + out->append("" + pynum::format_fixed(overhead, 6) + + ""); + out->append("" + pynum::format_fixed(listener, 6) + + ""); + out->append(""); + } + out->append(""); +} + +void append_atmos_master_block(const oamd::OamdTrim& trim, std::string* out) { + const std::string warp = std::to_string(trim_warp_mode(&trim)); + out->append(""); + out->append(warp); + out->append(""); + append_trim_records(trim, out); + out->append(""); + out->append(warp); + out->append(""); +} + } // namespace std::string ts(double seconds) { @@ -102,7 +163,8 @@ std::string build_chna() { return out; } -Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* out) { +Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* out, + const oamd::OamdTrim* trim) { if (out == nullptr) { return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null output"); } @@ -136,6 +198,10 @@ Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* ou segment9[106] = 0x01; segment9[111] = 0x22; segment9[112] = static_cast(0xFF); + // at[152]: the low three bits are the warp code, the other bits belong to + // the template's packed fields. + segment9[152] = static_cast( + (static_cast(segment9[152]) & 0xF8u) | trim_warp_mode(trim)); out->push_back(9); put_u16(out, 248); out->append(segment9); @@ -152,12 +218,35 @@ Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* ou segment10[i] = static_cast(0x84); } // Nine 15-byte configuration trims start at byte 7; the first byte of each - // carries the auto-trim flag in bit 0 (1 = automatic, 0 = manual). The - // default is automatic, so a freshly rendered master opens with automatic - // trims in the renderer. - for (std::size_t layout = 0; layout < 9; ++layout) { - const std::size_t flag = 7u + 15u * layout; - segment10[flag] = static_cast(static_cast(segment10[flag]) | 0x01u); + // carries the auto-trim flag in bit 0 (1 = automatic, 0 = manual). A + // configuration without an explicit record stays automatic, so a freshly + // rendered master opens with automatic trims in the renderer. + for (std::size_t layout = 0; layout < static_cast(oamd::kTrimConfigs); + ++layout) { + const std::size_t base = 7u + 15u * layout; + const oamd::TrimConfig* record = + trim != nullptr ? &trim->configs[layout] : nullptr; + const bool manual = record != nullptr && + (record->kind == oamd::TrimConfigKind::kExplicit || + record->kind == oamd::TrimConfigKind::kDisabled); + if (!manual) { + segment10[base] = + static_cast(static_cast(segment10[base]) | 0x01u); + continue; + } + segment10[base] = static_cast(static_cast(segment10[base]) & 0xFEu); + const bool explicit_record = record->kind == oamd::TrimConfigKind::kExplicit; + const double levels[3] = {explicit_record ? record->centre : 0.0, + explicit_record ? record->surround : 0.0, + explicit_record ? record->height : 0.0}; + for (std::size_t index = 0; index < 3; ++index) { + const float stored = static_cast(levels[index]); + std::memcpy(&segment10[base + 1u + 4u * index], &stored, sizeof(stored)); + } + const double overhead = explicit_record ? record->balance_overhead : 0.0; + const double listener = explicit_record ? record->balance_listener : 0.0; + segment10[base + 13u] = static_cast(balance_byte(overhead)); + segment10[base + 14u] = static_cast(balance_byte(listener)); } const std::size_t object_modes = 4u + 2u + 1u + 9u * 15u + object_count; for (std::uint32_t i = 10; i < std::min(object_count, 10u + kObjectCount); ++i) { @@ -176,7 +265,8 @@ Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* ou return Status::success(); } -std::string build_axml(const std::vector& tracks, double duration_sec, std::uint32_t rate) { +std::string build_axml(const std::vector& tracks, double duration_sec, std::uint32_t rate, + const oamd::OamdTrim* trim) { const double scale = static_cast(rate); std::string out; out.reserve(64u * 1024u); @@ -333,6 +423,9 @@ std::string build_axml(const std::vector& tracks, double duration_sec, st "_01"; out += ""; } + if (trim != nullptr) { + append_atmos_master_block(*trim, &out); + } out += ""; out += ""; return out; diff --git a/kernel/src/adm/adm_metadata.h b/kernel/src/adm/adm_metadata.h index 8049a7c..ea65247 100644 --- a/kernel/src/adm/adm_metadata.h +++ b/kernel/src/adm/adm_metadata.h @@ -7,6 +7,7 @@ #include #include "foundation/status.h" +#include "oamd/oamd_parser.h" namespace joc::adm { @@ -34,10 +35,16 @@ enum class BinauralMode : std::uint32_t { Off = 0, Near = 1, Far = 2, Mid = 3, U bool binaural_mode_from_name(const char* name, BinauralMode* out); -std::string build_axml(const std::vector& tracks, double duration_sec, std::uint32_t rate); +// ``trim`` is the OAMD trim element of the programme; when it is present the +// AXML carries the Dolby master-file block (content info, trim records, warp +// mode) and the dbmd chunk carries the per-configuration records and the warp +// code. +std::string build_axml(const std::vector& tracks, double duration_sec, std::uint32_t rate, + const oamd::OamdTrim* trim = nullptr); std::string build_chna(); -Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* out); +Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* out, + const oamd::OamdTrim* trim = nullptr); } // namespace joc::adm diff --git a/kernel/src/oamd/oamd_parser.cpp b/kernel/src/oamd/oamd_parser.cpp index 41ba3f1..b8a78af 100644 --- a/kernel/src/oamd/oamd_parser.cpp +++ b/kernel/src/oamd/oamd_parser.cpp @@ -15,6 +15,13 @@ constexpr int kRampDurationIndex[16] = {32, 64, 128, 256, 320, 480, 1000, 1001, 1024, 1600, 1601, 1602, 1920, 2000, 2002, 2048}; constexpr std::uint32_t kObjectElementId = 1; +// ETSI TS 103 420 Table 12 trim levels, 4-bit index, dB; index 15 is the +// minimum (-36 dB), not silence. +constexpr double kTrimLut[16] = {6.0, 3.0, 1.5, 0.75, -0.75, -1.5, + -3.0, -4.5, -6.0, -7.5, -9.0, -10.5, + -12.0, -13.5, -15.0, -36.0}; +// bal3d_y is coded as (code + 1) / 16, so the magnitude never reaches 0. +constexpr double kBalanceStep = 16.0; constexpr int kIsfObjectCounts[6] = {4, 8, 10, 14, 15, 30}; constexpr int kStandardBedChannelCount[10] = {2, 1, 1, 2, 2, 2, 2, 2, 2, 1}; @@ -329,6 +336,72 @@ Status parse_object_element(bits::BitReader& reader, std::uint32_t object_count, return Status::success(); } +// trim_element(): one programme-wide record per payload. The first bit of each +// configuration is b_default_trim; b_disable_trim and the value record follow +// only when that bit is clear. +Status parse_trim_element(bits::BitReader& reader, std::uint32_t object_count, OamdTrim* out) { + OamdTrim trim; + trim.warp_mode = reader.read(2); + reader.read(2); // reserved + trim.global_trim_mode = reader.read(2); + if (reader.failed()) { + return syntax("OAMD trim element header truncated"); + } + if (trim.global_trim_mode == 2u) { + for (int config = 0; config < kTrimConfigs; ++config) { + TrimConfig& record = trim.configs[config]; + if (reader.read(1) != 0u) { + record.kind = TrimConfigKind::kDefault; + continue; + } + if (reader.read(1) != 0u) { + record.kind = TrimConfigKind::kDisabled; + continue; + } + record.kind = TrimConfigKind::kExplicit; + const std::uint32_t presence = reader.read(5); + record.presence = static_cast(presence); + if ((presence & 0x01u) != 0u) { + record.centre = kTrimLut[reader.read(4) & 0x0Fu]; + } + if ((presence & 0x02u) != 0u) { + record.surround = kTrimLut[reader.read(4) & 0x0Fu]; + } + if ((presence & 0x04u) != 0u) { + record.height = kTrimLut[reader.read(4) & 0x0Fu]; + } + if ((presence & 0x08u) != 0u) { + const double sign = reader.read(1) != 0u ? 1.0 : -1.0; + record.balance_overhead = + (static_cast(reader.read(4)) + 1.0) / kBalanceStep * sign; + } + if ((presence & 0x10u) != 0u) { + const double sign = reader.read(1) != 0u ? 1.0 : -1.0; + record.balance_listener = + (static_cast(reader.read(4)) + 1.0) / kBalanceStep * sign; + } + if (reader.failed()) { + return syntax("OAMD trim configuration " + std::to_string(config) + + " truncated"); + } + } + } + if (reader.read(1) != 0u) { + for (std::uint32_t index = 0; index < object_count; ++index) { + const bool bypass = reader.read(1) != 0u; + if (index < static_cast(kMaxSlots)) { + trim.disable_per_object[index] = bypass; + } + } + } + if (reader.failed()) { + return syntax("OAMD trim bypass table truncated"); + } + trim.object_count = object_count; + *out = trim; + return Status::success(); +} + } // namespace int q_of(int k, int n) { @@ -377,6 +450,85 @@ Status parse_id11_bed_assignments(const std::uint8_t* payload, std::size_t paylo return Status::success(); } +Status parse_trim(const std::uint8_t* payload, std::size_t payload_size, OamdTrim* out, + bool* present) { + if (payload == nullptr || out == nullptr || present == nullptr) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, "null payload or output"); + } + *present = false; + if (payload_size == 0) { + return variant("header_truncated", "OAMD payload is empty"); + } + bits::BitReader reader(payload, payload_size); + std::uint32_t version = reader.read(2); + if (reader.failed()) { + return variant("header_truncated", "OAMD payload cannot hold the 2-bit version"); + } + if (version == 3u) { + version += reader.read(3); + if (reader.failed()) { + return syntax("OAMD version extension truncated"); + } + } + std::uint32_t object_count_bits = reader.read(5); + if (object_count_bits == 0x1Fu) { + object_count_bits += reader.read(7); + } + if (reader.failed()) { + return syntax("OAMD object count truncated"); + } + const std::uint32_t object_count = object_count_bits + 1u; + ProgramInfo program; + const Status program_status = parse_program_assignment(reader, &program); + if (!program_status.ok()) { + return program_status; + } + const std::uint32_t alternate_present = reader.read(1); + std::uint32_t element_count = reader.read(4); + if (element_count == 0xFu) { + element_count += reader.read(5); + } + if (reader.failed()) { + return syntax("OAMD element count truncated"); + } + for (std::uint32_t ordinal = 0; ordinal < element_count; ++ordinal) { + const std::uint32_t element_id = reader.read(4); + std::uint32_t size_field = 0; + if (!variable_bits_max(reader, 4, 4, &size_field)) { + return syntax("OAMD element header truncated at ordinal " + std::to_string(ordinal)); + } + const std::uint32_t size_bytes = size_field + 1u; + const std::size_t region_start = reader.position(); + const std::size_t region_end = region_start + static_cast(size_bytes) * 8u; + if (region_end > reader.limit()) { + return variant("element_bounds", + "element " + std::to_string(ordinal) + " (id " + + std::to_string(element_id) + ") declares " + + std::to_string(size_bytes) + " bytes, beyond the payload"); + } + if (alternate_present != 0u) { + reader.read(4); + } + reader.read(1); + if (reader.failed()) { + return syntax("OAMD element control fields truncated at ordinal " + + std::to_string(ordinal)); + } + if (element_id == kTrimElementId) { + const Status parsed = parse_trim_element(reader, object_count, out); + if (!parsed.ok()) { + return parsed; + } + *present = true; + return Status::success(); + } + if (!reader.skip(region_end - reader.position())) { + return syntax("OAMD element skip past payload end"); + } + } + return Status::success(); +} + Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out, ParseTrace* trace) { if (payload == nullptr || out == nullptr) { @@ -475,6 +627,20 @@ Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, reader.set_limit_bits(reader.limit()); reader.skip(region_end - reader.position()); } + } else if (element_id == kTrimElementId) { + OamdTrim trim; + const Status parsed = parse_trim_element(reader, object_count, &trim); + if (!parsed.ok()) { + return parsed; + } + if (!out->has_trim) { + out->trim = trim; + out->has_trim = true; + } + if (reader.position() < region_end) { + reader.set_limit_bits(reader.limit()); + reader.skip(region_end - reader.position()); + } } else { if (!reader.skip(region_end - reader.position())) { return syntax("OAMD element skip past payload end"); diff --git a/kernel/src/oamd/oamd_parser.h b/kernel/src/oamd/oamd_parser.h index cfff0e7..4a95478 100644 --- a/kernel/src/oamd/oamd_parser.h +++ b/kernel/src/oamd/oamd_parser.h @@ -17,6 +17,38 @@ inline constexpr int kAxes = 3; inline constexpr int kNQ12 = 62; inline constexpr int kNQ3 = 15; inline constexpr int kQ15Scale = 32768; +inline constexpr std::uint32_t kTrimElementId = 2; +inline constexpr int kTrimConfigs = 9; + +// One of the nine per-configuration trim records of the OAMD trim element. +// ``kAbsent`` means the configuration carries no data (the element only +// carries explicit records when global_trim_mode is 2). +enum class TrimConfigKind : std::uint8_t { + kAbsent = 0, + kDefault, + kDisabled, + kExplicit, +}; + +struct TrimConfig { + TrimConfigKind kind = TrimConfigKind::kAbsent; + std::uint8_t presence = 0; // b_*_trim_present bits of an explicit record + double centre = 0.0; // dB + double surround = 0.0; // dB + double height = 0.0; // dB + double balance_overhead = 0.0; // bal3d_y_tb, [-1, 1] + double balance_listener = 0.0; // bal3d_y_lis, [-1, 1] +}; + +// Programme calibration carried by the OAMD trim element: warp mode, global +// trim mode, the nine configuration records and the per-object bypass flags. +struct OamdTrim { + std::uint32_t warp_mode = 0; + std::uint32_t global_trim_mode = 0; + TrimConfig configs[kTrimConfigs]; + std::uint32_t object_count = 0; + bool disable_per_object[kMaxSlots] = {}; +}; // (the reference's None), i.e. hold the previous position. struct SlotUpdate { @@ -39,6 +71,11 @@ struct OamdUpdate { bool content_bed = false; bool content_isf = false; bool content_dynamic = false; + // Trim element of this payload. The element describes the programme, so + // ``has_trim`` only marks the payloads that carry it; a payload without it + // keeps whatever controls the caller already holds. + OamdTrim trim; + bool has_trim = false; }; class OamdState { @@ -84,6 +121,13 @@ int q_of(int k, int n); Status parse_id11(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out); +// Parses only the trim element of a payload. ``out`` receives the first trim +// element found; ``*present`` is false when the payload carries none. decode +// paths that need the OAMD calibration but no object state can use this +// without requiring an object element. +Status parse_trim(const std::uint8_t* payload, std::size_t payload_size, OamdTrim* out, + bool* present); + struct BedAssignment { bool lfe_only = false; bool standard = false; diff --git a/kernel/src/speaker/speaker_layout_lookup.cpp b/kernel/src/speaker/speaker_layout_lookup.cpp index d38f037..84a4f29 100644 --- a/kernel/src/speaker/speaker_layout_lookup.cpp +++ b/kernel/src/speaker/speaker_layout_lookup.cpp @@ -10,20 +10,23 @@ struct FrozenLayout { const char* name; std::uint32_t out_ch_config; std::uint32_t speaker_bitfield; + std::uint32_t surround_channels; + std::uint32_t height_channels; + bool carries_trim; }; constexpr FrozenLayout kLayouts[kLayoutCount] = { - {"2.0", 0, 1}, - {"3.1", 3, 7}, - {"4.0", 0xFF, 9}, - {"5.1", 7, 15}, - {"7.1", 11, 31}, - {"5.1.2", 13, 1039}, - {"5.1.4", 14, 2575}, - {"7.1.2", 15, 1055}, - {"7.1.4", 16, 2591}, - {"9.1.4", 19, 2719}, - {"9.1.6", 20, 3743}, + {"2.0", 0, 1, 0, 0, true}, + {"3.1", 3, 7, 0, 0, true}, + {"4.0", 0xFF, 9, 2, 0, true}, + {"5.1", 7, 15, 2, 0, true}, + {"7.1", 11, 31, 4, 0, true}, + {"5.1.2", 13, 1039, 2, 2, true}, + {"5.1.4", 14, 2575, 2, 4, true}, + {"7.1.2", 15, 1055, 4, 2, false}, + {"7.1.4", 16, 2591, 4, 4, false}, + {"9.1.4", 19, 2719, 4, 4, false}, + {"9.1.6", 20, 3743, 4, 6, false}, }; void fill(const FrozenLayout& source, LayoutInfo* out) { @@ -31,6 +34,9 @@ void fill(const FrozenLayout& source, LayoutInfo* out) { out->out_ch_config = source.out_ch_config; out->speaker_bitfield = source.speaker_bitfield; out->channel_count = ejoc_speaker_layout_channel_count(source.speaker_bitfield); + out->surround_channels = source.surround_channels; + out->height_channels = source.height_channels; + out->carries_trim = source.carries_trim; } } // namespace diff --git a/kernel/src/speaker/speaker_layout_lookup.h b/kernel/src/speaker/speaker_layout_lookup.h index bc0b424..e225cc9 100644 --- a/kernel/src/speaker/speaker_layout_lookup.h +++ b/kernel/src/speaker/speaker_layout_lookup.h @@ -15,6 +15,13 @@ struct LayoutInfo { std::uint32_t out_ch_config = 0; std::uint32_t speaker_bitfield = 0; std::uint32_t channel_count = 0; + // Ls/Rs/Lrs/Rrs and Ltf/Rtf/Ltr/Rtr/Ltm/Rtm, the labels the OAMD trim + // configuration index counts. + std::uint32_t surround_channels = 0; + std::uint32_t height_channels = 0; + // One of the seven layouts that carry OAMD trim and front-back balance + // semantics; wider layouts leave the programme untrimmed. + bool carries_trim = false; }; // All eleven layouts in the reference's order (2.0, 3.1, 4.0, 5.1, 7.1, 5.1.2, diff --git a/kernel/src/speaker/speaker_renderer.cpp b/kernel/src/speaker/speaker_renderer.cpp index 385cce5..d1ce5f1 100644 --- a/kernel/src/speaker/speaker_renderer.cpp +++ b/kernel/src/speaker/speaker_renderer.cpp @@ -1,5 +1,6 @@ #define EJOC_BUILD_DLL #include "eac3joc_core.h" +#include "speaker/speaker_trim.h" #include "speaker_layouts.h" #include @@ -216,6 +217,7 @@ public: const std::uint8_t* region_indices, const std::uint8_t* height_enabled, const double* object_gains, + const ejoc_speaker_controls* controls, double* output) noexcept { error_[0] = '\0'; if (!input || !output) { @@ -227,11 +229,21 @@ public: if (metadata_count && (!metadata_offsets || !ramp_durations || !positions_q15)) { return fail("metadata arrays are null while metadata_count is nonzero"); } + if (!validate_controls(controls)) { + return -1; + } if (!validate_metadata(sample_count, metadata_count, metadata_offsets, positions_q15, region_indices, object_gains)) { return -1; } + // A caller that passes no controls gets warp 0, no trim and no balance. + ejoc_speaker_controls fallback{}; + fallback.struct_size = sizeof(fallback); + fallback.struct_version = EJOC_SPEAKER_CONTROLS_VERSION; + fallback.trim_mode = EJOC_SPEAKER_TRIM_DISABLED; + const ejoc_speaker_controls& active = controls != nullptr ? *controls : fallback; + std::fill(output, output + static_cast(sample_count) * layout_->channel_count, 0.0); const bool has_lfe = (layout_->speaker_bitfield & 0x4u) != 0; const std::size_t total_blocks = sample_count / kBlock; @@ -239,14 +251,14 @@ public: for (std::size_t block = 0; block < total_blocks; ++block) { while (event < metadata_count && aligned_block(metadata_offsets[event]) == block) { apply_event(event, ramp_durations, positions_q15, region_indices, - height_enabled, object_gains); + height_enabled, object_gains, active); ++event; } mix_block(input, output, block, has_lfe); } while (event < metadata_count && aligned_block(metadata_offsets[event]) == total_blocks) { apply_event(event, ramp_durations, positions_q15, region_indices, - height_enabled, object_gains); + height_enabled, object_gains, active); ++event; } if (event != metadata_count) { @@ -268,6 +280,42 @@ private: return -1; } + bool validate_controls(const ejoc_speaker_controls* controls) noexcept { + if (controls == nullptr) { + return true; + } + if (controls->struct_size != sizeof(ejoc_speaker_controls)) { + fail("speaker controls struct_size mismatch"); + return false; + } + if (controls->struct_version != EJOC_SPEAKER_CONTROLS_VERSION) { + fail("unknown speaker controls struct_version"); + return false; + } + if (controls->warp_mode > 1u) { + fail("speaker rendering implements warp modes 0/1 only"); + return false; + } + if (controls->trim_mode > EJOC_SPEAKER_TRIM_EXPLICIT) { + fail("unknown speaker trim mode"); + return false; + } + for (const double level : + {controls->centre_db, controls->surround_db, controls->height_db}) { + if (!std::isfinite(level)) { + fail("trim level is not finite"); + return false; + } + } + for (const double balance : {controls->listener_balance, controls->overhead_balance}) { + if (!std::isfinite(balance) || balance < -1.0 || balance > 1.0) { + fail("balance is not finite or outside [-1, 1]"); + return false; + } + } + return true; + } + bool validate_metadata(const std::uint32_t sample_count, const std::uint32_t metadata_count, const std::uint32_t* metadata_offsets, @@ -318,13 +366,28 @@ private: const std::uint8_t region_index, const bool enable_height, const double object_gain, + const ejoc_speaker_controls& controls, + const bool bypass_trim, ChannelGains& output) const noexcept { output.fill(0.0); const auto& region = layout_->regions[region_index]; const double u = static_cast(position[0]) / kQ15Scale; const double v = static_cast(position[1]) / kQ15Scale; - const double w = static_cast(position[2]) / kQ15Scale; - const double floor_v = clamp(std::ldexp(v, floor_y_exponent_), 0.0, 1.0); + const double w = std::max(static_cast(position[2]) / kQ15Scale, 0.0); + const int shift = controls.warp_mode == 1u ? floor_y_exponent_ : 0; + double floor_v = clamp(std::ldexp(v, shift), 0.0, 1.0); + double top_v = clamp(v, 0.0, 1.0); + double trim_v = v; + if (controls.trim_mode == EJOC_SPEAKER_TRIM_EXPLICIT && !bypass_trim) { + // Warp feeds the floor coordinate only: the height and trim + // coordinates see the balance map on the original Y. + floor_v = joc::speaker::apply_balance(floor_v, w, controls.listener_balance, + controls.overhead_balance); + top_v = joc::speaker::apply_balance(top_v, w, controls.listener_balance, + controls.overhead_balance); + trim_v = joc::speaker::apply_balance(v, w, controls.listener_balance, + controls.overhead_balance); + } PointGains floor{}; plane_gains(region, region.axis0_groups, region.axis0_group_count, u, floor_v, region.mode, floor); @@ -332,7 +395,7 @@ private: if (region.mode == 3) { PointGains height{}; plane_gains(region, region.axis1_groups, region.axis1_group_count, - u, v, 3, height); + u, top_v, 3, height); const double z = enable_height ? clamp(w, 0.0, kQ15Max) : 0.0; if (z >= kQ15Max) { point = height; @@ -345,21 +408,33 @@ private: } } } - const double y_term = clamp(v / 0.6, 0.0, 1.0); - const double z_term = clamp((w - 0.2) / 0.8, 0.0, 1.0); - const double amount = clamp(y_term + z_term, 0.0, 1.0); - const double gain = std::pow(10.0, attenuation_db_ * amount / 20.0) * object_gain; + double gain = object_gain; + if (!bypass_trim) { + if (controls.trim_mode == EJOC_SPEAKER_TRIM_AUTOMATIC) { + gain *= position_gain(trim_v, w); + } else if (controls.trim_mode == EJOC_SPEAKER_TRIM_EXPLICIT) { + gain *= joc::speaker::explicit_position_gain(controls, u, trim_v, w); + } + } for (std::size_t index = 0; index < region.point_count; ++index) { output[region.points[index].speaker_id] = point[index] * gain; } } + double position_gain(const double v, const double w) const noexcept { + const double y_term = clamp(v / 0.6, 0.0, 1.0); + const double z_term = clamp((w - 0.2) / 0.8, 0.0, 1.0); + const double amount = clamp(y_term + z_term, 0.0, 1.0); + return std::pow(10.0, attenuation_db_ * amount / 20.0); + } + void apply_event(const std::size_t event, const std::uint32_t* ramp_durations, const std::uint16_t* positions_q15, const std::uint8_t* region_indices, const std::uint8_t* height_enabled, - const double* object_gains) noexcept { + const double* object_gains, + const ejoc_speaker_controls& controls) noexcept { const std::uint32_t blocks = ramp_blocks(ramp_durations[event]); for (std::size_t object = 0; object < kObjects; ++object) { const std::size_t object_event = event * kObjects + object; @@ -367,8 +442,9 @@ private: const std::uint8_t region = region_indices ? region_indices[object_event] : 0; const bool height = !height_enabled || height_enabled[object_event] != 0; const double object_gain = object_gains ? object_gains[object_event] : 1.0; + const bool bypass = controls.trim_bypass[object + 1] != 0u; ChannelGains next{}; - render_point(position, region, height, object_gain, next); + render_point(position, region, height, object_gain, controls, bypass, next); for (std::size_t channel = 0; channel < layout_->channel_count; ++channel) { const double difference = next[channel] - current_[object][channel]; target_[object][channel] = next[channel]; @@ -482,6 +558,7 @@ int EJOC_CALL ejoc_speaker_renderer_process( const uint8_t* region_indices, const uint8_t* height_enabled, const double* object_gains, + const ejoc_speaker_controls* controls, double* output_interleaved) { if (!handle) { return -1; @@ -489,7 +566,7 @@ int EJOC_CALL ejoc_speaker_renderer_process( return static_cast(handle)->process( objects16_interleaved, sample_count, metadata_count, metadata_offsets, ramp_durations, positions_q15, - region_indices, height_enabled, object_gains, output_interleaved); + region_indices, height_enabled, object_gains, controls, output_interleaved); } } // extern "C" diff --git a/kernel/src/speaker/speaker_step.cpp b/kernel/src/speaker/speaker_step.cpp index 8d97362..86d0ba5 100644 --- a/kernel/src/speaker/speaker_step.cpp +++ b/kernel/src/speaker/speaker_step.cpp @@ -26,8 +26,29 @@ Status step(SpeakerStep* context, const std::vector& objects16_planar, static_cast(JOC_FRAME_SAMPLES) * context->layout.channel_count, 0.0); context->last_had_payload = update != nullptr; + if (!context->controls_ready) { + const Status status = + parameters_from_oamd(nullptr, context->layout, &context->controls); + if (!status.ok()) { + if (error != nullptr) { + *error = status.message(); + } + return status; + } + context->controls_ready = true; + } std::uint32_t ramp = 0; if (update != nullptr) { + if (update->has_trim) { + const Status status = parameters_from_oamd(&update->trim, context->layout, + &context->controls); + if (!status.ok()) { + if (error != nullptr) { + *error = status.message(); + } + return status; + } + } context->state.apply(*update); ramp = update->ramp_duration_samples; context->last_block_offset = update->block_offset_samples; @@ -53,7 +74,7 @@ Status step(SpeakerStep* context, const std::vector& objects16_planar, context->handle, context->interleaved.data(), JOC_FRAME_SAMPLES, event_count, event_count != 0u ? &offset : nullptr, event_count != 0u ? &ramp : nullptr, event_count != 0u ? &positions[0][0] : nullptr, nullptr, nullptr, nullptr, - context->output.data()); + &context->controls, context->output.data()); if (result != 0) { const char* message = ejoc_speaker_renderer_last_error(context->handle); const std::string text = diff --git a/kernel/src/speaker/speaker_step.h b/kernel/src/speaker/speaker_step.h index 28d23f5..1dd9249 100644 --- a/kernel/src/speaker/speaker_step.h +++ b/kernel/src/speaker/speaker_step.h @@ -9,6 +9,7 @@ #include "foundation/status.h" #include "oamd/oamd_parser.h" #include "speaker/speaker_layout_lookup.h" +#include "speaker/speaker_trim.h" namespace joc::speaker { @@ -23,6 +24,10 @@ struct SpeakerStep { std::uint32_t last_ramp_duration = 0; std::uint32_t last_object_count = 0; bool last_had_payload = false; + // OAMD speaker controls. A payload carrying the trim element replaces + // them; a payload without it keeps the previous set. + ejoc_speaker_controls controls{}; + bool controls_ready = false; // bed 声道是固定通道:OAMD 不给位置,方位由声明的 bed 布局给出。这里存 // 对象槽 → Q15 方位;bed_override 为 false 的槽仍按 OAMD 坐标声像。 std::uint16_t bed_positions[oamd::kObjects][oamd::kAxes] = {}; diff --git a/kernel/src/speaker/speaker_trim.cpp b/kernel/src/speaker/speaker_trim.cpp new file mode 100644 index 0000000..2200eb6 --- /dev/null +++ b/kernel/src/speaker/speaker_trim.cpp @@ -0,0 +1,155 @@ +#include "speaker/speaker_trim.h" + +#include +#include +#include + +#include "foundation/py_num.h" + +namespace joc::speaker { + +namespace { + +int height_class(std::uint32_t count) { + if (count == 0u) { + return 0; + } + if (count == 2u) { + return 1; + } + return 2; +} + +int floor_class(std::uint32_t count) { + if (count == 0u) { + return 0; + } + if (count == 2u || count == 3u) { + return 1; + } + return 2; +} + +} // namespace + +int trim_config_index(const LayoutInfo& layout) { + return floor_class(layout.surround_channels) + 3 * height_class(layout.height_channels); +} + +Status parameters_from_oamd(const oamd::OamdTrim* trim, const LayoutInfo& layout, + ejoc_speaker_controls* out) { + if (out == nullptr) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender, + "null speaker controls"); + } + ejoc_speaker_controls controls{}; + controls.struct_size = sizeof(controls); + controls.struct_version = EJOC_SPEAKER_CONTROLS_VERSION; + controls.trim_mode = EJOC_SPEAKER_TRIM_AUTOMATIC; + if (trim != nullptr) { + controls.warp_mode = trim->warp_mode; + for (int slot = 0; slot < oamd::kMaxSlots; ++slot) { + controls.trim_bypass[slot] = trim->disable_per_object[slot] ? 1u : 0u; + } + } + if (controls.warp_mode > 1u) { + return Status::fail(JOC_ERR_NOT_SUPPORTED, stage::kRender, + "speaker: warp_mode_not_implemented: speaker rendering " + "implements warp modes 0/1; warp mode " + + std::to_string(controls.warp_mode) + " is reserved"); + } + if (!layout.carries_trim) { + controls.trim_mode = EJOC_SPEAKER_TRIM_DISABLED; + *out = controls; + return Status::success(); + } + const std::uint32_t mode = trim != nullptr ? trim->global_trim_mode : 0u; + if (mode == 1u) { + controls.trim_mode = EJOC_SPEAKER_TRIM_DISABLED; + *out = controls; + return Status::success(); + } + if (mode > 2u) { + return Status::fail(JOC_ERR_NOT_SUPPORTED, stage::kRender, + "speaker: trim_mode_not_implemented: OAMD global trim mode " + + std::to_string(mode) + " is reserved"); + } + if (mode == 2u) { + const oamd::TrimConfig& record = trim->configs[trim_config_index(layout)]; + if (record.kind == oamd::TrimConfigKind::kDefault || + record.kind == oamd::TrimConfigKind::kAbsent) { + *out = controls; + return Status::success(); + } + if (record.kind == oamd::TrimConfigKind::kDisabled) { + controls.trim_mode = EJOC_SPEAKER_TRIM_DISABLED; + *out = controls; + return Status::success(); + } + if (!std::isfinite(record.centre) || !std::isfinite(record.surround) || + !std::isfinite(record.height)) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender, + "speaker: trim levels must be finite"); + } + for (const double balance : {record.balance_listener, record.balance_overhead}) { + if (!std::isfinite(balance) || balance < -1.0 || balance > 1.0) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender, + "speaker: balance must be finite and in [-1,1]"); + } + } + // Surround and height are attenuation-only; the centre level keeps its + // sign. + controls.trim_mode = EJOC_SPEAKER_TRIM_EXPLICIT; + controls.centre_db = record.centre; + controls.surround_db = std::min(record.surround, 0.0); + controls.height_db = std::min(record.height, 0.0); + controls.listener_balance = record.balance_listener; + controls.overhead_balance = record.balance_overhead; + } + *out = controls; + return Status::success(); +} + +double front_back_balance(double position, double balance) { + const long long steps = + pynum::py_round(std::fabs(balance) * static_cast(kBalanceSteps)); + if (steps == 0) { + return position; + } + const double slope = static_cast(steps) / 18.0; + if (position <= kBalanceWall || position >= 1.0 - kBalanceWall) { + return position; + } + if (position < 0.5) { + return balance > 0.0 ? (1.0 + slope) * position - kBalanceWall * slope + : (1.0 - slope) * position + kBalanceWall * slope; + } + return balance > 0.0 + ? (1.0 - slope) * (position - 0.5) + 0.5 + 0.45 * slope + : (1.0 + slope) * (position - 0.5) + 0.5 - 0.45 * slope; +} + +double apply_balance(double position, double height, double listener, double overhead) { + if (listener == 0.0 && overhead == 0.0) { + return position; + } + const double low = front_back_balance(position, listener); + const double high = front_back_balance(position, overhead); + return (1.0 - height) * low + height * high; +} + +double explicit_position_gain(const ejoc_speaker_controls& controls, double x, double y, + double z) { + const double surround = std::min(std::max(2.0 * y, 0.0), 1.0) * (1.0 - z); + const double centre = 1.0 - std::min( + std::max(10.0 * std::max(std::max(std::fabs(z) - kBalanceWall, + y - kBalanceWall), + std::fabs(x - 0.5) - kBalanceWall), + 0.0), + 1.0); + const double db = surround * std::min(controls.surround_db, 0.0) + + z * std::min(controls.height_db, 0.0) + centre * controls.centre_db; + return std::pow(10.0, db / 20.0); +} + +} // namespace joc::speaker diff --git a/kernel/src/speaker/speaker_trim.h b/kernel/src/speaker/speaker_trim.h new file mode 100644 index 0000000..32d982f --- /dev/null +++ b/kernel/src/speaker/speaker_trim.h @@ -0,0 +1,49 @@ +// Port of src/trim_balance.py. + +#pragma once + +#include + +#include "eac3joc_core.h" +#include "foundation/status.h" +#include "oamd/oamd_parser.h" +#include "speaker/speaker_layout_lookup.h" + +namespace joc::speaker { + +// Front-back balance keeps a 5 percent wall band at each end unmapped. +inline constexpr double kBalanceWall = 0.05; +// bal3d_y is coded on a 1/16 grid, so a balance is quantised to that grid. +inline constexpr int kBalanceSteps = 16; + +// Trim configuration a layout selects, from its surround and height counts: +// +// cfg = kappa_f(surrounds) + 3 * kappa_h(heights) +// kappa_f: 0 -> 0, 2..3 -> 1, otherwise 2 +// kappa_h: 0 -> 0, 2 -> 1, otherwise 2 +int trim_config_index(const LayoutInfo& layout); + +// Converts OAMD calibration to the speaker controls of one layout. Warp modes +// 2/3 and the reserved global trim mode are reported as unsupported. A null +// trim selects warp 0 and automatic trim, or disabled trim for a layout that +// carries no trim semantics. +Status parameters_from_oamd(const oamd::OamdTrim* trim, const LayoutInfo& layout, + ejoc_speaker_controls* out); + +// Reparameterises a front-back coordinate in [0, 1] by ``balance``: 0 is the +// front wall and 1 the back wall, and the two wall bands pass through +// unchanged. +double front_back_balance(double position, double balance); + +// Blends the listener-plane and overhead balance by height: 0 at the listener +// plane, 1 at the top layer. +double apply_balance(double position, double height, double listener, double overhead); + +// Position-dependent object trim at the balanced original Y. Surround fades +// in with Y and out towards Z=1, height fades in with Z, and the centre trim is +// local to the front-centre region. The scalar multiplies the object's whole +// panning vector before accumulation. +double explicit_position_gain(const ejoc_speaker_controls& controls, double x, double y, + double z); + +} // namespace joc::speaker diff --git a/kernel/src/stream/stream.cpp b/kernel/src/stream/stream.cpp index 8112ba2..5bb8f92 100644 --- a/kernel/src/stream/stream.cpp +++ b/kernel/src/stream/stream.cpp @@ -59,6 +59,8 @@ void Stream::reset_state() { speaker_.state.reset(); speaker_.output.clear(); speaker_.last_had_payload = false; + // Back to the initial controls; the next trim element replaces them. + speaker_.controls_ready = false; } if (binaural_ready_) { binaural_.reset(); diff --git a/kernel/src/task/task.cpp b/kernel/src/task/task.cpp index 88ff446..36dfc5e 100644 --- a/kernel/src/task/task.cpp +++ b/kernel/src/task/task.cpp @@ -313,6 +313,91 @@ int probe_config_idx(const std::string& path) { return config; } +// Reads the programme trim element from the leading audio frames, without +// rendering them. The ADM master metadata carries it, and the reference scans +// the first 64 audio frames for it. +Status probe_atmos_trim(const std::string& path, std::uint64_t max_frames, + oamd::OamdTrim* trim, bool* present) { + *present = false; + std::ifstream input = fs_utf8::open_input(path); + if (!input) { + return Status::success(); + } + ::joc::eac3::FrameReader reader; + std::vector chunk(1u << 16); + std::vector<::joc::eac3::Frame> group; + std::uint64_t frames = 0; + auto scan_group = [&]() -> bool { + for (const ::joc::eac3::Frame& frame : group) { + bool has_joc = false; + ::joc::emdf::Container container; + if (!::joc::emdf::find_joc_emdf_optional( + frame.data, frame.size, &has_joc, &container, + &::joc::joc::score_container_as_joc, nullptr) + .ok() || + !has_joc) { + continue; + } + const ::joc::emdf::Payload* payload = container.find(::joc::emdf::kIdOamd); + if (payload == nullptr) { + continue; + } + std::vector bytes; + if (!::joc::emdf::extract_payload_bytes(frame.data, frame.size, *payload, &bytes) + .ok()) { + continue; + } + oamd::OamdUpdate update; + if (!oamd::parse_id11(bytes.data(), bytes.size(), &update).ok() || + !update.has_trim) { + continue; + } + *trim = update.trim; + *present = true; + return true; + } + group.clear(); + return false; + }; + bool done = false; + while (!done) { + input.read(chunk.data(), static_cast(chunk.size())); + const std::streamsize got = input.gcount(); + if (got > 0) { + reader.push(reinterpret_cast(chunk.data()), + static_cast(got)); + } else { + reader.finish(); + done = true; + } + for (;;) { + ::joc::eac3::Frame frame; + const ::joc::eac3::FrameReader::Next state = reader.next(&frame); + if (state != ::joc::eac3::FrameReader::Next::Ok) { + break; + } + bool repeats = false; + for (const ::joc::eac3::Frame& prior : group) { + if (prior.same_stream(frame)) { + repeats = true; + break; + } + } + if (repeats) { + ++frames; + if (scan_group() || frames >= max_frames) { + return Status::success(); + } + } + group.push_back(frame); + } + } + if (!group.empty()) { + scan_group(); + } + return Status::success(); +} + // Rendered PCM is spooled to a float32 file so the output format can be decided // once the peak is known, which is what makes --clip-action possible; the // reference CLI spools for the same reason. @@ -1311,6 +1396,13 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu ::joc::speaker::SpeakerStep speaker; bool bed_positions_ready = false; + // The ADM master metadata carries the programme's trim element, scanned + // from the leading frames before rendering starts. + ::joc::oamd::OamdTrim adm_trim; + bool adm_trim_present = false; + if (cfg.operation == JOC_OP_ADM_BWF) { + probe_atmos_trim(eac3_path, 64, &adm_trim, &adm_trim_present); + } const bool speaker_enabled = cfg.operation == JOC_OP_SPEAKER; if (speaker_enabled) { if (!::joc::speaker::layout_by_name(cfg.layout.c_str(), &speaker.layout)) { @@ -1323,6 +1415,21 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu return finalize_with_error(JOC_ERR_RENDER_FAILED, "render", "cannot create the speaker renderer"); } + // The reference seeds the initial controls from the leading frames, so + // the first trim element is in effect before its own event arrives. + ::joc::oamd::OamdTrim initial_trim; + bool has_initial_trim = false; + probe_atmos_trim(eac3_path, 64, &initial_trim, &has_initial_trim); + if (has_initial_trim) { + const Status seeded = ::joc::speaker::parameters_from_oamd( + &initial_trim, speaker.layout, &speaker.controls); + if (!seeded.ok()) { + ejoc_speaker_renderer_destroy(speaker.handle); + speaker.handle = nullptr; + return finalize_with_error(seeded.code(), "render", seeded.message()); + } + speaker.controls_ready = true; + } bus.emit(JOC_EV_RENDERER_INITIALIZED, JOC_STAGE_RENDER, JOC_LOG_INFO, fmt("speaker renderer: %s, %u channels", speaker.layout.name, speaker.layout.channel_count)); @@ -2107,13 +2214,16 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu } else { const double duration_sec = static_cast(processed) * static_cast(JOC_FRAME_SAMPLES) / 48000.0; - const std::string axml = ::joc::adm::build_axml(tracks, duration_sec, 48000); + const std::string axml = + ::joc::adm::build_axml(tracks, duration_sec, 48000, + adm_trim_present ? &adm_trim : nullptr); const std::string chna = ::joc::adm::build_chna(); std::string dbmd; const ::joc::adm::BinauralMode mode = static_cast<::joc::adm::BinauralMode>(cfg.adm_binaural_mode); - const Status dbmd_status = - ::joc::adm::build_dbmd(::joc::io::AdmBwfWriter::kChannels, mode, &dbmd); + const Status dbmd_status = ::joc::adm::build_dbmd( + ::joc::io::AdmBwfWriter::kChannels, mode, &dbmd, + adm_trim_present ? &adm_trim : nullptr); if (!dbmd_status.ok()) { failure = dbmd_status.message(); } else {