Media: footage audio at another sample rate no longer stutters (#274)

AAC audio in an MP4/MOV whose sample rate differs from the mix rate
(a 44.1 kHz clip in the 48 kHz preview or export, or the reverse)
stuttered and sounded low-pitched in previews and in exported movies.

Cause: FilmCraft's MP4 reader, asked for audio at a rate other than
the file's, applied the edit list's priming offset (1024 samples for
AAC) twice when it resampled: each read started about 1024 source
samples early and extrapolated a ramp over its first ~940 samples
(peaks near 100 against a 0.5 tone). Export and the preview mix ask
for audio a frame or a device buffer at a time, so the ramp repeated
dozens of times a second. Same-rate reads take FilmCraft's copy path
and were exact. FilmCraft fixed this upstream in 5bf2203, 87 commits
past the pinned revision.

Fix: MediaPool reads footage audio at the source's own rate and
resamples it itself (linear, by absolute sample position, so a range
comes out the same however it is split into reads), for every
container.

Test: resampled_audio_read_in_frame_pieces_matches_ffmpeg (44.1 kHz
AAC read at 48 kHz and 48 kHz at 44.1 kHz in 29.97 fps frame pieces,
against ffmpeg's decode: max error 44 before, 0.002 after) and
movies_carry_resampled_footage_audio (a 29.97 fps comp with 44.1 kHz
AAC footage exported to MP4/AAC and MOV/PCM, decoded by ffmpeg: max
error 8.7 before, 0.03 / 0.002 after).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bflatastic
2026-10-08 22:16:35 +07:00
co-authored by Claude Opus 5.5
parent 929a9c0f01
commit ec892ef145
3 changed files with 170 additions and 23 deletions
+62
View File
@@ -251,6 +251,68 @@ fn h264_audio_samples() {
assert!(pre.iter().all(|v| *v == 0.0));
}
/// H.264 + AAC at 44.1 kHz in MP4 (ffmpeg writes an edit list that skips the AAC priming):
/// 440 Hz left, a 200 → 1000 Hz sweep right, 29.97 fps.
fn aac_44k_mp4() -> Option<PathBuf> {
ffmpeg_fixture(
"aac_44k.mp4",
&[
"-f",
"lavfi",
"-i",
"testsrc2=size=64x48:rate=30000/1001:duration=3",
"-f",
"lavfi",
"-i",
"aevalsrc=0.5*sin(2*PI*440*t)|0.5*sin(2*PI*(200+400*t/3)*t):s=44100:d=3",
"-c:v",
"libx264",
"-pix_fmt",
"yuv420p",
"-c:a",
"aac",
"-shortest",
],
)
}
/// `src`'s audio decoded by ffmpeg as interleaved stereo `f32` at `rate` Hz (external oracle).
fn ffmpeg_audio(src: &Path, rate: u32) -> Vec<f32> {
let out = Command::new("ffmpeg")
.args(["-hide_banner", "-loglevel", "error", "-i"])
.arg(src)
.args(["-f", "f32le", "-ac", "2", "-ar", &rate.to_string(), "-"])
.output()
.expect("ffmpeg");
assert!(out.status.success(), "ffmpeg failed to decode {}", src.display());
out.stdout.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes(*b)).collect()
}
/// Footage audio at a rate other than the file's, read in one-frame pieces as export and the
/// preview mix do, matches ffmpeg's decode: a resampled MP4 read ramped from a wrong position
/// at the start of every piece (peaks near 100 against a 0.5 tone), so AAC footage stuttered in
/// previews and exports (#274).
#[test]
fn resampled_audio_read_in_frame_pieces_matches_ffmpeg() {
let (Some(a), Some(b)) = (aac_44k_mp4(), h264_mp4()) else { return };
// 44.1 kHz read at 48 kHz, 48 kHz read at 44.1 kHz; NTSC frames are fractional in samples.
for (p, rate) in [(a, 48_000u32), (b, 44_100)] {
let f = probe(&p).expect("probe");
let want = ffmpeg_audio(&p, rate);
let fps = FrameRate::new(30000, 1001);
let pool = MediaPool::new();
let (mut got, mut cursor) = (Vec::new(), 0i64);
for k in 1..=55 {
let end = fps.tick_of(k).to_units_floor(rate as i64);
got.extend(pool.audio_samples(&f, Tick::from_units(cursor, rate as i64), (end - cursor) as usize, rate));
cursor = end;
}
let worst = got.iter().zip(&want).map(|(g, w)| (g - w).abs()).fold(0.0f32, f32::max);
eprintln!("{} at {rate} Hz: max error {worst:.4}", p.display());
assert!(want.len() >= got.len() && worst < 0.01, "{} at {rate} Hz: max error {worst}", p.display());
}
}
#[test]
fn prores_4444_with_alpha() {
let Some(p) = prores_mov() else { return };