S9: drop EncoderConfig::chroma_mode_search and chroma_rd_weight; default off, so the chroma RD search (choose_chroma_mode) was never called
Browse files at this commit06fedd5bf46b1a91dd35a5621654d5857ea8ab88Parent: 6c44dc72 changed files +9 -146
- crates/ec-h265/src/ctu.rs +9 -115
- crates/ec-h265/src/encoder.rs +0 -31
crates/ec-h265/src/ctu.rsmodified+9 -115
| @@ -180,10 +180,6 @@ pub struct CtuEncoder<'a> { | |||
| 180 | strong_smoothing: bool, | 180 | strong_smoothing: bool, |
| 181 | /// How many of the 35 modes get a full rate-distortion trial. | 181 | /// How many of the 35 modes get a full rate-distortion trial. |
| 182 | candidates: usize, | 182 | candidates: usize, |
| 183 | - /// Whether the chroma mode is chosen by rate-distortion or always derived. | ||
| 184 | - chroma_rd: bool, | ||
| 185 | - /// Weight on chroma SSD in that decision, against luma's lambda. | ||
| 186 | - chroma_rd_weight: f64, | ||
| 187 | /// Whether an 8x8 coding unit may split into four 4x4 intra partitions. | 183 | /// Whether an 8x8 coding unit may split into four 4x4 intra partitions. |
| 188 | intra_nxn: bool, | 184 | intra_nxn: bool, |
| 189 | sign_hiding: bool, | 185 | sign_hiding: bool, |
| @@ -229,8 +225,6 @@ impl<'a> CtuEncoder<'a> { | |||
| 229 | qp: i32, | 225 | qp: i32, |
| 230 | strong_smoothing: bool, | 226 | strong_smoothing: bool, |
| 231 | candidates: usize, | 227 | candidates: usize, |
| 232 | - chroma_rd: bool, | ||
| 233 | - chroma_rd_weight: f64, | ||
| 234 | intra_nxn: bool, | 228 | intra_nxn: bool, |
| 235 | sign_hiding: bool, | 229 | sign_hiding: bool, |
| 236 | rdoq: bool, | 230 | rdoq: bool, |
| @@ -260,8 +254,6 @@ impl<'a> CtuEncoder<'a> { | |||
| 260 | lambda_satd: lambda_for(qp_bd).sqrt(), | 254 | lambda_satd: lambda_for(qp_bd).sqrt(), |
| 261 | strong_smoothing, | 255 | strong_smoothing, |
| 262 | candidates: candidates.clamp(1, 35), | 256 | candidates: candidates.clamp(1, 35), |
| 263 | - chroma_rd, | ||
| 264 | - chroma_rd_weight, | ||
| 265 | intra_nxn, | 257 | intra_nxn, |
| 266 | sign_hiding, | 258 | sign_hiding, |
| 267 | rdoq, | 259 | rdoq, |
| @@ -630,12 +622,8 @@ impl<'a> CtuEncoder<'a> { | |||
| 630 | 622 | ||
| 631 | // The chroma mode belongs to the coding unit and derives from the | 623 | // The chroma mode belongs to the coding unit and derives from the |
| 632 | // first partition's luma mode (8.4.3). | 624 | // first partition's luma mode (8.4.3). |
| 633 | - let (chroma_mode, chroma_idx) = if self.chroma_rd { | 625 | + let chroma_mode = modes[0]; |
| 634 | - self.choose_chroma_mode(x, y, 8, MIN_CB_LOG2, modes[0], self.transform_skip, enc) | 626 | + self.write_chroma_mode_syntax(enc); |
| 635 | - } else { | ||
| 636 | - (modes[0], None) | ||
| 637 | - }; | ||
| 638 | - self.write_chroma_mode_syntax(enc, chroma_idx); | ||
| 639 | let cb_cbf = self | 627 | let cb_cbf = self |
| 640 | .code_chroma(x, y, 8, chroma_mode, 1, self.transform_skip) | 628 | .code_chroma(x, y, 8, chroma_mode, 1, self.transform_skip) |
| 641 | .1; | 629 | .1; |
| @@ -726,12 +714,8 @@ impl<'a> CtuEncoder<'a> { | |||
| 726 | // levels in the luma scratch. | 714 | // levels in the luma scratch. |
| 727 | let mode = self.choose_luma_mode(x, y, HALF, LOG2 - 1, &mpm, enc); | 715 | let mode = self.choose_luma_mode(x, y, HALF, LOG2 - 1, &mpm, enc); |
| 728 | self.write_mode_syntax(enc, mode, &mpm); | 716 | self.write_mode_syntax(enc, mode, &mpm); |
| 729 | - let (chroma_mode, chroma_idx) = if self.chroma_rd { | 717 | + let chroma_mode = mode; |
| 730 | - self.choose_chroma_mode(x, y, HALF, LOG2 - 1, mode, chroma_tskip, enc) | 718 | + self.write_chroma_mode_syntax(enc); |
| 731 | - } else { | ||
| 732 | - (mode, None) | ||
| 733 | - }; | ||
| 734 | - self.write_chroma_mode_syntax(enc, chroma_idx); | ||
| 735 | 719 | ||
| 736 | let mut child_levels = [[0i32; 1024]; 4]; | 720 | let mut child_levels = [[0i32; 1024]; 4]; |
| 737 | let mut child_tskip = [self.transform_skip; 4]; | 721 | let mut child_tskip = [self.transform_skip; 4]; |
| @@ -839,12 +823,8 @@ impl<'a> CtuEncoder<'a> { | |||
| 839 | let mpm = self.mpm_at(x, y); | 823 | let mpm = self.mpm_at(x, y); |
| 840 | let mode = self.choose_luma_mode(x, y, n, log2, &mpm, enc); | 824 | let mode = self.choose_luma_mode(x, y, n, log2, &mpm, enc); |
| 841 | self.write_mode_syntax(enc, mode, &mpm); | 825 | self.write_mode_syntax(enc, mode, &mpm); |
| 842 | - let (chroma_mode, chroma_idx) = if self.chroma_rd { | 826 | + let chroma_mode = mode; |
| 843 | - self.choose_chroma_mode(x, y, n, log2, mode, chroma_tskip, enc) | 827 | + self.write_chroma_mode_syntax(enc); |
| 844 | - } else { | ||
| 845 | - (mode, None) | ||
| 846 | - }; | ||
| 847 | - self.write_chroma_mode_syntax(enc, chroma_idx); | ||
| 848 | 828 | ||
| 849 | // Luma was reconstructed by the mode search; chroma is coded now. | 829 | // Luma was reconstructed by the mode search; chroma is coded now. |
| 850 | let luma_cbf = self.scratch.luma_levels[..n * n].iter().any(|&v| v != 0); | 830 | let luma_cbf = self.scratch.luma_levels[..n * n].iter().any(|&v| v != 0); |
| @@ -1795,95 +1775,9 @@ impl<'a> CtuEncoder<'a> { | |||
| 1795 | 1775 | ||
| 1796 | // ---- chroma mode decision -------------------------------------------- | 1776 | // ---- chroma mode decision -------------------------------------------- |
| 1797 | 1777 | ||
| 1798 | - /// Write `intra_chroma_pred_mode`: the single bin 0 for the derived mode, | 1778 | + /// Write `intra_chroma_pred_mode` for the derived mode: the single bin 0. |
| 1799 | - /// or bin 1 and two bypass bits picking one of the four explicit modes. | 1779 | + fn write_chroma_mode_syntax(&self, enc: &mut CabacEncoder) { |
| 1800 | - fn write_chroma_mode_syntax(&self, enc: &mut CabacEncoder, idx: Option<u32>) { | 1780 | + enc.encode_bin(ctx::INTRA_CHROMA_PRED_MODE, 0); |
| 1801 | - match idx { | ||
| 1802 | - None => enc.encode_bin(ctx::INTRA_CHROMA_PRED_MODE, 0), | ||
| 1803 | - Some(i) => { | ||
| 1804 | - enc.encode_bin(ctx::INTRA_CHROMA_PRED_MODE, 1); | ||
| 1805 | - enc.encode_bypass_bits(i, 2); | ||
| 1806 | - } | ||
| 1807 | - } | ||
| 1808 | - } | ||
| 1809 | - | ||
| 1810 | - /// Pick the chroma prediction mode by rate-distortion over the five the | ||
| 1811 | - /// syntax allows: planar, vertical, horizontal and DC — each replaced by | ||
| 1812 | - /// angular 34 when it collides with the luma mode — plus the derived mode, | ||
| 1813 | - /// which is the luma mode itself and costs one bin instead of three. | ||
| 1814 | - /// | ||
| 1815 | - /// Returns the mode and the index to code, `None` meaning derived. Neither | ||
| 1816 | - /// the winner's reconstruction nor its levels survive the trial: the caller | ||
| 1817 | - /// codes the winner again for real. | ||
| 1818 | - #[allow(clippy::too_many_arguments)] | ||
| 1819 | - fn choose_chroma_mode( | ||
| 1820 | - &mut self, | ||
| 1821 | - x: usize, | ||
| 1822 | - y: usize, | ||
| 1823 | - n: usize, | ||
| 1824 | - log2: u32, | ||
| 1825 | - luma_mode: u8, | ||
| 1826 | - chroma_tskip: bool, | ||
| 1827 | - enc: &mut CabacEncoder, | ||
| 1828 | - ) -> (u8, Option<u32>) { | ||
| 1829 | - let mut explicit = [0u8, 26, 10, 1]; | ||
| 1830 | - for mode in explicit.iter_mut() { | ||
| 1831 | - if *mode == luma_mode { | ||
| 1832 | - *mode = 34; | ||
| 1833 | - } | ||
| 1834 | - } | ||
| 1835 | - let start = enc.snapshot(); | ||
| 1836 | - enc.set_counting(true); | ||
| 1837 | - let mut best = (f64::MAX, luma_mode, None); | ||
| 1838 | - for (i, mode) in explicit | ||
| 1839 | - .iter() | ||
| 1840 | - .copied() | ||
| 1841 | - .enumerate() | ||
| 1842 | - .map(|(i, m)| (Some(i as u32), m)) | ||
| 1843 | - .chain(std::iter::once((None, luma_mode))) | ||
| 1844 | - { | ||
| 1845 | - let bits_before = enc.bit_count(); | ||
| 1846 | - self.write_chroma_mode_syntax(enc, i); | ||
| 1847 | - let (cb_ssd, cb_cbf) = self.code_chroma(x, y, n, mode, 1, chroma_tskip); | ||
| 1848 | - let (cr_ssd, cr_cbf) = self.code_chroma(x, y, n, mode, 2, chroma_tskip); | ||
| 1849 | - enc.encode_bin(ctx::CBF_CHROMA, u32::from(cb_cbf)); | ||
| 1850 | - enc.encode_bin(ctx::CBF_CHROMA, u32::from(cr_cbf)); | ||
| 1851 | - let scan = intra::scan_index(mode, log2 - 1, false); | ||
| 1852 | - if cb_cbf { | ||
| 1853 | - let levels = std::mem::take(&mut self.scratch.cb_levels); | ||
| 1854 | - encode_residual( | ||
| 1855 | - enc, | ||
| 1856 | - &levels[..n * n / 4], | ||
| 1857 | - log2 - 1, | ||
| 1858 | - 1, | ||
| 1859 | - scan, | ||
| 1860 | - self.sign_hiding, | ||
| 1861 | - self.ts_flag(chroma_tskip), | ||
| 1862 | - ); | ||
| 1863 | - self.scratch.cb_levels = levels; | ||
| 1864 | - } | ||
| 1865 | - if cr_cbf { | ||
| 1866 | - let levels = std::mem::take(&mut self.scratch.cr_levels); | ||
| 1867 | - encode_residual( | ||
| 1868 | - enc, | ||
| 1869 | - &levels[..n * n / 4], | ||
| 1870 | - log2 - 1, | ||
| 1871 | - 2, | ||
| 1872 | - scan, | ||
| 1873 | - self.sign_hiding, | ||
| 1874 | - self.ts_flag(chroma_tskip), | ||
| 1875 | - ); | ||
| 1876 | - self.scratch.cr_levels = levels; | ||
| 1877 | - } | ||
| 1878 | - let bits = enc.bit_count() - bits_before; | ||
| 1879 | - enc.restore(&start); | ||
| 1880 | - let cost = self.chroma_rd_weight * (cb_ssd + cr_ssd) + self.lambda * bits as f64; | ||
| 1881 | - if cost < best.0 { | ||
| 1882 | - best = (cost, mode, i); | ||
| 1883 | - } | ||
| 1884 | - } | ||
| 1885 | - enc.set_counting(false); | ||
| 1886 | - (best.1, best.2) | ||
| 1887 | } | 1781 | } |
| 1888 | 1782 | ||
| 1889 | // ---- reference samples ----------------------------------------------- | 1783 | // ---- reference samples ----------------------------------------------- |
crates/ec-h265/src/encoder.rsmodified+0 -31
| @@ -126,33 +126,6 @@ pub struct EncoderConfig { | |||
| 126 | /// knee is where the default sits. `EC_H265_RDO_CANDIDATES` in the gate | 126 | /// knee is where the default sits. `EC_H265_RDO_CANDIDATES` in the gate |
| 127 | /// test re-runs the sweep. | 127 | /// test re-runs the sweep. |
| 128 | pub rdo_candidates: usize, | 128 | pub rdo_candidates: usize, |
| 129 | - /// Whether the chroma prediction mode is chosen by rate-distortion over the | ||
| 130 | - /// five modes the syntax allows, instead of always taking the derived mode. | ||
| 131 | - /// | ||
| 132 | - /// Off by default: measured against x265 on four real clips it is a wash, | ||
| 133 | - /// and on screen capture it is a loss (BD-PSNR luma / all-plane, derived | ||
| 134 | - /// mode -> search, `chroma_rd_weight: 1.0`): | ||
| 135 | - /// | ||
| 136 | - /// | clip | derived | search | | ||
| 137 | - /// |------|---------|--------| | ||
| 138 | - /// | 4K film | -0.685 / -0.703 | -0.757 / **-0.606** | | ||
| 139 | - /// | 1138x640 web | -0.507 / -0.556 | -0.584 / **-0.505** | | ||
| 140 | - /// | 1440p screen capture | **-2.465 / -2.360** | -2.617 / -2.460 | | ||
| 141 | - /// | 1080p phone | -0.366 / **-0.387** | -0.477 / -0.384 | | ||
| 142 | - /// | ||
| 143 | - /// Luma falls on every clip because the extra chroma bits come out of the | ||
| 144 | - /// same budget at a fixed QP; all-plane gains 0.10 dB and 0.05 dB on the two | ||
| 145 | - /// camera clips, is flat on the third and loses 0.10 dB on the screen | ||
| 146 | - /// capture, for 10-15% more encode time. The average is +0.01 dB all-plane, | ||
| 147 | - /// which is not a default. [`EncoderConfig::chroma_rd_weight`] was swept at | ||
| 148 | - /// 0.25/0.5/1.0/2.0 and did not change the shape of that table: the screen | ||
| 149 | - /// capture loses at every weight, so the cause is not the lambda. | ||
| 150 | - pub chroma_mode_search: bool, | ||
| 151 | - /// Weight on chroma SSD in that decision, against the luma lambda. Swept | ||
| 152 | - /// at 0.25/0.5/1.0/2.0 on three clips; 1.0 is the best of them everywhere | ||
| 153 | - /// the search wins at all (4K film all-plane: -0.606 at 1.0 against -0.649 | ||
| 154 | - /// at 0.25, -0.619 at 0.5, -0.657 at 2.0). | ||
| 155 | - pub chroma_rd_weight: f64, | ||
| 156 | /// Smallest coding unit the search will produce, 8 or 16. Sixteen is a | 129 | /// Smallest coding unit the search will produce, 8 or 16. Sixteen is a |
| 157 | /// *coarser* search that is not always faster: on real pictures the 8x8 | 130 | /// *coarser* search that is not always faster: on real pictures the 8x8 |
| 158 | /// blocks pay for their search in residual bits (measured: -27% bits and | 131 | /// blocks pay for their search in residual bits (measured: -27% bits and |
| @@ -299,8 +272,6 @@ impl EncoderConfig { | |||
| 299 | threads: default_threads(), | 272 | threads: default_threads(), |
| 300 | ctb_size: 32, | 273 | ctb_size: 32, |
| 301 | rdo_candidates: 5, | 274 | rdo_candidates: 5, |
| 302 | - chroma_mode_search: false, | ||
| 303 | - chroma_rd_weight: 1.0, | ||
| 304 | min_cu_size: 8, | 275 | min_cu_size: 8, |
| 305 | intra_nxn: true, | 276 | intra_nxn: true, |
| 306 | sign_hiding: false, | 277 | sign_hiding: false, |
| @@ -851,8 +822,6 @@ impl Encoder { | |||
| 851 | slice_qp, | 822 | slice_qp, |
| 852 | true, | 823 | true, |
| 853 | self.cfg.rdo_candidates, | 824 | self.cfg.rdo_candidates, |
| 854 | - self.cfg.chroma_mode_search, | ||
| 855 | - self.cfg.chroma_rd_weight, | ||
| 856 | self.cfg.intra_nxn, | 825 | self.cfg.intra_nxn, |
| 857 | self.cfg.sign_hiding, | 826 | self.cfg.sign_hiding, |
| 858 | self.cfg.rdoq, | 827 | self.cfg.rdoq, |