mux: make B1 concealment decode-clean on every gap shape

Closes the three residual holes where a concealed/lost gap could still let
a dangling-reference frame reach the muxer (degraded/undecryptable-disc
path only; clean rips are byte-identical and untouched). Root cause: the
discontinuity signal was reconstructed from the 4-bit continuity counter
and applied per-PES, both of which are lossy.

Three coordinated changes:

1. CC-INDEPENDENT marker. fill_null_ts_unit now tags its NULL packets with
   an adaptation-field discontinuity_indicator; the demuxer recognises a
   0x1FFF packet carrying it as a concealed gap and forces a discontinuity
   on every tracked PID (the lost unit's PID is unknowable). This survives
   a loss that is an exact multiple of 16 packets (CC aliases to in-sequence
   — hole 3) and a loss at a PID's very start (no prior CC — hole 4); it
   also drops any open, potentially-truncated partial PES.

2. PUSI ATTRIBUTION. A gap landing on a PES boundary now flags the PES
   STARTING after it, not the one flushed at the boundary (hole 1) —
   stamping the pre-gap frame could arm-then-disarm the gate on a keyframe
   and admit the real post-gap inter frame.

3. PER-FRAME signal. codec::Frame gains `discontinuity`; each parser
   propagates it onto the first post-gap frame. MPEG-2 buffers whole GOPs
   asynchronously, so it associates the gap by ES OFFSET (like PTS/source),
   landing it on the exact post-gap picture mid-GOP (hole 2) — a per-PES
   flag stamped the previous picture. consume_ts (and the EOF flush drain)
   gate on frame.discontinuity.

Tests: CC-independent marker with in-sequence CC + leading-loss; PUSI
attribution flags the post-gap PES; MPEG-2 offset-mark stamps the post-gap
picture through GOP reorder, not the previous one. Existing B1 gate + EOF
tests still green (2270 lib tests).
This commit is contained in:
Matthew Jackson
2026-06-29 09:39:03 -07:00
parent 71b4b09c93
commit 789b699f95
15 changed files with 396 additions and 82 deletions
+14
View File
@@ -44,6 +44,16 @@ pub struct Frame {
pub pts_ns: i64,
/// Whether this is a keyframe (used for cue points).
pub keyframe: bool,
/// This frame is the FIRST coded picture after a concealed/lost gap (P3/B1):
/// its data begins after packets the demuxer never received (an undecryptable
/// unit concealed as NULL-TS upstream, or a continuity break in a damaged
/// source). Inter-coded video frames carrying this flag reference data that is
/// gone, so the consumer's `ResyncGate` arms here and drops forward to the next
/// keyframe. Carried per-FRAME (not per-PES) because buffering parsers — MPEG-2
/// emits whole GOPs, H.264/HEVC lag one access unit — decouple the frame from
/// the PES that carried the gap signal. Default `false`; only ever set on the
/// degraded/conceal path, so a clean rip leaves every frame `false`.
pub discontinuity: bool,
/// Frame data (elementary stream bytes).
pub data: Vec<u8>,
/// Optional duration in nanoseconds — only set by parsers that
@@ -123,11 +133,15 @@ impl PassthroughParser {
impl CodecParser for PassthroughParser {
fn parse(&mut self, pes: &PesPacket) -> Vec<Frame> {
let pts_ns = pes.pts.or(pes.dts).map(pts_to_ns).unwrap_or(0);
// Passthrough emits exactly one frame per PES with no cross-PES buffering,
// so the PES's discontinuity maps directly onto this frame. (Buffering
// parsers must instead defer the flag to the next emitted frame.)
vec![Frame {
coding: None,
source: None,
pts_ns,
keyframe: self.keyframe,
discontinuity: pes.discontinuity,
data: pes.data.clone(),
duration_ns: None,
}]