Implement OAMD metadata rendering and runtime table generation

- Add oamd_params/oamd_math for OAMD metadata state, ISF matrix assembly,
  runtime size-table generation and bed routing.
- Apply per-block gain/inactive, zone/elevation, size, snap, divergence,
  position refinement, priority budget, ISF and bed distribution in the
  speaker renderer.
- Compute gain, size, priority, position, refinement and delta values from
  formulas; generate the size spread kernel from speaker-layout geometry.
- Derive bed labels, routing, reference positions and automatic attenuation
  from speaker layouts.
- Correct the surround positions of 5.1, 5.1.2 and 5.1.4 and add the 6.0
  speaker layout.
- Update README, data README and math notes for the metadata path and
  runtime table generation.
This commit is contained in:
TheM14 committed 2026-11-08 14:01:07 +08:00
1 parent 17e52a531c
commit 42a05d7722
13 files changed
+2031 -229

No files matched your search

+5 -3
View File
@@ -20,7 +20,7 @@ It covers the currently implemented stream forms. Unknown variants fail explicit
layout, while LFE keeps its separate path. layout, while LFE keeps its separate path.
- Reconstruct LFE plus 15 object channels through analysis QMF, parameter interpolation, the object matrix, and inverse QMF. - Reconstruct LFE plus 15 object channels through analysis QMF, parameter interpolation, the object matrix, and inverse QMF.
- Write a 25-channel ADM BWF: a 10-channel 7.1.2 bed (silent except for LFE) plus 15 objects. - Write a 25-channel ADM BWF: a 10-channel 7.1.2 bed (silent except for LFE) plus 15 objects.
- Render directly to `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` - Render directly to `2.0`, `3.1`, `4.0`, `5.1`, `6.0`, `7.1`, `5.1.2`, `5.1.4`, `7.1.2`, `7.1.4`, `9.1.4`
or `9.1.6` (`4.0` is the quad layout, a front and a back pair; see or `9.1.6` (`4.0` is the quad layout, a front and a back pair; see
[Speaker layouts and channel order](#speaker-layouts-and-channel-order)). [Speaker layouts and channel order](#speaker-layouts-and-channel-order)).
- Run binaural rendering directly from `pcm16 + ID11/OAMD`, without a temporary ADM BWF. - Run binaural rendering directly from `pcm16 + ID11/OAMD`, without a temporary ADM BWF.
@@ -242,7 +242,7 @@ Without `-o`, output goes to `output/` at the repository root.
## Speaker layouts and channel order ## 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: the table:
| Layout | Channel order | | Layout | Channel order |
@@ -251,6 +251,7 @@ the table:
| 3.1 | L, R, C, LFE | | 3.1 | L, R, C, LFE |
| 4.0 | L, R, Lrs, Rrs | | 4.0 | L, R, Lrs, Rrs |
| 5.1 | L, R, C, LFE, Ls, Rs | | 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 | | 7.1 | L, R, C, LFE, Ls, Rs, Lrs, Rrs |
| 5.1.2 | L, R, C, LFE, Ls, Rs, Ltm, Rtm | | 5.1.2 | L, R, C, LFE, Ls, Rs, Ltm, Rtm |
| 5.1.4 | L, R, C, LFE, Ls, Rs, Ltf, Rtf, Ltr, Rtr | | 5.1.4 | L, R, C, LFE, Ls, Rs, Ltf, Rtf, Ltr, Rtr |
@@ -299,6 +300,7 @@ The main documented stages are:
- the 1217-sample LFE delay with its sample-wise clipgain ramp; - the 1217-sample LFE delay with its sample-wise clipgain ramp;
- OAMD Q15 coordinate conversion; - OAMD Q15 coordinate conversion;
- equal-power panning over target-layout regions; - 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; - layout-dependent position compensation and sample-wise gain ramps;
- float32 and PCM24 output quantization; - float32 and PCM24 output quantization;
- SOFA canonical import, full-HRIR directional interpolation, explicit 64-QMF/77-hybrid compact export, `36×2×77` fifth-order fields, exactly-once delay/phase, and special LFE. - SOFA canonical import, full-HRIR directional interpolation, explicit 64-QMF/77-hybrid compact export, `36×2×77` fifth-order fields, exactly-once delay/phase, and special LFE.
@@ -312,7 +314,7 @@ See the [mathematical notes](docs/math.en.md) for the equations used by the deco
- When one audio frame holds several EMDF containers carrying ID11/ID14 (one of them may be something other than the frame's JOC parameters: its ID14 may declare more objects than its bytes can hold, and its ID11 may carry no group id), the container is chosen by the association rule of ETSI TS 103 420 section 8.2 and its Table 56: **the container whose OAMD and JOC payloads share a group id and whose ID14 parses completely** wins, then one whose ID14 merely parses; only a unique best score is used, the others are recorded as a diagnostic (`emdf/extra_container_ignored`), and a tie or all-zero score stays a variant error. - When one audio frame holds several EMDF containers carrying ID11/ID14 (one of them may be something other than the frame's JOC parameters: its ID14 may declare more objects than its bytes can hold, and its ID11 may carry no group id), the container is chosen by the association rule of ETSI TS 103 420 section 8.2 and its Table 56: **the container whose OAMD and JOC payloads share a group id and whose ID14 parses completely** wins, then one whose ID14 merely parses; only a unique best score is used, the others are recorded as a diagnostic (`emdf/extra_container_ignored`), and a tie or all-zero score stays a variant error.
- The extra pair's identity follows TS 103 420 Table 47/Table 48 ($k=1\to$Lb/Rb, $k\in\{2,4\}\to$Tfl/Tfr, $N=5,7,7,5,7$); Annex E.3 requires a dependent-substream `chanmap`, when present, to indicate one of the Table 47 configurations, so `*_5_1_2top_*` (chanmap says Lts/Rts) does not satisfy that "shall". Such material is tolerated with a diagnostic instead of being refused. - The extra pair's identity follows TS 103 420 Table 47/Table 48 ($k=1\to$Lb/Rb, $k\in\{2,4\}\to$Tfl/Tfr, $N=5,7,7,5,7$); Annex E.3 requires a dependent-substream `chanmap`, when present, to indicate one of the Table 47 configurations, so `*_5_1_2top_*` (chanmap says Lts/Rts) does not satisfy that "shall". Such material is tolerated with a diagnostic instead of being refused.
- E-AC-3 input without JOC metadata (ID11 + ID14) is outside the supported scope. If an individual audio frame has no JOC EMDF, that frame passes the core PCM through (the 8 core planes are written to output slots 0..7 by identity, each delayed by 1217 samples) and records `joc_frame_missing` in `diagnostics`. - E-AC-3 input without JOC metadata (ID11 + ID14) is outside the supported scope. If an individual audio frame has no JOC EMDF, that frame passes the core PCM through (the 8 core planes are written to output slots 0..7 by identity, each delayed by 1217 samples) and records `joc_frame_missing` in `diagnostics`.
- The speaker and SOFA binaural paths currently cover ordinary point objects; extent, spread, diffuse, divergence, channel lock, and similar controls are outside the supported scope. - Speaker rendering 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. The SOFA binaural path still covers ordinary point objects only; these object controls are outside the binaural path's scope.
- Speaker rendering reads warp, trims and front/back balance from OAMD, with no CLI warp override. Generic rendering parameters support warp 0/1 and modes 2/3, which render to 7.1.x before PLIIx or Standard (Lo/Ro) folding to 5.1.x; calibration always follows the final target layout. Source trajectories and binaural do not bake in these speaker controls; see [mathematics §12](docs/math.en.md) for parameters and evaluation order. - Speaker rendering reads warp, trims and front/back balance from OAMD, with no CLI warp override. Generic rendering parameters support warp 0/1 and modes 2/3, which render to 7.1.x before PLIIx or Standard (Lo/Ro) folding to 5.1.x; calibration always follows the final target layout. Source trajectories and binaural do not bake in these speaker controls; see [mathematics §12](docs/math.en.md) for parameters and evaluation order.
- Multi-data-point streams, uncommon band configurations, and unusual OAMD scheduling have less coverage than common 12-band, single-data-point material. - Multi-data-point streams, uncommon band configurations, and unusual OAMD scheduling have less coverage than common 12-band, single-data-point material.
- `joc_clipgain` is folded into the **analysis-side scalar** (`scalar = 1/16 x g`, with - `joc_clipgain` is folded into the **analysis-side scalar** (`scalar = 1/16 x g`, with
+5 -3
View File
@@ -18,7 +18,7 @@
作为点声源渲染,扬声器与双耳共用同一组源位置,不随输出布局改变;LFE 独立处理; 作为点声源渲染,扬声器与双耳共用同一组源位置,不随输出布局改变;LFE 独立处理;
- 通过 analysis QMF、参数插值、对象矩阵和 inverse QMF 重建 LFE + 15 路对象 PCM; - 通过 analysis QMF、参数插值、对象矩阵和 inverse QMF 重建 LFE + 15 路对象 PCM;
- 输出 25 声道 ADM BWF:10 声道 7.1.2 bed(除 LFE 外静音)+ 15 个对象; - 输出 25 声道 ADM BWF:10 声道 7.1.2 bed(除 LFE 外静音)+ 15 个对象;
- 直接渲染 `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`、 - 直接渲染 `2.0`、`3.1`、`4.0`、`5.1`、`6.0`、`7.1`、`5.1.2`、`5.1.4`、`7.1.2`、`7.1.4`、`9.1.4`、
`9.1.6`(`4.0` 是 Quad 布局,前后各一对;声道排列见[扬声器布局与声道顺序](#扬声器布局与声道顺序)); `9.1.6`(`4.0` 是 Quad 布局,前后各一对;声道排列见[扬声器布局与声道顺序](#扬声器布局与声道顺序));
- 从 `pcm16 + ID11/OAMD` 直接运行双耳渲染,不生成临时 ADM BWF; - 从 `pcm16 + ID11/OAMD` 直接运行双耳渲染,不生成临时 ADM BWF;
- 双耳 DSP 全程使用 float64/complex128;个性化路线保留原滤波器组与延迟规则,`.jochrtf` 路线保留 77 带方向场与跨帧状态; - 双耳 DSP 全程使用 float64/complex128;个性化路线保留原滤波器组与延迟规则,`.jochrtf` 路线保留 77 带方向场与跨帧状态;
@@ -209,7 +209,7 @@ python main.py --help
## 扬声器布局与声道顺序 ## 扬声器布局与声道顺序
`--speaker-layout` 只接受下表 11 个布局,PCM/WAV 的声道顺序就是表里的顺序: `--speaker-layout` 只接受下表 12 个布局,PCM/WAV 的声道顺序就是表里的顺序:
| 布局 | 声道顺序 | | 布局 | 声道顺序 |
|---|---| |---|---|
@@ -217,6 +217,7 @@ python main.py --help
| 3.1 | L, R, C, LFE | | 3.1 | L, R, C, LFE |
| 4.0 | L, R, Lrs, Rrs | | 4.0 | L, R, Lrs, Rrs |
| 5.1 | L, R, C, LFE, Ls, Rs | | 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 | | 7.1 | L, R, C, LFE, Ls, Rs, Lrs, Rrs |
| 5.1.2 | L, R, C, LFE, Ls, Rs, Ltm, Rtm | | 5.1.2 | L, R, C, LFE, Ls, Rs, Ltm, Rtm |
| 5.1.4 | L, R, C, LFE, Ls, Rs, Ltf, Rtf, Ltr, Rtr | | 5.1.4 | L, R, C, LFE, Ls, Rs, Ltf, Rtf, Ltr, Rtr |
@@ -261,6 +262,7 @@ JustOneCacophony/
- LFE 1217-sample 延迟与逐点 clipgain 斜坡; - LFE 1217-sample 延迟与逐点 clipgain 斜坡;
- OAMD Q15 坐标转换; - OAMD Q15 坐标转换;
- 基于目标布局 region 的等功率声像; - 基于目标布局 region 的等功率声像;
- OAMD 元数据应用(每块增益与 inactive、zone/elevation、size 核、snap、divergence、位置细化、priority 尺寸预算、ISF 矩阵、bed 分配);
- 布局位置补偿与逐样本增益斜坡; - 布局位置补偿与逐样本增益斜坡;
- float32 与 PCM24 输出量化; - float32 与 PCM24 输出量化;
- SOFA canonical importer、完整HRIR方向插值、显式64-QMF/77-hybrid压缩导出、`36×2×77`五阶方向field、exactly-once delay/phase与special LFE。 - SOFA canonical importer、完整HRIR方向插值、显式64-QMF/77-hybrid压缩导出、`36×2×77`五阶方向field、exactly-once delay/phase与special LFE。
@@ -274,7 +276,7 @@ JustOneCacophony/
- 同一音频帧里出现多个含 ID11/ID14 的 EMDF 容器时(其中一个容器可能不是本帧的 JOC 参数:ID14 声明的对象数超出其字节所能容纳的范围,ID11 也可能没有 groupid),按 ETSI TS 103 420 §8.2 + Table 56 的关联规则打分选取:**OAMD 与 JOC 载荷 groupid 相同且 ID14 能完整解析**的容器优先,其次只看 ID14 能否完整解析;只有唯一最高分才采用,其余记入 `diagnostics`(`emdf/extra_container_ignored`),并列或全部为 0 仍报变体。 - 同一音频帧里出现多个含 ID11/ID14 的 EMDF 容器时(其中一个容器可能不是本帧的 JOC 参数:ID14 声明的对象数超出其字节所能容纳的范围,ID11 也可能没有 groupid),按 ETSI TS 103 420 §8.2 + Table 56 的关联规则打分选取:**OAMD 与 JOC 载荷 groupid 相同且 ID14 能完整解析**的容器优先,其次只看 ID14 能否完整解析;只有唯一最高分才采用,其余记入 `diagnostics`(`emdf/extra_container_ignored`),并列或全部为 0 仍报变体。
- 额外那一对的身份以 TS 103 420 Table 47/Table 48 为准($k=1\to$Lb/Rb,$k\in\{2,4\}\to$Tfl/Tfr,$N=5,7,7,5,7$);Annex E.3 规定依赖子流的 `chanmap`(若存在)**应**指明 Table 47 的某种配置,因此 `*_5_1_2top_*`(chanmap 写 Lts/Rts)属于不满足 "shall" 的素材,我们按"记诊断 + 继续解码"容错处理。 - 额外那一对的身份以 TS 103 420 Table 47/Table 48 为准($k=1\to$Lb/Rb,$k\in\{2,4\}\to$Tfl/Tfr,$N=5,7,7,5,7$);Annex E.3 规定依赖子流的 `chanmap`(若存在)**应**指明 Table 47 的某种配置,因此 `*_5_1_2top_*`(chanmap 写 Lts/Rts)属于不满足 "shall" 的素材,我们按"记诊断 + 继续解码"容错处理。
- 不含 JOC 元数据(ID11 + ID14)的 E-AC-3 输入不在支持范围;解码过程中若个别音频帧没有 JOC EMDF,该帧输出核心 PCM 直通(8 路核心按 identity 写入输出槽 0..7,每路延迟 1217 采样),并以 `joc_frame_missing` 记入 `diagnostics`。 - 不含 JOC 元数据(ID11 + ID14)的 E-AC-3 输入不在支持范围;解码过程中若个别音频帧没有 JOC EMDF,该帧输出核心 PCM 直通(8 路核心按 identity 写入输出槽 0..7,每路延迟 1217 采样),并以 `joc_frame_missing` 记入 `diagnostics`。
- 扬声器与 SOFA 双耳路径当前只覆盖普通点对象;extent、spread、diffuse、divergence、channel lock 等对象控制不在支持范围内。 - 扬声器渲染应用 OAMD 的对象属性:每块增益/inactive、zone/elevation、size/snap/divergence、priority 尺寸预算、element 5 位置细化、ISF 基矩阵和 bed distribute;SOFA 双耳路径仍只覆盖普通点对象,这些对象控制不在双耳路径范围内。
- 扬声器渲染从 OAMD 元数据读取 warp、trim 和 front/back balance;命令行不提供 warp 覆盖。通用渲染参数支持 warp 0/1,以及先渲染到 7.1.x 再按 PLIIx 或 Standard(Lo/Ro)折回 5.1.x 的 warp 2/3;校准参数始终按最终目标布局选择。原始对象轨迹与 binaural 不预处理这些扬声器控制,参数和顺序见 [数学说明 §12](docs/math.md)。 - 扬声器渲染从 OAMD 元数据读取 warp、trim 和 front/back balance;命令行不提供 warp 覆盖。通用渲染参数支持 warp 0/1,以及先渲染到 7.1.x 再按 PLIIx 或 Standard(Lo/Ro)折回 5.1.x 的 warp 2/3;校准参数始终按最终目标布局选择。原始对象轨迹与 binaural 不预处理这些扬声器控制,参数和顺序见 [数学说明 §12](docs/math.md)。
- 多数据点、少见参数带配置和特殊 OAMD 调度的覆盖度低于常见 12-band、单数据点素材。 - 多数据点、少见参数带配置和特殊 OAMD 调度的覆盖度低于常见 12-band、单数据点素材。
- `joc_clipgain` 在**分析端**并入分析标量:`标量 = 1/16 × g`(`g = 2^(x−4)·(1 + y/32)`,值域 0.0625..15.75),出口的 - `joc_clipgain` 在**分析端**并入分析标量:`标量 = 1/16 × g`(`g = 2^(x−4)·(1 + y/32)`,值域 0.0625..15.75),出口的
+15
View File
@@ -19,6 +19,21 @@ joc_huff_code_7ch_pos_index_sparse int64[6,2]
`src/joc_qmf.py` loads the QMF tables, while `src/joc_decode.py` loads the JOC Huffman trees. `src/joc_qmf.py` loads the QMF tables, while `src/joc_decode.py` loads the JOC Huffman trees.
## ISF coefficient data
`oamd/metadata_params.json` provides the ISF compressed base coefficients and
layout descriptors as lookup data. Gain, size, priority, position, divergence,
delta and refinement values are computed in code from formulas or fixed
constants; the size spread kernel is generated at runtime from the
`speaker_layouts` region geometry with grouped equal-power panning, an S448
weight and a power sum (20/20/8 virtual points, 20 size nodes, 35/4 position
nodes). Bed labels, routing and reference positions are derived from
`speaker_layouts` as well.
`src/oamd_params.py` parses the data file lazily and `src/oamd_math.py`
computes coefficients in a fixed float32 order; `src/speaker_renderer.py`
applies them to the object target coefficients.
## Binaural rendering tables ## Binaural rendering tables
`rosella_kernels.npz` contains the fixed 64-QMF/77-hybrid tables used by `rosella_kernels.npz` contains the fixed 64-QMF/77-hybrid tables used by
+11
View File
@@ -19,6 +19,17 @@ joc_huff_code_7ch_pos_index_sparse int64[6,2]
`src/joc_qmf.py` 读取 QMF 表,`src/joc_decode.py` 读取 JOC Huffman 树。 `src/joc_qmf.py` 读取 QMF 表,`src/joc_decode.py` 读取 JOC Huffman 树。
## ISF 系数数据
`oamd/metadata_params.json` 提供 ISF 压缩基系数与布局描述的查表数据。
gain、size、priority、位置、divergence 与差分/细化表在代码中按公式或
固定常量计算;size 扩散核按分组等功率声像、S448 权重和幂和,在运行时从
`speaker_layouts` 的区域几何生成(20/20/8 虚拟点、20 个尺寸节点、35/4 个
位置节点);bed 的标签、路由和参考位置也由 `speaker_layouts` 推导。
`src/oamd_params.py` 延迟解析数据文件,`src/oamd_math.py` 按固定的 float32 运算
顺序生成系数;`src/speaker_renderer.py` 把它们接到对象目标系数上。
## 双耳渲染表 ## 双耳渲染表
`rosella_kernels.npz` 保存`.jochrtf`压缩渲染和个性化bank 1使用的64-QMF/77-hybrid固定表: `rosella_kernels.npz` 保存`.jochrtf`压缩渲染和个性化bank 1使用的64-QMF/77-hybrid固定表:
+1
View File
@@ -0,0 +1 @@
{"isf":{"configs":[{"idx":0,"counts":[3,1,0,0,4],"primary_row_map":[0,1,2,3],"secondary_row_map":[0,2,1,3],"matrix_row_stride":73,"matrix_rows":4,"matrix_values":[0.8065544962882996,0.5517973303794861,0.6069754362106323,0.3953828811645508,0.5534031987190247,0.5539230704307556,0.24255745112895966,0.47505271434783936,0.444728285074234,0.17205633223056793,0.4247773289680481,0.5103204250335693,0.18820077180862427,0.17929865419864655,0.452311247587204,-0.280463844537735,0.017338519915938377,-0.28909528255462646,0.0,0.0560874417424202,-0.2892860770225525,-0.013385031372308731,0.10039377957582474,-0.016056396067142487,-0.2825709581375122,-0.04064895957708359,0.10803624987602234,0.05630350857973099,-0.047425154596567154,-0.26719823479652405,-0.06350012123584747,0.097655288875103,0.1266908049583435,-0.029927069321274757,-0.04877259582281113,-0.2535346746444702,-0.050417669117450714,0.09264937043190002,0.07186891883611679,0.05187152326107025,-0.027603687718510628,-0.044422321021556854,-0.2613919973373413,0.0,0.03838589787483215,-0.12560494244098663,0.051136989146471024,-0.09426405280828476,0.06614412367343903,-0.05140332132577896,-0.1441955715417862,0.08474995940923691,-0.036616962403059006,-0.11979598551988602,0.07123114913702011,-0.03924524784088135,-0.03968926891684532,-0.13764509558677673,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.4201984107494354,-0.056425247341394424,0.4477916657924652,0.01371981855481863,0.44707077741622925,-0.05664261803030968,0.05117806792259216,0.4387761950492859,0.02798357792198658,0.05597246438264847,0.4283769726753235,-0.05218393728137016,0.02862025611102581,0.049770887941122055,0.4346124231815338,0.9679709672927856,0.18938815593719482,0.8870320916175842,0.0,0.5337828993797302,0.6927342414855957,-0.048569101840257645,0.36494335532188416,0.4001503586769104,0.6377595067024231,-0.11768897622823715,0.21134333312511444,0.5358392596244812,0.258344829082489,0.5548434853553772,-0.16074039041996002,0.14455674588680267,0.40664923191070557,0.4540003538131714,0.18882706761360168,0.4997166693210602,-0.1372985541820526,0.15293832123279572,0.16712871193885803,0.4936601221561432,0.19720278680324554,0.19437994062900543,0.5300741195678711,0.0,-0.04083092510700226,0.1691703349351883,-0.05588478967547417,0.1370640993118286,-0.07459205389022827,0.08213182538747787,0.1942090392112732,-0.09941107779741287,0.06275865435600281,0.17987869679927826,-0.08116155862808228,0.064299076795578,0.0623021274805069,0.1853865683078766,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.4335499405860901,-0.056425247341394424,-0.3872392475605011,-0.08972250670194626,-0.36937597393989563,-0.05664261803030968,-0.08691781014204025,-0.3389882743358612,-0.11113959550857544,-0.07409896701574326,-0.3167206048965454,-0.05218393728137016,-0.05974094942212105,-0.07198946177959442,-0.32919612526893616,0.09978409111499786,-0.08105161786079407,0.14544576406478882,0.0,-0.15092352032661438,0.2103460729122162,0.025524256750941277,-0.14595693349838257,-0.06471376121044159,0.22086507081985474,0.06565067917108536,-0.11256188154220581,-0.15150493383407593,-0.004101958591490984,0.22987015545368195,0.09314629435539246,-0.08828219771385193,-0.17176778614521027,-0.06250102072954178,0.013875355944037437,0.2309974730014801,0.07791588455438614,-0.08850760757923126,-0.08191754668951035,-0.13957907259464264,-0.012519026175141335,0.007122464943677187,0.23086495697498322,0.0,-0.046477995812892914,0.1691703349351883,-0.06098373979330063,0.11757846176624298,-0.0774788111448288,0.05829664319753647,0.1942090392112732,-0.09704835712909698,0.03865424543619156,0.14466993510723114,-0.08293674886226654,0.043386269360780716,0.045827239751815796,0.1853865683078766,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.006118591409176588,0.0,0.006118591409176588,0.0,0.006118591409176588,0.0,0.0,0.006118591409176588,0.0,0.0,0.006118591409176588,0.0,0.0,0.0,0.006118591409176588,0.6063833832740784,0.3690401017665863,0.3751586675643921,0.0,0.611859142780304,0.0,0.0,0.0,0.611859142780304,0.0,0.0,0.0,0.611859142780304,0.0,0.0,0.0,0.0,0.0,0.611859142780304,0.0,0.0,0.0,0.0,0.0,0.0,0.611859142780304,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.6125019788742065,0.3751586675643921,0.3751586675643921,0.0,0.6125019788742065,0.0,0.0,0.3751586675643921,0.3751586675643921,0.0,0.0,0.0,0.6125019788742065,0.0,0.0]},{"idx":1,"counts":[5,3,0,0,8],"primary_row_map":[0,1,2,3,4,5,6,7],"secondary_row_map":[0,4,3,2,1,5,7,6],"matrix_row_stride":73,"matrix_rows":8,"matrix_values":[0.7620719075202942,0.878515899181366,0.37703391909599304,0.6023024916648865,0.30184605717658997,0.7353588342666626,0.3631182610988617,0.22095273435115814,0.5886307954788208,0.23342837393283844,0.17362885177135468,0.666203498840332,0.2959349751472473,0.23725862801074982,0.19081860780715942,-0.04823773726820946,-0.14693273603916168,-0.07366316765546799,0.0,-0.23600386083126068,0.035208798944950104,0.020941175520420074,-0.15896914899349213,-0.17113232612609863,Line truncated
+44 -1
View File
@@ -392,7 +392,7 @@ q_N(k)=
\right). \right).
$$ $$
OAR coordinates are The normalized room coordinates are
$$ $$
u=\frac{q_1}{32768}, u=\frac{q_1}{32768},
@@ -835,3 +835,46 @@ $$
- A limiter is separate post-processing and is not included in the mixing equations above. - A limiter is separate post-processing and is not included in the mixing equations above.
- Trim and front-back balance apply to speaker rendering only; - Trim and front-back balance apply to speaker rendering only;
binaural rendering reads neither of them. binaural rendering reads neither of them.
## 16. OAMD metadata application (speaker path)
When mapping object PCM to the target layout, speaker rendering applies the OAMD
object attributes in addition to the geometric panning:
- per-block integer gain `Qg` (unity 4096) and `inactive`; the gain is applied to
the target coefficients once, and the LFE uses its owning metadata object's
gain/active state;
- full basic/render status 0/1/2/3 block expansion, same-object previous-block
inheritance, and local mask updates;
- `zone_constraints_idx` region selection mapped through `[0,1,2,3,4,5,6,0]`;
`enable_elevation=0` forces z=0 before position compensation and size spread;
- `object_size_idx` 5-bit sizes, the `size_curve` piecewise mapping, and the size
kernel; the kernel is generated at runtime from region geometry with
equal-power grouped panning, the S448 weight and a power sum (20/20/8
virtual points, 20 size nodes, 35/4 position nodes; boundary rows
X(0), X(1), Y(0), Y(1), Z(1));
- `snap` first-argmax speaker snapping, `divergence` left/right point-source
combination, and element 5 high-precision position refinement;
- `default_priority`/`priority_bits` choose which size objects keep their size
when the size budget is limited;
- `isf_idx` basis matrices (signed coefficients, no per-column normalization)
with the per-basis compensation;
- `b_bed_chan_distribute` label candidates, weight normalization, missing-channel
fallback, and the direct/distribute compensation.
The ISF path orders the N ISF object audio essences into the column vector t
according to the M.U.L.Z order of `isf_config`, selects the input configuration
by `isf_idx` and the matrix M for the target output layout, and evaluates
y = M·t: each ISF channel is distributed over the speakers by the corresponding
matrix row, LFE bypasses the matrix, and each basis carries its own
compensation. The matrix coefficients and layout descriptors are in
`data/oamd/metadata_params.json`.
Gain, size, priority, position, divergence, delta and refinement values are
computed in code from formulas or fixed constants; the size kernel is generated
at runtime from region geometry with grouped equal-power panning, the S448
weight and a power sum. Bed labels, routing and reference positions are
derived from `speaker_layouts` in `src/bed_layout.py`.
ADM export and the SOFA binaural path still use object positions only and do not
apply these speaker target coefficients.
+33 -1
View File
@@ -384,7 +384,7 @@ q_N(k)=
\right). \right).
$$ $$
OAR 坐标为 归一化房间坐标为
$$ $$
u=\frac{q_1}{32768}, u=\frac{q_1}{32768},
@@ -803,3 +803,35 @@ $$
- 多个 OAMD position block 必须按其时间顺序调度。 - 多个 OAMD position block 必须按其时间顺序调度。
- limiter 属于独立后处理,不包含在上述混音公式中。 - limiter 属于独立后处理,不包含在上述混音公式中。
- trim 与前后平衡只作用于扬声器渲染;双耳渲染不读取它们。 - trim 与前后平衡只作用于扬声器渲染;双耳渲染不读取它们。
## 16. OAMD 元数据应用(扬声器路径)
扬声器渲染把对象 PCM 映射到目标布局时,除几何声像外还应用 OAMD 的对象属性:
- 每块整数增益 `Qg`(单位增益 4096)与 `inactive`;增益在生成目标系数时乘一次,
LFE 使用所属元数据对象的增益/活动状态;
- basic/render 状态 0/1/2/3 的完整块展开、同对象前一块继承与局部 mask 更新;
- `zone_constraints_idx` 选择区域,映射为 `[0,1,2,3,4,5,6,0]`;`enable_elevation=0`
在位置补偿和尺寸扩散前令 z=0;
- `object_size_idx` 的 5 bit 尺寸、`size_curve` 分段线性映射与尺寸扩散核;
尺寸核按分组等功率声像、S448 权重和幂和,在运行时用区域几何生成:
20/20/8 个虚拟点、20 个尺寸节点、35/4 个位置节点,边界表为
X(0)、X(1)、Y(0)、Y(1)、Z(1);
- `snap` 的首个最大系数扬声器吸附、`divergence` 的左右两点合成、
element 5 的高精度位置补充;
- `default_priority`/`priority_bits` 在 size 数量预算不足时决定保留尺寸的对象;
- `isf_idx` 的 ISF 基矩阵(可含负系数,不做逐列归一化)与逐 basis 补偿;
- `b_bed_chan_distribute` 的 bed 标签候选筛选、权重归一化、缺失声道 fallback 和
直达/distribute 的补偿。
ISF 路径把 N 路 ISF object audio essences 按 `isf_config` 的 M.U.L.Z 顺序组成
列向量 t,按 `isf_idx` 选输入配置、按目标输出布局选矩阵 M,再计算 y = M·t:
每个 ISF 声道按矩阵对应的一行分配到目标扬声器,LFE 不参与矩阵、直接旁路,
每个 basis 另有补偿。矩阵系数与布局描述见 `data/oamd/metadata_params.json`。
gain、size、priority、位置、divergence 与差分/细化表由 `src/oamd_bits.py` 按
公式或固定常量计算,size 扩散核由 `src/oamd_math.py` 从区域几何生成(分组
等功率声像 + S448 权重 + 幂和)。bed 标签、路由和参考位置由
`src/bed_layout.py` 从 `speaker_layouts` 推导。
ADM 导出与 SOFA 双耳路径仍只使用对象位置,不应用这些扬声器目标系数。
+135
View File
@@ -4,6 +4,9 @@
那一对落到 ADM 骨架的槽位。JOC 的 DSP 不读取这些信息(规格 §10.1),它只用 那一对落到 ADM 骨架的槽位。JOC 的 DSP 不读取这些信息(规格 §10.1),它只用
``(N, sigma)``。 ``(N, sigma)``。
""" """
import math
from functools import lru_cache
from emdf import expand_chanmap from emdf import expand_chanmap
# JOC 核心通道序列的前 5 位(规格 §2):L R C Ls Rs。 # JOC 核心通道序列的前 5 位(规格 §2):L R C Ls Rs。
@@ -107,6 +110,137 @@ BED_CHANNEL_ADM_POSITION = {
# LFE 不是空间声源:它的 PCM 走独立通路,不参与对象摆位。 # LFE 不是空间声源:它的 PCM 走独立通路,不参与对象摆位。
BED_LFE_LABELS = frozenset(("RC_LFE", "RC_LFE2")) BED_LFE_LABELS = frozenset(("RC_LFE", "RC_LFE2"))
# 参考位置网格:bed 标签/输出 slots 的最近邻匹配使用这一组固定的 Q15 点。
BED_REFERENCE_POSITIONS_Q15 = (
(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。
BED_LAYOUT_ATTENUATION_DB = {
"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,
}
def _channel_positions(layout):
"""Channel index -> Q15 room position (LFE gets the special sentinel)."""
found = {}
for region in layout.regions:
for speaker, coordinate in zip(region.speaker_ids,
region.coordinates_q15):
found.setdefault(int(speaker), tuple(coordinate))
result = []
for index, channel in enumerate(layout.channels):
if channel == "LFE":
result.append((-1, -1, -1))
else:
result.append(found.get(index, (-1, -1, -1)))
return result
def _nearest_reference(coordinate):
"""Index of the closest entry in BED_REFERENCE_POSITIONS_Q15."""
if tuple(coordinate) == (-1, -1, -1):
return 3
position = (coordinate[0] / 32768.0, coordinate[1] / 32768.0,
coordinate[2] / 32768.0)
best = 3
best_distance = float("inf")
for index, candidate in enumerate(BED_REFERENCE_POSITIONS_Q15):
if candidate == (-1, -1, -1):
continue
distance = math.dist(position,
(candidate[0] / 32768.0,
candidate[1] / 32768.0,
candidate[2] / 32768.0))
if distance < best_distance:
best = index
best_distance = distance
return best
def _nearest_channel(desired, positions, tolerance=0.05):
"""Channel index nearest to desired (u, v, w), or None if too far."""
best = None
best_distance = float("inf")
for index, coordinate in enumerate(positions):
if coordinate == (-1, -1, -1):
continue
candidate = (coordinate[0] / 32768.0, coordinate[1] / 32768.0,
coordinate[2] / 32768.0)
distance = math.dist(desired, candidate)
if distance < best_distance:
best = index
best_distance = distance
return best if best_distance <= tolerance else None
@lru_cache(maxsize=32)
def bed_output_description(layout):
"""Derive the bed routing description from a speaker layout.
The description is keyed by the standard bed label order and contains the
output reference-position list, per-label slot routing and the automatic
trim attenuation for the layout.
"""
channels = tuple(layout.channels)
positions = _channel_positions(layout)
output_labels = [_nearest_reference(position) for position in positions]
label_to_slot = []
direct_output_labels = []
for label in (item for pair in BED_LAYOUT_ORDER
for item in pair.split("/")):
name = BED_CHANNEL_NAMES.get(label)
slot = None
if name in channels:
slot = channels.index(name)
elif label == "RC_LFE2" and "LFE" in channels:
slot = channels.index("LFE")
else:
desired = adm_xyz_to_render_coordinates(
*BED_CHANNEL_ADM_POSITION[label])
slot = _nearest_channel(desired, positions)
if slot is None:
label_to_slot.append(-1 if label in BED_LFE_LABELS else -2)
else:
label_to_slot.append(int(slot))
if label in BED_LFE_LABELS:
direct_output_labels.append(3)
else:
desired = adm_xyz_to_render_coordinates(
*BED_CHANNEL_ADM_POSITION[label])
best = None
best_distance = float("inf")
for index, candidate in enumerate(BED_REFERENCE_POSITIONS_Q15):
if candidate == (-1, -1, -1):
continue
point = (candidate[0] / 32768.0, candidate[1] / 32768.0,
candidate[2] / 32768.0)
distance = math.dist(desired, point)
if distance < best_distance:
best = index
best_distance = distance
direct_output_labels.append(int(best))
return {
"output_labels": output_labels,
"label_to_slot": label_to_slot,
"direct_output_labels": direct_output_labels,
"automatic_attenuation_db": BED_LAYOUT_ATTENUATION_DB.get(
layout.name, 0.0),
}
# 标准 bed 布局名:由声明声道的集合唯一确定(9.1.6 这类常用叫法即由此得来)。 # 标准 bed 布局名:由声明声道的集合唯一确定(9.1.6 这类常用叫法即由此得来)。
BED_LAYOUT_NAMES = { BED_LAYOUT_NAMES = {
frozenset(("L", "R")): "2.0", frozenset(("L", "R")): "2.0",
@@ -115,6 +249,7 @@ BED_LAYOUT_NAMES = {
frozenset(("L", "R", "Ls", "Rs")): "4.0", frozenset(("L", "R", "Ls", "Rs")): "4.0",
frozenset(("L", "R", "C", "Ls", "Rs")): "5.0", frozenset(("L", "R", "C", "Ls", "Rs")): "5.0",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs")): "5.1", frozenset(("L", "R", "C", "LFE", "Ls", "Rs")): "5.1",
frozenset(("L", "R", "Ls", "Rs", "Lrs", "Rrs")): "6.0",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs")): "7.1", frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs")): "7.1",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Lw", "Rw")): "9.1", frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Lw", "Rw")): "9.1",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Ltm", "Rtm")): "5.1.2", frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Ltm", "Rtm")): "5.1.2",
+501 -153
View File
@@ -5,6 +5,8 @@ ch1..15;bed/ISF/未激活对象没有位置字段,保持上一帧位置。el
驱动,未知 element 按边界跳过;个别编码器的 ``oa_element_size`` 比实际内容短时以 驱动,未知 element 按边界跳过;个别编码器的 ``oa_element_size`` 比实际内容短时以
结构解析为准,差异记入 ``diagnostics``,不算变体错误。 结构解析为准,差异记入 ``diagnostics``,不算变体错误。
""" """
import math
import numpy as np import numpy as np
from variant_error import UnsupportedVariantError, bytes_descriptor from variant_error import UnsupportedVariantError, bytes_descriptor
@@ -25,6 +27,18 @@ TRIM_LUT = (6.0, 3.0, 1.5, 0.75, -0.75, -1.5, -3.0, -4.5,
-6.0, -7.5, -9.0, -10.5, -12.0, -13.5, -15.0, -36.0) -6.0, -7.5, -9.0, -10.5, -12.0, -13.5, -15.0, -36.0)
# bal3d_y 的编码为 (code + 1) / 16,幅度落在 [1/16, 1],取不到 0。 # bal3d_y 的编码为 (code + 1) / 16,幅度落在 [1/16, 1],取不到 0。
BALANCE_STEP = 16 BALANCE_STEP = 16
# 5.5.3/5.5.5 divergence 码表(公开实现固定值),Q15 量纲。
DIVERGENCE_TABLE = (
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,
)
DIVERGENCE_QUICK_INDEX = (26, 29, 32, 63)
# 5.6.0.11 Table 11b:ISF 类型 → 对象数。 # 5.6.0.11 Table 11b:ISF 类型 → 对象数。
ISF_OBJECT_COUNTS = (4, 8, 10, 14, 15, 30) ISF_OBJECT_COUNTS = (4, 8, 10, 14, 15, 30)
# 5.6.1.1.4 Table 12:10 bit 标准 bed 掩码。array position 9(RC_L/RC_R)是 channel # 5.6.1.1.4 Table 12:10 bit 标准 bed 掩码。array position 9(RC_L/RC_R)是 channel
@@ -179,6 +193,8 @@ def _parse_program_assignment(reader, raw_payload):
"content_description": None, "content_description": None,
"bed_assignments": [], "bed_assignments": [],
"num_bed_objects": 0, "num_bed_objects": 0,
"b_bed_chan_distribute": False,
"bed_mask": 0,
"isf_idx": None, "isf_idx": None,
"num_isf_objects": 0, "num_isf_objects": 0,
"num_dynamic_objects": None, "num_dynamic_objects": None,
@@ -187,6 +203,11 @@ def _parse_program_assignment(reader, raw_payload):
program["lfe_present"] = bool(reader.read(1)) program["lfe_present"] = bool(reader.read(1))
# 5.6.4.8:对象顺序为 bed → ISF → dynamic,LFE 属于 bed,排在最前。 # 5.6.4.8:对象顺序为 bed → ISF → dynamic,LFE 属于 bed,排在最前。
program["num_bed_objects"] = 1 if program["lfe_present"] else 0 program["num_bed_objects"] = 1 if program["lfe_present"] else 0
if program["lfe_present"]:
program["bed_assignments"] = [{
"instance": 0, "lfe_only": True, "channels": ["RC_LFE"],
}]
program["bed_mask"] = 0x8
return program return program
mask = reader.read(4) mask = reader.read(4)
@@ -207,16 +228,19 @@ def _parse_program_assignment(reader, raw_payload):
continue continue
if reader.read(1): # b_standard_chan_assign if reader.read(1): # b_standard_chan_assign
bed_mask = reader.read(10) bed_mask = reader.read(10)
program["bed_assignments"].append({ standard = True
"instance": instance, "lfe_only": False, "standard": True, channels = standard_bed_channels(bed_mask)
"mask": bed_mask, "channels": standard_bed_channels(bed_mask), wide_mask = standard_bed_mask(bed_mask)
})
else: else:
bed_mask = reader.read(17) bed_mask = reader.read(17)
program["bed_assignments"].append({ standard = False
"instance": instance, "lfe_only": False, "standard": False, channels = nonstandard_bed_channels(bed_mask)
"mask": bed_mask, "channels": nonstandard_bed_channels(bed_mask), wide_mask = bed_mask
}) program["bed_assignments"].append({
"instance": instance, "lfe_only": False, "standard": standard,
"mask": bed_mask, "channels": channels,
})
program["bed_mask"] |= wide_mask
program["num_bed_objects"] = sum( program["num_bed_objects"] = sum(
len(assignment["channels"]) for assignment in program["bed_assignments"]) len(assignment["channels"]) for assignment in program["bed_assignments"])
if mask & 0x2: if mask & 0x2:
@@ -235,82 +259,264 @@ def _parse_program_assignment(reader, raw_payload):
return program return program
def _parse_object_info_block(reader, object_index, in_bed_or_isf, raw_payload, retain_spatial=False): # 17 bit 非标准 bed 标签在 wide mask 中的位顺序(与 NONSTD_BED_LABELS 相同)。
"""5.5.9 ``object_info_block(0)``:一个对象的属性更新。""" _STANDARD_BED_TO_WIDE = (0x3, 0x4, 0x8, 0x30, 0xC0,
0x300, 0xC00, 0x3000, 0xC000, 0x10000)
def standard_bed_mask(bed_mask):
"""5.6.1.1.4 的 10 bit 标准掩码 → 17 bit 非标准 bed 掩码。"""
wide = 0
for bit, expanded in enumerate(_STANDARD_BED_TO_WIDE):
if (int(bed_mask) >> bit) & 1:
wide |= expanded
return wide
def _q15_ratio(numerator, denominator):
"""Q15 of ``numerator / denominator``, rounded half up and capped."""
return min(32767, int(math.floor(32768.0 * numerator / denominator + 0.5)))
def _gain_q(gain_idx, gain_bits):
"""gain_idx/bits → 整数增益 Qg(单位增益 4096)。"""
if gain_idx == 0:
return 4096
if gain_idx == 1:
return 0
if gain_idx == 2:
bits = int(gain_bits)
db = 15 - bits if bits < 15 else 14 - bits
return int(math.floor(4096.0 * (10.0 ** (db / 20.0))))
raise ValueError("gain_idx=3 需要同块前一对象的状态")
def _size_q(mode, values):
"""size_idx 与 5 bit 数值 → (sx, sy, sz) 整数 Q15。"""
if mode == 1:
value = _q15_ratio(int(values), 31)
return (value, value, value)
if mode == 2:
return tuple(_q15_ratio(int(value), 31) for value in values)
return (0, 0, 0)
_RENDER_DEFAULTS = {
"qx": 16384, "qy": 16384, "qz": 0,
"coarse": (31, 31, 0),
"zone_idx": 0, "enable_elevation": True,
"size_mode": 0, "size_q": (0, 0, 0), "snap": 0,
"divergence_q": 0,
}
def _clamp_coarse(coarse):
x, y, z = coarse
return (min(max(int(x), 0), 62), min(max(int(y), 0), 62),
min(max(int(z), -15), 15))
def _coarse_to_q15(coarse):
x, y, z = coarse
qx = _q15_ratio(int(x), 62)
qy = _q15_ratio(int(y), 62)
magnitude = _q15_ratio(abs(int(z)), 15)
return qx, qy, (-magnitude if z < 0 else magnitude)
_POSITION_DELTA = (0, 1, 2, 3, -4, -3, -2, -1)
_REFINE_DELTA = (1, 2, -1, -2)
def _refine_xy(index):
"""Q15 code for one refined X/Y position index ``5*coarse + delta + 2``."""
return min(32767, max(0, int(math.floor(
(int(index) - 2) * 32768.0 / 310.0 + 0.5))))
def _refine_z(index):
"""Q15 code for one refined Z position index (signed)."""
return min(32767, max(-32767, int(math.floor(
(int(index) - 2) * 32768.0 / 75.0 + 0.5))))
def _copy_render_state(previous):
state = {key: previous[key] for key in _RENDER_DEFAULTS}
state["coarse"] = tuple(previous["coarse"])
state["size_q"] = tuple(previous["size_q"])
state["qx"], state["qy"], state["qz"] = previous["qx"], previous["qy"], previous["qz"]
state["divergence_q"] = int(previous["divergence_q"])
return state
def parse_object_info_block(reader, object_index, block_index, kind,
previous_block, raw_payload, retain_spatial=False,
object_states=None):
"""5.5.9 ``object_info_block``:展开一个对象的一个块。"""
info = { info = {
"object": object_index, "object": object_index,
"in_bed_or_isf": bool(in_bed_or_isf), "block": block_index,
"kind": kind,
"in_bed_or_isf": kind != "dynamic",
"not_active": bool(reader.read(1)), "not_active": bool(reader.read(1)),
} }
# blk == 0 且对象激活时 basic info 恒为 full update(status 0b01)。 inactive = info["not_active"]
basic_status = 0 if info["not_active"] else 1 if inactive:
info["basic_status"] = basic_status info["basic_status"] = 0
if basic_status in (1, 3): info["gain_q"] = 0
if basic_status == 1: info["priority_q"] = 0
# 5.5.10:object_basic_info[] = {true, true}; render = dict(_RENDER_DEFAULTS)
# 5.6.4.12:array[1] = object_gain_idx,array[0] = b_default_object_priority。 render["coarse"] = (31, 31, 0)
gain_present = priority_present = True render["size_q"] = tuple(render["size_q"])
else: info.update({key: render[key] for key in render if key != "coarse"})
flags = reader.read(2) info["coarse"] = render["coarse"]
gain_present = bool(flags & 1) info["render_status"] = 0
priority_present = bool(flags & 2) info["qx"], info["qy"], info["qz"] = _coarse_to_q15(render["coarse"])
if gain_present: else:
basic_status = 1 if block_index == 0 else reader.read(2)
info["basic_status"] = basic_status
if basic_status == 0:
info["gain_q"] = 0
info["priority_q"] = 0
elif basic_status == 1:
gain_idx = reader.read(2) gain_idx = reader.read(2)
info["gain_idx"] = gain_idx info["gain_idx"] = gain_idx
if gain_idx == 2: gain_bits = reader.read(6) if gain_idx == 2 else None
info["gain_bits"] = reader.read(6) if gain_bits is not None:
if priority_present: info["gain_bits"] = gain_bits
if gain_idx == 3:
if object_index == 0:
info["gain_q"] = 4096
elif object_states[object_index - 1][block_index]["not_active"]:
info["gain_q"] = 0
else:
info["gain_q"] = object_states[object_index - 1][block_index]["gain_q"]
else:
info["gain_q"] = _gain_q(gain_idx, gain_bits)
default_priority = bool(reader.read(1)) default_priority = bool(reader.read(1))
info["default_priority"] = default_priority info["default_priority"] = default_priority
if not default_priority: if default_priority:
info["priority_bits"] = reader.read(5) info["priority_q"] = 32767
render_status = 0 if (info["not_active"] or info["in_bed_or_isf"]) else 1 else:
info["render_status"] = render_status bits = reader.read(5)
if render_status in (1, 3): info["priority_bits"] = bits
if render_status == 1: info["priority_q"] = 1024 * bits
# 5.5.11:obj_render_info[] = {true, true, true, true}。 elif basic_status == 2:
position_present = zone_present = size_present = screen_present = True info["gain_q"] = previous_block["gain_q"]
info["priority_q"] = previous_block["priority_q"]
else: else:
flags = reader.read(4) flags = reader.read(2)
position_present = bool(flags & 0x1) info["basic_mask"] = flags
zone_present = bool(flags & 0x2) info["gain_q"] = previous_block["gain_q"]
size_present = bool(flags & 0x4) info["priority_q"] = previous_block["priority_q"]
screen_present = bool(flags & 0x8) if flags & 1:
if position_present: gain_idx = reader.read(2)
# blk == 0 时 b_differential_position_specified 恒为 FALSE。 info["gain_idx"] = gain_idx
x = reader.read(6) gain_bits = reader.read(6) if gain_idx == 2 else None
y = reader.read(6) if gain_bits is not None:
z_sign = reader.read(1) info["gain_bits"] = gain_bits
z = reader.read(4) if gain_idx == 3:
info["position"] = (x, y, z if z_sign else -z) if object_index == 0:
outside = bool(reader.read(1)) info["gain_q"] = 4096
infinite = False elif object_states[object_index - 1][block_index]["not_active"]:
distance_index = None info["gain_q"] = 0
if outside: else:
infinite = bool(reader.read(1)) info["gain_q"] = object_states[object_index - 1][block_index]["gain_q"]
if not infinite: else:
distance_index = reader.read(4) info["gain_q"] = _gain_q(gain_idx, gain_bits)
if retain_spatial: if flags & 2:
info.update(distance_specified=outside, at_infinity=infinite, default_priority = bool(reader.read(1))
distance_factor_idx=distance_index) info["default_priority"] = default_priority
if zone_present: if default_priority:
info["zone_constraints_idx"] = reader.read(3) info["priority_q"] = 32767
info["enable_elevation"] = bool(reader.read(1)) else:
if size_present: bits = reader.read(5)
size_idx = reader.read(2) info["priority_bits"] = bits
info["object_size_idx"] = size_idx info["priority_q"] = 1024 * bits
if size_idx == 1:
reader.read(5) render_status = 0 if kind in ("bed", "isf") else (
elif size_idx == 2: 1 if block_index == 0 else reader.read(2))
reader.read(15) info["render_status"] = render_status
if screen_present: if render_status in (1, 3):
screen_ref = bool(reader.read(1)) if render_status == 1:
screen_bits = reader.read(3) if screen_ref else None position_present = zone_present = size_present = screen_present = True
depth_index = reader.read(2) if screen_ref else None flags = 0xF
if retain_spatial: else:
info.update(use_screen_ref=screen_ref, screen_factor_bits=screen_bits, flags = reader.read(4)
depth_factor_idx=depth_index) position_present = bool(flags & 0x1)
info["snap"] = bool(reader.read(1)) zone_present = bool(flags & 0x2)
size_present = bool(flags & 0x4)
screen_present = bool(flags & 0x8)
info["render_mask"] = flags
state = _copy_render_state(previous_block) if render_status == 3 \
else dict(_RENDER_DEFAULTS)
state["coarse"] = tuple(state["coarse"])
state["size_q"] = tuple(state["size_q"])
if position_present:
differential = (block_index > 0 and bool(reader.read(1)))
if differential:
dx, dy, dz = (_POSITION_DELTA[reader.read(3)]
for _axis in range(3))
coarse = (previous_block["coarse"][0] + dx,
previous_block["coarse"][1] + dy,
previous_block["coarse"][2] + dz)
else:
x = reader.read(6)
y = reader.read(6)
z_sign = reader.read(1)
z = reader.read(4)
coarse = (x, y, z if z_sign else -z)
state["coarse"] = _clamp_coarse(coarse)
outside = bool(reader.read(1))
infinite = False
distance_index = None
if outside:
infinite = bool(reader.read(1))
if not infinite:
distance_index = reader.read(4)
if retain_spatial:
info["distance_specified"] = outside
info["at_infinity"] = infinite
info["distance_factor_idx"] = distance_index
if zone_present:
state["zone_idx"] = reader.read(3)
state["enable_elevation"] = bool(reader.read(1))
if size_present:
size_idx = reader.read(2)
state["size_mode"] = size_idx
if size_idx == 1:
state["size_q"] = _size_q(1, reader.read(5))
elif size_idx == 2:
state["size_q"] = _size_q(2, (reader.read(5), reader.read(5),
reader.read(5)))
else:
state["size_q"] = (0, 0, 0)
if screen_present:
screen_ref = bool(reader.read(1))
screen_bits = reader.read(3) if screen_ref else None
depth_index = reader.read(2) if screen_ref else None
if retain_spatial:
info["use_screen_ref"] = screen_ref
info["screen_factor_bits"] = screen_bits
info["depth_factor_idx"] = depth_index
state["snap"] = 1 if reader.read(1) else 0
info.update({key: state[key] for key in state if key != "coarse"})
info["coarse"] = state["coarse"]
info["qx"], info["qy"], info["qz"] = _coarse_to_q15(state["coarse"])
elif render_status == 2:
info.update({key: previous_block[key] for key in _RENDER_DEFAULTS
if key != "coarse"})
info["coarse"] = tuple(previous_block["coarse"])
info["size_q"] = tuple(previous_block["size_q"])
info["qx"], info["qy"], info["qz"] = (
previous_block["qx"], previous_block["qy"], previous_block["qz"])
info["divergence_q"] = previous_block["divergence_q"]
else:
info.update({key: _RENDER_DEFAULTS[key] for key in _RENDER_DEFAULTS
if key != "coarse"})
info["coarse"] = tuple(_RENDER_DEFAULTS["coarse"])
info["size_q"] = tuple(_RENDER_DEFAULTS["size_q"])
info["qx"], info["qy"], info["qz"] = _coarse_to_q15(info["coarse"])
if reader.read(1): # b_additional_table_data_exists if reader.read(1): # b_additional_table_data_exists
info["additional_table_bytes"] = reader.read(4) + 1 info["additional_table_bytes"] = reader.read(4) + 1
reader.skip(info["additional_table_bytes"] * 8) reader.skip(info["additional_table_bytes"] * 8)
@@ -318,7 +524,7 @@ def _parse_object_info_block(reader, object_index, in_bed_or_isf, raw_payload, r
def _parse_object_element(reader, element, raw_payload, retain_spatial=False): def _parse_object_element(reader, element, raw_payload, retain_spatial=False):
"""5.5.5 ``object_element()``:时间信息 + 每个对象一条 object_info_block。""" """5.5.5 ``object_element()``:时间头 + 对象外/块内顺序的完整块状态。"""
object_count = element["object_count"] object_count = element["object_count"]
try: try:
sample_offset_code = reader.read(2) sample_offset_code = reader.read(2)
@@ -355,11 +561,20 @@ def _parse_object_element(reader, element, raw_payload, retain_spatial=False):
reserved_data_not_present = bool(reader.read(1)) reserved_data_not_present = bool(reader.read(1))
if not reserved_data_not_present: if not reserved_data_not_present:
reader.read(5) reader.read(5)
objects = [ kinds = element["object_kinds"]
_parse_object_info_block( object_blocks = []
reader, index, index < element["bed_isf_objects"], raw_payload, retain_spatial) for object_index in range(object_count):
for index in range(object_count) kind = kinds[object_index]
] states = []
previous = None
for block_index in range(block_count):
info = parse_object_info_block(
reader, object_index, block_index, kind, previous,
raw_payload, retain_spatial, object_blocks)
info["previous"] = previous
states.append(info)
previous = info
object_blocks.append(states)
except UnsupportedVariantError: except UnsupportedVariantError:
raise raise
except ValueError as exc: except ValueError as exc:
@@ -374,10 +589,101 @@ def _parse_object_element(reader, element, raw_payload, retain_spatial=False):
"sample_offset": sample_offset, "sample_offset": sample_offset,
"blocks": blocks, "blocks": blocks,
"reserved_data_not_present": reserved_data_not_present, "reserved_data_not_present": reserved_data_not_present,
"objects": objects, "objects": object_blocks,
} }
def _parse_extended_element(reader, element, object_element, raw_payload):
"""扩展对象元素:divergence 与高精度位置补充。"""
blocks = object_element["blocks"]
block_count = len(blocks)
objects = object_element["objects"]
object_count = len(objects)
if object_element is None:
raise UnsupportedVariantError(
"oamd", "extended_without_object_element",
"OAMD 扩展对象元素在默认主对象元素之前出现",
details={"element": _element_details([element])[0],
"payload": bytes_descriptor(raw_payload)})
try:
divergence_present = bool(reader.read(1))
if divergence_present:
table = DIVERGENCE_TABLE
quick = DIVERGENCE_QUICK_INDEX
for object_index in range(object_count):
for block_index in range(block_count):
state = objects[object_index][block_index]
if state["not_active"]:
state["divergence_q"] = 0
continue
if state["kind"] != "dynamic":
continue
present = bool(reader.read(1))
if not present:
state["divergence_q"] = 0
continue
mode = reader.read(2)
if mode == 0:
state["divergence_q"] = int(table[quick[reader.read(2)]])
elif mode == 1:
state["divergence_q"] = (
0 if block_index == 0
else objects[object_index][block_index - 1]["divergence_q"])
elif mode == 2:
state["divergence_q"] = int(table[reader.read(6)])
else:
reader.read(6)
state["divergence_q"] = 0
refinement_present = bool(reader.read(1))
if refinement_present:
for object_index in range(object_count):
for block_index in range(block_count):
state = objects[object_index][block_index]
if state["not_active"] or state["kind"] != "dynamic":
continue
present = bool(reader.read(1))
if not present:
continue
mask = reader.read(3)
if not mask:
continue
deltas = [0, 0, 0]
for axis in range(3):
if mask & (1 << axis):
deltas[axis] = _REFINE_DELTA[reader.read(2)]
qx, qy, qz = state["qx"], state["qy"], state["qz"]
def _sign(value):
return -1 if value < 0 else 1
def _coarse_xy(value):
return 31 + _sign(value - 16384) * (
(62 * abs(value - 16384) + 16384) >> 15)
def _coarse_z(value):
value = max(value, -32767)
return _sign(value) * ((15 * abs(value) + 16384) >> 15)
ix = _coarse_xy(qx)
iy = _coarse_xy(qy)
iz = _coarse_z(qz)
sign = _sign(iz)
qx = _refine_xy(5 * ix + 2 + deltas[0])
qy = _refine_xy(5 * iy + 2 + deltas[1])
qz = sign * _refine_z(sign * (5 * iz + deltas[2]) + 2)
state["qx"], state["qy"], state["qz"] = qx, qy, qz
except ValueError as exc:
raise _syntax_error(
"extended_element_syntax",
"OAMD 扩展对象元素字段越界或不完整",
raw_payload, exc,
element=_element_details([element])[0],
object_count=object_count,
) from exc
return {"divergence_present": divergence_present,
"refinement_present": refinement_present}
def _parse_trim_element(reader, object_count, raw_payload): def _parse_trim_element(reader, object_count, raw_payload):
"""``trim_element``:全局 trim/warp 配置,每个 payload 出现一次。 """``trim_element``:全局 trim/warp 配置,每个 payload 出现一次。
@@ -490,8 +796,9 @@ def parse_trim(bits_one):
return None return None
def _parse_elements(reader, alternate_present, object_count, program, raw_payload, retain_spatial=False): def _parse_elements(reader, alternate_present, object_count, program,
"""5.5.4 ``oa_element_md()`` 目录;对象 element 按结构解析,其余按声明长度跳过。""" raw_payload, retain_spatial=False):
"""5.5.4 ``oa_element_md()`` 目录:先选替代变体,再解析内容。"""
try: try:
element_count = reader.read(4) element_count = reader.read(4)
if element_count == 0xF: if element_count == 0xF:
@@ -507,8 +814,18 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
details={"payload": bytes_descriptor(raw_payload)}) details={"payload": bytes_descriptor(raw_payload)})
bed_isf_objects = program["num_bed_objects"] + program["num_isf_objects"] bed_isf_objects = program["num_bed_objects"] + program["num_isf_objects"]
kinds = []
for object_index in range(object_count):
if object_index < program["num_bed_objects"]:
kinds.append("bed")
elif object_index < bed_isf_objects:
kinds.append("isf")
else:
kinds.append("dynamic")
elements = [] elements = []
object_element = None object_element = None
extended = None
seen = set()
for ordinal in range(element_count): for ordinal in range(element_count):
header_start = reader.position header_start = reader.position
try: try:
@@ -534,16 +851,8 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
"payload_bits": reader.limit, "payload_bits": reader.limit,
"payload": bytes_descriptor(raw_payload), "payload": bytes_descriptor(raw_payload),
}) })
try: alternate_data_id = reader.read(4) if alternate_present else 0
alternate_data_id = (reader.read(4) discard_unknown = bool(reader.read(1))
if alternate_present else None)
discard_unknown = bool(reader.read(1))
except ValueError as exc:
raise _syntax_error(
"element_control_bounds", "OAMD element 太短,无法容纳控制字段",
raw_payload, exc,
element_ordinal=ordinal, element_id=element_id,
size_bytes=size_bytes) from exc
element = { element = {
"ordinal": ordinal, "ordinal": ordinal,
"element_id": element_id, "element_id": element_id,
@@ -557,30 +866,38 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
"object_count": object_count, "object_count": object_count,
"bed_isf_objects": bed_isf_objects, "bed_isf_objects": bed_isf_objects,
} }
if element_id == OBJECT_ELEMENT_ID: selected = (element_id in (OBJECT_ELEMENT_ID, TRIM_ELEMENT_ID, 5)
if object_element is not None: and alternate_data_id == 0 and element_id not in seen)
raise UnsupportedVariantError( if not selected:
"oamd", "multiple_object_elements", element["parsed_end_bit"] = region_end
"OAMD 含多个 object element", reader.position = region_end
details={ elif element_id == OBJECT_ELEMENT_ID:
"elements": _element_details(elements + [element]), element["object_kinds"] = kinds
"payload": bytes_descriptor(raw_payload),
})
object_element = _parse_object_element( object_element = _parse_object_element(
reader, element, raw_payload, retain_spatial) reader, element, raw_payload, retain_spatial)
element["parsed_end_bit"] = reader.position element["parsed_end_bit"] = reader.position
element["object_element"] = object_element element["object_element"] = object_element
# 个别编码器声明的 oa_element_size 比实际内容短;以结构解析结果为准。 seen.add(element_id)
reader.position = max(region_end, reader.position) reader.position = max(region_end, reader.position)
elif element_id == TRIM_ELEMENT_ID: elif element_id == 5:
if object_element is None:
raise UnsupportedVariantError(
"oamd", "extended_before_object_element",
"OAMD 扩展对象元素出现在默认主对象元素之前",
details={"element": _element_details(elements + [element])[0],
"payload": bytes_descriptor(raw_payload)})
extended = _parse_extended_element(reader, element,
object_element, raw_payload)
element["parsed_end_bit"] = reader.position
element["extended_element"] = extended
seen.add(element_id)
reader.position = max(region_end, reader.position)
else:
element["trim_element"] = _parse_trim_element( element["trim_element"] = _parse_trim_element(
reader, object_count, raw_payload) reader, object_count, raw_payload)
element["parsed_end_bit"] = reader.position element["parsed_end_bit"] = reader.position
seen.add(element_id)
reader.position = max(region_end, reader.position) reader.position = max(region_end, reader.position)
else:
# extended / 未知 element:按声明长度整体跳过(5.5.4)。
element["parsed_end_bit"] = region_end
reader.position = region_end
elements.append(element) elements.append(element)
if object_element is None: if object_element is None:
@@ -605,6 +922,16 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
return object_element, elements return object_element, elements
def _element_kinds(program, object_count):
kinds = []
bed_end = program["num_bed_objects"]
isf_end = bed_end + program["num_isf_objects"]
for index in range(object_count):
kinds.append("bed" if index < bed_end
else ("isf" if index < isf_end else "dynamic"))
return kinds
def program_bed_summary(bits_one): def program_bed_summary(bits_one):
"""只走 OAMD 头与 program_assignment(),返回 bed 声明与内容类型。 """只走 OAMD 头与 program_assignment(),返回 bed 声明与内容类型。
@@ -645,6 +972,11 @@ def program_bed_summary(bits_one):
"num_bed_objects": program["num_bed_objects"], "num_bed_objects": program["num_bed_objects"],
"num_isf_objects": program["num_isf_objects"], "num_isf_objects": program["num_isf_objects"],
"num_dynamic_objects": program["num_dynamic_objects"], "num_dynamic_objects": program["num_dynamic_objects"],
"isf_idx": program["isf_idx"],
"dynamic_object_only": program["dynamic_object_only"],
"lfe_present": program["lfe_present"],
"b_bed_chan_distribute": program["b_bed_chan_distribute"],
"bed_mask": program["bed_mask"],
} }
@@ -657,8 +989,8 @@ def program_bed_channels(bits_one):
return program_bed_summary(bits_one)["channels"] return program_bed_summary(bits_one)["channels"]
def frame_update(bits_one, *, retain_spatial=False): def frame_state(bits_one, *, retain_spatial=False):
"""单帧 OAMD → 位置字段增量及其 sample offset/ramp duration。""" """完整展开一帧 OAMD:每个对象每一块的属性、节目分区与扩展对象元素更新。"""
bits, raw_payload = _payload_bits(bits_one) bits, raw_payload = _payload_bits(bits_one)
try: try:
version = bits[0] << 1 | bits[1] version = bits[0] << 1 | bits[1]
@@ -679,14 +1011,14 @@ def frame_update(bits_one, *, retain_spatial=False):
program = _parse_program_assignment(reader, raw_payload) program = _parse_program_assignment(reader, raw_payload)
alternate_present = bool(reader.read(1)) alternate_present = bool(reader.read(1))
object_element, elements = _parse_elements( object_element, elements = _parse_elements(
reader, alternate_present, object_count, program, raw_payload, retain_spatial) reader, alternate_present, object_count, program, raw_payload,
retain_spatial)
except UnsupportedVariantError: except UnsupportedVariantError:
raise raise
except ValueError as exc: except ValueError as exc:
raise _syntax_error( raise _syntax_error(
"header_truncated", "OAMD header 字段越界或不完整", "header_truncated", "OAMD header 字段越界或不完整",
raw_payload, exc, payload_bits=len(bits)) from exc raw_payload, exc, payload_bits=len(bits)) from exc
if version != 0: if version != 0:
raise UnsupportedVariantError( raise UnsupportedVariantError(
"oamd", "oamd_version", "oamd", "oamd_version",
@@ -695,46 +1027,27 @@ def frame_update(bits_one, *, retain_spatial=False):
"version": version, "version": version,
"payload": bytes_descriptor(raw_payload), "payload": bytes_descriptor(raw_payload),
}) })
if object_count > MAX_SLOTS:
raise UnsupportedVariantError(
"oamd", "object_count",
"OAMD 对象数超过当前 16 槽对象模型",
details={
"object_count": object_count,
"max_slots": MAX_SLOTS,
"payload": bytes_descriptor(raw_payload),
})
blocks = object_element["blocks"] blocks = object_element["blocks"]
if len(blocks) != 1: objects = object_element["objects"]
raise UnsupportedVariantError( # 对外契约:16 槽位置视图(block 0),保留旧 frame_update 的 values 形状。
"oamd", "multiple_position_blocks", values = {(slot, field): None for slot in range(MAX_SLOTS)
"OAMD 一帧含多个对象位置更新块,单次 frame_update 无法表达", for field in ("q1", "q2", "q3")}
details={ for object_index, states in enumerate(objects):
"block_count": len(blocks), if object_index >= MAX_SLOTS:
"blocks": blocks, break
"payload": bytes_descriptor(raw_payload), state = states[0]
"repair_hint": "按 ObjectInfoBlock 顺序逐块解析坐标,再生成分段 ADM ramp", if state["not_active"] or state.get("position") is None and \
}) state["render_status"] == 0 and state["kind"] != "dynamic":
# bed/ISF 与未激活对象没有位置字段:保持上一帧位置。
# 对外契约:始终给出 16 个槽;本帧没有覆盖到的槽保持上一帧位置。
out = {(slot, field): None for slot in range(MAX_SLOTS)
for field in ("q1", "q2", "q3")}
for info in object_element["objects"]:
slot = info["object"]
position = info.get("position")
if position is None:
# bed/ISF 对象与未激活对象没有位置字段:保持上一帧位置。
out[(slot, "q1")] = None
out[(slot, "q2")] = None
out[(slot, "q3")] = None
continue continue
x, y, z = position values[(object_index, "q1")] = state["qx"]
out[(slot, "q1")] = q_of(x, N_Q12) if 0 <= x <= N_Q12 else None values[(object_index, "q2")] = state["qy"]
out[(slot, "q2")] = q_of(y, N_Q12) if 0 <= y <= N_Q12 else None qz = state["qz"]
# Clamp negative height to zero unless signed spatial output is requested. if not state["enable_elevation"]:
out[(slot, "q3")] = q_of(z, N_Q3) if (0 <= z <= N_Q3 or retain_spatial and -N_Q3 <= z < 0) else ( # 描述符构建在 enable_elevation=0 时令 z=0。
0 if z < 0 else None) qz = 0
values[(object_index, "q3")] = (
qz if qz >= 0 or retain_spatial else 0)
size_mismatch = [ size_mismatch = [
{ {
"element_ordinal": element["ordinal"], "element_ordinal": element["ordinal"],
@@ -746,11 +1059,18 @@ def frame_update(bits_one, *, retain_spatial=False):
for element in elements for element in elements
if element["parsed_end_bit"] > element["body_end_bit"] if element["parsed_end_bit"] > element["body_end_bit"]
] ]
trim = next((element["trim_element"] for element in elements
if "trim_element" in element), None)
return { return {
"values": out, "values": values,
"block_offset_samples": blocks[0]["block_offset_samples"], "block_offset_samples": blocks[0]["block_offset_samples"],
"ramp_duration_samples": blocks[0]["ramp_duration_samples"], "ramp_duration_samples": blocks[0]["ramp_duration_samples"],
"blocks": blocks,
"sample_offset": object_element["sample_offset"],
"object_count": object_count, "object_count": object_count,
"kinds": [states[0]["kind"] for states in objects],
"objects": [dict(states[0]) for states in objects],
"object_blocks": objects,
"program": { "program": {
"dynamic_object_only": program["dynamic_object_only"], "dynamic_object_only": program["dynamic_object_only"],
"lfe_present": program["lfe_present"], "lfe_present": program["lfe_present"],
@@ -760,13 +1080,41 @@ def frame_update(bits_one, *, retain_spatial=False):
"num_dynamic_objects": program["num_dynamic_objects"], "num_dynamic_objects": program["num_dynamic_objects"],
"bed_channels": [list(assignment["channels"]) "bed_channels": [list(assignment["channels"])
for assignment in program["bed_assignments"]], for assignment in program["bed_assignments"]],
"bed_mask": program["bed_mask"],
"b_bed_chan_distribute": program["b_bed_chan_distribute"],
"isf_idx": program["isf_idx"],
}, },
"trim": trim,
"elements": _element_details(elements), "elements": _element_details(elements),
"objects": object_element["objects"],
"size_mismatch": size_mismatch, "size_mismatch": size_mismatch,
} }
def frame_update(bits_one, *, retain_spatial=False):
"""单帧 OAMD → 位置字段增量及其 sample offset/ramp duration。"""
state = frame_state(bits_one, retain_spatial=retain_spatial)
if state["object_count"] > MAX_SLOTS:
raise UnsupportedVariantError(
"oamd", "object_count",
"OAMD 对象数超过当前 16 槽对象模型",
details={
"object_count": state["object_count"],
"max_slots": MAX_SLOTS,
})
if len(state["blocks"]) != 1:
# 旧接口(ADM/双耳/sofa 轨迹)只表达单块位置;多块由 frame_state 提供给
# 扬声器渲染路径。这里保持与旧实现一致的显式拒绝,避免静默只取首块。
raise UnsupportedVariantError(
"oamd", "multiple_position_blocks",
"OAMD 一帧含多个对象位置更新块,单次 frame_update 无法表达",
details={
"block_count": len(state["blocks"]),
"blocks": state["blocks"],
"repair_hint": "扬声器渲染路径改用 frame_state()",
})
return state
def frame_update_values(bits_one): def frame_update_values(bits_one):
"""兼容接口:只返回 ``{(slot, field): q|None}``。""" """兼容接口:只返回 ``{(slot, field): q|None}``。"""
return frame_update(bits_one)["values"] return frame_update(bits_one)["values"]
+699
View File
@@ -0,0 +1,699 @@
"""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 :mod:`oamd_params`; this module
does not read files itself.
"""
from __future__ import annotations
import math
from functools import lru_cache
import numpy as np
from bed_layout import BED_REFERENCE_POSITIONS_Q15, bed_output_description
from speaker_layouts import get_speaker_layout
F = np.float32
TINY = F(2 ** -126)
def exp2_approx(value):
"""Specified ``2**x`` approximation used by the compensation kernels."""
x = F(value)
n = math.floor(float(x))
f = F(x - F(n))
poly = F(F(F(F(f * F(0.6862793)) + F(1)) + F(F(f * f) * F(0.25482178)))
+ F(F(f * F(f * f)) * F(0.058898926)))
return F(math.ldexp(float(poly), n))
def pow_approx(x, power):
"""Specified ``x**power`` approximation; ``power`` is a float32."""
x = F(x)
if abs(x) < TINY:
return F(0)
fraction, exponent = math.frexp(float(x))
fraction = F(fraction)
logarithm = F(F(F(F(fraction * F(4.0793457)) - F(2.6931152))
- F(F(fraction * fraction) * F(1.3862305))) + F(exponent))
return exp2_approx(F(logarithm * F(power)))
def norm(vector):
"""Stable float32 normalization with the tiny-norm floor."""
vector = np.asarray(vector, dtype=np.float32)
total = F(0)
for value in vector:
total = F(total + F(value * value))
if abs(total) < TINY:
total = F(0)
denominator = max(F(math.sqrt(float(total))), F(1e-5))
result = (vector / denominator).astype(np.float32)
result[np.abs(result) < TINY] = 0
return result
def boundary(axis, edge, position, t):
"""One boundary weight of the size kernel."""
edge, position, t = F(edge), F(position), F(t)
delta = F(F(edge - position) * F(0.25))
if t < F(0.000025) or abs(delta) > t:
return F(0)
ratio = F(float(delta) / float(t))
ratio2 = F(ratio * ratio)
weight = exp2_approx(F(F(-F(ratio2 * ratio2) * F(0.6328125))
* F(26.575424)))
_, exponent = math.frexp(float(weight))
if exponent < -31 or exponent > 1:
weight = F(0)
if axis == 2:
weight = F(F(math.cos(float(F(F(edge * F(0.34147748)) * F(4)))))
* weight)
return weight
def _region_coordinates(region):
"""Normalize the region's Q15 coordinates to the 0..1 room grid."""
return (np.asarray(region.coordinates_q15, dtype=np.float32)
/ F(32768))
def _group_pan_x(coordinates, groups, position, output):
"""Region X panning: equal-power between x-adjacent speakers."""
position = F(position)
for group in groups:
first = group[0]
last = group[-1]
x_first = coordinates[first][0]
x_last = coordinates[last][0]
if position < x_first:
output[first] = F(1)
continue
if position > x_last:
output[last] = F(1)
continue
for left, right in zip(group, group[1:]):
x_left = coordinates[left][0]
x_right = coordinates[right][0]
if not (x_left < x_right and x_left <= position <= x_right):
continue
fraction = F(F(position - x_left) / F(x_right - x_left))
if fraction <= F(0.5):
angle = F(F(F(fraction * F(0.78539819)) * F(0.5)) * F(4))
output[left] = F(np.cos(np.float32(angle)))
output[right] = F(np.sin(np.float32(angle)))
else:
angle = F(F(F(F(1) - fraction) * F(0.78539819))
* F(0.5) * F(4))
output[left] = F(np.sin(np.float32(angle)))
output[right] = F(np.cos(np.float32(angle)))
break
def _group_pan_y(coordinates, groups, position, output):
"""Region Y panning: equal-power between y-adjacent speaker rows."""
if not groups:
return
position = F(position)
rows = [coordinates[group[0]][1] for group in groups]
if position <= rows[0]:
for index in groups[0]:
output[index] = F(1)
return
if position > rows[-1]:
for index in groups[-1]:
output[index] = F(1)
return
for row in range(len(groups) - 1):
y_low = rows[row]
y_high = rows[row + 1]
if not (y_low <= position <= y_high and y_low < y_high):
continue
fraction = F(F(position - y_low) / F(y_high - y_low))
if fraction <= F(0.5):
angle = F(F(F(fraction * F(0.78539819)) * F(0.5)) * F(4))
low_gain = F(np.cos(np.float32(angle)))
high_gain = F(np.sin(np.float32(angle)))
else:
angle = F(F(F(F(1) - fraction) * F(0.78539819))
* F(0.5) * F(4))
low_gain = F(np.sin(np.float32(angle)))
high_gain = F(np.cos(np.float32(angle)))
for index in groups[row]:
output[index] = low_gain
for index in groups[row + 1]:
output[index] = high_gain
return
def _layer_pan_z(coordinates, position):
"""Region Z panning: floor/height equal-power split."""
position = F(position)
if position <= F(0):
floor_gain = F(1)
height_gain = F(0)
elif position < F(0.99996948):
angle = F(F(position * F(0.39269909)) * F(4))
floor_gain = F(np.cos(np.float32(angle)))
height_gain = F(np.sin(np.float32(angle)))
else:
floor_gain = F(0)
height_gain = F(1)
output = np.zeros(len(coordinates), dtype=np.float32)
for index, coordinate in enumerate(coordinates):
output[index] = (height_gain if coordinate[2] >= F(0.5)
else floor_gain)
return output
def _pan_x_all(coordinates, floor_groups, height_groups, position, mode):
output = np.zeros(len(coordinates), dtype=np.float32)
_group_pan_x(coordinates, floor_groups, position, output)
if mode == 3 and height_groups:
extra = np.zeros(len(coordinates), dtype=np.float32)
_group_pan_x(coordinates, height_groups, position, extra)
output = F(output + extra)
return output
def _pan_y_all(coordinates, floor_groups, height_groups, position, mode):
output = np.zeros(len(coordinates), dtype=np.float32)
_group_pan_y(coordinates, floor_groups, position, output)
if mode == 3 and height_groups:
extra = np.zeros(len(coordinates), dtype=np.float32)
_group_pan_y(coordinates, height_groups, position, extra)
output = F(output + extra)
return output
def _boundary_rows(coordinates, floor_groups, height_groups, mode):
x0 = _pan_x_all(coordinates, floor_groups, height_groups, F(0), mode)
x1 = _pan_x_all(coordinates, floor_groups, height_groups, F(1), mode)
y0 = _pan_y_all(coordinates, floor_groups, height_groups, F(0), mode)
y1 = _pan_y_all(coordinates, floor_groups, height_groups, F(1), mode)
if mode == 3:
z1 = _layer_pan_z(coordinates, F(1))
else:
z1 = np.zeros(len(coordinates), dtype=np.float32)
return [x0, x1, y0, y1, z1]
def _size_weight(position, u, t, axis):
"""Per-virtual-point S448 weight used by the generated size tables."""
t = F(t)
offset = F(F(F(u - position)) * F(0.25))
if t < F(0.000025) or abs(float(offset)) > float(t):
return F(0)
ratio = F(offset / t)
squared = F(ratio * ratio)
power = F(F(-F(squared * squared)) * F(0.6328125))
weight = exp2_approx(F(power * F(26.575424)))
if axis == 2:
angle = F(F(F(u * F(0.34147748))) * F(4))
weight = F(F(np.cos(np.float32(angle))) * weight)
return weight
def _axis_table(pan_vectors, position_nodes, speaker_count, offset_start,
axis):
table = np.zeros((position_nodes, 20, speaker_count), dtype=np.float32)
for position_index in range(position_nodes):
position = F(F(position_index) / F(position_nodes - 1))
for size_index in range(20):
t = F(F(size_index) * F(0.036842104))
m = (F(6) if t <= F(0.125)
else F(F(6.869565) - F(t * F(6.956522))))
for u, pan in enumerate(pan_vectors):
weight = _size_weight(position, F(F(u) / F(len(pan_vectors) - 1)),
t, axis)
if weight == F(0):
continue
for speaker in range(speaker_count):
value = F(pan[offset_start + speaker] * weight)
table[position_index, size_index, speaker] = F(
table[position_index, size_index, speaker]
+ pow_approx(value, m))
return table
def _axis2_table(pan_vectors, speaker_index):
table = np.zeros((4, 20), dtype=np.float32)
if speaker_index >= len(pan_vectors[0]):
return table
for position_index in range(4):
position = F(F(position_index) / F(3))
for size_index in range(20):
t = F(F(size_index) * F(0.036842104))
m = (F(6) if t <= F(0.125)
else F(F(6.869565) - F(t * F(6.956522))))
total = F(0)
for u, pan in enumerate(pan_vectors):
weight = _size_weight(position, F(F(u) / F(7)), t, 2)
if weight == F(0):
continue
total = F(total + pow_approx(
F(pan[speaker_index] * weight), m))
table[position_index, size_index] = total
return table
@lru_cache(maxsize=256)
def _region_size_tables(region):
"""Generate the region size tables from the region geometry."""
coordinates = _region_coordinates(region)
floor_groups = tuple(region.axis0_groups)
height_groups = tuple(region.axis1_groups) if region.mode == 3 else ()
floor_count = sum(len(group) for group in floor_groups)
height_count = sum(len(group) for group in height_groups)
mode = region.mode
x_vectors = [_pan_x_all(coordinates, floor_groups, height_groups,
F(F(index) / F(19)), mode) for index in range(20)]
y_vectors = [_pan_y_all(coordinates, floor_groups, height_groups,
F(F(index) / F(19)), mode) for index in range(20)]
z_vectors = ([_layer_pan_z(coordinates, F(F(index) / F(7)))
for index in range(8)] if mode == 3
else [np.zeros(len(coordinates), dtype=np.float32)
for _ in range(8)])
x_floor = _axis_table(x_vectors, 35, floor_count, 0, 0)
x_height = _axis_table(x_vectors, 35, height_count, floor_count, 0)
y_floor = _axis_table(y_vectors, 35, floor_count, 0, 1)
y_height = _axis_table(y_vectors, 35, height_count, floor_count, 1)
if mode == 3:
z_floor = _axis2_table(z_vectors, 0)
z_height = _axis2_table(z_vectors, floor_count)
else:
z_floor = np.zeros(0, dtype=np.float32)
z_height = np.zeros(0, dtype=np.float32)
entry = {
"floor_count": floor_count,
"height_count": height_count,
"mode": mode,
"floor_axis_group_count": len(floor_groups),
"height_axis_group_count": len(height_groups),
"boundary_3d": _boundary_rows(coordinates, floor_groups,
height_groups, mode),
"boundary_2d": _boundary_rows(coordinates, floor_groups, (), 2),
"axis_tables": {
"0": {"position_nodes": 35, "size_nodes": 20,
"floor_stride": floor_count,
"height_stride": height_count,
"floor": x_floor.reshape(-1).copy(),
"height": x_height.reshape(-1).copy()},
"1": {"position_nodes": 35, "size_nodes": 20,
"floor_stride": floor_count,
"height_stride": height_count,
"floor": y_floor.reshape(-1).copy(),
"height": y_height.reshape(-1).copy()},
"2": {"position_nodes": 4, "size_nodes": 20,
"floor_stride": 1, "height_stride": 1,
"floor": z_floor.reshape(-1).copy(),
"height": z_height.reshape(-1).copy()},
},
}
return entry
def _bracket(position, axis):
position = F(position)
step = F(F(1) / F(3 if axis == 2 else 34))
last = 3 if axis == 2 else 34
low = math.floor(float(F(F(last) * position)))
high = min(low + 1, last)
if low > last:
low = high = last
alpha = (F(0) if low == high
else max(F(0), F(F(position - F(F(low) * step)) / step)))
return low, high, alpha
def _size_bracket(t):
t = F(t)
step = F(0.036842104)
low = math.floor(float(F(F(F(t * F(0.71428573)) * F(2)) * F(19))))
if low > 19:
return 19, 19, F(0)
high = min(low + 1, 19)
beta = (F(0) if low == high
else max(F(0), F(float(F(t - F(F(low) * step))) / float(step))))
return low, high, beta
def _axis_weights(entry, axis, position, t, height):
low, high, alpha = _bracket(position, axis)
low_t, high_t, beta = _size_bracket(t)
weights = np.asarray([
F(1) - max(beta, alpha),
F(1) - max(beta, F(1) - alpha),
F(1) - max(F(1) - beta, alpha),
F(1) - max(F(1) - beta, F(1) - alpha)], dtype=np.float32)
total = F(F(F(weights[1] + weights[0]) + weights[2]) + weights[3])
weights = np.asarray([F(float(value) / float(total)) for value in weights],
dtype=np.float32)
table = entry["axis_tables"][str(axis)]
result = []
for group, count in (("floor", entry["floor_count"]),
("height", entry["height_count"] if height else 0)):
if not count:
continue
stride = table[group + "_stride"]
values = np.asarray(table[group], dtype=np.float32).reshape(
table["position_nodes"], 20, stride)
value = F(F(F(weights[0] * values[low, low_t]
+ weights[1] * values[high, low_t])
+ weights[2] * values[low, high_t])
+ weights[3] * values[high, high_t])
result.extend([value[0]] * count if axis == 2 else value[:count])
return np.asarray(result, dtype=np.float32)
def size_kernel(region, position, t, height=True):
"""Size kernel response in region-speaker order."""
entry = _region_size_tables(region)
position = np.asarray(position, dtype=np.float32)
t = F(t)
floor_count = entry["floor_count"]
height_count = entry["height_count"] if height else 0
total = floor_count + height_count
if not total:
return np.zeros(0, dtype=np.float32)
dim = (entry["mode"] if height
else (1 if entry["floor_axis_group_count"] == 1 else 2))
m = (F(6) if t <= F(0.125)
else F(F(6.869565) - F(t * F(6.956522))))
inverse_m = F(F(1) / m)
axes = [_axis_weights(entry, axis, position[axis], t, height)
for axis in range(dim)]
key = "boundary_3d" if dim == 3 else "boundary_2d"
boundary_table = np.asarray(entry[key], dtype=np.float32)[
[4, 0, 1, 2, 3], :total].copy()
for row, (axis, edge) in enumerate([(2, 1), (0, 0), (0, 1),
(1, 0), (1, 1)]):
boundary_table[row] *= boundary(axis, edge, position[axis], t)
inside = axes[0].copy()
if dim > 1:
inside *= axes[1]
if dim > 2:
inside *= axes[2]
inside = norm(inside)
if dim == 1:
return np.asarray([pow_approx(value, inverse_m) for value in inside],
dtype=np.float32)
powered = np.asarray([[pow_approx(value, m) for value in row]
for row in boundary_table], dtype=np.float32)
x, y = axes[0], axes[1]
if dim == 2:
edge = F(F(F(powered[1] * y + powered[2] * y)
+ powered[3] * x) + powered[4] * x)
else:
z = axes[2]
edge = F(F(F(F(F(x * y) * powered[0]
+ F(y * z) * powered[1])
+ F(y * z) * powered[2])
+ F(x * z) * powered[3])
+ F(x * z) * powered[4])
edge = norm(edge)
distances = [position[0], F(1) - position[0],
position[1], F(1) - position[1]]
if dim == 3:
distances.append(F(1) - position[2])
distance = min(distances)
if t < F(0.000025) or (F(F(0.25) * distance) > t
and F(F(0.25) * distance) > F(0.05)):
mix = F(1)
else:
mix = F(F(float(distance) / float(F(t * F(4))))
* F(F(distance * F(5)) * F(distance * F(5))))
joined = F(F(F(mix * inside) + edge) * F(0.5)) * F(0.0625)
return np.asarray([pow_approx(value, inverse_m) for value in joined],
dtype=np.float32)
_SIZE_CURVE_NODES = ((0.0, 0.0), (0.2, 0.075), (0.5, 0.25),
(0.75, 0.45), (1.0, 0.7))
def size_curve_value(size):
"""Map a scalar size in ``[0, 1]`` to ``t`` on the size curve."""
points = [(F(point[0]), F(point[1])) for point in _SIZE_CURVE_NODES]
size = F(size)
if size <= points[0][0]:
return F(0)
if size >= points[-1][0]:
return F(1)
for (size_low, t_low), (size_high, t_high) in zip(points, points[1:]):
if size <= size_high:
span = F(size_high - size_low)
fraction = F(F(size - size_low) / span) if span else F(0)
return F(t_low + F(fraction * F(t_high - t_low)))
return F(1)
def size_targets(meta, layout, region_index, position, size, height=True):
"""Return a size-kernel target vector in ``layout`` channel order."""
region = layout.regions[int(region_index)]
result = np.zeros(layout.channel_count, dtype=np.float32)
if not len(region.speaker_ids):
return result
t = size_curve_value(size)
kernel = size_kernel(region, position, t, height=height)
for value, channel in zip(kernel, region.speaker_ids):
result[channel] = value
return result
def assemble_isf(meta, layout, isf_idx):
"""Assemble one ISF basis matrix from the compressed base tables."""
config = meta["isf"]["configs"][isf_idx]
geometry = meta["isf"]["layouts"][layout]
row_count = config["counts"][4]
matrix = np.asarray(config["matrix_values"], dtype=np.float32).reshape(
-1, 73)
result = np.zeros((row_count, geometry["output_count"]), dtype=np.float32)
cursor = 0
def add_group(name, selector):
nonlocal cursor
record = next((item for item in meta["isf"]["layout_groups"][name]
if item[0] == selector), None)
if record is None:
return
for column in range(record[1], record[2]):
mappings = [config["primary_row_map"]]
if meta["isf"]["paired_columns"][column]:
mappings.append(config["secondary_row_map"])
for mapping in mappings:
speaker = geometry["output_ids"][cursor]
result[:, speaker] += matrix[np.asarray(mapping), column]
cursor += 1
floor_a, floor_b, height, additional = geometry["selectors"]
add_group("floor_a", floor_a)
cursor -= 2
add_group("floor_b", floor_b)
cursor -= 2
add_group("height", height)
if additional:
mid, upper, lower, zenith, _ = config["counts"]
result[mid:mid + upper] = 0
result[mid + upper + lower:mid + upper + lower + zenith] = 0
add_group("additional", additional)
return result
def isf_matrix(meta, layout, isf_idx):
"""Return the assembled ISF basis matrix ``(Nisf, output_count)``."""
return assemble_isf(meta, layout, isf_idx)
def isf_compensation(meta, layout, isf_idx, row, disabled=False):
"""Return the per-basis ISF compensation gain."""
if disabled:
return F(1)
config = meta["isf"]["configs"][isf_idx]
geom = meta["isf"]["layouts"][layout]
first = config["counts"][0]
value = F(geom["compensation_value"])
if row < first:
distance = F(min(row, first - row))
lower = max(F(F(first) * F(0.16666667)), F(1))
width = F(F(first) * F(0.11111111))
if distance <= lower:
fraction = F(0)
elif distance > F(lower + width):
fraction = F(1)
else:
fraction = F(F(distance - lower) / width)
value = F(fraction * value)
return exp2_approx(F(value * F(0.1660964)))
DIRECT_BED_LABEL = {1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7,
9: 13, 10: 14, 11: 17, 12: 18, 13: 21, 14: 22,
15: 11, 16: 12, 17: 3}
A_WEIGHT = F(0.5011872053146362)
B_WEIGHT = F(0.7079457640647888)
C_WEIGHT = F(0.3548133969306946)
def bed_weight(label, mask, target_label):
"""Return the per-label bed redistribution weight."""
value = F(1 if target_label == DIRECT_BED_LABEL.get(label, 3) else 0)
if 9 <= label <= 14:
rows = {9: {13: 1, 15: B_WEIGHT, 17: A_WEIGHT},
10: {14: 1, 16: B_WEIGHT, 18: A_WEIGHT},
11: {13: A_WEIGHT, 15: B_WEIGHT, 17: 1,
19: B_WEIGHT, 21: A_WEIGHT},
12: {14: A_WEIGHT, 16: B_WEIGHT, 18: 1,
20: B_WEIGHT, 22: A_WEIGHT},
13: {17: A_WEIGHT, 19: B_WEIGHT, 21: 1},
14: {18: A_WEIGHT, 20: B_WEIGHT, 22: 1}}
return F(rows[label].get(target_label, value))
if mask & 0xC0:
rows = {5: {4: 1, 25: B_WEIGHT, 27: B_WEIGHT},
6: {5: 1, 26: B_WEIGHT, 28: B_WEIGHT},
7: {6: 1, 8: C_WEIGHT, 29: B_WEIGHT, 31: B_WEIGHT,
33: A_WEIGHT},
8: {7: 1, 8: C_WEIGHT, 30: B_WEIGHT, 32: B_WEIGHT,
34: A_WEIGHT}}
elif mask & 0x30:
rows = {5: {4: 1, 6: F(0.5370317697525024),
8: F(0.2884031534194946), 25: B_WEIGHT,
27: F(0.8128305077552795),
29: F(0.6606934666633606),
31: F(0.43651583790779114), 33: C_WEIGHT},
6: {5: 1, 7: F(0.5370317697525024),
8: F(0.2884031534194946), 26: B_WEIGHT,
28: F(0.8128305077552795),
30: F(0.6606934666633606),
32: F(0.43651583790779114), 34: C_WEIGHT}}
else:
return value
if label == 5 and not mask & 0x4000 and target_label == 11:
return A_WEIGHT
if label == 6 and not mask & 0x8000 and target_label == 12:
return A_WEIGHT
return F(rows.get(label, {}).get(target_label, value))
def bed_candidates(label, mask, output_labels):
"""Return the five height-candidate flags for one input bed label."""
available = [int(value in output_labels)
for value in (13, 15, 17, 19, 21)]
a, b, m, d, e = available
remaining = available.copy()
bits = (8, 10, 12) if label in (9, 11, 13) else (9, 11, 13)
front, middle, rear = [(mask >> bit) & 1 for bit in bits]
if front:
if a:
remaining[0] = 0
elif b:
remaining[1] = 0
if middle and m:
remaining[2] = 0
if rear:
if e:
remaining[4] = 0
elif d:
remaining[3] = 0
special = (not m and sum(available) == 2 and not front and not rear
and middle and (a or b) and (d or e))
if label in (9, 10):
return [a, b, remaining[2], 0, 0] if (a or b or remaining[2]) \
else [0] * 5
if label in (11, 12):
return [0] * 5 if special else [remaining[0], remaining[1], m,
remaining[3], remaining[4]]
if label in (13, 14):
return [0, 0, remaining[2], d, e] if (d or e or remaining[2]) \
else [0] * 5
return [0] * 5
def bed_distribution(layout_key, label, mask, distribute):
"""Return ``(route, weights)`` for one input bed label.
``route == -1`` means the returned weights are the target; otherwise
``route`` is the reference position used to synthesize a point source.
The slot map is derived from the speaker layout, not stored separately.
"""
layout = bed_output_description(get_speaker_layout(layout_key))
labels = layout["output_labels"]
base = np.zeros(len(labels), dtype=np.float32)
slot = layout["label_to_slot"][label - 1]
fallback = layout["direct_output_labels"][label - 1]
height = 9 <= label <= 14
if slot == -2 and (not height or not distribute):
return fallback, base
if slot == -1:
return -1, base
if slot >= 0:
base[slot] = F(1)
if not distribute:
return -1, base
if height:
candidate = bed_candidates(label, mask, labels)
if not any(candidate):
return fallback, base
side_labels = (13, 15, 17, 19, 21) if label % 2 \
else (14, 16, 18, 20, 22)
allowed = {value for value, active in zip(side_labels, candidate)
if active}
weights = np.array([bed_weight(label, mask, value)
if value in allowed else F(0)
for value in labels], dtype=np.float32)
else:
weights = np.array([bed_weight(label, mask, value)
for value in labels], dtype=np.float32)
if all(value == F(0) or value == F(1) for value in weights):
return -1, base
count = np.count_nonzero(weights)
if count == 1:
weights[weights != 0] = F(1)
elif count > 1:
weights = norm(weights)
else:
return fallback, base
return -1, weights
def bed_reference_position(layout_key, index):
"""Return the Q15 reference position used by the bed fallback path."""
return BED_REFERENCE_POSITIONS_Q15[int(index)]
def bed_compensation(layout_key, label, mode=0, bypass=False,
levels=(0, 0, 0)):
"""Return the bed path compensation gain for one input bed label."""
if label in (4, 17) or bypass or mode == 1:
return F(1)
layout = bed_output_description(get_speaker_layout(layout_key))
source = layout["direct_output_labels"][label - 1]
position = BED_REFERENCE_POSITIONS_Q15[source]
x, y, z = [F(value / 32768) for value in position]
if mode == 0:
y_term = F(1) if y >= F(0.6) else (
F(0) if y <= 0 else F(float(y) / 0.6))
z_term = F(1) if z >= 1 else (
F(0) if z <= F(0.2) else F(float(F(z - F(0.2))) / 0.8))
amount = max(F(0), min(F(y_term + z_term), F(1)))
value_db = F(amount * F(layout["automatic_attenuation_db"]))
else:
surround, height, centre = map(F, levels)
surround_factor = F(surround if y >= F(0.5)
else F(F(y * surround) * F(2)))
centre_position = max(F(abs(z) - F(0.05)), F(y - F(0.05)),
F(abs(F(x - F(0.5))) - F(0.05)))
centre_factor = F(F(1) - min(max(F(10) * centre_position, F(0)),
F(1)))
value_db = F(F(F(surround_factor * F(F(1) - z))
+ F(z * height)) + F(centre_factor * centre))
return exp2_approx(F(value_db * F(0.1660964)))
+27
View File
@@ -0,0 +1,27 @@
"""Static lookup data used by the OAMD metadata application path.
``metadata_params.json``
ISF base coefficients and layout descriptors, applied as lookup tables.
gain, size, priority, position, delta and refinement values 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 data file is parsed once per process.
"""
from __future__ import annotations
import json
from functools import lru_cache
from pathlib import Path
_DATA_DIR = Path(__file__).resolve().parent.parent / "data" / "oamd"
@lru_cache(maxsize=1)
def metadata_params():
"""Return the parsed ``metadata_params.json`` mapping."""
with (_DATA_DIR / "metadata_params.json").open(
"r", encoding="utf-8") as handle:
return json.load(handle)
+58 -29
View File
@@ -128,11 +128,11 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis0_groups': ((0, 1), (2, 3)), 'axis0_groups': ((0, 1), (2, 3)),
'axis1_groups': (), 'axis1_groups': (),
'mode': 2}, 'mode': 2},
{'coordinates_q15': ((0, 32767, 0), (32767, 32767, 0)), {'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (0, 32767, 0), (32767, 32767, 0)),
'speaker_ids': (2, 3), 'speaker_ids': (0, 1, 2, 3),
'axis0_groups': ((0, 1),), 'axis0_groups': ((0, 1), (2, 3)),
'axis1_groups': (), 'axis1_groups': (),
'mode': 1}, 'mode': 2},
{'coordinates_q15': ((0, 0, 0), (32767, 0, 0)), {'coordinates_q15': ((0, 0, 0), (32767, 0, 0)),
'speaker_ids': (0, 1), 'speaker_ids': (0, 1),
'axis0_groups': ((0, 1),), 'axis0_groups': ((0, 1),),
@@ -152,22 +152,22 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'speaker_bitfield': 15, 'speaker_bitfield': 15,
'channels': ('L', 'R', 'C', 'LFE', 'Ls', 'Rs'), 'channels': ('L', 'R', 'C', 'LFE', 'Ls', 'Rs'),
'internal_to_standard': (0, 1, 2, 3, 4, 5), 'internal_to_standard': (0, 1, 2, 3, 4, 5),
'regions': ({'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 32767, 0), (32767, 32767, 0)), 'regions': ({'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 16384, 0), (32767, 16384, 0)),
'speaker_ids': (0, 1, 2, 4, 5), 'speaker_ids': (0, 1, 2, 4, 5),
'axis0_groups': ((0, 2, 1), (3, 4)), 'axis0_groups': ((0, 2, 1), (3, 4)),
'axis1_groups': (), 'axis1_groups': (),
'mode': 2}, 'mode': 2},
{'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 32767, 0), (32767, 32767, 0)), {'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 16384, 0), (32767, 16384, 0)),
'speaker_ids': (0, 1, 2, 4, 5), 'speaker_ids': (0, 1, 2, 4, 5),
'axis0_groups': ((0, 2, 1), (3, 4)), 'axis0_groups': ((0, 2, 1), (3, 4)),
'axis1_groups': (), 'axis1_groups': (),
'mode': 2}, 'mode': 2},
{'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 32767, 0), (32767, 32767, 0)), {'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 16384, 0), (32767, 16384, 0)),
'speaker_ids': (0, 1, 2, 4, 5), 'speaker_ids': (0, 1, 2, 4, 5),
'axis0_groups': ((0, 2, 1), (3, 4)), 'axis0_groups': ((0, 2, 1), (3, 4)),
'axis1_groups': (), 'axis1_groups': (),
'mode': 2}, 'mode': 2},
{'coordinates_q15': ((16384, 0, 0), (0, 32767, 0), (32767, 32767, 0)), {'coordinates_q15': ((16384, 0, 0), (0, 16384, 0), (32767, 16384, 0)),
'speaker_ids': (2, 4, 5), 'speaker_ids': (2, 4, 5),
'axis0_groups': ((0,), (1, 2)), 'axis0_groups': ((0,), (1, 2)),
'axis1_groups': (), 'axis1_groups': (),
@@ -177,7 +177,7 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis0_groups': ((0, 2, 1),), 'axis0_groups': ((0, 2, 1),),
'axis1_groups': (), 'axis1_groups': (),
'mode': 1}, 'mode': 1},
{'coordinates_q15': ((0, 32767, 0), (32767, 32767, 0)), {'coordinates_q15': ((0, 16384, 0), (32767, 16384, 0)),
'speaker_ids': (4, 5), 'speaker_ids': (4, 5),
'axis0_groups': ((0, 1),), 'axis0_groups': ((0, 1),),
'axis1_groups': (), 'axis1_groups': (),
@@ -239,8 +239,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'regions': ({'coordinates_q15': ((0, 0, 0), 'regions': ({'coordinates_q15': ((0, 0, 0),
(32767, 0, 0), (32767, 0, 0),
(16384, 0, 0), (16384, 0, 0),
(0, 32767, 0), (0, 16384, 0),
(32767, 32767, 0), (32767, 16384, 0),
(7928, 16384, 32767), (7928, 16384, 32767),
(24840, 16384, 32767)), (24840, 16384, 32767)),
'speaker_ids': (0, 1, 2, 4, 5, 6, 7), 'speaker_ids': (0, 1, 2, 4, 5, 6, 7),
@@ -250,8 +250,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
{'coordinates_q15': ((0, 0, 0), {'coordinates_q15': ((0, 0, 0),
(32767, 0, 0), (32767, 0, 0),
(16384, 0, 0), (16384, 0, 0),
(0, 32767, 0), (0, 16384, 0),
(32767, 32767, 0), (32767, 16384, 0),
(7928, 16384, 32767), (7928, 16384, 32767),
(24840, 16384, 32767)), (24840, 16384, 32767)),
'speaker_ids': (0, 1, 2, 4, 5, 6, 7), 'speaker_ids': (0, 1, 2, 4, 5, 6, 7),
@@ -261,8 +261,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
{'coordinates_q15': ((0, 0, 0), {'coordinates_q15': ((0, 0, 0),
(32767, 0, 0), (32767, 0, 0),
(16384, 0, 0), (16384, 0, 0),
(0, 32767, 0), (0, 16384, 0),
(32767, 32767, 0), (32767, 16384, 0),
(7928, 16384, 32767), (7928, 16384, 32767),
(24840, 16384, 32767)), (24840, 16384, 32767)),
'speaker_ids': (0, 1, 2, 4, 5, 6, 7), 'speaker_ids': (0, 1, 2, 4, 5, 6, 7),
@@ -270,8 +270,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis1_groups': ((5, 6),), 'axis1_groups': ((5, 6),),
'mode': 3}, 'mode': 3},
{'coordinates_q15': ((16384, 0, 0), {'coordinates_q15': ((16384, 0, 0),
(0, 32767, 0), (0, 16384, 0),
(32767, 32767, 0), (32767, 16384, 0),
(7928, 16384, 32767), (7928, 16384, 32767),
(24840, 16384, 32767)), (24840, 16384, 32767)),
'speaker_ids': (2, 4, 5, 6, 7), 'speaker_ids': (2, 4, 5, 6, 7),
@@ -287,8 +287,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis0_groups': ((0, 2, 1),), 'axis0_groups': ((0, 2, 1),),
'axis1_groups': ((3, 4),), 'axis1_groups': ((3, 4),),
'mode': 3}, 'mode': 3},
{'coordinates_q15': ((0, 32767, 0), {'coordinates_q15': ((0, 16384, 0),
(32767, 32767, 0), (32767, 16384, 0),
(7928, 16384, 32767), (7928, 16384, 32767),
(24840, 16384, 32767)), (24840, 16384, 32767)),
'speaker_ids': (4, 5, 6, 7), 'speaker_ids': (4, 5, 6, 7),
@@ -307,8 +307,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'regions': ({'coordinates_q15': ((0, 0, 0), 'regions': ({'coordinates_q15': ((0, 0, 0),
(32767, 0, 0), (32767, 0, 0),
(16384, 0, 0), (16384, 0, 0),
(0, 32767, 0), (0, 16384, 0),
(32767, 32767, 0), (32767, 16384, 0),
(7928, 7928, 32767), (7928, 7928, 32767),
(24840, 7928, 32767), (24840, 7928, 32767),
(7928, 24840, 32767), (7928, 24840, 32767),
@@ -320,8 +320,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
{'coordinates_q15': ((0, 0, 0), {'coordinates_q15': ((0, 0, 0),
(32767, 0, 0), (32767, 0, 0),
(16384, 0, 0), (16384, 0, 0),
(0, 32767, 0), (0, 16384, 0),
(32767, 32767, 0), (32767, 16384, 0),
(7928, 7928, 32767), (7928, 7928, 32767),
(24840, 7928, 32767), (24840, 7928, 32767),
(7928, 24840, 32767), (7928, 24840, 32767),
@@ -333,8 +333,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
{'coordinates_q15': ((0, 0, 0), {'coordinates_q15': ((0, 0, 0),
(32767, 0, 0), (32767, 0, 0),
(16384, 0, 0), (16384, 0, 0),
(0, 32767, 0), (0, 16384, 0),
(32767, 32767, 0), (32767, 16384, 0),
(7928, 7928, 32767), (7928, 7928, 32767),
(24840, 7928, 32767), (24840, 7928, 32767),
(7928, 24840, 32767), (7928, 24840, 32767),
@@ -344,8 +344,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis1_groups': ((5, 6), (7, 8)), 'axis1_groups': ((5, 6), (7, 8)),
'mode': 3}, 'mode': 3},
{'coordinates_q15': ((16384, 0, 0), {'coordinates_q15': ((16384, 0, 0),
(0, 32767, 0), (0, 16384, 0),
(32767, 32767, 0), (32767, 16384, 0),
(7928, 7928, 32767), (7928, 7928, 32767),
(24840, 7928, 32767), (24840, 7928, 32767),
(7928, 24840, 32767), (7928, 24840, 32767),
@@ -365,8 +365,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis0_groups': ((0, 2, 1),), 'axis0_groups': ((0, 2, 1),),
'axis1_groups': ((3, 4), (5, 6)), 'axis1_groups': ((3, 4), (5, 6)),
'mode': 3}, 'mode': 3},
{'coordinates_q15': ((0, 32767, 0), {'coordinates_q15': ((0, 16384, 0),
(32767, 32767, 0), (32767, 16384, 0),
(7928, 7928, 32767), (7928, 7928, 32767),
(24840, 7928, 32767), (24840, 7928, 32767),
(7928, 24840, 32767), (7928, 24840, 32767),
@@ -774,6 +774,34 @@ def _make_layout(name: str, item: dict) -> SpeakerLayout:
SPEAKER_LAYOUTS = {name: _make_layout(name, item) for name, item in _RAW_LAYOUTS.items()} SPEAKER_LAYOUTS = {name: _make_layout(name, item) for name, item in _RAW_LAYOUTS.items()}
def _make_six_zero():
"""6.0: L/R, side pair and rear pair without centre or LFE."""
base = SPEAKER_LAYOUTS["71"]
channels = ("L", "R", "Ls", "Rs", "Lrs", "Rrs")
source_ids = (0, 1, 4, 5, 6, 7)
remap = {old: new for new, old in enumerate(source_ids)}
regions = []
for region in base.regions:
kept = [index for index, speaker in enumerate(region.speaker_ids)
if speaker in remap]
if not kept:
continue
local = {old: new for new, old in enumerate(kept)}
def groups(items):
return tuple(tuple(local[index] for index in group if index in local)
for group in items if any(index in local for index in group))
regions.append(RegionGeometry(
tuple(region.coordinates_q15[index] for index in kept),
tuple(remap[region.speaker_ids[index]] for index in kept),
groups(region.axis0_groups), groups(region.axis1_groups),
region.mode))
return SpeakerLayout("60", 255, 25, channels,
tuple(range(6)), tuple(regions))
SPEAKER_LAYOUTS["60"] = _make_six_zero()
SPEAKER_LAYOUT_ALIASES = { SPEAKER_LAYOUT_ALIASES = {
"2.0": "20", "2.0": "20",
"3.1": "31", "3.1": "31",
@@ -786,6 +814,7 @@ SPEAKER_LAYOUT_ALIASES = {
"7.1.4": "714", "7.1.4": "714",
"9.1.4": "914", "9.1.4": "914",
"9.1.6": "916", "9.1.6": "916",
"6.0": "60",
} }
SPEAKER_LAYOUT_CHOICES = tuple(SPEAKER_LAYOUT_ALIASES) SPEAKER_LAYOUT_CHOICES = tuple(SPEAKER_LAYOUT_ALIASES)
SPEAKER_LAYOUT_DISPLAY_NAMES = {value: key for key, value in SPEAKER_LAYOUT_ALIASES.items()} SPEAKER_LAYOUT_DISPLAY_NAMES = {value: key for key, value in SPEAKER_LAYOUT_ALIASES.items()}
+497 -39
View File
@@ -13,7 +13,10 @@ from typing import Iterable
import numpy as np import numpy as np
import oamd_math
from oamd_bits import JocFieldState, frame_update, parse_trim from oamd_bits import JocFieldState, frame_update, parse_trim
from oamd_params import metadata_params
from variant_error import UnsupportedVariantError
from speaker_layouts import RegionGeometry, SpeakerLayout, get_speaker_layout from speaker_layouts import RegionGeometry, SpeakerLayout, get_speaker_layout
from trim_balance import (SpeakerRenderParameters, apply_balance, from trim_balance import (SpeakerRenderParameters, apply_balance,
explicit_position_gain, parameters_from_oamd) explicit_position_gain, parameters_from_oamd)
@@ -21,6 +24,57 @@ from trim_balance import (SpeakerRenderParameters, apply_balance,
Q15_SCALE = 32768.0 Q15_SCALE = 32768.0
Q15_MAX = 32767.0 / Q15_SCALE Q15_MAX = 32767.0 / Q15_SCALE
GAIN_SNAP_THRESHOLD = 1.0e-4 GAIN_SNAP_THRESHOLD = 1.0e-4
ZONE_TO_REGION = (0, 1, 2, 3, 4, 5, 6, 0)
LFE_BED_LABELS = ("RC_LFE", "RC_LFE2")
BED_LABEL_INDEX = {label: index + 1 for index, label in
enumerate(("RC_L", "RC_R", "RC_C", "RC_LFE",
"RC_LS", "RC_RS", "RC_LB", "RC_RB",
"RC_TFL", "RC_TFR", "RC_TSL", "RC_TSR",
"RC_TBL", "RC_TBR", "RC_LW", "RC_RW",
"RC_LFE2"))}
def _stable_norm(vector):
"""Reference vector normalization: tiny-norm floor and unit clamp."""
values = np.asarray(vector, dtype=np.float64)
total = float(np.dot(values, values))
if total == 0.0:
return np.zeros_like(values)
result = values / max(math.sqrt(total), 1.0e-5)
return np.minimum(result, 1.0)
def _region_vector(layout, region_index, u, floor_v, top_v, w,
enable_height):
"""Uncompensated point-source vector in ``layout`` internal order."""
region = layout.regions[int(region_index)]
coordinates = _coordinates(region)
floor = _plane_gains(region, coordinates, region.axis0_groups, u,
floor_v, region.mode)
gains = floor
if region.mode == 3:
top = _plane_gains(region, coordinates, region.axis1_groups, u,
top_v, 3)
height = min(max(float(w) if enable_height else 0.0, 0.0),
Q15_MAX)
if height >= Q15_MAX:
gains = top
elif height > 0.0:
floor_weight, height_weight = equal_power_pair(height)
gains = floor * floor_weight + top * height_weight
vector = np.zeros(layout.channel_count, dtype=np.float64)
for value, speaker in zip(gains, region.speaker_ids):
vector[speaker] = value
return vector
def _fold_vector(calc, final, warp_mode, vector):
"""Apply the existing warp 2/3 surround fold to a calculation vector."""
if calc is final:
return vector
return (_surround_fold_matrix(calc.speaker_bitfield,
final.speaker_bitfield, warp_mode)
@ vector)
def bed_object_targets(bed_channels) -> dict[int, tuple[float, float, float]]: def bed_object_targets(bed_channels) -> dict[int, tuple[float, float, float]]:
@@ -205,6 +259,8 @@ def _surround_fold_layout(target: SpeakerLayout) -> SpeakerLayout:
expanded = {"51": "71", "512": "712", "514": "714"} expanded = {"51": "71", "512": "712", "514": "714"}
if target.name in expanded: if target.name in expanded:
return get_speaker_layout(expanded[target.name]) return get_speaker_layout(expanded[target.name])
if target.name == "60":
return target
if target.name != "40": if target.name != "40":
raise ValueError(f"unsupported surround fold layout: {target.name}") raise ValueError(f"unsupported surround fold layout: {target.name}")
# 4.0 expands to six full-band speakers without adding centre or LFE. # 4.0 expands to six full-band speakers without adding centre or LFE.
@@ -299,10 +355,18 @@ def ramp_block_count(duration_samples: int, block_size: int = 32,
class PythonSpeakerRenderer: class PythonSpeakerRenderer:
"""Stateful float64 speaker renderer.""" """Stateful float64 speaker renderer.
Two input shapes are accepted. Payloads that expand to block-level object
state (``frame_update`` with ``object_blocks``) use the metadata path:
object gain/inactive, region/elevation, size/snap/divergence/refinement,
priority budget, ISF and bed distribution. Older payload dictionaries keep
the previous point-position path so existing callers and tests still work.
"""
def __init__(self, layout: str | SpeakerLayout, block_size: int = 32, def __init__(self, layout: str | SpeakerLayout, block_size: int = 32,
bed_channels=None, trim_meta=None, *, parameters=None): bed_channels=None, trim_meta=None, *, parameters=None,
size_budget: int = 0):
self.layout = get_speaker_layout(layout) if isinstance(layout, str) else layout self.layout = get_speaker_layout(layout) if isinstance(layout, str) else layout
self.block_size = int(block_size) self.block_size = int(block_size)
if self.block_size <= 0: if self.block_size <= 0:
@@ -312,6 +376,11 @@ class PythonSpeakerRenderer:
raise ValueError("pass normalized parameters or OAMD trim_meta, not both") raise ValueError("pass normalized parameters or OAMD trim_meta, not both")
self._parameters_override = parameters self._parameters_override = parameters
self._initial_parameters = parameters or parameters_from_oamd(trim_meta, self.layout) self._initial_parameters = parameters or parameters_from_oamd(trim_meta, self.layout)
self._size_budget = int(size_budget)
if self._size_budget < 0:
raise ValueError("size_budget must be non-negative")
self._raw_trim_mode = (trim_meta or {}).get("global_trim_mode", 0)
self._meta_params = metadata_params()
self.reset() self.reset()
def reset(self): def reset(self):
@@ -323,16 +392,341 @@ class PythonSpeakerRenderer:
self._step = np.zeros_like(self._current) self._step = np.zeros_like(self._current)
self._remaining = np.zeros((15, channels), dtype=np.int32) self._remaining = np.zeros((15, channels), dtype=np.int32)
self._window = np.arange(self.block_size, dtype=np.float64) / float(self.block_size) self._window = np.arange(self.block_size, dtype=np.float64) / float(self.block_size)
self._meta_mode = False
self._meta_ready = False
self._n_meta = 0
self._meta_slots: list[int | None] = []
self._meta_labels: list[str | None] = []
self._mc = None
self._mt = None
self._ms = None
self._mr = None
def _apply_payload(self, payload): # -------------------------------------------------------------- metadata state
update = frame_update(payload) def _update_trim(self, payload, update=None):
if self._parameters_override is None: trim = update.get("trim") if update is not None else None
if trim is None and update is None:
trim = parse_trim(payload) trim = parse_trim(payload)
if trim is not None: if trim is not None and self._parameters_override is None:
self.parameters = parameters_from_oamd(trim, self.layout) self.parameters = parameters_from_oamd(trim, self.layout)
if isinstance(trim, dict) and "global_trim_mode" in trim:
self._raw_trim_mode = trim["global_trim_mode"]
def _init_meta(self, update):
self._isf_cache = {}
program = update["program"]
kinds = update["kinds"]
count = int(update["object_count"])
labels = [label for group in program["bed_channels"] for label in group]
num_bed = int(program["num_bed_objects"])
if len(labels) != num_bed:
if program["dynamic_object_only"]:
labels = ["RC_LFE"] if program["lfe_present"] else []
if len(labels) != num_bed:
raise UnsupportedVariantError(
"speaker", "bed_declaration_mismatch",
"OAMD bed 声明数量与节目分区不一致",
details={"declared": len(labels), "num_bed_objects": num_bed})
slots = []
slot = 1
for object_index, kind in enumerate(kinds):
label = labels[object_index] if object_index < len(labels) else None
if kind == "bed" and label in LFE_BED_LABELS:
slots.append(0)
else:
if slot > 15:
raise UnsupportedVariantError(
"speaker", "object_slot_overflow",
"元数据对象数量超出 15 个 JOC 对象槽",
details={"object_count": count})
slots.append(slot)
slot += 1
if not program["dynamic_object_only"]:
declared = program["num_dynamic_objects"]
if (declared is not None
and num_bed + int(program["num_isf_objects"]) + int(declared)
!= count):
raise UnsupportedVariantError(
"speaker", "program_object_count_mismatch",
"OAMD 节目声明的对象分区与对象数不一致",
details={"object_count": count,
"num_bed_objects": num_bed,
"num_isf_objects": program["num_isf_objects"],
"num_dynamic_objects": declared})
if program["num_isf_objects"]:
isf_idx = program["isf_idx"]
config = (self._meta_params["isf"]["configs"][isf_idx]
if isf_idx is not None and isf_idx < 6 else None)
if config is None or config["counts"][4] != program["num_isf_objects"]:
raise UnsupportedVariantError(
"speaker", "isf_configuration_mismatch",
"ISF 配置数量与节目声明不一致",
details={"isf_idx": isf_idx,
"num_isf_objects": program["num_isf_objects"]})
if count != self._n_meta or slots != self._meta_slots:
channels = self.layout.channel_count
self._n_meta = count
self._meta_slots = slots
self._mc = np.zeros((count, channels), dtype=np.float64)
self._mt = np.zeros((count, channels), dtype=np.float64)
self._ms = np.zeros((count, channels), dtype=np.float64)
self._mr = np.zeros((count, channels), dtype=np.int32)
self._meta_labels = [labels[object_index]
if object_index < len(labels) else None
for object_index in range(count)]
self._meta_mode = True
def _block_targets(self, update, block_index):
count = int(update["object_count"])
channels = self.layout.channel_count
targets = np.zeros((count, channels), dtype=np.float64)
blocks = update["object_blocks"]
kinds = update["kinds"]
zero_size = set()
size_objects = []
for object_index in range(count):
state = blocks[object_index][block_index]
if (kinds[object_index] == "dynamic" and not state["not_active"]
and state["size_mode"] in (1, 2)
and not state["snap"] and state["divergence_q"] == 0):
size_objects.append(object_index)
if 0 < self._size_budget < len(size_objects):
ranked = sorted(
size_objects,
key=lambda index: (-((int(blocks[index][block_index]["priority_q"])
+ 512) >> 10), index))
zero_size = set(ranked[self._size_budget:])
for object_index in range(count):
state = blocks[object_index][block_index]
if state["not_active"]:
continue
spatial = self._object_spatial(update, object_index, block_index,
object_index in zero_size)
gain = float(state["gain_q"]) / 4096.0
targets[object_index] = spatial * gain
return targets
def _object_spatial(self, update, object_index, block_index, force_zero_size):
kind = update["kinds"][object_index]
if kind == "dynamic":
return self._dynamic_spatial(update, object_index, block_index,
force_zero_size)
if kind == "bed":
return self._bed_spatial(update, object_index)
if kind == "isf":
return self._isf_spatial(update, object_index)
return np.zeros(self.layout.channel_count, dtype=np.float64)
# -------------------------------------------------------------- dynamic path
def _dynamic_spatial(self, update, object_index, block_index,
force_zero_size):
state = update["object_blocks"][object_index][block_index]
final = self.layout
warp = self.parameters.warp_mode
calc = _surround_fold_layout(final) if warp in (2, 3) else final
region_index = ZONE_TO_REGION[int(state["zone_idx"]) & 7]
u = float(state["qx"]) / Q15_SCALE
v = float(state["qy"]) / Q15_SCALE
w = max(float(state["qz"]) / Q15_SCALE, 0.0)
enable_height = bool(state["enable_elevation"])
if not enable_height:
w = 0.0
bypass = self.parameters.bypasses(object_index)
floor_v = min(max(math.ldexp(v, floor_y_exponent(
calc.speaker_bitfield, warp != 0)), 0.0), 1.0)
top_v = min(max(v, 0.0), 1.0)
trim_v = v
if self.parameters.trim_mode == "explicit" and not bypass:
listener = self.parameters.listener_balance
overhead = self.parameters.overhead_balance
floor_v = apply_balance(floor_v, w, listener, overhead)
top_v = apply_balance(top_v, w, listener, overhead)
trim_v = apply_balance(v, w, listener, overhead)
region = calc.regions[region_index]
coordinates = _coordinates(region)
if state["snap"]:
point = _region_vector(calc, region_index, u, floor_v, top_v, w,
enable_height)
best = -1.0
chosen = 0
for index, speaker in enumerate(region.speaker_ids):
value = float(point[speaker])
if value > best:
best = value
chosen = index
speaker_u, speaker_v, speaker_w = coordinates[chosen]
scale = 1.0
distance = ((u - speaker_u) ** 2
+ (min(v, scale) - scale * speaker_v) ** 2
+ (w - speaker_w) ** 2)
if distance < 0.04:
vector = np.zeros(calc.channel_count, dtype=np.float64)
vector[region.speaker_ids[chosen]] = 1.0
comp = (float(speaker_u), float(speaker_v), float(speaker_w))
else:
vector = point
comp = (u, trim_v, w)
elif state["divergence_q"] > 0:
spread = min(float(state["divergence_q"]) / Q15_SCALE, u, 1.0 - u)
left = _region_vector(calc, region_index, u - spread, floor_v,
top_v, w, enable_height)
right = _region_vector(calc, region_index, u + spread, floor_v,
top_v, w, enable_height)
vector = _stable_norm(left + right)
comp = (u, trim_v, w)
elif state["size_mode"] in (1, 2) and not force_zero_size:
size_q = state["size_q"]
if size_q[0] == size_q[1] == size_q[2]:
q_size = int(size_q[0])
else:
q_size = (int(size_q[0]) + int(size_q[1]) + int(size_q[2])) // 3
size = q_size / Q15_SCALE
t = oamd_math.size_curve_value(size)
point = _region_vector(calc, region_index, u, floor_v, top_v, w,
enable_height)
if t < 0.000025:
vector = point
else:
query = (u, (1.0 - w) * floor_v + w * top_v, w)
spread = np.asarray(oamd_math.size_targets(
self._meta_params, calc, region_index, query, size,
height=enable_height), dtype=np.float64)
if t > 0.05:
vector = spread
else:
angle = 20.0 * t
if angle >= 0.99996948:
cosine, sine = 0.0, 1.0
else:
cosine = math.cos(angle * math.pi / 2.0)
sine = math.sin(angle * math.pi / 2.0)
vector = _stable_norm(0.5 * cosine * point
+ 0.5 * sine * spread)
comp = (u, trim_v, w)
else:
vector = _region_vector(calc, region_index, u, floor_v, top_v, w,
enable_height)
comp = (u, trim_v, w)
compensation = 1.0
if not bypass:
if self.parameters.trim_mode == "automatic":
compensation = position_gain(final.speaker_bitfield,
comp[1], comp[2])
elif self.parameters.trim_mode == "explicit":
compensation = explicit_position_gain(self.parameters,
comp[0], comp[1], comp[2])
return _fold_vector(calc, final, warp, vector) * compensation
# -------------------------------------------------------------- bed path
def _bed_spatial(self, update, object_index):
final = self.layout
warp = self.parameters.warp_mode
calc = _surround_fold_layout(final) if warp in (2, 3) else final
label = self._meta_labels[object_index]
bed_index = BED_LABEL_INDEX.get(label)
if bed_index is None:
return np.zeros(final.channel_count, dtype=np.float64)
program = update["program"]
route, weights = oamd_math.bed_distribution(
calc.name, bed_index, int(program["bed_mask"]),
bool(program["b_bed_chan_distribute"]))
bypass = self.parameters.bypasses(object_index)
mode = {"automatic": 0, "disabled": 1, "explicit": 2}[
self.parameters.trim_mode]
if route != -1:
position = oamd_math.bed_reference_position(calc.name, route)
qx, qy, qz = (int(value) for value in position)
if qz > 0:
qx = 32767 if qx > 16384 else 0
u, v = qx / Q15_SCALE, qy / Q15_SCALE
w = max(qz, 0) / Q15_SCALE
floor_v = min(max(math.ldexp(v, floor_y_exponent(
calc.speaker_bitfield, warp != 0)), 0.0), 1.0)
top_v = min(max(v, 0.0), 1.0)
vector = _region_vector(calc, 0, u, floor_v, top_v, w, True)
compensation = 1.0
if not bypass and self.parameters.trim_mode != "disabled":
if self.parameters.trim_mode == "automatic":
compensation = position_gain(final.speaker_bitfield, v, w)
else:
compensation = explicit_position_gain(
self.parameters, u, v, w)
else:
vector = np.asarray(weights, dtype=np.float64).copy()
levels = (self.parameters.surround_db,
self.parameters.height_db,
self.parameters.centre_db)
compensation = float(oamd_math.bed_compensation(
final.name, bed_index, mode, bypass, levels))
return _fold_vector(calc, final, warp, vector) * compensation
# -------------------------------------------------------------- ISF path
def _isf_spatial(self, update, object_index):
program = update["program"]
isf_idx = int(program["isf_idx"])
row = object_index - int(program["num_bed_objects"])
final = self.layout
warp = self.parameters.warp_mode
calc = _surround_fold_layout(final) if warp in (2, 3) else final
key = (calc.name, isf_idx)
matrix = self._isf_cache.get(key)
if matrix is None:
matrix = oamd_math.isf_matrix(self._meta_params, calc.name, isf_idx)
self._isf_cache[key] = matrix
vector = np.asarray(matrix[row], dtype=np.float64)
disabled = (int(self._raw_trim_mode) == 1
or self.parameters.bypasses(object_index))
compensation = float(oamd_math.isf_compensation(
self._meta_params, final.name, isf_idx, row, disabled))
return _fold_vector(calc, final, warp, vector) * compensation
# -------------------------------------------------------------- scheduling
def _apply_meta_targets(self, targets, ramp_samples):
channels = self.layout.channel_count
if self._mc is None or self._mc.shape != (len(targets), channels):
self._mc = np.zeros((len(targets), channels), dtype=np.float64)
self._mt = np.zeros_like(self._mc)
self._ms = np.zeros_like(self._mc)
self._mr = np.zeros((len(targets), channels), dtype=np.int32)
difference = np.asarray(targets, dtype=np.float64) - self._mc
significant = np.abs(difference) >= GAIN_SNAP_THRESHOLD
self._mt[...] = targets
self._mc[~significant] = self._mt[~significant]
self._ms[~significant] = 0.0
self._mr[~significant] = 0
blocks = ramp_block_count(int(ramp_samples), self.block_size)
if blocks:
self._ms[significant] = difference[significant] / float(blocks)
self._mr[significant] = blocks
else:
self._mc[significant] = self._mt[significant]
self._ms[significant] = 0.0
self._mr[significant] = 0
self._meta_ready = True
def _schedule_payload(self, update, event_sample):
if "object_blocks" not in update or not update["object_count"]:
return False
if update.get("trim") is not None:
self._update_trim(None, update)
self._init_meta(update)
for block_index, block in enumerate(update["blocks"]):
sample = (int(event_sample) + int(update["sample_offset"])
+ int(block["block_offset_samples"]))
aligned = align_metadata_sample(sample, self.block_size)
targets = self._block_targets(update, block_index)
self._pending.setdefault(aligned // self.block_size, []).append(
(targets, block["ramp_duration_samples"]))
return True
# -------------------------------------------------------------- legacy path
def _apply_legacy(self, update):
trim = parse_trim(self._current_payload)
if trim is not None and self._parameters_override is None:
self.parameters = parameters_from_oamd(trim, self.layout)
self._state.apply(update["values"]) self._state.apply(update["values"])
new_target = _all_object_targets(self.layout, self._state, self.bed_targets, new_target = _all_object_targets(self.layout, self._state,
self.parameters) self.bed_targets, self.parameters)
blocks = ramp_block_count(update["ramp_duration_samples"], self.block_size) blocks = ramp_block_count(update["ramp_duration_samples"], self.block_size)
difference = new_target - self._current difference = new_target - self._current
significant = np.abs(difference) >= GAIN_SNAP_THRESHOLD significant = np.abs(difference) >= GAIN_SNAP_THRESHOLD
@@ -348,6 +742,62 @@ class PythonSpeakerRenderer:
self._step[significant] = 0.0 self._step[significant] = 0.0
self._remaining[significant] = 0 self._remaining[significant] = 0
def _apply_payload(self, payload):
self._current_payload = payload
update = frame_update(payload)
if "object_blocks" in update and update["object_count"]:
self._update_trim(payload, update)
self._init_meta(update)
targets = self._block_targets(update, 0)
self._apply_meta_targets(targets,
update["blocks"][0]["ramp_duration_samples"])
else:
self._apply_legacy(update)
# -------------------------------------------------------------- process
def _render_meta_block(self, source, start, stop, out_block):
for object_index in range(self._n_meta):
slot = self._meta_slots[object_index]
if slot is None:
continue
active = self._mr[object_index] > 0
gains = np.broadcast_to(self._mt[object_index],
(self.block_size, self.layout.channel_count)).copy()
if np.any(active):
gains[:, active] = (
self._mc[object_index, active][None, :]
+ self._window[:, None] * self._ms[object_index, active][None, :]
)
out_block += source[start:stop, slot, None] * gains
if np.any(active):
self._mc[object_index, active] += self._ms[object_index, active]
self._mr[object_index, active] -= 1
finished = self._mr[object_index] == 0
self._mc[object_index, finished] = self._mt[object_index, finished]
def _render_legacy_block(self, source, start, stop, out_block):
channels = self.layout.channel_count
if "LFE" in self.layout.channels:
out_block[:, 3] = source[start:stop, 0].astype(np.float64, copy=False)
for object_index in range(15):
active = self._remaining[object_index] > 0
gains = np.broadcast_to(self._target[object_index],
(self.block_size, channels)).copy()
if np.any(active):
gains[:, active] = (
self._current[object_index, active][None, :]
+ self._window[:, None] * self._step[object_index, active][None, :]
)
out_block += (
source[start:stop, object_index + 1, None].astype(np.float64, copy=False)
* gains
)
if np.any(active):
self._current[object_index, active] += self._step[object_index, active]
self._remaining[object_index, active] -= 1
finished = self._remaining[object_index] == 0
self._current[object_index, finished] = self._target[object_index, finished]
def process(self, objects16: np.ndarray, events=(), *, standard_order: bool = True, def process(self, objects16: np.ndarray, events=(), *, standard_order: bool = True,
output: np.ndarray | None = None) -> np.ndarray: output: np.ndarray | None = None) -> np.ndarray:
source = np.asarray(objects16) source = np.asarray(objects16)
@@ -363,44 +813,48 @@ class PythonSpeakerRenderer:
raise ValueError((output.shape, output.dtype)) raise ValueError((output.shape, output.dtype))
else: else:
output[...] = 0.0 output[...] = 0.0
if "LFE" in self.layout.channels:
output[:, 3] = source[:, 0].astype(np.float64, copy=False)
events_by_block: dict[int, list[bytes]] = defaultdict(list) events_by_block: dict[int, list[tuple[int, bytes]]] = defaultdict(list)
for sample_offset, payload in events: for sample_offset, payload in events:
if not 0 <= int(sample_offset) <= total_samples: if not 0 <= int(sample_offset) <= total_samples:
raise ValueError(f"metadata offset outside process call: {sample_offset}") raise ValueError(f"metadata offset outside process call: {sample_offset}")
block = align_metadata_sample(sample_offset, self.block_size) // self.block_size block = align_metadata_sample(sample_offset, self.block_size) // self.block_size
events_by_block[block].append(bytes(payload)) events_by_block[block].append((align_metadata_sample(
sample_offset, self.block_size), bytes(payload)))
self._pending: dict[int, list] = {}
total_blocks = total_samples // self.block_size total_blocks = total_samples // self.block_size
for block in range(total_blocks): for block in range(total_blocks):
for payload in events_by_block.get(block, ()): for event_sample, payload in events_by_block.get(block, ()):
self._apply_payload(payload) update = frame_update(payload)
if "object_blocks" in update and update["object_count"]:
self._schedule_payload(update, event_sample)
else:
self._current_payload = payload
self._apply_legacy(update)
for targets, ramp_samples in self._pending.pop(block, ()):
self._apply_meta_targets(targets, ramp_samples)
start = block * self.block_size start = block * self.block_size
stop = start + self.block_size stop = start + self.block_size
out_block = output[start:stop] out_block = output[start:stop]
for object_index in range(15): if self._meta_mode and self._meta_ready:
active = self._remaining[object_index] > 0 self._render_meta_block(source, start, stop, out_block)
gains = np.broadcast_to(self._target[object_index], else:
(self.block_size, channels)).copy() self._render_legacy_block(source, start, stop, out_block)
if np.any(active):
gains[:, active] = (
self._current[object_index, active][None, :]
+ self._window[:, None] * self._step[object_index, active][None, :]
)
out_block += (
source[start:stop, object_index + 1, None].astype(np.float64, copy=False)
* gains
)
if np.any(active):
self._current[object_index, active] += self._step[object_index, active]
self._remaining[object_index, active] -= 1
finished = self._remaining[object_index] == 0
self._current[object_index, finished] = self._target[object_index, finished]
for payload in events_by_block.get(total_blocks, ()): for event_sample, payload in events_by_block.get(total_blocks, ()):
self._apply_payload(payload) update = frame_update(payload)
if "object_blocks" in update and update["object_count"]:
self._schedule_payload(update, event_sample)
else:
self._current_payload = payload
self._apply_legacy(update)
for targets, ramp_samples in self._pending.pop(total_blocks, ()):
self._apply_meta_targets(targets, ramp_samples)
for block_index in sorted(self._pending):
for targets, ramp_samples in self._pending[block_index]:
self._apply_meta_targets(targets, ramp_samples)
self._pending.clear()
if standard_order: if standard_order:
return output[:, list(self.layout.internal_to_standard)] return output[:, list(self.layout.internal_to_standard)]
return output return output
@@ -416,11 +870,14 @@ class PythonSpeakerRenderer:
def render_objects16(objects16: np.ndarray, payload_events: Iterable[tuple[int, bytes]], def render_objects16(objects16: np.ndarray, payload_events: Iterable[tuple[int, bytes]],
layout: str | SpeakerLayout, *, block_size: int = 32, layout: str | SpeakerLayout, *, block_size: int = 32,
standard_order: bool = True, output: np.ndarray | None = None, standard_order: bool = True, output: np.ndarray | None = None,
trim_meta=None, parameters=None) -> np.ndarray: trim_meta=None, parameters=None, size_budget: int = 0) -> np.ndarray:
"""Render a complete interleaved LFE + 15 object stream.""" """Render a complete interleaved LFE + 15 object stream."""
renderer = PythonSpeakerRenderer(layout, block_size=block_size, renderer = PythonSpeakerRenderer(layout, block_size=block_size,
trim_meta=trim_meta, parameters=parameters) trim_meta=trim_meta, parameters=parameters,
return renderer.process(objects16, payload_events, standard_order=standard_order, output=output) size_budget=size_budget)
return renderer.process(objects16, payload_events, standard_order=standard_order,
output=output)
def payload_events_from_index(index, frame_count: int, *, frame_samples: int = 1536, def payload_events_from_index(index, frame_count: int, *, frame_samples: int = 1536,
payload_id: int = 11, outer_offset_samples: int = 1473): payload_id: int = 11, outer_offset_samples: int = 1473):
@@ -429,6 +886,7 @@ def payload_events_from_index(index, frame_count: int, *, frame_samples: int = 1
if payload_id in subpayloads: if payload_id in subpayloads:
timing = frame_update(subpayloads[payload_id]) timing = frame_update(subpayloads[payload_id])
yield ( yield (
frame * frame_samples + outer_offset_samples + timing["block_offset_samples"], frame * frame_samples + outer_offset_samples
+ int(timing["sample_offset"]),
subpayloads[payload_id], subpayloads[payload_id],
) )