Search each inter partition against its own predictor
Browse files at this commit1db6da07f69583679a7f6c67bef241df8ff46df6Parent: 2b1c4a3- feat
- search each inter partition against its own predictor
2 changed files +91 -3
- crates/ec-h264/src/enc/mb.rs +59 -3
- lanes/h264x264-r2.report.md +32 -0
crates/ec-h264/src/enc/mb.rsmodified+59 -3
| @@ -611,8 +611,24 @@ const MV_BITS_WEIGHT: f64 = 0.5; | ||
| 611 | 611 | /// -0.369/-0.364/-0.353/-0.356 on the screen capture: both contents put the |
| 612 | 612 | /// optimum at twelve, and quarters are worth 0.005 dB on camera content and |
| 613 | 613 | /// 0.016 on screen capture. |
| 614 | +/// | |
| 615 | +/// Re-swept once each partition was searched against its own predictor rather | |
| 616 | +/// than the macroblock's, because that changes what a split is worth: 6, 12 | |
| 617 | +/// and 20 read -0.342/-0.343/-0.346 dB on the film clip and | |
| 618 | +/// -0.322/-0.314/-0.324 on the screen capture. Twelve survives the new path. | |
| 614 | 619 | const QUAD_BITS: f64 = 12.0; |
| 615 | 620 | |
| 621 | +/// The same charge for a 16x8 or 8x16: one extra mb_type bin over P_16x16, | |
| 622 | +/// with no sub_mb_type behind it. | |
| 623 | +/// | |
| 624 | +/// Swept on the per-partition-predictor path at 3, 6 and 10 bits: | |
| 625 | +/// -0.329/-0.343/-0.358 dB on the film clip and -0.310/-0.314/-0.326 on the | |
| 626 | +/// screen capture. Both contents want the halves charged half of what the old | |
| 627 | +/// whole-macroblock predictor made them look worth -- with each half searched | |
| 628 | +/// against its own predictor its mvd is genuinely cheaper, so the search no | |
| 629 | +/// longer has to be talked out of splitting. | |
| 630 | +const HALF_BITS: f64 = 3.0; | |
| 631 | + | |
| 616 | 632 | /// How a P macroblock is partitioned, in the mb_type numbering of Table 7-13. |
| 617 | 633 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 618 | 634 | enum PShape { |
| @@ -655,6 +671,21 @@ impl PShape { | ||
| 655 | 671 | } |
| 656 | 672 | } |
| 657 | 673 | |
| 674 | + /// Which partition covers the 4x4 block at `(x, y)` in macroblock | |
| 675 | + /// coordinates, or `None` when the block is outside the macroblock. | |
| 676 | + fn part_of(self, x: i32, y: i32) -> Option<usize> { | |
| 677 | + if !(0..4).contains(&x) || !(0..4).contains(&y) { | |
| 678 | + return None; | |
| 679 | + } | |
| 680 | + let (x, y) = (x as usize, y as usize); | |
| 681 | + Some(match self { | |
| 682 | + PShape::Whole => 0, | |
| 683 | + PShape::Horizontal => y / 2, | |
| 684 | + PShape::Vertical => x / 2, | |
| 685 | + PShape::Quad => (y / 2) * 2 + x / 2, | |
| 686 | + }) | |
| 687 | + } | |
| 688 | + | |
| 658 | 689 | /// `MbPartWidth`/`MbPartHeight` in 4x4 blocks, which select the |
| 659 | 690 | /// directional shortcuts of 8.4.1.3. |
| 660 | 691 | fn part_blocks(self) -> (usize, usize) { |
| @@ -867,14 +898,39 @@ fn choose_inter(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> Inter | ||
| 867 | 898 | if shape == PShape::Quad { |
| 868 | 899 | QUAD_BITS |
| 869 | 900 | } else { |
| 870 | - 6.0 | |
| 901 | + HALF_BITS | |
| 871 | 902 | }, |
| 872 | 903 | ); |
| 873 | 904 | let mut mvs = [[0i16; 2]; 4]; |
| 905 | + let (pw, ph) = shape.part_blocks(); | |
| 874 | 906 | for part in 0..shape.parts() { |
| 875 | 907 | let ((bx, by), (w, h)) = shape.geometry(part); |
| 876 | - let seeds = [mv16, mvp, skip_mv, [0, 0]]; | |
| 877 | - let (mv, cost) = search(bx * 4, by * 4, w, h, mvp, &seeds); | |
| 908 | + // Each partition is searched against the predictor it will | |
| 909 | + // actually be coded with (8.4.1.3), not against the whole | |
| 910 | + // macroblock's: the partitions before it in this macroblock | |
| 911 | + // are already chosen, so the neighbourhood the predictor reads | |
| 912 | + // can be completed from `mvs`. | |
| 913 | + let mut n = ctx.neighbours(pic, bx, by, pw, 0); | |
| 914 | + let inside = |x: i32, y: i32, nb: &mut crate::mv::Nb| { | |
| 915 | + if let Some(p) = shape.part_of(x, y).filter(|&p| p < part) { | |
| 916 | + *nb = crate::mv::Nb { | |
| 917 | + avail: true, | |
| 918 | + mv: mvs[p], | |
| 919 | + ref_idx: 0, | |
| 920 | + }; | |
| 921 | + } | |
| 922 | + }; | |
| 923 | + inside(bx as i32 - 1, by as i32, &mut n[0]); | |
| 924 | + inside(bx as i32, by as i32 - 1, &mut n[1]); | |
| 925 | + let cx = bx as i32 + pw as i32; | |
| 926 | + if shape.part_of(cx, by as i32 - 1).is_some() { | |
| 927 | + inside(cx, by as i32 - 1, &mut n[2]); | |
| 928 | + } else if !n[2].avail { | |
| 929 | + inside(bx as i32 - 1, by as i32 - 1, &mut n[2]); | |
| 930 | + } | |
| 931 | + let part_mvp = predict_mv(&n, 0, pw, ph, part); | |
| 932 | + let seeds = [mv16, part_mvp, mvp, skip_mv, [0, 0]]; | |
| 933 | + let (mv, cost) = search(bx * 4, by * 4, w, h, part_mvp, &seeds); | |
| 878 | 934 | mvs[part] = mv; |
| 879 | 935 | total += cost; |
| 880 | 936 | } |
lanes/h264x264-r2.report.mdmodified+32 -0
| @@ -571,3 +571,35 @@ that comes from: each sub-partition is searched against the whole macroblock's | ||
| 571 | 571 | motion predictor, not its own, so the search misjudges what a split costs to |
| 572 | 572 | signal and picks the wrong ones. Deriving the real predictor inside the search |
| 573 | 573 | is the next slice. |
| 574 | + | |
| 575 | +## Each partition searched against its own predictor | |
| 576 | + | |
| 577 | +Charging P_8x8 *more* rather than less was the tell. If the search wanted the | |
| 578 | +quarters talked out of, the thing it was mispricing was not the mb_type but the | |
| 579 | +mvd behind it -- and it was, because every partition of a split macroblock was | |
| 580 | +searched against the whole macroblock's motion predictor. 8.4.1.3 gives 16x8 and | |
| 581 | +8x16 partitions directional shortcuts and 8x8 sub-partitions the median of their | |
| 582 | +own neighbourhood, so the mvd the search costed was not the mvd the stream would | |
| 583 | +carry. Partitions whose true predictor was close by looked expensive; the search | |
| 584 | +declined them. | |
| 585 | + | |
| 586 | +The predictor is now derived inside the search loop, per partition. The | |
| 587 | +neighbourhood comes from the picture as before, then the entries that fall | |
| 588 | +inside this macroblock on partitions already chosen are overwritten from the | |
| 589 | +motion vectors this loop has picked -- which is exactly what the encoder will do | |
| 590 | +when it codes them. The chosen predictor also seeds the search. | |
| 591 | + | |
| 592 | +BD-PSNR against x264 at matched features, consecutive pictures, `Preset::Balanced`: | |
| 593 | + | |
| 594 | +| clip | before | after | | |
| 595 | +|---|---|---| | |
| 596 | +| 3840x1608 film | -0.377 dB | **-0.329 dB** | | |
| 597 | +| 2560x1440 screen capture | -0.353 dB | **-0.310 dB** | | |
| 598 | + | |
| 599 | +Both shape-signalling charges were re-swept on the new path, because a change to | |
| 600 | +what a split costs to code changes what it is worth to charge for. The quarters' | |
| 601 | +twelve bits survive (6/12/20 read -0.342/-0.343/-0.346 film, -0.322/-0.314/-0.324 | |
| 602 | +screen). The halves' six do not: at 3/6/10 bits they read -0.329/-0.343/-0.358 | |
| 603 | +film and -0.310/-0.314/-0.326 screen, so the charge is now three. With each half | |
| 604 | +searched against its own predictor its mvd is genuinely cheaper, and the search | |
| 605 | +no longer needs to be argued out of splitting. |