Port the OAMD metadata rendering and the frame-level JOC core
ci / ubuntu-latest (push) Failing after 19s
ci / macos-latest (push) Canceled after 0s
ci / windows-latest (push) Canceled after 0s
ci / release (push) Canceled after 0s

The JOC bitstream reports a per-frame parse status instead of treating every
anomaly as an exception: an unsupported downmix configuration, a clamped
object count, an unknown parameter-band count, bad offset_ts values and
trailing bytes each get their own status. The raw 5-bit offset_ts values are
kept as coded and validated to be strictly increasing per object, and the
clipgain uses 2^(x-4) * (1 + y/32).

The JOC core gains a frame-level state machine. It keeps the per-object
previous matrix and its presence marker, the seq counter, the 8-plane
passthrough history, the LFE clipgain ramp start and the previous actual
core-channel count. Normal, hold, hard-cut and passthrough frames are
distinguished: a frame without a previous state interpolates per the
data-point rules, an object that disappears keeps publishing a zero matrix,
a frame without metadata passes the 8 core planes through to slots 0..7 with
the 1217-sample delay, and a declared core-channel-count change hard-cuts and
clears the LFE ring while the clipgain ramp state is kept. The core LFE
applies the full clipgain as a sample-wise ramp before its delay.

The OAMD parser exposes the full frame state: every object's state in every
position block, the programme assignment and the trim element, with multiple
blocks and up to 30 declared objects accepted. The speaker renderer applies
this state: per-block gain/inactive, zone/elevation, the size kernel, snap,
divergence, element 5 refinement, the priority size budget, ISF basis
matrices and bed channel distribution, scheduled on the 32-sample metadata
grid with block alignment and gain ramps. The size kernel, ISF basis
assembly, bed routing and compensation are computed in oamd_math from the
speaker layout geometry; bed labels and reference positions derive from the
layout tables. The six ISF configurations and twelve layout descriptors are
compiled in as static tables (oamd/isf_tables.h); the JSON loader and the
configure-time embedding are gone. The layout tables gain 6.0, and the layout
lookup, bed map and trim probe follow.

The binaural position timeline consumes the full frame state as well: every
position block is scheduled at its own offset and ramp, and its track list
grows with the declared object count, so multi-block and over-16-object
programmes are rendered instead of rejected. The JOC binaural input stays one
LFE plus fifteen object sources; the extra tracks are followed but not
panned.

The file task and the stream parse the full state for speaker and binaural,
while the ADM export keeps the 15-track single-block view. The public headers
carry the new status values, the frame-level entry point and the raw offset_ts
description; the internal ABI versions go to 6 (joc_core), 2 (frame params and
EMDF info) and 4 (eac3joc).

ctest covers the frame-level core (clipgain formula and LFE ramp, offset_ts
validation, frame statuses, the 8-plane passthrough, the seq state machine and
the no-previous-state interpolation), the OAMD parser state, the compiled-in
ISF tables and the binaural timeline. The READMEs and the mathematical notes
describe the 6.0 layout, the frame state machine, the metadata application and
the ISF lookup tables.
This commit is contained in:
TheM14 committed 2026-11-08 14:04:21 +08:00
1 parent a2760286b0
commit 0bb82e4115
41 files changed
+8182 -678

No files matched your search

+301
View File
@@ -1,6 +1,7 @@
#include "joc_bitstream/bed_layout_map.h"
#include <cmath>
#include <cstring>
#include "speaker/speaker_layouts.h"
@@ -100,6 +101,9 @@ constexpr LayoutName kLayoutNames[] = {
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
bit(BedChannel::kCenter) | bit(BedChannel::kLfe), "5.1"},
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround), "6.0"},
{bit(BedChannel::kLeft) | bit(BedChannel::kRight) |
bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) |
bit(BedChannel::kCenter) | bit(BedChannel::kLfe) |
@@ -328,4 +332,301 @@ bool layout_bed_anchor(std::uint32_t out_ch_config, BedChannel channel,
return false;
}
namespace {
// ETSI TS 103 420 Table B.10: the fixed ADM position of every Table 12 bed
// channel. bed_output_description() matches these points against the target
// layout; the kChannels table above keeps the ADM/binaural path's coordinates.
struct TableB10Position {
BedChannel channel;
double xyz[3];
};
constexpr TableB10Position kTableB10[oamd::kBedLabelCount] = {
{BedChannel::kLeft, {-1.0, 1.0, 0.0}},
{BedChannel::kRight, {1.0, 1.0, 0.0}},
{BedChannel::kCenter, {0.0, 1.0, 0.0}},
{BedChannel::kLfe, {-1.0, 1.0, -1.0}},
{BedChannel::kLeftSurround, {-1.0, 0.0, 0.0}},
{BedChannel::kRightSurround, {1.0, 0.0, 0.0}},
{BedChannel::kLeftRearSurround, {-1.0, -1.0, 0.0}},
{BedChannel::kRightRearSurround, {1.0, -1.0, 0.0}},
{BedChannel::kTopFrontLeft, {-1.0, 1.0, 1.0}},
{BedChannel::kTopFrontRight, {1.0, 1.0, 1.0}},
{BedChannel::kTopSideLeft, {-1.0, 0.0, 1.0}},
{BedChannel::kTopSideRight, {1.0, 0.0, 1.0}},
{BedChannel::kTopBackLeft, {-1.0, -1.0, 1.0}},
{BedChannel::kTopBackRight, {1.0, -1.0, 1.0}},
{BedChannel::kLeftWide, {-1.0, 21.0 / 31.0, 0.0}},
{BedChannel::kRightWide, {1.0, 21.0 / 31.0, 0.0}},
{BedChannel::kLfe2, {1.0, 1.0, -1.0}},
};
// bed_layout.BED_REFERENCE_POSITIONS_Q15; index 3 is the non-spatial sentinel.
constexpr std::int32_t kReferencePositions[kBedReferencePositionCount][3] = {
{0, 0, 0}, {32767, 0, 0}, {16384, 0, 0}, {-1, -1, -1},
{0, 16384, 0}, {32767, 16384, 0}, {0, 32767, 0}, {32767, 32767, 0},
{16384, 32767, 0}, {5285, 0, 0}, {27483, 0, 0}, {0, 5285, 0},
{32767, 5285, 0}, {7928, 0, 32767}, {24840, 0, 32767}, {7928, 7928, 32767},
{24840, 7928, 32767}, {7928, 16384, 32767}, {24840, 16384, 32767},
{7928, 24840, 32767}, {24840, 24840, 32767}, {7928, 32767, 32767},
{24840, 32767, 32767}, {11099, 0, 0}, {21669, 0, 0}, {0, 11099, 0},
{32767, 11099, 0}, {0, 21669, 0}, {32767, 21669, 0}, {0, 27483, 0},
{32767, 27483, 0}, {5285, 32767, 0}, {27483, 32767, 0}, {11099, 32767, 0},
{21669, 32767, 0}, {16384, 0, 32767}, {16384, 16384, 32767},
{16384, 32767, 32767},
};
// 自动 trim 的每个布局基准衰减,单位 dB。
struct AttenuationEntry {
const char* compact_name;
double db;
};
constexpr AttenuationEntry kAttenuation[] = {
{"20", -4.5}, {"31", -4.5}, {"40", -3.0}, {"51", -3.0},
{"60", -1.5}, {"71", -1.5}, {"512", -2.25}, {"514", -1.5},
{"712", -0.75}, {"714", 0.0}, {"914", 0.0}, {"916", 0.0},
};
constexpr int kMaxChannels = static_cast<int>(ejoc::speaker_tables::kMaxChannels);
void set_sentinel(std::int32_t coordinate[3]) {
coordinate[0] = -1;
coordinate[1] = -1;
coordinate[2] = -1;
}
bool is_sentinel(const std::int32_t coordinate[3]) {
return coordinate[0] == -1 && coordinate[1] == -1 && coordinate[2] == -1;
}
bool is_lfe_label(BedChannel channel) {
return channel == BedChannel::kLfe || channel == BedChannel::kLfe2;
}
const TableB10Position* table_b10(BedChannel channel) {
for (const TableB10Position& entry : kTableB10) {
if (entry.channel == channel) {
return &entry;
}
}
return nullptr;
}
// ADM 笛卡尔坐标 -> 渲染器方位 (u 右, v 后, w 上)。
void render_coordinates(const double xyz[3], double out[3]) {
out[0] = (xyz[0] + 1.0) * 0.5;
out[1] = (1.0 - xyz[1]) * 0.5;
out[2] = xyz[2];
}
// 渲染器方位的欧氏距离,比较的是归一化到 0..1 的坐标。
double render_distance(const double left[3], const double right[3]) {
const double du = left[0] - right[0];
const double dv = left[1] - right[1];
const double dw = left[2] - right[2];
return std::sqrt(du * du + dv * dv + dw * dw);
}
// Reference-position matching: the non-spatial sentinel resolves to index 3.
int nearest_reference(const std::int32_t coordinate[3]) {
if (is_sentinel(coordinate)) {
return 3;
}
const double position[3] = {
static_cast<double>(coordinate[0]) / 32768.0,
static_cast<double>(coordinate[1]) / 32768.0,
static_cast<double>(coordinate[2]) / 32768.0,
};
int best = 3;
double best_distance = 0.0;
bool have_best = false;
for (int index = 0; index < kBedReferencePositionCount; ++index) {
const std::int32_t* candidate = kReferencePositions[index];
if (candidate[0] == -1 && candidate[1] == -1 && candidate[2] == -1) {
continue;
}
const double point[3] = {
static_cast<double>(candidate[0]) / 32768.0,
static_cast<double>(candidate[1]) / 32768.0,
static_cast<double>(candidate[2]) / 32768.0,
};
const double distance = render_distance(position, point);
if (!have_best || distance < best_distance) {
best = index;
best_distance = distance;
have_best = true;
}
}
return best;
}
// 目标布局里离 desired 最近、且在 0.05 容差内的输出声道;没有返回 -1。
int nearest_channel(const double desired[3], const std::int32_t positions[][3],
std::uint32_t channel_count) {
int best = -1;
double best_distance = 0.0;
bool have_best = false;
for (std::uint32_t index = 0; index < channel_count; ++index) {
if (is_sentinel(positions[index])) {
continue;
}
const double point[3] = {
static_cast<double>(positions[index][0]) / 32768.0,
static_cast<double>(positions[index][1]) / 32768.0,
static_cast<double>(positions[index][2]) / 32768.0,
};
const double distance = render_distance(desired, point);
if (!have_best || distance < best_distance) {
best = static_cast<int>(index);
best_distance = distance;
have_best = true;
}
}
if (have_best && best_distance <= 0.05) {
return best;
}
return -1;
}
// 输出声道 index -> 该声道的 Q15 方位;LFE 与非空间声道给 sentinel。
void channel_positions(const speaker::LayoutInfo& layout,
std::int32_t positions[][3]) {
for (int index = 0; index < kMaxChannels; ++index) {
set_sentinel(positions[index]);
}
const auto* geometry = ejoc::speaker_tables::find_layout(layout.speaker_bitfield);
if (geometry != nullptr) {
bool known[kMaxChannels] = {};
std::int32_t found[kMaxChannels][3];
for (int index = 0; index < kMaxChannels; ++index) {
set_sentinel(found[index]);
}
for (const auto& region : geometry->regions) {
for (std::size_t point = 0; point < region.point_count; ++point) {
const auto speaker = static_cast<int>(region.points[point].speaker_id);
if (speaker < 0 || speaker >= kMaxChannels || known[speaker]) {
continue;
}
for (int axis = 0; axis < 3; ++axis) {
found[speaker][axis] =
static_cast<std::int32_t>(region.points[point].coordinate_q15[axis]);
}
known[speaker] = true;
}
}
const std::uint32_t count =
layout.channel_count < static_cast<std::uint32_t>(kMaxChannels)
? layout.channel_count
: static_cast<std::uint32_t>(kMaxChannels);
for (std::uint32_t index = 0; index < count; ++index) {
const char* name = layout.channel_names != nullptr ? layout.channel_names[index]
: nullptr;
if (name != nullptr && std::strcmp(name, "LFE") == 0) {
continue;
}
if (known[index]) {
for (int axis = 0; axis < 3; ++axis) {
positions[index][axis] = found[index][axis];
}
}
}
}
}
int channel_index(const speaker::LayoutInfo& layout, const char* name) {
if (name == nullptr || layout.channel_names == nullptr) {
return -1;
}
for (std::uint32_t index = 0; index < layout.channel_count; ++index) {
if (std::strcmp(layout.channel_names[index], name) == 0) {
return static_cast<int>(index);
}
}
return -1;
}
double attenuation_db(const speaker::LayoutInfo& layout) {
for (const AttenuationEntry& entry : kAttenuation) {
if (std::strcmp(entry.compact_name, layout.compact_name) == 0) {
return entry.db;
}
}
return 0.0;
}
} // namespace
const std::int32_t (*bed_reference_positions_q15())[3] {
return kReferencePositions;
}
BedOutputDescription bed_output_description(const speaker::LayoutInfo& layout) {
BedOutputDescription description;
std::int32_t positions[kMaxChannels][3];
channel_positions(layout, positions);
const std::uint32_t output_count =
layout.channel_count < static_cast<std::uint32_t>(kMaxChannels)
? layout.channel_count
: static_cast<std::uint32_t>(kMaxChannels);
description.output_labels.reserve(output_count);
for (std::uint32_t index = 0; index < output_count; ++index) {
description.output_labels.push_back(nearest_reference(positions[index]));
}
for (int label = 0; label < oamd::kBedLabelCount; ++label) {
const BedChannel channel = static_cast<BedChannel>(label);
const char* name = channel == BedChannel::kLfe2 ? nullptr
: bed_channel_short_name(channel);
int slot = -1;
if (name != nullptr && name[0] != '\0') {
slot = channel_index(layout, name);
}
if (slot < 0 && channel == BedChannel::kLfe2) {
slot = channel_index(layout, "LFE");
}
const TableB10Position* position = table_b10(channel);
double desired[3] = {0.0, 0.0, 0.0};
if (position != nullptr) {
render_coordinates(position->xyz, desired);
}
if (slot < 0 && position != nullptr) {
slot = nearest_channel(desired, positions, output_count);
}
if (slot < 0) {
description.label_to_slot[label] = is_lfe_label(channel) ? -1 : -2;
} else {
description.label_to_slot[label] = slot;
}
if (is_lfe_label(channel)) {
description.direct_output_labels[label] = 3;
} else {
int best = 3;
double best_distance = 0.0;
bool have_best = false;
for (int index = 0; index < kBedReferencePositionCount; ++index) {
const std::int32_t* candidate = kReferencePositions[index];
if (candidate[0] == -1 && candidate[1] == -1 && candidate[2] == -1) {
continue;
}
const double point[3] = {
static_cast<double>(candidate[0]) / 32768.0,
static_cast<double>(candidate[1]) / 32768.0,
static_cast<double>(candidate[2]) / 32768.0,
};
const double distance = render_distance(desired, point);
if (!have_best || distance < best_distance) {
best = index;
best_distance = distance;
have_best = true;
}
}
description.direct_output_labels[label] = best;
}
}
description.automatic_attenuation_db = attenuation_db(layout);
return description;
}
} // namespace joc::joc