Give the chroma DC block a zero test
Browse files at this commit59070a12645f930c6f6d07376fca03a54a4fae22Parent: 832b640- perf
- give the chroma DC block a zero test
1 changed file +64 -1
- crates/ec-h264/src/enc/mb.rs +64 -1
crates/ec-h264/src/enc/mb.rsmodified+64 -1
| @@ -1338,6 +1338,26 @@ const CBP_CHROMA_BIT: f64 = 16.0; | ||
| 1338 | 1338 | /// takes without losing ground, and it is worth 0.06 dB on screen capture. |
| 1339 | 1339 | const GROUP_LAMBDA: f64 = 0.6; |
| 1340 | 1340 | |
| 1341 | +/// How much of the squared-error lambda the chroma DC zero test uses. | |
| 1342 | +/// | |
| 1343 | +/// BD-PSNR-YUV against x264 at GOP 10 over QP 22/26/30/34, `Preset::Balanced`, | |
| 1344 | +/// with the test off against a ladder of multipliers: | |
| 1345 | +/// | |
| 1346 | +/// ```text | |
| 1347 | +/// film screen phone handheld | |
| 1348 | +/// off -0.504 -0.227 -0.300 -0.886 | |
| 1349 | +/// 0.25 -0.506 -0.227 | |
| 1350 | +/// 0.5 -0.500 -0.220 -0.297 -0.884 | |
| 1351 | +/// 0.75 -0.500 -0.223 | |
| 1352 | +/// 1.0 -0.518 -0.246 | |
| 1353 | +/// 2.0 -0.708 -0.566 | |
| 1354 | +/// ``` | |
| 1355 | +/// | |
| 1356 | +/// Zeroing on squared error alone (a multiplier of 0) never fires, so the rate | |
| 1357 | +/// term is the whole test; above the peak it throws away chroma the picture | |
| 1358 | +/// needs. | |
| 1359 | +const CHROMA_DC_LAMBDA: f64 = 0.5; | |
| 1360 | + | |
| 1341 | 1361 | /// Squared error of coding `levels`, in the pixels the block reconstructs to. |
| 1342 | 1362 | fn block_ssd(e: &MbEnc<'_>, source: &[i32; 16], levels: &[i32; 16], qp: i32, skip_dc: bool) -> i64 { |
| 1343 | 1363 | let mut raster = [0i32; 16]; |
| @@ -1737,10 +1757,53 @@ fn code_chroma( | ||
| 1737 | 1757 | any_ac = false; |
| 1738 | 1758 | } |
| 1739 | 1759 | } |
| 1760 | + // The chroma DC block had no zero test at all: whatever the quantiser | |
| 1761 | + // produced was coded. Its four coefficients spread over all 64 samples of | |
| 1762 | + // the component, and with only DC in the block the inverse transform gives | |
| 1763 | + // every sample the same (dc + 32) >> 6, so the squared error zeroing costs | |
| 1764 | + // is exact in one pass over the AC residual that is already reconstructed | |
| 1765 | + // here. | |
| 1740 | 1766 | let mut any_dc = false; |
| 1741 | 1767 | for comp in 0..2 { |
| 1742 | 1768 | forward_hadamard_2x2(&mut dc_lists[comp]); |
| 1743 | - let nz = quant_chroma_dc(&dc_lists[comp], qp_c, intra, &mut lv.chroma_dc[comp]); | |
| 1769 | + let mut nz = quant_chroma_dc(&dc_lists[comp], qp_c, intra, &mut lv.chroma_dc[comp]); | |
| 1770 | + if nz > 0 { | |
| 1771 | + let mut dc = [ | |
| 1772 | + lv.chroma_dc[comp][0], | |
| 1773 | + lv.chroma_dc[comp][1], | |
| 1774 | + lv.chroma_dc[comp][2], | |
| 1775 | + lv.chroma_dc[comp][3], | |
| 1776 | + ]; | |
| 1777 | + chroma_dc_transform_420(&mut dc, &e.ls, qp_c); | |
| 1778 | + let mut delta = 0i64; | |
| 1779 | + for blk in 0..4 { | |
| 1780 | + let mut resid = [0i32; 16]; | |
| 1781 | + if any_ac && lv.chroma_nz[comp][blk] > 0 { | |
| 1782 | + let mut raster = [0i32; 16]; | |
| 1783 | + unzigzag_ac15(&ac[comp][blk], &mut raster); | |
| 1784 | + dequant_4x4(&mut raster, &e.ls, qp_c, true); | |
| 1785 | + inverse_transform_4x4(&raster, &mut resid); | |
| 1786 | + } | |
| 1787 | + let c = i64::from((dc[blk] + 32) >> 6); | |
| 1788 | + let err: i64 = (0..16) | |
| 1789 | + .map(|i| i64::from(srcs[comp][blk][i] - resid[i])) | |
| 1790 | + .sum(); | |
| 1791 | + delta += 16 * c * c - 2 * c * err; | |
| 1792 | + } | |
| 1793 | + let rate = match w.residual_block_cost( | |
| 1794 | + &lv.chroma_dc[comp], | |
| 1795 | + BlockCat::ChromaDc(comp as u8), | |
| 1796 | + Some(1), | |
| 1797 | + Some(1), | |
| 1798 | + ) { | |
| 1799 | + Some(bits) => f64::from(bits) / 256.0, | |
| 1800 | + None => block_bits(&lv.chroma_dc[comp]) as f64, | |
| 1801 | + }; | |
| 1802 | + if delta as f64 + CHROMA_DC_LAMBDA * lambda_ssd(qp_c) * rate >= 0.0 { | |
| 1803 | + lv.chroma_dc[comp] = [0; 16]; | |
| 1804 | + nz = 0; | |
| 1805 | + } | |
| 1806 | + } | |
| 1744 | 1807 | if nz > 0 { |
| 1745 | 1808 | any_dc = true; |
| 1746 | 1809 | } |