| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | mod common; |
| 6 | |
| 7 | use ec_h265::decode::decode_idr_au; |
| 8 | use ec_h265::encoder::{Encoder, EncoderConfig, RateControl}; |
| 9 | |
| 10 | fn roundtrip_at(width: u32, height: u32) { |
| 11 | let frame = common::test_frame(width, height, 7); |
| 12 | let cfg = EncoderConfig { |
| 13 | rate_control: RateControl::ConstantQp(27), |
| 14 | threads: 1, |
| 15 | keep_recon: true, |
| 16 | ..EncoderConfig::new(width, height) |
| 17 | }; |
| 18 | let enc = Encoder::new(cfg).expect("encoder config"); |
| 19 | let picture = enc.encode_idr(&frame).expect("encode"); |
| 20 | let recon = picture.recon.expect("keep_recon was on"); |
| 21 | |
| 22 | let decoded = decode_idr_au(&picture.au).unwrap_or_else(|e| { |
| 23 | panic!("decode {width}x{height}: {e}"); |
| 24 | }); |
| 25 | |
| 26 | let sps = enc.sps(); |
| 27 | assert_eq!(decoded.width, sps.pic_width_in_luma_samples as usize); |
| 28 | |
| 29 | |
| 30 | |
| 31 | let (w, h) = (width as usize, height as usize); |
| 32 | let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); |
| 33 | for row in 0..h { |
| 34 | let want = recon.planes[0].row(row, w).unwrap(); |
| 35 | let got = &decoded.y[row * decoded.width..row * decoded.width + w]; |
| 36 | assert_eq!(got, want, "{width}x{height} luma row {row} diverges"); |
| 37 | } |
| 38 | for row in 0..ch { |
| 39 | let want_cb = recon.planes[1].row(row, cw).unwrap(); |
| 40 | let got_cb = &decoded.cb[row * decoded.width / 2..row * decoded.width / 2 + cw]; |
| 41 | assert_eq!(got_cb, want_cb, "{width}x{height} Cb row {row} diverges"); |
| 42 | let want_cr = recon.planes[2].row(row, cw).unwrap(); |
| 43 | let got_cr = &decoded.cr[row * decoded.width / 2..row * decoded.width / 2 + cw]; |
| 44 | assert_eq!(got_cr, want_cr, "{width}x{height} Cr row {row} diverges"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn decode_matches_the_encoders_own_reconstruction_at_one_ctb() { |
| 50 | roundtrip_at(32, 32); |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn decode_matches_the_encoders_own_reconstruction_over_several_ctbs() { |
| 55 | roundtrip_at(96, 64); |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | fn decode_matches_the_encoders_own_reconstruction_at_an_odd_padded_size() { |
| 60 | roundtrip_at(70, 50); |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn decode_matches_the_encoders_own_reconstruction_at_another_odd_size() { |
| 65 | roundtrip_at(150, 94); |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn decode_matches_the_encoders_own_reconstruction_at_an_extreme_odd_size() { |
| 70 | roundtrip_at(41, 33); |
| 71 | } |