| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use crate::common::test_frame; |
| 6 | use ec_h265::decode::decode_idr_au; |
| 7 | use ec_h265::encoder::{Encoder, EncoderConfig, RateControl}; |
| 8 | |
| 9 | fn roundtrip_at(width: u32, height: u32) { |
| 10 | let frame = test_frame(width, height, 7); |
| 11 | let cfg = EncoderConfig { |
| 12 | rate_control: RateControl::ConstantQp(27), |
| 13 | threads: 1, |
| 14 | keep_recon: true, |
| 15 | ..EncoderConfig::new(width, height) |
| 16 | }; |
| 17 | let enc = Encoder::new(cfg).expect("encoder config"); |
| 18 | let picture = enc.encode_idr(&frame).expect("encode"); |
| 19 | let recon = picture.recon.expect("keep_recon was on"); |
| 20 | |
| 21 | let decoded = decode_idr_au(&picture.au).unwrap_or_else(|e| { |
| 22 | panic!("decode {width}x{height}: {e}"); |
| 23 | }); |
| 24 | |
| 25 | let sps = enc.sps(); |
| 26 | assert_eq!(decoded.width, sps.pic_width_in_luma_samples as usize); |
| 27 | |
| 28 | |
| 29 | |
| 30 | let (w, h) = (width as usize, height as usize); |
| 31 | let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); |
| 32 | for row in 0..h { |
| 33 | let want = recon.planes[0].row(row, w).unwrap(); |
| 34 | let got: Vec<u8> = decoded.y[row * decoded.width..row * decoded.width + w] |
| 35 | .iter() |
| 36 | .map(|&v| v as u8) |
| 37 | .collect(); |
| 38 | assert_eq!(&got[..], want, "{width}x{height} luma row {row} diverges"); |
| 39 | } |
| 40 | for row in 0..ch { |
| 41 | let want_cb = recon.planes[1].row(row, cw).unwrap(); |
| 42 | let got_cb: Vec<u8> = decoded.cb[row * decoded.width / 2..row * decoded.width / 2 + cw] |
| 43 | .iter() |
| 44 | .map(|&v| v as u8) |
| 45 | .collect(); |
| 46 | assert_eq!( |
| 47 | &got_cb[..], |
| 48 | want_cb, |
| 49 | "{width}x{height} Cb row {row} diverges" |
| 50 | ); |
| 51 | let want_cr = recon.planes[2].row(row, cw).unwrap(); |
| 52 | let got_cr: Vec<u8> = decoded.cr[row * decoded.width / 2..row * decoded.width / 2 + cw] |
| 53 | .iter() |
| 54 | .map(|&v| v as u8) |
| 55 | .collect(); |
| 56 | assert_eq!( |
| 57 | &got_cr[..], |
| 58 | want_cr, |
| 59 | "{width}x{height} Cr row {row} diverges" |
| 60 | ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | const CASES: &[(&str, u32, u32)] = &[ |
| 67 | ( |
| 68 | "decode_matches_the_encoders_own_reconstruction_at_one_ctb", |
| 69 | 32, |
| 70 | 32, |
| 71 | ), |
| 72 | ( |
| 73 | "decode_matches_the_encoders_own_reconstruction_over_several_ctbs", |
| 74 | 96, |
| 75 | 64, |
| 76 | ), |
| 77 | ( |
| 78 | "decode_matches_the_encoders_own_reconstruction_at_an_odd_padded_size", |
| 79 | 70, |
| 80 | 50, |
| 81 | ), |
| 82 | ( |
| 83 | "decode_matches_the_encoders_own_reconstruction_at_another_odd_size", |
| 84 | 150, |
| 85 | 94, |
| 86 | ), |
| 87 | ( |
| 88 | "decode_matches_the_encoders_own_reconstruction_at_an_extreme_odd_size", |
| 89 | 41, |
| 90 | 33, |
| 91 | ), |
| 92 | ]; |
| 93 | |
| 94 | #[test] |
| 95 | fn decode_matches_the_encoders_own_reconstruction() { |
| 96 | let mut failures = Vec::new(); |
| 97 | for &(name, width, height) in CASES { |
| 98 | let outcome = |
| 99 | std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| roundtrip_at(width, height))); |
| 100 | if outcome.is_err() { |
| 101 | failures.push(name); |
| 102 | } |
| 103 | } |
| 104 | assert!(failures.is_empty(), "failed cases: {failures:?}"); |
| 105 | } |