Merge branch 'lane-av1dec' (r3): inter-frame decode + ffmpeg cross-oracle
Browse files at this commit1 changed file +1096 -1
- crates/ec-av1/src/decode.rs +1096 -1
crates/ec-av1/src/decode.rsmodified+1096 -1
| @@ -24,10 +24,12 @@ | |||
| 24 | use ec_core::{Error, Result}; | 24 | use ec_core::{Error, Result}; |
| 25 | 25 | ||
| 26 | use crate::cdf; | 26 | use crate::cdf; |
| 27 | -use crate::cdf_state::{Cdfs, TxbSet, TxbTables}; | 27 | +use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; |
| 28 | use crate::encode::{Picture, Reach}; | 28 | use crate::encode::{Picture, Reach}; |
| 29 | use crate::intra::predict; | 29 | use crate::intra::predict; |
| 30 | +use crate::mc; | ||
| 30 | use crate::msac::SymbolDecoder; | 31 | use crate::msac::SymbolDecoder; |
| 32 | +use crate::mvstack::{MiGrid, MiInfo, find_mv_stack, single_ref_ctx}; | ||
| 31 | use crate::tile::{INTRA_MODE_CTX, block_grid, has_half}; | 33 | use crate::tile::{INTRA_MODE_CTX, block_grid, has_half}; |
| 32 | use crate::transform::dequant_and_inverse; | 34 | use crate::transform::dequant_and_inverse; |
| 33 | 35 | ||
| @@ -323,6 +325,18 @@ struct Neighbours { | |||
| 323 | left_mode: Vec<usize>, | 325 | left_mode: Vec<usize>, |
| 324 | above_side: Vec<usize>, | 326 | above_side: Vec<usize>, |
| 325 | left_side: Vec<usize>, | 327 | left_side: Vec<usize>, |
| 328 | + /// Whether the block above/left of this column/row was coded `skip` -- | ||
| 329 | + /// an inter frame's own `skip` context (spec `SkipContext`), which the | ||
| 330 | + /// key-frame writer never tracks (its skip context is always zero); a | ||
| 331 | + /// duplicate of [`crate::tile`]'s private `Neighbours::above_skip`/ | ||
| 332 | + /// `left_skip`. | ||
| 333 | + above_skip: Vec<bool>, | ||
| 334 | + left_skip: Vec<bool>, | ||
| 335 | + /// Whether the block above/left was coded inter -- the `is_inter` | ||
| 336 | + /// context (spec `av1_get_intra_inter_context`), a duplicate of | ||
| 337 | + /// [`crate::tile`]'s private `Neighbours::above_inter`/`left_inter`. | ||
| 338 | + above_inter: Vec<bool>, | ||
| 339 | + left_inter: Vec<bool>, | ||
| 326 | mi_cols: usize, | 340 | mi_cols: usize, |
| 327 | mi_rows: usize, | 341 | mi_rows: usize, |
| 328 | } | 342 | } |
| @@ -338,6 +352,10 @@ impl Neighbours { | |||
| 338 | left_mode: vec![DC_PRED; rows], | 352 | left_mode: vec![DC_PRED; rows], |
| 339 | above_side: vec![SB; cols], | 353 | above_side: vec![SB; cols], |
| 340 | left_side: vec![SB; rows], | 354 | left_side: vec![SB; rows], |
| 355 | + above_skip: vec![false; cols], | ||
| 356 | + left_skip: vec![false; rows], | ||
| 357 | + above_inter: vec![false; cols], | ||
| 358 | + left_inter: vec![false; rows], | ||
| 341 | mi_cols, | 359 | mi_cols, |
| 342 | mi_rows, | 360 | mi_rows, |
| 343 | } | 361 | } |
| @@ -347,6 +365,20 @@ impl Neighbours { | |||
| 347 | self.left.iter_mut().for_each(|l| *l = Default::default()); | 365 | self.left.iter_mut().for_each(|l| *l = Default::default()); |
| 348 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); | 366 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 349 | self.left_side.iter_mut().for_each(|s| *s = SB); | 367 | self.left_side.iter_mut().for_each(|s| *s = SB); |
| 368 | + self.left_skip.iter_mut().for_each(|s| *s = false); | ||
| 369 | + self.left_inter.iter_mut().for_each(|i| *i = false); | ||
| 370 | + } | ||
| 371 | + | ||
| 372 | + /// Records a block's `skip`/`is_inter` state for the next block that | ||
| 373 | + /// reads it as a neighbour -- [`crate::tile`]'s `record_inter`. | ||
| 374 | + fn record_inter(&mut self, at: (usize, usize), side: usize, skip: bool, is_inter: bool) { | ||
| 375 | + let (r, c) = at; | ||
| 376 | + for cell in 0..side / SUB { | ||
| 377 | + self.above_skip[c + cell] = skip; | ||
| 378 | + self.left_skip[r + cell] = skip; | ||
| 379 | + self.above_inter[c + cell] = is_inter; | ||
| 380 | + self.left_inter[r + cell] = is_inter; | ||
| 381 | + } | ||
| 350 | } | 382 | } |
| 351 | 383 | ||
| 352 | fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize { | 384 | fn partition_ctx(&self, at: (usize, usize), side: usize) -> usize { |
| @@ -959,6 +991,727 @@ pub fn decode_key_frame_tile( | |||
| 959 | }) | 991 | }) |
| 960 | } | 992 | } |
| 961 | 993 | ||
| 994 | +/// The `is_inter` context (spec 5.11.16 via `av1_get_intra_inter_context`), | ||
| 995 | +/// duplicating [`crate::tile`]'s private copy of the same rule. | ||
| 996 | +fn intra_inter_ctx(has_above: bool, has_left: bool, above_inter: bool, left_inter: bool) -> usize { | ||
| 997 | + match (has_above, has_left) { | ||
| 998 | + (true, true) => { | ||
| 999 | + let (above_intra, left_intra) = (!above_inter, !left_inter); | ||
| 1000 | + if above_intra && left_intra { | ||
| 1001 | + 3 | ||
| 1002 | + } else { | ||
| 1003 | + usize::from(above_intra || left_intra) | ||
| 1004 | + } | ||
| 1005 | + } | ||
| 1006 | + (true, false) => 2 * usize::from(!above_inter), | ||
| 1007 | + (false, true) => 2 * usize::from(!left_inter), | ||
| 1008 | + (false, false) => 0, | ||
| 1009 | + } | ||
| 1010 | +} | ||
| 1011 | + | ||
| 1012 | +/// `CLASS0_SIZE << (class + 2)`, duplicating `crate::tile`'s private | ||
| 1013 | +/// `mv_class_base` (spec 3): the magnitude an `MV_CLASS_n` component's own | ||
| 1014 | +/// bits start counting from. | ||
| 1015 | +fn mv_class_base(class: usize) -> i32 { | ||
| 1016 | + if class == 0 { 0 } else { 2i32 << (class + 2) } | ||
| 1017 | +} | ||
| 1018 | + | ||
| 1019 | +/// One motion vector component's non-zero diff (spec 5.11.32 | ||
| 1020 | +/// `read_mv_component`), the inverse of [`crate::tile`]'s private | ||
| 1021 | +/// `write_mv_component`. | ||
| 1022 | +fn read_mv_component(dec: &mut SymbolDecoder, c: &mut MvComponentCdfs) -> i32 { | ||
| 1023 | + let sign = dec.symbol(&mut c.sign); | ||
| 1024 | + let class = dec.symbol(&mut c.class); | ||
| 1025 | + let local = if class == 0 { | ||
| 1026 | + let bit = dec.symbol(&mut c.class0_bit); | ||
| 1027 | + let fr = dec.symbol(&mut c.class0_fr[bit]); | ||
| 1028 | + (bit << 3) | (fr << 1) | 1 | ||
| 1029 | + } else { | ||
| 1030 | + let mut d = 0; | ||
| 1031 | + for i in 0..class { | ||
| 1032 | + d |= dec.symbol(&mut c.bit[i]) << i; | ||
| 1033 | + } | ||
| 1034 | + let fr = dec.symbol(&mut c.fr); | ||
| 1035 | + (d << 3) | (fr << 1) | 1 | ||
| 1036 | + }; | ||
| 1037 | + let mag = mv_class_base(class) + local as i32 + 1; | ||
| 1038 | + if sign == 1 { -mag } else { mag } | ||
| 1039 | +} | ||
| 1040 | + | ||
| 1041 | +/// A motion vector coded as a residual against `pred` (spec 5.11.32 | ||
| 1042 | +/// `read_mv`), the inverse of [`crate::tile`]'s private `write_mv`. | ||
| 1043 | +fn read_mv( | ||
| 1044 | + dec: &mut SymbolDecoder, | ||
| 1045 | + mv_comp: &mut [MvComponentCdfs; 2], | ||
| 1046 | + mv_joint: &mut [u16; 5], | ||
| 1047 | + pred: (i32, i32), | ||
| 1048 | +) -> (i32, i32) { | ||
| 1049 | + let joint = dec.symbol(mv_joint); | ||
| 1050 | + let mut diff = (0, 0); | ||
| 1051 | + if joint == 2 || joint == 3 { | ||
| 1052 | + diff.0 = read_mv_component(dec, &mut mv_comp[0]); | ||
| 1053 | + } | ||
| 1054 | + if joint == 1 || joint == 3 { | ||
| 1055 | + diff.1 = read_mv_component(dec, &mut mv_comp[1]); | ||
| 1056 | + } | ||
| 1057 | + (pred.0 + diff.0, pred.1 + diff.1) | ||
| 1058 | +} | ||
| 1059 | + | ||
| 1060 | +/// A 1/8-pel motion vector component converted to the 1/16-pel offset | ||
| 1061 | +/// [`mc::predict`] takes, duplicating [`crate::encode`]'s private `mv_to_q4` | ||
| 1062 | +/// (its own doc comment explains the `*2`/`*1` luma/chroma split). | ||
| 1063 | +fn mv_to_q4(pos: usize, mv_component: i32, luma: bool) -> i32 { | ||
| 1064 | + (pos as i32) * 16 + mv_component * if luma { 2 } else { 1 } | ||
| 1065 | +} | ||
| 1066 | + | ||
| 1067 | +impl PlaneBuf { | ||
| 1068 | + /// Adds `residual` onto an already-computed `prediction` (motion | ||
| 1069 | + /// compensation's output, rather than [`predict`]'s intra one), writing | ||
| 1070 | + /// the clamped reconstruction into the plane at `(x, y)` -- the inter | ||
| 1071 | + /// counterpart of [`PlaneBuf::reconstruct`]. | ||
| 1072 | + fn reconstruct_mc( | ||
| 1073 | + &mut self, | ||
| 1074 | + x: usize, | ||
| 1075 | + y: usize, | ||
| 1076 | + side: usize, | ||
| 1077 | + prediction: &[u8], | ||
| 1078 | + residual: &[i32], | ||
| 1079 | + ) { | ||
| 1080 | + for row in 0..side { | ||
| 1081 | + for col in 0..side { | ||
| 1082 | + let sample = (i32::from(prediction[row * side + col]) + residual[row * side + col]) | ||
| 1083 | + .clamp(0, 255) as u8; | ||
| 1084 | + self.data[(y + row) * self.width + x + col] = sample; | ||
| 1085 | + } | ||
| 1086 | + } | ||
| 1087 | + } | ||
| 1088 | +} | ||
| 1089 | + | ||
| 1090 | +/// Reads one inter-coded plane's transform block and reconstructs it by | ||
| 1091 | +/// adding its residual onto a motion-compensated `prediction` -- the inter | ||
| 1092 | +/// counterpart of [`read_plane`] (every inter transform this decoder reads is | ||
| 1093 | +/// `side`-square with no oversized-64 case, so there is no padded-grid | ||
| 1094 | +/// branch to mirror). | ||
| 1095 | +#[allow(clippy::too_many_arguments)] | ||
| 1096 | +fn read_inter_plane( | ||
| 1097 | + dec: &mut SymbolDecoder, | ||
| 1098 | + cdfs: &mut Cdfs, | ||
| 1099 | + set: TxbSet, | ||
| 1100 | + scan: &[u16], | ||
| 1101 | + plane_idx: usize, | ||
| 1102 | + around: (bool, bool, i32), | ||
| 1103 | + tx_mode: usize, | ||
| 1104 | + plane: &mut PlaneBuf, | ||
| 1105 | + x: usize, | ||
| 1106 | + y: usize, | ||
| 1107 | + side: usize, | ||
| 1108 | + base_q_idx: u8, | ||
| 1109 | + prediction: &[u8], | ||
| 1110 | +) -> Result<Vec<i32>> { | ||
| 1111 | + let skip_ctx = if plane_idx == 0 { | ||
| 1112 | + 0 | ||
| 1113 | + } else { | ||
| 1114 | + usize::from(around.0) + usize::from(around.1) | ||
| 1115 | + }; | ||
| 1116 | + let mut coding = cdfs.txb(set, tx_mode); | ||
| 1117 | + let grid = read_coeffs(dec, &mut coding, scan, skip_ctx, dc_sign_ctx(around.2))?; | ||
| 1118 | + let residual = dequant_and_inverse(&grid, side, 8, i32::from(base_q_idx)); | ||
| 1119 | + plane.reconstruct_mc(x, y, side, prediction, &residual); | ||
| 1120 | + Ok(grid) | ||
| 1121 | +} | ||
| 1122 | + | ||
| 1123 | +/// Decodes one square inter-frame block (32x32 whole or 16x16 leaf): skip, | ||
| 1124 | +/// `is_inter`, then either the single-reference/MV-stack/motion-vector chain | ||
| 1125 | +/// and motion-compensated reconstruction, or the inter frame's own intra | ||
| 1126 | +/// path (`Y_MODE` by size group, not `KF_Y_MODE` by neighbour), mirroring | ||
| 1127 | +/// [`crate::tile`]'s `sb_coeff_inter_frame_tile`'s whole-block branch and its | ||
| 1128 | +/// private `write_inter_frame_leaf` -- both share this one function here, | ||
| 1129 | +/// parameterised by `side`/the transform sets/scan tables/size group they | ||
| 1130 | +/// each use. | ||
| 1131 | +/// | ||
| 1132 | +/// # Errors | ||
| 1133 | +/// Returns an error when a symbol names anything this decoder does not | ||
| 1134 | +/// reconstruct: a reference other than `LAST_FRAME`, an inter mode other | ||
| 1135 | +/// than `NEARESTMV`/`NEWMV`, or an intra mode/tx-type outside what | ||
| 1136 | +/// [`read_intra_mode`]/[`read_coeffs`] already refuse. | ||
| 1137 | +#[allow(clippy::too_many_arguments)] | ||
| 1138 | +fn decode_inter_block( | ||
| 1139 | + dec: &mut SymbolDecoder, | ||
| 1140 | + cdfs: &mut Cdfs, | ||
| 1141 | + neighbours: &mut Neighbours, | ||
| 1142 | + grid: &mut MiGrid, | ||
| 1143 | + at: (usize, usize), | ||
| 1144 | + side: usize, | ||
| 1145 | + mi_cols: u32, | ||
| 1146 | + mi_rows: u32, | ||
| 1147 | + y: &mut PlaneBuf, | ||
| 1148 | + u: &mut PlaneBuf, | ||
| 1149 | + v: &mut PlaneBuf, | ||
| 1150 | + ref_y: &PlaneBuf, | ||
| 1151 | + ref_u: &PlaneBuf, | ||
| 1152 | + ref_v: &PlaneBuf, | ||
| 1153 | + base_q_idx: u8, | ||
| 1154 | + luma_set_intra: TxbSet, | ||
| 1155 | + luma_set_inter: TxbSet, | ||
| 1156 | + chroma_set: TxbSet, | ||
| 1157 | + luma_tx: usize, | ||
| 1158 | + chroma_tx: usize, | ||
| 1159 | + scan_luma: &[u16], | ||
| 1160 | + scan_chroma: &[u16], | ||
| 1161 | + size_group: usize, | ||
| 1162 | +) -> Result<()> { | ||
| 1163 | + const LAST_FRAME: i8 = 1; | ||
| 1164 | + | ||
| 1165 | + let (r, c) = at; | ||
| 1166 | + let (px, py) = (c * SUB, r * SUB); | ||
| 1167 | + let (cpx, cpy) = (px / 2, py / 2); | ||
| 1168 | + let chroma_side = side / 2; | ||
| 1169 | + | ||
| 1170 | + let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); | ||
| 1171 | + let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) == 1; | ||
| 1172 | + | ||
| 1173 | + let (has_above, has_left) = (r > 0, c > 0); | ||
| 1174 | + let (above_inter, left_inter) = (neighbours.above_inter[c], neighbours.left_inter[r]); | ||
| 1175 | + let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); | ||
| 1176 | + let is_inter = dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1; | ||
| 1177 | + | ||
| 1178 | + let mode_for_tx; | ||
| 1179 | + let (luma_grid, u_grid, v_grid); | ||
| 1180 | + if is_inter { | ||
| 1181 | + let sr_ctx = single_ref_ctx(above_inter || left_inter); | ||
| 1182 | + let p1 = dec.symbol(&mut cdfs.single_ref[sr_ctx][0]); | ||
| 1183 | + let p3 = dec.symbol(&mut cdfs.single_ref[sr_ctx][2]); | ||
| 1184 | + let p4 = dec.symbol(&mut cdfs.single_ref[sr_ctx][3]); | ||
| 1185 | + if (p1, p3, p4) != (0, 0, 0) { | ||
| 1186 | + return Err(unsupported( | ||
| 1187 | + "a reference frame other than LAST_FRAME (round 2)", | ||
| 1188 | + )); | ||
| 1189 | + } | ||
| 1190 | + | ||
| 1191 | + let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); | ||
| 1192 | + let bw4 = side / 4; | ||
| 1193 | + let stack = find_mv_stack( | ||
| 1194 | + grid, | ||
| 1195 | + mi_row, | ||
| 1196 | + mi_col, | ||
| 1197 | + bw4, | ||
| 1198 | + bw4, | ||
| 1199 | + LAST_FRAME, | ||
| 1200 | + mi_cols as usize, | ||
| 1201 | + mi_rows as usize, | ||
| 1202 | + ); | ||
| 1203 | + | ||
| 1204 | + let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; | ||
| 1205 | + let (mv, is_new_mv) = if !not_new { | ||
| 1206 | + if stack.entries.len() > 1 { | ||
| 1207 | + dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[0]]); | ||
| 1208 | + } | ||
| 1209 | + ( | ||
| 1210 | + read_mv(dec, &mut cdfs.mv_comp, &mut cdfs.mv_joint, stack.pred_mv), | ||
| 1211 | + true, | ||
| 1212 | + ) | ||
| 1213 | + } else { | ||
| 1214 | + let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; | ||
| 1215 | + let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; | ||
| 1216 | + if !not_zero || !nearest { | ||
| 1217 | + return Err(unsupported( | ||
| 1218 | + "an inter mode other than NEARESTMV/NEWMV (round 2)", | ||
| 1219 | + )); | ||
| 1220 | + } | ||
| 1221 | + (stack.nearest_mv, false) | ||
| 1222 | + }; | ||
| 1223 | + for dr in 0..bw4 { | ||
| 1224 | + for dc in 0..bw4 { | ||
| 1225 | + grid.set( | ||
| 1226 | + mi_row + dr, | ||
| 1227 | + mi_col + dc, | ||
| 1228 | + MiInfo { | ||
| 1229 | + is_inter: true, | ||
| 1230 | + ref_frame: LAST_FRAME, | ||
| 1231 | + mv, | ||
| 1232 | + is_new_mv, | ||
| 1233 | + size: bw4, | ||
| 1234 | + }, | ||
| 1235 | + ); | ||
| 1236 | + } | ||
| 1237 | + } | ||
| 1238 | + mode_for_tx = 0; | ||
| 1239 | + | ||
| 1240 | + let mut pred_y = vec![0u8; side * side]; | ||
| 1241 | + mc::predict( | ||
| 1242 | + &ref_y.data, | ||
| 1243 | + ref_y.width, | ||
| 1244 | + ref_y.true_width, | ||
| 1245 | + ref_y.true_height, | ||
| 1246 | + mv_to_q4(px, mv.1, true), | ||
| 1247 | + mv_to_q4(py, mv.0, true), | ||
| 1248 | + side, | ||
| 1249 | + side, | ||
| 1250 | + &mut pred_y, | ||
| 1251 | + ); | ||
| 1252 | + let mut pred_u = vec![0u8; chroma_side * chroma_side]; | ||
| 1253 | + mc::predict( | ||
| 1254 | + &ref_u.data, | ||
| 1255 | + ref_u.width, | ||
| 1256 | + ref_u.true_width, | ||
| 1257 | + ref_u.true_height, | ||
| 1258 | + mv_to_q4(cpx, mv.1, false), | ||
| 1259 | + mv_to_q4(cpy, mv.0, false), | ||
| 1260 | + chroma_side, | ||
| 1261 | + chroma_side, | ||
| 1262 | + &mut pred_u, | ||
| 1263 | + ); | ||
| 1264 | + let mut pred_v = vec![0u8; chroma_side * chroma_side]; | ||
| 1265 | + mc::predict( | ||
| 1266 | + &ref_v.data, | ||
| 1267 | + ref_v.width, | ||
| 1268 | + ref_v.true_width, | ||
| 1269 | + ref_v.true_height, | ||
| 1270 | + mv_to_q4(cpx, mv.1, false), | ||
| 1271 | + mv_to_q4(cpy, mv.0, false), | ||
| 1272 | + chroma_side, | ||
| 1273 | + chroma_side, | ||
| 1274 | + &mut pred_v, | ||
| 1275 | + ); | ||
| 1276 | + | ||
| 1277 | + if skip { | ||
| 1278 | + y.reconstruct_mc(px, py, side, &pred_y, &vec![0i32; side * side]); | ||
| 1279 | + u.reconstruct_mc( | ||
| 1280 | + cpx, | ||
| 1281 | + cpy, | ||
| 1282 | + chroma_side, | ||
| 1283 | + &pred_u, | ||
| 1284 | + &vec![0i32; chroma_side * chroma_side], | ||
| 1285 | + ); | ||
| 1286 | + v.reconstruct_mc( | ||
| 1287 | + cpx, | ||
| 1288 | + cpy, | ||
| 1289 | + chroma_side, | ||
| 1290 | + &pred_v, | ||
| 1291 | + &vec![0i32; chroma_side * chroma_side], | ||
| 1292 | + ); | ||
| 1293 | + luma_grid = vec![0i32; side * side]; | ||
| 1294 | + u_grid = vec![0i32; chroma_side * chroma_side]; | ||
| 1295 | + v_grid = vec![0i32; chroma_side * chroma_side]; | ||
| 1296 | + } else { | ||
| 1297 | + let around = neighbours.around(at, side); | ||
| 1298 | + luma_grid = read_inter_plane( | ||
| 1299 | + dec, | ||
| 1300 | + cdfs, | ||
| 1301 | + luma_set_inter, | ||
| 1302 | + scan_luma, | ||
| 1303 | + 0, | ||
| 1304 | + around[0], | ||
| 1305 | + mode_for_tx, | ||
| 1306 | + y, | ||
| 1307 | + px, | ||
| 1308 | + py, | ||
| 1309 | + side, | ||
| 1310 | + base_q_idx, | ||
| 1311 | + &pred_y, | ||
| 1312 | + )?; | ||
| 1313 | + u_grid = read_inter_plane( | ||
| 1314 | + dec, | ||
| 1315 | + cdfs, | ||
| 1316 | + chroma_set, | ||
| 1317 | + scan_chroma, | ||
| 1318 | + 1, | ||
| 1319 | + around[1], | ||
| 1320 | + mode_for_tx, | ||
| 1321 | + u, | ||
| 1322 | + cpx, | ||
| 1323 | + cpy, | ||
| 1324 | + chroma_side, | ||
| 1325 | + base_q_idx, | ||
| 1326 | + &pred_u, | ||
| 1327 | + )?; | ||
| 1328 | + v_grid = read_inter_plane( | ||
| 1329 | + dec, | ||
| 1330 | + cdfs, | ||
| 1331 | + chroma_set, | ||
| 1332 | + scan_chroma, | ||
| 1333 | + 2, | ||
| 1334 | + around[2], | ||
| 1335 | + mode_for_tx, | ||
| 1336 | + v, | ||
| 1337 | + cpx, | ||
| 1338 | + cpy, | ||
| 1339 | + chroma_side, | ||
| 1340 | + base_q_idx, | ||
| 1341 | + &pred_v, | ||
| 1342 | + )?; | ||
| 1343 | + } | ||
| 1344 | + } else { | ||
| 1345 | + let mode = dec.symbol(&mut cdfs.y_mode[size_group]); | ||
| 1346 | + if mode >= 13 { | ||
| 1347 | + return Err(unsupported( | ||
| 1348 | + "an intra mode this decoder does not code (round 2)", | ||
| 1349 | + )); | ||
| 1350 | + } | ||
| 1351 | + if (V_PRED..=D67_PRED).contains(&mode) { | ||
| 1352 | + let angle = dec.symbol(&mut cdfs.angle_delta[mode - V_PRED]); | ||
| 1353 | + if angle != ANGLE_DELTA_ZERO { | ||
| 1354 | + return Err(unsupported( | ||
| 1355 | + "a nonzero angle delta (this encoder never writes one)", | ||
| 1356 | + )); | ||
| 1357 | + } | ||
| 1358 | + } | ||
| 1359 | + let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]); | ||
| 1360 | + if uv_mode != DC_PRED { | ||
| 1361 | + return Err(unsupported("a chroma-from-luma mode (round 2)")); | ||
| 1362 | + } | ||
| 1363 | + mode_for_tx = mode; | ||
| 1364 | + let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); | ||
| 1365 | + for dr in 0..side / 4 { | ||
| 1366 | + for dc in 0..side / 4 { | ||
| 1367 | + grid.set( | ||
| 1368 | + mi_row + dr, | ||
| 1369 | + mi_col + dc, | ||
| 1370 | + MiInfo { | ||
| 1371 | + is_inter: false, | ||
| 1372 | + ref_frame: -1, | ||
| 1373 | + mv: (0, 0), | ||
| 1374 | + is_new_mv: false, | ||
| 1375 | + size: side / 4, | ||
| 1376 | + }, | ||
| 1377 | + ); | ||
| 1378 | + } | ||
| 1379 | + } | ||
| 1380 | + | ||
| 1381 | + let reach = Reach::of(side, px, py, y.width, y.height); | ||
| 1382 | + if skip { | ||
| 1383 | + y.reconstruct(px, py, side, mode, reach, &vec![0i32; side * side]); | ||
| 1384 | + u.reconstruct( | ||
| 1385 | + cpx, | ||
| 1386 | + cpy, | ||
| 1387 | + chroma_side, | ||
| 1388 | + DC_PRED, | ||
| 1389 | + Reach::none(), | ||
| 1390 | + &vec![0i32; chroma_side * chroma_side], | ||
| 1391 | + ); | ||
| 1392 | + v.reconstruct( | ||
| 1393 | + cpx, | ||
| 1394 | + cpy, | ||
| 1395 | + chroma_side, | ||
| 1396 | + DC_PRED, | ||
| 1397 | + Reach::none(), | ||
| 1398 | + &vec![0i32; chroma_side * chroma_side], | ||
| 1399 | + ); | ||
| 1400 | + luma_grid = vec![0i32; side * side]; | ||
| 1401 | + u_grid = vec![0i32; chroma_side * chroma_side]; | ||
| 1402 | + v_grid = vec![0i32; chroma_side * chroma_side]; | ||
| 1403 | + } else { | ||
| 1404 | + let around = neighbours.around(at, side); | ||
| 1405 | + luma_grid = read_plane( | ||
| 1406 | + dec, | ||
| 1407 | + cdfs, | ||
| 1408 | + luma_set_intra, | ||
| 1409 | + scan_luma, | ||
| 1410 | + 0, | ||
| 1411 | + around[0], | ||
| 1412 | + mode, | ||
| 1413 | + mode, | ||
| 1414 | + reach, | ||
| 1415 | + y, | ||
| 1416 | + px, | ||
| 1417 | + py, | ||
| 1418 | + side, | ||
| 1419 | + luma_tx, | ||
| 1420 | + base_q_idx, | ||
| 1421 | + )?; | ||
| 1422 | + u_grid = read_plane( | ||
| 1423 | + dec, | ||
| 1424 | + cdfs, | ||
| 1425 | + chroma_set, | ||
| 1426 | + scan_chroma, | ||
| 1427 | + 1, | ||
| 1428 | + around[1], | ||
| 1429 | + mode, | ||
| 1430 | + DC_PRED, | ||
| 1431 | + Reach::none(), | ||
| 1432 | + u, | ||
| 1433 | + cpx, | ||
| 1434 | + cpy, | ||
| 1435 | + chroma_side, | ||
| 1436 | + chroma_tx, | ||
| 1437 | + base_q_idx, | ||
| 1438 | + )?; | ||
| 1439 | + v_grid = read_plane( | ||
| 1440 | + dec, | ||
| 1441 | + cdfs, | ||
| 1442 | + chroma_set, | ||
| 1443 | + scan_chroma, | ||
| 1444 | + 2, | ||
| 1445 | + around[2], | ||
| 1446 | + mode, | ||
| 1447 | + DC_PRED, | ||
| 1448 | + Reach::none(), | ||
| 1449 | + v, | ||
| 1450 | + cpx, | ||
| 1451 | + cpy, | ||
| 1452 | + chroma_side, | ||
| 1453 | + chroma_tx, | ||
| 1454 | + base_q_idx, | ||
| 1455 | + )?; | ||
| 1456 | + } | ||
| 1457 | + } | ||
| 1458 | + neighbours.record(at, side, mode_for_tx, &[luma_grid, u_grid, v_grid]); | ||
| 1459 | + neighbours.record_inter(at, side, skip, is_inter); | ||
| 1460 | + Ok(()) | ||
| 1461 | +} | ||
| 1462 | + | ||
| 1463 | +/// Decodes the payload [`crate::tile::sb_coeff_inter_frame_tile`] writes, | ||
| 1464 | +/// against `reference` (the previous frame's own decoded picture, at this | ||
| 1465 | +/// frame's true size -- a decoder's single-slot DPB, matching what this | ||
| 1466 | +/// crate's encoder always predicts from), returning the picture it | ||
| 1467 | +/// reconstructs to. Mirrors [`decode_key_frame_tile`]'s own contract | ||
| 1468 | +/// (`mi_cols`/`mi_rows`/`base_q_idx` from the frame header, | ||
| 1469 | +/// `frame_width`/`frame_height` its true render size). | ||
| 1470 | +/// | ||
| 1471 | +/// # Errors | ||
| 1472 | +/// Returns an error under the same conditions [`decode_inter_block`] does, | ||
| 1473 | +/// or when `reference` is not this frame's own true size, or when the tile | ||
| 1474 | +/// codes a 16x16 leaf whose own true edge cuts through it (round 2, same | ||
| 1475 | +/// refusal [`crate::tile`]'s writer gives its encoder). | ||
| 1476 | +pub fn decode_inter_frame_tile( | ||
| 1477 | + data: &[u8], | ||
| 1478 | + mi_cols: u32, | ||
| 1479 | + mi_rows: u32, | ||
| 1480 | + base_q_idx: u8, | ||
| 1481 | + frame_width: u32, | ||
| 1482 | + frame_height: u32, | ||
| 1483 | + reference: &Picture, | ||
| 1484 | +) -> Result<Picture> { | ||
| 1485 | + if mi_cols == 0 || mi_rows == 0 { | ||
| 1486 | + return Err(unsupported("a frame with no mode-info grid")); | ||
| 1487 | + } | ||
| 1488 | + let (true_width, true_height) = ((mi_cols * 4) as usize, (mi_rows * 4) as usize); | ||
| 1489 | + if reference.width != true_width || reference.height != true_height { | ||
| 1490 | + return Err(unsupported( | ||
| 1491 | + "a reference picture that is not this frame's own true size", | ||
| 1492 | + )); | ||
| 1493 | + } | ||
| 1494 | + let (cols, rows) = block_grid(mi_cols, mi_rows); | ||
| 1495 | + let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); | ||
| 1496 | + let (width, height) = (cols as usize * BLOCK, rows as usize * BLOCK); | ||
| 1497 | + | ||
| 1498 | + let ref_y = PlaneBuf { | ||
| 1499 | + data: reference.y.clone(), | ||
| 1500 | + width: reference.width, | ||
| 1501 | + height: reference.height, | ||
| 1502 | + true_width: reference.width, | ||
| 1503 | + true_height: reference.height, | ||
| 1504 | + }; | ||
| 1505 | + let ref_u = PlaneBuf { | ||
| 1506 | + data: reference.u.clone(), | ||
| 1507 | + width: reference.width / 2, | ||
| 1508 | + height: reference.height / 2, | ||
| 1509 | + true_width: reference.width / 2, | ||
| 1510 | + true_height: reference.height / 2, | ||
| 1511 | + }; | ||
| 1512 | + let ref_v = PlaneBuf { | ||
| 1513 | + data: reference.v.clone(), | ||
| 1514 | + width: reference.width / 2, | ||
| 1515 | + height: reference.height / 2, | ||
| 1516 | + true_width: reference.width / 2, | ||
| 1517 | + true_height: reference.height / 2, | ||
| 1518 | + }; | ||
| 1519 | + | ||
| 1520 | + let mut y = PlaneBuf { | ||
| 1521 | + data: vec![0u8; width * height], | ||
| 1522 | + width, | ||
| 1523 | + height, | ||
| 1524 | + true_width, | ||
| 1525 | + true_height, | ||
| 1526 | + }; | ||
| 1527 | + let mut u = PlaneBuf { | ||
| 1528 | + data: vec![0u8; width * height / 4], | ||
| 1529 | + width: width / 2, | ||
| 1530 | + height: height / 2, | ||
| 1531 | + true_width: true_width / 2, | ||
| 1532 | + true_height: true_height / 2, | ||
| 1533 | + }; | ||
| 1534 | + let mut v = PlaneBuf { | ||
| 1535 | + data: vec![0u8; width * height / 4], | ||
| 1536 | + width: width / 2, | ||
| 1537 | + height: height / 2, | ||
| 1538 | + true_width: true_width / 2, | ||
| 1539 | + true_height: true_height / 2, | ||
| 1540 | + }; | ||
| 1541 | + | ||
| 1542 | + let scan32 = default_scan(TX32); | ||
| 1543 | + let scan16 = default_scan(TX16); | ||
| 1544 | + let scan8 = default_scan(TX8); | ||
| 1545 | + | ||
| 1546 | + let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); | ||
| 1547 | + let mut dec = SymbolDecoder::new(data); | ||
| 1548 | + let mut neighbours = Neighbours::new( | ||
| 1549 | + cols as usize * 2, | ||
| 1550 | + rows as usize * 2, | ||
| 1551 | + mi_cols as usize, | ||
| 1552 | + mi_rows as usize, | ||
| 1553 | + ); | ||
| 1554 | + let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); | ||
| 1555 | + | ||
| 1556 | + for sb_r in 0..sb_rows { | ||
| 1557 | + neighbours.start_row(); | ||
| 1558 | + for sb_c in 0..sb_cols { | ||
| 1559 | + let sb_at = (sb_r as usize * 4, sb_c as usize * 4); | ||
| 1560 | + let sb_ctx = neighbours.partition_ctx(sb_at, SB); | ||
| 1561 | + let (has_cols, has_rows) = ( | ||
| 1562 | + sb_c * SB_MI + SB_MI / 2 < mi_cols, | ||
| 1563 | + sb_r * SB_MI + SB_MI / 2 < mi_rows, | ||
| 1564 | + ); | ||
| 1565 | + match (has_cols, has_rows) { | ||
| 1566 | + (true, true) => { | ||
| 1567 | + dec.symbol(&mut cdfs.partition_w64[sb_ctx]); | ||
| 1568 | + } | ||
| 1569 | + (true, false) => { | ||
| 1570 | + dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE)); | ||
| 1571 | + } | ||
| 1572 | + (false, true) => { | ||
| 1573 | + dec.symbol_fixed(&gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE)); | ||
| 1574 | + } | ||
| 1575 | + (false, false) => {} | ||
| 1576 | + } | ||
| 1577 | + | ||
| 1578 | + for quadrant in 0..4 { | ||
| 1579 | + let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); | ||
| 1580 | + if r32 >= rows || c32 >= cols { | ||
| 1581 | + continue; | ||
| 1582 | + } | ||
| 1583 | + let at = (r32 as usize * 2, c32 as usize * 2); | ||
| 1584 | + let ctx32 = neighbours.partition_ctx(at, BLOCK); | ||
| 1585 | + let (has_cols32, has_rows32) = ( | ||
| 1586 | + has_half(c32 * BLOCK_MI, BLOCK_MI, mi_cols), | ||
| 1587 | + has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), | ||
| 1588 | + ); | ||
| 1589 | + let part32 = if has_cols32 && has_rows32 { | ||
| 1590 | + dec.symbol(&mut cdfs.partition_w32[ctx32]) | ||
| 1591 | + } else { | ||
| 1592 | + match (has_cols32, has_rows32) { | ||
| 1593 | + (true, false) => { | ||
| 1594 | + dec.symbol_fixed(&gather(&cdfs.partition_w32[ctx32], VERT_ALIKE)); | ||
| 1595 | + } | ||
| 1596 | + (false, true) => { | ||
| 1597 | + dec.symbol_fixed(&gather(&cdfs.partition_w32[ctx32], HORZ_ALIKE)); | ||
| 1598 | + } | ||
| 1599 | + _ => {} | ||
| 1600 | + } | ||
| 1601 | + PARTITION_SPLIT | ||
| 1602 | + }; | ||
| 1603 | + match part32 { | ||
| 1604 | + PARTITION_NONE => { | ||
| 1605 | + decode_inter_block( | ||
| 1606 | + &mut dec, | ||
| 1607 | + &mut cdfs, | ||
| 1608 | + &mut neighbours, | ||
| 1609 | + &mut grid, | ||
| 1610 | + at, | ||
| 1611 | + BLOCK, | ||
| 1612 | + mi_cols, | ||
| 1613 | + mi_rows, | ||
| 1614 | + &mut y, | ||
| 1615 | + &mut u, | ||
| 1616 | + &mut v, | ||
| 1617 | + &ref_y, | ||
| 1618 | + &ref_u, | ||
| 1619 | + &ref_v, | ||
| 1620 | + base_q_idx, | ||
| 1621 | + TxbSet::Luma32, | ||
| 1622 | + TxbSet::Luma32Inter, | ||
| 1623 | + TxbSet::Chroma16, | ||
| 1624 | + TX32, | ||
| 1625 | + TX16, | ||
| 1626 | + &scan32, | ||
| 1627 | + &scan16, | ||
| 1628 | + 3, | ||
| 1629 | + )?; | ||
| 1630 | + } | ||
| 1631 | + PARTITION_SPLIT => { | ||
| 1632 | + for sub in 0..4 { | ||
| 1633 | + let (sr, sc) = (r32 as usize * 2 + sub / 2, c32 as usize * 2 + sub % 2); | ||
| 1634 | + if (sr as u32) * SUB_MI >= mi_rows || (sc as u32) * SUB_MI >= mi_cols { | ||
| 1635 | + continue; | ||
| 1636 | + } | ||
| 1637 | + let (has_cols16, has_rows16) = ( | ||
| 1638 | + has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), | ||
| 1639 | + has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), | ||
| 1640 | + ); | ||
| 1641 | + if !has_cols16 || !has_rows16 { | ||
| 1642 | + return Err(unsupported( | ||
| 1643 | + "a 16x16 inter block the true frame edge cuts through (round 2)", | ||
| 1644 | + )); | ||
| 1645 | + } | ||
| 1646 | + let at16 = (sr, sc); | ||
| 1647 | + let ctx16 = neighbours.partition_ctx(at16, SUB); | ||
| 1648 | + let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]); | ||
| 1649 | + if part16 != PARTITION_NONE { | ||
| 1650 | + return Err(unsupported( | ||
| 1651 | + "a partition below 16x16 (this encoder never writes one)", | ||
| 1652 | + )); | ||
| 1653 | + } | ||
| 1654 | + decode_inter_block( | ||
| 1655 | + &mut dec, | ||
| 1656 | + &mut cdfs, | ||
| 1657 | + &mut neighbours, | ||
| 1658 | + &mut grid, | ||
| 1659 | + at16, | ||
| 1660 | + SUB, | ||
| 1661 | + mi_cols, | ||
| 1662 | + mi_rows, | ||
| 1663 | + &mut y, | ||
| 1664 | + &mut u, | ||
| 1665 | + &mut v, | ||
| 1666 | + &ref_y, | ||
| 1667 | + &ref_u, | ||
| 1668 | + &ref_v, | ||
| 1669 | + base_q_idx, | ||
| 1670 | + TxbSet::Luma16, | ||
| 1671 | + TxbSet::Luma16Inter, | ||
| 1672 | + TxbSet::Chroma8, | ||
| 1673 | + TX16, | ||
| 1674 | + TX8, | ||
| 1675 | + &scan16, | ||
| 1676 | + &scan8, | ||
| 1677 | + 2, | ||
| 1678 | + )?; | ||
| 1679 | + } | ||
| 1680 | + } | ||
| 1681 | + _ => { | ||
| 1682 | + return Err(unsupported("a partition type this encoder never writes")); | ||
| 1683 | + } | ||
| 1684 | + } | ||
| 1685 | + } | ||
| 1686 | + } | ||
| 1687 | + } | ||
| 1688 | + | ||
| 1689 | + let (fw, fh) = (frame_width as usize, frame_height as usize); | ||
| 1690 | + if fw == width && fh == height { | ||
| 1691 | + return Ok(Picture { | ||
| 1692 | + width, | ||
| 1693 | + height, | ||
| 1694 | + y: y.data, | ||
| 1695 | + u: u.data, | ||
| 1696 | + v: v.data, | ||
| 1697 | + }); | ||
| 1698 | + } | ||
| 1699 | + let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u8> { | ||
| 1700 | + let mut out = Vec::with_capacity(w * h); | ||
| 1701 | + for row in 0..h { | ||
| 1702 | + out.extend_from_slice(&plane.data[row * plane.width..][..w]); | ||
| 1703 | + } | ||
| 1704 | + out | ||
| 1705 | + }; | ||
| 1706 | + Ok(Picture { | ||
| 1707 | + width: fw, | ||
| 1708 | + height: fh, | ||
| 1709 | + y: crop(&y, fw, fh), | ||
| 1710 | + u: crop(&u, fw / 2, fh / 2), | ||
| 1711 | + v: crop(&v, fw / 2, fh / 2), | ||
| 1712 | + }) | ||
| 1713 | +} | ||
| 1714 | + | ||
| 962 | #[cfg(test)] | 1715 | #[cfg(test)] |
| 963 | mod tests { | 1716 | mod tests { |
| 964 | use super::*; | 1717 | use super::*; |
| @@ -1067,4 +1820,346 @@ mod tests { | |||
| 1067 | "a skipped DC_PRED block with no neighbours predicts flat mid-grey" | 1820 | "a skipped DC_PRED block with no neighbours predicts flat mid-grey" |
| 1068 | ); | 1821 | ); |
| 1069 | } | 1822 | } |
| 1823 | + | ||
| 1824 | + // ffmpeg cross-oracle: an independent decoder (not this crate's own | ||
| 1825 | + // encoder-side reconstruction) agrees with what this module decodes, | ||
| 1826 | + // duplicating `crate::encode`'s own `#[cfg(test)]` helpers of the same | ||
| 1827 | + // name (that module is another lane's territory this round, and the | ||
| 1828 | + // helpers are `#[cfg(test)]`-private to it). | ||
| 1829 | + use std::io::Write; | ||
| 1830 | + use std::process::{Command, Stdio}; | ||
| 1831 | + | ||
| 1832 | + fn have_ffmpeg() -> bool { | ||
| 1833 | + Command::new("ffmpeg") | ||
| 1834 | + .arg("-version") | ||
| 1835 | + .stdout(Stdio::null()) | ||
| 1836 | + .stderr(Stdio::null()) | ||
| 1837 | + .status() | ||
| 1838 | + .is_ok_and(|s| s.success()) | ||
| 1839 | + } | ||
| 1840 | + | ||
| 1841 | + /// Decodes one AV1 OBU stream with ffmpeg and hands back its one 4:2:0 | ||
| 1842 | + /// frame's planes, at ffmpeg's own (coded, block-padded) size. | ||
| 1843 | + fn ffmpeg_decode(stream: &[u8], width: usize, height: usize) -> Picture { | ||
| 1844 | + let mut child = Command::new("ffmpeg") | ||
| 1845 | + .args([ | ||
| 1846 | + "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-", | ||
| 1847 | + ]) | ||
| 1848 | + .stdin(Stdio::piped()) | ||
| 1849 | + .stdout(Stdio::piped()) | ||
| 1850 | + .stderr(Stdio::piped()) | ||
| 1851 | + .spawn() | ||
| 1852 | + .expect("ffmpeg failed to start"); | ||
| 1853 | + child | ||
| 1854 | + .stdin | ||
| 1855 | + .take() | ||
| 1856 | + .expect("ffmpeg stdin") | ||
| 1857 | + .write_all(stream) | ||
| 1858 | + .expect("writing the stream to ffmpeg"); | ||
| 1859 | + let out = child.wait_with_output().expect("ffmpeg failed to run"); | ||
| 1860 | + assert!( | ||
| 1861 | + out.status.success(), | ||
| 1862 | + "ffmpeg refused the stream: {}", | ||
| 1863 | + String::from_utf8_lossy(&out.stderr) | ||
| 1864 | + ); | ||
| 1865 | + let (luma, chroma) = (width * height, width * height / 4); | ||
| 1866 | + assert_eq!( | ||
| 1867 | + out.stdout.len(), | ||
| 1868 | + luma + 2 * chroma, | ||
| 1869 | + "expected one 4:2:0 frame" | ||
| 1870 | + ); | ||
| 1871 | + Picture { | ||
| 1872 | + width, | ||
| 1873 | + height, | ||
| 1874 | + y: out.stdout[..luma].to_vec(), | ||
| 1875 | + u: out.stdout[luma..luma + chroma].to_vec(), | ||
| 1876 | + v: out.stdout[luma + chroma..].to_vec(), | ||
| 1877 | + } | ||
| 1878 | + } | ||
| 1879 | + | ||
| 1880 | + /// Decodes `frames` concatenated 4:2:0 frames out of one AV1 OBU stream. | ||
| 1881 | + fn ffmpeg_decode_sequence( | ||
| 1882 | + stream: &[u8], | ||
| 1883 | + width: usize, | ||
| 1884 | + height: usize, | ||
| 1885 | + frames: usize, | ||
| 1886 | + ) -> Vec<Picture> { | ||
| 1887 | + let mut child = Command::new("ffmpeg") | ||
| 1888 | + .args([ | ||
| 1889 | + "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-", | ||
| 1890 | + ]) | ||
| 1891 | + .stdin(Stdio::piped()) | ||
| 1892 | + .stdout(Stdio::piped()) | ||
| 1893 | + .stderr(Stdio::piped()) | ||
| 1894 | + .spawn() | ||
| 1895 | + .expect("ffmpeg failed to start"); | ||
| 1896 | + child | ||
| 1897 | + .stdin | ||
| 1898 | + .take() | ||
| 1899 | + .expect("ffmpeg stdin") | ||
| 1900 | + .write_all(stream) | ||
| 1901 | + .expect("writing the stream to ffmpeg"); | ||
| 1902 | + let out = child.wait_with_output().expect("ffmpeg failed to run"); | ||
| 1903 | + assert!( | ||
| 1904 | + out.status.success(), | ||
| 1905 | + "ffmpeg refused the stream: {}", | ||
| 1906 | + String::from_utf8_lossy(&out.stderr) | ||
| 1907 | + ); | ||
| 1908 | + let (luma, chroma) = (width * height, width * height / 4); | ||
| 1909 | + let frame_bytes = luma + 2 * chroma; | ||
| 1910 | + assert_eq!( | ||
| 1911 | + out.stdout.len(), | ||
| 1912 | + frame_bytes * frames, | ||
| 1913 | + "expected {frames} 4:2:0 frames, ffmpeg said: {}", | ||
| 1914 | + String::from_utf8_lossy(&out.stderr) | ||
| 1915 | + ); | ||
| 1916 | + (0..frames) | ||
| 1917 | + .map(|i| { | ||
| 1918 | + let base = i * frame_bytes; | ||
| 1919 | + Picture { | ||
| 1920 | + width, | ||
| 1921 | + height, | ||
| 1922 | + y: out.stdout[base..base + luma].to_vec(), | ||
| 1923 | + u: out.stdout[base + luma..base + luma + chroma].to_vec(), | ||
| 1924 | + v: out.stdout[base + luma + chroma..base + frame_bytes].to_vec(), | ||
| 1925 | + } | ||
| 1926 | + }) | ||
| 1927 | + .collect() | ||
| 1928 | + } | ||
| 1929 | + | ||
| 1930 | + /// `ffprobe`'s reported coded `width,height` for one OBU stream -- | ||
| 1931 | + /// duplicating `crate::encode`'s own helper (per-call temp naming: the | ||
| 1932 | + /// test binary runs callers on parallel threads, and a shared name lets | ||
| 1933 | + /// one test's cleanup race another's still-reading ffprobe -- | ||
| 1934 | + /// pid-keyed-temp-path class). | ||
| 1935 | + fn ffprobe_size(stream: &[u8]) -> (u32, u32) { | ||
| 1936 | + static PROBE_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); | ||
| 1937 | + let path = std::env::temp_dir().join(format!( | ||
| 1938 | + "ec-av1-decode-probe-{}-{}.obu", | ||
| 1939 | + std::process::id(), | ||
| 1940 | + PROBE_SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed) | ||
| 1941 | + )); | ||
| 1942 | + let _ = std::fs::remove_file(&path); | ||
| 1943 | + std::fs::write(&path, stream).expect("writing the probe stream"); | ||
| 1944 | + let out = Command::new("ffprobe") | ||
| 1945 | + .args([ | ||
| 1946 | + "-v", | ||
| 1947 | + "error", | ||
| 1948 | + "-f", | ||
| 1949 | + "obu", | ||
| 1950 | + "-select_streams", | ||
| 1951 | + "v:0", | ||
| 1952 | + "-show_entries", | ||
| 1953 | + "stream=width,height", | ||
| 1954 | + "-of", | ||
| 1955 | + "csv=p=0", | ||
| 1956 | + ]) | ||
| 1957 | + .arg(&path) | ||
| 1958 | + .output() | ||
| 1959 | + .expect("ffprobe failed to run"); | ||
| 1960 | + let _ = std::fs::remove_file(&path); | ||
| 1961 | + assert!( | ||
| 1962 | + out.status.success(), | ||
| 1963 | + "ffprobe refused the stream: {}", | ||
| 1964 | + String::from_utf8_lossy(&out.stderr) | ||
| 1965 | + ); | ||
| 1966 | + let text = String::from_utf8_lossy(&out.stdout); | ||
| 1967 | + let mut fields = text.trim().split(','); | ||
| 1968 | + let width: u32 = fields.next().expect("ffprobe width").parse().unwrap(); | ||
| 1969 | + let height: u32 = fields.next().expect("ffprobe height").parse().unwrap(); | ||
| 1970 | + (width, height) | ||
| 1971 | + } | ||
| 1972 | + | ||
| 1973 | + /// This module's own decoder against an independent one (ffmpeg), not | ||
| 1974 | + /// just against this crate's encoder-side reconstruction: both decoders | ||
| 1975 | + /// read the identical tile bytes, so agreement here rules out a bug this | ||
| 1976 | + /// crate's own writer and reader could otherwise share. | ||
| 1977 | + #[test] | ||
| 1978 | + fn ffmpeg_and_this_decoder_agree_on_a_key_frame() { | ||
| 1979 | + if !have_ffmpeg() { | ||
| 1980 | + eprintln!("SKIP ffmpeg_and_this_decoder_agree_on_a_key_frame: no ffmpeg"); | ||
| 1981 | + return; | ||
| 1982 | + } | ||
| 1983 | + use crate::encode::encode_key_frame; | ||
| 1984 | + for &(width, height) in &[(64usize, 64usize), (216, 96)] { | ||
| 1985 | + let picture = round_trip_test_card(width, height); | ||
| 1986 | + let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); | ||
| 1987 | + let (coded_w, coded_h) = ffprobe_size(&encoded.stream); | ||
| 1988 | + let ffmpeg_decoded = ffmpeg_decode(&encoded.stream, coded_w as usize, coded_h as usize); | ||
| 1989 | + let ours = decode_key_frame_tile( | ||
| 1990 | + &encoded.tile, | ||
| 1991 | + encoded.mi_cols, | ||
| 1992 | + encoded.mi_rows, | ||
| 1993 | + encoded.base_q_idx, | ||
| 1994 | + coded_w, | ||
| 1995 | + coded_h, | ||
| 1996 | + ) | ||
| 1997 | + .unwrap(); | ||
| 1998 | + assert_eq!(ours.y, ffmpeg_decoded.y, "{width}x{height}: luma vs ffmpeg"); | ||
| 1999 | + assert_eq!(ours.u, ffmpeg_decoded.u, "{width}x{height}: U vs ffmpeg"); | ||
| 2000 | + assert_eq!(ours.v, ffmpeg_decoded.v, "{width}x{height}: V vs ffmpeg"); | ||
| 2001 | + } | ||
| 2002 | + } | ||
| 2003 | + | ||
| 2004 | + fn round_trip_test_card(width: usize, height: usize) -> crate::encode::Picture { | ||
| 2005 | + let mut picture = crate::encode::Picture::grey(width, height); | ||
| 2006 | + for row in 0..height { | ||
| 2007 | + for col in 0..width { | ||
| 2008 | + picture.y[row * width + col] = ((row * 7 + col * 11) % 251) as u8; | ||
| 2009 | + } | ||
| 2010 | + } | ||
| 2011 | + for row in 0..height / 2 { | ||
| 2012 | + for col in 0..width / 2 { | ||
| 2013 | + let i = row * width / 2 + col; | ||
| 2014 | + picture.u[i] = (100 + (col * 60 / (width / 2).max(1))) as u8; | ||
| 2015 | + picture.v[i] = (200 - (row * 80 / (height / 2).max(1))) as u8; | ||
| 2016 | + } | ||
| 2017 | + } | ||
| 2018 | + picture | ||
| 2019 | + } | ||
| 2020 | + | ||
| 2021 | + fn panned_test_card(width: usize, height: usize, shift: i64) -> crate::encode::Picture { | ||
| 2022 | + let mut picture = crate::encode::Picture::grey(width, height); | ||
| 2023 | + for y in 0..height { | ||
| 2024 | + for x in 0..width { | ||
| 2025 | + let sx = (x as i64 - shift).rem_euclid(width as i64) as f64; | ||
| 2026 | + let gradient = sx * 200.0 / width as f64; | ||
| 2027 | + picture.y[y * width + x] = (20.0 + gradient).clamp(0.0, 255.0) as u8; | ||
| 2028 | + } | ||
| 2029 | + } | ||
| 2030 | + for y in 0..height / 2 { | ||
| 2031 | + for x in 0..width / 2 { | ||
| 2032 | + let sx = (x as i64 - shift / 2).rem_euclid((width / 2) as i64) as usize; | ||
| 2033 | + let i = y * width / 2 + x; | ||
| 2034 | + picture.u[i] = (100 + (sx * 60 / (width / 2))) as u8; | ||
| 2035 | + picture.v[i] = (200 - (y * 80 / (height / 2))) as u8; | ||
| 2036 | + } | ||
| 2037 | + } | ||
| 2038 | + picture | ||
| 2039 | + } | ||
| 2040 | + | ||
| 2041 | + /// A GOP (one key frame plus several inter frames, each panned so its | ||
| 2042 | + /// blocks actually take `NEARESTMV`/`NEWMV` rather than an all-skip | ||
| 2043 | + /// `(0, 0)` no-op) decodes bit-exact against the encoder's own | ||
| 2044 | + /// reconstruction, frame by frame, chaining each decoded frame straight | ||
| 2045 | + /// into the next as its reference -- exactly the single-slot DPB | ||
| 2046 | + /// [`crate::encode::encode_sequence`] itself threads. | ||
| 2047 | + fn gop_round_trips(width: usize, height: usize) { | ||
| 2048 | + use crate::encode::encode_sequence; | ||
| 2049 | + let pictures: Vec<_> = (0..4) | ||
| 2050 | + .map(|i| panned_test_card(width, height, i * 3)) | ||
| 2051 | + .collect(); | ||
| 2052 | + let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); | ||
| 2053 | + assert_eq!(encoded.frames.len(), 4); | ||
| 2054 | + | ||
| 2055 | + let key = &encoded.frames[0]; | ||
| 2056 | + let mut reference = decode_key_frame_tile( | ||
| 2057 | + &key.tile, | ||
| 2058 | + key.mi_cols, | ||
| 2059 | + key.mi_rows, | ||
| 2060 | + key.base_q_idx, | ||
| 2061 | + width as u32, | ||
| 2062 | + height as u32, | ||
| 2063 | + ) | ||
| 2064 | + .unwrap(); | ||
| 2065 | + assert_eq!( | ||
| 2066 | + reference.y, key.reconstruction.y, | ||
| 2067 | + "{width}x{height} frame 0 luma" | ||
| 2068 | + ); | ||
| 2069 | + assert_eq!( | ||
| 2070 | + reference.u, key.reconstruction.u, | ||
| 2071 | + "{width}x{height} frame 0 U" | ||
| 2072 | + ); | ||
| 2073 | + assert_eq!( | ||
| 2074 | + reference.v, key.reconstruction.v, | ||
| 2075 | + "{width}x{height} frame 0 V" | ||
| 2076 | + ); | ||
| 2077 | + | ||
| 2078 | + for (i, frame) in encoded.frames.iter().enumerate().skip(1) { | ||
| 2079 | + let decoded = decode_inter_frame_tile( | ||
| 2080 | + &frame.tile, | ||
| 2081 | + frame.mi_cols, | ||
| 2082 | + frame.mi_rows, | ||
| 2083 | + frame.base_q_idx, | ||
| 2084 | + width as u32, | ||
| 2085 | + height as u32, | ||
| 2086 | + &reference, | ||
| 2087 | + ) | ||
| 2088 | + .unwrap(); | ||
| 2089 | + assert_eq!( | ||
| 2090 | + decoded.y, frame.reconstruction.y, | ||
| 2091 | + "{width}x{height} frame {i} luma" | ||
| 2092 | + ); | ||
| 2093 | + assert_eq!( | ||
| 2094 | + decoded.u, frame.reconstruction.u, | ||
| 2095 | + "{width}x{height} frame {i} U" | ||
| 2096 | + ); | ||
| 2097 | + assert_eq!( | ||
| 2098 | + decoded.v, frame.reconstruction.v, | ||
| 2099 | + "{width}x{height} frame {i} V" | ||
| 2100 | + ); | ||
| 2101 | + reference = decoded; | ||
| 2102 | + } | ||
| 2103 | + } | ||
| 2104 | + | ||
| 2105 | + #[test] | ||
| 2106 | + fn a_gop_round_trips_bit_exact_against_the_encoder_reconstruction() { | ||
| 2107 | + gop_round_trips(128, 64); | ||
| 2108 | + } | ||
| 2109 | + | ||
| 2110 | + /// Same claim, at a size that is not a whole number of 32x32 blocks | ||
| 2111 | + /// (216x96: 216/32 is not exact), so an inter frame's own 16x16-leaf | ||
| 2112 | + /// split path is exercised too. | ||
| 2113 | + #[test] | ||
| 2114 | + fn an_odd_size_gop_round_trips_bit_exact_against_the_encoder_reconstruction() { | ||
| 2115 | + gop_round_trips(216, 96); | ||
| 2116 | + } | ||
| 2117 | + | ||
| 2118 | + /// The GOP cross-oracle: ffmpeg decodes the whole sequence the same way | ||
| 2119 | + /// this module does, frame for frame. | ||
| 2120 | + #[test] | ||
| 2121 | + fn ffmpeg_and_this_decoder_agree_on_a_gop() { | ||
| 2122 | + if !have_ffmpeg() { | ||
| 2123 | + eprintln!("SKIP ffmpeg_and_this_decoder_agree_on_a_gop: no ffmpeg"); | ||
| 2124 | + return; | ||
| 2125 | + } | ||
| 2126 | + use crate::encode::encode_sequence; | ||
| 2127 | + let (width, height) = (128usize, 64usize); | ||
| 2128 | + let pictures: Vec<_> = (0..3) | ||
| 2129 | + .map(|i| panned_test_card(width, height, i * 3)) | ||
| 2130 | + .collect(); | ||
| 2131 | + let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); | ||
| 2132 | + let (coded_w, coded_h) = ffprobe_size(&encoded.stream); | ||
| 2133 | + let ffmpeg_frames = | ||
| 2134 | + ffmpeg_decode_sequence(&encoded.stream, coded_w as usize, coded_h as usize, 3); | ||
| 2135 | + | ||
| 2136 | + let key = &encoded.frames[0]; | ||
| 2137 | + let mut reference = decode_key_frame_tile( | ||
| 2138 | + &key.tile, | ||
| 2139 | + key.mi_cols, | ||
| 2140 | + key.mi_rows, | ||
| 2141 | + key.base_q_idx, | ||
| 2142 | + coded_w, | ||
| 2143 | + coded_h, | ||
| 2144 | + ) | ||
| 2145 | + .unwrap(); | ||
| 2146 | + assert_eq!(reference.y, ffmpeg_frames[0].y, "frame 0 luma vs ffmpeg"); | ||
| 2147 | + | ||
| 2148 | + for (i, frame) in encoded.frames.iter().enumerate().skip(1) { | ||
| 2149 | + let decoded = decode_inter_frame_tile( | ||
| 2150 | + &frame.tile, | ||
| 2151 | + frame.mi_cols, | ||
| 2152 | + frame.mi_rows, | ||
| 2153 | + frame.base_q_idx, | ||
| 2154 | + coded_w, | ||
| 2155 | + coded_h, | ||
| 2156 | + &reference, | ||
| 2157 | + ) | ||
| 2158 | + .unwrap(); | ||
| 2159 | + assert_eq!(decoded.y, ffmpeg_frames[i].y, "frame {i} luma vs ffmpeg"); | ||
| 2160 | + assert_eq!(decoded.u, ffmpeg_frames[i].u, "frame {i} U vs ffmpeg"); | ||
| 2161 | + assert_eq!(decoded.v, ffmpeg_frames[i].v, "frame {i} V vs ffmpeg"); | ||
| 2162 | + reference = decoded; | ||
| 2163 | + } | ||
| 2164 | + } | ||
| 1070 | } | 2165 | } |