mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-21 20:38:55 +00:00
Seek independent ProRes frames in scanned AVI streams (#4047)
Co-authored-by: Codex <[email protected]>
This commit is contained in:
@@ -264,6 +264,11 @@ impl ContainerFile {
|
||||
}
|
||||
}
|
||||
|
||||
fn scanned_avi_has_picture(&self, track_idx: usize, sample_idx: usize) -> bool {
|
||||
let Self::Avi(file) = self else { return false };
|
||||
file.streams.get(track_idx).is_some_and(|s| !s.keyframes_known && s.chunks.get(sample_idx).is_some_and(|c| c.size != 0))
|
||||
}
|
||||
|
||||
fn sample_pts(&self, track_idx: usize, sample_idx: usize) -> i64 {
|
||||
match self {
|
||||
ContainerFile::Isobmff(f) => f.tracks[track_idx].samples[sample_idx].pts,
|
||||
@@ -1753,7 +1758,10 @@ impl MediaSource for CraftMediaSource {
|
||||
return Ok(hit.frame.clone());
|
||||
}
|
||||
|
||||
let sync_idx = self.file.sync_sample_before(video_idx, target_idx);
|
||||
// A scan cannot identify AVI keyframes, but each nonempty ProRes chunk is
|
||||
// an independent picture. Do not read an earlier, possibly damaged chunk.
|
||||
let direct_avi_prores = state.decoder.codec() == CodecId::ProRes && self.file.scanned_avi_has_picture(video_idx, target_idx);
|
||||
let sync_idx = if direct_avi_prores { target_idx } else { self.file.sync_sample_before(video_idx, target_idx) };
|
||||
let draft_changed = state.decoder.draft() != want_draft;
|
||||
if draft_changed {
|
||||
state.decoder.set_draft(want_draft);
|
||||
@@ -4764,13 +4772,17 @@ mod tests {
|
||||
}
|
||||
|
||||
fn generated_avi_prores(with_alpha: bool, invalid_first: bool) -> Vec<u8> {
|
||||
generated_avi_prores_pictures(with_alpha, invalid_first, &[512])
|
||||
}
|
||||
|
||||
fn generated_avi_prores_pictures(with_alpha: bool, invalid_first: bool, luma: &[u16]) -> Vec<u8> {
|
||||
use craft_video::prores::{ChromaFormat, ColorInfo as ProResColor, Encoder, EncoderConfig, Frame as ProResFrame, Interlace, Profile};
|
||||
|
||||
let mut cfg = EncoderConfig::new(Profile::P4444, 16, 16);
|
||||
cfg.encode_alpha = with_alpha;
|
||||
cfg.threads = false;
|
||||
let mut enc = Encoder::with_config(cfg);
|
||||
let frame = ProResFrame {
|
||||
let mut frame = ProResFrame {
|
||||
width: 16,
|
||||
height: 16,
|
||||
chroma: ChromaFormat::Yuv444,
|
||||
@@ -4784,7 +4796,6 @@ mod tests {
|
||||
aspect_ratio: 0,
|
||||
frame_rate_code: 0,
|
||||
};
|
||||
let pkt = enc.encode(&frame).expect("encode prores");
|
||||
let mut w = avi::AviWriter::new(
|
||||
std::io::Cursor::new(Vec::new()),
|
||||
vec![avi::StreamSpec::Video(avi::VideoSpec::new(*b"ap4h", 16, 16, 24, 1))],
|
||||
@@ -4794,7 +4805,11 @@ mod tests {
|
||||
if invalid_first {
|
||||
w.write_chunk(0, &[0], true).expect("write nonempty invalid first chunk");
|
||||
}
|
||||
w.write_chunk(0, &pkt, true).expect("write prores chunk");
|
||||
for &y in luma {
|
||||
frame.y.fill(y);
|
||||
let pkt = enc.encode(&frame).expect("encode prores");
|
||||
w.write_chunk(0, &pkt, true).expect("write prores chunk");
|
||||
}
|
||||
w.finish().expect("finish avi").into_inner()
|
||||
}
|
||||
|
||||
@@ -4839,6 +4854,64 @@ mod tests {
|
||||
assert!(matches!(frame.data, PixelData::Yuv16 { alpha: Some(_), .. }));
|
||||
}
|
||||
|
||||
// The fixture is a tiny AVI generated by this test. Neutralize only RIFF index
|
||||
// records (not coincidental FourCC strings inside compressed pictures).
|
||||
fn make_generated_avi_indexless(mut bytes: &mut [u8]) {
|
||||
while !bytes.is_empty() {
|
||||
let size = u32::from_le_bytes(bytes[4..8].try_into().expect("generated RIFF length")) as usize;
|
||||
let (chunk, rest) = bytes.split_at_mut(8 + size + size % 2);
|
||||
match &chunk[..4] {
|
||||
b"RIFF" | b"LIST" => make_generated_avi_indexless(&mut chunk[12..8 + size]),
|
||||
b"indx" | b"idx1" | b"ix00" => chunk[..4].copy_from_slice(b"JUNK"),
|
||||
b"avih" => chunk[20] &= !0x10, // Clear AVIF_HASINDEX in dwFlags.
|
||||
_ => {}
|
||||
}
|
||||
bytes = rest;
|
||||
}
|
||||
}
|
||||
|
||||
fn avi_prores_test_picture(source: &crate::stream::MediaSource, time: effectcraft_time::Tick) -> Arc<VideoFrame> {
|
||||
let crate::stream::DecodedFrame::Craft(frame) = source.video_frame(time, 1.0).expect("read shared ProRes picture") else {
|
||||
panic!("expected the shared AVI reader");
|
||||
};
|
||||
assert_eq!(frame.pts.0, time.0);
|
||||
frame
|
||||
}
|
||||
|
||||
fn avi_prores_test_pixels(frame: &VideoFrame) -> (&[u16], &[u16]) {
|
||||
let PixelData::Yuv16 { planes, alpha: Some(alpha), .. } = &frame.data else { panic!("expected a YUV picture with coded alpha") };
|
||||
(&planes[0], alpha)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexless_avi_prores_seeks_independent_pictures_past_a_bad_first_chunk() {
|
||||
const SLOT: i64 = craft_time::TICKS_PER_SECOND / 24;
|
||||
let mut bytes = generated_avi_prores_pictures(true, true, &[512, 768]);
|
||||
let indexed = crate::stream::open_bytes("indexed-prores.avi", Arc::from(bytes.clone())).expect("open indexed control");
|
||||
let first = effectcraft_time::Tick(SLOT);
|
||||
let second = effectcraft_time::Tick(2 * SLOT);
|
||||
let expected_first = avi_prores_test_picture(&indexed, first);
|
||||
let expected_second = avi_prores_test_picture(&indexed, second);
|
||||
assert_ne!(avi_prores_test_pixels(&expected_first).0, avi_prores_test_pixels(&expected_second).0);
|
||||
|
||||
make_generated_avi_indexless(&mut bytes);
|
||||
let demuxed = avi::open(bytes.as_slice()).expect("open scanned AVI");
|
||||
assert_eq!(demuxed.index, avi::IndexSource::Scan);
|
||||
let video = &demuxed.streams[0];
|
||||
assert!(!video.keyframes_known);
|
||||
assert_eq!((video.chunks.len(), video.chunks[0].size), (3, 1));
|
||||
|
||||
let opened = crate::stream::open_bytes("scanned-prores.avi", Arc::from(bytes)).expect("open indexless AVI");
|
||||
let cold = avi_prores_test_picture(&opened, second);
|
||||
assert_eq!(avi_prores_test_pixels(&cold), avi_prores_test_pixels(&expected_second));
|
||||
assert!(opened.video_frame(effectcraft_time::Tick(0), 1.0).is_err(), "the requested corrupt picture remains an error");
|
||||
|
||||
let after_error = avi_prores_test_picture(&opened, second);
|
||||
assert_eq!(avi_prores_test_pixels(&after_error), avi_prores_test_pixels(&expected_second));
|
||||
let backward = avi_prores_test_picture(&opened, first);
|
||||
assert_eq!(avi_prores_test_pixels(&backward), avi_prores_test_pixels(&expected_first));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_prores_alpha_probe_refuses_without_reading() {
|
||||
let mut read_count = 0;
|
||||
|
||||
Reference in New Issue
Block a user