forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-h265screen' (f07a63e8): per-4x4 luma transform-skip RD decision (TransformSkip::Rd, new default): screen-capture BD-PSNR all-plane vs x265 worst clip -0.438 -> +0.918, film unchanged; transform_skip_flag now written for every 4x4 once the PPS enables the tool (presence-vs-value bug caught by the decode gate); EC_H265_TSKIP test knob

Browse files at this commit

afff48e55d7ff3cf22d4bfe66ea28f988832d31cAhmet Kaan GümüşParent: 7302779c7b6783
crates/ec-h265/src/ctu.rsmodified+105 -19
@@ -23,6 +23,7 @@
23 //! every core instead.23 //! every core instead.
24 24
25 use crate::cabac::{CabacEncoder, CabacState, Contexts, ctx};25 use crate::cabac::{CabacEncoder, CabacState, Contexts, ctx};
26+use crate::encoder::TransformSkip;
26 use crate::intra::{self, Availability, Refs};27 use crate::intra::{self, Availability, Refs};
27 use crate::residual::{self, encode_residual};28 use crate::residual::{self, encode_residual};
28 use crate::transform::{29 use crate::transform::{
@@ -172,6 +173,11 @@ pub struct CtuEncoder<'a> {
172 /// Whether the levels get a rate-distortion search after quantisation.173 /// Whether the levels get a rate-distortion search after quantisation.
173 rdoq: bool,174 rdoq: bool,
174 transform_skip: bool,175 transform_skip: bool,
176+ /// Whether every 4x4 luma block decides transform skip by rate-distortion
177+ /// instead of taking `transform_skip` unconditionally.
178+ ts_rd: bool,
179+ /// The skip decision for the 4x4 luma block currently in the scratch.
180+ ts_luma: bool,
175 rqt: bool,181 rqt: bool,
176 /// Whether a 64x64 coding tree block may stay one coding unit rather than182 /// Whether a 64x64 coding tree block may stay one coding unit rather than
177 /// always splitting to 32x32.183 /// always splitting to 32x32.
@@ -215,7 +221,7 @@ impl<'a> CtuEncoder<'a> {
215 sign_hiding: bool,221 sign_hiding: bool,
216 rdoq: bool,222 rdoq: bool,
217 rdoq_estimate: bool,223 rdoq_estimate: bool,
218- transform_skip: bool,224+ transform_skip: TransformSkip,
219 rqt: bool,225 rqt: bool,
220 cu64: bool,226 cu64: bool,
221 min_cb_log2: u32,227 min_cb_log2: u32,
@@ -240,7 +246,9 @@ impl<'a> CtuEncoder<'a> {
240 intra_nxn,246 intra_nxn,
241 sign_hiding,247 sign_hiding,
242 rdoq,248 rdoq,
243- transform_skip,249+ transform_skip: transform_skip == TransformSkip::AlwaysFor4x4,
250+ ts_rd: transform_skip == TransformSkip::Rd,
251+ ts_luma: false,
244 rqt,252 rqt,
245 cu64,253 cu64,
246 rdoq_enc: {254 rdoq_enc: {
@@ -587,11 +595,15 @@ impl<'a> CtuEncoder<'a> {
587 let mut mpms = [[0u8; 3]; 4];595 let mut mpms = [[0u8; 3]; 4];
588 let mut levels = [[0i32; 16]; 4];596 let mut levels = [[0i32; 16]; 4];
589 let mut cbfs = [false; 4];597 let mut cbfs = [false; 4];
598+ let mut tskip = [self.transform_skip; 4];
590 for (part, &(dx, dy)) in offsets.iter().enumerate() {599 for (part, &(dx, dy)) in offsets.iter().enumerate() {
591 let (px, py) = (x + dx, y + dy);600 let (px, py) = (x + dx, y + dy);
592 let mpm = self.mpm_at(px, py);601 let mpm = self.mpm_at(px, py);
593 let mode = self.choose_luma_mode(px, py, 4, 2, &mpm, enc);602 let mode = self.choose_luma_mode(px, py, 4, 2, &mpm, enc);
594 levels[part].copy_from_slice(&self.scratch.luma_levels[..16]);603 levels[part].copy_from_slice(&self.scratch.luma_levels[..16]);
604+ if self.ts_rd {
605+ tskip[part] = self.ts_luma;
606+ }
595 cbfs[part] = levels[part].iter().any(|&v| v != 0);607 cbfs[part] = levels[part].iter().any(|&v| v != 0);
596 modes[part] = mode;608 modes[part] = mode;
597 mpms[part] = mpm;609 mpms[part] = mpm;
@@ -640,7 +652,7 @@ impl<'a> CtuEncoder<'a> {
640 0,652 0,
641 scan,653 scan,
642 self.sign_hiding,654 self.sign_hiding,
643- self.transform_skip,655+ self.ts_flag(tskip[part]),
644 );656 );
645 }657 }
646 if part == 3 {658 if part == 3 {
@@ -654,7 +666,7 @@ impl<'a> CtuEncoder<'a> {
654 1,666 1,
655 chroma_scan,667 chroma_scan,
656 self.sign_hiding,668 self.sign_hiding,
657- self.transform_skip,669+ self.ts_flag(self.transform_skip),
658 );670 );
659 self.scratch.cb_levels = l;671 self.scratch.cb_levels = l;
660 }672 }
@@ -667,7 +679,7 @@ impl<'a> CtuEncoder<'a> {
667 2,679 2,
668 chroma_scan,680 chroma_scan,
669 self.sign_hiding,681 self.sign_hiding,
670- self.transform_skip,682+ self.ts_flag(self.transform_skip),
671 );683 );
672 self.scratch.cr_levels = l;684 self.scratch.cr_levels = l;
673 }685 }
@@ -714,6 +726,7 @@ impl<'a> CtuEncoder<'a> {
714 self.write_chroma_mode_syntax(enc, chroma_idx);726 self.write_chroma_mode_syntax(enc, chroma_idx);
715 727
716 let mut child_levels = [[0i32; 1024]; 4];728 let mut child_levels = [[0i32; 1024]; 4];
729+ let mut child_tskip = [self.transform_skip; 4];
717 let mut child_cbf = [false; 4];730 let mut child_cbf = [false; 4];
718 let mut child_cb = [[0i32; 256]; 4];731 let mut child_cb = [[0i32; 256]; 4];
719 let mut child_cr = [[0i32; 256]; 4];732 let mut child_cr = [[0i32; 256]; 4];
@@ -738,6 +751,9 @@ impl<'a> CtuEncoder<'a> {
738 );751 );
739 }752 }
740 let (_, cbf) = self.transform_luma(HALF, mode, &source, &refs, true);753 let (_, cbf) = self.transform_luma(HALF, mode, &source, &refs, true);
754+ if self.ts_rd {
755+ child_tskip[part] = self.ts_luma;
756+ }
741 self.scratch.source = source;757 self.scratch.source = source;
742 child_levels[part][..HALF * HALF]758 child_levels[part][..HALF * HALF]
743 .copy_from_slice(&self.scratch.levels[..HALF * HALF]);759 .copy_from_slice(&self.scratch.levels[..HALF * HALF]);
@@ -776,6 +792,7 @@ impl<'a> CtuEncoder<'a> {
776 cr_cbf,792 cr_cbf,
777 &child_cbf,793 &child_cbf,
778 &child_levels,794 &child_levels,
795+ &child_tskip,
779 &child_cb_cbf,796 &child_cb_cbf,
780 &child_cr_cbf,797 &child_cr_cbf,
781 &child_cb,798 &child_cb,
@@ -885,6 +902,7 @@ impl<'a> CtuEncoder<'a> {
885 let half = n / 2;902 let half = n / 2;
886 let offsets = [(0usize, 0usize), (half, 0), (0, half), (half, half)];903 let offsets = [(0usize, 0usize), (half, 0), (0, half), (half, half)];
887 let mut child_levels = [[0i32; 1024]; 4];904 let mut child_levels = [[0i32; 1024]; 4];
905+ let mut child_tskip = [self.transform_skip; 4];
888 let mut child_cbf = [false; 4];906 let mut child_cbf = [false; 4];
889 let mut child_cb = [[0i32; 256]; 4];907 let mut child_cb = [[0i32; 256]; 4];
890 let mut child_cr = [[0i32; 256]; 4];908 let mut child_cr = [[0i32; 256]; 4];
@@ -901,6 +919,9 @@ impl<'a> CtuEncoder<'a> {
901 );919 );
902 }920 }
903 let (_, cbf) = self.transform_luma(half, mode, &source, &refs, true);921 let (_, cbf) = self.transform_luma(half, mode, &source, &refs, true);
922+ if self.ts_rd {
923+ child_tskip[part] = self.ts_luma;
924+ }
904 child_levels[part][..half * half]925 child_levels[part][..half * half]
905 .copy_from_slice(&self.scratch.levels[..half * half]);926 .copy_from_slice(&self.scratch.levels[..half * half]);
906 child_cbf[part] = cbf;927 child_cbf[part] = cbf;
@@ -953,6 +974,7 @@ impl<'a> CtuEncoder<'a> {
953 split_cr_cbf,974 split_cr_cbf,
954 &child_cbf,975 &child_cbf,
955 &child_levels,976 &child_levels,
977+ &child_tskip,
956 &child_cb_cbf,978 &child_cb_cbf,
957 &child_cr_cbf,979 &child_cr_cbf,
958 &child_cb,980 &child_cb,
@@ -1006,6 +1028,7 @@ impl<'a> CtuEncoder<'a> {
1006 split_cr_cbf,1028 split_cr_cbf,
1007 &child_cbf,1029 &child_cbf,
1008 &child_levels,1030 &child_levels,
1031+ &child_tskip,
1009 &child_cb_cbf,1032 &child_cb_cbf,
1010 &child_cr_cbf,1033 &child_cr_cbf,
1011 &child_cb,1034 &child_cb,
@@ -1069,7 +1092,7 @@ impl<'a> CtuEncoder<'a> {
1069 0,1092 0,
1070 scan,1093 scan,
1071 self.sign_hiding,1094 self.sign_hiding,
1072- self.transform_skip,1095+ self.ts_flag(self.transform_skip),
1073 );1096 );
1074 self.scratch.luma_levels = levels;1097 self.scratch.luma_levels = levels;
1075 }1098 }
@@ -1083,7 +1106,7 @@ impl<'a> CtuEncoder<'a> {
1083 1,1106 1,
1084 chroma_scan,1107 chroma_scan,
1085 self.sign_hiding,1108 self.sign_hiding,
1086- chroma_tskip,1109+ self.ts_flag(chroma_tskip),
1087 );1110 );
1088 self.scratch.cb_levels = levels;1111 self.scratch.cb_levels = levels;
1089 }1112 }
@@ -1096,7 +1119,7 @@ impl<'a> CtuEncoder<'a> {
1096 2,1119 2,
1097 chroma_scan,1120 chroma_scan,
1098 self.sign_hiding,1121 self.sign_hiding,
1099- chroma_tskip,1122+ self.ts_flag(chroma_tskip),
1100 );1123 );
1101 self.scratch.cr_levels = levels;1124 self.scratch.cr_levels = levels;
1102 }1125 }
@@ -1123,6 +1146,7 @@ impl<'a> CtuEncoder<'a> {
1123 cr_cbf: bool,1146 cr_cbf: bool,
1124 child_cbf: &[bool; 4],1147 child_cbf: &[bool; 4],
1125 child_levels: &[[i32; 1024]; 4],1148 child_levels: &[[i32; 1024]; 4],
1149+ child_tskip: &[bool; 4],
1126 child_cb_cbf: &[bool; 4],1150 child_cb_cbf: &[bool; 4],
1127 child_cr_cbf: &[bool; 4],1151 child_cr_cbf: &[bool; 4],
1128 child_cb: &[[i32; 256]; 4],1152 child_cb: &[[i32; 256]; 4],
@@ -1152,7 +1176,7 @@ impl<'a> CtuEncoder<'a> {
1152 0,1176 0,
1153 luma_scan,1177 luma_scan,
1154 self.sign_hiding,1178 self.sign_hiding,
1155- self.transform_skip,1179+ self.ts_flag(child_tskip[part]),
1156 );1180 );
1157 }1181 }
1158 if chroma_split {1182 if chroma_split {
@@ -1168,7 +1192,7 @@ impl<'a> CtuEncoder<'a> {
1168 1,1192 1,
1169 scan,1193 scan,
1170 self.sign_hiding,1194 self.sign_hiding,
1171- chroma_tskip,1195+ self.ts_flag(chroma_tskip),
1172 );1196 );
1173 }1197 }
1174 if cr_cbf && child_cr_cbf[part] {1198 if cr_cbf && child_cr_cbf[part] {
@@ -1179,7 +1203,7 @@ impl<'a> CtuEncoder<'a> {
1179 2,1203 2,
1180 scan,1204 scan,
1181 self.sign_hiding,1205 self.sign_hiding,
1182- chroma_tskip,1206+ self.ts_flag(chroma_tskip),
1183 );1207 );
1184 }1208 }
1185 } else if part == 3 {1209 } else if part == 3 {
@@ -1192,7 +1216,7 @@ impl<'a> CtuEncoder<'a> {
1192 1,1216 1,
1193 scan,1217 scan,
1194 self.sign_hiding,1218 self.sign_hiding,
1195- chroma_tskip,1219+ self.ts_flag(chroma_tskip),
1196 );1220 );
1197 }1221 }
1198 if cr_cbf {1222 if cr_cbf {
@@ -1203,7 +1227,7 @@ impl<'a> CtuEncoder<'a> {
1203 2,1227 2,
1204 scan,1228 scan,
1205 self.sign_hiding,1229 self.sign_hiding,
1206- chroma_tskip,1230+ self.ts_flag(chroma_tskip),
1207 );1231 );
1208 }1232 }
1209 }1233 }
@@ -1431,7 +1455,7 @@ impl<'a> CtuEncoder<'a> {
1431 // The trial above scored every candidate without RDOQ's refinement1455 // The trial above scored every candidate without RDOQ's refinement
1432 // loop; now that the mode is decided, pay for that loop exactly once,1456 // loop; now that the mode is decided, pay for that loop exactly once,
1433 // on the winner, so the committed levels are the ones RDOQ can reach.1457 // on the winner, so the committed levels are the ones RDOQ can reach.
1434- if self.rdoq {1458+ if self.rdoq || (self.ts_rd && n == 4) {
1435 self.transform_luma(n, best.1, &source, &refs, true);1459 self.transform_luma(n, best.1, &source, &refs, true);
1436 best_levels[..n * n].copy_from_slice(&self.scratch.levels[..n * n]);1460 best_levels[..n * n].copy_from_slice(&self.scratch.levels[..n * n]);
1437 self.scratch.best_recon[..n * n].copy_from_slice(&self.scratch.recon[..n * n]);1461 self.scratch.best_recon[..n * n].copy_from_slice(&self.scratch.recon[..n * n]);
@@ -1462,6 +1486,61 @@ impl<'a> CtuEncoder<'a> {
1462 source: &[u8],1486 source: &[u8],
1463 refs: &Refs,1487 refs: &Refs,
1464 refine: bool,1488 refine: bool,
1489+ ) -> (f64, bool) {
1490+ if self.ts_rd {
1491+ if n == 4 && refine {
1492+ return self.transform_luma_ts_rd(mode, source, refs);
1493+ }
1494+ // Candidate ranking prices the mode, not the skip decision, which
1495+ // the committed pass above makes once on the winner.
1496+ self.ts_luma = false;
1497+ }
1498+ self.transform_luma_at(n, mode, source, refs, refine)
1499+ }
1500+
1501+ /// The `transform_skip_flag` to write for a 4x4 block: `None` when the PPS
1502+ /// left transform skip off and the decoder reads no bin at all.
1503+ fn ts_flag(&self, skip: bool) -> Option<bool> {
1504+ (self.transform_skip || self.ts_rd).then_some(skip)
1505+ }
1506+
1507+ /// Code the 4x4 luma block both ways and keep the cheaper one, leaving its
1508+ /// levels and reconstruction in the scratch and its flag in `ts_luma`.
1509+ fn transform_luma_ts_rd(&mut self, mode: u8, source: &[u8], refs: &Refs) -> (f64, bool) {
1510+ let scan = intra::scan_index(mode, 2, true);
1511+ let (mut best_cost, mut best) = (f64::MAX, (0.0, false, false, [0i32; 16], [0u8; 16]));
1512+ for skip in [false, true] {
1513+ self.ts_luma = skip;
1514+ let (ssd, cbf) = self.transform_luma_at(4, mode, source, refs, true);
1515+ // The flag itself is one bin; the levels are priced by the same
1516+ // proxy the mode search uses.
1517+ let mut bits = 1.0;
1518+ if cbf {
1519+ bits += residual::estimate_residual_bits(&self.scratch.levels[..16], 2, scan);
1520+ }
1521+ let cost = ssd + self.lambda * bits;
1522+ if cost < best_cost {
1523+ let (mut levels, mut recon) = ([0i32; 16], [0u8; 16]);
1524+ levels.copy_from_slice(&self.scratch.levels[..16]);
1525+ recon.copy_from_slice(&self.scratch.recon[..16]);
1526+ best_cost = cost;
1527+ best = (ssd, skip, cbf, levels, recon);
1528+ }
1529+ }
1530+ let (ssd, skip, cbf, levels, recon) = best;
1531+ self.ts_luma = skip;
1532+ self.scratch.levels[..16].copy_from_slice(&levels);
1533+ self.scratch.recon[..16].copy_from_slice(&recon);
1534+ (ssd, cbf)
1535+ }
1536+
1537+ fn transform_luma_at(
1538+ &mut self,
1539+ n: usize,
1540+ mode: u8,
1541+ source: &[u8],
1542+ refs: &Refs,
1543+ refine: bool,
1465 ) -> (f64, bool) {1544 ) -> (f64, bool) {
1466 intra::predict(1545 intra::predict(
1467 refs,1546 refs,
@@ -1474,7 +1553,12 @@ impl<'a> CtuEncoder<'a> {
1474 for (i, &sample) in source[..n * n].iter().enumerate() {1553 for (i, &sample) in source[..n * n].iter().enumerate() {
1475 self.scratch.residual[i] = i32::from(sample) - i32::from(self.scratch.pred[i]);1554 self.scratch.residual[i] = i32::from(sample) - i32::from(self.scratch.pred[i]);
1476 }1555 }
1477- let skip = self.transform_skip && n == 4;1556+ let skip = n == 4
1557+ && if self.ts_rd {
1558+ self.ts_luma
1559+ } else {
1560+ self.transform_skip
1561+ };
1478 let dst = uses_dst(n, true);1562 let dst = uses_dst(n, true);
1479 if skip {1563 if skip {
1480 forward_transform_skip(&self.scratch.residual, &mut self.scratch.coeffs, n);1564 forward_transform_skip(&self.scratch.residual, &mut self.scratch.coeffs, n);
@@ -1492,6 +1576,7 @@ impl<'a> CtuEncoder<'a> {
1492 } else {1576 } else {
1493 quantize(&self.scratch.coeffs, &mut self.scratch.levels, n, self.qp)1577 quantize(&self.scratch.coeffs, &mut self.scratch.levels, n, self.qp)
1494 };1578 };
1579+ let ts = self.ts_flag(skip);
1495 if self.rdoq && refine && nonzero > 0 {1580 if self.rdoq && refine && nonzero > 0 {
1496 nonzero = residual::rdoq(1581 nonzero = residual::rdoq(
1497 &self.scratch.coeffs,1582 &self.scratch.coeffs,
@@ -1502,7 +1587,7 @@ impl<'a> CtuEncoder<'a> {
1502 intra::scan_index(mode, n.trailing_zeros(), true),1587 intra::scan_index(mode, n.trailing_zeros(), true),
1503 ctx::CBF_LUMA + 1,1588 ctx::CBF_LUMA + 1,
1504 self.lambda,1589 self.lambda,
1505- skip,1590+ ts,
1506 &mut self.rdoq_enc,1591 &mut self.rdoq_enc,
1507 );1592 );
1508 }1593 }
@@ -1589,6 +1674,7 @@ impl<'a> CtuEncoder<'a> {
1589 self.qp_c,1674 self.qp_c,
1590 )1675 )
1591 };1676 };
1677+ let ts = self.ts_flag(skip && cn == 4);
1592 if self.rdoq && nonzero > 0 {1678 if self.rdoq && nonzero > 0 {
1593 nonzero = residual::rdoq(1679 nonzero = residual::rdoq(
1594 &self.scratch.coeffs,1680 &self.scratch.coeffs,
@@ -1599,7 +1685,7 @@ impl<'a> CtuEncoder<'a> {
1599 intra::scan_index(pred_mode, cn.trailing_zeros(), false),1685 intra::scan_index(pred_mode, cn.trailing_zeros(), false),
1600 ctx::CBF_CHROMA,1686 ctx::CBF_CHROMA,
1601 self.lambda,1687 self.lambda,
1602- skip && cn == 4,1688+ ts,
1603 &mut self.rdoq_enc,1689 &mut self.rdoq_enc,
1604 );1690 );
1605 }1691 }
@@ -1713,7 +1799,7 @@ impl<'a> CtuEncoder<'a> {
1713 1,1799 1,
1714 scan,1800 scan,
1715 self.sign_hiding,1801 self.sign_hiding,
1716- chroma_tskip,1802+ self.ts_flag(chroma_tskip),
1717 );1803 );
1718 self.scratch.cb_levels = levels;1804 self.scratch.cb_levels = levels;
1719 }1805 }
@@ -1726,7 +1812,7 @@ impl<'a> CtuEncoder<'a> {
1726 2,1812 2,
1727 scan,1813 scan,
1728 self.sign_hiding,1814 self.sign_hiding,
1729- chroma_tskip,1815+ self.ts_flag(chroma_tskip),
1730 );1816 );
1731 self.scratch.cr_levels = levels;1817 self.scratch.cr_levels = levels;
1732 }1818 }
crates/ec-h265/src/encoder.rsmodified+28 -2
@@ -56,6 +56,12 @@ pub enum TransformSkip {
56 Off,56 Off,
57 /// Skip the transform on every 4x4 TU that has non-zero residual.57 /// Skip the transform on every 4x4 TU that has non-zero residual.
58 AlwaysFor4x4,58 AlwaysFor4x4,
59+ /// Decide per 4x4 luma transform block by rate-distortion: quantise and
60+ /// reconstruct the block both ways and keep the cheaper one. Unconditional
61+ /// skip is a big win on screen capture and a loss on camera content
62+ /// (measured, see `transform_skip`), which is exactly what a per-block
63+ /// decision resolves.
64+ Rd,
59 }65 }
60 66
61 /// Encoder settings.67 /// Encoder settings.
@@ -177,6 +183,26 @@ pub struct EncoderConfig {
177 /// loss. `false` restores exact per-trial CABAC replay for A/B.183 /// loss. `false` restores exact per-trial CABAC replay for A/B.
178 pub rdoq_estimate: bool,184 pub rdoq_estimate: bool,
179 /// Whether 4x4 TUs may skip the integer transform.185 /// Whether 4x4 TUs may skip the integer transform.
186+ ///
187+ /// `Rd` by default: the tool is the screen-content lever, and it wants a
188+ /// per-block decision, not a switch. BD-PSNR luma / all-plane against x265
189+ /// at matched features over the four-QP ladder (`EC_H265_TSKIP=0|always|rd`
190+ /// in `bd_psnr_vs_x265`, real clips, 24 pictures each):
191+ ///
192+ /// | clip | `Off` | `AlwaysFor4x4` | `Rd` |
193+ /// |------|-------|----------------|------|
194+ /// | 1440p screen capture A | -0.381 / -0.438 | +1.485 / +1.373 | +1.034 / +0.918 |
195+ /// | 1440p screen capture B | +0.716 / +0.561 | +0.783 / +0.222 | +1.133 / +0.781 |
196+ /// | 1440p screen capture C | +0.213 / +0.099 | | +0.124 / +0.008 |
197+ /// | 1080p film | +0.584 / +0.468 | | +0.580 / +0.460 |
198+ ///
199+ /// Unconditional skip is worth +1.8 dB all-plane on A and -0.34 dB on B, so
200+ /// neither setting of a switch is right; the per-block trial takes A's win
201+ /// without B's loss and leaves film where it was (-0.008 dB, the estimate
202+ /// proxy's noise). The cost is the second quantise-and-reconstruct pass on
203+ /// every committed 4x4 luma block: the whole four-QP ladder went 111s ->
204+ /// 290s on screen capture A and 427s -> 494s on film. Set `Off` to buy that
205+ /// time back.
180 pub transform_skip: TransformSkip,206 pub transform_skip: TransformSkip,
181 /// Whether the transform tree may split once (rate-quantisation transform):207 /// Whether the transform tree may split once (rate-quantisation transform):
182 /// the luma TU of a 2Nx2N coding unit may be coded as four half-size208 /// the luma TU of a 2Nx2N coding unit may be coded as four half-size
@@ -237,7 +263,7 @@ impl EncoderConfig {
237 sign_hiding: false,263 sign_hiding: false,
238 rdoq: true,264 rdoq: true,
239 rdoq_estimate: true,265 rdoq_estimate: true,
240- transform_skip: TransformSkip::Off,266+ transform_skip: TransformSkip::Rd,
241 rqt: true,267 rqt: true,
242 cu64: true,268 cu64: true,
243 video_signal_type: None,269 video_signal_type: None,
@@ -624,7 +650,7 @@ impl Encoder {
624 self.cfg.sign_hiding,650 self.cfg.sign_hiding,
625 self.cfg.rdoq,651 self.cfg.rdoq,
626 self.cfg.rdoq_estimate,652 self.cfg.rdoq_estimate,
627- self.cfg.transform_skip != TransformSkip::Off,653+ self.cfg.transform_skip,
628 self.cfg.rqt,654 self.cfg.rqt,
629 self.cfg.cu64,655 self.cfg.cu64,
630 self.cfg.min_cu_size.max(8).trailing_zeros(),656 self.cfg.min_cu_size.max(8).trailing_zeros(),
crates/ec-h265/src/residual.rsmodified+19 -15
@@ -289,7 +289,7 @@ fn levels_bits(
289 scan_idx: usize,289 scan_idx: usize,
290 cbf_ctx: usize,290 cbf_ctx: usize,
291 coded: bool,291 coded: bool,
292- transform_skip: bool,292+ transform_skip: Option<bool>,
293 ) -> f64 {293 ) -> f64 {
294 enc.restore(base);294 enc.restore(base);
295 let before = enc.bit_count_f64();295 let before = enc.bit_count_f64();
@@ -321,7 +321,7 @@ fn levels_bits_with(
321 scan_idx: usize,321 scan_idx: usize,
322 cbf_ctx: usize,322 cbf_ctx: usize,
323 coded: bool,323 coded: bool,
324- transform_skip: bool,324+ transform_skip: Option<bool>,
325 last_full: i32,325 last_full: i32,
326 csbf: [bool; 64],326 csbf: [bool; 64],
327 ) -> f64 {327 ) -> f64 {
@@ -366,7 +366,7 @@ pub fn rdoq(
366 scan_idx: usize,366 scan_idx: usize,
367 cbf_ctx: usize,367 cbf_ctx: usize,
368 lambda: f64,368 lambda: f64,
369- transform_skip: bool,369+ transform_skip: Option<bool>,
370 enc: &mut CabacEncoder,370 enc: &mut CabacEncoder,
371 ) -> usize {371 ) -> usize {
372 let log2_size = n.trailing_zeros();372 let log2_size = n.trailing_zeros();
@@ -628,7 +628,7 @@ pub fn encode_residual(
628 c_idx: usize,628 c_idx: usize,
629 scan_idx: usize,629 scan_idx: usize,
630 sign_hiding: bool,630 sign_hiding: bool,
631- transform_skip: bool,631+ transform_skip: Option<bool>,
632 ) {632 ) {
633 let (last_full, csbf) = residual_header(levels, log2_size, scan_idx);633 let (last_full, csbf) = residual_header(levels, log2_size, scan_idx);
634 encode_residual_with(634 encode_residual_with(
@@ -654,13 +654,17 @@ fn encode_residual_with(
654 c_idx: usize,654 c_idx: usize,
655 scan_idx: usize,655 scan_idx: usize,
656 sign_hiding: bool,656 sign_hiding: bool,
657- transform_skip: bool,657+ transform_skip: Option<bool>,
658 last_full: i32,658 last_full: i32,
659 mut csbf: [bool; 64],659 mut csbf: [bool; 64],
660 ) {660 ) {
661- if transform_skip && log2_size == 2 {661+ // `Some` means the PPS turned transform skip on, so the decoder reads a
662- let ctx_idx = ctx::TRANSFORM_SKIP + if c_idx == 0 { 0 } else { 1 };662+ // bin for every 4x4 transform block whether or not this one skips.
663- enc.encode_bin(ctx_idx, 1);663+ if let Some(skip) = transform_skip {
664+ if log2_size == 2 {
665+ let ctx_idx = ctx::TRANSFORM_SKIP + if c_idx == 0 { 0 } else { 1 };
666+ enc.encode_bin(ctx_idx, u32::from(skip));
667+ }
664 }668 }
665 let n = 1usize << log2_size;669 let n = 1usize << log2_size;
666 let sub_wide = n >> 2;670 let sub_wide = n >> 2;
@@ -1392,7 +1396,7 @@ mod tests {
1392 let mut costs = Vec::new();1396 let mut costs = Vec::new();
1393 for levels in [&sparse, &dense] {1397 for levels in [&sparse, &dense] {
1394 let mut enc = CabacEncoder::counter(Contexts::new(30));1398 let mut enc = CabacEncoder::counter(Contexts::new(30));
1395- encode_residual(&mut enc, levels, 3, 0, 0, false, false);1399+ encode_residual(&mut enc, levels, 3, 0, 0, false, None);
1396 costs.push(enc.bit_count());1400 costs.push(enc.bit_count());
1397 }1401 }
1398 assert!(costs[0] < costs[1], "{costs:?}");1402 assert!(costs[0] < costs[1], "{costs:?}");
@@ -1432,7 +1436,7 @@ mod tests {
1432 c_idx,1436 c_idx,
1433 scan_idx,1437 scan_idx,
1434 sign_hiding,1438 sign_hiding,
1435- false,1439+ None,
1436 );1440 );
1437 enc.encode_terminate(1);1441 enc.encode_terminate(1);
1438 let bytes = enc.finish();1442 let bytes = enc.finish();
@@ -1478,7 +1482,7 @@ mod tests {
1478 };1482 };
1479 }1483 }
1480 let mut enc = CabacEncoder::counter(Contexts::new(27));1484 let mut enc = CabacEncoder::counter(Contexts::new(27));
1481- encode_residual(&mut enc, &levels, log2, c_idx, scan_idx, false, false);1485+ encode_residual(&mut enc, &levels, log2, c_idx, scan_idx, false, None);
1482 assert!(enc.bit_count() > 0);1486 assert!(enc.bit_count() > 0);
1483 }1487 }
1484 }1488 }
@@ -1499,7 +1503,7 @@ mod tests {
1499 scan_idx: usize,1503 scan_idx: usize,
1500 cbf_ctx: usize,1504 cbf_ctx: usize,
1501 lambda: f64,1505 lambda: f64,
1502- transform_skip: bool,1506+ transform_skip: Option<bool>,
1503 enc: &mut CabacEncoder,1507 enc: &mut CabacEncoder,
1504 ) -> usize {1508 ) -> usize {
1505 let log2_size = n.trailing_zeros();1509 let log2_size = n.trailing_zeros();
@@ -1687,7 +1691,7 @@ mod tests {
1687 scan_idx,1691 scan_idx,
1688 ctx::CBF_LUMA,1692 ctx::CBF_LUMA,
1689 1.0,1693 1.0,
1690- false,1694+ None,
1691 &mut enc_a,1695 &mut enc_a,
1692 );1696 );
1693 1697
@@ -1702,7 +1706,7 @@ mod tests {
1702 scan_idx,1706 scan_idx,
1703 ctx::CBF_LUMA,1707 ctx::CBF_LUMA,
1704 1.0,1708 1.0,
1705- false,1709+ None,
1706 &mut enc_b,1710 &mut enc_b,
1707 );1711 );
1708 1712
@@ -1731,7 +1735,7 @@ mod tests {
1731 0,1735 0,
1732 scan_idx,1736 scan_idx,
1733 sign_hiding,1737 sign_hiding,
1734- false,1738+ None,
1735 );1739 );
1736 assert!(fenc.bit_count() > 0);1740 assert!(fenc.bit_count() > 0);
1737 }1741 }
crates/ec-h265/tests/encode.rsmodified+4 -4
@@ -452,10 +452,10 @@ fn bd_psnr_vs_x265() {
452 cfg.rdoq_estimate = v != "0";452 cfg.rdoq_estimate = v != "0";
453 }453 }
454 if let Ok(v) = std::env::var("EC_H265_TSKIP") {454 if let Ok(v) = std::env::var("EC_H265_TSKIP") {
455- cfg.transform_skip = if v != "0" {455+ cfg.transform_skip = match v.as_str() {
456- TransformSkip::AlwaysFor4x4456+ "0" => TransformSkip::Off,
457- } else {457+ "always" => TransformSkip::AlwaysFor4x4,
458- TransformSkip::Off458+ _ => TransformSkip::Rd,
459 };459 };
460 }460 }
461 if let Ok(v) = std::env::var("EC_H265_NXN") {461 if let Ok(v) = std::env::var("EC_H265_NXN") {
crates/ec-h265/tests/rdoq_cache_hash.rsmodified+7 -2
@@ -64,8 +64,13 @@ fn rdoq_cache_hash_416x240() {
64 /// (`EncoderConfig::rdoq_estimate`, now on by default) — deliberately64 /// (`EncoderConfig::rdoq_estimate`, now on by default) — deliberately
65 /// bit-different, not exact-mode-preserving; see that field's doc comment65 /// bit-different, not exact-mode-preserving; see that field's doc comment
66 /// for the BD/speed numbers that justified flipping the default.66 /// for the BD/speed numbers that justified flipping the default.
67+///
68+/// lane-h265screen: re-pinned after every committed 4x4 luma block gained a
69+/// rate-distortion decision on `transform_skip_flag` (`TransformSkip::Rd`, now
70+/// the default) — deliberately bit-different; see that field's doc comment for
71+/// the BD numbers per clip.
67 #[test]72 #[test]
68 fn rdoq_cache_hash_is_pinned() {73 fn rdoq_cache_hash_is_pinned() {
69- assert_eq!(encode_hash(416, 240), 0x3dc2876c9104b7b3);74+ assert_eq!(encode_hash(416, 240), 0x39d93f8b600b072e);
70- assert_eq!(encode_hash(1920, 1080), 0x03188155f21c2174);75+ assert_eq!(encode_hash(1920, 1080), 0x8c5e05c6f65515c8);
71 }76 }