Fix JOC clipgain, offset_ts, frame status, and renderer state machine

Decode clipgain multiplicatively as 2^(x-4)*(1 + y/32), keep the raw
5-bit offset_ts values and reject frames whose offsets leave 0..23 or are
not strictly increasing, and read the EMDF sample offset as
variable_bits(11). Report parse errors through the frame status: 2
unsupported downmix configuration, 3 object count clamped, 4 parameter
band index, 5 invalid offset_ts, 6 trailing data after byte padding.
Correct the 15/9/5/3 subband-to-parameter-band rows.

Apply the clipgain ramp to the LFE samples before the 1217-sample delay,
stop fading objects in from zero when they have no previous matrix, and
keep disappeared objects published with an all-zero matrix until they
reappear.

Render each frame through the seq state machine: hold the previous
matrices while the decoder is unsynchronized, hard cut on seq 0 or a core
channel count change, and pass the delayed core PCM through when no JOC
metadata is present. Keep the 16-channel output as LFE + 15 objects so
CBA bed channels stay on the fixed-position object path that the ADM,
speaker, and binaural backends already consume.

Update the math notes and README for the multiplicative clipgain, the
no-previous-state interpolation, the LFE gain ramp, and the frame-level
decoding behavior. State that E-AC-3 input without JOC metadata is
outside the supported scope.
This commit is contained in:
TheM14 committed 2026-11-06 11:35:59 +08:00
1 parent 4aef8db36e
commit 0820444f45
8 files changed
+469 -138

No files matched your search

+10 -8
View File
@@ -293,9 +293,10 @@ The main documented stages are:
- dense JOC differential reconstruction and dequantization;
- parameter-band mapping to 64 QMF subbands;
- cross-frame parameter interpolation;
- cross-frame parameter interpolation (hard cut when no previous state exists, zero-matrix publishing after an object disappears);
- the frame-level JOC state machine (normal, hold, hard cut, core passthrough);
- analysis/inverse QMF, surround delay, and FIR state;
- the 1217-sample LFE delay;
- the 1217-sample LFE delay with its sample-wise clipgain ramp;
- OAMD Q15 coordinate conversion;
- equal-power panning over target-layout regions;
- layout-dependent position compensation and sample-wise gain ramps;
@@ -310,15 +311,16 @@ See the [mathematical notes](docs/math.en.md) for the equations used by the deco
- All `joc_dmx_config_idx` values 0..4 are covered ($N=5,7,7,5,7$; the phase switch $\sigma(k)$ gates the Ls/Rs $-j$ rotation and the band-0 FIR together). The 7-channel configurations need an 8-channel bed and a dependent-substream `chanmap` to identify the extra pair: a missing independent substream or a missing `chanmap` is still a variant error, while a `chanmap` that disagrees with $k$ about the extra pair, or an OAMD bed mask that does, is recorded as a diagnostic and the frame still renders. The extra pair's **identity** is metadata only and never reaches the DSP (section 10.1); the bed mapping follows the channel codes the `chanmap` declares.
- 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.
- An audio frame with no JOC EMDF at all (for example when the encoder wrote none for the first frame) emits silent object channels and is recorded in the `[diagnostics]` line and the JSON report's `diagnostics` field.
- E-AC-3 input without JOC metadata (ID11 + ID14) is outside the supported scope. If an individual audio frame has no JOC EMDF, the implementation falls back to the decode state machine (hold the previous matrices while normal rendering was active, otherwise pass through the core PCM 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 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.
- `joc_clipgain` is folded into the **analysis-side scalar** (`scalar = 1/16 x g`, with `g` from the
`x:3 | y:5` field), so the x16 at the output cancels the 1/16 and the clamp sits **after** the gain.
The scalar is kept per frame: the first 256 samples ramp linearly from the previous frame's value and
the rest stays constant. For `g = 1` the interpolation is the identity and
`clamp(x)·1 == clamp(x·1)`.
- `joc_clipgain` is folded into the **analysis-side scalar** (`scalar = 1/16 x g`, with
`g = 2^(x-4)·(1 + y/32)` from the `x:3 | y:5` field, range 0.0625..15.75), so the x16 at the output
cancels the 1/16 and the clamp sits **after** the gain. The scalar is kept per frame: the first 256
samples ramp linearly from the previous frame's value and the rest stays constant. For `g = 1` the
interpolation is the identity and `clamp(x)·1 == clamp(x·1)`. LFE bypasses the analysis but applies
the same full `g` as a sample-wise ramp before its 1217-sample delay.
- A speaker limiter is outside the current primary formula.
- The SOFA importer currently supports the strict `SimpleFreeFieldHRIR` FIR subset; other SOFA conventions require explicit adapters.
- The binaural runtime is fixed at 48 kHz, fifth order, and one measurement-radius shell at a time.
+6 -4
View File
@@ -255,9 +255,10 @@ JustOneCacophony/
- dense JOC 差分还原与去量化;
- 参数带到 64 个 QMF 子带的映射;
- 跨帧参数插值;
- 跨帧参数插值(无上一帧时硬切,对象消失后按零矩阵发布);
- 帧级 JOC 状态机(正常 / 保持 / 硬切 / 核心直通);
- analysis / inverse QMF、环绕声道延迟与 FIR 状态;
- LFE 1217-sample 延迟;
- LFE 1217-sample 延迟与逐点 clipgain 斜坡;
- OAMD Q15 坐标转换;
- 基于目标布局 region 的等功率声像;
- 布局位置补偿与逐样本增益斜坡;
@@ -272,13 +273,14 @@ JustOneCacophony/
- 覆盖 `joc_dmx_config_idx` 0..4 的全部档位($N=5,7,7,5,7$,相位开关 $\sigma(k)$ 同时门控 Ls/Rs 的 $-j$ 旋转与 band-0 FIR)。7 通道档位必须解出 8 通道床,且需要依赖子流的 `chanmap` 才能确定额外那一对:缺少核心子流或缺少 `chanmap` 仍按变体报错;`chanmap` 声明的额外对与 $k$ 不一致、或 OAMD bed 掩码与 $k$ 不一致时,只记入 `diagnostics` 并继续解码——额外那对的**身份**只进元数据、不进 DSP(§10.1),床映射按 `chanmap` 的通道码做。
- 同一音频帧里出现多个含 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" 的素材,我们按"记诊断 + 继续解码"容错处理。
- 音频帧内没有任何 JOC EMDF 时(例如编码器未写首帧元数据),该帧的对象通道静音,并在 stderr 的 `[diagnostics]` 与 JSON 报告的 `diagnostics` 里记一笔。
- 不含 JOC 元数据(ID11 + ID14)的 E-AC-3 输入不在支持范围;解码过程中若个别音频帧没有 JOC EMDF,当前实现按状态机兜底(进入该帧前处于正常渲染时保持渲染,否则直通延迟 1217 采样的核心 PCM),并以 `joc_frame_missing` 记入 `diagnostics`。
- 扬声器与 SOFA 双耳路径当前只覆盖普通点对象;extent、spread、diffuse、divergence、channel lock 等对象控制不在支持范围内。
- 扬声器渲染从 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、单数据点素材。
- `joc_clipgain` 在**分析端**并入分析标量:`标量 = 1/16 × g`(`g = 1 + (y/32)·2^(x−4)`),出口的
- `joc_clipgain` 在**分析端**并入分析标量:`标量 = 1/16 × g`(`g = 2^(x−4)·(1 + y/32)`,值域 0.0625..15.75),出口的
×16 抵消 1/16,净效果是 `×g`,clamp 因此处在增益**之后**。标量按帧保存两个槽(上一帧值 → 本帧值),
本帧前 256 个采样线性过渡、之后为常数,复位后的第一帧直接取新值;`g = 1` 时插值恒等,且 `clamp(x)·1 == clamp(x·1)`。
LFE 走独立通路,但使用同一个完整 `g` 的逐点斜坡,并乘在 1217-sample 延迟之前。
- 扬声器 limiter 不属于当前实现的主公式。
- SOFA importer 当前严格支持 `SimpleFreeFieldHRIR` FIR;其它 SOFA convention 需要显式 adapter。
- 双耳 runtime 固定 48 kHz、五阶和一次选择一个 measurement-radius shell。
+46 -12
View File
@@ -147,15 +147,17 @@ If the clipgain field consists of integer $x$ and mantissa $y$, then
$$
G_{\mathrm{clip}}=
1+\frac{y}{32}2^{x-4}.
2^{x-4}\left(1+\frac{y}{32}\right).
$$
It is folded into the analysis-side scalar $S=\frac{1}{16}G_{\mathrm{clip}}$, which multiplies
every core-bed sample before the QMF analysis; the $\times16$ after the object inverse QMF cancels the
$1/16$, so the net effect is a multiply by $G_{\mathrm{clip}}$ and the $[-1,1]$ clamp sits **after** the
gain. The scalar keeps two per-frame slots: the first 256 samples of a frame ramp linearly from the
previous frame's value to this frame's value and the rest stays constant, and the first frame after a
reset takes the new value directly. LFE bypasses this scalar.
Its range is $[0.0625,15.75]$; at $x=4$ it reduces to $1+y/32$. It is folded into the analysis-side
scalar $S=\frac{1}{16}G_{\mathrm{clip}}$, which multiplies every core-bed sample before the QMF
analysis; the $\times16$ after the object inverse QMF cancels the $1/16$, so the net effect is a multiply
by $G_{\mathrm{clip}}$ and the $[-1,1]$ clamp sits **after** the gain. The scalar keeps two per-frame
slots: the first 256 samples of a frame ramp linearly from the previous frame's value to this frame's
value and the rest stays constant, and the first frame after a reset takes the new value directly. LFE
uses a separate path but applies the same full $G_{\mathrm{clip}}$ as a sample-wise ramp before its
1217-sample delay; see section 8.
## 3. Parameter-band expansion and time interpolation
@@ -190,7 +192,15 @@ Thus $p(b)=k$ if and only if $b\in\mathcal B_k$. Other parameter-band counts use
### 3.2 One-data-point interpolation
Let $P_{o,c,b}$ be the previous frame-end value and $D_{o,c,p(b)}$ the current target. For slot $t=0\ldots23$:
Let $P_{o,c,b}$ be the previous frame-end value and $D_{o,c,p(b)}$ the current target. When the object
has no previous state yet (the first frame of the stream, or the object's first appearance), all 24 slots
take the current target directly (hard cut, no fade-in from zero):
$$
M_{o,c,b,t}=D_{o,c,p(b)},\qquad t=0\ldots23.
$$
With a previous state, for slot $t=0\ldots23$:
$$
\alpha_t=\frac{t+1}{24},
@@ -208,11 +218,29 @@ $$
M_{o,c,b,23}=D_{o,c,p(b)}.
$$
This value then becomes the previous state for the next frame.
This value then becomes the previous state for the next frame. Once an object's present flag goes from 1
to 0, it keeps being published with an all-zero matrix and its previous state is updated to zero until it
becomes present again.
### 3.3 Multiple data points
When a frame contains two data points, `offset_ts` gives the segment boundary. Each segment uses the same linear relation between the previous and next targets; step mode switches targets at the designated slot.
`offset_ts` gives the first slot at which each data point becomes active, using the raw 5-bit value
(legal range $0\ldots23$); the values of one object must be strictly increasing. In the default mode
(`slope_idx=0`), two data points $D_0$ and $D_1$ interpolate as
$$
M_{o,c,b,t}=
\begin{cases}
(1-\alpha_t)P_{o,c,b}+\alpha_tD_{0}, & t=0\ldots10,\ \alpha_t=\dfrac{t+1}{12},\\[1mm]
D_{0}, & t=11,\\[1mm]
(1-\beta_t)D_{0}+\beta_tD_{1}, & t=12\ldots23,\ \beta_t=\dfrac{t-11}{12}.
\end{cases}
$$
Step mode (`slope_idx=1`) switches targets directly at the designated slots: for $t<o_0$ it keeps the
previous frame-end value $P_{o,c,b}$ (or $D_0$ when no previous state exists), for $o_0\le t<o_1$ it
uses $D_0$, and for $t\ge o_1$ it uses $D_1$; with a single data point it keeps $P$ for $t<o_0$ and uses
$D_0$ from $o_0$ on.
## 4. Analysis QMF for core PCM
@@ -337,15 +365,21 @@ where $r=0\ldots63$. Synthesis state must advance continuously by slot.
## 8. LFE path
LFE bypasses the object matrix and inverse QMF and uses a 1217-sample delay. After the input and output scale factors cancel:
LFE bypasses the object matrix and inverse QMF. The core LFE samples are first multiplied by the
sample-wise gain ramp $G[n]$ (the same full $G_{\mathrm{clip}}$ as section 2.4: for $n<256$,
$G[n]=G_{\mathrm{old}}+n(G_{\mathrm{new}}-G_{\mathrm{old}})/256$; for $n\ge256$, $G[n]=G_{\mathrm{new}}$;
on the first frame or after a reset, $G_{\mathrm{old}}=G_{\mathrm{new}}$). The scaled samples then
enter the 1217-sample delay, and the read-out is only clamped, with no further gain:
$$
y_{\mathrm{LFE}}[n]=
\mathrm{clip}\left(
x_{\mathrm{LFE,core}}[n-1217],-1,1
x_{\mathrm{LFE,core}}[n-1217]\cdot G[n-1217],-1,1
\right).
$$
Here $G[k]$ uses the frame-local position of input sample $k$, i.e. $k\bmod1536$.
## 9. OAMD coordinates
The lateral and longitudinal grids use $N=62$; the height grid uses $N=15$. The quantizer is
+37 -8
View File
@@ -147,13 +147,15 @@ $$
$$
G_{\mathrm{clip}}=
1+\frac{y}{32}2^{x-4}.
2^{x-4}\left(1+\frac{y}{32}\right).
$$
它并入分析端标量 $S=\frac{1}{16}G_{\mathrm{clip}}$,乘在核心 bed 的每个采样上(QMF 分析之前);
值域为 $[0.0625,15.75]$;$x=4$ 时化为 $1+y/32$。它并入分析端标量
$S=\frac{1}{16}G_{\mathrm{clip}}$,乘在核心 bed 的每个采样上(QMF 分析之前);
对象 inverse QMF 之后的 $\times16$ 抵消 $1/16$,净效果是乘 $G_{\mathrm{clip}}$,因此钳位 $[-1,1]$
处在增益**之后**。标量按帧保留两个槽,本帧前 256 个采样从上一帧值线性过渡到本帧值、之后为常数,
复位后的第一帧直接取新值。LFE 走旁通通路,不参与该标量。
复位后的第一帧直接取新值。LFE 走独立通路,但使用同一个完整 $G_{\mathrm{clip}}$ 的逐点斜坡:
斜坡在进入 1217-sample 延迟之前乘到 LFE 采样上,见 §8。
## 3. 参数带展开与时间插值
@@ -188,7 +190,14 @@ $$
### 3.2 单数据点插值
令上一帧末值为 $P_{o,c,b}$,当前目标值为 $D_{o,c,p(b)}$。对时槽 $t=0\ldots23$:
令上一帧末值为 $P_{o,c,b}$,当前目标值为 $D_{o,c,p(b)}$。对象没有 previous 状态时
(整条流第一帧或对象首次出现),24 个时槽直接取当前目标(硬切,不从零淡入):
$$
M_{o,c,b,t}=D_{o,c,p(b)},\qquad t=0\ldots23.
$$
有 previous 状态时,对时槽 $t=0\ldots23$:
$$
\alpha_t=\frac{t+1}{24},
@@ -206,11 +215,26 @@ $$
M_{o,c,b,23}=D_{o,c,p(b)}.
$$
该值随后成为下一帧的 previous 状态。
该值随后成为下一帧的 previous 状态。对象 present 由 1 变 0 后,从该帧起继续按全零矩阵发布,
并把 previous 更新为零矩阵,直到它重新 present=1。
### 3.3 多数据点
当一帧含两个数据点时,`offset_ts` 给出分段边界。每一段在上一目标和下一目标之间使用相同的线性关系;阶跃模式则在指定时槽直接切换目标。
`offset_ts` 给出各数据点开始生效的时隙序号,取原始 5 bit 值(合法范围 $0\ldots23$);
同一对象的数据点必须严格递增。默认模式(`slope_idx=0`)下,两个数据点 $D_0,D_1$ 的插值为
$$
M_{o,c,b,t}=
\begin{cases}
(1-\alpha_t)P_{o,c,b}+\alpha_tD_{0}, & t=0\ldots10,\ \alpha_t=\dfrac{t+1}{12},\\[1mm]
D_{0}, & t=11,\\[1mm]
(1-\beta_t)D_{0}+\beta_tD_{1}, & t=12\ldots23,\ \beta_t=\dfrac{t-11}{12}.
\end{cases}
$$
阶跃模式(`slope_idx=1`)在指定时槽直接切换目标:$t<o_0$ 保持上一帧末值 $P_{o,c,b}$
(没有 previous 时取 $D_0$),$o_0\le t<o_1$ 取 $D_0$,$t\ge o_1$ 取 $D_1$;
单数据点时 $t<o_0$ 保持 $P$、$t\ge o_0$ 取 $D_0$。
## 4. 核心 PCM 的 analysis QMF
@@ -334,15 +358,20 @@ $$
## 8. LFE 路径
LFE 不经过对象矩阵或 inverse QMF,而是使用 1217-sample 延迟。输入与输出端的比例因子抵消后:
LFE 不经过对象矩阵或 inverse QMF。核心 LFE 先乘逐点增益斜坡 $G[n]$(与 §2.4 同一个完整
$G_{\mathrm{clip}}$:$n<256$ 时 $G[n]=G_{\mathrm{old}}+n(G_{\mathrm{new}}-G_{\mathrm{old}})/256$,
$n\ge256$ 时 $G[n]=G_{\mathrm{new}}$,首帧或复位后 $G_{\mathrm{old}}=G_{\mathrm{new}}$),
再进入 1217-sample 延迟;读出后只做钳位,不再乘任何增益:
$$
y_{\mathrm{LFE}}[n]=
\mathrm{clip}\left(
x_{\mathrm{LFE,core}}[n-1217],-1,1
x_{\mathrm{LFE,core}}[n-1217]\cdot G[n-1217],-1,1
\right).
$$
其中 $G[k]$ 取输入采样 $k$ 所在帧内的位置,即 $k\bmod1536$。
## 9. OAMD 坐标
横向和纵向网格使用 $N=62$,高度网格使用 $N=15$。量化函数为
+1 -1
View File
@@ -115,7 +115,7 @@ def _parse_at(data, start_bit):
raise EmdfError(f"同一 EMDF 容器重复 payload id {payload_id}")
has_sample_offset = bool(reader.read(1))
sample_offset = (reader.read(12) >> 1) if has_sample_offset else 0
sample_offset = variable_bits(reader, 11) if has_sample_offset else 0
if reader.read(1):
variable_bits(reader, 11) # duration
if reader.read(1):
+70 -25
View File
@@ -79,54 +79,96 @@ def get_huff_code(mode, typ, nch):
else "joc_huff_code_fine_generic"] # MTX
def parse_joc_header(payload):
def _tail_info(out, br, payload):
"""记录 joc_data 结束位置与载荷尾部(剩余位数、尾字节)。"""
total_bits = len(payload) * 8
out["data_end_bits"] = br.p
out["remaining_bits"] = total_bits - br.p
out["tail_bytes"] = payload[br.p // 8:]
return total_bits
def parse_joc_header(payload, strict=True):
"""只读 joc_header 的 3+6+3 bit,不做 Huffman 解码。
跨层一致性校验(规格 §11.2)只需要这些字段,因此不必为校验再走一遍完整解析。
配置不受支持时严格模式抛异常;宽松模式返回 ``status = 2``,由调用方按整帧无效处理。
"""
br = BR(payload)
out = {
"dmx_config_idx": br.bits(3),
"num_objects_bits": br.bits(6),
"ext_config_idx": br.bits(3),
"status": 0,
}
out["n_channels"] = JOC_NUM_CHANNELS.get(out["dmx_config_idx"])
if out["n_channels"] is None:
raise ValueError(f"未知的 JOC downmix 配置 {out['dmx_config_idx']}")
# 声明对象数超过 15 时截断到 15;截断本身另行记入 diagnostics。
# 核心声道数决定对象语法的展开方式,配置未知时无法继续解析。
if strict:
raise ValueError(f"未知的 JOC downmix 配置 {out['dmx_config_idx']}")
out["status"] = 2
out["objects_clamped"] = False
out["n_objects"] = 0
return out
# 声明对象数超过 15 时截断到 15;截断记入 diagnostics,整帧状态记为 3。
declared = out["num_objects_bits"] + 1
out["objects_clamped"] = declared > 15
out["n_objects"] = min(declared, 15)
if out["objects_clamped"]:
out["status"] = 3
return out
def parse_joc(payload):
"""解析 id14 载荷(joc() 位流)。返回字段 dict + 解析后剩余位数。"""
"""解析 id14 载荷(joc() 位流),返回字段 dict 与整帧状态 ``status``。
status:0 正常;2 downmix 配置不受支持;3 对象数超过 15 被截断;
4 参数带索引越界;5 offset_ts 非法;6 ext_config_idx=0 时对齐后仍有多余数据。
"""
br = BR(payload)
out = parse_joc_header(payload)
n_objects, n_channels = out["n_objects"], out["n_channels"]
out = parse_joc_header(payload, strict=False)
br.p = 12
out["clipgain_x_bits"] = br.bits(3)
out["clipgain_y_bits"] = br.bits(5)
out["seq_count_bits"] = br.bits(10)
# clipgain = 1 + (y/32)·2^(x−4),值域为 [1, 8.75]。
out["clipgain"] = 1 + out["clipgain_y_bits"] / 32.0 * 2 ** (out["clipgain_x_bits"] - 4)
# clipgain = 2^(x−4)·(1 + y/32),值域为 [0.0625, 15.75]。
out["clipgain"] = 2 ** (out["clipgain_x_bits"] - 4) * (
1 + out["clipgain_y_bits"] / 32.0)
objs = []
for obj in range(n_objects):
o = {}
o["present"] = br.bits(1)
if o["present"]:
o["num_bands_idx"] = br.bits(3)
o["n_bands"] = JOC_NUM_BANDS[o["num_bands_idx"]]
o["sparse"] = br.bits(1)
o["quant_idx"] = br.bits(1)
o["slope_idx"] = br.bits(1)
o["num_dpoints_bits"] = br.bits(1)
o["n_dpoints"] = o["num_dpoints_bits"] + 1
if o["slope_idx"] == 1:
o["offset_ts"] = [br.bits(5) + 1 for _ in range(o["n_dpoints"])]
objs.append(o)
out["objs"] = objs
n_channels = out["n_channels"]
if n_channels is None:
# 核心声道数未知时对象语法无法展开,只保留头部字段与状态。
_tail_info(out, br, payload)
return out
for obj in range(out["n_objects"]):
o = {"present": br.bits(1)}
objs.append(o)
if not o["present"]:
continue
o["num_bands_idx"] = br.bits(3)
n_bands = JOC_NUM_BANDS.get(o["num_bands_idx"])
if n_bands is None:
# 参数带数未知时无法确定后续符号数量,停止解析。
if out["status"] == 0:
out["status"] = 4
_tail_info(out, br, payload)
return out
o["n_bands"] = n_bands
o["sparse"] = br.bits(1)
o["quant_idx"] = br.bits(1)
o["slope_idx"] = br.bits(1)
o["num_dpoints_bits"] = br.bits(1)
o["n_dpoints"] = o["num_dpoints_bits"] + 1
if o["slope_idx"] == 1:
offsets = [br.bits(5) for _ in range(o["n_dpoints"])]
o["offset_ts"] = offsets
invalid = any(offset >= 24 for offset in offsets)
if not invalid and o["n_dpoints"] > 1:
invalid = any(second <= first for first, second
in zip(offsets, offsets[1:]))
if invalid and out["status"] == 0:
out["status"] = 5
# joc_data(Huffman):dense 逐声道逐带读 MTX;sparse 读 IDX 后读 VEC。
for obj, o in enumerate(objs):
if not o["present"]:
@@ -150,9 +192,12 @@ def parse_joc(payload):
o["mtx"].append(mtx)
o["channel_idx"].append(None)
o["vec"].append(None)
out["data_end_bits"] = br.p
out["remaining_bits"] = len(payload) * 8 - br.p
out["tail_bytes"] = payload[br.p // 8:]
total_bits = _tail_info(out, br, payload)
if out["status"] == 0:
# 尾部先补 0 到 8 bit 对齐,补出来的位不是数据;对齐后仍有字节即多余。
pad = (8 - (br.p % 8)) % 8
if out["ext_config_idx"] == 0 and total_bits - br.p - pad > 0:
out["status"] = 6
return out
+16 -9
View File
@@ -16,12 +16,12 @@ QMF5_WINDOW = np.asarray(_TABLES["qmf5_window"], dtype=np.float64)
# 子带到参数带的映射表。
_TABLE39 = {
23: "0 1 2 3 4 5 6 7 8 9 10 11 12 12 13 13 14 14 15 15 16 16 16 17 17 17 18 18 18 18 19 19 19 19 19 20 20 20 20 20 20 21 21 21 21 21 21 21 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22",
15: "0 1 2 3 4 5 6 7 8 9 9 10 10 11 11 11 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14",
15: "0 1 2 3 4 5 6 7 8 9 9 10 10 10 11 11 11 11 12 12 12 12 12 13 13 13 13 13 13 13 13 13 13 13 13 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14",
12: "0 1 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 8 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11",
9: "0 1 2 3 3 3 4 5 5 6 6 6 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8",
9: "0 1 2 3 3 4 4 5 5 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8",
7: "0 1 2 2 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6",
5: "0 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4",
3: "0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2",
5: "0 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4",
3: "0 0 0 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2",
1: "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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",
}
_PB_MAP = {k: np.fromstring(v, dtype=np.int64, sep=" ") for k, v in _TABLE39.items()}
@@ -34,13 +34,19 @@ def sb_to_pb_table(n_bands):
raise ValueError(f"不支持的 JOC 参数带数: {n_bands}") from exc
def interp_matrix(obj_info, dq, prev, n_ts=24):
"""按数据点和跨帧状态插值,返回 ``[channel,subband,timeslot]``。"""
def interp_matrix(obj_info, dq, prev=None, n_ts=24):
"""按数据点和跨帧状态插值,返回 ``[channel,subband,timeslot]``。
``prev`` 是上一帧末时槽的矩阵;``prev`` 为 None 表示该对象还没有历史,
此时所有时槽直接使用本帧第一个数据点(硬切,不从零淡入)。
"""
dq_sb = np.asarray(dq, dtype=np.float64)[:, :, sb_to_pb_table(dq.shape[2])]
previous = np.asarray(prev, dtype=np.float64)
previous = None if prev is None else np.asarray(prev, dtype=np.float64)
n_dp = obj_info["n_dpoints"]
slope = obj_info["slope_idx"]
if slope == 0:
if previous is None:
return np.repeat(dq_sb[0, :, :, None], n_ts, axis=2)
if n_dp == 1:
alpha = (np.arange(n_ts, dtype=np.float64) + 1.0) / n_ts
return previous[:, :, None] * (1.0 - alpha) + dq_sb[0, :, :, None] * alpha
@@ -53,9 +59,10 @@ def interp_matrix(obj_info, dq, prev, n_ts=24):
ts = np.arange(n_ts)
offsets = obj_info.get("offset_ts", [])
base = dq_sb[0, :, :, None] if previous is None else previous[:, :, None]
if n_dp == 1:
return np.where(ts[None, None, :] < offsets[0], previous[:, :, None], dq_sb[0, :, :, None])
out = np.where(ts[None, None, :] < offsets[0], previous[:, :, None], dq_sb[0, :, :, None])
return np.where(ts[None, None, :] < offsets[0], base, dq_sb[0, :, :, None])
out = np.where(ts[None, None, :] < offsets[0], base, dq_sb[0, :, :, None])
return np.where(ts[None, None, :] < offsets[1], out, dq_sb[1, :, :, None])
+283 -71
View File
@@ -1,12 +1,14 @@
"""把 E-AC-3 核心 PCM 和 JOC 元数据重建为 LFE 加 15 路对象 PCM。
管线(每帧):
核心 PCM(N=5 或 7 路全频段通道,按 k 查表)→ QMF 分析 x → 填充器 z = Σ m·x(每对象)
核心 PCM(N=5 或 7 路全频段通道,按 k 查表)→ QMF 分析 x → 对象矩阵 z = Σ m·x
→ 对象时域组装(前步、64 点 FFT、旋转、合成窗)→ ×16 + clamp
→ 对象波形 × joc_clipgain
+ LFE 专用 1217-sample 环形延迟
→ LFE 专用 1217-sample 环形延迟(逐点乘 clipgain 斜坡)
→ 16ch(ch0 = LFE, ch1-15 = 15 对象)→ ×output_scale
跨帧状态:seq 状态机(state/expected/无效帧计数)、每对象的 prev 矩阵与存在标记、
QMF 分析与合成状态、LFE 延迟环、直通渲染的核心 PCM 历史。
output_scale:
默认 1.0(0 dB)。用户选择的 dB 在命令行转换为 float32 系数;该系数
不进入 JOC 数学本体,也不是 joc_clipgain。
@@ -19,29 +21,55 @@ from joc_qmf import (N, QMF5_WINDOW, qmf_analysis_frame,
from evo_unpack import unpack_evolution
CORE_CHANNELS = [0, 1, 2, 4, 5] # L R C Ls Rs(EAC3 5.1 核心顺序)
FRAME_SAMPLES = 1536
MAX_OBJECTS = 15
MAX_CORE_CHANNELS = 7
LFE_DELAY = 1217
SEQ_MODULUS = 1024
# 没有 JOC 元数据的帧:状态机短路分支使用这个状态码。
STATUS_NO_METADATA = 8
# 渲染层给出的“核心声道数变化”状态码。
STATUS_CORE_CHANNEL_CHANGE = 9
def next_seq(value):
"""seq 的推进规则:1023 之后回到 1。"""
return 1 if value == SEQ_MODULUS - 1 else value + 1
class JocRenderer:
"""E-AC-3 JOC 帧渲染器。状态跨帧保持(FIFO/插值 prev/合成状态)。"""
"""E-AC-3 JOC 帧渲染器。状态跨帧保持(FIFO/插值 prev/合成状态/状态机)。"""
def __init__(self, output_scale=1.0):
# 成品增益明确按 float32 运算;默认值为 1.0。
self.output_scale = np.float32(output_scale)
self._prev = {} # 每对象插值 prev(m 的帧末 sb 值)
self._synthesis_state = np.zeros((15, 640), dtype=np.float64)
# 每对象插值 prev:上一帧发布的末时槽矩阵与“prev 存在”标记;槽位固定 15 个。
self._prev_mtx = np.zeros((MAX_OBJECTS, MAX_CORE_CHANNELS, N), dtype=np.float64)
self._prev_present = np.zeros(MAX_OBJECTS, dtype=bool)
self._synthesis_state = np.zeros((MAX_OBJECTS, 640), dtype=np.float64)
# 分析 QMF:最多七路,各保留 9×64 FIFO。L/R/C 与 N=7 时的第 6、7 路
# (额外那一对)在这里做 10 时隙延迟;Ls/Rs 的同一延迟在环绕通路内完成。
self._analysis_fifo = np.zeros((7, 9, 64), dtype=np.float64)
self._analysis_fifo = np.zeros((MAX_CORE_CHANNELS, 9, 64), dtype=np.float64)
self._analysis_delay = np.zeros((5, 10, 64), dtype=np.float32)
# 哨兵:复位后(或整条流的第一帧)直接用新标量,不做帧内过渡。
self._analysis_phase = np.float32(-0.5)
self._surround_qmf_delay = np.zeros((2, 10, 64), dtype=np.complex128)
self._surround_dc_hist = np.zeros((2, 20), dtype=np.complex128)
# ch0/LFE 使用循环历史,读指针恒落后写指针 1217 个采样。
# phase=1/16 与输出端 *16 抵消,净效果是 LFE 延迟。
self._lfe_delay = np.zeros(1217, dtype=np.float64)
self._last_x = None # 仅供逐槽诊断读取,不参与状态推进
self._rot = None # 每带旋转表
# 逐点增益斜坡在写入延迟环之前乘上,读出后不再乘任何增益。
self._lfe_delay = np.zeros(LFE_DELAY, dtype=np.float64)
self._lfe_clipgain = None # LFE 增益斜坡的起点;None = 还没有历史
# 直通渲染的核心 PCM 延迟历史:保存上一帧输入。
self._core_hist = np.zeros((MAX_CORE_CHANNELS, FRAME_SAMPLES), dtype=np.float64)
# seq 状态机与逐帧分派所需的跨帧状态。
self._state = 0
self._expected = 0
self._invalid_count = 0
self._last_frame_valid = False
self._n_channels_prev = None
self._config_idx = 3
self._last_x = None # 仅供逐槽诊断读取,不参与状态推进
self._rot = None # 每带旋转表
def held_analysis_scalar(self):
"""当前分析端标量;还没有过 JOC 帧时用结构常数 1/16。"""
@@ -50,21 +78,27 @@ class JocRenderer:
return np.float32(0.0625)
return value
def held_clipgain(self):
"""当前生效的完整 clipgain;没有历史时为 1.0。"""
return float(self.held_analysis_scalar()) * 16.0
@staticmethod
def decode_payload(payload_bytes):
"""id14 载荷 → (parse 结果, mix_q, mix_dq)。"""
subs, _ = unpack_evolution(payload_bytes, loose=True)
out = parse_joc(subs[14])
mix_q = diff_decode(out)
mix_dq = dequantize(out, mix_q)
return out, mix_q, mix_dq
return JocRenderer.decode_subpayloads(subs)
@staticmethod
def decode_subpayloads(subs):
"""已解析 EMDF 子载荷 → (parse 结果, mix_q, mix_dq)。"""
"""已解析 EMDF 子载荷 → (parse 结果, mix_q, mix_dq)。
整帧状态不是 0 时只返回解析结果:这一帧按无效帧处理,不做矩阵还原。
"""
if 14 not in subs:
raise ValueError("EMDF 缺少 ID14/JOC")
out = parse_joc(subs[14])
if out["status"] != 0:
return out, {}, {}
mix_q = diff_decode(out)
mix_dq = dequantize(out, mix_q)
return out, mix_q, mix_dq
@@ -80,13 +114,13 @@ class JocRenderer:
"""
pcm = np.asarray(bed_pcm, dtype=np.float32)
n_channels = pcm.shape[0] if pcm.ndim == 2 else 0
if n_channels not in (5, 7) or pcm.shape != (n_channels, 1536):
if n_channels not in (5, 7) or pcm.shape != (n_channels, FRAME_SAMPLES):
raise ValueError(f"核心帧应为 (5,1536) 或 (7,1536),实际 {pcm.shape}")
new_phase = np.float32(phase_new)
old_phase = self._analysis_phase
if old_phase <= 0.0 and old_phase != 0.0: # 哨兵:首帧/复位后不做过过渡
old_phase = new_phase
gains = np.full(1536, new_phase, dtype=np.float32)
gains = np.full(FRAME_SAMPLES, new_phase, dtype=np.float32)
if old_phase != new_phase:
step = np.float32((new_phase - old_phase) / np.float32(256.0))
gains[:256] = old_phase + np.arange(256, dtype=np.float32) * step
@@ -110,24 +144,112 @@ class JocRenderer:
apply_dc_filter=apply_dc_filter, rotate=rotate)
return x
def _clear_unused_channels(self, n_channels):
"""核心声道数变化后,只清除新布局中未使用声道的历史状态。"""
if n_channels >= MAX_CORE_CHANNELS:
return
self._analysis_fifo[n_channels:] = 0.0
self._prev_mtx[:, n_channels:, :] = 0.0
self._core_hist[n_channels:] = 0.0
if n_channels == 5:
# N=5 不用额外那一对,10 时隙延迟线 3、4 号槽不再推进。
self._analysis_delay[3:] = 0.0
def _advance_state(self, status, seq):
"""按 seq 推进状态机,返回进入本帧前的 ``state``。
status:0 正常;2..6 解析层无效;8 无元数据;9 核心声道数变化。
"""
pre_state = self._state
valid = status in (0, STATUS_CORE_CHANNEL_CHANGE)
if status == STATUS_NO_METADATA and pre_state != 0:
# 无元数据帧不推进期望、不比较 seq。
self._state = 3
return pre_state
self._expected = next_seq(self._expected)
if valid and seq == 0 and pre_state != 0:
# seq 回到 0:节目重新开始,硬切并清空对象历史。
self._state = 4
return pre_state
state = self._state
if state == 0:
if valid:
state = 1
self._expected = seq
elif state == 1:
if valid:
if seq != self._expected:
state = 4
else:
state = 2
self._invalid_count = 1
elif state == 2:
if valid:
state = 1
self._expected = seq
elif self._invalid_count == 5:
state = 3
else:
self._invalid_count += 1
elif state == 3:
if valid:
state = 1
self._expected = seq
else:
self._invalid_count += 1
elif state == 4:
if valid:
state = 1
self._expected = seq
else:
state = 2
self._invalid_count = 1
self._state = state
if status == STATUS_CORE_CHANNEL_CHANGE:
self._state = 4
self._last_frame_valid = valid
return pre_state
def object_z(self, out, mix_dq, x):
"""计算 ``z[obj] = Σ_ch m[ch,sb,ts]·x[ch,sb,ts]``。"""
"""计算恢复正常渲染时的 ``z[obj] = Σ_ch m[ch,sb,ts]·x[ch,sb,ts]``。
对象槽固定 15 个:本帧声明范围内 present=1 的对象按插值规则生成矩阵;
present 由 1 变 0(或落在声明范围之外)的对象按“present=1 且矩阵全零”
继续发布,直到它重新 present=1;从未出现过的槽输出零。
"""
n_channels = x.shape[0]
objs = out["objs"]
z_all = {}
new_prev = {}
for obj, o in enumerate(out["objs"]):
if not o["present"]:
continue
dq = mix_dq[obj]
prev = self._prev.get(obj)
if prev is None:
prev = np.zeros((out["n_channels"], N), dtype=np.float64)
m = interp_matrix(o, dq, prev) # [ch][sb][ts](内部按 o["n_bands"] 映射)
z = np.sum(x * m, axis=0) # [sb][ts](复)
z_all[obj] = z
new_prev[obj] = m[:, :, -1]
self._prev.update(new_prev)
for obj in range(MAX_OBJECTS):
present = obj < len(objs) and objs[obj]["present"]
if present:
prev = self._prev_mtx[obj][:n_channels] if self._prev_present[obj] else None
mtx = interp_matrix(objs[obj], mix_dq[obj], prev)
z_all[obj] = np.sum(x * mtx, axis=0)
self._prev_mtx[obj][:n_channels] = mtx[:, :, -1]
self._prev_present[obj] = True
elif self._prev_present[obj]:
z_all[obj] = np.zeros((N, 24), dtype=np.complex128)
self._prev_mtx[obj][:n_channels] = 0.0
self._prev_present[obj] = True
else:
z_all[obj] = np.zeros((N, 24), dtype=np.complex128)
return z_all
def _hold_objects(self, x):
"""保持渲染:24 个时隙全部使用上一帧发布的矩阵,不更新 prev。"""
n_channels = x.shape[0]
z_all = {}
for obj in range(MAX_OBJECTS):
z_all[obj] = np.sum(x * self._prev_mtx[obj][:n_channels][:, :, None], axis=0)
return z_all
@staticmethod
def _zero_objects():
return {obj: np.zeros((N, 24), dtype=np.complex128)
for obj in range(MAX_OBJECTS)}
def _rot_table_86840(self):
"""生成 ``θ=πk/128`` 的 ``0.5·(sin θ, cos θ)`` 旋转表。"""
if self._rot is None:
@@ -218,7 +340,7 @@ class JocRenderer:
z = np.stack([z_all[obj] for obj in object_ids], axis=0)
state = self._synthesis_state[object_ids].copy()
rot868 = self._rot_table_86840()
pcm = np.zeros((len(object_ids), 1536), dtype=np.float64)
pcm = np.zeros((len(object_ids), FRAME_SAMPLES), dtype=np.float64)
for tsg in range(6):
ring = np.zeros((len(object_ids), 512), dtype=np.float64)
chunk = z[:, :, tsg * 4:tsg * 4 + 4].transpose(0, 2, 1)
@@ -242,39 +364,134 @@ class JocRenderer:
"""返回逆 QMF 使用的 640 项有效合成窗表。"""
return QMF5_WINDOW
def decode_lfe(self, lfe_pcm):
"""将核心 ch3/LFE 经过跨帧 1217-sample 延迟后输出到 ch0。"""
def decode_lfe(self, lfe_pcm, clipgain=None):
"""核心 ch3/LFE 乘逐点增益斜坡,再经跨帧 1217-sample 延迟输出到 ch0。
clipgain 为本帧完整增益 G_new;None 表示本帧没有新值(沿用当前值)。
斜坡:n<256 时 G_old + n·(G_new−G_old)/256,n≥256 时 G_new;
首帧(没有历史)时 G_old = G_new。
"""
current = np.asarray(lfe_pcm, dtype=np.float64)
if current.shape != (1536,):
if current.shape != (FRAME_SAMPLES,):
raise ValueError(f"LFE 帧应为 (1536,),实际 {current.shape}")
delayed = np.concatenate([self._lfe_delay, current])
out = np.clip(delayed[:1536], -1.0, 1.0)
self._lfe_delay = delayed[-1217:].copy()
new_gain = self.held_clipgain() if clipgain is None else float(clipgain)
old_gain = new_gain if self._lfe_clipgain is None else self._lfe_clipgain
gains = np.full(FRAME_SAMPLES, new_gain, dtype=np.float64)
if old_gain != new_gain:
gains[:256] = old_gain + np.arange(256, dtype=np.float64) * (
(new_gain - old_gain) / 256.0)
self._lfe_clipgain = new_gain
delayed = np.concatenate([self._lfe_delay, current * gains])
out = np.clip(delayed[:FRAME_SAMPLES], -1.0, 1.0)
self._lfe_delay = delayed[-LFE_DELAY:].copy()
return out
def render_missing_frame(self, bed_pcm, lfe_pcm=None, config_idx=3):
"""没有 JOC 元数据的音频帧:对象通道静音,只保留 LFE 旁通。
@staticmethod
def _validate_bed(bed_pcm):
pcm = np.asarray(bed_pcm, dtype=np.float32)
n_channels = pcm.shape[0] if pcm.ndim == 2 else 0
if n_channels not in (5, 7) or pcm.shape != (n_channels, FRAME_SAMPLES):
raise ValueError(f"核心帧应为 (5,1536) 或 (7,1536),实际 {pcm.shape}")
return pcm
QMF 分析与对象合成状态照常推进,否则后续帧的时间轴会整体少一格。
"""
sigma = config_idx in SIGMA_DMX_CONFIGS
# 本帧没有 JOC 元数据,分析端标量保持上一帧的值(槽位不被改写)。
x = self.qmf_x(bed_pcm, phase_new=self.held_analysis_scalar(),
apply_dc_filter=sigma, rotate=sigma)
@staticmethod
def _delayed_core(hist, current):
"""直通渲染:把核心 PCM 延迟 1217 个采样(只处理实际有的声道数)。"""
n_channels = current.shape[0]
out = np.zeros((MAX_CORE_CHANNELS, FRAME_SAMPLES), dtype=np.float64)
out[:n_channels, :LFE_DELAY] = hist[:n_channels, 319:]
out[:n_channels, LFE_DELAY:] = current[:, :FRAME_SAMPLES - LFE_DELAY]
return out[:n_channels]
def _render_frame(self, *, status, seq, out, mix_dq, bed_pcm, lfe_pcm,
config_idx=None):
"""每帧执行一次状态机并按结果分派渲染。"""
pcm = self._validate_bed(bed_pcm)
n_channels = pcm.shape[0]
if self._n_channels_prev is not None and n_channels != self._n_channels_prev:
self._clear_unused_channels(n_channels)
pre_state = self._advance_state(status, seq)
state = self._state
if status == STATUS_NO_METADATA:
mode = "hold" if pre_state == 1 else "passthrough"
elif state == 4:
mode = "hard_cut"
elif state in (2, 3):
mode = "hold"
elif state == 1 and status == 0:
mode = "normal"
else:
mode = "passthrough"
if out is not None and out.get("n_channels") is not None:
config = out["dmx_config_idx"]
self._config_idx = config
elif config_idx is not None:
config = config_idx
self._config_idx = config
else:
config = self._config_idx
sigma = config in SIGMA_DMX_CONFIGS
valid_metadata = (out is not None and out.get("status") == 0
and out.get("n_channels") is not None)
if mode in ("normal", "hard_cut") and valid_metadata:
clipgain = float(out["clipgain"])
phase_new = 0.0625 * clipgain
else:
clipgain = self.held_clipgain()
phase_new = self.held_analysis_scalar()
lfe = (np.zeros(FRAME_SAMPLES, dtype=np.float64)
if lfe_pcm is None else np.asarray(lfe_pcm, dtype=np.float64))
if lfe.shape != (FRAME_SAMPLES,):
raise ValueError(f"LFE 帧应为 (1536,),实际 {lfe.shape}")
x = self.qmf_x(pcm, phase_new=phase_new, apply_dc_filter=sigma, rotate=sigma)
self._last_x = x.copy()
z_all = {obj: np.zeros((x.shape[1], x.shape[2]), dtype=np.complex128)
for obj in range(15)}
self.dll_synth_objects(z_all)
pcm16 = np.zeros((16, 1536), dtype=np.float64)
if lfe_pcm is not None:
pcm16[0] = self.decode_lfe(lfe_pcm)
pcm16 = np.zeros((MAX_OBJECTS + 1, FRAME_SAMPLES), dtype=np.float64)
if mode == "normal":
z_all = self.object_z(out, mix_dq, x)
objs_pcm = self.dll_synth_objects(z_all)
pcm16[0] = self.decode_lfe(lfe, clipgain)
for obj, pcm_obj in objs_pcm.items():
pcm16[obj + 1] = pcm_obj
elif mode == "hold":
z_all = self._hold_objects(x)
objs_pcm = self.dll_synth_objects(z_all)
pcm16[0] = self.decode_lfe(lfe, clipgain)
for obj, pcm_obj in objs_pcm.items():
pcm16[obj + 1] = pcm_obj
elif mode == "hard_cut":
# 全部对象矩阵清零,保留“prev 存在”标记,下一有效帧从 0 淡入。
z_all = self._zero_objects()
self.dll_synth_objects(z_all)
self.decode_lfe(lfe, clipgain) # 环照常写入并推进写游标
self._prev_present[:] = True
self._prev_mtx[:] = 0.0
else: # passthrough
z_all = self._zero_objects()
self.dll_synth_objects(z_all)
self.decode_lfe(lfe, clipgain)
pcm16[:n_channels] = self._delayed_core(self._core_hist, pcm)
self._core_hist[:n_channels] = pcm
self._n_channels_prev = n_channels
pcm16_f32 = np.asarray(pcm16, dtype=np.float32)
return np.multiply(pcm16_f32, self.output_scale, dtype=np.float32), z_all
def render_missing_frame(self, bed_pcm, lfe_pcm=None, config_idx=3):
"""没有 JOC 元数据的音频帧:按状态机结果保持渲染或直通核心 PCM。"""
return self._render_frame(status=STATUS_NO_METADATA, seq=None,
out=None, mix_dq=None,
bed_pcm=bed_pcm, lfe_pcm=lfe_pcm,
config_idx=config_idx)
def render_frame(self, payload_bytes, bed_pcm, lfe_pcm=None):
"""payload_bytes = evolution 载荷(含 id14 JOC);bed_pcm = N×1536 核心 PCM。
lfe_pcm = 1536 核心 LFE;提供时生成 ch0,省略时 ch0 静音
(保留给只验证对象/z 链的诊断脚本)。
返回 (pcm16, z_all):pcm16 = 16×1536(ch0 LFE + 15 对象)f32,
已用 FP32 系数乘 output_scale。"""
subs, _ = unpack_evolution(payload_bytes, loose=True)
@@ -283,19 +500,14 @@ class JocRenderer:
def render_subpayloads(self, subs, bed_pcm, lfe_pcm=None):
"""以已拆出的 EMDF payload 字典渲染一帧,避免绑定 transport 容器。"""
out, mix_q, mix_dq = self.decode_subpayloads(subs)
# σ(k) 同时门控 -j 旋转与 band-0 FIR(规格 §4)。
sigma = out["dmx_config_idx"] in SIGMA_DMX_CONFIGS
x = self.qmf_x(bed_pcm, phase_new=0.0625 * out["clipgain"],
apply_dc_filter=sigma, rotate=sigma)
self._last_x = x.copy()
z_all = self.object_z(out, mix_dq, x)
# joc_clipgain 已在分析端进入 qmf_x,与用户 output_scale 分离。
objs_pcm = self.dll_synth_objects(z_all)
pcm16 = np.zeros((16, 1536), dtype=np.float64)
pcm16[0] = self.decode_lfe(lfe_pcm) if lfe_pcm is not None else 0.0
for obj, p in objs_pcm.items():
pcm16[obj + 1] = p
pcm16_f32 = np.asarray(pcm16, dtype=np.float32)
return np.multiply(pcm16_f32, self.output_scale, dtype=np.float32), z_all
pcm = self._validate_bed(bed_pcm)
status = out["status"]
if (status == 0 and out["n_channels"] is not None
and self._n_channels_prev is not None
and out["n_channels"] != self._n_channels_prev):
status = STATUS_CORE_CHANNEL_CHANGE
config_idx = (out["dmx_config_idx"] if out["n_channels"] is not None else None)
return self._render_frame(status=status, seq=out.get("seq_count_bits"),
out=out, mix_dq=mix_dq,
bed_pcm=pcm, lfe_pcm=lfe_pcm,
config_idx=config_idx)