wip(av1): gather 32x32-level partial partitions; refusal test names the true-frame messages
Browse files at this commitec3ad48279b102745734eeb2b1b1f264d879beabParent: 6b1cdcc2 changed files +201 -14
- crates/ec-av1/src/encode.rs +68 -8
- crates/ec-av1/src/tile.rs +133 -6
crates/ec-av1/src/encode.rsmodified+68 -8
| @@ -1143,6 +1143,54 @@ pub(crate) fn encode_key_frame_inner( | ||
| 1143 | 1143 | let (c0, r0) = (col * 2, row * 2); |
| 1144 | 1144 | let base = snapshot(&luma, &chroma, (x, y)); |
| 1145 | 1145 | |
| 1146 | + // spec `decode_partition`'s hasRows/hasCols recomputed at this | |
| 1147 | + // 32x32 block's own half (see `crate::tile::has_half`): the | |
| 1148 | + // true frame edge can fall inside a quadrant a superblock-level | |
| 1149 | + // check already let through. A quadrant that fails either may | |
| 1150 | + // not be left whole; a 16x16 sub-block that fails either (once | |
| 1151 | + // split) is a leaf this writer has no rectangular transform | |
| 1152 | + // for, so the search must not pick a split that would need one. | |
| 1153 | + let (has_cols32, has_rows32) = ( | |
| 1154 | + crate::tile::has_half( | |
| 1155 | + col as u32 * crate::tile::BLOCK_MI, | |
| 1156 | + crate::tile::BLOCK_MI, | |
| 1157 | + header.mi_cols, | |
| 1158 | + ), | |
| 1159 | + crate::tile::has_half( | |
| 1160 | + row as u32 * crate::tile::BLOCK_MI, | |
| 1161 | + crate::tile::BLOCK_MI, | |
| 1162 | + header.mi_rows, | |
| 1163 | + ), | |
| 1164 | + ); | |
| 1165 | + let whole_legal = has_cols32 && has_rows32; | |
| 1166 | + let split_legal = (0..4) | |
| 1167 | + .map(|i| (r0 + i / 2, c0 + i % 2)) | |
| 1168 | + .filter(|&(sr, sc)| { | |
| 1169 | + (sr as u32) * crate::tile::SUB_MI < header.mi_rows | |
| 1170 | + && (sc as u32) * crate::tile::SUB_MI < header.mi_cols | |
| 1171 | + }) | |
| 1172 | + .all(|(sr, sc)| { | |
| 1173 | + crate::tile::has_half( | |
| 1174 | + sc as u32 * crate::tile::SUB_MI, | |
| 1175 | + crate::tile::SUB_MI, | |
| 1176 | + header.mi_cols, | |
| 1177 | + ) && crate::tile::has_half( | |
| 1178 | + sr as u32 * crate::tile::SUB_MI, | |
| 1179 | + crate::tile::SUB_MI, | |
| 1180 | + header.mi_rows, | |
| 1181 | + ) | |
| 1182 | + }); | |
| 1183 | + if !whole_legal && !split_legal { | |
| 1184 | + return Err(Error::unsupported( | |
| 1185 | + "AV1 encode", | |
| 1186 | + format!( | |
| 1187 | + "the 32x32 block at ({col},{row}) straddles the true frame edge \ | |
| 1188 | + in a way that needs a rectangular (HORZ/VERT) transform this \ | |
| 1189 | + encoder does not code yet" | |
| 1190 | + ), | |
| 1191 | + )); | |
| 1192 | + } | |
| 1193 | + | |
| 1146 | 1194 | // What the whole 32x32 costs, including the partition symbol |
| 1147 | 1195 | // that says it is not split. |
| 1148 | 1196 | let (whole, mut cost_whole) = code_square( |
| @@ -1180,7 +1228,7 @@ pub(crate) fn encode_key_frame_inner( | ||
| 1180 | 1228 | split.push(block); |
| 1181 | 1229 | } |
| 1182 | 1230 | |
| 1183 | - if split_blocks && cost_split < cost_whole { | |
| 1231 | + if split_legal && (!whole_legal || (split_blocks && cost_split < cost_whole)) { | |
| 1184 | 1232 | modes.extend_from_slice(&split_modes); |
| 1185 | 1233 | blocks.push(Quadrant::Split(split)); |
| 1186 | 1234 | } else { |
| @@ -2915,15 +2963,27 @@ mod tests { | ||
| 2915 | 2963 | } |
| 2916 | 2964 | |
| 2917 | 2965 | /// The sizes the encoder refuses, refused for a reason rather than by |
| 2918 | - /// panicking somewhere below. 40x32 and 32x48 are off the 32x32 block | |
| 2919 | - /// grid, which used to be refused outright; the encoder now pads a | |
| 2920 | - /// picture like that internally (see `a_frame_round_trips_at_its_own_size`) | |
| 2921 | - /// rather than refusing it, so those two are an accept, not a refusal, | |
| 2922 | - /// here. | |
| 2966 | + /// panicking somewhere below. Most sizes off the 32x32 block grid encode | |
| 2967 | + /// fine now (see `a_frame_round_trips_at_its_own_size`) -- the true frame | |
| 2968 | + /// edge lands past the halfway point of whichever block it falls in, so | |
| 2969 | + /// `PARTITION_NONE` or a single gathered split flag still says everything | |
| 2970 | + /// the spec needs. 40x32 and 32x48 are the sizes where the edge instead | |
| 2971 | + /// falls *at or before* the halfway point of a 16x16 leaf: the spec's | |
| 2972 | + /// `decode_partition` then requires that leaf to split again, to 8x8, | |
| 2973 | + /// which this writer has no transform/coefficient path for yet, so it is | |
| 2974 | + /// still refused, by name rather than by corrupting the stream. | |
| 2923 | 2975 | #[test] |
| 2924 | 2976 | fn a_picture_off_the_block_grid_is_refused() { |
| 2925 | - assert!(encode_key_frame(&Picture::grey(40, 32), 100, 0.5).is_ok()); | |
| 2926 | - assert!(encode_key_frame(&Picture::grey(32, 48), 100, 0.5).is_ok()); | |
| 2977 | + let refused = |w, h| { | |
| 2978 | + let msg = encode_key_frame(&Picture::grey(w, h), 100, 0.5) | |
| 2979 | + .expect_err(&format!( | |
| 2980 | + "{w}x{h} needs an 8x8 split this writer does not code" | |
| 2981 | + )) | |
| 2982 | + .to_string(); | |
| 2983 | + msg.contains("true frame") | |
| 2984 | + }; | |
| 2985 | + assert!(refused(40, 32)); | |
| 2986 | + assert!(refused(32, 48)); | |
| 2927 | 2987 | let mut short = Picture::grey(64, 64); |
| 2928 | 2988 | short.u.truncate(10); |
| 2929 | 2989 | assert!(encode_key_frame(&short, 100, 0.5).is_err()); |
crates/ec-av1/src/tile.rsmodified+133 -6
| @@ -34,7 +34,22 @@ const VERT_ALIKE: [usize; 6] = [2, PARTITION_SPLIT, 4, 6, 7, 9]; | ||
| 34 | 34 | const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8]; |
| 35 | 35 | |
| 36 | 36 | /// Mode-info units across a 32x32 block. |
| 37 | -const BLOCK_MI: u32 = SB_MI / 2; | |
| 37 | +pub(crate) const BLOCK_MI: u32 = SB_MI / 2; | |
| 38 | + | |
| 39 | +/// Mode-info units across a 16x16 block, the smallest this crate's key-frame | |
| 40 | +/// writer codes. | |
| 41 | +pub(crate) const SUB_MI: u32 = BLOCK_MI / 2; | |
| 42 | + | |
| 43 | +/// spec `decode_partition`'s `hasRows`/`hasCols` (5.11.4), recomputed at | |
| 44 | +/// whichever block size is asking: a block of `side_mi` mode-info units at mi | |
| 45 | +/// position `pos` may be coded unsplit (its own `PARTITION_NONE`) only when | |
| 46 | +/// this is true in both directions -- the frame's *true* size can put the | |
| 47 | +/// boundary inside a superblock, a 32x32 quadrant or (since this writer's | |
| 48 | +/// smallest block is 16x16) even a leaf, and each level must ask again with | |
| 49 | +/// its own half, not just once at the superblock. | |
| 50 | +pub(crate) fn has_half(pos: u32, side_mi: u32, bound: u32) -> bool { | |
| 51 | + pos + side_mi / 2 < bound | |
| 52 | +} | |
| 38 | 53 | |
| 39 | 54 | /// The number of 32x32 block columns/rows a frame whose *true* (unpadded) |
| 40 | 55 | /// size gives `mi_cols`/`mi_rows` is coded over: every block whose mi origin |
| @@ -808,8 +823,25 @@ pub fn sb_coeff_key_frame_tile( | ||
| 808 | 823 | for (quadrant, (r, c)) in quadrants.iter().zip(quadrant_positions) { |
| 809 | 824 | let at = (r * 2, c * 2); |
| 810 | 825 | let ctx = neighbours.partition_ctx(at, BLOCK); |
| 826 | + // Recomputed at this 32x32 block's own half (spec | |
| 827 | + // `decode_partition`, called again at every size, not | |
| 828 | + // just once for the superblock): the true frame edge | |
| 829 | + // can fall inside this quadrant even when the | |
| 830 | + // superblock it sits in was itself whole or safely | |
| 831 | + // split above. | |
| 832 | + let (has_cols32, has_rows32) = ( | |
| 833 | + has_half(c as u32 * BLOCK_MI, BLOCK_MI, mi_cols), | |
| 834 | + has_half(r as u32 * BLOCK_MI, BLOCK_MI, mi_rows), | |
| 835 | + ); | |
| 811 | 836 | match quadrant { |
| 812 | 837 | Quadrant::Whole(block) => { |
| 838 | + if !has_cols32 || !has_rows32 { | |
| 839 | + return Err(Error::unsupported( | |
| 840 | + "AV1 tile", | |
| 841 | + "a 32x32 block that is half outside the true frame \ | |
| 842 | + cannot be left whole", | |
| 843 | + )); | |
| 844 | + } | |
| 813 | 845 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[ctx]); |
| 814 | 846 | let grids = [ |
| 815 | 847 | level_grid(&block.luma, TX32)?, |
| @@ -830,15 +862,72 @@ pub fn sb_coeff_key_frame_tile( | ||
| 830 | 862 | ); |
| 831 | 863 | } |
| 832 | 864 | Quadrant::Split(blocks) => { |
| 833 | - enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w32[ctx]); | |
| 834 | - if blocks.len() != 4 { | |
| 865 | + // The 16x16 sub-blocks this quadrant's split | |
| 866 | + // carries: only those whose own mi origin is | |
| 867 | + // inside the true frame (spec `decode_partition`'s | |
| 868 | + // `r >= MiRows || c >= MiCols` early return), | |
| 869 | + // which need not be all four when the true | |
| 870 | + // edge falls inside this quadrant. | |
| 871 | + let sub_positions: Vec<(usize, usize)> = (0..4) | |
| 872 | + .map(|i| (r * 2 + i / 2, c * 2 + i % 2)) | |
| 873 | + .filter(|&(sr, sc)| { | |
| 874 | + (sr as u32) * SUB_MI < mi_rows | |
| 875 | + && (sc as u32) * SUB_MI < mi_cols | |
| 876 | + }) | |
| 877 | + .collect(); | |
| 878 | + if blocks.len() != sub_positions.len() { | |
| 835 | 879 | return Err(Error::unsupported( |
| 836 | 880 | "AV1 tile", |
| 837 | - "a 32x32 block that is split carries four 16x16 blocks", | |
| 881 | + "a split 32x32 block needs one 16x16 entry per \ | |
| 882 | + sub-block inside the true frame", | |
| 838 | 883 | )); |
| 839 | 884 | } |
| 840 | - for (i, block) in blocks.iter().enumerate() { | |
| 841 | - let at = (at.0 + i / 2, at.1 + i % 2); | |
| 885 | + // Same three-way spec signaling as the | |
| 886 | + // superblock level above, recomputed at this | |
| 887 | + // block's own half: a full alphabet symbol | |
| 888 | + // only when both halves are inside, a single | |
| 889 | + // gathered bit when just one is, and nothing | |
| 890 | + // at all (SPLIT is inferred) when neither is. | |
| 891 | + match (has_cols32, has_rows32) { | |
| 892 | + (true, true) => { | |
| 893 | + enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w32[ctx]); | |
| 894 | + } | |
| 895 | + (true, false) => { | |
| 896 | + enc.symbol_fixed( | |
| 897 | + 1, | |
| 898 | + &gather(&cdfs.partition_w32[ctx], VERT_ALIKE), | |
| 899 | + ); | |
| 900 | + } | |
| 901 | + (false, true) => { | |
| 902 | + enc.symbol_fixed( | |
| 903 | + 1, | |
| 904 | + &gather(&cdfs.partition_w32[ctx], HORZ_ALIKE), | |
| 905 | + ); | |
| 906 | + } | |
| 907 | + (false, false) => {} | |
| 908 | + } | |
| 909 | + for (block, (sr, sc)) in blocks.iter().zip(sub_positions) { | |
| 910 | + // A 16x16 leaf's own hasRows/hasCols: this | |
| 911 | + // writer has no smaller partition than | |
| 912 | + // 16x16 (no 8x8 or rectangular transform), | |
| 913 | + // so a leaf the true edge itself cuts | |
| 914 | + // through -- legal in the spec only via a | |
| 915 | + // `PARTITION_HORZ`/`VERT` this writer does | |
| 916 | + // not code -- is refused rather than | |
| 917 | + // silently miscoded. | |
| 918 | + let (has_cols16, has_rows16) = ( | |
| 919 | + has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), | |
| 920 | + has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), | |
| 921 | + ); | |
| 922 | + if !has_cols16 || !has_rows16 { | |
| 923 | + return Err(Error::unsupported( | |
| 924 | + "AV1 tile", | |
| 925 | + "a 16x16 block the true frame edge cuts through \ | |
| 926 | + needs a rectangular transform this writer does \ | |
| 927 | + not code yet", | |
| 928 | + )); | |
| 929 | + } | |
| 930 | + let at = (sr, sc); | |
| 842 | 931 | let ctx = neighbours.partition_ctx(at, SUB); |
| 843 | 932 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx]); |
| 844 | 933 | let grids = [ |
| @@ -3141,6 +3230,44 @@ mod tests { | ||
| 3141 | 3230 | } |
| 3142 | 3231 | } |
| 3143 | 3232 | |
| 3233 | + /// The transpose of [`a_frame_that_is_not_a_whole_number_of_superblocks_codes_every_block`]: | |
| 3234 | + /// an odd number of 32x32 block *rows* (superblock hangs off the | |
| 3235 | + /// *bottom*, `has_cols=true, has_rows=false`) rather than an odd number of | |
| 3236 | + /// columns (hangs off the *right*, `has_cols=false, has_rows=true`) -- | |
| 3237 | + /// the two halves of the `(has_cols, has_rows)` match in | |
| 3238 | + /// `sb_coeff_key_frame_tile` gather from different tables | |
| 3239 | + /// (`VERT_ALIKE`/`HORZ_ALIKE`), and only the right-hand-edge direction had | |
| 3240 | + /// a real-decoder test before this one. | |
| 3241 | + #[test] | |
| 3242 | + fn a_frame_that_hangs_off_the_bottom_codes_every_block() { | |
| 3243 | + if !have_ffmpeg() { | |
| 3244 | + eprintln!("SKIP a_frame_that_hangs_off_the_bottom_codes_every_block: no ffmpeg"); | |
| 3245 | + return; | |
| 3246 | + } | |
| 3247 | + let blocks: Vec<BlockCoeffs> = (0..6) | |
| 3248 | + .map(|i| { | |
| 3249 | + let (row, col) = if i % 2 == 0 { (0, 1) } else { (1, 0) }; | |
| 3250 | + BlockCoeffs::from(vec![Coeff { | |
| 3251 | + row, | |
| 3252 | + col, | |
| 3253 | + level: 12, | |
| 3254 | + }]) | |
| 3255 | + }) | |
| 3256 | + .collect(); | |
| 3257 | + let rows = decode_luma_at(64, 96, &blocks); | |
| 3258 | + for block_row in 0..3 { | |
| 3259 | + for block_col in 0..2 { | |
| 3260 | + let name = format!("block ({block_row},{block_col})"); | |
| 3261 | + let block = block_at(&rows, block_row, block_col); | |
| 3262 | + if (block_row * 2 + block_col) % 2 == 0 { | |
| 3263 | + assert_falls_across(&block, &name); | |
| 3264 | + } else { | |
| 3265 | + assert_falls_down(&block, &name); | |
| 3266 | + } | |
| 3267 | + } | |
| 3268 | + } | |
| 3269 | + } | |
| 3270 | + | |
| 3144 | 3271 | /// A frame whose blocks do not tile it is refused rather than written as a |
| 3145 | 3272 | /// stream a decoder walks off the end of. |
| 3146 | 3273 | #[test] |