From 0bb82e4115e64b073cc1ceb77f12308136502fec Mon Sep 17 00:00:00 2001 From: TheM14 Date: Sun, 8 Nov 2026 14:04:21 +0800 Subject: [PATCH] Port the OAMD metadata rendering and the frame-level JOC core 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. --- CMakeLists.txt | 28 +- README.en.md | 92 +- README.md | 67 +- docs/binaural.en.md | 14 +- docs/binaural.md | 9 +- docs/math.en.md | 108 +- docs/math.md | 82 +- include/eac3joc_core.h | 76 +- include/joc_core.h | 33 +- src/binaural/jochrtf_runtime.cpp | 3 +- src/cli/joc_cli.cpp | 4 +- src/emdf/emdf_parser.cpp | 10 +- src/emdf/emdf_parser.h | 2 +- src/hrtf/rosella_renderer.cpp | 3 +- src/joc_bitstream/bed_layout_map.cpp | 301 ++++++ src/joc_bitstream/bed_layout_map.h | 47 +- src/joc_bitstream/joc_parser.cpp | 193 +++- src/joc_core/eac3joc_core.cpp | 567 +++++++++- src/joc_core/objects16.cpp | 52 +- src/joc_core/objects16.h | 17 +- src/joc_core/qmf_tables.h | 16 +- src/oamd/isf_tables.h | 1078 +++++++++++++++++++ src/oamd/oamd_math.cpp | 1264 ++++++++++++++++++++++ src/oamd/oamd_math.h | 101 ++ src/oamd/oamd_params.cpp | 108 ++ src/oamd/oamd_params.h | 82 ++ src/oamd/oamd_parser.cpp | 1405 +++++++++++++++++++------ src/oamd/oamd_parser.h | 183 +++- src/speaker/speaker_layout_lookup.cpp | 45 +- src/speaker/speaker_layout_lookup.h | 11 +- src/speaker/speaker_layouts.h | 88 +- src/speaker/speaker_meta.h | 90 ++ src/speaker/speaker_renderer.cpp | 1098 +++++++++++++++++++ src/speaker/speaker_step.cpp | 54 +- src/speaker/speaker_step.h | 11 + src/stream/stream.cpp | 15 +- src/task/task.cpp | 59 +- src/timeline/position_timeline.cpp | 206 +++- src/timeline/position_timeline.h | 65 +- tests/test_core.cpp | 736 ++++++++++++- tests/test_oamd_metadata.cpp | 437 ++++++++ 41 files changed, 8182 insertions(+), 678 deletions(-) create mode 100644 src/oamd/isf_tables.h create mode 100644 src/oamd/oamd_math.cpp create mode 100644 src/oamd/oamd_math.h create mode 100644 src/oamd/oamd_params.cpp create mode 100644 src/oamd/oamd_params.h create mode 100644 src/speaker/speaker_meta.h create mode 100644 tests/test_oamd_metadata.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b1a59f..ef9fe80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,11 +81,13 @@ if(MSVC AND CMAKE_GENERATOR MATCHES "Ninja" AND NOT CMAKE_CL_SHOWINCLUDES_PREFIX "messages in the language CMake probes for.") endif() -# Verbatim copies of the upstream native library; see THIRD_PARTY_NOTICES.md. -# These files are never edited: they are the validated DSP kernels. +# Sources copied verbatim from the upstream native library; see +# THIRD_PARTY_NOTICES.md. binaural_renderer.cpp is still an unmodified copy and +# is never edited; the unchanged reused headers include/eac3joc_core.h and +# src/joc_bitstream/joc_huffman_tables.h belong to the same set. The JOC core, +# the speaker renderer and the remaining tables started as copies and are +# maintained in-tree now, so they are listed as internal sources below. set(JOC_REUSED_SOURCES - src/joc_core/eac3joc_core.cpp - src/speaker/speaker_renderer.cpp src/binaural/binaural_renderer.cpp ) @@ -119,8 +121,12 @@ set(JOC_INTERNAL_SOURCES src/io/zip_reader.cpp src/joc_bitstream/bed_layout_map.cpp src/joc_bitstream/joc_parser.cpp + src/joc_core/eac3joc_core.cpp src/joc_core/objects16.cpp + src/oamd/oamd_math.cpp + src/oamd/oamd_params.cpp src/oamd/oamd_parser.cpp + src/speaker/speaker_renderer.cpp src/speaker/speaker_layout_lookup.cpp src/speaker/speaker_step.cpp src/speaker/speaker_trim.cpp @@ -236,6 +242,12 @@ if(JOC_BUILD_TESTS OR JOC_BUILD_DEVTOOLS) PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src" ) target_link_libraries(joc_core_impl PUBLIC Threads::Threads) + # The same sources back both the shared library and this static archive. In + # the archive the C entry points are plain definitions: EJOC_STATIC stops the + # kernel sources that carry their own EJOC_BUILD_DLL define from marking them + # as dllexports, which would collide with the DLL import library in a link + # that uses both libraries (the tests do). + target_compile_definitions(joc_core_impl PUBLIC EJOC_STATIC) endif() add_library(joc_core SHARED @@ -291,6 +303,10 @@ if(JOC_BUILD_TESTS) target_link_libraries(joc_rosella_filterbank_tests PRIVATE joc_core_impl) target_include_directories(joc_rosella_filterbank_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") add_test(NAME rosella_filterbank COMMAND joc_rosella_filterbank_tests) + add_executable(joc_oamd_metadata_tests tests/test_oamd_metadata.cpp) + target_link_libraries(joc_oamd_metadata_tests PRIVATE joc_core_impl) + target_include_directories(joc_oamd_metadata_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") + add_test(NAME oamd_metadata COMMAND joc_oamd_metadata_tests) # The public headers must stay valid C and C++: these targets exist to prove it. add_library(joc_headers_c OBJECT tests/test_headers.c) target_include_directories(joc_headers_c PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") @@ -317,7 +333,8 @@ if(JOC_BUILD_TESTS OR JOC_BUILD_DEVTOOLS) list(APPEND JOC_MSVC_TARGETS joc_core_impl) endif() if(JOC_BUILD_TESTS) - list(APPEND JOC_MSVC_TARGETS joc_tests joc_rosella_filterbank_tests joc_headers_c joc_headers_cpp) + list(APPEND JOC_MSVC_TARGETS joc_tests joc_rosella_filterbank_tests + joc_oamd_metadata_tests joc_headers_c joc_headers_cpp) endif() if(JOC_BUILD_DEVTOOLS) endif() @@ -358,6 +375,7 @@ target_sources(joc_core PRIVATE include/eac3joc_core.h src/joc_bitstream/joc_huffman_tables.h src/joc_core/qmf_tables.h + src/oamd/oamd_parser.h src/speaker/speaker_layouts.h ) diff --git a/README.en.md b/README.en.md index a6af5e8..2f889cf 100644 --- a/README.en.md +++ b/README.en.md @@ -113,7 +113,7 @@ This build adds `--bed` (pre-decoded ## Speaker layouts and channel order -`--speaker-layout` accepts the eleven layouts below, and the PCM/WAV channel order is the order in +`--speaker-layout` accepts the twelve layouts below, and the PCM/WAV channel order is the order in the table: | Layout | Channel order | @@ -122,6 +122,7 @@ the table: | 3.1 | L, R, C, LFE | | 4.0 | L, R, Lrs, Rrs | | 5.1 | L, R, C, LFE, Ls, Rs | +| 6.0 | L, R, Ls, Rs, Lrs, Rrs | | 7.1 | L, R, C, LFE, Ls, Rs, Lrs, Rrs | | 5.1.2 | L, R, C, LFE, Ls, Rs, Ltm, Rtm | | 5.1.4 | L, R, C, LFE, Ls, Rs, Ltf, Rtf, Ltr, Rtr | @@ -140,7 +141,7 @@ into mask-bit order. Players, ASIO, DAWs and other devices have to map the chann themselves; this project guarantees the content and numbering of each PCM channel, not that a downstream player recognises Dolby speaker positions on its own. -`--speaker-layout` accepts only the eleven names in the table; anything else fails as an unknown +`--speaker-layout` accepts only the twelve names in the table; anything else fails as an unknown layout. Speaker rendering reads warp, trim and front/back balance from the OAMD metadata; the CLI has no @@ -149,6 +150,18 @@ then fold back to 5.1.x with PLIIx or Standard (Lo/Ro) coefficients, and calibra follows the final target layout. Source trajectories and binaural rendering do not pre-apply these controls; see [mathematics §12](docs/math.en.md) for the parameters and evaluation order. +Speaker rendering also applies the OAMD object attributes: per-block gain/inactive, +zone/elevation, size/snap/divergence, the priority size budget, element 5 position +refinement, ISF basis matrices, and bed distribution. ADM export and the binaural +paths still place point objects and do not apply these speaker target coefficients. + +Object position updates are scheduled from the complete `frame_state`: speaker and +binaural rendering both accept multiple position blocks and programmes with more than +16 objects and schedule every block at its own time and ramp. The binaural audio input +is still one LFE plus fifteen object sources; the extra position tracks are followed +but not panned. ADM export keeps the 15-track single-block position view and rejects +anything larger as an unsupported variant. + ## Library integration The engine and the file task are exposed by `joc_core.h`: `joc_task_validate` / @@ -191,18 +204,49 @@ tests/ unit tests (CTest, self-contained, no external data) docs/ mathematics, the binaural rendering flow and SIMD dispatch ``` -Eight files under `src/` are byte-identical copies of the upstream JustOneCacophony -native library and are never edited (see [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md)). +The QMF and binaural tables and the JOC Huffman tables under `src/` are written from +publicly documented standards; [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) records +their sources. + +The ISF compressed base coefficients and layout descriptors are obtained from +lookup tables stored in the generated header (`src/oamd/isf_tables.h`), which is +compiled into the binary with the code, so nothing reads a data file at run +time. Gain, size, priority, position, divergence, delta and refinement values are +computed in code, while the size kernel is generated from the speaker layout +region geometry and bed routing is derived from those layouts. + +## Mathematical implementation + +The main documented stages are: + +- dense JOC differential reconstruction and dequantization; +- parameter-band mapping to 64 QMF subbands; +- cross-frame parameter interpolation (hard cut when no previous state exists, zero-matrix publishing after an object disappears); +- the frame-level JOC state machine (normal, hold, hard cut, core passthrough); +- analysis/inverse QMF, surround delay, and FIR state; +- the 1217-sample LFE delay with its sample-wise clipgain ramp; +- OAMD Q15 coordinate conversion; +- equal-power panning over target-layout regions; +- OAMD metadata application (per-block gain and inactive, zone/elevation, size kernel, snap, divergence, position refinement, priority size budget, ISF matrices, bed distribution); +- layout-dependent position compensation and sample-wise gain ramps; +- float32 and PCM24 output quantization; +- Rosella and compiled `.jochrtf` binaural rendering, with exactly-once delay/phase and special LFE. + +See the [mathematical notes](docs/math.en.md) for the equations used by the decoding +and rendering process. ## Compatibility note ### How `joc_clipgain` is applied -`joc_clipgain` is folded into the **analysis-side scalar**: `scalar = 1/16 x g` (`g = 1 + (y/32)·2^(x-4)`, -from the 8-bit field `x:3 | y:5`). The x16 at the output cancels the 1/16, so the net effect is a multiply -by `g` and the clamp therefore sits **after** the gain (`clamp(16·IQMF·g_interp)`). The scalar is kept in -two per-frame slots: the first 256 samples of a frame ramp linearly from the previous value, the rest stays -constant, and the first frame after a reset takes the new value directly. For `g = 1` the interpolation is the identity and `clamp(x)·1 == clamp(x·1)`. +`joc_clipgain` is folded into the **analysis-side scalar**: `scalar = 1/16 x g`, with +`g = 2^(x-4)·(1 + y/32)` from the 8-bit field `x:3 | y:5`; the range is 0.0625..15.75. The x16 at the +output cancels the 1/16, so the net effect is a multiply by `g` and the clamp therefore sits **after** +the gain (`clamp(16·IQMF·g_interp)`). The scalar is kept in two per-frame slots: the first 256 samples +of a frame ramp linearly from the previous value, the rest stays constant, and the first frame after a +reset takes the new value directly. For `g = 1` the interpolation is the identity and +`clamp(x)·1 == clamp(x·1)`. LFE bypasses the analysis but applies the same full `g` as a sample-wise +ramp before its 1217-sample delay. `object_delay_samples` defaults to **1473**, preserving the behaviour of the existing implementation; it is a configurable field and changing it changes the OAMD/ADM time @@ -239,9 +283,33 @@ only a unique best score is used and the others are recorded as a diagnostic $k\in\{2,4\}\to8/9$) is recorded, but bed channels are not output channels under the current contract. -An audio frame with no JOC EMDF at all (for example when the encoder wrote none for the first -frame) emits silent object channels while the QMF analysis and synthesis state keep advancing, -and it is counted separately in the statistics. +### Frame status and the JOC decode state machine + +Every frame carries a status; a non-zero status is reported as a diagnostic. A non-zero status +makes the frame invalid for rendering, so the renderer holds the previous matrices or passes the +core PCM through instead of decoding that frame's matrix: 2 unsupported downmix configuration, +3 object count clamped to 15, 4 unknown parameter-band index, 5 invalid `offset_ts`, and 6 +trailing data after byte padding. A file task refuses a `joc_dmx_config_idx` outside 0..4 +earlier, in the metadata scan, as an unsupported variant. Status 3 still yields the clamped +object list for diagnostics, while status 2 and 4 stop the parse at the failing header field. + +Rendering then runs a per-frame state machine: normal, hold, hard cut, and core passthrough. An +unusable-JOC frame that arrives during normal rendering enters hold, where the previous frame-end +matrices keep being published; six consecutive unusable frames move the internal hold state on but +stay in hold, and a valid frame resynchronises the sequence number. A sequence number of 0 after +the renderer left its initial state, a sequence mismatch, or a change of the declared core channel +count is a hard cut: every object matrix is zeroed and each object's previous state becomes +the zero matrix, while a core channel count change also clears the LFE delay ring but keeps +the clipgain ramp state. Before the first valid frame the renderer passes the core PCM +through. + +A frame with no JOC EMDF at all (for example when the encoder wrote none for the first frame) +always takes the passthrough path: the eight core planes L, R, C, LFE, Ls, Rs, (Lrs, Rrs) are +written to output slots 0..7 by identity, each delayed by 1217 samples, slots 8..15 stay zero, +and the 8-plane history keeps advancing. Those frames are counted separately in the statistics, +and E-AC-3 input without JOC metadata (ID11 + ID14) is outside the supported scope. While hold or +passthrough runs without fresh parameters, the analysis scalar and the LFE ramp keep the +currently held clipgain. ## License diff --git a/README.md b/README.md index 84ae017..5acc48d 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ joc_cli input.eac3 --metadata-only --print-metadata summary # 只解析并打 ## 扬声器布局与声道顺序 -`--speaker-layout` 只接受下表 11 个布局,PCM/WAV 的声道顺序就是表里的顺序: +`--speaker-layout` 只接受下表 12 个布局,PCM/WAV 的声道顺序就是表里的顺序: | 布局 | 声道顺序 | |---|---| @@ -107,6 +107,7 @@ joc_cli input.eac3 --metadata-only --print-metadata summary # 只解析并打 | 3.1 | L, R, C, LFE | | 4.0 | L, R, Lrs, Rrs | | 5.1 | L, R, C, LFE, Ls, Rs | +| 6.0 | L, R, Ls, Rs, Lrs, Rrs | | 7.1 | L, R, C, LFE, Ls, Rs, Lrs, Rrs | | 5.1.2 | L, R, C, LFE, Ls, Rs, Ltm, Rtm | | 5.1.4 | L, R, C, LFE, Ls, Rs, Ltf, Rtf, Ltr, Rtr | @@ -122,13 +123,21 @@ joc_cli input.eac3 --metadata-only --print-metadata summary # 只解析并打 PCM 声道顺序严格按上表,不按掩码位序重排。播放器、ASIO、DAW 或其他设备需要按声道编号自行完成 输出映射;本项目保证各 PCM 声道的内容和编号符合上表,不保证下游播放器自动识别杜比扬声器位置。 -`--speaker-layout` 只接受上表 11 个名字;其它名字按未知布局报错。 +`--speaker-layout` 只接受上表 12 个名字;其它名字按未知布局报错。 扬声器渲染从 OAMD 元数据读取 warp、trim 和 front/back balance;命令行不提供 warp 覆盖。 warp 0/1 直接渲染;warp 2/3 先渲染到 7.1.x 中间布局,再按 PLIIx 或 Standard(Lo/Ro) 折回 5.1.x,校准参数始终按最终目标布局选择。原始对象轨迹与双耳渲染不预处理这些控制, 参数与求值顺序见[数学说明 §12](docs/math.md)。 +扬声器渲染还会应用 OAMD 的对象属性:每块增益/inactive、zone/elevation、size/snap/divergence、 +priority 尺寸预算、element 5 位置细化、ISF 基矩阵和 bed distribute。ADM 导出与双耳路径仍按点 +对象放置,不应用这些扬声器目标系数。 + +对象位置的调度按完整 `frame_state` 进行:扬声器与双耳渲染都接受多 position block 和超过 16 +个对象的节目,按各块的时间与 ramp 逐个调度;双耳渲染的音频输入仍是 1 LFE + 15 对象,多出的 +位置轨道只跟踪不参与声像。ADM 导出仍使用 15 轨、单 block 的对象位置视图,超出时按变体报错。 + ## 库集成 引擎与文件任务使用 `joc_core.h`:`joc_task_validate` / `joc_task_execute` 跑一个文件任务并 @@ -164,17 +173,41 @@ tests/ 单元测试(CTest,自足,不需要外部素材) docs/ 数学说明、双耳渲染流程与 SIMD 派发 ``` -`src/` 下 8 个文件是 JustOneCacophony 原生库的逐字节副本,永不修改(见 -[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md))。 +`src/` 下的 QMF/双耳表与 JOC Huffman 表按公开标准实现;来源记录见 +[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md)。 + +ISF 压缩基系数与布局描述以查表方式提供:表数据写在生成头文件 `src/oamd/isf_tables.h` 中, +与代码一起编译进二进制,运行时不读取数据文件。gain、size、priority、位置、divergence +与差分/细化值都在代码中计算,size 核由扬声器布局的 region 几何生成,bed 路由由布局表推导。 + +## 数学实现 + +主要文档化阶段: + +- dense JOC 差分还原与去量化; +- 参数带到 64 个 QMF 子带的映射; +- 跨帧参数插值(无上一帧时硬切,对象消失后按零矩阵发布); +- 帧级 JOC 状态机(正常 / 保持 / 硬切 / 核心直通); +- analysis / inverse QMF、环绕声道延迟与 FIR 状态; +- LFE 1217-sample 延迟与逐点 clipgain 斜坡; +- OAMD Q15 坐标转换; +- 基于目标布局 region 的等功率声像; +- OAMD 元数据应用(每块增益与 inactive、zone/elevation、size 核、snap、divergence、位置细化、priority 尺寸预算、ISF 矩阵、bed 分配); +- 布局位置补偿与逐样本增益斜坡; +- float32 与 PCM24 输出量化; +- Rosella 与编译好的 `.jochrtf` 双耳渲染,含 exactly-once delay/phase 与 special LFE。 + +解码与渲染所用的公式见[数学说明](docs/math.md)。 ## 兼容性说明 ### `joc_clipgain` 的施加方式 -`joc_clipgain` 在**分析端**并入分析标量:`标量 = 1/16 × g`(`g = 1 + (y/32)·2^(x−4)`,8 bit 字段 -`x:3 | y:5`)。出口的 ×16 抵消 1/16,净效果是 `×g`,因此 clamp 处在增益**之后** +`joc_clipgain` 在**分析端**并入分析标量:`标量 = 1/16 × g`(`g = 2^(x−4)·(1 + y/32)`,8 bit 字段 +`x:3 | y:5`,值域 0.0625..15.75)。出口的 ×16 抵消 1/16,净效果是 `×g`,因此 clamp 处在增益**之后** (`clamp(16·IQMF·g_interp)`)。标量按帧保存在两个槽里:本帧前 256 个采样从上一帧值线性过渡、之后为 -常数,复位后的第一帧直接取新值。`g = 1` 时插值恒等,且 `clamp(x)·1 == clamp(x·1)`。 +常数,复位后的第一帧直接取新值。`g = 1` 时插值恒等,且 `clamp(x)·1 == clamp(x·1)`。LFE 走独立 +通路,但使用同一个完整 `g` 的逐点斜坡,并乘在 1217-sample 延迟之前。 `object_delay_samples` 默认 **1473**,与既有实现的行为保持一致;它是可配置字段,改动它会 改变 OAMD/ADM 时间对齐。上游调查认为该值应为 0,本项目为保持逐字节等价暂不改默认值。 @@ -202,8 +235,24 @@ OAMD 与 JOC 载荷 groupid 相同且 ID14 能完整解析的优先,其次只 其余记入 diagnostics(`emdf/extra_container_ignored`)。额外那一对在 10 槽 ADM 骨架上的落位 ($k=1\to6/7$、$k\in\{2,4\}\to8/9$)会被记录,但床通道按现行契约不作为输出通道。 -音频帧内完全没有 JOC EMDF 时(例如编码器没写首帧元数据),该帧的对象通道静音, -QMF 分析与合成状态照常推进,并在统计里单独计数。 +### 帧状态与 JOC 解码状态机 + +每个帧都带一个状态,非零状态按诊断报告。非零状态使该帧不能用于渲染,渲染器因此保持上一帧矩阵或输出 +核心 PCM 直通,而不还原该帧的矩阵:2 不支持的下混配置,3 对象数截断到 15,4 未知参数带索引, +5 非法 `offset_ts`,6 字节对齐后存在尾随数据。文件任务对 `joc_dmx_config_idx` 超出 0..4 的 +输入更早在元数据扫描阶段按不支持变体拒绝。状态 3 仍给出截断后的对象列表(供诊断),状态 2、4 +在出错的头字段处停止解析。 + +渲染随后按帧级状态机运行:正常、保持、硬切、核心直通。正常渲染期间出现 JOC 参数不可用的帧时 +进入保持,继续发布上一帧末的矩阵;连续六个不可用帧只把内部保持状态推进一档、仍保持;出现有效 +帧后重新对齐序号。渲染器离开初始状态后序号回到 0、序号不匹配、或声明的核心声道数变化都 +触发硬切:所有对象矩阵清零、每个对象的 previous 状态置零,核心声道数变化还清空 LFE 延迟环 +但保留 clipgain 斜坡状态。首个有效帧之前,渲染器输出核心 PCM 直通。 + +音频帧内完全没有 JOC EMDF 时(例如编码器没写首帧元数据),该帧一律走直通路径:8 路核心平面 +L、R、C、LFE、Ls、Rs、(Lrs, Rrs) 按 identity 写入输出槽 0..7,每路延迟 1217 采样,槽 8..15 +保持零,8 平面历史照常推进。这类帧在统计里单独计数;不含 JOC 元数据(ID11 + ID14)的 E-AC-3 +输入不在支持范围。保持或直通期间没有新参数时,分析标量与 LFE 斜坡沿用当前保持的 clipgain。 ## 许可 diff --git a/docs/binaural.en.md b/docs/binaural.en.md index 990b264..35025a2 100644 --- a/docs/binaural.en.md +++ b/docs/binaural.en.md @@ -26,7 +26,7 @@ performs no parsing or conversion of measurement data formats. ```powershell joc_cli input.m4a --binaural ` --compiled-hrtf-cache path\to\subject.jochrtf ` - --kernels data\rosella_kernels.npz ` + --kernels path\to\rosella_kernels.npz ` --binaural-mode mid --binaural-tail-seconds 5.0 ``` @@ -95,8 +95,8 @@ compiler does not sample 77 FFT bins. Defaults are `1e-3` projection ridge and `1e-5` SH ridge. Coincident directions are merged before a spherical-Voronoi weighted ridge fit. -The fixed resource is `data/rosella_kernels.npz`, which implements publicly -standardized filter banks, computable from the following formulas. +The fixed tables implement publicly standardized filter banks, compile into the +sources, and are computable from the following formulas. The hybrid analysis kernels are defined in [3GPP TS 26.405 / ETSI TS 126 405](https://www.etsi.org/deliver/etsi_ts/126400_126499/126405/06.00.00_60/ts_126405v060000p.pdf), Section 5.2.2 (Table 1 $Q=8$/ @@ -125,11 +125,9 @@ $8+4+4$ child partition): $$Y_p = \sum_{q\in C_p}\Bigl(\mathrm{Re}X_q + j\,s_q\,\mathrm{Im}X_q\Bigr),\qquad s_q\in\{\pm1\}$$ -The loader verifies the archive and every array by SHA-256; the table version, -all array hashes, and the 77 reference band-center values are part of the cache -key. Public availability of a standard does not by itself grant permission to -practice related patent claims. See -[`data/README.en.md`](../data/README.en.md) and +The tables are fixed at compile time, so no external archive is loaded and +nothing enters a cache key at run time. Public availability of a standard does +not by itself grant permission to practice related patent claims. See [`THIRD_PARTY_NOTICES.md`](../THIRD_PARTY_NOTICES.md) for the sources and the rights boundary. diff --git a/docs/binaural.md b/docs/binaural.md index c3a40c3..5598d62 100644 --- a/docs/binaural.md +++ b/docs/binaural.md @@ -24,7 +24,7 @@ cache,不是交换格式。 ```powershell joc_cli input.m4a --binaural ` --compiled-hrtf-cache path\to\subject.jochrtf ` - --kernels data\rosella_kernels.npz ` + --kernels path\to\rosella_kernels.npz ` --jochrtf-room on ` --binaural-mode mid --binaural-tail-seconds 5.0 joc_cli input.m4a --binaural --personalized-headphone HRTF\binaural.personalized_headphone @@ -78,7 +78,7 @@ joc_cli input.m4a --binaural --personalized-headphone HRTF\binaural.personalized ridge 为 `1e-3`,SH ridge 为 `1e-5`。同方向 measurement 先合并,再用球面 Voronoi 面积权重做 ridge fit。 -固定表位于 `data/rosella_kernels.npz`,实现公开标准化的滤波器组,各表可由如下 +固定表按公开标准化的滤波器组实现并编译进源码,各表可由如下 公式计算。 hybrid 分析核定义于 [3GPP TS 26.405 / ETSI TS 126 405](https://www.etsi.org/deliver/etsi_ts/126400_126499/126405/06.00.00_60/ts_126405v060000p.pdf) @@ -106,9 +106,8 @@ $8+4+4$ 子带划分): $$Y_p = \sum_{q\in C_p}\Bigl(\mathrm{Re}X_q + j\,s_q\,\mathrm{Im}X_q\Bigr),\qquad s_q\in\{\pm1\}$$ -loader 校验 archive 和每个数组的 SHA-256;table version、所有数组 hash 与 -77 个 band-center 参考值都属于 cache key。标准可公开获取不等于获准实施相关 -专利;更多来源信息见 [`data/README.md`](../data/README.md) 与 +固定表在编译期确定,运行时不加载外部 archive,也不参与缓存键。标准可公开 +获取不等于获准实施相关专利;来源与权利边界见 [`THIRD_PARTY_NOTICES.md`](../THIRD_PARTY_NOTICES.md)。 ## `.jochrtf` diff --git a/docs/math.en.md b/docs/math.en.md index 64de2ae..46874ff 100644 --- a/docs/math.en.md +++ b/docs/math.en.md @@ -147,15 +147,17 @@ If the clipgain field consists of integer $x$ and mantissa $y$, then $$ G_{\mathrm{clip}}= -1+\frac{y}{32}2^{x-4}. +2^{x-4}\left(1+\frac{y}{32}\right). $$ -It is folded into the analysis-side scalar $S=\frac{1}{16}G_{\mathrm{clip}}$, which multiplies -every core-bed sample before the QMF analysis; the $\times16$ after the object inverse QMF cancels the -$1/16$, so the net effect is a multiply by $G_{\mathrm{clip}}$ and the $[-1,1]$ clamp sits **after** the -gain. The scalar keeps two per-frame slots: the first 256 samples of a frame ramp linearly from the -previous frame's value to this frame's value and the rest stays constant, and the first frame after a -reset takes the new value directly. LFE bypasses this scalar. +Its range is $[0.0625,15.75]$; at $x=4$ it reduces to $1+y/32$. It is folded into the analysis-side +scalar $S=\frac{1}{16}G_{\mathrm{clip}}$, which multiplies every core-bed sample before the QMF +analysis; the $\times16$ after the object inverse QMF cancels the $1/16$, so the net effect is a multiply +by $G_{\mathrm{clip}}$ and the $[-1,1]$ clamp sits **after** the gain. The scalar keeps two per-frame +slots: the first 256 samples of a frame ramp linearly from the previous frame's value to this frame's +value and the rest stays constant, and the first frame after a reset takes the new value directly. LFE +uses a separate path but applies the same full $G_{\mathrm{clip}}$ as a sample-wise ramp before its +1217-sample delay; see section 8. ## 3. Parameter-band expansion and time interpolation @@ -190,7 +192,13 @@ Thus $p(b)=k$ if and only if $b\in\mathcal B_k$. Other parameter-band counts use ### 3.2 One-data-point interpolation -Let $P_{o,c,b}$ be the previous frame-end value and $D_{o,c,p(b)}$ the current target. For slot $t=0\ldots23$: +Let $P_{o,c,b}$ be the previous frame-end value and $D_{o,c,p(b)}$ the current target. When the object +has no previous state yet (the first frame of the stream, or the object's first appearance), a single +data point keeps all 24 slots at $D_{o,c,p(b)}$ (hard cut, no fade-in from zero); with two data points, +the first 12 slots stay at $D_0$ and the last 12 slots still interpolate from $D_0$ to $D_1$ using the +second segment of section 3.3 (with $P$ set to $D_0$). + +With a previous state, for slot $t=0\ldots23$: $$ \alpha_t=\frac{t+1}{24}, @@ -208,11 +216,31 @@ $$ M_{o,c,b,23}=D_{o,c,p(b)}. $$ -This value then becomes the previous state for the next frame. +This value then becomes the previous state for the next frame. Once an object's present flag goes from 1 +to 0, it keeps being published with an all-zero matrix and its previous state is updated to zero until it +becomes present again. ### 3.3 Multiple data points -When a frame contains two data points, `offset_ts` gives the segment boundary. Each segment uses the same linear relation between the previous and next targets; step mode switches targets at the designated slot. +`offset_ts` gives the first slot at which each data point becomes active, using the raw 5-bit value +(legal range $0\ldots23$); the values of one object must be strictly increasing. In the default mode +(`slope_idx=0`), two data points $D_0$ and $D_1$ interpolate as + +$$ +M_{o,c,b,t}= +\begin{cases} +(1-\alpha_t)P_{o,c,b}+\alpha_tD_{0}, & t=0\ldots10,\ \alpha_t=\dfrac{t+1}{12},\\[1mm] +D_{0}, & t=11,\\[1mm] +(1-\beta_t)D_{0}+\beta_tD_{1}, & t=12\ldots23,\ \beta_t=\dfrac{t-11}{12}. +\end{cases} +$$ + +When no previous state exists, the same formulas apply with $P_{o,c,b}$ set to $D_0$. + +Step mode (`slope_idx=1`) switches targets directly at the designated slots: for $t(processed_samples_), - positions); + &positions[0][0], + timeline::kTimelineObjects); if (!queried.ok()) { return queried; } diff --git a/src/cli/joc_cli.cpp b/src/cli/joc_cli.cpp index 9a509c9..74460cc 100644 --- a/src/cli/joc_cli.cpp +++ b/src/cli/joc_cli.cpp @@ -83,7 +83,7 @@ struct Options { }; const char* kLayoutChoices = - "2.0 3.1 4.0 5.1 7.1 5.1.2 5.1.4 7.1.2 7.1.4 9.1.4 9.1.6"; + "2.0 3.1 4.0 5.1 7.1 5.1.2 5.1.4 7.1.2 7.1.4 9.1.4 9.1.6 6.0"; void print_usage() { std::printf( @@ -97,7 +97,7 @@ void print_usage() { " input 输入 .m4a/.eac3/.ec3\n" "\n" "模式(默认输出 ADM BWF):\n" - " --speaker-layout L 直接扬声器渲染布局,例如 2.0、5.1、7.1.2\n" + " --speaker-layout L 直接扬声器渲染布局,例如 2.0、5.1、6.0、7.1.2\n" " 可选值: %s\n" " --binaural 直接双耳渲染;不生成临时 ADM BWF\n" "\n" diff --git a/src/emdf/emdf_parser.cpp b/src/emdf/emdf_parser.cpp index 2f85c1f..f6a0788 100644 --- a/src/emdf/emdf_parser.cpp +++ b/src/emdf/emdf_parser.cpp @@ -102,9 +102,15 @@ Status parse_at(const std::uint8_t* data, std::size_t size, std::size_t start_bi } const std::uint32_t has_sample_offset = reader.read(1); - std::uint16_t sample_offset = 0; + std::uint32_t sample_offset = 0; if (has_sample_offset != 0u) { - sample_offset = static_cast(reader.read(12) >> 1); + std::uint32_t value = 0; + if (!bits::variable_bits(reader, 11, 8, &value)) { + return reader.error() == JOC_ERR_BITSTREAM_TRUNCATED + ? truncated_fail(reader) + : syntax_fail(reader.error_message()); + } + sample_offset = value; } if (reader.read(1) != 0u) { std::uint32_t ignored = 0; diff --git a/src/emdf/emdf_parser.h b/src/emdf/emdf_parser.h index 667c7ef..e1fe9a9 100644 --- a/src/emdf/emdf_parser.h +++ b/src/emdf/emdf_parser.h @@ -17,7 +17,7 @@ inline constexpr std::size_t kMaxPayloads = JOC_MAX_EMDF_PAYLOADS; struct Payload { std::uint8_t id = 0; - std::uint16_t sample_offset = 0; + std::uint32_t sample_offset = 0; // EMDF outer smpoffst: variable_bits(11) std::size_t bit_offset = 0; // MSB-first bit position of the payload bytes std::size_t size = 0; // payload byte count // Payload configuration data (ETSI TS 102 366 H.2.2.3). Spec section 8.2 and diff --git a/src/hrtf/rosella_renderer.cpp b/src/hrtf/rosella_renderer.cpp index 31f9462..85c9f09 100644 --- a/src/hrtf/rosella_renderer.cpp +++ b/src/hrtf/rosella_renderer.cpp @@ -1120,7 +1120,8 @@ struct RosellaRuntime::Impl { // rosella_binaural_renderer._set_block_parameters. Status set_block_parameters(std::int64_t sample) { double positions[timeline::kTimelineObjects][3] = {}; - Status status = timeline.positions_at(sample, positions); + Status status = timeline.positions_at(sample, &positions[0][0], + timeline::kTimelineObjects); if (!status.ok()) { return status; } diff --git a/src/joc_bitstream/bed_layout_map.cpp b/src/joc_bitstream/bed_layout_map.cpp index d5aae8d..369dd98 100644 --- a/src/joc_bitstream/bed_layout_map.cpp +++ b/src/joc_bitstream/bed_layout_map.cpp @@ -1,6 +1,7 @@ #include "joc_bitstream/bed_layout_map.h" +#include #include #include "speaker/speaker_layouts.h" @@ -100,6 +101,9 @@ constexpr LayoutName kLayoutNames[] = { {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | bit(BedChannel::kCenter) | bit(BedChannel::kLfe), "5.1"}, + {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | + bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | + bit(BedChannel::kLeftRearSurround) | bit(BedChannel::kRightRearSurround), "6.0"}, {bit(BedChannel::kLeft) | bit(BedChannel::kRight) | bit(BedChannel::kLeftSurround) | bit(BedChannel::kRightSurround) | bit(BedChannel::kCenter) | bit(BedChannel::kLfe) | @@ -328,4 +332,301 @@ bool layout_bed_anchor(std::uint32_t out_ch_config, BedChannel channel, return false; } + +namespace { + +// ETSI TS 103 420 Table B.10: the fixed ADM position of every Table 12 bed +// channel. bed_output_description() matches these points against the target +// layout; the kChannels table above keeps the ADM/binaural path's coordinates. +struct TableB10Position { + BedChannel channel; + double xyz[3]; +}; + +constexpr TableB10Position kTableB10[oamd::kBedLabelCount] = { + {BedChannel::kLeft, {-1.0, 1.0, 0.0}}, + {BedChannel::kRight, {1.0, 1.0, 0.0}}, + {BedChannel::kCenter, {0.0, 1.0, 0.0}}, + {BedChannel::kLfe, {-1.0, 1.0, -1.0}}, + {BedChannel::kLeftSurround, {-1.0, 0.0, 0.0}}, + {BedChannel::kRightSurround, {1.0, 0.0, 0.0}}, + {BedChannel::kLeftRearSurround, {-1.0, -1.0, 0.0}}, + {BedChannel::kRightRearSurround, {1.0, -1.0, 0.0}}, + {BedChannel::kTopFrontLeft, {-1.0, 1.0, 1.0}}, + {BedChannel::kTopFrontRight, {1.0, 1.0, 1.0}}, + {BedChannel::kTopSideLeft, {-1.0, 0.0, 1.0}}, + {BedChannel::kTopSideRight, {1.0, 0.0, 1.0}}, + {BedChannel::kTopBackLeft, {-1.0, -1.0, 1.0}}, + {BedChannel::kTopBackRight, {1.0, -1.0, 1.0}}, + {BedChannel::kLeftWide, {-1.0, 21.0 / 31.0, 0.0}}, + {BedChannel::kRightWide, {1.0, 21.0 / 31.0, 0.0}}, + {BedChannel::kLfe2, {1.0, 1.0, -1.0}}, +}; + +// bed_layout.BED_REFERENCE_POSITIONS_Q15; index 3 is the non-spatial sentinel. +constexpr std::int32_t kReferencePositions[kBedReferencePositionCount][3] = { + {0, 0, 0}, {32767, 0, 0}, {16384, 0, 0}, {-1, -1, -1}, + {0, 16384, 0}, {32767, 16384, 0}, {0, 32767, 0}, {32767, 32767, 0}, + {16384, 32767, 0}, {5285, 0, 0}, {27483, 0, 0}, {0, 5285, 0}, + {32767, 5285, 0}, {7928, 0, 32767}, {24840, 0, 32767}, {7928, 7928, 32767}, + {24840, 7928, 32767}, {7928, 16384, 32767}, {24840, 16384, 32767}, + {7928, 24840, 32767}, {24840, 24840, 32767}, {7928, 32767, 32767}, + {24840, 32767, 32767}, {11099, 0, 0}, {21669, 0, 0}, {0, 11099, 0}, + {32767, 11099, 0}, {0, 21669, 0}, {32767, 21669, 0}, {0, 27483, 0}, + {32767, 27483, 0}, {5285, 32767, 0}, {27483, 32767, 0}, {11099, 32767, 0}, + {21669, 32767, 0}, {16384, 0, 32767}, {16384, 16384, 32767}, + {16384, 32767, 32767}, +}; + +// 自动 trim 的每个布局基准衰减,单位 dB。 +struct AttenuationEntry { + const char* compact_name; + double db; +}; + +constexpr AttenuationEntry kAttenuation[] = { + {"20", -4.5}, {"31", -4.5}, {"40", -3.0}, {"51", -3.0}, + {"60", -1.5}, {"71", -1.5}, {"512", -2.25}, {"514", -1.5}, + {"712", -0.75}, {"714", 0.0}, {"914", 0.0}, {"916", 0.0}, +}; + +constexpr int kMaxChannels = static_cast(ejoc::speaker_tables::kMaxChannels); + +void set_sentinel(std::int32_t coordinate[3]) { + coordinate[0] = -1; + coordinate[1] = -1; + coordinate[2] = -1; +} + +bool is_sentinel(const std::int32_t coordinate[3]) { + return coordinate[0] == -1 && coordinate[1] == -1 && coordinate[2] == -1; +} + +bool is_lfe_label(BedChannel channel) { + return channel == BedChannel::kLfe || channel == BedChannel::kLfe2; +} + +const TableB10Position* table_b10(BedChannel channel) { + for (const TableB10Position& entry : kTableB10) { + if (entry.channel == channel) { + return &entry; + } + } + return nullptr; +} + +// ADM 笛卡尔坐标 -> 渲染器方位 (u 右, v 后, w 上)。 +void render_coordinates(const double xyz[3], double out[3]) { + out[0] = (xyz[0] + 1.0) * 0.5; + out[1] = (1.0 - xyz[1]) * 0.5; + out[2] = xyz[2]; +} + +// 渲染器方位的欧氏距离,比较的是归一化到 0..1 的坐标。 +double render_distance(const double left[3], const double right[3]) { + const double du = left[0] - right[0]; + const double dv = left[1] - right[1]; + const double dw = left[2] - right[2]; + return std::sqrt(du * du + dv * dv + dw * dw); +} + +// Reference-position matching: the non-spatial sentinel resolves to index 3. +int nearest_reference(const std::int32_t coordinate[3]) { + if (is_sentinel(coordinate)) { + return 3; + } + const double position[3] = { + static_cast(coordinate[0]) / 32768.0, + static_cast(coordinate[1]) / 32768.0, + static_cast(coordinate[2]) / 32768.0, + }; + int best = 3; + double best_distance = 0.0; + bool have_best = false; + for (int index = 0; index < kBedReferencePositionCount; ++index) { + const std::int32_t* candidate = kReferencePositions[index]; + if (candidate[0] == -1 && candidate[1] == -1 && candidate[2] == -1) { + continue; + } + const double point[3] = { + static_cast(candidate[0]) / 32768.0, + static_cast(candidate[1]) / 32768.0, + static_cast(candidate[2]) / 32768.0, + }; + const double distance = render_distance(position, point); + if (!have_best || distance < best_distance) { + best = index; + best_distance = distance; + have_best = true; + } + } + return best; +} + +// 目标布局里离 desired 最近、且在 0.05 容差内的输出声道;没有返回 -1。 +int nearest_channel(const double desired[3], const std::int32_t positions[][3], + std::uint32_t channel_count) { + int best = -1; + double best_distance = 0.0; + bool have_best = false; + for (std::uint32_t index = 0; index < channel_count; ++index) { + if (is_sentinel(positions[index])) { + continue; + } + const double point[3] = { + static_cast(positions[index][0]) / 32768.0, + static_cast(positions[index][1]) / 32768.0, + static_cast(positions[index][2]) / 32768.0, + }; + const double distance = render_distance(desired, point); + if (!have_best || distance < best_distance) { + best = static_cast(index); + best_distance = distance; + have_best = true; + } + } + if (have_best && best_distance <= 0.05) { + return best; + } + return -1; +} + +// 输出声道 index -> 该声道的 Q15 方位;LFE 与非空间声道给 sentinel。 +void channel_positions(const speaker::LayoutInfo& layout, + std::int32_t positions[][3]) { + for (int index = 0; index < kMaxChannels; ++index) { + set_sentinel(positions[index]); + } + const auto* geometry = ejoc::speaker_tables::find_layout(layout.speaker_bitfield); + if (geometry != nullptr) { + bool known[kMaxChannels] = {}; + std::int32_t found[kMaxChannels][3]; + for (int index = 0; index < kMaxChannels; ++index) { + set_sentinel(found[index]); + } + for (const auto& region : geometry->regions) { + for (std::size_t point = 0; point < region.point_count; ++point) { + const auto speaker = static_cast(region.points[point].speaker_id); + if (speaker < 0 || speaker >= kMaxChannels || known[speaker]) { + continue; + } + for (int axis = 0; axis < 3; ++axis) { + found[speaker][axis] = + static_cast(region.points[point].coordinate_q15[axis]); + } + known[speaker] = true; + } + } + const std::uint32_t count = + layout.channel_count < static_cast(kMaxChannels) + ? layout.channel_count + : static_cast(kMaxChannels); + for (std::uint32_t index = 0; index < count; ++index) { + const char* name = layout.channel_names != nullptr ? layout.channel_names[index] + : nullptr; + if (name != nullptr && std::strcmp(name, "LFE") == 0) { + continue; + } + if (known[index]) { + for (int axis = 0; axis < 3; ++axis) { + positions[index][axis] = found[index][axis]; + } + } + } + } +} + +int channel_index(const speaker::LayoutInfo& layout, const char* name) { + if (name == nullptr || layout.channel_names == nullptr) { + return -1; + } + for (std::uint32_t index = 0; index < layout.channel_count; ++index) { + if (std::strcmp(layout.channel_names[index], name) == 0) { + return static_cast(index); + } + } + return -1; +} + +double attenuation_db(const speaker::LayoutInfo& layout) { + for (const AttenuationEntry& entry : kAttenuation) { + if (std::strcmp(entry.compact_name, layout.compact_name) == 0) { + return entry.db; + } + } + return 0.0; +} + +} // namespace + +const std::int32_t (*bed_reference_positions_q15())[3] { + return kReferencePositions; +} + +BedOutputDescription bed_output_description(const speaker::LayoutInfo& layout) { + BedOutputDescription description; + std::int32_t positions[kMaxChannels][3]; + channel_positions(layout, positions); + const std::uint32_t output_count = + layout.channel_count < static_cast(kMaxChannels) + ? layout.channel_count + : static_cast(kMaxChannels); + description.output_labels.reserve(output_count); + for (std::uint32_t index = 0; index < output_count; ++index) { + description.output_labels.push_back(nearest_reference(positions[index])); + } + for (int label = 0; label < oamd::kBedLabelCount; ++label) { + const BedChannel channel = static_cast(label); + const char* name = channel == BedChannel::kLfe2 ? nullptr + : bed_channel_short_name(channel); + int slot = -1; + if (name != nullptr && name[0] != '\0') { + slot = channel_index(layout, name); + } + if (slot < 0 && channel == BedChannel::kLfe2) { + slot = channel_index(layout, "LFE"); + } + const TableB10Position* position = table_b10(channel); + double desired[3] = {0.0, 0.0, 0.0}; + if (position != nullptr) { + render_coordinates(position->xyz, desired); + } + if (slot < 0 && position != nullptr) { + slot = nearest_channel(desired, positions, output_count); + } + if (slot < 0) { + description.label_to_slot[label] = is_lfe_label(channel) ? -1 : -2; + } else { + description.label_to_slot[label] = slot; + } + if (is_lfe_label(channel)) { + description.direct_output_labels[label] = 3; + } else { + int best = 3; + double best_distance = 0.0; + bool have_best = false; + for (int index = 0; index < kBedReferencePositionCount; ++index) { + const std::int32_t* candidate = kReferencePositions[index]; + if (candidate[0] == -1 && candidate[1] == -1 && candidate[2] == -1) { + continue; + } + const double point[3] = { + static_cast(candidate[0]) / 32768.0, + static_cast(candidate[1]) / 32768.0, + static_cast(candidate[2]) / 32768.0, + }; + const double distance = render_distance(desired, point); + if (!have_best || distance < best_distance) { + best = index; + best_distance = distance; + have_best = true; + } + } + description.direct_output_labels[label] = best; + } + } + description.automatic_attenuation_db = attenuation_db(layout); + return description; +} + } // namespace joc::joc diff --git a/src/joc_bitstream/bed_layout_map.h b/src/joc_bitstream/bed_layout_map.h index 306c2d3..c5e2f2b 100644 --- a/src/joc_bitstream/bed_layout_map.h +++ b/src/joc_bitstream/bed_layout_map.h @@ -6,31 +6,12 @@ #include #include "oamd/oamd_parser.h" +#include "speaker/speaker_layout_lookup.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, -}; +// BedChannel lives in oamd/oamd_parser.h: the enum is the OAMD Table 12 +// label order and OAMD frame state carries it directly. // 内容声明的 bed 布局。bed 对象在 OAMD 里没有位置字段,位置由这里的布局决定。 struct BedLayout { @@ -61,8 +42,28 @@ bool bed_channel_adm_position(BedChannel channel, double xyz[3]); bool bed_channel_q15(BedChannel channel, std::uint16_t q15[3]); // 目标布局里同名扬声器的 Q15 方位:bed 声道直连到该扬声器,而不是参与声像。 -// 目标布局没有这个声道时返回 false。 +// 目标布局没有这个声道时返回 false。4.0 与 6.0 共用未分配的 out_ch_config +// 0xFF,因此这里按 4.0 的声道集合解释 0xFF。 bool layout_bed_anchor(std::uint32_t out_ch_config, BedChannel channel, std::uint16_t q15[3]); +// 参考位置网格 ``BED_REFERENCE_POSITIONS_Q15``:bed 标签与输出 slots 的最近邻 +// 匹配都用这一组固定 Q15 点。索引 3 是 (-1,-1,-1),表示 LFE 那样的非空间声源。 +inline constexpr int kBedReferencePositionCount = 38; +const std::int32_t (*bed_reference_positions_q15())[3]; + +// 一个目标扬声器布局的 bed 路由描述(bed_layout.bed_output_description): +// 按标准 bed 标签序(Table 12 的 17 个标签)给出目标 layout 的输出参考位置、 +// 每个标签的输出 slot 与直连参考位置,以及该布局的自动 trim 基准衰减。 +struct BedOutputDescription { + std::vector output_labels; // 每个输出声道最近邻的参考位置索引 + int label_to_slot[oamd::kBedLabelCount] = {}; // -2 不可达,-1 LFE 通路 + int direct_output_labels[oamd::kBedLabelCount] = {}; + double automatic_attenuation_db = 0.0; +}; + +// 由布局名/几何派生 bed 路由描述;``layout.channel_names`` 必须按标准声道 +// 顺序给出 ``layout.channel_count`` 个名字。 +BedOutputDescription bed_output_description(const speaker::LayoutInfo& layout); + } // namespace joc::joc diff --git a/src/joc_bitstream/joc_parser.cpp b/src/joc_bitstream/joc_parser.cpp index 8c079f9..08055b4 100644 --- a/src/joc_bitstream/joc_parser.cpp +++ b/src/joc_bitstream/joc_parser.cpp @@ -182,6 +182,55 @@ std::int16_t num_bands_for_index(std::uint32_t num_bands_idx) { return -1; } +namespace { + +// Reads the 12-bit joc_header at the start of the payload. Mirrors +// parse_joc_header(): a downmix configuration outside 0..4 is a frame status, +// not an exception, because a metadata scan records it as an unsupported +// variant while the frame itself stays classified. +Status parse_id14_header(const std::uint8_t* payload, std::size_t payload_size, + joc_frame_params* out) { + bits::BitReader reader(payload, payload_size); + out->dmx_config_idx = static_cast(reader.read(3)); + out->num_objects_bits = static_cast(reader.read(6)); + out->ext_config_idx = static_cast(reader.read(3)); + if (reader.failed()) { + return truncated_fail(reader); + } + const std::int16_t n_channels = num_channels_for_config(out->dmx_config_idx); + if (n_channels < 0) { + // The core channel count decides how the object syntax expands, so an + // unknown configuration stops the parse after the header fields. + out->status = JOC_FRAME_STATUS_UNSUPPORTED_DMX; + out->objects_clamped = 0u; + out->n_objects = 0u; + return Status::success(); + } + out->n_channels = static_cast(n_channels); + const std::uint32_t declared = static_cast(out->num_objects_bits) + 1u; + out->objects_clamped = declared > JOC_MAX_OBJECTS ? 1u : 0u; + out->n_objects = static_cast( + std::min(declared, static_cast(JOC_MAX_OBJECTS))); + if (out->objects_clamped != 0u) { + out->status = JOC_FRAME_STATUS_OBJECTS_CLAMPED; + } + return Status::success(); +} + +// Records where joc_data ends and the first trailing bytes, like _tail_info(). +void tail_info(joc_frame_params* out, const bits::BitReader& reader, const std::uint8_t* payload, + std::size_t payload_size) { + out->data_end_bits = static_cast(reader.position()); + out->trailing_bits = static_cast(payload_size * 8u - reader.position()); + const std::size_t tail_offset = reader.position() / 8u; + if (tail_offset < payload_size) { + const std::size_t tail_bytes = std::min(8u, payload_size - tail_offset); + std::memcpy(out->tail_bytes, payload + tail_offset, tail_bytes); + } +} + +} // namespace + Status parse_id14(const std::uint8_t* payload, std::size_t payload_size, joc_frame_params* out, FrameSymbols* symbols) { if (payload == nullptr || out == nullptr) { @@ -195,45 +244,53 @@ Status parse_id14(const std::uint8_t* payload, std::size_t payload_size, joc_fra FrameSymbols& capture = (symbols != nullptr) ? *symbols : local_symbols; std::memset(&capture, 0, sizeof(capture)); - bits::BitReader reader(payload, payload_size); - - out->dmx_config_idx = static_cast(reader.read(3)); - out->num_objects_bits = static_cast(reader.read(6)); - out->ext_config_idx = static_cast(reader.read(3)); - - const std::uint32_t declared_objects = static_cast(out->num_objects_bits) + 1u; - const std::int16_t n_channels = num_channels_for_config(out->dmx_config_idx); - if (n_channels < 0) { - return syntax_fail("unknown JOC downmix configuration " + - std::to_string(out->dmx_config_idx)); + const Status header = parse_id14_header(payload, payload_size, out); + if (!header.ok()) { + return header; } - out->n_channels = static_cast(n_channels); - // A declared object count above 15 is clamped to 15: the frame is parsed for - // 15 objects only, and the clamp is reported separately. - out->objects_clamped = declared_objects > JOC_MAX_OBJECTS ? 1u : 0u; - const std::uint32_t n_objects = - std::min(declared_objects, JOC_MAX_OBJECTS); - out->n_objects = static_cast(n_objects); + bits::BitReader reader(payload, payload_size); + reader.skip(12); out->clipgain_x_bits = static_cast(reader.read(3)); out->clipgain_y_bits = static_cast(reader.read(5)); out->seq_count = reader.read(10); - // clipgain = 1 + (y/32) * 2^(x-4); ldexp builds the power of two exactly - // over the whole legal range. - out->clipgain = 1.0 + static_cast(out->clipgain_y_bits) / 32.0 * - std::ldexp(1.0, static_cast(out->clipgain_x_bits) - 4); + if (reader.failed()) { + return truncated_fail(reader); + } + // clipgain = 2^(x-4) * (1 + y/32); ldexp builds the power of two exactly + // over the whole legal range, so the result is bit-exact to the reference. + out->clipgain = std::ldexp(1.0, static_cast(out->clipgain_x_bits) - 4) * + (1.0 + static_cast(out->clipgain_y_bits) / 32.0); + + if (out->status == JOC_FRAME_STATUS_UNSUPPORTED_DMX) { + tail_info(out, reader, payload, payload_size); + return Status::success(); + } + const std::int16_t n_channels = num_channels_for_config(out->dmx_config_idx); + const std::uint32_t n_objects = out->n_objects; for (std::uint32_t obj = 0; obj < n_objects; ++obj) { joc_object_params& info = out->objects[obj]; info.present = static_cast(reader.read(1)); + if (reader.failed()) { + return truncated_fail(reader); + } if (info.present == 0u) { continue; } info.num_bands_idx = static_cast(reader.read(3)); + if (reader.failed()) { + return truncated_fail(reader); + } const std::int16_t bands = num_bands_for_index(info.num_bands_idx); if (bands < 0) { - return syntax_fail("unknown JOC num_bands index " + - std::to_string(info.num_bands_idx)); + // The symbol count depends on the parameter-band count, so the rest + // of the payload cannot be parsed. + if (out->status == JOC_FRAME_STATUS_OK) { + out->status = JOC_FRAME_STATUS_BAD_PARAMETER_BAND; + } + tail_info(out, reader, payload, payload_size); + return Status::success(); } info.n_bands = static_cast(bands); info.sparse = static_cast(reader.read(1)); @@ -241,16 +298,42 @@ Status parse_id14(const std::uint8_t* payload, std::size_t payload_size, joc_fra info.slope_idx = static_cast(reader.read(1)); info.num_dpoints_bits = static_cast(reader.read(1)); info.n_dpoints = static_cast(info.num_dpoints_bits + 1u); + if (reader.failed()) { + return truncated_fail(reader); + } if (info.slope_idx == 1u) { for (std::uint32_t dp = 0; dp < info.n_dpoints; ++dp) { - info.offset_ts[dp] = static_cast(reader.read(5) + 1u); + info.offset_ts[dp] = static_cast(reader.read(5)); + } + if (reader.failed()) { + return truncated_fail(reader); + } + // offset_ts is the raw 5-bit slot index (legal range 0..23) and one + // object's data points must be strictly increasing. + bool invalid = false; + for (std::uint32_t dp = 0; dp < info.n_dpoints; ++dp) { + if (info.offset_ts[dp] >= JOC_TIMESLOTS) { + invalid = true; + break; + } + } + if (!invalid && info.n_dpoints > 1u) { + for (std::uint32_t dp = 1; dp < info.n_dpoints; ++dp) { + if (info.offset_ts[dp] <= info.offset_ts[dp - 1u]) { + invalid = true; + break; + } + } + } + if (invalid && out->status == JOC_FRAME_STATUS_OK) { + out->status = JOC_FRAME_STATUS_BAD_OFFSET_TS; } } } - if (reader.failed()) { - return truncated_fail(reader); - } + // joc_data (Huffman): dense reads MTX per channel and band, sparse reads IDX + // then VEC. A frame whose status is already non-zero dequantizes below but + // is never rendered, so a misaligned parse cannot reach the DSP. for (std::uint32_t obj = 0; obj < n_objects; ++obj) { const joc_object_params& info = out->objects[obj]; if (info.present == 0u) { @@ -300,12 +383,16 @@ Status parse_id14(const std::uint8_t* payload, std::size_t payload_size, joc_fra } } - out->data_end_bits = static_cast(reader.position()); - out->trailing_bits = static_cast(payload_size * 8u - reader.position()); - const std::size_t tail_offset = reader.position() / 8u; - if (tail_offset < payload_size) { - const std::size_t tail_bytes = std::min(8u, payload_size - tail_offset); - std::memcpy(out->tail_bytes, payload + tail_offset, tail_bytes); + tail_info(out, reader, payload, payload_size); + if (out->status == JOC_FRAME_STATUS_OK && out->ext_config_idx == 0u) { + // The tail pads with zero bits up to the next byte; a whole byte after + // that alignment is unexpected data. + const std::uint32_t pad = + (8u - (static_cast(reader.position()) % 8u)) % 8u; + const std::uint32_t total_bits = static_cast(payload_size * 8u); + if (total_bits > static_cast(reader.position()) + pad) { + out->status = JOC_FRAME_STATUS_TRAILING_DATA; + } } for (std::uint32_t obj = 0; obj < n_objects; ++obj) { @@ -313,8 +400,7 @@ Status parse_id14(const std::uint8_t* payload, std::size_t payload_size, joc_fra if (info.present == 0u) { continue; } - std::uint32_t mask_bit = 1u << obj; - out->present_mask |= mask_bit; + out->present_mask |= 1u << obj; const std::int64_t nquant = (info.quant_idx == 0u) ? 96 : 192; std::int64_t q[JOC_MAX_DPOINTS][JOC_MAX_CORE_CHANNELS][JOC_MAX_PARAMETER_BANDS] = {}; @@ -372,7 +458,13 @@ int score_container_as_joc(const std::uint8_t* data, std::size_t size, if (emdf::extract_payload_bytes(data, size, *payload, &bytes).ok()) { joc_frame_params params; FrameSymbols symbols; - parses = parse_id14(bytes.data(), bytes.size(), ¶ms, &symbols).ok(); + if (parse_id14(bytes.data(), bytes.size(), ¶ms, &symbols).ok()) { + // The reference scores a payload by the frame status: an unsupported + // downmix configuration (2) or an unknown parameter-band count (4) + // leaves the syntax unparsable and must not win the container choice. + parses = params.status != JOC_FRAME_STATUS_UNSUPPORTED_DMX && + params.status != JOC_FRAME_STATUS_BAD_PARAMETER_BAND; + } } const bool associated = oamd->has_group_id != 0u && payload->has_group_id != 0u && oamd->group_id == payload->group_id; @@ -406,7 +498,20 @@ Status parse_eac3_frame(const std::uint8_t* frame, std::size_t frame_size, joc_f if (!extract.ok()) { return extract; } - return parse_id14(bytes.data(), bytes.size(), out, symbols); + const Status parsed = parse_id14(bytes.data(), bytes.size(), out, symbols); + if (!parsed.ok()) { + return parsed; + } + // A metadata scan rejects an unsupported downmix configuration outright: the + // core channel count decides how every later frame is expanded, so this is a + // variant error rather than a per-frame status. + if (out->status == JOC_FRAME_STATUS_UNSUPPORTED_DMX) { + const std::string message = + "downmix_config_unsupported: ID14 declares joc_dmx_config_idx=" + + std::to_string(out->dmx_config_idx) + " (legal values are 0..4)"; + return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage::kJoc, message); + } + return Status::success(); } Status check_id14_padding(const std::uint8_t* payload, std::size_t payload_size, @@ -417,6 +522,17 @@ Status check_id14_padding(const std::uint8_t* payload, std::size_t payload_size, if (!status.ok()) { return status; } + // The two statuses that stop the parse early leave the payload without a + // joc_data end position, so they keep reporting the syntax failure they + // reported before the frame-status split. + if (params.status == JOC_FRAME_STATUS_UNSUPPORTED_DMX) { + return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage::kJoc, + "downmix_config_unsupported: joc_dmx_config_idx outside 0..4"); + } + if (params.status == JOC_FRAME_STATUS_BAD_PARAMETER_BAND) { + return Status::fail(JOC_ERR_JOC_SYNTAX, stage::kJoc, + "unknown JOC num_bands index stops the payload"); + } if (out_trailing_bits != nullptr) { *out_trailing_bits = params.trailing_bits; } @@ -861,6 +977,7 @@ Status check_cross_layer(const joc_frame_params& params, const eac3::Frame* core std::uint16_t chanmap, const std::vector* bed_assignments, std::string* error, CrossLayerNotes* notes) { + (void)has_dependent; const std::uint8_t config_idx = params.dmx_config_idx; const int core_channels = params.n_channels; std::vector wanted; diff --git a/src/joc_core/eac3joc_core.cpp b/src/joc_core/eac3joc_core.cpp index b92da56..42b52a3 100644 --- a/src/joc_core/eac3joc_core.cpp +++ b/src/joc_core/eac3joc_core.cpp @@ -69,6 +69,19 @@ public: std::memset(synthesis_state_, 0, sizeof(synthesis_state_)); std::memset(x_, 0, sizeof(x_)); std::memset(z_, 0, sizeof(z_)); + // Frame-level state: per-object previous matrices and presence markers, + // the seq state machine, the 8-plane passthrough history, the LFE ramp + // start value and the previous actual core-channel count. + std::memset(prev_matrix_, 0, sizeof(prev_matrix_)); + std::memset(prev_present_, 0, sizeof(prev_present_)); + std::memset(core_history_, 0, sizeof(core_history_)); + state_ = 0; + expected_seq_ = 0; + invalid_count_ = 0; + last_frame_valid_ = false; + n_channels_prev_ = -1; + has_lfe_clipgain_ = false; + lfe_clipgain_ = 0.0; // Sentinel: the first frame after reset takes the new scalar directly, // without an intra-frame transition. analysis_phase_ = -0.5f; @@ -174,13 +187,22 @@ public: } std::memset(output16, 0, sizeof(float) * EJOC_OUTPUT_CHANNELS * EJOC_FRAME_SAMPLES); - analysis(bed_planar, phase_new, clipgain); - render_lfe(lfe, output_scale, output16); + // Analysis-side scalar = the structural 1/16 times this frame's clipgain. + // clipgain enters the analysis at this stage and the x16 at the output + // cancels the 1/16, so the net effect is a multiply by joc_clipgain. + analysis(bed_planar, static_cast(phase_new * clipgain)); + decode_lfe(lfe, clipgain, output16, output_scale); process_objects(object_mask, n_bands, n_dpoints, slope_idx, offset_ts, dq, output_scale, output16); return 0; } + // Frame-level entry point; the state machine and per-object history live + // inside the renderer (see render_frame_impl()). + int render_frame(const ejoc_frame_input& input) noexcept { + return render_frame_impl(input); + } + private: void initialize_tables() noexcept { for (int i = 0; i < 64; ++i) { @@ -378,13 +400,12 @@ private: } } - void analysis(const float* bed_planar, const float phase_new, const double clipgain) noexcept { + // Frames the analysis QMF of the core PCM with the given analysis-side + // scalar (the structural 1/16 times joc_clipgain). The scalar ramps over the + // frame's first 256 samples from the previous frame's value; the first frame + // after a reset takes it directly. + void analysis(const float* bed_planar, const float scalar) noexcept { job_bed_ = bed_planar; - // Analysis-side scalar = the structural 1/16 times this frame's clipgain. - // clipgain enters the analysis at this stage and the x16 at the output - // cancels the 1/16, so the net effect is a multiply by joc_clipgain; the - // previous frame's scalar is the ramp start. - const float scalar = static_cast(phase_new * clipgain); job_phase_old_ = analysis_phase_; if (job_phase_old_ <= 0.0f && job_phase_old_ != 0.0f) { job_phase_old_ = scalar; @@ -509,11 +530,11 @@ private: } qmf5_step(state, rotated, pcm64); for (int sample = 0; sample < 64; ++sample) { + // The clamp is on the ×16 object PCM; joc_clipgain already + // entered the analysis, so only the user output gain remains, and + // it is a float32 multiply like the reference. const double clipped = clamp_unit(16.0 * pcm64[sample]); - // joc_clipgain already entered the analysis (see analysis()); what - // remains here is the user output gain. - const float value = static_cast(clipped * output_scale); - destination[timeslot * 64 + sample] = value; + destination[timeslot * 64 + sample] = static_cast(clipped) * output_scale; } } } @@ -533,8 +554,10 @@ private: const int item = job_objects_[index]; if (job_kind_ == JobKind::Analysis) { analysis_channel(item); - } else { + } else if (job_kind_ == JobKind::Objects) { process_one_object(item); + } else { + process_one_object_frame(item); } } } @@ -620,24 +643,477 @@ private: dispatch_jobs(); } - void render_lfe(const float* lfe, const float output_scale, float* output16) noexcept { - if (!lfe) { - return; + // Core LFE: the full joc_clipgain is applied as a sample-wise ramp before the + // 1217-sample delay, and the read-out is only clamped. The ramp is + // G_old + n*(G_new-G_old)/256 for n < 256 and G_new afterwards; the first + // frame after a reset uses G_old = G_new. + void decode_lfe(const float* lfe, const double clipgain, float* output16, + const float output_scale) noexcept { + const double new_gain = clipgain; + const double old_gain = has_lfe_clipgain_ ? lfe_clipgain_ : new_gain; + const bool ramped = old_gain != new_gain; + const double step = (new_gain - old_gain) / 256.0; + double ramp[256]; + for (int n = 0; n < 256; ++n) { + ramp[n] = ramped ? old_gain + static_cast(n) * step : new_gain; + } + has_lfe_clipgain_ = true; + lfe_clipgain_ = new_gain; + + float* channel0 = output16; + for (int sample = 0; sample < kLfeDelay; ++sample) { + channel0[sample] = + static_cast(clamp_unit(lfe_delay_[sample])) * output_scale; + } + for (int sample = kLfeDelay; sample < EJOC_FRAME_SAMPLES; ++sample) { + const int input = sample - kLfeDelay; + const double gain = input < 256 ? ramp[input] : new_gain; + const double value = (lfe != nullptr) ? static_cast(lfe[input]) : 0.0; + channel0[sample] = + static_cast(clamp_unit(value * gain)) * output_scale; } for (int sample = 0; sample < kLfeDelay; ++sample) { - const float value = static_cast(clamp_unit(lfe_delay_[sample])); - output16[sample] = static_cast(value * output_scale); - } - for (int sample = kLfeDelay; sample < 1536; ++sample) { - const float value = static_cast(clamp_unit(static_cast(lfe[sample - kLfeDelay]))); - output16[sample] = static_cast(value * output_scale); - } - for (int sample = 0; sample < kLfeDelay; ++sample) { - lfe_delay_[sample] = static_cast(lfe[sample + (1536 - kLfeDelay)]); + const int input = sample + (EJOC_FRAME_SAMPLES - kLfeDelay); + const double gain = input < 256 ? ramp[input] : new_gain; + const double value = (lfe != nullptr) ? static_cast(lfe[input]) : 0.0; + lfe_delay_[sample] = value * gain; } } - enum class JobKind : uint8_t { Analysis, Objects }; + enum class FrameMode : uint8_t { Normal, Hold, HardCut, Passthrough }; + + // ---- frame-level state machine ------------------------------------- // + // The Renderer owns the seq state machine (normal / hold / hard cut / + // passthrough), the per-object previous matrix and its presence marker, the + // analysis/synthesis state, the LFE delay ring with its clipgain ramp, and + // the 8-plane core history of the passthrough path, so one audio frame in + // produces the complete 16-channel object frame. + + float held_analysis_scalar() const noexcept { + // Sentinel: a renderer that has not seen a JOC frame yet reports the + // structural 1/16, i.e. clipgain 1.0. + return (analysis_phase_ <= 0.0f && analysis_phase_ != 0.0f) ? 0.0625f + : analysis_phase_; + } + + double held_clipgain() const noexcept { + return static_cast(held_analysis_scalar()) * 16.0; + } + + // The 10-bit JOC sequence counter wraps from 1023 back to 1. + static uint32_t next_seq(const uint32_t value) noexcept { + return value == 1023u ? 1u : value + 1u; + } + + // Drops the state of the core channels a smaller layout does not use. + void clear_unused_channels(const uint32_t n_channels) noexcept { + if (n_channels >= EJOC_MAX_CORE_CHANNELS) { + return; + } + for (uint32_t channel = n_channels; channel < EJOC_MAX_CORE_CHANNELS; ++channel) { + std::memset(analysis_fifo_[channel], 0, sizeof(analysis_fifo_[channel])); + } + for (int object = 0; object < EJOC_MAX_OBJECTS; ++object) { + for (uint32_t channel = n_channels; channel < EJOC_MAX_CORE_CHANNELS; ++channel) { + std::memset(prev_matrix_[object][channel], 0, + sizeof(prev_matrix_[object][channel])); + } + } + if (n_channels == 5u) { + // A 5-channel frame does not drive the extra-pair delay lanes. + std::memset(analysis_delay_[3], 0, sizeof(analysis_delay_[3])); + std::memset(analysis_delay_[4], 0, sizeof(analysis_delay_[4])); + } + } + + // A declared core-channel-count change (renderer status 9) clears the LFE + // ring; the clipgain ramp state is kept, so the gain does not retune. + void maybe_clear_lfe_ring(const uint32_t status) noexcept { + if (status == 9u) { + std::memset(lfe_delay_, 0, sizeof(lfe_delay_)); + } + } + + // Advances the seq state machine for this frame and reports the state the + // renderer was in before the frame. status 8 (no metadata) neither advances + // the expected counter nor compares the sequence. + uint32_t advance_state(const uint32_t status, const uint32_t seq) noexcept { + const uint32_t pre_state = state_; + const bool valid = status == 0u || status == 9u; + if (status == EJOC_FRAME_STATUS_NO_METADATA && pre_state != 0u) { + state_ = 3; + return pre_state; + } + expected_seq_ = next_seq(expected_seq_); + if (valid && seq == 0u && pre_state != 0u) { + // The programme restarted: hard cut and drop the object history. + state_ = 4; + return pre_state; + } + uint32_t state = state_; + if (state == 0u) { + if (valid) { + state = 1u; + expected_seq_ = seq; + } + } else if (state == 1u) { + if (valid) { + if (seq != expected_seq_) { + state = 4u; + } + } else { + state = 2u; + invalid_count_ = 1u; + } + } else if (state == 2u) { + if (valid) { + state = 1u; + expected_seq_ = seq; + } else if (invalid_count_ == 5u) { + state = 3u; + } else { + ++invalid_count_; + } + } else if (state == 3u) { + if (valid) { + state = 1u; + expected_seq_ = seq; + } else { + ++invalid_count_; + } + } else { + // state 4: the hard cut lasted one frame; a valid frame resyncs and + // an invalid one starts the invalid-frame count. + if (valid) { + state = 1u; + expected_seq_ = seq; + } else { + state = 2u; + invalid_count_ = 1u; + } + } + state_ = state; + if (status == 9u) { + state_ = 4u; + } + last_frame_valid_ = valid; + return pre_state; + } + + FrameMode select_mode(const uint32_t status) const noexcept { + if (status == EJOC_FRAME_STATUS_NO_METADATA) { + return FrameMode::Passthrough; + } + if (state_ == 4u) { + return FrameMode::HardCut; + } + if (state_ == 2u || state_ == 3u) { + return FrameMode::Hold; + } + if (state_ == 1u && status == 0u) { + return FrameMode::Normal; + } + return FrameMode::Passthrough; + } + + // JOC input order 5/7 channels (without LFE) -> 8-plane core order + // L,R,C,LFE,Ls,Rs,(Lrs,Rrs). Used by the passthrough path and its history. + void build_core8(const float* bed_planar, const float* lfe, + const uint32_t n_channels) noexcept { + std::memset(core8_, 0, sizeof(core8_)); + for (uint32_t channel = 0; channel < 3u && channel < n_channels; ++channel) { + for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) { + core8_[channel][sample] = + static_cast(bed_planar[channel * EJOC_FRAME_SAMPLES + sample]); + } + } + if (lfe != nullptr) { + for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) { + core8_[EJOC_LFE_SLOT][sample] = static_cast(lfe[sample]); + } + } + const uint32_t rest = n_channels > 3u ? n_channels - 3u : 0u; + for (uint32_t index = 0; index < rest; ++index) { + const uint32_t channel = 3u + index; + for (int sample = 0; sample < EJOC_FRAME_SAMPLES; ++sample) { + core8_[4u + index][sample] = + static_cast(bed_planar[channel * EJOC_FRAME_SAMPLES + sample]); + } + } + } + + // Passthrough output: slots 0..7 take the previous frame's 8 core planes + // delayed by 1217 samples plus this frame's leading 319 samples; slots 8..15 + // stay zero. + void write_passthrough(float* output16, const float output_scale) const noexcept { + for (int channel = 0; channel < EJOC_CORE_OUT_CHANNELS; ++channel) { + float* destination = output16 + channel * EJOC_FRAME_SAMPLES; + for (int sample = 0; sample < kLfeDelay; ++sample) { + destination[sample] = static_cast( + core_history_[channel][EJOC_FRAME_SAMPLES - kLfeDelay + sample]) * + output_scale; + } + for (int sample = kLfeDelay; sample < EJOC_FRAME_SAMPLES; ++sample) { + destination[sample] = + static_cast(core8_[channel][sample - kLfeDelay]) * output_scale; + } + } + } + + // Normal rendering: interpolate the per-object matrix from its previous + // frame-end value (or hard cut when there is none) and publish objects that + // disappeared as an all-zero matrix until they come back. + void matrix_object_frame(const int object) noexcept { + std::memset(z_[object], 0, sizeof(z_[object])); + const int n_channels = static_cast(core_channels_); + const bool present = (job_object_mask_ & (1u << object)) != 0u; + if (!present) { + if (prev_present_[object]) { + for (int channel = 0; channel < n_channels; ++channel) { + std::memset(prev_matrix_[object][channel], 0, + sizeof(prev_matrix_[object][channel])); + } + prev_present_[object] = true; + } + return; + } + const int bands = job_n_bands_[object]; + const int points = job_n_dpoints_[object]; + const int slope = job_slope_idx_[object]; + const uint8_t* pb_map = parameter_band_map(bands); + const bool has_previous = prev_present_[object]; + for (int channel = 0; channel < n_channels; ++channel) { + for (int subband = 0; subband < EJOC_SUBBANDS; ++subband) { + const int parameter_band = pb_map[subband]; + const double target0 = job_dq_[dq_index(object, 0, channel, parameter_band)]; + const double target1 = points == 2 + ? job_dq_[dq_index(object, 1, channel, parameter_band)] + : target0; + const double previous = + has_previous ? prev_matrix_[object][channel][subband] : 0.0; + double last = target0; + for (int timeslot = 0; timeslot < EJOC_TIMESLOTS; ++timeslot) { + double coefficient = 0.0; + if (slope == 0) { + if (!has_previous) { + if (points == 1 || timeslot < EJOC_TIMESLOTS / 2) { + // Hard cut: no fade-in from zero. With two data + // points the second half still interpolates. + coefficient = target0; + } else { + const double alpha = + static_cast(timeslot - (EJOC_TIMESLOTS / 2 - 1)) / + static_cast(EJOC_TIMESLOTS / 2); + coefficient = target0 * (1.0 - alpha) + target1 * alpha; + } + } else if (points == 1) { + const double alpha = + static_cast(timeslot + 1) / + static_cast(EJOC_TIMESLOTS); + coefficient = previous * (1.0 - alpha) + target0 * alpha; + } else if (timeslot < EJOC_TIMESLOTS / 2) { + const double alpha = static_cast(timeslot + 1) / + (EJOC_TIMESLOTS / 2); + coefficient = previous * (1.0 - alpha) + target0 * alpha; + } else { + const double alpha = + static_cast(timeslot - (EJOC_TIMESLOTS / 2 - 1)) / + static_cast(EJOC_TIMESLOTS / 2); + coefficient = target0 * (1.0 - alpha) + target1 * alpha; + } + } else { + // Step mode switches targets at the raw offset slots. + const double base = has_previous ? previous : target0; + coefficient = timeslot < job_offset_ts_[object * EJOC_MAX_DPOINTS] + ? base + : target0; + if (points == 2 && + timeslot >= job_offset_ts_[object * EJOC_MAX_DPOINTS + 1]) { + coefficient = target1; + } + } + z_[object][subband][timeslot].re += + x_[channel][subband][timeslot].re * coefficient; + z_[object][subband][timeslot].im += + x_[channel][subband][timeslot].im * coefficient; + last = coefficient; + } + prev_matrix_[object][channel][subband] = last; + } + } + prev_present_[object] = true; + } + + // Hold rendering: all 24 slots reuse the previous frame-end matrix and the + // previous state is not updated. + void matrix_object_hold(const int object) noexcept { + std::memset(z_[object], 0, sizeof(z_[object])); + const int n_channels = static_cast(core_channels_); + for (int channel = 0; channel < n_channels; ++channel) { + for (int subband = 0; subband < EJOC_SUBBANDS; ++subband) { + const double previous = prev_matrix_[object][channel][subband]; + for (int timeslot = 0; timeslot < EJOC_TIMESLOTS; ++timeslot) { + z_[object][subband][timeslot].re += + x_[channel][subband][timeslot].re * previous; + z_[object][subband][timeslot].im += + x_[channel][subband][timeslot].im * previous; + } + } + } + } + + void process_one_object_frame(const int object) noexcept { + if (frame_mode_ == FrameMode::Normal) { + matrix_object_frame(object); + } else if (frame_mode_ == FrameMode::Hold) { + matrix_object_hold(object); + } else { + // Hard cut and passthrough publish silence, but every object slot is + // still synthesised so the synthesis state stays aligned. + std::memset(z_[object], 0, sizeof(z_[object])); + } + synthesis_object(object, job_output_scale_, job_output16_); + } + + // One E-AC-3 audio frame: validates the description, advances the state + // machine and dispatches one of the four rendering modes. + int render_frame_impl(const ejoc_frame_input& input) noexcept { + error_[0] = '\0'; + if (input.struct_size != sizeof(ejoc_frame_input) || + input.struct_version != EJOC_FRAME_INPUT_VERSION) { + return fail("unsupported ejoc_frame_input struct version"); + } + if (input.bed_planar == nullptr || input.output16_planar == nullptr) { + return fail("null bed or output pointer passed to ejoc_renderer_render_frame"); + } + const uint32_t n_channels = input.core_channel_count; + if (n_channels != EJOC_CORE_CHANNELS && n_channels != EJOC_MAX_CORE_CHANNELS) { + return fail("core_channel_count must be 5 or 7"); + } + if (!std::isfinite(input.clipgain) || !std::isfinite(input.output_scale)) { + return fail("clipgain and output_scale must be finite"); + } + const bool has_metadata = input.has_metadata != 0u; + uint32_t status = input.frame_status; + if (!has_metadata) { + if (status != EJOC_FRAME_STATUS_NO_METADATA) { + return fail("a frame without JOC metadata must use frame_status 8"); + } + } else if (status != 0u && (status < 2u || status > 6u)) { + return fail("frame_status is not a JOC parse status"); + } + if ((input.object_mask & ~((1u << EJOC_MAX_OBJECTS) - 1u)) != 0u) { + return fail("object_mask contains an object index above 14"); + } + if (has_metadata && status == 0u) { + for (int object = 0; object < EJOC_MAX_OBJECTS; ++object) { + if ((input.object_mask & (1u << object)) == 0u) { + continue; + } + if (parameter_band_map(input.n_bands[object]) == nullptr) { + return fail("unsupported parameter-band count"); + } + if (input.n_dpoints[object] < 1u || input.n_dpoints[object] > 2u) { + return fail("n_dpoints must be 1 or 2"); + } + if (input.slope_idx[object] > 1u) { + return fail("slope_idx must be 0 or 1"); + } + if (input.slope_idx[object] == 1u) { + const uint8_t* offsets = + input.offset_ts + object * EJOC_MAX_DPOINTS; + for (uint32_t point = 0; point < input.n_dpoints[object]; ++point) { + if (offsets[point] >= EJOC_TIMESLOTS) { + return fail("offset_ts leaves the 0..23 range"); + } + if (point != 0u && offsets[point] <= offsets[point - 1u]) { + return fail("offset_ts is not strictly increasing"); + } + } + } + } + } + + if (n_channels_prev_ >= 0 && n_channels != static_cast(n_channels_prev_)) { + clear_unused_channels(n_channels); + } + // The parse status decides whether the frame's clipgain is usable; a + // declared-channel-count change (status 9) is only the renderer's own + // dispatch tag, so the new gain still applies to that frame. + const uint32_t parse_status = status; + // A declared core-channel-count change is the renderer's status 9: hard + // cut with the LFE ring cleared, while the clipgain ramp state is kept. + if (has_metadata && status == 0u && input.declared_core_channels != 0u && + n_channels_prev_ >= 0 && + input.declared_core_channels != static_cast(n_channels_prev_)) { + status = 9u; + } + advance_state(status, input.seq); + maybe_clear_lfe_ring(status); + const FrameMode mode = select_mode(status); + if (input.phase_shift_valid != 0u) { + phase_shift_enabled_ = input.phase_shift != 0u; + } + const bool valid_metadata = + has_metadata && parse_status == 0u && input.declared_core_channels != 0u; + double clipgain; + float phase_new; + if ((mode == FrameMode::Normal || mode == FrameMode::HardCut) && valid_metadata) { + clipgain = input.clipgain; + phase_new = static_cast(0.0625 * clipgain); + } else { + // Held frames reuse the current clipgain; they do not retune it. + clipgain = held_clipgain(); + phase_new = held_analysis_scalar(); + } + + core_channels_ = n_channels; + std::memset(input.output16_planar, 0, + sizeof(float) * EJOC_OUTPUT_CHANNELS * EJOC_FRAME_SAMPLES); + build_core8(input.bed_planar, input.lfe, n_channels); + analysis(input.bed_planar, phase_new); + + job_kind_ = JobKind::FrameObjects; + frame_mode_ = mode; + job_object_mask_ = mode == FrameMode::Normal ? input.object_mask : 0u; + job_n_bands_ = input.n_bands; + job_n_dpoints_ = input.n_dpoints; + job_slope_idx_ = input.slope_idx; + job_offset_ts_ = input.offset_ts; + job_dq_ = input.dq; + job_output_scale_ = input.output_scale; + job_output16_ = input.output16_planar; + job_count_ = EJOC_MAX_OBJECTS; + for (int object = 0; object < EJOC_MAX_OBJECTS; ++object) { + job_objects_[object] = object; + } + dispatch_jobs(); + + decode_lfe(input.lfe, clipgain, input.output16_planar, input.output_scale); + if (mode == FrameMode::HardCut) { + // All matrices are zero this frame; the presence markers stay set so + // the next valid frame fades in from zero. + std::memset(input.output16_planar, 0, + sizeof(float) * EJOC_OUTPUT_CHANNELS * EJOC_FRAME_SAMPLES); + for (int object = 0; object < EJOC_MAX_OBJECTS; ++object) { + prev_present_[object] = true; + for (int channel = 0; channel < EJOC_MAX_CORE_CHANNELS; ++channel) { + std::memset(prev_matrix_[object][channel], 0, + sizeof(prev_matrix_[object][channel])); + } + } + } else if (mode == FrameMode::Passthrough) { + write_passthrough(input.output16_planar, input.output_scale); + } + + // The 8-plane history is updated on every frame so a later passthrough + // frame is continuous with the last core PCM. + std::memcpy(core_history_, core8_, sizeof(core_history_)); + n_channels_prev_ = static_cast(n_channels); + return 0; + } + + enum class JobKind : uint8_t { Analysis, Objects, FrameObjects }; std::vector workers_; std::unique_ptr> work_barrier_; @@ -657,6 +1133,8 @@ private: const double* job_dq_ = nullptr; float job_output_scale_ = 1.0f; float* job_output16_ = nullptr; + uint32_t job_object_mask_ = 0; + FrameMode frame_mode_ = FrameMode::Normal; uint32_t core_channels_ = EJOC_CORE_CHANNELS; alignas(64) double analysis_fifo_[EJOC_MAX_CORE_CHANNELS][9][64]; @@ -668,11 +1146,29 @@ private: // sigma(k) switch; the caller sets it for downmix configurations 3 and 4. bool phase_shift_enabled_ = true; alignas(64) double lfe_delay_[kLfeDelay]; + // LFE gain ramp start; cleared by reset() and cleared/unset only there, so a + // held frame keeps the last applied gain. + double lfe_clipgain_ = 0.0; + bool has_lfe_clipgain_ = false; alignas(64) double matrix_previous_[15][EJOC_MAX_CORE_CHANNELS][64]; alignas(64) double synthesis_state_[15][640]; alignas(64) Complex x_[EJOC_MAX_CORE_CHANNELS][64][24]; alignas(64) Complex z_[15][64][24]; + // Frame-level state: the seq state machine and the per-object interpolation + // previous matrix (frame-end timeslot of the last published matrix). + uint32_t state_ = 0; + uint32_t expected_seq_ = 0; + uint32_t invalid_count_ = 0; + bool last_frame_valid_ = false; + int n_channels_prev_ = -1; + alignas(64) double prev_matrix_[EJOC_MAX_OBJECTS][EJOC_MAX_CORE_CHANNELS][64]; + bool prev_present_[EJOC_MAX_OBJECTS]; + // Passthrough history: the 8 core planes of the previous frame and of this + // frame, in the order L,R,C,LFE,Ls,Rs,(Lrs,Rrs). + alignas(64) double core_history_[EJOC_CORE_OUT_CHANNELS][EJOC_FRAME_SAMPLES]; + alignas(64) double core8_[EJOC_CORE_OUT_CHANNELS][EJOC_FRAME_SAMPLES]; + uint8_t bit_reverse_[64]; Complex fft_twiddle_[32]; double rotation_sin_[64]; @@ -690,13 +1186,13 @@ uint32_t EJOC_CALL ejoc_abi_version(void) { const char* EJOC_CALL ejoc_build_info(void) { #if defined(_MSC_VER) - return "eac3joc-core abi=1 compiler=MSVC fft=fixed64 speaker=double binaural=double crt=static-by-build"; + return "eac3joc-core abi=4 compiler=MSVC fft=fixed64 speaker=double binaural=double crt=static-by-build"; #elif defined(__clang__) - return "eac3joc-core abi=1 compiler=Clang fft=fixed64 speaker=double binaural=double"; + return "eac3joc-core abi=4 compiler=Clang fft=fixed64 speaker=double binaural=double"; #elif defined(__GNUC__) - return "eac3joc-core abi=1 compiler=GCC fft=fixed64 speaker=double binaural=double"; + return "eac3joc-core abi=4 compiler=GCC fft=fixed64 speaker=double binaural=double"; #else - return "eac3joc-core abi=1 compiler=unknown fft=fixed64 speaker=double binaural=double"; + return "eac3joc-core abi=4 compiler=unknown fft=fixed64 speaker=double binaural=double"; #endif } @@ -760,4 +1256,15 @@ int EJOC_CALL ejoc_renderer_process( slope_idx, offset_ts, dq, clipgain, phase_new, output_scale, output16_planar); } +int EJOC_CALL ejoc_renderer_render_frame(ejoc_renderer_handle handle, + const ejoc_frame_input* input) { + if (!handle) { + return -1; + } + if (!input) { + return -1; + } + return static_cast(handle)->render_frame(*input); +} + } // extern "C" diff --git a/src/joc_core/objects16.cpp b/src/joc_core/objects16.cpp index aa2925b..54a9aa0 100644 --- a/src/joc_core/objects16.cpp +++ b/src/joc_core/objects16.cpp @@ -6,16 +6,17 @@ namespace joc::joc { Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& params, - const float* bed_planar, const float* lfe, float gain, + const float* bed_planar, const float* lfe, + std::uint32_t core_channel_count, bool has_metadata, float gain, std::vector* out16, std::string* error) { if (handle == nullptr || bed_planar == nullptr || out16 == nullptr) { return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kDsp, "null argument"); } - if (params.n_channels != JOC_CORE_CHANNELS && - params.n_channels != JOC_MAX_CORE_CHANNELS) { + if (core_channel_count != JOC_CORE_CHANNELS && + core_channel_count != JOC_MAX_CORE_CHANNELS) { if (error != nullptr) { - *error = "the reused JOC kernel requires 5 or 7 core channels, frame declares " + - std::to_string(static_cast(params.n_channels)); + *error = "the reused JOC kernel requires 5 or 7 core channels, the frame has " + + std::to_string(core_channel_count); } return Status::fail(JOC_ERR_JOC_UNSUPPORTED_VARIANT, stage::kDsp, *error); } @@ -39,7 +40,7 @@ Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& pa offset_ts[obj * JOC_MAX_DPOINTS + dp] = object.offset_ts[dp]; } for (unsigned dp = 0; dp < object.n_dpoints; ++dp) { - for (unsigned ch = 0; ch < params.n_channels; ++ch) { + for (unsigned ch = 0; ch < core_channel_count; ++ch) { for (unsigned pb = 0; pb < object.n_bands; ++pb) { const std::size_t index = ((static_cast(obj) * JOC_MAX_DPOINTS + dp) * @@ -53,19 +54,42 @@ Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& pa } } - out16->assign(static_cast(JOC_OUTPUT_CHANNELS) * JOC_FRAME_SAMPLES, 0.0f); + ejoc_frame_input input{}; + input.struct_size = sizeof(input); + input.struct_version = EJOC_FRAME_INPUT_VERSION; + input.bed_planar = bed_planar; + input.lfe = lfe; + input.core_channel_count = core_channel_count; + // A frame without metadata has no declared channel count; the renderer's + // status-9 path is gated on has_metadata, so the value is not consulted. + input.declared_core_channels = + has_metadata ? static_cast(params.n_channels) : 0u; // sigma(k) = (k in {3,4}) gates the Ls/Rs -j rotation and the band-0 21-tap // DC compensation together; other configurations keep the common processing. - const std::uint32_t phase_shift = - (params.dmx_config_idx == 3 || params.dmx_config_idx == 4) ? 1u : 0u; - const int result = ejoc_renderer_process( - handle, bed_planar, lfe, params.n_channels, phase_shift, params.present_mask, n_bands, - n_dpoints, slope_idx, offset_ts, dq.data(), params.clipgain, 0.0625f, gain, - out16->data()); + // An unsupported downmix configuration (status 2) has no usable switch, so + // the renderer keeps the previous one. + input.phase_shift = + (params.dmx_config_idx == 3u || params.dmx_config_idx == 4u) ? 1u : 0u; + input.phase_shift_valid = params.status == JOC_FRAME_STATUS_UNSUPPORTED_DMX ? 0u : 1u; + input.frame_status = has_metadata ? params.status : EJOC_FRAME_STATUS_NO_METADATA; + input.seq = params.seq_count; + input.has_metadata = has_metadata ? 1u : 0u; + input.object_mask = params.present_mask; + input.n_bands = n_bands; + input.n_dpoints = n_dpoints; + input.slope_idx = slope_idx; + input.offset_ts = offset_ts; + input.dq = dq.data(); + input.clipgain = params.clipgain; + input.output_scale = gain; + out16->assign(static_cast(JOC_OUTPUT_CHANNELS) * JOC_FRAME_SAMPLES, 0.0f); + input.output16_planar = out16->data(); + + const int result = ejoc_renderer_render_frame(handle, &input); if (result != 0) { const char* detail = ejoc_renderer_last_error(handle); const std::string message = - "ejoc_renderer_process failed (" + std::to_string(result) + "): " + + "ejoc_renderer_render_frame failed (" + std::to_string(result) + "): " + (detail != nullptr ? detail : "unknown"); if (error != nullptr) { *error = message; diff --git a/src/joc_core/objects16.h b/src/joc_core/objects16.h index 6ef0e9a..9e4b6e5 100644 --- a/src/joc_core/objects16.h +++ b/src/joc_core/objects16.h @@ -1,6 +1,7 @@ #pragma once +#include #include #include @@ -10,10 +11,20 @@ namespace joc::joc { -// [16][1536] planar float32. bed_planar is [params.n_channels][1536], where -// n_channels comes from joc_dmx_config_idx (5 or 7). +// Rebuilds the 16-channel object frame ([16][1536] planar float32) for one +// audio frame through the renderer's frame-level state machine. bed_planar is +// [core_channel_count][1536] in JOC lane order (L,R,C,Ls,Rs,(extra pair)), where +// core_channel_count is the actual bed lane count (5 or 7) and lfe is the +// separate core LFE plane (NULL = zeros; the LFE ring still advances). +// +// has_metadata is false for an audio frame without a JOC EMDF container: such a +// frame always passes the 8 core planes through to output slots 0..7. For a +// frame with metadata, params.status selects normal rendering or the invalid +// frame path (hold / hard cut), and params.seq_count drives the state machine. +// gain is the user output gain (not joc_clipgain). Status rebuild_objects16(ejoc_renderer_handle handle, const joc_frame_params& params, - const float* bed_planar, const float* lfe, float gain, + const float* bed_planar, const float* lfe, + std::uint32_t core_channel_count, bool has_metadata, float gain, std::vector* out16, std::string* error); } // namespace joc::joc diff --git a/src/joc_core/qmf_tables.h b/src/joc_core/qmf_tables.h index e63f0e3..cf6d0ab 100644 --- a/src/joc_core/qmf_tables.h +++ b/src/joc_core/qmf_tables.h @@ -338,7 +338,7 @@ inline constexpr uint8_t kPbMap1[64] = { }; inline constexpr uint8_t kPbMap3[64] = { - 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, + 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, @@ -346,8 +346,8 @@ inline constexpr uint8_t kPbMap3[64] = { inline constexpr uint8_t kPbMap5[64] = { 0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, }; @@ -359,8 +359,8 @@ inline constexpr uint8_t kPbMap7[64] = { }; inline constexpr uint8_t kPbMap9[64] = { - 0, 1, 2, 3, 3, 3, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 0, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, }; @@ -373,9 +373,9 @@ inline constexpr uint8_t kPbMap12[64] = { }; inline constexpr uint8_t kPbMap15[64] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 10, 11, 11, 11, - 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 10, 10, 11, 11, + 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, }; diff --git a/src/oamd/isf_tables.h b/src/oamd/isf_tables.h new file mode 100644 index 0000000..8827d70 --- /dev/null +++ b/src/oamd/isf_tables.h @@ -0,0 +1,1078 @@ +// Static OAMD lookup data: the ISF base coefficients and layout descriptors, +// applied as lookup tables by the metadata path. The remaining control values +// (gain, size, priority, position, divergence, delta, refinement) are computed +// in code, with the size tables generated from the speaker-layout geometry at +// run time and bed routing derived from the layout tables. +// +// oamd_params.cpp adapts these arrays into the MetadataParams interface the +// metadata renderer consumes; this header is not parsed at run time and no data +// file is read. Layout keys are the compact speaker-layout names of +// speaker/speaker_layout_lookup.h. Values are exact float32 / float64 +// literals, formatted for reading. + +#pragma once + +#include + +namespace joc::oamd::isf_tables { + +inline constexpr int kConfigCount = 6; +inline constexpr int kLayoutCount = 12; +inline constexpr int kMatrixColumns = 73; +inline constexpr int kOutputIdSlots = 37; +inline constexpr int kPairedColumnCount = 73; + +// ---------------------------------------------------------- ISF configs -- +// counts = {mid, upper, lower, zenith, rows}. The matrix is row major with +// kMatrixColumns columns and rows * kMatrixColumns coefficients; the two row +// maps expand the rows to the layout output channels. + +// config 0: mid=3 upper=1 lower=0 zenith=0 rows=4 +inline constexpr int kConfig0Counts[5] = { + 3, 1, 0, 0, 4, +}; +inline constexpr int kConfig0PrimaryRowMap[4] = { + 0, 1, 2, 3, +}; +inline constexpr int kConfig0SecondaryRowMap[4] = { + 0, 2, 1, 3, +}; +inline constexpr float kConfig0Matrix[292] = { + 0.806554496f, 0.55179733f, 0.606975436f, 0.395382881f, 0.553403199f, 0.55392307f, 0.242557451f, 0.475052714f, + 0.444728285f, 0.172056332f, 0.424777329f, 0.510320425f, 0.188200772f, 0.179298654f, 0.452311248f, -0.280463845f, + 0.0173385199f, -0.289095283f, 0.0f, 0.0560874417f, -0.289286077f, -0.0133850314f, 0.10039378f, -0.0160563961f, + -0.282570958f, -0.0406489596f, 0.10803625f, 0.0563035086f, -0.0474251546f, -0.267198235f, -0.0635001212f, 0.0976552889f, + 0.126690805f, -0.0299270693f, -0.0487725958f, -0.253534675f, -0.0504176691f, 0.0926493704f, 0.0718689188f, 0.0518715233f, + -0.0276036877f, -0.044422321f, -0.261391997f, 0.0f, 0.0383858979f, -0.125604942f, 0.0511369891f, -0.0942640528f, + 0.0661441237f, -0.0514033213f, -0.144195572f, 0.0847499594f, -0.0366169624f, -0.119795986f, 0.0712311491f, -0.0392452478f, + -0.0396892689f, -0.137645096f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.420198411f, -0.0564252473f, 0.447791666f, 0.0137198186f, 0.447070777f, -0.056642618f, 0.0511780679f, + 0.438776195f, 0.0279835779f, 0.0559724644f, 0.428376973f, -0.0521839373f, 0.0286202561f, 0.0497708879f, 0.434612423f, + 0.967970967f, 0.189388156f, 0.887032092f, 0.0f, 0.533782899f, 0.692734241f, -0.0485691018f, 0.364943355f, + 0.400150359f, 0.637759507f, -0.117688976f, 0.211343333f, 0.53583926f, 0.258344829f, 0.554843485f, -0.16074039f, + 0.144556746f, 0.406649232f, 0.454000354f, 0.188827068f, 0.499716669f, -0.137298554f, 0.152938321f, 0.167128712f, + 0.493660122f, 0.197202787f, 0.194379941f, 0.53007412f, 0.0f, -0.0408309251f, 0.169170335f, -0.0558847897f, + 0.137064099f, -0.0745920539f, 0.0821318254f, 0.194209039f, -0.0994110778f, 0.0627586544f, 0.179878697f, -0.0811615586f, + 0.0642990768f, 0.0623021275f, 0.185386568f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, -0.433549941f, -0.0564252473f, -0.387239248f, -0.0897225067f, -0.369375974f, -0.056642618f, + -0.0869178101f, -0.338988274f, -0.111139596f, -0.074098967f, -0.316720605f, -0.0521839373f, -0.0597409494f, -0.0719894618f, + -0.329196125f, 0.0997840911f, -0.0810516179f, 0.145445764f, 0.0f, -0.15092352f, 0.210346073f, 0.0255242568f, + -0.145956933f, -0.0647137612f, 0.220865071f, 0.0656506792f, -0.112561882f, -0.151504934f, -0.00410195859f, 0.229870155f, + 0.0931462944f, -0.0882821977f, -0.171767786f, -0.0625010207f, 0.0138753559f, 0.230997473f, 0.0779158846f, -0.0885076076f, + -0.0819175467f, -0.139579073f, -0.0125190262f, 0.00712246494f, 0.230864957f, 0.0f, -0.0464779958f, 0.169170335f, + -0.0609837398f, 0.117578462f, -0.0774788111f, 0.0582966432f, 0.194209039f, -0.0970483571f, 0.0386542454f, 0.144669935f, + -0.0829367489f, 0.0433862694f, 0.0458272398f, 0.185386568f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.00611859141f, 0.0f, 0.00611859141f, 0.0f, 0.00611859141f, + 0.0f, 0.0f, 0.00611859141f, 0.0f, 0.0f, 0.00611859141f, 0.0f, 0.0f, + 0.0f, 0.00611859141f, 0.606383383f, 0.369040102f, 0.375158668f, 0.0f, 0.611859143f, 0.0f, + 0.0f, 0.0f, 0.611859143f, 0.0f, 0.0f, 0.0f, 0.611859143f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.611859143f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.611859143f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.612501979f, 0.375158668f, 0.375158668f, + 0.0f, 0.612501979f, 0.0f, 0.0f, 0.375158668f, 0.375158668f, 0.0f, 0.0f, + 0.0f, 0.612501979f, 0.0f, 0.0f, +}; + +// config 1: mid=5 upper=3 lower=0 zenith=0 rows=8 +inline constexpr int kConfig1Counts[5] = { + 5, 3, 0, 0, 8, +}; +inline constexpr int kConfig1PrimaryRowMap[8] = { + 0, 1, 2, 3, 4, 5, 6, 7, +}; +inline constexpr int kConfig1SecondaryRowMap[8] = { + 0, 4, 3, 2, 1, 5, 7, 6, +}; +inline constexpr float kConfig1Matrix[584] = { + 0.762071908f, 0.878515899f, 0.377033919f, 0.602302492f, 0.301846057f, 0.735358834f, 0.363118261f, 0.220952734f, + 0.588630795f, 0.233428374f, 0.173628852f, 0.666203499f, 0.295934975f, 0.237258628f, 0.190818608f, -0.0482377373f, + -0.146932736f, -0.0736631677f, 0.0f, -0.236003861f, 0.0352087989f, 0.0209411755f, -0.158969149f, -0.171132326f, + 0.0534950085f, 0.0365601592f, -0.0768198147f, -0.211696476f, -0.0881093368f, 0.0709446594f, 0.0414093435f, -0.0339665115f, + -0.159735352f, -0.169836f, -0.0431928299f, 0.0798262656f, 0.0405054167f, -0.0412958451f, -0.0919670537f, -0.199776128f, + -0.088245824f, -0.0477168672f, 0.0767729729f, 0.0f, -0.0307735596f, 0.0985779911f, -0.0392005667f, 0.0792217776f, + -0.0505279973f, 0.052526392f, 0.101350658f, -0.0588776022f, 0.0393758528f, 0.0858848915f, -0.0538843945f, 0.0424048752f, + 0.0462349318f, 0.0970347077f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.937940419f, -0.00769200735f, 0.899410963f, 0.126798227f, 0.871568501f, -0.0223797821f, 0.185356721f, + 0.828346968f, 0.114407614f, 0.183640212f, 0.79527539f, -0.0292743035f, 0.0918392688f, 0.171103626f, 0.808582366f, + 0.206779495f, 0.358817786f, 0.0825442672f, 0.0f, 0.642706811f, -0.112112649f, -0.0861416832f, 0.584561706f, + 0.295036942f, -0.138497636f, -0.188061967f, 0.454636872f, 0.533027947f, 0.0888761356f, -0.160920992f, -0.252963245f, + 0.348745584f, 0.564465702f, 0.292688578f, 0.0118646659f, -0.170682266f, -0.228604719f, 0.343061805f, 0.329741448f, + 0.480107397f, 0.0967416689f, 0.0195746161f, -0.16750212f, 0.0f, 0.0474122688f, -0.153277308f, 0.0604792088f, + -0.127292126f, 0.0780469254f, -0.0902892724f, -0.1581873f, 0.0909527242f, -0.0722234547f, -0.141861156f, 0.0832374766f, + -0.0748685598f, -0.0760156587f, -0.150556237f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, -0.304383546f, -0.0303982291f, -0.220497102f, -0.106823772f, -0.196243078f, -0.023437323f, + -0.128933266f, -0.167011917f, -0.101109743f, -0.11954622f, -0.148343876f, -0.0201002434f, -0.086018987f, -0.115593486f, + -0.155219316f, 1.21921885f, 0.102440052f, 1.24002981f, 0.0f, 0.485315174f, 0.966492474f, -0.0068521183f, + 0.163353473f, 0.499345928f, 0.889867902f, -0.00783566292f, -0.00159235171f, 0.398163289f, 0.422603458f, 0.795950294f, + -0.00490560709f, -0.0508767478f, 0.164154053f, 0.479463786f, 0.339748263f, 0.733749926f, -0.00640182663f, -0.0438284799f, + 0.0233886968f, 0.356164217f, 0.292895287f, 0.33110559f, 0.75674963f, 0.0f, -0.0857753381f, 0.281249166f, + -0.113504238f, 0.240856141f, -0.156313241f, 0.169199303f, 0.294449866f, -0.192601502f, 0.13385731f, 0.291493505f, + -0.170708984f, 0.134193152f, 0.12414363f, 0.27399984f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.170405373f, -0.0303982291f, 0.112205595f, 0.0618856214f, 0.0952003002f, + -0.023437323f, 0.0879907683f, 0.0747608244f, 0.0584826432f, 0.0813396871f, 0.0617192835f, -0.0201002434f, 0.0623168834f, + 0.0795734003f, 0.0665103942f, 0.075145781f, -0.0728718638f, -0.012045294f, 0.0f, -0.22572042f, 0.146051824f, + 0.00830142666f, -0.123446301f, -0.192037627f, 0.174334779f, 0.0139118731f, -0.037325222f, -0.199925452f, -0.128428787f, + 0.200995028f, 0.0153292557f, -0.000348456204f, -0.12391822f, -0.189742953f, -0.0855381936f, 0.213837251f, 0.0150561947f, + -0.00537870033f, -0.0499928407f, -0.187326372f, -0.119944714f, -0.0898441374f, 0.209528834f, 0.0f, -0.0886065736f, + 0.281249166f, -0.111125939f, 0.167644471f, -0.14063479f, 0.06759049f, 0.294449866f, -0.161815479f, 0.0321036093f, + 0.187027574f, -0.149270341f, 0.040523231f, 0.0545981489f, 0.27399984f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, -0.300034016f, -0.00769200735f, -0.202703446f, -0.143218562f, + -0.176246122f, -0.0223797821f, -0.153738067f, -0.14485833f, -0.138404354f, -0.128099248f, -0.124981523f, -0.0292743035f, + -0.118762553f, -0.127032593f, -0.132334724f, 0.0504921116f, 0.0590969697f, -0.0229987167f, 0.0f, 0.136037752f, + -0.109948844f, -0.00557306409f, 0.0754442587f, 0.109731086f, -0.124035843f, -0.00783402007f, 0.0148949223f, 0.12415529f, + 0.0588529259f, -0.13683331f, -0.00702152913f, -0.0127910776f, 0.0770407692f, 0.109433554f, 0.0278809145f, -0.142805412f, + -0.00764764473f, -0.00724919979f, 0.0341393389f, 0.118285336f, 0.0638631433f, 0.0321903788f, -0.140816942f, 0.0f, + 0.0431105457f, -0.153277308f, 0.054900866f, -0.114664845f, 0.0709655732f, -0.0683135763f, -0.1581873f, 0.0830023512f, + -0.0474123918f, -0.125473127f, 0.0758143663f, -0.0521559119f, -0.058864262f, -0.150556237f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.773103654f, 0.502101004f, 0.599211693f, + 0.361467451f, 0.551739991f, 0.504062116f, 0.224565148f, 0.481551975f, 0.406888843f, 0.163988039f, 0.435890615f, + 0.464395523f, 0.176444262f, 0.170078427f, 0.460943937f, -0.234267876f, 0.0425338559f, -0.265175283f, 0.0f, + 0.133138046f, -0.291941315f, -0.0185326077f, 0.144658074f, 0.0492976494f, -0.293917656f, -0.0519035049f, 0.126778528f, + 0.133658051f, -0.00121734152f, -0.292637914f, -0.0777671039f, 0.10918206f, 0.173826173f, 0.0455834344f, -0.0140971048f, + -0.289001673f, -0.0631545857f, 0.106131598f, 0.0901212394f, 0.123139985f, 0.00694660237f, -0.00906375423f, -0.291274369f, + 0.0f, 0.0704466701f, -0.185726225f, 0.0931427553f, -0.137706608f, 0.119271301f, -0.0701175705f, -0.213744387f, + 0.150606424f, -0.0505540818f, -0.173665211f, 0.12800543f, -0.0552965403f, -0.0573151633f, -0.204431102f, 0.56612289f, + 0.817858756f, -0.265175283f, 0.63635397f, 0.256778181f, -0.274516433f, 0.503343821f, 0.458294213f, -0.0613140129f, + -0.227749035f, 0.613715291f, 0.241714627f, 0.17510584f, -0.0508979261f, -0.26767385f, 0.321738452f, -0.051343441f, + 0.347683936f, 0.006025346f, 0.348360479f, -0.0515439734f, 0.0373121612f, 0.343742609f, 0.0166435167f, 0.0427011438f, + 0.336791873f, -0.0474877842f, 0.0205944292f, 0.0376427211f, 0.341008186f, 1.05165339f, 0.153493077f, 1.01606774f, + 0.0f, 0.452669322f, 0.854260981f, -0.0388549864f, 0.30219245f, 0.349872649f, 0.807741523f, -0.0937659815f, + 0.170891151f, 0.454437345f, 0.23488909f, 0.737081289f, -0.127760932f, 0.117567502f, 0.335013062f, 0.399134427f, + 0.179207087f, 0.68976742f, -0.109282233f, 0.124995716f, 0.138513863f, 0.418675959f, 0.180101186f, 0.18259339f, + 0.715864658f, 0.0f, -0.0751868263f, 0.250144362f, -0.103858836f, 0.212196305f, -0.140037924f, 0.12825419f, + 0.28788048f, -0.188863486f, 0.105921999f, 0.283425152f, -0.15301314f, 0.106071144f, 0.101434737f, 0.275336921f, + 1.30693853f, 0.474550724f, 1.01606774f, 0.134772465f, 0.873045802f, 0.590207696f, 0.0506976545f, 0.48848173f, + 0.74828583f, 0.41624406f, 0.116299741f, 0.212984651f, 0.595359862f, 0.359290928f, 0.556994319f, -0.373098612f, + -0.051343441f, -0.339502692f, -0.0765592009f, -0.325877279f, -0.0515439734f, -0.0735355169f, -0.302233577f, -0.094417952f, + -0.064061746f, -0.284557551f, -0.0474877842f, -0.0514369234f, -0.0621193834f, -0.294481814f, 0.0753973797f, -0.0816763043f, + 0.0781327635f, 0.0f, -0.186393261f, 0.159423888f, 0.0242178626f, -0.155916929f, -0.108236723f, 0.177117124f, + 0.0613375828f, -0.109327897f, -0.187121257f, -0.0453299657f, 0.199342862f, 0.0862600654f, -0.0841221288f, -0.179724813f, + -0.115603447f, -0.0224825423f, 0.211013034f, 0.0725142062f, -0.0855255499f, -0.0830333382f, -0.172395974f, -0.041446019f, + -0.0279278699f, 0.204894245f, 0.0f, -0.0759574473f, 0.250144362f, -0.0979449451f, 0.162011087f, -0.121711358f, + 0.0691804588f, 0.28788048f, -0.147857264f, 0.0430569202f, 0.192753717f, -0.129263669f, 0.0518759377f, 0.0585309565f, + 0.275336921f, -0.174692735f, -0.463384271f, 0.0781327635f, -0.2283701f, -0.359489441f, 0.227065101f, -0.140649319f, + -0.351305634f, -0.0915015116f, 0.224897102f, -0.210952863f, -0.168474212f, -0.245148182f, -0.0221679397f, 0.229741707f, +}; + +// config 2: mid=7 upper=3 lower=0 zenith=0 rows=10 +inline constexpr int kConfig2Counts[5] = { + 7, 3, 0, 0, 10, +}; +inline constexpr int kConfig2PrimaryRowMap[10] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +}; +inline constexpr int kConfig2SecondaryRowMap[10] = { + 0, 6, 5, 4, 3, 2, 1, 7, 9, 8, +}; +inline constexpr float kConfig2Matrix[730] = { + 0.590163708f, 1.15013433f, 0.0700185299f, 0.716697991f, 0.00425747363f, 0.843643665f, 0.394270122f, -0.0429384597f, + 0.66195029f, 0.217330843f, -0.0711021721f, 0.753234923f, 0.354373872f, 0.206693694f, -0.0657758638f, -4.7513091e-05f, + -0.167901263f, 0.0757671818f, 0.0f, -0.040750619f, -0.0127972439f, 0.0448508672f, -0.145666167f, 0.0805866346f, + -0.0316450037f, 0.0809035003f, -0.155651093f, -0.0239470415f, 0.0892155319f, -0.0462846756f, 0.103338771f, -0.137186021f, + -0.114719488f, 0.0653950199f, 0.071380958f, -0.055379387f, 0.0988774896f, -0.134143308f, -0.112234809f, -0.0189714208f, + 0.0755512714f, 0.0702525303f, -0.0536089465f, 0.0f, 0.0365414247f, -0.0974724218f, 0.0447400473f, -0.0782524347f, + 0.056151934f, -0.0497316569f, -0.095714651f, 0.0619511232f, -0.0328197964f, -0.0799686015f, 0.0591097288f, -0.0361355916f, + -0.0461363122f, -0.0914919898f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.3213439f, 0.0700185299f, 1.15013433f, 0.325333416f, 1.07009876f, 0.0213425104f, 0.392537892f, + 0.99264425f, 0.224603862f, 0.373883933f, 0.930602074f, 0.00789254531f, 0.205467775f, 0.353408158f, 0.943017602f, + -0.0675214827f, 0.171209574f, -0.195778787f, 0.0f, 0.0700185299f, -0.040750619f, -0.0800355971f, 0.325333416f, + -0.160504267f, -0.0111656478f, -0.157490045f, 0.392537892f, 0.0213425104f, -0.155825138f, 0.011691873f, -0.219532207f, + 0.373883933f, 0.224603862f, -0.135907158f, -0.115695231f, 0.026199447f, -0.207116723f, 0.353408158f, 0.205467775f, + 0.00789254531f, -0.134069085f, -0.112607829f, 0.0233875047f, 0.0f, -0.0387839153f, 0.0892530382f, -0.0471618883f, + 0.0756332502f, -0.0586109795f, 0.0512010567f, 0.0879816115f, -0.0642168075f, 0.0356264152f, 0.0763714239f, -0.0614660382f, + 0.0389328189f, 0.0485167168f, 0.0847408697f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, -0.0978827327f, -0.040750619f, 0.0700185299f, -0.145666167f, 0.114869401f, -0.0239470415f, + -0.155651093f, 0.15092203f, -0.114719488f, -0.137186021f, 0.173357293f, -0.0189714208f, -0.112234809f, -0.134143308f, + 0.168896019f, 0.970568776f, 0.520145178f, 0.590163708f, 0.0f, 1.15013433f, 0.0700185299f, -0.0657610595f, + 0.716697991f, 0.716697991f, 0.00425747363f, -0.112956993f, 0.394270122f, 0.843643665f, 0.394270122f, -0.0429384597f, + -0.141120702f, 0.217330843f, 0.66195029f, 0.66195029f, 0.217330843f, -0.0711021721f, -0.135794401f, 0.206693694f, + 0.354373872f, 0.753234923f, 0.354373872f, 0.206693694f, -0.0657758638f, 0.0f, 0.0814544782f, -0.155878082f, + 0.0993518308f, -0.134175256f, 0.124161452f, -0.0992822051f, -0.15271464f, 0.136370912f, -0.0755712241f, -0.138591811f, + 0.130345002f, -0.0792669877f, -0.0874261558f, -0.145184755f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0320089012f, -0.0127972439f, -0.040750619f, 0.0785987899f, -0.0623021014f, + -0.0102930274f, 0.0891766846f, -0.0792738423f, 0.0619094968f, 0.0767014772f, -0.0897534713f, -0.00955905393f, 0.0700433701f, + 0.0756867006f, -0.0876928344f, 1.12336349f, -0.155028164f, 1.3213439f, 0.0f, 0.0700185299f, 1.15013433f, + 0.0295849703f, -0.160504267f, 0.325333416f, 1.07009876f, 0.0524424911f, -0.155825138f, 0.0213425104f, 0.392537892f, + 0.99264425f, 0.0669500679f, -0.115695231f, -0.135907158f, 0.224603862f, 0.373883933f, 0.930602074f, 0.0641381219f, + -0.112607829f, -0.134069085f, 0.00789254531f, 0.205467775f, 0.353408158f, 0.943017602f, 0.0f, -0.159002692f, + 0.394036174f, -0.208434805f, 0.354459465f, -0.286087513f, 0.284319371f, 0.379695565f, -0.331883788f, 0.224936441f, + 0.395964384f, -0.309212714f, 0.224246368f, 0.204306394f, 0.346628249f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, -0.0685512051f, -0.0127972439f, -0.0127972439f, -0.0951516554f, + 0.00747283641f, -0.0102930274f, -0.0973943248f, 0.0234881286f, -0.0778310373f, -0.0799424276f, 0.0337362848f, -0.00955905393f, + -0.0781459734f, -0.0783035085f, 0.0317285992f, 0.0827682614f, 0.0885644257f, -0.0978827327f, 0.0f, -0.040750619f, + 0.0700185299f, -0.0188477617f, 0.0805866346f, -0.145666167f, 0.114869401f, -0.0334874317f, 0.0892155319f, -0.0239470415f, + -0.155651093f, 0.15092203f, -0.0425821431f, 0.071380958f, 0.0653950199f, -0.114719488f, -0.137186021f, 0.173357293f, + -0.0408117026f, 0.0702525303f, 0.0755512714f, -0.0189714208f, -0.112234809f, -0.134143308f, 0.168896019f, 0.0f, + -0.113689706f, 0.394036174f, -0.136367202f, 0.206241444f, -0.16558373f, 0.0688553825f, 0.379695565f, -0.179268241f, + 0.0192085002f, 0.216399133f, -0.172697663f, 0.0283477232f, 0.0625052154f, 0.346628249f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0757671818f, -0.040750619f, -0.0127972439f, + 0.0805866346f, -0.0316450037f, -0.0239470415f, 0.0892155319f, -0.0462846756f, 0.0653950199f, 0.071380958f, -0.055379387f, + -0.0189714208f, 0.0755512714f, 0.0702525303f, -0.0536089465f, -0.050330855f, -0.0557539612f, 0.0320089012f, 0.0f, + -0.0127972439f, -0.040750619f, 0.0202700812f, -0.0951516554f, 0.0785987899f, -0.0623021014f, 0.0362853743f, -0.0973943248f, + -0.0102930274f, 0.0891766846f, -0.0792738423f, 0.0465335287f, -0.0799424276f, -0.0778310373f, 0.0619094968f, 0.0767014772f, + -0.0897534713f, 0.0445258431f, -0.0783035085f, -0.0781459734f, -0.00955905393f, 0.0700433701f, 0.0756867006f, -0.0876928344f, + 0.0f, 0.0528094359f, -0.155878082f, 0.064438872f, -0.112467363f, 0.0803585127f, -0.059129715f, -0.15271464f, + 0.0883368775f, -0.0322308689f, -0.115333222f, 0.0844521448f, -0.0374365039f, -0.0550054647f, -0.145184755f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.195778787f, 0.0700185299f, + -0.040750619f, -0.160504267f, -0.0111656478f, 0.0213425104f, -0.155825138f, 0.011691873f, -0.135907158f, -0.115695231f, + 0.026199447f, 0.00789254531f, -0.134069085f, -0.112607829f, 0.0233875047f, -0.022377478f, 0.072759524f, -0.0685512051f, + 0.0f, -0.0127972439f, -0.0127972439f, -0.0215514805f, 0.0785987899f, -0.0951516554f, 0.00747283641f, -0.0385232233f, + 0.0891766846f, -0.0102930274f, -0.0973943248f, 0.0234881286f, -0.0490028523f, 0.0767014772f, 0.0619094968f, -0.0778310373f, + -0.0799424276f, 0.0337362848f, -0.0469422154f, 0.0756867006f, 0.0700433701f, -0.00955905393f, -0.0781459734f, -0.0783035085f, + 0.0317285992f, 0.0f, -0.0337162726f, 0.0892530382f, -0.0410008803f, 0.0701590255f, -0.0508800298f, 0.0404851101f, + 0.0879816115f, -0.0557178557f, 0.0238055475f, 0.0707092956f, -0.0533588789f, 0.0274445098f, 0.039371945f, 0.0847408697f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.773103654f, + 0.502101004f, 0.599211693f, 0.361467451f, 0.551739991f, 0.504062116f, 0.224565148f, 0.481551975f, 0.406888843f, + 0.163988039f, 0.435890615f, 0.464395523f, 0.176444262f, 0.170078427f, 0.460943937f, -0.234267876f, 0.0425338559f, + -0.265175283f, 0.0f, 0.133138046f, -0.291941315f, -0.0185326077f, 0.144658074f, 0.0492976494f, -0.293917656f, + -0.0519035049f, 0.126778528f, 0.133658051f, -0.00121734152f, -0.292637914f, -0.0777671039f, 0.10918206f, 0.173826173f, + 0.0455834344f, -0.0140971048f, -0.289001673f, -0.0631545857f, 0.106131598f, 0.0901212394f, 0.123139985f, 0.00694660237f, + -0.00906375423f, -0.291274369f, 0.0f, 0.0704466701f, -0.185726225f, 0.0931427553f, -0.137706608f, 0.119271301f, + -0.0701175705f, -0.213744387f, 0.150606424f, -0.0505540818f, -0.173665211f, 0.12800543f, -0.0552965403f, -0.0573151633f, + -0.204431102f, 0.56612289f, 0.817858756f, -0.265175283f, 0.63635397f, 0.256778181f, -0.274516433f, 0.503343821f, + 0.458294213f, -0.0613140129f, -0.227749035f, 0.613715291f, 0.241714627f, 0.17510584f, -0.0508979261f, -0.26767385f, + 0.321738452f, -0.051343441f, 0.347683936f, 0.006025346f, 0.348360479f, -0.0515439734f, 0.0373121612f, 0.343742609f, + 0.0166435167f, 0.0427011438f, 0.336791873f, -0.0474877842f, 0.0205944292f, 0.0376427211f, 0.341008186f, 1.05165339f, + 0.153493077f, 1.01606774f, 0.0f, 0.452669322f, 0.854260981f, -0.0388549864f, 0.30219245f, 0.349872649f, + 0.807741523f, -0.0937659815f, 0.170891151f, 0.454437345f, 0.23488909f, 0.737081289f, -0.127760932f, 0.117567502f, + 0.335013062f, 0.399134427f, 0.179207087f, 0.68976742f, -0.109282233f, 0.124995716f, 0.138513863f, 0.418675959f, + 0.180101186f, 0.18259339f, 0.715864658f, 0.0f, -0.0751868263f, 0.250144362f, -0.103858836f, 0.212196305f, + -0.140037924f, 0.12825419f, 0.28788048f, -0.188863486f, 0.105921999f, 0.283425152f, -0.15301314f, 0.106071144f, + 0.101434737f, 0.275336921f, 1.30693853f, 0.474550724f, 1.01606774f, 0.134772465f, 0.873045802f, 0.590207696f, + 0.0506976545f, 0.48848173f, 0.74828583f, 0.41624406f, 0.116299741f, 0.212984651f, 0.595359862f, 0.359290928f, + 0.556994319f, -0.373098612f, -0.051343441f, -0.339502692f, -0.0765592009f, -0.325877279f, -0.0515439734f, -0.0735355169f, + -0.302233577f, -0.094417952f, -0.064061746f, -0.284557551f, -0.0474877842f, -0.0514369234f, -0.0621193834f, -0.294481814f, + 0.0753973797f, -0.0816763043f, 0.0781327635f, 0.0f, -0.186393261f, 0.159423888f, 0.0242178626f, -0.155916929f, + -0.108236723f, 0.177117124f, 0.0613375828f, -0.109327897f, -0.187121257f, -0.0453299657f, 0.199342862f, 0.0862600654f, + -0.0841221288f, -0.179724813f, -0.115603447f, -0.0224825423f, 0.211013034f, 0.0725142062f, -0.0855255499f, -0.0830333382f, + -0.172395974f, -0.041446019f, -0.0279278699f, 0.204894245f, 0.0f, -0.0759574473f, 0.250144362f, -0.0979449451f, + 0.162011087f, -0.121711358f, 0.0691804588f, 0.28788048f, -0.147857264f, 0.0430569202f, 0.192753717f, -0.129263669f, + 0.0518759377f, 0.0585309565f, 0.275336921f, -0.174692735f, -0.463384271f, 0.0781327635f, -0.2283701f, -0.359489441f, + 0.227065101f, -0.140649319f, -0.351305634f, -0.0915015116f, 0.224897102f, -0.210952863f, -0.168474212f, -0.245148182f, + -0.0221679397f, 0.229741707f, +}; + +// config 3: mid=9 upper=5 lower=0 zenith=0 rows=14 +inline constexpr int kConfig3Counts[5] = { + 9, 5, 0, 0, 14, +}; +inline constexpr int kConfig3PrimaryRowMap[14] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, +}; +inline constexpr int kConfig3SecondaryRowMap[14] = { + 0, 8, 7, 6, 5, 4, 3, 2, 1, 9, 13, 12, + 11, 10, +}; +inline constexpr float kConfig3Matrix[1022] = { + 0.5962255f, 1.29044223f, -0.101921558f, 0.751091719f, -0.143631235f, 0.900211036f, 0.369144142f, -0.152780354f, + 0.688704669f, 0.167323455f, -0.155934289f, 0.794200063f, 0.374481708f, 0.138836622f, -0.155855611f, 0.108822614f, + 0.143761158f, 0.0113373511f, 0.0f, 0.0833561867f, -0.049193725f, 0.0587441772f, 0.0203793868f, -0.00768033322f, + -0.0213853531f, 0.0881572366f, -0.0427768491f, 0.0706740916f, -0.0537361912f, -0.00864335801f, 0.111609437f, -0.0719173327f, + 0.0483185016f, 0.0184360128f, -0.0635197014f, 0.000861906388f, 0.110495962f, -0.0716306344f, 0.0134670446f, 0.0667132214f, + -0.0186731499f, -0.0622218139f, 0.000424343772f, 0.0f, -0.0241671242f, 0.0641711354f, -0.0277090184f, 0.0482170768f, + -0.0306202173f, 0.0262052231f, 0.0602546781f, -0.0316153914f, 0.0136604067f, 0.0482162312f, -0.0312806666f, 0.0161819402f, + 0.0305759404f, 0.0578158498f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.2725848f, 0.169859856f, 1.15321529f, 0.564439118f, 0.954299152f, 0.0865950584f, 0.607200623f, + 0.854735196f, 0.341703385f, 0.547834218f, 0.779156744f, 0.0656172782f, 0.339260012f, 0.514484763f, 0.782652378f, + -0.160787135f, -0.191378683f, -0.0671630129f, 0.0f, -0.181637585f, 0.0297505353f, -0.044602368f, -0.0305175688f, + -0.0390409417f, 0.002432751f, -0.0831193626f, 0.109130651f, -0.144699514f, 0.0368482396f, -0.0100244014f, -0.11629945f, + 0.177588359f, -0.0934856981f, -0.0646873787f, 0.0566249453f, -0.0193500537f, -0.114702441f, 0.173106149f, -0.0290840473f, + -0.133515581f, 0.000784702133f, 0.0566008873f, -0.0189207885f, 0.0f, 0.0286674574f, -0.076031059f, 0.0330908932f, + -0.0590275824f, 0.0370821878f, -0.035730768f, -0.0703385174f, 0.0387417674f, -0.0221207868f, -0.0590978712f, 0.0381264016f, + -0.0245837923f, -0.0382085331f, -0.0668660924f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.521037579f, -0.0510371886f, 0.580719292f, -0.150138319f, 0.645650446f, -0.0298032481f, + -0.117010772f, 0.663906157f, -0.112968571f, -0.0703355521f, 0.675015807f, -0.0239982698f, -0.108471796f, -0.0648832247f, + 0.674538553f, 0.476639867f, 0.554697216f, 0.0843934938f, 0.0f, 0.797805965f, -0.11149127f, -0.19089362f, + 0.932056606f, 0.11490079f, -0.0654472932f, -0.27350083f, 0.733284056f, 0.53939873f, -0.0708512366f, -0.0439920351f, + -0.332386732f, 0.542778909f, 0.708017707f, 0.172961697f, -0.107473545f, -0.0280977059f, -0.329729468f, 0.497534603f, + 0.536235213f, 0.47004804f, -0.00375610101f, -0.10627041f, -0.028825108f, 0.0f, -0.0286145266f, 0.0673843846f, + -0.032006111f, 0.052617956f, -0.033786796f, 0.0306260604f, 0.063445285f, -0.0337688476f, 0.018209951f, 0.0525771081f, + -0.0338453166f, 0.0213118345f, 0.0372459218f, 0.0609918796f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, -0.0944515839f, -0.0131119061f, -0.159688964f, 0.0719657764f, -0.166244119f, + -0.0114918659f, 0.06054499f, -0.164502576f, 0.0523887575f, 0.0356325954f, -0.161466002f, -0.0110740354f, 0.0583236329f, + 0.0324800275f, -0.161638409f, 1.13869512f, 0.346134752f, 1.02552938f, 0.0f, 0.995281756f, 0.364953518f, + 0.0393236503f, 0.266180724f, 0.965530276f, 0.203266442f, 0.0603831932f, 0.00635949243f, 0.683666289f, 0.690409064f, + 0.137203127f, 0.0769751593f, -0.0748991817f, 0.310154408f, 0.770378232f, 0.4741579f, 0.0915349573f, 0.0761973485f, + -0.0798328444f, 0.0780500248f, 0.599535465f, 0.542622209f, 0.4297221f, 0.093568936f, 0.0f, 0.110057637f, + -0.13099198f, 0.129386202f, -0.0998656228f, 0.146712959f, -0.0619897172f, -0.121056952f, 0.154472992f, -0.042517662f, + -0.099858582f, 0.151402771f, -0.0473538525f, -0.070232518f, -0.114768825f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.00171658548f, -0.00848072302f, 0.0646080896f, -0.0844760984f, + 0.0753143355f, -0.00451179687f, -0.0715204626f, 0.0772944093f, -0.0625838935f, -0.0470612347f, 0.077599071f, -0.00346811884f, + -0.0635091513f, -0.0430319495f, 0.0776075199f, 1.04575908f, -0.151017264f, 1.08369434f, 0.0f, -0.165156215f, + 1.25519621f, -0.0231963769f, -0.0901965946f, 0.0851045698f, 1.16376662f, -0.0353484638f, 0.0002055933f, -0.14024277f, + 0.233505189f, 1.10424221f, -0.045045346f, 0.0381168015f, -0.105575331f, -0.023777023f, 0.286342949f, 1.05556834f, + -0.0445874967f, 0.0408558771f, -0.0362393595f, -0.132026911f, 0.0453441255f, 0.275376111f, 1.05787611f, 0.0f, + -0.266678393f, 0.497592449f, -0.334168702f, 0.457070857f, -0.424113542f, 0.369209319f, 0.43001467f, -0.479326457f, + 0.294789672f, 0.460314155f, -0.4569709f, 0.290369868f, 0.267114908f, 0.388906091f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.0242597573f, -0.00848072302f, -0.0826566368f, + 0.0697856247f, -0.0911891237f, -0.00451179687f, 0.0576908998f, -0.0922586173f, 0.0531342812f, 0.0346515924f, -0.092061922f, + -0.00346811884f, 0.0557018183f, 0.0311647449f, -0.0920900255f, 0.176903412f, 0.0981914401f, 0.132674068f, 0.0f, + 0.0804900825f, 0.01092319f, 0.0244016852f, 0.0287676714f, -0.0276459828f, 0.0862624571f, 0.036888808f, -0.0263876487f, + 0.0708200112f, -0.0855363309f, 0.120501988f, 0.0466776155f, -0.0489929542f, 0.0472313762f, 0.0147917252f, -0.101314582f, + 0.146809474f, 0.0462177992f, -0.0496215858f, 0.00874480046f, 0.0676532611f, -0.0221460536f, -0.0989337415f, 0.145576403f, + 0.0f, -0.110277422f, 0.497592449f, -0.125671327f, 0.239584535f, -0.139402449f, 0.0782381892f, 0.43001467f, + -0.144911557f, 0.0157100502f, 0.240201011f, -0.142901912f, 0.0226368401f, 0.0745499656f, 0.388906091f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.0188171472f, -0.0131119061f, + 0.0635067895f, -0.0870094821f, 0.073191084f, -0.0114918659f, -0.067136094f, 0.0748212188f, -0.0683006868f, -0.0388967469f, + 0.0750071108f, -0.0110740354f, -0.0644336566f, -0.0344492681f, 0.075018771f, -0.111183263f, -0.105940618f, -0.0773901939f, + 0.0f, -0.104050383f, 0.00803318527f, -0.020773178f, -0.043402493f, -0.0158665087f, -0.0236677732f, -0.0315692313f, + 0.0119093712f, -0.0841804892f, 0.0354086608f, -0.0378925353f, -0.0401192047f, 0.0366419069f, -0.0578193404f, -0.0392942764f, + 0.0523482449f, -0.0485864393f, -0.0397153236f, 0.0379876383f, -0.0189114492f, -0.0784108341f, -0.00156278361f, 0.0522862673f, + -0.048090484f, 0.0f, 0.0441635586f, -0.13099198f, 0.0509284176f, -0.090839982f, 0.0571723692f, -0.0413055606f, + -0.121056952f, 0.0597706251f, -0.0153014967f, -0.0909656882f, 0.0588208511f, -0.0189673919f, -0.0432679355f, -0.114768825f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.00548725249f, + -0.0510371886f, -0.0872709975f, 0.0715174079f, -0.0943147689f, -0.0298032481f, 0.0582316928f, -0.0943913683f, 0.0538211875f, + 0.0299165044f, -0.0933183357f, -0.0239982698f, 0.0593632385f, 0.0256493129f, -0.093389824f, 0.0459910706f, 0.0949200094f, + 0.0140228961f, 0.0f, 0.0811021701f, -0.0549040288f, 0.0229882058f, 0.0322649814f, -0.00129409728f, -0.0272669289f, + 0.0350106061f, -0.0179535709f, 0.0683825314f, -0.0461783558f, -0.014372116f, 0.044487413f, -0.0410590954f, 0.0488459021f, + 0.0238176696f, -0.0590588786f, -0.004708983f, 0.0440405682f, -0.0418855585f, 0.016438419f, 0.0645243824f, -0.00927114021f, + -0.0582435392f, -0.00515556894f, 0.0f, -0.0236927327f, 0.0673843846f, -0.0267920755f, 0.046923954f, -0.0289552212f, + 0.0192538314f, 0.063445285f, -0.0293781385f, 0.00420041243f, 0.0468944907f, -0.029290475f, 0.00705782836f, 0.0246233921f, + 0.0609918796f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + -0.0848988295f, 0.169859856f, 0.0543909408f, -0.142247871f, 0.0717676133f, 0.0865950584f, -0.118372157f, 0.0756901056f, + -0.104428969f, -0.0685101971f, 0.0769715309f, 0.0656172782f, -0.110332556f, -0.0598677732f, 0.0769442096f, -0.0879169479f, + -0.11062035f, -0.0434484147f, 0.0f, -0.102289714f, 0.0316346548f, -0.0260510612f, -0.0506048016f, -0.00907983631f, + 0.00688241888f, -0.039290037f, 0.00500181178f, -0.0820315778f, 0.0389038436f, -0.00450874493f, -0.0498314276f, 0.0322972387f, + -0.0642173737f, -0.0311565138f, 0.0524473153f, -0.0130618289f, -0.0493316427f, 0.0338696502f, -0.0283173751f, -0.0761672705f, + 0.00704149064f, 0.0520674251f, -0.0126663046f, 0.0f, 0.0264374148f, -0.076031059f, 0.0304064434f, -0.0535396785f, + 0.0338749997f, -0.0265273359f, -0.0703385174f, 0.0352090187f, -0.0116873784f, -0.0535743013f, 0.0347309597f, -0.0142977778f, + -0.0300456565f, -0.0668660924f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.785944343f, 0.805999875f, 0.454482079f, 0.560058355f, 0.384766042f, 0.674772203f, 0.350957274f, + 0.308072299f, 0.547285378f, 0.235707879f, 0.26165086f, 0.611315131f, 0.279808253f, 0.237059981f, 0.278263688f, + -0.0853048638f, -0.0899630636f, -0.148209289f, 0.0f, -0.181213826f, -0.0357419662f, 0.00726660667f, -0.0848960355f, + -0.185241461f, -0.012772399f, 0.0083396323f, -0.0111122318f, -0.167882279f, -0.135844126f, 0.0117737642f, 0.00417754194f, + 0.0174665432f, -0.08875864f, -0.182160407f, -0.0971545577f, 0.0260233823f, 0.00589526538f, 0.00958736148f, -0.0437768809f, + -0.1606424f, -0.115741767f, -0.0985649601f, 0.0209319368f, 0.0f, -0.0556032471f, 0.121464081f, -0.0686128139f, + 0.0945059806f, -0.0840097219f, 0.0472384878f, 0.124010995f, -0.0934383348f, 0.0240579769f, 0.0970674232f, -0.0880473182f, + 0.0325129256f, 0.0469961166f, 0.120115027f, 0.656395733f, 0.693550348f, -0.148209289f, 0.787868857f, -0.210398719f, + 0.0244817678f, 0.709801912f, 0.108553007f, -0.230014607f, 0.0790278986f, 0.728269339f, 0.149673283f, -0.185549408f, + -0.132376865f, 0.0727257952f, 0.749153137f, -0.0151918838f, 0.743870795f, 0.0904163793f, 0.725858152f, -0.0284509752f, + 0.138672873f, 0.696808815f, 0.0805369541f, 0.139208183f, 0.673090696f, -0.0341076739f, 0.0644519404f, 0.128681615f, + 0.681902766f, 0.369808257f, 0.32577917f, 0.249595046f, 0.0f, 0.727370977f, 0.00875582919f, -0.07678812f, + 0.581906736f, 0.427010357f, -0.0330419131f, -0.165314898f, 0.416799754f, 0.607160032f, 0.218108818f, -0.0751467273f, + -0.221418396f, 0.307580382f, 0.565320551f, 0.419812441f, 0.121110566f, -0.0980340242f, -0.201185688f, 0.304171383f, + 0.313428491f, 0.549114227f, 0.188008174f, 0.125972882f, -0.0900269225f, 0.0f, 0.0995378867f, -0.197062358f, + 0.124362446f, -0.163828611f, 0.154535398f, -0.101871327f, -0.202284217f, 0.173487544f, -0.0706288293f, -0.175791696f, + 0.162611648f, -0.0798696205f, -0.092163533f, -0.194259822f, 1.12114859f, 1.09630704f, 0.249595046f, 0.530103147f, + 1.0134877f, -0.0956245214f, 0.268539041f, 0.92575568f, 0.487135619f, -0.169882268f, 0.306160808f, 0.560391605f, + 0.767297506f, 0.184175417f, -0.162984431f, -0.284626216f, -0.0268656444f, -0.238345027f, -0.0897421911f, -0.222510248f, + -0.0205096174f, -0.109521627f, -0.202667087f, -0.0849388689f, -0.103656881f, -0.189411402f, -0.0174187813f, -0.0716776401f, + -0.0995435715f, -0.194260612f, 1.23108459f, 0.0323383994f, 1.33512092f, 0.0f, 0.241278589f, 1.16727686f, + 0.00381910987f, 0.020380795f, 0.302016884f, 1.11377382f, 0.012579374f, -0.0702786967f, 0.190156981f, 0.289754242f, + 1.04574847f, 0.0204732157f, -0.087442033f, 0.0228108317f, 0.287110627f, 0.249692336f, 0.999488056f, 0.017454559f, + -0.0825986117f, -0.0408127941f, 0.165998057f, 0.184359923f, 0.239153519f, 1.01719999f, 0.0f, -0.179138601f, + 0.418652326f, -0.239200652f, 0.389352232f, -0.329209119f, 0.2853944f, 0.438236862f, -0.403558195f, 0.225739822f, + 0.474742621f, -0.358207464f, 0.227803364f, 0.21126765f, 0.408426881f, 0.987125218f, -0.295005888f, 1.33512092f, + -0.243456423f, 0.364650667f, 1.00489175f, -0.164058641f, -0.0941023901f, 0.72022295f, 0.740241826f, -0.177447543f, + -0.154486373f, 0.258320421f, 0.541402519f, 0.784219503f, 0.163471252f, -0.0268656444f, 0.132145807f, 0.0517311804f, + 0.12079452f, -0.0205096174f, 0.0762855262f, 0.106442675f, 0.0488971286f, 0.0733744651f, 0.0968198106f, -0.0174187813f, + 0.0529743694f, 0.071141161f, 0.10035608f, 0.0598621108f, -0.0339826196f, -0.108781949f, 0.0f, -0.153750852f, + 0.0410845242f, 0.00130789774f, -0.0513869077f, -0.170660675f, 0.0742369518f, 7.21841861e-05f, 0.01137775f, -0.13447623f, + -0.146834746f, 0.110382281f, -0.0022178425f, 0.0319381319f, -0.0530611388f, -0.166233703f, -0.119735397f, 0.131896257f, + -0.00128461607f, 0.0279658791f, -0.00634207344f, -0.124800295f, -0.115073167f, -0.118792132f, 0.124231398f, 0.0f, + -0.124856472f, 0.418652326f, -0.150763556f, 0.212809891f, -0.179730907f, 0.0498502702f, 0.438236862f, -0.196334913f, + -0.00137086341f, 0.225102708f, -0.186951518f, 0.0146026313f, 0.0478232615f, 0.408426881f, 0.184908807f, 0.159349129f, + -0.108781949f, 0.142724514f, -0.198574141f, 0.129562557f, 0.0964779407f, 0.0234734211f, -0.243038014f, 0.212791249f, + 0.104537755f, 0.0728127137f, -0.160118237f, -0.166709453f, 0.203562438f, -0.285919398f, -0.0151918838f, -0.22266452f, + -0.128905758f, -0.202599213f, -0.0284509752f, -0.140100867f, -0.17790024f, -0.124938786f, -0.120852917f, -0.161565244f, + -0.0341076739f, -0.107172683f, -0.118954949f, -0.167531237f, 0.0633655339f, 0.0243620891f, 0.035627991f, 0.0f, + 0.0881999061f, -0.0533521138f, 0.0012146068f, 0.0175533239f, 0.110432841f, -0.0718007907f, 0.00559099531f, -0.0304933768f, + 0.0818924755f, 0.0911090001f, -0.0914510116f, 0.0100810379f, -0.0457622968f, 0.0205301996f, 0.108312838f, 0.0698677823f, + -0.102728777f, 0.0083619887f, -0.0407417715f, -0.00411251979f, 0.0785926357f, 0.0768310651f, 0.0706149191f, -0.0987645835f, + 0.0f, 0.0702720582f, -0.197062358f, 0.0870602801f, -0.140467182f, 0.107510909f, -0.0640047714f, -0.202284217f, + 0.120740525f, -0.0308822244f, -0.147731632f, 0.113058038f, -0.0414264947f, -0.0603006929f, -0.194259822f, -0.176864818f, + -0.290847898f, 0.035627991f, -0.255454212f, 0.102170832f, -0.101525724f, -0.190900356f, -0.0924148858f, 0.136958897f, + -0.142318815f, -0.202040881f, -0.117652103f, 0.0902495757f, 0.0842475221f, -0.138043836f, +}; + +// config 4: mid=7 upper=5 lower=3 zenith=0 rows=15 +inline constexpr int kConfig4Counts[5] = { + 7, 5, 3, 0, 15, +}; +inline constexpr int kConfig4PrimaryRowMap[15] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, +}; +inline constexpr int kConfig4SecondaryRowMap[15] = { + 0, 6, 5, 4, 3, 2, 1, 7, 11, 10, 9, 8, + 12, 14, 13, +}; +inline constexpr float kConfig4Matrix[1095] = { + 0.590163708f, 1.15013433f, 0.0700185299f, 0.716697991f, 0.00425747363f, 0.843643665f, 0.394270122f, -0.0429384597f, + 0.66195029f, 0.217330843f, -0.0711021721f, 0.753234923f, 0.354373872f, 0.206693694f, -0.0657758638f, -4.7513091e-05f, + -0.167901263f, 0.0757671818f, 0.0f, -0.040750619f, -0.0127972439f, 0.0448508672f, -0.145666167f, 0.0805866346f, + -0.0316450037f, 0.0809035003f, -0.155651093f, -0.0239470415f, 0.0892155319f, -0.0462846756f, 0.103338771f, -0.137186021f, + -0.114719488f, 0.0653950199f, 0.071380958f, -0.055379387f, 0.0988774896f, -0.134143308f, -0.112234809f, -0.0189714208f, + 0.0755512714f, 0.0702525303f, -0.0536089465f, 0.0f, 0.0365414247f, -0.0974724218f, 0.0447400473f, -0.0782524347f, + 0.056151934f, -0.0497316569f, -0.095714651f, 0.0619511232f, -0.0328197964f, -0.0799686015f, 0.0591097288f, -0.0361355916f, + -0.0461363122f, -0.0914919898f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.3213439f, 0.0700185299f, 1.15013433f, 0.325333416f, 1.07009876f, 0.0213425104f, 0.392537892f, + 0.99264425f, 0.224603862f, 0.373883933f, 0.930602074f, 0.00789254531f, 0.205467775f, 0.353408158f, 0.943017602f, + -0.0675214827f, 0.171209574f, -0.195778787f, 0.0f, 0.0700185299f, -0.040750619f, -0.0800355971f, 0.325333416f, + -0.160504267f, -0.0111656478f, -0.157490045f, 0.392537892f, 0.0213425104f, -0.155825138f, 0.011691873f, -0.219532207f, + 0.373883933f, 0.224603862f, -0.135907158f, -0.115695231f, 0.026199447f, -0.207116723f, 0.353408158f, 0.205467775f, + 0.00789254531f, -0.134069085f, -0.112607829f, 0.0233875047f, 0.0f, -0.0387839153f, 0.0892530382f, -0.0471618883f, + 0.0756332502f, -0.0586109795f, 0.0512010567f, 0.0879816115f, -0.0642168075f, 0.0356264152f, 0.0763714239f, -0.0614660382f, + 0.0389328189f, 0.0485167168f, 0.0847408697f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, -0.0978827327f, -0.040750619f, 0.0700185299f, -0.145666167f, 0.114869401f, -0.0239470415f, + -0.155651093f, 0.15092203f, -0.114719488f, -0.137186021f, 0.173357293f, -0.0189714208f, -0.112234809f, -0.134143308f, + 0.168896019f, 0.970568776f, 0.520145178f, 0.590163708f, 0.0f, 1.15013433f, 0.0700185299f, -0.0657610595f, + 0.716697991f, 0.716697991f, 0.00425747363f, -0.112956993f, 0.394270122f, 0.843643665f, 0.394270122f, -0.0429384597f, + -0.141120702f, 0.217330843f, 0.66195029f, 0.66195029f, 0.217330843f, -0.0711021721f, -0.135794401f, 0.206693694f, + 0.354373872f, 0.753234923f, 0.354373872f, 0.206693694f, -0.0657758638f, 0.0f, 0.0814544782f, -0.155878082f, + 0.0993518308f, -0.134175256f, 0.124161452f, -0.0992822051f, -0.15271464f, 0.136370912f, -0.0755712241f, -0.138591811f, + 0.130345002f, -0.0792669877f, -0.0874261558f, -0.145184755f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0320089012f, -0.0127972439f, -0.040750619f, 0.0785987899f, -0.0623021014f, + -0.0102930274f, 0.0891766846f, -0.0792738423f, 0.0619094968f, 0.0767014772f, -0.0897534713f, -0.00955905393f, 0.0700433701f, + 0.0756867006f, -0.0876928344f, 1.12336349f, -0.155028164f, 1.3213439f, 0.0f, 0.0700185299f, 1.15013433f, + 0.0295849703f, -0.160504267f, 0.325333416f, 1.07009876f, 0.0524424911f, -0.155825138f, 0.0213425104f, 0.392537892f, + 0.99264425f, 0.0669500679f, -0.115695231f, -0.135907158f, 0.224603862f, 0.373883933f, 0.930602074f, 0.0641381219f, + -0.112607829f, -0.134069085f, 0.00789254531f, 0.205467775f, 0.353408158f, 0.943017602f, 0.0f, -0.159002692f, + 0.394036174f, -0.208434805f, 0.354459465f, -0.286087513f, 0.284319371f, 0.379695565f, -0.331883788f, 0.224936441f, + 0.395964384f, -0.309212714f, 0.224246368f, 0.204306394f, 0.346628249f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, -0.0685512051f, -0.0127972439f, -0.0127972439f, -0.0951516554f, + 0.00747283641f, -0.0102930274f, -0.0973943248f, 0.0234881286f, -0.0778310373f, -0.0799424276f, 0.0337362848f, -0.00955905393f, + -0.0781459734f, -0.0783035085f, 0.0317285992f, 0.0827682614f, 0.0885644257f, -0.0978827327f, 0.0f, -0.040750619f, + 0.0700185299f, -0.0188477617f, 0.0805866346f, -0.145666167f, 0.114869401f, -0.0334874317f, 0.0892155319f, -0.0239470415f, + -0.155651093f, 0.15092203f, -0.0425821431f, 0.071380958f, 0.0653950199f, -0.114719488f, -0.137186021f, 0.173357293f, + -0.0408117026f, 0.0702525303f, 0.0755512714f, -0.0189714208f, -0.112234809f, -0.134143308f, 0.168896019f, 0.0f, + -0.113689706f, 0.394036174f, -0.136367202f, 0.206241444f, -0.16558373f, 0.0688553825f, 0.379695565f, -0.179268241f, + 0.0192085002f, 0.216399133f, -0.172697663f, 0.0283477232f, 0.0625052154f, 0.346628249f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0757671818f, -0.040750619f, -0.0127972439f, + 0.0805866346f, -0.0316450037f, -0.0239470415f, 0.0892155319f, -0.0462846756f, 0.0653950199f, 0.071380958f, -0.055379387f, + -0.0189714208f, 0.0755512714f, 0.0702525303f, -0.0536089465f, -0.050330855f, -0.0557539612f, 0.0320089012f, 0.0f, + -0.0127972439f, -0.040750619f, 0.0202700812f, -0.0951516554f, 0.0785987899f, -0.0623021014f, 0.0362853743f, -0.0973943248f, + -0.0102930274f, 0.0891766846f, -0.0792738423f, 0.0465335287f, -0.0799424276f, -0.0778310373f, 0.0619094968f, 0.0767014772f, + -0.0897534713f, 0.0445258431f, -0.0783035085f, -0.0781459734f, -0.00955905393f, 0.0700433701f, 0.0756867006f, -0.0876928344f, + 0.0f, 0.0528094359f, -0.155878082f, 0.064438872f, -0.112467363f, 0.0803585127f, -0.059129715f, -0.15271464f, + 0.0883368775f, -0.0322308689f, -0.115333222f, 0.0844521448f, -0.0374365039f, -0.0550054647f, -0.145184755f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.195778787f, 0.0700185299f, + -0.040750619f, -0.160504267f, -0.0111656478f, 0.0213425104f, -0.155825138f, 0.011691873f, -0.135907158f, -0.115695231f, + 0.026199447f, 0.00789254531f, -0.134069085f, -0.112607829f, 0.0233875047f, -0.022377478f, 0.072759524f, -0.0685512051f, + 0.0f, -0.0127972439f, -0.0127972439f, -0.0215514805f, 0.0785987899f, -0.0951516554f, 0.00747283641f, -0.0385232233f, + 0.0891766846f, -0.0102930274f, -0.0973943248f, 0.0234881286f, -0.0490028523f, 0.0767014772f, 0.0619094968f, -0.0778310373f, + -0.0799424276f, 0.0337362848f, -0.0469422154f, 0.0756867006f, 0.0700433701f, -0.00955905393f, -0.0781459734f, -0.0783035085f, + 0.0317285992f, 0.0f, -0.0337162726f, 0.0892530382f, -0.0410008803f, 0.0701590255f, -0.0508800298f, 0.0404851101f, + 0.0879816115f, -0.0557178557f, 0.0238055475f, 0.0707092956f, -0.0533588789f, 0.0274445098f, 0.039371945f, 0.0847408697f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.785944343f, + 0.805999875f, 0.454482079f, 0.560058355f, 0.384766042f, 0.674772203f, 0.350957274f, 0.308072299f, 0.547285378f, + 0.235707879f, 0.26165086f, 0.611315131f, 0.279808253f, 0.237059981f, 0.278263688f, -0.0853048638f, -0.0899630636f, + -0.148209289f, 0.0f, -0.181213826f, -0.0357419662f, 0.00726660667f, -0.0848960355f, -0.185241461f, -0.012772399f, + 0.0083396323f, -0.0111122318f, -0.167882279f, -0.135844126f, 0.0117737642f, 0.00417754194f, 0.0174665432f, -0.08875864f, + -0.182160407f, -0.0971545577f, 0.0260233823f, 0.00589526538f, 0.00958736148f, -0.0437768809f, -0.1606424f, -0.115741767f, + -0.0985649601f, 0.0209319368f, 0.0f, -0.0556032471f, 0.121464081f, -0.0686128139f, 0.0945059806f, -0.0840097219f, + 0.0472384878f, 0.124010995f, -0.0934383348f, 0.0240579769f, 0.0970674232f, -0.0880473182f, 0.0325129256f, 0.0469961166f, + 0.120115027f, 0.656395733f, 0.693550348f, -0.148209289f, 0.787868857f, -0.210398719f, 0.0244817678f, 0.709801912f, + 0.108553007f, -0.230014607f, 0.0790278986f, 0.728269339f, 0.149673283f, -0.185549408f, -0.132376865f, 0.0727257952f, + 0.749153137f, -0.0151918838f, 0.743870795f, 0.0904163793f, 0.725858152f, -0.0284509752f, 0.138672873f, 0.696808815f, + 0.0805369541f, 0.139208183f, 0.673090696f, -0.0341076739f, 0.0644519404f, 0.128681615f, 0.681902766f, 0.369808257f, + 0.32577917f, 0.249595046f, 0.0f, 0.727370977f, 0.00875582919f, -0.07678812f, 0.581906736f, 0.427010357f, + -0.0330419131f, -0.165314898f, 0.416799754f, 0.607160032f, 0.218108818f, -0.0751467273f, -0.221418396f, 0.307580382f, + 0.565320551f, 0.419812441f, 0.121110566f, -0.0980340242f, -0.201185688f, 0.304171383f, 0.313428491f, 0.549114227f, + 0.188008174f, 0.125972882f, -0.0900269225f, 0.0f, 0.0995378867f, -0.197062358f, 0.124362446f, -0.163828611f, + 0.154535398f, -0.101871327f, -0.202284217f, 0.173487544f, -0.0706288293f, -0.175791696f, 0.162611648f, -0.0798696205f, + -0.092163533f, -0.194259822f, 1.12114859f, 1.09630704f, 0.249595046f, 0.530103147f, 1.0134877f, -0.0956245214f, + 0.268539041f, 0.92575568f, 0.487135619f, -0.169882268f, 0.306160808f, 0.560391605f, 0.767297506f, 0.184175417f, + -0.162984431f, -0.284626216f, -0.0268656444f, -0.238345027f, -0.0897421911f, -0.222510248f, -0.0205096174f, -0.109521627f, + -0.202667087f, -0.0849388689f, -0.103656881f, -0.189411402f, -0.0174187813f, -0.0716776401f, -0.0995435715f, -0.194260612f, + 1.23108459f, 0.0323383994f, 1.33512092f, 0.0f, 0.241278589f, 1.16727686f, 0.00381910987f, 0.020380795f, + 0.302016884f, 1.11377382f, 0.012579374f, -0.0702786967f, 0.190156981f, 0.289754242f, 1.04574847f, 0.0204732157f, + -0.087442033f, 0.0228108317f, 0.287110627f, 0.249692336f, 0.999488056f, 0.017454559f, -0.0825986117f, -0.0408127941f, + 0.165998057f, 0.184359923f, 0.239153519f, 1.01719999f, 0.0f, -0.179138601f, 0.418652326f, -0.239200652f, + 0.389352232f, -0.329209119f, 0.2853944f, 0.438236862f, -0.403558195f, 0.225739822f, 0.474742621f, -0.358207464f, + 0.227803364f, 0.21126765f, 0.408426881f, 0.987125218f, -0.295005888f, 1.33512092f, -0.243456423f, 0.364650667f, + 1.00489175f, -0.164058641f, -0.0941023901f, 0.72022295f, 0.740241826f, -0.177447543f, -0.154486373f, 0.258320421f, + 0.541402519f, 0.784219503f, 0.163471252f, -0.0268656444f, 0.132145807f, 0.0517311804f, 0.12079452f, -0.0205096174f, + 0.0762855262f, 0.106442675f, 0.0488971286f, 0.0733744651f, 0.0968198106f, -0.0174187813f, 0.0529743694f, 0.071141161f, + 0.10035608f, 0.0598621108f, -0.0339826196f, -0.108781949f, 0.0f, -0.153750852f, 0.0410845242f, 0.00130789774f, + -0.0513869077f, -0.170660675f, 0.0742369518f, 7.21841861e-05f, 0.01137775f, -0.13447623f, -0.146834746f, 0.110382281f, + -0.0022178425f, 0.0319381319f, -0.0530611388f, -0.166233703f, -0.119735397f, 0.131896257f, -0.00128461607f, 0.0279658791f, + -0.00634207344f, -0.124800295f, -0.115073167f, -0.118792132f, 0.124231398f, 0.0f, -0.124856472f, 0.418652326f, + -0.150763556f, 0.212809891f, -0.179730907f, 0.0498502702f, 0.438236862f, -0.196334913f, -0.00137086341f, 0.225102708f, + -0.186951518f, 0.0146026313f, 0.0478232615f, 0.408426881f, 0.184908807f, 0.159349129f, -0.108781949f, 0.142724514f, + -0.198574141f, 0.129562557f, 0.0964779407f, 0.0234734211f, -0.243038014f, 0.212791249f, 0.104537755f, 0.0728127137f, + -0.160118237f, -0.166709453f, 0.203562438f, -0.285919398f, -0.0151918838f, -0.22266452f, -0.128905758f, -0.202599213f, + -0.0284509752f, -0.140100867f, -0.17790024f, -0.124938786f, -0.120852917f, -0.161565244f, -0.0341076739f, -0.107172683f, + -0.118954949f, -0.167531237f, 0.0633655339f, 0.0243620891f, 0.035627991f, 0.0f, 0.0881999061f, -0.0533521138f, + 0.0012146068f, 0.0175533239f, 0.110432841f, -0.0718007907f, 0.00559099531f, -0.0304933768f, 0.0818924755f, 0.0911090001f, + -0.0914510116f, 0.0100810379f, -0.0457622968f, 0.0205301996f, 0.108312838f, 0.0698677823f, -0.102728777f, 0.0083619887f, + -0.0407417715f, -0.00411251979f, 0.0785926357f, 0.0768310651f, 0.0706149191f, -0.0987645835f, 0.0f, 0.0702720582f, + -0.197062358f, 0.0870602801f, -0.140467182f, 0.107510909f, -0.0640047714f, -0.202284217f, 0.120740525f, -0.0308822244f, + -0.147731632f, 0.113058038f, -0.0414264947f, -0.0603006929f, -0.194259822f, -0.176864818f, -0.290847898f, 0.035627991f, + -0.255454212f, 0.102170832f, -0.101525724f, -0.190900356f, -0.0924148858f, 0.136958897f, -0.142318815f, -0.202040881f, + -0.117652103f, 0.0902495757f, 0.0842475221f, -0.138043836f, 0.824844658f, 0.699901104f, 0.545507491f, 0.494377375f, + 0.473387361f, 0.702622354f, 0.285964251f, 0.371104538f, 0.552347839f, 0.191208676f, 0.307890743f, 0.647253513f, + 0.226186201f, 0.204878047f, 0.342236161f, -0.389524847f, -0.0027190079f, -0.293110013f, 0.0f, 0.016338611f, + -0.28964901f, -0.00337499753f, 0.0437209345f, -0.0184517168f, -0.28538686f, -0.0119487355f, 0.0517441966f, 0.0164014976f, + -0.0321926922f, -0.276355326f, -0.0199505799f, 0.0503956415f, 0.0563733429f, -0.0271563437f, -0.033068236f, -0.268714488f, + -0.0153386025f, 0.0473099537f, 0.0352082662f, 0.0151038794f, -0.0206748806f, -0.0305464864f, -0.273113042f, 0.0f, + 0.0536773503f, -0.158766866f, 0.0713428259f, -0.118848756f, 0.0920027122f, -0.0627678484f, -0.182675123f, 0.117365845f, + -0.0456710458f, -0.150012314f, 0.099000141f, -0.0492286086f, -0.0503466912f, -0.174710378f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.528343618f, -0.0715699196f, 0.553352475f, + 0.0467505828f, 0.543873489f, -0.0718481913f, 0.10617815f, 0.515431225f, 0.0755406842f, 0.109365083f, 0.486796081f, + -0.0661863238f, 0.0622062385f, 0.0995173827f, 0.503569841f, 0.922893167f, 0.137242407f, 0.833464324f, 0.0f, + 0.422121555f, 0.696524382f, -0.0365473144f, 0.298695922f, 0.312287569f, 0.658098519f, -0.0900438875f, 0.183347464f, + 0.423746288f, 0.201697022f, 0.600156426f, -0.124471664f, 0.137148693f, 0.334758788f, 0.353019327f, 0.155394837f, + 0.561779976f, -0.105635114f, 0.142419264f, 0.148350567f, 0.390221238f, 0.160567254f, 0.159439564f, 0.582974136f, + 0.0f, -0.0588760264f, 0.213834286f, -0.0811875239f, 0.178226024f, -0.109368324f, 0.107662991f, 0.246034995f, + -0.147493556f, 0.0868313611f, 0.235415995f, -0.119480886f, 0.0875750482f, 0.084181197f, 0.235307723f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.490655869f, -0.0715699196f, + -0.407928556f, -0.135812357f, -0.376764953f, -0.0718481913f, -0.130829349f, -0.324881077f, -0.169207901f, -0.107575506f, + -0.28776589f, -0.0661863238f, -0.090451628f, -0.106454618f, -0.308451205f, -0.0101267314f, -0.046756357f, 0.167633057f, + 0.0f, -0.0971884057f, 0.209498495f, 0.0149198696f, -0.0935893506f, -0.0450083427f, 0.217391923f, 0.0387464575f, + -0.0741454065f, -0.0975624844f, -0.00855807308f, 0.225966424f, 0.0553709827f, -0.0624441504f, -0.110149816f, -0.0448806621f, + 0.00277358131f, 0.229429886f, 0.0461538844f, -0.0619172975f, -0.055746913f, -0.0898437425f, -0.0120804524f, -0.00108115491f, + 0.227769464f, 0.0f, -0.0637187809f, 0.213834286f, -0.082922034f, 0.143712938f, -0.104269817f, 0.0662070438f, + 0.246034995f, -0.128779233f, 0.0432550013f, 0.17326057f, -0.111220099f, 0.0500033908f, 0.0545153245f, 0.235307723f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, +}; + +// config 5: mid=15 upper=9 lower=5 zenith=1 rows=30 +inline constexpr int kConfig5Counts[5] = { + 15, 9, 5, 1, 30, +}; +inline constexpr int kConfig5PrimaryRowMap[30] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, +}; +inline constexpr int kConfig5SecondaryRowMap[30] = { + 0, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, + 3, 2, 1, 15, 23, 22, 21, 20, 19, 18, 17, 16, + 24, 28, 27, 26, 25, 29, +}; +inline constexpr float kConfig5Matrix[2190] = { + 0.673723519f, 1.19093812f, -0.0701653063f, 0.649795473f, -0.0238566417f, 1.00667691f, 0.187031582f, -0.0104889553f, + 0.705212057f, 0.0158566795f, -0.00245481334f, 0.855731785f, 0.353140473f, -0.0262302384f, 0.000243895789f, -0.0256515574f, + -0.0365070328f, 0.000193966713f, -0.00728288945f, -0.0290487166f, -0.0245455913f, 0.0f, -0.0472547449f, -0.014038397f, + 0.00526633253f, -0.00887277164f, -0.0307118203f, 0.0223211851f, -0.0334220864f, 0.0227861982f, -0.0144879622f, -0.0072568804f, + -0.0186657459f, 0.0168705937f, -0.0301180687f, 0.0208873879f, -0.0161217246f, 0.00296449522f, -0.0335194245f, 0.0212091245f, + -0.0081425067f, -0.023612529f, 0.0194778275f, 0.0f, 0.0215762202f, -0.0339281f, 0.0183300395f, -0.0160196815f, + 0.0154633131f, 0.000154794398f, -0.0282441098f, 0.0132158753f, 0.00797203556f, -0.0186210498f, 0.0135914534f, 0.00788380858f, + -0.00751127675f, -0.0263710562f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.07957375f, 0.685713172f, 0.515679955f, 1.15957403f, 0.161173001f, 0.247922152f, 1.00118577f, + 0.0963975191f, 0.616149306f, 0.753210127f, 0.059593305f, 0.200598687f, 0.674185216f, 0.602421999f, 0.0473654568f, + 0.0496948287f, 0.105642244f, -0.0235941391f, -0.0380983613f, 0.00414487813f, 0.0121587003f, 0.0f, 0.0221591536f, + 0.00133252877f, -0.0226443652f, 0.0403096303f, -0.00692859199f, -0.0358187035f, 0.0257348847f, -0.0318878219f, 0.0669343621f, + -0.0352488048f, 0.00778992614f, -0.0280643441f, 0.0241054427f, -0.0293827858f, 0.0745327175f, -0.0439230651f, 0.021046767f, + -0.0327194408f, 0.00149537111f, 0.0181993097f, -0.0277211349f, 0.0f, -0.0127877854f, 0.0278066527f, -0.00907365512f, + 0.0127531681f, -0.00586266769f, -0.00219390634f, 0.0239962023f, -0.00337429624f, -0.00951075554f, 0.0159269348f, -0.00378876389f, + -0.0092753619f, 0.00646289252f, 0.0226595104f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.06358993f, 0.000233982515f, 1.29223299f, 0.0791335776f, 1.13913786f, -0.0422942005f, + 0.31353116f, 1.03960538f, -0.0569440536f, 0.440851331f, 0.968678117f, -0.0352459215f, 0.0207754094f, 0.461492836f, + 0.938044786f, -0.105183244f, -0.144518808f, -0.00325221592f, 0.0212145373f, -0.0588367246f, -0.0293253046f, 0.0f, + -0.0639383495f, -0.0170610063f, 0.0151295066f, -0.0551014096f, 0.077809304f, 0.00276407599f, -0.039480187f, 0.0218117014f, + -0.09899275f, 0.199192226f, -0.0582580082f, 0.0161276497f, -0.0353418067f, 0.0194849502f, -0.121107988f, 0.241340905f, + -0.052512411f, 0.00555603718f, -0.0101789273f, -0.0274277274f, 0.0179590322f, 0.0f, 0.0120766563f, -0.035349004f, + 0.00888795871f, -0.0190655645f, 0.00603490137f, -0.0026048366f, -0.0292866863f, 0.00377468741f, 0.00584249059f, -0.0213424321f, + 0.00415363163f, 0.00577378459f, -0.0103157843f, -0.0272955075f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.51201272f, -0.0171175227f, 0.384186745f, -0.00560966041f, 0.4580383f, + -0.00881147012f, -0.0529597029f, 0.496913373f, 0.0216342825f, -0.0633659363f, 0.519459486f, -0.00809258595f, 0.00479581347f, + -0.0571713485f, 0.523566186f, 0.558019757f, 0.625771821f, -0.0670664161f, 0.395643622f, 0.442609936f, 0.0070985998f, + 0.0f, 0.881162167f, -0.0429992415f, -0.0347428322f, -0.103463098f, 0.954516292f, 0.0363033824f, 0.0107537676f, + -0.0392059162f, -0.167226136f, 0.833089471f, 0.352293342f, -0.0656336769f, 0.0213994347f, -0.0360963568f, -0.190219566f, + 0.719047368f, 0.505202055f, 0.0211477913f, -0.0252848864f, 0.0177478194f, -0.0341215618f, 0.0f, -0.00716604898f, + 0.0279499162f, -0.00271705328f, 0.0133212749f, 0.00107402389f, -0.00222858321f, 0.024269674f, 0.00398897193f, -0.00997234695f, + 0.0169389378f, 0.00350465439f, -0.00963676162f, 0.00769401621f, 0.0229545627f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, -0.0645011291f, -0.0180180874f, -0.0648218915f, -0.0388750322f, + -0.0703236386f, -0.00341586093f, -0.00117048679f, -0.0723594651f, -0.0377457365f, 0.0172499567f, -0.0721437559f, -0.00213102554f, + -0.0284806099f, 0.0205009021f, -0.0714788809f, 1.10197616f, 0.949446201f, 0.377922058f, 0.0364316069f, 1.14330184f, + -0.06534978f, 0.0f, 1.03049529f, 0.264213145f, 0.00731180748f, 0.0119402083f, 0.462371528f, 0.916850805f, + 0.00181921897f, 0.0179320034f, 0.0210845452f, 0.18819429f, 0.8670941f, 0.452677548f, -0.0586053245f, 0.0171728898f, + 0.0246903058f, 0.0927128568f, 0.565344155f, 0.777771294f, 0.143756717f, -0.0587303974f, 0.0159722604f, 0.0f, + 0.00508541847f, -0.0418245792f, -0.000264200557f, -0.0226891059f, -0.00454183901f, -0.00392945623f, -0.0342080146f, -0.00782645214f, + 0.00560235931f, -0.0252470616f, -0.00728129456f, 0.00552659295f, -0.0127223991f, -0.0317037627f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00796984695f, -0.00200935267f, -0.000183831653f, + 0.0221403688f, 0.006615791f, -0.00683281804f, -0.0110368207f, 0.0125337243f, 0.0255848076f, -0.0284815654f, 0.0157822873f, + -0.0062816292f, 0.0187490918f, -0.0315778665f, 0.0167363435f, 1.019086f, 0.0902081057f, 1.09393394f, -0.00608733995f, + 0.912090719f, 0.327199817f, 0.0f, -0.0200661086f, 1.24762559f, -0.0479246713f, -0.0069691129f, -0.0687378198f, + 0.535192907f, 0.870310783f, -0.0782554224f, -0.0118469652f, -0.0330948196f, 0.095075056f, 0.821692407f, 0.551846921f, + -0.082876876f, -0.0135420598f, -0.0134619111f, -0.0395413227f, 0.447488397f, 0.729092717f, 0.399233997f, -0.0817863867f, + 0.0f, 0.0429355875f, 0.0206595212f, 0.0656185523f, -0.00543382857f, 0.0824154913f, -0.024380669f, 0.020591069f, + 0.0949229002f, -0.0311095435f, 0.00482541183f, 0.0928643718f, -0.0298566669f, -0.00355012855f, 0.0201929752f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.0373948663f, -0.00992185157f, + -0.019365292f, -0.0328406543f, -0.0215675961f, -0.00152471568f, 0.000103675018f, -0.0270285662f, -0.0320443921f, 0.017939331f, + -0.0300990436f, -0.000940775324f, -0.0241078287f, 0.0219190959f, -0.0309195761f, 1.0448395f, -0.00903022476f, 1.03558636f, + -0.00800357666f, 0.130907714f, 1.08538294f, 0.0f, 0.0155836465f, 0.392929494f, 0.85141021f, 0.00847642403f, + 0.0129238777f, -0.0711679906f, 0.607504845f, 0.743840098f, 0.014129254f, -0.00872275326f, -0.0120291263f, 0.0335123688f, + 0.673975408f, 0.664502442f, 0.0161332376f, -0.0177830867f, 0.0194131564f, -0.064321503f, 0.181224778f, 0.647009969f, + 0.631758153f, 0.0f, 0.0570750572f, -0.0475801937f, 0.0314656086f, 0.0604673736f, 0.00519695366f, 0.125569373f, + -0.0469470397f, -0.0148645882f, 0.146822557f, 0.0170895848f, -0.0115328524f, 0.138501167f, 0.0353704691f, -0.0457491018f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0153558226f, + -0.00446326192f, 0.00448248489f, 0.0224931687f, 0.00775335077f, -0.00378075242f, -0.00831502583f, 0.0136030316f, 0.0256675538f, + -0.0242357105f, 0.0169941615f, -0.003284798f, 0.0179258417f, -0.0271288659f, 0.0180118568f, 1.01207149f, -0.0300437268f, + 1.03033531f, 0.00144849508f, -0.0485058874f, 1.00731957f, 0.0f, -0.0482567102f, -0.0631050691f, 1.1583308f, + -0.00679620402f, -0.0261049271f, 0.0177402832f, -0.0691778362f, 1.18050134f, -0.0113285724f, -0.00123082579f, -0.0241476037f, + 0.00274911593f, -0.029673906f, 1.19075954f, -0.0128800208f, 0.00871498324f, -0.0345420092f, 0.0169894043f, -0.0331216007f, + -0.00172009377f, 1.18098783f, 0.0f, -0.482578397f, 0.75088948f, -0.596834481f, 0.705204248f, -0.675374568f, + 0.54242754f, 0.533168137f, -0.728206038f, 0.410569787f, 0.583271801f, -0.719814658f, 0.38985765f, 0.399218261f, + 0.468439549f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + -0.0313186981f, -0.00446326192f, -0.0144389905f, -0.0295605026f, -0.0170352142f, -0.00378075242f, 0.00282917754f, -0.022124365f, + -0.031267263f, 0.0191078652f, -0.0249892864f, -0.003284798f, -0.0213391986f, 0.0223117657f, -0.0257814322f, 0.210083216f, + 0.0166338794f, 0.188237414f, 0.000978274387f, -0.00112774875f, 0.222046554f, 0.0f, 0.0305762272f, -0.00300621823f, + 0.0887882262f, 0.00720908958f, 0.0137852896f, -0.0320369452f, 0.0208421554f, 0.064561151f, 0.0120423539f, -0.00684887683f, + 0.0127361547f, -0.0324065275f, 0.0174889695f, 0.0848960578f, 0.0137454802f, -0.0147953304f, 0.0244892668f, -0.0285155643f, + -0.00486356439f, 0.0106039513f, 0.0941676572f, 0.0f, -0.157937974f, 0.75088948f, -0.159039855f, 0.286859602f, + -0.156561404f, 0.090824753f, 0.533168137f, -0.153499886f, 0.0177511536f, 0.284433663f, -0.154063225f, 0.0144972755f, + 0.108390026f, 0.468439549f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0162403639f, -0.00992185157f, -0.00023401955f, 0.0208236203f, 0.0100251455f, -0.00152471568f, -0.0103542386f, + 0.0154009536f, 0.0261204559f, -0.0249325261f, 0.0184544623f, -0.000940775324f, 0.0160798058f, -0.027025843f, 0.0193556324f, + -0.0584808141f, -0.0277388934f, -0.0427341536f, -0.0113108726f, -0.0192387048f, -0.0606374629f, 0.0f, -0.0405222103f, + -0.0127255814f, -0.0199935641f, -0.00638149353f, -0.0216032658f, 0.0191571619f, -0.0367677957f, -0.00294120307f, -0.010687422f, + -0.000217779263f, -0.0203249305f, 0.0204763319f, -0.0370604172f, -0.00669073872f, -0.0121713737f, 0.00846377853f, -0.0295157302f, + 0.0177119281f, -0.0036246879f, -0.0314455442f, -0.00882939063f, 0.0f, 0.041676607f, -0.0475801937f, 0.0393914655f, + -0.0504247136f, 0.0367558673f, -0.0160459895f, -0.0469470397f, 0.034439452f, 0.00218664063f, -0.0497839749f, 0.0348381847f, + 0.00285806996f, -0.0233128741f, -0.0457491018f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, -0.0317522734f, -0.00200935267f, 0.000139962794f, -0.0290889312f, -0.0185070392f, -0.00683281804f, + 0.0051539829f, -0.0232248604f, -0.0330060273f, 0.0207151938f, -0.025801653f, -0.0062816292f, -0.0202133879f, 0.023173904f, + -0.026491208f, -0.00585797569f, 0.0203284342f, -0.0213349145f, 0.0168192107f, 0.00518546812f, 0.00495977141f, 0.0f, + 0.0305026937f, -0.00130385556f, -0.0257941615f, 0.00677399943f, 0.0155317811f, -0.0267935842f, 0.0234320834f, -0.0411534943f, + 0.0113799823f, -0.00383371627f, 0.0127175972f, -0.0274395421f, 0.0255520679f, -0.0392661393f, 0.0130148074f, -0.011685852f, + 0.0239995606f, -0.0241965633f, -0.003443419f, 0.0215250459f, -0.0375080258f, 0.0f, -0.0139387855f, 0.0206595212f, + -0.00834036432f, 0.00815727096f, -0.00375616271f, -0.00957319327f, 0.020591069f, -0.000291561126f, -0.0171134714f, 0.0123989414f, + -0.000863671536f, -0.0168126076f, 0.000737507304f, 0.0201929752f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0153138917f, -0.0180180874f, -0.0163715873f, 0.0194425825f, 0.0121584861f, + -0.00341586093f, -0.0136072747f, 0.0173782464f, 0.0265480001f, -0.0276642889f, 0.0202520397f, -0.00213102554f, 0.0146286897f, + -0.0289338361f, 0.021076601f, -0.0285390709f, -0.0285341553f, -0.00370878959f, -0.0196169578f, -0.019042071f, -0.0258287713f, + 0.0f, -0.0382074788f, -0.0088507561f, 0.00589001505f, -0.00626126397f, -0.0227561556f, 0.0203761365f, -0.0309214666f, + 0.0234801136f, -0.0105442479f, -0.00258161733f, -0.0171214994f, 0.0199697744f, -0.0315160491f, 0.0221103132f, -0.0120293433f, + 0.00605327543f, -0.0280867014f, 0.0189325735f, -0.00275007985f, -0.0267054588f, 0.020736767f, 0.0f, 0.0194517467f, + -0.0418245792f, 0.0153241865f, -0.0166477114f, 0.0118441163f, 0.00314126536f, -0.0342080146f, 0.00916203763f, 0.0115851415f, + -0.0196502693f, 0.0096075777f, 0.0115324929f, -0.00501359859f, -0.0317037627f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, -0.0379184932f, -0.0171175227f, 0.00795940217f, -0.0356279537f, + -0.0219320524f, -0.00881147012f, 0.00401614606f, -0.0265283082f, -0.0388818793f, 0.021392934f, -0.0289252233f, -0.00809258595f, + -0.0234701764f, 0.0238871276f, -0.0295282397f, 0.00339208334f, 0.0224190205f, -0.0177543722f, 0.0150146857f, 0.00914502423f, + 0.0102314726f, 0.0f, 0.0299813189f, 0.00151848036f, -0.0149406893f, 0.00688516675f, 0.0175199602f, -0.02736995f, + 0.0241374485f, -0.0346891284f, 0.011629303f, -0.00149540196f, 0.010490614f, -0.0245366003f, 0.0249951072f, -0.033159405f, + 0.0133240866f, -0.00961429998f, 0.0236277077f, -0.0249364488f, -0.000596011872f, 0.0206710044f, -0.0316867977f, 0.0f, + -0.0183923673f, 0.0279499162f, -0.0137240915f, 0.00843574945f, -0.00990375131f, -0.00757100061f, 0.024269674f, -0.0070135491f, + -0.0142318401f, 0.0123716202f, -0.00749109313f, -0.0139615592f, 0.00153088814f, 0.0229545627f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00083953212f, 0.000233982515f, -0.024972029f, + 0.0172351804f, 0.0111431684f, -0.0422942005f, -0.0164569467f, 0.0171301756f, 0.0185905118f, -0.0326333418f, 0.0203838516f, + -0.0352459215f, 0.0144612659f, -0.0339437053f, 0.0213216878f, -0.0278416276f, -0.0317200422f, 0.000467850186f, -0.0138754155f, + -0.0223380234f, -0.0233746786f, 0.0f, -0.0393698215f, -0.0107116792f, -0.000761054805f, -0.00663055619f, -0.025958268f, + 0.0220011119f, -0.0308618937f, 0.0240054298f, -0.0111897821f, -0.0055475058f, -0.0154697085f, 0.0182200074f, -0.0296391491f, + 0.0225615595f, -0.01275454f, 0.00360855227f, -0.0287381802f, 0.0205841176f, -0.00512335263f, -0.0242440179f, 0.0212344006f, + 0.0f, 0.0266369339f, -0.035349004f, 0.0229424685f, -0.0142805139f, 0.0197895039f, 0.00268650986f, -0.0292866863f, + 0.0173553061f, 0.0102553917f, -0.0172539204f, 0.0177600309f, 0.0101617714f, -0.00519839348f, -0.0272955075f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.0296939202f, 0.685713172f, + 0.00446987245f, -0.0815515593f, -0.0260452218f, 0.247922152f, -0.0307494234f, -0.0313843377f, -0.0350868627f, 0.00673371414f, + -0.0341946185f, 0.200598687f, -0.0568262078f, 0.0166082419f, -0.0348976403f, 0.00189600466f, 0.0229054056f, -0.0175013952f, + 0.0085457433f, 0.0105245113f, 0.0124363927f, 0.0f, 0.0296937823f, 0.00366632431f, -0.00591262709f, 0.00792715698f, + 0.0175444167f, -0.0303182956f, 0.0252976492f, -0.0317334421f, 0.0133156814f, -0.00265327631f, 0.00835055765f, -0.0236843582f, + 0.0243351124f, -0.0298437793f, 0.0152336964f, -0.0113384733f, 0.0236372612f, -0.0277254712f, 0.00186364574f, 0.0191988349f, + -0.0283409804f, 0.0f, -0.0270336829f, 0.0278066527f, -0.0230459832f, 0.0105058886f, -0.0196816679f, -0.00464583701f, + 0.0239962023f, -0.0170993898f, -0.0115057388f, 0.0139729911f, -0.0175281167f, -0.0112823648f, 0.00398767367f, 0.0226595104f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.582739174f, + 1.18186677f, 0.0757991001f, 0.730422735f, -0.00800202321f, 0.827471495f, 0.403052866f, -0.0384798497f, 0.652289629f, + 0.218360215f, -0.0582393631f, 0.729540408f, 0.375910103f, 0.189087152f, -0.0573888123f, -0.0431344137f, -0.167226762f, + 0.0261532031f, -0.0480152182f, 0.0448281579f, -0.0324989781f, 0.0f, -0.0675013065f, 0.0669114739f, -0.080466412f, + 0.0218996126f, -0.102699116f, 0.0446141399f, 0.0290621445f, -0.0717086568f, 0.0382435545f, -0.105438292f, -0.0243505333f, + 0.0665615797f, 0.000683896011f, -0.0634761527f, 0.03748101f, -0.102012455f, -0.0491760038f, 0.0443241186f, 0.0454801209f, + -0.0024018609f, -0.0638857111f, 0.0f, -0.0168742239f, 0.0759329423f, -0.0121138031f, 0.0382853374f, -0.00214445707f, + -0.0125027746f, 0.0712819472f, 0.00586218294f, -0.0323962308f, 0.0379560329f, 0.00245559681f, -0.0273106527f, 0.00836485904f, + 0.0681104138f, 0.629074037f, 0.628333569f, 0.0261532031f, 0.598059177f, 0.0382491834f, -0.0767985135f, 0.738980293f, + -0.156953365f, 0.0393243134f, -0.0238208733f, 0.760217369f, -0.127812356f, 0.0450994335f, -0.0489285849f, 0.00398488715f, + 1.32819426f, 0.145652324f, 1.0660125f, 0.457446933f, 0.92983228f, 0.0721431002f, 0.500318289f, 0.857310414f, + 0.27393952f, 0.463171214f, 0.798977673f, 0.0530846044f, 0.270976216f, 0.436253756f, 0.80167079f, 0.00227978104f, + 0.100900829f, -0.112847999f, 0.0779791996f, -0.0814096257f, 0.0169646572f, 0.0f, 0.188748643f, -0.139059275f, + 0.0597440749f, -0.0486377068f, 0.29368785f, -0.0857900232f, -0.0728850514f, 0.0516753979f, -0.0901838318f, 0.314857215f, + 0.0622595996f, -0.129210323f, -0.0248210169f, 0.0439084247f, -0.0882466659f, 0.299947053f, 0.106472068f, -0.0857023075f, + -0.0861023292f, -0.0183310769f, 0.0442977473f, 0.0f, 0.0207005646f, -0.0937152579f, 0.0162043143f, -0.0537156053f, + 0.00644587399f, 0.00169577065f, -0.0866018906f, -0.00127935945f, 0.0251667053f, -0.0534464382f, 0.00199984876f, 0.0195589922f, + -0.0184557494f, -0.0818293691f, 1.1592958f, 1.18877792f, -0.112847999f, 1.29548562f, -0.0601255447f, 0.0564329997f, + 0.891406357f, 0.568785012f, -0.118513547f, 0.00484155025f, 0.671773016f, 0.827042222f, -0.0841051117f, 0.016765615f, + -0.0224117637f, -0.0471593477f, -0.0451850332f, 0.169879988f, -0.135042548f, 0.223735228f, -0.0258368328f, -0.122918002f, + 0.243862137f, -0.0972137451f, -0.0951537266f, 0.257110775f, -0.0203695837f, -0.0975799263f, -0.0901699364f, 0.2565054f, + 0.863268971f, 0.765923738f, 0.406189859f, 0.138914764f, 1.08361351f, -0.110916175f, 0.0f, 0.870351315f, + 0.487305045f, -0.161588728f, -0.0579231046f, 0.584466875f, 0.754994929f, 0.188306943f, -0.148234397f, -0.0990928933f, + 0.388529688f, 0.724075556f, 0.474692971f, 0.0509642996f, -0.134741649f, -0.0972554907f, 0.350363612f, 0.479307711f, + 0.664330959f, 0.219033927f, 0.0340592079f, -0.135423258f, 0.0f, -0.00489442702f, 0.078410998f, 0.00835659634f, + 0.0347666033f, 0.0311492905f, -0.0273456089f, 0.074050881f, 0.0475502014f, -0.0519629419f, 0.0342045352f, 0.0406870171f, + -0.0438469388f, 0.00716661569f, 0.0710672662f, 0.93129009f, 0.836355388f, 0.406189859f, -0.0636283979f, 1.30614126f, + -0.156092018f, -0.161588863f, 1.03128588f, 0.383788198f, -0.0694780797f, -0.160115048f, 0.506635129f, 0.962623894f, + -0.0214956142f, -0.0229166038f, 0.0333205797f, -0.0125639606f, -0.0564445332f, 0.0639558733f, -0.0773992315f, -0.0111972904f, + 0.0641763434f, -0.0851455107f, 0.0441768058f, 0.0498437658f, -0.0901892036f, -0.010862601f, 0.0528890193f, 0.0472065769f, + -0.0899637341f, 1.03582823f, -0.102467388f, 1.12751198f, -0.0376018547f, 0.473867297f, 0.838153303f, 0.0f, + -0.00444244361f, 0.707575321f, 0.628336668f, 0.0179209504f, -0.103094168f, 0.308361769f, 0.642724693f, 0.539946139f, + 0.0315784961f, -0.109399155f, 0.0421842113f, 0.498108655f, 0.535509825f, 0.469615489f, 0.0309500229f, -0.1053956f, + -0.0570739061f, 0.263574481f, 0.416164786f, 0.498899013f, 0.472894877f, 0.0f, 0.156371534f, -0.160826966f, + 0.161342978f, -0.0661459714f, 0.143018141f, 0.0580478087f, -0.149082303f, 0.11702013f, 0.107820667f, -0.0649349689f, + 0.128951609f, 0.0871349275f, -0.0207075868f, -0.141114011f, 1.06564355f, 0.0316621885f, 1.12751198f, 0.0395403914f, + 0.599200428f, 0.589783609f, 0.079078339f, -0.118299805f, 1.13110876f, 0.216215298f, 0.0773141533f, -0.144427344f, + 0.412166238f, 0.959416151f, 0.0772020072f, -0.0780194998f, -0.00735362386f, 0.000363675063f, -0.075123474f, 0.0203515664f, + -0.00378955039f, -0.0721240565f, 0.0284447931f, -0.0540062673f, -0.0565585978f, 0.0340634398f, -0.00286118267f, -0.0578126274f, + -0.0531127378f, 0.0338108912f, 1.11200178f, 0.0619372092f, 1.00760245f, 0.0215742458f, -0.139597476f, 1.19554067f, + 0.0f, 0.00766781298f, -0.132116616f, 1.3038758f, -0.0101415189f, 0.0534071885f, -0.104609512f, -0.0616015159f, + 1.29565048f, -0.017841015f, 0.0588370375f, -0.0158591624f, -0.129339963f, -0.0102927061f, 1.27426279f, -0.0174845178f, + 0.0573458746f, 0.0278902594f, -0.0943378136f, -0.0973501652f, -0.00749969203f, 1.27550352f, 0.0f, -0.523674428f, + 0.731159925f, -0.650714755f, 0.731598377f, -0.805953026f, 0.553590357f, 0.634527743f, -0.896543324f, 0.396371365f, + 0.736270785f, -0.860052168f, 0.402345479f, 0.421151519f, 0.57293129f, 1.020594f, -0.0509611666f, 1.00760245f, + -0.0815047622f, -0.157254815f, 1.29990494f, -0.0990225971f, 0.0608489886f, -0.0345645696f, 1.17251825f, -0.0915668458f, + 0.0664598122f, -0.127120763f, 0.262966812f, 1.03888929f, 0.0522631928f, -0.00735362386f, -0.0180247445f, 0.0628565848f, + -0.0352828763f, -0.00378955039f, 0.0604595579f, -0.0419499427f, 0.0463127159f, 0.0456026196f, -0.0464715175f, -0.00286118267f, + 0.0519024506f, 0.0426710807f, -0.0462690927f, 0.108263962f, -0.0716764927f, 0.198088512f, -0.0219139494f, 0.0545913465f, + 0.0321381651f, 0.0f, -0.0380837917f, 0.0671429113f, -0.0690789968f, 0.0103994273f, -0.0640888438f, 0.0448947661f, + 0.0316911563f, -0.0465719625f, 0.0183032919f, -0.0638383031f, -0.0139386365f, 0.0668630674f, 0.00387227722f, -0.0266879089f, + 0.0179381166f, -0.0615384355f, -0.0376799926f, 0.0419567749f, 0.0517186001f, 0.0016762031f, -0.0276715048f, 0.0f, + -0.0976846814f, 0.731159925f, -0.0913622752f, 0.242031932f, -0.0729917437f, -0.0104251392f, 0.634527743f, -0.0573079549f, + -0.0698950365f, 0.241379306f, -0.064051196f, -0.0610059313f, 0.0244102236f, 0.57293129f, 0.178593248f, -0.00195333292f, + 0.198088512f, 0.0559793152f, 0.0569108054f, -0.059676107f, 0.0783174559f, -0.087702401f, 0.024943538f, 0.0794327334f, + 0.0747087076f, -0.0793564543f, 0.0512976386f, -0.0887095332f, 0.155677408f, -0.074570559f, -0.0125639606f, 0.00553065771f, + -0.0791650414f, 0.0247458518f, -0.0111972904f, -0.0701041445f, 0.0322953463f, -0.060717091f, -0.0503898859f, 0.037434753f, + -0.010862601f, -0.0607655235f, -0.0464904234f, 0.0372062437f, -0.0622534528f, 0.0572579652f, -0.0972820967f, 0.0187821202f, + -0.0710400417f, -0.0309458692f, 0.0f, 0.0212488528f, -0.0812904239f, 0.041837655f, -0.00893362053f, 0.050857313f, + -0.0562724397f, -0.047537107f, 0.0325544812f, -0.0157633554f, 0.0547331348f, 0.000140254633f, -0.0752995387f, -0.0207397863f, + 0.024055887f, -0.0154457008f, 0.0533954352f, 0.0264748298f, -0.0520576425f, -0.0568081737f, -0.0174228959f, 0.0244794134f, + 0.0f, 0.0386469737f, -0.160826966f, 0.0349122807f, -0.0871790722f, 0.0249476209f, 0.00432396773f, -0.149082303f, + 0.0165950172f, 0.0353767201f, -0.0868968219f, 0.0201769732f, 0.0299899597f, -0.0158331841f, -0.141114011f, -0.0980876833f, + -0.0364487022f, -0.0972820967f, -0.0778823867f, -0.0774388984f, 0.0379873961f, -0.0974593237f, 0.065887183f, -0.0625820383f, + -0.0208417196f, -0.0926596969f, 0.0651858151f, -0.0657962859f, 0.0266904905f, -0.052135475f, 0.0538055003f, -0.0451850332f, + -0.0244025104f, 0.0640602559f, -0.0426751934f, -0.0258368328f, 0.0626258552f, -0.0494355746f, 0.0468212999f, 0.0435178243f, + -0.0538747124f, -0.0203695837f, 0.056458924f, 0.0398882106f, -0.053679429f, -0.00618188689f, -0.0645236969f, 0.0244251546f, + -0.0208616983f, 0.0545782149f, -0.0139032742f, 0.0f, -0.030374676f, 0.0652720109f, -0.0837489814f, 0.0101233115f, + -0.0583433434f, 0.0468386337f, 0.033645425f, -0.0758886784f, 0.0178945828f, -0.0618915446f, -0.00576738268f, 0.0634849593f, + 0.00913732313f, -0.0682660937f, 0.0175337102f, -0.0601090528f, -0.0286491681f, 0.0444599055f, 0.0468597561f, 0.00647120597f, + -0.0686502382f, 0.0f, -0.0135422889f, 0.078410998f, -0.00717338221f, 0.0348111652f, 0.00490456121f, -0.0218920782f, + 0.074050881f, 0.014155806f, -0.0410081632f, 0.0344434977f, 0.0102459323f, -0.0365146324f, -0.000932206225f, 0.0710672662f, + -0.00964884274f, -0.00221918756f, 0.0244251546f, 0.0575291663f, 0.0553040355f, -0.0804556161f, 0.0807336792f, -0.0833179206f, + 0.0407857075f, -0.02971901f, 0.076847136f, -0.0834470615f, 0.0519244485f, -0.0417934321f, -0.00151402177f, -0.15377757f, + 0.145652324f, -0.0217619799f, -0.125659063f, 0.010311014f, 0.0721431002f, -0.121516079f, 0.0229200404f, -0.0905859768f, + -0.0853690282f, 0.0315206163f, 0.0530846044f, -0.104386151f, -0.0777412206f, 0.0311410092f, -0.0242997147f, 0.0710536689f, + -0.0599458255f, 0.0224779882f, -0.0764651373f, -0.0160161797f, 0.0f, 0.016137816f, -0.0779882371f, 0.0578846261f, + -0.0105021074f, 0.0497768857f, -0.0629218668f, -0.0394360498f, 0.0507961623f, -0.018422991f, 0.0566346161f, -0.00772698922f, + -0.0748444572f, -0.0112896822f, 0.0439815894f, -0.0180538278f, 0.0555960275f, 0.0200266857f, -0.059025608f, -0.051404044f, + -0.00785764121f, 0.044324372f, 0.0f, 0.0203549154f, -0.0937152579f, 0.016323518f, -0.0461964719f, 0.00748390704f, + 0.0099849198f, -0.0866018906f, 0.00036300841f, 0.031040214f, -0.045912452f, 0.00340024778f, 0.0264116563f, -0.00840165f, + -0.0818293691f, -0.0868645608f, -0.0736514851f, -0.0599458255f, -0.15754354f, -0.0809542984f, 0.0549478307f, -0.172217339f, + 0.0671939924f, -0.0565627851f, 0.00907990243f, -0.155633926f, 0.0649065003f, -0.0716813579f, 0.0302743725f, -0.0158908814f, + 0.704220295f, 1.09487367f, 0.208552673f, 0.706096768f, 0.122132368f, 0.915762305f, 0.358030409f, 0.0374837928f, + 0.693629563f, 0.184605107f, -0.00709123444f, 0.828318894f, 0.320717663f, 0.196761206f, 0.00840260368f, -0.0130651109f, + -0.125395924f, -0.0455287844f, -0.0202821344f, -0.203194708f, 0.02878277f, 0.0f, -0.165674269f, -0.141658992f, + 0.042547036f, 0.0210559927f, -0.118124738f, -0.181637511f, -0.0818542689f, 0.0567569099f, 0.0327538289f, -0.0891854689f, + -0.162847593f, -0.139821172f, -0.0503618345f, 0.0647253171f, 0.0287345536f, -0.0903743282f, -0.103578396f, -0.169840291f, + -0.0754184574f, -0.0524862036f, 0.0618928745f, 0.0f, -0.0414998606f, 0.113086142f, -0.0523509979f, 0.0890031978f, + -0.0663246438f, 0.0524148606f, 0.115401499f, -0.0759791061f, 0.0342834443f, 0.0945489779f, -0.070321165f, 0.0398522019f, + 0.0484587774f, 0.111803986f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.07001293f, 0.0159779917f, 0.972711325f, 0.26213187f, 0.908521593f, -0.00379784219f, 0.356212884f, + 0.815430462f, 0.240949571f, 0.338399172f, 0.746121466f, -0.012894203f, 0.195461631f, 0.319672465f, 0.772469282f, + 0.114888079f, 0.244070247f, 0.0272408407f, 0.0521857217f, 0.441699147f, -0.102313913f, 0.0f, 0.399137944f, + 0.214693084f, -0.12298917f, -0.0628200322f, 0.321973115f, 0.364080578f, 0.0815507844f, -0.142943427f, -0.104283564f, + 0.257773817f, 0.384328038f, 0.211762577f, 0.0275186319f, -0.153316602f, -0.0885735452f, 0.25054428f, 0.2255961f, + 0.326239705f, 0.0759063587f, 0.0315083303f, -0.149699599f, 0.0f, 0.0629908741f, -0.179830566f, 0.0794664174f, + -0.149200112f, 0.10045851f, -0.100369096f, -0.184574783f, 0.114760645f, -0.0758999065f, -0.163952455f, 0.106373966f, + -0.0813786536f, -0.0869862214f, -0.177205622f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, -0.298132032f, -0.0409807786f, -0.146753982f, -0.16170758f, -0.105632558f, -0.0322165005f, + -0.179538548f, -0.0605623499f, -0.153791294f, -0.149794176f, -0.035010539f, -0.0279518943f, -0.130370915f, -0.149086967f, + -0.0440064855f, 1.16531885f, 0.155903116f, 1.18803382f, 0.0218112301f, 0.441699147f, 0.989723206f, 0.0f, + 0.214693084f, 0.399137944f, 0.937964976f, -0.0212091338f, 0.0815507844f, 0.364080578f, 0.321973115f, 0.874893963f, + -0.032410875f, 0.0275186319f, 0.211762577f, 0.384328038f, 0.257773817f, 0.832350254f, -0.0285255015f, 0.0315083303f, + 0.0759063587f, 0.326239705f, 0.2255961f, 0.25054428f, 0.847757876f, 0.0f, -0.123843387f, 0.357752383f, + -0.166795522f, 0.320787907f, -0.232866481f, 0.232148886f, 0.37436986f, -0.289016604f, 0.1846506f, 0.389989406f, + -0.254294962f, 0.185414791f, 0.171074092f, 0.3485654f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.162128717f, -0.0409807786f, 0.0583499782f, 0.0950700045f, 0.0310710669f, + -0.0322165005f, 0.112997331f, 0.00141776435f, 0.0902922079f, 0.087241739f, -0.0154405357f, -0.0279518943f, 0.0888380855f, + 0.0890449658f, -0.00948492996f, 0.104501873f, -0.0895829052f, 0.0272408407f, -0.0141824139f, -0.203194708f, 0.136102572f, + 0.0f, -0.141658992f, -0.165674269f, 0.157454148f, 0.0147253973f, -0.0818542689f, -0.181637511f, -0.118124738f, + 0.179527253f, 0.023066787f, -0.0503618345f, -0.139821172f, -0.162847593f, -0.0891854689f, 0.191527873f, 0.0201097317f, + -0.0524862036f, -0.0754184574f, -0.169840291f, -0.103578396f, -0.0903743282f, 0.187201604f, 0.0f, -0.120963819f, + 0.357752383f, -0.149877861f, 0.196296439f, -0.185765192f, 0.062523149f, 0.37436986f, -0.209632516f, 0.0178464595f, + 0.212942481f, -0.19569543f, 0.0303968824f, 0.0542614087f, 0.3485654f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, -0.29321155f, 0.0159779917f, -0.138143972f, -0.188942865f, + -0.0993492082f, -0.00379784219f, -0.179760471f, -0.0570687652f, -0.182338327f, -0.127412245f, -0.0325699598f, -0.012894203f, + -0.149023175f, -0.130768359f, -0.041299358f, 0.000643067586f, 0.0700838715f, -0.0455287844f, 0.0108055621f, 0.124451198f, + -0.104888268f, 0.0f, 0.0951217487f, 0.0951217487f, -0.115680546f, -0.0110183544f, 0.0586574934f, 0.113968179f, + 0.0586574934f, -0.126555473f, -0.0170631353f, 0.0381399356f, 0.0941646397f, 0.0941646397f, 0.0381399356f, -0.132354185f, + -0.0149815697f, 0.0400710925f, 0.0567575656f, 0.107884102f, 0.0567575656f, 0.0400710925f, -0.130281866f, 0.0f, + 0.0591389909f, -0.179830566f, 0.0748410523f, -0.13067317f, 0.0955092534f, -0.0689531043f, -0.184574783f, 0.110315956f, + -0.0414837413f, -0.140548959f, 0.101519629f, -0.0491492897f, -0.061672125f, -0.177205622f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.00611859141f, 0.0f, 0.00611859141f, + 0.0f, 0.00611859141f, 0.0f, 0.0f, 0.00611859141f, 0.0f, 0.0f, 0.00611859141f, + 0.0f, 0.0f, 0.0f, 0.00611859141f, 0.606383383f, 0.369040102f, 0.375158668f, 0.0f, + 0.611859143f, 0.0f, 0.0f, 0.0f, 0.611859143f, 0.0f, 0.0f, 0.0f, + 0.611859143f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.611859143f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.611859143f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.612501979f, + 0.375158668f, 0.375158668f, 0.0f, 0.612501979f, 0.0f, 0.0f, 0.375158668f, 0.375158668f, + 0.0f, 0.0f, 0.0f, 0.612501979f, 0.0f, 0.0f, +}; + +struct Config { + const int* counts; + const int* primary_row_map; + const int* secondary_row_map; + const float* matrix; +}; + +inline constexpr Config kConfigs[kConfigCount] = { + {kConfig0Counts, kConfig0PrimaryRowMap, kConfig0SecondaryRowMap, kConfig0Matrix}, + {kConfig1Counts, kConfig1PrimaryRowMap, kConfig1SecondaryRowMap, kConfig1Matrix}, + {kConfig2Counts, kConfig2PrimaryRowMap, kConfig2SecondaryRowMap, kConfig2Matrix}, + {kConfig3Counts, kConfig3PrimaryRowMap, kConfig3SecondaryRowMap, kConfig3Matrix}, + {kConfig4Counts, kConfig4PrimaryRowMap, kConfig4SecondaryRowMap, kConfig4Matrix}, + {kConfig5Counts, kConfig5PrimaryRowMap, kConfig5SecondaryRowMap, kConfig5Matrix}, +}; + +// --------------------------------------------------------- ISF layouts -- +// output_ids maps the padded matrix columns to output channels; output_count +// is the number of real channels (the rest of the table is zero padding). +// selectors = {floor_a, floor_b, height, additional}; compensation_value is +// the automatic per-layout compensation in dB. + +// layout "20": 2 outputs, compensation -4.5 +inline constexpr std::uint8_t kLayout20OutputIds[37] = { + 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "31": 4 outputs, compensation -4.5 +inline constexpr std::uint8_t kLayout31OutputIds[37] = { + 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "40": 4 outputs, compensation -3.0 +inline constexpr std::uint8_t kLayout40OutputIds[37] = { + 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "51": 6 outputs, compensation -3.0 +inline constexpr std::uint8_t kLayout51OutputIds[37] = { + 2, 0, 1, 4, 5, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "71": 8 outputs, compensation -1.5 +inline constexpr std::uint8_t kLayout71OutputIds[37] = { + 2, 0, 1, 4, 5, 6, 7, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "512": 8 outputs, compensation -2.25 +inline constexpr std::uint8_t kLayout512OutputIds[37] = { + 2, 0, 1, 4, 5, 6, 7, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "514": 10 outputs, compensation -1.5 +inline constexpr std::uint8_t kLayout514OutputIds[37] = { + 2, 0, 1, 4, 5, 6, 7, 8, 9, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "712": 10 outputs, compensation -0.75 +inline constexpr std::uint8_t kLayout712OutputIds[37] = { + 2, 0, 1, 4, 5, 6, 7, 8, 9, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "714": 12 outputs, compensation -0.0 +inline constexpr std::uint8_t kLayout714OutputIds[37] = { + 2, 0, 1, 4, 5, 6, 7, 8, 9, 10, 11, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "914": 14 outputs, compensation -0.0 +inline constexpr std::uint8_t kLayout914OutputIds[37] = { + 2, 0, 1, 8, 9, 4, 5, 6, 7, 10, 11, 12, + 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "916": 16 outputs, compensation -0.0 +inline constexpr std::uint8_t kLayout916OutputIds[37] = { + 2, 0, 1, 8, 9, 4, 5, 6, 7, 10, 11, 12, + 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +// layout "60": 6 outputs, compensation -1.5 +inline constexpr std::uint8_t kLayout60OutputIds[37] = { + 0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, +}; + +struct Layout { + const char* key; + int output_count; + int selectors[4]; + double compensation_value; + const std::uint8_t* output_ids; +}; + +inline constexpr Layout kLayouts[kLayoutCount] = { + {"20", 2, {0, 0, 0, 0}, -4.5, kLayout20OutputIds}, + {"31", 4, {1, 0, 0, 0}, -4.5, kLayout31OutputIds}, + {"40", 4, {0, 1, 0, 0}, -3.0, kLayout40OutputIds}, + {"51", 6, {1, 1, 0, 0}, -3.0, kLayout51OutputIds}, + {"71", 8, {1, 2, 0, 0}, -1.5, kLayout71OutputIds}, + {"512", 8, {1, 1, 0, 1}, -2.25, kLayout512OutputIds}, + {"514", 10, {1, 1, 0, 2}, -1.5, kLayout514OutputIds}, + {"712", 10, {1, 2, 0, 1}, -0.75, kLayout712OutputIds}, + {"714", 12, {1, 2, 0, 2}, -0.0, kLayout714OutputIds}, + {"914", 14, {1, 3, 0, 2}, -0.0, kLayout914OutputIds}, + {"916", 16, {1, 3, 0, 3}, -0.0, kLayout916OutputIds}, + {"60", 6, {0, 2, 0, 0}, -1.5, kLayout60OutputIds}, +}; + +// ------------------------------------------------------ layout groups -- +// One record per layout group entry: selector id and the half-open matrix +// column range [begin, end). + +struct GroupRecord { + int selector; + int begin; + int end; +}; + +// floor_a: 6 records +inline constexpr GroupRecord kFloorAGroups[6] = { + {0, 0, 1}, + {1, 1, 3}, + {2, 3, 5}, + {3, 5, 8}, + {4, 8, 11}, + {5, 11, 15}, +}; + +// floor_b: 7 records +inline constexpr GroupRecord kFloorBGroups[7] = { + {0, 15, 16}, + {1, 16, 18}, + {2, 18, 21}, + {3, 21, 25}, + {4, 25, 30}, + {5, 30, 36}, + {6, 36, 43}, +}; + +// height: 6 records +inline constexpr GroupRecord kHeightGroups[6] = { + {0, 43, 44}, + {1, 44, 46}, + {2, 46, 48}, + {3, 48, 51}, + {4, 51, 54}, + {5, 54, 58}, +}; + +// additional: 5 records +inline constexpr GroupRecord kAdditionalGroups[5] = { + {1, 58, 59}, + {2, 59, 61}, + {3, 61, 64}, + {4, 64, 68}, + {5, 68, 73}, +}; + +struct LayoutGroups { + const GroupRecord* floor_a; + int floor_a_count; + const GroupRecord* floor_b; + int floor_b_count; + const GroupRecord* height; + int height_count; + const GroupRecord* additional; + int additional_count; +}; + +inline constexpr LayoutGroups kLayoutGroups = { + kFloorAGroups, 6, + kFloorBGroups, 7, + kHeightGroups, 6, + kAdditionalGroups, 5, +}; + +// ------------------------------------------------------ paired columns -- +// One flag per matrix column: whether the column also contributes through +// the secondary row map. + +inline constexpr std::uint8_t kPairedColumns[73] = { + 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, + 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, +}; + +} // namespace joc::oamd::isf_tables diff --git a/src/oamd/oamd_math.cpp b/src/oamd/oamd_math.cpp new file mode 100644 index 0000000..e771f99 --- /dev/null +++ b/src/oamd/oamd_math.cpp @@ -0,0 +1,1264 @@ +#include "oamd/oamd_math.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "joc_bitstream/bed_layout_map.h" + +namespace joc::oamd { + +namespace { + +// 2**-126, the reference's TINY. +constexpr float kTiny = 1.17549435e-38f; +// The float32 constants of the reference equations, written as the reference +// writes them so that the rounding matches. +constexpr float kQuarter = 0.25f; +constexpr float kHalf = 0.5f; +constexpr float kPanAngle = 0.78539819f; +constexpr float kLayerAngle = 0.39269909f; +constexpr float kBoundaryAngle = 0.34147748f; +constexpr float kBoundaryPower = 26.575424f; +constexpr float kBoundaryShape = 0.6328125f; +constexpr float kSizeStep = 0.036842104f; +constexpr float kSizeCurveScale = 0.71428573f; +constexpr float kQ15Scale = 32768.0f; +constexpr float kLogScale = 4.0793457f; +constexpr float kLogOffset = 2.6931152f; +constexpr float kLogSquare = 1.3862305f; +constexpr float kExpFactor = -0.1660964f; +constexpr std::size_t kMaxPoints = ::ejoc::speaker_tables::kMaxPoints; +constexpr std::size_t kMaxGroups = ::ejoc::speaker_tables::kMaxGroups; + +inline float max_float(const float left, const float right) { + return left > right ? left : right; +} + +inline float min_float(const float left, const float right) { + return left < right ? left : right; +} + +} // namespace + +float exp2_approx(const float value) { + const float x = value; + const int n = static_cast(std::floor(static_cast(x))); + const float f = x - static_cast(n); + float poly = f * 0.6862793f; + poly = poly + 1.0f; + poly = poly + (f * f) * 0.25482178f; + poly = poly + (f * (f * f)) * 0.058898926f; + return static_cast(std::ldexp(static_cast(poly), n)); +} + +float pow_approx(const float value, const float power) { + const float x = value; + if (std::fabs(x) < kTiny) { + return 0.0f; + } + int exponent = 0; + const double fraction = std::frexp(static_cast(x), &exponent); + const float fraction32 = static_cast(fraction); + float logarithm = fraction32 * kLogScale; + logarithm = logarithm - kLogOffset; + logarithm = logarithm - (fraction32 * fraction32) * kLogSquare; + logarithm = logarithm + static_cast(exponent); + return exp2_approx(logarithm * power); +} + +std::vector norm(const std::vector& vector) { + float total = 0.0f; + for (const float value : vector) { + total = total + value * value; + } + if (std::fabs(total) < kTiny) { + total = 0.0f; + } + const float root = static_cast(std::sqrt(static_cast(total))); + const float denominator = max_float(root, 1e-5f); + std::vector result(vector.size()); + for (std::size_t index = 0; index < vector.size(); ++index) { + float value = vector[index] / denominator; + if (std::fabs(value) < kTiny) { + value = 0.0f; + } + result[index] = value; + } + return result; +} + +float boundary(const int axis, const int edge, const float position, const float t) { + const float edge32 = static_cast(edge); + const float position32 = position; + const float t32 = t; + const float delta = (edge32 - position32) * kQuarter; + if (t32 < 0.000025f || std::fabs(delta) > t32) { + return 0.0f; + } + const float ratio = static_cast(static_cast(delta) / + static_cast(t32)); + const float ratio2 = ratio * ratio; + float weight = exp2_approx((-(ratio2 * ratio2) * kBoundaryShape) * kBoundaryPower); + int exponent = 0; + std::frexp(static_cast(weight), &exponent); + if (exponent < -31 || exponent > 1) { + weight = 0.0f; + } + if (axis == 2) { + const float angle = (edge32 * kBoundaryAngle) * 4.0f; + weight = std::cos(angle) * weight; + } + return weight; +} + +namespace { + +struct PanVector { + std::vector values; +}; + +struct RegionCoordinates { + std::array, kMaxPoints> values{}; + int count = 0; +}; + +RegionCoordinates region_coordinates(const ::ejoc::speaker_tables::RegionGeometry& region) { + RegionCoordinates coordinates; + coordinates.count = region.point_count; + for (int index = 0; index < region.point_count; ++index) { + for (int component = 0; component < 3; ++component) { + coordinates.values[static_cast(index)][static_cast( + component)] = static_cast(region.points[static_cast(index)] + .coordinate_q15[static_cast( + component)]) / + kQ15Scale; + } + } + return coordinates; +} + +using GroupList = std::vector>; + +GroupList collect_groups(const std::array<::ejoc::speaker_tables::AxisGroup, kMaxGroups>& groups, + const int count) { + GroupList result; + result.reserve(static_cast(count)); + for (int row = 0; row < count; ++row) { + const auto& group = groups[static_cast(row)]; + std::vector indices; + indices.reserve(group.size); + for (std::size_t index = 0; index < group.size; ++index) { + indices.push_back(static_cast(group.indices[index])); + } + result.push_back(std::move(indices)); + } + return result; +} + +int group_total(const GroupList& groups) { + int total = 0; + for (const auto& group : groups) { + total += static_cast(group.size()); + } + return total; +} + +void group_pan_x(const RegionCoordinates& coordinates, const GroupList& groups, + const float position, std::vector& output) { + for (const auto& group : groups) { + if (group.empty()) { + continue; + } + const std::size_t first = static_cast(group.front()); + const std::size_t last = static_cast(group.back()); + const float x_first = coordinates.values[first][0]; + const float x_last = coordinates.values[last][0]; + if (position < x_first) { + output[first] = 1.0f; + continue; + } + if (position > x_last) { + output[last] = 1.0f; + continue; + } + for (std::size_t index = 0; index + 1 < group.size(); ++index) { + const std::size_t left = static_cast(group[index]); + const std::size_t right = static_cast(group[index + 1]); + const float x_left = coordinates.values[left][0]; + const float x_right = coordinates.values[right][0]; + if (!(x_left < x_right && x_left <= position && position <= x_right)) { + continue; + } + const float fraction = (position - x_left) / (x_right - x_left); + if (fraction <= kHalf) { + const float angle = ((fraction * kPanAngle) * kHalf) * 4.0f; + output[left] = std::cos(angle); + output[right] = std::sin(angle); + } else { + const float angle = (((1.0f - fraction) * kPanAngle) * kHalf) * 4.0f; + output[left] = std::sin(angle); + output[right] = std::cos(angle); + } + break; + } + } +} + +void group_pan_y(const RegionCoordinates& coordinates, const GroupList& groups, + const float position, std::vector& output) { + if (groups.empty()) { + return; + } + std::vector rows; + rows.reserve(groups.size()); + for (const auto& group : groups) { + rows.push_back(coordinates.values[static_cast(group.front())][1]); + } + if (position <= rows.front()) { + for (const int index : groups.front()) { + output[static_cast(index)] = 1.0f; + } + return; + } + if (position > rows.back()) { + for (const int index : groups.back()) { + output[static_cast(index)] = 1.0f; + } + return; + } + for (std::size_t row = 0; row + 1 < groups.size(); ++row) { + const float y_low = rows[row]; + const float y_high = rows[row + 1]; + if (!(y_low <= position && position <= y_high && y_low < y_high)) { + continue; + } + const float fraction = (position - y_low) / (y_high - y_low); + float low_gain = 0.0f; + float high_gain = 0.0f; + if (fraction <= kHalf) { + const float angle = ((fraction * kPanAngle) * kHalf) * 4.0f; + low_gain = std::cos(angle); + high_gain = std::sin(angle); + } else { + const float angle = (((1.0f - fraction) * kPanAngle) * kHalf) * 4.0f; + low_gain = std::sin(angle); + high_gain = std::cos(angle); + } + for (const int index : groups[row]) { + output[static_cast(index)] = low_gain; + } + for (const int index : groups[row + 1]) { + output[static_cast(index)] = high_gain; + } + return; + } +} + +std::vector layer_pan_z(const RegionCoordinates& coordinates, const float position) { + float floor_gain = 0.0f; + float height_gain = 0.0f; + if (position <= 0.0f) { + floor_gain = 1.0f; + height_gain = 0.0f; + } else if (position < 0.99996948f) { + const float angle = (position * kLayerAngle) * 4.0f; + floor_gain = std::cos(angle); + height_gain = std::sin(angle); + } else { + floor_gain = 0.0f; + height_gain = 1.0f; + } + std::vector output(static_cast(coordinates.count), 0.0f); + for (int index = 0; index < coordinates.count; ++index) { + output[static_cast(index)] = + coordinates.values[static_cast(index)][2] >= kHalf ? height_gain + : floor_gain; + } + return output; +} + +std::vector pan_x_all(const RegionCoordinates& coordinates, const GroupList& floor_groups, + const GroupList& height_groups, const float position, + const int mode) { + std::vector output(static_cast(coordinates.count), 0.0f); + group_pan_x(coordinates, floor_groups, position, output); + if (mode == 3 && !height_groups.empty()) { + std::vector extra(static_cast(coordinates.count), 0.0f); + group_pan_x(coordinates, height_groups, position, extra); + for (std::size_t index = 0; index < output.size(); ++index) { + output[index] = output[index] + extra[index]; + } + } + return output; +} + +std::vector pan_y_all(const RegionCoordinates& coordinates, const GroupList& floor_groups, + const GroupList& height_groups, const float position, + const int mode) { + std::vector output(static_cast(coordinates.count), 0.0f); + group_pan_y(coordinates, floor_groups, position, output); + if (mode == 3 && !height_groups.empty()) { + std::vector extra(static_cast(coordinates.count), 0.0f); + group_pan_y(coordinates, height_groups, position, extra); + for (std::size_t index = 0; index < output.size(); ++index) { + output[index] = output[index] + extra[index]; + } + } + return output; +} + +using BoundaryRows = std::array, 5>; + +BoundaryRows boundary_rows(const RegionCoordinates& coordinates, const GroupList& floor_groups, + const GroupList& height_groups, const int mode) { + BoundaryRows rows; + rows[0] = pan_x_all(coordinates, floor_groups, height_groups, 0.0f, mode); + rows[1] = pan_x_all(coordinates, floor_groups, height_groups, 1.0f, mode); + rows[2] = pan_y_all(coordinates, floor_groups, height_groups, 0.0f, mode); + rows[3] = pan_y_all(coordinates, floor_groups, height_groups, 1.0f, mode); + if (mode == 3) { + rows[4] = layer_pan_z(coordinates, 1.0f); + } else { + rows[4] = std::vector(static_cast(coordinates.count), 0.0f); + } + return rows; +} + +// Per-virtual-point S448 weight used by the generated size tables. +float size_weight(const float position, const float u, const float t, const int axis) { + const float offset = ((u - position) * kQuarter); + if (t < 0.000025f || std::fabs(offset) > t) { + return 0.0f; + } + const float ratio = offset / t; + const float squared = ratio * ratio; + const float power = (-(squared * squared)) * kBoundaryShape; + float weight = exp2_approx(power * kBoundaryPower); + if (axis == 2) { + const float angle = (u * kBoundaryAngle) * 4.0f; + weight = std::cos(angle) * weight; + } + return weight; +} + +float size_m(const float t) { + return t <= 0.125f ? 6.0f : (6.869565f - t * 6.956522f); +} + +struct AxisTable { + int position_nodes = 0; + int size_nodes = 0; + int floor_stride = 0; + int height_stride = 0; + std::vector floor_values; + std::vector height_values; +}; + +AxisTable axis_table(const std::vector& pan_vectors, const int position_nodes, + const int speaker_count, const int offset_start, const int axis) { + AxisTable table; + table.position_nodes = position_nodes; + table.size_nodes = 20; + table.floor_stride = 0; + table.height_stride = 0; + table.floor_values.assign( + static_cast(position_nodes) * 20u * + static_cast(speaker_count), + 0.0f); + const float denominator = static_cast(pan_vectors.size() - 1u); + for (int position_index = 0; position_index < position_nodes; ++position_index) { + const float position = static_cast(position_index) / + static_cast(position_nodes - 1); + for (int size_index = 0; size_index < 20; ++size_index) { + const float t = static_cast(size_index) * kSizeStep; + const float m = size_m(t); + for (std::size_t u = 0; u < pan_vectors.size(); ++u) { + const float weight = + size_weight(position, static_cast(u) / denominator, t, axis); + if (weight == 0.0f) { + continue; + } + for (int speaker = 0; speaker < speaker_count; ++speaker) { + const float value = + pan_vectors[u].values[static_cast(offset_start + speaker)] * + weight; + const std::size_t index = + (static_cast(position_index) * 20u + + static_cast(size_index)) * + static_cast(speaker_count) + + static_cast(speaker); + table.floor_values[index] = + table.floor_values[index] + pow_approx(value, m); + } + } + } + } + return table; +} + +AxisTable axis2_table(const std::vector& pan_vectors, const int speaker_index) { + AxisTable table; + table.position_nodes = 4; + table.size_nodes = 20; + table.floor_stride = 1; + table.height_stride = 1; + // The z axis tables are stored scalar per (position node, size node); the + // reference repeats the scalar over the group members when it consumes it. + table.floor_values.assign(4u * 20u, 0.0f); + table.height_values.assign(4u * 20u, 0.0f); + if (pan_vectors.empty() || + static_cast(speaker_index) >= pan_vectors.front().values.size()) { + return table; + } + std::vector& target = table.floor_values; + for (int position_index = 0; position_index < 4; ++position_index) { + const float position = static_cast(position_index) / 3.0f; + for (int size_index = 0; size_index < 20; ++size_index) { + const float t = static_cast(size_index) * kSizeStep; + const float m = size_m(t); + float total = 0.0f; + for (std::size_t u = 0; u < pan_vectors.size(); ++u) { + const float weight = size_weight(position, static_cast(u) / 7.0f, t, 2); + if (weight == 0.0f) { + continue; + } + const float value = + pan_vectors[u].values[static_cast(speaker_index)] * weight; + total = total + pow_approx(value, m); + } + target[static_cast(position_index) * 20u + + static_cast(size_index)] = total; + } + } + return table; +} + +struct RegionSizeTables { + int floor_count = 0; + int height_count = 0; + int mode = 0; + int floor_axis_group_count = 0; + int height_axis_group_count = 0; + BoundaryRows boundary_3d; + BoundaryRows boundary_2d; + AxisTable axes[3]; +}; + +RegionSizeTables build_region_size_tables(const ::ejoc::speaker_tables::RegionGeometry& region) { + RegionSizeTables entry; + const RegionCoordinates coordinates = region_coordinates(region); + const GroupList floor_groups = collect_groups(region.axis0_groups, + region.axis0_group_count); + const GroupList height_groups = + region.mode == 3 ? collect_groups(region.axis1_groups, region.axis1_group_count) + : GroupList{}; + entry.floor_count = group_total(floor_groups); + entry.height_count = group_total(height_groups); + entry.mode = region.mode; + entry.floor_axis_group_count = static_cast(floor_groups.size()); + entry.height_axis_group_count = static_cast(height_groups.size()); + + std::vector x_vectors; + std::vector y_vectors; + std::vector z_vectors; + x_vectors.reserve(20); + y_vectors.reserve(20); + z_vectors.reserve(8); + for (int index = 0; index < 20; ++index) { + const float position = static_cast(index) / 19.0f; + x_vectors.push_back(PanVector{pan_x_all(coordinates, floor_groups, height_groups, + position, region.mode)}); + y_vectors.push_back(PanVector{pan_y_all(coordinates, floor_groups, height_groups, + position, region.mode)}); + } + if (region.mode == 3) { + for (int index = 0; index < 8; ++index) { + z_vectors.push_back( + PanVector{layer_pan_z(coordinates, static_cast(index) / 7.0f)}); + } + } else { + for (int index = 0; index < 8; ++index) { + z_vectors.push_back(PanVector{ + std::vector(static_cast(coordinates.count), 0.0f)}); + } + } + + entry.axes[0] = axis_table(x_vectors, 35, entry.floor_count, 0, 0); + entry.axes[0].height_values = + axis_table(x_vectors, 35, entry.height_count, entry.floor_count, 0).floor_values; + entry.axes[0].height_stride = entry.height_count; + entry.axes[0].floor_stride = entry.floor_count; + + entry.axes[1] = axis_table(y_vectors, 35, entry.floor_count, 0, 1); + entry.axes[1].height_values = + axis_table(y_vectors, 35, entry.height_count, entry.floor_count, 1).floor_values; + entry.axes[1].height_stride = entry.height_count; + entry.axes[1].floor_stride = entry.floor_count; + + if (region.mode == 3) { + entry.axes[2] = axis2_table(z_vectors, 0); + entry.axes[2].height_values = axis2_table(z_vectors, entry.floor_count).floor_values; + } else { + entry.axes[2] = AxisTable{}; + entry.axes[2].position_nodes = 4; + entry.axes[2].size_nodes = 20; + entry.axes[2].floor_stride = 1; + entry.axes[2].height_stride = 1; + } + + entry.boundary_3d = boundary_rows(coordinates, floor_groups, height_groups, region.mode); + entry.boundary_2d = boundary_rows(coordinates, floor_groups, GroupList{}, 2); + return entry; +} + +const RegionSizeTables& region_size_tables(const ::ejoc::speaker_tables::RegionGeometry& region) { + static std::unordered_map cache; + const auto found = cache.find(®ion); + if (found != cache.end()) { + return found->second; + } + return cache.emplace(®ion, build_region_size_tables(region)).first->second; +} + +void bracket(const float position, const int axis, int* low, int* high, float* alpha) { + const int last = axis == 2 ? 3 : 34; + const float step = 1.0f / static_cast(last); + int low_value = static_cast( + std::floor(static_cast(static_cast(last) * position))); + int high_value = std::min(low_value + 1, last); + if (low_value > last) { + low_value = last; + high_value = last; + } + *low = low_value; + *high = high_value; + if (low_value == high_value) { + *alpha = 0.0f; + } else { + const float position32 = position; + const float remainder = position32 - + (static_cast(low_value) * step); + *alpha = max_float(0.0f, remainder / step); + } +} + +void size_bracket(const float t, int* low, int* high, float* beta) { + const float step = kSizeStep; + int low_value = static_cast(std::floor(static_cast( + ((t * kSizeCurveScale) * 2.0f) * 19.0f))); + if (low_value > 19) { + *low = 19; + *high = 19; + *beta = 0.0f; + return; + } + const int high_value = std::min(low_value + 1, 19); + *low = low_value; + *high = high_value; + if (low_value == high_value) { + *beta = 0.0f; + } else { + const float remainder = t - (static_cast(low_value) * step); + *beta = max_float( + 0.0f, static_cast(static_cast(remainder) / + static_cast(step))); + } +} + +std::vector axis_weights(const RegionSizeTables& entry, const int axis, + const float position, const float t, const bool height) { + int low = 0; + int high = 0; + float alpha = 0.0f; + bracket(position, axis, &low, &high, &alpha); + int low_t = 0; + int high_t = 0; + float beta = 0.0f; + size_bracket(t, &low_t, &high_t, &beta); + const float weight0 = 1.0f - max_float(beta, alpha); + const float weight1 = 1.0f - max_float(beta, 1.0f - alpha); + const float weight2 = 1.0f - max_float(1.0f - beta, alpha); + const float weight3 = 1.0f - max_float(1.0f - beta, 1.0f - alpha); + const float total = ((weight1 + weight0) + weight2) + weight3; + const float scales[4] = {weight0 / total, weight1 / total, weight2 / total, + weight3 / total}; + + const AxisTable& table = entry.axes[axis]; + std::vector result; + result.reserve(static_cast(entry.floor_count + + (height ? entry.height_count : 0))); + const auto append_group = [&](const std::vector& values, const int count, + const int stride) { + if (count == 0) { + return; + } + const auto index = [&](const int position_index, const int size_index, + const int speaker) { + const int column = stride == 1 ? 0 : speaker; + return (static_cast(position_index) * 20u + + static_cast(size_index)) * + static_cast(stride) + + static_cast(column); + }; + for (int speaker = 0; speaker < count; ++speaker) { + const float value = + ((scales[0] * values[index(low, low_t, speaker)] + + scales[1] * values[index(high, low_t, speaker)]) + + scales[2] * values[index(low, high_t, speaker)]) + + scales[3] * values[index(high, high_t, speaker)]; + result.push_back(value); + } + }; + append_group(table.floor_values, entry.floor_count, table.floor_stride); + if (height && entry.height_count > 0) { + append_group(table.height_values, entry.height_count, table.height_stride); + } + return result; +} + +float size_curve(const double size_value) { + static constexpr std::array, 5> kNodes{{ + {0.0f, 0.0f}, {0.2f, 0.075f}, {0.5f, 0.25f}, {0.75f, 0.45f}, {1.0f, 0.7f}}}; + const float size = static_cast(size_value); + if (size <= kNodes.front()[0]) { + return 0.0f; + } + if (size >= kNodes.back()[0]) { + return 1.0f; + } + for (std::size_t index = 0; index + 1 < kNodes.size(); ++index) { + const float size_low = kNodes[index][0]; + const float size_high = kNodes[index + 1][0]; + if (size <= size_high) { + const float span = size_high - size_low; + const float fraction = span != 0.0f ? (size - size_low) / span : 0.0f; + const float t_low = kNodes[index][1]; + const float t_high = kNodes[index + 1][1]; + return t_low + fraction * (t_high - t_low); + } + } + return 1.0f; +} + +} // namespace + +float size_curve_value(const double size) { + return size_curve(size); +} + +std::vector size_kernel(const ::ejoc::speaker_tables::RegionGeometry& region, + const float position[3], const float t, const bool height) { + const RegionSizeTables& entry = region_size_tables(region); + const int floor_count = entry.floor_count; + const int height_count = height ? entry.height_count : 0; + const int total = floor_count + height_count; + if (total == 0) { + return std::vector(); + } + const int dim = height ? entry.mode + : (entry.floor_axis_group_count == 1 ? 1 : 2); + const float m = size_m(t); + const float inverse_m = 1.0f / m; + std::array position32{position[0], position[1], position[2]}; + std::vector axes[3]; + for (int axis = 0; axis < dim; ++axis) { + axes[axis] = axis_weights(entry, axis, position32[static_cast(axis)], t, + height); + } + + const BoundaryRows& source = dim == 3 ? entry.boundary_3d : entry.boundary_2d; + // The reference reorders the five boundary rows to z1, x0, x1, y0, y1 and + // keeps the first ``total`` columns. + std::array, 5> boundary_table; + constexpr std::array kOrder{4, 0, 1, 2, 3}; + for (std::size_t row = 0; row < 5; ++row) { + const std::vector& line = source[static_cast(kOrder[row])]; + boundary_table[row].assign(line.begin(), + line.begin() + std::min( + line.size(), + static_cast(total))); + boundary_table[row].resize(static_cast(total), 0.0f); + } + constexpr std::array, 5> kRowAxes{{ + {2, 1}, {0, 0}, {0, 1}, {1, 0}, {1, 1}}}; + for (std::size_t row = 0; row < 5; ++row) { + const int axis = kRowAxes[row][0]; + const int edge = kRowAxes[row][1]; + const float weight = boundary(axis, edge, position32[static_cast(axis)], t); + for (float& value : boundary_table[row]) { + value = value * weight; + } + } + + std::vector inside = axes[0]; + if (dim > 1) { + for (std::size_t index = 0; index < inside.size(); ++index) { + inside[index] = inside[index] * axes[1][index]; + } + } + if (dim > 2) { + for (std::size_t index = 0; index < inside.size(); ++index) { + inside[index] = inside[index] * axes[2][index]; + } + } + inside = norm(inside); + if (dim == 1) { + std::vector result(inside.size()); + for (std::size_t index = 0; index < inside.size(); ++index) { + result[index] = pow_approx(inside[index], inverse_m); + } + return result; + } + + std::array, 5> powered; + for (std::size_t row = 0; row < 5; ++row) { + powered[row].resize(boundary_table[row].size()); + for (std::size_t index = 0; index < boundary_table[row].size(); ++index) { + powered[row][index] = pow_approx(boundary_table[row][index], m); + } + } + std::vector edge(static_cast(total), 0.0f); + if (dim == 2) { + const std::vector& x = axes[0]; + const std::vector& y = axes[1]; + for (std::size_t index = 0; index < edge.size(); ++index) { + edge[index] = ((powered[1][index] * y[index] + powered[2][index] * y[index]) + + powered[3][index] * x[index]) + + powered[4][index] * x[index]; + } + } else { + const std::vector& x = axes[0]; + const std::vector& y = axes[1]; + const std::vector& z = axes[2]; + for (std::size_t index = 0; index < edge.size(); ++index) { + const float xy = x[index] * y[index]; + const float yz = y[index] * z[index]; + const float xz = x[index] * z[index]; + edge[index] = ((((xy * powered[0][index] + yz * powered[1][index]) + + yz * powered[2][index]) + + xz * powered[3][index]) + + xz * powered[4][index]); + } + } + edge = norm(edge); + + float distance = std::min(position32[0], 1.0f - position32[0]); + distance = std::min(distance, position32[1]); + distance = std::min(distance, 1.0f - position32[1]); + if (dim == 3) { + distance = std::min(distance, 1.0f - position32[2]); + } + const float quarter_distance = kQuarter * distance; + float mix = 0.0f; + if (t < 0.000025f || (quarter_distance > t && quarter_distance > 0.05f)) { + mix = 1.0f; + } else { + const float four_t = t * 4.0f; + const float first = static_cast(static_cast(distance) / + static_cast(four_t)); + const float scaled = distance * 5.0f; + mix = first * (scaled * scaled); + } + std::vector result(static_cast(total)); + for (std::size_t index = 0; index < result.size(); ++index) { + const float joined = ((mix * inside[index] + edge[index]) * kHalf) * 0.0625f; + result[index] = pow_approx(joined, inverse_m); + } + return result; +} + +std::vector size_targets(const MetadataParams& meta, + const ::ejoc::speaker_tables::LayoutGeometry& layout, + const int region_index, const double position[3], + const double size, const bool height) { + std::vector result(static_cast(layout.channel_count), 0.0f); + if (region_index < 0 || region_index >= static_cast(layout.regions.size())) { + return result; + } + const ::ejoc::speaker_tables::RegionGeometry& region = + layout.regions[static_cast(region_index)]; + if (region.point_count == 0) { + return result; + } + (void)meta; + const float t = size_curve_value(size); + const float position32[3] = {static_cast(position[0]), + static_cast(position[1]), + static_cast(position[2])}; + const std::vector kernel = size_kernel(region, position32, t, height); + const std::size_t count = std::min( + kernel.size(), static_cast(region.point_count)); + for (std::size_t index = 0; index < count; ++index) { + const std::size_t speaker = region.points[index].speaker_id; + if (speaker < result.size()) { + result[speaker] = kernel[index]; + } + } + return result; +} + +IsfBasis assemble_isf(const MetadataParams& meta, const char* layout_key, + const int isf_idx) { + IsfBasis basis; + if (isf_idx < 0 || static_cast(isf_idx) >= meta.configs.size()) { + return basis; + } + const IsfConfig& config = meta.configs[static_cast(isf_idx)]; + const IsfLayout* geometry = meta.layout(layout_key); + if (geometry == nullptr || geometry->output_count <= 0) { + return basis; + } + basis.rows = config.row_count(); + basis.columns = geometry->output_count; + basis.values.assign(static_cast(basis.rows) * + static_cast(basis.columns), + 0.0f); + const std::size_t output_ids = geometry->output_ids.size(); + std::size_t cursor = 0; + const auto add_group = [&](const char* name, const int selector) { + const std::vector* records = meta.layout_groups.find(name); + if (records == nullptr) { + return; + } + const IsfGroupRecord* record = nullptr; + for (const IsfGroupRecord& candidate : *records) { + if (candidate.selector == selector) { + record = &candidate; + break; + } + } + if (record == nullptr) { + return; + } + for (int column = record->begin; column < record->end; ++column) { + const bool paired = column >= 0 && static_cast(column) < + meta.paired_columns.size() && + meta.paired_columns[static_cast(column)]; + const std::vector* mappings[2] = {&config.primary_row_map, + &config.secondary_row_map}; + const int mapping_count = paired ? 2 : 1; + for (int which = 0; which < mapping_count; ++which) { + std::size_t speaker = 0; + if (cursor < output_ids) { + speaker = geometry->output_ids[cursor]; + } else if (cursor >= output_ids && output_ids > 0) { + // The reference indexes the padded 37-entry table; a cursor + // past the table is impossible for the shipped data. + speaker = 0; + } + const std::vector& mapping = *mappings[which]; + for (int row = 0; row < basis.rows; ++row) { + const int source_row = mapping[static_cast(row)]; + const std::size_t matrix_index = + static_cast(source_row) * 73u + + static_cast(column); + if (matrix_index < config.matrix.size() && + speaker < static_cast(basis.columns)) { + float& target = + basis.values[static_cast(row) * + static_cast(basis.columns) + + speaker]; + target = target + config.matrix[matrix_index]; + } + } + ++cursor; + } + } + }; + const auto& selectors = geometry->selectors; + add_group("floor_a", selectors[0]); + cursor = cursor >= 2 ? cursor - 2 : 0; + add_group("floor_b", selectors[1]); + cursor = cursor >= 2 ? cursor - 2 : 0; + add_group("height", selectors[2]); + const int counts[5] = {config.counts[0], config.counts[1], config.counts[2], + config.counts[3], config.counts[4]}; + if (selectors[3] != 0) { + const int mid = counts[0]; + const int upper = counts[1]; + const int lower = counts[2]; + const int zenith = counts[3]; + const auto clear_rows = [&](int begin, int end) { + begin = std::max(begin, 0); + end = std::min(end, basis.rows); + for (int row = begin; row < end; ++row) { + for (int column = 0; column < basis.columns; ++column) { + basis.values[static_cast(row) * + static_cast(basis.columns) + + static_cast(column)] = 0.0f; + } + } + }; + clear_rows(mid, mid + upper); + clear_rows(mid + upper + lower, mid + upper + lower + zenith); + add_group("additional", selectors[3]); + } + return basis; +} + +float isf_compensation(const MetadataParams& meta, const char* layout_key, + const int isf_idx, const int row, const bool disabled) { + if (disabled) { + return 1.0f; + } + if (isf_idx < 0 || static_cast(isf_idx) >= meta.configs.size()) { + return 1.0f; + } + const IsfConfig& config = meta.configs[static_cast(isf_idx)]; + const IsfLayout* geometry = meta.layout(layout_key); + if (geometry == nullptr) { + return 1.0f; + } + const int first = config.counts[0]; + float value = static_cast(geometry->compensation_value); + if (row < first) { + const float distance = static_cast(std::min(row, first - row)); + const float lower = max_float(static_cast(first) * 0.16666667f, 1.0f); + const float width = static_cast(first) * 0.11111111f; + float fraction = 0.0f; + if (distance <= lower) { + fraction = 0.0f; + } else if (distance > lower + width) { + fraction = 1.0f; + } else { + fraction = (distance - lower) / width; + } + value = fraction * value; + } + return exp2_approx(value * 0.1660964f); +} + +namespace { + +constexpr int kDirectBedLabels[17] = {0, 1, 2, 3, 4, 5, 6, 7, 13, 14, + 17, 18, 21, 22, 11, 12, 3}; +constexpr float kAWeight = 0.5011872053146362f; +constexpr float kBWeight = 0.7079457640647888f; +constexpr float kCWeight = 0.3548133969306946f; +constexpr float kRow537 = 0.5370317697525024f; +constexpr float kRow288 = 0.2884031534194946f; +constexpr float kRow813 = 0.8128305077552795f; +constexpr float kRow661 = 0.6606934666633606f; +constexpr float kRow437 = 0.43651583790779114f; + +int direct_bed_label(const int label) { + if (label >= 1 && label <= 17) { + return kDirectBedLabels[label - 1]; + } + return 3; +} + +struct RowEntry { + int target = 0; + float weight = 0.0f; +}; + +const std::vector* bed_rows(const int label, const std::uint32_t mask) { + static const std::vector row9{{13, 1.0f}, {15, kBWeight}, {17, kAWeight}}; + static const std::vector row10{{14, 1.0f}, {16, kBWeight}, {18, kAWeight}}; + static const std::vector row11{{13, kAWeight}, {15, kBWeight}, {17, 1.0f}, + {19, kBWeight}, {21, kAWeight}}; + static const std::vector row12{{14, kAWeight}, {16, kBWeight}, {18, 1.0f}, + {20, kBWeight}, {22, kAWeight}}; + static const std::vector row13{{17, kAWeight}, {19, kBWeight}, {21, 1.0f}}; + static const std::vector row14{{18, kAWeight}, {20, kBWeight}, {22, 1.0f}}; + static const std::vector maskC05{{4, 1.0f}, {25, kBWeight}, {27, kBWeight}}; + static const std::vector maskC06{{5, 1.0f}, {26, kBWeight}, {28, kBWeight}}; + static const std::vector maskC07{{6, 1.0f}, {8, kCWeight}, {29, kBWeight}, + {31, kBWeight}, {33, kAWeight}}; + static const std::vector maskC08{{7, 1.0f}, {8, kCWeight}, {30, kBWeight}, + {32, kBWeight}, {34, kAWeight}}; + static const std::vector mask305{{4, 1.0f}, {6, kRow537}, {8, kRow288}, + {25, kBWeight}, {27, kRow813}, {29, kRow661}, + {31, kRow437}, {33, kCWeight}}; + static const std::vector mask306{{5, 1.0f}, {7, kRow537}, {8, kRow288}, + {26, kBWeight}, {28, kRow813}, {30, kRow661}, + {32, kRow437}, {34, kCWeight}}; + if (label >= 9 && label <= 14) { + switch (label) { + case 9: return &row9; + case 10: return &row10; + case 11: return &row11; + case 12: return &row12; + case 13: return &row13; + default: return &row14; + } + } + if ((mask & 0xC0u) != 0u) { + switch (label) { + case 5: return &maskC05; + case 6: return &maskC06; + case 7: return &maskC07; + case 8: return &maskC08; + default: return nullptr; + } + } + if ((mask & 0x30u) != 0u) { + switch (label) { + case 5: return &mask305; + case 6: return &mask306; + default: return nullptr; + } + } + return nullptr; +} + +} // namespace + +float bed_weight(const int label, const std::uint32_t mask, const int target_label) { + const float value = + target_label == direct_bed_label(label) ? 1.0f : 0.0f; + if (label >= 9 && label <= 14) { + const std::vector* rows = bed_rows(label, mask); + for (const RowEntry& entry : *rows) { + if (entry.target == target_label) { + return entry.weight; + } + } + return value; + } + if ((mask & 0xC0u) == 0u && (mask & 0x30u) == 0u) { + return value; + } + if (label == 5 && (mask & 0x4000u) == 0u && target_label == 11) { + return kAWeight; + } + if (label == 6 && (mask & 0x8000u) == 0u && target_label == 12) { + return kAWeight; + } + const std::vector* rows = bed_rows(label, mask); + if (rows == nullptr) { + return value; + } + for (const RowEntry& entry : *rows) { + if (entry.target == target_label) { + return entry.weight; + } + } + return value; +} + +std::array bed_candidates(const int label, const std::uint32_t mask, + const std::vector& output_labels) { + const auto present = [&](const int value) { + return std::find(output_labels.begin(), output_labels.end(), value) != + output_labels.end() + ? 1 + : 0; + }; + std::array available{present(13), present(15), present(17), present(19), + present(21)}; + std::array remaining = available; + const bool front_bits = label == 9 || label == 11 || label == 13; + const int front_bit = front_bits ? 8 : 9; + const int middle_bit = front_bits ? 10 : 11; + const int rear_bit = front_bits ? 12 : 13; + const int front = static_cast((mask >> static_cast(front_bit)) & 1u); + const int middle = static_cast((mask >> static_cast(middle_bit)) & 1u); + const int rear = static_cast((mask >> static_cast(rear_bit)) & 1u); + if (front != 0) { + if (available[0] != 0) { + remaining[0] = 0; + } else if (available[1] != 0) { + remaining[1] = 0; + } + } + if (middle != 0 && available[2] != 0) { + remaining[2] = 0; + } + if (rear != 0) { + if (available[4] != 0) { + remaining[4] = 0; + } else if (available[3] != 0) { + remaining[3] = 0; + } + } + const int available_sum = + available[0] + available[1] + available[2] + available[3] + available[4]; + const bool special = available[2] == 0 && available_sum == 2 && front == 0 && + rear == 0 && middle != 0 && + (available[0] != 0 || available[1] != 0) && + (available[3] != 0 || available[4] != 0); + if (label == 9 || label == 10) { + if (available[0] != 0 || available[1] != 0 || remaining[2] != 0) { + return {available[0], available[1], remaining[2], 0, 0}; + } + return {0, 0, 0, 0, 0}; + } + if (label == 11 || label == 12) { + if (special) { + return {0, 0, 0, 0, 0}; + } + return {remaining[0], remaining[1], available[2], remaining[3], remaining[4]}; + } + if (label == 13 || label == 14) { + if (available[3] != 0 || available[4] != 0 || remaining[2] != 0) { + return {0, 0, remaining[2], available[3], available[4]}; + } + return {0, 0, 0, 0, 0}; + } + return {0, 0, 0, 0, 0}; +} + +BedDistribution bed_distribution(const ::joc::speaker::LayoutInfo& layout, const int label, + const std::uint32_t mask, const bool distribute) { + const ::joc::joc::BedOutputDescription description = + ::joc::joc::bed_output_description(layout); + const std::vector& labels = description.output_labels; + BedDistribution result; + result.weights.assign(labels.size(), 0.0f); + const int bed_index = label - 1; + if (bed_index < 0 || bed_index >= kBedLabelCount) { + return result; + } + const int slot = description.label_to_slot[bed_index]; + const int fallback = description.direct_output_labels[bed_index]; + const bool height = label >= 9 && label <= 14; + if (slot == -2 && (!height || !distribute)) { + result.route = fallback; + return result; + } + if (slot == -1) { + result.route = -1; + return result; + } + if (slot >= 0 && static_cast(slot) < result.weights.size()) { + result.weights[static_cast(slot)] = 1.0f; + } + if (!distribute) { + result.route = -1; + return result; + } + std::vector weights; + if (height) { + const std::array candidates = bed_candidates(label, mask, labels); + if (std::none_of(candidates.begin(), candidates.end(), + [](const int value) { return value != 0; })) { + result.route = fallback; + return result; + } + static constexpr std::array kSideOdd{13, 15, 17, 19, 21}; + static constexpr std::array kSideEven{14, 16, 18, 20, 22}; + const std::array& side_labels = (label % 2) != 0 ? kSideOdd : kSideEven; + weights.assign(labels.size(), 0.0f); + for (std::size_t index = 0; index < labels.size(); ++index) { + bool allowed = false; + for (std::size_t candidate = 0; candidate < candidates.size(); ++candidate) { + if (candidates[candidate] != 0 && side_labels[candidate] == labels[index]) { + allowed = true; + break; + } + } + weights[index] = allowed ? bed_weight(label, mask, labels[index]) : 0.0f; + } + } else { + weights.resize(labels.size()); + bool binary = true; + for (std::size_t index = 0; index < labels.size(); ++index) { + weights[index] = bed_weight(label, mask, labels[index]); + if (weights[index] != 0.0f && weights[index] != 1.0f) { + binary = false; + } + } + if (binary) { + result.route = -1; + return result; + } + } + std::size_t count = 0; + for (const float value : weights) { + if (value != 0.0f) { + ++count; + } + } + if (count == 1) { + for (float& value : weights) { + if (value != 0.0f) { + value = 1.0f; + } + } + } else if (count > 1) { + weights = norm(weights); + } else { + result.route = fallback; + return result; + } + result.route = -1; + result.weights = std::move(weights); + return result; +} + +void bed_reference_position(const int index, int q15[3]) { + q15[0] = 0; + q15[1] = 0; + q15[2] = 0; + if (index < 0 || index >= ::joc::joc::kBedReferencePositionCount) { + return; + } + const std::int32_t(*positions)[3] = ::joc::joc::bed_reference_positions_q15(); + if (positions == nullptr) { + return; + } + q15[0] = static_cast(positions[index][0]); + q15[1] = static_cast(positions[index][1]); + q15[2] = static_cast(positions[index][2]); +} + +float bed_attenuation_db(const ::joc::speaker::LayoutInfo& layout) { + return static_cast(::joc::joc::bed_output_description(layout).automatic_attenuation_db); +} + +float bed_compensation(const ::joc::speaker::LayoutInfo& layout, const int label, + const int mode, const bool bypass, const double levels[3]) { + if (label == 4 || label == 17 || bypass || mode == 1) { + return 1.0f; + } + if (label < 1 || label > kBedLabelCount) { + return 1.0f; + } + const ::joc::joc::BedOutputDescription description = + ::joc::joc::bed_output_description(layout); + const int source = description.direct_output_labels[label - 1]; + int position[3] = {0, 0, 0}; + bed_reference_position(source, position); + const float x = static_cast(static_cast(position[0]) / 32768.0); + const float y = static_cast(static_cast(position[1]) / 32768.0); + const float z = static_cast(static_cast(position[2]) / 32768.0); + float value_db = 0.0f; + if (mode == 0) { + const float y_term = y >= 0.6f ? 1.0f + : (y <= 0.0f + ? 0.0f + : static_cast(static_cast(y) / 0.6)); + const float z_term = + z >= 1.0f ? 1.0f + : (z <= 0.2f + ? 0.0f + : static_cast(static_cast(z - 0.2f) / 0.8)); + const float amount = max_float(0.0f, min_float(y_term + z_term, 1.0f)); + value_db = amount * static_cast(description.automatic_attenuation_db); + } else { + const float surround = static_cast(levels[0]); + const float height = static_cast(levels[1]); + const float centre = static_cast(levels[2]); + const float surround_factor = + y >= 0.5f ? surround + : static_cast(static_cast(y * surround) * 2.0); + float centre_position = max_float(std::fabs(z) - 0.05f, y - 0.05f); + centre_position = + max_float(centre_position, std::fabs(x - 0.5f) - 0.05f); + const float centre_factor = + 1.0f - min_float(max_float(10.0f * centre_position, 0.0f), 1.0f); + value_db = ((surround_factor * (1.0f - z)) + (z * height)) + + (centre_factor * centre); + } + return exp2_approx(value_db * 0.1660964f); +} + +} // namespace joc::oamd diff --git a/src/oamd/oamd_math.h b/src/oamd/oamd_math.h new file mode 100644 index 0000000..e8dac19 --- /dev/null +++ b/src/oamd/oamd_math.h @@ -0,0 +1,101 @@ +// Port of src/oamd_math.py: float32 equations for the metadata application path. +// +// The routines implement the fixed float32 equations for the size tables, ISF +// basis assembly, bed channel routing and the position-dependent compensation. +// The size tables are generated from the region geometry with grouped equal-power +// panning, an S448 weight and a power sum; every operation is evaluated in +// float32 in a fixed order so the coefficients are reproducible. +// +// The caller passes the parameter mappings from oamd_params; this module does +// not read files itself. + +#pragma once + +#include +#include +#include +#include + +#include "oamd/oamd_params.h" +#include "speaker/speaker_layout_lookup.h" +#include "speaker/speaker_layouts.h" + +namespace joc::oamd { + +// The specified 2**x approximation used by the compensation kernels. +float exp2_approx(float value); + +// The specified x**power approximation; power is a float32. +float pow_approx(float value, float power); + +// Stable float32 normalisation with the tiny-norm floor. +std::vector norm(const std::vector& vector); + +// One boundary weight of the size kernel. +float boundary(int axis, int edge, float position, float t); + +// Maps a scalar size in [0, 1] to t on the size curve. +float size_curve_value(double size); + +// Size kernel response in region-speaker order (the region point order). +std::vector size_kernel(const ::ejoc::speaker_tables::RegionGeometry& region, + const float position[3], float t, bool height); + +// Size-kernel target vector in layout channel order. +std::vector size_targets(const MetadataParams& meta, + const ::ejoc::speaker_tables::LayoutGeometry& layout, + int region_index, const double position[3], + double size, bool height); + +// One assembled ISF basis matrix (row_count x layout output_count, row major). +struct IsfBasis { + int rows = 0; + int columns = 0; + std::vector values; + + float at(int row, int column) const { + return values[static_cast(row) * static_cast(columns) + + static_cast(column)]; + } +}; + +// Assembles one ISF basis matrix from the compressed base tables. +IsfBasis assemble_isf(const MetadataParams& meta, const char* layout_key, int isf_idx); + +// Returns the per-basis ISF compensation gain. +float isf_compensation(const MetadataParams& meta, const char* layout_key, int isf_idx, + int row, bool disabled); + +// Per-label bed redistribution weight, in the reference-position index space +// used by the layout descriptors. +float bed_weight(int label, std::uint32_t mask, int target_label); + +// The five height-candidate flags for one input bed label. +std::array bed_candidates(int label, std::uint32_t mask, + const std::vector& output_labels); + +// Route and weights for one input bed label. route == -1 means the returned +// weights are the target; otherwise route is the reference position used to +// synthesise a point source. +struct BedDistribution { + int route = -1; + std::vector weights; +}; + +// Derives the bed routing of one input bed label from the target layout. +BedDistribution bed_distribution(const ::joc::speaker::LayoutInfo& layout, int label, + std::uint32_t mask, bool distribute); + +// Returns the Q15 reference position used by the bed fallback path. +void bed_reference_position(int index, int q15[3]); + +// Returns the bed path compensation gain for one input bed label. ``mode`` is +// the trim mode (0 automatic, 1 disabled, 2 explicit) and ``levels`` holds the +// surround, height and centre levels in dB. +float bed_compensation(const ::joc::speaker::LayoutInfo& layout, int label, int mode, + bool bypass, const double levels[3]); + +// Automatic attenuation of a target layout in dB. +float bed_attenuation_db(const ::joc::speaker::LayoutInfo& layout); + +} // namespace joc::oamd diff --git a/src/oamd/oamd_params.cpp b/src/oamd/oamd_params.cpp new file mode 100644 index 0000000..822ca17 --- /dev/null +++ b/src/oamd/oamd_params.cpp @@ -0,0 +1,108 @@ +#include "oamd/oamd_params.h" + +#include +#include +#include + +#include "oamd/isf_tables.h" + +namespace joc::oamd { + +namespace { + +namespace tables = isf_tables; + +// Builds the renderer-facing view from the checked-in table header. There is no +// file I/O and no runtime parsing: the numbers are compiled in as static data. +MetadataParams build_params() { + MetadataParams params; + params.configs.reserve(tables::kConfigCount); + for (const tables::Config& source : tables::kConfigs) { + IsfConfig config; + for (std::size_t index = 0; index < config.counts.size(); ++index) { + config.counts[index] = source.counts[index]; + } + const std::size_t rows = static_cast(config.row_count()); + config.primary_row_map.assign(source.primary_row_map, source.primary_row_map + rows); + config.secondary_row_map.assign(source.secondary_row_map, + source.secondary_row_map + rows); + const std::size_t coefficients = rows * static_cast(tables::kMatrixColumns); + config.matrix.assign(source.matrix, source.matrix + coefficients); + params.configs.push_back(std::move(config)); + } + + params.layouts.reserve(tables::kLayoutCount); + for (const tables::Layout& source : tables::kLayouts) { + IsfLayout layout; + layout.output_count = source.output_count; + for (std::size_t index = 0; index < layout.selectors.size(); ++index) { + layout.selectors[index] = source.selectors[index]; + } + layout.compensation_value = source.compensation_value; + layout.output_ids.assign(source.output_ids, + source.output_ids + tables::kOutputIdSlots); + params.layouts.emplace(source.key, std::move(layout)); + } + + const auto copy_groups = [](const tables::GroupRecord* records, int count, + std::vector* out) { + out->reserve(static_cast(count)); + for (int index = 0; index < count; ++index) { + out->push_back(IsfGroupRecord{records[index].selector, records[index].begin, + records[index].end}); + } + }; + copy_groups(tables::kLayoutGroups.floor_a, tables::kLayoutGroups.floor_a_count, + ¶ms.layout_groups.floor_a); + copy_groups(tables::kLayoutGroups.floor_b, tables::kLayoutGroups.floor_b_count, + ¶ms.layout_groups.floor_b); + copy_groups(tables::kLayoutGroups.height, tables::kLayoutGroups.height_count, + ¶ms.layout_groups.height); + copy_groups(tables::kLayoutGroups.additional, tables::kLayoutGroups.additional_count, + ¶ms.layout_groups.additional); + + params.paired_columns.assign(tables::kPairedColumns, + tables::kPairedColumns + tables::kPairedColumnCount); + return params; +} + +const MetadataParams& loaded_params() { + static const MetadataParams params = build_params(); + return params; +} + +} // namespace + +const std::vector* IsfLayoutGroups::find(const char* name) const { + if (name == nullptr) { + return nullptr; + } + const std::string key(name); + if (key == "floor_a") { + return &floor_a; + } + if (key == "floor_b") { + return &floor_b; + } + if (key == "height") { + return &height; + } + if (key == "additional") { + return &additional; + } + return nullptr; +} + +const IsfLayout* MetadataParams::layout(const char* key) const { + if (key == nullptr) { + return nullptr; + } + const auto found = layouts.find(key); + return found != layouts.end() ? &found->second : nullptr; +} + +const MetadataParams* metadata_params() { return &loaded_params(); } + +const char* metadata_params_error() { return ""; } + +} // namespace joc::oamd diff --git a/src/oamd/oamd_params.h b/src/oamd/oamd_params.h new file mode 100644 index 0000000..eec7679 --- /dev/null +++ b/src/oamd/oamd_params.h @@ -0,0 +1,82 @@ +// Port of src/oamd_params.py: the static ISF lookup data of the metadata path. +// +// The numbers are compiled in as the generated header `oamd/isf_tables.h`: +// ISF base coefficients and layout descriptors, applied as lookup tables. +// The remaining values (gain, size, priority, position, delta and refinement +// tables) are computed in code; the size tables are generated from the region +// geometry at runtime, and bed routing is derived from the speaker layouts. +// The arrays are adapted once per process into the structures below; nothing +// reads a data file. + +#pragma once + +#include +#include +#include +#include +#include + +#include "foundation/status.h" + +namespace joc::oamd { + +// One ISF configuration: the compressed basis matrix and the two row maps +// that expand it to the layout output channels. +struct IsfConfig { + // mid, upper, lower, zenith, row_count (the reference's counts tuple). + std::array counts{}; + std::vector primary_row_map; + std::vector secondary_row_map; + // row_count * 73 float32 coefficients, row major. + std::vector matrix; + + int row_count() const { return counts[4]; } +}; + +// One layout descriptor: how many output channels the basis has, +// the layout-group selectors and the automatic compensation value. +struct IsfLayout { + int output_count = 0; + std::array selectors{}; // floor_a, floor_b, height, additional + double compensation_value = 0.0; + // ISF channel id of each matrix output column, padded with zeros to 37. + std::vector output_ids; +}; + +// One record of a layout group: selector id and the half-open column range +// [begin, end). +struct IsfGroupRecord { + int selector = 0; + int begin = 0; + int end = 0; +}; + +// The four layout groups of the ISF tables. +struct IsfLayoutGroups { + std::vector floor_a; + std::vector floor_b; + std::vector height; + std::vector additional; + + const std::vector* find(const char* name) const; +}; + +// The ISF lookup data in the shape the metadata renderer consumes. +struct MetadataParams { + std::vector configs; + std::unordered_map layouts; + IsfLayoutGroups layout_groups; + // One flag per matrix column: whether the column also contributes through + // the secondary row map. + std::vector paired_columns; + + const IsfLayout* layout(const char* key) const; +}; + +// Returns the process-wide tables. They are compiled in, so the pointer is +// never null; ``metadata_params_error`` is kept for callers that log a load +// failure and always returns an empty string. +const MetadataParams* metadata_params(); +const char* metadata_params_error(); + +} // namespace joc::oamd diff --git a/src/oamd/oamd_parser.cpp b/src/oamd/oamd_parser.cpp index b8a78af..fcc72f7 100644 --- a/src/oamd/oamd_parser.cpp +++ b/src/oamd/oamd_parser.cpp @@ -1,7 +1,10 @@ #include "oamd/oamd_parser.h" +#include #include +#include #include +#include #include "foundation/bit_reader.h" @@ -14,7 +17,7 @@ constexpr int kRampDurations[3] = {0, 512, 1536}; constexpr int kRampDurationIndex[16] = {32, 64, 128, 256, 320, 480, 1000, 1001, 1024, 1600, 1601, 1602, 1920, 2000, 2002, 2048}; -constexpr std::uint32_t kObjectElementId = 1; +constexpr int kIsfObjectCounts[6] = {4, 8, 10, 14, 15, 30}; // ETSI TS 103 420 Table 12 trim levels, 4-bit index, dB; index 15 is the // minimum (-36 dB), not silence. constexpr double kTrimLut[16] = {6.0, 3.0, 1.5, 0.75, -0.75, -1.5, @@ -22,8 +25,74 @@ constexpr double kTrimLut[16] = {6.0, 3.0, 1.5, 0.75, -0.75, -1.5, -12.0, -13.5, -15.0, -36.0}; // bal3d_y is coded as (code + 1) / 16, so the magnitude never reaches 0. constexpr double kBalanceStep = 16.0; -constexpr int kIsfObjectCounts[6] = {4, 8, 10, 14, 15, 30}; -constexpr int kStandardBedChannelCount[10] = {2, 1, 1, 2, 2, 2, 2, 2, 2, 1}; +// 5.6.1.1.4 的 10 bit 标准 bed 掩码 -> 17 bit 非标准 bed 掩码。 +constexpr std::uint32_t kStandardBedToWide[10] = { + 0x3u, 0x4u, 0x8u, 0x30u, 0xC0u, + 0x300u, 0xC00u, 0x3000u, 0xC000u, 0x10000u}; +// 5.5.9 位置差分码表。 +constexpr int kPositionDelta[8] = {0, 1, 2, 3, -4, -3, -2, -1}; +// 位置细化码表。 +constexpr int kRefineDelta[4] = {1, 2, -1, -2}; +// 5.5.3/5.5.5 divergence 码表(公开实现固定值),Q15 量纲。 +constexpr int kDivergenceQuickIndex[4] = {26, 29, 32, 63}; +constexpr int kDivergenceTable[64] = { + 0, 0, 66, 117, 209, 331, 467, 659, 829, 1042, 1309, + 1643, 2059, 2302, 2573, 2872, 3202, 3564, 3960, 4391, 4856, 5354, + 5882, 6437, 7014, 7605, 8204, 8803, 9394, 9970, 10524, 11051, 11548, + 12011, 12441, 12835, 13197, 13525, 13823, 14092, 14335, 14553, 14748, 14924, + 15081, 15221, 15347, 15459, 15559, 15649, 15728, 15799, 15863, 15970, 16055, + 16123, 16176, 16219, 16267, 16301, 16332, 16355, 16368, 16384, +}; + +// 5.6.1.1.4 的 10 bit 掩码位展开(LSB 在前,逐位给出 1..2 个声道)。 +struct StandardBedBit { + std::uint8_t count; + ::joc::joc::BedChannel channels[2]; +}; + +constexpr StandardBedBit kStandardBedBits[10] = { + {2, {::joc::joc::BedChannel::kLeft, ::joc::joc::BedChannel::kRight}}, + {1, {::joc::joc::BedChannel::kCenter, ::joc::joc::BedChannel::kLeft}}, + {1, {::joc::joc::BedChannel::kLfe, ::joc::joc::BedChannel::kLeft}}, + {2, + {::joc::joc::BedChannel::kLeftSurround, + ::joc::joc::BedChannel::kRightSurround}}, + {2, + {::joc::joc::BedChannel::kLeftRearSurround, + ::joc::joc::BedChannel::kRightRearSurround}}, + {2, + {::joc::joc::BedChannel::kTopFrontLeft, + ::joc::joc::BedChannel::kTopFrontRight}}, + {2, + {::joc::joc::BedChannel::kTopSideLeft, + ::joc::joc::BedChannel::kTopSideRight}}, + {2, + {::joc::joc::BedChannel::kTopBackLeft, + ::joc::joc::BedChannel::kTopBackRight}}, + {2, {::joc::joc::BedChannel::kLeftWide, ::joc::joc::BedChannel::kRightWide}}, + {1, {::joc::joc::BedChannel::kLfe2, ::joc::joc::BedChannel::kLeft}}, +}; + +// 5.6.1.1.5 的 17 bit 非标准掩码位(LSB 在前)。 +constexpr ::joc::joc::BedChannel kNonStandardBedBits[kBedLabelCount] = { + ::joc::joc::BedChannel::kLeft, + ::joc::joc::BedChannel::kRight, + ::joc::joc::BedChannel::kCenter, + ::joc::joc::BedChannel::kLfe, + ::joc::joc::BedChannel::kLeftSurround, + ::joc::joc::BedChannel::kRightSurround, + ::joc::joc::BedChannel::kLeftRearSurround, + ::joc::joc::BedChannel::kRightRearSurround, + ::joc::joc::BedChannel::kTopFrontLeft, + ::joc::joc::BedChannel::kTopFrontRight, + ::joc::joc::BedChannel::kTopSideLeft, + ::joc::joc::BedChannel::kTopSideRight, + ::joc::joc::BedChannel::kTopBackLeft, + ::joc::joc::BedChannel::kTopBackRight, + ::joc::joc::BedChannel::kLeftWide, + ::joc::joc::BedChannel::kRightWide, + ::joc::joc::BedChannel::kLfe2, +}; Status variant(const std::string& name, const std::string& message) { return Status::fail(JOC_ERR_OAMD_UNSUPPORTED_VARIANT, stage::kOamd, @@ -68,19 +137,141 @@ bool variable_bits_max(bits::BitReader& reader, unsigned width, unsigned max_gro return true; } -struct ProgramInfo { +// q15_ratio(n, d) = min(32767, floor(32768 n / d + 0.5)),参考实现的取整规则。 +std::int32_t q15_ratio(int numerator, int denominator) { + const double value = std::floor( + 32768.0 * static_cast(numerator) / static_cast(denominator) + 0.5); + const std::int32_t quantised = static_cast(value); + return quantised > 32767 ? 32767 : quantised; +} + +// gain_idx/gain_bits -> 整数增益 Qg(单位增益 4096)。 +std::int32_t gain_value(std::uint32_t gain_idx, std::uint32_t gain_bits) { + switch (gain_idx) { + case 0u: + return 4096; + case 1u: + return 0; + case 2u: { + const int bits = static_cast(gain_bits); + const int db = bits < 15 ? 15 - bits : 14 - bits; + return static_cast( + std::floor(4096.0 * std::pow(10.0, static_cast(db) / 20.0))); + } + default: + return 4096; + } +} + +// size_idx 与 5 bit 数值 -> (sx, sy, sz) Q15。 +void size_value(std::uint32_t mode, const std::uint32_t* values, std::uint16_t out[3]) { + if (mode == 1u) { + const auto value = static_cast(q15_ratio(static_cast(values[0]), 31)); + out[0] = value; + out[1] = value; + out[2] = value; + return; + } + if (mode == 2u) { + for (int axis = 0; axis < 3; ++axis) { + out[axis] = + static_cast(q15_ratio(static_cast(values[axis]), 31)); + } + return; + } + out[0] = 0; + out[1] = 0; + out[2] = 0; +} + +// coarse (x, y, z) -> Q15 位置,z 带符号。 +void coarse_to_q15(const int coarse[3], std::int32_t* qx, std::int32_t* qy, + std::int32_t* qz) { + *qx = q15_ratio(coarse[0], kNQ12); + *qy = q15_ratio(coarse[1], kNQ12); + const std::int32_t magnitude = q15_ratio(std::abs(coarse[2]), kNQ3); + *qz = coarse[2] < 0 ? -magnitude : magnitude; +} + +void clamp_coarse(int coarse[3]) { + coarse[0] = coarse[0] < 0 ? 0 : (coarse[0] > kNQ12 ? kNQ12 : coarse[0]); + coarse[1] = coarse[1] < 0 ? 0 : (coarse[1] > kNQ12 ? kNQ12 : coarse[1]); + coarse[2] = coarse[2] < -kNQ3 ? -kNQ3 : (coarse[2] > kNQ3 ? kNQ3 : coarse[2]); +} + +// bed/ISF 与未激活对象的默认位置:中心 16384/16384/0。 +ObjectRenderState render_defaults() { + ObjectRenderState state; + state.qx = 16384; + state.qy = 16384; + state.qz = 0; + state.zone_idx = 0; + state.enable_elevation = true; + state.size_mode = 0; + state.size_q[0] = 0; + state.size_q[1] = 0; + state.size_q[2] = 0; + state.snap = false; + state.divergence_q = 0; + return state; +} + +// render_status 2/3 沿用上一块的可渲染字段(增益与优先级不在这里)。 +void copy_render_fields(const ObjectRenderState& source, ObjectRenderState* target) { + target->qx = source.qx; + target->qy = source.qy; + target->qz = source.qz; + target->zone_idx = source.zone_idx; + target->enable_elevation = source.enable_elevation; + target->size_mode = source.size_mode; + for (int axis = 0; axis < 3; ++axis) { + target->size_q[axis] = source.size_q[axis]; + } + target->snap = source.snap; + target->divergence_q = source.divergence_q; +} + +std::vector<::joc::joc::BedChannel> standard_bed_channels(std::uint32_t bed_mask) { + std::vector<::joc::joc::BedChannel> channels; + for (int bit = 0; bit < 10; ++bit) { + if (((bed_mask >> bit) & 1u) == 0u) { + continue; + } + for (std::uint8_t index = 0; index < kStandardBedBits[bit].count; ++index) { + channels.push_back(kStandardBedBits[bit].channels[index]); + } + } + return channels; +} + +std::vector<::joc::joc::BedChannel> nonstandard_bed_channels(std::uint32_t bed_mask) { + std::vector<::joc::joc::BedChannel> channels; + for (int bit = 0; bit < kBedLabelCount; ++bit) { + if (((bed_mask >> bit) & 1u) != 0u) { + channels.push_back(kNonStandardBedBits[bit]); + } + } + return channels; +} + +struct RawProgram { bool dynamic_object_only = false; bool lfe_present = false; + bool content_reserved = false; bool content_bed = false; bool content_isf = false; bool content_dynamic = false; + bool b_bed_chan_distribute = false; std::uint32_t num_bed_objects = 0; std::uint32_t num_isf_objects = 0; std::int32_t num_dynamic_objects = -1; + std::uint32_t bed_mask = 0; + std::int32_t isf_idx = -1; std::vector bed_assignments; }; -Status parse_program_assignment(bits::BitReader& reader, ProgramInfo* program) { +// 5.5.3 program_assignment():bed / ISF / dynamic 对象数量与 bed 声明。 +Status parse_program_assignment(bits::BitReader& reader, RawProgram* program) { program->dynamic_object_only = reader.read(1) != 0u; if (reader.failed()) { return syntax("program_assignment truncated"); @@ -91,6 +282,13 @@ Status parse_program_assignment(bits::BitReader& reader, ProgramInfo* program) { return syntax("program_assignment truncated"); } program->num_bed_objects = program->lfe_present ? 1u : 0u; + if (program->lfe_present) { + BedAssignment assignment; + assignment.lfe_only = true; + assignment.channels.push_back(::joc::joc::BedChannel::kLfe); + program->bed_assignments.push_back(assignment); + program->bed_mask = 0x8u; + } return Status::success(); } @@ -98,61 +296,65 @@ 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_reserved = (mask & 0x8u) != 0u; program->content_dynamic = (mask & 0x4u) != 0u; - if ((mask & 0x1u) != 0u) { - reader.read(1); - const std::uint32_t multi = reader.read(1); + program->content_isf = (mask & 0x2u) != 0u; + program->content_bed = (mask & 0x1u) != 0u; + if (program->content_bed) { + program->b_bed_chan_distribute = reader.read(1) != 0u; + const bool multi = reader.read(1) != 0u; + std::uint32_t instances = 1; + if (multi) { + instances = reader.read(3) + 2u; + } if (reader.failed()) { return syntax("program_assignment truncated"); } - std::uint32_t instances = 1; - if (multi != 0u) { - instances = reader.read(3) + 2u; - } for (std::uint32_t instance = 0; instance < instances; ++instance) { BedAssignment assignment; - if (reader.read(1) != 0u) { + (void)instance; + if (reader.read(1) != 0u) { // b_lfe_only assignment.lfe_only = true; - assignment.mask = 0; + assignment.channels.push_back(::joc::joc::BedChannel::kLfe); + if (reader.failed()) { + return syntax("program_assignment truncated"); + } program->bed_assignments.push_back(assignment); program->num_bed_objects += 1; continue; } - if (reader.read(1) != 0u) { + if (reader.read(1) != 0u) { // b_standard_chan_assign assignment.standard = true; assignment.mask = reader.read(10); - for (int bit = 0; bit < 10; ++bit) { - if (((assignment.mask >> bit) & 1u) != 0u) { - program->num_bed_objects += - static_cast(kStandardBedChannelCount[bit]); - } + if (reader.failed()) { + return syntax("program_assignment truncated"); } + assignment.channels = standard_bed_channels(assignment.mask); + program->bed_mask |= standard_bed_mask(assignment.mask); } else { assignment.standard = false; assignment.mask = reader.read(17); - for (int bit = 0; bit < 17; ++bit) { - if (((assignment.mask >> bit) & 1u) != 0u) { - program->num_bed_objects += 1; - } + if (reader.failed()) { + return syntax("program_assignment truncated"); } + assignment.channels = nonstandard_bed_channels(assignment.mask); + program->bed_mask |= assignment.mask; } + program->num_bed_objects += + static_cast(assignment.channels.size()); program->bed_assignments.push_back(assignment); - if (reader.failed()) { - return syntax("program_assignment truncated"); - } } } - if ((mask & 0x2u) != 0u) { + if (program->content_isf) { const std::uint32_t isf_idx = reader.read(3); if (reader.failed()) { return syntax("program_assignment truncated"); } + program->isf_idx = static_cast(isf_idx); program->num_isf_objects = (isf_idx < 6u) ? static_cast(kIsfObjectCounts[isf_idx]) : 0u; } - if ((mask & 0x4u) != 0u) { + if (program->content_dynamic) { std::uint32_t count = reader.read(5); if (count == 0x1Fu) { count += reader.read(7); @@ -162,7 +364,7 @@ Status parse_program_assignment(bits::BitReader& reader, ProgramInfo* program) { } program->num_dynamic_objects = static_cast(count + 1u); } - if ((mask & 0x8u) != 0u) { + if (program->content_reserved) { const std::uint32_t reserved_bytes = reader.read(4) + 1u; if (!reader.skip(static_cast(reserved_bytes) * 8u)) { return syntax("program_assignment reserved data truncated"); @@ -171,119 +373,295 @@ Status parse_program_assignment(bits::BitReader& reader, ProgramInfo* program) { return Status::success(); } -Status parse_object_info_block(bits::BitReader& reader, int object_index, bool in_bed_or_isf, - int* position_x, int* position_y, int* position_z, bool* has_position) { - *has_position = false; +// 5.5.9 object_info_block:一个对象一个块的完整状态。 +struct RenderWork { + ObjectRenderState render; + int coarse[3] = {31, 31, 0}; +}; + +Status parse_object_info_block(bits::BitReader& reader, int object_index, int block_index, + ObjectKind kind, const RenderWork* previous, + const std::vector* earlier_objects, + RenderWork* out) { + *out = RenderWork{}; const bool not_active = reader.read(1) != 0u; if (reader.failed()) { return syntax("object_info_block truncated"); } - const std::uint32_t basic_status = not_active ? 0u : 1u; - if (basic_status == 1u) { - const std::uint32_t gain_idx = reader.read(2); - if (gain_idx == 2u) { - reader.read(6); - } - const bool default_priority = reader.read(1) != 0u; - if (!default_priority) { - reader.read(5); - } + out->render.not_active = not_active; + std::int32_t gain_q = 0; + std::int32_t priority_q = 0; + ObjectRenderState render; + std::uint32_t render_status = 0; + + if (not_active) { + render = render_defaults(); + render.not_active = true; + coarse_to_q15(out->coarse, &render.qx, &render.qy, &render.qz); + } else { + const std::uint32_t basic_status = block_index == 0 ? 1u : reader.read(2); if (reader.failed()) { return syntax("object_info_block truncated"); } - } - const std::uint32_t render_status = (not_active || in_bed_or_isf) ? 0u : 1u; - if (render_status == 1u) { - const int x = static_cast(reader.read(6)); - const int y = static_cast(reader.read(6)); - const int z_sign = static_cast(reader.read(1)); - const int z = static_cast(reader.read(4)); - if (reader.failed()) { - return syntax("object_info_block truncated"); - } - *position_x = x; - *position_y = y; - *position_z = z_sign != 0 ? z : -z; - *has_position = true; - if (reader.read(1) != 0u) { - if (reader.read(1) == 0u) { - reader.read(4); + // gain_idx=3 沿用同块前一对象的增益;对象 0 用单位增益,前一对象未激活 + // 时为 0。 + const auto read_gain = [&](std::int32_t* value) -> bool { + const std::uint32_t gain_idx = reader.read(2); + std::uint32_t gain_bits = 0; + if (gain_idx == 2u) { + gain_bits = reader.read(6); + } + if (reader.failed()) { + return false; + } + if (gain_idx == 3u) { + if (object_index == 0) { + *value = 4096; + } else { + const ObjectRenderState& earlier = + (*earlier_objects)[static_cast(object_index) - 1] + .blocks[static_cast(block_index)] + .render; + *value = earlier.not_active ? 0 : earlier.gain_q; + } + } else { + *value = gain_value(gain_idx, gain_bits); + } + return true; + }; + const auto read_priority = [&](std::int32_t* value) -> bool { + const bool default_priority = reader.read(1) != 0u; + if (reader.failed()) { + return false; + } + if (default_priority) { + *value = 32767; + } else { + const std::uint32_t bits = reader.read(5); + if (reader.failed()) { + return false; + } + *value = static_cast(1024u * bits); + } + return true; + }; + + switch (basic_status) { + case 0u: + gain_q = 0; + priority_q = 0; + break; + case 1u: + if (!read_gain(&gain_q) || !read_priority(&priority_q)) { + return syntax("object_info_block truncated"); + } + break; + case 2u: + if (previous == nullptr) { + return syntax("object_info_block status 2 without a previous block"); + } + gain_q = previous->render.gain_q; + priority_q = previous->render.priority_q; + break; + default: { + const std::uint32_t flags = reader.read(2); + if (reader.failed()) { + return syntax("object_info_block truncated"); + } + if (previous == nullptr) { + return syntax("object_info_block mask without a previous block"); + } + gain_q = previous->render.gain_q; + priority_q = previous->render.priority_q; + if ((flags & 1u) != 0u) { + if (!read_gain(&gain_q)) { + return syntax("object_info_block truncated"); + } + } + if ((flags & 2u) != 0u) { + if (!read_priority(&priority_q)) { + return syntax("object_info_block truncated"); + } + } + break; } } - reader.read(3); - reader.read(1); - const std::uint32_t size_idx = reader.read(2); - if (size_idx == 1u) { - reader.read(5); - } else if (size_idx == 2u) { - reader.read(15); - } - if (reader.read(1) != 0u) { - reader.read(3); - reader.read(2); - } - reader.read(1); + + render_status = (kind == ObjectKind::kBed || kind == ObjectKind::kIsf) + ? 0u + : (block_index == 0 ? 1u : reader.read(2)); if (reader.failed()) { return syntax("object_info_block truncated"); } + if (render_status == 1u || render_status == 3u) { + std::uint32_t flags = 0xFu; + if (render_status == 3u) { + if (previous == nullptr) { + return syntax("object_info_block differential without a previous block"); + } + flags = reader.read(4); + if (reader.failed()) { + return syntax("object_info_block truncated"); + } + render = render_defaults(); + copy_render_fields(previous->render, &render); + out->coarse[0] = previous->coarse[0]; + out->coarse[1] = previous->coarse[1]; + out->coarse[2] = previous->coarse[2]; + } else { + render = render_defaults(); + } + const bool position_present = (flags & 0x1u) != 0u; + const bool zone_present = (flags & 0x2u) != 0u; + const bool size_present = (flags & 0x4u) != 0u; + const bool screen_present = (flags & 0x8u) != 0u; + if (position_present) { + bool differential = false; + if (block_index > 0) { + differential = reader.read(1) != 0u; + } + if (reader.failed()) { + return syntax("object_info_block truncated"); + } + if (differential) { + for (int axis = 0; axis < 3; ++axis) { + out->coarse[axis] += kPositionDelta[reader.read(3) & 0x7u]; + if (reader.failed()) { + return syntax("object_info_block truncated"); + } + } + } else { + const int x = static_cast(reader.read(6)); + const int y = static_cast(reader.read(6)); + const std::uint32_t z_sign = reader.read(1); + const int z = static_cast(reader.read(4)); + if (reader.failed()) { + return syntax("object_info_block truncated"); + } + out->coarse[0] = x; + out->coarse[1] = y; + out->coarse[2] = z_sign != 0u ? z : -z; + } + clamp_coarse(out->coarse); + const bool outside = reader.read(1) != 0u; + if (outside) { + const bool infinite = reader.read(1) != 0u; + if (!infinite) { + reader.read(4); + } + } + if (reader.failed()) { + return syntax("object_info_block truncated"); + } + } + if (zone_present) { + render.zone_idx = static_cast(reader.read(3)); + render.enable_elevation = reader.read(1) != 0u; + } + if (size_present) { + const std::uint32_t size_mode = reader.read(2); + render.size_mode = static_cast(size_mode); + std::uint32_t values[3] = {0, 0, 0}; + if (size_mode == 1u) { + values[0] = reader.read(5); + } else if (size_mode == 2u) { + values[0] = reader.read(5); + values[1] = reader.read(5); + values[2] = reader.read(5); + } + size_value(size_mode, values, render.size_q); + } + if (screen_present) { + const bool screen_ref = reader.read(1) != 0u; + if (screen_ref) { + reader.read(3); + reader.read(2); + } + } + render.snap = reader.read(1) != 0u; + if (reader.failed()) { + return syntax("object_info_block truncated"); + } + if (position_present) { + coarse_to_q15(out->coarse, &render.qx, &render.qy, &render.qz); + } + } else if (render_status == 2u) { + if (previous == nullptr) { + return syntax("object_info_block hold without a previous block"); + } + render = render_defaults(); + copy_render_fields(previous->render, &render); + out->coarse[0] = previous->coarse[0]; + out->coarse[1] = previous->coarse[1]; + out->coarse[2] = previous->coarse[2]; + } else { + render = render_defaults(); + coarse_to_q15(out->coarse, &render.qx, &render.qy, &render.qz); + } } - if (reader.read(1) != 0u) { + + if (reader.read(1) != 0u) { // b_additional_table_data_exists const std::uint32_t additional_bytes = reader.read(4) + 1u; if (!reader.skip(static_cast(additional_bytes) * 8u)) { return syntax("object_info_block additional table truncated"); } } - (void)object_index; + if (reader.failed()) { + return syntax("object_info_block truncated"); + } + render.not_active = not_active; + render.gain_q = gain_q; + render.priority_q = priority_q; + out->render = render; return Status::success(); } -struct ObjectElementInfo { - std::uint32_t sample_offset_code = 0; - std::uint32_t sample_offset = 0; - std::uint32_t block_count = 0; - std::uint32_t block_offset_samples = 0; - std::uint32_t ramp_duration_samples = 0; - bool reserved_data_not_present = false; -}; - +// 5.5.5 object_element():时间头 + 对象外/块内顺序的完整块状态。 Status parse_object_element(bits::BitReader& reader, std::uint32_t object_count, - std::uint32_t bed_isf_objects, ObjectElementInfo* out, OamdUpdate* update) { - out->sample_offset_code = reader.read(2); + const ObjectKind* kinds, FrameState* frame, + std::uint32_t* sample_offset_code_out, + bool* reserved_data_not_present_out) { + const std::uint32_t sample_offset_code = reader.read(2); if (reader.failed()) { return syntax("object_element truncated"); } - switch (out->sample_offset_code) { - case 0: out->sample_offset = 0; break; - case 1: { - const std::uint32_t index = reader.read(2); - if (reader.failed()) { - return syntax("object_element truncated"); - } - out->sample_offset = static_cast(kSampleOffsetIndex[index & 3u]); + *sample_offset_code_out = sample_offset_code; + std::uint32_t sample_offset = 0; + switch (sample_offset_code) { + case 0u: + sample_offset = 0; + break; + case 1u: + sample_offset = static_cast( + kSampleOffsetIndex[reader.read(2) & 0x3u]); + break; + case 2u: + sample_offset = reader.read(5); break; - } - case 2: out->sample_offset = reader.read(5); break; default: return variant("md_sample_offset_mode", - "MD sample-offset mode " + - std::to_string(out->sample_offset_code) + - " is not covered by the 16-slot model"); + "MD sample-offset mode " + std::to_string(sample_offset_code) + + " is not covered"); } - out->block_count = reader.read(3) + 1u; if (reader.failed()) { return syntax("object_element truncated"); } - for (std::uint32_t block = 0; block < out->block_count; ++block) { + const std::uint32_t block_count = reader.read(3) + 1u; + if (reader.failed()) { + return syntax("object_element truncated"); + } + frame->block_count = block_count; + frame->sample_offset = sample_offset; + frame->blocks.clear(); + frame->blocks.reserve(block_count); + for (std::uint32_t block = 0; block < block_count; ++block) { const std::uint32_t block_offset_factor = reader.read(6); const std::uint32_t ramp_code = reader.read(2); std::uint32_t ramp_duration = 0; if (ramp_code == 3u) { if (reader.read(1) != 0u) { - const std::uint32_t index = reader.read(4); - if (reader.failed()) { - return syntax("object_element truncated"); - } - ramp_duration = static_cast(kRampDurationIndex[index & 15u]); + ramp_duration = static_cast( + kRampDurationIndex[reader.read(4) & 0xFu]); } else { ramp_duration = reader.read(11); } @@ -293,44 +671,190 @@ Status parse_object_element(bits::BitReader& reader, std::uint32_t object_count, if (reader.failed()) { return syntax("object_element truncated"); } - if (block == 0) { - out->block_offset_samples = out->sample_offset + block_offset_factor * 32u; - out->ramp_duration_samples = ramp_duration; - } + BlockTiming timing; + timing.block_offset_samples = sample_offset + block_offset_factor * 32u; + timing.ramp_duration_samples = ramp_duration; + frame->blocks.push_back(timing); } - out->reserved_data_not_present = reader.read(1) != 0u; - if (!out->reserved_data_not_present) { + const bool reserved_data_not_present = reader.read(1) != 0u; + if (!reserved_data_not_present) { reader.read(5); } if (reader.failed()) { return syntax("object_element truncated"); } + *reserved_data_not_present_out = reserved_data_not_present; - for (std::uint32_t index = 0; index < object_count; ++index) { - int x = 0; - int y = 0; - int z = 0; - bool has_position = false; - const bool in_bed_or_isf = index < bed_isf_objects; - const Status status = - parse_object_info_block(reader, static_cast(index), in_bed_or_isf, &x, &y, &z, - &has_position); - if (!status.ok()) { - return status; + frame->objects.clear(); + frame->objects.reserve(object_count); + std::vector> work( + object_count, std::vector(block_count)); + for (std::uint32_t object_index = 0; object_index < object_count; ++object_index) { + ObjectElementState element; + element.kind = kinds[object_index]; + element.blocks.resize(block_count); + for (std::uint32_t block_index = 0; block_index < block_count; ++block_index) { + RenderWork& current = + work[object_index][static_cast(block_index)]; + const RenderWork* previous = + block_index > 0 + ? &work[object_index][static_cast(block_index) - 1] + : nullptr; + const Status status = parse_object_info_block( + reader, static_cast(object_index), static_cast(block_index), + element.kind, previous, &frame->objects, ¤t); + if (!status.ok()) { + return status; + } + element.blocks[block_index].render = current.render; } - if (!has_position || index >= static_cast(kMaxSlots)) { - continue; + frame->objects.push_back(std::move(element)); + } + return Status::success(); +} + +// Q15 位置索引细化:refined index = 5 * coarse + 2 + delta。 +std::int32_t refine_xy(int index) { + const double value = + std::floor((static_cast(index) - 2.0) * 32768.0 / 310.0 + 0.5); + std::int32_t quantised = static_cast(value); + if (quantised < 0) { + quantised = 0; + } + if (quantised > 32767) { + quantised = 32767; + } + return quantised; +} + +std::int32_t refine_z(int index) { + const double value = + std::floor((static_cast(index) - 2.0) * 32768.0 / 75.0 + 0.5); + std::int32_t quantised = static_cast(value); + if (quantised < -32767) { + quantised = -32767; + } + if (quantised > 32767) { + quantised = 32767; + } + return quantised; +} + +int sign_of(int value) { + return value < 0 ? -1 : 1; +} + +int coarse_xy_from_q15(int value) { + const int offset = value - 16384; + return 31 + sign_of(offset) * ((62 * std::abs(offset) + 16384) >> 15); +} + +int coarse_z_from_q15(int value) { + value = value > -32767 ? value : -32767; + return sign_of(value) * ((15 * std::abs(value) + 16384) >> 15); +} + +// 扩展对象元素:divergence 与高精度位置补充。 +Status parse_extended_element(bits::BitReader& reader, + std::vector* objects, + std::uint32_t block_count) { + const bool divergence_present = reader.read(1) != 0u; + if (reader.failed()) { + return syntax("extended object element truncated"); + } + if (divergence_present) { + for (std::size_t object_index = 0; object_index < objects->size(); ++object_index) { + ObjectElementState& element = (*objects)[object_index]; + for (std::uint32_t block_index = 0; block_index < block_count; ++block_index) { + ObjectRenderState& state = element.blocks[block_index].render; + if (state.not_active) { + state.divergence_q = 0; + continue; + } + if (element.kind != ObjectKind::kDynamic) { + continue; + } + const bool present = reader.read(1) != 0u; + if (reader.failed()) { + return syntax("extended object element truncated"); + } + if (!present) { + state.divergence_q = 0; + continue; + } + const std::uint32_t mode = reader.read(2); + if (reader.failed()) { + return syntax("extended object element truncated"); + } + switch (mode) { + case 0u: + state.divergence_q = static_cast( + kDivergenceTable[kDivergenceQuickIndex[reader.read(2) & 0x3u]]); + break; + case 1u: + state.divergence_q = + block_index == 0 + ? 0 + : element.blocks[block_index - 1].render.divergence_q; + break; + case 2u: + state.divergence_q = static_cast( + kDivergenceTable[reader.read(6) & 0x3Fu]); + break; + default: + reader.read(6); + state.divergence_q = 0; + break; + } + if (reader.failed()) { + return syntax("extended object element truncated"); + } + } } - update->slots[index].q1 = - (x >= 0 && x <= kNQ12) ? static_cast(q_of(x, kNQ12)) : static_cast(-1); - update->slots[index].q2 = - (y >= 0 && y <= kNQ12) ? static_cast(q_of(y, kNQ12)) : static_cast(-1); - if (z >= 0 && z <= kNQ3) { - update->slots[index].q3 = static_cast(q_of(z, kNQ3)); - } else if (z < 0) { - update->slots[index].q3 = 0; - } else { - update->slots[index].q3 = static_cast(-1); + } + const bool refinement_present = reader.read(1) != 0u; + if (reader.failed()) { + return syntax("extended object element truncated"); + } + if (refinement_present) { + for (std::size_t object_index = 0; object_index < objects->size(); ++object_index) { + ObjectElementState& element = (*objects)[object_index]; + for (std::uint32_t block_index = 0; block_index < block_count; ++block_index) { + ObjectRenderState& state = element.blocks[block_index].render; + if (state.not_active || element.kind != ObjectKind::kDynamic) { + continue; + } + const bool present = reader.read(1) != 0u; + if (reader.failed()) { + return syntax("extended object element truncated"); + } + if (!present) { + continue; + } + const std::uint32_t mask = reader.read(3); + if (reader.failed()) { + return syntax("extended object element truncated"); + } + if (mask == 0u) { + continue; + } + int deltas[3] = {0, 0, 0}; + for (int axis = 0; axis < 3; ++axis) { + if ((mask & (1u << axis)) != 0u) { + deltas[axis] = kRefineDelta[reader.read(2) & 0x3u]; + if (reader.failed()) { + return syntax("extended object element truncated"); + } + } + } + const int ix = coarse_xy_from_q15(state.qx); + const int iy = coarse_xy_from_q15(state.qy); + const int iz = coarse_z_from_q15(state.qz); + const int z_sign = sign_of(iz); + state.qx = refine_xy(5 * ix + 2 + deltas[0]); + state.qy = refine_xy(5 * iy + 2 + deltas[1]); + state.qz = z_sign * refine_z(z_sign * (5 * iz + deltas[2]) + 2); + } } } return Status::success(); @@ -402,27 +926,99 @@ Status parse_trim_element(bits::BitReader& reader, std::uint32_t object_count, O return Status::success(); } -} // namespace +// 一个 element 的目录信息,供 frame_state 的 size_mismatch 记录。 +struct ElementRecord { + std::uint32_t ordinal = 0; + std::uint32_t element_id = 0; + std::uint32_t size_bytes = 0; + std::size_t body_end_bit = 0; + std::size_t parsed_end_bit = 0; + bool has_trim = false; + OamdTrim trim; +}; -int q_of(int k, int n) { - const double value = std::floor(32768.0 * static_cast(k) / static_cast(n) + 0.5); - const int quantised = static_cast(value); - return quantised > 32767 ? 32767 : quantised; +std::vector object_kinds(const RawProgram& program, + std::uint32_t object_count) { + std::vector kinds(object_count, ObjectKind::kDynamic); + const std::uint32_t bed_end = program.num_bed_objects; + const std::uint32_t isf_end = bed_end + program.num_isf_objects; + for (std::uint32_t index = 0; index < object_count; ++index) { + if (index < bed_end) { + kinds[index] = ObjectKind::kBed; + } else if (index < isf_end) { + kinds[index] = ObjectKind::kIsf; + } + } + return kinds; } -Status parse_id11(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out) { - return parse_id11_verbose(payload, payload_size, out, nullptr); +// 尾部 padding 必须全零。 +Status check_padding(const bits::BitReader& reader) { + const std::size_t start = reader.position(); + bits::BitReader tail; + tail.reset(reader.data(), (reader.limit() + 7u) / 8u, start); + while (tail.remaining_bits() > 0) { + const std::size_t chunk = tail.remaining_bits() > 32u ? 32u : tail.remaining_bits(); + if (tail.read(static_cast(chunk)) != 0u || tail.failed()) { + return Status::fail(JOC_ERR_BITSTREAM_PADDING, stage::kOamd, + "OAMD payload padding is not zero (from bit " + + std::to_string(start) + ")"); + } + } + return Status::success(); } -Status parse_id11_bed_assignments(const std::uint8_t* payload, std::size_t payload_size, - std::vector* out) { - if (payload == nullptr || out == nullptr) { - return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, "null payload or output"); +void fill_program_state(const RawProgram& program, ProgramState* out) { + out->dynamic_object_only = program.dynamic_object_only; + out->lfe_present = program.lfe_present; + out->b_bed_chan_distribute = program.b_bed_chan_distribute; + out->content_reserved = program.content_reserved; + out->content_bed = program.content_bed; + out->content_isf = program.content_isf; + out->content_dynamic = program.content_dynamic; + 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->bed_mask = program.bed_mask; + out->isf_idx = program.isf_idx; + out->bed_labels.clear(); + for (const BedAssignment& assignment : program.bed_assignments) { + for (const ::joc::joc::BedChannel channel : assignment.channels) { + out->bed_labels.push_back(channel); + } } - if (payload_size == 0) { - return variant("header_truncated", "OAMD payload is empty"); +} + +// 参考 frame_update() 的 16 槽位置视图:bed/ISF 与未激活对象保持上一帧位置。 +void fill_legacy_slots(const FrameState& state, OamdUpdate* out) { + const std::uint32_t count = + state.object_count < static_cast(kMaxSlots) + ? state.object_count + : static_cast(kMaxSlots); + for (std::uint32_t index = 0; index < count; ++index) { + const ObjectElementState& element = state.objects[index]; + if (element.blocks.empty()) { + continue; + } + const ObjectRenderState& render = element.blocks[0].render; + if (render.not_active) { + continue; + } + if (element.kind != ObjectKind::kDynamic) { + continue; + } + SlotUpdate& slot = out->slots[index]; + slot.q1 = static_cast(render.qx); + slot.q2 = static_cast(render.qy); + const std::int32_t elevation = render.enable_elevation ? render.qz : 0; + slot.q3 = static_cast(elevation > 0 ? elevation : 0); } - out->clear(); +} + +// 完整解析:header + program + element 目录 + frame state。 +Status parse_full(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out, + ParseTrace* trace, bool* have_object_element_out) { + *out = OamdUpdate{}; bits::BitReader reader(payload, payload_size); std::uint32_t version = reader.read(2); if (reader.failed()) { @@ -441,11 +1037,324 @@ Status parse_id11_bed_assignments(const std::uint8_t* payload, std::size_t paylo if (reader.failed()) { return syntax("OAMD object count truncated"); } - ProgramInfo program; - const Status status = parse_program_assignment(reader, &program); + const std::uint32_t object_count = object_count_bits + 1u; + RawProgram program; + Status status = parse_program_assignment(reader, &program); if (!status.ok()) { return status; } + const bool alternate_present = reader.read(1) != 0u; + if (reader.failed()) { + return syntax("OAMD alternate data flag truncated"); + } + + FrameState state; + state.object_count = object_count; + std::vector records; + records.reserve(16); + bool have_object_element = false; + std::uint32_t element_count = 0; + std::uint32_t sample_offset_code = 0; + bool reserved_data_not_present = false; + std::size_t object_parsed_end = 0; + (void)reserved_data_not_present; + const std::vector kinds = object_kinds(program, object_count); + + const std::uint32_t declared_elements_base = reader.read(4); + std::uint32_t declared_elements = declared_elements_base; + if (declared_elements_base == 0xFu) { + declared_elements += reader.read(5); + } + if (reader.failed()) { + return syntax("OAMD element count truncated"); + } + if (declared_elements == 0u) { + return variant("missing_object_element", "OAMD declares no element"); + } + element_count = declared_elements; + + bool seen_object = false; + bool seen_trim = false; + bool seen_extended = false; + for (std::uint32_t ordinal = 0; ordinal < element_count; ++ordinal) { + const std::size_t header_start = reader.position(); + const std::uint32_t element_id = reader.read(4); + std::uint32_t size_field = 0; + if (!variable_bits_max(reader, 4, 4, &size_field)) { + return syntax("OAMD element header truncated at ordinal " + + std::to_string(ordinal) + " (header at bit " + + std::to_string(header_start) + ")"); + } + const std::uint32_t size_bytes = size_field + 1u; + const std::size_t region_start = reader.position(); + const std::size_t region_end = + region_start + static_cast(size_bytes) * 8u; + if (region_end > reader.limit()) { + return variant("element_bounds", + "element " + std::to_string(ordinal) + " (id " + + std::to_string(element_id) + ") declares " + + std::to_string(size_bytes) + " bytes, beyond the payload"); + } + const std::uint32_t alternate_data_id = alternate_present ? reader.read(4) : 0u; + reader.read(1); // discard_unknown + if (reader.failed()) { + return syntax("OAMD element control fields truncated at ordinal " + + std::to_string(ordinal)); + } + ElementRecord record; + record.ordinal = ordinal; + record.element_id = element_id; + record.size_bytes = size_bytes; + record.body_end_bit = region_end; + record.parsed_end_bit = region_end; + const bool selected = + (element_id == kObjectElementId || element_id == kTrimElementId || + element_id == kExtendedElementId) && + alternate_data_id == 0u && + !((element_id == kObjectElementId && seen_object) || + (element_id == kTrimElementId && seen_trim) || + (element_id == kExtendedElementId && seen_extended)); + if (!selected) { + if (!reader.skip(region_end - reader.position())) { + return syntax("OAMD element skip past payload end"); + } + } else if (element_id == kObjectElementId) { + const Status parsed = parse_object_element( + reader, object_count, kinds.data(), &state, &sample_offset_code, + &reserved_data_not_present); + if (!parsed.ok()) { + return parsed; + } + record.parsed_end_bit = reader.position(); + object_parsed_end = reader.position(); + seen_object = true; + have_object_element = true; + if (reader.position() < region_end) { + if (!reader.skip(region_end - reader.position())) { + return syntax("OAMD element skip past payload end"); + } + } + } else if (element_id == kExtendedElementId) { + if (!have_object_element) { + return variant("extended_before_object_element", + "OAMD extended object element precedes the default object " + "element"); + } + const Status parsed = + parse_extended_element(reader, &state.objects, state.block_count); + if (!parsed.ok()) { + return parsed; + } + record.parsed_end_bit = reader.position(); + seen_extended = true; + if (reader.position() < region_end) { + if (!reader.skip(region_end - reader.position())) { + return syntax("OAMD element skip past payload end"); + } + } + } else { + OamdTrim trim; + const Status parsed = parse_trim_element(reader, object_count, &trim); + if (!parsed.ok()) { + return parsed; + } + record.parsed_end_bit = reader.position(); + record.has_trim = true; + record.trim = trim; + seen_trim = true; + if (reader.position() < region_end) { + if (!reader.skip(region_end - reader.position())) { + return syntax("OAMD element skip past payload end"); + } + } + } + if (reader.failed()) { + return syntax("OAMD element parse failed at ordinal " + std::to_string(ordinal)); + } + records.push_back(record); + } + if (!have_object_element) { + return variant("missing_object_element", "OAMD has no object element"); + } + + status = check_padding(reader); + if (!status.ok()) { + return status; + } + if (version != 0u) { + return variant("oamd_version", + "OAMD syntax version " + std::to_string(version) + " is not covered"); + } + + // 对外状态:节目分区、trim、元素长度不一致记录。 + fill_program_state(program, &state.program); + state.has_trim = false; + for (const ElementRecord& record : records) { + if (record.parsed_end_bit > record.body_end_bit) { + ElementSizeMismatch mismatch; + mismatch.element_ordinal = record.ordinal; + mismatch.element_id = record.element_id; + mismatch.declared_size_bytes = record.size_bytes; + mismatch.declared_end_bit = static_cast(record.body_end_bit); + mismatch.parsed_end_bit = static_cast(record.parsed_end_bit); + state.size_mismatch.push_back(mismatch); + } + if (record.has_trim && !state.has_trim) { + state.has_trim = true; + state.trim = record.trim; + } + } + + // 旧接口字段:16 槽位置视图、block 0 的时间参数与节目声明。 + if (!state.blocks.empty()) { + out->block_offset_samples = state.blocks[0].block_offset_samples; + out->ramp_duration_samples = state.blocks[0].ramp_duration_samples; + } + out->object_count = object_count; + out->dynamic_object_only = program.dynamic_object_only; + out->lfe_present = program.lfe_present; + 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; + out->has_trim = state.has_trim; + out->trim = state.trim; + fill_legacy_slots(state, out); + out->has_frame_state = true; + out->frame_state = std::move(state); + + if (trace != nullptr) { + trace->bed_assignments = program.bed_assignments; + trace->element_count = element_count; + trace->object_element_count = have_object_element ? 1u : 0u; + trace->sample_offset_code = sample_offset_code; + trace->sample_offset = out->frame_state.sample_offset; + trace->block_count = out->frame_state.block_count; + trace->parsed_end_bit = static_cast(object_parsed_end); + trace->reserved_data_not_present = reserved_data_not_present; + } + *have_object_element_out = have_object_element; + return Status::success(); +} + +Status parse_payload(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out, + ParseTrace* trace, bool require_legacy_shape) { + if (payload == nullptr || out == nullptr) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, "null payload or output"); + } + if (payload_size == 0) { + return variant("header_truncated", "OAMD payload is empty"); + } + bool have_object_element = false; + const Status status = parse_full(payload, payload_size, out, trace, &have_object_element); + if (!status.ok()) { + return status; + } + if (!require_legacy_shape) { + return Status::success(); + } + if (out->object_count > static_cast(kMaxSlots)) { + return variant("object_count", "OAMD object count " + + std::to_string(out->object_count) + + " exceeds the 16-slot model"); + } + if (out->frame_state.block_count != 1u) { + return variant("multiple_position_blocks", + "OAMD frame carries " + + std::to_string(out->frame_state.block_count) + + " position blocks; a single frame_update cannot express that"); + } + return Status::success(); +} + +// 只解析头部与 program_assignment(),供跨层 bed 掩码校验使用。 +Status parse_header_program(const std::uint8_t* payload, std::size_t payload_size, + std::uint32_t* object_count_out, RawProgram* program_out) { + bits::BitReader reader(payload, payload_size); + std::uint32_t version = reader.read(2); + if (reader.failed()) { + return variant("header_truncated", "OAMD payload cannot hold the 2-bit version"); + } + if (version == 3u) { + version += reader.read(3); + if (reader.failed()) { + return syntax("OAMD version extension truncated"); + } + } + std::uint32_t object_count_bits = reader.read(5); + if (object_count_bits == 0x1Fu) { + object_count_bits += reader.read(7); + } + if (reader.failed()) { + return syntax("OAMD object count truncated"); + } + *object_count_out = object_count_bits + 1u; + return parse_program_assignment(reader, program_out); +} + +} // namespace + +int q_of(int k, int n) { + const double value = + std::floor(32768.0 * static_cast(k) / static_cast(n) + 0.5); + const int quantised = static_cast(value); + return quantised > 32767 ? 32767 : quantised; +} + +std::uint32_t standard_bed_mask(std::uint32_t bed_mask) { + std::uint32_t wide = 0; + for (int bit = 0; bit < 10; ++bit) { + if (((bed_mask >> bit) & 1u) != 0u) { + wide |= kStandardBedToWide[bit]; + } + } + return wide; +} + +Status parse_id11(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out) { + return parse_id11_verbose(payload, payload_size, out, nullptr); +} + +Status parse_id11_state(const std::uint8_t* payload, std::size_t payload_size, + OamdUpdate* out) { + if (payload == nullptr || out == nullptr) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, "null payload or output"); + } + if (payload_size == 0) { + return variant("header_truncated", "OAMD payload is empty"); + } + return parse_payload(payload, payload_size, out, nullptr, false); +} + +Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, + OamdUpdate* out, ParseTrace* trace) { + if (payload == nullptr || out == nullptr) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, "null payload or output"); + } + if (payload_size == 0) { + return variant("header_truncated", "OAMD payload is empty"); + } + return parse_payload(payload, payload_size, out, trace, true); +} + +Status parse_id11_bed_assignments(const std::uint8_t* payload, std::size_t payload_size, + std::vector* out) { + if (payload == nullptr || out == nullptr) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, "null payload or output"); + } + if (payload_size == 0) { + return variant("header_truncated", "OAMD payload is empty"); + } + out->clear(); + std::uint32_t object_count = 0; + RawProgram program; + const Status status = parse_header_program(payload, payload_size, &object_count, &program); + if (!status.ok()) { + return status; + } + (void)object_count; *out = program.bed_assignments; return Status::success(); } @@ -478,16 +1387,13 @@ Status parse_trim(const std::uint8_t* payload, std::size_t payload_size, OamdTri return syntax("OAMD object count truncated"); } const std::uint32_t object_count = object_count_bits + 1u; - ProgramInfo program; + RawProgram program; const Status program_status = parse_program_assignment(reader, &program); if (!program_status.ok()) { return program_status; } const std::uint32_t alternate_present = reader.read(1); std::uint32_t element_count = reader.read(4); - if (element_count == 0xFu) { - element_count += reader.read(5); - } if (reader.failed()) { return syntax("OAMD element count truncated"); } @@ -495,11 +1401,13 @@ Status parse_trim(const std::uint8_t* payload, std::size_t payload_size, OamdTri const std::uint32_t element_id = reader.read(4); std::uint32_t size_field = 0; if (!variable_bits_max(reader, 4, 4, &size_field)) { - return syntax("OAMD element header truncated at ordinal " + std::to_string(ordinal)); + return syntax("OAMD element header truncated at ordinal " + + std::to_string(ordinal)); } const std::uint32_t size_bytes = size_field + 1u; const std::size_t region_start = reader.position(); - const std::size_t region_end = region_start + static_cast(size_bytes) * 8u; + const std::size_t region_end = + region_start + static_cast(size_bytes) * 8u; if (region_end > reader.limit()) { return variant("element_bounds", "element " + std::to_string(ordinal) + " (id " + @@ -529,183 +1437,4 @@ Status parse_trim(const std::uint8_t* payload, std::size_t payload_size, OamdTri return Status::success(); } -Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out, - ParseTrace* trace) { - if (payload == nullptr || out == nullptr) { - return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, "null payload or output"); - } - if (payload_size == 0) { - return variant("header_truncated", "OAMD payload is empty"); - } - *out = OamdUpdate{}; - - bits::BitReader reader(payload, payload_size); - std::uint32_t version = reader.read(2); - if (reader.failed()) { - return variant("header_truncated", "OAMD payload cannot hold the 2-bit version"); - } - if (version == 3u) { - version += reader.read(3); - if (reader.failed()) { - return syntax("OAMD version extension truncated"); - } - } - std::uint32_t object_count_bits = reader.read(5); - if (object_count_bits == 0x1Fu) { - object_count_bits += reader.read(7); - } - if (reader.failed()) { - return syntax("OAMD object count truncated"); - } - const std::uint32_t object_count = object_count_bits + 1u; - - ProgramInfo program; - Status status = parse_program_assignment(reader, &program); - if (!status.ok()) { - return status; - } - const std::uint32_t alternate_present = reader.read(1); - if (reader.failed()) { - return syntax("OAMD alternate data flag truncated"); - } - - std::uint32_t element_count = reader.read(4); - if (element_count == 0xFu) { - element_count += reader.read(5); - } - if (reader.failed()) { - return syntax("OAMD element count truncated"); - } - if (element_count == 0u) { - return variant("missing_object_element", "OAMD declares no element"); - } - const std::uint32_t bed_isf_objects = program.num_bed_objects + program.num_isf_objects; - - bool have_object_element = false; - ObjectElementInfo object_element; - for (std::uint32_t ordinal = 0; ordinal < element_count; ++ordinal) { - const std::size_t header_start = reader.position(); - const std::uint32_t element_id = reader.read(4); - std::uint32_t size_field = 0; - if (!variable_bits_max(reader, 4, 4, &size_field)) { - return syntax("OAMD element header truncated at ordinal " + std::to_string(ordinal)); - } - const std::uint32_t size_bytes = size_field + 1u; - const std::size_t region_start = reader.position(); - const std::size_t region_end = region_start + static_cast(size_bytes) * 8u; - if (region_end > reader.limit()) { - return variant("element_bounds", - "element " + std::to_string(ordinal) + " (id " + - std::to_string(element_id) + ") declares " + - std::to_string(size_bytes) + - " bytes, beyond the payload (header at bit " + - std::to_string(header_start) + ")"); - } - if (alternate_present != 0u) { - reader.read(4); - } - reader.read(1); - if (reader.failed()) { - return syntax("OAMD element control fields truncated at ordinal " + - std::to_string(ordinal)); - } - - if (element_id == kObjectElementId) { - if (have_object_element) { - return variant("multiple_object_elements", "OAMD contains several object elements"); - } - have_object_element = true; - const Status parsed = parse_object_element(reader, object_count, bed_isf_objects, - &object_element, out); - if (!parsed.ok()) { - return parsed; - } - if (trace != nullptr) { - trace->parsed_end_bit = static_cast(reader.position()); - } - if (reader.position() < region_end) { - reader.set_limit_bits(reader.limit()); - reader.skip(region_end - reader.position()); - } - } else if (element_id == kTrimElementId) { - OamdTrim trim; - const Status parsed = parse_trim_element(reader, object_count, &trim); - if (!parsed.ok()) { - return parsed; - } - if (!out->has_trim) { - out->trim = trim; - out->has_trim = true; - } - if (reader.position() < region_end) { - reader.set_limit_bits(reader.limit()); - reader.skip(region_end - reader.position()); - } - } else { - if (!reader.skip(region_end - reader.position())) { - return syntax("OAMD element skip past payload end"); - } - } - if (reader.failed()) { - return syntax("OAMD element parse failed at ordinal " + std::to_string(ordinal)); - } - } - - if (!have_object_element) { - return variant("missing_object_element", "OAMD has no object element"); - } - - // Trailing padding must be zero over the whole remainder. - { - bits::BitReader tail(payload, payload_size); - tail.reset(payload, payload_size, reader.position()); - while (tail.remaining_bits() > 0) { - const std::size_t chunk = - tail.remaining_bits() > 32u ? 32u : tail.remaining_bits(); - if (tail.read(static_cast(chunk)) != 0u) { - return Status::fail(JOC_ERR_BITSTREAM_PADDING, stage::kOamd, - "OAMD payload padding is not zero (from bit " + - std::to_string(reader.position()) + ")"); - } - } - } - - if (version != 0u) { - return variant("oamd_version", - "OAMD syntax version " + std::to_string(version) + " is not covered"); - } - if (object_count > static_cast(kMaxSlots)) { - return variant("object_count", "OAMD object count " + std::to_string(object_count) + - " exceeds the 16-slot model"); - } - if (object_element.block_count != 1u) { - return variant("multiple_position_blocks", - "OAMD frame carries " + std::to_string(object_element.block_count) + - " position blocks; a single frame_update cannot express that"); - } - - out->block_offset_samples = object_element.block_offset_samples; - out->ramp_duration_samples = object_element.ramp_duration_samples; - out->object_count = object_count; - out->dynamic_object_only = program.dynamic_object_only; - out->lfe_present = program.lfe_present; - 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; - trace->element_count = element_count; - trace->object_element_count = have_object_element ? 1u : 0u; - trace->sample_offset_code = object_element.sample_offset_code; - trace->sample_offset = object_element.sample_offset; - trace->block_count = object_element.block_count; - trace->reserved_data_not_present = object_element.reserved_data_not_present; - } - return Status::success(); -} - } // namespace joc::oamd diff --git a/src/oamd/oamd_parser.h b/src/oamd/oamd_parser.h index 4a95478..1c12b70 100644 --- a/src/oamd/oamd_parser.h +++ b/src/oamd/oamd_parser.h @@ -9,6 +9,34 @@ #include "foundation/status.h" #include "joc_core.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。 +// The enum values are the reference's BED_LABEL_INDEX - 1. +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, +}; + +} // namespace joc::joc + namespace joc::oamd { inline constexpr int kMaxSlots = 16; @@ -17,8 +45,16 @@ inline constexpr int kAxes = 3; inline constexpr int kNQ12 = 62; inline constexpr int kNQ3 = 15; inline constexpr int kQ15Scale = 32768; +// Largest ISF configuration of Table 11b (5.6.0.11). A frame cannot carry more +// objects than this through any of the standard programmes, but the parser does +// not reject a larger count: it only bounds the legacy 16-slot view. +inline constexpr int kMaxObjects = 30; +inline constexpr int kMaxBlocks = 8; inline constexpr std::uint32_t kTrimElementId = 2; +inline constexpr std::uint32_t kObjectElementId = 1; +inline constexpr std::uint32_t kExtendedElementId = 5; inline constexpr int kTrimConfigs = 9; +inline constexpr int kBedLabelCount = 17; // One of the nine per-configuration trim records of the OAMD trim element. // ``kAbsent`` means the configuration carries no data (the element only @@ -50,6 +86,92 @@ struct OamdTrim { bool disable_per_object[kMaxSlots] = {}; }; +// OAMD object kind: the programme assigns objects to the bed, to an ISF +// configuration, or to the dynamic (positional) set, in that order. +enum class ObjectKind : std::uint8_t { + kBed = 0, + kIsf = 1, + kDynamic = 2, +}; + +// Render state of one object in one block (5.5.9 object_info_block). +struct ObjectRenderState { + bool not_active = false; + std::int32_t gain_q = 0; // integer gain, unity = 4096 + std::int32_t priority_q = 0; // Q10 priority, 32767 = default + std::int32_t qx = 0; // Q15 room position, 0..32767 + std::int32_t qy = 0; // Q15 room position, 0..32767 + std::int32_t qz = 0; // signed Q15 elevation + std::uint8_t zone_idx = 0; + bool enable_elevation = true; + std::uint8_t size_mode = 0; // 0 none, 1 isotropic, 2 per-axis + std::uint16_t size_q[3] = {0, 0, 0}; // Q15 size per axis + bool snap = false; + std::uint16_t divergence_q = 0; // Q15 divergence width +}; + +struct ObjectBlockState { + ObjectRenderState render; +}; + +// One object of the frame: its kind and the state of every block. +struct ObjectElementState { + ObjectKind kind = ObjectKind::kDynamic; + std::vector blocks; +}; + +// Timing shared by every object of one block. ``block_offset_samples`` already +// includes the element's sample_offset, like the reference block dictionary. +struct BlockTiming { + std::uint32_t block_offset_samples = 0; + std::uint32_t ramp_duration_samples = 0; +}; + +// Program assignment (5.5.3) as consumed by the speaker renderer. +struct ProgramState { + bool dynamic_object_only = false; + bool lfe_present = false; + bool b_bed_chan_distribute = false; + // program_assignment() content_description flags. + bool content_reserved = 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; // -1 when the frame declares none + std::uint32_t bed_mask = 0; // 17-bit non-standard bed mask + std::int32_t isf_idx = -1; // -1 when the frame declares none + // Declared bed labels in channel order, including LFE/LFE2. Index i is the + // label of object i for i < num_bed_objects. + std::vector<::joc::joc::BedChannel> bed_labels; +}; + +// One element whose parsed content ended past its declared size. The reference +// keeps parsing such elements and only records the difference. +struct ElementSizeMismatch { + std::uint32_t element_ordinal = 0; + std::uint32_t element_id = 0; + std::uint32_t declared_size_bytes = 0; + std::uint32_t declared_end_bit = 0; + std::uint32_t parsed_end_bit = 0; +}; + +// Full frame state (frame_state() in the reference): per-object per-block +// render state plus programme assignment, timing and trim. +struct FrameState { + std::uint32_t object_count = 0; + std::uint32_t block_count = 0; + std::uint32_t sample_offset = 0; // object_element sample offset + std::uint32_t ramp_duration_samples = 0; // block 0 + std::vector blocks; + std::vector objects; + ProgramState program; + bool has_trim = false; + OamdTrim trim; + std::vector size_mismatch; +}; + // (the reference's None), i.e. hold the previous position. struct SlotUpdate { std::int16_t q1 = -1; @@ -76,6 +198,12 @@ struct OamdUpdate { // keeps whatever controls the caller already holds. OamdTrim trim; bool has_trim = false; + // Full per-object per-block state of this payload. ``has_frame_state`` is + // true whenever the parser produced it; the speaker metadata path and the + // binaural position timeline consume it, while the legacy slot fields above + // stay authoritative for ADM export and the point-renderer path. + bool has_frame_state = false; + FrameState frame_state; }; class OamdState { @@ -103,6 +231,23 @@ public: std::int16_t q(int slot, int axis) const { return q_[slot][axis]; } + // Resolves one frame-state position block for one object. The legacy + // apply() entry above keeps the reference frame_update() view (16 slots, + // block 0); the binaural position timeline uses this entry so a frame may + // carry more objects and more blocks than that view can express. Callers + // skip inactive and non-dynamic objects themselves, exactly like the + // reference's frame_update() value construction. + void apply_render_state(int object_index, const ObjectRenderState& render) { + if (object_index < 0 || object_index >= kMaxObjects) { + return; + } + q_[object_index][0] = static_cast(render.qx); + q_[object_index][1] = static_cast(render.qy); + const std::int32_t elevation = render.enable_elevation ? render.qz : 0; + q_[object_index][2] = + static_cast(elevation > 0 ? elevation : 0); + } + // Objects 1..15 as q15 triples, the layout the speaker renderer consumes. void object_positions_q15(std::uint16_t out[kObjects][kAxes]) const { for (int object = 0; object < kObjects; ++object) { @@ -113,25 +258,18 @@ public: } private: - std::int16_t q_[kMaxSlots][kAxes] = {}; + std::int16_t q_[kMaxObjects][kAxes] = {}; }; // q_of(k, n) = min(32767, floor(32768 k / n + 0.5)), the reference's quantiser. int q_of(int k, int n); -Status parse_id11(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out); - -// Parses only the trim element of a payload. ``out`` receives the first trim -// element found; ``*present`` is false when the payload carries none. decode -// paths that need the OAMD calibration but no object state can use this -// without requiring an object element. -Status parse_trim(const std::uint8_t* payload, std::size_t payload_size, OamdTrim* out, - bool* present); - struct BedAssignment { bool lfe_only = false; bool standard = false; - std::uint32_t mask = 0; + std::uint32_t mask = 0; // 10-bit standard or 17-bit non-standard mask + // Declared channels in channel order; LFE is kept in place. + std::vector<::joc::joc::BedChannel> channels; }; struct ParseTrace { @@ -145,6 +283,26 @@ struct ParseTrace { bool reserved_data_not_present = false; }; +// Legacy single-frame view (frame_update() in the reference): the 16-slot +// position update plus programme and trim. A payload carrying more than 16 +// objects or more than one position block is rejected, exactly like the +// reference legacy entry point. The ADM export path keeps this view; the +// speaker and binaural renderers use parse_id11_state(). +Status parse_id11(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out); + +// Full frame_state() view: per-object per-block state, no 16-slot or +// single-block restriction. The legacy fields are filled as well whenever the +// frame fits them, so a caller can parse once and hand the same update to the +// renderers and to the ADM export path where its shape allows it. +Status parse_id11_state(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out); + +// Parses only the trim element of a payload. ``out`` receives the first trim +// element found; ``*present`` is false when the payload carries none. decode +// paths that need the OAMD calibration but no object state can use this +// without requiring an object element. +Status parse_trim(const std::uint8_t* payload, std::size_t payload_size, OamdTrim* out, + bool* present); + Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, OamdUpdate* out, ParseTrace* trace); @@ -153,4 +311,7 @@ Status parse_id11_verbose(const std::uint8_t* payload, std::size_t payload_size, Status parse_id11_bed_assignments(const std::uint8_t* payload, std::size_t payload_size, std::vector* out); +// 10-bit standard bed mask -> 17-bit non-standard bed mask (5.6.1.1.4). +std::uint32_t standard_bed_mask(std::uint32_t bed_mask); + } // namespace joc::oamd diff --git a/src/speaker/speaker_layout_lookup.cpp b/src/speaker/speaker_layout_lookup.cpp index 84a4f29..f46d776 100644 --- a/src/speaker/speaker_layout_lookup.cpp +++ b/src/speaker/speaker_layout_lookup.cpp @@ -8,32 +8,55 @@ namespace { struct FrozenLayout { const char* name; + const char* compact_name; std::uint32_t out_ch_config; std::uint32_t speaker_bitfield; std::uint32_t surround_channels; std::uint32_t height_channels; bool carries_trim; + const char* const* channel_names; }; +const char* const kChannels20[] = {"L", "R"}; +const char* const kChannels31[] = {"L", "R", "C", "LFE"}; +const char* const kChannels40[] = {"L", "R", "Lrs", "Rrs"}; +const char* const kChannels51[] = {"L", "R", "C", "LFE", "Ls", "Rs"}; +const char* const kChannels71[] = {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs"}; +const char* const kChannels512[] = {"L", "R", "C", "LFE", "Ls", "Rs", "Ltm", "Rtm"}; +const char* const kChannels514[] = {"L", "R", "C", "LFE", "Ls", "Rs", "Ltf", "Rtf", + "Ltr", "Rtr"}; +const char* const kChannels712[] = {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", + "Ltm", "Rtm"}; +const char* const kChannels714[] = {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", + "Ltf", "Rtf", "Ltr", "Rtr"}; +const char* const kChannels914[] = {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", + "Lw", "Rw", "Ltf", "Rtf", "Ltr", "Rtr"}; +const char* const kChannels916[] = {"L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", + "Lw", "Rw", "Ltf", "Rtf", "Ltm", "Rtm", "Ltr", "Rtr"}; +const char* const kChannels60[] = {"L", "R", "Ls", "Rs", "Lrs", "Rrs"}; + constexpr FrozenLayout kLayouts[kLayoutCount] = { - {"2.0", 0, 1, 0, 0, true}, - {"3.1", 3, 7, 0, 0, true}, - {"4.0", 0xFF, 9, 2, 0, true}, - {"5.1", 7, 15, 2, 0, true}, - {"7.1", 11, 31, 4, 0, true}, - {"5.1.2", 13, 1039, 2, 2, true}, - {"5.1.4", 14, 2575, 2, 4, true}, - {"7.1.2", 15, 1055, 4, 2, false}, - {"7.1.4", 16, 2591, 4, 4, false}, - {"9.1.4", 19, 2719, 4, 4, false}, - {"9.1.6", 20, 3743, 4, 6, false}, + {"2.0", "20", 0, 1, 0, 0, true, kChannels20}, + {"3.1", "31", 3, 7, 0, 0, true, kChannels31}, + {"4.0", "40", 0xFF, 9, 2, 0, true, kChannels40}, + {"5.1", "51", 7, 15, 2, 0, true, kChannels51}, + {"7.1", "71", 11, 31, 4, 0, true, kChannels71}, + {"5.1.2", "512", 13, 1039, 2, 2, true, kChannels512}, + {"5.1.4", "514", 14, 2575, 2, 4, true, kChannels514}, + {"7.1.2", "712", 15, 1055, 4, 2, false, kChannels712}, + {"7.1.4", "714", 16, 2591, 4, 4, false, kChannels714}, + {"9.1.4", "914", 19, 2719, 4, 4, false, kChannels914}, + {"9.1.6", "916", 20, 3743, 4, 6, false, kChannels916}, + {"6.0", "60", 0xFF, 25, 4, 0, false, kChannels60}, }; void fill(const FrozenLayout& source, LayoutInfo* out) { out->name = source.name; + out->compact_name = source.compact_name; out->out_ch_config = source.out_ch_config; out->speaker_bitfield = source.speaker_bitfield; out->channel_count = ejoc_speaker_layout_channel_count(source.speaker_bitfield); + out->channel_names = source.channel_names; out->surround_channels = source.surround_channels; out->height_channels = source.height_channels; out->carries_trim = source.carries_trim; diff --git a/src/speaker/speaker_layout_lookup.h b/src/speaker/speaker_layout_lookup.h index e225cc9..b7c8ce1 100644 --- a/src/speaker/speaker_layout_lookup.h +++ b/src/speaker/speaker_layout_lookup.h @@ -8,13 +8,18 @@ namespace joc::speaker { -inline constexpr int kLayoutCount = 11; +inline constexpr int kLayoutCount = 12; struct LayoutInfo { const char* name = ""; + // The reference's compact layout key ("2.0" -> "20", ...), the key of the + // automatic-attenuation and divide-by-default tables. + const char* compact_name = ""; std::uint32_t out_ch_config = 0; std::uint32_t speaker_bitfield = 0; std::uint32_t channel_count = 0; + // Standard channel names in output order, ``channel_count`` entries. + const char* const* channel_names = nullptr; // Ls/Rs/Lrs/Rrs and Ltf/Rtf/Ltr/Rtr/Ltm/Rtm, the labels the OAMD trim // configuration index counts. std::uint32_t surround_channels = 0; @@ -24,8 +29,8 @@ struct LayoutInfo { bool carries_trim = false; }; -// All eleven layouts in the reference's order (2.0, 3.1, 4.0, 5.1, 7.1, 5.1.2, -// 5.1.4, 7.1.2, 7.1.4, 9.1.4, 9.1.6). +// All twelve layouts in the reference's order (2.0, 3.1, 4.0, 5.1, 7.1, 5.1.2, +// 5.1.4, 7.1.2, 7.1.4, 9.1.4, 9.1.6, 6.0). bool layout_at(int index, LayoutInfo* out); // False when the name is unknown (case-sensitive, as the reference CLI). "4.0" diff --git a/src/speaker/speaker_layouts.h b/src/speaker/speaker_layouts.h index e17586c..7f67cf3 100644 --- a/src/speaker/speaker_layouts.h +++ b/src/speaker/speaker_layouts.h @@ -39,7 +39,7 @@ struct LayoutGeometry { std::array regions{}; }; -inline constexpr std::array kLayouts{{ +inline constexpr std::array kLayouts{{ LayoutGeometry{ 0x1u, 0, 2, {{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, @@ -159,9 +159,9 @@ inline constexpr std::array kLayouts{{ {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ - 2, 1, 1, 0, - {{SpeakerPoint{{0, 32767, 0}, 2}, SpeakerPoint{{32767, 32767, 0}, 3}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, + 4, 2, 2, 0, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{0, 32767, 0}, 2}, SpeakerPoint{{32767, 32767, 0}, 3}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{AxisGroup{2, {0, 1, 0}}, AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ @@ -190,25 +190,25 @@ inline constexpr std::array kLayouts{{ {{ RegionGeometry{ 5, 2, 2, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ 5, 2, 2, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ 5, 2, 2, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ 3, 2, 2, 0, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, @@ -220,7 +220,7 @@ inline constexpr std::array kLayouts{{ }, RegionGeometry{ 2, 1, 1, 0, - {{SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, @@ -286,25 +286,25 @@ inline constexpr std::array kLayouts{{ {{ RegionGeometry{ 7, 3, 2, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ 7, 3, 2, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ 7, 3, 2, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ 5, 3, 2, 1, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, @@ -316,7 +316,7 @@ inline constexpr std::array kLayouts{{ }, RegionGeometry{ 4, 3, 1, 1, - {{SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} }, @@ -334,25 +334,25 @@ inline constexpr std::array kLayouts{{ {{ RegionGeometry{ 9, 3, 2, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ 9, 3, 2, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ 9, 3, 2, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}} }, RegionGeometry{ 7, 3, 2, 2, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}}} }, @@ -364,7 +364,7 @@ inline constexpr std::array kLayouts{{ }, RegionGeometry{ 6, 3, 1, 2, - {{SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, {{AxisGroup{2, {2, 3, 0}}, AxisGroup{2, {4, 5, 0}}, AxisGroup{}, AxisGroup{}}} }, @@ -567,7 +567,55 @@ inline constexpr std::array kLayouts{{ {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} } }} - } + }, + LayoutGeometry{ + 0x19u, 0xFFu, 6, + {{0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + {{ + RegionGeometry{ + 6, 2, 3, 0, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{0, 16384, 0}, 2}, SpeakerPoint{{32767, 16384, 0}, 3}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{AxisGroup{2, {0, 1, 0}}, AxisGroup{2, {2, 3, 0}}, AxisGroup{2, {4, 5, 0}}, AxisGroup{}}}, + {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} + }, + RegionGeometry{ + 4, 2, 2, 0, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{0, 16384, 0}, 2}, SpeakerPoint{{32767, 16384, 0}, 3}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{AxisGroup{2, {0, 1, 0}}, AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}}}, + {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} + }, + RegionGeometry{ + 4, 2, 2, 0, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{AxisGroup{2, {0, 1, 0}}, AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}}}, + {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} + }, + RegionGeometry{ + 2, 2, 1, 0, + {{SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, + {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} + }, + RegionGeometry{ + 2, 1, 1, 0, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, + {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} + }, + RegionGeometry{ + 4, 2, 2, 0, + {{SpeakerPoint{{0, 16384, 0}, 2}, SpeakerPoint{{32767, 16384, 0}, 3}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{AxisGroup{2, {0, 1, 0}}, AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}}}, + {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} + }, + RegionGeometry{ + 2, 1, 1, 0, + {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, + {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, + {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} + } + }} + }, }}; inline constexpr const LayoutGeometry* find_layout(const std::uint32_t speaker_bitfield) noexcept { diff --git a/src/speaker/speaker_meta.h b/src/speaker/speaker_meta.h new file mode 100644 index 0000000..3745570 --- /dev/null +++ b/src/speaker/speaker_meta.h @@ -0,0 +1,90 @@ +// Port of the metadata application path of src/speaker_renderer.py HEAD. +// +// The metadata path consumes the full per-object per-block OAMD frame state and +// schedules block targets frame by frame: object gain/inactive, zone/elevation, +// snap, divergence, size kernel fusion, priority size budget, ISF basis rows and +// bed channel distribution, with the same float64 gain ramps and block alignment +// as the reference renderer. + +#pragma once + +#include +#include +#include + +#include "eac3joc_core.h" +#include "foundation/status.h" +#include "oamd/oamd_parser.h" +#include "speaker/speaker_layout_lookup.h" +#include "speaker/speaker_trim.h" + +namespace joc::speaker { + +// One metadata target matrix: object_count x layout channel_count in the +// renderer's internal channel order, before the ramp/timing stage. +struct MetadataTargets { + std::uint32_t object_count = 0; + std::uint32_t channel_count = 0; + std::vector values; // object major + + double at(std::uint32_t object, std::uint32_t channel) const { + return values[static_cast(object) * channel_count + channel]; + } +}; + +// Computes the metadata target matrix of one block of a parsed frame without +// touching renderer state: the reference's _init_meta validation plus +// _block_targets. ``trim`` carries the current programme trim (bypass flags), +// ``controls`` the derived per-layout controls, ``size_budget`` the priority +// budget (0 means unlimited, the production value). +Status metadata_block_targets(const LayoutInfo& layout, const oamd::OamdUpdate& update, + std::uint32_t block_index, + const ejoc_speaker_controls& controls, + const oamd::OamdTrim* trim, std::uint32_t size_budget, + MetadataTargets* out); + +// Stateful metadata-path renderer. It owns the object target ramps and the +// cross-block pending schedule; the legacy point-renderer state stays in the +// C ABI renderer, which is used for the blocks of a frame that precede the first +// metadata targets (the reference renders those with its legacy block path). +class MetaRenderer { +public: + MetaRenderer(); + ~MetaRenderer(); + MetaRenderer(MetaRenderer&&) noexcept; + MetaRenderer& operator=(MetaRenderer&&) noexcept; + MetaRenderer(const MetaRenderer&) = delete; + MetaRenderer& operator=(const MetaRenderer&) = delete; + + // Binds the target layout. Must be called once before process_frame(). + Status configure(const LayoutInfo& layout); + + // Drops the ramp, schedule and metadata state (the layout stays bound). + Status reset(); + + bool configured() const; + // True once a metadata frame has been accepted (the reference's _meta_mode). + bool engaged() const; + + const char* last_error() const; + + Status process_frame(ejoc_speaker_renderer_handle legacy, + const float* objects16_interleaved, std::uint32_t sample_count, + const oamd::OamdUpdate* update, std::uint32_t metadata_offset, + const ejoc_speaker_controls& controls, const oamd::OamdTrim* trim, + std::uint32_t raw_trim_mode, std::uint32_t size_budget, + double* output_interleaved); + + // Computes the target matrix of one block of ``update`` into ``out`` using a + // scratch context; unlike metadata_block_targets() this validates through + // the renderer's own state-independent path. + Status block_targets(const oamd::OamdUpdate& update, std::uint32_t block_index, + const ejoc_speaker_controls& controls, const oamd::OamdTrim* trim, + std::uint32_t size_budget, MetadataTargets* out) const; + +private: + struct Impl; + std::unique_ptr impl_; +}; + +} // namespace joc::speaker diff --git a/src/speaker/speaker_renderer.cpp b/src/speaker/speaker_renderer.cpp index 9ae1418..3fc035e 100644 --- a/src/speaker/speaker_renderer.cpp +++ b/src/speaker/speaker_renderer.cpp @@ -1,5 +1,13 @@ +// The shared library compiles this unit with JOC_BUILD_DLL and exports the C +// entry points; the static joc_core_impl build compiles it with EJOC_STATIC and +// keeps them local, so the two libraries can be linked into the same tool. +#if defined(JOC_BUILD_DLL) && !defined(EJOC_STATIC) #define EJOC_BUILD_DLL +#endif #include "eac3joc_core.h" +#include "joc_bitstream/bed_layout_map.h" +#include "oamd/oamd_math.h" +#include "speaker/speaker_meta.h" #include "speaker/speaker_trim.h" #include "speaker_layouts.h" @@ -9,7 +17,12 @@ #include #include #include +#include +#include #include +#include +#include +#include namespace ejoc::speaker { @@ -269,6 +282,14 @@ public: return 0; } + // Renders one 32-sample legacy block into an already zeroed interleaved + // output, using the same point-renderer state as process(). The metadata + // path consumes this for the blocks that precede its first targets. + void render_legacy_block(const float* input, const std::size_t block, + double* output) noexcept { + mix_block(input, output, block, (layout_->speaker_bitfield & 0x4u) != 0); + } + private: // Warp 2/3 render the 5.1-family and 4.0 layouts through an expanded // surround layout, then fold the rear pair into the target surround pair. @@ -749,3 +770,1080 @@ int EJOC_CALL ejoc_speaker_renderer_process( } } // extern "C" + +// Metadata application path (src/speaker_renderer.py HEAD). The legacy point +// renderer above stays untouched: this section owns the block-state path. +#include "speaker/speaker_meta.h" + +#include "joc_bitstream/bed_layout_map.h" +#include "oamd/oamd_math.h" + +namespace joc::speaker { + +namespace { + +constexpr int kBlockSamples = EJOC_SPEAKER_BLOCK_SAMPLES; +constexpr int kOutputChannels = EJOC_OUTPUT_CHANNELS; +constexpr double kQ15 = 32768.0; +constexpr double kQ15MaxValue = 32767.0 / 32768.0; +constexpr double kGainSnapThreshold = 1.0e-4; +constexpr double kPi = 3.141592653589793238462643383279502884; +constexpr int kZoneToRegion[8] = {0, 1, 2, 3, 4, 5, 6, 0}; +constexpr int kLfeBedLabel = 4; // Python BED_LABEL_INDEX["RC_LFE"] +constexpr int kLfe2BedLabel = 17; // Python BED_LABEL_INDEX["RC_LFE2"] + +inline double clamp(const double value, const double low, const double high) { + return value < low ? low : (value > high ? high : value); +} + +// The reference's _stable_norm: tiny-norm floor and unit clamp in float64. +std::vector stable_norm(const std::vector& vector) { + double total = 0.0; + for (const double value : vector) { + total += value * value; + } + std::vector result(vector.size(), 0.0); + if (total == 0.0) { + return result; + } + const double denominator = std::max(std::sqrt(total), 1.0e-5); + for (std::size_t index = 0; index < vector.size(); ++index) { + result[index] = std::min(vector[index] / denominator, 1.0); + } + return result; +} + +void equal_power_pair(const double position, double& lower, double& upper) { + const double angle = kPi * 0.5 * position; + lower = std::cos(angle); + upper = std::sin(angle); +} + +std::uint32_t align_metadata_sample(const std::uint32_t sample) { + return static_cast( + kBlockSamples * ((static_cast(sample) + kBlockSamples / 2 - 1) / + kBlockSamples)); +} + +std::uint32_t ramp_block_count(const std::uint32_t duration_samples) { + return static_cast( + (static_cast(duration_samples) + kBlockSamples / 2 - 1) / + kBlockSamples); +} + +// One 16x16 channel matrix, rows indexed by the target layout's channel id. +struct ChannelMatrix { + double value[EJOC_OUTPUT_CHANNELS][EJOC_OUTPUT_CHANNELS] = {}; +}; + +void make_identity(ChannelMatrix& matrix) { + for (int row = 0; row < kOutputChannels; ++row) { + for (int column = 0; column < kOutputChannels; ++column) { + matrix.value[row][column] = row == column ? 1.0 : 0.0; + } + } +} + +// Copies shared speakers and folds the rear pair into the target surround pair, +// exactly like the reference's _surround_fold_matrix. +void make_fold_matrix(const std::uint32_t source_bitfield, + const std::uint32_t target_bitfield, const int warp_mode, + ChannelMatrix& matrix) { + make_identity(matrix); + const std::uint64_t source_mask = ejoc::speaker::expand_speaker_bitfield(source_bitfield); + const std::uint64_t target_mask = ejoc::speaker::expand_speaker_bitfield(target_bitfield); + int source_ids[64]; + int target_ids[64]; + std::size_t source_count = 0; + std::size_t target_count = 0; + for (int id = 0; id < 64; ++id) { + if (((source_mask >> id) & 1ULL) != 0u) { + source_ids[source_count++] = id; + } + if (((target_mask >> id) & 1ULL) != 0u) { + target_ids[target_count++] = id; + } + } + for (auto& row : matrix.value) { + for (double& value : row) { + value = 0.0; + } + } + const auto position = [&](const int id) { + for (std::size_t index = 0; index < target_count; ++index) { + if (target_ids[index] == id) { + return static_cast(index); + } + } + return -1; + }; + const double same_side = warp_mode == 2 ? std::sqrt(3.0) / 2.0 : 1.0; + const double cross_side = warp_mode == 2 ? 0.5 : 0.0; + for (std::size_t column = 0; column < source_count; ++column) { + const int id = source_ids[column]; + const int direct = position(id); + if (direct >= 0) { + matrix.value[direct][column] = 1.0; + continue; + } + if (id != 6 && id != 7) { + continue; + } + const int same = position(id - 2); + const int cross = position(id == 6 ? 5 : 4); + if (same >= 0) { + matrix.value[same][column] = same_side; + } + if (cross >= 0) { + matrix.value[cross][column] = cross_side; + } + } +} + +// Uncompensated point-source vector in internal channel order (the reference's +// _region_vector). +void region_vector(const ejoc::speaker_tables::LayoutGeometry& layout, + const int region_index, const double u, const double floor_v, + const double top_v, const double w, const bool enable_height, + std::vector& vector) { + using PointGains = std::array; + vector.assign(layout.channel_count, 0.0); + const auto& region = layout.regions[static_cast(region_index)]; + PointGains floor{}; + ejoc::speaker::plane_gains(region, region.axis0_groups, region.axis0_group_count, u, + floor_v, region.mode, floor); + PointGains gains = floor; + if (region.mode == 3) { + PointGains top{}; + ejoc::speaker::plane_gains(region, region.axis1_groups, region.axis1_group_count, u, + top_v, 3, top); + const double height = + enable_height ? clamp(w, 0.0, kQ15MaxValue) : 0.0; + if (height >= kQ15MaxValue) { + gains = top; + } else if (height > 0.0) { + double floor_weight = 0.0; + double height_weight = 0.0; + equal_power_pair(height, floor_weight, height_weight); + for (std::size_t index = 0; index < region.point_count; ++index) { + gains[index] = floor[index] * floor_weight + top[index] * height_weight; + } + } + } + for (std::size_t point = 0; point < region.point_count; ++point) { + vector[region.points[point].speaker_id] = gains[point]; + } +} + +double layout_attenuation(const std::uint32_t bitfield) { + return ejoc::speaker::layout_attenuation_db(bitfield); +} + +// The reference's position_gain: float64 layout compensation of one point. +double point_position_gain(const std::uint32_t bitfield, const double v, const double w) { + const double y_term = clamp(v / 0.6, 0.0, 1.0); + const double z_term = clamp((w - 0.2) / 0.8, 0.0, 1.0); + const double amount = clamp(y_term + z_term, 0.0, 1.0); + return std::pow(10.0, layout_attenuation(bitfield) * amount / 20.0); +} + +int warp_floor_exponent(const std::uint32_t bitfield, const std::uint32_t warp_mode) { + if (warp_mode == 0) { + return 0; + } + return ejoc::speaker::floor_y_exponent(bitfield); +} + +int zone_region(const std::uint8_t zone_idx) { + return kZoneToRegion[static_cast(zone_idx) & 7]; +} + +} // namespace + +struct MetaRenderer::Impl { + struct PendingTargets { + std::vector targets; + std::uint32_t ramp_samples = 0; + }; + + bool configured = false; + LayoutInfo info{}; + const ejoc::speaker_tables::LayoutGeometry* geometry = nullptr; + const ejoc::speaker_tables::LayoutGeometry* calc = nullptr; + LayoutInfo calc_info{}; + bool fold_possible = false; // the final layout expands for warp 2/3 + ChannelMatrix fold_pl2x{}; + ChannelMatrix fold_loro{}; + int channels = 0; + int calc_channels = 0; + // Output (standard) channel index of every internal channel. The layout + // table stores internal_to_standard, so the renderer needs its inverse. + std::array standard_for_internal{}; + double window[kBlockSamples] = {}; + + bool meta_mode = false; + bool meta_ready = false; + std::uint32_t n_meta = 0; + std::uint32_t raw_trim_mode = 0; + std::vector current; + std::vector target; + std::vector step; + std::vector remaining; + std::vector slots; + std::vector labels; + std::unordered_map isf_cache; + std::map> pending; + std::string error; + + Status fail(const joc_error code, const char* reason, const std::string& message) { + error = std::string("speaker: ") + reason + ": " + message; + return Status::fail(code, stage::kRender, error); + } + + Status configure(const LayoutInfo& layout); + void reset_state(); + bool bypassed(const oamd::OamdTrim* trim, const int object_index) const; + Status init_meta(const oamd::FrameState& frame); + Status compute_targets(const oamd::FrameState& frame, const std::uint32_t block_index, + const ejoc_speaker_controls& controls, + const oamd::OamdTrim* trim, const std::uint32_t size_budget, + std::vector* targets); + void dynamic_spatial(const oamd::ObjectRenderState& state, const std::uint32_t warp_mode, + const ejoc_speaker_controls& controls, const oamd::OamdTrim* trim, + const int object_index, const bool force_zero_size, + std::vector& spatial); + void bed_spatial(const oamd::ProgramState& program, const int label, + const std::uint32_t warp_mode, const ejoc_speaker_controls& controls, + const oamd::OamdTrim* trim, const int object_index, + std::vector& spatial) const; + void isf_spatial(const oamd::ProgramState& program, const int object_index, + const std::uint32_t warp_mode, const oamd::OamdTrim* trim, + std::vector& spatial); + void fold_vector(const std::vector& input, const std::uint32_t warp_mode, + std::vector& output) const; + void apply_targets(const std::vector& targets, const std::uint32_t ramp_samples); + void render_meta_block(const float* input, const std::uint32_t block, double* output); + Status schedule(const oamd::OamdUpdate& update, const std::uint32_t event_sample, + const ejoc_speaker_controls& controls, const oamd::OamdTrim* trim, + const std::uint32_t size_budget); +}; + +Status MetaRenderer::Impl::configure(const LayoutInfo& layout) { + configured = false; + info = layout; + geometry = ejoc::speaker_tables::find_layout(layout.speaker_bitfield); + if (geometry == nullptr || geometry->channel_count == 0) { + return fail(JOC_ERR_LAYOUT_UNSUPPORTED, "layout_unavailable", + std::string("no geometry for layout ") + layout.name); + } + channels = geometry->channel_count; + if (channels > kOutputChannels) { + return fail(JOC_ERR_LAYOUT_UNSUPPORTED, "layout_unavailable", + "speaker layout has too many channels"); + } + for (int index = 0; index < kBlockSamples; ++index) { + window[index] = static_cast(index) / static_cast(kBlockSamples); + } + for (int internal = 0; internal < channels; ++internal) { + standard_for_internal[static_cast(internal)] = + static_cast(internal); + } + for (int standard = 0; standard < channels; ++standard) { + standard_for_internal[geometry->standard_from_internal[static_cast( + standard)]] = static_cast(standard); + } + + // Warp 2/3 render the 5.1-family and 4.0 layouts through the expanded + // surround layout and fold the rear pair back (reference + // _surround_fold_layout). Everything else renders directly. + calc = geometry; + calc_info = info; + fold_possible = false; + if (ejoc::speaker::floor_y_exponent(layout.speaker_bitfield) != 0) { + std::uint32_t source_bitfield = 0; + switch (layout.speaker_bitfield) { + case 0xFu: source_bitfield = 0x1Fu; break; // 5.1 -> 7.1 + case 0x40Fu: source_bitfield = 0x41Fu; break; // 5.1.2 -> 7.1.2 + case 0xA0Fu: source_bitfield = 0xA1Fu; break; // 5.1.4 -> 7.1.4 + case 0x9u: source_bitfield = 0x19u; break; // 4.0 -> 6.0 + default: + return fail(JOC_ERR_LAYOUT_UNSUPPORTED, "surround_fold_layout", + std::string("unsupported surround fold layout: ") + layout.name); + } + const auto* source = ejoc::speaker_tables::find_layout(source_bitfield); + if (source == nullptr) { + return fail(JOC_ERR_LAYOUT_UNSUPPORTED, "surround_fold_layout", + "missing surround fold geometry"); + } + calc = source; + calc_channels = source->channel_count; + fold_possible = true; + if (!layout_by_bitfield(source_bitfield, &calc_info)) { + return fail(JOC_ERR_LAYOUT_UNSUPPORTED, "surround_fold_layout", + "missing surround fold layout info"); + } + make_fold_matrix(source_bitfield, layout.speaker_bitfield, 2, fold_pl2x); + make_fold_matrix(source_bitfield, layout.speaker_bitfield, 3, fold_loro); + } else { + calc_channels = channels; + } + configured = true; + return Status::success(); +} + +void MetaRenderer::Impl::reset_state() { + meta_mode = false; + meta_ready = false; + n_meta = 0; + raw_trim_mode = 0; + current.clear(); + target.clear(); + step.clear(); + remaining.clear(); + slots.clear(); + labels.clear(); + isf_cache.clear(); + pending.clear(); + error.clear(); +} + +bool MetaRenderer::Impl::bypassed(const oamd::OamdTrim* trim, const int object_index) const { + if (trim == nullptr || object_index < 0 || object_index >= oamd::kMaxSlots) { + return false; + } + return trim->disable_per_object[object_index]; +} + +Status MetaRenderer::Impl::init_meta(const oamd::FrameState& frame) { + const oamd::ProgramState& program = frame.program; + std::vector declared_labels; + declared_labels.reserve(program.bed_labels.size()); + for (const ::joc::joc::BedChannel channel : program.bed_labels) { + declared_labels.push_back(static_cast(channel) + 1); + } + const std::uint32_t num_bed = program.num_bed_objects; + if (declared_labels.size() != num_bed) { + if (program.dynamic_object_only) { + declared_labels.clear(); + if (program.lfe_present) { + declared_labels.push_back(kLfeBedLabel); + } + } + if (declared_labels.size() != num_bed) { + return fail(JOC_ERR_OAMD_UNSUPPORTED_VARIANT, "bed_declaration_mismatch", + "OAMD bed declaration does not match the programme section " + "(declared=" + + std::to_string(declared_labels.size()) + ", num_bed_objects=" + + std::to_string(num_bed) + ")"); + } + } + + std::vector object_slots; + object_slots.reserve(frame.object_count); + int slot = 1; + for (std::uint32_t object = 0; object < frame.object_count; ++object) { + const int label = object < declared_labels.size() + ? declared_labels[object] + : -1; + const oamd::ObjectKind kind = + object < frame.objects.size() ? frame.objects[object].kind + : oamd::ObjectKind::kDynamic; + if (kind == oamd::ObjectKind::kBed && + (label == kLfeBedLabel || label == kLfe2BedLabel)) { + object_slots.push_back(0); + } else { + if (slot > 15) { + return fail(JOC_ERR_OAMD_UNSUPPORTED_VARIANT, "object_slot_overflow", + "OAMD metadata carries more than 15 JOC object slots " + "(object_count=" + + std::to_string(frame.object_count) + ")"); + } + object_slots.push_back(slot); + ++slot; + } + } + if (!program.dynamic_object_only) { + const std::int32_t dynamic_objects = program.num_dynamic_objects; + if (dynamic_objects != -1 && + static_cast(num_bed) + + static_cast(program.num_isf_objects) + + static_cast(dynamic_objects) != + static_cast(frame.object_count)) { + return fail(JOC_ERR_OAMD_UNSUPPORTED_VARIANT, "program_object_count_mismatch", + "OAMD programme object sections do not add up to the object count"); + } + } + if (program.num_isf_objects != 0u) { + const oamd::MetadataParams* params = oamd::metadata_params(); + const oamd::IsfConfig* config = + params != nullptr && program.isf_idx >= 0 && + program.isf_idx < static_cast(params->configs.size()) + ? ¶ms->configs[static_cast(program.isf_idx)] + : nullptr; + if (config == nullptr || + config->row_count() != static_cast(program.num_isf_objects)) { + return fail(JOC_ERR_OAMD_UNSUPPORTED_VARIANT, "isf_configuration_mismatch", + "ISF configuration does not match the declared ISF object count"); + } + } + if (frame.object_count != n_meta || object_slots != slots) { + n_meta = frame.object_count; + slots = object_slots; + const std::size_t total = + static_cast(n_meta) * static_cast(channels); + current.assign(total, 0.0); + target.assign(total, 0.0); + step.assign(total, 0.0); + remaining.assign(total, 0); + } + labels.assign(n_meta, -1); + for (std::uint32_t object = 0; object < n_meta; ++object) { + if (object < declared_labels.size()) { + labels[object] = declared_labels[object]; + } + } + meta_mode = true; + return Status::success(); +} + +Status MetaRenderer::Impl::compute_targets(const oamd::FrameState& frame, + const std::uint32_t block_index, + const ejoc_speaker_controls& controls, + const oamd::OamdTrim* trim, + const std::uint32_t size_budget, + std::vector* targets) { + const std::uint32_t count = frame.object_count; + targets->assign(static_cast(count) * static_cast(channels), + 0.0); + if (block_index >= frame.block_count) { + return Status::success(); + } + std::vector size_objects; + for (std::uint32_t object = 0; object < count; ++object) { + if (object >= frame.objects.size() || + block_index >= frame.objects[object].blocks.size()) { + continue; + } + const oamd::ObjectRenderState& state = + frame.objects[object].blocks[block_index].render; + if (frame.objects[object].kind == oamd::ObjectKind::kDynamic && + !state.not_active && (state.size_mode == 1u || state.size_mode == 2u) && + !state.snap && state.divergence_q == 0u) { + size_objects.push_back(object); + } + } + std::vector zero_size(count, 0); + if (size_budget > 0u && size_budget < size_objects.size()) { + std::vector ranked = size_objects; + std::stable_sort(ranked.begin(), ranked.end(), + [&](const std::uint32_t left, const std::uint32_t right) { + const auto bucket = [&](const std::uint32_t object) { + return (frame.objects[object].blocks[block_index].render + .priority_q + + 512) >> 10; + }; + const int left_bucket = bucket(left); + const int right_bucket = bucket(right); + if (left_bucket != right_bucket) { + return left_bucket > right_bucket; + } + return left < right; + }); + for (std::size_t index = size_budget; index < ranked.size(); ++index) { + zero_size[ranked[index]] = 1; + } + } + std::vector spatial; + for (std::uint32_t object = 0; object < count; ++object) { + if (object >= frame.objects.size() || + block_index >= frame.objects[object].blocks.size()) { + continue; + } + const oamd::ObjectRenderState& state = + frame.objects[object].blocks[block_index].render; + if (state.not_active) { + continue; + } + const oamd::ObjectKind kind = frame.objects[object].kind; + const std::uint32_t warp_mode = controls.warp_mode; + if (kind == oamd::ObjectKind::kDynamic) { + dynamic_spatial(state, warp_mode, controls, trim, static_cast(object), + zero_size[object] != 0, spatial); + } else if (kind == oamd::ObjectKind::kBed) { + bed_spatial(frame.program, labels[object], warp_mode, controls, trim, + static_cast(object), spatial); + } else if (kind == oamd::ObjectKind::kIsf) { + isf_spatial(frame.program, static_cast(object), warp_mode, trim, + spatial); + } else { + spatial.assign(static_cast(channels), 0.0); + } + const double gain = static_cast(state.gain_q) / 4096.0; + const std::size_t base = + static_cast(object) * static_cast(channels); + for (int channel = 0; channel < channels; ++channel) { + (*targets)[base + static_cast(channel)] = + spatial[static_cast(channel)] * gain; + } + } + return Status::success(); +} + +void MetaRenderer::Impl::dynamic_spatial(const oamd::ObjectRenderState& state, + const std::uint32_t warp_mode, + const ejoc_speaker_controls& controls, + const oamd::OamdTrim* trim, const int object_index, + const bool force_zero_size, + std::vector& spatial) { + const bool folding = fold_possible && warp_mode >= 2u; + const ejoc::speaker_tables::LayoutGeometry& layout = folding ? *calc : *geometry; + const int region_index = zone_region(state.zone_idx); + double u = static_cast(state.qx) / kQ15; + const double v = static_cast(state.qy) / kQ15; + double w = std::max(static_cast(state.qz) / kQ15, 0.0); + const bool enable_height = state.enable_elevation; + if (!enable_height) { + w = 0.0; + } + const bool bypass = bypassed(trim, object_index); + double floor_v = clamp(std::ldexp(v, warp_floor_exponent(layout.speaker_bitfield, + warp_mode)), + 0.0, 1.0); + double top_v = clamp(v, 0.0, 1.0); + double trim_v = v; + if (controls.trim_mode == EJOC_SPEAKER_TRIM_EXPLICIT && !bypass) { + floor_v = apply_balance(floor_v, w, controls.listener_balance, + controls.overhead_balance); + top_v = apply_balance(top_v, w, controls.listener_balance, + controls.overhead_balance); + trim_v = apply_balance(v, w, controls.listener_balance, + controls.overhead_balance); + } + const auto& region = layout.regions[static_cast(region_index)]; + std::vector vector; + double comp_u = u; + double comp_v = trim_v; + double comp_w = w; + if (state.snap) { + std::vector point; + region_vector(layout, region_index, u, floor_v, top_v, w, enable_height, point); + double best = -1.0; + int chosen = 0; + for (std::size_t index = 0; index < region.point_count; ++index) { + const double value = + point[region.points[index].speaker_id]; + if (value > best) { + best = value; + chosen = static_cast(index); + } + } + const double speaker_u = + static_cast(region.points[static_cast(chosen)] + .coordinate_q15[0]) / + kQ15; + const double speaker_v = + static_cast(region.points[static_cast(chosen)] + .coordinate_q15[1]) / + kQ15; + const double speaker_w = + static_cast(region.points[static_cast(chosen)] + .coordinate_q15[2]) / + kQ15; + const double scale = 1.0; + const double distance = + (u - speaker_u) * (u - speaker_u) + + (std::min(v, scale) - scale * speaker_v) * + (std::min(v, scale) - scale * speaker_v) + + (w - speaker_w) * (w - speaker_w); + if (distance < 0.04) { + vector.assign(static_cast(layout.channel_count), 0.0); + vector[region.points[static_cast(chosen)].speaker_id] = 1.0; + comp_u = speaker_u; + comp_v = speaker_v; + comp_w = speaker_w; + } else { + vector = point; + } + } else if (state.divergence_q > 0u) { + const double spread = + std::min(static_cast(state.divergence_q) / kQ15, std::min(u, 1.0 - u)); + std::vector left; + std::vector right; + region_vector(layout, region_index, u - spread, floor_v, top_v, w, + enable_height, left); + region_vector(layout, region_index, u + spread, floor_v, top_v, w, + enable_height, right); + for (std::size_t index = 0; index < left.size(); ++index) { + left[index] += right[index]; + } + vector = stable_norm(left); + } else if ((state.size_mode == 1u || state.size_mode == 2u) && !force_zero_size) { + const std::uint16_t* size_q = state.size_q; + int q_size = 0; + if (size_q[0] == size_q[1] && size_q[1] == size_q[2]) { + q_size = size_q[0]; + } else { + q_size = (static_cast(size_q[0]) + static_cast(size_q[1]) + + static_cast(size_q[2])) / + 3; + } + const double size = static_cast(q_size) / kQ15; + const float t = oamd::size_curve_value(size); + std::vector point; + region_vector(layout, region_index, u, floor_v, top_v, w, enable_height, point); + if (t < 0.000025f) { + vector = point; + } else { + const double query[3] = {u, (1.0 - w) * floor_v + w * top_v, w}; + const std::vector spread_float = oamd::size_targets( + *oamd::metadata_params(), layout, region_index, query, size, enable_height); + std::vector spread(spread_float.size()); + for (std::size_t index = 0; index < spread_float.size(); ++index) { + spread[index] = static_cast(spread_float[index]); + } + if (t > 0.05f) { + vector = spread; + } else { + const double angle = 20.0 * static_cast(t); + double cosine = 0.0; + double sine = 0.0; + if (angle >= 0.99996948) { + cosine = 0.0; + sine = 1.0; + } else { + cosine = std::cos(angle * kPi / 2.0); + sine = std::sin(angle * kPi / 2.0); + } + std::vector mixed(point.size(), 0.0); + for (std::size_t index = 0; index < point.size() && index < spread.size(); + ++index) { + mixed[index] = 0.5 * cosine * point[index] + 0.5 * sine * spread[index]; + } + vector = stable_norm(mixed); + } + } + } else { + region_vector(layout, region_index, u, floor_v, top_v, w, enable_height, vector); + } + double compensation = 1.0; + if (!bypass) { + if (controls.trim_mode == EJOC_SPEAKER_TRIM_AUTOMATIC) { + compensation = point_position_gain(geometry->speaker_bitfield, comp_v, comp_w); + } else if (controls.trim_mode == EJOC_SPEAKER_TRIM_EXPLICIT) { + compensation = explicit_position_gain(controls, comp_u, comp_v, + comp_w); + } + } + fold_vector(vector, warp_mode, spatial); + for (double& value : spatial) { + value *= compensation; + } +} + +void MetaRenderer::Impl::bed_spatial(const oamd::ProgramState& program, const int label, + const std::uint32_t warp_mode, + const ejoc_speaker_controls& controls, + const oamd::OamdTrim* trim, const int object_index, + std::vector& spatial) const { + spatial.assign(static_cast(channels), 0.0); + if (label < 1 || label > oamd::kBedLabelCount) { + return; + } + const bool folding = fold_possible && warp_mode >= 2u; + const LayoutInfo& calc_layout = folding ? calc_info : info; + const ejoc::speaker_tables::LayoutGeometry& layout = folding ? *calc : *geometry; + const oamd::MetadataParams* params = oamd::metadata_params(); + if (params == nullptr) { + return; + } + const oamd::BedDistribution distribution = oamd::bed_distribution( + calc_layout, label, program.bed_mask, program.b_bed_chan_distribute); + const bool bypass = bypassed(trim, object_index); + int mode = 1; + if (controls.trim_mode == EJOC_SPEAKER_TRIM_AUTOMATIC) { + mode = 0; + } else if (controls.trim_mode == EJOC_SPEAKER_TRIM_EXPLICIT) { + mode = 2; + } + std::vector vector; + double compensation = 1.0; + if (distribution.route != -1) { + int position[3] = {0, 0, 0}; + oamd::bed_reference_position(distribution.route, position); + int qx = position[0]; + const int qy = position[1]; + const int qz = position[2]; + if (qz > 0) { + qx = qx > 16384 ? 32767 : 0; + } + const double u = static_cast(qx) / kQ15; + const double v = static_cast(qy) / kQ15; + const double w = std::max(qz, 0) / kQ15; + const double floor_v = clamp( + std::ldexp(v, warp_floor_exponent(layout.speaker_bitfield, warp_mode)), 0.0, + 1.0); + const double top_v = clamp(v, 0.0, 1.0); + region_vector(layout, 0, u, floor_v, top_v, w, true, vector); + if (!bypass && controls.trim_mode != EJOC_SPEAKER_TRIM_DISABLED) { + if (controls.trim_mode == EJOC_SPEAKER_TRIM_AUTOMATIC) { + compensation = point_position_gain(geometry->speaker_bitfield, v, w); + } else { + compensation = explicit_position_gain(controls, u, v, w); + } + } + } else { + vector.assign(distribution.weights.size(), 0.0); + for (std::size_t index = 0; index < distribution.weights.size(); ++index) { + vector[index] = static_cast(distribution.weights[index]); + } + const double levels[3] = {controls.surround_db, controls.height_db, + controls.centre_db}; + compensation = + static_cast(oamd::bed_compensation(info, label, mode, bypass, levels)); + } + fold_vector(vector, warp_mode, spatial); + for (double& value : spatial) { + value *= compensation; + } +} + +void MetaRenderer::Impl::isf_spatial(const oamd::ProgramState& program, + const int object_index, const std::uint32_t warp_mode, + const oamd::OamdTrim* trim, + std::vector& spatial) { + spatial.assign(static_cast(channels), 0.0); + const oamd::MetadataParams* params = oamd::metadata_params(); + if (params == nullptr || program.isf_idx < 0) { + return; + } + const int isf_idx = program.isf_idx; + const int row = object_index - static_cast(program.num_bed_objects); + const bool folding = fold_possible && warp_mode >= 2u; + const ejoc::speaker_tables::LayoutGeometry& layout = folding ? *calc : *geometry; + const std::string& calc_key = + folding ? std::string(calc_info.compact_name) : std::string(info.compact_name); + auto found = isf_cache.find(isf_idx); + if (found == isf_cache.end()) { + found = isf_cache.emplace(isf_idx, + oamd::assemble_isf(*params, calc_key.c_str(), isf_idx)) + .first; + } + const oamd::IsfBasis& basis = found->second; + if (row < 0 || row >= basis.rows) { + return; + } + const bool bypass = bypassed(trim, object_index); + const bool disabled = raw_trim_mode == 1u || bypass; + std::vector vector(static_cast(layout.channel_count), 0.0); + for (int column = 0; column < basis.columns && column < layout.channel_count; + ++column) { + vector[static_cast(column)] = + static_cast(basis.at(row, column)); + } + const double compensation = static_cast(oamd::isf_compensation( + *params, info.compact_name, isf_idx, row, disabled)); + fold_vector(vector, warp_mode, spatial); + for (double& value : spatial) { + value *= compensation; + } +} + +void MetaRenderer::Impl::fold_vector(const std::vector& input, + const std::uint32_t warp_mode, + std::vector& output) const { + const bool folding = fold_possible && warp_mode >= 2u; + if (!folding) { + output = input; + if (output.size() > static_cast(channels)) { + output.resize(static_cast(channels)); + } + output.resize(static_cast(channels), 0.0); + return; + } + output.assign(static_cast(channels), 0.0); + const ChannelMatrix& matrix = warp_mode == 2 ? fold_pl2x : fold_loro; + for (int row = 0; row < channels; ++row) { + double sum = 0.0; + const int source_count = std::min(calc_channels, static_cast(input.size())); + for (int source = 0; source < source_count; ++source) { + sum += matrix.value[row][source] * input[static_cast(source)]; + } + output[static_cast(row)] = sum; + } +} + +void MetaRenderer::Impl::apply_targets(const std::vector& targets, + const std::uint32_t ramp_samples) { + if (current.size() != targets.size()) { + current.assign(targets.size(), 0.0); + target.assign(targets.size(), 0.0); + step.assign(targets.size(), 0.0); + remaining.assign(targets.size(), 0); + } + const std::uint32_t blocks = ramp_block_count(ramp_samples); + for (std::size_t index = 0; index < targets.size(); ++index) { + const double difference = targets[index] - current[index]; + const bool significant = std::fabs(difference) >= kGainSnapThreshold; + target[index] = targets[index]; + if (!significant) { + current[index] = target[index]; + step[index] = 0.0; + remaining[index] = 0; + continue; + } + if (blocks != 0u) { + step[index] = difference / static_cast(blocks); + remaining[index] = static_cast(blocks); + } else { + current[index] = target[index]; + step[index] = 0.0; + remaining[index] = 0; + } + } + meta_ready = true; +} + +void MetaRenderer::Impl::render_meta_block(const float* input, const std::uint32_t block, + double* output) { + const std::uint32_t start = block * static_cast(kBlockSamples); + const std::uint8_t* standard_map = standard_for_internal.data(); + for (std::uint32_t object = 0; object < n_meta; ++object) { + const int slot = slots[object]; + if (slot < 0) { + continue; + } + const std::size_t base = + static_cast(object) * static_cast(channels); + for (int channel = 0; channel < channels; ++channel) { + const std::size_t index = base + static_cast(channel); + const bool active = remaining[index] > 0; + const double fixed_gain = target[index]; + if (!active && fixed_gain == 0.0) { + continue; + } + const std::size_t standard = + standard_map[static_cast(channel)]; + for (int sample = 0; sample < kBlockSamples; ++sample) { + const double gain = + active ? current[index] + + window[static_cast(sample)] * step[index] + : fixed_gain; + output[(static_cast(start) + + static_cast(sample)) * + static_cast(channels) + + standard] += + static_cast( + input[(static_cast(start) + + static_cast(sample)) * + static_cast(kOutputChannels) + + static_cast(slot)]) * + gain; + } + } + for (int channel = 0; channel < channels; ++channel) { + const std::size_t index = base + static_cast(channel); + if (remaining[index] > 0) { + current[index] += step[index]; + --remaining[index]; + } + if (remaining[index] == 0) { + current[index] = target[index]; + } + } + } +} + +Status MetaRenderer::Impl::schedule(const oamd::OamdUpdate& update, + const std::uint32_t event_sample, + const ejoc_speaker_controls& controls, + const oamd::OamdTrim* trim, + const std::uint32_t size_budget) { + const oamd::FrameState& frame = update.frame_state; + const Status initialized = init_meta(frame); + if (!initialized.ok()) { + return initialized; + } + const std::size_t block_count = + std::min(frame.block_count, frame.blocks.size()); + for (std::size_t block_index = 0; block_index < block_count; ++block_index) { + // The reference accumulates the element sample offset twice here: once + // through this addition and once through block_offset_samples, which + // already contains it. Keep the same arithmetic. + const std::uint32_t sample = + event_sample + frame.sample_offset + frame.blocks[block_index].block_offset_samples; + const std::uint32_t aligned = align_metadata_sample(sample); + std::vector targets; + const Status computed = compute_targets(frame, static_cast(block_index), + controls, trim, size_budget, &targets); + if (!computed.ok()) { + return computed; + } + pending[aligned / static_cast(kBlockSamples)].push_back( + PendingTargets{std::move(targets), + frame.blocks[block_index].ramp_duration_samples}); + } + return Status::success(); +} + +MetaRenderer::MetaRenderer() : impl_(std::make_unique()) {} + +MetaRenderer::~MetaRenderer() = default; + +MetaRenderer::MetaRenderer(MetaRenderer&&) noexcept = default; + +MetaRenderer& MetaRenderer::operator=(MetaRenderer&&) noexcept = default; + +Status MetaRenderer::configure(const LayoutInfo& layout) { + const Status status = impl_->configure(layout); + impl_->reset_state(); + return status; +} + +Status MetaRenderer::reset() { + impl_->reset_state(); + return Status::success(); +} + +bool MetaRenderer::configured() const { return impl_->configured; } + +bool MetaRenderer::engaged() const { return impl_->meta_mode; } + +const char* MetaRenderer::last_error() const { + return impl_->error.empty() ? "" : impl_->error.c_str(); +} + +Status MetaRenderer::block_targets(const oamd::OamdUpdate& update, + const std::uint32_t block_index, + const ejoc_speaker_controls& controls, + const oamd::OamdTrim* trim, + const std::uint32_t size_budget, + MetadataTargets* out) const { + if (!impl_->configured || out == nullptr) { + return Status::fail(JOC_ERR_STATE, stage::kRender, + "speaker: metadata renderer is not configured"); + } + // block_targets must not disturb the live renderer state, so it validates + // and computes on a copy of the object bookkeeping only. + Impl scratch = *impl_; + scratch.meta_mode = true; + scratch.current.clear(); + scratch.target.clear(); + scratch.step.clear(); + scratch.remaining.clear(); + scratch.pending.clear(); + const Status initialized = scratch.init_meta(update.frame_state); + if (!initialized.ok()) { + return initialized; + } + std::vector targets; + const Status computed = + scratch.compute_targets(update.frame_state, block_index, controls, trim, + size_budget, &targets); + if (!computed.ok()) { + return computed; + } + out->object_count = update.frame_state.object_count; + out->channel_count = static_cast(scratch.channels); + out->values = std::move(targets); + return Status::success(); +} + +Status MetaRenderer::process_frame( + ejoc_speaker_renderer_handle legacy, const float* objects16_interleaved, + const std::uint32_t sample_count, const oamd::OamdUpdate* update, + const std::uint32_t metadata_offset, const ejoc_speaker_controls& controls, + const oamd::OamdTrim* trim, const std::uint32_t raw_trim_mode, + const std::uint32_t size_budget, double* output_interleaved) { + Impl& state = *impl_; + state.error.clear(); + if (!state.configured) { + return Status::fail(JOC_ERR_STATE, stage::kRender, + "speaker: metadata renderer is not configured"); + } + if (!objects16_interleaved || !output_interleaved) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender, + "speaker: null PCM pointer passed to the metadata renderer"); + } + if (sample_count == 0u || sample_count % static_cast(kBlockSamples) != 0u) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender, + "speaker: sample_count must be a multiple of 32"); + } + if (metadata_offset > sample_count) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kRender, + "speaker: metadata offset outside process call"); + } + state.raw_trim_mode = raw_trim_mode; + const std::size_t total_samples = sample_count; + std::fill(output_interleaved, + output_interleaved + total_samples * static_cast(state.channels), + 0.0); + + const std::uint32_t total_blocks = sample_count / static_cast(kBlockSamples); + const std::uint32_t event_block = + align_metadata_sample(metadata_offset) / static_cast(kBlockSamples); + const bool has_metadata = + update != nullptr && update->has_frame_state && update->frame_state.object_count != 0u; + bool scheduled = false; + for (std::uint32_t block = 0; block < total_blocks; ++block) { + if (has_metadata && !scheduled && block == event_block) { + const Status status = state.schedule(*update, metadata_offset, controls, trim, + size_budget); + if (!status.ok()) { + return status; + } + scheduled = true; + } + const auto found = state.pending.find(block); + if (found != state.pending.end()) { + for (const Impl::PendingTargets& targets : found->second) { + state.apply_targets(targets.targets, targets.ramp_samples); + } + state.pending.erase(found); + } + if (state.meta_mode && state.meta_ready) { + state.render_meta_block(objects16_interleaved, block, output_interleaved); + } else if (legacy != nullptr) { + static_cast(legacy)->render_legacy_block( + objects16_interleaved, block, output_interleaved); + } + } + if (has_metadata && !scheduled) { + // The reference only handles events exactly at the end of the call. + if (event_block == total_blocks) { + const Status status = state.schedule(*update, metadata_offset, controls, trim, + size_budget); + if (!status.ok()) { + return status; + } + } + } + const auto tail = state.pending.find(total_blocks); + if (tail != state.pending.end()) { + for (const Impl::PendingTargets& targets : tail->second) { + state.apply_targets(targets.targets, targets.ramp_samples); + } + state.pending.erase(tail); + } + for (auto& entry : state.pending) { + for (const Impl::PendingTargets& targets : entry.second) { + state.apply_targets(targets.targets, targets.ramp_samples); + } + } + state.pending.clear(); + return Status::success(); +} + +Status metadata_block_targets(const LayoutInfo& layout, const oamd::OamdUpdate& update, + const std::uint32_t block_index, + const ejoc_speaker_controls& controls, + const oamd::OamdTrim* trim, const std::uint32_t size_budget, + MetadataTargets* out) { + MetaRenderer renderer; + const Status configured = renderer.configure(layout); + if (!configured.ok()) { + return configured; + } + return renderer.block_targets(update, block_index, controls, trim, size_budget, out); +} + +} // namespace joc::speaker diff --git a/src/speaker/speaker_step.cpp b/src/speaker/speaker_step.cpp index 86d0ba5..5de6ec1 100644 --- a/src/speaker/speaker_step.cpp +++ b/src/speaker/speaker_step.cpp @@ -37,18 +37,52 @@ Status step(SpeakerStep* context, const std::vector& objects16_planar, } context->controls_ready = true; } + if (update != nullptr && update->has_trim) { + const Status status = + parameters_from_oamd(&update->trim, context->layout, &context->controls); + if (!status.ok()) { + if (error != nullptr) { + *error = status.message(); + } + return status; + } + context->trim = update->trim; + context->has_trim = true; + context->raw_trim_mode = update->trim.global_trim_mode; + } + + const bool metadata_frame = + update != nullptr && update->has_frame_state && + update->frame_state.object_count != 0u; + if (metadata_frame || context->meta_engaged) { + if (!context->meta_configured) { + const Status configured = context->meta.configure(context->layout); + if (!configured.ok()) { + if (error != nullptr) { + *error = configured.message(); + } + return configured; + } + context->meta_configured = true; + } + const oamd::OamdTrim* trim = context->has_trim ? &context->trim : nullptr; + const Status processed = context->meta.process_frame( + context->handle, context->interleaved.data(), JOC_FRAME_SAMPLES, + metadata_frame ? update : nullptr, context->metadata_offset, + context->controls, trim, context->raw_trim_mode, 0u, + context->output.data()); + if (!processed.ok()) { + if (error != nullptr) { + *error = processed.message(); + } + return processed; + } + context->meta_engaged = context->meta.engaged(); + return Status::success(); + } + std::uint32_t ramp = 0; if (update != nullptr) { - if (update->has_trim) { - const Status status = parameters_from_oamd(&update->trim, context->layout, - &context->controls); - if (!status.ok()) { - if (error != nullptr) { - *error = status.message(); - } - return status; - } - } context->state.apply(*update); ramp = update->ramp_duration_samples; context->last_block_offset = update->block_offset_samples; diff --git a/src/speaker/speaker_step.h b/src/speaker/speaker_step.h index 1dd9249..57a5877 100644 --- a/src/speaker/speaker_step.h +++ b/src/speaker/speaker_step.h @@ -9,6 +9,7 @@ #include "foundation/status.h" #include "oamd/oamd_parser.h" #include "speaker/speaker_layout_lookup.h" +#include "speaker/speaker_meta.h" #include "speaker/speaker_trim.h" namespace joc::speaker { @@ -32,6 +33,16 @@ struct SpeakerStep { // 对象槽 → Q15 方位;bed_override 为 false 的槽仍按 OAMD 坐标声像。 std::uint16_t bed_positions[oamd::kObjects][oamd::kAxes] = {}; bool bed_override[oamd::kObjects] = {}; + // Metadata path: a frame that carries the full per-object block state + // switches the speaker output to the metadata renderer. The programme trim + // element and its raw global mode stay here because the metadata path needs + // the per-object bypass flags and the ISF compensation mode. + MetaRenderer meta; + bool meta_configured = false; + bool meta_engaged = false; + oamd::OamdTrim trim{}; + bool has_trim = false; + std::uint32_t raw_trim_mode = 0; }; Status step(SpeakerStep* context, const std::vector& objects16_planar, diff --git a/src/stream/stream.cpp b/src/stream/stream.cpp index 5bb8f92..80b617b 100644 --- a/src/stream/stream.cpp +++ b/src/stream/stream.cpp @@ -343,7 +343,14 @@ Status Stream::push_eac3(const std::uint8_t* data, std::size_t size, std::size_t if (!extracted.ok()) { return extracted; } - const Status oamd = oamd::parse_id11(bytes.data(), bytes.size(), &pending_frame_.update); + // Speaker and binaural consume the full per-object block state; the + // other stream paths keep the reference's legacy single-block view. + const bool full_metadata_state = speaker_enabled_ || binaural_enabled_; + const Status oamd = + full_metadata_state ? oamd::parse_id11_state(bytes.data(), bytes.size(), + &pending_frame_.update) + : oamd::parse_id11(bytes.data(), bytes.size(), + &pending_frame_.update); if (!oamd.ok()) { return oamd; } @@ -536,8 +543,10 @@ Status Stream::process_ready_frames(bool drain_all) { compact_bed_pending(); std::string error; - const Status rebuilt = joc::rebuild_objects16(rebuilder_, entry.params, bed5_.data(), - lfe_.data(), gain_, &objects16_, &error); + const Status rebuilt = joc::rebuild_objects16( + rebuilder_, entry.params, bed5_.data(), lfe_.data(), + static_cast(core_channels), entry.has_joc, gain_, &objects16_, + &error); if (!rebuilt.ok()) { return Status::fail(rebuilt.code(), stage::kDsp, error); } diff --git a/src/task/task.cpp b/src/task/task.cpp index 36dfc5e..8b3da7b 100644 --- a/src/task/task.cpp +++ b/src/task/task.cpp @@ -348,7 +348,7 @@ Status probe_atmos_trim(const std::string& path, std::uint64_t max_frames, continue; } oamd::OamdUpdate update; - if (!oamd::parse_id11(bytes.data(), bytes.size(), &update).ok() || + if (!oamd::parse_id11_state(bytes.data(), bytes.size(), &update).ok() || !update.has_trim) { continue; } @@ -1429,6 +1429,11 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu return finalize_with_error(seeded.code(), "render", seeded.message()); } speaker.controls_ready = true; + // The metadata path needs the programme trim before its own event + // arrives; the probe above read it from the leading frames. + speaker.trim = initial_trim; + speaker.has_trim = true; + speaker.raw_trim_mode = initial_trim.global_trim_mode; } bus.emit(JOC_EV_RENDERER_INITIALIZED, JOC_STAGE_RENDER, JOC_LOG_INFO, fmt("speaker renderer: %s, %u channels", speaker.layout.name, @@ -1590,6 +1595,9 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu // container that is not this frame's JOC metadata. std::uint64_t missing_joc_frames = 0; std::uint64_t objects_clamped_frames = 0; + std::uint64_t bad_parameter_band_frames = 0; + std::uint64_t bad_offset_ts_frames = 0; + std::uint64_t trailing_data_frames = 0; std::uint64_t extra_container_frames = 0; std::uint64_t chanmap_pair_mismatch_frames = 0; std::uint64_t bed_mask_mismatch_frames = 0; @@ -1660,6 +1668,19 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu if (params.objects_clamped != 0u) { ++objects_clamped_frames; } + switch (params.status) { + case JOC_FRAME_STATUS_BAD_PARAMETER_BAND: + ++bad_parameter_band_frames; + break; + case JOC_FRAME_STATUS_BAD_OFFSET_TS: + ++bad_offset_ts_frames; + break; + case JOC_FRAME_STATUS_TRAILING_DATA: + ++trailing_data_frames; + break; + default: + break; + } } else { ++missing_joc_frames; } @@ -1691,8 +1712,9 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu } std::string dsp_error; const Status rebuilt = ::joc::joc::rebuild_objects16( - rebuilder, params, bed5.data(), lfe.data(), gain, &objects16, - &dsp_error); + rebuilder, params, bed5.data(), lfe.data(), + static_cast(resolved_core_channels), has_joc, gain, + &objects16, &dsp_error); if (!rebuilt.ok()) { return Status::fail(rebuilt.code(), "joc", dsp_error); } @@ -1819,8 +1841,16 @@ 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; const Status oamd = - ::joc::oamd::parse_id11(bytes.data(), bytes.size(), &job.update); + full_metadata_state + ? ::joc::oamd::parse_id11_state(bytes.data(), bytes.size(), + &job.update) + : ::joc::oamd::parse_id11(bytes.data(), bytes.size(), + &job.update); if (!oamd.ok()) { failures.record(produced, StageRank::kJoc, oamd.code(), oamd.stage(), "frame " + std::to_string(produced) + ": " + @@ -2166,11 +2196,28 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu joiner.shutdown(false); if (missing_joc_frames != 0u) { bus.warning(std::to_string(missing_joc_frames) + - " audio frame(s) carried no JOC payload; their object channels stay silent"); + " audio frame(s) carried no JOC payload; their core PCM was passed " + "through (8 core planes, 1217-sample delay)"); } if (objects_clamped_frames != 0u) { bus.warning(std::to_string(objects_clamped_frames) + - " audio frame(s) declared more than 15 objects; the count was clamped to 15"); + " audio frame(s) declared more than 15 objects; the count was clamped " + "to 15 and the frame took the invalid-frame path"); + } + if (bad_parameter_band_frames != 0u) { + bus.warning(std::to_string(bad_parameter_band_frames) + + " audio frame(s) declared an unknown JOC parameter-band count; the " + "frame took the invalid-frame path"); + } + if (bad_offset_ts_frames != 0u) { + bus.warning(std::to_string(bad_offset_ts_frames) + + " audio frame(s) carried an invalid JOC offset_ts value; the frame " + "took the invalid-frame path"); + } + if (trailing_data_frames != 0u) { + bus.warning(std::to_string(trailing_data_frames) + + " audio frame(s) carried JOC data after the byte-alignment padding; " + "the frame took the invalid-frame path"); } if (extra_container_frames != 0u) { bus.warning(std::to_string(extra_container_frames) + diff --git a/src/timeline/position_timeline.cpp b/src/timeline/position_timeline.cpp index 5c0f2e5..de46260 100644 --- a/src/timeline/position_timeline.cpp +++ b/src/timeline/position_timeline.cpp @@ -95,37 +95,100 @@ Status ObjectPositionTrack::position_at(std::int64_t sample, double out[3]) { return Status::success(); } -OamdPositionTimeline::OamdPositionTimeline(int object_count) : object_count_(object_count) { - if (object_count != kTimelineObjects) { - object_count_ = kTimelineObjects; +OamdPositionTimeline::OamdPositionTimeline(int object_count) + : object_count_(std::clamp(object_count, 1, oamd::kMaxObjects - 1)) { + const std::size_t count = static_cast(object_count_); + tracks_.resize(count); + previous_targets_.resize(count); + has_previous_.assign(count, false); + fixed_.assign(count, false); + fixed_xyz_.resize(count); +} + +void OamdPositionTimeline::ensure_tracks(int count) { + const int capped = std::clamp(count, 1, oamd::kMaxObjects - 1); + if (capped <= object_count_) { + return; + } + const std::size_t size = static_cast(capped); + object_count_ = capped; + tracks_.resize(size); + previous_targets_.resize(size); + has_previous_.resize(size, false); + fixed_.resize(size, false); + fixed_xyz_.resize(size); +} + +void OamdPositionTimeline::set_fixed_position(int object_index, const double position[3]) { + if (object_index < 1 || object_index > object_count_) { + return; + } + const std::size_t index = static_cast(object_index - 1); + fixed_[index] = true; + for (int axis = 0; axis < 3; ++axis) { + fixed_xyz_[index][static_cast(axis)] = position[axis]; } } -Status OamdPositionTimeline::submit_update(const oamd::OamdUpdate& update, - std::int64_t frame_start_sample, - std::int64_t outer_sample_offset, - std::int64_t object_delay_samples, - std::int64_t processed_sample) { - if (frame_start_sample < 0 || outer_sample_offset < 0 || object_delay_samples < 0) { - return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, - "OAMD frame, outer offset and object delay must be non-negative"); +void OamdPositionTimeline::reset() { + state_.reset(); + for (int index = 0; index < object_count_; ++index) { + tracks_[static_cast(index)] = ObjectPositionTrack(); + has_previous_[static_cast(index)] = false; + previous_targets_[static_cast(index)] = {0.0, 0.0, 0.0}; } - state_.apply(update); + initialized_ = false; + payload_count_ = 0; + transition_count_ = 0; + last_coded_event_sample_ = -1; +} - double targets[kTimelineObjects][3] = {}; - for (int index = 0; index < kTimelineObjects; ++index) { - if (fixed_[index]) { - std::memcpy(targets[index], fixed_xyz_[index], sizeof(targets[index])); +void OamdPositionTimeline::apply_block(const oamd::FrameState& state, + std::uint32_t block_index) { + // Track i reads OAMD object i + 1: object 0 is the LFE/bed slot the JOC + // renderers do not position. Bed and ISF objects carry no position field, + // and an inactive block keeps the previous position, exactly like the + // reference frame_update() value construction. + for (int track = 0; track < object_count_; ++track) { + const int object_index = track + 1; + if (object_index >= static_cast(state.objects.size())) { + break; + } + const oamd::ObjectElementState& element = + state.objects[static_cast(object_index)]; + if (block_index >= element.blocks.size()) { 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]); + const oamd::ObjectRenderState& render = + element.blocks[static_cast(block_index)].render; + if (render.not_active || element.kind != oamd::ObjectKind::kDynamic) { + continue; + } + state_.apply_render_state(object_index, render); } +} - const std::int64_t coded_event = - frame_start_sample + outer_sample_offset + - static_cast(update.block_offset_samples); +void OamdPositionTimeline::targets(std::vector>* out) const { + out->resize(static_cast(object_count_)); + for (int index = 0; index < object_count_; ++index) { + const std::size_t slot = static_cast(index); + if (fixed_[slot]) { + (*out)[slot] = fixed_xyz_[slot]; + continue; + } + double x = 0.0; + double y = 0.0; + double z = 0.0; + geometry::q_to_adm_xyz(state_.q(index + 1, 0), state_.q(index + 1, 1), + state_.q(index + 1, 2), &x, &y, &z); + (*out)[slot] = {x, y, z}; + } +} + +Status OamdPositionTimeline::schedule(const std::vector>& targets, + std::int64_t coded_event, std::int64_t ramp_samples, + std::int64_t object_delay_samples, + std::int64_t processed_sample) { if (coded_event < last_coded_event_sample_) { return timeline_fail(JOC_ERR_OAMD_UNSUPPORTED_VARIANT, "non_monotonic_binaural_updates: event sample " + @@ -141,44 +204,105 @@ Status OamdPositionTimeline::submit_update(const oamd::OamdUpdate& update, "sample " + std::to_string(processed_sample) + " had been processed, so sample 0 cannot be backfilled"); } - for (int index = 0; index < kTimelineObjects; ++index) { - tracks_[index].set_initial(targets[index]); - std::memcpy(previous_targets_[index], targets[index], sizeof(targets[index])); - has_previous_[index] = true; + for (int index = 0; index < object_count_; ++index) { + const std::size_t slot = static_cast(index); + tracks_[slot].set_initial(targets[slot].data()); + previous_targets_[slot] = targets[slot]; + has_previous_[slot] = true; } initialized_ = true; - ++payload_count_; return Status::success(); } - const std::int64_t effective_ramp = - std::max(0, static_cast(update.ramp_duration_samples)); + const std::int64_t effective_ramp = std::max(0, ramp_samples); const std::int64_t transition_start = timing::align_metadata_sample(coded_event + object_delay_samples); - for (int index = 0; index < kTimelineObjects; ++index) { - const bool changed = !has_previous_[index] || - std::memcmp(previous_targets_[index], targets[index], - sizeof(targets[index])) != 0; + for (int index = 0; index < object_count_; ++index) { + const std::size_t slot = static_cast(index); + const bool changed = + !has_previous_[slot] || + std::memcmp(previous_targets_[slot].data(), targets[slot].data(), + sizeof(targets[slot])) != 0; if (!changed) { continue; } - const Status status = tracks_[index].append(transition_start, effective_ramp, - targets[index], index + 1); + const Status status = + tracks_[slot].append(transition_start, effective_ramp, targets[slot].data(), + index + 1); if (!status.ok()) { return status; } - std::memcpy(previous_targets_[index], targets[index], sizeof(targets[index])); - has_previous_[index] = true; + previous_targets_[slot] = targets[slot]; + has_previous_[slot] = true; ++transition_count_; } + return Status::success(); +} + +Status OamdPositionTimeline::submit_update(const oamd::OamdUpdate& update, + std::int64_t frame_start_sample, + std::int64_t outer_sample_offset, + std::int64_t object_delay_samples, + std::int64_t processed_sample) { + if (frame_start_sample < 0 || outer_sample_offset < 0 || object_delay_samples < 0) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, + "OAMD frame, outer offset and object delay must be non-negative"); + } + if (!update.has_frame_state) { + state_.apply(update); + targets(&targets_); + const std::int64_t coded_event = + frame_start_sample + outer_sample_offset + + static_cast(update.block_offset_samples); + const Status status = + schedule(targets_, coded_event, update.ramp_duration_samples, object_delay_samples, + processed_sample); + if (status.ok()) { + ++payload_count_; + } + return status; + } + + const oamd::FrameState& state = update.frame_state; + ensure_tracks(state.object_count > 0u ? static_cast(state.object_count) - 1 : 0); + const std::uint32_t block_count = + state.blocks.empty() ? 1u : static_cast(state.blocks.size()); + for (std::uint32_t block = 0; block < block_count; ++block) { + apply_block(state, block); + targets(&targets_); + std::int64_t block_offset = 0; + std::int64_t ramp_samples = 0; + if (block < state.blocks.size()) { + block_offset = static_cast(state.blocks[block].block_offset_samples); + ramp_samples = static_cast(state.blocks[block].ramp_duration_samples); + } + const std::int64_t coded_event = + frame_start_sample + outer_sample_offset + block_offset; + const Status status = schedule(targets_, coded_event, ramp_samples, + object_delay_samples, processed_sample); + if (!status.ok()) { + return status; + } + } ++payload_count_; return Status::success(); } -Status OamdPositionTimeline::positions_at(std::int64_t sample, - double positions[kTimelineObjects][3]) { - for (int index = 0; index < kTimelineObjects; ++index) { - const Status status = tracks_[index].position_at(sample, positions[index]); +Status OamdPositionTimeline::positions_at(std::int64_t sample, double* positions, + int count) { + if (positions == nullptr || count < 0) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, + "binaural positions need a non-negative count and a buffer"); + } + if (count > object_count_) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOamd, + "binaural positions requested " + std::to_string(count) + + " objects but the timeline tracks " + + std::to_string(object_count_)); + } + for (int index = 0; index < count; ++index) { + const Status status = tracks_[static_cast(index)].position_at( + sample, positions + static_cast(index) * 3u); if (!status.ok()) { return status; } diff --git a/src/timeline/position_timeline.h b/src/timeline/position_timeline.h index 55e7763..6e8bcc3 100644 --- a/src/timeline/position_timeline.h +++ b/src/timeline/position_timeline.h @@ -2,6 +2,7 @@ #pragma once +#include #include #include @@ -10,6 +11,11 @@ namespace joc::timeline { +// The JOC binaural renderers carry one LFE plus fifteen object sources, but the +// timeline is not tied to that product: it keeps one track per declared OAMD +// object (bounded only by oamd::kMaxObjects) and the renderer reads as many as +// its source layout consumes. A binaural output with a different source count +// (for example one fed by a TrueHD decode) can ask for more tracks. inline constexpr int kTimelineObjects = 15; struct PositionTransition { @@ -42,56 +48,53 @@ class OamdPositionTimeline { public: explicit OamdPositionTimeline(int object_count = kTimelineObjects); + // Schedules one OAMD payload. A payload parsed by parse_id11_state() carries + // the full frame state: every position block is applied at its own offset and + // ramp, and the track list grows to cover every declared object. The legacy + // single-block slot view is still accepted for direct callers. Status submit_update(const oamd::OamdUpdate& update, std::int64_t frame_start_sample, std::int64_t outer_sample_offset, std::int64_t object_delay_samples, std::int64_t processed_sample); - // positions[15][3]; queries must be monotonically increasing. - Status positions_at(std::int64_t sample, double positions[kTimelineObjects][3]); + // positions[count][3]; queries must be monotonically increasing. + Status positions_at(std::int64_t sample, double* positions, int count); // 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]; - } + void set_fixed_position(int object_index, const double position[3]); // Drops the OAMD state and every transition but keeps the fixed bed positions: // they describe the content, not the playback position. - void reset() { - state_.reset(); - for (int index = 0; index < kTimelineObjects; ++index) { - tracks_[index] = ObjectPositionTrack(); - has_previous_[index] = false; - for (int axis = 0; axis < 3; ++axis) { - previous_targets_[index][axis] = 0.0; - } - } - initialized_ = false; - payload_count_ = 0; - transition_count_ = 0; - last_coded_event_sample_ = -1; - } + void reset(); + int object_count() const { return object_count_; } std::uint64_t payload_count() const { return payload_count_; } std::uint64_t transition_count() const { return transition_count_; } bool initialized() const { return initialized_; } - const ObjectPositionTrack& track(int index) const { return tracks_[index]; } + const ObjectPositionTrack& track(int index) const { return tracks_[static_cast(index)]; } private: + // Grows the track list so every declared OAMD object has one (the JOC + // renderers use the first kTimelineObjects). + void ensure_tracks(int count); + // OAMD position state after applying one frame-state block. + void apply_block(const oamd::FrameState& state, std::uint32_t block_index); + // Current ADM cartesian target of every track. + void targets(std::vector>* out) const; + // Initialises from the first update or appends the changed transitions. + Status schedule(const std::vector>& targets, + std::int64_t coded_event, std::int64_t ramp_samples, + std::int64_t object_delay_samples, std::int64_t processed_sample); + int object_count_; oamd::OamdState state_; - ObjectPositionTrack tracks_[kTimelineObjects]; + std::vector tracks_; bool initialized_ = false; - double previous_targets_[kTimelineObjects][3] = {}; - bool has_previous_[kTimelineObjects] = {}; - bool fixed_[kTimelineObjects] = {}; - double fixed_xyz_[kTimelineObjects][3] = {}; + std::vector> previous_targets_; + std::vector has_previous_; + std::vector fixed_; + std::vector> fixed_xyz_; + std::vector> targets_; std::uint64_t payload_count_ = 0; std::uint64_t transition_count_ = 0; std::int64_t last_coded_event_sample_ = -1; diff --git a/tests/test_core.cpp b/tests/test_core.cpp index 08e7b46..067e58c 100644 --- a/tests/test_core.cpp +++ b/tests/test_core.cpp @@ -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 warp0 = {0.9126902722074105, 0, 0, 0, 0.18155106823708969, 0}; - const std::vector warp1 = {0.85973219419392444, 0, 0, 0, 0.35612480624995158, 0}; + const std::vector warp0 = {0.85973646214220434, 0, 0, 0, 0.35611450268596406, 0}; + const std::vector warp1 = {0.65801380052399572, 0, 0, 0, 0.65801380052399572, 0}; const std::vector 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 trimmed = {0.68117911059016056, 0, 0, 0, 0.41744281702545366, 0}; + const std::vector 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 bypassed = {0.92387494614065435, 0, 0, 0, 0.38269450465561039, 0}; + const std::vector 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 warp1 = {0.85973219419392444, 0, 0, 0, 0.35612480624995158, 0}; + const std::vector warp1 = {0.65801380052399572, 0, 0, 0, 0.65801380052399572, 0}; std::vector 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 half_warp1 = {0.9461247896349676, 0, 0, 0, - 0.18820181552760662, 0}; + const std::vector 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 half_warp0 = {0.96001622724230795, 0, 0, 0, - 0.094556249720175942, 0}; + const std::vector 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(sample)] != + 2.0f * unity[static_cast(sample)]) { + lfe_scaled = false; + break; + } + } + CHECK(lfe_scaled); + for (int sample = 0; sample < JOC_LFE_DELAY_SAMPLES; ++sample) { + CHECK(unity[static_cast(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 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(1u << (7u - (bit_count & 7u))); + } + ++bit_count; + } + } +}; + +bool huffman_path(const int (*nodes)[2], int count, int value, std::vector* path) { + struct Entry { + int node; + std::vector path; + }; + std::vector 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 left = queue[index].path; + left.push_back(0); + queue.push_back({nodes[node][0], std::move(left)}); + std::vector 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 path; + CHECK(huffman_path(nodes, count, value, &path)); + for (const int bit : path) { + writer->write(static_cast(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 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(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 payload = make_id14_payload(spec); + joc_frame_params params{}; + CHECK(joc::joc::parse_id14(payload.data(), payload.size(), ¶ms, 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 payload = make_id14_payload(spec); + joc_frame_params params{}; + CHECK(joc::joc::parse_id14(payload.data(), payload.size(), ¶ms, 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 payload = make_id14_payload(spec); + joc_frame_params params{}; + CHECK(joc::joc::parse_id14(payload.data(), payload.size(), ¶ms, 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 payload = make_id14_payload(spec); + joc_frame_params params{}; + CHECK(joc::joc::parse_id14(payload.data(), payload.size(), ¶ms, 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 payload = make_id14_payload(spec); + joc_frame_params params{}; + CHECK(joc::joc::parse_id14(payload.data(), payload.size(), ¶ms, 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 payload = make_id14_payload(spec); + joc_frame_params params{}; + CHECK(joc::joc::parse_id14(payload.data(), payload.size(), ¶ms, 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 payload = make_id14_payload(spec); + joc_frame_params params{}; + CHECK(joc::joc::parse_id14(payload.data(), payload.size(), ¶ms, 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 payload = make_id14_payload(spec); + joc_frame_params params{}; + CHECK(joc::joc::parse_id14(payload.data(), payload.size(), ¶ms, 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 bed; + std::vector 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 dq; + std::vector out; + + FrameCase() + : bed(static_cast(EJOC_MAX_CORE_CHANNELS) * EJOC_FRAME_SAMPLES, 0.0f), + lfe(EJOC_FRAME_SAMPLES, 0.0f), + dq(static_cast(EJOC_MAX_OBJECTS) * EJOC_MAX_DPOINTS * + EJOC_MAX_CORE_CHANNELS * EJOC_MAX_PARAMETER_BANDS, + 0.0), + out(static_cast(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( + (((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(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(channel) * EJOC_FRAME_SAMPLES + sample] = + step * static_cast(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(EJOC_FRAME_SAMPLES) - static_cast(JOC_LFE_DELAY_SAMPLES); ++input) { + const double gain = input < 256 + ? 1.0 + static_cast(input) * ((2.0 - 1.0) / 256.0) + : 2.0; + const double scaled = static_cast(frame.lfe[static_cast(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(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(channel) * EJOC_FRAME_SAMPLES]; + } + expected[3] = frame.lfe[0]; + for (int index = 0; index < 2; ++index) { + expected[4 + index] = + frame.bed[static_cast(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(channel) * EJOC_FRAME_SAMPLES]; + } + current[3] = frame.lfe[0]; + for (int index = 0; index < 2; ++index) { + current[4 + index] = + frame.bed[static_cast(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(channel) * EJOC_FRAME_SAMPLES]; + } + expected[3] = frame.lfe[0]; + for (int index = 0; index < 4; ++index) { + expected[4 + index] = + frame.bed[static_cast(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(sample)] = + static_cast(0.2 * std::sin(static_cast(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(sample)] = + static_cast(0.2 * std::sin(static_cast(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(sample)] = + static_cast(0.2 * std::sin(static_cast(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 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 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 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; } diff --git a/tests/test_oamd_metadata.cpp b/tests/test_oamd_metadata.cpp new file mode 100644 index 0000000..a0103ad --- /dev/null +++ b/tests/test_oamd_metadata.cpp @@ -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 +#include +#include +#include +#include +#include + +#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 from_hex(const char* text) { + std::vector 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(text[index]); + const auto low = static_cast(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((digit(high) << 4) | digit(low))); + } + return bytes; +} + +joc::oamd::OamdUpdate parse_state(const char* hex) { + const std::vector 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(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(block)] + .render.gain_q == expected_gain[block], + "first object gain inheritance"); + check(update.frame_state.objects[1].blocks[static_cast(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(block)] + .render.qx == 16384, + "first object holds centre x"); + check(update.frame_state.objects[1].blocks[static_cast(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(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 payload = from_hex("021100422600022100"); + std::vector 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 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 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 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 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 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 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(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(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::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; +}