mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-26 23:58:48 +00:00
Match Matroska cold-seek audio frame rounding
This commit is contained in:
@@ -174,25 +174,30 @@ impl ContainerFile {
|
||||
ContainerFile::Matroska(f) => {
|
||||
let track = &f.tracks[track_idx];
|
||||
// Indexed PTS already subtracts CodecDelay rounded to the container clock.
|
||||
// Translate only that rounding difference, not the full delay a second time.
|
||||
// Translate between that clock and the warm path's whole-frame delay.
|
||||
let tick_ns = i128::from(f.info.timestamp_scale.max(1));
|
||||
let delay_ns = i128::from(track.codec_delay_ns);
|
||||
let rounding_ns = ((delay_ns + tick_ns / 2) / tick_ns) * tick_ns - delay_ns;
|
||||
let indexed_delay_ns = ((delay_ns + tick_ns / 2) / tick_ns) * tick_ns;
|
||||
let delay_frames = audio_frames_from_ns(delay_ns, native_rate);
|
||||
let preroll_ns = i128::from(track.seek_pre_roll_ns.max(80_000_000));
|
||||
let target_ns = (i128::from(start.max(0)) * 1_000_000_000) / i128::from(native_rate);
|
||||
let seek_ns = (target_ns - preroll_ns - rounding_ns).max(0);
|
||||
let target_ns = ((i128::from(start.max(0)) + delay_frames) * 1_000_000_000) / i128::from(native_rate);
|
||||
let seek_ns = (target_ns - preroll_ns - indexed_delay_ns).max(0);
|
||||
let t_track = track.ns_to_ticks(i64::try_from(seek_ns).unwrap_or(i64::MAX));
|
||||
let sample_idx = track.sample_at_pts(t_track).unwrap_or(0);
|
||||
let seek_idx = sample_idx.saturating_sub(2);
|
||||
let sample_pts = track.samples.get(seek_idx).map_or(0, |s| s.pts);
|
||||
let sample_ns = i128::from(track.ticks_to_ns(sample_pts)) + rounding_ns;
|
||||
let decoded_pos = i64::try_from((sample_ns * i128::from(native_rate)) / 1_000_000_000).unwrap_or(0);
|
||||
let sample_ns = i128::from(track.ticks_to_ns(sample_pts)) + indexed_delay_ns;
|
||||
let decoded_pos = i64::try_from(audio_frames_from_ns(sample_ns, native_rate) - delay_frames).unwrap_or(0);
|
||||
(seek_idx, decoded_pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn audio_frames_from_ns(ns: i128, rate: u32) -> i128 {
|
||||
(ns * i128::from(rate)) / 1_000_000_000
|
||||
}
|
||||
|
||||
fn open_audio_tracks<I>(container: &ContainerFile, source: &dyn ByteSource, candidates: I) -> Option<(Vec<AudioTrackEntry>, Vec<AudioStreamInfo>)>
|
||||
where
|
||||
I: IntoIterator<Item = (usize, usize, CodecParams, u64)>,
|
||||
@@ -213,7 +218,7 @@ where
|
||||
let dec = AudioTrackDecoder::new(¶ms, &refs).ok()?;
|
||||
let native_rate = dec.sample_rate().max(1);
|
||||
let channels = u32::from(dec.layout().channels()).max(1);
|
||||
let delay_samples = i64::try_from((i128::from(codec_delay_ns) * i128::from(native_rate)) / 1_000_000_000).unwrap_or(0);
|
||||
let delay_samples = i64::try_from(audio_frames_from_ns(i128::from(codec_delay_ns), native_rate)).unwrap_or(0);
|
||||
audio_infos.push(AudioStreamInfo {
|
||||
sample_rate: native_rate,
|
||||
channels,
|
||||
|
||||
@@ -1596,13 +1596,21 @@ fn filmcraft_fallback_handles_mjpeg_mov() {
|
||||
/// contiguous, so the test isolates reset positioning from separately handled timestamp gaps.
|
||||
#[test]
|
||||
fn matroska_cold_seek_applies_codec_delay_once() {
|
||||
assert_matroska_cold_seek_codec_delay(8_250_000); // Exactly 396 samples, but 8 indexed ticks.
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matroska_cold_seek_matches_fractional_codec_delay_rounding() {
|
||||
assert_matroska_cold_seek_codec_delay(8_260_000); // 396.48 samples, also 8 indexed ticks.
|
||||
}
|
||||
|
||||
fn assert_matroska_cold_seek_codec_delay(delay_ns: i64) {
|
||||
use craft_media::matroska::{self, MkvWriter, MuxOptions, TrackKind, TrackSpec};
|
||||
use craft_video::prores::{ChromaFormat, Encoder, Frame, Profile};
|
||||
|
||||
const RATE: u32 = 48_000;
|
||||
const PACKET_FRAMES: usize = 4_800;
|
||||
const PACKET_NS: i64 = 100_000_000;
|
||||
const DELAY_NS: i64 = 8_250_000; // 396 samples, but 8 rounded container ticks.
|
||||
const CHANGE_PACKET: i64 = 32;
|
||||
const HALF_WINDOW: usize = 192;
|
||||
|
||||
@@ -1616,7 +1624,7 @@ fn matroska_cold_seek_applies_codec_delay_once() {
|
||||
|
||||
let mut audio = TrackSpec::new(TrackKind::Audio, "A_PCM/INT/LIT");
|
||||
audio.audio = Some((f64::from(RATE), 1, Some(16)));
|
||||
audio.codec_delay_ns = DELAY_NS as u64;
|
||||
audio.codec_delay_ns = delay_ns as u64;
|
||||
audio.default_duration_ns = Some(PACKET_NS as u64);
|
||||
let mut mux = MkvWriter::new(std::io::Cursor::new(Vec::new()), vec![video, audio], MuxOptions::default()).unwrap();
|
||||
mux.write_frame(0, 0, true, &picture, None).unwrap();
|
||||
@@ -1624,7 +1632,7 @@ fn matroska_cold_seek_applies_codec_delay_once() {
|
||||
let signal = 16_384i16.to_le_bytes().repeat(PACKET_FRAMES);
|
||||
for n in 0..40 {
|
||||
let pcm = if n < CHANGE_PACKET { &silent } else { &signal };
|
||||
mux.write_frame(1, n * PACKET_NS - DELAY_NS, true, pcm, None).unwrap();
|
||||
mux.write_frame(1, n * PACKET_NS - delay_ns, true, pcm, None).unwrap();
|
||||
}
|
||||
let bytes = mux.finish().unwrap().into_inner();
|
||||
let mkv = matroska::open(bytes.as_slice()).unwrap();
|
||||
@@ -1648,7 +1656,7 @@ fn matroska_cold_seek_applies_codec_delay_once() {
|
||||
let t = Tick::from_units(n * PACKET_FRAMES as i64, i64::from(RATE));
|
||||
let _ = warm_pool.audio_samples(&warm, t, PACKET_FRAMES, RATE);
|
||||
}
|
||||
let boundary = CHANGE_PACKET * PACKET_FRAMES as i64 - DELAY_NS * i64::from(RATE) / 1_000_000_000;
|
||||
let boundary = CHANGE_PACKET * PACKET_FRAMES as i64 - delay_ns * i64::from(RATE) / 1_000_000_000;
|
||||
let t = Tick::from_units(boundary - HALF_WINDOW as i64, i64::from(RATE));
|
||||
let sequential = warm_pool.audio_samples(&warm, t, 2 * HALF_WINDOW, RATE);
|
||||
assert_eq!(sequential.len(), 4 * HALF_WINDOW);
|
||||
|
||||
Reference in New Issue
Block a user