f67d38b35cc5
tests.rs
#[test]
fn open_gop_sync_walks_a_long_chain_of_leading_pictures_without_recursing() {
// Every GOP of 3 starts at a key whose leading picture is shown before it (#2803).
let gops = 200_000usize;
let sync = |i: usize| i - i % 3;
let leads = |i: usize, key: usize| i == key + 2;
assert_eq!(super::open_gop_sync(gops * 3 - 1, sync, leads), 0);
// A picture that is not a leading one keeps its own key.
assert_eq!(super::open_gop_sync(7, sync, |_, _| false), 6);
// An index whose sync sample lies past `i` ends the walk instead of looping.
assert_eq!(super::open_gop_sync(5, |_| 9, |_, _| true), 9);
}
use super::*;
use crate::video::DecodedFrame;
use std::collections::VecDeque;
/// `n` samples, a sync sample every `gop`, pts = 1000 · index (no reordering).
struct Samples {
n: usize,
gop: usize,
delay: usize,
intra: bool,
resets: Arc<AtomicUsize>,
decodes: Arc<AtomicUsize>,
}
/// Outputs each picture `delay` samples late, like a frame-threaded decoder.
struct Dec {
delay: usize,
intra: bool,
/// (pts, decoded as a draft picture)
held: VecDeque<(i64, bool)>,
draft: bool,
/// made for background work (few threads, like the H.264 decoder's)
limited: bool,
resets: Arc<AtomicUsize>,
decodes: Arc<AtomicUsize>,
}
/// A picture whose pixel encodes its index (and, in blue, whether it is a draft picture).
fn picture((pts, draft): (i64, bool)) -> DecodedFrame {
let i = (pts / 1000) as u32;
DecodedFrame { pts, frame: VideoFrame::rgba8(1, 1, vec![i as u8, (i >> 8) as u8, draft as u8, 255]), draft }
}
impl VideoDecoder for Dec {
fn decode(&mut self, sample: &[u8], pts: i64) -> crate::Result<Vec<DecodedFrame>> {
self.decodes.fetch_add(1, Ordering::Relaxed);
// like H.264 draft mode: only non-reference (odd) pictures are approximate
self.held.push_back((pts, self.draft && sample[0] % 2 == 1));
let mut out = Vec::new();
while self.held.len() > self.delay {
out.push(picture(self.held.pop_front().expect("held")));
}
Ok(out)
}
fn flush(&mut self) -> Vec<DecodedFrame> {
self.held.drain(..).map(picture).collect()
}
fn reset(&mut self) {
self.resets.fetch_add(1, Ordering::Relaxed);
self.held.clear();
}
fn set_draft(&mut self, on: bool) {
self.draft = on;
}
fn name(&self) -> &str {
"test"
}
fn thread_limited(&self) -> bool {
self.limited
}
fn intra_only(&self) -> bool {
self.intra
}
// odd samples are non-reference pictures
fn is_disposable(&self, sample: &[u8]) -> bool {
sample[0] % 2 == 1
}
}
impl VideoSamples for Samples {
fn count(&self) -> usize {
self.n
}
fn pts(&self, i: usize) -> i64 {
i as i64 * 1000
}
fn sync_before(&self, i: usize) -> usize {
if self.intra { i } else { i / self.gop * self.gop }
}
fn sample_at(&self, t: i64) -> Option<usize> {
let i = (t / 1000) as usize;
(i < self.n).then_some(i)
}
fn read(&self, i: usize) -> crate::Result<Vec<u8>> {
Ok(vec![i as u8])
}
fn make_decoder(&self) -> crate::Result<Box<dyn VideoDecoder>> {
Ok(Box::new(Dec {
delay: self.delay,
intra: self.intra,
held: VecDeque::new(),
draft: false,
limited: filmcraft_media::cancel::background(),
resets: self.resets.clone(),
decodes: self.decodes.clone(),
}))
}
}
fn samples(n: usize, gop: usize, delay: usize, intra: bool) -> Samples {
Samples { n, gop, delay, intra, resets: Default::default(), decodes: Default::default() }
}
fn index_of(f: &VideoFrame) -> usize {
match &f.data {
filmcraft_frame::PixelData::Rgba8(d) => d[0] as usize | (d[1] as usize) << 8,
_ => unreachable!(),
}
}
#[test]
fn frames_held_by_a_threaded_decoder_do_not_restart_it() {
// 24 pictures in flight (more than any fixed look-ahead margin), one long GOP.
let s = samples(300, 250, 24, false);
let c = GopCache::new(None);
// Playback order with prefetch running ahead: a later frame first, then earlier ones the
// decoder has been fed but not output yet.
for i in [10usize, 11, 40, 12, 30, 13, 60, 14, 15] {
assert_eq!(index_of(&c.frame(&s, i as i64 * 1000).expect("frame")), i);
}
assert_eq!(s.resets.load(Ordering::Relaxed), 1, "only the first request seeks");
assert!(s.decodes.load(Ordering::Relaxed) <= 60 + 24 + 1);
}
/// A decoder that, like a slice-threaded one, splits each picture's work over rayon.
struct ParDec(Box<dyn VideoDecoder>);
impl VideoDecoder for ParDec {
fn decode(&mut self, sample: &[u8], pts: i64) -> crate::Result<Vec<DecodedFrame>> {
use rayon::prelude::*;
let rows: u64 = (0..64u32)
.into_par_iter()
.with_max_len(1)
.map(|r| (0..20_000u64).fold(u64::from(r), |a, x| a.wrapping_mul(31).wrapping_add(x)))
.reduce(|| 0, u64::wrapping_add);
std::hint::black_box(rows);
self.0.decode(sample, pts)
}
fn flush(&mut self) -> Vec<DecodedFrame> {
self.0.flush()
}
fn reset(&mut self) {
self.0.reset()
}
fn name(&self) -> &str {
"par"
}
}
struct ParSamples(Samples);
impl VideoSamples for ParSamples {
fn count(&self) -> usize {
self.0.count()
}
fn pts(&self, i: usize) -> i64 {
self.0.pts(i)
}
fn sync_before(&self, i: usize) -> usize {
self.0.sync_before(i)
}
fn sample_at(&self, t: i64) -> Option<usize> {
self.0.sample_at(t)
}
fn read(&self, i: usize) -> crate::Result<Vec<u8>> {
self.0.read(i)
}
fn make_decoder(&self) -> crate::Result<Box<dyn VideoDecoder>> {
Ok(Box::new(ParDec(self.0.make_decoder()?)))
}
}
/// #480: render jobs on rayon asking one source for frames while its decoding (a decoder's
/// slices, rotating the frames) runs on rayon too. A worker that took part of that work and,
/// waiting inside it, picked up another frame request blocked on the cache's lock for good:
/// an export of rotated iPhone clips stopped making frames.
#[test]
fn rayon_frame_requests_while_decoding_on_rayon_never_deadlock() {
use rayon::prelude::*;
let s = Arc::new(ParSamples(samples(240, 30, 4, false)));
let (tx, rx) = std::sync::mpsc::channel();
let done = Arc::new(AtomicUsize::new(0));
let counted = done.clone();
std::thread::spawn(move || {
for round in 0..40usize {
// export-like: many frame workers, frames of several GOPs of a fresh source at once
let c = GopCache::new(None).with_rotation(1);
(0..96usize).into_par_iter().with_max_len(1).for_each(|k| {
let i = (k * 37 + round * 53) % 240;
assert_eq!(index_of(&c.frame(&*s, i as i64 * 1000).expect("frame")), i);
counted.fetch_add(1, Ordering::Relaxed);
});
}
let _ = tx.send(());
});
// A deadlock stops every request of a round for good. On a loaded machine the 3,840
// requests can take minutes, so the limit applies to the time since the last answered one.
if let Err(frames) = wait_while_answered(&rx, &done, Duration::from_secs(120)) {
panic!("frame requests deadlocked after {frames} frames");
}
}
/// Waits for `rx` while `done` keeps growing. Returns `Err` with the count in `done` once
/// `limit` has passed since the count last grew (or since the call, before it grows).
fn wait_while_answered(rx: &std::sync::mpsc::Receiver<()>, done: &AtomicUsize, limit: Duration) -> Result<(), usize> {
let mut answered = done.load(Ordering::Relaxed);
let mut last_answer = Instant::now();
loop {
match rx.recv_timeout(limit / 120) {
Ok(()) => return Ok(()),
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
let now = done.load(Ordering::Relaxed);
if now > answered {
answered = now;
last_answer = Instant::now();
} else if last_answer.elapsed() >= limit {
return Err(now);
}
}
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => panic!("a frame request failed"),
}
}
}
#[test]
fn a_stall_right_after_an_answer_is_reported_once_the_limit_has_passed() {
let (_tx, rx) = std::sync::mpsc::channel();
let done = Arc::new(AtomicUsize::new(0));
let answering = done.clone();
let (answer_tx, answer_rx) = std::sync::mpsc::channel();
let limit = Duration::from_secs(2);
std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(50));
let answer = Instant::now();
answering.fetch_add(1, Ordering::Relaxed);
let _ = answer_tx.send(answer);
});
assert_eq!(wait_while_answered(&rx, &done, limit), Err(1));
let waited = answer_rx.recv().expect("answer time").elapsed();
assert!(waited >= limit && waited < limit * 3 / 2, "the stall was reported {waited:?} after the answer");
}
#[test]
fn running_decoder_continues_through_a_nearby_sync_sample() {
let s = samples(200, 30, 0, false);
let c = GopCache::new(None);
assert_eq!(index_of(&c.frame(&s, 25_000).expect("frame")), 25);
// frame 40 lies in the next GOP (sync sample 30): decode 26..40 instead of restarting at 30
assert_eq!(index_of(&c.frame(&s, 40_000).expect("frame")), 40);
assert_eq!(index_of(&c.frame(&s, 28_000).expect("frame")), 28);
assert_eq!(s.resets.load(Ordering::Relaxed), 1);
// far ahead: restart at the sync sample rather than decode everything in between
assert_eq!(index_of(&c.frame(&s, 150_000).expect("frame")), 150);
assert_eq!(s.resets.load(Ordering::Relaxed), 2);
assert!(s.decodes.load(Ordering::Relaxed) <= 41 + 1);
}
#[test]
fn intra_frames_decode_in_parallel_once_each() {
let s = Arc::new(samples(64, 1, 0, true));
let c = Arc::new(GopCache::new(None));
// First decode 64 distinct cold frames: each of the four workers owns
// 16 indices. Multiplication by seven permutes them without overlap.
// Then let every worker request all 64 cached frames in overlapping order.
// Concurrent misses for the same cold frame may legitimately decode twice,
// so they cannot support a scheduler-independent duplicate allowance.
for requests_per_worker in [16usize, 64] {
let start = Arc::new(std::sync::Barrier::new(4));
std::thread::scope(|scope| {
for t in 0..4usize {
let (s, c, start) = (s.clone(), c.clone(), start.clone());
scope.spawn(move || {
start.wait();
for k in 0..requests_per_worker {
let i = (k * 7 + t * 16) % 64;
assert_eq!(index_of(&c.frame(&*s, i as i64 * 1000).expect("frame")), i);
}
});
}
});
assert_eq!(s.decodes.load(Ordering::Relaxed), 64, "cold frames decode once; warm requests hit the cache");
}
assert_eq!(s.resets.load(Ordering::Relaxed), 0);
}
#[test]
fn catching_up_skips_late_non_reference_samples_only() {
let s = samples(300, 250, 4, false);
let c = GopCache::new(None);
assert_eq!(index_of(&c.frame(&s, 2_000).expect("frame")), 2);
let before = s.decodes.load(Ordering::Relaxed);
// playback is behind: frame 41 is due, 7..=40 are late
let f = c.frame_late(&s, 41_000, Some(41_000)).expect("frame");
assert_eq!(index_of(&f), 41, "the wanted (odd, disposable) frame itself is decoded");
let fed = s.decodes.load(Ordering::Relaxed) - before;
// samples 7..41 minus the odd ones before 41, plus the decoder's output delay
assert!(fed <= 18 + 4 + 1, "fed {fed}");
// the decoder continues from there: the next frames are decoded, not skipped
for i in 42..48usize {
assert_eq!(index_of(&c.frame(&s, i as i64 * 1000).expect("frame")), i);
}
assert_eq!(s.resets.load(Ordering::Relaxed), 1);
// without the hint nothing is skipped
let s2 = samples(300, 250, 4, false);
let c2 = GopCache::new(None);
assert_eq!(index_of(&c2.frame(&s2, 41_000).expect("frame")), 41);
assert!(s2.decodes.load(Ordering::Relaxed) >= 42);
// a prefetch job for frame 41 while frame 30 is due: only frames before 30 are late
let s3 = samples(300, 250, 0, false);
let c3 = GopCache::new(None);
assert_eq!(index_of(&c3.frame_late(&s3, 41_000, Some(30_000)).expect("frame")), 41);
assert_eq!(s3.decodes.load(Ordering::Relaxed), 42 - 15, "odd samples 1..=29 skipped");
for i in 30..41usize {
assert_eq!(index_of(&c3.frame(&s3, i as i64 * 1000).expect("cached")), i);
}
assert_eq!(s3.resets.load(Ordering::Relaxed), 1);
}
fn is_draft(f: &VideoFrame) -> bool {
match &f.data {
filmcraft_frame::PixelData::Rgba8(d) => d[2] == 1,
_ => unreachable!(),
}
}
#[test]
fn draft_frames_are_served_to_draft_requests_only() {
let s = samples(300, 250, 3, false);
let c = GopCache::new(None);
let before = gop_stats().draft;
// reduced-resolution playback: odd (non-reference) pictures come out as drafts
for i in 0..12usize {
let f = filmcraft_media::cancel::with_draft(true, || c.frame(&s, i as i64 * 1000)).expect("frame");
assert_eq!(index_of(&f), i);
assert_eq!(is_draft(&f), i % 2 == 1, "frame {i}");
}
assert!(gop_stats().draft - before >= 6);
assert_eq!(s.resets.load(Ordering::Relaxed), 1);
// paused / export (no draft hint): an even frame is exact and cached
let f = c.frame(&s, 4_000).expect("frame");
assert!(!is_draft(&f) && index_of(&f) == 4);
assert_eq!(s.resets.load(Ordering::Relaxed), 1, "exact frames are served from the cache");
// a draft frame is decoded again, exactly
let f = c.frame(&s, 5_000).expect("frame");
assert!(!is_draft(&f) && index_of(&f) == 5);
assert_eq!(s.resets.load(Ordering::Relaxed), 2, "re-decoded from the sync sample");
// and a draft request takes the exact frame now cached
let f = filmcraft_media::cancel::with_draft(true, || c.frame(&s, 5_000)).expect("frame");
assert!(!is_draft(&f));
// without the hint nothing is ever a draft
let s2 = samples(300, 250, 3, false);
let c2 = GopCache::new(None);
assert!((0..12).all(|i| !is_draft(&c2.frame(&s2, i * 1000).expect("frame"))));
}
#[test]
fn cancelled_request_stops_decoding_and_keeps_state() {
let s = samples(300, 250, 0, false);
let c = GopCache::new(None);
assert_eq!(index_of(&c.frame(&s, 5_000).expect("frame")), 5);
let flag = Arc::new(std::sync::atomic::AtomicBool::new(true));
let r = filmcraft_media::cancel::with_cancel(&flag, || c.frame(&s, 200_000));
assert!(matches!(r, Err(CodecError::Cancelled)));
assert_eq!(s.decodes.load(Ordering::Relaxed), 6);
// cached frames are still served, and the decoder carries on from where it was
let r = filmcraft_media::cancel::with_cancel(&flag, || c.frame(&s, 3_000));
assert_eq!(index_of(&r.expect("cached")), 3);
assert_eq!(index_of(&c.frame(&s, 7_000).expect("frame")), 7);
assert_eq!(s.resets.load(Ordering::Relaxed), 1);
}
#[test]
fn idle_caches_give_up_their_decoders_beyond_the_cap() {
let pool = Arc::new(Pool::new(2, Duration::ZERO, FRAME_BUDGET));
let live = || pool.decoders.load(Ordering::Relaxed);
let s: Vec<Samples> = (0..5).map(|_| samples(300, 250, 3, false)).collect();
let c: Vec<GopCache> = (0..5).map(|_| GopCache::in_pool(None, pool.clone())).collect();
// a clip after another, as when clips are dropped on a timeline
for k in 0..5 {
assert_eq!(index_of(&c[k].frame(&s[k], 10_000).expect("frame")), 10);
assert!(live() <= 2, "{} live decoders after clip {k}", live());
}
assert_eq!(live(), 2);
// the most recently used keep theirs and carry on without a seek
assert_eq!(index_of(&c[4].frame(&s[4], 20_000).expect("frame")), 20);
assert_eq!(s[4].resets.load(Ordering::Relaxed), 1);
// a cache that lost its decoder still serves what it cached…
let decodes = s[0].decodes.load(Ordering::Relaxed);
assert_eq!(index_of(&c[0].frame(&s[0], 10_000).expect("cached")), 10);
assert_eq!(s[0].decodes.load(Ordering::Relaxed), decodes);
// …and decodes again with a new decoder, from the sync sample
assert_eq!(index_of(&c[0].frame(&s[0], 20_000).expect("frame")), 20);
assert_eq!(s[0].resets.load(Ordering::Relaxed), 2);
assert_eq!(live(), 2);
// a dropped cache no longer counts
drop(c);
assert_eq!(live(), 0);
}
#[test]
fn caches_in_use_keep_their_decoders_whatever_the_cap() {
// every layer of one composited frame was asked moments ago: none is idle
let pool = Arc::new(Pool::new(2, Duration::from_secs(3600), FRAME_BUDGET));
let s: Vec<Samples> = (0..5).map(|_| samples(300, 250, 3, false)).collect();
let c: Vec<GopCache> = (0..5).map(|_| GopCache::in_pool(None, pool.clone())).collect();
for t in [10_000, 11_000, 12_000] {
for k in 0..5 {
assert_eq!(index_of(&c[k].frame(&s[k], t).expect("frame")) as i64, t / 1000);
}
}
assert_eq!(pool.decoders.load(Ordering::Relaxed), 5);
assert!(s.iter().all(|s| s.resets.load(Ordering::Relaxed) == 1), "no decoder restarted");
}
/// A burst of navigation (every clip of a timeline asked once within moments, as when
/// thumbnails are made or the playhead jumps clip to clip) must not keep every clip's decoder
/// and frames: only the caches of the frame being composited are in use.
#[test]
fn a_burst_of_single_requests_does_not_keep_every_decoder_and_frame() {
// the real decoder caps and idle time; RECENT scaled down from 500 ms to the test's pace
// (3 ms per clip). With the old rule (recent = idle) all 24 decoders stay live.
let pool = Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET).tiered(Duration::from_millis(2), HARD_DECODERS));
let n = 24;
let s: Vec<Samples> = (0..n).map(|_| samples(300, 250, 3, false)).collect();
let c: Vec<GopCache> = (0..n).map(|_| GopCache::in_pool(None, pool.clone())).collect();
for k in 0..n {
assert_eq!(index_of(&c[k].frame(&s[k], 10_000).expect("frame")), 10);
std::thread::sleep(Duration::from_millis(3));
}
assert!(pool.decoders.load(Ordering::Relaxed) <= HARD_DECODERS + 1, "{} live decoders after a burst over {n} clips", pool.decoders.load(Ordering::Relaxed));
// the clip asked last, and a clip in use together with it, keep theirs and carry on
let resets = s[n - 1].resets.load(Ordering::Relaxed);
assert_eq!(index_of(&c[n - 1].frame(&s[n - 1], 11_000).expect("frame")), 11);
assert_eq!(s[n - 1].resets.load(Ordering::Relaxed), resets);
}
/// The thumbnails an import queues: every clip asked once, quickly (the real caps and recency,
/// no sleeping: all of them are "recent"). Each 4K software decoder costs ~0.5 GB, and 42
/// clips used to hold 6+ GB at once.
#[test]
fn background_requests_leave_no_decoders_behind() {
use filmcraft_media::cancel::with_background;
let pool = Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET).tiered(RECENT, HARD_DECODERS));
let n = 24;
let s: Vec<Samples> = (0..n).map(|_| samples(300, 250, 3, false)).collect();
let c: Vec<GopCache> = (0..n).map(|_| GopCache::in_pool(None, pool.clone())).collect();
// a clip in use by a monitor
assert_eq!(index_of(&c[0].frame(&s[0], 10_000).expect("frame")), 10);
for k in 1..n {
assert_eq!(index_of(&with_background(true, || c[k].frame(&s[k], 10_000)).expect("frame")), 10);
assert!(pool.decoders.load(Ordering::Relaxed) <= 2, "{} live decoders after thumbnail {k}", pool.decoders.load(Ordering::Relaxed));
}
// the monitor's clip and the last thumbnail keep theirs and carry on without a seek
assert_eq!(pool.decoders.load(Ordering::Relaxed), 2);
assert_eq!(index_of(&c[0].frame(&s[0], 11_000).expect("frame")), 11);
assert_eq!(s[0].resets.load(Ordering::Relaxed), 1);
assert_eq!(index_of(&with_background(true, || c[n - 1].frame(&s[n - 1], 12_000)).expect("frame")), 12);
assert_eq!(s[n - 1].resets.load(Ordering::Relaxed), 1);
// a monitor asking for a frame of a thumbnail's clip makes its decoder one in use
assert_eq!(index_of(&c[n - 1].frame(&s[n - 1], 13_000).expect("frame")), 13);
for k in 1..4 {
with_background(true, || c[k].frame(&s[k], 20_000)).expect("frame");
}
assert_eq!(index_of(&c[n - 1].frame(&s[n - 1], 14_000).expect("frame")), 14);
assert!(c[n - 1].shared.state.lock().unwrap().decoder.is_some(), "the thumbnails left its decoder");
// a cache that lost its decoder still decodes (from the sync sample)
assert_eq!(index_of(&with_background(true, || c[1].frame(&s[1], 30_000)).expect("frame")), 30);
}
#[test]
fn a_monitor_replaces_a_decoder_made_for_a_thumbnail() {
use filmcraft_media::cancel::with_background;
let pool = Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET).tiered(RECENT, HARD_DECODERS));
let (s, c) = (samples(300, 250, 3, false), GopCache::in_pool(None, pool.clone()));
assert_eq!(index_of(&with_background(true, || c.frame(&s, 10_000)).expect("frame")), 10);
// another thumbnail of the same clip carries on with the thumbnail decoder
assert_eq!(index_of(&with_background(true, || c.frame(&s, 12_000)).expect("frame")), 12);
assert_eq!(s.resets.load(Ordering::Relaxed), 1);
// a frame a monitor waits for (not cached: far ahead) is decoded by a full-thread decoder
assert_eq!(index_of(&c.frame(&s, 100_000).expect("frame")), 100);
assert!(!c.shared.state.lock().unwrap().decoder.as_ref().is_some_and(|d| d.thread_limited()));
assert_eq!(pool.decoders.load(Ordering::Relaxed), 1);
// and keeps it for the next frames
let resets = s.resets.load(Ordering::Relaxed);
assert_eq!(index_of(&c.frame(&s, 101_000).expect("frame")), 101);
assert_eq!(s.resets.load(Ordering::Relaxed), resets);
}
#[test]
fn a_burst_of_single_requests_does_not_keep_every_clips_frames() {
// 1×1 RGBA pictures (4 bytes): a shared budget of ten of them
let pool = Arc::new(Pool::new(usize::MAX, Duration::from_secs(3600), 40).tiered(Duration::from_millis(2), usize::MAX));
let n = 12;
let s: Vec<Samples> = (0..n).map(|_| samples(300, 250, 0, false)).collect();
let c: Vec<GopCache> = (0..n).map(|_| GopCache::in_pool(None, pool.clone())).collect();
for k in 0..n {
assert_eq!(index_of(&c[k].frame(&s[k], 10_000).expect("frame")), 10);
std::thread::sleep(Duration::from_millis(5));
}
let held = pool.bytes.load(Ordering::Relaxed);
assert!(held <= 40 + 11 * 4, "{held} bytes held after a burst over {n} clips");
}
#[test]
fn intra_caches_give_up_their_spare_decoders_too() {
let pool = Arc::new(Pool::new(1, Duration::ZERO, FRAME_BUDGET));
let (a, b) = (samples(50, 1, 0, true), samples(50, 1, 0, true));
let (ca, cb) = (GopCache::in_pool(None, pool.clone()), GopCache::in_pool(None, pool.clone()));
assert_eq!(index_of(&ca.frame(&a, 3_000).expect("frame")), 3);
assert_eq!(index_of(&cb.frame(&b, 4_000).expect("frame")), 4);
let st = ca.shared.state.lock().unwrap();
assert!(st.decoder.is_none() && st.spare.is_empty());
drop(st);
assert_eq!(pool.decoders.load(Ordering::Relaxed), 1);
assert_eq!(index_of(&ca.frame(&a, 5_000).expect("frame")), 5);
}
#[test]
fn idle_caches_give_up_their_frames_beyond_the_shared_budget() {
// 1×1 RGBA pictures: 4 bytes each, a shared budget of ten of them
let pool = Arc::new(Pool::new(usize::MAX, Duration::ZERO, 40));
let held = || pool.bytes.load(Ordering::Relaxed);
let (a, b) = (samples(300, 250, 0, false), samples(300, 250, 0, false));
let (ca, cb) = (GopCache::in_pool(None, pool.clone()), GopCache::in_pool(None, pool.clone()));
// the only cache keeps what its own budget allows
assert_eq!(index_of(&ca.frame(&a, 10_000).expect("frame")), 10);
assert_eq!(held(), 44);
// a second clip takes the room from the idle first one, earliest frames first
assert_eq!(index_of(&cb.frame(&b, 5_000).expect("frame")), 5);
assert_eq!(held(), 40);
let decodes = b.decodes.load(Ordering::Relaxed);
assert!((0..=5).all(|i| index_of(&cb.frame(&b, i * 1000).expect("cached")) as i64 == i));
assert_eq!(b.decodes.load(Ordering::Relaxed), decodes, "the clip in use lost nothing");
let st = ca.shared.state.lock().unwrap();
assert_eq!((st.frames.len(), st.bytes), (4, 16));
assert_eq!(st.frames.keys().next(), Some(&7_000));
drop(st);
// an evicted frame decodes again
assert_eq!(index_of(&ca.frame(&a, 2_000).expect("frame")), 2);
assert!(held() <= 40 + 4 * 3, "{} bytes held", held());
drop((ca, cb));
assert_eq!(held(), 0);
}
#[test]
fn caches_in_use_keep_their_frames_whatever_the_shared_budget() {
let pool = Arc::new(Pool::new(usize::MAX, Duration::from_secs(3600), 40));
let (a, b) = (samples(300, 250, 0, false), samples(300, 250, 0, false));
let (ca, cb) = (GopCache::in_pool(None, pool.clone()), GopCache::in_pool(None, pool.clone()));
assert_eq!(index_of(&ca.frame(&a, 10_000).expect("frame")), 10);
assert_eq!(index_of(&cb.frame(&b, 10_000).expect("frame")), 10);
assert_eq!(pool.bytes.load(Ordering::Relaxed), 88);
let decodes = a.decodes.load(Ordering::Relaxed);
assert_eq!(index_of(&ca.frame(&a, 0).expect("cached")), 0);
assert_eq!(a.decodes.load(Ordering::Relaxed), decodes);
}
/// Pictures big enough for the plane pool (its own size, so that parallel tests cannot take
/// the buffers), no reordering, one long GOP.
struct Big(usize);
const BIG: (u32, u32) = (200, 101);
impl VideoDecoder for Big {
fn decode(&mut self, _sample: &[u8], pts: i64) -> crate::Result<Vec<DecodedFrame>> {
let mut px = filmcraft_frame::pool::take_u8((BIG.0 * BIG.1 * 4) as usize);
px.resize((BIG.0 * BIG.1 * 4) as usize, (pts / 1000) as u8);
Ok(vec![DecodedFrame { pts, frame: VideoFrame::rgba8(BIG.0, BIG.1, px), draft: false }])
}
fn flush(&mut self) -> Vec<DecodedFrame> {
Vec::new()
}
fn reset(&mut self) {}
fn name(&self) -> &str {
"big"
}
}
impl VideoSamples for Big {
fn count(&self) -> usize {
self.0
}
fn pts(&self, i: usize) -> i64 {
i as i64 * 1000
}
fn sync_before(&self, _i: usize) -> usize {
0
}
fn sample_at(&self, t: i64) -> Option<usize> {
let i = (t / 1000) as usize;
(i < self.0).then_some(i)
}
fn read(&self, i: usize) -> crate::Result<Vec<u8>> {
Ok(vec![i as u8])
}
fn make_decoder(&self) -> crate::Result<Box<dyn VideoDecoder>> {
Ok(Box::new(Big(self.0)))
}
}
#[test]
fn large_frames_cannot_enlarge_the_gop_cache_ceiling() {
let mut c = GopCache::new(None);
c.budget = usize::MAX;
assert_eq!(c.cache_budget(1), FRAME_CACHE_CEILING);
assert_eq!(c.cache_budget(FRAME_CACHE_CEILING), FRAME_CACHE_CEILING);
c.budget = 0;
assert_eq!(c.cache_budget(4), MIN_FRAMES * 4, "small frames retain the playback reserve");
assert_eq!(c.cache_budget(FRAME_CACHE_CEILING), FRAME_CACHE_CEILING, "large frames are capped instead of reserving 64 copies");
}
#[test]
fn evicted_frames_are_decoded_into_again() {
let s = Big(200);
// room for MIN_FRAMES pictures only
let mut c = GopCache::new(None);
c.budget = 0;
let reused = || filmcraft_frame::pool::stats().reused;
let before = reused();
// the cache holds MIN_FRAMES pictures: none is evicted yet, every plane is new
for i in 0..MIN_FRAMES as i64 {
assert_eq!(c.frame(&s, i * 1000).expect("frame").width, BIG.0);
}
assert_eq!(reused(), before);
// from here on each picture evicts one, whose buffer the next picture is decoded into
let more = 40;
for i in MIN_FRAMES as i64..MIN_FRAMES as i64 + more {
let f = c.frame(&s, i * 1000).expect("frame");
assert!(matches!(&f.data, filmcraft_frame::PixelData::Rgba8(d) if d.iter().all(|&b| b == i as u8)), "frame {i} has its own pixels");
}
assert!(reused() - before >= more as u64 - 2, "{} of {more} planes recycled", reused() - before);
}
/// An open-GOP stream like iPhone HEVC: 8 pictures a GOP; after the first, each GOP stores
/// its random-access picture (display 8·g) first, then two leading pictures shown before it
/// (8·g − 2, 8·g − 1, predicted from the GOP before), then the rest. pts = 1000 · display.
struct OpenGop {
/// (display index, sync) in decode order
order: Vec<(i64, bool)>,
resets: Arc<AtomicUsize>,
}
impl OpenGop {
fn new(gops: i64) -> Self {
let mut order: Vec<(i64, bool)> = (0..6).map(|d| (d, d == 0)).collect();
for g in 1..gops {
order.extend([(8 * g, true), (8 * g - 2, false), (8 * g - 1, false)]);
order.extend((8 * g + 1..8 * g + 6).map(|d| (d, false)));
}
Self { order, resets: Default::default() }
}
}
/// Leaves out the leading pictures of the random-access picture a run starts at (RASL
/// pictures, as the HEVC decoders do), outputs the others at once.
struct OpenGopDec {
run_start: Option<i64>,
resets: Arc<AtomicUsize>,
}
impl VideoDecoder for OpenGopDec {
fn decode(&mut self, _sample: &[u8], pts: i64) -> crate::Result<Vec<DecodedFrame>> {
let start = *self.run_start.get_or_insert(pts);
Ok(if pts < start { Vec::new() } else { vec![picture((pts, false))] })
}
fn flush(&mut self) -> Vec<DecodedFrame> {
Vec::new()
}
fn reset(&mut self) {
self.resets.fetch_add(1, Ordering::Relaxed);
self.run_start = None;
}
fn name(&self) -> &str {
"open gop"
}
}
impl VideoSamples for OpenGop {
fn count(&self) -> usize {
self.order.len()
}
fn pts(&self, i: usize) -> i64 {
self.order[i].0 * 1000
}
fn sync_before(&self, i: usize) -> usize {
(0..=i).rev().find(|&k| self.order[k].1).unwrap_or(0)
}
fn sample_at(&self, t: i64) -> Option<usize> {
self.order.iter().position(|&(d, _)| d == t / 1000)
}
fn read(&self, i: usize) -> crate::Result<Vec<u8>> {
Ok(vec![i as u8])
}
fn make_decoder(&self) -> crate::Result<Box<dyn VideoDecoder>> {
Ok(Box::new(OpenGopDec { run_start: None, resets: self.resets.clone() }))
}
}
/// A leading picture of an open GOP, asked for first (a clip trimmed to start on it, the
/// playhead parked on it), came out black: decoding started at the random-access picture
/// after it, which leaves it out.
#[test]
fn leading_pictures_of_an_open_gop_decode_from_the_gop_before() {
let s = OpenGop::new(6);
for d in [6usize, 7, 14, 15, 39] {
let c = GopCache::new(None);
assert_eq!(index_of(&c.frame(&s, d as i64 * 1000).expect("frame")), d);
}
// the random-access picture first, then its leading pictures: the run that started at
// it left them out, so they restart a GOP earlier
let c = GopCache::new(None);
assert_eq!(index_of(&c.frame(&s, 16_000).expect("frame")), 16);
assert_eq!(index_of(&c.frame(&s, 15_000).expect("frame")), 15);
assert_eq!(index_of(&c.frame(&s, 14_000).expect("frame")), 14);
// playing through: one seek, every picture in turn
let s = OpenGop::new(6);
let c = GopCache::new(None);
for d in 0..46usize {
assert_eq!(index_of(&c.frame(&s, d as i64 * 1000).expect("frame")), d);
}
assert_eq!(s.resets.load(Ordering::Relaxed), 1);
}
/// Many pictures in one GOP; a decoder that emits a burst past the wanted sample (frame-
/// threaded H.264). Eviction used to pivot on the newest pts and drop the playhead frame.
struct Burst {
n: usize,
/// Pictures held before a decode dumps them all.
burst: usize,
held: VecDeque<i64>,
resets: Arc<AtomicUsize>,
decodes: Arc<AtomicUsize>,
}
impl VideoDecoder for Burst {
fn decode(&mut self, _sample: &[u8], pts: i64) -> crate::Result<Vec<DecodedFrame>> {
self.decodes.fetch_add(1, Ordering::Relaxed);
self.held.push_back(pts);
if self.held.len() < self.burst {
return Ok(Vec::new());
}
Ok(self.held.drain(..).map(|p| picture((p, false))).collect())
}
fn flush(&mut self) -> Vec<DecodedFrame> {
self.held.drain(..).map(|p| picture((p, false))).collect()
}
fn reset(&mut self) {
self.resets.fetch_add(1, Ordering::Relaxed);
self.held.clear();
}
fn name(&self) -> &str {
"burst"
}
}
impl VideoSamples for Burst {
fn count(&self) -> usize {
self.n
}
fn pts(&self, i: usize) -> i64 {
i as i64 * 1000
}
fn sync_before(&self, _i: usize) -> usize {
0
}
fn sample_at(&self, t: i64) -> Option<usize> {
let i = (t / 1000) as usize;
(i < self.n).then_some(i)
}
fn read(&self, i: usize) -> crate::Result<Vec<u8>> {
Ok(vec![i as u8])
}
fn make_decoder(&self) -> crate::Result<Box<dyn VideoDecoder>> {
Ok(Box::new(Burst { n: self.n, burst: self.burst, held: VecDeque::new(), resets: self.resets.clone(), decodes: self.decodes.clone() }))
}
}
#[test]
fn long_gop_seek_keeps_the_target_and_does_not_walk_twice() {
let s = Burst { n: 200, burst: 90, held: VecDeque::new(), resets: Default::default(), decodes: Default::default() };
let mut c = GopCache::new(None);
c.budget = 0;
// Want a picture early in the GOP; the burst dumps 90 pictures at once (past MIN_FRAMES).
assert_eq!(index_of(&c.frame(&s, 10_000).expect("frame")), 10);
assert_eq!(s.resets.load(Ordering::Relaxed), 1);
let decodes = s.decodes.load(Ordering::Relaxed);
assert!(decodes <= 90 + 1, "walked {decodes} samples");
// The wanted picture is still cached: asking again must not restart or re-feed.
assert_eq!(index_of(&c.frame(&s, 10_000).expect("cached")), 10);
assert_eq!(s.resets.load(Ordering::Relaxed), 1);
assert_eq!(s.decodes.load(Ordering::Relaxed), decodes);
// Playback continues from the same run.
assert_eq!(index_of(&c.frame(&s, 11_000).expect("frame")), 11);
assert_eq!(s.resets.load(Ordering::Relaxed), 1);
}
#[test]
fn long_gop_continue_does_not_restart_for_the_next_frame() {
let s = samples(300, 250, 8, false);
let mut c = GopCache::new(None);
c.budget = 0;
assert_eq!(index_of(&c.frame(&s, 80_000).expect("frame")), 80);
let resets = s.resets.load(Ordering::Relaxed);
let decodes = s.decodes.load(Ordering::Relaxed);
assert_eq!(index_of(&c.frame(&s, 81_000).expect("frame")), 81);
assert_eq!(s.resets.load(Ordering::Relaxed), resets);
assert!(s.decodes.load(Ordering::Relaxed) - decodes <= 8 + 2);
}
/// #3467: a rotated stream's frames are cached raw while the decoder runs (the lock held), and
/// only the frames requests take are rotated, off the lock; the result replaces the raw frame.
#[test]
fn rotation_waits_for_the_frames_requests_take() {
let s = samples(60, 30, 0, false);
let c = GopCache::new(None).with_rotation(1);
let raw = |c: &GopCache| c.shared.state.lock().unwrap().raw.iter().map(|p| p / 1000).collect::<Vec<_>>();
let cached = |c: &GopCache, i: i64| c.shared.state.lock().unwrap().frames.get(&(i * 1000)).cloned().expect("cached");
let f5 = c.frame(&s, 5_000).expect("frame");
assert_eq!(index_of(&f5), 5);
// decoding up to frame 5 rotated none of the frames before it
assert_eq!(raw(&c), vec![0, 1, 2, 3, 4]);
assert!(Arc::ptr_eq(&cached(&c, 5), &f5));
// a cache hit finishes the raw frame once; the next hit gets that same frame
let f3 = c.frame(&s, 3_000).expect("frame");
assert_eq!(index_of(&f3), 3);
assert_eq!(raw(&c), vec![0, 1, 2, 4]);
assert!(Arc::ptr_eq(&c.frame(&s, 3_000).expect("frame"), &f3));
assert_eq!(s.decodes.load(Ordering::Relaxed), 6);
// without crop or rotation nothing waits
let plain = GopCache::new(None);
assert_eq!(index_of(&plain.frame(&s, 5_000).expect("frame")), 5);
assert!(raw(&plain).is_empty());
}
/// Samples whose pictures are `w`×`h`: the index in the first pixel, 255 everywhere else.
struct Wide(Samples, usize, usize);
struct WideDec(Box<dyn VideoDecoder>, usize, usize);
impl VideoDecoder for WideDec {
fn decode(&mut self, sample: &[u8], pts: i64) -> crate::Result<Vec<DecodedFrame>> {
let (w, h) = (self.1, self.2);
Ok(self.0.decode(sample, pts)?.into_iter().map(|d| widen(d, w, h)).collect())
}
fn flush(&mut self) -> Vec<DecodedFrame> {
let (w, h) = (self.1, self.2);
self.0.flush().into_iter().map(|d| widen(d, w, h)).collect()
}
fn reset(&mut self) {
self.0.reset()
}
fn name(&self) -> &str {
"wide"
}
fn intra_only(&self) -> bool {
self.0.intra_only()
}
}
fn widen(d: DecodedFrame, w: usize, h: usize) -> DecodedFrame {
let mut px = vec![255; w * h * 4];
px[..4].copy_from_slice(&[index_of(&d.frame) as u8, 0, 0, 255]);
DecodedFrame { frame: VideoFrame::rgba8(w as u32, h as u32, px), ..d }
}
impl VideoSamples for Wide {
fn count(&self) -> usize {
self.0.count()
}
fn pts(&self, i: usize) -> i64 {
self.0.pts(i)
}
fn sync_before(&self, i: usize) -> usize {
self.0.sync_before(i)
}
fn sample_at(&self, t: i64) -> Option<usize> {
self.0.sample_at(t)
}
fn read(&self, i: usize) -> crate::Result<Vec<u8>> {
self.0.read(i)
}
fn make_decoder(&self) -> crate::Result<Box<dyn VideoDecoder>> {
Ok(Box::new(WideDec(self.0.make_decoder()?, self.1, self.2)))
}
}
/// #3467: a rotated frame really is turned, whether it is decoded for the request or taken raw
/// from the cache later.
#[test]
fn raw_frames_taken_from_the_cache_come_out_rotated() {
// long-GOP and intra-only (ProRes, DNx) decoding alike
for intra in [false, true] {
let s = Wide(samples(30, 30, 0, intra), 2, 1);
let c = GopCache::new(None).with_rotation(1);
// frame 4 is decoded for the request, frame 2 taken raw from the cache afterwards
for i in [4usize, 2] {
let f = c.frame(&s, i as i64 * 1000).expect("frame");
assert_eq!((f.width, f.height), (1, 2), "intra {intra} frame {i}");
// a clockwise quarter turn puts the left pixel on top
assert_eq!(f.to_rgba8()[..4], [i as u8, 0, 0, 255], "intra {intra} frame {i}");
}
// left to the caller, the turn is not done: frame 3 comes as decoded, then turned on request
let (f, turns) = c.frame_late_unturned(&s, 3000, None).expect("frame");
assert_eq!((f.width, f.height, turns), (2, 1, 1), "intra {intra}");
assert_eq!((c.frame(&s, 3000).expect("frame").width, turns), (1, 1), "intra {intra}");
}
}
/// The pixel at (x, y) of a frame, as RGBA.
fn px(f: &VideoFrame, x: usize, y: usize) -> [u8; 4] {
let rgba = f.to_rgba8();
let at = (y * f.width as usize + x) * 4;
[rgba[at], rgba[at + 1], rgba[at + 2], rgba[at + 3]]
}
/// #3467: every display rotation swaps or keeps the size of a non-square clip and puts the marked
/// pixel (top left as stored) where a clockwise turn puts it, for frames decoded for the request
/// and for frames taken raw from the cache alike.
#[test]
fn every_quarter_turn_of_a_non_square_clip_is_right() {
// marked pixel after 1, 2 and 3 clockwise quarter turns of a 3×2 picture: (x, y)
let marked = [(1, 0), (2, 1), (0, 2)];
for (turns, (x, y)) in (1u8..4).zip(marked) {
let s = Wide(samples(30, 30, 0, false), 3, 2);
let c = GopCache::new(None).with_rotation(turns);
let size = if turns % 2 == 1 { (2, 3) } else { (3, 2) };
for i in [4usize, 2] {
let f = c.frame(&s, i as i64 * 1000).expect("frame");
assert_eq!((f.width, f.height), size, "turns {turns} frame {i}");
assert_eq!(px(&f, x, y), [i as u8, 0, 0, 255], "turns {turns} frame {i}");
assert_eq!(px(&f, 0, 0) == [i as u8, 0, 0, 255], (x, y) == (0, 0));
}
// a second hit gets the finished frame, not a second turn
let again = c.frame(&s, 2_000).expect("frame");
assert_eq!(px(&again, x, y), [2, 0, 0, 255], "turns {turns}");
}
}
/// #3467: the clean aperture is cut before the turn, for decoded and raw-cached frames.
#[test]
fn crop_and_rotation_compose_on_raw_frames() {
// 4×2 picture, marked pixel (0, 0); keep the 2×2 block at x = 0: marked pixel stays top left,
// then a clockwise quarter turn moves it to the top right of the 2×2 result
let region = Region { x: 0, y: 0, w: 2, h: 2 };
let s = Wide(samples(30, 30, 0, false), 4, 2);
let c = GopCache::new(None).with_crop(Some(region)).with_rotation(1);
for i in [4usize, 2] {
let f = c.frame(&s, i as i64 * 1000).expect("frame");
assert_eq!((f.width, f.height), (2, 2), "frame {i}");
assert_eq!(px(&f, 1, 0), [i as u8, 0, 0, 255], "frame {i}");
}
// the block at x = 2 holds no marked pixel at all
let c = GopCache::new(None).with_crop(Some(Region { x: 2, y: 0, w: 2, h: 2 })).with_rotation(1);
let f = c.frame(&s, 4_000).expect("frame");
assert!((0..2).all(|x| (0..2).all(|y| px(&f, x, y) == [255; 4])));
}
/// #3467: a raw frame decoded as a draft is still a draft once finished, and no exact request is
/// served it.
#[test]
fn raw_draft_frames_are_not_served_to_exact_requests() {
let s = samples(300, 250, 3, false);
let c = GopCache::new(None).with_rotation(1);
for i in 0..8usize {
let f = filmcraft_media::cancel::with_draft(true, || c.frame(&s, i as i64 * 1000)).expect("frame");
assert_eq!((index_of(&f), is_draft(&f)), (i, i % 2 == 1), "frame {i}");
}
let resets = s.resets.load(Ordering::Relaxed);
// finishing the raw frame kept its draft mark
assert!(c.shared.state.lock().unwrap().drafts.contains(&5_000));
// an exact request decodes frame 5 again and gets an exact, rotated one
let f = c.frame(&s, 5_000).expect("frame");
assert!(!is_draft(&f) && index_of(&f) == 5);
assert_eq!(s.resets.load(Ordering::Relaxed), resets + 1);
assert!(!c.shared.state.lock().unwrap().drafts.contains(&5_000));
// an exact even frame is served from the cache
assert!(!is_draft(&c.frame(&s, 4_000).expect("frame")));
assert_eq!(s.resets.load(Ordering::Relaxed), resets + 1);
}
/// #3467: eviction drops a frame from `frames`, `raw`, `drafts` and the byte count together, and
/// what is left still comes out rotated.
#[test]
fn evicting_raw_frames_keeps_the_cache_consistent() {
let s = Wide(samples(200, 200, 0, false), 2, 1);
let mut c = GopCache::new(None).with_rotation(1);
c.budget = 0;
let n = MIN_FRAMES as i64 + 30;
for i in 0..n {
assert_eq!(c.frame(&s, i * 1000).expect("frame").width, 1);
}
let st = c.shared.state.lock().unwrap();
assert!(st.frames.len() < n as usize, "some frames were evicted");
assert!(st.raw.iter().chain(st.drafts.iter()).all(|p| st.frames.contains_key(p)), "no mark outlives its frame");
assert_eq!(st.bytes, st.frames.values().map(|f| f.byte_size()).sum::<usize>());
drop(st);
// decoding up to an evicted frame again still turns it
let f = c.frame(&s, 1_000).expect("frame");
assert_eq!((f.width, f.height), (1, 2));
}
#[test]
fn release_idle_empties_idle_caches_and_leaves_one_in_use_alone() {
let pool = Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET).tiered(RECENT, HARD_DECODERS));
let s: Vec<Samples> = (0..3).map(|_| samples(300, 250, 3, false)).collect();
let c: Vec<GopCache> = (0..3).map(|_| GopCache::in_pool(None, pool.clone())).collect();
for k in 0..3 {
assert_eq!(index_of(&c[k].frame(&s[k], 10_000).expect("frame")), 10);
}
assert!(pool.bytes.load(Ordering::Relaxed) > 0);
assert_eq!(pool.decoders.load(Ordering::Relaxed), 3);
// a cache another thread holds (it is decoding) keeps its frames and decoder
let busy = c[2].shared.state.lock().unwrap();
let held_bytes = busy.bytes;
pool.release_idle();
assert_eq!(busy.bytes, held_bytes);
assert!(busy.decoder.is_some() && !busy.frames.is_empty());
drop(busy);
for k in 0..2 {
let st = c[k].shared.state.lock().unwrap();
assert!(st.frames.is_empty() && st.drafts.is_empty() && st.bytes == 0 && st.decoder.is_none() && st.spare.is_empty());
}
assert_eq!(pool.bytes.load(Ordering::Relaxed), held_bytes, "only the busy cache still counts");
assert_eq!(pool.decoders.load(Ordering::Relaxed), 1);
// an emptied cache decodes again from its sync sample, and a second release is harmless
assert_eq!(index_of(&c[0].frame(&s[0], 20_000).expect("frame")), 20);
assert_eq!(pool.bytes.load(Ordering::Relaxed), held_bytes + c[0].shared.state.lock().unwrap().bytes);
// the cache that was in use is idle now: nothing is left, and a second release is harmless
pool.release_idle();
pool.release_idle();
assert_eq!(pool.bytes.load(Ordering::Relaxed), 0);
assert_eq!(pool.decoders.load(Ordering::Relaxed), 0);
}
#[test]
fn eviction_reaches_interior_frames_when_both_ends_are_protected() {
// 1×1 RGBA pictures (4 bytes): the budget is the 64-frame playback reserve
let mut c = GopCache::new(None);
c.budget = 0;
let budget = c.cache_budget(4);
let mut st = c.shared.state.lock().unwrap();
st.pin = 0;
let pic = || VideoFrame::rgba8(1, 1, vec![0; 4]);
c.store_finished(&mut st, 0, pic(), false);
// each new decode is the newest frame and the request is the oldest: both ends protected
for k in 1..500 {
c.store_finished(&mut st, k * 1000, pic(), false);
}
assert!(st.bytes <= budget, "{} bytes cached over a {budget}-byte budget", st.bytes);
assert!(st.frames.contains_key(&0) && st.frames.contains_key(&499_000));
}
#[test]
fn two_frames_over_the_ceiling_keep_only_the_requested_one() {
// two ~200 MiB RGBA frames (zeroed, never touched) are over the 384 MiB ceiling together
let c = GopCache::new(None);
let mut st = c.shared.state.lock().unwrap();
st.pin = 0;
let big = || VideoFrame::rgba8(8192, 6400, vec![0; 8192 * 6400 * 4]);
c.store_finished(&mut st, 0, big(), false);
let second = c.store_finished(&mut st, 1000, big(), false);
assert!(st.bytes <= FRAME_CACHE_CEILING, "{} bytes cached over the ceiling", st.bytes);
assert!(st.frames.contains_key(&0), "the requested frame is kept");
// the frame a decode produced is handed back even though the cache had to drop it (another request owns the pin)
assert!(!st.frames.contains_key(&1000));
assert_eq!((second.width, second.height), (8192, 6400));
}
#[test]
fn a_decode_whose_only_fallback_was_evicted_still_returns_it() {
// two ~200 MiB frames around the pin: the cache keeps the newest one and drops the older, which is the
// only frame at or before the wanted pts
let c = GopCache::new(None);
let mut st = c.shared.state.lock().unwrap();
st.pin = 1000;
let big = |pts| crate::video::DecodedFrame { pts, frame: VideoFrame::rgba8(8192, 6400, vec![0; 8192 * 6400 * 4]), draft: false };
let nearest = c.store_output(&mut st, vec![big(0), big(2000)], 1500).expect("the frame at 0");
assert_eq!(nearest.pts, 0);
assert!(!st.frames.contains_key(&0), "the cache dropped it");
assert_eq!((nearest.frame.width, nearest.frame.height), (8192, 6400));
}
/// Every sample claims to be a keyframe but none can start a decode: the search for a random-access
/// sample gives up after a bounded number of probes instead of reading them all.
#[test]
fn random_access_search_is_bounded_when_every_claimed_keyframe_is_not_one() {
struct Aliased {
reads: std::sync::atomic::AtomicUsize,
}
impl VideoSamples for Aliased {
fn count(&self) -> usize {
1 << 20
}
fn pts(&self, i: usize) -> i64 {
i as i64
}
fn sync_before(&self, i: usize) -> usize {
i
}
fn sample_at(&self, t: i64) -> Option<usize> {
usize::try_from(t).ok()
}
fn read(&self, _: usize) -> crate::Result<Vec<u8>> {
self.reads.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
Ok(vec![0; 16])
}
fn make_decoder(&self) -> crate::Result<Box<dyn VideoDecoder>> {
Err(crate::CodecError::Unsupported("test".into()))
}
}
let s = Aliased { reads: Default::default() };
struct Huge(Aliased);
impl VideoSamples for Huge {
fn count(&self) -> usize {
self.0.count()
}
fn pts(&self, i: usize) -> i64 {
self.0.pts(i)
}
fn sync_before(&self, i: usize) -> usize {
i
}
fn sample_at(&self, t: i64) -> Option<usize> {
self.0.sample_at(t)
}
fn read(&self, i: usize) -> crate::Result<Vec<u8>> {
self.0.read(i)
}
fn sample_size(&self, _: usize) -> Option<u64> {
Some(MAX_RANDOM_ACCESS_PROBE_BYTES as u64 + 1)
}
fn make_decoder(&self) -> crate::Result<Box<dyn VideoDecoder>> {
self.0.make_decoder()
}
}
// a candidate declared larger than the byte budget is refused without being read
let h = Huge(Aliased { reads: Default::default() });
assert!(random_access_at_or_before(&h, 100, |_| Some(true)).is_err());
assert_eq!(h.0.reads.load(std::sync::atomic::Ordering::Relaxed), 0);
assert!(random_access_at_or_before(&s, 1 << 19, |_| Some(false)).is_err());
assert!(s.reads.load(std::sync::atomic::Ordering::Relaxed) <= MAX_RANDOM_ACCESS_PROBES);
// a sample that can start a decode ends the search at once
assert_eq!(random_access_at_or_before(&s, 100, |_| Some(true)).unwrap(), 100);
assert_eq!(random_access_at_or_before(&s, 0, |_| Some(false)).unwrap(), 0);
// a container that cannot say a sample's size: the oversized candidate is refused after its one read
struct Unsized(Aliased);
impl VideoSamples for Unsized {
fn count(&self) -> usize {
self.0.count()
}
fn pts(&self, i: usize) -> i64 {
self.0.pts(i)
}
fn sync_before(&self, i: usize) -> usize {
i
}
fn sample_at(&self, t: i64) -> Option<usize> {
self.0.sample_at(t)
}
fn read(&self, _: usize) -> crate::Result<Vec<u8>> {
self.0.reads.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
Ok(vec![0; MAX_RANDOM_ACCESS_PROBE_BYTES + 1])
}
fn make_decoder(&self) -> crate::Result<Box<dyn VideoDecoder>> {
self.0.make_decoder()
}
}
let u = Unsized(Aliased { reads: Default::default() });
assert!(random_access_at_or_before(&u, 100, |_| Some(true)).is_err());
assert_eq!(u.0.reads.load(std::sync::atomic::Ordering::Relaxed), 1);
// a cancelled request is refused even when the search starts at sample zero
let flag = Arc::new(std::sync::atomic::AtomicBool::new(true));
let r = filmcraft_media::cancel::with_cancel(&flag, || random_access_at_or_before(&s, 0, |_| Some(true)));
assert!(matches!(r, Err(CodecError::Cancelled)));
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.