Arm is_motion_mode_switchable and code a SIMPLE motion_mode symbol on every eligible inter block
Browse files at this commitba4398ce4881a117654a6e9fece82267b999ddf4Parent: fbeac19- feat
- arm is_motion_mode_switchable and code a SIMPLE motion_mode symbol on every eligible inter block
3 changed files +208 -9
- crates/ec-av1/src/decode.rs +51 -7
- crates/ec-av1/src/encode.rs +27 -2
- crates/ec-av1/src/tile.rs +130 -0
crates/ec-av1/src/decode.rsmodified+51 -7
| @@ -24179,6 +24179,25 @@ fn overlappable_left( | |||
| 24179 | out | 24179 | out |
| 24180 | } | 24180 | } |
| 24181 | 24181 | ||
| 24182 | +/// libaom `motion_mode_allowed`'s neighbour term: does ANY bordering inter | ||
| 24183 | +/// block sit on this block's above row or left column ("check_num_overlappable | ||
| 24184 | +/// _neighbors", `obmc.h`). The single source of truth for the DECODER's own | ||
| 24185 | +/// `motion_mode_eligible` and for the tile WRITER's (`tile::write_motion_mode`) | ||
| 24186 | +/// -- the two disagreeing by one block writes a `motion_mode` symbol the | ||
| 24187 | +/// decoder never reads (class `symbol-consumption-gap`). | ||
| 24188 | +pub(crate) fn has_overlappable_neighbour( | ||
| 24189 | + grid: &MiGrid, | ||
| 24190 | + mi_row: usize, | ||
| 24191 | + mi_col: usize, | ||
| 24192 | + bw4: usize, | ||
| 24193 | + bh4: usize, | ||
| 24194 | + mi_cols: usize, | ||
| 24195 | + mi_rows: usize, | ||
| 24196 | +) -> bool { | ||
| 24197 | + !overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, 1).is_empty() | ||
| 24198 | + || !overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, 1).is_empty() | ||
| 24199 | +} | ||
| 24200 | + | ||
| 24182 | /// The warp-sample gather's own `has_top_right` probe (libaom `av1_findSamples` | 24201 | /// The warp-sample gather's own `has_top_right` probe (libaom `av1_findSamples` |
| 24183 | /// calls the same `mvref_common.c` function the MV stack does). lane-sb128 r2: | 24202 | /// calls the same `mvref_common.c` function the MV stack does). lane-sb128 r2: |
| 24184 | /// this used to hardcode `SB_MI` (a 64x64 superblock) -- at `sb_size == 128` | 24203 | /// this used to hardcode `SB_MI` (a 64x64 superblock) -- at `sb_size == 128` |
| @@ -27249,9 +27268,9 @@ fn decode_inter_block( | |||
| 27249 | // min(bw, bh) >= 8, false for a 16x4 / 4x16 strip. | 27268 | // min(bw, bh) >= 8, false for a 16x4 / 4x16 strip. |
| 27250 | && write_w.min(write_h) >= 8 | 27269 | && write_w.min(write_h) >= 8 |
| 27251 | && interintra_mode.is_none() | 27270 | && interintra_mode.is_none() |
| 27252 | - && (!overlappable_above(grid, mi_row, mi_col, bw4, mi_cols as usize, 1).is_empty() | 27271 | + && has_overlappable_neighbour( |
| 27253 | - || !overlappable_left(grid, mi_row, mi_col, bh4, mi_rows as usize, 1) | 27272 | + grid, mi_row, mi_col, bw4, bh4, mi_cols as usize, mi_rows as usize, |
| 27254 | - .is_empty()) | 27273 | + ) |
| 27255 | // libaom `motion_mode_allowed`: a GLOBALMV block whose model | 27274 | // libaom `motion_mode_allowed`: a GLOBALMV block whose model |
| 27256 | // is non-IDENTITY-non-TRANSLATION under free (non-integer) | 27275 | // is non-IDENTITY-non-TRANSLATION under free (non-integer) |
| 27257 | // mv reads no motion_mode/obmc symbol at all -- implicit | 27276 | // mv reads no motion_mode/obmc symbol at all -- implicit |
| @@ -31988,8 +32007,9 @@ fn decode_inter_block8( | |||
| 31988 | && !skip_mode | 32007 | && !skip_mode |
| 31989 | && interintra_mode.is_none() | 32008 | && interintra_mode.is_none() |
| 31990 | && !(is_global_mv_block && !force_integer_mv) | 32009 | && !(is_global_mv_block && !force_integer_mv) |
| 31991 | - && (!overlappable_above(grid, mi_row, mi_col, 2, mi_cols as usize, 1).is_empty() | 32010 | + && has_overlappable_neighbour( |
| 31992 | - || !overlappable_left(grid, mi_row, mi_col, 2, mi_rows as usize, 1).is_empty()); | 32011 | + grid, mi_row, mi_col, 2, 2, mi_cols as usize, mi_rows as usize, |
| 32012 | + ); | ||
| 31993 | let mut warp_params: Option<crate::warp::WarpParams> = None; | 32013 | let mut warp_params: Option<crate::warp::WarpParams> = None; |
| 31994 | let mut warped_selected = false; | 32014 | let mut warped_selected = false; |
| 31995 | let mut obmc_selected = false; | 32015 | let mut obmc_selected = false; |
| @@ -33043,13 +33063,19 @@ pub(crate) fn decode_inter_frame_tile( | |||
| 33043 | // starts from what the previous frame stored, not from the defaults. Same | 33063 | // starts from what the previous frame stored, not from the defaults. Same |
| 33044 | // class as `tx_select` above, third instance. | 33064 | // class as `tx_select` above, third instance. |
| 33045 | initial_cdfs: Option<Cdfs>, | 33065 | initial_cdfs: Option<Cdfs>, |
| 33066 | + // lane-av1obmc: this frame header's own `is_motion_mode_switchable` | ||
| 33067 | + // (spec 5.9.2). Hard-coding `false` here desynced every stream this | ||
| 33068 | + // crate's own encoder writes once inter blocks started carrying a | ||
| 33069 | + // `motion_mode` symbol (class: test asserts against a stale header). | ||
| 33070 | + switchable_motion_mode: bool, | ||
| 33046 | fctx: &crate::decode::FrameCtx, | 33071 | fctx: &crate::decode::FrameCtx, |
| 33047 | ) -> Result<Picture> { | 33072 | ) -> Result<Picture> { |
| 33048 | decode_inter_frame_tile_lr( | 33073 | decode_inter_frame_tile_lr( |
| 33049 | data, mi_cols, mi_rows, base_q_idx, frame_width, frame_height, refpix, cdef, | 33074 | data, mi_cols, mi_rows, base_q_idx, frame_width, frame_height, refpix, cdef, |
| 33050 | loop_filter, allow_high_precision_mv, force_integer_mv, interp_fixed, | 33075 | loop_filter, allow_high_precision_mv, force_integer_mv, interp_fixed, |
| 33051 | enable_dual_filter, reference_select, tx_select, | 33076 | enable_dual_filter, reference_select, tx_select, |
| 33052 | - &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, false, fctx, | 33077 | + &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, false, |
| 33078 | + switchable_motion_mode, fctx, | ||
| 33053 | ) | 33079 | ) |
| 33054 | } | 33080 | } |
| 33055 | 33081 | ||
| @@ -33090,6 +33116,11 @@ pub(crate) fn decode_inter_frame_tile_lr( | |||
| 33090 | // `sign_bias` rather than beside the other bools on purpose (the | 33116 | // `sign_bias` rather than beside the other bools on purpose (the |
| 33091 | // `adjacent-same-typed-args` class). | 33117 | // `adjacent-same-typed-args` class). |
| 33092 | allow_screen_content_tools: bool, | 33118 | allow_screen_content_tools: bool, |
| 33119 | + // lane-av1obmc: this frame header's own `is_motion_mode_switchable` | ||
| 33120 | + // (spec 5.9.2). Hard-coding `false` here desynced every stream this | ||
| 33121 | + // crate's own encoder writes once inter blocks started carrying a | ||
| 33122 | + // `motion_mode` symbol (class: test asserts against a stale header). | ||
| 33123 | + switchable_motion_mode: bool, | ||
| 33093 | fctx: &crate::decode::FrameCtx, | 33124 | fctx: &crate::decode::FrameCtx, |
| 33094 | ) -> Result<Picture> { | 33125 | ) -> Result<Picture> { |
| 33095 | let single_tile = TileInfo { | 33126 | let single_tile = TileInfo { |
| @@ -33118,6 +33149,7 @@ pub(crate) fn decode_inter_frame_tile_lr( | |||
| 33118 | initial_cdfs, | 33149 | initial_cdfs, |
| 33119 | sign_bias, | 33150 | sign_bias, |
| 33120 | allow_screen_content_tools, | 33151 | allow_screen_content_tools, |
| 33152 | + switchable_motion_mode, | ||
| 33121 | fctx, | 33153 | fctx, |
| 33122 | ) | 33154 | ) |
| 33123 | } | 33155 | } |
| @@ -33148,6 +33180,11 @@ pub(crate) fn decode_inter_frame_tiles_lr( | |||
| 33148 | // This frame header's own `allow_screen_content_tools` (see the | 33180 | // This frame header's own `allow_screen_content_tools` (see the |
| 33149 | // single-tile wrapper): the palette syntax its intra blocks carry. | 33181 | // single-tile wrapper): the palette syntax its intra blocks carry. |
| 33150 | allow_screen_content_tools: bool, | 33182 | allow_screen_content_tools: bool, |
| 33183 | + // lane-av1obmc: this frame header's own `is_motion_mode_switchable` | ||
| 33184 | + // (spec 5.9.2). Hard-coding `false` here desynced every stream this | ||
| 33185 | + // crate's own encoder writes once inter blocks started carrying a | ||
| 33186 | + // `motion_mode` symbol (class: test asserts against a stale header). | ||
| 33187 | + switchable_motion_mode: bool, | ||
| 33151 | fctx: &crate::decode::FrameCtx, | 33188 | fctx: &crate::decode::FrameCtx, |
| 33152 | ) -> Result<Picture> { | 33189 | ) -> Result<Picture> { |
| 33153 | decode_inter_frame_tile_with_cdfs( | 33190 | decode_inter_frame_tile_with_cdfs( |
| @@ -33182,7 +33219,7 @@ pub(crate) fn decode_inter_frame_tiles_lr( | |||
| 33182 | [0; 2], | 33219 | [0; 2], |
| 33183 | true, | 33220 | true, |
| 33184 | tx_select, | 33221 | tx_select, |
| 33185 | - false, | 33222 | + switchable_motion_mode, |
| 33186 | false, | 33223 | false, |
| 33187 | allow_screen_content_tools, | 33224 | allow_screen_content_tools, |
| 33188 | DeltaParams::default(), | 33225 | DeltaParams::default(), |
| @@ -37314,6 +37351,7 @@ mod tests { | |||
| 37314 | false, | 37351 | false, |
| 37315 | false, | 37352 | false, |
| 37316 | None, | 37353 | None, |
| 37354 | + false, | ||
| 37317 | fctx, | 37355 | fctx, |
| 37318 | ) | 37356 | ) |
| 37319 | .unwrap_err(); | 37357 | .unwrap_err(); |
| @@ -37732,6 +37770,9 @@ mod tests { | |||
| 37732 | // This frame's own `allow_screen_content_tools`, off the | 37770 | // This frame's own `allow_screen_content_tools`, off the |
| 37733 | // `Encoded` the encoder handed back (stale-header class). | 37771 | // `Encoded` the encoder handed back (stale-header class). |
| 37734 | frame.screen, | 37772 | frame.screen, |
| 37773 | + // Same stale-header class: the bit that makes this frame's | ||
| 37774 | + // inter blocks carry a `motion_mode` symbol. | ||
| 37775 | + frame.switchable_motion_mode, | ||
| 37735 | fctx, | 37776 | fctx, |
| 37736 | ) | 37777 | ) |
| 37737 | .unwrap(); | 37778 | .unwrap(); |
| @@ -38063,6 +38104,9 @@ mod tests { | |||
| 38063 | // This frame's own `allow_screen_content_tools`, off the | 38104 | // This frame's own `allow_screen_content_tools`, off the |
| 38064 | // `Encoded` the encoder handed back (stale-header class). | 38105 | // `Encoded` the encoder handed back (stale-header class). |
| 38065 | frame.screen, | 38106 | frame.screen, |
| 38107 | + // Same stale-header class: the bit that makes this frame's | ||
| 38108 | + // inter blocks carry a `motion_mode` symbol. | ||
| 38109 | + frame.switchable_motion_mode, | ||
| 38066 | fctx, | 38110 | fctx, |
| 38067 | ) | 38111 | ) |
| 38068 | .unwrap(); | 38112 | .unwrap(); |
crates/ec-av1/src/encode.rsmodified+27 -2
| @@ -693,6 +693,7 @@ pub(crate) fn crop_encoded(encoded: &Encoded, width: usize, height: usize) -> En | |||
| 693 | mi_rows: encoded.mi_rows, | 693 | mi_rows: encoded.mi_rows, |
| 694 | base_q_idx: encoded.base_q_idx, | 694 | base_q_idx: encoded.base_q_idx, |
| 695 | tx_select: encoded.tx_select, | 695 | tx_select: encoded.tx_select, |
| 696 | + switchable_motion_mode: encoded.switchable_motion_mode, | ||
| 696 | screen: encoded.screen, | 697 | screen: encoded.screen, |
| 697 | start_cdfs: encoded.start_cdfs.clone(), | 698 | start_cdfs: encoded.start_cdfs.clone(), |
| 698 | next_cdfs: encoded.next_cdfs.clone(), | 699 | next_cdfs: encoded.next_cdfs.clone(), |
| @@ -747,6 +748,12 @@ pub struct Encoded { | |||
| 747 | /// a `tx_depth` frame as a `TxMode::Largest` one and desyncs at the first | 748 | /// a `tx_depth` frame as a `TxMode::Largest` one and desyncs at the first |
| 748 | /// block. | 749 | /// block. |
| 749 | pub(crate) tx_select: bool, | 750 | pub(crate) tx_select: bool, |
| 751 | + /// This frame header's `is_motion_mode_switchable` (spec 5.9.2) -- the | ||
| 752 | + /// bit that makes every eligible single-reference inter block carry a | ||
| 753 | + /// `motion_mode` symbol. Same contract as `tx_select` above: a test | ||
| 754 | + /// decoding `tile` directly must pass it along or it desyncs at the | ||
| 755 | + /// first eligible block. | ||
| 756 | + pub(crate) switchable_motion_mode: bool, | ||
| 750 | /// The CDF state this frame's tile writer started from -- the defaults | 757 | /// The CDF state this frame's tile writer started from -- the defaults |
| 751 | /// for a key frame and for the first inter frame, and the previous | 758 | /// for a key frame and for the first inter frame, and the previous |
| 752 | /// frame's stored tables after that. A test that decodes `tile` directly | 759 | /// frame's stored tables after that. A test that decodes `tile` directly |
| @@ -1152,7 +1159,11 @@ pub fn inter_frame_headers_slots( | |||
| 1152 | }, | 1159 | }, |
| 1153 | allow_high_precision_mv: false, | 1160 | allow_high_precision_mv: false, |
| 1154 | interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap, | 1161 | interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap, |
| 1155 | - is_motion_mode_switchable: false, | 1162 | + // lane-av1obmc: every single-reference inter block libaom's |
| 1163 | + // `motion_mode_allowed` accepts carries a `motion_mode` symbol | ||
| 1164 | + // (`crate::tile::write_motion_mode`). `allow_warped_motion` stays | ||
| 1165 | + // false, so the alphabet is the 2-symbol `obmc_cdf`. | ||
| 1166 | + is_motion_mode_switchable: true, | ||
| 1156 | use_ref_frame_mvs: false, | 1167 | use_ref_frame_mvs: false, |
| 1157 | // Forces `get_tx_set` (spec 5.11.48) to the two-symbol | 1168 | // Forces `get_tx_set` (spec 5.11.48) to the two-symbol |
| 1158 | // `TX_SET_INTER_3` for every inter transform below 32x32 -- the only | 1169 | // `TX_SET_INTER_3` for every inter transform below 32x32 -- the only |
| @@ -3855,6 +3866,7 @@ fn code_square_inter( | |||
| 3855 | palette_uv: None, | 3866 | palette_uv: None, |
| 3856 | tx_depth, | 3867 | tx_depth, |
| 3857 | inter: Some(info), | 3868 | inter: Some(info), |
| 3869 | + obmc: false, | ||
| 3858 | }, | 3870 | }, |
| 3859 | cost + dcost, | 3871 | cost + dcost, |
| 3860 | ); | 3872 | ); |
| @@ -3886,6 +3898,7 @@ fn code_square_inter( | |||
| 3886 | palette: None, | 3898 | palette: None, |
| 3887 | palette_uv: None, | 3899 | palette_uv: None, |
| 3888 | tx_depth, | 3900 | tx_depth, |
| 3901 | + obmc: false, | ||
| 3889 | inter: Some(InterInfo { | 3902 | inter: Some(InterInfo { |
| 3890 | ref1: None, | 3903 | ref1: None, |
| 3891 | mv1: (0, 0), | 3904 | mv1: (0, 0), |
| @@ -5047,6 +5060,8 @@ pub(crate) fn encode_key_frame_inner( | |||
| 5047 | mi_rows: header.mi_rows, | 5060 | mi_rows: header.mi_rows, |
| 5048 | base_q_idx, | 5061 | base_q_idx, |
| 5049 | tx_select, | 5062 | tx_select, |
| 5063 | + // A key frame codes no inter block, so no motion_mode symbol. | ||
| 5064 | + switchable_motion_mode: false, | ||
| 5050 | screen, | 5065 | screen, |
| 5051 | next_cdfs: start_cdfs.clone(), | 5066 | next_cdfs: start_cdfs.clone(), |
| 5052 | start_cdfs, | 5067 | start_cdfs, |
| @@ -6261,6 +6276,7 @@ fn search_inter_block( | |||
| 6261 | uv_mode: DC_PRED, | 6276 | uv_mode: DC_PRED, |
| 6262 | skip: best.skip, | 6277 | skip: best.skip, |
| 6263 | inter: best.inter, | 6278 | inter: best.inter, |
| 6279 | + obmc: false, | ||
| 6264 | eight: None, | 6280 | eight: None, |
| 6265 | dv: None, | 6281 | dv: None, |
| 6266 | palette: None, | 6282 | palette: None, |
| @@ -6975,6 +6991,7 @@ pub(crate) fn encode_inter_frame( | |||
| 6975 | .unwrap_or_else(|| crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx))); | 6991 | .unwrap_or_else(|| crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx))); |
| 6976 | let start_cdfs = CdfSnapshot(cdfs); | 6992 | let start_cdfs = CdfSnapshot(cdfs); |
| 6977 | let reference_select_bit = header.reference_select; | 6993 | let reference_select_bit = header.reference_select; |
| 6994 | + let switchable_motion_mode = header.is_motion_mode_switchable; | ||
| 6978 | let order_hint_bits = seq.order_hint_bits; | 6995 | let order_hint_bits = seq.order_hint_bits; |
| 6979 | let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32; | 6996 | let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32; |
| 6980 | let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32; | 6997 | let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32; |
| @@ -6996,6 +7013,7 @@ pub(crate) fn encode_inter_frame( | |||
| 6996 | crate::tile::arm_sign_bias(sign_bias); | 7013 | crate::tile::arm_sign_bias(sign_bias); |
| 6997 | // Thread-locals: every tile job arms its own worker. | 7014 | // Thread-locals: every tile job arms its own worker. |
| 6998 | crate::tile::arm_reference_select(reference_select_bit); | 7015 | crate::tile::arm_reference_select(reference_select_bit); |
| 7016 | + crate::tile::arm_motion_mode(switchable_motion_mode); | ||
| 6999 | crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); | 7017 | crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); |
| 7000 | crate::tile::arm_screen(screen); | 7018 | crate::tile::arm_screen(screen); |
| 7001 | let mut cdfs = start_cdfs.0.clone(); | 7019 | let mut cdfs = start_cdfs.0.clone(); |
| @@ -7122,6 +7140,7 @@ pub(crate) fn encode_inter_frame( | |||
| 7122 | Some(start_cdfs.0.clone()), | 7140 | Some(start_cdfs.0.clone()), |
| 7123 | sign_bias, | 7141 | sign_bias, |
| 7124 | h.allow_screen_content_tools, | 7142 | h.allow_screen_content_tools, |
| 7143 | + switchable_motion_mode, | ||
| 7125 | fctx, | 7144 | fctx, |
| 7126 | ) | 7145 | ) |
| 7127 | }, | 7146 | }, |
| @@ -7154,6 +7173,7 @@ pub(crate) fn encode_inter_frame( | |||
| 7154 | mi_rows: header.mi_rows, | 7173 | mi_rows: header.mi_rows, |
| 7155 | base_q_idx, | 7174 | base_q_idx, |
| 7156 | tx_select, | 7175 | tx_select, |
| 7176 | + switchable_motion_mode, | ||
| 7157 | screen, | 7177 | screen, |
| 7158 | start_cdfs, | 7178 | start_cdfs, |
| 7159 | next_cdfs, | 7179 | next_cdfs, |
| @@ -7924,6 +7944,7 @@ mod tests { | |||
| 7924 | mv: (0, 0), | 7944 | mv: (0, 0), |
| 7925 | ref_mv_idx: 0, | 7945 | ref_mv_idx: 0, |
| 7926 | }), | 7946 | }), |
| 7947 | + obmc: false, | ||
| 7927 | }; | 7948 | }; |
| 7928 | let skipped_block = BlockCoeffs { | 7949 | let skipped_block = BlockCoeffs { |
| 7929 | skip: true, | 7950 | skip: true, |
| @@ -7943,6 +7964,10 @@ mod tests { | |||
| 7943 | Quadrant::Whole(skipped_block.clone()), | 7964 | Quadrant::Whole(skipped_block.clone()), |
| 7944 | Quadrant::Whole(skipped_block), | 7965 | Quadrant::Whole(skipped_block), |
| 7945 | ]; | 7966 | ]; |
| 7967 | + // lane-av1obmc: the header this tile is decoded under carries | ||
| 7968 | + // `is_motion_mode_switchable`, so the writer must be armed with it | ||
| 7969 | + // too or the decoder reads a `motion_mode` symbol nobody wrote. | ||
| 7970 | + crate::tile::arm_motion_mode(inter_header.is_motion_mode_switchable); | ||
| 7946 | let tile = | 7971 | let tile = |
| 7947 | crate::tile::sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks) | 7972 | crate::tile::sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks) |
| 7948 | .unwrap(); | 7973 | .unwrap(); |
| @@ -10548,7 +10573,7 @@ mod tests { | |||
| 10548 | }) | 10573 | }) |
| 10549 | }; | 10574 | }; |
| 10550 | let pins: [(u8, usize, u64); 2] = | 10575 | let pins: [(u8, usize, u64); 2] = |
| 10551 | - [(150, 7106, 0x1da4_9acd_a892_68e3), (60, 25963, 0x94df_8f46_174e_1a5e)]; | 10576 | + [(150, 7111, 0xd375_2e36_13c4_9fc4), (60, 25969, 0x83b2_8522_cec6_38d4)]; |
| 10552 | for (q, bytes, hash) in pins { | 10577 | for (q, bytes, hash) in pins { |
| 10553 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); | 10578 | let encoded = encode_sequence(&source, q, 0.5).unwrap(); |
| 10554 | assert_eq!( | 10579 | assert_eq!( |
crates/ec-av1/src/tile.rsmodified+130 -0
| @@ -381,6 +381,99 @@ pub(crate) fn arm_reference_select(on: bool) { | |||
| 381 | REFERENCE_SELECT.with(|c| c.set(on)); | 381 | REFERENCE_SELECT.with(|c| c.set(on)); |
| 382 | } | 382 | } |
| 383 | 383 | ||
| 384 | +thread_local! { | ||
| 385 | + /// This frame header's own `is_motion_mode_switchable` bit (spec 5.9.2), | ||
| 386 | + /// armed exactly like [`REFERENCE_SELECT`]. `true` makes every | ||
| 387 | + /// single-reference inter block that libaom's `motion_mode_allowed` | ||
| 388 | + /// accepts carry a `motion_mode`/`obmc` symbol right after its MV syntax, | ||
| 389 | + /// exactly where decode.rs' `read_motion_mode` reads one. | ||
| 390 | + static MOTION_MODE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; | ||
| 391 | +} | ||
| 392 | + | ||
| 393 | +/// Arms the frame's `is_motion_mode_switchable` bit for the next tile written | ||
| 394 | +/// on this thread; cleared by [`CdefIdxGuard`] when that writer returns. | ||
| 395 | +pub(crate) fn arm_motion_mode(on: bool) { | ||
| 396 | + MOTION_MODE.with(|c| c.set(on)); | ||
| 397 | +} | ||
| 398 | + | ||
| 399 | +/// How many single-reference inter blocks this writer coded a `motion_mode` | ||
| 400 | +/// symbol for, split by the value it coded and by the block's own footprint | ||
| 401 | +/// -- the histogram a gate prints so that "OBMC is on" is a measurement, not | ||
| 402 | +/// a claim (class `gate-blind-to-feature`). Index: `0 + 2 * bucket` for | ||
| 403 | +/// SIMPLE, `1 + 2 * bucket` for OBMC, bucket 0/1/2 = 32x32/16x16/8x8. | ||
| 404 | +static MOTION_MODE_HITS: [std::sync::atomic::AtomicUsize; 6] = | ||
| 405 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 6]; | ||
| 406 | + | ||
| 407 | +/// Reads [`MOTION_MODE_HITS`] and zeroes it, so a gate can attribute the | ||
| 408 | +/// counts to its own encode. | ||
| 409 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | ||
| 410 | +pub(crate) fn take_motion_mode_hits() -> [usize; 6] { | ||
| 411 | + std::array::from_fn(|i| MOTION_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 412 | +} | ||
| 413 | + | ||
| 414 | +/// `read_motion_mode`'s write side (spec 5.11.24), called on a | ||
| 415 | +/// single-reference inter block right after its MV syntax and before the | ||
| 416 | +/// (never switchable here) interpolation filter -- libaom's own sequential | ||
| 417 | +/// order in `read_inter_block_mode_info`. | ||
| 418 | +/// | ||
| 419 | +/// Eligibility is decode.rs' own `motion_mode_eligible`, term for term, with | ||
| 420 | +/// the terms this writer settles statically dropped: no block it codes is | ||
| 421 | +/// `skip_mode`, interintra or a non-IDENTITY-global `GLOBALMV` one, so what | ||
| 422 | +/// is left is the footprint floor and the overlappable-neighbour walk | ||
| 423 | +/// ([`crate::decode::has_overlappable_neighbour`], shared with the decoder so | ||
| 424 | +/// the two cannot drift). The frame header keeps `allow_warped_motion == 0`, | ||
| 425 | +/// so the alphabet is always the 2-symbol `obmc_cdf`, never the 3-symbol | ||
| 426 | +/// `motion_mode_cdf`. | ||
| 427 | +fn write_motion_mode( | ||
| 428 | + enc: &mut SymbolEncoder, | ||
| 429 | + cdfs: &mut Cdfs, | ||
| 430 | + grid: &MiGrid, | ||
| 431 | + (mi_row, mi_col): (usize, usize), | ||
| 432 | + (bw4, bh4): (usize, usize), | ||
| 433 | + (write_w, write_h): (usize, usize), | ||
| 434 | + (mi_cols, mi_rows): (usize, usize), | ||
| 435 | + obmc: bool, | ||
| 436 | +) -> Result<()> { | ||
| 437 | + let eligible = MOTION_MODE.with(std::cell::Cell::get) | ||
| 438 | + && write_w.min(write_h) >= 8 | ||
| 439 | + && crate::decode::has_overlappable_neighbour( | ||
| 440 | + grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows, | ||
| 441 | + ); | ||
| 442 | + if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { | ||
| 443 | + eprintln!( | ||
| 444 | + "EC_MMW mi_row={mi_row} mi_col={mi_col} w={write_w} h={write_h} elig={}", | ||
| 445 | + eligible as u8 | ||
| 446 | + ); | ||
| 447 | + } | ||
| 448 | + if !eligible { | ||
| 449 | + // The encoder trialled an OBMC prediction for a block this writer | ||
| 450 | + // codes as SIMPLE_TRANSLATION: its reconstruction is not the | ||
| 451 | + // decoder's (class encoder-grid-drift), which is exactly what the | ||
| 452 | + // `EC_COMP_MISMATCH` rung exists to catch. | ||
| 453 | + if obmc && crate::envflags::env_flag!("EC_COMP_MISMATCH") { | ||
| 454 | + eprintln!( | ||
| 455 | + "EC_COMP_MISMATCH motion_mode enc=OBMC wrote=SIMPLE mi=({mi_row},{mi_col}) wh=({write_w}x{write_h})" | ||
| 456 | + ); | ||
| 457 | + } | ||
| 458 | + return Ok(()); | ||
| 459 | + } | ||
| 460 | + let row = crate::decode::motion_mode_cdf_row(write_w, write_h).ok_or_else(|| { | ||
| 461 | + Error::unsupported( | ||
| 462 | + "AV1 tile", | ||
| 463 | + format!("a {write_w}x{write_h} block has no motion_mode CDF row"), | ||
| 464 | + ) | ||
| 465 | + })?; | ||
| 466 | + enc.symbol(usize::from(obmc), &mut cdfs.obmc[row]); | ||
| 467 | + let bucket = match write_w.min(write_h) { | ||
| 468 | + 32.. => 0, | ||
| 469 | + 16 => 1, | ||
| 470 | + _ => 2, | ||
| 471 | + }; | ||
| 472 | + MOTION_MODE_HITS[usize::from(obmc) + 2 * bucket] | ||
| 473 | + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 474 | + Ok(()) | ||
| 475 | +} | ||
| 476 | + | ||
| 384 | /// The compound half of a block's end-of-block neighbour record: nothing for | 477 | /// The compound half of a block's end-of-block neighbour record: nothing for |
| 385 | /// a single-reference or intra block, and `record_compound_mi`'s overwrite of | 478 | /// a single-reference or intra block, and `record_compound_mi`'s overwrite of |
| 386 | /// the ALTREF default for a compound one (this writer only codes | 479 | /// the ALTREF default for a compound one (this writer only codes |
| @@ -759,6 +852,7 @@ impl Drop for CdefIdxGuard { | |||
| 759 | SIGN_BIAS.with(|c| c.set(crate::mvstack::NO_SIGN_BIAS)); | 852 | SIGN_BIAS.with(|c| c.set(crate::mvstack::NO_SIGN_BIAS)); |
| 760 | SCREEN.with(|c| c.set(false)); | 853 | SCREEN.with(|c| c.set(false)); |
| 761 | REFERENCE_SELECT.with(|c| c.set(false)); | 854 | REFERENCE_SELECT.with(|c| c.set(false)); |
| 855 | + MOTION_MODE.with(|c| c.set(false)); | ||
| 762 | INTRABC.with(|c| c.set(false)); | 856 | INTRABC.with(|c| c.set(false)); |
| 763 | INTRABC_GRID.with(|c| *c.borrow_mut() = None); | 857 | INTRABC_GRID.with(|c| *c.borrow_mut() = None); |
| 764 | ORDER_HINTS.with(|c| c.set((7, 0, [0; 7]))); | 858 | ORDER_HINTS.with(|c| c.set((7, 0, [0; 7]))); |
| @@ -1240,6 +1334,12 @@ pub struct BlockCoeffs { | |||
| 1240 | /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key | 1334 | /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key |
| 1241 | /// frame writers never read this field. | 1335 | /// frame writers never read this field. |
| 1242 | pub inter: Option<InterInfo>, | 1336 | pub inter: Option<InterInfo>, |
| 1337 | + /// Whether this (single-reference inter) block's prediction was built | ||
| 1338 | + /// with OBMC -- spec `motion_mode == OBMC_CAUSAL`, written by | ||
| 1339 | + /// [`write_motion_mode`]. `false` (the [`Default`]) is | ||
| 1340 | + /// `SIMPLE_TRANSLATION`, the only motion mode this writer coded before | ||
| 1341 | + /// lane-av1obmc. | ||
| 1342 | + pub obmc: bool, | ||
| 1243 | /// The 8x8 leaves a straddling 16x16 block is split into (lane-av1-rect), | 1343 | /// The 8x8 leaves a straddling 16x16 block is split into (lane-av1-rect), |
| 1244 | /// each with its own 8x8 luma transform and 4x4 chroma transforms, in | 1344 | /// each with its own 8x8 luma transform and 4x4 chroma transforms, in |
| 1245 | /// raster order among the leaves that are inside the true frame. `Some` | 1345 | /// raster order among the leaves that are inside the true frame. `Some` |
| @@ -5439,6 +5539,16 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5439 | ); | 5539 | ); |
| 5440 | } | 5540 | } |
| 5441 | } | 5541 | } |
| 5542 | + write_motion_mode( | ||
| 5543 | + &mut enc, | ||
| 5544 | + &mut cdfs, | ||
| 5545 | + &grid, | ||
| 5546 | + (mi_row, mi_col), | ||
| 5547 | + (8, 8), | ||
| 5548 | + (BLOCK, BLOCK), | ||
| 5549 | + (mi_cols as usize, mi_rows as usize), | ||
| 5550 | + block.obmc, | ||
| 5551 | + )?; | ||
| 5442 | mode_for_tx = 0; | 5552 | mode_for_tx = 0; |
| 5443 | } | 5553 | } |
| 5444 | } else { | 5554 | } else { |
| @@ -5663,6 +5773,16 @@ fn write_inter_frame_leaf( | |||
| 5663 | ); | 5773 | ); |
| 5664 | } | 5774 | } |
| 5665 | } | 5775 | } |
| 5776 | + write_motion_mode( | ||
| 5777 | + enc, | ||
| 5778 | + cdfs, | ||
| 5779 | + grid, | ||
| 5780 | + (mi_row, mi_col), | ||
| 5781 | + (SUB_MI as usize, SUB_MI as usize), | ||
| 5782 | + (SUB, SUB), | ||
| 5783 | + (mi_cols as usize, mi_rows as usize), | ||
| 5784 | + block.obmc, | ||
| 5785 | + )?; | ||
| 5666 | mode_for_tx = 0; | 5786 | mode_for_tx = 0; |
| 5667 | } | 5787 | } |
| 5668 | } else { | 5788 | } else { |
| @@ -5893,6 +6013,16 @@ fn write_inter_frame_leaf8( | |||
| 5893 | ); | 6013 | ); |
| 5894 | } | 6014 | } |
| 5895 | } | 6015 | } |
| 6016 | + write_motion_mode( | ||
| 6017 | + enc, | ||
| 6018 | + cdfs, | ||
| 6019 | + grid, | ||
| 6020 | + (mi_row, mi_col), | ||
| 6021 | + (2, 2), | ||
| 6022 | + (8, 8), | ||
| 6023 | + (mi_cols as usize, mi_rows as usize), | ||
| 6024 | + block.obmc, | ||
| 6025 | + )?; | ||
| 5896 | mode_for_tx = 0; | 6026 | mode_for_tx = 0; |
| 5897 | } | 6027 | } |
| 5898 | } else { | 6028 | } else { |