Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Merge pull request #129 from carlitosteso3/fix/he-aac-108

Brandon Thomas566164cf7d53
diff --cc crates/codecs/src/mp4.rs
index 110926af9,1bc43f86b..cd152393b
--- a/crates/codecs/src/mp4.rs
+++ b/crates/codecs/src/mp4.rs
@@@ -690,81 -620,41 +707,118 @@@ mod tests
let s = Mp4Source::open("cw2.mov", rotated_mov([0, 2 * ONE, 0, -2 * ONE, 0, 0, 64 * ONE, 0, W])).expect("open");
assert_eq!(s.info().video.as_ref().map(|v| (v.width, v.height)), Some((32, 64)));
}
+
+ #[test]
+ fn hevc_pixel_format_comes_from_hvcc() {
+ for (chroma, bits, want) in [(1, 8, "YUV 4:2:0 8-bit"), (1, 10, "YUV 4:2:0 10-bit"), (2, 8, "YUV 4:2:2 8-bit"), (2, 10, "YUV 4:2:2 10-bit")] {
+ let c = filmcraft_isobmff::HevcConfig { general_profile_idc: 1, chroma_format_idc: chroma, bit_depth_luma: bits, ..Default::default() };
+ assert_eq!(pixfmt_label(&CodecConfig::Hevc(c)), want);
+ }
+ }
+
+ /// A High-profile SPS NAL unit (ITU-T H.264 §7.3.2.1.1) with the given chroma format and bit depth, 16×16.
+ fn high_sps(profile: u8, chroma: u32, bits: u32) -> Vec<u8> {
+ let mut w = filmcraft_bitstream::BitWriter::new();
+ w.write_bits(0x67, 8);
+ w.write_bits(profile as u32, 8);
+ w.write_bits(0, 8); // constraint flags
+ w.write_bits(30, 8); // level
+ w.write_ue(0); // seq_parameter_set_id
+ w.write_ue(chroma);
+ if chroma == 3 {
+ w.write_bit(false); // separate_colour_plane_flag
+ }
+ w.write_ue(bits - 8); // luma
+ w.write_ue(bits - 8); // chroma
+ w.write_bits(0, 2); // no transform bypass, no scaling matrices
+ w.write_ue(0); // log2_max_frame_num_minus4
+ w.write_ue(2); // pic_order_cnt_type
+ w.write_ue(1); // max_num_ref_frames
+ w.write_bit(false); // gaps_in_frame_num_allowed
+ w.write_ue(0); // pic_width_in_mbs_minus1
+ w.write_ue(0); // pic_height_in_map_units_minus1
+ w.write_bits(0b110, 3); // frame_mbs_only, direct_8x8_inference, no cropping
+ w.write_bit(false); // no VUI
+ w.rbsp_trailing();
+ w.finish()
+ }
+
+ #[test]
+ fn avc_pixel_format_comes_from_avcc_not_the_profile() {
+ let avc =
+ |profile: u8, sps: Vec<u8>, ext: Vec<u8>| CodecConfig::Avc(filmcraft_isobmff::AvcConfig { profile, sps: vec![sps], ext, ..Default::default() });
+ // High 4:2:2 at 10 and 8 bits, from the avcC extension (chroma_format 2, bit_depth_luma_minus8)
+ assert_eq!(pixfmt_label(&avc(122, high_sps(122, 2, 10), vec![0xFE, 0xFA, 0xFA, 0])), "YUV 4:2:2 10-bit");
+ assert_eq!(pixfmt_label(&avc(122, high_sps(122, 2, 8), vec![0xFE, 0xF8, 0xF8, 0])), "YUV 4:2:2 8-bit");
+ // no extension (or not a valid one): the SPS says
+ assert_eq!(pixfmt_label(&avc(122, high_sps(122, 2, 10), Vec::new())), "YUV 4:2:2 10-bit");
+ assert_eq!(pixfmt_label(&avc(110, high_sps(110, 1, 10), vec![0, 0, 0, 0])), "YUV 4:2:0 10-bit");
+ assert_eq!(pixfmt_label(&avc(244, high_sps(244, 3, 10), Vec::new())), "YUV 4:4:4 10-bit");
+ // an unreadable SPS and no extension: unknown, not a guess
+ assert_eq!(pixfmt_label(&avc(122, vec![0x67, 122], Vec::new())), "");
+ }
+
+ /// A one-frame 32×16 ProRes MOV with the given profile, chroma format and fourcc.
+ fn prores_mov(profile: filmcraft_prores::Profile, fourcc: &[u8; 4], chroma: filmcraft_prores::ChromaFormat, alpha: bool) -> Arc<[u8]> {
+ let (w, h) = (32u32, 16u32);
+ let fr = filmcraft_prores::Frame::new(w, h, chroma, if chroma == filmcraft_prores::ChromaFormat::Yuv444 { 12 } else { 10 }, alpha);
+ let data = filmcraft_prores::Encoder::new(profile, w, h).encode(&fr).expect("encode");
+ let mut mux = Mp4Writer::new(std::io::Cursor::new(Vec::new()), WriterOptions::new(Brand::Mov)).expect("writer");
+ let t = mux.add_track(TrackConfig::new(SampleEntry::prores(FourCc(*fourcc), w as u16, h as u16), 25)).expect("track");
+ mux.write_sample(t, WriteSample { data: &data, duration: 1, composition_offset: 0, is_sync: true }).expect("sample");
+ mux.finish().expect("finish").into_inner().into()
+ }
+
+ /// `ap4h` / `ap4x` don't imply alpha: the frame header's alpha_channel_type does.
+ #[test]
+ fn prores_alpha_comes_from_the_frame_header() {
+ use filmcraft_prores::{ChromaFormat, Profile};
+ for (profile, fourcc, chroma, alpha, want) in [
+ (Profile::P4444, b"ap4h", ChromaFormat::Yuv444, false, "YUV 4:4:4 12-bit"),
+ (Profile::P4444, b"ap4h", ChromaFormat::Yuv444, true, "YUVA 4:4:4 12-bit"),
+ (Profile::P4444Xq, b"ap4x", ChromaFormat::Yuv444, false, "YUV 4:4:4 12-bit"),
+ (Profile::Hq, b"apch", ChromaFormat::Yuv422, false, "YUV 4:2:2 10-bit"),
+ ] {
+ let s = Mp4Source::open("p.mov", prores_mov(profile, fourcc, chroma, alpha)).expect("open");
+ let v = s.info().video.as_ref().expect("video");
+ assert_eq!((v.pixel_format.as_str(), v.has_alpha), (want, alpha), "{}", String::from_utf8_lossy(fourcc));
+ }
+ }
}
+
+ #[cfg(test)]
+ mod he_aac_tests {
+ use filmcraft_isobmff::{Brand, Mp4Writer, SampleEntry, TrackConfig, WriteSample, WriterOptions};
+ use filmcraft_media::MediaSource;
+
+ /// #108: an HE-AAC MP4 (explicit SBR signalling, core 22.05 kHz, output 44.1 kHz, timescale
+ /// 44.1 kHz) built here from our AAC-LC encoder: the stream has no SBR data, which only the
+ /// high band would need. It was reported at the 22.05 kHz core rate, so sequences made from
+ /// such a clip were created at 22.05 kHz.
+ #[test]
+ fn he_aac_plays_at_the_output_rate() {
+ let core = 22_050u32;
+ let n = core as usize * 2;
+ let tone: Vec<f32> = (0..n).map(|i| 0.25 * (2.0 * std::f32::consts::PI * 1000.0 * i as f32 / core as f32).sin()).collect();
+ let mut enc = filmcraft_aac::Encoder::new(filmcraft_aac::EncoderConfig::cbr(core, 2, 64_000)).unwrap();
+ let mut aus = enc.encode(&[&tone, &tone]);
+ aus.extend(enc.flush());
+ // AOT 5, core 22.05 kHz, stereo, extension 44.1 kHz, core AOT 2
+ let asc = vec![0x2B, 0x92, 0x08, 0x00];
+ let mut mux = Mp4Writer::new(std::io::Cursor::new(Vec::new()), WriterOptions::new(Brand::Mp4)).unwrap();
+ let t = mux.add_track(TrackConfig::new(SampleEntry::aac(asc, 2, 44_100), 44_100)).unwrap();
+ for au in &aus {
+ mux.write_sample(t, WriteSample { data: au, duration: 2048, composition_offset: 0, is_sync: true }).unwrap();
+ }
+ let b = mux.finish().unwrap().into_inner();
+ let src = super::Mp4Source::open("he.mp4", b.into()).unwrap();
+ assert_eq!(src.info().audio.as_ref().unwrap().sample_rate, 44_100);
+ // one second from 0.5 s at 44.1 kHz: the 1 kHz tone at its level (not silence, not shifted)
+ let buf = src.audio(22_050, 44_100, 44_100).unwrap();
+ let x = &buf.channels[0];
+ let peak = x.iter().fold(0f32, |m, v| m.max(v.abs()));
+ assert!((0.2..0.3).contains(&peak), "peak {peak}");
+ let crossings = x.windows(2).filter(|w| w[0] < 0.0 && w[1] >= 0.0).count();
+ assert!((995..=1005).contains(&crossings), "{crossings} Hz");
+ }
+ }