use super::tests::*; use super::*; #[test] fn every_frame_of_a_sequence_decodes_through_our_own_decoder() { let fctx = &crate::decode::FrameCtx::for_encoder(); let (width, height) = (256usize, 128usize); let pictures: Vec = (0..5) .map(|i| panned_test_card(width, height, i * 3)) .collect(); let _ = take_inter_tx_split_hits(); let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap(); let raw = take_inter_tx_split_hits(); let split = [ raw.iter().step_by(2).sum::(), raw.iter().skip(1).step_by(2).sum::(), ]; eprintln!("inter var-tx: {} blocks flat, {} split", split[0], split[1]); if tx_select() && tx_select_inter() { assert!(split[1] > 0, "the inter var-tx split never fired"); } let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); assert_eq!(decoded.len(), encoded.frames.len(), "frame count"); for (i, (frame, dec)) in encoded.frames.iter().zip(&decoded).enumerate() { let recon = &frame.reconstruction; for (plane, got, want, stride) in [ ("luma", &dec.y, &recon.y, width), ("U", &dec.u, &recon.u, width / 2), ("V", &dec.v, &recon.v, width / 2), ] { assert_eq!(got.len(), want.len(), "frame {i}: {plane} size"); if let Some(at) = got.iter().zip(want).position(|(a, b)| a != b) { panic!( "frame {i}: {plane} differs first at ({}, {}): decoded {} vs \ reconstruction {} ({} of {} samples differ)", at % stride, at / stride, got[at], want[at], got.iter().zip(want).filter(|(a, b)| a != b).count(), got.len(), ); } } } } #[test] fn predicted_coeff_bits_track_the_tile_the_writer_wrote() { let fctx = &crate::decode::FrameCtx::for_encoder(); let (width, height) = (256usize, 128usize); let pictures: Vec = (0..5) .map(|i| panned_test_card(width, height, i * 2)) .collect(); let _ = take_predicted_bits(); let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap(); let predicted = take_predicted_bits(); assert_eq!(predicted.len(), encoded.frames.len()); eprintln!("frame predicted bits written bits drift"); let mut worst_over: f64 = 0.0; let mut worst_under: f64 = 0.0; let coded: Vec<&Encoded> = encoded .coding_order .iter() .map(|&i| &encoded.frames[i]) .collect(); for (i, (frame, &bits)) in coded.into_iter().zip(&predicted).enumerate() { let written = frame.tile.len() as f64 * 8.0; let drift = (written - bits) / written; worst_over = worst_over.max(-drift); if written >= 512.0 { worst_under = worst_under.max(drift); } eprintln!( "{i:5} {bits:14.0} {written:12.0} {:+6.2}%", drift * 100.0 ); } assert!( worst_over <= 0.16, "the search's rate term OVER-priced by {:.2}% against the bits the writer spent", worst_over * 100.0 ); assert!( worst_under <= 0.40, "the writer spent {:.2}% more than the search priced -- more than \ the mode/mv syntax outside the coefficient sum explains", worst_under * 100.0 ); } #[test] fn low_motion_makes_an_inter_frame_smaller_than_the_key_frame() { let fctx = &crate::decode::FrameCtx::for_encoder(); let (width, height) = (256usize, 128usize); let pictures: Vec = (0..5) .map(|i| panned_test_card(width, height, i * 2)) .collect(); let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap(); let key_bytes = encoded.frames[0].stream.len(); eprintln!("frame bytes inter share"); for (i, frame) in encoded.frames.iter().enumerate() { eprintln!( "{i:5} {:5} {:11.2}", frame.stream.len(), frame.inter_block_share ); } assert!( encoded.frames[1..] .iter() .any(|f| f.stream.len() < key_bytes), "no inter frame of a panning sequence beat the key frame's {key_bytes} bytes" ); assert!( encoded.frames[1..] .iter() .any(|f| f.inter_block_share > 0.0), "no inter frame coded a single inter block -- the search never fired" ); } pub(super) fn clip_frames( clip: &str, skip: &str, width: usize, height: usize, frames: usize, ) -> Vec { clip_frames_vf( clip, skip, &format!("scale={width}:{height}"), width, height, frames, ) } pub(super) fn clip_frames_vf( clip: &str, skip: &str, vf: &str, width: usize, height: usize, frames: usize, ) -> Vec { crate::probe::source(clip, skip, vf, width, height, frames) } #[test] fn the_encoders_own_streams_are_byte_identical_to_their_pins() { let was = crate::speed::speed(); crate::speed::set_speed(0); let clip = ec_testkit::fixture("open/sintel_trailer-1080p.mp4"); let source = clip_frames(clip.to_str().unwrap(), "20", 640, 384, 4); let fnv = |b: &[u8]| { b.iter().fold(0xcbf2_9ce4_8422_2325u64, |h, &v| { (h ^ u64::from(v)).wrapping_mul(0x0000_0100_0000_01b3) }) }; let pins: [(u8, usize, u64); 2] = [ (150, 23666, 11535713837929557386), (60, 102035, 3886390906684528357), ]; let coded: Vec<(u8, usize, u64)> = pins .iter() .map(|&(q, _, _)| { let e = encode_sequence(&source, q, 0.5).unwrap(); (q, e.stream.len(), fnv(&e.stream)) }) .collect(); crate::speed::set_speed(was); for (&(q, bytes, hash), &got) in pins.iter().zip(&coded) { assert_eq!(got, (q, bytes, hash), "q={q}: the encoder's stream moved"); } }