Compare commits

...
6 Commits
Author SHA1 Message Date
Matthew Jackson ec59ffe105 v1.2.1: bump version (freemkv-unlock git-pinned for the tag) 2026-07-01 19:08:53 -07:00
Matthew Jackson 02132f52b5 changelog: describe the shipped re-base DTS fix + faithful-passthrough note 2026-07-01 19:02:04 -07:00
Matthew Jackson ca8d046f2a mux/dts: re-base PTS per PES instead of a running clock (fix long-title drift)
The first cut used a global running clock (max(next, own-PES PTS) + advance),
which fixed the same-PES collision but DRIFTED: once accumulated frame
durations exceeded the PES-timestamp spacing, it never re-based, so a
feature-long DVD DTS track ran minutes past its real length (2h44 for a 2h03
film) while AC-3 from the same source stayed exact.

Match the AC-3 path: re-base to each PES's own container timestamp, and advance
by one frame duration ONLY within a run of AUs sharing one PES. Fixes the DVD
multi-frame-per-PES collision without drift; the UHD DTS-HD MA per-PES
attribution (da85f56) is preserved (each AU still takes its own core PES's PTS).
Adds new_pes_rebases_to_its_own_pts_no_drift; full mux suite green (905).
2026-07-01 18:21:25 -07:00
Matthew Jackson f33a9f6c22 v1.2.1: bump version + changelog (DVD DTS timestamp fix) 2026-07-01 17:30:25 -07:00
Matthew Jackson a9aee0d5a4 mux/dts: monotonic per-frame PTS + real frame duration (fixes DVD DTS)
DVD packs several DTS core frames into one PES; the parser stamped every
access unit with that single PES PTS and duration_ns=None, so consecutive
frames collided on one timestamp — ffmpeg rejected the output as 'non
monotonically increasing dts to muxer: X >= X' (deep-decode = corrupt,
e.g. The Punisher). The UHD DTS-HD MA path (one AU per PES, distinct PTS)
was unaffected, which is why this only surfaced on DVD.

Parse the DTS core header for samples ((NBLKS+1)*32) and sample rate
(SFREQ, 48kHz fallback) to derive each AU's duration, and stamp a running
monotonic PTS: max(next_clock, own-core-PES PTS), then advance by the
frame duration. A later PES whose PTS is ahead of the clock still wins
(preserves the UHD per-PES attribution from da85f56/571c613); frames
sharing one PES advance frame-by-frame instead of colliding.

Tests: the 3 that encoded 'same PES -> same PTS' now assert monotonic
advance; new dvd_many_cores_one_pes_are_strictly_monotonic reproduces the
Punisher bug; duration/SFREQ-fallback unit tests added.
2026-07-01 17:17:35 -07:00
Matthew Jackson e16f207efa restore freemkv-unlock path dep for local dev (post-v1.2.0) 2026-07-01 16:53:06 -07:00
3 changed files with 258 additions and 32 deletions
+20
View File
@@ -1,5 +1,25 @@
# Changelog
## [1.2.1] — 2026-07-02
### Fixed
- **DVD DTS audio no longer muxes with non-monotonic timestamps.** A DVD
Program Stream packs several DTS core frames into one PES packet; the parser
stamped every access unit with that single PES timestamp and no per-frame
duration, so consecutive frames collided on one PTS and a strict decode/remux
(ffmpeg) rejected the track — `non monotonically increasing dts to muxer`.
The DTS parser now derives each core frame's duration from its header
(`(NBLKS+1)*32` samples ÷ the `SFREQ` sample rate) and re-bases to each PES's
own container timestamp, advancing by a frame duration only *within* a single
PES — so the track stays monotonic and does not drift past its real length on
a feature-long title. The UHD DTS-HD MA path (one access unit per PES) is
unaffected: each unit keeps its own PES timestamp, preserving the 1.2.0 per-PES
attribution. Completes the DVD DTS fix begun in 1.2.0 (which corrected the
silent-track routing, exposing this timing bug). Note: genuinely corrupt
source DTS frames — valid framing, bad audio blocks — are passed through
faithfully; freemkv never fabricates or drops audio it can't prove is bad.
## [1.2.0] — 2026-07-01
### Breaking
+2 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "libfreemkv"
version = "1.2.0"
version = "1.2.1"
edition = "2024"
rust-version = "1.86"
license = "AGPL-3.0-only"
@@ -28,7 +28,7 @@ cbc = "0.1"
# Interim path dep for local cross-repo dev; the release script re-pins this to
# `{ git = ".../freemkv-unlock", tag = "vX.Y.Z" }` before tagging libfreemkv (so
# the released tag resolves freemkv-unlock from git, not a sibling path).
freemkv-unlock = { git = "https://github.com/freemkv/freemkv-unlock", tag = "v1.2.0" }
freemkv-unlock = { git = "https://github.com/freemkv/freemkv-unlock", tag = "v1.2.1" }
num-bigint = "0.4"
num-traits = "0.2"
num-integer = "0.1"
+236 -30
View File
@@ -38,6 +38,17 @@ pub struct DtsParser {
/// current `buf` start and rebased whenever bytes are drained from the
/// front.
pts_marks: Vec<(usize, i64)>,
/// The `front_pts` of the PREVIOUS emitted access unit. When the current
/// AU's `front_pts` differs, it began a new PES → re-base to it. When it is
/// unchanged, this AU shares the previous AU's PES → advance one frame
/// duration. This per-PES re-base (rather than a global running clock) is
/// what keeps a feature-long DVD DTS track from drifting past its real
/// length. `PTS_UNSET` = no AU emitted yet.
last_front_pts: i64,
/// The PTS for the NEXT AU *if it shares the current PES* (the within-PES
/// running cursor: previous emit + its duration). Only consulted when
/// `front_pts` is unchanged from `last_front_pts`. `PTS_UNSET` = no base yet.
next_pts_ns: i64,
}
impl Default for DtsParser {
@@ -52,9 +63,41 @@ impl DtsParser {
buf: Vec::with_capacity(32768),
pending_pts: 0,
pts_marks: Vec::new(),
last_front_pts: PTS_UNSET,
next_pts_ns: PTS_UNSET,
}
}
/// Stamp an access unit's PTS. `front` is the AU's own core-PES PTS (from
/// [`front_pts`]); `dur_ns` its decoded duration.
///
/// The model matches the (correct) AC-3 path: **re-base to each PES's own
/// container timestamp, and advance by one frame duration ONLY within a run
/// of AUs that share the same PES.** A new PES (its `front` differs from the
/// previous AU's) trusts its own timestamp — so the emitted timeline tracks
/// the container and never drifts. Advancing within a PES is what fixes the
/// DVD case (several DTS core frames packed in one PES, which otherwise all
/// collided on that single PES timestamp → "non monotonically increasing
/// dts"). A global running clock was WRONG here: once accumulated frame
/// durations exceeded the PES spacing it never re-based, drifting the track
/// minutes past the real length over a feature-long title.
fn stamp_pts(&mut self, front: i64, dur_ns: i64) -> i64 {
let base = if front != PTS_UNSET && front != self.last_front_pts {
// New PES (or the first AU): trust its own timestamp — no drift.
front
} else if self.next_pts_ns != PTS_UNSET {
// Same PES as the previous AU (front unchanged) → advance one frame.
self.next_pts_ns
} else if front != PTS_UNSET {
front
} else {
0
};
self.last_front_pts = front;
self.next_pts_ns = base + dur_ns;
base
}
/// Drop `n` bytes from the front of `buf` and rebase the PTS markers so
/// their offsets stay relative to the new buffer start. A marker that now
/// sits at or before offset 0 is clamped to 0 (it still covers the front).
@@ -137,6 +180,10 @@ impl CodecParser for DtsParser {
self.buf.clear();
self.pts_marks.clear();
self.pending_pts = PTS_UNSET;
// A concealed gap is a timeline discontinuity: let the post-gap AU
// re-base to its own PES PTS rather than the pre-gap cursor.
self.next_pts_ns = PTS_UNSET;
self.last_front_pts = PTS_UNSET;
}
if pes.data.is_empty() {
return Vec::new();
@@ -267,10 +314,13 @@ impl CodecParser for DtsParser {
};
let au: Vec<u8> = self.buf[..au_end].to_vec();
// The AU takes the PTS of the PES covering its first byte — its own
// core's PES, even if that PES preceded the one(s) carrying its
// extensions or the next core.
let au_pts = self.front_pts();
// The AU's own core PES PTS (the PES covering its first byte, even if
// that PES preceded the one(s) carrying its extensions or the next
// core), stamped monotonically: honored when it advances past the
// running clock (UHD one-AU-per-PES), but never allowed to collide
// with the previous AU when several cores share ONE PES (DVD).
let dur_ns = dts_core_duration_ns(&au) as i64;
let au_pts = self.stamp_pts(self.front_pts(), dur_ns);
frames.push(Frame {
discontinuity: false,
coding: None,
@@ -278,7 +328,7 @@ impl CodecParser for DtsParser {
pts_ns: au_pts,
keyframe: true,
data: au,
duration_ns: None,
duration_ns: Some(dur_ns as u64),
});
self.drain_front(au_end);
// After draining, the marker covering the new front (if any) carries
@@ -324,9 +374,9 @@ impl CodecParser for DtsParser {
}
// The final AU's PTS is the PES covering the buffer front (its core's
// PES). Fall back to pending_pts, clamping the sentinel to 0.
let front = self.front_pts();
let pts_ns = if front == PTS_UNSET { 0 } else { front };
let au = std::mem::take(&mut self.buf);
let dur_ns = dts_core_duration_ns(&au) as i64;
let pts_ns = self.stamp_pts(self.front_pts(), dur_ns);
self.pts_marks.clear();
vec![Frame {
discontinuity: false,
@@ -335,7 +385,7 @@ impl CodecParser for DtsParser {
pts_ns,
keyframe: true,
data: au,
duration_ns: None,
duration_ns: Some(dur_ns as u64),
}]
}
@@ -501,6 +551,49 @@ fn dts_core_frame_size(data: &[u8]) -> usize {
fsize + 1
}
/// DTS core `SFREQ` → sample rate (Hz). 4-bit index; reserved/invalid entries
/// fall back to 48 kHz (the DVD/UHD norm) so a bogus value never yields a zero
/// rate (division) or a wildly wrong frame duration.
const DTS_CORE_SAMPLE_RATES: [u32; 16] = [
48_000, // 0: invalid → fallback
8_000, 16_000, 32_000, 48_000, // 4: invalid → fallback
48_000, // 5: invalid → fallback
11_025, 22_050, 44_100, 48_000, // 9: invalid → fallback
48_000, // 10: invalid → fallback
12_000, 24_000, 48_000, 96_000, 192_000,
];
/// Samples in one DTS core frame: `(NBLKS + 1) * 32`. `NBLKS` (7 bits) is the
/// core-header PCM-sample-block count — the same field ffmpeg's `dca` decoder
/// uses to timestamp frames. Bit layout after the 32-bit sync: FTYPE(1) SHORT(5)
/// CPF(1) **NBLKS(7)** FSIZE(14) …, so NBLKS = byte4 bit0 + byte5 bits7-2.
fn dts_core_samples(data: &[u8]) -> u32 {
if data.len() < CORE_HEADER_MIN_BYTES {
return 512; // typical; only reached on a truncated header
}
let nblks = ((data[4] as u32 & 0x01) << 6) | (data[5] as u32 >> 2);
(nblks + 1) * 32
}
/// DTS core sample rate (Hz) from `SFREQ` (4 bits: byte8 bits5-2), with a 48 kHz
/// fallback for reserved indices.
fn dts_core_sample_rate(data: &[u8]) -> u32 {
if data.len() < CORE_HEADER_MIN_BYTES {
return 48_000;
}
let sfreq = (data[8] as usize >> 2) & 0x0F;
DTS_CORE_SAMPLE_RATES[sfreq]
}
/// Duration of one DTS core access unit in nanoseconds: `samples / rate`,
/// rounded to nearest. This is what lets consecutive core frames packed in a
/// single DVD PES advance monotonically instead of colliding on one PES PTS.
fn dts_core_duration_ns(data: &[u8]) -> u64 {
let samples = dts_core_samples(data) as u64;
let rate = dts_core_sample_rate(data) as u64;
(samples * 1_000_000_000 + rate / 2) / rate
}
#[cfg(test)]
mod tests {
use super::*;
@@ -521,12 +614,22 @@ mod tests {
let fsize = size - 1;
let mut data = vec![0u8; size];
data[0..4].copy_from_slice(&DTS_CORE_SYNC);
data[5] = (data[5] & 0xFC) | ((fsize >> 12) & 0x03) as u8;
// NBLKS = 15 → (15+1)*32 = 512 samples/frame (the DVD/UHD DTS-core norm).
// NBLKS is byte4 bit0 + byte5 bits7-2; here byte4 bit0 = 0, byte5 = 15<<2.
data[5] = (15u8 << 2) | ((fsize >> 12) & 0x03) as u8;
data[6] = ((fsize >> 4) & 0xFF) as u8;
data[7] = (data[7] & 0x0F) | (((fsize & 0x0F) << 4) as u8);
// SFREQ = 13 → 48 kHz (byte8 bits5-2). Only when the header byte exists.
if size > 8 {
data[8] = 13u8 << 2;
}
data
}
// 512 samples @ 48 kHz, rounded to nearest ns — the duration make_dts_core
// frames advance by. (512 * 1e9 + 24000) / 48000 = 10_666_667 ns.
const DTS_CORE_DUR_NS: i64 = (512 * 1_000_000_000 + 48_000 / 2) / 48_000;
/// A real DTS-HD EXSS substream of `total` bytes (short header form), with an
/// optional false DTS core syncword embedded in its payload (decoding to a
/// plausible core size) — to prove precise sizing, not a payload scan, bounds
@@ -657,10 +760,11 @@ mod tests {
}
#[test]
fn two_cores_back_to_back_emit_first_on_boundary() {
// The first complete unit is emitted as soon as the next core sync is
// seen; the second is held until flush. Both cores arrived in ONE PES,
// so both legitimately carry that PES's PTS.
fn two_cores_back_to_back_advance_within_one_pes() {
// Both cores arrive in ONE PES — the DVD layout. AU1 keeps the PES PTS;
// AU2 must ADVANCE by one frame duration to stay monotonic. Reusing the
// single PES PTS for both was the "non monotonically increasing dts to
// muxer" bug (fixed for 1.2.1).
let mut parser = DtsParser::new();
let mut stream = make_dts_core(512);
stream.extend_from_slice(&make_dts_core(640));
@@ -668,13 +772,18 @@ mod tests {
assert_eq!(f.len(), 1);
assert_eq!(f[0].data.len(), 512);
assert_eq!(f[0].pts_ns, pts_to_ns(90000), "AU1 keeps its PES PTS");
assert_eq!(
f[0].duration_ns,
Some(DTS_CORE_DUR_NS as u64),
"AU1 carries a real frame duration (was None)"
);
let tail = parser.flush();
assert_eq!(tail.len(), 1);
assert_eq!(tail[0].data.len(), 640);
assert_eq!(
tail[0].pts_ns,
pts_to_ns(90000),
"AU2 came in the same PES → same PTS"
pts_to_ns(90000) + DTS_CORE_DUR_NS,
"AU2 in the same PES advances one frame duration (monotonic)"
);
}
@@ -689,31 +798,33 @@ mod tests {
let mut parser = DtsParser::new();
// PES A: just core1 (held — no following core yet).
let f0 = parser.parse(&make_pes(make_dts_core(512), Some(100)));
let f0 = parser.parse(&make_pes(make_dts_core(512), Some(90000)));
assert!(f0.is_empty(), "core1 held awaiting next core");
// PES B: core2 + core3. Closes AU1 (core1) and AU2 (core2).
// PES B (realistically LATER — far past one frame): core2 + core3.
// Closes AU1 (core1) and AU2 (core2).
let mut pes_b = make_dts_core(600);
pes_b.extend_from_slice(&make_dts_core(640));
let f = parser.parse(&make_pes(pes_b, Some(200)));
let f = parser.parse(&make_pes(pes_b, Some(190000)));
assert_eq!(f.len(), 2, "AU1 and AU2 both close in this call");
assert_eq!(f[0].data.len(), 512, "AU1 = core1");
assert_eq!(
f[0].pts_ns,
pts_to_ns(100),
pts_to_ns(90000),
"AU1 keeps PES A's PTS, not the later PES B PTS"
);
assert_eq!(f[1].data.len(), 600, "AU2 = core2");
assert_eq!(
f[1].pts_ns,
pts_to_ns(200),
"AU2's core arrived in PES B → PES B PTS"
pts_to_ns(190000),
"AU2's core is in PES B → attributes to PES B PTS (ahead of the clock, so it wins)"
);
// AU3 (core3) drains on flush, also PES B PTS.
// AU3 (core3) drains on flush — 2nd core in PES B, so it advances one
// frame duration from AU2 to stay monotonic.
let tail = parser.flush();
assert_eq!(tail.len(), 1);
assert_eq!(tail[0].pts_ns, pts_to_ns(200));
assert_eq!(tail[0].pts_ns, pts_to_ns(190000) + DTS_CORE_DUR_NS);
}
#[test]
@@ -728,23 +839,118 @@ mod tests {
// AU2 (core2) held awaiting a third core.
let mut pes_a = make_dts_core(512);
pes_a.extend_from_slice(&make_dts_core(600));
let f = parser.parse(&make_pes(pes_a, Some(100)));
let f = parser.parse(&make_pes(pes_a, Some(90000)));
assert_eq!(f.len(), 1);
assert_eq!(f[0].pts_ns, pts_to_ns(100), "AU1 PES A PTS");
assert_eq!(f[0].pts_ns, pts_to_ns(90000), "AU1 PES A PTS");
// PES B: core3 — closes AU2 (core2). AU2's core was in PES A.
let f2 = parser.parse(&make_pes(make_dts_core(640), Some(200)));
// PES B (realistically later): core3 — closes AU2 (core2). AU2's core
// was in PES A, so it is PES A's 2nd frame: it advances one frame
// duration from AU1 (still on PES A's timeline, and monotonic) — it does
// NOT inherit the closing PES B PTS.
let f2 = parser.parse(&make_pes(make_dts_core(640), Some(190000)));
assert_eq!(f2.len(), 1);
assert_eq!(f2[0].data.len(), 600, "AU2 = core2");
assert_eq!(
f2[0].pts_ns,
pts_to_ns(100),
"AU2's core arrived in PES A → must keep PES A's PTS, not PES B's"
pts_to_ns(90000) + DTS_CORE_DUR_NS,
"AU2 = 2nd frame of PES A → PES A base + one frame, not the closing PES B PTS"
);
// AU3 = core3, whose own core is in PES B → jumps to PES B's PTS.
let tail = parser.flush();
assert_eq!(tail.len(), 1);
assert_eq!(tail[0].pts_ns, pts_to_ns(200), "AU3 = core3, PES B PTS");
assert_eq!(
tail[0].pts_ns,
pts_to_ns(190000),
"AU3 = core3 in PES B → PES B PTS"
);
}
#[test]
fn dvd_many_cores_one_pes_are_strictly_monotonic() {
// Punisher-DVD reproduction: a single PES carrying SEVERAL DTS core
// frames (the DVD packing) must emit STRICTLY-increasing PTSs. The old
// code stamped every AU with the one PES PTS, which ffmpeg rejected as
// "non monotonically increasing dts to muxer: X >= X".
let mut parser = DtsParser::new();
let mut stream = Vec::new();
for _ in 0..6 {
stream.extend_from_slice(&make_dts_core(512));
}
let mut frames = parser.parse(&make_pes(stream, Some(90000)));
frames.extend(parser.flush());
assert_eq!(frames.len(), 6, "all six cores emitted");
for w in frames.windows(2) {
assert!(
w[1].pts_ns > w[0].pts_ns,
"consecutive DTS AUs must STRICTLY increase: {} !> {}",
w[1].pts_ns,
w[0].pts_ns
);
}
// Each advances by exactly one frame duration, and carries that duration.
assert_eq!(frames[0].pts_ns, pts_to_ns(90000));
assert_eq!(frames[1].pts_ns, pts_to_ns(90000) + DTS_CORE_DUR_NS);
assert_eq!(frames[5].pts_ns, pts_to_ns(90000) + 5 * DTS_CORE_DUR_NS);
for f in &frames {
assert_eq!(f.duration_ns, Some(DTS_CORE_DUR_NS as u64));
}
}
#[test]
fn dts_core_duration_512_samples_48khz() {
// NBLKS=15 → (15+1)*32 = 512 samples; SFREQ=13 → 48 kHz.
let core = make_dts_core(512);
assert_eq!(dts_core_samples(&core), 512);
assert_eq!(dts_core_sample_rate(&core), 48_000);
assert_eq!(dts_core_duration_ns(&core), DTS_CORE_DUR_NS as u64);
}
#[test]
fn dts_core_sfreq_reserved_falls_back_to_48k() {
// A bogus SFREQ index must never yield a zero rate (division) — fall
// back to 48 kHz.
let mut core = make_dts_core(512);
core[8] = 0; // SFREQ = 0 (reserved)
assert_eq!(dts_core_sample_rate(&core), 48_000);
}
#[test]
fn new_pes_rebases_to_its_own_pts_no_drift() {
// Regression for the drift bug: a global running clock overshot a
// feature-long DTS track by minutes (2h44 for a 2h03 film). When a NEW
// PES arrives whose PTS is BEHIND where accumulated frame durations
// would put a running clock, the AU must re-base to that PES's OWN
// timestamp — tracking the container, not drifting ahead of it.
let mut parser = DtsParser::new();
// PES A: core1 + core2 (2 frames), pts 90000.
let mut pes_a = make_dts_core(512);
pes_a.extend_from_slice(&make_dts_core(600));
let f = parser.parse(&make_pes(pes_a, Some(90000)));
assert_eq!(f.len(), 1, "AU1 (core1) emits on the core2 boundary");
assert_eq!(f[0].pts_ns, pts_to_ns(90000), "AU1 = PES A base");
// PES B: core3, pts only 500 ticks after PES A — LESS than one frame
// (960 ticks @ 48 kHz). AU2 (core2, still PES A) advances within PES A;
// AU3 (core3, PES B) must RE-BASE to PES B's own PTS.
let f2 = parser.parse(&make_pes(make_dts_core(640), Some(90500)));
assert_eq!(f2.len(), 1, "AU2 (core2) closes on core3");
assert_eq!(
f2[0].pts_ns,
pts_to_ns(90000) + DTS_CORE_DUR_NS,
"AU2 = 2nd frame of PES A → advances one frame within PES A"
);
let tail = parser.flush();
assert_eq!(tail.len(), 1);
assert_eq!(
tail[0].pts_ns,
pts_to_ns(90500),
"AU3 = core3 in PES B → re-bases to PES B's PTS"
);
assert_ne!(
tail[0].pts_ns,
pts_to_ns(90000) + 2 * DTS_CORE_DUR_NS,
"must NOT carry the accumulated running clock across a PES (drift)"
);
}
/// Build a minimal DTS-HD extension substream of `size` bytes (just the