h264: price the 8x8 inter blocks on their own bins too
Browse files at this commitfad429706a3281a599a67f3c4bfb6c195008e6c9Parent: e21a7645 changed files +227 -26
- crates/ec-h264/src/enc/cabac_enc.rs +75 -14
- crates/ec-h264/src/enc/entropy.rs +7 -0
- crates/ec-h264/src/enc/mb.rs +102 -7
- crates/ec-h264/src/enc/quant.rs +15 -5
- lanes/h264x264-r2.report.md +28 -0
crates/ec-h264/src/enc/cabac_enc.rsmodified+75 -14
| @@ -393,21 +393,23 @@ impl CabacEnc { | |||
| 393 | /// coded_block_flag: for 4:2:0 the coded_block_pattern bit is the whole | 393 | /// coded_block_flag: for 4:2:0 the coded_block_pattern bit is the whole |
| 394 | /// signal (7.3.5.3.3), so an all-zero block must not reach here. | 394 | /// signal (7.3.5.3.3), so an all-zero block must not reach here. |
| 395 | pub(crate) fn residual_block_8x8(&mut self, coeff: &[i32; 64]) -> u8 { | 395 | pub(crate) fn residual_block_8x8(&mut self, coeff: &[i32; 64]) -> u8 { |
| 396 | - let last = (0..64) | 396 | + residual_bins_8x8(self, coeff) |
| 397 | - .rev() | 397 | + } |
| 398 | - .find(|&i| coeff[i] != 0) | 398 | + |
| 399 | - .expect("an 8x8 block with cbp set has a coefficient"); | 399 | + /// What [`Self::residual_block_8x8`] would spend on `coeff`, in 1/256ths |
| 400 | - for i in 0..63 { | 400 | + /// of a bit, against the context state as it stands. An all-zero block |
| 401 | - let significant = coeff[i] != 0; | 401 | + /// costs nothing here: it is not coded at all, and the coded block pattern |
| 402 | - self.decision(OFF_SIG_8X8 + usize::from(SIG_8X8_FRAME[i]), significant); | 402 | + /// that says so is priced by whoever writes it. |
| 403 | - if significant { | 403 | + pub(crate) fn residual_block_8x8_cost(&self, coeff: &[i32; 64]) -> u32 { |
| 404 | - self.decision(OFF_LAST_8X8 + usize::from(LAST_8X8[i]), i == last); | 404 | + if coeff.iter().all(|&c| c == 0) { |
| 405 | - if i == last { | 405 | + return 0; |
| 406 | - break; | ||
| 407 | - } | ||
| 408 | - } | ||
| 409 | } | 406 | } |
| 410 | - levels_bins(self, coeff, last, OFF_ABS_8X8, false) | 407 | + let mut sink = CostSink { |
| 408 | + state: self.state, | ||
| 409 | + bits: 0, | ||
| 410 | + }; | ||
| 411 | + residual_bins_8x8(&mut sink, coeff); | ||
| 412 | + sink.bits | ||
| 411 | } | 413 | } |
| 412 | 414 | ||
| 413 | /// One residual block in scan order (7.3.5.3.3): coded_block_flag, the | 415 | /// One residual block in scan order (7.3.5.3.3): coded_block_flag, the |
| @@ -550,6 +552,26 @@ impl BinSink for CostSink { | |||
| 550 | 552 | ||
| 551 | /// coded_block_flag, the significance map and the levels of one residual block | 553 | /// coded_block_flag, the significance map and the levels of one residual block |
| 552 | /// (7.3.5.3.3), against a caller-derived coded_block_flag context. | 554 | /// (7.3.5.3.3), against a caller-derived coded_block_flag context. |
| 555 | +/// The bins of one 8x8 luma residual block (7.3.5.3.3, cat 5): its own | ||
| 556 | +/// significance map and last-position contexts, then the levels. | ||
| 557 | +fn residual_bins_8x8<S: BinSink>(s: &mut S, coeff: &[i32; 64]) -> u8 { | ||
| 558 | + let last = (0..64) | ||
| 559 | + .rev() | ||
| 560 | + .find(|&i| coeff[i] != 0) | ||
| 561 | + .expect("an 8x8 block with cbp set has a coefficient"); | ||
| 562 | + for i in 0..63 { | ||
| 563 | + let significant = coeff[i] != 0; | ||
| 564 | + s.bin(OFF_SIG_8X8 + usize::from(SIG_8X8_FRAME[i]), significant); | ||
| 565 | + if significant { | ||
| 566 | + s.bin(OFF_LAST_8X8 + usize::from(LAST_8X8[i]), i == last); | ||
| 567 | + if i == last { | ||
| 568 | + break; | ||
| 569 | + } | ||
| 570 | + } | ||
| 571 | + } | ||
| 572 | + levels_bins(s, coeff, last, OFF_ABS_8X8, false) | ||
| 573 | +} | ||
| 574 | + | ||
| 553 | fn residual_bins<S: BinSink>(s: &mut S, coeff: &[i32], cat: BlockCat, cbf_ctx: usize) -> u8 { | 575 | fn residual_bins<S: BinSink>(s: &mut S, coeff: &[i32], cat: BlockCat, cbf_ctx: usize) -> u8 { |
| 554 | let cat_i = cat.ctx_block_cat(); | 576 | let cat_i = cat.ctx_block_cat(); |
| 555 | let max = cat.max_num_coeff(); | 577 | let max = cat.max_num_coeff(); |
| @@ -785,4 +807,43 @@ mod tests { | |||
| 785 | "predicted {predicted}/256 bits, spent {spent}/256" | 807 | "predicted {predicted}/256 bits, spent {spent}/256" |
| 786 | ); | 808 | ); |
| 787 | } | 809 | } |
| 810 | + | ||
| 811 | + /// The same claim for the 8x8 block: `residual_block_8x8_cost` predicts | ||
| 812 | + /// what `residual_block_8x8` spends, which is what the 8x8 quantisation | ||
| 813 | + /// decision buys its levels with. | ||
| 814 | + #[test] | ||
| 815 | + fn residual_8x8_cost_predicts_the_bits_spent() { | ||
| 816 | + let mut state = 0x0bad_f00du32; | ||
| 817 | + let mut rand = move |m: u32| { | ||
| 818 | + state ^= state << 13; | ||
| 819 | + state ^= state >> 17; | ||
| 820 | + state ^= state << 5; | ||
| 821 | + state % m | ||
| 822 | + }; | ||
| 823 | + let mut enc = CabacEnc::new(BitWriter::new(), 26, 0); | ||
| 824 | + let (mut predicted, mut spent) = (0i64, 0i64); | ||
| 825 | + for _ in 0..200 { | ||
| 826 | + let mut coeff = [0i32; 64]; | ||
| 827 | + let nz = 1 + rand(20); | ||
| 828 | + for _ in 0..nz { | ||
| 829 | + let i = rand(64) as usize; | ||
| 830 | + let mag = 1 + rand(6) as i32 * rand(4) as i32; | ||
| 831 | + coeff[i] = if rand(2) == 0 { mag } else { -mag }; | ||
| 832 | + } | ||
| 833 | + if coeff.iter().all(|&c| c == 0) { | ||
| 834 | + coeff[0] = 1; | ||
| 835 | + } | ||
| 836 | + let cost = enc.residual_block_8x8_cost(&coeff); | ||
| 837 | + let before = enc.bit_len() * 256 + u64::from(enc.range.leading_zeros()); | ||
| 838 | + enc.residual_block_8x8(&coeff); | ||
| 839 | + let after = enc.bit_len() * 256 + u64::from(enc.range.leading_zeros()); | ||
| 840 | + predicted += i64::from(cost); | ||
| 841 | + spent += after as i64 - before as i64; | ||
| 842 | + } | ||
| 843 | + let err = (predicted - spent).abs(); | ||
| 844 | + assert!( | ||
| 845 | + err * 100 < spent, | ||
| 846 | + "predicted {predicted}/256 bits, spent {spent}/256" | ||
| 847 | + ); | ||
| 848 | + } | ||
| 788 | } | 849 | } |
crates/ec-h264/src/enc/entropy.rsmodified+7 -0
| @@ -197,6 +197,13 @@ impl EncEntropy { | |||
| 197 | /// What [`Self::residual_block`] would spend on `coeff`, in 1/256ths of a | 197 | /// What [`Self::residual_block`] would spend on `coeff`, in 1/256ths of a |
| 198 | /// bit, without writing it. `None` under CAVLC, whose cost a caller can | 198 | /// bit, without writing it. `None` under CAVLC, whose cost a caller can |
| 199 | /// only estimate. | 199 | /// only estimate. |
| 200 | + pub(crate) fn residual_block_8x8_cost(&self, coeff: &[i32; 64]) -> Option<u32> { | ||
| 201 | + match self { | ||
| 202 | + EncEntropy::Cavlc { .. } => None, | ||
| 203 | + EncEntropy::Cabac(c) => Some(c.residual_block_8x8_cost(coeff)), | ||
| 204 | + } | ||
| 205 | + } | ||
| 206 | + | ||
| 200 | pub(crate) fn residual_block_cost( | 207 | pub(crate) fn residual_block_cost( |
| 201 | &self, | 208 | &self, |
| 202 | coeff: &[i32], | 209 | coeff: &[i32], |
crates/ec-h264/src/enc/mb.rsmodified+102 -7
| @@ -39,7 +39,7 @@ use crate::transform::{ | |||
| 39 | 39 | ||
| 40 | use super::quant::{ | 40 | use super::quant::{ |
| 41 | forward_4x4, forward_8x8, forward_hadamard_2x2, forward_hadamard_4x4, quant_4x4, | 41 | forward_4x4, forward_8x8, forward_hadamard_2x2, forward_hadamard_4x4, quant_4x4, |
| 42 | - quant_4x4_offset, quant_8x8, quant_chroma_dc, quant_luma_dc, | 42 | + quant_4x4_offset, quant_8x8, quant_8x8_offset, quant_chroma_dc, quant_luma_dc, |
| 43 | }; | 43 | }; |
| 44 | use super::{ | 44 | use super::{ |
| 45 | entropy::{EncEntropy, sub_block_4x4}, | 45 | entropy::{EncEntropy, sub_block_4x4}, |
| @@ -987,6 +987,15 @@ const RDOQ_MAX_LEVEL: i32 = 2; | |||
| 987 | /// back; what pays is only the pruning. | 987 | /// back; what pays is only the pruning. |
| 988 | const RDOQ_CHROMA_DEAD_ZONE: i32 = 6; | 988 | const RDOQ_CHROMA_DEAD_ZONE: i32 = 6; |
| 989 | 989 | ||
| 990 | +/// The rounding the 8x8 luma path keeps under the search. It answers the way | ||
| 991 | +/// 4x4 luma did, not the way chroma did: measured as BD-PSNR of the opt-in | ||
| 992 | +/// `transform_8x8` flag against flag-off, which isolates this path because the | ||
| 993 | +/// flag-off curve does not run through it. Film 3840x1608 / screen capture | ||
| 994 | +/// 2560x1440, no search +0.535 / +0.042, 1/6 +0.551 / --, 1/3 +0.605 / +0.075, | ||
| 995 | +/// and the 1/2 kept here +0.619 / +0.067. The 8x8 blocks carry enough | ||
| 996 | +/// coefficients for the extra levels to pay for themselves, as 4x4 luma's do. | ||
| 997 | +const RDOQ8_DEAD_ZONE: i32 = 2; | ||
| 998 | + | ||
| 990 | /// Rate-distortion quantisation of one inter luma block: each level is offered | 999 | /// Rate-distortion quantisation of one inter luma block: each level is offered |
| 991 | /// the two cheaper magnitudes below it, and takes one when the squared error it | 1000 | /// the two cheaper magnitudes below it, and takes one when the squared error it |
| 992 | /// gives up costs less than the bits it saves. | 1001 | /// gives up costs less than the bits it saves. |
| @@ -1188,6 +1197,7 @@ fn code_luma_i16( | |||
| 1188 | } | 1197 | } |
| 1189 | 1198 | ||
| 1190 | /// Chroma residual for both components (8.5.11 + the AC path). | 1199 | /// Chroma residual for both components (8.5.11 + the AC path). |
| 1200 | +#[allow(clippy::too_many_arguments)] | ||
| 1191 | fn code_chroma( | 1201 | fn code_chroma( |
| 1192 | pic: &mut Picture, | 1202 | pic: &mut Picture, |
| 1193 | e: &MbEnc<'_>, | 1203 | e: &MbEnc<'_>, |
| @@ -1574,9 +1584,11 @@ fn predicted_mode8(pic: &Picture, nbr: &MbNeighbors, bx: usize, by: usize, blk8: | |||
| 1574 | /// Residual of one 8x8 luma block against the prediction already in the | 1584 | /// Residual of one 8x8 luma block against the prediction already in the |
| 1575 | /// plane: forward transform, quantise, and — when anything survives — | 1585 | /// plane: forward transform, quantise, and — when anything survives — |
| 1576 | /// reconstruct with the decoder's own inverse. Returns the non-zero count. | 1586 | /// reconstruct with the decoder's own inverse. Returns the non-zero count. |
| 1587 | +#[allow(clippy::too_many_arguments)] | ||
| 1577 | fn code_block_8x8( | 1588 | fn code_block_8x8( |
| 1578 | pic: &mut Picture, | 1589 | pic: &mut Picture, |
| 1579 | e: &MbEnc<'_>, | 1590 | e: &MbEnc<'_>, |
| 1591 | + w: &EncEntropy, | ||
| 1580 | x: usize, | 1592 | x: usize, |
| 1581 | y: usize, | 1593 | y: usize, |
| 1582 | qp: i32, | 1594 | qp: i32, |
| @@ -1592,16 +1604,29 @@ fn code_block_8x8( | |||
| 1592 | - i32::from(pic.y.data[origin + ry * stride + rx]); | 1604 | - i32::from(pic.y.data[origin + ry * stride + rx]); |
| 1593 | } | 1605 | } |
| 1594 | } | 1606 | } |
| 1595 | - let nz = quant_8x8(&forward_8x8(&d), &e.ls8, qp, intra, out); | 1607 | + let rdoq = !intra && w.is_cabac(); |
| 1608 | + let fwd = forward_8x8(&d); | ||
| 1609 | + let mut nz = if rdoq { | ||
| 1610 | + quant_8x8_offset(&fwd, &e.ls8, qp, RDOQ8_DEAD_ZONE, out) | ||
| 1611 | + } else { | ||
| 1612 | + quant_8x8(&fwd, &e.ls8, qp, intra, out) | ||
| 1613 | + }; | ||
| 1596 | if nz == 0 { | 1614 | if nz == 0 { |
| 1597 | return 0; | 1615 | return 0; |
| 1598 | } | 1616 | } |
| 1617 | + if rdoq { | ||
| 1618 | + nz = rdoq_8x8(e, w, &d, out, qp); | ||
| 1619 | + if nz == 0 { | ||
| 1620 | + return 0; | ||
| 1621 | + } | ||
| 1622 | + } | ||
| 1599 | let mut raster = [0i32; 64]; | 1623 | let mut raster = [0i32; 64]; |
| 1600 | unzigzag_8x8(out, &mut raster); | 1624 | unzigzag_8x8(out, &mut raster); |
| 1601 | dequant_8x8(&mut raster, &e.ls8, qp); | 1625 | dequant_8x8(&mut raster, &e.ls8, qp); |
| 1602 | let mut resid = [0i32; 64]; | 1626 | let mut resid = [0i32; 64]; |
| 1603 | inverse_transform_8x8(&raster, &mut resid); | 1627 | inverse_transform_8x8(&raster, &mut resid); |
| 1604 | - if !intra | 1628 | + if !rdoq |
| 1629 | + && !intra | ||
| 1605 | && !worth_coding( | 1630 | && !worth_coding( |
| 1606 | &d, | 1631 | &d, |
| 1607 | &resid, | 1632 | &resid, |
| @@ -1619,9 +1644,11 @@ fn code_block_8x8( | |||
| 1619 | /// Code the luma of an Intra_8x8 macroblock, the 8x8 sibling of | 1644 | /// Code the luma of an Intra_8x8 macroblock, the 8x8 sibling of |
| 1620 | /// [`code_intra_4x4`]: neighbours are the decoder's filtered reference | 1645 | /// [`code_intra_4x4`]: neighbours are the decoder's filtered reference |
| 1621 | /// samples, and every 4x4 mode slot of a block carries its mode. | 1646 | /// samples, and every 4x4 mode slot of a block carries its mode. |
| 1647 | +#[allow(clippy::too_many_arguments)] | ||
| 1622 | fn code_intra_8x8( | 1648 | fn code_intra_8x8( |
| 1623 | pic: &mut Picture, | 1649 | pic: &mut Picture, |
| 1624 | e: &MbEnc<'_>, | 1650 | e: &MbEnc<'_>, |
| 1651 | + w: &EncEntropy, | ||
| 1625 | mb_x: usize, | 1652 | mb_x: usize, |
| 1626 | mb_y: usize, | 1653 | mb_y: usize, |
| 1627 | qp: i32, | 1654 | qp: i32, |
| @@ -1669,7 +1696,7 @@ fn code_intra_8x8( | |||
| 1669 | let row = origin + ry * stride; | 1696 | let row = origin + ry * stride; |
| 1670 | pic.y.data[row..row + 8].copy_from_slice(&p[ry * 8..ry * 8 + 8]); | 1697 | pic.y.data[row..row + 8].copy_from_slice(&p[ry * 8..ry * 8 + 8]); |
| 1671 | } | 1698 | } |
| 1672 | - if code_block_8x8(pic, e, x, y, qp, true, &mut lv.luma8[blk8]) > 0 { | 1699 | + if code_block_8x8(pic, e, w, x, y, qp, true, &mut lv.luma8[blk8]) > 0 { |
| 1673 | lv.cbp_luma |= 1 << blk8; | 1700 | lv.cbp_luma |= 1 << blk8; |
| 1674 | } | 1701 | } |
| 1675 | } | 1702 | } |
| @@ -1681,6 +1708,7 @@ fn code_intra_8x8( | |||
| 1681 | fn code_luma_8x8( | 1708 | fn code_luma_8x8( |
| 1682 | pic: &mut Picture, | 1709 | pic: &mut Picture, |
| 1683 | e: &MbEnc<'_>, | 1710 | e: &MbEnc<'_>, |
| 1711 | + w: &EncEntropy, | ||
| 1684 | mb_x: usize, | 1712 | mb_x: usize, |
| 1685 | mb_y: usize, | 1713 | mb_y: usize, |
| 1686 | qp: i32, | 1714 | qp: i32, |
| @@ -1689,12 +1717,79 @@ fn code_luma_8x8( | |||
| 1689 | lv.trans8 = true; | 1717 | lv.trans8 = true; |
| 1690 | for blk8 in 0..4 { | 1718 | for blk8 in 0..4 { |
| 1691 | let (x, y) = (mb_x * 16 + (blk8 % 2) * 8, mb_y * 16 + (blk8 / 2) * 8); | 1719 | let (x, y) = (mb_x * 16 + (blk8 % 2) * 8, mb_y * 16 + (blk8 / 2) * 8); |
| 1692 | - if code_block_8x8(pic, e, x, y, qp, false, &mut lv.luma8[blk8]) > 0 { | 1720 | + if code_block_8x8(pic, e, w, x, y, qp, false, &mut lv.luma8[blk8]) > 0 { |
| 1693 | lv.cbp_luma |= 1 << blk8; | 1721 | lv.cbp_luma |= 1 << blk8; |
| 1694 | } | 1722 | } |
| 1695 | } | 1723 | } |
| 1696 | } | 1724 | } |
| 1697 | 1725 | ||
| 1726 | +/// Squared error of coding `levels` as an 8x8 block, in its pixels. | ||
| 1727 | +fn block_ssd_8x8(e: &MbEnc<'_>, source: &[i32; 64], levels: &[i32; 64], qp: i32) -> i64 { | ||
| 1728 | + let mut raster = [0i32; 64]; | ||
| 1729 | + unzigzag_8x8(levels, &mut raster); | ||
| 1730 | + dequant_8x8(&mut raster, &e.ls8, qp); | ||
| 1731 | + let mut resid = [0i32; 64]; | ||
| 1732 | + inverse_transform_8x8(&raster, &mut resid); | ||
| 1733 | + let mut ssd = 0i64; | ||
| 1734 | + for k in 0..64 { | ||
| 1735 | + let d = i64::from(source[k]) - i64::from(resid[k]); | ||
| 1736 | + ssd += d * d; | ||
| 1737 | + } | ||
| 1738 | + ssd | ||
| 1739 | +} | ||
| 1740 | + | ||
| 1741 | +/// [`rdoq_4x4`] for the 8x8 transform: the same walk down the scan against the | ||
| 1742 | +/// CABAC coder's own price, with the 8x8 block's own contexts. | ||
| 1743 | +fn rdoq_8x8( | ||
| 1744 | + e: &MbEnc<'_>, | ||
| 1745 | + w: &EncEntropy, | ||
| 1746 | + source: &[i32; 64], | ||
| 1747 | + levels: &mut [i32; 64], | ||
| 1748 | + qp: i32, | ||
| 1749 | +) -> u8 { | ||
| 1750 | + let Some(bits) = w.residual_block_8x8_cost(levels) else { | ||
| 1751 | + return levels.iter().filter(|&&l| l != 0).count() as u8; | ||
| 1752 | + }; | ||
| 1753 | + let lambda = RDOQ_LAMBDA * lambda_ssd(qp) / 256.0; | ||
| 1754 | + let mut cost = block_ssd_8x8(e, source, levels, qp) as f64 + lambda * f64::from(bits); | ||
| 1755 | + for pos in (0..64).rev() { | ||
| 1756 | + let level = levels[pos]; | ||
| 1757 | + if level == 0 { | ||
| 1758 | + continue; | ||
| 1759 | + } | ||
| 1760 | + let mag = level.abs(); | ||
| 1761 | + if mag > RDOQ_MAX_LEVEL { | ||
| 1762 | + continue; | ||
| 1763 | + } | ||
| 1764 | + let mut tried = mag; | ||
| 1765 | + for candidate in [mag - 1, 0] { | ||
| 1766 | + if candidate == tried { | ||
| 1767 | + continue; | ||
| 1768 | + } | ||
| 1769 | + tried = candidate; | ||
| 1770 | + let keep = levels[pos]; | ||
| 1771 | + levels[pos] = candidate * level.signum(); | ||
| 1772 | + let bits = w | ||
| 1773 | + .residual_block_8x8_cost(levels) | ||
| 1774 | + .expect("CAVLC returned early"); | ||
| 1775 | + let trial = block_ssd_8x8(e, source, levels, qp) as f64 + lambda * f64::from(bits); | ||
| 1776 | + if trial < cost { | ||
| 1777 | + cost = trial; | ||
| 1778 | + } else { | ||
| 1779 | + levels[pos] = keep; | ||
| 1780 | + break; | ||
| 1781 | + } | ||
| 1782 | + } | ||
| 1783 | + } | ||
| 1784 | + // The whole block against nothing, on the same exact price. | ||
| 1785 | + let ssd_zero: i64 = source.iter().map(|&c| i64::from(c) * i64::from(c)).sum(); | ||
| 1786 | + if ssd_zero as f64 <= cost { | ||
| 1787 | + *levels = [0; 64]; | ||
| 1788 | + return 0; | ||
| 1789 | + } | ||
| 1790 | + levels.iter().filter(|&&l| l != 0).count() as u8 | ||
| 1791 | +} | ||
| 1792 | + | ||
| 1698 | /// Squared error of the luma macroblock against the source. | 1793 | /// Squared error of the luma macroblock against the source. |
| 1699 | fn ssd_luma(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 { | 1794 | fn ssd_luma(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 { |
| 1700 | let stride = pic.y.stride; | 1795 | let stride = pic.y.stride; |
| @@ -2057,7 +2152,7 @@ fn encode_intra_mb( | |||
| 2057 | let mut lv8 = chroma_lv.clone(); | 2152 | let mut lv8 = chroma_lv.clone(); |
| 2058 | let mut recon8 = [0u8; 256]; | 2153 | let mut recon8 = [0u8; 256]; |
| 2059 | let cost8 = if e.t8x8_intra(mb_y) { | 2154 | let cost8 = if e.t8x8_intra(mb_y) { |
| 2060 | - let (m8, p8) = code_intra_8x8(pic, e, mb_x, mb_y, qp, &nbr, &mut lv8); | 2155 | + let (m8, p8) = code_intra_8x8(pic, e, w, mb_x, mb_y, qp, &nbr, &mut lv8); |
| 2061 | modes8 = m8; | 2156 | modes8 = m8; |
| 2062 | pred_modes8 = p8; | 2157 | pred_modes8 = p8; |
| 2063 | save_luma(pic, mb_x, mb_y, &mut recon8); | 2158 | save_luma(pic, mb_x, mb_y, &mut recon8); |
| @@ -2541,7 +2636,7 @@ fn encode_inter_mb( | |||
| 2541 | save_luma(pic, mb_x, mb_y, &mut recon4); | 2636 | save_luma(pic, mb_x, mb_y, &mut recon4); |
| 2542 | restore_luma(pic, mb_x, mb_y, &pred); | 2637 | restore_luma(pic, mb_x, mb_y, &pred); |
| 2543 | let mut lv8 = chroma_lv; | 2638 | let mut lv8 = chroma_lv; |
| 2544 | - code_luma_8x8(pic, e, mb_x, mb_y, qp, &mut lv8); | 2639 | + code_luma_8x8(pic, e, w, mb_x, mb_y, qp, &mut lv8); |
| 2545 | let bits8 = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv8); | 2640 | let bits8 = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv8); |
| 2546 | let cost8 = ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits8 as f64; | 2641 | let cost8 = ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits8 as f64; |
| 2547 | if cost8 + e.lambda * e.t8x8_margin_bits < cost4 { | 2642 | if cost8 + e.lambda * e.t8x8_margin_bits < cost4 { |
crates/ec-h264/src/enc/quant.rsmodified+15 -5
| @@ -162,10 +162,6 @@ pub(crate) fn forward_8x8(d: &[i32; 64]) -> [i64; 64] { | |||
| 162 | /// precomputed shift because [`forward_8x8`]'s coefficients already carry | 162 | /// precomputed shift because [`forward_8x8`]'s coefficients already carry |
| 163 | /// far more headroom than a `<<15`-ish fixed point would give. | 163 | /// far more headroom than a `<<15`-ish fixed point would give. |
| 164 | #[inline] | 164 | #[inline] |
| 165 | -fn round_div(raw: i64, denom: i64, intra: bool) -> i64 { | ||
| 166 | - (raw + denom / if intra { 3 } else { 6 }) / denom | ||
| 167 | -} | ||
| 168 | - | ||
| 169 | /// Quantise an 8x8 [`forward_8x8`] output into scan (zig-zag) order, undoing | 165 | /// Quantise an 8x8 [`forward_8x8`] output into scan (zig-zag) order, undoing |
| 170 | /// both the `2_088_025` transform gain and the per-position [`LevelScale8x8`] | 166 | /// both the `2_088_025` transform gain and the per-position [`LevelScale8x8`] |
| 171 | /// / QP scale that [`crate::transform::dequant_8x8`] will re-apply. | 167 | /// / QP scale that [`crate::transform::dequant_8x8`] will re-apply. |
| @@ -175,6 +171,20 @@ pub(crate) fn quant_8x8( | |||
| 175 | qp: i32, | 171 | qp: i32, |
| 176 | intra: bool, | 172 | intra: bool, |
| 177 | out: &mut [i32; 64], | 173 | out: &mut [i32; 64], |
| 174 | +) -> u8 { | ||
| 175 | + quant_8x8_offset(coef, ls, qp, if intra { 3 } else { 6 }, out) | ||
| 176 | +} | ||
| 177 | + | ||
| 178 | +/// [`quant_8x8`] with the rounding offset named outright: the level is | ||
| 179 | +/// `(raw + denom / divisor) / denom`, so a divisor of 2 rounds to nearest and | ||
| 180 | +/// 6 is the inter dead zone. A search that offers smaller magnitudes wants to | ||
| 181 | +/// pick this itself. | ||
| 182 | +pub(crate) fn quant_8x8_offset( | ||
| 183 | + coef: &[i64; 64], | ||
| 184 | + ls: &LevelScale8x8, | ||
| 185 | + qp: i32, | ||
| 186 | + divisor: i32, | ||
| 187 | + out: &mut [i32; 64], | ||
| 178 | ) -> u8 { | 188 | ) -> u8 { |
| 179 | // 11560^2 / 64, see FWD8's doc comment. | 189 | // 11560^2 / 64, see FWD8's doc comment. |
| 180 | const TOTAL_SCALE: i64 = 2_088_025; | 190 | const TOTAL_SCALE: i64 = 2_088_025; |
| @@ -193,7 +203,7 @@ pub(crate) fn quant_8x8( | |||
| 193 | } else { | 203 | } else { |
| 194 | (c.unsigned_abs() as i64, denom << -p) | 204 | (c.unsigned_abs() as i64, denom << -p) |
| 195 | }; | 205 | }; |
| 196 | - let level = round_div(num, denom, intra) as i32; | 206 | + let level = ((num + denom / i64::from(divisor)) / denom) as i32; |
| 197 | if level != 0 { | 207 | if level != 0 { |
| 198 | nz += 1; | 208 | nz += 1; |
| 199 | } | 209 | } |
lanes/h264x264-r2.report.mdmodified+28 -0
| @@ -383,3 +383,31 @@ x264's trellis is worth 0.193 dB on the film clip and 0.208 on the screen | |||
| 383 | capture, so the two slices together take 0.135 and 0.032 of it. | 383 | capture, so the two slices together take 0.135 and 0.032 of it. |
| 384 | 384 | ||
| 385 | The 8x8 luma transform path is still untouched. | 385 | The 8x8 luma transform path is still untouched. |
| 386 | + | ||
| 387 | +## The same price, applied to the 8x8 transform path | ||
| 388 | + | ||
| 389 | +`code_block_8x8` now quantises, searches and zero-tests its inter blocks the | ||
| 390 | +way the 4x4 luma path does, on a `residual_block_8x8_cost` that shares its | ||
| 391 | +bins with the writer (asserted to 1% over 200 random 64-coefficient blocks, | ||
| 392 | +`residual_8x8_cost_predicts_the_bits_spent`). | ||
| 393 | + | ||
| 394 | +The x264 comparison cannot see this path -- both sides run with `8x8dct=0` at | ||
| 395 | +matched features -- so it is measured as BD-PSNR of the opt-in `transform_8x8` | ||
| 396 | +flag against flag-off, which isolates it: the flag-off curve does not go | ||
| 397 | +through an 8x8 block. | ||
| 398 | + | ||
| 399 | +| rounding under the search | film 3840x1608 | screen capture 2560x1440 | | ||
| 400 | +|---|---|---| | ||
| 401 | +| no search (baseline) | +0.535 | +0.042 | | ||
| 402 | +| 1/6 | +0.551 | -- | | ||
| 403 | +| 1/3 | +0.605 | +0.075 | | ||
| 404 | +| **1/2** | **+0.619** | +0.067 | | ||
| 405 | + | ||
| 406 | +8x8 answers the way 4x4 luma did and chroma did not: its blocks carry enough | ||
| 407 | +coefficients for the wider rounding to pay for itself. Landed at 1/2 -- | ||
| 408 | +film +0.535 -> +0.619 (0.084 dB), screen capture +0.042 -> +0.067. The average | ||
| 409 | +8x8 macroblock share rises with it (19.0% -> 20.2% film), which is the flag | ||
| 410 | +being chosen more often now that the blocks it picks are cheaper. | ||
| 411 | + | ||
| 412 | +`transform_8x8` is still opt-in, so this changes no default stream byte; the | ||
| 413 | +open decision to flip that default is worth 0.084 dB more than it was. | ||