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

+725 -11
View File
@@ -19,6 +19,8 @@
#include "hrtf/jochrtf.h"
#include "hrtf/rosella_model.h"
#include "hrtf/rosella_renderer.h"
#include "joc_bitstream/joc_huffman_tables.h"
#include "joc_bitstream/joc_parser.h"
#include "foundation/mini_json.h"
#include "foundation/sha256.h"
#include "io/inflate.h"
@@ -33,6 +35,7 @@
#include "speaker/speaker_trim.h"
#include "stream/stream.h"
#include "task/task.h"
#include "timeline/position_timeline.h"
namespace {
@@ -580,8 +583,8 @@ void test_speaker_warp_and_trim() {
CHECK(joc::speaker::parameters_from_oamd(nullptr, layout514, &automatic514).ok());
// Warp 0 leaves the floor Y alone; warp 1 doubles it before the clamp.
const std::vector<double> warp0 = {0.9126902722074105, 0, 0, 0, 0.18155106823708969, 0};
const std::vector<double> warp1 = {0.85973219419392444, 0, 0, 0, 0.35612480624995158, 0};
const std::vector<double> warp0 = {0.85973646214220434, 0, 0, 0, 0.35611450268596406, 0};
const std::vector<double> warp1 = {0.65801380052399572, 0, 0, 0, 0.65801380052399572, 0};
const std::vector<double> clamped = {0, 0, 0, 0, 0.70794578438413791, 0};
check_gains(speaker_gains(layout51, automatic51, 0, 4096, 0), warp0);
ejoc_speaker_controls warped = automatic51;
@@ -605,10 +608,10 @@ void test_speaker_warp_and_trim() {
explicit_controls.surround_db = -6.0;
explicit_controls.listener_balance = 9.0 / 16.0;
explicit_controls.overhead_balance = -9.0 / 16.0;
const std::vector<double> trimmed = {0.68117911059016056, 0, 0, 0, 0.41744281702545366, 0};
const std::vector<double> trimmed = {0.36269933962647599, 0, 0, 0, 0.71183753429070229, 0};
check_gains(speaker_gains(layout51, explicit_controls, 0, 4096, 0), trimmed);
// Per-object bypass keeps warp but drops balance and trim.
const std::vector<double> bypassed = {0.92387494614065435, 0, 0, 0, 0.38269450465561039, 0};
const std::vector<double> bypassed = {0.70710678118654757, 0, 0, 0, 0.70710678118654757, 0};
check_gains(speaker_gains(layout51, explicit_controls, 0, 4096, 0, true), bypassed);
// Centre trim is local to the front-centre region; height trim scales with Z.
@@ -789,7 +792,7 @@ void test_speaker_step_trim_events() {
update.trim.global_trim_mode = 0;
update.trim.object_count = 16;
CHECK(joc::speaker::step(&step, frame, &update, &error).ok());
const std::vector<double> warp1 = {0.85973219419392444, 0, 0, 0, 0.35612480624995158, 0};
const std::vector<double> warp1 = {0.65801380052399572, 0, 0, 0, 0.65801380052399572, 0};
std::vector<double> gains(step.layout.channel_count);
for (std::uint32_t channel = 0; channel < step.layout.channel_count; ++channel) {
gains[channel] = step.output[read + channel];
@@ -803,8 +806,8 @@ void test_speaker_step_trim_events() {
for (std::uint32_t channel = 0; channel < step.layout.channel_count; ++channel) {
gains[channel] = step.output[read + channel];
}
const std::vector<double> half_warp1 = {0.9461247896349676, 0, 0, 0,
0.18820181552760662, 0};
const std::vector<double> half_warp1 = {0.89123112643513935, 0, 0, 0,
0.36916001977848539, 0};
check_gains(gains, half_warp1);
// A new trim element switches the controls back to warp 0.
@@ -818,8 +821,8 @@ void test_speaker_step_trim_events() {
for (std::uint32_t channel = 0; channel < step.layout.channel_count; ++channel) {
gains[channel] = step.output[read + channel];
}
const std::vector<double> half_warp0 = {0.96001622724230795, 0, 0, 0,
0.094556249720175942, 0};
const std::vector<double> half_warp0 = {0.94612591737884011, 0, 0, 0,
0.18819614606507579, 0};
check_gains(gains, half_warp0);
// A warp 2 element renders through the expanded layout and folds.
@@ -1399,8 +1402,20 @@ void test_clipgain_first_frame_uses_its_own_gain() {
// A fresh handle takes the current gain with no transition.
CHECK(peak > 1.0e-3f);
CHECK(scaled);
// LFE bypasses the scalar, so its samples are bit-identical.
CHECK(std::memcmp(unity.data(), doubled.data(), sizeof(float) * EJOC_FRAME_SAMPLES) == 0);
// The LFE now carries the same full clipgain: a fresh ring starts with 1217
// zero samples and the rest scales by the frame's gain (2x is exact).
bool lfe_scaled = true;
for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) {
if (doubled[static_cast<std::size_t>(sample)] !=
2.0f * unity[static_cast<std::size_t>(sample)]) {
lfe_scaled = false;
break;
}
}
CHECK(lfe_scaled);
for (int sample = 0; sample < JOC_LFE_DELAY_SAMPLES; ++sample) {
CHECK(unity[static_cast<std::size_t>(sample)] == 0.0f);
}
}
void test_clipgain_change_applies_to_the_current_frame() {
@@ -1480,6 +1495,698 @@ void test_clipgain_reset_clears_the_previous_gain() {
sizeof(float) * after_reset.size()) == 0);
}
// ---------------------------------------------------------------------------
// JOC frame-level core: clipgain formula, frame status, offset_ts validation,
// LFE clipgain ramp, 8-plane passthrough, seq state machine and the
// no-previous-state interpolation rules.
// ---------------------------------------------------------------------------
struct JocBitWriter {
std::vector<std::uint8_t> bytes;
std::size_t bit_count = 0;
void write(std::uint32_t value, int count) {
for (int index = count - 1; index >= 0; --index) {
if ((bit_count & 7u) == 0u) {
bytes.push_back(0u);
}
if (((value >> index) & 1u) != 0u) {
bytes.back() |= static_cast<std::uint8_t>(1u << (7u - (bit_count & 7u)));
}
++bit_count;
}
}
};
bool huffman_path(const int (*nodes)[2], int count, int value, std::vector<int>* path) {
struct Entry {
int node;
std::vector<int> path;
};
std::vector<Entry> queue;
queue.push_back({0, {}});
for (std::size_t index = 0; index < queue.size() && index < 4096u; ++index) {
const int node = queue[index].node;
if (node < 0) {
if (-node - 1 == value) {
*path = queue[index].path;
return true;
}
continue;
}
if (node >= count) {
continue;
}
std::vector<int> left = queue[index].path;
left.push_back(0);
queue.push_back({nodes[node][0], std::move(left)});
std::vector<int> right = queue[index].path;
right.push_back(1);
queue.push_back({nodes[node][1], std::move(right)});
}
return false;
}
void write_huffman_symbol(JocBitWriter* writer, const int (*nodes)[2], int count, int value) {
std::vector<int> path;
CHECK(huffman_path(nodes, count, value, &path));
for (const int bit : path) {
writer->write(static_cast<std::uint32_t>(bit), 1);
}
}
// One synthetic ID14 payload with at most one present object (object 0), one
// parameter band, dense syntax and 5 core channels.
struct Id14Spec {
std::uint8_t dmx_config_idx = 3;
std::uint32_t num_objects_bits = 0; // declares one object
std::uint8_t ext_config_idx = 0;
std::uint8_t clipgain_x = 4;
std::uint8_t clipgain_y = 0;
std::uint32_t seq = 1;
bool present = false;
std::uint8_t n_bands_idx = 0; // one parameter band
std::uint8_t sparse = 0;
std::uint8_t quant = 0;
std::uint8_t slope = 0;
bool two_data_points = false;
std::uint8_t offset0 = 0;
std::uint8_t offset1 = 0;
bool trailing = false;
};
std::vector<std::uint8_t> make_id14_payload(const Id14Spec& spec) {
JocBitWriter writer;
writer.write(spec.dmx_config_idx, 3);
writer.write(spec.num_objects_bits, 6);
writer.write(spec.ext_config_idx, 3);
writer.write(spec.clipgain_x, 3);
writer.write(spec.clipgain_y, 5);
writer.write(spec.seq, 10);
const std::uint32_t declared = spec.num_objects_bits + 1u;
const std::uint32_t objects = declared > 15u ? 15u : declared;
for (std::uint32_t object = 0; object < objects; ++object) {
const bool present = object == 0u && spec.present;
writer.write(present ? 1u : 0u, 1);
if (!present) {
continue;
}
writer.write(spec.n_bands_idx, 3);
writer.write(spec.sparse, 1);
writer.write(spec.quant, 1);
writer.write(spec.slope, 1);
writer.write(spec.two_data_points ? 1u : 0u, 1);
if (spec.slope == 1u) {
writer.write(spec.offset0, 5);
if (spec.two_data_points) {
writer.write(spec.offset1, 5);
}
}
}
if (spec.present && spec.sparse == 0u) {
constexpr int kCount = static_cast<int>(sizeof(joc_huff_code_coarse_generic) /
sizeof(joc_huff_code_coarse_generic[0]));
const int points = spec.two_data_points ? 2 : 1;
for (int point = 0; point < points; ++point) {
for (int channel = 0; channel < 5; ++channel) {
write_huffman_symbol(&writer, joc_huff_code_coarse_generic, kCount, 0);
}
}
}
while ((writer.bit_count & 7u) != 0u) {
writer.write(0u, 1);
}
if (spec.trailing) {
writer.bytes.push_back(0u);
writer.bytes.push_back(0u);
}
return writer.bytes;
}
void test_joc_clipgain_formula() {
struct Case {
std::uint8_t x;
std::uint8_t y;
double expected;
};
const Case cases[] = {
{0, 0, 0.0625}, {2, 8, 0.3125}, {3, 0, 0.5}, {4, 0, 1.0},
{4, 16, 1.5}, {5, 16, 3.0}, {7, 31, 15.75},
};
for (const Case& item : cases) {
Id14Spec spec;
spec.clipgain_x = item.x;
spec.clipgain_y = item.y;
spec.seq = 7;
const std::vector<std::uint8_t> payload = make_id14_payload(spec);
joc_frame_params params{};
CHECK(joc::joc::parse_id14(payload.data(), payload.size(), &params, nullptr).ok());
CHECK(params.status == JOC_FRAME_STATUS_OK);
CHECK(params.clipgain == item.expected);
CHECK(params.seq_count == 7u);
CHECK(params.n_channels == 5u);
}
}
void test_joc_parse_status_and_offset_ts() {
// Unsupported downmix configuration: status 2, and the object syntax is not
// expanded because the core channel count is unknown.
{
Id14Spec spec;
spec.dmx_config_idx = 5;
const std::vector<std::uint8_t> payload = make_id14_payload(spec);
joc_frame_params params{};
CHECK(joc::joc::parse_id14(payload.data(), payload.size(), &params, nullptr).ok());
CHECK(params.status == JOC_FRAME_STATUS_UNSUPPORTED_DMX);
CHECK(params.n_channels == 0u);
CHECK(params.n_objects == 0u);
}
// More than 15 declared objects: the count is clamped and the
// corresponding frame status is reported, while the frame still parses.
{
Id14Spec spec;
spec.num_objects_bits = 15; // declares 16 objects
const std::vector<std::uint8_t> payload = make_id14_payload(spec);
joc_frame_params params{};
CHECK(joc::joc::parse_id14(payload.data(), payload.size(), &params, nullptr).ok());
CHECK(params.status == JOC_FRAME_STATUS_OBJECTS_CLAMPED);
CHECK(params.objects_clamped == 1u);
CHECK(params.n_objects == 15u);
CHECK(params.n_channels == 5u);
}
// Legal raw 5-bit offset_ts values stay as coded (no +1).
{
Id14Spec spec;
spec.present = true;
spec.slope = 1;
spec.two_data_points = true;
spec.offset0 = 5;
spec.offset1 = 10;
const std::vector<std::uint8_t> payload = make_id14_payload(spec);
joc_frame_params params{};
CHECK(joc::joc::parse_id14(payload.data(), payload.size(), &params, nullptr).ok());
CHECK(params.status == JOC_FRAME_STATUS_OK);
CHECK(params.objects[0].offset_ts[0] == 5u);
CHECK(params.objects[0].offset_ts[1] == 10u);
CHECK(params.objects[0].n_dpoints == 2u);
CHECK((params.present_mask & 1u) != 0u);
}
// offset_ts outside 0..23 -> status 5.
{
Id14Spec spec;
spec.present = true;
spec.slope = 1;
spec.two_data_points = true;
spec.offset0 = 24;
spec.offset1 = 25;
const std::vector<std::uint8_t> payload = make_id14_payload(spec);
joc_frame_params params{};
CHECK(joc::joc::parse_id14(payload.data(), payload.size(), &params, nullptr).ok());
CHECK(params.status == JOC_FRAME_STATUS_BAD_OFFSET_TS);
CHECK(params.objects[0].offset_ts[0] == 24u);
}
// Non-increasing offset_ts -> status 5.
{
Id14Spec spec;
spec.present = true;
spec.slope = 1;
spec.two_data_points = true;
spec.offset0 = 10;
spec.offset1 = 10;
const std::vector<std::uint8_t> payload = make_id14_payload(spec);
joc_frame_params params{};
CHECK(joc::joc::parse_id14(payload.data(), payload.size(), &params, nullptr).ok());
CHECK(params.status == JOC_FRAME_STATUS_BAD_OFFSET_TS);
}
// Data after the byte alignment of joc_data -> status 6.
{
Id14Spec spec;
spec.present = true;
spec.trailing = true;
const std::vector<std::uint8_t> payload = make_id14_payload(spec);
joc_frame_params params{};
CHECK(joc::joc::parse_id14(payload.data(), payload.size(), &params, nullptr).ok());
CHECK(params.status == JOC_FRAME_STATUS_TRAILING_DATA);
CHECK(params.trailing_bits > 0u);
}
// The same payload without the extra bytes is a normal frame.
{
Id14Spec spec;
spec.present = true;
const std::vector<std::uint8_t> payload = make_id14_payload(spec);
joc_frame_params params{};
CHECK(joc::joc::parse_id14(payload.data(), payload.size(), &params, nullptr).ok());
CHECK(params.status == JOC_FRAME_STATUS_OK);
CHECK(params.objects[0].n_bands == 1u);
CHECK(params.present_mask == 1u);
}
}
// Synthetic audio frames driven through the frame-level kernel.
struct FrameCase {
ejoc_renderer_handle handle = nullptr;
std::vector<float> bed;
std::vector<float> lfe;
std::uint8_t n_bands[EJOC_MAX_OBJECTS] = {};
std::uint8_t n_dpoints[EJOC_MAX_OBJECTS] = {};
std::uint8_t slope[EJOC_MAX_OBJECTS] = {};
std::uint8_t offsets[EJOC_MAX_OBJECTS * EJOC_MAX_DPOINTS] = {};
std::vector<double> dq;
std::vector<float> out;
FrameCase()
: bed(static_cast<std::size_t>(EJOC_MAX_CORE_CHANNELS) * EJOC_FRAME_SAMPLES, 0.0f),
lfe(EJOC_FRAME_SAMPLES, 0.0f),
dq(static_cast<std::size_t>(EJOC_MAX_OBJECTS) * EJOC_MAX_DPOINTS *
EJOC_MAX_CORE_CHANNELS * EJOC_MAX_PARAMETER_BANDS,
0.0),
out(static_cast<std::size_t>(EJOC_OUTPUT_CHANNELS) * EJOC_FRAME_SAMPLES, 0.0f) {}
~FrameCase() {
if (handle != nullptr) {
ejoc_renderer_destroy(handle);
}
}
void create() {
handle = ejoc_renderer_create();
ejoc_renderer_set_threads(handle, 1);
}
static std::size_t dq_index(int object, int point, int channel, int band) {
return static_cast<std::size_t>(
(((object * EJOC_MAX_DPOINTS + point) * EJOC_MAX_CORE_CHANNELS + channel) *
EJOC_MAX_PARAMETER_BANDS) +
band);
}
float at(int channel, int sample) const {
return out[static_cast<std::size_t>(channel) * EJOC_FRAME_SAMPLES + sample];
}
bool silent_slots(int first_channel, int last_channel) const {
for (int channel = first_channel; channel <= last_channel; ++channel) {
for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) {
if (at(channel, sample) != 0.0f) {
return false;
}
}
}
return true;
}
bool render(std::uint32_t core_channels, std::uint32_t declared_channels,
std::uint32_t status, std::uint32_t seq, bool has_metadata,
std::uint32_t object_mask, double clipgain) {
ejoc_frame_input input{};
input.struct_size = sizeof(input);
input.struct_version = EJOC_FRAME_INPUT_VERSION;
input.bed_planar = bed.data();
input.lfe = lfe.data();
input.core_channel_count = core_channels;
input.declared_core_channels = declared_channels;
input.phase_shift = 0u;
input.phase_shift_valid = 1u;
input.frame_status = status;
input.seq = seq;
input.has_metadata = has_metadata ? 1u : 0u;
input.object_mask = object_mask;
input.n_bands = n_bands;
input.n_dpoints = n_dpoints;
input.slope_idx = slope;
input.offset_ts = offsets;
input.dq = dq.data();
input.clipgain = clipgain;
input.output_scale = 1.0f;
input.output16_planar = out.data();
std::fill(out.begin(), out.end(), 0.0f);
return ejoc_renderer_render_frame(handle, &input) == 0;
}
};
void fill_frame_bed(FrameCase* frame, float step, float lfe_value) {
for (std::uint32_t channel = 0; channel < EJOC_MAX_CORE_CHANNELS; ++channel) {
for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) {
frame->bed[static_cast<std::size_t>(channel) * EJOC_FRAME_SAMPLES + sample] =
step * static_cast<float>(channel + 1u);
}
}
for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) {
frame->lfe[sample] = lfe_value;
}
}
bool near(float value, float expected, float tolerance) {
return std::fabs(value - expected) <= tolerance;
}
void test_joc_lfe_clipgain_ramp() {
FrameCase frame;
frame.create();
fill_frame_bed(&frame, 0.01f, 0.1f);
// First frame: gain 1, the delay ring is empty.
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 1u, true, 0u, 1.0));
for (int sample = 0; sample < JOC_LFE_DELAY_SAMPLES; ++sample) {
CHECK(frame.at(0, sample) == 0.0f);
}
for (int sample = JOC_LFE_DELAY_SAMPLES; sample < EJOC_FRAME_SAMPLES; ++sample) {
CHECK(frame.at(0, sample) == 0.1f);
}
// Second frame: gain 2 ramps over the frame-local first 256 samples before the
// delay; the read-out of the first frame is untouched by the new gain.
frame.lfe.assign(EJOC_FRAME_SAMPLES, 0.2f);
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 2u, true, 0u, 2.0));
for (int sample = 0; sample < JOC_LFE_DELAY_SAMPLES; ++sample) {
CHECK(frame.at(0, sample) == 0.1f);
}
for (int input = 0; input < static_cast<int>(EJOC_FRAME_SAMPLES) - static_cast<int>(JOC_LFE_DELAY_SAMPLES); ++input) {
const double gain = input < 256
? 1.0 + static_cast<double>(input) * ((2.0 - 1.0) / 256.0)
: 2.0;
const double scaled = static_cast<double>(frame.lfe[static_cast<std::size_t>(input)]) *
gain;
const double clipped = scaled < -1.0 ? -1.0 : (scaled > 1.0 ? 1.0 : scaled);
CHECK(frame.at(0, JOC_LFE_DELAY_SAMPLES + input) ==
static_cast<float>(clipped));
}
// A held frame (invalid status while normal) reuses the held clipgain: the
// analysis scalar was 1/16*2, so the LFE gain stays 2 with no new ramp.
frame.lfe.assign(EJOC_FRAME_SAMPLES, 0.3f);
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 5u, 3u, true, 0u, 2.0));
for (int sample = 0; sample < JOC_LFE_DELAY_SAMPLES; ++sample) {
CHECK(frame.at(0, sample) == 0.4f);
}
for (int sample = JOC_LFE_DELAY_SAMPLES; sample < EJOC_FRAME_SAMPLES; ++sample) {
CHECK(frame.at(0, sample) == 0.6f);
}
}
void test_joc_passthrough_8_plane_layout() {
// 5-channel core: slots 0..5 = L,R,C,LFE,Ls,Rs and slots 6..7 stay zero.
{
FrameCase frame;
frame.create();
fill_frame_bed(&frame, 0.1f, 0.25f);
CHECK(frame.render(EJOC_CORE_CHANNELS, 0u, EJOC_FRAME_STATUS_NO_METADATA, 0u, false,
0u, 1.0));
float expected[8] = {};
for (int channel = 0; channel < 3; ++channel) {
expected[channel] = frame.bed[static_cast<std::size_t>(channel) * EJOC_FRAME_SAMPLES];
}
expected[3] = frame.lfe[0];
for (int index = 0; index < 2; ++index) {
expected[4 + index] =
frame.bed[static_cast<std::size_t>(3 + index) * EJOC_FRAME_SAMPLES];
}
for (int channel = 0; channel < 8; ++channel) {
for (int sample = 0; sample < JOC_LFE_DELAY_SAMPLES; ++sample) {
CHECK(frame.at(channel, sample) == 0.0f);
}
for (int sample = JOC_LFE_DELAY_SAMPLES; sample < EJOC_FRAME_SAMPLES; ++sample) {
CHECK(frame.at(channel, sample) == expected[channel]);
}
}
CHECK(frame.silent_slots(8, EJOC_OUTPUT_CHANNELS - 1));
// The 8-plane history is kept every frame: the next passthrough frame
// reads the previous frame's last 1217 samples.
fill_frame_bed(&frame, 0.05f, 0.125f);
CHECK(frame.render(EJOC_CORE_CHANNELS, 0u, EJOC_FRAME_STATUS_NO_METADATA, 0u, false,
0u, 1.0));
float previous[8] = {};
float current[8] = {};
for (int channel = 0; channel < 3; ++channel) {
current[channel] = frame.bed[static_cast<std::size_t>(channel) * EJOC_FRAME_SAMPLES];
}
current[3] = frame.lfe[0];
for (int index = 0; index < 2; ++index) {
current[4 + index] =
frame.bed[static_cast<std::size_t>(3 + index) * EJOC_FRAME_SAMPLES];
}
previous[0] = 0.1f;
previous[1] = 0.2f;
previous[2] = 0.3f;
previous[3] = 0.25f;
previous[4] = 0.4f;
previous[5] = 0.5f;
for (int channel = 0; channel < 8; ++channel) {
for (int sample = 0; sample < JOC_LFE_DELAY_SAMPLES; ++sample) {
CHECK(frame.at(channel, sample) == previous[channel]);
}
for (int sample = JOC_LFE_DELAY_SAMPLES; sample < EJOC_FRAME_SAMPLES; ++sample) {
CHECK(frame.at(channel, sample) == current[channel]);
}
}
}
// 7-channel core: the extra pair is written to slots 6 and 7.
{
FrameCase frame;
frame.create();
fill_frame_bed(&frame, 0.01f, 0.07f);
CHECK(frame.render(EJOC_MAX_CORE_CHANNELS, 0u, EJOC_FRAME_STATUS_NO_METADATA, 0u,
false, 0u, 1.0));
float expected[8] = {};
for (int channel = 0; channel < 3; ++channel) {
expected[channel] = frame.bed[static_cast<std::size_t>(channel) * EJOC_FRAME_SAMPLES];
}
expected[3] = frame.lfe[0];
for (int index = 0; index < 4; ++index) {
expected[4 + index] =
frame.bed[static_cast<std::size_t>(3 + index) * EJOC_FRAME_SAMPLES];
}
for (int channel = 0; channel < 8; ++channel) {
for (int sample = JOC_LFE_DELAY_SAMPLES; sample < EJOC_FRAME_SAMPLES; ++sample) {
CHECK(frame.at(channel, sample) == expected[channel]);
}
}
CHECK(frame.silent_slots(8, EJOC_OUTPUT_CHANNELS - 1));
}
}
// One present object that passes core channel 0 through with an all-ones matrix,
// so normal rendering produces a non-zero object slot and every other mode does
// not.
void configure_passthrough_object(FrameCase* frame) {
frame->n_bands[0] = 1;
frame->n_dpoints[0] = 1;
frame->dq[FrameCase::dq_index(0, 0, 0, 0)] = 1.0;
}
float object_peak(const FrameCase& frame, int object_channel) {
float peak = 0.0f;
for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) {
peak = std::max(peak, std::fabs(frame.at(object_channel, sample)));
}
return peak;
}
void test_joc_frame_state_machine() {
// seq 0 on the first frame is not a hard cut, and a mismatch later is.
{
FrameCase frame;
frame.create();
for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) {
frame.bed[static_cast<std::size_t>(sample)] =
static_cast<float>(0.2 * std::sin(static_cast<double>(sample) * 0.05));
}
configure_passthrough_object(&frame);
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 0u, true, 1u, 1.0));
CHECK(object_peak(frame, 1) > 1.0e-3f);
// Expected seq becomes 1; seq 2 is a mismatch -> hard cut publishes zeros.
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 2u, true, 1u, 1.0));
CHECK(frame.silent_slots(0, EJOC_OUTPUT_CHANNELS - 1));
// The next valid frame resyncs and renders normally again.
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 3u, true, 1u, 1.0));
CHECK(object_peak(frame, 1) > 1.0e-3f);
// A seq that restarts at 0 is a hard cut as well.
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 0u, true, 1u, 1.0));
CHECK(frame.silent_slots(0, EJOC_OUTPUT_CHANNELS - 1));
}
// A frame without JOC metadata always passes the core through, even while
// the renderer is in its normal state.
{
FrameCase frame;
frame.create();
fill_frame_bed(&frame, 0.1f, 0.2f);
configure_passthrough_object(&frame);
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 1u, true, 1u, 1.0));
CHECK(object_peak(frame, 1) > 1.0e-3f);
CHECK(frame.render(EJOC_CORE_CHANNELS, 0u, EJOC_FRAME_STATUS_NO_METADATA, 0u, false,
1u, 1.0));
CHECK(frame.silent_slots(8, EJOC_OUTPUT_CHANNELS - 1));
CHECK(frame.at(0, EJOC_FRAME_SAMPLES - 1) != 0.0f);
}
// Invalid frames hold the previous matrices; after six of them the state
// machine stays in hold and a valid frame still resyncs.
{
FrameCase frame;
frame.create();
for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) {
frame.bed[static_cast<std::size_t>(sample)] =
static_cast<float>(0.2 * std::sin(static_cast<double>(sample) * 0.05));
}
configure_passthrough_object(&frame);
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 1u, true, 1u, 1.0));
CHECK(object_peak(frame, 1) > 1.0e-3f);
for (int invalid = 0; invalid < 6; ++invalid) {
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 5u, 2u, true, 1u, 1.0));
CHECK(object_peak(frame, 1) > 1.0e-3f);
}
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 100u, true, 1u, 1.0));
CHECK(object_peak(frame, 1) > 1.0e-3f);
}
// A declared core-channel-count change is a hard cut that clears the LFE
// ring: the next frame reads only the samples written after the change.
{
FrameCase frame;
frame.create();
fill_frame_bed(&frame, 0.1f, 0.1f);
configure_passthrough_object(&frame);
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 1u, true, 1u, 1.0));
frame.lfe.assign(EJOC_FRAME_SAMPLES, 0.5f);
// The frame that declares the change keeps its own clipgain (2.0) even
// though it renders as a hard cut: it clears the LFE ring and refills it
// with the new gain before the next frame reads it out.
CHECK(frame.render(EJOC_MAX_CORE_CHANNELS, EJOC_MAX_CORE_CHANNELS, 0u, 2u, true, 1u,
2.0));
CHECK(frame.silent_slots(0, EJOC_OUTPUT_CHANNELS - 1));
CHECK(frame.render(EJOC_MAX_CORE_CHANNELS, EJOC_MAX_CORE_CHANNELS, 0u, 3u, true, 1u,
1.0));
CHECK(near(frame.at(0, 0), 1.0f, 1.0e-6f));
CHECK(near(frame.at(0, JOC_LFE_DELAY_SAMPLES), 1.0f, 1.0e-6f));
// Samples past the 256-sample ramp use the frame's own gain again.
CHECK(near(frame.at(0, JOC_LFE_DELAY_SAMPLES + 300), 0.5f, 1.0e-6f));
}
}
void test_joc_no_previous_interpolation() {
auto prepare = [](FrameCase* frame) {
frame->create();
for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) {
frame->bed[static_cast<std::size_t>(sample)] =
static_cast<float>(0.2 * std::sin(static_cast<double>(sample) * 0.05));
}
frame->n_bands[0] = 1;
frame->dq[FrameCase::dq_index(0, 0, 0, 0)] = 0.0;
};
// A two-data-point frame without previous state keeps dp0 in the first half
// and still interpolates dp0 -> dp1 in the second half, exactly like the
// same frame with a previous state equal to dp0.
{
FrameCase with_previous;
prepare(&with_previous);
with_previous.n_dpoints[0] = 1;
CHECK(with_previous.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 1u, true, 1u,
1.0));
FrameCase without_previous;
prepare(&without_previous);
CHECK(without_previous.render(EJOC_CORE_CHANNELS, 0u,
EJOC_FRAME_STATUS_NO_METADATA, 0u, false, 1u, 1.0));
for (FrameCase* frame : {&with_previous, &without_previous}) {
frame->n_dpoints[0] = 2;
frame->dq[FrameCase::dq_index(0, 0, 0, 0)] = 0.0;
frame->dq[FrameCase::dq_index(0, 1, 0, 0)] = 1.0;
CHECK(frame->render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 2u, true, 1u,
1.0));
}
CHECK(with_previous.out == without_previous.out);
}
// A single data point without previous state is a hard cut, not a fade from
// zero: the same target with a previous state of zero ramps instead.
{
FrameCase with_previous;
prepare(&with_previous);
with_previous.n_dpoints[0] = 1;
CHECK(with_previous.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 1u, true, 1u,
1.0));
FrameCase without_previous;
prepare(&without_previous);
CHECK(without_previous.render(EJOC_CORE_CHANNELS, 0u,
EJOC_FRAME_STATUS_NO_METADATA, 0u, false, 1u, 1.0));
for (FrameCase* frame : {&with_previous, &without_previous}) {
frame->n_dpoints[0] = 1;
frame->dq[FrameCase::dq_index(0, 0, 0, 0)] = 1.0;
CHECK(frame->render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 2u, true, 1u,
1.0));
}
CHECK(with_previous.out != without_previous.out);
}
// An object that disappears keeps being published as an all-zero matrix:
// the synthesis state still advances, so only the window tail of the
// preceding frame is left in the object slot.
{
FrameCase frame;
frame.create();
fill_frame_bed(&frame, 0.1f, 0.1f);
configure_passthrough_object(&frame);
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 1u, true, 1u, 1.0));
CHECK(object_peak(frame, 1) > 1.0e-3f);
CHECK(frame.render(EJOC_CORE_CHANNELS, EJOC_CORE_CHANNELS, 0u, 2u, true, 0u, 1.0));
for (int sample = 640; sample < EJOC_FRAME_SAMPLES; ++sample) {
CHECK(frame.at(1, sample) == 0.0f);
}
}
}
// The binaural position timeline consumes the full OAMD frame state: multi-block
// payloads and programmes past the 16-slot legacy view are scheduled instead of
// being rejected, and a renderer with a different source count can read more
// tracks than the fifteen JOC object sources.
void test_binaural_timeline_full_frame_state() {
// Two position blocks in one object element: the legacy frame_update() view
// rejects this, the full frame state schedules both blocks.
const std::vector<std::uint8_t> multiblock = from_hex("0104600800450132831d6a00");
joc::oamd::OamdUpdate update;
CHECK(joc::oamd::parse_id11_state(multiblock.data(), multiblock.size(), &update).ok());
CHECK(update.has_frame_state);
CHECK(update.frame_state.block_count == 2);
joc::timeline::OamdPositionTimeline timeline;
CHECK(timeline.submit_update(update, 0, 0, 1473, 0).ok());
CHECK(timeline.initialized());
CHECK(timeline.payload_count() == 1);
double positions[joc::timeline::kTimelineObjects][3] = {};
CHECK(timeline.positions_at(0, &positions[0][0], joc::timeline::kTimelineObjects).ok());
// Seventeen declared objects: the timeline grows one track per object instead
// of stopping at the 16-slot legacy view.
const char* many_objects_hex =
"21044E600022A29808054530100A8A60201514C0402A29808054530100A8A60201514C040"
"2A29808054530100A8A60201514C0402A29808054530100A8A60201514C0402A2980800";
const std::vector<std::uint8_t> many_objects = from_hex(many_objects_hex);
joc::oamd::OamdUpdate many;
CHECK(joc::oamd::parse_id11_state(many_objects.data(), many_objects.size(), &many).ok());
CHECK(many.frame_state.object_count == 17);
joc::timeline::OamdPositionTimeline wide;
CHECK(wide.submit_update(many, 0, 0, 1473, 0).ok());
CHECK(wide.object_count() == 16);
double wide_positions[16][3] = {};
CHECK(wide.positions_at(0, &wide_positions[0][0], 16).ok());
CHECK(wide.positions_at(0, &wide_positions[0][0], 17).code() ==
JOC_ERR_INVALID_ARGUMENT);
// The legacy slot view still works and matches the frame-state path for a
// single-block payload.
const std::vector<std::uint8_t> single = from_hex("0304680004545313880D45301000");
joc::oamd::OamdUpdate parsed;
CHECK(joc::oamd::parse_id11_state(single.data(), single.size(), &parsed).ok());
joc::timeline::OamdPositionTimeline full;
joc::timeline::OamdPositionTimeline legacy;
CHECK(full.submit_update(parsed, 0, 0, 1473, 0).ok());
joc::oamd::OamdUpdate slot_view = parsed;
slot_view.has_frame_state = false;
CHECK(legacy.submit_update(slot_view, 0, 0, 1473, 0).ok());
double frame_position[3] = {};
double slot_position[3] = {};
CHECK(full.positions_at(0, frame_position, 1).ok());
CHECK(legacy.positions_at(0, slot_position, 1).ok());
CHECK(frame_position[0] == slot_position[0] && frame_position[1] == slot_position[1] &&
frame_position[2] == slot_position[2]);
}
int main() {
test_sha256();
test_crc32_and_inflate();
@@ -1510,6 +2217,13 @@ int main() {
test_clipgain_first_frame_uses_its_own_gain();
test_clipgain_change_applies_to_the_current_frame();
test_clipgain_reset_clears_the_previous_gain();
test_joc_clipgain_formula();
test_joc_parse_status_and_offset_ts();
test_joc_lfe_clipgain_ramp();
test_joc_passthrough_8_plane_layout();
test_joc_frame_state_machine();
test_joc_no_previous_interpolation();
test_binaural_timeline_full_frame_state();
std::printf("%d checks, %d failure(s)\n", g_checks, g_failures);
return g_failures == 0 ? 0 : 1;
}
+437
View File
@@ -0,0 +1,437 @@
// State-only checks of the OAMD parser, ported from tests/oamd_metadata_test.py.
// The speaker-renderer targets are covered by the speaker tests; this file keeps
// the bitstream expansion, formula tables and programme parsing checks.
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <vector>
#include "oamd/oamd_params.h"
#include "oamd/oamd_parser.h"
namespace {
int failures = 0;
void check(bool passed, const char* name) {
if (!passed) {
std::fprintf(stderr, "FAIL: %s\n", name);
++failures;
}
}
std::vector<std::uint8_t> from_hex(const char* text) {
std::vector<std::uint8_t> bytes;
const std::size_t length = std::strlen(text);
for (std::size_t index = 0; index + 1 < length; index += 2) {
const auto high = static_cast<unsigned>(text[index]);
const auto low = static_cast<unsigned>(text[index + 1]);
const auto digit = [](unsigned value) -> unsigned {
if (value >= '0' && value <= '9') {
return value - '0';
}
if (value >= 'a' && value <= 'f') {
return value - 'a' + 10;
}
if (value >= 'A' && value <= 'F') {
return value - 'A' + 10;
}
return 0;
};
bytes.push_back(static_cast<std::uint8_t>((digit(high) << 4) | digit(low)));
}
return bytes;
}
joc::oamd::OamdUpdate parse_state(const char* hex) {
const std::vector<std::uint8_t> payload = from_hex(hex);
joc::oamd::OamdUpdate update;
const joc::Status status =
joc::oamd::parse_id11_state(payload.data(), payload.size(), &update);
check(status.ok(), "parse_id11_state succeeds");
if (!status.ok()) {
std::fprintf(stderr, " %s: %s\n", hex, status.message().c_str());
}
return update;
}
const joc::oamd::ObjectRenderState& block0(const joc::oamd::OamdUpdate& update,
int object) {
return update.frame_state.objects[static_cast<std::size_t>(object)]
.blocks[0]
.render;
}
// Two object blocks in one object element: block 0 is a full update, block 1 is
// an inactive block. Ported from test_multiblock_state_expansion.
void test_multiblock_state_expansion() {
const joc::oamd::OamdUpdate update = parse_state("0104600800450132831d6a00");
check(update.has_frame_state, "frame state present");
check(update.frame_state.object_count == 1, "one object");
check(update.frame_state.block_count == 2, "two blocks");
check(update.frame_state.objects.size() == 1, "one object state");
check(update.frame_state.objects[0].blocks.size() == 2, "two object blocks");
const joc::oamd::ObjectRenderState& first = block0(update, 0);
check(first.gain_q == 23033, "block 0 gain_q");
check(first.priority_q == 32767, "block 0 priority_q");
check(first.qx == 6342 && first.qy == 21141 && first.qz == -13107, "block 0 position");
check(first.zone_idx == 3 && first.enable_elevation, "block 0 zone/elevation");
check(first.size_mode == 1, "block 0 size mode");
check(first.snap, "block 0 snap");
const joc::oamd::ObjectRenderState& second =
update.frame_state.objects[0].blocks[1].render;
// The second block is active but carries basic_status 0: no gain update.
check(!second.not_active, "block 1 active");
check(second.gain_q == 0 && second.priority_q == 0, "block 1 gain/priority");
check(second.qx == 16384 && second.qy == 16384 && second.qz == 0,
"block 1 default position");
}
// Two objects with three blocks: the first object's block 1 is inactive, the
// second is a full update. Ported from test_multiblock_gain_inheritance.
void test_multiblock_gain_inheritance() {
const joc::oamd::OamdUpdate update =
parse_state("038474100040850146527514c0407c3e00");
check(update.frame_state.objects.size() == 2, "two objects");
check(update.frame_state.block_count == 3, "three blocks");
const std::int32_t expected_gain[3] = {23033, 0, 2052};
for (int block = 0; block < 3; ++block) {
check(update.frame_state.objects[0].blocks[static_cast<std::size_t>(block)]
.render.gain_q == expected_gain[block],
"first object gain inheritance");
check(update.frame_state.objects[1].blocks[static_cast<std::size_t>(block)]
.render.gain_q == expected_gain[block],
"second object gain inheritance");
}
check(update.frame_state.objects[0].blocks[1].render.not_active,
"first object block 1 inactive");
check(!update.frame_state.objects[1].blocks[1].render.not_active,
"second object block 1 active");
for (int block = 0; block < 3; ++block) {
check(update.frame_state.objects[0].blocks[static_cast<std::size_t>(block)]
.render.qx == 16384,
"first object holds centre x");
check(update.frame_state.objects[1].blocks[static_cast<std::size_t>(block)]
.render.qx == 10570,
"second object x");
}
}
// gain_idx 0/1/2: unity, mute and the 6-bit dB table.
void test_gain_formula() {
check(block0(parse_state("010458000501514C0400"), 0).gain_q == 23033, "gain bits 0");
check(block0(parse_state("01045800057F514C0400"), 0).gain_q == 14, "gain bits 63");
check(block0(parse_state("0104540004D4530100"), 0).gain_q == 0, "gain mute");
}
// size_idx 1/2: isotropic and per-axis Q15 sizes.
void test_size_formula() {
const joc::oamd::OamdUpdate size16 = parse_state("01045800045453016000");
check(block0(size16, 0).size_mode == 1, "size mode 1");
check(block0(size16, 0).size_q[0] == 16913, "size value 16");
check(block0(size16, 0).size_q[1] == 16913 && block0(size16, 0).size_q[2] == 16913,
"isotropic size");
const joc::oamd::OamdUpdate size31 = parse_state("01045800045453017E00");
check(block0(size31, 0).size_q[0] == 32767, "size value 31");
const joc::oamd::OamdUpdate size2 = parse_state("01045C00045453018AAA00");
check(block0(size2, 0).size_mode == 2, "size mode 2");
check(block0(size2, 0).size_q[0] == 5285 && block0(size2, 0).size_q[1] == 10570 &&
block0(size2, 0).size_q[2] == 21141,
"per-axis size");
}
// Position quantisation, zone/snap, elevation flag and signed z.
void test_position_formula() {
const joc::oamd::OamdUpdate base = parse_state("010454000454530100");
check(block0(base, 0).qx == 10570 && block0(base, 0).qy == 10570 &&
block0(base, 0).qz == 17476,
"coarse position");
check(block0(base, 0).enable_elevation, "elevation enabled");
check(block0(base, 0).zone_idx == 0, "zone 0");
const joc::oamd::OamdUpdate zone = parse_state("0104540004420A0B10");
check(block0(zone, 0).qx == 1057 && block0(zone, 0).qy == 1057 && block0(zone, 0).qz == 0,
"zone position");
check(block0(zone, 0).zone_idx == 5, "zone index");
check(block0(zone, 0).snap, "snap flag");
const joc::oamd::OamdUpdate height = parse_state("010454000454530000");
check(!block0(height, 0).enable_elevation, "elevation disabled");
const joc::oamd::OamdUpdate negative = parse_state("010454000454510100");
check(block0(negative, 0).qz == -17476, "signed elevation");
}
// Extended object element position refinement.
void test_refinement() {
const joc::oamd::OamdUpdate update = parse_state("010854000454530100A23E00");
check(block0(update, 0).qx == 10676, "refined x");
check(block0(update, 0).qy == 10676, "refined y");
check(block0(update, 0).qz == 17913, "refined z");
}
// Extended object element divergence table.
void test_divergence() {
check(block0(parse_state("010854000454530100A27400"), 0).divergence_q == 11548,
"divergence table value 32");
check(block0(parse_state("010854000454530100A277E0"), 0).divergence_q == 16384,
"divergence table value 63");
}
// Programme assignment: bed labels, ISF index, masks and distribution flag.
void test_program_assignment() {
const joc::oamd::OamdUpdate lfe = parse_state("018448000440");
check(lfe.frame_state.program.dynamic_object_only, "dynamic object only");
check(lfe.frame_state.program.lfe_present, "lfe present");
check(lfe.frame_state.program.num_bed_objects == 1, "one bed object");
check(lfe.frame_state.program.bed_labels.size() == 1 &&
lfe.frame_state.program.bed_labels[0] == joc::joc::BedChannel::kLfe,
"lfe bed label");
check(lfe.frame_state.program.bed_mask == 0x8u, "lfe bed mask");
check(lfe.frame_state.objects[0].kind == joc::oamd::ObjectKind::kBed, "bed object kind");
check(block0(lfe, 0).qx == 16384 && block0(lfe, 0).qy == 16384 && block0(lfe, 0).qz == 0,
"bed object holds centre");
const joc::oamd::OamdUpdate isf = parse_state("062011400011084200");
check(isf.frame_state.object_count == 4, "isf object count");
check(isf.frame_state.program.isf_idx == 0, "isf index");
check(isf.frame_state.program.num_isf_objects == 4, "isf object number");
for (int index = 0; index < 4; ++index) {
check(isf.frame_state.objects[static_cast<std::size_t>(index)].kind ==
joc::oamd::ObjectKind::kIsf,
"isf object kind");
}
const joc::oamd::OamdUpdate standard = parse_state("021100422600022100");
check(standard.frame_state.program.num_bed_objects == 2, "standard bed object number");
check(standard.frame_state.program.bed_mask == 0x3u, "standard bed wide mask");
check(standard.frame_state.program.bed_labels.size() == 2 &&
standard.frame_state.program.bed_labels[0] == joc::joc::BedChannel::kLeft &&
standard.frame_state.program.bed_labels[1] == joc::joc::BedChannel::kRight,
"standard bed labels");
const joc::oamd::OamdUpdate nonstandard = parse_state("001000800448000440");
check(nonstandard.frame_state.program.num_bed_objects == 1, "non-standard bed number");
check(nonstandard.frame_state.program.bed_mask == 0x100u, "non-standard bed mask");
check(nonstandard.frame_state.program.bed_labels.size() == 1 &&
nonstandard.frame_state.program.bed_labels[0] ==
joc::joc::BedChannel::kTopFrontLeft,
"non-standard bed label");
check(!nonstandard.frame_state.program.b_bed_chan_distribute, "distribution off");
const joc::oamd::OamdUpdate distributed = parse_state("001800800448000440");
check(distributed.frame_state.program.b_bed_chan_distribute, "distribution on");
}
// The parsed bed declaration keeps its expanded channels for the ADM check.
void test_bed_assignments() {
const std::vector<std::uint8_t> payload = from_hex("021100422600022100");
std::vector<joc::oamd::BedAssignment> assignments;
check(joc::oamd::parse_id11_bed_assignments(payload.data(), payload.size(),
&assignments)
.ok(),
"bed assignment parse");
check(assignments.size() == 1, "one assignment");
check(assignments[0].standard && !assignments[0].lfe_only, "standard assignment");
check(assignments[0].channels.size() == 2 &&
assignments[0].channels[0] == joc::joc::BedChannel::kLeft &&
assignments[0].channels[1] == joc::joc::BedChannel::kRight,
"assignment channels");
}
// Legacy frame_update() view: 16 slots from block 0, >16 objects and multi-block
// frames stay rejected; the full state accepts both.
void test_legacy_view() {
const std::vector<std::uint8_t> base_payload = from_hex("010454000454530100");
joc::oamd::OamdUpdate legacy;
check(joc::oamd::parse_id11(base_payload.data(), base_payload.size(), &legacy).ok(),
"legacy parse");
check(legacy.has_frame_state, "legacy carries frame state");
check(legacy.frame_state.block_count == 1, "legacy block count");
check(legacy.frame_state.blocks[0].block_offset_samples == 0 &&
legacy.frame_state.blocks[0].ramp_duration_samples == 0,
"legacy block timing");
check(legacy.slots[0].q1 == 10570 && legacy.slots[0].q2 == 10570 &&
legacy.slots[0].q3 == 17476,
"legacy slot position");
check(legacy.slots[1].q1 == -1 && legacy.slots[1].q2 == -1 && legacy.slots[1].q3 == -1,
"legacy unused slot hold");
// The legacy slot view never publishes a negative elevation.
const std::vector<std::uint8_t> negative = from_hex("010454000454510100");
check(joc::oamd::parse_id11(negative.data(), negative.size(), &legacy).ok(),
"negative legacy parse");
check(legacy.slots[0].q3 == 0, "legacy negative z clamps to side");
const std::vector<std::uint8_t> elevation_off = from_hex("010454000454530000");
check(joc::oamd::parse_id11(elevation_off.data(), elevation_off.size(), &legacy).ok(),
"elevation-off legacy parse");
check(legacy.slots[0].q3 == 0, "legacy elevation flag zeroes z");
// Bed objects have no position field: the slot keeps the previous position.
const std::vector<std::uint8_t> bed = from_hex("018448000440");
check(joc::oamd::parse_id11(bed.data(), bed.size(), &legacy).ok(), "bed legacy parse");
check(legacy.slots[0].q1 == -1 && legacy.slots[0].q2 == -1 && legacy.slots[0].q3 == -1,
"legacy bed slot holds");
const std::vector<std::uint8_t> multiblock = from_hex("0104600800450132831d6a00");
const joc::Status legacy_status =
joc::oamd::parse_id11(multiblock.data(), multiblock.size(), &legacy);
check(!legacy_status.ok() &&
legacy_status.code() == JOC_ERR_OAMD_UNSUPPORTED_VARIANT,
"legacy rejects multiple blocks");
const char* many_objects_hex =
"21044E600022A29808054530100A8A60201514C0402A29808054530100A8A60201514C040"
"2A29808054530100A8A60201514C0402A29808054530100A8A60201514C0402A2980800";
const std::vector<std::uint8_t> many_objects = from_hex(many_objects_hex);
const joc::Status many_legacy =
joc::oamd::parse_id11(many_objects.data(), many_objects.size(), &legacy);
check(!many_legacy.ok() && many_legacy.code() == JOC_ERR_OAMD_UNSUPPORTED_VARIANT,
"legacy rejects more than 16 objects");
joc::oamd::OamdUpdate many;
check(joc::oamd::parse_id11_state(many_objects.data(), many_objects.size(), &many).ok(),
"full state accepts more than 16 objects");
check(many.frame_state.object_count == 17, "full state object count");
check(many.frame_state.objects.size() == 17, "full state expands every object");
}
// Optional position fields must be consumed exactly: distance, screen reference
// and the priority mask.
void test_optional_fields() {
const joc::oamd::OamdUpdate distance = parse_state("0304680004545313880D45301000");
check(distance.frame_state.object_count == 2, "distance payload object count");
check(block0(distance, 0).qx == 10570, "distance payload keeps alignment");
check(block0(distance, 1).gain_q == 0, "distance payload second object");
const joc::oamd::OamdUpdate infinity = parse_state("010454000454531880");
check(block0(infinity, 0).qz == 17476, "infinite distance keeps alignment");
const joc::oamd::OamdUpdate screen = parse_state("01045800045453013600");
check(block0(screen, 0).qx == 10570 && block0(screen, 0).snap == false,
"screen reference keeps alignment");
const joc::oamd::OamdUpdate priority = parse_state("03046800043EA29808000A298080");
check(block0(priority, 0).priority_q == 31744, "priority bits");
check(block0(priority, 1).priority_q == 0, "priority zero");
const joc::oamd::OamdUpdate inactive = parse_state("010448000600");
check(block0(inactive, 0).not_active, "inactive object");
check(block0(inactive, 0).gain_q == 0 && block0(inactive, 0).priority_q == 0,
"inactive gain/priority");
check(block0(inactive, 0).qx == 16384 && block0(inactive, 0).qy == 16384 &&
block0(inactive, 0).qz == 0,
"inactive default position");
}
// The compiled-in ISF tables must be complete: six configurations, twelve
// layout descriptors, four layout groups, 73 paired column flags, and exact
// matrix coefficients.
void test_metadata_params() {
const joc::oamd::MetadataParams* params = joc::oamd::metadata_params();
check(params != nullptr, "metadata params");
if (params == nullptr) {
std::fprintf(stderr, " %s\n", joc::oamd::metadata_params_error());
return;
}
check(params->configs.size() == 6, "six ISF configurations");
check(params->layouts.size() == 12, "twelve ISF layout descriptors");
check(!params->layout_groups.floor_a.empty() && !params->layout_groups.floor_b.empty() &&
!params->layout_groups.height.empty() &&
!params->layout_groups.additional.empty(),
"four layout groups");
check(!params->paired_columns.empty(), "paired column flags");
for (const joc::oamd::IsfConfig& config : params->configs) {
check(config.counts[4] == static_cast<int>(config.primary_row_map.size()),
"isf row count");
// Every configuration stores its basis with a 73-column row stride.
check(config.matrix.size() ==
static_cast<std::size_t>(config.counts[4]) * 73u,
"isf matrix size");
}
const joc::oamd::IsfLayout* layout20 = params->layout("20");
const joc::oamd::IsfLayout* layout916 = params->layout("916");
check(layout20 != nullptr && layout20->output_count == 2 &&
layout20->compensation_value == -4.5,
"2.0 layout descriptor");
check(layout916 != nullptr && layout916->output_count == 16 &&
layout916->compensation_value == 0.0,
"9.1.6 layout descriptor");
check(params->layout("60") != nullptr && params->layout("60")->output_count == 6 &&
params->layout("60")->compensation_value == -1.5,
"6.0 layout descriptor");
check(!params->layout("unknown"), "unknown layout descriptor");
// Exact table contents: the config counts and the first coefficients pin
// the reference data, and the row/column structure follows from them.
const joc::oamd::IsfConfig& first = params->configs[0];
check(first.counts[0] == 3 && first.counts[1] == 1 && first.counts[2] == 0 &&
first.counts[3] == 0 && first.counts[4] == 4,
"first config counts");
check(first.matrix.size() == 4u * 73u && first.matrix[0] == 0.806554496f &&
first.matrix[1] == 0.55179733f && first.matrix[15] == -0.280463845f,
"first config coefficients");
check(first.primary_row_map.size() == 4 && first.primary_row_map[3] == 3 &&
first.secondary_row_map.size() == 4 && first.secondary_row_map[3] == 3,
"first config row maps");
const joc::oamd::IsfConfig& last = params->configs[5];
check(last.counts[0] == 15 && last.counts[1] == 9 && last.counts[2] == 5 &&
last.counts[3] == 1 && last.counts[4] == 30 &&
last.matrix.size() == 30u * 73u,
"last config shape");
check(params->layout("20")->output_ids.size() == 37 &&
params->layout("20")->output_ids[0] == 0 &&
params->layout("20")->output_ids[1] == 1 &&
params->layout("20")->output_ids[2] == 0,
"layout output ids");
check(params->layout_groups.floor_a.size() == 6 &&
params->layout_groups.floor_b.size() == 7 &&
params->layout_groups.height.size() == 6 &&
params->layout_groups.additional.size() == 5,
"layout group sizes");
check(params->layout_groups.floor_a[1].selector == 1 &&
params->layout_groups.floor_a[1].begin == 1 &&
params->layout_groups.floor_a[1].end == 3,
"layout group record");
const std::size_t paired = static_cast<std::size_t>(
std::count(params->paired_columns.begin(), params->paired_columns.end(), true));
check(params->paired_columns.size() == 73 && paired == 67, "paired column flags");
}
// q_of is the reference's Q15 quantiser.
void test_q_of() {
check(joc::oamd::q_of(0, 62) == 0, "q_of 0");
check(joc::oamd::q_of(62, 62) == 32767, "q_of max");
check(joc::oamd::q_of(31, 62) == 16384, "q_of centre");
check(joc::oamd::q_of(15, 15) == 32767, "q_of z max");
check(joc::oamd::q_of(20, 62) == 10570, "q_of 20/62");
check(joc::oamd::q_of(8, 15) == 17476, "q_of 8/15");
}
} // namespace
int main() {
test_multiblock_state_expansion();
test_multiblock_gain_inheritance();
test_gain_formula();
test_size_formula();
test_position_formula();
test_refinement();
test_divergence();
test_program_assignment();
test_bed_assignments();
test_legacy_view();
test_optional_fields();
test_metadata_params();
test_q_of();
if (failures != 0) {
std::fprintf(stderr, "%d OAMD metadata check(s) failed\n", failures);
return 1;
}
std::printf("OAMD metadata checks passed\n");
return 0;
}