From 45ddc4ffa6243c5509ef34c8b5260acdcc90b645 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 12 Nov 2026 22:05:44 +0100 Subject: [PATCH] mpegts: a repeated PAT or PMT packet is not appended to the section in progress The scan fed every PAT and PMT packet to the section parser, so a packet sent twice (same continuity counter and payload, H.222.0 2.4.3.3) appended its payload to the section being collected. PAT and PMT packets now go through a per-PID repeat guard first, like the elementary stream packets; a packet that signals a discontinuity is never taken for a repeat. The rest of storytold/Filmcraft#1438 is already in craft-media (#993). Co-Authored-By: Claude Sonnet 5.5 --- crates/media/src/mpegts/tests.rs | 17 +++++++++++++++++ crates/media/src/mpegts/ts.rs | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/crates/media/src/mpegts/tests.rs b/crates/media/src/mpegts/tests.rs index 0215680ad..b9cb1af3b 100644 --- a/crates/media/src/mpegts/tests.rs +++ b/crates/media/src/mpegts/tests.rs @@ -791,6 +791,23 @@ fn scrambled_packet_is_skipped_with_a_warning() { assert!(total(&f) < total(&base)); } +#[test] +fn repeated_pat_and_pmt_packets_are_dropped_before_the_section_parser() { + let clean = audio_ts(2); + let first = |pid: u16| { + (0..clean.len() / 188).map(|i| i * 188).find(|&o| (((clean[o + 1] & 0x1F) as u16) << 8 | clean[o + 2] as u16) == pid).expect("packet exists") + }; + let mut dup = clean.clone(); + for pid in [0x100, 0] { + let o = first(pid); + let copy = dup[o..o + 188].to_vec(); + dup.splice(o + 188..o + 188, copy); + } + let f = open(&dup).unwrap(); + assert!(f.warnings.iter().any(|w| w.contains("2 duplicate packet(s) dropped")), "{:?}", f.warnings); + assert!(!f.warnings.iter().any(|w| w.contains("continuity") || w.contains("CRC")), "{:?}", f.warnings); +} + #[test] fn duplicate_packet_is_dropped_but_a_different_payload_with_the_same_counter_is_kept() { let clean = audio_ts(6); diff --git a/crates/media/src/mpegts/ts.rs b/crates/media/src/mpegts/ts.rs index a4c01a986..1d28bb736 100644 --- a/crates/media/src/mpegts/ts.rs +++ b/crates/media/src/mpegts/ts.rs @@ -497,6 +497,7 @@ pub(crate) fn scan(src: &S, packet_size: usize, sync_off let mut warnings: Vec = Vec::new(); let mut pat = Psi::default(); let mut pmt = PmtState::default(); + let mut psi_dup: HashMap = HashMap::new(); let mut es: HashMap = HashMap::new(); let mut order: Vec = Vec::new(); // one timestamp unwrapper for the program: its streams share one clock, so a stream whose @@ -560,6 +561,13 @@ pub(crate) fn scan(src: &S, packet_size: usize, sync_off } continue; } + if pk.pid == 0 || Some(pk.pid) == pmt.pmt_pid { + // a repeated packet would append its payload to the section in progress + if psi_dup.entry(pk.pid).or_default().is_dup(&pk) { + duplicates += 1; + continue; + } + } if pk.pid == 0 { feed_pat(&pk, &mut pat, &mut pmt, &mut crc_errors); continue;