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
Browse files at this commit315ba37eb4f2e0495306f04f00502cd47da47ee3Parent: be888452 changed files +46 -20
crates/ec-mp4/src/mux.rsmodified+44 -18
| @@ -1431,33 +1431,59 @@ fn sample_entry( | ||
| 1431 | 1431 | let at = open(out, name); |
| 1432 | 1432 | out.extend_from_slice(&[0; 6]); |
| 1433 | 1433 | out.extend_from_slice(&1u16.to_be_bytes()); // data_reference_index |
| 1434 | - // QuickTime sound description version 1 for the PCM wider than 16 bits, | |
| 1435 | - // which is what ffmpeg writes for them in a .mov. | |
| 1434 | + // QuickTime PCM wider than 16 bits: sound description version 1 with a | |
| 1435 | + // `wave`/`enda` up to two channels, and version 2 (`lpcm`) beyond that, | |
| 1436 | + // as ffmpeg writes them in a .mov. | |
| 1436 | 1437 | let qt = qt_pcm(codec); |
| 1437 | - out.extend_from_slice(&[0, u8::from(qt.is_some()), 0, 0, 0, 0, 0, 0]); // version, revision, vendor | |
| 1438 | - out.extend_from_slice(&(audio.layout.channel_count() as u16).to_be_bytes()); | |
| 1438 | + let channels = audio.layout.channel_count() as u32; | |
| 1439 | + let v2 = qt.is_some() && channels > 2; | |
| 1440 | + out.extend_from_slice(&[0, if v2 { 2 } else { u8::from(qt.is_some()) }, 0, 0, 0, 0, 0, 0]); // version, revision, vendor | |
| 1441 | + let channel_field: u16 = if v2 { 3 } else { channels as u16 }; | |
| 1442 | + out.extend_from_slice(&channel_field.to_be_bytes()); | |
| 1439 | 1443 | let bits = if qt.is_some() { |
| 1440 | 1444 | 16 |
| 1441 | 1445 | } else { |
| 1442 | 1446 | audio.bits_per_sample.unwrap_or(16) |
| 1443 | 1447 | }; |
| 1444 | 1448 | out.extend_from_slice(&(bits as u16).to_be_bytes()); |
| 1445 | - out.extend_from_slice(&0u16.to_be_bytes()); // pre_defined | |
| 1446 | - out.extend_from_slice(&0u16.to_be_bytes()); // reserved | |
| 1449 | + let pre_defined: u16 = if v2 { 0xFFFE } else { 0 }; | |
| 1450 | + out.extend_from_slice(&pre_defined.to_be_bytes()); // compression id -2 in version 2 | |
| 1451 | + out.extend_from_slice(&0u16.to_be_bytes()); // reserved: packet size | |
| 1447 | 1452 | // 16.16, so a rate over 65535 is stated as its low half — which is what |
| 1448 | 1453 | // every writer does and what the `mdhd` timescale says exactly anyway. |
| 1449 | - out.extend_from_slice(&((audio.sample_rate as u16 as u32) << 16).to_be_bytes()); | |
| 1450 | - if let Some((bytes, little)) = qt { | |
| 1451 | - let channels = audio.layout.channel_count() as u32; | |
| 1452 | - out.extend_from_slice(&1u32.to_be_bytes()); // samples per packet | |
| 1453 | - out.extend_from_slice(&bytes.to_be_bytes()); // bytes per packet | |
| 1454 | - out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per frame | |
| 1455 | - out.extend_from_slice(&2u32.to_be_bytes()); // bytes per sample (ffmpeg's constant) | |
| 1456 | - let wave = open(out, b"wave"); | |
| 1457 | - leaf(out, b"frma", name); | |
| 1458 | - leaf(out, b"enda", &u16::from(little).to_be_bytes()); | |
| 1459 | - out.extend_from_slice(&[0, 0, 0, 8, 0, 0, 0, 0]); // terminator: an 8-byte box of type zero | |
| 1460 | - close(out, wave); | |
| 1454 | + let rate: u32 = if v2 { | |
| 1455 | + 0x0001_0000 | |
| 1456 | + } else { | |
| 1457 | + (audio.sample_rate as u16 as u32) << 16 | |
| 1458 | + }; | |
| 1459 | + out.extend_from_slice(&rate.to_be_bytes()); | |
| 1460 | + match qt { | |
| 1461 | + Some((bytes, little)) if v2 => { | |
| 1462 | + let float = matches!(codec, CodecId::PcmF32Le | CodecId::PcmF32Be); | |
| 1463 | + let kind: u32 = if float { 1 } else { 4 }; | |
| 1464 | + let order: u32 = if little { 0 } else { 2 }; | |
| 1465 | + let flags = kind | order | 8; | |
| 1466 | + out.extend_from_slice(&72u32.to_be_bytes()); // size of this description | |
| 1467 | + out.extend_from_slice(&f64::from(audio.sample_rate).to_be_bytes()); | |
| 1468 | + out.extend_from_slice(&channels.to_be_bytes()); | |
| 1469 | + out.extend_from_slice(&0x7f00_0000u32.to_be_bytes()); | |
| 1470 | + out.extend_from_slice(&(bytes * 8).to_be_bytes()); // bits per channel | |
| 1471 | + out.extend_from_slice(&flags.to_be_bytes()); | |
| 1472 | + out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per packet | |
| 1473 | + out.extend_from_slice(&1u32.to_be_bytes()); // frames per packet | |
| 1474 | + } | |
| 1475 | + Some((bytes, little)) => { | |
| 1476 | + out.extend_from_slice(&1u32.to_be_bytes()); // samples per packet | |
| 1477 | + out.extend_from_slice(&bytes.to_be_bytes()); // bytes per packet | |
| 1478 | + out.extend_from_slice(&(bytes * channels).to_be_bytes()); // bytes per frame | |
| 1479 | + out.extend_from_slice(&2u32.to_be_bytes()); // bytes per sample (ffmpeg's constant) | |
| 1480 | + let wave = open(out, b"wave"); | |
| 1481 | + leaf(out, b"frma", name); | |
| 1482 | + leaf(out, b"enda", &u16::from(little).to_be_bytes()); | |
| 1483 | + out.extend_from_slice(&[0, 0, 0, 8, 0, 0, 0, 0]); // terminator: an 8-byte box of type zero | |
| 1484 | + close(out, wave); | |
| 1485 | + } | |
| 1486 | + None => {} | |
| 1461 | 1487 | } |
| 1462 | 1488 | match codec { |
| 1463 | 1489 | CodecId::Aac => { |
crates/ec-mp4/tests/it/qt_pcm_hdr.rsmodified+2 -2
| @@ -130,8 +130,8 @@ const PCM_TESTS: &[PcmTest] = &[ | ||
| 130 | 130 | ("pcm_s24le_stereo_mov", "s24le", "pcm_s24le", 2, b"in24", true), |
| 131 | 131 | ("pcm_s24be_stereo_mov", "s24be", "pcm_s24be", 2, b"in24", true), |
| 132 | 132 | ("pcm_s24le_mono_mov", "s24m", "pcm_s24le", 1, b"in24", true), |
| 133 | - ("pcm_s24le_5_1_mov", "s24-51", "pcm_s24le", 6, b"in24", true), | |
| 134 | - ("pcm_s24be_5_1_mov", "s24be-51", "pcm_s24be", 6, b"in24", true), | |
| 133 | + ("pcm_s24le_5_1_mov", "s24-51", "pcm_s24le", 6, b"lpcm", true), | |
| 134 | + ("pcm_s24be_5_1_mov", "s24be-51", "pcm_s24be", 6, b"lpcm", true), | |
| 135 | 135 | ("pcm_s32le_stereo_mov", "s32le", "pcm_s32le", 2, b"in32", true), |
| 136 | 136 | ("pcm_s32be_stereo_mov", "s32be", "pcm_s32be", 2, b"in32", true), |
| 137 | 137 | ("pcm_f32le_stereo_mov", "f32le", "pcm_f32le", 2, b"fl32", true), |