From 315ba37eb4f2e0495306f04f00502cd47da47ee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 06:44:57 +0300 Subject: [PATCH] ec-mp4 QuickTime PCM over 2 channels: write sound description version 2 (lpcm, compression id -2, flags, size 72) as ffmpeg 9 does, instead of a version 1 in24/in32/fl32 with wave; qt_pcm_hdr 5.1 cases expect lpcm --- crates/ec-mp4/src/mux.rs | 62 ++++++++++++++++++++-------- crates/ec-mp4/tests/it/qt_pcm_hdr.rs | 4 +- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/crates/ec-mp4/src/mux.rs b/crates/ec-mp4/src/mux.rs index 08759326..b047d19e 100644 --- a/crates/ec-mp4/src/mux.rs +++ b/crates/ec-mp4/src/mux.rs @@ -1431,33 +1431,59 @@ fn sample_entry( let at = open(out, name); out.extend_from_slice(&[0; 6]); out.extend_from_slice(&1u16.to_be_bytes()); // data_reference_index - // QuickTime sound description version 1 for the PCM wider than 16 bits, - // which is what ffmpeg writes for them in a .mov. + // QuickTime PCM wider than 16 bits: sound description version 1 with a + // `wave`/`enda` up to two channels, and version 2 (`lpcm`) beyond that, + // as ffmpeg writes them in a .mov. let qt = qt_pcm(codec); - out.extend_from_slice(&[0, u8::from(qt.is_some()), 0, 0, 0, 0, 0, 0]); // version, revision, vendor - out.extend_from_slice(&(audio.layout.channel_count() as u16).to_be_bytes()); + let channels = audio.layout.channel_count() as u32; + let v2 = qt.is_some() && channels > 2; + out.extend_from_slice(&[0, if v2 { 2 } else { u8::from(qt.is_some()) }, 0, 0, 0, 0, 0, 0]); // version, revision, vendor + let channel_field: u16 = if v2 { 3 } else { channels as u16 }; + out.extend_from_slice(&channel_field.to_be_bytes()); let bits = if qt.is_some() { 16 } else { audio.bits_per_sample.unwrap_or(16) }; out.extend_from_slice(&(bits as u16).to_be_bytes()); - out.extend_from_slice(&0u16.to_be_bytes()); // pre_defined - out.extend_from_slice(&0u16.to_be_bytes()); // reserved + let pre_defined: u16 = if v2 { 0xFFFE } else { 0 }; + out.extend_from_slice(&pre_defined.to_be_bytes()); // compression id -2 in version 2 + out.extend_from_slice(&0u16.to_be_bytes()); // reserved: packet size // 16.16, so a rate over 65535 is stated as its low half — which is what // every writer does and what the `mdhd` timescale says exactly anyway. - out.extend_from_slice(&((audio.sample_rate as u16 as u32) << 16).to_be_bytes()); - if let Some((bytes, little)) = qt { - let channels = audio.layout.channel_count() as u32; - out.extend_from_slice(&1u32.to_be_bytes()); // samples per packet - out.extend_from_slice(&bytes.to_be_bytes()); // bytes per packet - out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per frame - out.extend_from_slice(&2u32.to_be_bytes()); // bytes per sample (ffmpeg's constant) - let wave = open(out, b"wave"); - leaf(out, b"frma", name); - leaf(out, b"enda", &u16::from(little).to_be_bytes()); - out.extend_from_slice(&[0, 0, 0, 8, 0, 0, 0, 0]); // terminator: an 8-byte box of type zero - close(out, wave); + let rate: u32 = if v2 { + 0x0001_0000 + } else { + (audio.sample_rate as u16 as u32) << 16 + }; + out.extend_from_slice(&rate.to_be_bytes()); + match qt { + Some((bytes, little)) if v2 => { + let float = matches!(codec, CodecId::PcmF32Le | CodecId::PcmF32Be); + let kind: u32 = if float { 1 } else { 4 }; + let order: u32 = if little { 0 } else { 2 }; + let flags = kind | order | 8; + out.extend_from_slice(&72u32.to_be_bytes()); // size of this description + out.extend_from_slice(&f64::from(audio.sample_rate).to_be_bytes()); + out.extend_from_slice(&channels.to_be_bytes()); + out.extend_from_slice(&0x7f00_0000u32.to_be_bytes()); + out.extend_from_slice(&(bytes * 8).to_be_bytes()); // bits per channel + out.extend_from_slice(&flags.to_be_bytes()); + out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per packet + out.extend_from_slice(&1u32.to_be_bytes()); // frames per packet + } + Some((bytes, little)) => { + out.extend_from_slice(&1u32.to_be_bytes()); // samples per packet + out.extend_from_slice(&bytes.to_be_bytes()); // bytes per packet + out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per frame + out.extend_from_slice(&2u32.to_be_bytes()); // bytes per sample (ffmpeg's constant) + let wave = open(out, b"wave"); + leaf(out, b"frma", name); + leaf(out, b"enda", &u16::from(little).to_be_bytes()); + out.extend_from_slice(&[0, 0, 0, 8, 0, 0, 0, 0]); // terminator: an 8-byte box of type zero + close(out, wave); + } + None => {} } match codec { CodecId::Aac => { diff --git a/crates/ec-mp4/tests/it/qt_pcm_hdr.rs b/crates/ec-mp4/tests/it/qt_pcm_hdr.rs index ef10bb9a..71ce9462 100644 --- a/crates/ec-mp4/tests/it/qt_pcm_hdr.rs +++ b/crates/ec-mp4/tests/it/qt_pcm_hdr.rs @@ -130,8 +130,8 @@ const PCM_TESTS: &[PcmTest] = &[ ("pcm_s24le_stereo_mov", "s24le", "pcm_s24le", 2, b"in24", true), ("pcm_s24be_stereo_mov", "s24be", "pcm_s24be", 2, b"in24", true), ("pcm_s24le_mono_mov", "s24m", "pcm_s24le", 1, b"in24", true), - ("pcm_s24le_5_1_mov", "s24-51", "pcm_s24le", 6, b"in24", true), - ("pcm_s24be_5_1_mov", "s24be-51", "pcm_s24be", 6, b"in24", true), + ("pcm_s24le_5_1_mov", "s24-51", "pcm_s24le", 6, b"lpcm", true), + ("pcm_s24be_5_1_mov", "s24be-51", "pcm_s24be", 6, b"lpcm", true), ("pcm_s32le_stereo_mov", "s32le", "pcm_s32le", 2, b"in32", true), ("pcm_s32be_stereo_mov", "s32be", "pcm_s32be", 2, b"in32", true), ("pcm_f32le_stereo_mov", "f32le", "pcm_f32le", 2, b"fl32", true),