Real-clip BD-PSNR harness for transform_8x8
Browse files at this commit04d77cb897060a57917e8b3629742292018ac22dParent: de1d687- test
- real-clip BD-PSNR harness for transform_8x8
1 changed file +108 -0
- crates/ec-h264/tests/encode.rs +108 -0
crates/ec-h264/tests/encode.rsmodified+108 -0
| @@ -699,3 +699,111 @@ fn real_library_frames_encode_and_decode_exactly() { | |||
| 699 | ); | 699 | ); |
| 700 | assert!(quality > 30.0, "luma PSNR {quality:.2} dB at 8 Mbit/s"); | 700 | assert!(quality > 30.0, "luma PSNR {quality:.2} dB at 8 Mbit/s"); |
| 701 | } | 701 | } |
| 702 | + | ||
| 703 | +/// BD-PSNR of the opt-in `transform_8x8` flag against flag-off on one real | ||
| 704 | +/// clip from the library, full size, over a QP sweep: the number the synthetic | ||
| 705 | +/// gate above cannot give, because the synthetic clip is content the flag wins | ||
| 706 | +/// on. Prints the per-QP points, the BD-PSNR and the 8x8 macroblock share; | ||
| 707 | +/// asserts nothing, so a regressing clip fails a gate, not the test run. | ||
| 708 | +/// | ||
| 709 | +/// EC_H264_CLIP=<file> cargo test --release -p ec-h264 --test encode \ | ||
| 710 | +/// real_clip_t8x8_bd_psnr -- --ignored --nocapture | ||
| 711 | +#[test] | ||
| 712 | +#[ignore = "set EC_H264_CLIP to a media file of yours"] | ||
| 713 | +fn real_clip_t8x8_bd_psnr() { | ||
| 714 | + use ec_core::registry::{Decoder as _, Demuxer as _}; | ||
| 715 | + | ||
| 716 | + let Some(path) = std::env::var_os("EC_H264_CLIP") else { | ||
| 717 | + panic!("EC_H264_CLIP must name a media file"); | ||
| 718 | + }; | ||
| 719 | + // The first 30 pictures a second apart, demuxed by ec-mp4 and decoded by | ||
| 720 | + // this crate: every Nth frame, N the source frame rate. | ||
| 721 | + let file = std::fs::File::open(&path).expect("source opens"); | ||
| 722 | + let mut demux = | ||
| 723 | + ec_mp4::Mp4Demuxer::new(std::io::BufReader::new(file)).expect("ec-mp4 opens the file"); | ||
| 724 | + let (index, params) = demux | ||
| 725 | + .streams() | ||
| 726 | + .iter() | ||
| 727 | + .find(|s| s.params.codec == ec_core::registry::CodecId::H264) | ||
| 728 | + .map(|s| (s.index, s.params.clone())) | ||
| 729 | + .expect("an H.264 track"); | ||
| 730 | + let video = match ¶ms.media { | ||
| 731 | + ec_core::registry::MediaParameters::Video(v) => v, | ||
| 732 | + _ => panic!("the H.264 track carries no video parameters"), | ||
| 733 | + }; | ||
| 734 | + let (w, h) = (video.width as usize, video.height as usize); | ||
| 735 | + let stride = video | ||
| 736 | + .frame_rate | ||
| 737 | + .map_or(25, |r| (r.as_secs_f64().round() as usize).max(1)); | ||
| 738 | + let mut dec = ec_h264::H264Decoder::new(params).expect("decoder"); | ||
| 739 | + let mut sources: Vec<Planes> = Vec::new(); | ||
| 740 | + let mut seen = 0usize; | ||
| 741 | + while sources.len() < 30 { | ||
| 742 | + let Ok(packet) = demux.next_packet() else { | ||
| 743 | + break; | ||
| 744 | + }; | ||
| 745 | + if packet.stream != index { | ||
| 746 | + continue; | ||
| 747 | + } | ||
| 748 | + dec.send_packet(&packet).expect("decode"); | ||
| 749 | + while let Ok(ec_core::frame::Frame::Video(f)) = dec.receive_frame() { | ||
| 750 | + if sources.len() < 30 && seen % stride == 0 { | ||
| 751 | + sources.push(planes_of(&f)); | ||
| 752 | + } | ||
| 753 | + seen += 1; | ||
| 754 | + } | ||
| 755 | + } | ||
| 756 | + assert!( | ||
| 757 | + sources.len() >= 10, | ||
| 758 | + "only {} pictures decoded from {}", | ||
| 759 | + sources.len(), | ||
| 760 | + path.to_string_lossy() | ||
| 761 | + ); | ||
| 762 | + eprintln!( | ||
| 763 | + "{}: {}x{}, {} pictures 1 s apart (every {stride}th of {seen})", | ||
| 764 | + path.to_string_lossy(), | ||
| 765 | + w, | ||
| 766 | + h, | ||
| 767 | + sources.len() | ||
| 768 | + ); | ||
| 769 | + | ||
| 770 | + let run = |t8x8: bool, qp: i32| { | ||
| 771 | + let mut cfg = EncoderConfig::new(w as u32, h as u32); | ||
| 772 | + cfg.qp = qp; | ||
| 773 | + cfg.gop_size = 10; | ||
| 774 | + cfg.cabac = true; | ||
| 775 | + cfg.transform_8x8 = t8x8; | ||
| 776 | + cfg.framerate = 1.0; // the kept pictures are a second apart | ||
| 777 | + cfg.threads = 0; | ||
| 778 | + let mut enc = Encoder::new(cfg).expect("encoder"); | ||
| 779 | + let (mut bits, mut sum_psnr) = (0usize, 0.0); | ||
| 780 | + for (y, u, v) in &sources { | ||
| 781 | + let view = PictureView::i420(w as u32, h as u32, y, u, v); | ||
| 782 | + bits += enc.encode(&view).expect("encode").au.len() * 8; | ||
| 783 | + let rec = enc.reconstruction().expect("reconstruction"); | ||
| 784 | + sum_psnr += psnr(y, &rec.0); | ||
| 785 | + } | ||
| 786 | + let (i8x8, p8x8) = enc.transform_8x8_mbs(); | ||
| 787 | + let share = (i8x8 + p8x8) as f64 / (sources.len() * (w / 16) * (h / 16)) as f64; | ||
| 788 | + (bits as f64, sum_psnr / sources.len() as f64, share) | ||
| 789 | + }; | ||
| 790 | + let qps = [22, 26, 30, 34]; | ||
| 791 | + let off: Vec<_> = qps.iter().map(|&q| run(false, q)).collect(); | ||
| 792 | + let on: Vec<_> = qps.iter().map(|&q| run(true, q)).collect(); | ||
| 793 | + for (i, &qp) in qps.iter().enumerate() { | ||
| 794 | + let (ob, op, _) = off[i]; | ||
| 795 | + let (nb, np, share) = on[i]; | ||
| 796 | + eprintln!( | ||
| 797 | + "qp {qp}: flag-off {ob:.0} bits {op:.2} dB | flag-on {nb:.0} bits {np:.2} dB, 8x8 share {:.1}%", | ||
| 798 | + share * 100.0 | ||
| 799 | + ); | ||
| 800 | + } | ||
| 801 | + let on_curve: Vec<_> = on.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect(); | ||
| 802 | + let off_curve: Vec<_> = off.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect(); | ||
| 803 | + let bd = bd_psnr_delta(&on_curve, &off_curve); | ||
| 804 | + let share = on.iter().map(|&(_, _, s)| s).sum::<f64>() / on.len() as f64; | ||
| 805 | + eprintln!( | ||
| 806 | + "BD-PSNR flag-on vs flag-off over q22/26/30/34: {bd:+.3} dB, average 8x8 MB share {:.1}%", | ||
| 807 | + share * 100.0 | ||
| 808 | + ); | ||
| 809 | +} | ||