Add RD ablation controls
Browse files at this commit74491d6ebe26a15d568106382a3019cfcee86932Parent: 67fd459- test
- add RD ablation controls
3 changed files +180 -35
- crates/ec-h264/Cargo.toml +4 -0
- crates/ec-h264/src/enc/mb.rs +59 -25
- crates/ec-h264/src/enc/mod.rs +117 -10
crates/ec-h264/Cargo.tomlmodified+4 -0
| @@ -6,6 +6,10 @@ edition.workspace = true | |||
| 6 | license.workspace = true | 6 | license.workspace = true |
| 7 | authors.workspace = true | 7 | authors.workspace = true |
| 8 | 8 | ||
| 9 | +[features] | ||
| 10 | +# Internal-only RD experiment switches, compiled only by the measurement harness. | ||
| 11 | +rd-ablation = [] | ||
| 12 | + | ||
| 9 | [dependencies] | 13 | [dependencies] |
| 10 | ec-core = { workspace = true } | 14 | ec-core = { workspace = true } |
| 11 | ec-h264-syntax = { workspace = true } | 15 | ec-h264-syntax = { workspace = true } |
crates/ec-h264/src/enc/mb.rsmodified+59 -25
| @@ -16,18 +16,18 @@ use ec_h264_syntax::SliceType; | |||
| 16 | use wide::i16x16; | 16 | use wide::i16x16; |
| 17 | 17 | ||
| 18 | use crate::decoder::{ | 18 | use crate::decoder::{ |
| 19 | - MbNeighbors, chroma_nz_pair, gather_nbr4, gather_nbr8, luma_nz_pair, mb_neighbors, | 19 | + chroma_nz_pair, gather_nbr4, gather_nbr8, luma_nz_pair, mb_neighbors, MbNeighbors, |
| 20 | }; | 20 | }; |
| 21 | -use crate::dpb::{BLK_SKIP, Picture}; | 21 | +use crate::dpb::{Picture, BLK_SKIP}; |
| 22 | use crate::entropy::{ | 22 | use crate::entropy::{ |
| 23 | - BlockCat, FLAG_CHROMA_PRED, FLAG_DECODED, FLAG_I16, FLAG_INTER, FLAG_SKIP, FLAG_TRANS8X8, | 23 | + BlockCat, MbCtx, MbInfo, FLAG_CHROMA_PRED, FLAG_DECODED, FLAG_I16, FLAG_INTER, FLAG_SKIP, |
| 24 | - MbCtx, MbInfo, | 24 | + FLAG_TRANS8X8, |
| 25 | }; | 25 | }; |
| 26 | -use crate::inter::{RefPlane, integer_origin, mc_chroma, mc_luma}; | 26 | +use crate::inter::{integer_origin, mc_chroma, mc_luma, RefPlane}; |
| 27 | -use crate::mv::{MvCtx, neighbour_mvd, predict_mv, write_block, write_intra_mb, write_mvd}; | 27 | +use crate::mv::{neighbour_mvd, predict_mv, write_block, write_intra_mb, write_mvd, MvCtx}; |
| 28 | use crate::pred::{ | 28 | use crate::pred::{ |
| 29 | - PlaneWindow, add_residual_4x4, add_residual_8x8, filter_nbr8, pred_4x4, pred_8x8, pred_16x16, | 29 | + add_residual_4x4, add_residual_8x8, filter_nbr8, pred_16x16, pred_4x4, pred_8x8, |
| 30 | - pred_chroma_8x8, | 30 | + pred_chroma_8x8, PlaneWindow, |
| 31 | }; | 31 | }; |
| 32 | use crate::tables::{BLK4_POS, CHROMA_QP}; | 32 | use crate::tables::{BLK4_POS, CHROMA_QP}; |
| 33 | use crate::transform::{ | 33 | use crate::transform::{ |
| @@ -81,10 +81,19 @@ pub(crate) struct MbEnc<'a> { | |||
| 81 | /// Lagrangian multiplier; it is in the squared-error domain when 8x8 | 81 | /// Lagrangian multiplier; it is in the squared-error domain when 8x8 |
| 82 | /// transform decisions are enabled. | 82 | /// transform decisions are enabled. |
| 83 | pub lambda: f64, | 83 | pub lambda: f64, |
| 84 | + /// Whether `lambda` is the standard squared-error multiplier. | ||
| 85 | + pub lambda_standard: bool, | ||
| 86 | + /// Extra rate saving an 8x8 trial must buy before it may replace 4x4. | ||
| 87 | + pub t8x8_margin_bits: f64, | ||
| 84 | pub preset: Preset, | 88 | pub preset: Preset, |
| 85 | /// The PPS carries transform_8x8_mode_flag: transform_size_8x8_flag must | 89 | /// The PPS carries transform_8x8_mode_flag: transform_size_8x8_flag must |
| 86 | /// be written for every eligible macroblock (7.3.5). | 90 | /// be written for every eligible macroblock (7.3.5). |
| 87 | pub transform_8x8: bool, | 91 | pub transform_8x8: bool, |
| 92 | + /// Whether intra macroblocks may select 8x8. The PPS can remain enabled | ||
| 93 | + /// while an ablation measures the classes independently. | ||
| 94 | + pub transform_8x8_intra: bool, | ||
| 95 | + /// Whether inter macroblocks may select 8x8. | ||
| 96 | + pub transform_8x8_inter: bool, | ||
| 88 | pub ls: LevelScale4x4, | 97 | pub ls: LevelScale4x4, |
| 89 | pub ls8: LevelScale8x8, | 98 | pub ls8: LevelScale8x8, |
| 90 | /// Neighbourhood of the macroblock being written (CABAC). | 99 | /// Neighbourhood of the macroblock being written (CABAC). |
| @@ -283,7 +292,7 @@ fn modes_allowed(have_top: bool, have_left: bool, have_tl: bool) -> [bool; 9] { | |||
| 283 | 292 | ||
| 284 | #[inline] | 293 | #[inline] |
| 285 | fn motion_rate(e: &MbEnc<'_>, bits: f64) -> i32 { | 294 | fn motion_rate(e: &MbEnc<'_>, bits: f64) -> i32 { |
| 286 | - let lambda = if e.transform_8x8 { | 295 | + let lambda = if e.lambda_standard { |
| 287 | e.lambda.sqrt() | 296 | e.lambda.sqrt() |
| 288 | } else { | 297 | } else { |
| 289 | e.lambda | 298 | e.lambda |
| @@ -688,7 +697,14 @@ fn code_luma_4x4( | |||
| 688 | // half of rate-distortion optimised quantisation, and the half that | 697 | // half of rate-distortion optimised quantisation, and the half that |
| 689 | // pays. Intra blocks are left alone: their reconstruction is what the | 698 | // pays. Intra blocks are left alone: their reconstruction is what the |
| 690 | // next block in the macroblock predicts from. | 699 | // next block in the macroblock predicts from. |
| 691 | - if !intra && !worth_coding(&source, &resid, &lv.luma[blk], zero_block_lambda(e, qp)) { | 700 | + if !intra |
| 701 | + && !worth_coding( | ||
| 702 | + &source, | ||
| 703 | + &resid, | ||
| 704 | + &lv.luma[blk], | ||
| 705 | + zero_block_lambda(e, qp, e.transform_8x8_inter), | ||
| 706 | + ) | ||
| 707 | + { | ||
| 692 | lv.luma[blk] = [0; 16]; | 708 | lv.luma[blk] = [0; 16]; |
| 693 | lv.luma_nz[blk] = 0; | 709 | lv.luma_nz[blk] = 0; |
| 694 | continue; | 710 | continue; |
| @@ -708,8 +724,8 @@ fn code_luma_4x4( | |||
| 708 | /// without costing camera content. | 724 | /// without costing camera content. |
| 709 | const ZERO_BLOCK_LAMBDA: f64 = 0.4; | 725 | const ZERO_BLOCK_LAMBDA: f64 = 0.4; |
| 710 | 726 | ||
| 711 | -fn zero_block_lambda(e: &MbEnc<'_>, qp: i32) -> f64 { | 727 | +fn zero_block_lambda(e: &MbEnc<'_>, qp: i32, transform_8x8: bool) -> f64 { |
| 712 | - if e.transform_8x8 { | 728 | + if transform_8x8 { |
| 713 | e.lambda | 729 | e.lambda |
| 714 | } else { | 730 | } else { |
| 715 | ZERO_BLOCK_LAMBDA * lambda_ssd(qp) | 731 | ZERO_BLOCK_LAMBDA * lambda_ssd(qp) |
| @@ -871,7 +887,20 @@ fn code_chroma( | |||
| 871 | dequant_4x4(&mut raster, &e.ls, qp_c, true); | 887 | dequant_4x4(&mut raster, &e.ls, qp_c, true); |
| 872 | let mut resid = [0i32; 16]; | 888 | let mut resid = [0i32; 16]; |
| 873 | inverse_transform_4x4(&raster, &mut resid); | 889 | inverse_transform_4x4(&raster, &mut resid); |
| 874 | - if !worth_coding(&source, &resid, &ac[comp][blk], zero_block_lambda(e, qp_c)) { | 890 | + if !worth_coding( |
| 891 | + &source, | ||
| 892 | + &resid, | ||
| 893 | + &ac[comp][blk], | ||
| 894 | + zero_block_lambda( | ||
| 895 | + e, | ||
| 896 | + qp_c, | ||
| 897 | + if intra { | ||
| 898 | + e.transform_8x8_intra | ||
| 899 | + } else { | ||
| 900 | + e.transform_8x8_inter | ||
| 901 | + }, | ||
| 902 | + ), | ||
| 903 | + ) { | ||
| 875 | ac[comp][blk] = [0; 16]; | 904 | ac[comp][blk] = [0; 16]; |
| 876 | nz = 0; | 905 | nz = 0; |
| 877 | } | 906 | } |
| @@ -1125,7 +1154,14 @@ fn code_block_8x8( | |||
| 1125 | dequant_8x8(&mut raster, &e.ls8, qp); | 1154 | dequant_8x8(&mut raster, &e.ls8, qp); |
| 1126 | let mut resid = [0i32; 64]; | 1155 | let mut resid = [0i32; 64]; |
| 1127 | inverse_transform_8x8(&raster, &mut resid); | 1156 | inverse_transform_8x8(&raster, &mut resid); |
| 1128 | - if !intra && !worth_coding(&d, &resid, out, zero_block_lambda(e, qp)) { | 1157 | + if !intra |
| 1158 | + && !worth_coding( | ||
| 1159 | + &d, | ||
| 1160 | + &resid, | ||
| 1161 | + out, | ||
| 1162 | + zero_block_lambda(e, qp, e.transform_8x8_inter), | ||
| 1163 | + ) | ||
| 1164 | + { | ||
| 1129 | *out = [0; 64]; | 1165 | *out = [0; 64]; |
| 1130 | return 0; | 1166 | return 0; |
| 1131 | } | 1167 | } |
| @@ -1265,7 +1301,6 @@ fn entropy_bits(w: &EncEntropy, write: impl FnOnce(&mut EncEntropy)) -> i64 { | |||
| 1265 | (probe.bit_len() - before) as i64 | 1301 | (probe.bit_len() - before) as i64 |
| 1266 | } | 1302 | } |
| 1267 | 1303 | ||
| 1268 | - | ||
| 1269 | fn cost_luma_nz_pair( | 1304 | fn cost_luma_nz_pair( |
| 1270 | pic: &Picture, | 1305 | pic: &Picture, |
| 1271 | nbr: &MbNeighbors, | 1306 | nbr: &MbNeighbors, |
| @@ -1545,7 +1580,7 @@ fn encode_intra_mb( | |||
| 1545 | if cost.allow_i4 { | 1580 | if cost.allow_i4 { |
| 1546 | let mut lv4 = chroma_lv.clone(); | 1581 | let mut lv4 = chroma_lv.clone(); |
| 1547 | let (m4, p4) = code_intra_4x4(pic, e, mb_x, mb_y, qp, &nbr, &mut lv4); | 1582 | let (m4, p4) = code_intra_4x4(pic, e, mb_x, mb_y, qp, &nbr, &mut lv4); |
| 1548 | - let cost4 = if e.transform_8x8 { | 1583 | + let cost4 = if e.transform_8x8_intra { |
| 1549 | let bits = intra_mb_bits( | 1584 | let bits = intra_mb_bits( |
| 1550 | pic, | 1585 | pic, |
| 1551 | e, | 1586 | e, |
| @@ -1573,7 +1608,7 @@ fn encode_intra_mb( | |||
| 1573 | // Intra_8x8 on the same terms, when the PPS allows it. | 1608 | // Intra_8x8 on the same terms, when the PPS allows it. |
| 1574 | let mut lv8 = chroma_lv.clone(); | 1609 | let mut lv8 = chroma_lv.clone(); |
| 1575 | let mut recon8 = [0u8; 256]; | 1610 | let mut recon8 = [0u8; 256]; |
| 1576 | - let cost8 = if e.transform_8x8 { | 1611 | + let cost8 = if e.transform_8x8_intra { |
| 1577 | let (m8, p8) = code_intra_8x8(pic, e, mb_x, mb_y, qp, &nbr, &mut lv8); | 1612 | let (m8, p8) = code_intra_8x8(pic, e, mb_x, mb_y, qp, &nbr, &mut lv8); |
| 1578 | modes8 = m8; | 1613 | modes8 = m8; |
| 1579 | pred_modes8 = p8; | 1614 | pred_modes8 = p8; |
| @@ -1596,7 +1631,7 @@ fn encode_intra_mb( | |||
| 1596 | 1631 | ||
| 1597 | let mut lv16 = chroma_lv; | 1632 | let mut lv16 = chroma_lv; |
| 1598 | code_i16_luma(pic, e, mb_x, mb_y, qp, cost.i16_mode, &nbr, &mut lv16); | 1633 | code_i16_luma(pic, e, mb_x, mb_y, qp, cost.i16_mode, &nbr, &mut lv16); |
| 1599 | - let cost16 = if e.transform_8x8 { | 1634 | + let cost16 = if e.transform_8x8_intra { |
| 1600 | let bits = intra_mb_bits( | 1635 | let bits = intra_mb_bits( |
| 1601 | pic, | 1636 | pic, |
| 1602 | e, | 1637 | e, |
| @@ -1614,7 +1649,9 @@ fn encode_intra_mb( | |||
| 1614 | + lambda_ssd(qp) * rough_intra_bits(&lv16, None) as f64 | 1649 | + lambda_ssd(qp) * rough_intra_bits(&lv16, None) as f64 |
| 1615 | }; | 1650 | }; |
| 1616 | 1651 | ||
| 1617 | - if cost8 <= cost4 && cost8 <= cost16 { | 1652 | + if cost8 + e.lambda * e.t8x8_margin_bits <= cost4 |
| 1653 | + && cost8 + e.lambda * e.t8x8_margin_bits <= cost16 | ||
| 1654 | + { | ||
| 1618 | use_i4 = true; | 1655 | use_i4 = true; |
| 1619 | use_i8 = true; | 1656 | use_i8 = true; |
| 1620 | restore_luma(pic, mb_x, mb_y, &recon8); | 1657 | restore_luma(pic, mb_x, mb_y, &recon8); |
| @@ -1643,7 +1680,6 @@ fn encode_intra_mb( | |||
| 1643 | code_i16_luma(pic, e, mb_x, mb_y, qp, cost.i16_mode, &nbr, &mut lv); | 1680 | code_i16_luma(pic, e, mb_x, mb_y, qp, cost.i16_mode, &nbr, &mut lv); |
| 1644 | } | 1681 | } |
| 1645 | 1682 | ||
| 1646 | - | ||
| 1647 | // ---- syntax ---- | 1683 | // ---- syntax ---- |
| 1648 | w.begin_mb(&e.mb_ctx); | 1684 | w.begin_mb(&e.mb_ctx); |
| 1649 | w.coded_mb(e.slice_type == SliceType::P, e.skip_inc); | 1685 | w.coded_mb(e.slice_type == SliceType::P, e.skip_inc); |
| @@ -1698,7 +1734,6 @@ fn encode_intra_mb( | |||
| 1698 | qp, | 1734 | qp, |
| 1699 | }, | 1735 | }, |
| 1700 | ); | 1736 | ); |
| 1701 | - | ||
| 1702 | } | 1737 | } |
| 1703 | 1738 | ||
| 1704 | /// Squared error of the whole macroblock against the source, luma and chroma: | 1739 | /// Squared error of the whole macroblock against the source, luma and chroma: |
| @@ -1928,7 +1963,7 @@ fn encode_inter_mb( | |||
| 1928 | let mut chroma_lv = Levels::default(); | 1963 | let mut chroma_lv = Levels::default(); |
| 1929 | code_chroma(pic, e, mb_x, mb_y, qp, false, &mut chroma_lv); | 1964 | code_chroma(pic, e, mb_x, mb_y, qp, false, &mut chroma_lv); |
| 1930 | let mut lv = chroma_lv.clone(); | 1965 | let mut lv = chroma_lv.clone(); |
| 1931 | - if e.transform_8x8 { | 1966 | + if e.transform_8x8_inter { |
| 1932 | // Both transform sizes on the same prediction; the cheaper | 1967 | // Both transform sizes on the same prediction; the cheaper |
| 1933 | // rate-distortion cost is coded. | 1968 | // rate-distortion cost is coded. |
| 1934 | let mut pred = [0u8; 256]; | 1969 | let mut pred = [0u8; 256]; |
| @@ -1944,7 +1979,7 @@ fn encode_inter_mb( | |||
| 1944 | code_luma_8x8(pic, e, mb_x, mb_y, qp, &mut lv8); | 1979 | code_luma_8x8(pic, e, mb_x, mb_y, qp, &mut lv8); |
| 1945 | let bits8 = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv8); | 1980 | let bits8 = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv8); |
| 1946 | let cost8 = ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits8 as f64; | 1981 | let cost8 = ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits8 as f64; |
| 1947 | - if cost8 < cost4 { | 1982 | + if cost8 + e.lambda * e.t8x8_margin_bits < cost4 { |
| 1948 | lv = lv8; | 1983 | lv = lv8; |
| 1949 | } else { | 1984 | } else { |
| 1950 | lv = lv4; | 1985 | lv = lv4; |
| @@ -1958,7 +1993,7 @@ fn encode_inter_mb( | |||
| 1958 | None => false, | 1993 | None => false, |
| 1959 | Some(_) if empty => true, | 1994 | Some(_) if empty => true, |
| 1960 | Some(ssd_skip) => { | 1995 | Some(ssd_skip) => { |
| 1961 | - let coded = if e.transform_8x8 { | 1996 | + let coded = if e.transform_8x8_inter { |
| 1962 | let bits = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv); | 1997 | let bits = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv); |
| 1963 | ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits as f64 | 1998 | ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits as f64 |
| 1964 | } else { | 1999 | } else { |
| @@ -2009,7 +2044,6 @@ fn encode_inter_mb( | |||
| 2009 | pic.i4_modes[base..base + 4].fill(2); | 2044 | pic.i4_modes[base..base + 4].fill(2); |
| 2010 | } | 2045 | } |
| 2011 | 2046 | ||
| 2012 | - | ||
| 2013 | // ---- syntax ---- | 2047 | // ---- syntax ---- |
| 2014 | w.begin_mb(&e.mb_ctx); | 2048 | w.begin_mb(&e.mb_ctx); |
| 2015 | w.coded_mb(true, e.skip_inc); | 2049 | w.coded_mb(true, e.skip_inc); |
crates/ec-h264/src/enc/mod.rsmodified+117 -10
| @@ -30,7 +30,7 @@ mod vlc; | |||
| 30 | 30 | ||
| 31 | use ec_core::error::{Error, Result}; | 31 | use ec_core::error::{Error, Result}; |
| 32 | use ec_core::BitWriter; | 32 | use ec_core::BitWriter; |
| 33 | -use ec_h264_syntax::nal::{NalUnitType, escape_rbsp}; | 33 | +use ec_h264_syntax::nal::{escape_rbsp, NalUnitType}; |
| 34 | use ec_h264_syntax::{SliceType, Sps}; | 34 | use ec_h264_syntax::{SliceType, Sps}; |
| 35 | 35 | ||
| 36 | use crate::decoder::deblock_picture; | 36 | use crate::decoder::deblock_picture; |
| @@ -39,8 +39,8 @@ use crate::entropy::{FLAG_INTER, FLAG_TRANS8X8}; | |||
| 39 | use crate::transform::{LevelScale4x4, LevelScale8x8}; | 39 | use crate::transform::{LevelScale4x4, LevelScale8x8}; |
| 40 | 40 | ||
| 41 | use entropy::EncEntropy; | 41 | use entropy::EncEntropy; |
| 42 | -use headers::{SeqParams, SliceParams, write_pps, write_slice_header, write_sps}; | 42 | +use headers::{write_pps, write_slice_header, write_sps, SeqParams, SliceParams}; |
| 43 | -use mb::{MbEnc, Source, encode_mb}; | 43 | +use mb::{encode_mb, MbEnc, Source}; |
| 44 | use rc::RateControl; | 44 | use rc::RateControl; |
| 45 | 45 | ||
| 46 | pub use mb::Preset; | 46 | pub use mb::Preset; |
| @@ -79,6 +79,14 @@ pub struct EncoderConfig { | |||
| 79 | /// rate-distortion cost. Leaving it off preserves the Baseline/Main-profile | 79 | /// rate-distortion cost. Leaving it off preserves the Baseline/Main-profile |
| 80 | /// 4x4 path. | 80 | /// 4x4 path. |
| 81 | pub transform_8x8: bool, | 81 | pub transform_8x8: bool, |
| 82 | + #[cfg(feature = "rd-ablation")] | ||
| 83 | + rd_lambda_standard: bool, | ||
| 84 | + #[cfg(feature = "rd-ablation")] | ||
| 85 | + rd_transform_8x8_intra: bool, | ||
| 86 | + #[cfg(feature = "rd-ablation")] | ||
| 87 | + rd_transform_8x8_inter: bool, | ||
| 88 | + #[cfg(feature = "rd-ablation")] | ||
| 89 | + rd_transform_8x8_margin_bits: f64, | ||
| 82 | } | 90 | } |
| 83 | 91 | ||
| 84 | impl EncoderConfig { | 92 | impl EncoderConfig { |
| @@ -96,6 +104,14 @@ impl EncoderConfig { | |||
| 96 | threads: 0, | 104 | threads: 0, |
| 97 | qp: 26, | 105 | qp: 26, |
| 98 | transform_8x8: false, | 106 | transform_8x8: false, |
| 107 | + #[cfg(feature = "rd-ablation")] | ||
| 108 | + rd_lambda_standard: false, | ||
| 109 | + #[cfg(feature = "rd-ablation")] | ||
| 110 | + rd_transform_8x8_intra: false, | ||
| 111 | + #[cfg(feature = "rd-ablation")] | ||
| 112 | + rd_transform_8x8_inter: false, | ||
| 113 | + #[cfg(feature = "rd-ablation")] | ||
| 114 | + rd_transform_8x8_margin_bits: 0.0, | ||
| 99 | } | 115 | } |
| 100 | } | 116 | } |
| 101 | } | 117 | } |
| @@ -170,6 +186,10 @@ impl<'a> PictureView<'a> { | |||
| 170 | impl Encoder { | 186 | impl Encoder { |
| 171 | /// Build an encoder for `cfg`, refusing what it cannot code. | 187 | /// Build an encoder for `cfg`, refusing what it cannot code. |
| 172 | pub fn new(cfg: EncoderConfig) -> Result<Encoder> { | 188 | pub fn new(cfg: EncoderConfig) -> Result<Encoder> { |
| 189 | + #[cfg(feature = "rd-ablation")] | ||
| 190 | + let mut cfg = cfg; | ||
| 191 | + #[cfg(feature = "rd-ablation")] | ||
| 192 | + apply_rd_ablation(&mut cfg); | ||
| 173 | if cfg.width < 16 || cfg.height < 16 { | 193 | if cfg.width < 16 || cfg.height < 16 { |
| 174 | return Err(Error::unsupported( | 194 | return Err(Error::unsupported( |
| 175 | "picture smaller than one macroblock", | 195 | "picture smaller than one macroblock", |
| @@ -510,9 +530,13 @@ fn code_band(pic: &mut Picture, job: BandJob<'_>) -> Vec<u8> { | |||
| 510 | slice_id: job.slice_id, | 530 | slice_id: job.slice_id, |
| 511 | qp: job.qp, | 531 | qp: job.qp, |
| 512 | target_qp: job.qp, | 532 | target_qp: job.qp, |
| 513 | - lambda: lambda_for(job.qp, job.cfg.transform_8x8), | 533 | + lambda: lambda_for(job.qp, rd_lambda_standard(job.cfg)), |
| 534 | + t8x8_margin_bits: rd_transform_8x8_margin_bits(job.cfg), | ||
| 514 | preset: job.cfg.preset, | 535 | preset: job.cfg.preset, |
| 536 | + lambda_standard: rd_lambda_standard(job.cfg), | ||
| 515 | transform_8x8: job.cfg.transform_8x8, | 537 | transform_8x8: job.cfg.transform_8x8, |
| 538 | + transform_8x8_intra: rd_transform_8x8_intra(job.cfg), | ||
| 539 | + transform_8x8_inter: rd_transform_8x8_inter(job.cfg), | ||
| 516 | ls: LevelScale4x4::new(&[16; 16]), | 540 | ls: LevelScale4x4::new(&[16; 16]), |
| 517 | ls8: LevelScale8x8::new(&[16; 64]), | 541 | ls8: LevelScale8x8::new(&[16; 64]), |
| 518 | mb_ctx: crate::entropy::MbCtx::default(), | 542 | mb_ctx: crate::entropy::MbCtx::default(), |
| @@ -528,7 +552,7 @@ fn code_band(pic: &mut Picture, job: BandJob<'_>) -> Vec<u8> { | |||
| 528 | let expected = job.band_target * n as f64 / rows as f64; | 552 | let expected = job.band_target * n as f64 / rows as f64; |
| 529 | let delta = job.rc.row_delta(spent, expected); | 553 | let delta = job.rc.row_delta(spent, expected); |
| 530 | e.target_qp = (job.qp + delta).clamp(10, 51); | 554 | e.target_qp = (job.qp + delta).clamp(10, 51); |
| 531 | - e.lambda = lambda_for(e.target_qp, job.cfg.transform_8x8); | 555 | + e.lambda = lambda_for(e.target_qp, rd_lambda_standard(job.cfg)); |
| 532 | } | 556 | } |
| 533 | for mb_x in 0..mb_w { | 557 | for mb_x in 0..mb_w { |
| 534 | let addr = mb_y * mb_w + mb_x; | 558 | let addr = mb_y * mb_w + mb_x; |
| @@ -545,17 +569,90 @@ pub(crate) fn lambda_ssd(qp: i32) -> f64 { | |||
| 545 | 0.85 * ((f64::from(qp) - 12.0) / 3.0).exp2() | 569 | 0.85 * ((f64::from(qp) - 12.0) / 3.0).exp2() |
| 546 | } | 570 | } |
| 547 | 571 | ||
| 548 | -/// Lagrangian multiplier; the 8x8 path uses the standard squared-error | 572 | +/// Lagrangian multiplier. Production uses the established byte-stable ladder; |
| 549 | -/// multiplier, while the established 4x4 path keeps its byte-stable ladder. | 573 | +/// the ablation feature can select the standard squared-error multiplier. |
| 550 | -fn lambda_for(qp: i32, transform_8x8: bool) -> f64 { | 574 | +fn lambda_for(qp: i32, standard: bool) -> f64 { |
| 551 | - if transform_8x8 { | 575 | + if standard { |
| 552 | lambda_ssd(qp) | 576 | lambda_ssd(qp) |
| 553 | } else { | 577 | } else { |
| 554 | - // The established 4x4 path stays byte-stable when the feature is off. | ||
| 555 | (((f64::from(qp) - 12.0) / 6.0).exp2().round()).max(1.0) | 578 | (((f64::from(qp) - 12.0) / 6.0).exp2().round()).max(1.0) |
| 556 | } | 579 | } |
| 557 | } | 580 | } |
| 558 | 581 | ||
| 582 | +#[cfg(feature = "rd-ablation")] | ||
| 583 | +fn apply_rd_ablation(cfg: &mut EncoderConfig) { | ||
| 584 | + if let Ok(lambda) = std::env::var("EC_H264_RD_LAMBDA") { | ||
| 585 | + cfg.rd_lambda_standard = match lambda.as_str() { | ||
| 586 | + "ladder" => false, | ||
| 587 | + "standard" => true, | ||
| 588 | + _ => panic!("EC_H264_RD_LAMBDA must be ladder or standard"), | ||
| 589 | + }; | ||
| 590 | + } | ||
| 591 | + if let Ok(transform) = std::env::var("EC_H264_RD_T8X8") { | ||
| 592 | + let (intra, inter) = match transform.as_str() { | ||
| 593 | + "off" => (false, false), | ||
| 594 | + "on" => (true, true), | ||
| 595 | + "intra" => (true, false), | ||
| 596 | + "inter" => (false, true), | ||
| 597 | + _ => panic!("EC_H264_RD_T8X8 must be off, on, intra, or inter"), | ||
| 598 | + }; | ||
| 599 | + cfg.rd_transform_8x8_intra = intra; | ||
| 600 | + cfg.rd_transform_8x8_inter = inter; | ||
| 601 | + cfg.transform_8x8 = intra || inter; | ||
| 602 | + } else if cfg.transform_8x8 { | ||
| 603 | + cfg.rd_transform_8x8_intra = true; | ||
| 604 | + cfg.rd_transform_8x8_inter = true; | ||
| 605 | + } | ||
| 606 | + if let Ok(margin) = std::env::var("EC_H264_RD_T8X8_MARGIN_BITS") { | ||
| 607 | + cfg.rd_transform_8x8_margin_bits = margin | ||
| 608 | + .parse() | ||
| 609 | + .expect("EC_H264_RD_T8X8_MARGIN_BITS must be a number"); | ||
| 610 | + } | ||
| 611 | +} | ||
| 612 | + | ||
| 613 | +#[cfg(feature = "rd-ablation")] | ||
| 614 | +fn rd_lambda_standard(cfg: &EncoderConfig) -> bool { | ||
| 615 | + cfg.rd_lambda_standard | ||
| 616 | +} | ||
| 617 | + | ||
| 618 | +#[cfg(not(feature = "rd-ablation"))] | ||
| 619 | +fn rd_lambda_standard(cfg: &EncoderConfig) -> bool { | ||
| 620 | + cfg.transform_8x8 | ||
| 621 | +} | ||
| 622 | + | ||
| 623 | +#[cfg(feature = "rd-ablation")] | ||
| 624 | +fn rd_transform_8x8_intra(cfg: &EncoderConfig) -> bool { | ||
| 625 | + cfg.rd_transform_8x8_intra | ||
| 626 | +} | ||
| 627 | + | ||
| 628 | +#[cfg(not(feature = "rd-ablation"))] | ||
| 629 | +fn rd_transform_8x8_intra(cfg: &EncoderConfig) -> bool { | ||
| 630 | + cfg.transform_8x8 | ||
| 631 | +} | ||
| 632 | + | ||
| 633 | +#[cfg(feature = "rd-ablation")] | ||
| 634 | +fn rd_transform_8x8_inter(cfg: &EncoderConfig) -> bool { | ||
| 635 | + cfg.rd_transform_8x8_inter | ||
| 636 | +} | ||
| 637 | + | ||
| 638 | +#[cfg(not(feature = "rd-ablation"))] | ||
| 639 | +fn rd_transform_8x8_inter(cfg: &EncoderConfig) -> bool { | ||
| 640 | + cfg.transform_8x8 | ||
| 641 | +} | ||
| 642 | + | ||
| 643 | +#[cfg(not(feature = "rd-ablation"))] | ||
| 644 | +const TRANSFORM_8X8_MARGIN_BITS: f64 = 0.0; | ||
| 645 | + | ||
| 646 | +#[cfg(feature = "rd-ablation")] | ||
| 647 | +fn rd_transform_8x8_margin_bits(cfg: &EncoderConfig) -> f64 { | ||
| 648 | + cfg.rd_transform_8x8_margin_bits | ||
| 649 | +} | ||
| 650 | + | ||
| 651 | +#[cfg(not(feature = "rd-ablation"))] | ||
| 652 | +fn rd_transform_8x8_margin_bits(_: &EncoderConfig) -> f64 { | ||
| 653 | + TRANSFORM_8X8_MARGIN_BITS | ||
| 654 | +} | ||
| 655 | + | ||
| 559 | /// Split `mb_h` macroblock rows into at most `n` contiguous bands. | 656 | /// Split `mb_h` macroblock rows into at most `n` contiguous bands. |
| 560 | fn split_bands(mb_h: usize, n: usize) -> Vec<(usize, usize)> { | 657 | fn split_bands(mb_h: usize, n: usize) -> Vec<(usize, usize)> { |
| 561 | let n = n.clamp(1, mb_h); | 658 | let n = n.clamp(1, mb_h); |
| @@ -711,4 +808,14 @@ mod tests { | |||
| 711 | assert!(matches!(err, Error::Unsupported { .. }), "{err}"); | 808 | assert!(matches!(err, Error::Unsupported { .. }), "{err}"); |
| 712 | assert!(Encoder::new(EncoderConfig::new(640, 480)).is_ok()); | 809 | assert!(Encoder::new(EncoderConfig::new(640, 480)).is_ok()); |
| 713 | } | 810 | } |
| 811 | + #[test] | ||
| 812 | + fn production_lambda_tracks_transform_mode() { | ||
| 813 | + let mut cfg = EncoderConfig::new(640, 480); | ||
| 814 | + assert_eq!( | ||
| 815 | + lambda_for(26, rd_lambda_standard(&cfg)), | ||
| 816 | + lambda_for(26, false) | ||
| 817 | + ); | ||
| 818 | + cfg.transform_8x8 = true; | ||
| 819 | + assert_eq!(lambda_for(26, rd_lambda_standard(&cfg)), lambda_ssd(26)); | ||
| 820 | + } | ||
| 714 | } | 821 | } |