2 Commits
Author SHA1 Message Date
TheM14 8424e6bdfb Apply the OAMD trim and front-back balance in speaker rendering
Select the trim configuration that applies to the output layout from its
surround and height speaker counts:

    cfg = kappa_f(surrounds) + 3 * kappa_h(heights)

Trim and balance apply to seven layouts: 2.0, 3.1, 4.0, 5.1, 5.1.2, 5.1.4
and 7.1.  Wider layouts are left untrimmed.

An explicit configuration contributes a static level for the centre,
surround and height groups, which multiplies the panning gains per output
channel.  Automatic and disabled configurations contribute no static
level: the attenuation an automatic configuration describes is already
applied per object by the layout position compensation.

An explicit configuration also contributes the two front-back balance
values.  Writing n = round(16 |b|) and s = n / 18, each balance
reparameterises the OAMD front-back axis in [0, 1]:

    y <= 0.05 or y >= 0.95        -> y
    y < 0.5,  b > 0               -> (1 + s) y - 0.05 s
    y >= 0.5, b > 0               -> (1 - s) (y - 0.5) + 0.5 + 0.45 s
    y < 0.5,  b < 0               -> (1 - s) y + 0.05 s
    y >= 0.5, b < 0               -> (1 + s) (y - 0.5) + 0.5 - 0.45 s

The map is monotonic onto [0.05, 0.95], keeps both walls fixed, and needs
no clamping.  Height blends the listener-plane and overhead values before
panning; the horizontal and vertical coordinates are untouched.

Warp is not read here: it selects a downmix mode and does not enter the
speaker path.
2026-11-02 17:18:53 +08:00
TheM14 220e791ce9 Carry the OAMD trim element into the ADM BWF metadata
Parse the trim element: warp_mode, global_trim_mode, the nine
per-configuration trim_balance_presence records, and the per-object
bypass flags.  Select the configuration that applies to a layout from
its surround and height speaker counts:

    cfg = kappa_f(surrounds) + 3 * kappa_h(heights)

    kappa_f: 0 -> 0, 2..3 -> 1, otherwise 2
    kappa_h: 0 -> 0, 2 -> 1, otherwise 2

Write the configuration to both carriers:

  * AXML gets a DolbyAtmosMasterFile container holding
    DolbyMasterContentInfo, DolbyTrimModeSetup (one records entry per
    configuration with centerTrim, surroundTrim, heightTrim and the two
    balance fields) and DolbyWarpMode.
  * Segment 10 of the dbmd chunk gets the nine 15-byte records: bit 0
    is the automatic flag, and an explicit record carries three float32
    trims followed by two sign-magnitude balance bytes.

Configurations left at default keep the automatic flag and carry no
value elements.
2026-11-02 17:00:24 +08:00
11 changed files with 686 additions and 23 deletions

No files matched your search

+117 -3
View File
@@ -519,7 +519,119 @@ G_{\mathrm{pos}}
\mathbf G_{\mathrm{point}}.
$$
## 12. OAMD time alignment and gain ramps
## 12. OAMD trim and front-back balance
### 12.1 Configuration selection
The OAMD trim element splits a programme's calibration data into nine
configurations, chosen by the target layout's additional horizontal speaker
count $N_f$ and height speaker count $N_h$:
$$
\kappa_f(N)=
\begin{cases}
0, & N=0,\\
1, & N\in\{2,3\},\\
2, & \text{otherwise},
\end{cases}
\qquad
\kappa_h(N)=
\begin{cases}
0, & N=0,\\
1, & N=2,\\
2, & \text{otherwise}.
\end{cases}
$$
$$
c_{\mathrm{trim}}=\kappa_f(N_f)+3\,\kappa_h(N_h).
$$
The nine configurations enumerate "no / some / many additional horizontal
speakers by no / some / many height speakers".
Trim and front-back balance apply to seven layouts only: 2.0, 3.1, 4.0, 5.1,
5.1.2, 5.1.4 and 7.1. Wider layouts (7.1.2, 7.1.4, 9.1.4, 9.1.6) are left
untrimmed, with $G_c=1$ and $b=0$.
### 12.2 Group levels
Each configuration supplies one level $T_g$ in dB for the centre, surround and
height groups. Only an explicit configuration contributes a static level:
$$
T_g=
\begin{cases}
\text{coded value}, & \text{explicit},\
0, & \text{disabled},\
0, & \text{automatic}.
\end{cases}
$$
An automatic configuration carries no static group gain: the attenuation it
describes is already applied per object by the position compensation of §11,
and multiplying it in a second time would count it twice.
The static gain of channel $c$ is
$$
G_c=10^{T_{g(c)}/20},
$$
where $g(c)$ follows the channel label: centre maps to centre, side, rear and
wide map to surround, top maps to height, and left, right and LFE stay at
$G_c=1$.
### 12.3 Front-back balance
Each configuration also carries two front-back balance values:
$b_{\mathrm{lis}}$ for the listener plane and $b_{\mathrm{tb}}$ for the overhead
layer. Both are quantised to a $1/16$ grid and are non-zero only in an explicit
configuration.
Writing $n=\mathrm{round}(16\,|b|)$ and $s=n/18$, the single-layer map is
$$
B_b(y)=
\begin{cases}
y, & y\le 0.05\ \text{or}\ y\ge 0.95,\\[2pt]
(1+s)y-0.05s, & 0.05<y<0.5,\ b>0,\\[2pt]
(1-s)(y-0.5)+0.5+0.45s, & 0.5\le y<0.95,\ b>0,\\[2pt]
(1-s)y+0.05s, & 0.05<y<0.5,\ b<0,\\[2pt]
(1+s)(y-0.5)+0.5-0.45s, & 0.5\le y<0.95,\ b<0.
\end{cases}
$$
It maps $[0.05,0.95]$ monotonically onto itself: the two walls stay fixed, the
two halves meet at $y=0.5$, and their slopes are $1+s$ and $1-s$. The 5 percent
wall bands and the case $b=0$ pass through unchanged, so the map never leaves
the interval and needs no extra clamp.
The height $w$ blends the two layers:
$$
y'=(1-w)\,B_{b_{\mathrm{lis}}}(y)+w\,B_{b_{\mathrm{tb}}}(y).
$$
The front-back axis $y$ is oriented as in §9, with $0$ at the front wall and
$1$ at the back wall; $x$ and $z$ are unchanged.
### 12.4 Where it is applied
The balance acts on the second component of the object position before
panning:
$$
y_o\longmapsto y_o',
$$
while the group gain multiplies the panning result, that is $g_{o,c}$ in the
mix formula of §14 becomes $G_c\,g_{o,c}$.
Warp takes no part in either step: it selects a downmix mode, affects only the
downmix path, and leaves object coordinates untouched.
## 13. OAMD time alignment and gain ramps
The coded position of an OAMD update is
@@ -575,7 +687,7 @@ $$
If no new metadata update intervenes, this is equivalent to a sample-wise linear ramp of total length $KB$.
## 13. Final speaker mix
## 14. Final speaker mix
For target output channel $c$:
@@ -607,9 +719,11 @@ y_{24}[n]=
\right).
$$
## 14. Scope of the formulas
## 15. Scope of the formulas
- The JOC matrix section covers both the dense MTX and the sparse IDX/VEC differential syntax.
- The speaker-panning section describes ordinary point objects; extent, spread, divergence, and similar modes require additional models.
- Multiple OAMD position blocks must be scheduled in time order.
- 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;
binaural rendering reads neither of them.
+104 -3
View File
@@ -516,7 +516,107 @@ G_{\mathrm{pos}}
\mathbf G_{\mathrm{point}}.
$$
## 12. OAMD 时间对齐与增益斜坡
## 12. OAMD trim 与前后平衡
### 12.1 配置选择
OAMD trim element 把节目的校准数据分成九个配置,由目标布局的附加水平扬声器数
$N_f$ 与高度扬声器数 $N_h$ 选定:
$$
\kappa_f(N)=
\begin{cases}
0, & N=0,\\
1, & N\in\{2,3\},\\
2, & \text{其他},
\end{cases}
\qquad
\kappa_h(N)=
\begin{cases}
0, & N=0,\\
1, & N=2,\\
2, & \text{其他}.
\end{cases}
$$
$$
c_{\mathrm{trim}}=\kappa_f(N_f)+3\,\kappa_h(N_h).
$$
九个配置依次为“no / some / many 附加水平 × no / some / many 高度”。
trim 与前后平衡只在七种布局下施加:2.0、3.1、4.0、5.1、5.1.2、5.1.4、7.1。
更宽的布局(7.1.2、7.1.4、9.1.4、9.1.6)不施加,取 $G_c=1$、$b=0$。
### 12.2 组电平
每个配置给 centre、surround、height 三个组各一个电平 $T_g$(dB)。只有显式配置
施加静态电平:
$$
T_g=
\begin{cases}
\text{记录值}, & \text{显式},\
0, & \text{禁用},\
0, & \text{自动}.
\end{cases}
$$
自动配置不带静态组增益:它描述的衰减量已由 §11 的位置补偿按对象逐个施加,再乘
一次会重复计算。
声道 $c$ 的静态增益为
$$
G_c=10^{T_{g(c)}/20},
$$
$g(c)$ 由声道标签决定:中置归 centre,侧、后、宽归 surround,顶部归 height,左右
与 LFE 不参与,取 $G_c=1$。
### 12.3 前后平衡
每个配置还携带两个前后平衡量:$b_{\mathrm{lis}}$ 作用于听音平面,
$b_{\mathrm{tb}}$ 作用于顶上层层。二者按 $1/16$ 网格量化,只在显式配置下非零。
记 $n=\mathrm{round}(16\,|b|)$、$s=n/18$,单层映射为
$$
B_b(y)=
\begin{cases}
y, & y\le 0.05\ \text{或}\ y\ge 0.95,\\[2pt]
(1+s)y-0.05s, & 0.05<y<0.5,\ b>0,\\[2pt]
(1-s)(y-0.5)+0.5+0.45s, & 0.5\le y<0.95,\ b>0,\\[2pt]
(1-s)y+0.05s, & 0.05<y<0.5,\ b<0,\\[2pt]
(1+s)(y-0.5)+0.5-0.45s, & 0.5\le y<0.95,\ b<0.
\end{cases}
$$
它把 $[0.05,0.95]$ 单调映到自身:两端不动,$y=0.5$ 处两段取值相同,斜率分别为
$1+s$ 与 $1-s$。首尾各 $5\%$ 的墙区以及 $b=0$ 的情形都原样通过,因此映射不会
越界,也无需额外钳位。
高度 $w$ 把两层混合起来:
$$
y'=(1-w)\,B_{b_{\mathrm{lis}}}(y)+w\,B_{b_{\mathrm{tb}}}(y).
$$
前后轴 $y$ 的取向见 §9,$0$ 为前、$1$ 为后;$x$ 与 $z$ 不变。
### 12.4 施加位置
前后平衡在声像之前作用于对象坐标的第二个分量:
$$
y_o\longmapsto y_o',
$$
组增益则乘在声像结果上,即 §14 混音式中的 $g_{o,c}$ 替换为 $G_c\,g_{o,c}$。
warp 不参与上述任何一步:它是下混模式的选择位,只影响下混路径,不改变对象坐标。
## 13. OAMD 时间对齐与增益斜坡
OAMD 更新的编码位置为
@@ -572,7 +672,7 @@ $$
如果中途没有新的 metadata 更新,该过程等价于总长度 $KB$ 的逐样本线性斜坡。
## 13. 最终扬声器混音
## 14. 最终扬声器混音
对目标输出声道 $c$:
@@ -604,9 +704,10 @@ y_{24}[n]=
\right).
$$
## 14. 公式适用范围
## 15. 公式适用范围
- JOC 矩阵部分同时描述 dense MTX 与 sparse IDX/VEC 两条差分语法。
- 扬声器声像部分描述普通点对象;extent、spread、divergence 等模式需要额外模型。
- 多个 OAMD position block 必须按其时间顺序调度。
- limiter 属于独立后处理,不包含在上述混音公式中。
- trim 与前后平衡只作用于扬声器渲染;双耳渲染不读取它们。
+6 -2
View File
@@ -699,8 +699,10 @@ def main(argv=None):
timings["create_binaural_renderer"] = 0.0
layout = get_speaker_layout(args.speaker_layout)
speaker_name = speaker_layout_display_name(layout)
speaker_meta = oamd_tracks.read_atmos_meta(index)
speaker_decoder, speaker_backend_info = create_speaker_renderer(
layout, bed_channels=getattr(index, "bed_channels", ()))
layout, bed_channels=getattr(index, "bed_channels", ()),
trim_meta=speaker_meta)
print(f"[speaker] layout={speaker_name} backend={speaker_backend_info['name']} "
f"channels={layout.channel_count}", flush=True)
spool = SpeakerPcmSpool(
@@ -844,9 +846,11 @@ def main(argv=None):
f"samples={binaural_clip_info['kept_samples']}")
else:
timings["create_binaural_renderer"] = 0.0
atmos_meta = oamd_tracks.read_atmos_meta(index)
master = adm_assemble.StreamingMaster(
output, duration_sec, rate=RATE,
joc_binaural_mode=adm_atmos.JOC_BINAURAL_MODES[args.binaural_mode])
joc_binaural_mode=adm_atmos.JOC_BINAURAL_MODES[args.binaural_mode],
atmos_meta=atmos_meta)
try:
render_seconds, renderer_backend, render_breakdown = timed_call(
timings, "render_and_stream", variant_call,
+6 -3
View File
@@ -71,13 +71,14 @@ class StreamingMaster:
"""
def __init__(self, out_path, duration_sec, rate=48000, block_samples=131072,
joc_binaural_mode=4):
joc_binaural_mode=4, atmos_meta=None):
if block_samples < 1536:
raise ValueError("block_samples must be at least one E-AC-3 frame")
self.out_path = os.fspath(out_path)
self.duration_sec = float(duration_sec)
self.rate = int(rate)
self.joc_binaural_mode = joc_binaural_mode
self.atmos_meta = atmos_meta
self._sink = adm_atmos.Sink25(self.out_path, 25, self.rate)
self._buffer = np.empty((int(block_samples), 25), dtype=np.float32)
self._used = 0
@@ -113,10 +114,12 @@ class StreamingMaster:
from . import adm_serializer
except ImportError:
import adm_serializer
axml = adm_serializer.build_axml(kf_tracks, self.duration_sec)
axml = adm_serializer.build_axml(kf_tracks, self.duration_sec,
atmos_meta=self.atmos_meta)
chna = adm_atmos.build_chna()
dbmd = adm_atmos.build_dbmd(
25, joc_binaural_mode=self.joc_binaural_mode)
25, joc_binaural_mode=self.joc_binaural_mode,
atmos_meta=self.atmos_meta)
trajectory_blocks = sum(len(track[1]) for track in kf_tracks)
self.metadata_info = {
"axml_bytes": len(axml),
+44 -3
View File
@@ -32,6 +32,10 @@ JOC_BINAURAL_MODES = {
JOC_BINAURAL_MODE_DEFAULT = "unspecified"
def q_to_adm_xyz(q1, q2, q3):
"""OAMD 的 q1/q2/q3 编码 → ADM 笛卡尔坐标。
posX/posY 为 62 级量化,posZ 为 15 级;ADM 的 Y 轴与 OAMD 编码方向相反。
"""
posX = min(1.0, round(q1 * 62 / 32767.0) / 62.0)
posY = min(1.0, round(q2 * 62 / 32767.0) / 62.0)
posZ = round(q3 * 15 / 32767.0) / 15.0
@@ -195,8 +199,22 @@ def _checksum(seg):
s += b
return (~s + 1) & 0xFF
def build_dbmd(object_count=25, joc_binaural_mode=4):
"""仅覆盖 segment 10 中 JOC object slots 10..24 的 mode 低 3 bit。"""
def _balance_byte(value):
"""trim 记录里的 balance 字段:bit7 为符号,bit0-6 为幅度。
bal3d_y 的取值落在 [0, 1],7 bit 幅度即该区间上的定点表示,步长 1/127。
"""
value = float(value)
magnitude = min(int(round(abs(value) * 127.0)), 127)
return magnitude | (0x80 if value < 0 else 0)
def build_dbmd(object_count=25, joc_binaural_mode=4, atmos_meta=None):
"""segment 10:9 个 trim 配置 + 逐轨 bypass + JOC object slots 10..24 的 mode。
含显式值的配置写成显式记录(3 个 float32 + 2 个 balance 字节),其余保持
自动标志。
"""
mode = operator.index(joc_binaural_mode)
if mode not in JOC_BINAURAL_MODES.values():
raise ValueError(f"invalid JOC binaural render mode: {mode}")
@@ -230,7 +248,30 @@ def build_dbmd(object_count=25, joc_binaural_mode=4):
# default is automatic, so a freshly rendered master opens with automatic
# trims in the renderer.
for layout in range(9):
ob[7 + 15 * layout] |= 1
base = 7 + 15 * layout
record = None
if atmos_meta is not None:
configs = atmos_meta.get("configs") or ()
if layout < len(configs):
record = configs[layout]
explicit = isinstance(record, dict) and not record.get("default")
if not explicit:
ob[base] |= 1 # bit0 = 1:由渲染器推导
continue
ob[base] &= 0xFE # bit0 = 0:显式
if record.get("disabled"):
trims = (0.0, 0.0, 0.0)
bal3d_y_tb = bal3d_y_lis = 0.0
else:
trims = (record.get("centre", 0.0), record.get("surround", 0.0),
record.get("height", 0.0))
bal3d_y_tb = record.get("bal3d_y_tb", 0.0)
bal3d_y_lis = record.get("bal3d_y_lis", 0.0)
for index, value in enumerate(trims):
offset = base + 1 + 4 * index
ob[offset:offset + 4] = struct.pack("<f", float(value))
ob[base + 13] = _balance_byte(bal3d_y_tb)
ob[base + 14] = _balance_byte(bal3d_y_lis)
object_modes = 4 + 2 + 1 + 9 * 15 + object_count
for i in range(10, min(object_count, 10 + N_OBJ)):
ob[object_modes + i] = (ob[object_modes + i] & 0xF8) | mode
+70 -2
View File
@@ -23,8 +23,74 @@ def ts(seconds):
def esc(v):
return (str(v).replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;"))
def build_axml(obj_tracks, duration_sec):
"""obj_tracks: [(name, [(rtime, x, y, z, dur), ...]) ×15]"""
def _fmt_float(value):
return f"{float(value):.6f}"
def _trim_records(atmos_meta):
"""把 OAMD trim element 展开成 DolbyTrimModeSetup 的 records。
``configs`` 每项有三种形态:
* ``{"default": True}`` —— 交给渲染器推导,不写任何值子元素;
* ``{"disabled": True}`` —— 该配置不施加 trim,写全 0;
* 含 centre/surround/height/bal3d_y_tb/bal3d_y_lis 的显式配置。
缺省字段一律补 0。
"""
lines = ["<DolbyTrimModeSetup>"]
for index, record in enumerate(atmos_meta.get("configs") or ()):
if record is None:
continue
lines.append("<records>")
lines.append(f"<type>{index}</type>")
if isinstance(record, dict) and not record.get("default"):
if record.get("disabled"):
values = {"centerTrim": 0.0, "surroundTrim": 0.0,
"heightTrim": 0.0, "balanceOverhead": 0.0,
"balanceListener": 0.0}
else:
values = {
"centerTrim": record.get("centre", 0.0),
"surroundTrim": record.get("surround", 0.0),
"heightTrim": record.get("height", 0.0),
"balanceOverhead": record.get("bal3d_y_tb", 0.0),
"balanceListener": record.get("bal3d_y_lis", 0.0),
}
for name, value in values.items():
lines.append(f"<{name}>{_fmt_float(value)}</{name}>")
lines.append("</records>")
lines.append("</DolbyTrimModeSetup>")
return lines
def _warp_xml_value(atmos_meta):
"""OAMD 的 warp_mode → ADM ``DolbyWarpMode`` 取值。
warp_mode 选择下混/渲染模式,四档依次为 Direct render、Direct render
with room balance、Dolby Pro Logic IIx、Standard (Lo/Ro),ADM 沿用同一
编号,因此直接照搬码流值。
"""
return max(0, min(3, int(atmos_meta.get("warp_mode", 0))))
def _atmos_master_block(atmos_meta):
"""Dolby 扩展容器:内容信息 + trim 记录 + warp。"""
lines = ["<DolbyAtmosMasterFile>"]
warp = _warp_xml_value(atmos_meta)
lines.append("<DolbyMasterContentInfo>")
lines.append(f"<render_warp_mode>{warp}</render_warp_mode>")
lines.append("</DolbyMasterContentInfo>")
lines.extend(_trim_records(atmos_meta))
lines.append(f"<DolbyWarpMode>{warp}</DolbyWarpMode>")
lines.append("</DolbyAtmosMasterFile>")
return lines
def build_axml(obj_tracks, duration_sec, atmos_meta=None):
"""obj_tracks: [(name, [(rtime, x, y, z, dur), ...]) ×15]
``atmos_meta`` 为 ``oamd_bits.parse_trim`` 的结果;给出时把 trim 与 warp
写进 ADM。
"""
w = []
a = w.append
a('<?xml version="1.0" encoding="utf-8"?>')
@@ -131,6 +197,8 @@ def build_axml(obj_tracks, duration_sec):
a(f'<audioPackFormatIDRef>AP_0003{0x1001 + i:04x}</audioPackFormatIDRef>')
a(f'<audioTrackFormatIDRef>AT_0003{0x1001 + i:04x}_01</audioTrackFormatIDRef>')
a('</audioStreamFormat>')
if atmos_meta:
w.extend(_atmos_master_block(atmos_meta))
a('</audioFormatExtended></format></coreMetadata>')
a('</ebuCoreMain>')
return ''.join(w).encode('utf-8')
+125 -1
View File
@@ -18,6 +18,13 @@ RAMP_DURATION_INDEX = (
1024, 1600, 1601, 1602, 1920, 2000, 2002, 2048,
)
OBJECT_ELEMENT_ID = 1
TRIM_ELEMENT_ID = 2
# Table 12 of ETSI TS 103 420:trim 电平表,4 bit 索引,单位 dB。
# 索引 15 的 -36 dB 是最小值(并非静音)。
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)
# bal3d_y 的编码为 (code + 1) / 16,幅度落在 [1/16, 1],取不到 0。
BALANCE_STEP = 16
# 5.6.0.11 Table 11b:ISF 类型 → 对象数。
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
@@ -371,6 +378,118 @@ def _parse_object_element(reader, element, raw_payload, retain_spatial=False):
}
def _parse_trim_element(reader, object_count, raw_payload):
"""``trim_element``:全局 trim/warp 配置,每个 payload 出现一次。
该 element 描述整段节目的渲染校准意图,不随 block 变化,因此解析结果应当
在 payload 之间保持,而不是逐帧重算。
"""
trim = {
"warp_mode": reader.read(2),
"reserved": reader.read(2),
"global_trim_mode": reader.read(2),
"configs": [None] * 9,
"disable_per_obj": None,
}
# global_trim_mode == 2 才携带 9 个配置的显式数据;其余取值表示内容
# 把 trim 交给渲染器自行推导(auto)。
if trim["global_trim_mode"] == 2:
for config in range(9):
if reader.read(1): # b_default_trim
trim["configs"][config] = {"default": True}
continue
if reader.read(1): # b_disable_trim
trim["configs"][config] = {"disabled": True}
continue
present = reader.read(5)
entry = {"present": present}
if present & 0x01:
entry["centre"] = TRIM_LUT[reader.read(4)]
if present & 0x02:
entry["surround"] = TRIM_LUT[reader.read(4)]
if present & 0x04:
entry["height"] = TRIM_LUT[reader.read(4)]
if present & 0x08:
sign = 1.0 if reader.read(1) else -1.0
entry["bal3d_y_tb"] = (
(reader.read(4) + 1) / BALANCE_STEP * sign)
if present & 0x10:
sign = 1.0 if reader.read(1) else -1.0
entry["bal3d_y_lis"] = (
(reader.read(4) + 1) / BALANCE_STEP * sign)
trim["configs"][config] = entry
if reader.read(1): # b_disable_trim_per_obj
trim["disable_per_obj"] = [bool(reader.read(1))
for _ in range(object_count)]
return trim
def parse_trim(bits_one):
"""独立解析一个 OAMD payload 的 trim element。
返回 ``None`` 表示该 payload 不含 trim element。异常统一转成
``UnsupportedVariantError``,与其它解析入口一致。
"""
bits, raw_payload = _payload_bits(bits_one)
try:
version = bits[0] << 1 | bits[1]
except IndexError:
raise UnsupportedVariantError(
"oamd", "header_truncated", "OAMD payload 不足以容纳 header",
details={"payload_bits": len(bits),
"payload": bytes_descriptor(raw_payload)}) from None
reader = _BitReader(bits, 2)
try:
if version == 3:
version += reader.read(3)
object_count_bits = reader.read(5)
if object_count_bits == 0x1F:
object_count_bits += reader.read(7)
object_count = object_count_bits + 1
program = _parse_program_assignment(reader, raw_payload)
except UnsupportedVariantError:
raise
except ValueError as exc:
raise _syntax_error(
"header_truncated", "OAMD header 字段越界或不完整",
raw_payload, exc, payload_bits=len(bits)) from exc
alternate_present = bool(reader.read(1))
try:
element_count = reader.read(4)
except ValueError as exc:
raise _syntax_error(
"element_count_truncated", "OAMD element 数量字段不完整",
raw_payload, exc) from exc
for ordinal in range(element_count):
header_start = reader.position
try:
element_id = reader.read(4)
size_bytes = _variable_bits_max(reader, 4, 4) + 1
except ValueError as exc:
raise _syntax_error(
"element_header_truncated", "OAMD element header 不完整",
raw_payload, exc,
element_ordinal=ordinal, header_start_bit=header_start) from exc
region_start = reader.position
region_end = region_start + size_bytes * 8
if region_end > reader.limit:
raise UnsupportedVariantError(
"oamd", "element_bounds",
"OAMD element 声明长度超过 payload 边界",
details={"element_ordinal": ordinal, "element_id": element_id,
"size_bytes": size_bytes,
"payload": bytes_descriptor(raw_payload)})
if alternate_present:
reader.read(4)
reader.read(1)
if element_id == TRIM_ELEMENT_ID:
trim = _parse_trim_element(reader, object_count, raw_payload)
trim["object_count"] = object_count
return trim
reader.position = region_end
return None
def _parse_elements(reader, alternate_present, object_count, program, raw_payload, retain_spatial=False):
"""5.5.4 ``oa_element_md()`` 目录;对象 element 按结构解析,其余按声明长度跳过。"""
try:
@@ -453,8 +572,13 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
element["object_element"] = object_element
# 个别编码器声明的 oa_element_size 比实际内容短;以结构解析结果为准。
reader.position = max(region_end, reader.position)
elif element_id == TRIM_ELEMENT_ID:
element["trim_element"] = _parse_trim_element(
reader, object_count, raw_payload)
element["parsed_end_bit"] = reader.position
reader.position = max(region_end, reader.position)
else:
# trim / extended / 未知 element:按声明长度整体跳过(5.5.4)。
# extended / 未知 element:按声明长度整体跳过(5.5.4)。
element["parsed_end_bit"] = region_end
reader.position = region_end
elements.append(element)
+18 -1
View File
@@ -3,7 +3,7 @@ import math
from adm_atmos import q_to_adm_xyz
from bed_layout import bed_object_positions
from oamd_bits import JocFieldState, frame_update
from oamd_bits import JocFieldState, frame_update, parse_trim
from variant_error import UnsupportedVariantError
# 元数据更新时刻按渲染器处理块对齐,与 Dolby 的 processing_block_size 及
@@ -192,6 +192,23 @@ def _expand_events(events, total_samples, rate, update_block_samples,
object_delay_samples, object_index)
raise ValueError(f"未知 trajectory_mode: {trajectory_mode}")
def read_atmos_meta(index, frames=None, max_scan=64):
"""从 OAMD payload 读取 trim/warp 配置。
trim element 描述整段节目的校准意图,跨帧恒定,因此扫到第一帧含 trim 的
payload 即可。返回 ``None`` 表示该节目没有 trim element。
"""
rows = index.rows if frames is None else frames
for row in rows[:max_scan]:
subs = index.subpayloads(row)
if not subs or 11 not in subs:
continue
meta = parse_trim(subs[11])
if meta is not None:
return meta
return None
def build_adm_tracks(index, frames=None, rate=48000, frame_samples=1536,
update_block_samples=METADATA_BLOCK_SAMPLES,
object_delay_samples=1473, trajectory_mode="compact"):
+4 -2
View File
@@ -11,10 +11,12 @@ from speaker_renderer import PythonSpeakerRenderer, payload_events_from_index, r
FRAME_SAMPLES = 1536
def create_speaker_renderer(layout: str | SpeakerLayout, bed_channels=None):
def create_speaker_renderer(layout: str | SpeakerLayout, bed_channels=None,
trim_meta=None):
"""Create a stateful speaker renderer and return ``(renderer, info)``."""
target = get_speaker_layout(layout) if isinstance(layout, str) else layout
renderer = PythonSpeakerRenderer(target, bed_channels=bed_channels)
renderer = PythonSpeakerRenderer(target, bed_channels=bed_channels,
trim_meta=trim_meta)
return renderer, {
"name": "python",
"layout": target.name,
+11 -3
View File
@@ -14,6 +14,7 @@ import numpy as np
from oamd_bits import JocFieldState, frame_update
from speaker_layouts import RegionGeometry, SpeakerLayout, get_speaker_layout
from trim_balance import apply_balance, balance_layers, channel_gains
Q15_SCALE = 32768.0
Q15_MAX = 32767.0 / Q15_SCALE
@@ -34,8 +35,9 @@ def bed_object_targets(bed_channels) -> dict[int, tuple[float, float, float]]:
def _all_object_targets(layout: SpeakerLayout, state: JocFieldState,
bed_targets=None) -> np.ndarray:
bed_targets=None, balance=(0.0, 0.0)) -> np.ndarray:
bed_targets = bed_targets or {}
listener, overhead = balance
result = np.zeros((15, layout.channel_count), dtype=np.float64)
for object_index in range(1, 16):
fixed = bed_targets.get(object_index)
@@ -45,6 +47,7 @@ def _all_object_targets(layout: SpeakerLayout, state: JocFieldState,
u = state.q[(object_index, "q1")] / Q15_SCALE
v = state.q[(object_index, "q2")] / Q15_SCALE
w = state.q[(object_index, "q3")] / Q15_SCALE
v = apply_balance(v, w, listener, overhead)
result[object_index - 1] = render_point_gains(
layout, u, v, w, standard_order=False)
return result
@@ -210,12 +213,15 @@ class PythonSpeakerRenderer:
"""Stateful float64 speaker renderer."""
def __init__(self, layout: str | SpeakerLayout, block_size: int = 32,
bed_channels=None):
bed_channels=None, trim_meta=None):
self.layout = get_speaker_layout(layout) if isinstance(layout, str) else layout
self.block_size = int(block_size)
if self.block_size <= 0:
raise ValueError("block_size must be positive")
self.bed_targets = bed_object_targets(bed_channels)
self.trim_gains = np.asarray(channel_gains(trim_meta, self.layout),
dtype=np.float64)
self.balance = balance_layers(trim_meta, self.layout)
self.reset()
def reset(self):
@@ -230,7 +236,8 @@ class PythonSpeakerRenderer:
def _apply_payload(self, payload):
update = frame_update(payload)
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.bed_targets,
self.balance)
blocks = ramp_block_count(update["ramp_duration_samples"], self.block_size)
difference = new_target - self._current
significant = np.abs(difference) >= GAIN_SNAP_THRESHOLD
@@ -299,6 +306,7 @@ class PythonSpeakerRenderer:
for payload in events_by_block.get(total_blocks, ()):
self._apply_payload(payload)
output *= self.trim_gains[None, :]
if standard_order:
return output[:, list(self.layout.internal_to_standard)]
return output
+181
View File
@@ -0,0 +1,181 @@
"""Programme trim and front-back balance taken from the OAMD trim element.
TS 103 420 splits a programme's trim data into nine configurations, selected
by a layout's surround and height speaker counts:
cfg = kappa_f(surrounds) + 3 * kappa_h(heights)
kappa_f(n) = 0 for n == 0, 1 for n in (2, 3), 2 otherwise
kappa_h(n) = 0 for n == 0, 1 for n == 2, 2 otherwise
Each configuration holds one unit of calibration:
* a level in dB for the centre, surround and height speaker groups, and
* a front-back balance for the listener plane and for the overhead layer.
Only the layouts listed in ``SUPPORTED_LAYOUTS`` carry these semantics.
Configurations left at default contribute neither a static level nor a balance:
the automatic attenuation is already applied per object by the layout position
compensation. Configurations declared as disabled contribute nothing either.
The balance reparameterises the front-back axis; it does not move objects off
the axis and it leaves the two walls untouched. See ``front_back``.
"""
from __future__ import annotations
# Layouts that carry trim and front-back balance semantics, by their compact
# names: 2.0, 3.1, 4.0, 5.1, 5.1.2, 5.1.4 and 7.1. Wider layouts leave the
# programme untrimmed.
SUPPORTED_LAYOUTS = ("20", "31", "40", "51", "512", "514", "71")
# Channel label -> trim group. Left, right and LFE carry no trim in any
# configuration, so they are absent from the table.
GROUPS = {
"C": "centre",
"Ls": "surround", "Rs": "surround",
"Lrs": "surround", "Rrs": "surround",
"Ltf": "height", "Rtf": "height",
"Ltr": "height", "Rtr": "height",
"Ltm": "height", "Rtm": "height",
}
FLOOR_LABELS = ("Ls", "Rs", "Lrs", "Rrs")
HEIGHT_LABELS = ("Ltf", "Rtf", "Ltr", "Rtr", "Ltm", "Rtm")
# bal3d_y is coded in steps of 1/16, so a balance is quantised to that grid.
BALANCE_STEPS = 16
# Front-back balance keeps a 5 percent wall band at each end and leaves it unmapped.
WALL = 0.05
def _height_class(count):
if count == 0:
return 0
if count == 2:
return 1
return 2
def _floor_class(count):
if count == 0:
return 0
if count in (2, 3):
return 1
return 2
def layout_classes(layout):
"""Return ``(config_index, height_count, floor_count)`` for a layout."""
channels = tuple(getattr(layout, "channels", ()))
heights = sum(1 for c in channels if c in HEIGHT_LABELS)
floor = sum(1 for c in channels if c in FLOOR_LABELS)
return _floor_class(floor) + 3 * _height_class(heights), heights, floor
def _supported(layout):
return getattr(layout, "name", None) in SUPPORTED_LAYOUTS
def _record(atmos_meta, layout):
if not atmos_meta or not _supported(layout):
return None
index, _, _ = layout_classes(layout)
configs = atmos_meta.get("configs") or ()
if index >= len(configs):
return None
return configs[index]
def group_levels_db(atmos_meta, layout):
"""Trim level in dB for the centre, surround and height speaker groups.
Only an explicit configuration contributes a static level. An automatic
configuration carries no static gain: the attenuation it describes is
already applied per object by the layout position compensation, and
multiplying it in a second time would count it twice.
"""
record = _record(atmos_meta, layout)
if not isinstance(record, dict) or record.get("default") or record.get("disabled"):
return {"centre": 0.0, "surround": 0.0, "height": 0.0}
return {
"centre": float(record.get("centre", 0.0)),
"surround": float(record.get("surround", 0.0)),
"height": float(record.get("height", 0.0)),
}
def channel_gains(atmos_meta, layout):
"""Per-channel linear gain for the layout, in channel order."""
levels = group_levels_db(atmos_meta, layout)
gains = []
for label in getattr(layout, "channels", ()):
group = GROUPS.get(label)
db = levels[group] if group else 0.0
gains.append(10.0 ** (db / 20.0))
return gains
def balance_layers(atmos_meta, layout):
"""Front-back balance for the listener plane and the overhead layer.
Both are returned as signed magnitudes in [-1, 1]; zero means no balance.
Only an explicit configuration carries a balance.
"""
record = _record(atmos_meta, layout)
if not isinstance(record, dict) or record.get("default") or record.get("disabled"):
return 0.0, 0.0
listener = float(record.get("bal3d_y_lis", 0.0))
overhead = float(record.get("bal3d_y_tb", 0.0))
return listener, overhead
def _curve(balance):
"""Segment coefficients for one balance value.
Returns ``(a0, a1, a2, a3)`` for the two halves of the axis, or ``None``
when the balance is zero.
Writing ``s = |n| / 18`` with ``n`` the balance quantised to the coding
grid, a positive balance stretches the front half by ``1 + s`` and
compresses the back half by ``1 - s``; a negative balance does the
reverse. Both halves anchor at the axis midpoint and at the two walls,
so the mapped value always stays inside the wall range.
"""
n = int(round(abs(balance) * BALANCE_STEPS))
if n == 0:
return None
s = n / 18.0
if balance > 0:
return 1.0 + s, -WALL * s, 1.0 - s, 0.5 + 0.45 * s
return 1.0 - s, WALL * s, 1.0 + s, 0.5 - 0.45 * s
def front_back(position, balance):
"""Reparameterise a front-back coordinate in [0, 1] by ``balance``.
``0`` is the front wall and ``1`` the back wall. Positions inside the
wall bands are returned unchanged, and so is every position when the
balance is zero.
"""
curve = _curve(balance)
if curve is None:
return position
if position <= WALL or position >= 1.0 - WALL:
return position
a0, a1, a2, a3 = curve
if position < 0.5:
return a0 * position + a1
return a2 * (position - 0.5) + a3
def apply_balance(position, height, listener, overhead):
"""Blend the two balance layers by height and map the coordinate.
``height`` runs from 0 at the listener plane to 1 at the top layer.
"""
if not listener and not overhead:
return position
low = front_back(position, listener)
high = front_back(position, overhead)
return (1.0 - height) * low + height * high