Transform_size_8x8_flag emission + cat-5 / interleaved-CAVLC 8x8 residual writers (E3)
Browse files at this commit655dfcc5224f6bad2b89007ebbf22abd11464857Parent: 250a6ac- feat
- transform_size_8x8_flag emission + cat-5 / interleaved-CAVLC 8x8 residual writers (E3)
8 changed files +202 -17
- crates/ec-h264/src/cabac.rs +4 -4
- crates/ec-h264/src/enc/cabac_enc.rs +103 -8
- crates/ec-h264/src/enc/entropy.rs +27 -0
- crates/ec-h264/src/enc/mb.rs +12 -0
- crates/ec-h264/src/enc/mod.rs +1 -0
- crates/ec-h264/src/enc/vlc.rs +38 -0
- crates/ec-h264/src/entropy.rs +7 -0
- crates/ec-h264/tests/encode.rs +10 -5
crates/ec-h264/src/cabac.rsmodified+4 -4
| @@ -61,12 +61,12 @@ pub(crate) const OFF_CBF: usize = 85; | |||
| 61 | pub(crate) const OFF_SIG: usize = 105; | 61 | pub(crate) const OFF_SIG: usize = 105; |
| 62 | pub(crate) const OFF_LAST: usize = 166; | 62 | pub(crate) const OFF_LAST: usize = 166; |
| 63 | pub(crate) const OFF_ABS: usize = 227; | 63 | pub(crate) const OFF_ABS: usize = 227; |
| 64 | -const OFF_TRANSFORM_8X8: usize = 399; | 64 | +pub(crate) const OFF_TRANSFORM_8X8: usize = 399; |
| 65 | /// significant_coeff_flag / last_significant_coeff_flag / coeff_abs_level_minus1 | 65 | /// significant_coeff_flag / last_significant_coeff_flag / coeff_abs_level_minus1 |
| 66 | /// of ctxBlockCat 5 (frame coded), Table 9-34. | 66 | /// of ctxBlockCat 5 (frame coded), Table 9-34. |
| 67 | -const OFF_SIG_8X8: usize = 402; | 67 | +pub(crate) const OFF_SIG_8X8: usize = 402; |
| 68 | -const OFF_LAST_8X8: usize = 417; | 68 | +pub(crate) const OFF_LAST_8X8: usize = 417; |
| 69 | -const OFF_ABS_8X8: usize = 426; | 69 | +pub(crate) const OFF_ABS_8X8: usize = 426; |
| 70 | 70 | ||
| 71 | /// The ctxIdx values the Intra_16x16 part of an mb_type bin string uses, in | 71 | /// The ctxIdx values the Intra_16x16 part of an mb_type bin string uses, in |
| 72 | /// bin order: `[b0, CodedBlockPatternLuma, chroma bin 0, chroma bin 1, | 72 | /// bin order: `[b0, CodedBlockPatternLuma, chroma bin 0, chroma bin 1, |
crates/ec-h264/src/enc/cabac_enc.rsmodified+103 -8
| @@ -20,12 +20,15 @@ | |||
| 20 | use ec_core::BitWriter; | 20 | use ec_core::BitWriter; |
| 21 | 21 | ||
| 22 | use crate::cabac::{ | 22 | use crate::cabac::{ |
| 23 | - ABS_CAT_OFF, CBF_CAT_OFF, I_MB_TYPE_CTX, I_SUFFIX_CTX_P, NUM_CTX, OFF_ABS, OFF_CBF, | 23 | + ABS_CAT_OFF, CBF_CAT_OFF, I_MB_TYPE_CTX, I_SUFFIX_CTX_P, NUM_CTX, OFF_ABS, OFF_ABS_8X8, |
| 24 | - OFF_CBP_CHROMA, OFF_CBP_LUMA, OFF_CHROMA_PRED, OFF_LAST, OFF_MB_TYPE, OFF_MB_TYPE_P, OFF_MVD, | 24 | + OFF_CBF, OFF_CBP_CHROMA, OFF_CBP_LUMA, OFF_CHROMA_PRED, OFF_LAST, OFF_LAST_8X8, OFF_MB_TYPE, |
| 25 | - OFF_PREV_I4, OFF_QP_DELTA, OFF_REM_I4, OFF_SIG, OFF_SKIP_P, SIG_CAT_OFF, init_contexts, | 25 | + OFF_MB_TYPE_P, OFF_MVD, OFF_PREV_I4, OFF_QP_DELTA, OFF_REM_I4, OFF_SIG, OFF_SIG_8X8, |
| 26 | - qp_delta_inc, sig_inc, | 26 | + OFF_SKIP_P, OFF_TRANSFORM_8X8, SIG_CAT_OFF, init_contexts, qp_delta_inc, sig_inc, |
| 27 | +}; | ||
| 28 | +use crate::cabac_tables::{LAST_8X8, SIG_8X8_FRAME}; | ||
| 29 | +use crate::entropy::{ | ||
| 30 | + BlockCat, FLAG_CHROMA_PRED, FLAG_I16, FLAG_INTER, FLAG_PCM, FLAG_TRANS8X8, MbCtx, MbInfo, | ||
| 27 | }; | 31 | }; |
| 28 | -use crate::entropy::{BlockCat, FLAG_CHROMA_PRED, FLAG_I16, FLAG_INTER, FLAG_PCM, MbCtx, MbInfo}; | ||
| 29 | 32 | ||
| 30 | /// The arithmetic encoder of 9.3.4 plus the context state. | 33 | /// The arithmetic encoder of 9.3.4 plus the context state. |
| 31 | pub(crate) struct CabacEnc { | 34 | pub(crate) struct CabacEnc { |
| @@ -385,6 +388,38 @@ impl CabacEnc { | |||
| 385 | } | 388 | } |
| 386 | } | 389 | } |
| 387 | 390 | ||
| 391 | + /// transform_size_8x8_flag (9.3.3.1.1.10), mirroring the decoder's | ||
| 392 | + /// neighbour condition: available and carrying the flag itself. | ||
| 393 | + pub(crate) fn transform_size_8x8_flag(&mut self, flag: bool) { | ||
| 394 | + let cond = |n: Option<MbInfo>| match n { | ||
| 395 | + None => 0, | ||
| 396 | + Some(i) => usize::from(i.flags & FLAG_TRANS8X8 != 0), | ||
| 397 | + }; | ||
| 398 | + let inc = cond(self.ctx.a) + cond(self.ctx.b); | ||
| 399 | + self.decision(OFF_TRANSFORM_8X8 + inc, flag); | ||
| 400 | + } | ||
| 401 | + | ||
| 402 | + /// One luma 8x8 residual block in 8x8 zigzag order (ctxBlockCat 5). No | ||
| 403 | + /// coded_block_flag: for 4:2:0 the coded_block_pattern bit is the whole | ||
| 404 | + /// signal (7.3.5.3.3), so an all-zero block must not reach here. | ||
| 405 | + pub(crate) fn residual_block_8x8(&mut self, coeff: &[i32; 64]) -> u8 { | ||
| 406 | + let last = (0..64) | ||
| 407 | + .rev() | ||
| 408 | + .find(|&i| coeff[i] != 0) | ||
| 409 | + .expect("an 8x8 block with cbp set has a coefficient"); | ||
| 410 | + for i in 0..63 { | ||
| 411 | + let significant = coeff[i] != 0; | ||
| 412 | + self.decision(OFF_SIG_8X8 + usize::from(SIG_8X8_FRAME[i]), significant); | ||
| 413 | + if significant { | ||
| 414 | + self.decision(OFF_LAST_8X8 + usize::from(LAST_8X8[i]), i == last); | ||
| 415 | + if i == last { | ||
| 416 | + break; | ||
| 417 | + } | ||
| 418 | + } | ||
| 419 | + } | ||
| 420 | + self.encode_levels(coeff, last, OFF_ABS_8X8, false) | ||
| 421 | + } | ||
| 422 | + | ||
| 388 | /// One residual block in scan order (7.3.5.3.3): coded_block_flag, the | 423 | /// One residual block in scan order (7.3.5.3.3): coded_block_flag, the |
| 389 | /// significance map, then the levels highest scanning position first. | 424 | /// significance map, then the levels highest scanning position first. |
| 390 | /// | 425 | /// |
| @@ -397,6 +432,7 @@ impl CabacEnc { | |||
| 397 | na: Option<u8>, | 432 | na: Option<u8>, |
| 398 | nb: Option<u8>, | 433 | nb: Option<u8>, |
| 399 | ) -> u8 { | 434 | ) -> u8 { |
| 435 | + debug_assert!(cat != BlockCat::Luma8x8, "cat 5 goes through residual_block_8x8"); | ||
| 400 | let cat_i = cat.ctx_block_cat(); | 436 | let cat_i = cat.ctx_block_cat(); |
| 401 | let max = cat.max_num_coeff(); | 437 | let max = cat.max_num_coeff(); |
| 402 | let last = (0..max).rev().find(|&i| coeff[i] != 0); | 438 | let last = (0..max).rev().find(|&i| coeff[i] != 0); |
| @@ -436,9 +472,13 @@ impl CabacEnc { | |||
| 436 | } | 472 | } |
| 437 | } | 473 | } |
| 438 | 474 | ||
| 439 | - // Levels, highest scanning position first (9.3.3.1.3 counters). | 475 | + self.encode_levels(coeff, last, OFF_ABS + ABS_CAT_OFF[cat_i], cat_i == 3) |
| 440 | - let abs_base = OFF_ABS + ABS_CAT_OFF[cat_i]; | 476 | + } |
| 441 | - let chroma_dc = cat_i == 3; | 477 | + |
| 478 | + /// coeff_abs_level_minus1 and coeff_sign_flag of `coeff[..=last]`, highest | ||
| 479 | + /// scanning position first (9.3.3.1.3 counters). Returns the number of | ||
| 480 | + /// levels written. | ||
| 481 | + fn encode_levels(&mut self, coeff: &[i32], last: usize, abs_base: usize, chroma_dc: bool) -> u8 { | ||
| 442 | let mut num_eq1 = 0u32; | 482 | let mut num_eq1 = 0u32; |
| 443 | let mut num_gt1 = 0u32; | 483 | let mut num_gt1 = 0u32; |
| 444 | let mut total = 0u8; | 484 | let mut total = 0u8; |
| @@ -530,4 +570,59 @@ mod tests { | |||
| 530 | assert!(dec.more_macroblocks().expect("terminate"), "bin {i} end"); | 570 | assert!(dec.more_macroblocks().expect("terminate"), "bin {i} end"); |
| 531 | } | 571 | } |
| 532 | } | 572 | } |
| 573 | + | ||
| 574 | + /// transform_size_8x8_flag under every neighbour combination and ctxBlockCat | ||
| 575 | + /// 5 blocks over a spread of densities and magnitudes all decode back | ||
| 576 | + /// through the decoder's own syntax readers. | ||
| 577 | + #[test] | ||
| 578 | + fn cat5_blocks_and_transform_flag_round_trip() { | ||
| 579 | + let mut state = 0x9E37_79B9u32; | ||
| 580 | + let mut rand = move |m: u32| { | ||
| 581 | + state ^= state << 13; | ||
| 582 | + state ^= state >> 17; | ||
| 583 | + state ^= state << 5; | ||
| 584 | + state % m | ||
| 585 | + }; | ||
| 586 | + let nbr = |t: bool| MbInfo { | ||
| 587 | + flags: if t { FLAG_TRANS8X8 } else { 0 }, | ||
| 588 | + cbp: 0, | ||
| 589 | + dc_cbf: 0, | ||
| 590 | + }; | ||
| 591 | + let mut plan: Vec<(MbCtx, bool, [i32; 64])> = Vec::new(); | ||
| 592 | + for n in 0..300 { | ||
| 593 | + let ctx = MbCtx { | ||
| 594 | + a: [None, Some(nbr(false)), Some(nbr(true))][rand(3) as usize], | ||
| 595 | + b: [None, Some(nbr(false)), Some(nbr(true))][rand(3) as usize], | ||
| 596 | + qp_delta_inc: 0, | ||
| 597 | + }; | ||
| 598 | + let mut c = [0i32; 64]; | ||
| 599 | + let density = [1, 3, 8, 20, 64][rand(5) as usize]; | ||
| 600 | + let mag = [1, 2, 5, 40, 3000][rand(5) as usize]; | ||
| 601 | + for _ in 0..density { | ||
| 602 | + let v = rand(mag) as i32 + 1; | ||
| 603 | + c[rand(64) as usize] = if rand(2) == 0 { v } else { -v }; | ||
| 604 | + } | ||
| 605 | + c[if n % 7 == 0 { 63 } else { 0 }] = 1; // non-empty; exercise the last position | ||
| 606 | + plan.push((ctx, rand(2) == 1, c)); | ||
| 607 | + } | ||
| 608 | + let mut enc = CabacEnc::new(BitWriter::new(), 30, 0); | ||
| 609 | + for (ctx, flag, c) in &plan { | ||
| 610 | + enc.begin_mb(ctx); | ||
| 611 | + enc.transform_size_8x8_flag(*flag); | ||
| 612 | + let tc = enc.residual_block_8x8(c); | ||
| 613 | + assert_eq!(usize::from(tc), c.iter().filter(|&&v| v != 0).count()); | ||
| 614 | + enc.not_end_of_slice(); | ||
| 615 | + } | ||
| 616 | + let bytes = enc.finish().into_bytes(); | ||
| 617 | + | ||
| 618 | + let mut dec = Cabac::new(&bytes, 0, 30, 0).expect("decoder starts"); | ||
| 619 | + for (i, (ctx, flag, c)) in plan.iter().enumerate() { | ||
| 620 | + dec.begin_mb(ctx); | ||
| 621 | + assert_eq!(dec.transform_size_8x8_flag().expect("flag"), *flag, "flag {i}"); | ||
| 622 | + let mut got = [0i32; 64]; | ||
| 623 | + dec.residual_block_8x8(&mut got).expect("block"); | ||
| 624 | + assert_eq!(&got[..], &c[..], "block {i}"); | ||
| 625 | + assert!(dec.more_macroblocks().expect("terminate"), "block {i} end"); | ||
| 626 | + } | ||
| 627 | + } | ||
| 533 | } | 628 | } |
crates/ec-h264/src/enc/entropy.rsmodified+27 -0
| @@ -13,6 +13,12 @@ use crate::entropy::{BlockCat, MbCtx}; | |||
| 13 | use super::cabac_enc::CabacEnc; | 13 | use super::cabac_enc::CabacEnc; |
| 14 | use super::vlc::{write_cbp, write_residual_block}; | 14 | use super::vlc::{write_cbp, write_residual_block}; |
| 15 | 15 | ||
| 16 | +/// CAVLC's 4x4 sub-block `j` of an 8x8 block in 8x8 zigzag order (7.3.5.3.1, | ||
| 17 | +/// 8.5.7): coefficient `i` of sub-block `j` is `c8[4 * i + j]`. | ||
| 18 | +pub(crate) fn sub_block_4x4(c8: &[i32; 64], j: usize) -> [i32; 16] { | ||
| 19 | + std::array::from_fn(|i| c8[4 * i + j]) | ||
| 20 | +} | ||
| 21 | + | ||
| 16 | pub(crate) enum EncEntropy { | 22 | pub(crate) enum EncEntropy { |
| 17 | Cavlc { | 23 | Cavlc { |
| 18 | w: BitWriter, | 24 | w: BitWriter, |
| @@ -109,6 +115,16 @@ impl EncEntropy { | |||
| 109 | } | 115 | } |
| 110 | } | 116 | } |
| 111 | 117 | ||
| 118 | + /// `transform_size_8x8_flag` (7.3.5), where the decoder reads it: after | ||
| 119 | + /// mb_type for an Intra_NxN macroblock, after coded_block_pattern for an | ||
| 120 | + /// eligible inter one. | ||
| 121 | + pub(crate) fn transform_size_8x8_flag(&mut self, flag: bool) { | ||
| 122 | + match self { | ||
| 123 | + EncEntropy::Cavlc { w, .. } => w.write_bit(flag), | ||
| 124 | + EncEntropy::Cabac(c) => c.transform_size_8x8_flag(flag), | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + | ||
| 112 | pub(crate) fn intra_chroma_pred_mode(&mut self, mode: u8) { | 128 | pub(crate) fn intra_chroma_pred_mode(&mut self, mode: u8) { |
| 113 | match self { | 129 | match self { |
| 114 | EncEntropy::Cavlc { w, .. } => w.write_ue(u32::from(mode)), | 130 | EncEntropy::Cavlc { w, .. } => w.write_ue(u32::from(mode)), |
| @@ -141,6 +157,11 @@ impl EncEntropy { | |||
| 141 | 157 | ||
| 142 | /// One residual block in scan order; returns its non-zero count, which the | 158 | /// One residual block in scan order; returns its non-zero count, which the |
| 143 | /// caller records for the neighbours. | 159 | /// caller records for the neighbours. |
| 160 | + /// | ||
| 161 | + /// `Luma8x8` (64 coefficients, 8x8 zigzag order) is CABAC only; the decoder | ||
| 162 | + /// records its count in all four 4x4 slots. Under CAVLC the caller writes | ||
| 163 | + /// the four [`sub_block_4x4`] blocks as `Luma4x4`, each with its own nC | ||
| 164 | + /// and its own count (decoder.rs finish_macroblock, CAVLC branch). | ||
| 144 | pub(crate) fn residual_block( | 165 | pub(crate) fn residual_block( |
| 145 | &mut self, | 166 | &mut self, |
| 146 | coeff: &[i32], | 167 | coeff: &[i32], |
| @@ -148,6 +169,12 @@ impl EncEntropy { | |||
| 148 | na: Option<u8>, | 169 | na: Option<u8>, |
| 149 | nb: Option<u8>, | 170 | nb: Option<u8>, |
| 150 | ) -> u8 { | 171 | ) -> u8 { |
| 172 | + if cat == BlockCat::Luma8x8 { | ||
| 173 | + let EncEntropy::Cabac(c) = self else { | ||
| 174 | + unreachable!("CAVLC codes an 8x8 block as four interleaved 4x4 blocks"); | ||
| 175 | + }; | ||
| 176 | + return c.residual_block_8x8(coeff.try_into().expect("64 coefficients")); | ||
| 177 | + } | ||
| 151 | match self { | 178 | match self { |
| 152 | EncEntropy::Cavlc { w, .. } => { | 179 | EncEntropy::Cavlc { w, .. } => { |
| 153 | let nc = if matches!(cat, BlockCat::ChromaDc(_)) { | 180 | let nc = if matches!(cat, BlockCat::ChromaDc(_)) { |
crates/ec-h264/src/enc/mb.rsmodified+12 -0
| @@ -71,6 +71,9 @@ pub(crate) struct MbEnc<'a> { | |||
| 71 | /// Lagrangian multiplier in the SATD domain. | 71 | /// Lagrangian multiplier in the SATD domain. |
| 72 | pub lambda: i32, | 72 | pub lambda: i32, |
| 73 | pub preset: Preset, | 73 | pub preset: Preset, |
| 74 | + /// The PPS carries transform_8x8_mode_flag: transform_size_8x8_flag must | ||
| 75 | + /// be written for every eligible macroblock (7.3.5). | ||
| 76 | + pub transform_8x8: bool, | ||
| 74 | pub ls: LevelScale4x4, | 77 | pub ls: LevelScale4x4, |
| 75 | /// Neighbourhood of the macroblock being written (CABAC). | 78 | /// Neighbourhood of the macroblock being written (CABAC). |
| 76 | pub mb_ctx: MbCtx, | 79 | pub mb_ctx: MbCtx, |
| @@ -1157,6 +1160,10 @@ fn encode_intra_mb( | |||
| 1157 | let offset = if p_slice { 5 } else { 0 }; | 1160 | let offset = if p_slice { 5 } else { 0 }; |
| 1158 | w.mb_type(p_slice, intra_type + offset); | 1161 | w.mb_type(p_slice, intra_type + offset); |
| 1159 | if use_i4 { | 1162 | if use_i4 { |
| 1163 | + if e.transform_8x8 { | ||
| 1164 | + // corner-cut: always 0 until Intra_8x8 prediction exists (E4). | ||
| 1165 | + w.transform_size_8x8_flag(false); | ||
| 1166 | + } | ||
| 1160 | for blk in 0..16 { | 1167 | for blk in 0..16 { |
| 1161 | let rem = if modes[blk] == pred_modes[blk] { | 1168 | let rem = if modes[blk] == pred_modes[blk] { |
| 1162 | None | 1169 | None |
| @@ -1473,6 +1480,11 @@ fn encode_inter_mb( | |||
| 1473 | } | 1480 | } |
| 1474 | } | 1481 | } |
| 1475 | w.coded_block_pattern(lv.cbp_luma, lv.cbp_chroma, false); | 1482 | w.coded_block_pattern(lv.cbp_luma, lv.cbp_chroma, false); |
| 1483 | + // Every shape this encoder emits is 8x8-aligned, so the decoder reads the | ||
| 1484 | + // flag whenever luma is coded. corner-cut: always 0 until E4 decides. | ||
| 1485 | + if lv.cbp_luma != 0 && e.transform_8x8 { | ||
| 1486 | + w.transform_size_8x8_flag(false); | ||
| 1487 | + } | ||
| 1476 | finish_mb( | 1488 | finish_mb( |
| 1477 | pic, | 1489 | pic, |
| 1478 | e, | 1490 | e, |
crates/ec-h264/src/enc/mod.rsmodified+1 -0
| @@ -499,6 +499,7 @@ fn code_band(pic: &mut Picture, job: BandJob<'_>) -> Vec<u8> { | |||
| 499 | target_qp: job.qp, | 499 | target_qp: job.qp, |
| 500 | lambda: lambda_for(job.qp), | 500 | lambda: lambda_for(job.qp), |
| 501 | preset: job.cfg.preset, | 501 | preset: job.cfg.preset, |
| 502 | + transform_8x8: job.cfg.transform_8x8, | ||
| 502 | ls: LevelScale4x4::new(&[16; 16]), | 503 | ls: LevelScale4x4::new(&[16; 16]), |
| 503 | mb_ctx: crate::entropy::MbCtx::default(), | 504 | mb_ctx: crate::entropy::MbCtx::default(), |
| 504 | skip_inc: 0, | 505 | skip_inc: 0, |
crates/ec-h264/src/enc/vlc.rsmodified+38 -0
| @@ -248,6 +248,44 @@ mod tests { | |||
| 248 | } | 248 | } |
| 249 | } | 249 | } |
| 250 | 250 | ||
| 251 | + /// An 8x8 block written as four interleaved 4x4 blocks reads back through | ||
| 252 | + /// the decoder's 4x4 path into the same 64 coefficients, with the | ||
| 253 | + /// decoder's reassembly rule (`scan8[4 * i + i4] = scan[i]`). | ||
| 254 | + #[test] | ||
| 255 | + fn interleaved_8x8_round_trips() { | ||
| 256 | + let mut state = 0xC0FF_EE00u32; | ||
| 257 | + let mut rand = move |m: u32| { | ||
| 258 | + state = state.wrapping_mul(1664525).wrapping_add(1013904223); | ||
| 259 | + (state >> 16) % m | ||
| 260 | + }; | ||
| 261 | + for _ in 0..200 { | ||
| 262 | + let mut c8 = [0i32; 64]; | ||
| 263 | + for _ in 0..rand(64) { | ||
| 264 | + let v = rand(70) as i32 + 1; | ||
| 265 | + c8[rand(64) as usize] = if rand(2) == 0 { v } else { -v }; | ||
| 266 | + } | ||
| 267 | + let mut w = BitWriter::new(); | ||
| 268 | + let mut tcs = [0u8; 4]; | ||
| 269 | + for j in 0..4 { | ||
| 270 | + tcs[j] = write_residual_block(&mut w, &super::super::entropy::sub_block_4x4(&c8, j), 16, j as i32 * 3); | ||
| 271 | + } | ||
| 272 | + w.write_bit(true); | ||
| 273 | + w.align_to_byte(); | ||
| 274 | + let bytes = w.into_bytes(); | ||
| 275 | + let mut r = BitCursor::new(&bytes, 0); | ||
| 276 | + let mut got = [0i32; 64]; | ||
| 277 | + for j in 0..4 { | ||
| 278 | + let mut scan = [0i32; 16]; | ||
| 279 | + let tc = residual_block(&mut r, &mut scan, 16, j as i32 * 3).expect("decodes"); | ||
| 280 | + assert_eq!(tc, tcs[j]); | ||
| 281 | + for i in 0..16 { | ||
| 282 | + got[4 * i + j] = scan[i]; | ||
| 283 | + } | ||
| 284 | + } | ||
| 285 | + assert_eq!(got, c8); | ||
| 286 | + } | ||
| 287 | + } | ||
| 288 | + | ||
| 251 | /// Every block this writer emits decodes back to the same levels, over a | 289 | /// Every block this writer emits decodes back to the same levels, over a |
| 252 | /// spread that covers empty blocks, trailing ones, long runs, the | 290 | /// spread that covers empty blocks, trailing ones, long runs, the |
| 253 | /// suffix-length ladder and the escape codes. | 291 | /// suffix-length ladder and the escape codes. |
crates/ec-h264/src/entropy.rsmodified+7 -0
| @@ -55,6 +55,11 @@ pub(crate) enum BlockCat { | |||
| 55 | ChromaDc(u8), | 55 | ChromaDc(u8), |
| 56 | /// ChromaACLevel, `ctxBlockCat` 4. | 56 | /// ChromaACLevel, `ctxBlockCat` 4. |
| 57 | ChromaAc, | 57 | ChromaAc, |
| 58 | + /// LumaLevel8x8, `ctxBlockCat` 5: one 64-coefficient block under | ||
| 59 | + /// transform_size_8x8_flag. CABAC codes it whole (no coded_block_flag for | ||
| 60 | + /// 4:2:0); CAVLC never sees this category, it codes the block as four | ||
| 61 | + /// interleaved `Luma4x4` blocks (7.3.5.3.1). | ||
| 62 | + Luma8x8, | ||
| 58 | } | 63 | } |
| 59 | 64 | ||
| 60 | impl BlockCat { | 65 | impl BlockCat { |
| @@ -64,6 +69,7 @@ impl BlockCat { | |||
| 64 | BlockCat::LumaDc | BlockCat::Luma4x4 => 16, | 69 | BlockCat::LumaDc | BlockCat::Luma4x4 => 16, |
| 65 | BlockCat::LumaAc | BlockCat::ChromaAc => 15, | 70 | BlockCat::LumaAc | BlockCat::ChromaAc => 15, |
| 66 | BlockCat::ChromaDc(_) => 4, | 71 | BlockCat::ChromaDc(_) => 4, |
| 72 | + BlockCat::Luma8x8 => 64, | ||
| 67 | } | 73 | } |
| 68 | } | 74 | } |
| 69 | 75 | ||
| @@ -75,6 +81,7 @@ impl BlockCat { | |||
| 75 | BlockCat::Luma4x4 => 2, | 81 | BlockCat::Luma4x4 => 2, |
| 76 | BlockCat::ChromaDc(_) => 3, | 82 | BlockCat::ChromaDc(_) => 3, |
| 77 | BlockCat::ChromaAc => 4, | 83 | BlockCat::ChromaAc => 4, |
| 84 | + BlockCat::Luma8x8 => 5, | ||
| 78 | } | 85 | } |
| 79 | } | 86 | } |
| 80 | } | 87 | } |
crates/ec-h264/tests/encode.rsmodified+10 -5
| @@ -129,11 +129,13 @@ fn reconstruction_matches_the_decoder() { | |||
| 129 | for (w, h) in [(176, 144), (208, 122), (64, 48)] { | 129 | for (w, h) in [(176, 144), (208, 122), (64, 48)] { |
| 130 | let mut clip = Clip::new(w, h); | 130 | let mut clip = Clip::new(w, h); |
| 131 | for qp in [12, 24, 33, 44] { | 131 | for qp in [12, 24, 33, 44] { |
| 132 | - for (threads, preset, cabac) in [ | 132 | + for (threads, preset, cabac, t8x8) in [ |
| 133 | - (1usize, Preset::Fast, true), | 133 | + (1usize, Preset::Fast, true, false), |
| 134 | - (4, Preset::Balanced, true), | 134 | + (4, Preset::Balanced, true, false), |
| 135 | - (1, Preset::Fast, false), | 135 | + (1, Preset::Fast, false, false), |
| 136 | - (3, Preset::Balanced, false), | 136 | + (3, Preset::Balanced, false, false), |
| 137 | + (2, Preset::Balanced, true, true), | ||
| 138 | + (2, Preset::Balanced, false, true), | ||
| 137 | ] { | 139 | ] { |
| 138 | let mut cfg = EncoderConfig::new(w as u32, h as u32); | 140 | let mut cfg = EncoderConfig::new(w as u32, h as u32); |
| 139 | cfg.qp = qp; | 141 | cfg.qp = qp; |
| @@ -141,6 +143,7 @@ fn reconstruction_matches_the_decoder() { | |||
| 141 | cfg.threads = threads; | 143 | cfg.threads = threads; |
| 142 | cfg.preset = preset; | 144 | cfg.preset = preset; |
| 143 | cfg.cabac = cabac; | 145 | cfg.cabac = cabac; |
| 146 | + cfg.transform_8x8 = t8x8; | ||
| 144 | let mut enc = Encoder::new(cfg).expect("encoder"); | 147 | let mut enc = Encoder::new(cfg).expect("encoder"); |
| 145 | let mut stream = Vec::new(); | 148 | let mut stream = Vec::new(); |
| 146 | let mut recons = Vec::new(); | 149 | let mut recons = Vec::new(); |
| @@ -344,9 +347,11 @@ fn ffmpeg_decodes_bit_exactly() { | |||
| 344 | for (w, h) in [(176, 144), (322, 242)] { | 347 | for (w, h) in [(176, 144), (322, 242)] { |
| 345 | for qp in [10, 20, 28, 37, 47] { | 348 | for qp in [10, 20, 28, 37, 47] { |
| 346 | let cavlc = qp % 2 == 0; // both entropy coders across the sweep | 349 | let cavlc = qp % 2 == 0; // both entropy coders across the sweep |
| 350 | + let t8x8 = qp % 3 == 1; // High-profile PPS with the per-MB flag | ||
| 347 | let (stream, recons) = encode_clip(w, h, 6, |cfg| { | 351 | let (stream, recons) = encode_clip(w, h, 6, |cfg| { |
| 348 | cfg.qp = qp; | 352 | cfg.qp = qp; |
| 349 | cfg.cabac = !cavlc; | 353 | cfg.cabac = !cavlc; |
| 354 | + cfg.transform_8x8 = t8x8; | ||
| 350 | }); | 355 | }); |
| 351 | let decoded = ffmpeg_decode(&stream, w, h, &[]).expect("ffmpeg decodes our stream"); | 356 | let decoded = ffmpeg_decode(&stream, w, h, &[]).expect("ffmpeg decodes our stream"); |
| 352 | assert_eq!(decoded.len(), recons.len(), "{w}x{h} qp {qp}: frame count"); | 357 | assert_eq!(decoded.len(), recons.len(), "{w}x{h} qp {qp}: frame count"); |