From 1227fd6bbb9a64628411632f23f468cc055f4f7e Mon Sep 17 00:00:00 2001 From: TheM14 Date: Fri, 30 Oct 2026 04:00:38 +0800 Subject: [PATCH] Port the OAMD bed layout map and the fixed bed positions into the kernel --- CMakeLists.txt | 1 + README.en.md | 8 +- README.md | 3 +- src/adm/adm_tracks.cpp | 15 ++ src/adm/adm_tracks.h | 14 ++ src/binaural/binaural_runtime.h | 5 + src/joc_bitstream/bed_layout_map.cpp | 331 +++++++++++++++++++++++++++ src/joc_bitstream/bed_layout_map.h | 68 ++++++ src/joc_bitstream/joc_parser.cpp | 57 +++-- src/oamd/oamd_parser.cpp | 9 + src/oamd/oamd_parser.h | 4 + src/speaker/speaker_step.cpp | 8 + src/speaker/speaker_step.h | 4 + src/task/task.cpp | 59 ++++- src/timeline/position_timeline.cpp | 4 + src/timeline/position_timeline.h | 13 ++ 16 files changed, 576 insertions(+), 27 deletions(-) create mode 100644 src/joc_bitstream/bed_layout_map.cpp create mode 100644 src/joc_bitstream/bed_layout_map.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bde8e58..fce7982 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,6 +118,7 @@ set(JOC_INTERNAL_SOURCES src/io/process.cpp src/io/wav_writer.cpp src/io/zip_reader.cpp + src/joc_bitstream/bed_layout_map.cpp src/joc_bitstream/joc_parser.cpp src/joc_core/objects16.cpp src/oamd/oamd_parser.cpp diff --git a/README.en.md b/README.en.md index cdeeea8..66dbb05 100644 --- a/README.en.md +++ b/README.en.md @@ -212,9 +212,11 @@ ffmpeg gets `-ac 6` only for the $N=5$ configurations; $N=7$ keeps the decoder's a non-zero $joc\_ext\_config\_idx$ is an unmapped variant, and a declared object count above 15 is clamped to 15 and recorded as a diagnostic - those change the DSP input, so a mismatch is refused. The extra pair's **identity** never reaches the DSP (section 10.1): when the dependent-substream -`chanmap` disagrees with $k$, or an OAMD standard bed mask does, the mismatch is recorded as a -diagnostic and the frame still renders, with the bed mapping following the channel codes the -`chanmap` declares. When one audio frame holds several EMDF containers carrying ID11/ID14, they +`chanmap` disagrees with $k$, the mismatch is recorded as a diagnostic and the frame still +renders, with the bed mapping following the channel codes the `chanmap` declares. The bed +layout the OAMD declares may hold more channels than the core substream codes (a 9.1.6 bed +with a 5.1 core, for example): only a core channel missing from that layout counts as a +mismatch. When one audio frame holds several EMDF containers carrying ID11/ID14, they are scored by the association rule of section 8.2 and its Table 56: a container whose OAMD and JOC payloads share a group id and whose ID14 parses completely wins, then one whose ID14 merely parses; only a unique best score is used and the others are recorded as a diagnostic diff --git a/README.md b/README.md index 09aa7c8..3defbcc 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,8 @@ ffmpeg 侧只在 $N=5$ 档位传 `-ac 6`;$N=7$ 档位不传 `-ac`,保留解 **跨层一致性校验**(§11.2):子流声明的全频段通道数必须等于 $N$,$joc\_ext\_config\_idx$ 非零 时报未映射变体,对象数超过 15 时截断到 15 并记入 diagnostics——这几项会改变 DSP 输入,不一致就 拒绝。额外那一对的**身份**只进元数据、不进 DSP(§10.1):依赖子流 `chanmap` 与 $k$ 不一致、或 -OAMD 标准 bed 掩码与 $k$ 不一致时,只记 diagnostics 并照常解码,床映射按 `chanmap` 的通道码走。 +OAMD 声明的 bed 布局可以比核心子流实际编码的声道多(例如 9.1.6 bed 只编 5.1 核心),只有核心 +声道在 bed 布局里找不到位置才记 diagnostics 并照常解码,床映射按 `chanmap` 的通道码走。 同一音频帧里出现多个含 ID11/ID14 的 EMDF 容器时,按规格 §8.2 + Table 56 的关联规则打分: OAMD 与 JOC 载荷 groupid 相同且 ID14 能完整解析的优先,其次只看 ID14 能否解析;唯一最高分才用, 其余记入 diagnostics(`emdf/extra_container_ignored`)。额外那一对在 10 槽 ADM 骨架上的落位 diff --git a/src/adm/adm_tracks.cpp b/src/adm/adm_tracks.cpp index 067f8fa..437ea9e 100644 --- a/src/adm/adm_tracks.cpp +++ b/src/adm/adm_tracks.cpp @@ -236,6 +236,9 @@ void TrajectoryBuilder::submit_frame(std::int64_t frame_index, const oamd::OamdU ramp_samples = static_cast(update->ramp_duration_samples); } for (int object = 1; object <= kObjectCount; ++object) { + if (fixed_[object - 1]) { + continue; // 固定 bed 声道:位置由布局给出,不跟 OAMD 更新 + } double x = 0.0; double y = 0.0; double z = 0.0; @@ -260,6 +263,18 @@ Status TrajectoryBuilder::build(std::int64_t total_samples, TrajectoryMode mode, for (int object = 1; object <= kObjectCount; ++object) { Track track; track.name = "JOC_Object_" + std::to_string(object); + if (fixed_[object - 1]) { + Keyframe block; + block.rtime_samples = 0; + block.x = fixed_xyz_[object - 1][0]; + block.y = fixed_xyz_[object - 1][1]; + block.z = fixed_xyz_[object - 1][2]; + block.duration_samples = total_samples; + block.interpolation_samples = 0; + track.blocks.push_back(block); + out->push_back(std::move(track)); + continue; + } const Status status = (mode == TrajectoryMode::Compact) ? expand_compact(events_[object - 1], total_samples, rate_, block_, diff --git a/src/adm/adm_tracks.h b/src/adm/adm_tracks.h index 8ad2505..316ccb1 100644 --- a/src/adm/adm_tracks.h +++ b/src/adm/adm_tracks.h @@ -38,6 +38,18 @@ public: Status build(std::int64_t total_samples, TrajectoryMode mode, std::vector* out) const; + // bed 声道是固定通道:OAMD 不给位置,整片静止在声明的 bed 方位。 + void set_fixed_position(int object_index, double x, double y, double z) { + if (object_index < 1 || object_index > kObjectCount) { + return; + } + const int slot = object_index - 1; + fixed_[slot] = true; + fixed_xyz_[slot][0] = x; + fixed_xyz_[slot][1] = y; + fixed_xyz_[slot][2] = z; + } + const std::vector& events(int object_index) const { return events_[object_index]; } std::uint32_t rate() const { return rate_; } std::int64_t object_delay_samples() const { return object_delay_; } @@ -50,6 +62,8 @@ private: std::vector events_[kObjectCount]; bool has_previous_[kObjectCount] = {}; double previous_[kObjectCount][3] = {}; + bool fixed_[kObjectCount] = {}; + double fixed_xyz_[kObjectCount][3] = {}; }; Status expand_compact(const std::vector& events, std::int64_t total_samples, std::uint32_t rate, diff --git a/src/binaural/binaural_runtime.h b/src/binaural/binaural_runtime.h index 79111db..db9fc75 100644 --- a/src/binaural/binaural_runtime.h +++ b/src/binaural/binaural_runtime.h @@ -72,6 +72,11 @@ public: std::size_t staged_samples() const { return staged_; } const timeline::OamdPositionTimeline& timeline() const { return timeline_; } + // bed 声道是固定通道:OAMD 不给位置,方位由声明的 bed 布局给出。 + void set_fixed_position(int object_index, const double position[3]) { + timeline_.set_fixed_position(object_index, position); + } + // finish_output_capacity as the reference computes it (plan 21.4). std::uint32_t finish_capacity(double tail_seconds) const; diff --git a/src/joc_bitstream/bed_layout_map.cpp b/src/joc_bitstream/bed_layout_map.cpp new file mode 100644 index 0000000..d5aae8d --- /dev/null +++ b/src/joc_bitstream/bed_layout_map.cpp @@ -0,0 +1,331 @@ + +#include "joc_bitstream/bed_layout_map.h" + +#include + +#include "speaker/speaker_layouts.h" + +namespace joc::joc { +namespace { + +constexpr std::uint32_t bit(BedChannel channel) { + return 1u << static_cast(channel); +} + +// Table 12 每个 bit 展开的声道(LSB 在前)。 +struct StandardBit { + BedChannel first; + BedChannel second; // kCount 表示单声道 +}; + +constexpr StandardBit kStandardBits[10] = { + {BedChannel::kLeft, BedChannel::kRight}, + {BedChannel::kCenter, BedChannel::kCount}, + {BedChannel::kLfe, BedChannel::kCount}, + {BedChannel::kLeftSurround, BedChannel::kRightSurround}, + {BedChannel::kLeftRearSurround, BedChannel::kRightRearSurround}, + {BedChannel::kTopFrontLeft, BedChannel::kTopFrontRight}, + {BedChannel::kTopSideLeft, BedChannel::kTopSideRight}, + {BedChannel::kTopBackLeft, BedChannel::kTopBackRight}, + {BedChannel::kLeftWide, BedChannel::kRightWide}, + {BedChannel::kLfe2, BedChannel::kCount}, +}; + +// Table 13 的 17 bit 非标准掩码(LSB 在前)。 +constexpr BedChannel kNonStandardBits[17] = { + BedChannel::kLfe2, BedChannel::kRightWide, BedChannel::kLeftWide, + BedChannel::kTopBackRight, BedChannel::kTopBackLeft, + BedChannel::kTopSideRight, BedChannel::kTopSideLeft, + BedChannel::kTopFrontRight, BedChannel::kTopFrontLeft, + BedChannel::kRightRearSurround, BedChannel::kLeftRearSurround, + BedChannel::kRightSurround, BedChannel::kLeftSurround, + BedChannel::kLfe, BedChannel::kCenter, BedChannel::kRight, + BedChannel::kLeft, +}; + +struct ChannelSpec { + BedChannel channel; + const char* short_name; + const char* label; + double xyz[3]; +}; + +constexpr ChannelSpec kChannels[] = { + {BedChannel::kLeft, "L", "RC_L", {-1.0, 1.0, 0.0}}, + {BedChannel::kRight, "R", "RC_R", {1.0, 1.0, 0.0}}, + {BedChannel::kCenter, "C", "RC_C", {0.0, 1.0, 0.0}}, + {BedChannel::kLfe, "LFE", "RC_LFE", {0.0, 0.0, 0.0}}, + {BedChannel::kLeftSurround, "Ls", "RC_LS", {-1.0, 0.0, 0.0}}, + {BedChannel::kRightSurround, "Rs", "RC_RS", {1.0, 0.0, 0.0}}, + {BedChannel::kLeftRearSurround, "Lrs", "RC_LB", {-1.0, -1.0, 0.0}}, + {BedChannel::kRightRearSurround, "Rrs", "RC_RB", {1.0, -1.0, 0.0}}, + {BedChannel::kTopFrontLeft, "Ltf", "RC_TFL", {-0.5, 0.5, 1.0}}, + {BedChannel::kTopFrontRight, "Rtf", "RC_TFR", {0.5, 0.5, 1.0}}, + {BedChannel::kTopSideLeft, "Ltm", "RC_TSL", {-1.0, 0.0, 1.0}}, + {BedChannel::kTopSideRight, "Rtm", "RC_TSR", {1.0, 0.0, 1.0}}, + {BedChannel::kTopBackLeft, "Ltr", "RC_TBL", {-0.5, -0.5, 1.0}}, + {BedChannel::kTopBackRight, "Rtr", "RC_TBR", {0.5, -0.5, 1.0}}, + {BedChannel::kLeftWide, "Lw", "RC_LW", {-1.0, 0.5, 0.0}}, + {BedChannel::kRightWide, "Rw", "RC_RW", {1.0, 0.5, 0.0}}, + {BedChannel::kLfe2, "LFE2", "RC_LFE2", {0.0, 0.0, 0.0}}, +}; + +const ChannelSpec* spec_of(BedChannel channel) { + for (const ChannelSpec& spec : kChannels) { + if (spec.channel == channel) { + return &spec; + } + } + return nullptr; +} + +// 标准 bed 布局名,键是声明声道的集合。 +struct LayoutName { + std::uint32_t mask; + const char* name; +}; + +constexpr std::uint32_t mask_of() { return 0u; } + +constexpr LayoutName kLayoutNames[] = { + {bit(BedChannel::kLeft) | bit(BedChannel::kRight), "2.0"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | bit(BedChannel::kCenter), "3.0"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | bit(BedChannel::kCenter) | + bit(BedChannel::kLfe), "3.1"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround), "4.0"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kCenter), "5.0"}, + {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::kCenter) | bit(BedChannel::kLfe) | + bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround), "7.1"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kCenter) | bit(BedChannel::kLfe) | + bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) | + bit(BedChannel::kLeftWide) | bit(BedChannel::kRightWide), "9.1"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kCenter) | bit(BedChannel::kLfe) | + bit(BedChannel::kTopSideLeft) | bit(BedChannel::kTopSideRight), "5.1.2"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kCenter) | bit(BedChannel::kLfe) | + bit(BedChannel::kTopFrontLeft) | bit(BedChannel::kTopFrontRight) | + bit(BedChannel::kTopBackLeft) | bit(BedChannel::kTopBackRight), "5.1.4"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kCenter) | bit(BedChannel::kLfe) | + bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) | + bit(BedChannel::kTopSideLeft) | bit(BedChannel::kTopSideRight), "7.1.2"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kCenter) | bit(BedChannel::kLfe) | + bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) | + bit(BedChannel::kTopFrontLeft) | bit(BedChannel::kTopFrontRight) | + bit(BedChannel::kTopBackLeft) | bit(BedChannel::kTopBackRight), "7.1.4"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kCenter) | bit(BedChannel::kLfe) | + bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) | + bit(BedChannel::kTopFrontLeft) | bit(BedChannel::kTopFrontRight) | + bit(BedChannel::kTopSideLeft) | bit(BedChannel::kTopSideRight) | + bit(BedChannel::kTopBackLeft) | bit(BedChannel::kTopBackRight), "7.1.6"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kCenter) | bit(BedChannel::kLfe) | + bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) | + bit(BedChannel::kLeftWide) | bit(BedChannel::kRightWide) | + bit(BedChannel::kTopSideLeft) | bit(BedChannel::kTopSideRight), "9.1.2"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kCenter) | bit(BedChannel::kLfe) | + bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) | + bit(BedChannel::kLeftWide) | bit(BedChannel::kRightWide) | + bit(BedChannel::kTopFrontLeft) | bit(BedChannel::kTopFrontRight) | + bit(BedChannel::kTopBackLeft) | bit(BedChannel::kTopBackRight), "9.1.4"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kCenter) | bit(BedChannel::kLfe) | + bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround) | + bit(BedChannel::kLeftWide) | bit(BedChannel::kRightWide) | + bit(BedChannel::kTopFrontLeft) | bit(BedChannel::kTopFrontRight) | + bit(BedChannel::kTopSideLeft) | bit(BedChannel::kTopSideRight) | + bit(BedChannel::kTopBackLeft) | bit(BedChannel::kTopBackRight), "9.1.6"}, +}; + +const char* layout_name_for(std::uint32_t mask) { + for (const LayoutName& entry : kLayoutNames) { + if (entry.mask == mask) { + return entry.name; + } + } + return ""; +} + +// 每个扬声器布局的标准声道名(按该布局的标准声道顺序排列)。 +struct LayoutChannels { + std::uint32_t out_ch_config; + std::uint32_t count; + const char* names[16]; +}; + +constexpr LayoutChannels kLayoutChannels[] = { + {0, 2, {"L", "R"}}, + {3, 4, {"L", "R", "C", "LFE"}}, + {255, 4, {"L", "R", "Lrs", "Rrs"}}, + {7, 6, {"L", "R", "C", "LFE", "Ls", "Rs"}}, + {11, 8, {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs"}}, + {13, 8, {"L", "R", "C", "LFE", "Ls", "Rs", "Ltm", "Rtm"}}, + {14, 10, {"L", "R", "C", "LFE", "Ls", "Rs", "Ltf", "Rtf", "Ltr", "Rtr"}}, + {15, 10, {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Ltm", "Rtm"}}, + {16, 12, {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Ltf", "Rtf", "Ltr", "Rtr"}}, + {19, 14, {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Lw", "Rw", + "Ltf", "Rtf", "Ltr", "Rtr"}}, + {20, 16, {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Lw", "Rw", + "Ltf", "Rtf", "Ltm", "Rtm", "Ltr", "Rtr"}}, +}; + +const ejoc::speaker_tables::LayoutGeometry* geometry_for(std::uint32_t out_ch_config) { + for (const ejoc::speaker_tables::LayoutGeometry& layout : ejoc::speaker_tables::kLayouts) { + if (layout.out_ch_config == out_ch_config) { + return &layout; + } + } + return nullptr; +} + +} // namespace + +BedLayout resolve_bed_layout(const std::vector& assignments, + bool bed_only) { + BedLayout layout; + layout.bed_only = bed_only; + std::uint32_t mask = 0; + for (const oamd::BedAssignment& assignment : assignments) { + if (assignment.lfe_only) { + mask |= bit(BedChannel::kLfe); + layout.complete = false; + continue; + } + if (assignment.standard) { + for (int index = 0; index < 10; ++index) { + if (((assignment.mask >> index) & 1u) == 0u) { + continue; + } + mask |= bit(kStandardBits[index].first); + if (kStandardBits[index].second != BedChannel::kCount) { + mask |= bit(kStandardBits[index].second); + } + } + continue; + } + for (int index = 0; index < 17; ++index) { + if (((assignment.mask >> index) & 1u) != 0u) { + mask |= bit(kNonStandardBits[index]); + } + } + layout.complete = false; // 非标准掩码的声道名不在本标准表里 + } + if (mask == 0u) { + return layout; + } + layout.present = true; + layout.mask = mask; + layout.name = layout_name_for(mask); + for (const ChannelSpec& spec : kChannels) { + const BedChannel channel = spec.channel; + if ((mask & bit(channel)) == 0u) { + continue; + } + if (channel == BedChannel::kLfe || channel == BedChannel::kLfe2) { + continue; + } + layout.channels.push_back(channel); + } + return layout; +} + +const char* bed_channel_short_name(BedChannel channel) { + const ChannelSpec* spec = spec_of(channel); + return spec != nullptr ? spec->short_name : ""; +} + +const char* bed_channel_label(BedChannel channel) { + const ChannelSpec* spec = spec_of(channel); + return spec != nullptr ? spec->label : ""; +} + +bool bed_channel_adm_position(BedChannel channel, double xyz[3]) { + const ChannelSpec* spec = spec_of(channel); + if (spec == nullptr || xyz == nullptr) { + return false; + } + std::memcpy(xyz, spec->xyz, sizeof(spec->xyz)); + return true; +} + +bool bed_channel_q15(BedChannel channel, std::uint16_t q15[3]) { + double xyz[3] = {0.0, 0.0, 0.0}; + if (!bed_channel_adm_position(channel, xyz) || q15 == nullptr) { + return false; + } + const double q1 = (xyz[0] + 1.0) * 0.5 * 32767.0; + const double q2 = (1.0 - xyz[1]) * 0.5 * 32767.0; + const double q3 = xyz[2] * 32767.0; + q15[0] = static_cast(q1 < 0.0 ? 0.0 : (q1 > 32767.0 ? 32767.0 : q1)); + q15[1] = static_cast(q2 < 0.0 ? 0.0 : (q2 > 32767.0 ? 32767.0 : q2)); + q15[2] = static_cast(q3 < 0.0 ? 0.0 : (q3 > 32767.0 ? 32767.0 : q3)); + return true; +} + +bool layout_bed_anchor(std::uint32_t out_ch_config, BedChannel channel, + std::uint16_t q15[3]) { + if (q15 == nullptr) { + return false; + } + const char* name = bed_channel_short_name(channel); + if (name == nullptr || name[0] == '\0' || std::strcmp(name, "LFE") == 0) { + return false; + } + const LayoutChannels* table = nullptr; + for (const LayoutChannels& entry : kLayoutChannels) { + if (entry.out_ch_config == out_ch_config) { + table = &entry; + break; + } + } + const ejoc::speaker_tables::LayoutGeometry* geometry = geometry_for(out_ch_config); + if (table == nullptr || geometry == nullptr) { + return false; + } + int standard_index = -1; + for (std::uint32_t index = 0; index < table->count; ++index) { + if (std::strcmp(table->names[index], name) == 0) { + standard_index = static_cast(index); + break; + } + } + if (standard_index < 0) { + return false; + } + const std::uint8_t internal = geometry->standard_from_internal[standard_index]; + const ejoc::speaker_tables::RegionGeometry& region = geometry->regions[0]; + for (std::uint32_t point = 0; point < region.point_count; ++point) { + if (region.points[point].speaker_id != internal) { + continue; + } + for (int axis = 0; axis < 3; ++axis) { + q15[axis] = region.points[point].coordinate_q15[axis]; + } + return true; + } + return false; +} + +} // namespace joc::joc diff --git a/src/joc_bitstream/bed_layout_map.h b/src/joc_bitstream/bed_layout_map.h new file mode 100644 index 0000000..306c2d3 --- /dev/null +++ b/src/joc_bitstream/bed_layout_map.h @@ -0,0 +1,68 @@ + +#pragma once + +#include +#include +#include + +#include "oamd/oamd_parser.h" + +namespace joc::joc { + +// OAMD Table 12 的标准 bed 声道。LSB 顺序与声明顺序一致:L/R、C、LFE、 +// Ls/Rs、Lrs/Rrs、TFL/TFR、TSL/TSR、TBL/TBR、LW/RW、LFE2。 +enum class BedChannel : std::uint8_t { + kLeft = 0, + kRight, + kCenter, + kLfe, + kLeftSurround, + kRightSurround, + kLeftRearSurround, + kRightRearSurround, + kTopFrontLeft, + kTopFrontRight, + kTopSideLeft, + kTopSideRight, + kTopBackLeft, + kTopBackRight, + kLeftWide, + kRightWide, + kLfe2, + kCount, +}; + +// 内容声明的 bed 布局。bed 对象在 OAMD 里没有位置字段,位置由这里的布局决定。 +struct BedLayout { + bool present = false; // 有 bed_assignment() 声明 + bool bed_only = false; // program 只由 bed 组成(无 ISF、无动态对象) + bool complete = true; // 所有声明声道都在本标准表里 + const char* name = ""; // "9.1.6" 等;未知集合为空串 + std::uint32_t mask = 0; // 声明声道的位集合(1 << BedChannel) + std::vector channels; // 去掉 LFE/LFE2,按 JOC 对象槽顺序 +}; + +// 声明里是否包含某个 bed 声道。 +inline bool bed_layout_has(const BedLayout& layout, BedChannel channel) { + return (layout.mask & (1u << static_cast(channel))) != 0u; +} + +// 展开 bed_assignment() 声明;``bed_only`` 来自 program_assignment() 的 +// content_description。 +BedLayout resolve_bed_layout(const std::vector& assignments, + bool bed_only); + +// 扬声器布局里的短名("Lw")与 Table 12 的标签("RC_LW")。 +const char* bed_channel_short_name(BedChannel channel); +const char* bed_channel_label(BedChannel channel); + +// 固定方位:ADM 笛卡尔坐标(x 右、y 前、z 上)与渲染器使用的 Q15 方位。 +bool bed_channel_adm_position(BedChannel channel, double xyz[3]); +bool bed_channel_q15(BedChannel channel, std::uint16_t q15[3]); + +// 目标布局里同名扬声器的 Q15 方位:bed 声道直连到该扬声器,而不是参与声像。 +// 目标布局没有这个声道时返回 false。 +bool layout_bed_anchor(std::uint32_t out_ch_config, BedChannel channel, + std::uint16_t q15[3]); + +} // namespace joc::joc diff --git a/src/joc_bitstream/joc_parser.cpp b/src/joc_bitstream/joc_parser.cpp index 6650d3c..8c079f9 100644 --- a/src/joc_bitstream/joc_parser.cpp +++ b/src/joc_bitstream/joc_parser.cpp @@ -1,5 +1,7 @@ #include "joc_bitstream/joc_parser.h" +#include "joc_bitstream/bed_layout_map.h" + #include #include #include @@ -545,6 +547,26 @@ std::string position_name(Position position) { } } +// 核心声道位置 → bed 布局声道;返回 kCount 表示本标准表没有对应身份。 +BedChannel bed_channel_for_position(Position position) { + switch (position) { + case Position::kLeft: return BedChannel::kLeft; + case Position::kRight: return BedChannel::kRight; + case Position::kCenter: return BedChannel::kCenter; + case Position::kLfe: return BedChannel::kLfe; + case Position::kLfe2: return BedChannel::kLfe2; + case Position::kLeftSurround: return BedChannel::kLeftSurround; + case Position::kRightSurround: return BedChannel::kRightSurround; + case Position::kLeftRearSurround: return BedChannel::kLeftRearSurround; + case Position::kRightRearSurround: return BedChannel::kRightRearSurround; + case Position::kVerticalHeightLeft: return BedChannel::kTopFrontLeft; + case Position::kVerticalHeightRight: return BedChannel::kTopFrontRight; + case Position::kTopSurroundLeft: return BedChannel::kTopSideLeft; + case Position::kTopSurroundRight: return BedChannel::kTopSideRight; + default: return BedChannel::kCount; + } +} + std::string position_list(const std::vector& positions) { std::string text = "["; for (std::size_t index = 0; index < positions.size(); ++index) { @@ -917,25 +939,24 @@ Status check_cross_layer(const joc_frame_params& params, const eac3::Frame* core error); } - if (bed_assignments != nullptr) { - static constexpr std::uint32_t kBedBitLeftRear = 1u << 4; // RC_LB/RC_RB - static constexpr std::uint32_t kBedBitTopFront = 1u << 5; // RC_TFL/RC_TFR - std::vector declared_pair; - for (const oamd::BedAssignment& assignment : *bed_assignments) { - if (!assignment.standard || assignment.mask == 0u) { - continue; + if (bed_assignments != nullptr && !bed_assignments->empty()) { + // OAMD 的 bed 掩码描述的是内容的 bed 布局,可以比核心子流实际编码的声道多 + // (例如 9.1.6 bed 只编 5.1 核心)。只有核心声道在 bed 布局里找不到位置才是 + // 真正的不一致;多出来的 bed 声道是布局的一部分。 + const BedLayout bed = resolve_bed_layout(*bed_assignments, true); + if (bed.present && bed.complete) { + bool covered = true; + for (const Position position : core_positions) { + const BedChannel channel = bed_channel_for_position(position); + if (channel == BedChannel::kCount) { + continue; + } + if (!bed_layout_has(bed, channel)) { + covered = false; + break; + } } - if ((assignment.mask & kBedBitLeftRear) != 0u) { - push_unique(&declared_pair, Position::kLeftRearSurround); - push_unique(&declared_pair, Position::kRightRearSurround); - } - if ((assignment.mask & kBedBitTopFront) != 0u) { - push_unique(&declared_pair, Position::kVerticalHeightLeft); - push_unique(&declared_pair, Position::kVerticalHeightRight); - } - } - if (!declared_pair.empty() && declared_pair != wanted) { - if (notes != nullptr) { + if (!covered && notes != nullptr) { notes->bed_mask_mismatch = true; } } diff --git a/src/oamd/oamd_parser.cpp b/src/oamd/oamd_parser.cpp index 8e8094a..41ba3f1 100644 --- a/src/oamd/oamd_parser.cpp +++ b/src/oamd/oamd_parser.cpp @@ -64,6 +64,9 @@ bool variable_bits_max(bits::BitReader& reader, unsigned width, unsigned max_gro struct ProgramInfo { bool dynamic_object_only = false; bool lfe_present = false; + bool content_bed = false; + bool content_isf = false; + bool content_dynamic = false; std::uint32_t num_bed_objects = 0; std::uint32_t num_isf_objects = 0; std::int32_t num_dynamic_objects = -1; @@ -88,6 +91,9 @@ Status parse_program_assignment(bits::BitReader& reader, ProgramInfo* program) { if (reader.failed()) { return syntax("program_assignment truncated"); } + program->content_bed = (mask & 0x1u) != 0u; + program->content_isf = (mask & 0x2u) != 0u; + program->content_dynamic = (mask & 0x4u) != 0u; if ((mask & 0x1u) != 0u) { reader.read(1); const std::uint32_t multi = reader.read(1); @@ -520,6 +526,9 @@ Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, out->num_bed_objects = program.num_bed_objects; out->num_isf_objects = program.num_isf_objects; out->num_dynamic_objects = program.num_dynamic_objects; + out->content_bed = program.content_bed; + out->content_isf = program.content_isf; + out->content_dynamic = program.content_dynamic; if (trace != nullptr) { trace->bed_assignments = program.bed_assignments; diff --git a/src/oamd/oamd_parser.h b/src/oamd/oamd_parser.h index 10a8af3..cfff0e7 100644 --- a/src/oamd/oamd_parser.h +++ b/src/oamd/oamd_parser.h @@ -35,6 +35,10 @@ struct OamdUpdate { std::uint32_t num_bed_objects = 0; std::uint32_t num_isf_objects = 0; std::int32_t num_dynamic_objects = -1; + // program_assignment() 的 content_description:bed / ISF / 动态对象三类内容。 + bool content_bed = false; + bool content_isf = false; + bool content_dynamic = false; }; class OamdState { diff --git a/src/speaker/speaker_step.cpp b/src/speaker/speaker_step.cpp index 58f99a7..8d97362 100644 --- a/src/speaker/speaker_step.cpp +++ b/src/speaker/speaker_step.cpp @@ -37,6 +37,14 @@ Status step(SpeakerStep* context, const std::vector& objects16_planar, std::uint16_t positions[oamd::kObjects][3] = {}; context->state.object_positions_q15(positions); + for (int object = 0; object < oamd::kObjects; ++object) { + if (!context->bed_override[object]) { + continue; + } + for (int axis = 0; axis < oamd::kAxes; ++axis) { + positions[object][axis] = context->bed_positions[object][axis]; + } + } // A frame without OAMD must not touch the gains at all (no event), otherwise a // pending ramp would snap - matching the reference exactly. const std::uint32_t event_count = context->last_had_payload ? 1u : 0u; diff --git a/src/speaker/speaker_step.h b/src/speaker/speaker_step.h index 26ed6ce..28d23f5 100644 --- a/src/speaker/speaker_step.h +++ b/src/speaker/speaker_step.h @@ -23,6 +23,10 @@ struct SpeakerStep { std::uint32_t last_ramp_duration = 0; std::uint32_t last_object_count = 0; bool last_had_payload = false; + // bed 声道是固定通道:OAMD 不给位置,方位由声明的 bed 布局给出。这里存 + // 对象槽 → Q15 方位;bed_override 为 false 的槽仍按 OAMD 坐标声像。 + std::uint16_t bed_positions[oamd::kObjects][oamd::kAxes] = {}; + bool bed_override[oamd::kObjects] = {}; }; Status step(SpeakerStep* context, const std::vector& objects16_planar, diff --git a/src/task/task.cpp b/src/task/task.cpp index 1467bd1..247eafb 100644 --- a/src/task/task.cpp +++ b/src/task/task.cpp @@ -41,6 +41,7 @@ #include "io/process.h" #include "io/wav_writer.h" #include "joc_bitstream/joc_parser.h" +#include "joc_bitstream/bed_layout_map.h" #include "joc_core/objects16.h" #include "oamd/oamd_parser.h" #include "speaker/speaker_step.h" @@ -1322,6 +1323,7 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu } ::joc::speaker::SpeakerStep speaker; + bool bed_positions_ready = false; const bool speaker_enabled = cfg.operation == JOC_OP_SPEAKER; if (speaker_enabled) { if (!::joc::speaker::layout_by_name(cfg.layout.c_str(), &speaker.layout)) { @@ -1745,6 +1747,58 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu job.outer_offset = static_cast(payload->sample_offset); const Status bed_ok = ::joc::oamd::parse_id11_bed_assignments( bytes.data(), bytes.size(), &group_bed_assignments); + if (bed_ok.ok() && !bed_positions_ready && + !group_bed_assignments.empty()) { + const ::joc::joc::BedLayout bed_layout = + ::joc::joc::resolve_bed_layout( + group_bed_assignments, + job.update.content_bed && !job.update.content_isf && + !job.update.content_dynamic); + if (bed_layout.present) { + bed_positions_ready = true; + bus.emit(JOC_EV_RENDERER_INITIALIZED, JOC_STAGE_RENDER, + JOC_LOG_INFO, + fmt("bed layout: %s (%u bed channels)", + bed_layout.name[0] != '\0' + ? bed_layout.name : "custom", + static_cast( + bed_layout.channels.size()))); + if (bed_layout.bed_only) { + // bed 声道是固定通道:OAMD 不给位置,方位由声明的 bed 布局给出。 + for (std::size_t slot = 0; + slot < bed_layout.channels.size() && + slot < static_cast(::joc::oamd::kObjects); + ++slot) { + const ::joc::joc::BedChannel channel = + bed_layout.channels[slot]; + double xyz[3] = {0.0, 0.0, 0.0}; + if (::joc::joc::bed_channel_adm_position(channel, xyz)) { + trajectory.set_fixed_position( + static_cast(slot) + 1, xyz[0], xyz[1], + xyz[2]); + if (binaural_enabled) { + binaural.set_fixed_position( + static_cast(slot) + 1, xyz); + } + } + if (!speaker_enabled) { + continue; + } + // 目标布局有同名扬声器时直连该声道,否则按标准方位声像。 + std::uint16_t q15[3] = {0, 0, 0}; + if (!::joc::joc::layout_bed_anchor( + speaker.layout.out_ch_config, channel, q15) && + !::joc::joc::bed_channel_q15(channel, q15)) { + continue; + } + speaker.bed_override[slot] = true; + for (int axis = 0; axis < ::joc::oamd::kAxes; ++axis) { + speaker.bed_positions[slot][axis] = q15[axis]; + } + } + } + } + } if (!bed_ok.ok()) { failures.record(produced, StageRank::kJoc, bed_ok.code(), bed_ok.stage(), bed_ok.message()); @@ -2039,11 +2093,6 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu " audio frame(s): the chanmap declares a different extra pair than " "joc_dmx_config_idx; the bed mapping follows the chanmap"); } - if (bed_mask_mismatch_frames != 0u) { - bus.warning(std::to_string(bed_mask_mismatch_frames) + - " audio frame(s): the OAMD bed mask declares a different extra pair than " - "joc_dmx_config_idx; the bed mask only affects metadata"); - } StageFailure recorded; if (failures.earliest(&recorded)) { return finalize_with_error(recorded.code, recorded.stage_text, recorded.message); diff --git a/src/timeline/position_timeline.cpp b/src/timeline/position_timeline.cpp index 329016c..5c0f2e5 100644 --- a/src/timeline/position_timeline.cpp +++ b/src/timeline/position_timeline.cpp @@ -114,6 +114,10 @@ Status OamdPositionTimeline::submit_update(const oamd::OamdUpdate& update, double targets[kTimelineObjects][3] = {}; for (int index = 0; index < kTimelineObjects; ++index) { + if (fixed_[index]) { + std::memcpy(targets[index], fixed_xyz_[index], sizeof(targets[index])); + continue; + } geometry::q_to_adm_xyz(state_.q(index + 1, 0), state_.q(index + 1, 1), state_.q(index + 1, 2), &targets[index][0], &targets[index][1], &targets[index][2]); diff --git a/src/timeline/position_timeline.h b/src/timeline/position_timeline.h index 8b40c6d..cf87d39 100644 --- a/src/timeline/position_timeline.h +++ b/src/timeline/position_timeline.h @@ -49,6 +49,17 @@ public: // positions[15][3]; queries must be monotonically increasing. Status positions_at(std::int64_t sample, double positions[kTimelineObjects][3]); + // bed 声道是固定通道:OAMD 不给位置,方位由声明的 bed 布局给出。 + void set_fixed_position(int object_index, const double position[3]) { + if (object_index < 1 || object_index > kTimelineObjects) { + return; + } + fixed_[object_index - 1] = true; + fixed_xyz_[object_index - 1][0] = position[0]; + fixed_xyz_[object_index - 1][1] = position[1]; + fixed_xyz_[object_index - 1][2] = position[2]; + } + std::uint64_t payload_count() const { return payload_count_; } std::uint64_t transition_count() const { return transition_count_; } bool initialized() const { return initialized_; } @@ -62,6 +73,8 @@ private: bool initialized_ = false; double previous_targets_[kTimelineObjects][3] = {}; bool has_previous_[kTimelineObjects] = {}; + bool fixed_[kTimelineObjects] = {}; + double fixed_xyz_[kTimelineObjects][3] = {}; std::uint64_t payload_count_ = 0; std::uint64_t transition_count_ = 0; std::int64_t last_coded_event_sample_ = -1;