Merge branch 'lane-mp4qt' (e0049a23): QuickTime .mov sound descriptions -- esds inside `wave`, version-2 sample rate
Browse files at this commit3 changed files +264 -9
- crates/ec-mp4/src/demux.rs +112 -8
- crates/ec-mp4/src/lib.rs +8 -0
- crates/ec-mp4/tests/mp4.rs +144 -1
crates/ec-mp4/src/demux.rsmodified+112 -8
| @@ -993,20 +993,34 @@ fn sample_entry(kind: &FourCc, payload: &[u8]) -> Result<Option<Entry>> { | |||
| 993 | 0 => 28, | 993 | 0 => 28, |
| 994 | 1 => 28 + 16, | 994 | 1 => 28 + 16, |
| 995 | _ => { | 995 | _ => { |
| 996 | - // QuickTime's version 2 sound entry states the channel count as a | 996 | + // QuickTime's version 2 sound entry replaces the classic fields |
| 997 | - // 32-bit field of its own; its sample rate is a double, which the | 997 | + // with a block of its own: the channel count is a 32-bit field and |
| 998 | - // `mdhd` timescale says exactly instead. | 998 | + // the sample rate a double, and the fixed 16-bit rate the classic |
| 999 | + // layout holds reads as 65536 for every one of them. | ||
| 999 | audio.layout = | 1000 | audio.layout = |
| 1000 | - ChannelLayout::from_count(be32(payload, 28 + 12).unwrap_or(2).max(1) as usize); | 1001 | + ChannelLayout::from_count(be32(payload, 28 + 4 + 8).unwrap_or(2).max(1) as usize); |
| 1001 | - audio.sample_rate = 0; | 1002 | + audio.sample_rate = be64(payload, 28 + 4) |
| 1003 | + .map(|bits| f64::from_bits(bits).round()) | ||
| 1004 | + .unwrap_or(0.0) | ||
| 1005 | + .clamp(0.0, f64::from(u32::MAX)) as u32; | ||
| 1002 | 28 + 36 | 1006 | 28 + 36 |
| 1003 | } | 1007 | } |
| 1004 | }; | 1008 | }; |
| 1005 | let mut config = None; | 1009 | let mut config = None; |
| 1006 | for child in Boxes::new(payload.get(children..).unwrap_or(&[])) { | 1010 | for child in Boxes::new(payload.get(children..).unwrap_or(&[])) { |
| 1007 | - let (kind, body) = child?; | 1011 | + let (child_kind, body) = child?; |
| 1008 | - config = Some((kind, body)); | 1012 | + // QuickTime keeps the codec configuration one level down, in a `wave` |
| 1009 | - if !matches!(&kind, b"btrt" | b"wave" | b"chan") { | 1013 | + // box beside the `frma` naming the format again: a reader that walks |
| 1014 | + // past `wave` finds no `esds` at all and calls the track silent. | ||
| 1015 | + if &child_kind == b"wave" { | ||
| 1016 | + if let Some(found) = wave_config(kind, body) { | ||
| 1017 | + config = Some(found); | ||
| 1018 | + break; | ||
| 1019 | + } | ||
| 1020 | + continue; | ||
| 1021 | + } | ||
| 1022 | + config = Some((child_kind, body)); | ||
| 1023 | + if !matches!(&child_kind, b"btrt" | b"chan") { | ||
| 1010 | break; | 1024 | break; |
| 1011 | } | 1025 | } |
| 1012 | } | 1026 | } |
| @@ -1078,6 +1092,37 @@ fn sample_entry(kind: &FourCc, payload: &[u8]) -> Result<Option<Entry>> { | |||
| 1078 | })) | 1092 | })) |
| 1079 | } | 1093 | } |
| 1080 | 1094 | ||
| 1095 | +/// The configuration box a `wave` holds for the sample entry `kind`, or | ||
| 1096 | +/// [`None`] where it holds none: `wave`'s other children are the `frma` | ||
| 1097 | +/// restating the format, an ISO-shaped copy of the sample entry itself, and a | ||
| 1098 | +/// zero-sized terminator, and a malformed one ends the walk rather than | ||
| 1099 | +/// erroring the whole track. | ||
| 1100 | +fn wave_config<'a>(kind: &FourCc, wave: &'a [u8]) -> Option<(FourCc, &'a [u8])> { | ||
| 1101 | + let want: &FourCc = match kind { | ||
| 1102 | + b"mp4a" => b"esds", | ||
| 1103 | + b"ac-3" => b"dac3", | ||
| 1104 | + b"ec-3" => b"dec3", | ||
| 1105 | + b"alac" => b"alac", | ||
| 1106 | + b"fLaC" => b"dfLa", | ||
| 1107 | + b"Opus" => b"dOps", | ||
| 1108 | + _ => return None, | ||
| 1109 | + }; | ||
| 1110 | + for child in Boxes::new(wave) { | ||
| 1111 | + let Ok((child_kind, body)) = child else { | ||
| 1112 | + return None; | ||
| 1113 | + }; | ||
| 1114 | + // The terminator: an eight-byte box of type zero, which is where a | ||
| 1115 | + // `wave` ends even when the parent box says there is room for more. | ||
| 1116 | + if child_kind == [0, 0, 0, 0] { | ||
| 1117 | + return None; | ||
| 1118 | + } | ||
| 1119 | + if &child_kind == want { | ||
| 1120 | + return Some((child_kind, body)); | ||
| 1121 | + } | ||
| 1122 | + } | ||
| 1123 | + None | ||
| 1124 | +} | ||
| 1125 | + | ||
| 1081 | fn config_bytes(config: Option<(FourCc, &[u8])>, want: &FourCc) -> Option<Buf> { | 1126 | fn config_bytes(config: Option<(FourCc, &[u8])>, want: &FourCc) -> Option<Buf> { |
| 1082 | config | 1127 | config |
| 1083 | .filter(|(kind, _)| kind == want) | 1128 | .filter(|(kind, _)| kind == want) |
| @@ -1273,6 +1318,65 @@ mod tests { | |||
| 1273 | use super::*; | 1318 | use super::*; |
| 1274 | use crate::boxes; | 1319 | use crate::boxes; |
| 1275 | 1320 | ||
| 1321 | + /// A QuickTime version-2 sound description, which no ffmpeg-written | ||
| 1322 | + /// fixture in this corpus has: its 36-byte block states the sample rate as | ||
| 1323 | + /// a double and the channel count as a 32-bit field, and the `esds` sits | ||
| 1324 | + /// two levels down, inside the `wave`. | ||
| 1325 | + #[test] | ||
| 1326 | + fn quicktime_version_2_sound_entry_reads_its_rate_and_its_wave_esds() { | ||
| 1327 | + fn boxed(kind: &[u8; 4], body: &[u8]) -> Vec<u8> { | ||
| 1328 | + let mut out = ((body.len() + 8) as u32).to_be_bytes().to_vec(); | ||
| 1329 | + out.extend_from_slice(kind); | ||
| 1330 | + out.extend_from_slice(body); | ||
| 1331 | + out | ||
| 1332 | + } | ||
| 1333 | + // esds: an ES descriptor naming AAC-LC with a two-byte config. | ||
| 1334 | + let esds = boxed( | ||
| 1335 | + b"esds", | ||
| 1336 | + &[ | ||
| 1337 | + 0, 0, 0, 0, // FullBox version and flags | ||
| 1338 | + 0x03, 0x16, 0x00, 0x01, 0x00, // ES_Descr | ||
| 1339 | + // DecoderConfigDescr: AAC-LC, audio stream, zero buffer and | ||
| 1340 | + // bitrates, then the config itself. | ||
| 1341 | + 0x04, 0x11, 0x40, 0x15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x05, 0x02, 0x11, | ||
| 1342 | + 0x90, // DecoderSpecificInfo | ||
| 1343 | + ], | ||
| 1344 | + ); | ||
| 1345 | + let mut wave = boxed(b"frma", b"mp4a"); | ||
| 1346 | + wave.extend_from_slice(&boxed(b"mp4a", &[0, 0, 0, 0])); | ||
| 1347 | + wave.extend_from_slice(&esds); | ||
| 1348 | + wave.extend_from_slice(&[0, 0, 0, 8, 0, 0, 0, 0]); // terminator | ||
| 1349 | + | ||
| 1350 | + let mut entry = vec![0u8; 6]; | ||
| 1351 | + entry.extend_from_slice(&1u16.to_be_bytes()); // data_reference_index | ||
| 1352 | + entry.extend_from_slice(&2u16.to_be_bytes()); // version 2 | ||
| 1353 | + entry.extend_from_slice(&[0; 6]); // revision, vendor | ||
| 1354 | + entry.extend_from_slice(&3u16.to_be_bytes()); // "always 3" | ||
| 1355 | + entry.extend_from_slice(&16u16.to_be_bytes()); | ||
| 1356 | + entry.extend_from_slice(&0xFFFEu16.to_be_bytes()); | ||
| 1357 | + entry.extend_from_slice(&[0, 0]); | ||
| 1358 | + entry.extend_from_slice(&0x0001_0000u32.to_be_bytes()); // the fixed 65536 | ||
| 1359 | + entry.extend_from_slice(&72u32.to_be_bytes()); // sizeOfStructOnly | ||
| 1360 | + entry.extend_from_slice(&48_000f64.to_bits().to_be_bytes()); | ||
| 1361 | + entry.extend_from_slice(&2u32.to_be_bytes()); // numAudioChannels | ||
| 1362 | + entry.extend_from_slice(&0x7F00_0000u32.to_be_bytes()); | ||
| 1363 | + entry.extend_from_slice(&[0; 16]); // the four constant LPCM fields | ||
| 1364 | + entry.extend_from_slice(&boxed(b"wave", &wave)); | ||
| 1365 | + | ||
| 1366 | + let read = sample_entry(b"mp4a", &entry) | ||
| 1367 | + .expect("entry parses") | ||
| 1368 | + .expect("mp4a is a codec this build has an id for"); | ||
| 1369 | + let audio = read.audio.expect("a sound entry"); | ||
| 1370 | + assert_eq!(read.codec, CodecId::Aac); | ||
| 1371 | + assert_eq!(audio.sample_rate, 48_000); | ||
| 1372 | + assert_eq!(audio.layout.channel_count(), 2); | ||
| 1373 | + assert_eq!( | ||
| 1374 | + read.extradata.as_deref(), | ||
| 1375 | + Some(&[0x11, 0x90][..]), | ||
| 1376 | + "the AudioSpecificConfig inside wave/esds" | ||
| 1377 | + ); | ||
| 1378 | + } | ||
| 1379 | + | ||
| 1276 | /// The named bug: NTSC film must come back off the sample table as itself. | 1380 | /// The named bug: NTSC film must come back off the sample table as itself. |
| 1277 | #[test] | 1381 | #[test] |
| 1278 | fn ntsc_frame_rates_stay_rational() { | 1382 | fn ntsc_frame_rates_stay_rational() { |
crates/ec-mp4/src/lib.rsmodified+8 -0
| @@ -16,6 +16,14 @@ | |||
| 16 | //! an MP3 track, an `ac-3`/`ec-3`/`alac`/`Opus`/`fLaC` entry is that codec, | 16 | //! an MP3 track, an `ac-3`/`ec-3`/`alac`/`Opus`/`fLaC` entry is that codec, |
| 17 | //! and an entry this build has no [`ec_core::CodecId`] for leaves its track | 17 | //! and an entry this build has no [`ec_core::CodecId`] for leaves its track |
| 18 | //! listed as nothing rather than decoded as something else. | 18 | //! listed as nothing rather than decoded as something else. |
| 19 | +//! - **QuickTime's own sound descriptions are read as such.** A `.mov` out of | ||
| 20 | +//! Resolve, Final Cut or `ffmpeg -f mov` states `mp4a` as a version-1 or | ||
| 21 | +//! version-2 SoundDescription — the second one carrying its sample rate as a | ||
| 22 | +//! double and its channel count as a 32-bit field — and keeps the `esds` | ||
| 23 | +//! inside a `wave` box rather than directly under the entry. Both shapes give | ||
| 24 | +//! the same codec, rate, channels and `AudioSpecificConfig` as the ISO | ||
| 25 | +//! branding of the same stream; a reader that walks past `wave` has a silent | ||
| 26 | +//! sound track instead. | ||
| 19 | //! - **`esds` hands back bytes.** [`Esds::decoder_specific`] is the raw | 27 | //! - **`esds` hands back bytes.** [`Esds::decoder_specific`] is the raw |
| 20 | //! `DecoderSpecificInfo` — an AAC track's `AudioSpecificConfig` verbatim, not | 28 | //! `DecoderSpecificInfo` — an AAC track's `AudioSpecificConfig` verbatim, not |
| 21 | //! a triplet of parsed fields a caller has to reassemble. | 29 | //! a triplet of parsed fields a caller has to reassemble. |
crates/ec-mp4/tests/mp4.rsmodified+144 -1
| @@ -137,11 +137,19 @@ fn mp4_fixtures() -> Vec<PathBuf> { | |||
| 137 | fn derived_fixtures() -> Vec<PathBuf> { | 137 | fn derived_fixtures() -> Vec<PathBuf> { |
| 138 | let h264 = fixtures().join("video/h264-1080p-23.976-8bit.mp4"); | 138 | let h264 = fixtures().join("video/h264-1080p-23.976-8bit.mp4"); |
| 139 | let src = h264.to_string_lossy().into_owned(); | 139 | let src = h264.to_string_lossy().into_owned(); |
| 140 | + let aac = fixtures().join("audio/aac-mp4-stereo-48000.mp4"); | ||
| 141 | + let aac_src = aac.to_string_lossy().into_owned(); | ||
| 140 | let out = work(); | 142 | let out = work(); |
| 141 | let mov = out.join("h264.mov"); | 143 | let mov = out.join("h264.mov"); |
| 144 | + let qt = out.join("aac-quicktime.mov"); | ||
| 142 | let frag = out.join("h264-fragmented.mp4"); | 145 | let frag = out.join("h264-fragmented.mp4"); |
| 143 | - let jobs: [(&PathBuf, Vec<&str>); 2] = [ | 146 | + let jobs: [(&PathBuf, Vec<&str>); 3] = [ |
| 144 | (&mov, vec!["-i", &src, "-c", "copy", "-f", "mov"]), | 147 | (&mov, vec!["-i", &src, "-c", "copy", "-f", "mov"]), |
| 148 | + // The same AAC in a QuickTime-brand file, which states it as a version-1 | ||
| 149 | + // SoundDescription with its `esds` inside a `wave`: what Resolve, Final | ||
| 150 | + // Cut and `ffmpeg -f mov` write, and what an ISO-only reader hears | ||
| 151 | + // nothing from. | ||
| 152 | + (&qt, vec!["-i", &aac_src, "-c", "copy", "-f", "mov"]), | ||
| 145 | ( | 153 | ( |
| 146 | &frag, | 154 | &frag, |
| 147 | vec![ | 155 | vec![ |
| @@ -871,3 +879,138 @@ fn name_of(path: &Path) -> String { | |||
| 871 | let name = path.file_name().unwrap_or_default().to_string_lossy(); | 879 | let name = path.file_name().unwrap_or_default().to_string_lossy(); |
| 872 | name.chars().take(56).collect() | 880 | name.chars().take(56).collect() |
| 873 | } | 881 | } |
| 882 | + | ||
| 883 | +/// The extradata ffprobe reads out of a stream, as bytes: its `-show_data` | ||
| 884 | +/// hexdump, which is the only place it prints the octets themselves. | ||
| 885 | +fn probe_extradata(path: &Path, stream: &str) -> Vec<u8> { | ||
| 886 | + let out = Command::new("ffprobe") | ||
| 887 | + .args([ | ||
| 888 | + "-v", | ||
| 889 | + "error", | ||
| 890 | + "-show_streams", | ||
| 891 | + "-show_data", | ||
| 892 | + "-select_streams", | ||
| 893 | + ]) | ||
| 894 | + .arg(stream) | ||
| 895 | + .arg(path) | ||
| 896 | + .output() | ||
| 897 | + .expect("ffprobe runs"); | ||
| 898 | + assert!(out.status.success(), "ffprobe refused {}", path.display()); | ||
| 899 | + let text = String::from_utf8_lossy(&out.stdout).into_owned(); | ||
| 900 | + let mut bytes = Vec::new(); | ||
| 901 | + let mut inside = false; | ||
| 902 | + for line in text.lines() { | ||
| 903 | + if line.starts_with("extradata=") { | ||
| 904 | + inside = true; | ||
| 905 | + continue; | ||
| 906 | + } | ||
| 907 | + if !inside { | ||
| 908 | + continue; | ||
| 909 | + } | ||
| 910 | + // "00000000: 1190 56e5 00 ..V.." — offset, hex groups, then ASCII. | ||
| 911 | + let Some((_, rest)) = line.split_once(": ") else { | ||
| 912 | + break; | ||
| 913 | + }; | ||
| 914 | + for group in rest.split_whitespace().take(8) { | ||
| 915 | + let Ok(pair) = u32::from_str_radix(group, 16) else { | ||
| 916 | + break; | ||
| 917 | + }; | ||
| 918 | + match group.len() { | ||
| 919 | + 4 => bytes.extend_from_slice(&(pair as u16).to_be_bytes()), | ||
| 920 | + 2 => bytes.push(pair as u8), | ||
| 921 | + _ => break, | ||
| 922 | + } | ||
| 923 | + } | ||
| 924 | + } | ||
| 925 | + bytes | ||
| 926 | +} | ||
| 927 | + | ||
| 928 | +/// A QuickTime-brand `.mov` keeps its `mp4a` configuration in a `wave` box and | ||
| 929 | +/// prefixes the entry with a version-1 SoundDescription tail; an ISO-only | ||
| 930 | +/// reader takes that tail for the first child box, never finds the `esds`, and | ||
| 931 | +/// hands the decoder no extradata at all — a silent sound track. | ||
| 932 | +#[test] | ||
| 933 | +fn quicktime_sound_description_states_its_esds_inside_wave() { | ||
| 934 | + let iso = fixtures().join("audio/aac-mp4-stereo-48000.mp4"); | ||
| 935 | + let qt = work().join("aac-quicktime.mov"); | ||
| 936 | + derived_fixtures(); | ||
| 937 | + | ||
| 938 | + let want = probe_extradata(&qt, "a:0"); | ||
| 939 | + assert!(!want.is_empty(), "ffprobe reads no extradata from the .mov"); | ||
| 940 | + | ||
| 941 | + let demuxer = open(&qt); | ||
| 942 | + let audio = demuxer | ||
| 943 | + .streams() | ||
| 944 | + .iter() | ||
| 945 | + .find(|s| matches!(s.params.media, MediaParameters::Audio(_))) | ||
| 946 | + .expect("the .mov has a sound track"); | ||
| 947 | + let MediaParameters::Audio(params) = &audio.params.media else { | ||
| 948 | + unreachable!() | ||
| 949 | + }; | ||
| 950 | + assert_eq!(audio.params.codec, CodecId::Aac); | ||
| 951 | + assert_eq!(params.sample_rate, 48_000); | ||
| 952 | + assert_eq!(params.layout.channel_count(), 2); | ||
| 953 | + let got = audio | ||
| 954 | + .params | ||
| 955 | + .extradata | ||
| 956 | + .as_ref() | ||
| 957 | + .map(|b| b.as_ref().to_vec()) | ||
| 958 | + .unwrap_or_default(); | ||
| 959 | + assert_eq!(got, want, "AudioSpecificConfig differs from ffprobe's"); | ||
| 960 | + | ||
| 961 | + // ...and it is the same configuration the ISO-branded copy of the same | ||
| 962 | + // stream states, which is the claim "QuickTime is only a different shape". | ||
| 963 | + let iso_demuxer = open(&iso); | ||
| 964 | + let iso_extra = iso_demuxer.streams()[0] | ||
| 965 | + .params | ||
| 966 | + .extradata | ||
| 967 | + .as_ref() | ||
| 968 | + .map(|b| b.as_ref().to_vec()) | ||
| 969 | + .unwrap_or_default(); | ||
| 970 | + assert_eq!(got, iso_extra, "QuickTime and ISO read different extradata"); | ||
| 971 | +} | ||
| 972 | + | ||
| 973 | +/// The real exports this shape was found in: set `EC_MP4_QT_MOV` to a | ||
| 974 | +/// colon-separated list of `.mov` files to check this crate against ffprobe on | ||
| 975 | +/// files that are not in the corpus. | ||
| 976 | +#[test] | ||
| 977 | +fn real_quicktime_exports_demux_like_ffprobe_says() { | ||
| 978 | + let Ok(list) = std::env::var("EC_MP4_QT_MOV") else { | ||
| 979 | + return; | ||
| 980 | + }; | ||
| 981 | + for path in list.split(':').filter(|p| !p.is_empty()).map(Path::new) { | ||
| 982 | + assert!(path.exists(), "{}: not there", path.display()); | ||
| 983 | + let want = probe_extradata(path, "a:0"); | ||
| 984 | + let demuxer = open(path); | ||
| 985 | + let audio = demuxer | ||
| 986 | + .streams() | ||
| 987 | + .iter() | ||
| 988 | + .find(|s| matches!(s.params.media, MediaParameters::Audio(_))) | ||
| 989 | + .unwrap_or_else(|| panic!("{}: no sound track", path.display())); | ||
| 990 | + let MediaParameters::Audio(params) = &audio.params.media else { | ||
| 991 | + unreachable!() | ||
| 992 | + }; | ||
| 993 | + let rate = probe_field(path, "stream=sample_rate") | ||
| 994 | + .into_iter() | ||
| 995 | + .find(|f| !f.is_empty() && f != "N/A") | ||
| 996 | + .and_then(|f| f.trim().parse::<u32>().ok()) | ||
| 997 | + .expect("ffprobe states a sample rate"); | ||
| 998 | + assert_eq!(audio.params.codec, CodecId::Aac, "{}", path.display()); | ||
| 999 | + assert_eq!(params.sample_rate, rate, "{}", path.display()); | ||
| 1000 | + let got = audio | ||
| 1001 | + .params | ||
| 1002 | + .extradata | ||
| 1003 | + .as_ref() | ||
| 1004 | + .map(|b| b.as_ref().to_vec()) | ||
| 1005 | + .unwrap_or_default(); | ||
| 1006 | + assert_eq!(got, want, "{}: extradata", path.display()); | ||
| 1007 | + println!( | ||
| 1008 | + "{}: {:?} {} Hz {} ch, extradata {}", | ||
| 1009 | + path.display(), | ||
| 1010 | + audio.params.codec, | ||
| 1011 | + params.sample_rate, | ||
| 1012 | + params.layout.channel_count(), | ||
| 1013 | + got.len() | ||
| 1014 | + ); | ||
| 1015 | + } | ||
| 1016 | +} | ||