extern crate std; use super::*; use alloc::vec::Vec; fn psnr(a: &[u8], b: &[u8]) -> f64 { let mse: f64 = a .iter() .zip(b) .map(|(x, y)| (f64::from(*x) - f64::from(*y)).powi(2)) .sum::() / a.len() as f64; 10.0 * (255.0 * 255.0 / mse.max(1e-9)).log10() } /// A test picture: a gradient with a square at `t`. fn picture(t: usize) -> video::Frame { let (w, h) = (240, 320); let rgb: Vec = (0..w * h) .map(|i| { let (x, y) = (i % w, i / w); let in_square = (x + 240 - t * 4) % 240 < 48 && (100..160).contains(&y); if in_square { 0x00e0_5a4f } else { ((x * 255 / w) as u32) << 16 | ((y * 255 / h) as u32) << 8 | 0x80 } }) .collect(); video::Frame::from_rgb(w, h, &rgb) } #[test] fn video_decodes_close_to_the_source_and_delta_frames_are_small() { let mut encoder = video::Encoder::new(240, 320, 6); let mut decoder = video::Decoder::new(240, 320, 6); let key: usize = encoder .encode(&picture(0), true) .iter() .map(|p| { assert!(decoder.decode(p)); p.len() }) .sum(); assert!( psnr(&decoder.frame.y, &picture(0).y) > 32.0, "key frame PSNR {}", psnr(&decoder.frame.y, &picture(0).y) ); let mut delta = 0; for t in 1..10 { for p in encoder.encode(&picture(t), false) { assert!(p.len() <= 1100 + 7 + 6 * 200); delta += p.len(); assert!(decoder.decode(&p)); } } assert!( psnr(&decoder.frame.y, &picture(9).y) > 30.0, "after deltas PSNR {}", psnr(&decoder.frame.y, &picture(9).y) ); assert!( delta / 9 < key / 3, "a delta frame ({}) should be far smaller than a key frame ({key})", delta / 9 ); } #[test] fn a_lost_packet_heals_at_the_next_key_frame() { let mut encoder = video::Encoder::new(240, 320, 6); let mut decoder = video::Decoder::new(240, 320, 6); for (i, p) in encoder.encode(&picture(0), true).iter().enumerate() { if i != 1 { decoder.decode(p); } } for p in encoder.encode(&picture(5), true) { decoder.decode(&p); } assert!(psnr(&decoder.frame.y, &picture(5).y) > 32.0); } #[test] fn adpcm_keeps_a_tone() { // A 440 Hz tone at 16 kHz, by a rotating phasor (no libm here). let (c, s) = (0.984_891_5f64, 0.173_139_5f64); let (mut x, mut y) = (8000.0f64, 0.0f64); let samples: Vec = (0..adpcm::FRAME * 4) .map(|_| { let v = x as i16; (x, y) = (x * c - y * s, x * s + y * c); v }) .collect(); let mut state = adpcm::State::default(); let mut decoded = Vec::new(); for frame in samples.chunks(adpcm::FRAME) { let coded = adpcm::encode(&mut state, frame); assert_eq!(coded.len(), 3 + adpcm::FRAME / 2); decoded.extend(adpcm::decode(&coded)); } let signal: f64 = samples.iter().map(|&v| f64::from(v).powi(2)).sum(); let noise: f64 = samples .iter() .zip(&decoded) .skip(64) .map(|(&a, &b)| (f64::from(a) - f64::from(b)).powi(2)) .sum(); let snr = 10.0 * (signal / noise).log10(); assert!(snr > 20.0, "SNR {snr} dB"); } /// A PNG `w` × `h`: red on the left half, see-through blue on the right. fn png(w: u32, h: u32) -> Vec { let mut image = wil_core::Image::new(w, h); for (i, p) in image.pixels.as_chunks_mut::<4>().0.iter_mut().enumerate() { let left = (i as u32 % w) < w / 2; p.copy_from_slice(if left { &[255, 0, 0, 255] } else { &[0, 0, 255, 0] }); } wil_core::png::encode(&image, wil_core::deflate::Level::DEFAULT) } #[test] fn a_picture_is_told_by_its_bytes_not_its_name() { assert_eq!(picture::sniff(&png(4, 4)), Some(picture::Format::Png)); assert_eq!(picture::sniff(b"Shopping list\nbread"), None); // A file of zeros and odd bytes, which nothing opens. assert_eq!(picture::sniff(&[0, 1, 2, 3, 0xfe, 0, 7, 8]), None); assert_eq!(picture::sniff(&[]), None); } #[test] fn a_picture_is_fitted_into_its_box_keeping_its_shape() { let mut p = picture::Picture::decode(&png(400, 200)).unwrap(); assert_eq!((p.width(), p.height()), (400, 200)); // A tall box: as wide as the box, centred from top to bottom. let f = p.fit(200, 300, 0x00_ff_ff_ff); assert_eq!((f.width, f.height, f.x, f.y), (200, 100, 0, 100)); assert_eq!(f.pixels.len(), 200 * 100); // Red where it is opaque; the see-through half is the background. assert_eq!(f.pixels[0], 0x00_ff_00_00); assert_eq!(f.pixels[199], 0x00_ff_ff_ff); // A wide box: as high as the box, centred from side to side. let f = p.fit(1000, 50, 0); assert_eq!((f.width, f.height, f.x, f.y), (100, 50, 450, 0)); assert_eq!(f.pixels[99], 0); // A box larger than the picture: never grown, only centred. let f = p.fit(800, 600, 0); assert_eq!((f.width, f.height, f.x, f.y), (400, 200, 200, 200)); } #[test] fn a_broken_or_huge_picture_is_refused_not_a_panic() { let mut data = png(64, 64); data.truncate(data.len() / 2); assert!(picture::Picture::decode(&data).is_err()); // Says it is 60000 × 60000 (3.6 gigapixels) in its header. let mut data = png(1, 1); data[16..20].copy_from_slice(&60_000u32.to_be_bytes()); data[20..24].copy_from_slice(&60_000u32.to_be_bytes()); assert!(picture::Picture::decode(&data).is_err()); }