S8: move B direct prediction (predict_direct, colocated, map_col_to_list0) into direct.rs
Browse files at this commit292c4a4d6d2ccfb88ef6cfc91afa37791ee99c44Parent: 493fa903 changed files +417 -408
- crates/ec-h264/src/decoder.rs +31 -408
- crates/ec-h264/src/direct.rs +385 -0
- crates/ec-h264/src/lib.rs +1 -0
crates/ec-h264/src/decoder.rsmodified+31 -408
| @@ -31,17 +31,18 @@ use ec_h264_syntax::{ | |||
| 31 | 31 | ||
| 32 | use crate::deblock::{chroma_qp, deblock_picture}; | 32 | use crate::deblock::{chroma_qp, deblock_picture}; |
| 33 | use crate::dpb::{ | 33 | use crate::dpb::{ |
| 34 | - BLK_DIRECT, BLK_INTRA, BLK_SKIP, BOTTOM, CODED_AFRM, CODED_FLD, Dpb, FRAME, Mark, NO_SLICE, | 34 | + BLK_DIRECT, BLK_SKIP, BOTTOM, CODED_AFRM, Dpb, FRAME, Mark, NO_SLICE, |
| 35 | PicInfo, PicPlane, Picture, RefList, SliceParams, TOP, | 35 | PicInfo, PicPlane, Picture, RefList, SliceParams, TOP, |
| 36 | }; | 36 | }; |
| 37 | use crate::entropy::{ | 37 | use crate::entropy::{ |
| 38 | BlockCat, Entropy, FLAG_CHROMA_PRED, FLAG_DECODED, FLAG_DIRECT, FLAG_I16, FLAG_INTER, FLAG_PCM, | 38 | BlockCat, Entropy, FLAG_CHROMA_PRED, FLAG_DECODED, FLAG_DIRECT, FLAG_I16, FLAG_INTER, FLAG_PCM, |
| 39 | FLAG_SKIP, FLAG_TRANS8X8, MbCtx, MbInfo, MbaffA, | 39 | FLAG_SKIP, FLAG_TRANS8X8, MbCtx, MbInfo, MbaffA, |
| 40 | }; | 40 | }; |
| 41 | +use crate::direct::{predict_direct, predict_direct_8x8}; | ||
| 41 | use crate::inter::Weights; | 42 | use crate::inter::Weights; |
| 42 | use crate::mbaff::{self, MbPos}; | 43 | use crate::mbaff::{self, MbPos}; |
| 43 | use crate::mv::{ | 44 | use crate::mv::{ |
| 44 | - B_SHAPES, B_SUB, MbShape, MvCtx, P_SHAPES, P_SUB, Pred, SubShape, min_positive, neighbour_mvd, | 45 | + B_SHAPES, B_SUB, MbShape, MvCtx, P_SHAPES, P_SUB, Pred, SubShape, neighbour_mvd, |
| 45 | predict_mv, ref_idx_cond, write_block, write_intra_mb, write_mvd, | 46 | predict_mv, ref_idx_cond, write_block, write_intra_mb, write_mvd, |
| 46 | }; | 47 | }; |
| 47 | use crate::pred::{ | 48 | use crate::pred::{ |
| @@ -1582,48 +1583,48 @@ impl Geo { | |||
| 1582 | 1583 | ||
| 1583 | /// Mutable per-slice decode state. | 1584 | /// Mutable per-slice decode state. |
| 1584 | pub(crate) struct SliceCtx { | 1585 | pub(crate) struct SliceCtx { |
| 1585 | - slice_id: u16, | 1586 | + pub(crate) slice_id: u16, |
| 1586 | - slice_type: SliceType, | 1587 | + pub(crate) slice_type: SliceType, |
| 1587 | - qp: i32, | 1588 | + pub(crate) qp: i32, |
| 1588 | - cb_qp_offset: i32, | 1589 | + pub(crate) cb_qp_offset: i32, |
| 1589 | - cr_qp_offset: i32, | 1590 | + pub(crate) cr_qp_offset: i32, |
| 1590 | /// Chroma rows per macroblock: 8 (4:2:0) or 16 (4:2:2). | 1591 | /// Chroma rows per macroblock: 8 (4:2:0) or 16 (4:2:2). |
| 1591 | - cmb_h: usize, | 1592 | + pub(crate) cmb_h: usize, |
| 1592 | /// ChromaArrayType of the picture being decoded. | 1593 | /// ChromaArrayType of the picture being decoded. |
| 1593 | pub(crate) cat: u8, | 1594 | pub(crate) cat: u8, |
| 1594 | - transform_8x8_mode: bool, | 1595 | + pub(crate) transform_8x8_mode: bool, |
| 1595 | - constrained_intra_pred: bool, | 1596 | + pub(crate) constrained_intra_pred: bool, |
| 1596 | /// ctxIdxInc for the next macroblock's first mb_qp_delta bin: set when the | 1597 | /// ctxIdxInc for the next macroblock's first mb_qp_delta bin: set when the |
| 1597 | /// macroblock just decoded coded a non-zero mb_qp_delta (9.3.3.1.1.5). | 1598 | /// macroblock just decoded coded a non-zero mb_qp_delta (9.3.3.1.1.5). |
| 1598 | - qp_delta_inc: u8, | 1599 | + pub(crate) qp_delta_inc: u8, |
| 1599 | /// `RefPicList0` and `RefPicList1` as decoded picture buffer indices. | 1600 | /// `RefPicList0` and `RefPicList1` as decoded picture buffer indices. |
| 1600 | - lists: [RefList; 2], | 1601 | + pub(crate) lists: [RefList; 2], |
| 1601 | - num_ref_idx: [usize; 2], | 1602 | + pub(crate) num_ref_idx: [usize; 2], |
| 1602 | - direct_spatial: bool, | 1603 | + pub(crate) direct_spatial: bool, |
| 1603 | - direct_8x8_inference: bool, | 1604 | + pub(crate) direct_8x8_inference: bool, |
| 1604 | - weighted_pred: bool, | 1605 | + pub(crate) weighted_pred: bool, |
| 1605 | - weighted_bipred_idc: u8, | 1606 | + pub(crate) weighted_bipred_idc: u8, |
| 1606 | - weights: Option<PredWeightTable>, | 1607 | + pub(crate) weights: Option<PredWeightTable>, |
| 1607 | /// `PicOrderCnt( CurrPic )`. | 1608 | /// `PicOrderCnt( CurrPic )`. |
| 1608 | - poc: i32, | 1609 | + pub(crate) poc: i32, |
| 1609 | /// `TopFieldOrderCnt` and `BottomFieldOrderCnt` of the current picture. | 1610 | /// `TopFieldOrderCnt` and `BottomFieldOrderCnt` of the current picture. |
| 1610 | - fpoc: [i32; 2], | 1611 | + pub(crate) fpoc: [i32; 2], |
| 1611 | /// Structure of the picture being decoded: [`FRAME`], [`TOP`] or [`BOTTOM`]. | 1612 | /// Structure of the picture being decoded: [`FRAME`], [`TOP`] or [`BOTTOM`]. |
| 1612 | - structure: u8, | 1613 | + pub(crate) structure: u8, |
| 1613 | /// Parity of the macroblock being decoded when it is a field macroblock | 1614 | /// Parity of the macroblock being decoded when it is a field macroblock |
| 1614 | /// ([`TOP`] or [`BOTTOM`]), [`FRAME`] for a frame macroblock. In a field | 1615 | /// ([`TOP`] or [`BOTTOM`]), [`FRAME`] for a frame macroblock. In a field |
| 1615 | /// picture this is the picture's own parity. | 1616 | /// picture this is the picture's own parity. |
| 1616 | - mb_parity: u8, | 1617 | + pub(crate) mb_parity: u8, |
| 1617 | /// Where the macroblock being decoded sits when the picture is an MBAFF | 1618 | /// Where the macroblock being decoded sits when the picture is an MBAFF |
| 1618 | /// frame (`pos.active`); inert otherwise. | 1619 | /// frame (`pos.active`); inert otherwise. |
| 1619 | - pos: MbPos, | 1620 | + pub(crate) pos: MbPos, |
| 1620 | } | 1621 | } |
| 1621 | 1622 | ||
| 1622 | impl SliceCtx { | 1623 | impl SliceCtx { |
| 1623 | /// `PicOrderCnt` of the current picture, or of the field of the current | 1624 | /// `PicOrderCnt` of the current picture, or of the field of the current |
| 1624 | /// macroblock's parity for a field macroblock of an MBAFF frame (8.4.1.2.3, 8.4.2.3.1). | 1625 | /// macroblock's parity for a field macroblock of an MBAFF frame (8.4.1.2.3, 8.4.2.3.1). |
| 1625 | #[inline] | 1626 | #[inline] |
| 1626 | - fn cur_poc(&self) -> i32 { | 1627 | + pub(crate) fn cur_poc(&self) -> i32 { |
| 1627 | if self.pos.active && self.pos.field { | 1628 | if self.pos.active && self.pos.field { |
| 1628 | self.fpoc[usize::from(self.mb_parity == BOTTOM)] | 1629 | self.fpoc[usize::from(self.mb_parity == BOTTOM)] |
| 1629 | } else { | 1630 | } else { |
| @@ -1631,7 +1632,7 @@ impl SliceCtx { | |||
| 1631 | } | 1632 | } |
| 1632 | } | 1633 | } |
| 1633 | 1634 | ||
| 1634 | - fn is_b(&self) -> bool { | 1635 | + pub(crate) fn is_b(&self) -> bool { |
| 1635 | self.slice_type == SliceType::B | 1636 | self.slice_type == SliceType::B |
| 1636 | } | 1637 | } |
| 1637 | 1638 | ||
| @@ -1663,7 +1664,7 @@ impl SliceCtx { | |||
| 1663 | 1664 | ||
| 1664 | /// [`SliceCtx::reference`] for a field macroblock of an MBAFF frame. | 1665 | /// [`SliceCtx::reference`] for a field macroblock of an MBAFF frame. |
| 1665 | #[inline(never)] | 1666 | #[inline(never)] |
| 1666 | - fn field_mb_reference<'a, S: Sample>( | 1667 | + pub(crate) fn field_mb_reference<'a, S: Sample>( |
| 1667 | &self, | 1668 | &self, |
| 1668 | refs: &'a [Picture<S>], | 1669 | refs: &'a [Picture<S>], |
| 1669 | list: usize, | 1670 | list: usize, |
| @@ -2094,13 +2095,13 @@ fn build_mb_ctx<S: Sample>( | |||
| 2094 | 2095 | ||
| 2095 | /// Motion data parsed for one macroblock, before prediction derivation. | 2096 | /// Motion data parsed for one macroblock, before prediction derivation. |
| 2096 | #[derive(Clone, Copy)] | 2097 | #[derive(Clone, Copy)] |
| 2097 | -struct MbMotion { | 2098 | +pub(crate) struct MbMotion { |
| 2098 | /// Sub-macroblock types of a P_8x8 / B_8x8 macroblock. | 2099 | /// Sub-macroblock types of a P_8x8 / B_8x8 macroblock. |
| 2099 | - sub: [SubShape; 4], | 2100 | + pub(crate) sub: [SubShape; 4], |
| 2100 | /// `ref_idx_lX[ mbPartIdx ]`, -1 when the partition does not use the list. | 2101 | /// `ref_idx_lX[ mbPartIdx ]`, -1 when the partition does not use the list. |
| 2101 | - ref_idx: [[i8; 4]; 2], | 2102 | + pub(crate) ref_idx: [[i8; 4]; 2], |
| 2102 | /// `mvd_lX[ mbPartIdx ][ subMbPartIdx ]`, indexed by 4x4 raster position. | 2103 | /// `mvd_lX[ mbPartIdx ][ subMbPartIdx ]`, indexed by 4x4 raster position. |
| 2103 | - mvd: [[[i16; 2]; 16]; 2], | 2104 | + pub(crate) mvd: [[[i16; 2]; 16]; 2], |
| 2104 | } | 2105 | } |
| 2105 | 2106 | ||
| 2106 | /// Decode one macroblock (spec 7.3.5, 7.4.5) and reconstruct it. `ctx.qp` | 2107 | /// Decode one macroblock (spec 7.3.5, 7.4.5) and reconstruct it. `ctx.qp` |
| @@ -3047,384 +3048,6 @@ fn predict_p_skip<S: Sample>( | |||
| 3047 | ) | 3048 | ) |
| 3048 | } | 3049 | } |
| 3049 | 3050 | ||
| 3050 | -/// B_Skip / B_Direct_16x16 (clause 8.4.1.2) over the whole macroblock. | ||
| 3051 | -fn predict_direct<S: Sample>( | ||
| 3052 | - pic: &mut Picture<S>, | ||
| 3053 | - refs: &[Picture<S>], | ||
| 3054 | - ctx: &SliceCtx, | ||
| 3055 | - mvc: &mut MvCtx, | ||
| 3056 | - skip: bool, | ||
| 3057 | -) -> Result<()> { | ||
| 3058 | - for quad in 0..4 { | ||
| 3059 | - direct_quadrant(pic, refs, ctx, mvc, quad, skip)?; | ||
| 3060 | - } | ||
| 3061 | - Ok(()) | ||
| 3062 | -} | ||
| 3063 | - | ||
| 3064 | -/// B_Direct_8x8 for one 8x8 sub-macroblock. | ||
| 3065 | -fn predict_direct_8x8<S: Sample>( | ||
| 3066 | - pic: &mut Picture<S>, | ||
| 3067 | - refs: &[Picture<S>], | ||
| 3068 | - ctx: &SliceCtx, | ||
| 3069 | - mvc: &mut MvCtx, | ||
| 3070 | - part: usize, | ||
| 3071 | -) -> Result<()> { | ||
| 3072 | - direct_quadrant(pic, refs, ctx, mvc, part, false) | ||
| 3073 | -} | ||
| 3074 | - | ||
| 3075 | -/// One 8x8 quadrant of a direct-mode macroblock. With | ||
| 3076 | -/// `direct_8x8_inference_flag` the whole quadrant shares the motion of its | ||
| 3077 | -/// outer corner 4x4 block; without it each 4x4 block is derived on its own. | ||
| 3078 | -fn direct_quadrant<S: Sample>( | ||
| 3079 | - pic: &mut Picture<S>, | ||
| 3080 | - refs: &[Picture<S>], | ||
| 3081 | - ctx: &SliceCtx, | ||
| 3082 | - mvc: &mut MvCtx, | ||
| 3083 | - quad: usize, | ||
| 3084 | - skip: bool, | ||
| 3085 | -) -> Result<()> { | ||
| 3086 | - let (mb_x, mb_y) = (mvc.mb_x, mvc.mb_y); | ||
| 3087 | - let (qx, qy) = (quad % 2 * 2, quad / 2 * 2); | ||
| 3088 | - let flags = BLK_DIRECT | if skip { BLK_SKIP } else { 0 }; | ||
| 3089 | - if ctx.direct_8x8_inference { | ||
| 3090 | - // 8.4.1.2.1: luma4x4BlkIdx = 5 * mbPartIdx, i.e. the quadrant's own | ||
| 3091 | - // outer corner of the macroblock. | ||
| 3092 | - let corner = (quad % 2 * 3, quad / 2 * 3); | ||
| 3093 | - let (mv, ref_idx, ref_id) = direct_block(pic, refs, ctx, mvc, corner)?; | ||
| 3094 | - for dy in 0..2 { | ||
| 3095 | - for dx in 0..2 { | ||
| 3096 | - write_block(pic, mvc, qx + dx, qy + dy, mv, ref_idx, ref_id, flags); | ||
| 3097 | - } | ||
| 3098 | - } | ||
| 3099 | - S::compensate(pic, refs, ctx, mb_x, mb_y, qx, qy, 2, 2, mv, ref_idx) | ||
| 3100 | - } else { | ||
| 3101 | - // Each 4x4 block derives its own motion, but they usually come out | ||
| 3102 | - // equal: interpolation is position-invariant, so one 8x8 compensation | ||
| 3103 | - // of the shared vector produces the same samples as four 4x4 ones and | ||
| 3104 | - // pays the per-partition setup once instead of four times. | ||
| 3105 | - let mut motion = [([[0i16; 2]; 2], [0i8; 2]); 4]; | ||
| 3106 | - for dy in 0..2 { | ||
| 3107 | - for dx in 0..2 { | ||
| 3108 | - let (px, py) = (qx + dx, qy + dy); | ||
| 3109 | - let (mv, ref_idx, ref_id) = direct_block(pic, refs, ctx, mvc, (px, py))?; | ||
| 3110 | - write_block(pic, mvc, px, py, mv, ref_idx, ref_id, flags); | ||
| 3111 | - motion[dy * 2 + dx] = (mv, ref_idx); | ||
| 3112 | - } | ||
| 3113 | - } | ||
| 3114 | - if motion.iter().all(|m| *m == motion[0]) { | ||
| 3115 | - let (mv, ref_idx) = motion[0]; | ||
| 3116 | - return S::compensate(pic, refs, ctx, mb_x, mb_y, qx, qy, 2, 2, mv, ref_idx); | ||
| 3117 | - } | ||
| 3118 | - for dy in 0..2 { | ||
| 3119 | - for dx in 0..2 { | ||
| 3120 | - let (mv, ref_idx) = motion[dy * 2 + dx]; | ||
| 3121 | - S::compensate( | ||
| 3122 | - pic, | ||
| 3123 | - refs, | ||
| 3124 | - ctx, | ||
| 3125 | - mb_x, | ||
| 3126 | - mb_y, | ||
| 3127 | - qx + dx, | ||
| 3128 | - qy + dy, | ||
| 3129 | - 1, | ||
| 3130 | - 1, | ||
| 3131 | - mv, | ||
| 3132 | - ref_idx, | ||
| 3133 | - )?; | ||
| 3134 | - } | ||
| 3135 | - } | ||
| 3136 | - Ok(()) | ||
| 3137 | - } | ||
| 3138 | -} | ||
| 3139 | - | ||
| 3140 | -/// Motion of one direct-mode 4x4 block: `(mvLX, refIdxLX, reference picture | ||
| 3141 | -/// identity)`, spatial (8.4.1.2.2) or temporal (8.4.1.2.3). | ||
| 3142 | -#[allow(clippy::type_complexity)] | ||
| 3143 | -fn direct_block<S: Sample>( | ||
| 3144 | - pic: &Picture<S>, | ||
| 3145 | - refs: &[Picture<S>], | ||
| 3146 | - ctx: &SliceCtx, | ||
| 3147 | - mvc: &MvCtx, | ||
| 3148 | - at: (usize, usize), | ||
| 3149 | -) -> Result<([[i16; 2]; 2], [i8; 2], [i32; 2])> { | ||
| 3150 | - // The co-located block, in both modes (8.4.1.2.1): its list 0 motion, or | ||
| 3151 | - // its list 1 motion when list 0 is unused. `col_ref_idx` is refIdxCol — | ||
| 3152 | - // an index into the co-located picture's own reference list, not ours. | ||
| 3153 | - let (col_ref_id, col_mv, col_ref_idx, vscale) = colocated(ctx, refs, mvc, at); | ||
| 3154 | - let col = ctx.reference(refs, 1, 0); | ||
| 3155 | - | ||
| 3156 | - if ctx.direct_spatial { | ||
| 3157 | - // Reference indices from the macroblock's own neighbours, once. | ||
| 3158 | - let mut ref_idx = [-1i8; 2]; | ||
| 3159 | - let mut mvp = [[0i16; 2]; 2]; | ||
| 3160 | - for list in 0..2 { | ||
| 3161 | - let n = mvc.neighbours(pic, 0, 0, 4, list); | ||
| 3162 | - ref_idx[list] = min_positive(n[0].ref_idx, min_positive(n[1].ref_idx, n[2].ref_idx)); | ||
| 3163 | - mvp[list] = predict_mv(&n, ref_idx[list], 4, 4, 0); | ||
| 3164 | - } | ||
| 3165 | - let zero_prediction = ref_idx[0] < 0 && ref_idx[1] < 0; | ||
| 3166 | - if zero_prediction { | ||
| 3167 | - ref_idx = [0, 0]; | ||
| 3168 | - } | ||
| 3169 | - // colZeroFlag (8.4.1.2.2): the co-located block sits still on a | ||
| 3170 | - // short-term picture, referencing that picture's own list entry 0. The | ||
| 3171 | - // index is the one belonging to the list 8.4.1.2.1 picked — reading | ||
| 3172 | - // "either list is 0" instead lets a bi-predicted co-located block whose | ||
| 3173 | - // list 1 index happens to be 0 zero a motion vector it must not, which | ||
| 3174 | - // only shows on a B picture used as the co-located one. | ||
| 3175 | - let col_short = col.is_some_and(|c| c.mark == Mark::Short); | ||
| 3176 | - let col_zero = col_short | ||
| 3177 | - && col_ref_id >= 0 | ||
| 3178 | - && col_ref_idx == 0 | ||
| 3179 | - && (-1..=1).contains(&col_mv[0]) | ||
| 3180 | - && (-1..=1).contains(&col_mv[1]); | ||
| 3181 | - let mut mv = [[0i16; 2]; 2]; | ||
| 3182 | - let mut ref_id = [-1i32; 2]; | ||
| 3183 | - for list in 0..2 { | ||
| 3184 | - if ref_idx[list] < 0 { | ||
| 3185 | - continue; | ||
| 3186 | - } | ||
| 3187 | - ref_id[list] = ctx | ||
| 3188 | - .reference(refs, list, ref_idx[list]) | ||
| 3189 | - .map(|p| p.id) | ||
| 3190 | - .unwrap_or(-1); | ||
| 3191 | - if zero_prediction || (ref_idx[list] == 0 && col_zero) { | ||
| 3192 | - mv[list] = [0; 2]; | ||
| 3193 | - } else { | ||
| 3194 | - mv[list] = mvp[list]; | ||
| 3195 | - } | ||
| 3196 | - } | ||
| 3197 | - return Ok((mv, ref_idx, ref_id)); | ||
| 3198 | - } | ||
| 3199 | - | ||
| 3200 | - // Temporal direct (8.4.1.2.3). | ||
| 3201 | - let ref_idx0 = if col_ref_id < 0 { | ||
| 3202 | - 0 | ||
| 3203 | - } else { | ||
| 3204 | - map_col_to_list0(ctx, refs, col_ref_id, vscale) | ||
| 3205 | - }; | ||
| 3206 | - let col_mv = match vscale { | ||
| 3207 | - // 8-195 and 8-196: "/" truncates toward zero. | ||
| 3208 | - COL_FRM_TO_FLD => [col_mv[0], col_mv[1] / 2], | ||
| 3209 | - COL_FLD_TO_FRM => [col_mv[0], col_mv[1].wrapping_mul(2)], | ||
| 3210 | - _ => col_mv, | ||
| 3211 | - }; | ||
| 3212 | - let pic0 = ctx.reference(refs, 0, ref_idx0); | ||
| 3213 | - let pic1 = ctx.reference(refs, 1, 0); | ||
| 3214 | - let ref_id = [ | ||
| 3215 | - pic0.map(|p| p.id).unwrap_or(-1), | ||
| 3216 | - pic1.map(|p| p.id).unwrap_or(-1), | ||
| 3217 | - ]; | ||
| 3218 | - let (Some(p0), Some(p1)) = (pic0, pic1) else { | ||
| 3219 | - return Ok(([[0; 2]; 2], [ref_idx0, 0], ref_id)); | ||
| 3220 | - }; | ||
| 3221 | - let td = (p1.poc - p0.poc).clamp(-128, 127); | ||
| 3222 | - if p0.mark == Mark::Long || td == 0 { | ||
| 3223 | - return Ok(([col_mv, [0; 2]], [ref_idx0, 0], ref_id)); | ||
| 3224 | - } | ||
| 3225 | - let tb = (ctx.cur_poc() - p0.poc).clamp(-128, 127); | ||
| 3226 | - let tx = (16384 + (td / 2).abs()) / td; | ||
| 3227 | - let dist = ((tb * tx + 32) >> 6).clamp(-1024, 1023); | ||
| 3228 | - let mv0 = [ | ||
| 3229 | - ((dist * i32::from(col_mv[0]) + 128) >> 8) as i16, | ||
| 3230 | - ((dist * i32::from(col_mv[1]) + 128) >> 8) as i16, | ||
| 3231 | - ]; | ||
| 3232 | - let mv1 = [mv0[0] - col_mv[0], mv0[1] - col_mv[1]]; | ||
| 3233 | - Ok(([mv0, mv1], [ref_idx0, 0], ref_id)) | ||
| 3234 | -} | ||
| 3235 | - | ||
| 3236 | -/// `MapColToList0` (8.4.1.2.3): the lowest RefPicList0 index that names the | ||
| 3237 | -/// picture the co-located block referenced. | ||
| 3238 | -fn map_col_to_list0<S: Sample>( | ||
| 3239 | - ctx: &SliceCtx, | ||
| 3240 | - refs: &[Picture<S>], | ||
| 3241 | - col_ref_id: i32, | ||
| 3242 | - vscale: u8, | ||
| 3243 | -) -> i8 { | ||
| 3244 | - // Picture ids step by 4: a field id is its frame's id + 1 + parity. | ||
| 3245 | - let field_mb = ctx.pos.active && ctx.pos.field; | ||
| 3246 | - let want = match vscale { | ||
| 3247 | - // The field of the referenced frame with the current picture's (or | ||
| 3248 | - // macroblock's) parity. | ||
| 3249 | - COL_FRM_TO_FLD => col_ref_id + i32::from(ctx.mb_parity), | ||
| 3250 | - // A field macroblock of an MBAFF frame names the field of the frame | ||
| 3251 | - // the co-located field macroblock referenced that has its own parity. | ||
| 3252 | - _ if field_mb && MBAFF_ONE_TO_ONE_SAME_PARITY => { | ||
| 3253 | - (col_ref_id & !3) + i32::from(ctx.mb_parity) | ||
| 3254 | - } | ||
| 3255 | - _ => col_ref_id, | ||
| 3256 | - }; | ||
| 3257 | - let n = if field_mb { | ||
| 3258 | - 2 * ctx.lists[0].len() | ||
| 3259 | - } else { | ||
| 3260 | - ctx.lists[0].len() | ||
| 3261 | - }; | ||
| 3262 | - for i in 0..n { | ||
| 3263 | - let hit = ctx.reference(refs, 0, i as i8).is_some_and(|p| { | ||
| 3264 | - if vscale == COL_FLD_TO_FRM { | ||
| 3265 | - p.id == col_ref_id & !3 | ||
| 3266 | - } else { | ||
| 3267 | - p.id == want | ||
| 3268 | - } | ||
| 3269 | - }); | ||
| 3270 | - if hit { | ||
| 3271 | - return i as i8; | ||
| 3272 | - } | ||
| 3273 | - } | ||
| 3274 | - 0 | ||
| 3275 | -} | ||
| 3276 | - | ||
| 3277 | -/// Whether a field macroblock of an MBAFF frame whose co-located block is a | ||
| 3278 | -/// field macroblock maps the referenced field to the field of the same parity | ||
| 3279 | -/// as itself (true) or to that very field (false). | ||
| 3280 | -const MBAFF_ONE_TO_ONE_SAME_PARITY: bool = false; | ||
| 3281 | - | ||
| 3282 | -/// `vertMvScale` of 8.4.1.2.1. | ||
| 3283 | -const COL_ONE_TO_ONE: u8 = 0; | ||
| 3284 | -const COL_FRM_TO_FLD: u8 = 1; | ||
| 3285 | -const COL_FLD_TO_FRM: u8 = 2; | ||
| 3286 | - | ||
| 3287 | -/// [`colocated`] for a frame macroblock or field macroblock of an MBAFF frame. | ||
| 3288 | -#[inline(never)] | ||
| 3289 | -fn colocated_mbaff<S: Sample>( | ||
| 3290 | - ctx: &SliceCtx, | ||
| 3291 | - refs: &[Picture<S>], | ||
| 3292 | - mvc: &MvCtx, | ||
| 3293 | - at: (usize, usize), | ||
| 3294 | -) -> (i32, [i16; 2], i8, u8) { | ||
| 3295 | - const NONE: (i32, [i16; 2], i8, u8) = (-1, [0; 2], -1, COL_ONE_TO_ONE); | ||
| 3296 | - let Some((k, _)) = ctx.lists[1].get_ref(0) else { | ||
| 3297 | - return NONE; | ||
| 3298 | - }; | ||
| 3299 | - let Some(store) = refs.get(k) else { | ||
| 3300 | - return NONE; | ||
| 3301 | - }; | ||
| 3302 | - let (mb_x, mb_y) = (mvc.mb_x, mvc.mb_y); | ||
| 3303 | - let w = store.mb_w.max(1); | ||
| 3304 | - let (c, bx, by, vs) = { | ||
| 3305 | - let field_mb = ctx.pos.field; | ||
| 3306 | - let bot = mb_y & 1; | ||
| 3307 | - let top = mb_y & !1; | ||
| 3308 | - // The field of a complementary pair, or of an MBAFF frame's field | ||
| 3309 | - // macroblock pair, a frame macroblock takes its co-located block from. | ||
| 3310 | - let by_poc = | ||
| 3311 | - usize::from((store.fpoc[0] - ctx.poc).abs() >= (store.fpoc[1] - ctx.poc).abs()); | ||
| 3312 | - if store.coding == CODED_FLD { | ||
| 3313 | - let p = if field_mb { bot } else { by_poc }; | ||
| 3314 | - let Some(f) = store.fields.get(p) else { | ||
| 3315 | - return NONE; | ||
| 3316 | - }; | ||
| 3317 | - if field_mb { | ||
| 3318 | - (f, mb_x * 4 + at.0, (mb_y / 2) * 4 + at.1, COL_ONE_TO_ONE) | ||
| 3319 | - } else { | ||
| 3320 | - // mbAddrCol5 and yM = 8 * (CurrMbAddr % 2) + 4 * (yCol / 8). | ||
| 3321 | - let by = (mb_y / 2) * 4 + 2 * bot + at.1 / 2; | ||
| 3322 | - (f, mb_x * 4 + at.0, by, COL_FLD_TO_FRM) | ||
| 3323 | - } | ||
| 3324 | - } else { | ||
| 3325 | - let col_field = store.mb_field.get(top * w + mb_x).copied().unwrap_or(0) != 0; | ||
| 3326 | - match (field_mb, col_field) { | ||
| 3327 | - (false, false) => (store, mb_x * 4 + at.0, mb_y * 4 + at.1, COL_ONE_TO_ONE), | ||
| 3328 | - (false, true) => { | ||
| 3329 | - let slot = top + by_poc; | ||
| 3330 | - let by = slot * 4 + 2 * bot + at.1 / 2; | ||
| 3331 | - (store, mb_x * 4 + at.0, by, COL_FLD_TO_FRM) | ||
| 3332 | - } | ||
| 3333 | - (true, false) => { | ||
| 3334 | - let slot = top + at.1 / 2; | ||
| 3335 | - let by = slot * 4 + (2 * at.1) % 4; | ||
| 3336 | - (store, mb_x * 4 + at.0, by, COL_FRM_TO_FLD) | ||
| 3337 | - } | ||
| 3338 | - (true, true) => (store, mb_x * 4 + at.0, mb_y * 4 + at.1, COL_ONE_TO_ONE), | ||
| 3339 | - } | ||
| 3340 | - } | ||
| 3341 | - }; | ||
| 3342 | - let idx = by * c.mb_w.max(1) * 4 + bx; | ||
| 3343 | - if idx < c.ref_id.len() && c.blk[idx] & BLK_INTRA == 0 { | ||
| 3344 | - let list = usize::from(c.ref_id[idx][0] < 0); | ||
| 3345 | - ( | ||
| 3346 | - c.ref_id[idx][list], | ||
| 3347 | - c.mv[idx][list], | ||
| 3348 | - c.ref_idx[idx][list], | ||
| 3349 | - vs, | ||
| 3350 | - ) | ||
| 3351 | - } else { | ||
| 3352 | - (-1, [0; 2], -1, vs) | ||
| 3353 | - } | ||
| 3354 | -} | ||
| 3355 | - | ||
| 3356 | -/// The co-located block of 8.4.1.2.1 for the 4x4 block `at` of the current | ||
| 3357 | -/// macroblock: `(refPicCol identity, mvCol, refIdxCol, vertMvScale)`; the | ||
| 3358 | -/// identity is -1 for an intra block. | ||
| 3359 | -fn colocated<S: Sample>( | ||
| 3360 | - ctx: &SliceCtx, | ||
| 3361 | - refs: &[Picture<S>], | ||
| 3362 | - mvc: &MvCtx, | ||
| 3363 | - at: (usize, usize), | ||
| 3364 | -) -> (i32, [i16; 2], i8, u8) { | ||
| 3365 | - const NONE: (i32, [i16; 2], i8, u8) = (-1, [0; 2], -1, COL_ONE_TO_ONE); | ||
| 3366 | - let Some((k, _)) = ctx.lists[1].get_ref(0) else { | ||
| 3367 | - return NONE; | ||
| 3368 | - }; | ||
| 3369 | - let Some(store) = refs.get(k) else { | ||
| 3370 | - return NONE; | ||
| 3371 | - }; | ||
| 3372 | - if ctx.pos.active { | ||
| 3373 | - return colocated_mbaff(ctx, refs, mvc, at); | ||
| 3374 | - } | ||
| 3375 | - let Some(cur) = ctx.reference(refs, 1, 0) else { | ||
| 3376 | - return NONE; | ||
| 3377 | - }; | ||
| 3378 | - let (mb_x, mb_y) = (mvc.mb_x, mvc.mb_y); | ||
| 3379 | - let w = store.mb_w.max(1); | ||
| 3380 | - // Table 8-6 and Table 8-8. `mb_y` is a slot row for an MBAFF frame (the | ||
| 3381 | - // pair is `mb_y / 2`, the macroblock of the pair `mb_y % 2`). | ||
| 3382 | - let (c, bx, by, vs) = if ctx.structure != FRAME { | ||
| 3383 | - if store.coding == CODED_FLD { | ||
| 3384 | - (cur, mb_x * 4 + at.0, mb_y * 4 + at.1, COL_ONE_TO_ONE) | ||
| 3385 | - } else if store.coding == CODED_AFRM { | ||
| 3386 | - let top = 2 * mb_y; | ||
| 3387 | - if store.mb_field.get(top * w + mb_x).copied().unwrap_or(0) == 0 { | ||
| 3388 | - // mbAddrCol6 = 2 * CurrMbAddr + (yCol / 8), yM = (2 * yCol) % 16. | ||
| 3389 | - let by = (top + at.1 / 2) * 4 + (2 * at.1) % 4; | ||
| 3390 | - (store, mb_x * 4 + at.0, by, COL_FRM_TO_FLD) | ||
| 3391 | - } else { | ||
| 3392 | - // mbAddrCol7 = 2 * CurrMbAddr + bottom_field_flag. | ||
| 3393 | - let slot = top + usize::from(ctx.structure == BOTTOM); | ||
| 3394 | - (store, mb_x * 4 + at.0, slot * 4 + at.1, COL_ONE_TO_ONE) | ||
| 3395 | - } | ||
| 3396 | - } else { | ||
| 3397 | - // mbAddrCol1 and yM = (2 * yCol) % 16. | ||
| 3398 | - let by = (2 * mb_y + at.1 / 2) * 4 + (2 * at.1) % 4; | ||
| 3399 | - (store, mb_x * 4 + at.0, by, COL_FRM_TO_FLD) | ||
| 3400 | - } | ||
| 3401 | - } else if store.coding == CODED_FLD { | ||
| 3402 | - let top = (store.fpoc[0] - ctx.poc).abs(); | ||
| 3403 | - let bottom = (store.fpoc[1] - ctx.poc).abs(); | ||
| 3404 | - let p = usize::from(top >= bottom); | ||
| 3405 | - let Some(f) = store.fields.get(p) else { | ||
| 3406 | - return NONE; | ||
| 3407 | - }; | ||
| 3408 | - // mbAddrCol4 and yM = 8 * ((CurrMbAddr / PicWidthInMbs) % 2) + 4 * (yCol / 8). | ||
| 3409 | - let by = (mb_y / 2) * 4 + 2 * (mb_y % 2) + at.1 / 2; | ||
| 3410 | - (f, mb_x * 4 + at.0, by, COL_FLD_TO_FRM) | ||
| 3411 | - } else { | ||
| 3412 | - (store, mb_x * 4 + at.0, mb_y * 4 + at.1, COL_ONE_TO_ONE) | ||
| 3413 | - }; | ||
| 3414 | - let idx = by * c.mb_w.max(1) * 4 + bx; | ||
| 3415 | - if idx < c.ref_id.len() && c.blk[idx] & BLK_INTRA == 0 { | ||
| 3416 | - let list = usize::from(c.ref_id[idx][0] < 0); | ||
| 3417 | - ( | ||
| 3418 | - c.ref_id[idx][list], | ||
| 3419 | - c.mv[idx][list], | ||
| 3420 | - c.ref_idx[idx][list], | ||
| 3421 | - vs, | ||
| 3422 | - ) | ||
| 3423 | - } else { | ||
| 3424 | - (-1, [0; 2], -1, vs) | ||
| 3425 | - } | ||
| 3426 | -} | ||
| 3427 | - | ||
| 3428 | 3051 | ||
| 3429 | /// Gather the 13 neighbour samples of a luma 4x4 block (spec 8.3.1.2), | 3052 | /// Gather the 13 neighbour samples of a luma 4x4 block (spec 8.3.1.2), |
| 3430 | /// including the top-right substitution rule. | 3053 | /// including the top-right substitution rule. |
crates/ec-h264/src/direct.rsadded+385 -0
| @@ -0,0 +1,385 @@ | |||
| 1 | +use ec_core::error::Result; | ||
| 2 | + | ||
| 3 | +use crate::decoder::SliceCtx; | ||
| 4 | +use crate::dpb::{BLK_DIRECT, BLK_INTRA, BLK_SKIP, BOTTOM, CODED_AFRM, CODED_FLD, FRAME, Mark, Picture}; | ||
| 5 | +use crate::mv::{MvCtx, min_positive, predict_mv, write_block}; | ||
| 6 | +use crate::sample::Sample; | ||
| 7 | + | ||
| 8 | +/// B_Skip / B_Direct_16x16 (clause 8.4.1.2) over the whole macroblock. | ||
| 9 | +pub(crate) fn predict_direct<S: Sample>( | ||
| 10 | + pic: &mut Picture<S>, | ||
| 11 | + refs: &[Picture<S>], | ||
| 12 | + ctx: &SliceCtx, | ||
| 13 | + mvc: &mut MvCtx, | ||
| 14 | + skip: bool, | ||
| 15 | +) -> Result<()> { | ||
| 16 | + for quad in 0..4 { | ||
| 17 | + direct_quadrant(pic, refs, ctx, mvc, quad, skip)?; | ||
| 18 | + } | ||
| 19 | + Ok(()) | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +/// B_Direct_8x8 for one 8x8 sub-macroblock. | ||
| 23 | +pub(crate) fn predict_direct_8x8<S: Sample>( | ||
| 24 | + pic: &mut Picture<S>, | ||
| 25 | + refs: &[Picture<S>], | ||
| 26 | + ctx: &SliceCtx, | ||
| 27 | + mvc: &mut MvCtx, | ||
| 28 | + part: usize, | ||
| 29 | +) -> Result<()> { | ||
| 30 | + direct_quadrant(pic, refs, ctx, mvc, part, false) | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +/// One 8x8 quadrant of a direct-mode macroblock. With | ||
| 34 | +/// `direct_8x8_inference_flag` the whole quadrant shares the motion of its | ||
| 35 | +/// outer corner 4x4 block; without it each 4x4 block is derived on its own. | ||
| 36 | +fn direct_quadrant<S: Sample>( | ||
| 37 | + pic: &mut Picture<S>, | ||
| 38 | + refs: &[Picture<S>], | ||
| 39 | + ctx: &SliceCtx, | ||
| 40 | + mvc: &mut MvCtx, | ||
| 41 | + quad: usize, | ||
| 42 | + skip: bool, | ||
| 43 | +) -> Result<()> { | ||
| 44 | + let (mb_x, mb_y) = (mvc.mb_x, mvc.mb_y); | ||
| 45 | + let (qx, qy) = (quad % 2 * 2, quad / 2 * 2); | ||
| 46 | + let flags = BLK_DIRECT | if skip { BLK_SKIP } else { 0 }; | ||
| 47 | + if ctx.direct_8x8_inference { | ||
| 48 | + // 8.4.1.2.1: luma4x4BlkIdx = 5 * mbPartIdx, i.e. the quadrant's own | ||
| 49 | + // outer corner of the macroblock. | ||
| 50 | + let corner = (quad % 2 * 3, quad / 2 * 3); | ||
| 51 | + let (mv, ref_idx, ref_id) = direct_block(pic, refs, ctx, mvc, corner)?; | ||
| 52 | + for dy in 0..2 { | ||
| 53 | + for dx in 0..2 { | ||
| 54 | + write_block(pic, mvc, qx + dx, qy + dy, mv, ref_idx, ref_id, flags); | ||
| 55 | + } | ||
| 56 | + } | ||
| 57 | + S::compensate(pic, refs, ctx, mb_x, mb_y, qx, qy, 2, 2, mv, ref_idx) | ||
| 58 | + } else { | ||
| 59 | + // Each 4x4 block derives its own motion, but they usually come out | ||
| 60 | + // equal: interpolation is position-invariant, so one 8x8 compensation | ||
| 61 | + // of the shared vector produces the same samples as four 4x4 ones and | ||
| 62 | + // pays the per-partition setup once instead of four times. | ||
| 63 | + let mut motion = [([[0i16; 2]; 2], [0i8; 2]); 4]; | ||
| 64 | + for dy in 0..2 { | ||
| 65 | + for dx in 0..2 { | ||
| 66 | + let (px, py) = (qx + dx, qy + dy); | ||
| 67 | + let (mv, ref_idx, ref_id) = direct_block(pic, refs, ctx, mvc, (px, py))?; | ||
| 68 | + write_block(pic, mvc, px, py, mv, ref_idx, ref_id, flags); | ||
| 69 | + motion[dy * 2 + dx] = (mv, ref_idx); | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | + if motion.iter().all(|m| *m == motion[0]) { | ||
| 73 | + let (mv, ref_idx) = motion[0]; | ||
| 74 | + return S::compensate(pic, refs, ctx, mb_x, mb_y, qx, qy, 2, 2, mv, ref_idx); | ||
| 75 | + } | ||
| 76 | + for dy in 0..2 { | ||
| 77 | + for dx in 0..2 { | ||
| 78 | + let (mv, ref_idx) = motion[dy * 2 + dx]; | ||
| 79 | + S::compensate( | ||
| 80 | + pic, | ||
| 81 | + refs, | ||
| 82 | + ctx, | ||
| 83 | + mb_x, | ||
| 84 | + mb_y, | ||
| 85 | + qx + dx, | ||
| 86 | + qy + dy, | ||
| 87 | + 1, | ||
| 88 | + 1, | ||
| 89 | + mv, | ||
| 90 | + ref_idx, | ||
| 91 | + )?; | ||
| 92 | + } | ||
| 93 | + } | ||
| 94 | + Ok(()) | ||
| 95 | + } | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +/// Motion of one direct-mode 4x4 block: `(mvLX, refIdxLX, reference picture | ||
| 99 | +/// identity)`, spatial (8.4.1.2.2) or temporal (8.4.1.2.3). | ||
| 100 | +#[allow(clippy::type_complexity)] | ||
| 101 | +fn direct_block<S: Sample>( | ||
| 102 | + pic: &Picture<S>, | ||
| 103 | + refs: &[Picture<S>], | ||
| 104 | + ctx: &SliceCtx, | ||
| 105 | + mvc: &MvCtx, | ||
| 106 | + at: (usize, usize), | ||
| 107 | +) -> Result<([[i16; 2]; 2], [i8; 2], [i32; 2])> { | ||
| 108 | + // The co-located block, in both modes (8.4.1.2.1): its list 0 motion, or | ||
| 109 | + // its list 1 motion when list 0 is unused. `col_ref_idx` is refIdxCol — | ||
| 110 | + // an index into the co-located picture's own reference list, not ours. | ||
| 111 | + let (col_ref_id, col_mv, col_ref_idx, vscale) = colocated(ctx, refs, mvc, at); | ||
| 112 | + let col = ctx.reference(refs, 1, 0); | ||
| 113 | + | ||
| 114 | + if ctx.direct_spatial { | ||
| 115 | + // Reference indices from the macroblock's own neighbours, once. | ||
| 116 | + let mut ref_idx = [-1i8; 2]; | ||
| 117 | + let mut mvp = [[0i16; 2]; 2]; | ||
| 118 | + for list in 0..2 { | ||
| 119 | + let n = mvc.neighbours(pic, 0, 0, 4, list); | ||
| 120 | + ref_idx[list] = min_positive(n[0].ref_idx, min_positive(n[1].ref_idx, n[2].ref_idx)); | ||
| 121 | + mvp[list] = predict_mv(&n, ref_idx[list], 4, 4, 0); | ||
| 122 | + } | ||
| 123 | + let zero_prediction = ref_idx[0] < 0 && ref_idx[1] < 0; | ||
| 124 | + if zero_prediction { | ||
| 125 | + ref_idx = [0, 0]; | ||
| 126 | + } | ||
| 127 | + // colZeroFlag (8.4.1.2.2): the co-located block sits still on a | ||
| 128 | + // short-term picture, referencing that picture's own list entry 0. The | ||
| 129 | + // index is the one belonging to the list 8.4.1.2.1 picked — reading | ||
| 130 | + // "either list is 0" instead lets a bi-predicted co-located block whose | ||
| 131 | + // list 1 index happens to be 0 zero a motion vector it must not, which | ||
| 132 | + // only shows on a B picture used as the co-located one. | ||
| 133 | + let col_short = col.is_some_and(|c| c.mark == Mark::Short); | ||
| 134 | + let col_zero = col_short | ||
| 135 | + && col_ref_id >= 0 | ||
| 136 | + && col_ref_idx == 0 | ||
| 137 | + && (-1..=1).contains(&col_mv[0]) | ||
| 138 | + && (-1..=1).contains(&col_mv[1]); | ||
| 139 | + let mut mv = [[0i16; 2]; 2]; | ||
| 140 | + let mut ref_id = [-1i32; 2]; | ||
| 141 | + for list in 0..2 { | ||
| 142 | + if ref_idx[list] < 0 { | ||
| 143 | + continue; | ||
| 144 | + } | ||
| 145 | + ref_id[list] = ctx | ||
| 146 | + .reference(refs, list, ref_idx[list]) | ||
| 147 | + .map(|p| p.id) | ||
| 148 | + .unwrap_or(-1); | ||
| 149 | + if zero_prediction || (ref_idx[list] == 0 && col_zero) { | ||
| 150 | + mv[list] = [0; 2]; | ||
| 151 | + } else { | ||
| 152 | + mv[list] = mvp[list]; | ||
| 153 | + } | ||
| 154 | + } | ||
| 155 | + return Ok((mv, ref_idx, ref_id)); | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + // Temporal direct (8.4.1.2.3). | ||
| 159 | + let ref_idx0 = if col_ref_id < 0 { | ||
| 160 | + 0 | ||
| 161 | + } else { | ||
| 162 | + map_col_to_list0(ctx, refs, col_ref_id, vscale) | ||
| 163 | + }; | ||
| 164 | + let col_mv = match vscale { | ||
| 165 | + // 8-195 and 8-196: "/" truncates toward zero. | ||
| 166 | + COL_FRM_TO_FLD => [col_mv[0], col_mv[1] / 2], | ||
| 167 | + COL_FLD_TO_FRM => [col_mv[0], col_mv[1].wrapping_mul(2)], | ||
| 168 | + _ => col_mv, | ||
| 169 | + }; | ||
| 170 | + let pic0 = ctx.reference(refs, 0, ref_idx0); | ||
| 171 | + let pic1 = ctx.reference(refs, 1, 0); | ||
| 172 | + let ref_id = [ | ||
| 173 | + pic0.map(|p| p.id).unwrap_or(-1), | ||
| 174 | + pic1.map(|p| p.id).unwrap_or(-1), | ||
| 175 | + ]; | ||
| 176 | + let (Some(p0), Some(p1)) = (pic0, pic1) else { | ||
| 177 | + return Ok(([[0; 2]; 2], [ref_idx0, 0], ref_id)); | ||
| 178 | + }; | ||
| 179 | + let td = (p1.poc - p0.poc).clamp(-128, 127); | ||
| 180 | + if p0.mark == Mark::Long || td == 0 { | ||
| 181 | + return Ok(([col_mv, [0; 2]], [ref_idx0, 0], ref_id)); | ||
| 182 | + } | ||
| 183 | + let tb = (ctx.cur_poc() - p0.poc).clamp(-128, 127); | ||
| 184 | + let tx = (16384 + (td / 2).abs()) / td; | ||
| 185 | + let dist = ((tb * tx + 32) >> 6).clamp(-1024, 1023); | ||
| 186 | + let mv0 = [ | ||
| 187 | + ((dist * i32::from(col_mv[0]) + 128) >> 8) as i16, | ||
| 188 | + ((dist * i32::from(col_mv[1]) + 128) >> 8) as i16, | ||
| 189 | + ]; | ||
| 190 | + let mv1 = [mv0[0] - col_mv[0], mv0[1] - col_mv[1]]; | ||
| 191 | + Ok(([mv0, mv1], [ref_idx0, 0], ref_id)) | ||
| 192 | +} | ||
| 193 | + | ||
| 194 | +/// `MapColToList0` (8.4.1.2.3): the lowest RefPicList0 index that names the | ||
| 195 | +/// picture the co-located block referenced. | ||
| 196 | +fn map_col_to_list0<S: Sample>( | ||
| 197 | + ctx: &SliceCtx, | ||
| 198 | + refs: &[Picture<S>], | ||
| 199 | + col_ref_id: i32, | ||
| 200 | + vscale: u8, | ||
| 201 | +) -> i8 { | ||
| 202 | + // Picture ids step by 4: a field id is its frame's id + 1 + parity. | ||
| 203 | + let field_mb = ctx.pos.active && ctx.pos.field; | ||
| 204 | + let want = match vscale { | ||
| 205 | + // The field of the referenced frame with the current picture's (or | ||
| 206 | + // macroblock's) parity. | ||
| 207 | + COL_FRM_TO_FLD => col_ref_id + i32::from(ctx.mb_parity), | ||
| 208 | + // A field macroblock of an MBAFF frame names the field of the frame | ||
| 209 | + // the co-located field macroblock referenced that has its own parity. | ||
| 210 | + _ if field_mb && MBAFF_ONE_TO_ONE_SAME_PARITY => { | ||
| 211 | + (col_ref_id & !3) + i32::from(ctx.mb_parity) | ||
| 212 | + } | ||
| 213 | + _ => col_ref_id, | ||
| 214 | + }; | ||
| 215 | + let n = if field_mb { | ||
| 216 | + 2 * ctx.lists[0].len() | ||
| 217 | + } else { | ||
| 218 | + ctx.lists[0].len() | ||
| 219 | + }; | ||
| 220 | + for i in 0..n { | ||
| 221 | + let hit = ctx.reference(refs, 0, i as i8).is_some_and(|p| { | ||
| 222 | + if vscale == COL_FLD_TO_FRM { | ||
| 223 | + p.id == col_ref_id & !3 | ||
| 224 | + } else { | ||
| 225 | + p.id == want | ||
| 226 | + } | ||
| 227 | + }); | ||
| 228 | + if hit { | ||
| 229 | + return i as i8; | ||
| 230 | + } | ||
| 231 | + } | ||
| 232 | + 0 | ||
| 233 | +} | ||
| 234 | + | ||
| 235 | +/// Whether a field macroblock of an MBAFF frame whose co-located block is a | ||
| 236 | +/// field macroblock maps the referenced field to the field of the same parity | ||
| 237 | +/// as itself (true) or to that very field (false). | ||
| 238 | +const MBAFF_ONE_TO_ONE_SAME_PARITY: bool = false; | ||
| 239 | + | ||
| 240 | +/// `vertMvScale` of 8.4.1.2.1. | ||
| 241 | +const COL_ONE_TO_ONE: u8 = 0; | ||
| 242 | +const COL_FRM_TO_FLD: u8 = 1; | ||
| 243 | +const COL_FLD_TO_FRM: u8 = 2; | ||
| 244 | + | ||
| 245 | +/// [`colocated`] for a frame macroblock or field macroblock of an MBAFF frame. | ||
| 246 | +#[inline(never)] | ||
| 247 | +fn colocated_mbaff<S: Sample>( | ||
| 248 | + ctx: &SliceCtx, | ||
| 249 | + refs: &[Picture<S>], | ||
| 250 | + mvc: &MvCtx, | ||
| 251 | + at: (usize, usize), | ||
| 252 | +) -> (i32, [i16; 2], i8, u8) { | ||
| 253 | + const NONE: (i32, [i16; 2], i8, u8) = (-1, [0; 2], -1, COL_ONE_TO_ONE); | ||
| 254 | + let Some((k, _)) = ctx.lists[1].get_ref(0) else { | ||
| 255 | + return NONE; | ||
| 256 | + }; | ||
| 257 | + let Some(store) = refs.get(k) else { | ||
| 258 | + return NONE; | ||
| 259 | + }; | ||
| 260 | + let (mb_x, mb_y) = (mvc.mb_x, mvc.mb_y); | ||
| 261 | + let w = store.mb_w.max(1); | ||
| 262 | + let (c, bx, by, vs) = { | ||
| 263 | + let field_mb = ctx.pos.field; | ||
| 264 | + let bot = mb_y & 1; | ||
| 265 | + let top = mb_y & !1; | ||
| 266 | + // The field of a complementary pair, or of an MBAFF frame's field | ||
| 267 | + // macroblock pair, a frame macroblock takes its co-located block from. | ||
| 268 | + let by_poc = | ||
| 269 | + usize::from((store.fpoc[0] - ctx.poc).abs() >= (store.fpoc[1] - ctx.poc).abs()); | ||
| 270 | + if store.coding == CODED_FLD { | ||
| 271 | + let p = if field_mb { bot } else { by_poc }; | ||
| 272 | + let Some(f) = store.fields.get(p) else { | ||
| 273 | + return NONE; | ||
| 274 | + }; | ||
| 275 | + if field_mb { | ||
| 276 | + (f, mb_x * 4 + at.0, (mb_y / 2) * 4 + at.1, COL_ONE_TO_ONE) | ||
| 277 | + } else { | ||
| 278 | + // mbAddrCol5 and yM = 8 * (CurrMbAddr % 2) + 4 * (yCol / 8). | ||
| 279 | + let by = (mb_y / 2) * 4 + 2 * bot + at.1 / 2; | ||
| 280 | + (f, mb_x * 4 + at.0, by, COL_FLD_TO_FRM) | ||
| 281 | + } | ||
| 282 | + } else { | ||
| 283 | + let col_field = store.mb_field.get(top * w + mb_x).copied().unwrap_or(0) != 0; | ||
| 284 | + match (field_mb, col_field) { | ||
| 285 | + (false, false) => (store, mb_x * 4 + at.0, mb_y * 4 + at.1, COL_ONE_TO_ONE), | ||
| 286 | + (false, true) => { | ||
| 287 | + let slot = top + by_poc; | ||
| 288 | + let by = slot * 4 + 2 * bot + at.1 / 2; | ||
| 289 | + (store, mb_x * 4 + at.0, by, COL_FLD_TO_FRM) | ||
| 290 | + } | ||
| 291 | + (true, false) => { | ||
| 292 | + let slot = top + at.1 / 2; | ||
| 293 | + let by = slot * 4 + (2 * at.1) % 4; | ||
| 294 | + (store, mb_x * 4 + at.0, by, COL_FRM_TO_FLD) | ||
| 295 | + } | ||
| 296 | + (true, true) => (store, mb_x * 4 + at.0, mb_y * 4 + at.1, COL_ONE_TO_ONE), | ||
| 297 | + } | ||
| 298 | + } | ||
| 299 | + }; | ||
| 300 | + let idx = by * c.mb_w.max(1) * 4 + bx; | ||
| 301 | + if idx < c.ref_id.len() && c.blk[idx] & BLK_INTRA == 0 { | ||
| 302 | + let list = usize::from(c.ref_id[idx][0] < 0); | ||
| 303 | + ( | ||
| 304 | + c.ref_id[idx][list], | ||
| 305 | + c.mv[idx][list], | ||
| 306 | + c.ref_idx[idx][list], | ||
| 307 | + vs, | ||
| 308 | + ) | ||
| 309 | + } else { | ||
| 310 | + (-1, [0; 2], -1, vs) | ||
| 311 | + } | ||
| 312 | +} | ||
| 313 | + | ||
| 314 | +/// The co-located block of 8.4.1.2.1 for the 4x4 block `at` of the current | ||
| 315 | +/// macroblock: `(refPicCol identity, mvCol, refIdxCol, vertMvScale)`; the | ||
| 316 | +/// identity is -1 for an intra block. | ||
| 317 | +fn colocated<S: Sample>( | ||
| 318 | + ctx: &SliceCtx, | ||
| 319 | + refs: &[Picture<S>], | ||
| 320 | + mvc: &MvCtx, | ||
| 321 | + at: (usize, usize), | ||
| 322 | +) -> (i32, [i16; 2], i8, u8) { | ||
| 323 | + const NONE: (i32, [i16; 2], i8, u8) = (-1, [0; 2], -1, COL_ONE_TO_ONE); | ||
| 324 | + let Some((k, _)) = ctx.lists[1].get_ref(0) else { | ||
| 325 | + return NONE; | ||
| 326 | + }; | ||
| 327 | + let Some(store) = refs.get(k) else { | ||
| 328 | + return NONE; | ||
| 329 | + }; | ||
| 330 | + if ctx.pos.active { | ||
| 331 | + return colocated_mbaff(ctx, refs, mvc, at); | ||
| 332 | + } | ||
| 333 | + let Some(cur) = ctx.reference(refs, 1, 0) else { | ||
| 334 | + return NONE; | ||
| 335 | + }; | ||
| 336 | + let (mb_x, mb_y) = (mvc.mb_x, mvc.mb_y); | ||
| 337 | + let w = store.mb_w.max(1); | ||
| 338 | + // Table 8-6 and Table 8-8. `mb_y` is a slot row for an MBAFF frame (the | ||
| 339 | + // pair is `mb_y / 2`, the macroblock of the pair `mb_y % 2`). | ||
| 340 | + let (c, bx, by, vs) = if ctx.structure != FRAME { | ||
| 341 | + if store.coding == CODED_FLD { | ||
| 342 | + (cur, mb_x * 4 + at.0, mb_y * 4 + at.1, COL_ONE_TO_ONE) | ||
| 343 | + } else if store.coding == CODED_AFRM { | ||
| 344 | + let top = 2 * mb_y; | ||
| 345 | + if store.mb_field.get(top * w + mb_x).copied().unwrap_or(0) == 0 { | ||
| 346 | + // mbAddrCol6 = 2 * CurrMbAddr + (yCol / 8), yM = (2 * yCol) % 16. | ||
| 347 | + let by = (top + at.1 / 2) * 4 + (2 * at.1) % 4; | ||
| 348 | + (store, mb_x * 4 + at.0, by, COL_FRM_TO_FLD) | ||
| 349 | + } else { | ||
| 350 | + // mbAddrCol7 = 2 * CurrMbAddr + bottom_field_flag. | ||
| 351 | + let slot = top + usize::from(ctx.structure == BOTTOM); | ||
| 352 | + (store, mb_x * 4 + at.0, slot * 4 + at.1, COL_ONE_TO_ONE) | ||
| 353 | + } | ||
| 354 | + } else { | ||
| 355 | + // mbAddrCol1 and yM = (2 * yCol) % 16. | ||
| 356 | + let by = (2 * mb_y + at.1 / 2) * 4 + (2 * at.1) % 4; | ||
| 357 | + (store, mb_x * 4 + at.0, by, COL_FRM_TO_FLD) | ||
| 358 | + } | ||
| 359 | + } else if store.coding == CODED_FLD { | ||
| 360 | + let top = (store.fpoc[0] - ctx.poc).abs(); | ||
| 361 | + let bottom = (store.fpoc[1] - ctx.poc).abs(); | ||
| 362 | + let p = usize::from(top >= bottom); | ||
| 363 | + let Some(f) = store.fields.get(p) else { | ||
| 364 | + return NONE; | ||
| 365 | + }; | ||
| 366 | + // mbAddrCol4 and yM = 8 * ((CurrMbAddr / PicWidthInMbs) % 2) + 4 * (yCol / 8). | ||
| 367 | + let by = (mb_y / 2) * 4 + 2 * (mb_y % 2) + at.1 / 2; | ||
| 368 | + (f, mb_x * 4 + at.0, by, COL_FLD_TO_FRM) | ||
| 369 | + } else { | ||
| 370 | + (store, mb_x * 4 + at.0, mb_y * 4 + at.1, COL_ONE_TO_ONE) | ||
| 371 | + }; | ||
| 372 | + let idx = by * c.mb_w.max(1) * 4 + bx; | ||
| 373 | + if idx < c.ref_id.len() && c.blk[idx] & BLK_INTRA == 0 { | ||
| 374 | + let list = usize::from(c.ref_id[idx][0] < 0); | ||
| 375 | + ( | ||
| 376 | + c.ref_id[idx][list], | ||
| 377 | + c.mv[idx][list], | ||
| 378 | + c.ref_idx[idx][list], | ||
| 379 | + vs, | ||
| 380 | + ) | ||
| 381 | + } else { | ||
| 382 | + (-1, [0; 2], -1, vs) | ||
| 383 | + } | ||
| 384 | +} | ||
| 385 | + | ||
crates/ec-h264/src/lib.rsmodified+1 -0
| @@ -43,6 +43,7 @@ mod cavlc; | |||
| 43 | mod codec; | 43 | mod codec; |
| 44 | mod deblock; | 44 | mod deblock; |
| 45 | mod deblock_mbaff; | 45 | mod deblock_mbaff; |
| 46 | +mod direct; | ||
| 46 | mod decoder; | 47 | mod decoder; |
| 47 | mod dpb; | 48 | mod dpb; |
| 48 | mod enc; | 49 | mod enc; |