Unify 8x8 RD lambda
Browse files at this commit67fd45956afd3a3b6757404d52bf87718cf4dffcParent: b6c9578- fix
- unify 8x8 RD lambda
3 changed files +29 -17
- crates/ec-h264/src/enc/mb.rs +20 -10
- crates/ec-h264/src/enc/mod.rs +5 -4
- crates/ec-h264/tests/encode.rs +4 -3
crates/ec-h264/src/enc/mb.rsmodified+20 -10
| @@ -78,8 +78,9 @@ pub(crate) struct MbEnc<'a> { | |||
| 78 | pub qp: i32, | 78 | pub qp: i32, |
| 79 | /// What rate control wants this macroblock coded at. | 79 | /// What rate control wants this macroblock coded at. |
| 80 | pub target_qp: i32, | 80 | pub target_qp: i32, |
| 81 | - /// Lagrangian multiplier for SATD and motion-vector costs. | 81 | + /// Lagrangian multiplier; it is in the squared-error domain when 8x8 |
| 82 | - pub lambda_motion: f64, | 82 | + /// transform decisions are enabled. |
| 83 | + pub lambda: f64, | ||
| 83 | pub preset: Preset, | 84 | pub preset: Preset, |
| 84 | /// The PPS carries transform_8x8_mode_flag: transform_size_8x8_flag must | 85 | /// The PPS carries transform_8x8_mode_flag: transform_size_8x8_flag must |
| 85 | /// be written for every eligible macroblock (7.3.5). | 86 | /// be written for every eligible macroblock (7.3.5). |
| @@ -282,7 +283,12 @@ fn modes_allowed(have_top: bool, have_left: bool, have_tl: bool) -> [bool; 9] { | |||
| 282 | 283 | ||
| 283 | #[inline] | 284 | #[inline] |
| 284 | fn motion_rate(e: &MbEnc<'_>, bits: f64) -> i32 { | 285 | fn motion_rate(e: &MbEnc<'_>, bits: f64) -> i32 { |
| 285 | - (e.lambda_motion * bits).round() as i32 | 286 | + let lambda = if e.transform_8x8 { |
| 287 | + e.lambda.sqrt() | ||
| 288 | + } else { | ||
| 289 | + e.lambda | ||
| 290 | + }; | ||
| 291 | + (lambda * bits).round() as i32 | ||
| 286 | } | 292 | } |
| 287 | 293 | ||
| 288 | /// Code one macroblock: decide its mode, reconstruct it into `pic` and write | 294 | /// Code one macroblock: decide its mode, reconstruct it into `pic` and write |
| @@ -704,7 +710,7 @@ const ZERO_BLOCK_LAMBDA: f64 = 0.4; | |||
| 704 | 710 | ||
| 705 | fn zero_block_lambda(e: &MbEnc<'_>, qp: i32) -> f64 { | 711 | fn zero_block_lambda(e: &MbEnc<'_>, qp: i32) -> f64 { |
| 706 | if e.transform_8x8 { | 712 | if e.transform_8x8 { |
| 707 | - lambda_ssd(qp) | 713 | + e.lambda |
| 708 | } else { | 714 | } else { |
| 709 | ZERO_BLOCK_LAMBDA * lambda_ssd(qp) | 715 | ZERO_BLOCK_LAMBDA * lambda_ssd(qp) |
| 710 | } | 716 | } |
| @@ -1259,6 +1265,7 @@ fn entropy_bits(w: &EncEntropy, write: impl FnOnce(&mut EncEntropy)) -> i64 { | |||
| 1259 | (probe.bit_len() - before) as i64 | 1265 | (probe.bit_len() - before) as i64 |
| 1260 | } | 1266 | } |
| 1261 | 1267 | ||
| 1268 | + | ||
| 1262 | fn cost_luma_nz_pair( | 1269 | fn cost_luma_nz_pair( |
| 1263 | pic: &Picture, | 1270 | pic: &Picture, |
| 1264 | nbr: &MbNeighbors, | 1271 | nbr: &MbNeighbors, |
| @@ -1550,7 +1557,7 @@ fn encode_intra_mb( | |||
| 1550 | cost.i16_mode, | 1557 | cost.i16_mode, |
| 1551 | chroma_mode, | 1558 | chroma_mode, |
| 1552 | ); | 1559 | ); |
| 1553 | - ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda_ssd(qp) * bits as f64 | 1560 | + ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits as f64 |
| 1554 | } else { | 1561 | } else { |
| 1555 | ssd_luma(pic, e, mb_x, mb_y) as f64 | 1562 | ssd_luma(pic, e, mb_x, mb_y) as f64 |
| 1556 | + lambda_ssd(qp) * rough_intra_bits(&lv4, Some((&m4, &p4))) as f64 | 1563 | + lambda_ssd(qp) * rough_intra_bits(&lv4, Some((&m4, &p4))) as f64 |
| @@ -1582,7 +1589,7 @@ fn encode_intra_mb( | |||
| 1582 | cost.i16_mode, | 1589 | cost.i16_mode, |
| 1583 | chroma_mode, | 1590 | chroma_mode, |
| 1584 | ); | 1591 | ); |
| 1585 | - ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda_ssd(qp) * bits as f64 | 1592 | + ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits as f64 |
| 1586 | } else { | 1593 | } else { |
| 1587 | f64::INFINITY | 1594 | f64::INFINITY |
| 1588 | }; | 1595 | }; |
| @@ -1601,7 +1608,7 @@ fn encode_intra_mb( | |||
| 1601 | cost.i16_mode, | 1608 | cost.i16_mode, |
| 1602 | chroma_mode, | 1609 | chroma_mode, |
| 1603 | ); | 1610 | ); |
| 1604 | - ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda_ssd(qp) * bits as f64 | 1611 | + ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits as f64 |
| 1605 | } else { | 1612 | } else { |
| 1606 | ssd_luma(pic, e, mb_x, mb_y) as f64 | 1613 | ssd_luma(pic, e, mb_x, mb_y) as f64 |
| 1607 | + lambda_ssd(qp) * rough_intra_bits(&lv16, None) as f64 | 1614 | + lambda_ssd(qp) * rough_intra_bits(&lv16, None) as f64 |
| @@ -1636,6 +1643,7 @@ fn encode_intra_mb( | |||
| 1636 | code_i16_luma(pic, e, mb_x, mb_y, qp, cost.i16_mode, &nbr, &mut lv); | 1643 | code_i16_luma(pic, e, mb_x, mb_y, qp, cost.i16_mode, &nbr, &mut lv); |
| 1637 | } | 1644 | } |
| 1638 | 1645 | ||
| 1646 | + | ||
| 1639 | // ---- syntax ---- | 1647 | // ---- syntax ---- |
| 1640 | w.begin_mb(&e.mb_ctx); | 1648 | w.begin_mb(&e.mb_ctx); |
| 1641 | w.coded_mb(e.slice_type == SliceType::P, e.skip_inc); | 1649 | w.coded_mb(e.slice_type == SliceType::P, e.skip_inc); |
| @@ -1690,6 +1698,7 @@ fn encode_intra_mb( | |||
| 1690 | qp, | 1698 | qp, |
| 1691 | }, | 1699 | }, |
| 1692 | ); | 1700 | ); |
| 1701 | + | ||
| 1693 | } | 1702 | } |
| 1694 | 1703 | ||
| 1695 | /// Squared error of the whole macroblock against the source, luma and chroma: | 1704 | /// Squared error of the whole macroblock against the source, luma and chroma: |
| @@ -1927,14 +1936,14 @@ fn encode_inter_mb( | |||
| 1927 | let mut lv4 = chroma_lv.clone(); | 1936 | let mut lv4 = chroma_lv.clone(); |
| 1928 | code_luma_4x4(pic, e, mb_x, mb_y, qp, false, &mut lv4); | 1937 | code_luma_4x4(pic, e, mb_x, mb_y, qp, false, &mut lv4); |
| 1929 | let bits4 = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv4); | 1938 | let bits4 = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv4); |
| 1930 | - let cost4 = ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda_ssd(qp) * bits4 as f64; | 1939 | + let cost4 = ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits4 as f64; |
| 1931 | let mut recon4 = [0u8; 256]; | 1940 | let mut recon4 = [0u8; 256]; |
| 1932 | save_luma(pic, mb_x, mb_y, &mut recon4); | 1941 | save_luma(pic, mb_x, mb_y, &mut recon4); |
| 1933 | restore_luma(pic, mb_x, mb_y, &pred); | 1942 | restore_luma(pic, mb_x, mb_y, &pred); |
| 1934 | let mut lv8 = chroma_lv; | 1943 | let mut lv8 = chroma_lv; |
| 1935 | code_luma_8x8(pic, e, mb_x, mb_y, qp, &mut lv8); | 1944 | code_luma_8x8(pic, e, mb_x, mb_y, qp, &mut lv8); |
| 1936 | let bits8 = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv8); | 1945 | let bits8 = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv8); |
| 1937 | - let cost8 = ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda_ssd(qp) * bits8 as f64; | 1946 | + let cost8 = ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits8 as f64; |
| 1938 | if cost8 < cost4 { | 1947 | if cost8 < cost4 { |
| 1939 | lv = lv8; | 1948 | lv = lv8; |
| 1940 | } else { | 1949 | } else { |
| @@ -1951,7 +1960,7 @@ fn encode_inter_mb( | |||
| 1951 | Some(ssd_skip) => { | 1960 | Some(ssd_skip) => { |
| 1952 | let coded = if e.transform_8x8 { | 1961 | let coded = if e.transform_8x8 { |
| 1953 | let bits = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv); | 1962 | let bits = inter_mb_bits(pic, e, w, mb_addr, &nbr, shape, &mvd, &inc, &lv); |
| 1954 | - ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda_ssd(qp) * bits as f64 | 1963 | + ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits as f64 |
| 1955 | } else { | 1964 | } else { |
| 1956 | ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda_ssd(qp) * estimate_bits(&lv) as f64 | 1965 | ssd_mb(pic, e, mb_x, mb_y) as f64 + lambda_ssd(qp) * estimate_bits(&lv) as f64 |
| 1957 | }; | 1966 | }; |
| @@ -2000,6 +2009,7 @@ fn encode_inter_mb( | |||
| 2000 | pic.i4_modes[base..base + 4].fill(2); | 2009 | pic.i4_modes[base..base + 4].fill(2); |
| 2001 | } | 2010 | } |
| 2002 | 2011 | ||
| 2012 | + | ||
| 2003 | // ---- syntax ---- | 2013 | // ---- syntax ---- |
| 2004 | w.begin_mb(&e.mb_ctx); | 2014 | w.begin_mb(&e.mb_ctx); |
| 2005 | w.coded_mb(true, e.skip_inc); | 2015 | w.coded_mb(true, e.skip_inc); |
crates/ec-h264/src/enc/mod.rsmodified+5 -4
| @@ -510,7 +510,7 @@ fn code_band(pic: &mut Picture, job: BandJob<'_>) -> Vec<u8> { | |||
| 510 | slice_id: job.slice_id, | 510 | slice_id: job.slice_id, |
| 511 | qp: job.qp, | 511 | qp: job.qp, |
| 512 | target_qp: job.qp, | 512 | target_qp: job.qp, |
| 513 | - lambda_motion: lambda_for(job.qp, job.cfg.transform_8x8), | 513 | + lambda: lambda_for(job.qp, job.cfg.transform_8x8), |
| 514 | preset: job.cfg.preset, | 514 | preset: job.cfg.preset, |
| 515 | transform_8x8: job.cfg.transform_8x8, | 515 | transform_8x8: job.cfg.transform_8x8, |
| 516 | ls: LevelScale4x4::new(&[16; 16]), | 516 | ls: LevelScale4x4::new(&[16; 16]), |
| @@ -528,7 +528,7 @@ fn code_band(pic: &mut Picture, job: BandJob<'_>) -> Vec<u8> { | |||
| 528 | let expected = job.band_target * n as f64 / rows as f64; | 528 | let expected = job.band_target * n as f64 / rows as f64; |
| 529 | let delta = job.rc.row_delta(spent, expected); | 529 | let delta = job.rc.row_delta(spent, expected); |
| 530 | e.target_qp = (job.qp + delta).clamp(10, 51); | 530 | e.target_qp = (job.qp + delta).clamp(10, 51); |
| 531 | - e.lambda_motion = lambda_for(e.target_qp, job.cfg.transform_8x8); | 531 | + e.lambda = lambda_for(e.target_qp, job.cfg.transform_8x8); |
| 532 | } | 532 | } |
| 533 | for mb_x in 0..mb_w { | 533 | for mb_x in 0..mb_w { |
| 534 | let addr = mb_y * mb_w + mb_x; | 534 | let addr = mb_y * mb_w + mb_x; |
| @@ -545,10 +545,11 @@ pub(crate) fn lambda_ssd(qp: i32) -> f64 { | |||
| 545 | 0.85 * ((f64::from(qp) - 12.0) / 3.0).exp2() | 545 | 0.85 * ((f64::from(qp) - 12.0) / 3.0).exp2() |
| 546 | } | 546 | } |
| 547 | 547 | ||
| 548 | -/// Lagrangian multiplier for SATD and motion-vector costs. | 548 | +/// Lagrangian multiplier; the 8x8 path uses the standard squared-error |
| 549 | +/// multiplier, while the established 4x4 path keeps its byte-stable ladder. | ||
| 549 | fn lambda_for(qp: i32, transform_8x8: bool) -> f64 { | 550 | fn lambda_for(qp: i32, transform_8x8: bool) -> f64 { |
| 550 | if transform_8x8 { | 551 | if transform_8x8 { |
| 551 | - lambda_ssd(qp).sqrt() | 552 | + lambda_ssd(qp) |
| 552 | } else { | 553 | } else { |
| 553 | // The established 4x4 path stays byte-stable when the feature is off. | 554 | // The established 4x4 path stays byte-stable when the feature is off. |
| 554 | (((f64::from(qp) - 12.0) / 6.0).exp2().round()).max(1.0) | 555 | (((f64::from(qp) - 12.0) / 6.0).exp2().round()).max(1.0) |
crates/ec-h264/tests/encode.rsmodified+4 -3
| @@ -442,11 +442,12 @@ fn eight_by_eight_gains_on_flat_and_text() { | |||
| 442 | clip.u.fill(128); | 442 | clip.u.fill(128); |
| 443 | clip.v.fill(128); | 443 | clip.v.fill(128); |
| 444 | }; | 444 | }; |
| 445 | - let run = |t8x8: bool, qp: i32| { | 445 | + let run = |t8x8: bool, qp: i32, cabac: bool| { |
| 446 | let mut cfg = EncoderConfig::new(w as u32, h as u32); | 446 | let mut cfg = EncoderConfig::new(w as u32, h as u32); |
| 447 | cfg.qp = qp; | 447 | cfg.qp = qp; |
| 448 | cfg.gop_size = 10; | 448 | cfg.gop_size = 10; |
| 449 | cfg.transform_8x8 = t8x8; | 449 | cfg.transform_8x8 = t8x8; |
| 450 | + cfg.cabac = cabac; | ||
| 450 | let mut enc = Encoder::new(cfg).expect("encoder"); | 451 | let mut enc = Encoder::new(cfg).expect("encoder"); |
| 451 | let mut clip = Clip::new(w, h); | 452 | let mut clip = Clip::new(w, h); |
| 452 | let (mut bits, mut sum_psnr) = (0usize, 0.0); | 453 | let (mut bits, mut sum_psnr) = (0usize, 0.0); |
| @@ -465,8 +466,8 @@ fn eight_by_eight_gains_on_flat_and_text() { | |||
| 465 | (bits as f64, sum_psnr / 10.0, share) | 466 | (bits as f64, sum_psnr / 10.0, share) |
| 466 | }; | 467 | }; |
| 467 | let qps = [22, 26, 30, 34]; | 468 | let qps = [22, 26, 30, 34]; |
| 468 | - let on: Vec<_> = qps.iter().map(|&q| run(true, q)).collect(); | 469 | + let on: Vec<_> = qps.iter().map(|&q| run(true, q, true)).collect(); |
| 469 | - let off: Vec<_> = qps.iter().map(|&q| run(false, q)).collect(); | 470 | + let off: Vec<_> = qps.iter().map(|&q| run(false, q, true)).collect(); |
| 470 | let on_curve: Vec<_> = on.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect(); | 471 | let on_curve: Vec<_> = on.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect(); |
| 471 | let off_curve: Vec<_> = off.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect(); | 472 | let off_curve: Vec<_> = off.iter().map(|&(bits, psnr, _)| (bits, psnr)).collect(); |
| 472 | let bd = bd_psnr_delta(&on_curve, &off_curve); | 473 | let bd = bd_psnr_delta(&on_curve, &off_curve); |