Add JOC downmix configurations 0..4, cross-layer checks, and drop the native core
This commit is contained in:
1 parent
428772eb87
commit
a1853f2dc5
37 files changed
+926
-6420
No files matched your search
+278
-26
@@ -6,26 +6,218 @@ import json
|
||||
from pathlib import Path
|
||||
|
||||
from adm_atmos import q_to_adm_xyz
|
||||
from bed_layout import EXTRA_PAIR_BY_CONFIG, extra_pair
|
||||
from evo_unpack import unpack_evolution
|
||||
from emdf import EmdfError, find_joc_emdf, iter_eac3_frames, parse_container
|
||||
from joc_decode import parse_joc
|
||||
from oamd_bits import JocFieldState, frame_update_values
|
||||
from emdf import (ACMOD_LOCATIONS, EmdfError, audio_frame_groups, expand_chanmap,
|
||||
find_joc_emdf, iter_syncframes, parse_container)
|
||||
from joc_decode import parse_joc, parse_joc_header
|
||||
from oamd_bits import JocFieldState, frame_update_values, program_bed_channels
|
||||
from variant_error import UnsupportedVariantError, bytes_descriptor
|
||||
|
||||
|
||||
# 规格 §11.2 校验 2:OAMD 标准 bed 掩码(Table 12)里的额外对标签。
|
||||
_BED_PAIR_GROUPS = (
|
||||
(frozenset(("RC_LB", "RC_RB")), ("Lrs", "Rrs")),
|
||||
(frozenset(("RC_TFL", "RC_TFR")), ("Vhl", "Vhr")),
|
||||
(frozenset(("RC_TBL", "RC_TBR")), ("Lts", "Rts")),
|
||||
)
|
||||
|
||||
|
||||
def _joc_pair_score(container):
|
||||
"""这个容器作为本帧 JOC 元数据的可信度(规格 §8.2 / Table 56)。
|
||||
|
||||
§8.2:OAMD 与 JOC 载荷放在同一个 EMDF 容器里;Table 56:两者的 groupid
|
||||
必须相同。同一个同步帧里可能出现多个含 ID11/ID14 的顶层容器,其中不构成一对的那个
|
||||
(ID14 声明的对象数超出其字节所能容纳的范围、ID11 未声明 groupid)既不是一对
|
||||
合法载荷,也不可能是本帧的 JOC 参数。
|
||||
|
||||
2 = 两个载荷 groupid 相同且 ID14 完整解析;1 = ID14 完整解析;0 = 其余。
|
||||
"""
|
||||
payload = container.payloads.get(14)
|
||||
if payload is None or 11 not in container.payloads:
|
||||
return 0
|
||||
parses = True
|
||||
try:
|
||||
parse_joc(payload)
|
||||
except (IndexError, ValueError):
|
||||
parses = False
|
||||
group_ids = getattr(container, "group_ids", None) or {}
|
||||
first = group_ids.get(11)
|
||||
second = group_ids.get(14)
|
||||
associated = first is not None and second is not None and first == second
|
||||
if associated and parses:
|
||||
return 2
|
||||
return 1 if parses else 0
|
||||
|
||||
|
||||
def _transport_record(group):
|
||||
"""从一个音频帧的同步帧组提取规格 §11.2 需要的 L2 声明。"""
|
||||
core_locations = ()
|
||||
core_lfe = False
|
||||
chanmap = None
|
||||
for sync in group:
|
||||
if sync.codec == "ac3" or sync.strmtyp == 0:
|
||||
core_locations = ACMOD_LOCATIONS.get(sync.acmod, ())
|
||||
core_lfe = bool(sync.lfeon)
|
||||
elif sync.chanmap is not None:
|
||||
chanmap = sync.chanmap
|
||||
return {
|
||||
"core_locations": tuple(core_locations),
|
||||
"core_lfe": core_lfe,
|
||||
"chanmap": chanmap,
|
||||
"syncframes": [
|
||||
{
|
||||
"offset": sync.offset,
|
||||
"size": sync.size,
|
||||
"codec": sync.codec,
|
||||
"strmtyp": sync.strmtyp,
|
||||
"substreamid": sync.substreamid,
|
||||
"acmod": sync.acmod,
|
||||
"lfeon": sync.lfeon,
|
||||
}
|
||||
for sync in group
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _declared_bed_pairs(bed_channels):
|
||||
"""标准 bed 掩码里声明的"额外那一对",按 Table 12 的标签判断。"""
|
||||
labels = {name for instance in bed_channels for name in instance}
|
||||
pairs = []
|
||||
for members, pair in _BED_PAIR_GROUPS:
|
||||
if members.issubset(labels):
|
||||
pairs.append(pair)
|
||||
return pairs
|
||||
|
||||
|
||||
def check_cross_layer(parsed, *, frame, transport=None, bed_channels=None,
|
||||
diagnostics=None):
|
||||
"""规格 §11.2 的跨层一致性校验。
|
||||
|
||||
校验 1/2(额外那对的身份)不一致时只记 diagnostics:通道身份不进 DSP
|
||||
(§10.1),床映射按 chanmap 给出的通道码做,音频与 k 一致时逐样本相同。
|
||||
校验 3(核心通道数)与 joc_ext_config_idx 会改变 DSP 输入,仍按变体报错。
|
||||
"""
|
||||
config_idx = parsed["dmx_config_idx"]
|
||||
n_channels = parsed["n_channels"]
|
||||
expected_pair = EXTRA_PAIR_BY_CONFIG.get(config_idx)
|
||||
if parsed["ext_config_idx"] != 0:
|
||||
raise UnsupportedVariantError(
|
||||
"joc", "ext_config_idx_unsupported",
|
||||
"joc_ext_config_idx 取到当前未映射的值",
|
||||
frame=frame,
|
||||
details={
|
||||
"downmix_config": config_idx,
|
||||
"extension_config_idx": parsed["ext_config_idx"],
|
||||
"payload": bytes_descriptor(parsed.get("payload", b"")),
|
||||
})
|
||||
if diagnostics is not None and parsed.get("objects_clamped"):
|
||||
diagnostics.append({
|
||||
"stage": "joc",
|
||||
"variant": "num_objects_clamped",
|
||||
"frame": frame,
|
||||
"message": "对象数超过 15,已截断",
|
||||
"details": {"declared": parsed["num_objects_bits"] + 1,
|
||||
"used": parsed["n_objects"]},
|
||||
})
|
||||
|
||||
if transport is not None:
|
||||
chanmap = transport["chanmap"]
|
||||
core_locations = transport["core_locations"]
|
||||
details = {
|
||||
"config_idx": config_idx,
|
||||
"n_channels": n_channels,
|
||||
"syncframes": transport["syncframes"],
|
||||
}
|
||||
if n_channels == 7:
|
||||
if not core_locations or chanmap is None:
|
||||
raise UnsupportedVariantError(
|
||||
"eac3", "core_extension_pair",
|
||||
"7 通道配置需要独立子流与依赖子流 chanmap 同时在场",
|
||||
frame=frame, details=details)
|
||||
pair = extra_pair(core_locations, chanmap)
|
||||
details["chanmap"] = chanmap
|
||||
details["chanmap_locations"] = expand_chanmap(chanmap)
|
||||
details["extra_pair"] = list(pair)
|
||||
if pair != expected_pair:
|
||||
if diagnostics is not None:
|
||||
diagnostics.append({
|
||||
"stage": "eac3",
|
||||
"variant": "chanmap_cfg_mismatch",
|
||||
"frame": frame,
|
||||
"message": "依赖子流 chanmap 声明的额外对与 joc_dmx_config_idx 不一致,"
|
||||
"按 chanmap 的通道码映射,音频不受影响",
|
||||
"details": {**details, "expected_pair": list(expected_pair or ())},
|
||||
})
|
||||
elif chanmap is not None and extra_pair(core_locations, chanmap):
|
||||
raise UnsupportedVariantError(
|
||||
"eac3", "core_channel_count_mismatch",
|
||||
"5 通道配置的依赖子流声明了额外通道",
|
||||
frame=frame,
|
||||
details={**details, "chanmap_locations": expand_chanmap(chanmap)})
|
||||
# 校验 3:核心通道数(子流声明汇总,去掉 LFE)
|
||||
declared = set(core_locations)
|
||||
if chanmap is not None:
|
||||
declared |= set(expand_chanmap(chanmap))
|
||||
declared -= {"LFE", "LFE2"}
|
||||
details["declared_full_band_channels"] = len(declared)
|
||||
if len(declared) != n_channels:
|
||||
raise UnsupportedVariantError(
|
||||
"eac3", "core_channel_count_mismatch",
|
||||
"子流声明的全频段通道数与 joc_dmx_config_idx 不一致",
|
||||
frame=frame, details=details)
|
||||
|
||||
if bed_channels is not None:
|
||||
declared_pairs = _declared_bed_pairs(bed_channels)
|
||||
if declared_pairs and (expected_pair is None or
|
||||
any(pair != expected_pair for pair in declared_pairs)):
|
||||
if diagnostics is not None:
|
||||
diagnostics.append({
|
||||
"stage": "oamd",
|
||||
"variant": "bed_mask_cfg_mismatch",
|
||||
"frame": frame,
|
||||
"message": "OAMD 标准 bed 掩码声明的额外对与 joc_dmx_config_idx 不一致,"
|
||||
"只影响 bed 槽位记录,音频不受影响",
|
||||
"details": {
|
||||
"config_idx": config_idx,
|
||||
"bed_channels": [list(instance) for instance in bed_channels],
|
||||
"declared_pairs": [list(pair) for pair in declared_pairs],
|
||||
"expected_pair": list(expected_pair or ()),
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
class DirectPayloadIndex:
|
||||
"""One-pass in-memory view of contiguous EMDF containers in an E-AC-3 file.
|
||||
|
||||
The optional cache directory keeps the existing ``frames.csv + emdf/``
|
||||
contract, but normal processing reuses the containers already parsed during
|
||||
the scan instead of reading and parsing thousands of small files again.
|
||||
|
||||
音频帧 = 同一 (编码, strmtyp, substreamid) 组合出现一次为止的同步帧组;
|
||||
JOC 通常只在依赖子流里,核心子流(AC-3 或 E-AC-3 独立子流)没有 EMDF。
|
||||
"""
|
||||
|
||||
def __init__(self, rows, subpayloads, sample_offsets, directory=None):
|
||||
def __init__(self, rows, subpayloads, sample_offsets, directory=None,
|
||||
transports=None, diagnostics=None, config_idx=None, n_channels=None):
|
||||
self.rows = rows
|
||||
self._subpayloads = subpayloads
|
||||
self._sample_offsets = sample_offsets
|
||||
self.directory = Path(directory) if directory is not None else None
|
||||
self.transports = transports if transports is not None else []
|
||||
self.diagnostics = diagnostics if diagnostics is not None else []
|
||||
self.config_idx = config_idx
|
||||
self.n_channels = n_channels
|
||||
|
||||
def transport(self, row):
|
||||
return self.transports[int(row["frame"])] if self.transports else None
|
||||
|
||||
def subpayloads(self, row):
|
||||
return self._subpayloads[int(row["frame"])]
|
||||
|
||||
def subpayload_sample_offset(self, row, payload_id):
|
||||
offsets = self._sample_offsets[int(row["frame"])]
|
||||
return 0 if offsets is None else int(offsets.get(payload_id, 0))
|
||||
|
||||
@classmethod
|
||||
def from_eac3(cls, eac3_path, max_frames=None, cache_dir=None):
|
||||
@@ -34,24 +226,68 @@ class DirectPayloadIndex:
|
||||
if cache_dir is not None:
|
||||
emdf_dir = cache_dir / "emdf"
|
||||
emdf_dir.mkdir(parents=True, exist_ok=True)
|
||||
data = Path(eac3_path).read_bytes()
|
||||
rows = []
|
||||
payloads = []
|
||||
offsets = []
|
||||
frames = iter_eac3_frames(Path(eac3_path).read_bytes())
|
||||
for frame_number, frame in enumerate(frames):
|
||||
transports = []
|
||||
diagnostics = []
|
||||
config_idx = None
|
||||
n_channels = None
|
||||
missing_joc_frames = 0
|
||||
groups = audio_frame_groups(iter_syncframes(data))
|
||||
for frame_number, group in enumerate(groups):
|
||||
if max_frames is not None and frame_number >= max_frames:
|
||||
break
|
||||
try:
|
||||
container = find_joc_emdf(frame)
|
||||
except UnsupportedVariantError as exc:
|
||||
exc.add_context(frame=frame_number, details={"syncframe_bytes": len(frame)})
|
||||
raise
|
||||
except EmdfError as exc:
|
||||
raise UnsupportedVariantError(
|
||||
"emdf_transport", "container_syntax",
|
||||
"EMDF 容器语法无法解析",
|
||||
frame=frame_number,
|
||||
details={"syncframe_bytes": len(frame), "parser_error": str(exc)}) from exc
|
||||
transport = _transport_record(group)
|
||||
transports.append(transport)
|
||||
container = None
|
||||
for sync in group:
|
||||
chunk = data[sync.offset:sync.offset + sync.size]
|
||||
try:
|
||||
container = find_joc_emdf(chunk, usable=_joc_pair_score,
|
||||
diagnostics=diagnostics,
|
||||
frame_number=frame_number)
|
||||
except UnsupportedVariantError as exc:
|
||||
if exc.variant != "no_contiguous_joc_container":
|
||||
exc.add_context(frame=frame_number,
|
||||
details={"syncframe_bytes": len(chunk)})
|
||||
raise
|
||||
continue
|
||||
except EmdfError as exc:
|
||||
raise UnsupportedVariantError(
|
||||
"emdf_transport", "container_syntax",
|
||||
"EMDF 容器语法无法解析",
|
||||
frame=frame_number,
|
||||
details={"syncframe_bytes": len(chunk),
|
||||
"parser_error": str(exc)}) from exc
|
||||
break
|
||||
if container is None:
|
||||
missing_joc_frames += 1
|
||||
rows.append({
|
||||
"frame": frame_number,
|
||||
"emdf_hash": "",
|
||||
"emdf_size": 0,
|
||||
"emdf_start_bit": "",
|
||||
"payload_ids": "",
|
||||
"error": "no_joc",
|
||||
})
|
||||
payloads.append(None)
|
||||
offsets.append(None)
|
||||
continue
|
||||
|
||||
if 14 in container.payloads:
|
||||
parsed = parse_joc_header(container.payloads[14])
|
||||
parsed["payload"] = container.payloads[14]
|
||||
check_cross_layer(parsed, frame=frame_number, transport=transport,
|
||||
bed_channels=(
|
||||
program_bed_channels(container.payloads[11])
|
||||
if 11 in container.payloads else None),
|
||||
diagnostics=diagnostics)
|
||||
if config_idx is None:
|
||||
config_idx = parsed["dmx_config_idx"]
|
||||
n_channels = parsed["n_channels"]
|
||||
|
||||
digest = hashlib.sha256(container.raw).hexdigest()
|
||||
if emdf_dir is not None:
|
||||
target = emdf_dir / f"{digest}.bin"
|
||||
@@ -71,19 +307,27 @@ class DirectPayloadIndex:
|
||||
print(f"[metadata] {frame_number + 1} frames", flush=True)
|
||||
if not rows:
|
||||
raise EmdfError("E-AC-3 输入中没有可处理的同步帧")
|
||||
if config_idx is None:
|
||||
raise UnsupportedVariantError(
|
||||
"emdf_transport", "no_contiguous_joc_container",
|
||||
"输入中没有找到可连续解析且同时包含 ID11/ID14 的 EMDF 容器",
|
||||
details={"audio_frames": len(rows), "syncframe_bytes": len(data)})
|
||||
if missing_joc_frames:
|
||||
diagnostics.append({
|
||||
"stage": "eac3",
|
||||
"variant": "joc_frame_missing",
|
||||
"frame": None,
|
||||
"message": "部分音频帧没有 JOC 元数据,这些帧的对象通道静音",
|
||||
"details": {"frames": missing_joc_frames, "total": len(rows)},
|
||||
})
|
||||
if cache_dir is not None:
|
||||
with (cache_dir / "frames.csv").open("w", encoding="utf-8", newline="") as fp:
|
||||
fields = ("frame", "emdf_hash", "emdf_size", "emdf_start_bit", "payload_ids", "error")
|
||||
writer = csv.DictWriter(fp, fieldnames=fields)
|
||||
writer.writeheader()
|
||||
writer.writerows(rows)
|
||||
return cls(rows, payloads, offsets, cache_dir)
|
||||
|
||||
def subpayloads(self, row):
|
||||
return self._subpayloads[int(row["frame"])]
|
||||
|
||||
def subpayload_sample_offset(self, row, payload_id):
|
||||
return int(self._sample_offsets[int(row["frame"])].get(payload_id, 0))
|
||||
return cls(rows, payloads, offsets, cache_dir, transports, diagnostics,
|
||||
config_idx, n_channels)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.rows)
|
||||
@@ -194,6 +438,10 @@ def inspect(index, limit=None, print_frames=False):
|
||||
"元数据容器无法解析",
|
||||
frame=frame_number,
|
||||
details={"exception_type": type(exc).__name__, "parser_error": str(exc)}) from exc
|
||||
if subs is None:
|
||||
# 该音频帧没有任何 JOC EMDF(例如编码器未写首帧元数据):对象静音。
|
||||
frame_records.append({"frame": frame_number, "no_joc": True})
|
||||
continue
|
||||
if 11 not in subs or 14 not in subs:
|
||||
raise UnsupportedVariantError(
|
||||
"emdf_payloads", "missing_required_payload",
|
||||
@@ -246,10 +494,10 @@ def inspect(index, limit=None, print_frames=False):
|
||||
"parser_error": str(exc),
|
||||
"repair_hint": "检查 JOC header、对象数、参数带、Huffman 或扩展字段",
|
||||
}) from exc
|
||||
if parsed["n_channels"] != 5 or parsed["n_objects"] > 15:
|
||||
if parsed["n_channels"] not in (5, 7):
|
||||
raise UnsupportedVariantError(
|
||||
"joc", "unsupported_configuration",
|
||||
"JOC 核心通道数或对象数超出当前渲染器范围",
|
||||
"JOC 核心通道数超出当前渲染器范围",
|
||||
frame=frame_number,
|
||||
details={
|
||||
"downmix_config": parsed["dmx_config_idx"],
|
||||
@@ -258,6 +506,10 @@ def inspect(index, limit=None, print_frames=False):
|
||||
"extension_config": parsed["ext_config_idx"],
|
||||
"payload": bytes_descriptor(subs[14]),
|
||||
})
|
||||
check_cross_layer(
|
||||
parsed, frame=frame_number,
|
||||
transport=(index.transport(row) if hasattr(index, "transport") else None),
|
||||
bed_channels=program_bed_channels(subs[11]))
|
||||
rec = _frame_record(frame_number, parsed, oamd)
|
||||
configs[(parsed["dmx_config_idx"], parsed["ext_config_idx"],
|
||||
parsed["n_channels"], parsed["n_objects"])] += 1
|
||||
|
||||
Reference in new issue
Block a user