diff --git a/main.py b/main.py index d5b3d60..ccc7d7e 100644 --- a/main.py +++ b/main.py @@ -844,9 +844,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, diff --git a/src/adm_assemble.py b/src/adm_assemble.py index 3752769..331262e 100644 --- a/src/adm_assemble.py +++ b/src/adm_assemble.py @@ -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), diff --git a/src/adm_atmos.py b/src/adm_atmos.py index a9bbb0d..85021c0 100644 --- a/src/adm_atmos.py +++ b/src/adm_atmos.py @@ -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("", ">")) -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 = [""] + for index, record in enumerate(atmos_meta.get("configs") or ()): + if record is None: + continue + lines.append("") + lines.append(f"{index}") + 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)}") + lines.append("") + lines.append("") + 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 = [""] + warp = _warp_xml_value(atmos_meta) + lines.append("") + lines.append(f"{warp}") + lines.append("") + lines.extend(_trim_records(atmos_meta)) + lines.append(f"{warp}") + lines.append("") + 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('') @@ -131,6 +197,8 @@ def build_axml(obj_tracks, duration_sec): a(f'AP_0003{0x1001 + i:04x}') a(f'AT_0003{0x1001 + i:04x}_01') a('') + if atmos_meta: + w.extend(_atmos_master_block(atmos_meta)) a('') a('') return ''.join(w).encode('utf-8') diff --git a/src/oamd_bits.py b/src/oamd_bits.py index 550365f..dda79fc 100644 --- a/src/oamd_bits.py +++ b/src/oamd_bits.py @@ -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) diff --git a/src/oamd_tracks.py b/src/oamd_tracks.py index f92dd00..d2ab484 100644 --- a/src/oamd_tracks.py +++ b/src/oamd_tracks.py @@ -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"):