S8: move residual_luma_style into residual.rs; shared decoder helpers pub(crate); drop unused imports
Browse files at this commitd0af1638ba4dac9fb4a21bef14d6ff872625868dParent: 4fd4ed43 changed files +348 -328
- crates/ec-h264/src/decoder.rs +28 -328
- crates/ec-h264/src/lib.rs +1 -0
- crates/ec-h264/src/residual.rs +319 -0
crates/ec-h264/src/decoder.rsmodified+28 -328
| @@ -45,16 +45,15 @@ use crate::mv::{ | |||
| 45 | predict_mv, ref_idx_cond, write_block, write_intra_mb, write_mvd, | 45 | predict_mv, ref_idx_cond, write_block, write_intra_mb, write_mvd, |
| 46 | }; | 46 | }; |
| 47 | use crate::pred::{ | 47 | use crate::pred::{ |
| 48 | - Nbr4, Nbr8, PlaneWindow, add_residual_4x4, add_residual_8x8, add_residual_dpcm, filter_nbr8, | 48 | + Nbr4, Nbr8, PlaneWindow, add_residual_4x4, add_residual_dpcm, pred_chroma_8x8_halves, |
| 49 | - pred_4x4, pred_8x8, pred_16x16, pred_chroma_8x8_halves, pred_chroma_8x16, | 49 | + pred_chroma_8x16, |
| 50 | }; | 50 | }; |
| 51 | +use crate::residual::residual_luma_style; | ||
| 51 | use crate::sample::Sample; | 52 | use crate::sample::Sample; |
| 52 | use crate::tables::BLK4_POS; | 53 | use crate::tables::BLK4_POS; |
| 53 | use crate::transform::{ | 54 | use crate::transform::{ |
| 54 | CHROMA_DC_422_RASTER, LevelScale4x4, LevelScale8x8, chroma_dc_transform_420, | 55 | CHROMA_DC_422_RASTER, LevelScale4x4, LevelScale8x8, chroma_dc_transform_420, |
| 55 | - chroma_dc_transform_422, dequant_4x4, dequant_8x8, inverse_transform_4x4, | 56 | + chroma_dc_transform_422, dequant_4x4, inverse_transform_4x4, unfield_ac15, unzigzag_ac15, |
| 56 | - inverse_transform_8x8, luma_dc_transform, unfield, unfield_8x8, unfield_ac15, unzigzag, | ||
| 57 | - unzigzag_8x8, unzigzag_ac15, | ||
| 58 | }; | 57 | }; |
| 59 | 58 | ||
| 60 | /// Where a 4x4 block's top-right neighbour samples come from, per | 59 | /// Where a 4x4 block's top-right neighbour samples come from, per |
| @@ -1560,17 +1559,17 @@ fn check_supported<S: Sample>( | |||
| 1560 | 1559 | ||
| 1561 | /// See [`SliceCtx::geo`]. Chroma rows are half of each luma row value. | 1560 | /// See [`SliceCtx::geo`]. Chroma rows are half of each luma row value. |
| 1562 | #[derive(Clone, Copy)] | 1561 | #[derive(Clone, Copy)] |
| 1563 | -struct Geo { | 1562 | +pub(crate) struct Geo { |
| 1564 | - src_y: usize, | 1563 | + pub(crate) src_y: usize, |
| 1565 | - dst_y: usize, | 1564 | + pub(crate) dst_y: usize, |
| 1566 | - dst_cy: usize, | 1565 | + pub(crate) dst_cy: usize, |
| 1567 | - mul: usize, | 1566 | + pub(crate) mul: usize, |
| 1568 | } | 1567 | } |
| 1569 | 1568 | ||
| 1570 | impl Geo { | 1569 | impl Geo { |
| 1571 | /// Picture row of luma row `r` of the macroblock. | 1570 | /// Picture row of luma row `r` of the macroblock. |
| 1572 | #[inline] | 1571 | #[inline] |
| 1573 | - fn y(&self, r: usize) -> usize { | 1572 | + pub(crate) fn y(&self, r: usize) -> usize { |
| 1574 | self.dst_y + r * self.mul | 1573 | self.dst_y + r * self.mul |
| 1575 | } | 1574 | } |
| 1576 | 1575 | ||
| @@ -1931,7 +1930,7 @@ pub(crate) fn luma_nz_pair<S: Sample>( | |||
| 1931 | /// The count grid of colour component `comp` laid out like the luma one: `nz_y`, | 1930 | /// The count grid of colour component `comp` laid out like the luma one: `nz_y`, |
| 1932 | /// or for 4:4:4 the Cb / Cr grids, which are coded like luma. | 1931 | /// or for 4:4:4 the Cb / Cr grids, which are coded like luma. |
| 1933 | #[inline] | 1932 | #[inline] |
| 1934 | -fn nz_grid<S: Sample>(pic: &Picture<S>, comp: usize) -> &[u8] { | 1933 | +pub(crate) fn nz_grid<S: Sample>(pic: &Picture<S>, comp: usize) -> &[u8] { |
| 1935 | if comp == 0 { | 1934 | if comp == 0 { |
| 1936 | &pic.nz_y | 1935 | &pic.nz_y |
| 1937 | } else { | 1936 | } else { |
| @@ -1940,7 +1939,7 @@ fn nz_grid<S: Sample>(pic: &Picture<S>, comp: usize) -> &[u8] { | |||
| 1940 | } | 1939 | } |
| 1941 | 1940 | ||
| 1942 | #[inline] | 1941 | #[inline] |
| 1943 | -fn nz_grid_mut<S: Sample>(pic: &mut Picture<S>, comp: usize) -> &mut [u8] { | 1942 | +pub(crate) fn nz_grid_mut<S: Sample>(pic: &mut Picture<S>, comp: usize) -> &mut [u8] { |
| 1944 | if comp == 0 { | 1943 | if comp == 0 { |
| 1945 | &mut pic.nz_y | 1944 | &mut pic.nz_y |
| 1946 | } else { | 1945 | } else { |
| @@ -1950,7 +1949,7 @@ fn nz_grid_mut<S: Sample>(pic: &mut Picture<S>, comp: usize) -> &mut [u8] { | |||
| 1950 | 1949 | ||
| 1951 | /// [`luma_nz_pair`] over an explicit count grid (`mb_w * 4` wide). | 1950 | /// [`luma_nz_pair`] over an explicit count grid (`mb_w * 4` wide). |
| 1952 | #[inline] | 1951 | #[inline] |
| 1953 | -fn nz_pair_in<S: Sample>( | 1952 | +pub(crate) fn nz_pair_in<S: Sample>( |
| 1954 | pic: &Picture<S>, | 1953 | pic: &Picture<S>, |
| 1955 | grid: &[u8], | 1954 | grid: &[u8], |
| 1956 | n: &MbNeighbors, | 1955 | n: &MbNeighbors, |
| @@ -1975,7 +1974,7 @@ fn nz_pair_in<S: Sample>( | |||
| 1975 | /// (9.3.3.1.1.9, ctxBlockCat 5, 9, 13): a neighbouring macroblock that did not | 1974 | /// (9.3.3.1.1.9, ctxBlockCat 5, 9, 13): a neighbouring macroblock that did not |
| 1976 | /// use the 8x8 transform has no 8x8 block there, which counts as "not coded" | 1975 | /// use the 8x8 transform has no 8x8 block there, which counts as "not coded" |
| 1977 | /// (I_PCM counts as coded). | 1976 | /// (I_PCM counts as coded). |
| 1978 | -fn nz_pair_8x8<S: Sample>( | 1977 | +pub(crate) fn nz_pair_8x8<S: Sample>( |
| 1979 | pic: &Picture<S>, | 1978 | pic: &Picture<S>, |
| 1980 | comp: usize, | 1979 | comp: usize, |
| 1981 | n: &MbNeighbors, | 1980 | n: &MbNeighbors, |
| @@ -2408,30 +2407,30 @@ fn fill_chroma_nz<S: Sample>(pic: &mut Picture<S>, mb_x: usize, mb_y: usize, val | |||
| 2408 | } | 2407 | } |
| 2409 | 2408 | ||
| 2410 | /// Everything the shared residual path needs after prediction is decided. | 2409 | /// Everything the shared residual path needs after prediction is decided. |
| 2411 | -struct MbFinish { | 2410 | +pub(crate) struct MbFinish { |
| 2412 | /// Neighbour availability for *prediction*, which honours | 2411 | /// Neighbour availability for *prediction*, which honours |
| 2413 | /// constrained_intra_pred_flag. The residual contexts use the plain | 2412 | /// constrained_intra_pred_flag. The residual contexts use the plain |
| 2414 | /// availability instead — clause 9.2.1 and 9.3.3.1.1.9 only exclude an | 2413 | /// availability instead — clause 9.2.1 and 9.3.3.1.1.9 only exclude an |
| 2415 | /// inter neighbour there when slice data partitioning is in use, and this | 2414 | /// inter neighbour there when slice data partitioning is in use, and this |
| 2416 | /// decoder refuses partitioned slices outright. | 2415 | /// decoder refuses partitioned slices outright. |
| 2417 | - pred_nbr: MbNeighbors, | 2416 | + pub(crate) pred_nbr: MbNeighbors, |
| 2418 | - is_i16: bool, | 2417 | + pub(crate) is_i16: bool, |
| 2419 | - i16_mode: u8, | 2418 | + pub(crate) i16_mode: u8, |
| 2420 | - cbp_luma: u8, | 2419 | + pub(crate) cbp_luma: u8, |
| 2421 | - cbp_chroma: u8, | 2420 | + pub(crate) cbp_chroma: u8, |
| 2422 | - modes: [u8; 16], | 2421 | + pub(crate) modes: [u8; 16], |
| 2423 | /// Intra8x8PredMode per 8x8 block, when `trans8x8` and `intra`. | 2422 | /// Intra8x8PredMode per 8x8 block, when `trans8x8` and `intra`. |
| 2424 | - modes8: [u8; 4], | 2423 | + pub(crate) modes8: [u8; 4], |
| 2425 | /// transform_size_8x8_flag: the luma residual is four 8x8 transform blocks. | 2424 | /// transform_size_8x8_flag: the luma residual is four 8x8 transform blocks. |
| 2426 | - trans8x8: bool, | 2425 | + pub(crate) trans8x8: bool, |
| 2427 | - chroma_mode: u8, | 2426 | + pub(crate) chroma_mode: u8, |
| 2428 | - intra: bool, | 2427 | + pub(crate) intra: bool, |
| 2429 | - flags: u8, | 2428 | + pub(crate) flags: u8, |
| 2430 | } | 2429 | } |
| 2431 | 2430 | ||
| 2432 | /// The plane of colour component `comp` (0 Y, 1 Cb, 2 Cr). | 2431 | /// The plane of colour component `comp` (0 Y, 1 Cb, 2 Cr). |
| 2433 | #[inline] | 2432 | #[inline] |
| 2434 | -fn plane_mut<S: Sample>(pic: &mut Picture<S>, comp: usize) -> &mut PicPlane<S> { | 2433 | +pub(crate) fn plane_mut<S: Sample>(pic: &mut Picture<S>, comp: usize) -> &mut PicPlane<S> { |
| 2435 | match comp { | 2434 | match comp { |
| 2436 | 0 => &mut pic.y, | 2435 | 0 => &mut pic.y, |
| 2437 | 1 => &mut pic.cb, | 2436 | 1 => &mut pic.cb, |
| @@ -2440,7 +2439,7 @@ fn plane_mut<S: Sample>(pic: &mut Picture<S>, comp: usize) -> &mut PicPlane<S> { | |||
| 2440 | } | 2439 | } |
| 2441 | 2440 | ||
| 2442 | #[inline] | 2441 | #[inline] |
| 2443 | -fn plane_ref<S: Sample>(pic: &Picture<S>, comp: usize) -> &PicPlane<S> { | 2442 | +pub(crate) fn plane_ref<S: Sample>(pic: &Picture<S>, comp: usize) -> &PicPlane<S> { |
| 2444 | match comp { | 2443 | match comp { |
| 2445 | 0 => &pic.y, | 2444 | 0 => &pic.y, |
| 2446 | 1 => &pic.cb, | 2445 | 1 => &pic.cb, |
| @@ -2448,305 +2447,6 @@ fn plane_ref<S: Sample>(pic: &Picture<S>, comp: usize) -> &PicPlane<S> { | |||
| 2448 | } | 2447 | } |
| 2449 | } | 2448 | } |
| 2450 | 2449 | ||
| 2451 | -/// `residual_luma( )` (7.3.5.3.1) of one colour component plus its | ||
| 2452 | -/// reconstruction (8.5): the luma residual, and for ChromaArrayType 3 the Cb | ||
| 2453 | -/// and Cr residual too, which is coded and reconstructed exactly like luma | ||
| 2454 | -/// (against the plane of `comp`, with `qp` its QP', and `ls` / `ls8` its | ||
| 2455 | -/// scaling lists). | ||
| 2456 | -/// | ||
| 2457 | -/// Bitstream order equals reconstruction dependency order, so each block | ||
| 2458 | -/// reconstructs the moment its levels are parsed. `bypass` is | ||
| 2459 | -/// TransformBypassModeFlag: the levels are the residual (8.5.15). | ||
| 2460 | -#[allow(clippy::needless_range_loop, clippy::too_many_arguments)] | ||
| 2461 | -fn residual_luma_style<S: Sample>( | ||
| 2462 | - pic: &mut Picture<S>, | ||
| 2463 | - r: &mut Entropy<'_>, | ||
| 2464 | - nbr: &MbNeighbors, | ||
| 2465 | - m: &MbFinish, | ||
| 2466 | - mb_x: usize, | ||
| 2467 | - mb_y: usize, | ||
| 2468 | - comp: usize, | ||
| 2469 | - ls: &LevelScale4x4, | ||
| 2470 | - ls8: &LevelScale8x8, | ||
| 2471 | - qp: i32, | ||
| 2472 | - bypass: bool, | ||
| 2473 | - fld: bool, | ||
| 2474 | - geo: Geo, | ||
| 2475 | - dc_cbf: &mut u8, | ||
| 2476 | -) -> Result<()> { | ||
| 2477 | - let bd = pic.bd; | ||
| 2478 | - let max = (1i32 << bd) - 1; | ||
| 2479 | - let w4 = pic.mb_w * 4; | ||
| 2480 | - let (bx0, by0) = (mb_x * 4, mb_y * 4); | ||
| 2481 | - let mul = geo.mul; | ||
| 2482 | - let cbp_luma = m.cbp_luma; | ||
| 2483 | - let c = comp as u8; | ||
| 2484 | - let cat3 = pic.cat == 3; | ||
| 2485 | - let mut scan = [0i32; 16]; | ||
| 2486 | - let mut raster = [0i32; 16]; | ||
| 2487 | - let mut resid = [0i32; 16]; | ||
| 2488 | - if m.is_i16 { | ||
| 2489 | - { | ||
| 2490 | - let pl = plane_mut(pic, comp); | ||
| 2491 | - let mut w = PlaneWindow { | ||
| 2492 | - origin: pl.at(mb_x * 16, geo.y(0)), | ||
| 2493 | - stride: pl.stride * mul, | ||
| 2494 | - data: &mut pl.data, | ||
| 2495 | - }; | ||
| 2496 | - pred_16x16(m.i16_mode, bd, &mut w, m.pred_nbr.b, m.pred_nbr.a); | ||
| 2497 | - } | ||
| 2498 | - // Luma DC: un-zigzag over the 4x4 DC array, Hadamard + scale. | ||
| 2499 | - let (na, nb) = nz_pair_in(pic, nz_grid(pic, comp), nbr, bx0, by0); | ||
| 2500 | - let dc_tc = r.residual_block_c(&mut scan, BlockCat::LumaDc, c, na, nb)?; | ||
| 2501 | - *dc_cbf |= u8::from(dc_tc != 0) << comp; | ||
| 2502 | - if fld { | ||
| 2503 | - unfield(&scan, &mut raster); | ||
| 2504 | - } else { | ||
| 2505 | - unzigzag(&scan, &mut raster); | ||
| 2506 | - } | ||
| 2507 | - if !bypass { | ||
| 2508 | - luma_dc_transform(&mut raster, ls, qp); | ||
| 2509 | - } | ||
| 2510 | - let dc = raster; | ||
| 2511 | - // Lossless horizontal / vertical prediction accumulates the residual | ||
| 2512 | - // over the whole macroblock, so every block takes part. | ||
| 2513 | - let dpcm = bypass && m.i16_mode < 2; | ||
| 2514 | - let horizontal = m.i16_mode == 1; | ||
| 2515 | - for blk in 0..16 { | ||
| 2516 | - let (dx, dy) = BLK4_POS[blk]; | ||
| 2517 | - let (bx, by) = (bx0 + dx as usize, by0 + dy as usize); | ||
| 2518 | - let tc = if cbp_luma != 0 { | ||
| 2519 | - let (na, nb) = nz_pair_in(pic, nz_grid(pic, comp), nbr, bx, by); | ||
| 2520 | - r.residual_block_c(&mut scan, BlockCat::LumaAc, c, na, nb)? | ||
| 2521 | - } else { | ||
| 2522 | - 0 | ||
| 2523 | - }; | ||
| 2524 | - nz_grid_mut(pic, comp)[by * w4 + bx] = tc; | ||
| 2525 | - let dc_blk = dc[dy as usize * 4 + dx as usize]; | ||
| 2526 | - if tc == 0 && dc_blk == 0 && !dpcm { | ||
| 2527 | - continue; // prediction already in place | ||
| 2528 | - } | ||
| 2529 | - if tc > 0 { | ||
| 2530 | - if fld { | ||
| 2531 | - unfield_ac15(&scan, &mut raster); | ||
| 2532 | - } else { | ||
| 2533 | - unzigzag_ac15(&scan, &mut raster); | ||
| 2534 | - } | ||
| 2535 | - if !bypass { | ||
| 2536 | - dequant_4x4(&mut raster, ls, qp, true); | ||
| 2537 | - } | ||
| 2538 | - } else { | ||
| 2539 | - raster = [0; 16]; // DC-only block | ||
| 2540 | - } | ||
| 2541 | - raster[0] = dc_blk; | ||
| 2542 | - let pl = plane_mut(pic, comp); | ||
| 2543 | - let origin = pl.at(mb_x * 16 + dx as usize * 4, geo.y(dy as usize * 4)); | ||
| 2544 | - let stride = pl.stride * mul; | ||
| 2545 | - if bypass { | ||
| 2546 | - if dpcm { | ||
| 2547 | - let first = if horizontal { dx == 0 } else { dy == 0 }; | ||
| 2548 | - add_residual_dpcm( | ||
| 2549 | - &mut pl.data, | ||
| 2550 | - bd, | ||
| 2551 | - stride, | ||
| 2552 | - origin, | ||
| 2553 | - 4, | ||
| 2554 | - 4, | ||
| 2555 | - &raster, | ||
| 2556 | - horizontal, | ||
| 2557 | - first, | ||
| 2558 | - ); | ||
| 2559 | - } else { | ||
| 2560 | - add_residual_4x4(&mut pl.data, bd, stride, origin, &raster); | ||
| 2561 | - } | ||
| 2562 | - } else { | ||
| 2563 | - inverse_transform_4x4(&raster, &mut resid); | ||
| 2564 | - add_residual_4x4(&mut pl.data, bd, stride, origin, &resid); | ||
| 2565 | - } | ||
| 2566 | - } | ||
| 2567 | - } else if m.trans8x8 { | ||
| 2568 | - // 8x8 transform (8.5.13): predict, parse and reconstruct one 8x8 block | ||
| 2569 | - // at a time, because an Intra_8x8 block predicts from the reconstructed | ||
| 2570 | - // samples of the blocks before it. | ||
| 2571 | - let mut scan8 = [0i32; 64]; | ||
| 2572 | - let mut raster8 = [0i32; 64]; | ||
| 2573 | - let mut resid8 = [0i32; 64]; | ||
| 2574 | - for blk8 in 0..4 { | ||
| 2575 | - let (x, y) = (mb_x * 16 + (blk8 % 2) * 8, geo.y((blk8 / 2) * 8)); | ||
| 2576 | - let (bx, by) = (bx0 + (blk8 % 2) * 2, by0 + (blk8 / 2) * 2); | ||
| 2577 | - let origin = plane_ref(pic, comp).at(x, y); | ||
| 2578 | - let stride = plane_ref(pic, comp).stride * mul; | ||
| 2579 | - if m.intra { | ||
| 2580 | - let n = filter_nbr8(&gather_nbr8_in( | ||
| 2581 | - &plane_ref(pic, comp).data, | ||
| 2582 | - &m.pred_nbr, | ||
| 2583 | - blk8, | ||
| 2584 | - origin, | ||
| 2585 | - stride, | ||
| 2586 | - )); | ||
| 2587 | - let mut p = [S::default(); 64]; | ||
| 2588 | - pred_8x8(m.modes8[blk8], bd, &n, &mut p); | ||
| 2589 | - let pl = plane_mut(pic, comp); | ||
| 2590 | - for ry in 0..8 { | ||
| 2591 | - let row = origin + ry * stride; | ||
| 2592 | - pl.data[row..row + 8].copy_from_slice(&p[ry * 8..ry * 8 + 8]); | ||
| 2593 | - } | ||
| 2594 | - } | ||
| 2595 | - if cbp_luma & (1 << blk8) == 0 { | ||
| 2596 | - let grid = nz_grid_mut(pic, comp); | ||
| 2597 | - for dy in 0..2 { | ||
| 2598 | - let base = (by + dy) * w4 + bx; | ||
| 2599 | - grid[base..base + 2].fill(0); | ||
| 2600 | - } | ||
| 2601 | - continue; | ||
| 2602 | - } | ||
| 2603 | - let tc = if r.is_cabac() { | ||
| 2604 | - // ctxBlockCat 5: one 64-coefficient block, and every 4x4 slot | ||
| 2605 | - // of it reports the block's own count to the neighbours | ||
| 2606 | - // (9.3.3.1.1.9 resolves them to the 8x8 transform block). Only | ||
| 2607 | - // ChromaArrayType 3 sends a coded_block_flag for it. | ||
| 2608 | - let cbf = cat3.then(|| nz_pair_8x8(pic, comp, nbr, bx, by)); | ||
| 2609 | - let tc = r.residual_block_8x8(&mut scan8, c, cbf)?; | ||
| 2610 | - let grid = nz_grid_mut(pic, comp); | ||
| 2611 | - for dy in 0..2 { | ||
| 2612 | - let base = (by + dy) * w4 + bx; | ||
| 2613 | - grid[base..base + 2].fill(tc.min(16)); | ||
| 2614 | - } | ||
| 2615 | - tc | ||
| 2616 | - } else { | ||
| 2617 | - // CAVLC codes the same 64 coefficients as four interleaved 4x4 | ||
| 2618 | - // blocks (7.3.5.3.1), each with its own nC neighbourhood. | ||
| 2619 | - let mut tc = 0u8; | ||
| 2620 | - for i4 in 0..4 { | ||
| 2621 | - let (dx, dy) = BLK4_POS[blk8 * 4 + i4]; | ||
| 2622 | - let (sx, sy) = (bx0 + dx as usize, by0 + dy as usize); | ||
| 2623 | - let (na, nb) = nz_pair_in(pic, nz_grid(pic, comp), nbr, sx, sy); | ||
| 2624 | - let n = r.residual_block_c(&mut scan, BlockCat::Luma4x4, c, na, nb)?; | ||
| 2625 | - nz_grid_mut(pic, comp)[sy * w4 + sx] = n; | ||
| 2626 | - tc += n; | ||
| 2627 | - for i in 0..16 { | ||
| 2628 | - scan8[4 * i + i4] = scan[i]; | ||
| 2629 | - } | ||
| 2630 | - } | ||
| 2631 | - tc | ||
| 2632 | - }; | ||
| 2633 | - if tc == 0 { | ||
| 2634 | - continue; | ||
| 2635 | - } | ||
| 2636 | - if fld { | ||
| 2637 | - unfield_8x8(&scan8, &mut raster8); | ||
| 2638 | - } else { | ||
| 2639 | - unzigzag_8x8(&scan8, &mut raster8); | ||
| 2640 | - } | ||
| 2641 | - let pl = plane_mut(pic, comp); | ||
| 2642 | - if bypass { | ||
| 2643 | - if m.intra && m.modes8[blk8] < 2 { | ||
| 2644 | - add_residual_dpcm( | ||
| 2645 | - &mut pl.data, | ||
| 2646 | - bd, | ||
| 2647 | - stride, | ||
| 2648 | - origin, | ||
| 2649 | - 8, | ||
| 2650 | - 8, | ||
| 2651 | - &raster8, | ||
| 2652 | - m.modes8[blk8] == 1, | ||
| 2653 | - true, | ||
| 2654 | - ); | ||
| 2655 | - } else { | ||
| 2656 | - add_residual_8x8(&mut pl.data, bd, stride, origin, &raster8); | ||
| 2657 | - } | ||
| 2658 | - } else { | ||
| 2659 | - dequant_8x8(&mut raster8, ls8, qp); | ||
| 2660 | - inverse_transform_8x8(&raster8, &mut resid8); | ||
| 2661 | - add_residual_8x8(&mut pl.data, bd, stride, origin, &resid8); | ||
| 2662 | - } | ||
| 2663 | - } | ||
| 2664 | - } else { | ||
| 2665 | - for blk in 0..16 { | ||
| 2666 | - let (dx, dy) = BLK4_POS[blk]; | ||
| 2667 | - let (bx, by) = (bx0 + dx as usize, by0 + dy as usize); | ||
| 2668 | - let tc = if cbp_luma & (1 << (blk >> 2)) != 0 { | ||
| 2669 | - let (na, nb) = nz_pair_in(pic, nz_grid(pic, comp), nbr, bx, by); | ||
| 2670 | - r.residual_block_c(&mut scan, BlockCat::Luma4x4, c, na, nb)? | ||
| 2671 | - } else { | ||
| 2672 | - 0 | ||
| 2673 | - }; | ||
| 2674 | - nz_grid_mut(pic, comp)[by * w4 + bx] = tc; | ||
| 2675 | - let (x, y) = (mb_x * 16 + dx as usize * 4, geo.y(dy as usize * 4)); | ||
| 2676 | - let origin = plane_ref(pic, comp).at(x, y); | ||
| 2677 | - let stride = plane_ref(pic, comp).stride * mul; | ||
| 2678 | - if m.intra { | ||
| 2679 | - let n = | ||
| 2680 | - gather_nbr4_in(&plane_ref(pic, comp).data, &m.pred_nbr, blk, origin, stride); | ||
| 2681 | - let mut p = [S::default(); 16]; | ||
| 2682 | - pred_4x4(m.modes[blk], bd, &n, &mut p); | ||
| 2683 | - let pl = plane_mut(pic, comp); | ||
| 2684 | - if tc == 0 { | ||
| 2685 | - // Pure prediction: copy the 16 samples in. | ||
| 2686 | - for ry in 0..4 { | ||
| 2687 | - let row = origin + ry * stride; | ||
| 2688 | - pl.data[row..row + 4].copy_from_slice(&p[ry * 4..ry * 4 + 4]); | ||
| 2689 | - } | ||
| 2690 | - continue; | ||
| 2691 | - } | ||
| 2692 | - if fld { | ||
| 2693 | - unfield(&scan, &mut raster); | ||
| 2694 | - } else { | ||
| 2695 | - unzigzag(&scan, &mut raster); | ||
| 2696 | - } | ||
| 2697 | - if bypass { | ||
| 2698 | - for ry in 0..4 { | ||
| 2699 | - let row = origin + ry * stride; | ||
| 2700 | - pl.data[row..row + 4].copy_from_slice(&p[ry * 4..ry * 4 + 4]); | ||
| 2701 | - } | ||
| 2702 | - if m.modes[blk] < 2 { | ||
| 2703 | - add_residual_dpcm( | ||
| 2704 | - &mut pl.data, | ||
| 2705 | - bd, | ||
| 2706 | - stride, | ||
| 2707 | - origin, | ||
| 2708 | - 4, | ||
| 2709 | - 4, | ||
| 2710 | - &raster, | ||
| 2711 | - m.modes[blk] == 1, | ||
| 2712 | - true, | ||
| 2713 | - ); | ||
| 2714 | - } else { | ||
| 2715 | - add_residual_4x4(&mut pl.data, bd, stride, origin, &raster); | ||
| 2716 | - } | ||
| 2717 | - continue; | ||
| 2718 | - } | ||
| 2719 | - dequant_4x4(&mut raster, ls, qp, false); | ||
| 2720 | - inverse_transform_4x4(&raster, &mut resid); | ||
| 2721 | - for ry in 0..4 { | ||
| 2722 | - let row = origin + ry * stride; | ||
| 2723 | - for rx in 0..4 { | ||
| 2724 | - let v = p[ry * 4 + rx].to_i32() + resid[ry * 4 + rx]; | ||
| 2725 | - pl.data[row + rx] = S::clip(v, max); | ||
| 2726 | - } | ||
| 2727 | - } | ||
| 2728 | - } else { | ||
| 2729 | - if tc == 0 { | ||
| 2730 | - continue; // motion-compensated prediction already in place | ||
| 2731 | - } | ||
| 2732 | - if fld { | ||
| 2733 | - unfield(&scan, &mut raster); | ||
| 2734 | - } else { | ||
| 2735 | - unzigzag(&scan, &mut raster); | ||
| 2736 | - } | ||
| 2737 | - let pl = plane_mut(pic, comp); | ||
| 2738 | - if bypass { | ||
| 2739 | - add_residual_4x4(&mut pl.data, bd, stride, origin, &raster); | ||
| 2740 | - } else { | ||
| 2741 | - dequant_4x4(&mut raster, ls, qp, false); | ||
| 2742 | - inverse_transform_4x4(&raster, &mut resid); | ||
| 2743 | - add_residual_4x4(&mut pl.data, bd, stride, origin, &resid); | ||
| 2744 | - } | ||
| 2745 | - } | ||
| 2746 | - } | ||
| 2747 | - } | ||
| 2748 | - Ok(()) | ||
| 2749 | -} | ||
| 2750 | 2450 | ||
| 2751 | /// The residual half of a macroblock (7.3.5.3 + 8.5), shared by the intra and | 2451 | /// The residual half of a macroblock (7.3.5.3 + 8.5), shared by the intra and |
| 2752 | /// inter paths. | 2452 | /// inter paths. |
crates/ec-h264/src/lib.rsmodified+1 -0
| @@ -53,6 +53,7 @@ mod inter16; | |||
| 53 | mod mbaff; | 53 | mod mbaff; |
| 54 | mod mv; | 54 | mod mv; |
| 55 | mod pred; | 55 | mod pred; |
| 56 | +mod residual; | ||
| 56 | mod sample; | 57 | mod sample; |
| 57 | mod tables; | 58 | mod tables; |
| 58 | mod transform; | 59 | mod transform; |
crates/ec-h264/src/residual.rsadded+319 -0
| @@ -0,0 +1,319 @@ | |||
| 1 | +use ec_core::error::Result; | ||
| 2 | + | ||
| 3 | +use crate::decoder::{ | ||
| 4 | + Geo, MbFinish, MbNeighbors, gather_nbr4_in, gather_nbr8_in, nz_grid, nz_grid_mut, nz_pair_8x8, nz_pair_in, | ||
| 5 | + plane_mut, plane_ref, | ||
| 6 | +}; | ||
| 7 | +use crate::dpb::Picture; | ||
| 8 | +use crate::entropy::{BlockCat, Entropy}; | ||
| 9 | +use crate::pred::{ | ||
| 10 | + PlaneWindow, add_residual_4x4, add_residual_8x8, add_residual_dpcm, filter_nbr8, pred_16x16, | ||
| 11 | + pred_4x4, pred_8x8, | ||
| 12 | +}; | ||
| 13 | +use crate::sample::Sample; | ||
| 14 | +use crate::tables::BLK4_POS; | ||
| 15 | +use crate::transform::{ | ||
| 16 | + LevelScale4x4, LevelScale8x8, dequant_4x4, dequant_8x8, inverse_transform_4x4, | ||
| 17 | + inverse_transform_8x8, unfield, unfield_8x8, unfield_ac15, unzigzag, unzigzag_8x8, | ||
| 18 | + luma_dc_transform, unzigzag_ac15, | ||
| 19 | +}; | ||
| 20 | + | ||
| 21 | +/// `residual_luma( )` (7.3.5.3.1) of one colour component plus its | ||
| 22 | +/// reconstruction (8.5): the luma residual, and for ChromaArrayType 3 the Cb | ||
| 23 | +/// and Cr residual too, which is coded and reconstructed exactly like luma | ||
| 24 | +/// (against the plane of `comp`, with `qp` its QP', and `ls` / `ls8` its | ||
| 25 | +/// scaling lists). | ||
| 26 | +/// | ||
| 27 | +/// Bitstream order equals reconstruction dependency order, so each block | ||
| 28 | +/// reconstructs the moment its levels are parsed. `bypass` is | ||
| 29 | +/// TransformBypassModeFlag: the levels are the residual (8.5.15). | ||
| 30 | +#[allow(clippy::needless_range_loop, clippy::too_many_arguments)] | ||
| 31 | +pub(crate) fn residual_luma_style<S: Sample>( | ||
| 32 | + pic: &mut Picture<S>, | ||
| 33 | + r: &mut Entropy<'_>, | ||
| 34 | + nbr: &MbNeighbors, | ||
| 35 | + m: &MbFinish, | ||
| 36 | + mb_x: usize, | ||
| 37 | + mb_y: usize, | ||
| 38 | + comp: usize, | ||
| 39 | + ls: &LevelScale4x4, | ||
| 40 | + ls8: &LevelScale8x8, | ||
| 41 | + qp: i32, | ||
| 42 | + bypass: bool, | ||
| 43 | + fld: bool, | ||
| 44 | + geo: Geo, | ||
| 45 | + dc_cbf: &mut u8, | ||
| 46 | +) -> Result<()> { | ||
| 47 | + let bd = pic.bd; | ||
| 48 | + let max = (1i32 << bd) - 1; | ||
| 49 | + let w4 = pic.mb_w * 4; | ||
| 50 | + let (bx0, by0) = (mb_x * 4, mb_y * 4); | ||
| 51 | + let mul = geo.mul; | ||
| 52 | + let cbp_luma = m.cbp_luma; | ||
| 53 | + let c = comp as u8; | ||
| 54 | + let cat3 = pic.cat == 3; | ||
| 55 | + let mut scan = [0i32; 16]; | ||
| 56 | + let mut raster = [0i32; 16]; | ||
| 57 | + let mut resid = [0i32; 16]; | ||
| 58 | + if m.is_i16 { | ||
| 59 | + { | ||
| 60 | + let pl = plane_mut(pic, comp); | ||
| 61 | + let mut w = PlaneWindow { | ||
| 62 | + origin: pl.at(mb_x * 16, geo.y(0)), | ||
| 63 | + stride: pl.stride * mul, | ||
| 64 | + data: &mut pl.data, | ||
| 65 | + }; | ||
| 66 | + pred_16x16(m.i16_mode, bd, &mut w, m.pred_nbr.b, m.pred_nbr.a); | ||
| 67 | + } | ||
| 68 | + // Luma DC: un-zigzag over the 4x4 DC array, Hadamard + scale. | ||
| 69 | + let (na, nb) = nz_pair_in(pic, nz_grid(pic, comp), nbr, bx0, by0); | ||
| 70 | + let dc_tc = r.residual_block_c(&mut scan, BlockCat::LumaDc, c, na, nb)?; | ||
| 71 | + *dc_cbf |= u8::from(dc_tc != 0) << comp; | ||
| 72 | + if fld { | ||
| 73 | + unfield(&scan, &mut raster); | ||
| 74 | + } else { | ||
| 75 | + unzigzag(&scan, &mut raster); | ||
| 76 | + } | ||
| 77 | + if !bypass { | ||
| 78 | + luma_dc_transform(&mut raster, ls, qp); | ||
| 79 | + } | ||
| 80 | + let dc = raster; | ||
| 81 | + // Lossless horizontal / vertical prediction accumulates the residual | ||
| 82 | + // over the whole macroblock, so every block takes part. | ||
| 83 | + let dpcm = bypass && m.i16_mode < 2; | ||
| 84 | + let horizontal = m.i16_mode == 1; | ||
| 85 | + for blk in 0..16 { | ||
| 86 | + let (dx, dy) = BLK4_POS[blk]; | ||
| 87 | + let (bx, by) = (bx0 + dx as usize, by0 + dy as usize); | ||
| 88 | + let tc = if cbp_luma != 0 { | ||
| 89 | + let (na, nb) = nz_pair_in(pic, nz_grid(pic, comp), nbr, bx, by); | ||
| 90 | + r.residual_block_c(&mut scan, BlockCat::LumaAc, c, na, nb)? | ||
| 91 | + } else { | ||
| 92 | + 0 | ||
| 93 | + }; | ||
| 94 | + nz_grid_mut(pic, comp)[by * w4 + bx] = tc; | ||
| 95 | + let dc_blk = dc[dy as usize * 4 + dx as usize]; | ||
| 96 | + if tc == 0 && dc_blk == 0 && !dpcm { | ||
| 97 | + continue; // prediction already in place | ||
| 98 | + } | ||
| 99 | + if tc > 0 { | ||
| 100 | + if fld { | ||
| 101 | + unfield_ac15(&scan, &mut raster); | ||
| 102 | + } else { | ||
| 103 | + unzigzag_ac15(&scan, &mut raster); | ||
| 104 | + } | ||
| 105 | + if !bypass { | ||
| 106 | + dequant_4x4(&mut raster, ls, qp, true); | ||
| 107 | + } | ||
| 108 | + } else { | ||
| 109 | + raster = [0; 16]; // DC-only block | ||
| 110 | + } | ||
| 111 | + raster[0] = dc_blk; | ||
| 112 | + let pl = plane_mut(pic, comp); | ||
| 113 | + let origin = pl.at(mb_x * 16 + dx as usize * 4, geo.y(dy as usize * 4)); | ||
| 114 | + let stride = pl.stride * mul; | ||
| 115 | + if bypass { | ||
| 116 | + if dpcm { | ||
| 117 | + let first = if horizontal { dx == 0 } else { dy == 0 }; | ||
| 118 | + add_residual_dpcm( | ||
| 119 | + &mut pl.data, | ||
| 120 | + bd, | ||
| 121 | + stride, | ||
| 122 | + origin, | ||
| 123 | + 4, | ||
| 124 | + 4, | ||
| 125 | + &raster, | ||
| 126 | + horizontal, | ||
| 127 | + first, | ||
| 128 | + ); | ||
| 129 | + } else { | ||
| 130 | + add_residual_4x4(&mut pl.data, bd, stride, origin, &raster); | ||
| 131 | + } | ||
| 132 | + } else { | ||
| 133 | + inverse_transform_4x4(&raster, &mut resid); | ||
| 134 | + add_residual_4x4(&mut pl.data, bd, stride, origin, &resid); | ||
| 135 | + } | ||
| 136 | + } | ||
| 137 | + } else if m.trans8x8 { | ||
| 138 | + // 8x8 transform (8.5.13): predict, parse and reconstruct one 8x8 block | ||
| 139 | + // at a time, because an Intra_8x8 block predicts from the reconstructed | ||
| 140 | + // samples of the blocks before it. | ||
| 141 | + let mut scan8 = [0i32; 64]; | ||
| 142 | + let mut raster8 = [0i32; 64]; | ||
| 143 | + let mut resid8 = [0i32; 64]; | ||
| 144 | + for blk8 in 0..4 { | ||
| 145 | + let (x, y) = (mb_x * 16 + (blk8 % 2) * 8, geo.y((blk8 / 2) * 8)); | ||
| 146 | + let (bx, by) = (bx0 + (blk8 % 2) * 2, by0 + (blk8 / 2) * 2); | ||
| 147 | + let origin = plane_ref(pic, comp).at(x, y); | ||
| 148 | + let stride = plane_ref(pic, comp).stride * mul; | ||
| 149 | + if m.intra { | ||
| 150 | + let n = filter_nbr8(&gather_nbr8_in( | ||
| 151 | + &plane_ref(pic, comp).data, | ||
| 152 | + &m.pred_nbr, | ||
| 153 | + blk8, | ||
| 154 | + origin, | ||
| 155 | + stride, | ||
| 156 | + )); | ||
| 157 | + let mut p = [S::default(); 64]; | ||
| 158 | + pred_8x8(m.modes8[blk8], bd, &n, &mut p); | ||
| 159 | + let pl = plane_mut(pic, comp); | ||
| 160 | + for ry in 0..8 { | ||
| 161 | + let row = origin + ry * stride; | ||
| 162 | + pl.data[row..row + 8].copy_from_slice(&p[ry * 8..ry * 8 + 8]); | ||
| 163 | + } | ||
| 164 | + } | ||
| 165 | + if cbp_luma & (1 << blk8) == 0 { | ||
| 166 | + let grid = nz_grid_mut(pic, comp); | ||
| 167 | + for dy in 0..2 { | ||
| 168 | + let base = (by + dy) * w4 + bx; | ||
| 169 | + grid[base..base + 2].fill(0); | ||
| 170 | + } | ||
| 171 | + continue; | ||
| 172 | + } | ||
| 173 | + let tc = if r.is_cabac() { | ||
| 174 | + // ctxBlockCat 5: one 64-coefficient block, and every 4x4 slot | ||
| 175 | + // of it reports the block's own count to the neighbours | ||
| 176 | + // (9.3.3.1.1.9 resolves them to the 8x8 transform block). Only | ||
| 177 | + // ChromaArrayType 3 sends a coded_block_flag for it. | ||
| 178 | + let cbf = cat3.then(|| nz_pair_8x8(pic, comp, nbr, bx, by)); | ||
| 179 | + let tc = r.residual_block_8x8(&mut scan8, c, cbf)?; | ||
| 180 | + let grid = nz_grid_mut(pic, comp); | ||
| 181 | + for dy in 0..2 { | ||
| 182 | + let base = (by + dy) * w4 + bx; | ||
| 183 | + grid[base..base + 2].fill(tc.min(16)); | ||
| 184 | + } | ||
| 185 | + tc | ||
| 186 | + } else { | ||
| 187 | + // CAVLC codes the same 64 coefficients as four interleaved 4x4 | ||
| 188 | + // blocks (7.3.5.3.1), each with its own nC neighbourhood. | ||
| 189 | + let mut tc = 0u8; | ||
| 190 | + for i4 in 0..4 { | ||
| 191 | + let (dx, dy) = BLK4_POS[blk8 * 4 + i4]; | ||
| 192 | + let (sx, sy) = (bx0 + dx as usize, by0 + dy as usize); | ||
| 193 | + let (na, nb) = nz_pair_in(pic, nz_grid(pic, comp), nbr, sx, sy); | ||
| 194 | + let n = r.residual_block_c(&mut scan, BlockCat::Luma4x4, c, na, nb)?; | ||
| 195 | + nz_grid_mut(pic, comp)[sy * w4 + sx] = n; | ||
| 196 | + tc += n; | ||
| 197 | + for i in 0..16 { | ||
| 198 | + scan8[4 * i + i4] = scan[i]; | ||
| 199 | + } | ||
| 200 | + } | ||
| 201 | + tc | ||
| 202 | + }; | ||
| 203 | + if tc == 0 { | ||
| 204 | + continue; | ||
| 205 | + } | ||
| 206 | + if fld { | ||
| 207 | + unfield_8x8(&scan8, &mut raster8); | ||
| 208 | + } else { | ||
| 209 | + unzigzag_8x8(&scan8, &mut raster8); | ||
| 210 | + } | ||
| 211 | + let pl = plane_mut(pic, comp); | ||
| 212 | + if bypass { | ||
| 213 | + if m.intra && m.modes8[blk8] < 2 { | ||
| 214 | + add_residual_dpcm( | ||
| 215 | + &mut pl.data, | ||
| 216 | + bd, | ||
| 217 | + stride, | ||
| 218 | + origin, | ||
| 219 | + 8, | ||
| 220 | + 8, | ||
| 221 | + &raster8, | ||
| 222 | + m.modes8[blk8] == 1, | ||
| 223 | + true, | ||
| 224 | + ); | ||
| 225 | + } else { | ||
| 226 | + add_residual_8x8(&mut pl.data, bd, stride, origin, &raster8); | ||
| 227 | + } | ||
| 228 | + } else { | ||
| 229 | + dequant_8x8(&mut raster8, ls8, qp); | ||
| 230 | + inverse_transform_8x8(&raster8, &mut resid8); | ||
| 231 | + add_residual_8x8(&mut pl.data, bd, stride, origin, &resid8); | ||
| 232 | + } | ||
| 233 | + } | ||
| 234 | + } else { | ||
| 235 | + for blk in 0..16 { | ||
| 236 | + let (dx, dy) = BLK4_POS[blk]; | ||
| 237 | + let (bx, by) = (bx0 + dx as usize, by0 + dy as usize); | ||
| 238 | + let tc = if cbp_luma & (1 << (blk >> 2)) != 0 { | ||
| 239 | + let (na, nb) = nz_pair_in(pic, nz_grid(pic, comp), nbr, bx, by); | ||
| 240 | + r.residual_block_c(&mut scan, BlockCat::Luma4x4, c, na, nb)? | ||
| 241 | + } else { | ||
| 242 | + 0 | ||
| 243 | + }; | ||
| 244 | + nz_grid_mut(pic, comp)[by * w4 + bx] = tc; | ||
| 245 | + let (x, y) = (mb_x * 16 + dx as usize * 4, geo.y(dy as usize * 4)); | ||
| 246 | + let origin = plane_ref(pic, comp).at(x, y); | ||
| 247 | + let stride = plane_ref(pic, comp).stride * mul; | ||
| 248 | + if m.intra { | ||
| 249 | + let n = | ||
| 250 | + gather_nbr4_in(&plane_ref(pic, comp).data, &m.pred_nbr, blk, origin, stride); | ||
| 251 | + let mut p = [S::default(); 16]; | ||
| 252 | + pred_4x4(m.modes[blk], bd, &n, &mut p); | ||
| 253 | + let pl = plane_mut(pic, comp); | ||
| 254 | + if tc == 0 { | ||
| 255 | + // Pure prediction: copy the 16 samples in. | ||
| 256 | + for ry in 0..4 { | ||
| 257 | + let row = origin + ry * stride; | ||
| 258 | + pl.data[row..row + 4].copy_from_slice(&p[ry * 4..ry * 4 + 4]); | ||
| 259 | + } | ||
| 260 | + continue; | ||
| 261 | + } | ||
| 262 | + if fld { | ||
| 263 | + unfield(&scan, &mut raster); | ||
| 264 | + } else { | ||
| 265 | + unzigzag(&scan, &mut raster); | ||
| 266 | + } | ||
| 267 | + if bypass { | ||
| 268 | + for ry in 0..4 { | ||
| 269 | + let row = origin + ry * stride; | ||
| 270 | + pl.data[row..row + 4].copy_from_slice(&p[ry * 4..ry * 4 + 4]); | ||
| 271 | + } | ||
| 272 | + if m.modes[blk] < 2 { | ||
| 273 | + add_residual_dpcm( | ||
| 274 | + &mut pl.data, | ||
| 275 | + bd, | ||
| 276 | + stride, | ||
| 277 | + origin, | ||
| 278 | + 4, | ||
| 279 | + 4, | ||
| 280 | + &raster, | ||
| 281 | + m.modes[blk] == 1, | ||
| 282 | + true, | ||
| 283 | + ); | ||
| 284 | + } else { | ||
| 285 | + add_residual_4x4(&mut pl.data, bd, stride, origin, &raster); | ||
| 286 | + } | ||
| 287 | + continue; | ||
| 288 | + } | ||
| 289 | + dequant_4x4(&mut raster, ls, qp, false); | ||
| 290 | + inverse_transform_4x4(&raster, &mut resid); | ||
| 291 | + for ry in 0..4 { | ||
| 292 | + let row = origin + ry * stride; | ||
| 293 | + for rx in 0..4 { | ||
| 294 | + let v = p[ry * 4 + rx].to_i32() + resid[ry * 4 + rx]; | ||
| 295 | + pl.data[row + rx] = S::clip(v, max); | ||
| 296 | + } | ||
| 297 | + } | ||
| 298 | + } else { | ||
| 299 | + if tc == 0 { | ||
| 300 | + continue; // motion-compensated prediction already in place | ||
| 301 | + } | ||
| 302 | + if fld { | ||
| 303 | + unfield(&scan, &mut raster); | ||
| 304 | + } else { | ||
| 305 | + unzigzag(&scan, &mut raster); | ||
| 306 | + } | ||
| 307 | + let pl = plane_mut(pic, comp); | ||
| 308 | + if bypass { | ||
| 309 | + add_residual_4x4(&mut pl.data, bd, stride, origin, &raster); | ||
| 310 | + } else { | ||
| 311 | + dequant_4x4(&mut raster, ls, qp, false); | ||
| 312 | + inverse_transform_4x4(&raster, &mut resid); | ||
| 313 | + add_residual_4x4(&mut pl.data, bd, stride, origin, &resid); | ||
| 314 | + } | ||
| 315 | + } | ||
| 316 | + } | ||
| 317 | + } | ||
| 318 | + Ok(()) | ||
| 319 | +} | ||