From 787984afe5b1c59c2dd2b3210437b3b0e72bc706 Mon Sep 17 00:00:00 2001 From: TheM14 Date: Thu, 12 Nov 2026 02:01:13 +0800 Subject: [PATCH] Write OAMD object render metadata into the ADM BWF Port the Python reference ADM metadata change. The ADM export path now parses the full OAMD frame state, so the same block-0 render state the speaker renderer consumes is available to the trajectory builder. adm::ObjectMetadata (gain, width/depth/height size, divergence, snap) is attached to Keyframe and OamdEvent. TrajectoryBuilder updates the per-object state from frame_state, emits an event when position or metadata changes, and both compact and dense64 expansion modes carry the snapshot into every ADM block. The state is held across frames without a payload; inactive objects write gain 0. Divergence is taken from the current payload (0 when the extended element does not carry it), matching the speaker renderer instead of holding a stale value. build_axml writes width/height/depth (relative 0..1), gain in linear units and objectDivergence positionRange, omitting defaults. The ADM path requests parse_id11_state() so frame_state is populated. test_core adds coverage for the AXML fields and for the trajectory metadata and divergence reset. Bump JOC_VERSION to 1.3.2. --- include/joc_version.h | 4 +-- src/adm/adm_metadata.cpp | 14 ++++++++ src/adm/adm_metadata.h | 10 ++++++ src/adm/adm_tracks.cpp | 74 +++++++++++++++++++++++++++++++++------- src/adm/adm_tracks.h | 3 ++ src/task/task.cpp | 9 ++--- tests/test_core.cpp | 56 ++++++++++++++++++++++++++++++ 7 files changed, 151 insertions(+), 19 deletions(-) diff --git a/include/joc_version.h b/include/joc_version.h index 0c131d4..d887d51 100644 --- a/include/joc_version.h +++ b/include/joc_version.h @@ -10,7 +10,7 @@ #define JOC_VERSION_MAJOR 1 #define JOC_VERSION_MINOR 3 -#define JOC_VERSION_PATCH 1 -#define JOC_VERSION_STRING "1.3.1" +#define JOC_VERSION_PATCH 2 +#define JOC_VERSION_STRING "1.3.2" #endif /* JOC_VERSION_H */ diff --git a/src/adm/adm_metadata.cpp b/src/adm/adm_metadata.cpp index fb724a9..3d13192 100644 --- a/src/adm/adm_metadata.cpp +++ b/src/adm/adm_metadata.cpp @@ -363,6 +363,20 @@ std::string build_axml(const std::vector& tracks, double duration_sec, st out += "" + pynum::format_fixed(block.z, 10) + ""; } + const ObjectMetadata& meta = block.meta; + if (meta.size[0] > 0.0 || meta.size[1] > 0.0 || meta.size[2] > 0.0) { + out += "" + pynum::format_fixed(meta.size[0], 6) + ""; + out += "" + pynum::format_fixed(meta.size[2], 6) + ""; + out += "" + pynum::format_fixed(meta.size[1], 6) + ""; + } + if (std::fabs(meta.gain - 1.0) > 1e-9) { + out += "" + pynum::format_fixed(meta.gain, 6) + + ""; + } + if (meta.divergence > 0.0) { + out += ""; + } out += "(block.interpolation_samples) / scale, 5) + diff --git a/src/adm/adm_metadata.h b/src/adm/adm_metadata.h index ea65247..7cd0c44 100644 --- a/src/adm/adm_metadata.h +++ b/src/adm/adm_metadata.h @@ -14,6 +14,15 @@ namespace joc::adm { inline constexpr int kObjectCount = 15; inline constexpr std::uint32_t kTrackCount = 25; +// Per-block OAMD render metadata the ADM audioBlockFormat can express. +struct ObjectMetadata { + double gain = 1.0; + // OAMD order: width, depth, height (relative units 0..1). + double size[3] = {0.0, 0.0, 0.0}; + double divergence = 0.0; + bool snap = false; +}; + struct Keyframe { std::int64_t rtime_samples = 0; double x = 0.0; @@ -21,6 +30,7 @@ struct Keyframe { double z = 0.0; std::int64_t duration_samples = 0; std::int64_t interpolation_samples = 0; + ObjectMetadata meta; }; struct Track { diff --git a/src/adm/adm_tracks.cpp b/src/adm/adm_tracks.cpp index 437ea9e..4605b96 100644 --- a/src/adm/adm_tracks.cpp +++ b/src/adm/adm_tracks.cpp @@ -16,15 +16,22 @@ struct Point { double y = 0.0; double z = 0.0; std::int64_t interpolation_samples = 0; + ObjectMetadata meta; }; +bool same_meta(const ObjectMetadata& left, const ObjectMetadata& right) { + return left.gain == right.gain && left.size[0] == right.size[0] && + left.size[1] == right.size[1] && left.size[2] == right.size[2] && + left.divergence == right.divergence && left.snap == right.snap; +} + // otherwise (the reference drops silently). void append_point(std::vector* points, std::int64_t sample, double x, double y, double z, - std::int64_t interpolation_samples) { + std::int64_t interpolation_samples, const ObjectMetadata& meta) { if (!points->empty() && sample == points->back().sample) { - points->back() = Point{sample, x, y, z, interpolation_samples}; + points->back() = Point{sample, x, y, z, interpolation_samples, meta}; } else if (points->empty() || sample > points->back().sample) { - points->push_back(Point{sample, x, y, z, interpolation_samples}); + points->push_back(Point{sample, x, y, z, interpolation_samples, meta}); } } @@ -52,6 +59,7 @@ void points_to_blocks(const std::vector& points, std::int64_t total_sampl keyframe.z = point.z; keyframe.duration_samples = duration; keyframe.interpolation_samples = std::min(point.interpolation_samples, duration); + keyframe.meta = point.meta; out->push_back(keyframe); } } @@ -85,7 +93,8 @@ Status expand_compact(const std::vector& events, std::int64_t total_s std::vector points; double current[3] = {events[0].x, events[0].y, events[0].z}; - append_point(&points, 0, current[0], current[1], current[2], 0); + ObjectMetadata current_meta = events[0].meta; + append_point(&points, 0, current[0], current[1], current[2], 0, current_meta); for (std::size_t index = 1; index < events.size(); ++index) { const OamdEvent& event = events[index]; @@ -131,10 +140,12 @@ Status expand_compact(const std::vector& events, std::int64_t total_s target[2] = z; interpolation = available; } - append_point(&points, block_start, target[0], target[1], target[2], interpolation); + append_point(&points, block_start, target[0], target[1], target[2], interpolation, + event.meta); current[0] = event.x; current[1] = event.y; current[2] = event.z; + current_meta = event.meta; } points_to_blocks(points, total_samples, out); @@ -156,7 +167,8 @@ Status expand_dense64(const std::vector& events, std::int64_t total_s std::vector points; double current[3] = {events[0].x, events[0].y, events[0].z}; - append_point(&points, 0, current[0], current[1], current[2], 0); + ObjectMetadata current_meta = events[0].meta; + append_point(&points, 0, current[0], current[1], current[2], 0, current_meta); for (std::size_t index = 1; index < events.size(); ++index) { const OamdEvent& event = events[index]; @@ -171,14 +183,15 @@ Status expand_dense64(const std::vector& events, std::int64_t total_s points.back().sample); } if (start > points.back().sample) { - append_point(&points, start, current[0], current[1], current[2], 0); + append_point(&points, start, current[0], current[1], current[2], 0, current_meta); } const std::int64_t ramp = std::max(0, event.ramp_samples); if (ramp == 0) { - append_point(&points, start, event.x, event.y, event.z, 0); + append_point(&points, start, event.x, event.y, event.z, 0, event.meta); current[0] = event.x; current[1] = event.y; current[2] = event.z; + current_meta = event.meta; continue; } const std::int64_t steps = (ramp + update_block_samples - 1) / update_block_samples; @@ -214,12 +227,13 @@ Status expand_dense64(const std::vector& events, std::int64_t total_s break; } append_point(&points, sample, position[0], position[1], position[2], - update_block_samples); + update_block_samples, event.meta); future -= update_block_samples; } current[0] = event.x; current[1] = event.y; current[2] = event.z; + current_meta = event.meta; } points_to_blocks(points, total_samples, out); @@ -239,18 +253,52 @@ void TrajectoryBuilder::submit_frame(std::int64_t frame_index, const oamd::OamdU if (fixed_[object - 1]) { continue; // 固定 bed 声道:位置由布局给出,不跟 OAMD 更新 } + const int slot = object - 1; + // OAMD block 0 对动态对象显式携带 gain/size/snap;divergence 只在扩展 + // 元素里出现,缺失时按 0 处理,与 speaker 渲染器的逐 payload 语义一致。 + if (update != nullptr && update->has_frame_state && + static_cast(object) < update->frame_state.objects.size()) { + const oamd::ObjectElementState& element = + update->frame_state.objects[static_cast(object)]; + if (!element.blocks.empty() && element.kind == oamd::ObjectKind::kDynamic) { + const oamd::ObjectRenderState& render = element.blocks[0].render; + ObjectMetadata& meta = meta_[slot]; + if (render.not_active) { + meta.gain = 0.0; + } else { + meta.gain = static_cast(render.gain_q) / 4096.0; + if (render.size_mode == 1u || render.size_mode == 2u) { + for (int axis = 0; axis < 3; ++axis) { + meta.size[axis] = std::min( + std::max(static_cast(render.size_q[axis]) / 32767.0, 0.0), + 1.0); + } + } else { + meta.size[0] = 0.0; + meta.size[1] = 0.0; + meta.size[2] = 0.0; + } + meta.divergence = static_cast(render.divergence_q) / 32767.0; + meta.snap = render.snap; + } + } + } double x = 0.0; double y = 0.0; double z = 0.0; geometry::q_to_adm_xyz(state_.q(object, 0), state_.q(object, 1), state_.q(object, 2), &x, &y, &z); - const int slot = object - 1; - if (!has_previous_[slot] || previous_[slot][0] != x || previous_[slot][1] != y || - previous_[slot][2] != z) { - events_[slot].push_back(OamdEvent{event_sample, x, y, z, ramp_samples}); + const ObjectMetadata& meta = meta_[slot]; + const bool position_changed = + !has_previous_[slot] || previous_[slot][0] != x || previous_[slot][1] != y || + previous_[slot][2] != z; + const bool meta_changed = !has_previous_[slot] || !same_meta(previous_meta_[slot], meta); + if (position_changed || meta_changed) { + events_[slot].push_back(OamdEvent{event_sample, x, y, z, ramp_samples, meta}); previous_[slot][0] = x; previous_[slot][1] = y; previous_[slot][2] = z; + previous_meta_[slot] = meta; has_previous_[slot] = true; } } diff --git a/src/adm/adm_tracks.h b/src/adm/adm_tracks.h index 316ccb1..acb6411 100644 --- a/src/adm/adm_tracks.h +++ b/src/adm/adm_tracks.h @@ -19,6 +19,7 @@ struct OamdEvent { double y = 0.0; double z = 0.0; std::int64_t ramp_samples = 0; + ObjectMetadata meta; }; enum class TrajectoryMode { Compact, Dense64 }; @@ -62,6 +63,8 @@ private: std::vector events_[kObjectCount]; bool has_previous_[kObjectCount] = {}; double previous_[kObjectCount][3] = {}; + ObjectMetadata meta_[kObjectCount]; + ObjectMetadata previous_meta_[kObjectCount]; bool fixed_[kObjectCount] = {}; double fixed_xyz_[kObjectCount][3] = {}; }; diff --git a/src/task/task.cpp b/src/task/task.cpp index 0e05dc4..f7fa566 100644 --- a/src/task/task.cpp +++ b/src/task/task.cpp @@ -1845,10 +1845,11 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu stream_done = true; break; } - // The speaker and binaural renderers consume the full per-object - // block state (multi-block and every declared object); the ADM - // export path keeps the reference's 15-track single-block view. - const bool full_metadata_state = speaker_enabled || binaural_enabled; + // The speaker, binaural and ADM paths consume the full per-object + // block state (multi-block and every declared object); the legacy + // stream/ADM single-block view only needs the slot fields. + const bool full_metadata_state = + speaker_enabled || binaural_enabled || adm_enabled; const Status oamd = full_metadata_state ? ::joc::oamd::parse_id11_state(bytes.data(), bytes.size(), diff --git a/tests/test_core.cpp b/tests/test_core.cpp index f171a9b..43afc6d 100644 --- a/tests/test_core.cpp +++ b/tests/test_core.cpp @@ -12,6 +12,7 @@ #include #include "adm/adm_metadata.h" +#include "adm/adm_tracks.h" #include "eac3_transport/eac3_reader.h" #include "eac3joc_core.h" #include "foundation/bit_reader.h" @@ -955,6 +956,60 @@ void test_dbmd_and_axml_trim() { CHECK(plain_axml.find("") == std::string::npos); } +void test_adm_object_metadata() { + std::vector tracks(15); + for (std::size_t i = 0; i < tracks.size(); ++i) { + tracks[i].name = "obj" + std::to_string(i); + } + joc::adm::Keyframe block; + block.duration_samples = 48000; + block.meta.gain = 0.5; + block.meta.size[0] = 0.516155; + block.meta.size[1] = 0.516155; + block.meta.size[2] = 0.516155; + block.meta.divergence = 0.25; + tracks[1].blocks.push_back(block); + const std::string axml = joc::adm::build_axml(tracks, 1.0, 48000); + CHECK(axml.find("0.5161550.5161550.516155") != + std::string::npos); + CHECK(axml.find("0.500000") != std::string::npos); + CHECK(axml.find("") != std::string::npos); + + // Default metadata writes no fields. + tracks[1].blocks[0].meta = joc::adm::ObjectMetadata{}; + const std::string plain = joc::adm::build_axml(tracks, 1.0, 48000); + CHECK(plain.find("") == std::string::npos); + CHECK(plain.find(" trajectory; + CHECK(builder.build(96000, joc::adm::TrajectoryMode::Compact, &trajectory).ok()); + CHECK(trajectory[1].blocks.size() >= 2); + CHECK(std::fabs(trajectory[1].blocks.front().meta.gain - 0.5) < 1e-12); + CHECK(trajectory[1].blocks.back().meta.divergence == 0.0); +} + void test_int24_packing() { // Interleaved (L,R) for two frames: 0.0, +1.0, -1.0, +2.0 (clips). const float values[4] = {0.0f, 1.0f, -1.0f, 2.0f}; @@ -2421,6 +2476,7 @@ int main() { test_speaker_surround_fold(); test_speaker_step_trim_events(); test_dbmd_and_axml_trim(); + test_adm_object_metadata(); test_int24_packing(); test_utf8_paths(); test_process_runner();