diff --git a/src/joc_decode.py b/src/joc_decode.py index 78b74b7..9ff9766 100644 --- a/src/joc_decode.py +++ b/src/joc_decode.py @@ -46,14 +46,28 @@ JOC_DENSE_OFFSET = {0: 48, 1: 96} JOC_SPARSE_OFFSET = {0: 50, 1: 100} +class JocTruncatedPayload(ValueError): + """载荷在语法读完之前用尽比特(例如 ID14 的 joc_data 被编码器写短)。 + + 属于 ``ValueError`` 子类:现有把 ``IndexError``/``ValueError`` 当作“载荷不可用” + 的调用点(容器候选打分等)不需要改动。 + """ + + class BR: - """MSB-first 位读取器;位置以载荷内的 bit offset 表示。""" + """MSB-first 位读取器;位置以载荷内的 bit offset 表示。 + + 越界抛出 :class:`JocTruncatedPayload`,而不是让 bytes 下标抛 IndexError。 + """ def __init__(self, data, pos=0): self.d = data self.p = pos 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 for _ in range(n): 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``。 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) out = parse_joc_header(payload, strict=False) 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_y_bits"] = br.bits(5) out["seq_count_bits"] = br.bits(10) diff --git a/src/metadata.py b/src/metadata.py index 77cfbb5..37e50ff 100644 --- a/src/metadata.py +++ b/src/metadata.py @@ -25,7 +25,8 @@ def _joc_pair_score(container): (ID14 声明的对象数超出其字节所能容纳的范围、ID11 未声明 groupid)既不是一对 合法载荷,也不可能是本帧的 JOC 参数。 - 2 = 两个载荷 groupid 相同且 ID14 完整解析;1 = ID14 完整解析;0 = 其余。 + 2 = 两个载荷 groupid 相同且 ID14 完整解析;1 = ID14 完整解析;0 = 其余, + 包括在 joc_data 内提前结束的载荷(状态 7):它缺对象矩阵,不应赢得容器选择。 """ payload = container.payloads.get(14) if payload is None or 11 not in container.payloads: @@ -33,7 +34,7 @@ def _joc_pair_score(container): parses = False try: parsed = parse_joc(payload) - parses = parsed["status"] not in (2, 4) + parses = parsed["status"] not in (2, 4, 7) except (IndexError, ValueError): parses = False group_ids = getattr(container, "group_ids", None) or {} @@ -457,6 +458,7 @@ def inspect(index, limit=None, print_frames=False): active = Counter() band_totals = Counter() sparse_counts = Counter() + statuses = Counter() clipgains = [] for seq, row in enumerate(rows): 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 band_totals.update({int(k): v for k, v in rec["parameter_bands"].items()}) sparse_counts[len(rec["sparse_objects"])] += 1 + statuses[parsed["status"]] += 1 clipgains.append(float(parsed["clipgain"])) if print_frames: 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} 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())}, "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())}, diff --git a/src/renderer.py b/src/renderer.py index ed579c3..5d45c54 100644 --- a/src/renderer.py +++ b/src/renderer.py @@ -165,7 +165,8 @@ class JocRenderer: def _advance_state(self, status, seq): """按 seq 推进状态机,返回进入本帧前的 ``state``。 - status:0 正常;2..6 解析层无效;8 无元数据;9 核心声道数变化。 + status:0 正常;2..7 解析层无效(7 = ID14 载荷在 joc_data 内提前结束); + 8 无元数据;9 核心声道数变化。 """ pre_state = self._state valid = status in (0, STATUS_CORE_CHANNEL_CHANGE)