Trace pre/post-deblock SSD under rd-ablation
Browse files at this commitad5202c89fb7da6ca8cfaca8dc8fc91c54b58777Parent: 74491d6- feat
- trace pre/post-deblock SSD under rd-ablation
1 changed file +80 -0
crates/ec-h264/src/enc/mod.rsmodified+80 -0
| @@ -36,6 +36,8 @@ use ec_h264_syntax::{SliceType, Sps}; | |||
| 36 | use crate::decoder::deblock_picture; | 36 | use crate::decoder::deblock_picture; |
| 37 | use crate::dpb::{Picture, SliceParams as DeblockParams}; | 37 | use crate::dpb::{Picture, SliceParams as DeblockParams}; |
| 38 | use crate::entropy::{FLAG_INTER, FLAG_TRANS8X8}; | 38 | use crate::entropy::{FLAG_INTER, FLAG_TRANS8X8}; |
| 39 | +#[cfg(feature = "rd-ablation")] | ||
| 40 | +use crate::entropy::FLAG_SKIP; | ||
| 39 | use crate::transform::{LevelScale4x4, LevelScale8x8}; | 41 | use crate::transform::{LevelScale4x4, LevelScale8x8}; |
| 40 | 42 | ||
| 41 | use entropy::EncEntropy; | 43 | use entropy::EncEntropy; |
| @@ -399,7 +401,16 @@ impl Encoder { | |||
| 399 | } | 401 | } |
| 400 | } | 402 | } |
| 401 | } | 403 | } |
| 404 | + // H5 measurement hook: the chosen candidate's trial SSD is the | ||
| 405 | + // pre-deblock SSD of the macroblock in this picture (round 4 verified | ||
| 406 | + // trial bits and SSD against the final picture), so capturing the | ||
| 407 | + // per-macroblock SSD right before and right after the filter is the | ||
| 408 | + // exact pre/post comparison with no hooks inside the RD trials. | ||
| 409 | + #[cfg(feature = "rd-ablation")] | ||
| 410 | + let rd_pre = rd_trace_capture(master, &self.src); | ||
| 402 | deblock_picture(master); | 411 | deblock_picture(master); |
| 412 | + #[cfg(feature = "rd-ablation")] | ||
| 413 | + rd_trace_write(master, &self.src, self.frames, rd_pre); | ||
| 403 | master.extend_borders(); | 414 | master.extend_borders(); |
| 404 | master.complete = true; | 415 | master.complete = true; |
| 405 | std::mem::swap(master, &mut self.reference); | 416 | std::mem::swap(master, &mut self.reference); |
| @@ -610,6 +621,75 @@ fn apply_rd_ablation(cfg: &mut EncoderConfig) { | |||
| 610 | } | 621 | } |
| 611 | } | 622 | } |
| 612 | 623 | ||
| 624 | +/// Luma and chroma SSD of every macroblock against the source, before the | ||
| 625 | +/// deblocking filter — the distortion the RD decision actually paid. | ||
| 626 | +#[cfg(feature = "rd-ablation")] | ||
| 627 | +fn rd_trace_capture(pic: &Picture, src: &Source) -> Option<Vec<(i64, i64)>> { | ||
| 628 | + if std::env::var_os("EC_H264_RD_TRACE").is_none() { | ||
| 629 | + return None; | ||
| 630 | + } | ||
| 631 | + let mut out = Vec::with_capacity(pic.mb_w * pic.mb_h); | ||
| 632 | + for mb_y in 0..pic.mb_h { | ||
| 633 | + for mb_x in 0..pic.mb_w { | ||
| 634 | + out.push(rd_trace_ssd(pic, src, mb_x, mb_y)); | ||
| 635 | + } | ||
| 636 | + } | ||
| 637 | + Some(out) | ||
| 638 | +} | ||
| 639 | + | ||
| 640 | +/// One line per macroblock: `frame addr intra trans8 skip qp pre_luma | ||
| 641 | +/// pre_chroma post_luma post_chroma`, appended to `$EC_H264_RD_TRACE`. | ||
| 642 | +#[cfg(feature = "rd-ablation")] | ||
| 643 | +fn rd_trace_write(pic: &Picture, src: &Source, frame: u64, pre: Option<Vec<(i64, i64)>>) { | ||
| 644 | + use std::io::Write; | ||
| 645 | + let Some(pre) = pre else { return }; | ||
| 646 | + let mut out = std::fs::OpenOptions::new() | ||
| 647 | + .create(true) | ||
| 648 | + .append(true) | ||
| 649 | + .open(std::env::var_os("EC_H264_RD_TRACE").expect("checked in capture")) | ||
| 650 | + .expect("open EC_H264_RD_TRACE"); | ||
| 651 | + for (addr, &(pre_luma, pre_chroma)) in pre.iter().enumerate() { | ||
| 652 | + let (post_luma, post_chroma) = rd_trace_ssd(pic, src, addr % pic.mb_w, addr / pic.mb_w); | ||
| 653 | + let f = pic.mb_flags[addr]; | ||
| 654 | + let _ = writeln!( | ||
| 655 | + out, | ||
| 656 | + "{frame} {addr} {} {} {} {} {pre_luma} {pre_chroma} {post_luma} {post_chroma}", | ||
| 657 | + u8::from(f & FLAG_INTER == 0), | ||
| 658 | + u8::from(f & FLAG_TRANS8X8 != 0), | ||
| 659 | + u8::from(f & FLAG_SKIP != 0), | ||
| 660 | + pic.mb_qp[addr] | ||
| 661 | + ); | ||
| 662 | + } | ||
| 663 | +} | ||
| 664 | + | ||
| 665 | +/// Luma and chroma SSD of one macroblock against the source planes. | ||
| 666 | +#[cfg(feature = "rd-ablation")] | ||
| 667 | +fn rd_trace_ssd(pic: &Picture, src: &Source, mb_x: usize, mb_y: usize) -> (i64, i64) { | ||
| 668 | + let mut luma = 0i64; | ||
| 669 | + let stride = pic.y.stride; | ||
| 670 | + let origin = pic.y.at(mb_x * 16, mb_y * 16); | ||
| 671 | + for row in 0..16 { | ||
| 672 | + let s = (mb_y * 16 + row) * src.stride + mb_x * 16; | ||
| 673 | + for x in 0..16 { | ||
| 674 | + let d = i64::from(src.y[s + x]) - i64::from(pic.y.data[origin + row * stride + x]); | ||
| 675 | + luma += d * d; | ||
| 676 | + } | ||
| 677 | + } | ||
| 678 | + let mut chroma = 0i64; | ||
| 679 | + for (plane, source) in [(&pic.cb, &src.u), (&pic.cr, &src.v)] { | ||
| 680 | + let stride = plane.stride; | ||
| 681 | + let origin = plane.at(mb_x * 8, mb_y * 8); | ||
| 682 | + for row in 0..8 { | ||
| 683 | + let s = (mb_y * 8 + row) * src.c_stride + mb_x * 8; | ||
| 684 | + for x in 0..8 { | ||
| 685 | + let d = i64::from(source[s + x]) - i64::from(plane.data[origin + row * stride + x]); | ||
| 686 | + chroma += d * d; | ||
| 687 | + } | ||
| 688 | + } | ||
| 689 | + } | ||
| 690 | + (luma, chroma) | ||
| 691 | +} | ||
| 692 | + | ||
| 613 | #[cfg(feature = "rd-ablation")] | 693 | #[cfg(feature = "rd-ablation")] |
| 614 | fn rd_lambda_standard(cfg: &EncoderConfig) -> bool { | 694 | fn rd_lambda_standard(cfg: &EncoderConfig) -> bool { |
| 615 | cfg.rd_lambda_standard | 695 | cfg.rd_lambda_standard |