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.
This commit is contained in:
TheM14 committed 2026-11-03 13:35:01 +08:00
1 parent 43b16687cc
commit d35f6abe7a
16 files changed
+824 -40

No files matched your search

+41 -1
View File
@@ -23,7 +23,7 @@ extern "C" {
#endif #endif
enum { enum {
EJOC_ABI_VERSION = 2, EJOC_ABI_VERSION = 3,
EJOC_FRAME_SAMPLES = 1536, EJOC_FRAME_SAMPLES = 1536,
EJOC_TIMESLOTS = 24, EJOC_TIMESLOTS = 24,
EJOC_SUBBANDS = 64, EJOC_SUBBANDS = 64,
@@ -35,6 +35,7 @@ enum {
EJOC_MAX_PARAMETER_BANDS = 23, EJOC_MAX_PARAMETER_BANDS = 23,
EJOC_SPEAKER_BLOCK_SAMPLES = 32, EJOC_SPEAKER_BLOCK_SAMPLES = 32,
EJOC_SPEAKER_COORDINATES = 3, EJOC_SPEAKER_COORDINATES = 3,
EJOC_SPEAKER_CONTROLS_VERSION = 1,
EJOC_BINAURAL_BLOCK_SAMPLES = 512, EJOC_BINAURAL_BLOCK_SAMPLES = 512,
EJOC_BINAURAL_INPUT_CHANNELS = 16, EJOC_BINAURAL_INPUT_CHANNELS = 16,
EJOC_BINAURAL_OUTPUT_CHANNELS = 2, 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 be a multiple of EJOC_SPEAKER_BLOCK_SAMPLES. State and unfinished ramps are
preserved across calls. 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 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 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); 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* region_indices,
const uint8_t* height_enabled, const uint8_t* height_enabled,
const double* object_gains, const double* object_gains,
const ejoc_speaker_controls* controls,
double* output_interleaved); double* output_interleaved);
EJOC_API ejoc_binaural_renderer_handle EJOC_CALL ejoc_binaural_renderer_create(void); EJOC_API ejoc_binaural_renderer_handle EJOC_CALL ejoc_binaural_renderer_create(void);
+2 -2
View File
@@ -9,8 +9,8 @@
#define JOC_VERSION_H #define JOC_VERSION_H
#define JOC_VERSION_MAJOR 1 #define JOC_VERSION_MAJOR 1
#define JOC_VERSION_MINOR 1 #define JOC_VERSION_MINOR 2
#define JOC_VERSION_PATCH 0 #define JOC_VERSION_PATCH 0
#define JOC_VERSION_STRING "1.1.0" #define JOC_VERSION_STRING "1.2.0"
#endif /* JOC_VERSION_H */ #endif /* JOC_VERSION_H */
+2
View File
@@ -180,6 +180,7 @@
<ClCompile Include="src\oamd\oamd_parser.cpp" /> <ClCompile Include="src\oamd\oamd_parser.cpp" />
<ClCompile Include="src\speaker\speaker_layout_lookup.cpp" /> <ClCompile Include="src\speaker\speaker_layout_lookup.cpp" />
<ClCompile Include="src\speaker\speaker_step.cpp" /> <ClCompile Include="src\speaker\speaker_step.cpp" />
<ClCompile Include="src\speaker\speaker_trim.cpp" />
<ClCompile Include="src\stream\stream.cpp" /> <ClCompile Include="src\stream\stream.cpp" />
<ClCompile Include="src\task\task.cpp" /> <ClCompile Include="src\task\task.cpp" />
<ClCompile Include="src\telemetry\event_bus.cpp" /> <ClCompile Include="src\telemetry\event_bus.cpp" />
@@ -260,6 +261,7 @@
<ClInclude Include="src\simd\simd.h" /> <ClInclude Include="src\simd\simd.h" />
<!-- src\simd\kernels_intrin_neon.cpp is excluded on every platform; see above. --> <!-- src\simd\kernels_intrin_neon.cpp is excluded on every platform; see above. -->
<ClInclude Include="src\speaker\speaker_layout_lookup.h" /> <ClInclude Include="src\speaker\speaker_layout_lookup.h" />
<ClInclude Include="src\speaker\speaker_trim.h" />
<ClInclude Include="src\speaker\speaker_layouts.h" /> <ClInclude Include="src\speaker\speaker_layouts.h" />
<ClInclude Include="src\speaker\speaker_step.h" /> <ClInclude Include="src\speaker\speaker_step.h" />
<ClInclude Include="src\stream\stream.h" /> <ClInclude Include="src\stream\stream.h" />
+101 -8
View File
@@ -54,6 +54,67 @@ std::uint8_t checksum(const std::string& segment) {
return static_cast<std::uint8_t>((~sum + 1) & 0xFF); return static_cast<std::uint8_t>((~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<std::uint32_t>(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<long long>(pynum::py_round(std::fabs(value) * 127.0), 127);
return static_cast<std::uint8_t>(static_cast<unsigned>(magnitude) |
(value < 0.0 ? 0x80u : 0x00u));
}
void append_trim_records(const oamd::OamdTrim& trim, std::string* out) {
out->append("<DolbyTrimModeSetup>");
for (int index = 0; index < oamd::kTrimConfigs; ++index) {
const oamd::TrimConfig& record = trim.configs[index];
if (record.kind == oamd::TrimConfigKind::kAbsent) {
continue;
}
out->append("<records><type>");
out->append(std::to_string(index));
out->append("</type>");
if (record.kind == oamd::TrimConfigKind::kDefault) {
out->append("</records>");
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("<centerTrim>" + pynum::format_fixed(centre, 6) + "</centerTrim>");
out->append("<surroundTrim>" + pynum::format_fixed(surround, 6) + "</surroundTrim>");
out->append("<heightTrim>" + pynum::format_fixed(height, 6) + "</heightTrim>");
out->append("<balanceOverhead>" + pynum::format_fixed(overhead, 6) +
"</balanceOverhead>");
out->append("<balanceListener>" + pynum::format_fixed(listener, 6) +
"</balanceListener>");
out->append("</records>");
}
out->append("</DolbyTrimModeSetup>");
}
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("<DolbyAtmosMasterFile><DolbyMasterContentInfo><render_warp_mode>");
out->append(warp);
out->append("</render_warp_mode></DolbyMasterContentInfo>");
append_trim_records(trim, out);
out->append("<DolbyWarpMode>");
out->append(warp);
out->append("</DolbyWarpMode></DolbyAtmosMasterFile>");
}
} // namespace } // namespace
std::string ts(double seconds) { std::string ts(double seconds) {
@@ -102,7 +163,8 @@ std::string build_chna() {
return out; 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) { if (out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null output"); 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[106] = 0x01;
segment9[111] = 0x22; segment9[111] = 0x22;
segment9[112] = static_cast<char>(0xFF); segment9[112] = static_cast<char>(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<char>(
(static_cast<unsigned char>(segment9[152]) & 0xF8u) | trim_warp_mode(trim));
out->push_back(9); out->push_back(9);
put_u16(out, 248); put_u16(out, 248);
out->append(segment9); out->append(segment9);
@@ -152,12 +218,35 @@ Status build_dbmd(std::uint32_t object_count, BinauralMode mode, std::string* ou
segment10[i] = static_cast<char>(0x84); segment10[i] = static_cast<char>(0x84);
} }
// Nine 15-byte configuration trims start at byte 7; the first byte of each // 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 // carries the auto-trim flag in bit 0 (1 = automatic, 0 = manual). A
// default is automatic, so a freshly rendered master opens with automatic // configuration without an explicit record stays automatic, so a freshly
// trims in the renderer. // rendered master opens with automatic trims in the renderer.
for (std::size_t layout = 0; layout < 9; ++layout) { for (std::size_t layout = 0; layout < static_cast<std::size_t>(oamd::kTrimConfigs);
const std::size_t flag = 7u + 15u * layout; ++layout) {
segment10[flag] = static_cast<char>(static_cast<unsigned char>(segment10[flag]) | 0x01u); 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<char>(static_cast<unsigned char>(segment10[base]) | 0x01u);
continue;
}
segment10[base] = static_cast<char>(static_cast<unsigned char>(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<float>(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<char>(balance_byte(overhead));
segment10[base + 14u] = static_cast<char>(balance_byte(listener));
} }
const std::size_t object_modes = 4u + 2u + 1u + 9u * 15u + object_count; const std::size_t object_modes = 4u + 2u + 1u + 9u * 15u + object_count;
for (std::uint32_t i = 10; i < std::min<std::uint32_t>(object_count, 10u + kObjectCount); ++i) { for (std::uint32_t i = 10; i < std::min<std::uint32_t>(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(); return Status::success();
} }
std::string build_axml(const std::vector<Track>& tracks, double duration_sec, std::uint32_t rate) { std::string build_axml(const std::vector<Track>& tracks, double duration_sec, std::uint32_t rate,
const oamd::OamdTrim* trim) {
const double scale = static_cast<double>(rate); const double scale = static_cast<double>(rate);
std::string out; std::string out;
out.reserve(64u * 1024u); out.reserve(64u * 1024u);
@@ -333,6 +423,9 @@ std::string build_axml(const std::vector<Track>& tracks, double duration_sec, st
"_01</audioTrackFormatIDRef>"; "_01</audioTrackFormatIDRef>";
out += "</audioStreamFormat>"; out += "</audioStreamFormat>";
} }
if (trim != nullptr) {
append_atmos_master_block(*trim, &out);
}
out += "</audioFormatExtended></format></coreMetadata>"; out += "</audioFormatExtended></format></coreMetadata>";
out += "</ebuCoreMain>"; out += "</ebuCoreMain>";
return out; return out;
+9 -2
View File
@@ -7,6 +7,7 @@
#include <vector> #include <vector>
#include "foundation/status.h" #include "foundation/status.h"
#include "oamd/oamd_parser.h"
namespace joc::adm { 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); bool binaural_mode_from_name(const char* name, BinauralMode* out);
std::string build_axml(const std::vector<Track>& 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<Track>& tracks, double duration_sec, std::uint32_t rate,
const oamd::OamdTrim* trim = nullptr);
std::string build_chna(); 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 } // namespace joc::adm
+166
View File
@@ -15,6 +15,13 @@ constexpr int kRampDurationIndex[16] = {32, 64, 128, 256, 320, 480,
1000, 1001, 1024, 1600, 1601, 1602, 1000, 1001, 1024, 1600, 1601, 1602,
1920, 2000, 2002, 2048}; 1920, 2000, 2002, 2048};
constexpr std::uint32_t kObjectElementId = 1; 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 kIsfObjectCounts[6] = {4, 8, 10, 14, 15, 30};
constexpr int kStandardBedChannelCount[10] = {2, 1, 1, 2, 2, 2, 2, 2, 2, 1}; 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(); 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<std::uint8_t>(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<double>(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<double>(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<std::uint32_t>(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 } // namespace
int q_of(int k, int n) { 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(); 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<std::size_t>(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, Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out,
ParseTrace* trace) { ParseTrace* trace) {
if (payload == nullptr || out == nullptr) { 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.set_limit_bits(reader.limit());
reader.skip(region_end - reader.position()); 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 { } else {
if (!reader.skip(region_end - reader.position())) { if (!reader.skip(region_end - reader.position())) {
return syntax("OAMD element skip past payload end"); return syntax("OAMD element skip past payload end");
+44
View File
@@ -17,6 +17,38 @@ inline constexpr int kAxes = 3;
inline constexpr int kNQ12 = 62; inline constexpr int kNQ12 = 62;
inline constexpr int kNQ3 = 15; inline constexpr int kNQ3 = 15;
inline constexpr int kQ15Scale = 32768; 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. // (the reference's None), i.e. hold the previous position.
struct SlotUpdate { struct SlotUpdate {
@@ -39,6 +71,11 @@ struct OamdUpdate {
bool content_bed = false; bool content_bed = false;
bool content_isf = false; bool content_isf = false;
bool content_dynamic = 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 { 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); 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 { struct BedAssignment {
bool lfe_only = false; bool lfe_only = false;
bool standard = false; bool standard = false;
+17 -11
View File
@@ -10,20 +10,23 @@ struct FrozenLayout {
const char* name; const char* name;
std::uint32_t out_ch_config; std::uint32_t out_ch_config;
std::uint32_t speaker_bitfield; std::uint32_t speaker_bitfield;
std::uint32_t surround_channels;
std::uint32_t height_channels;
bool carries_trim;
}; };
constexpr FrozenLayout kLayouts[kLayoutCount] = { constexpr FrozenLayout kLayouts[kLayoutCount] = {
{"2.0", 0, 1}, {"2.0", 0, 1, 0, 0, true},
{"3.1", 3, 7}, {"3.1", 3, 7, 0, 0, true},
{"4.0", 0xFF, 9}, {"4.0", 0xFF, 9, 2, 0, true},
{"5.1", 7, 15}, {"5.1", 7, 15, 2, 0, true},
{"7.1", 11, 31}, {"7.1", 11, 31, 4, 0, true},
{"5.1.2", 13, 1039}, {"5.1.2", 13, 1039, 2, 2, true},
{"5.1.4", 14, 2575}, {"5.1.4", 14, 2575, 2, 4, true},
{"7.1.2", 15, 1055}, {"7.1.2", 15, 1055, 4, 2, false},
{"7.1.4", 16, 2591}, {"7.1.4", 16, 2591, 4, 4, false},
{"9.1.4", 19, 2719}, {"9.1.4", 19, 2719, 4, 4, false},
{"9.1.6", 20, 3743}, {"9.1.6", 20, 3743, 4, 6, false},
}; };
void fill(const FrozenLayout& source, LayoutInfo* out) { 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->out_ch_config = source.out_ch_config;
out->speaker_bitfield = source.speaker_bitfield; out->speaker_bitfield = source.speaker_bitfield;
out->channel_count = ejoc_speaker_layout_channel_count(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 } // namespace
@@ -15,6 +15,13 @@ struct LayoutInfo {
std::uint32_t out_ch_config = 0; std::uint32_t out_ch_config = 0;
std::uint32_t speaker_bitfield = 0; std::uint32_t speaker_bitfield = 0;
std::uint32_t channel_count = 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, // All eleven layouts in the reference's order (2.0, 3.1, 4.0, 5.1, 7.1, 5.1.2,
+89 -12
View File
@@ -1,5 +1,6 @@
#define EJOC_BUILD_DLL #define EJOC_BUILD_DLL
#include "eac3joc_core.h" #include "eac3joc_core.h"
#include "speaker/speaker_trim.h"
#include "speaker_layouts.h" #include "speaker_layouts.h"
#include <algorithm> #include <algorithm>
@@ -216,6 +217,7 @@ public:
const std::uint8_t* region_indices, const std::uint8_t* region_indices,
const std::uint8_t* height_enabled, const std::uint8_t* height_enabled,
const double* object_gains, const double* object_gains,
const ejoc_speaker_controls* controls,
double* output) noexcept { double* output) noexcept {
error_[0] = '\0'; error_[0] = '\0';
if (!input || !output) { if (!input || !output) {
@@ -227,11 +229,21 @@ public:
if (metadata_count && (!metadata_offsets || !ramp_durations || !positions_q15)) { if (metadata_count && (!metadata_offsets || !ramp_durations || !positions_q15)) {
return fail("metadata arrays are null while metadata_count is nonzero"); 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, if (!validate_metadata(sample_count, metadata_count, metadata_offsets,
positions_q15, region_indices, object_gains)) { positions_q15, region_indices, object_gains)) {
return -1; 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<std::size_t>(sample_count) * layout_->channel_count, 0.0); std::fill(output, output + static_cast<std::size_t>(sample_count) * layout_->channel_count, 0.0);
const bool has_lfe = (layout_->speaker_bitfield & 0x4u) != 0; const bool has_lfe = (layout_->speaker_bitfield & 0x4u) != 0;
const std::size_t total_blocks = sample_count / kBlock; const std::size_t total_blocks = sample_count / kBlock;
@@ -239,14 +251,14 @@ public:
for (std::size_t block = 0; block < total_blocks; ++block) { for (std::size_t block = 0; block < total_blocks; ++block) {
while (event < metadata_count && aligned_block(metadata_offsets[event]) == block) { while (event < metadata_count && aligned_block(metadata_offsets[event]) == block) {
apply_event(event, ramp_durations, positions_q15, region_indices, apply_event(event, ramp_durations, positions_q15, region_indices,
height_enabled, object_gains); height_enabled, object_gains, active);
++event; ++event;
} }
mix_block(input, output, block, has_lfe); mix_block(input, output, block, has_lfe);
} }
while (event < metadata_count && aligned_block(metadata_offsets[event]) == total_blocks) { while (event < metadata_count && aligned_block(metadata_offsets[event]) == total_blocks) {
apply_event(event, ramp_durations, positions_q15, region_indices, apply_event(event, ramp_durations, positions_q15, region_indices,
height_enabled, object_gains); height_enabled, object_gains, active);
++event; ++event;
} }
if (event != metadata_count) { if (event != metadata_count) {
@@ -268,6 +280,42 @@ private:
return -1; 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, bool validate_metadata(const std::uint32_t sample_count,
const std::uint32_t metadata_count, const std::uint32_t metadata_count,
const std::uint32_t* metadata_offsets, const std::uint32_t* metadata_offsets,
@@ -318,13 +366,28 @@ private:
const std::uint8_t region_index, const std::uint8_t region_index,
const bool enable_height, const bool enable_height,
const double object_gain, const double object_gain,
const ejoc_speaker_controls& controls,
const bool bypass_trim,
ChannelGains& output) const noexcept { ChannelGains& output) const noexcept {
output.fill(0.0); output.fill(0.0);
const auto& region = layout_->regions[region_index]; const auto& region = layout_->regions[region_index];
const double u = static_cast<double>(position[0]) / kQ15Scale; const double u = static_cast<double>(position[0]) / kQ15Scale;
const double v = static_cast<double>(position[1]) / kQ15Scale; const double v = static_cast<double>(position[1]) / kQ15Scale;
const double w = static_cast<double>(position[2]) / kQ15Scale; const double w = std::max(static_cast<double>(position[2]) / kQ15Scale, 0.0);
const double floor_v = clamp(std::ldexp(v, floor_y_exponent_), 0.0, 1.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{}; PointGains floor{};
plane_gains(region, region.axis0_groups, region.axis0_group_count, plane_gains(region, region.axis0_groups, region.axis0_group_count,
u, floor_v, region.mode, floor); u, floor_v, region.mode, floor);
@@ -332,7 +395,7 @@ private:
if (region.mode == 3) { if (region.mode == 3) {
PointGains height{}; PointGains height{};
plane_gains(region, region.axis1_groups, region.axis1_group_count, 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; const double z = enable_height ? clamp(w, 0.0, kQ15Max) : 0.0;
if (z >= kQ15Max) { if (z >= kQ15Max) {
point = height; point = height;
@@ -345,21 +408,33 @@ private:
} }
} }
} }
const double y_term = clamp(v / 0.6, 0.0, 1.0); double gain = object_gain;
const double z_term = clamp((w - 0.2) / 0.8, 0.0, 1.0); if (!bypass_trim) {
const double amount = clamp(y_term + z_term, 0.0, 1.0); if (controls.trim_mode == EJOC_SPEAKER_TRIM_AUTOMATIC) {
const double gain = std::pow(10.0, attenuation_db_ * amount / 20.0) * object_gain; 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) { for (std::size_t index = 0; index < region.point_count; ++index) {
output[region.points[index].speaker_id] = point[index] * gain; 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, void apply_event(const std::size_t event,
const std::uint32_t* ramp_durations, const std::uint32_t* ramp_durations,
const std::uint16_t* positions_q15, const std::uint16_t* positions_q15,
const std::uint8_t* region_indices, const std::uint8_t* region_indices,
const std::uint8_t* height_enabled, 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]); const std::uint32_t blocks = ramp_blocks(ramp_durations[event]);
for (std::size_t object = 0; object < kObjects; ++object) { for (std::size_t object = 0; object < kObjects; ++object) {
const std::size_t object_event = event * 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 std::uint8_t region = region_indices ? region_indices[object_event] : 0;
const bool height = !height_enabled || height_enabled[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 double object_gain = object_gains ? object_gains[object_event] : 1.0;
const bool bypass = controls.trim_bypass[object + 1] != 0u;
ChannelGains next{}; 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) { for (std::size_t channel = 0; channel < layout_->channel_count; ++channel) {
const double difference = next[channel] - current_[object][channel]; const double difference = next[channel] - current_[object][channel];
target_[object][channel] = next[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* region_indices,
const uint8_t* height_enabled, const uint8_t* height_enabled,
const double* object_gains, const double* object_gains,
const ejoc_speaker_controls* controls,
double* output_interleaved) { double* output_interleaved) {
if (!handle) { if (!handle) {
return -1; return -1;
@@ -489,7 +566,7 @@ int EJOC_CALL ejoc_speaker_renderer_process(
return static_cast<ejoc::speaker::Renderer*>(handle)->process( return static_cast<ejoc::speaker::Renderer*>(handle)->process(
objects16_interleaved, sample_count, metadata_count, objects16_interleaved, sample_count, metadata_count,
metadata_offsets, ramp_durations, positions_q15, 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" } // extern "C"
+22 -1
View File
@@ -26,8 +26,29 @@ Status step(SpeakerStep* context, const std::vector<float>& objects16_planar,
static_cast<std::size_t>(JOC_FRAME_SAMPLES) * context->layout.channel_count, 0.0); static_cast<std::size_t>(JOC_FRAME_SAMPLES) * context->layout.channel_count, 0.0);
context->last_had_payload = update != nullptr; 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; std::uint32_t ramp = 0;
if (update != nullptr) { 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); context->state.apply(*update);
ramp = update->ramp_duration_samples; ramp = update->ramp_duration_samples;
context->last_block_offset = update->block_offset_samples; context->last_block_offset = update->block_offset_samples;
@@ -53,7 +74,7 @@ Status step(SpeakerStep* context, const std::vector<float>& objects16_planar,
context->handle, context->interleaved.data(), JOC_FRAME_SAMPLES, event_count, context->handle, context->interleaved.data(), JOC_FRAME_SAMPLES, event_count,
event_count != 0u ? &offset : nullptr, event_count != 0u ? &ramp : nullptr, event_count != 0u ? &offset : nullptr, event_count != 0u ? &ramp : nullptr,
event_count != 0u ? &positions[0][0] : nullptr, nullptr, nullptr, nullptr, event_count != 0u ? &positions[0][0] : nullptr, nullptr, nullptr, nullptr,
context->output.data()); &context->controls, context->output.data());
if (result != 0) { if (result != 0) {
const char* message = ejoc_speaker_renderer_last_error(context->handle); const char* message = ejoc_speaker_renderer_last_error(context->handle);
const std::string text = const std::string text =
+5
View File
@@ -9,6 +9,7 @@
#include "foundation/status.h" #include "foundation/status.h"
#include "oamd/oamd_parser.h" #include "oamd/oamd_parser.h"
#include "speaker/speaker_layout_lookup.h" #include "speaker/speaker_layout_lookup.h"
#include "speaker/speaker_trim.h"
namespace joc::speaker { namespace joc::speaker {
@@ -23,6 +24,10 @@ struct SpeakerStep {
std::uint32_t last_ramp_duration = 0; std::uint32_t last_ramp_duration = 0;
std::uint32_t last_object_count = 0; std::uint32_t last_object_count = 0;
bool last_had_payload = false; 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 布局给出。这里存 // bed 声道是固定通道:OAMD 不给位置,方位由声明的 bed 布局给出。这里存
// 对象槽 → Q15 方位;bed_override 为 false 的槽仍按 OAMD 坐标声像。 // 对象槽 → Q15 方位;bed_override 为 false 的槽仍按 OAMD 坐标声像。
std::uint16_t bed_positions[oamd::kObjects][oamd::kAxes] = {}; std::uint16_t bed_positions[oamd::kObjects][oamd::kAxes] = {};
+155
View File
@@ -0,0 +1,155 @@
#include "speaker/speaker_trim.h"
#include <algorithm>
#include <cmath>
#include <string>
#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<double>(kBalanceSteps));
if (steps == 0) {
return position;
}
const double slope = static_cast<double>(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
+49
View File
@@ -0,0 +1,49 @@
// Port of src/trim_balance.py.
#pragma once
#include <cstdint>
#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
+2
View File
@@ -59,6 +59,8 @@ void Stream::reset_state() {
speaker_.state.reset(); speaker_.state.reset();
speaker_.output.clear(); speaker_.output.clear();
speaker_.last_had_payload = false; speaker_.last_had_payload = false;
// Back to the initial controls; the next trim element replaces them.
speaker_.controls_ready = false;
} }
if (binaural_ready_) { if (binaural_ready_) {
binaural_.reset(); binaural_.reset();
+113 -3
View File
@@ -313,6 +313,91 @@ int probe_config_idx(const std::string& path) {
return config; 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<char> 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<std::uint8_t> 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<std::streamsize>(chunk.size()));
const std::streamsize got = input.gcount();
if (got > 0) {
reader.push(reinterpret_cast<const std::uint8_t*>(chunk.data()),
static_cast<std::size_t>(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 // 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 // once the peak is known, which is what makes --clip-action possible; the
// reference CLI spools for the same reason. // 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; ::joc::speaker::SpeakerStep speaker;
bool bed_positions_ready = false; 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; const bool speaker_enabled = cfg.operation == JOC_OP_SPEAKER;
if (speaker_enabled) { if (speaker_enabled) {
if (!::joc::speaker::layout_by_name(cfg.layout.c_str(), &speaker.layout)) { 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", return finalize_with_error(JOC_ERR_RENDER_FAILED, "render",
"cannot create the speaker renderer"); "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, bus.emit(JOC_EV_RENDERER_INITIALIZED, JOC_STAGE_RENDER, JOC_LOG_INFO,
fmt("speaker renderer: %s, %u channels", speaker.layout.name, fmt("speaker renderer: %s, %u channels", speaker.layout.name,
speaker.layout.channel_count)); speaker.layout.channel_count));
@@ -2107,13 +2214,16 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu
} else { } else {
const double duration_sec = const double duration_sec =
static_cast<double>(processed) * static_cast<std::int64_t>(JOC_FRAME_SAMPLES) / 48000.0; static_cast<double>(processed) * static_cast<std::int64_t>(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(); const std::string chna = ::joc::adm::build_chna();
std::string dbmd; std::string dbmd;
const ::joc::adm::BinauralMode mode = const ::joc::adm::BinauralMode mode =
static_cast<::joc::adm::BinauralMode>(cfg.adm_binaural_mode); static_cast<::joc::adm::BinauralMode>(cfg.adm_binaural_mode);
const Status dbmd_status = const Status dbmd_status = ::joc::adm::build_dbmd(
::joc::adm::build_dbmd(::joc::io::AdmBwfWriter::kChannels, mode, &dbmd); ::joc::io::AdmBwfWriter::kChannels, mode, &dbmd,
adm_trim_present ? &adm_trim : nullptr);
if (!dbmd_status.ok()) { if (!dbmd_status.ok()) {
failure = dbmd_status.message(); failure = dbmd_status.message();
} else { } else {