Conceal a truncated JOC payload as an invalid frame status

A real E-AC-3 JOC stream carried one ID14 payload that ends inside joc_data:
the header, clipgain, sequence counter and object parameters are complete,
only the per-object matrices are missing.  The bit reader used to raise
IndexError there, so the metadata scan and the frame loop stopped instead of
playing.

The overrun now raises a typed JocTruncatedPayload, and parse_joc classifies
it as frame status 7 after the 12-bit joc_header: the parsed fields are kept
and the renderer takes its existing invalid-frame path (hold the previous
matrices).  A payload too short for the header still raises, the container
score ignores status 7, and the metadata summary reports a frame-status
histogram.
This commit is contained in:
TheM14 committed 2026-11-09 23:51:37 +08:00
1 parent 42a05d7722
commit bd522cd0c4
3 files changed
+55 -5

No files matched your search

+47 -2
View File
@@ -46,14 +46,28 @@ JOC_DENSE_OFFSET = {0: 48, 1: 96}
JOC_SPARSE_OFFSET = {0: 50, 1: 100} JOC_SPARSE_OFFSET = {0: 50, 1: 100}
class JocTruncatedPayload(ValueError):
"""载荷在语法读完之前用尽比特(例如 ID14 的 joc_data 被编码器写短)。
属于 ``ValueError`` 子类:现有把 ``IndexError``/``ValueError`` 当作“载荷不可用”
的调用点(容器候选打分等)不需要改动。
"""
class BR: class BR:
"""MSB-first 位读取器;位置以载荷内的 bit offset 表示。""" """MSB-first 位读取器;位置以载荷内的 bit offset 表示。
越界抛出 :class:`JocTruncatedPayload`,而不是让 bytes 下标抛 IndexError。
"""
def __init__(self, data, pos=0): def __init__(self, data, pos=0):
self.d = data self.d = data
self.p = pos self.p = pos
def bits(self, n): def bits(self, n):
if self.p + n > len(self.d) * 8:
raise JocTruncatedPayload(
f"位流越界: 需要 {n} bit @bit{self.p},载荷共 {len(self.d) * 8} bit")
v = 0 v = 0
for _ in range(n): for _ in range(n):
v = (v << 1) | ((self.d[self.p >> 3] >> (7 - (self.p & 7))) & 1) v = (v << 1) | ((self.d[self.p >> 3] >> (7 - (self.p & 7))) & 1)
@@ -123,11 +137,42 @@ def parse_joc(payload):
"""解析 id14 载荷(joc() 位流),返回字段 dict 与整帧状态 ``status``。 """解析 id14 载荷(joc() 位流),返回字段 dict 与整帧状态 ``status``。
status:0 正常;2 downmix 配置不受支持;3 对象数超过 15 被截断; status:0 正常;2 downmix 配置不受支持;3 对象数超过 15 被截断;
4 参数带索引越界;5 offset_ts 非法;6 ext_config_idx=0 时对齐后仍有多余数据。 4 参数带索引越界;5 offset_ts 非法;6 ext_config_idx=0 时对齐后仍有多余数据;
7 载荷在 joc_data 内提前结束(头与对象参数完整,只缺对象矩阵)。状态 7 是可
分类的坏帧:渲染器按无效帧保持上一帧矩阵,而不是中断播放。
""" """
br = BR(payload) br = BR(payload)
out = parse_joc_header(payload, strict=False) out = parse_joc_header(payload, strict=False)
br.p = 12 br.p = 12
try:
return _parse_joc_body(payload, br, out)
except JocTruncatedPayload:
# 头(12 bit)之后的越界才可分类;头之前的越界在 parse_joc_header 里抛出,
# 那种载荷无法识别为 JOC 帧。
return _truncated_payload(payload, br, out)
def _truncated_payload(payload, br, out):
"""把 joc_data 内的越界归为整帧状态 7,保留已解析的头与对象参数。"""
out["status"] = 7
out.setdefault("clipgain_x_bits", 0)
out.setdefault("clipgain_y_bits", 0)
out.setdefault("clipgain", 1.0)
out.setdefault("seq_count_bits", 0)
objs = out.setdefault("objs", [])
while len(objs) < out.get("n_objects", 0):
objs.append({"present": 0})
for o in objs:
o.setdefault("present", 0)
if o["present"] and "n_bands" not in o:
# 参数区没读完的对象记为未出现,下游不会读到半成品字段。
o["present"] = 0
_tail_info(out, br, payload)
return out
def _parse_joc_body(payload, br, out):
"""从 clipgain 字段开始解析 joc() 载荷;比特越界抛 JocTruncatedPayload。"""
out["clipgain_x_bits"] = br.bits(3) out["clipgain_x_bits"] = br.bits(3)
out["clipgain_y_bits"] = br.bits(5) out["clipgain_y_bits"] = br.bits(5)
out["seq_count_bits"] = br.bits(10) out["seq_count_bits"] = br.bits(10)
+6 -2
View File
@@ -25,7 +25,8 @@ def _joc_pair_score(container):
(ID14 声明的对象数超出其字节所能容纳的范围、ID11 未声明 groupid)既不是一对 (ID14 声明的对象数超出其字节所能容纳的范围、ID11 未声明 groupid)既不是一对
合法载荷,也不可能是本帧的 JOC 参数。 合法载荷,也不可能是本帧的 JOC 参数。
2 = 两个载荷 groupid 相同且 ID14 完整解析;1 = ID14 完整解析;0 = 其余。 2 = 两个载荷 groupid 相同且 ID14 完整解析;1 = ID14 完整解析;0 = 其余,
包括在 joc_data 内提前结束的载荷(状态 7):它缺对象矩阵,不应赢得容器选择。
""" """
payload = container.payloads.get(14) payload = container.payloads.get(14)
if payload is None or 11 not in container.payloads: if payload is None or 11 not in container.payloads:
@@ -33,7 +34,7 @@ def _joc_pair_score(container):
parses = False parses = False
try: try:
parsed = parse_joc(payload) parsed = parse_joc(payload)
parses = parsed["status"] not in (2, 4) parses = parsed["status"] not in (2, 4, 7)
except (IndexError, ValueError): except (IndexError, ValueError):
parses = False parses = False
group_ids = getattr(container, "group_ids", None) or {} group_ids = getattr(container, "group_ids", None) or {}
@@ -457,6 +458,7 @@ def inspect(index, limit=None, print_frames=False):
active = Counter() active = Counter()
band_totals = Counter() band_totals = Counter()
sparse_counts = Counter() sparse_counts = Counter()
statuses = Counter()
clipgains = [] clipgains = []
for seq, row in enumerate(rows): for seq, row in enumerate(rows):
frame_number = int(row.get("frame", seq)) frame_number = int(row.get("frame", seq))
@@ -549,6 +551,7 @@ def inspect(index, limit=None, print_frames=False):
active[len(rec["objects_present"])] += 1 active[len(rec["objects_present"])] += 1
band_totals.update({int(k): v for k, v in rec["parameter_bands"].items()}) band_totals.update({int(k): v for k, v in rec["parameter_bands"].items()})
sparse_counts[len(rec["sparse_objects"])] += 1 sparse_counts[len(rec["sparse_objects"])] += 1
statuses[parsed["status"]] += 1
clipgains.append(float(parsed["clipgain"])) clipgains.append(float(parsed["clipgain"]))
if print_frames: if print_frames:
print(json.dumps(rec, ensure_ascii=False, separators=(",", ":"))) print(json.dumps(rec, ensure_ascii=False, separators=(",", ":")))
@@ -568,6 +571,7 @@ def inspect(index, limit=None, print_frames=False):
"objects": k[3], "frames": count} "objects": k[3], "frames": count}
for k, count in sorted(configs.items()) for k, count in sorted(configs.items())
], ],
"frame_status_histogram": {str(k): v for k, v in sorted(statuses.items())},
"active_object_count_histogram": {str(k): v for k, v in sorted(active.items())}, "active_object_count_histogram": {str(k): v for k, v in sorted(active.items())},
"parameter_band_totals": {str(k): v for k, v in sorted(band_totals.items())}, "parameter_band_totals": {str(k): v for k, v in sorted(band_totals.items())},
"sparse_object_count_histogram": {str(k): v for k, v in sorted(sparse_counts.items())}, "sparse_object_count_histogram": {str(k): v for k, v in sorted(sparse_counts.items())},
+2 -1
View File
@@ -165,7 +165,8 @@ class JocRenderer:
def _advance_state(self, status, seq): def _advance_state(self, status, seq):
"""按 seq 推进状态机,返回进入本帧前的 ``state``。 """按 seq 推进状态机,返回进入本帧前的 ``state``。
status:0 正常;2..6 解析层无效;8 无元数据;9 核心声道数变化。 status:0 正常;2..7 解析层无效(7 = ID14 载荷在 joc_data 内提前结束);
8 无元数据;9 核心声道数变化。
""" """
pre_state = self._state pre_state = self._state
valid = status in (0, STATUS_CORE_CHANNEL_CHANGE) valid = status in (0, STATUS_CORE_CHANNEL_CHANGE)