Merge branch 'lane-av1cdef': the CDEF filter, ported and pixel-proven
Browse files at this commit2 changed files +428 -27
- crates/ec-av1/src/decode.rs +365 -3
- crates/ec-av1/src/stream.rs +63 -24
crates/ec-av1/src/decode.rsmodified+365 -3
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | //! too). Anything outside that refuses with [`Error::unsupported`] rather than |
| 22 | 22 | //! silently miscoding. |
| 23 | 23 | |
| 24 | +use ec_av1_syntax::CdefParams; | |
| 24 | 25 | use ec_core::{Error, Result}; |
| 25 | 26 | |
| 26 | 27 | use crate::cdf; |
| @@ -393,6 +394,15 @@ struct Neighbours { | ||
| 393 | 394 | left_inter: Vec<bool>, |
| 394 | 395 | mi_cols: usize, |
| 395 | 396 | mi_rows: usize, |
| 397 | + /// Per-8x8 (2x2 mi cells) `skip_txfm` flag over the whole padded frame, | |
| 398 | + /// spec `av1_cdef_compute_sb_list`'s `is_8x8_block_skip`: CDEF (spec | |
| 399 | + /// 7.15) never filters an 8x8 luma block whose covering coded block read | |
| 400 | + /// `skip` -- one flag per 4x4 mi cell (finer than needed, but matches the | |
| 401 | + /// grain [`Self::record_mi`] already writes at), read back at 8x8 | |
| 402 | + /// granularity by [`apply_cdef`] since `skip` is constant across every | |
| 403 | + /// mi cell one coded block covers. | |
| 404 | + skip_grid: Vec<bool>, | |
| 405 | + skip_grid_cols_mi: usize, | |
| 396 | 406 | } |
| 397 | 407 | |
| 398 | 408 | impl Neighbours { |
| @@ -414,9 +424,45 @@ impl Neighbours { | ||
| 414 | 424 | left_inter: vec![false; rows], |
| 415 | 425 | mi_cols, |
| 416 | 426 | mi_rows, |
| 427 | + skip_grid: vec![false; cols * (SUB / MI) * rows * (SUB / MI)], | |
| 428 | + skip_grid_cols_mi: cols * (SUB / MI), | |
| 417 | 429 | } |
| 418 | 430 | } |
| 419 | 431 | |
| 432 | + /// Marks every 4x4 mi cell a just-decoded coded block covers with its | |
| 433 | + /// `skip` flag -- `at_mi`/`side_mi` are the block's own position and | |
| 434 | + /// width/height in 4x4 mode-info units (mirroring [`Self::record_mi`]'s | |
| 435 | + /// own units, not [`Self::record`]'s [`SUB`]-grid ones). | |
| 436 | + fn fill_skip_grid(&mut self, at_mi: (usize, usize), side_mi: usize, skip: bool) { | |
| 437 | + let (mi_r, mi_c) = at_mi; | |
| 438 | + for rr in 0..side_mi { | |
| 439 | + for cc in 0..side_mi { | |
| 440 | + if let Some(cell) = self | |
| 441 | + .skip_grid | |
| 442 | + .get_mut((mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc)) | |
| 443 | + { | |
| 444 | + *cell = skip; | |
| 445 | + } | |
| 446 | + } | |
| 447 | + } | |
| 448 | + } | |
| 449 | + | |
| 450 | + /// Whether the 8x8 luma block at mi position `(mi_r, mi_c)` (2x2 mi | |
| 451 | + /// cells) was `skip_txfm` -- [`apply_cdef`]'s dlist membership test. | |
| 452 | + /// Spec/libaom `is_8x8_block_skip` requires *every* one of the four 4x4 | |
| 453 | + /// mi cells the 8x8 covers to be skip (a smaller-than-8x8 partition can | |
| 454 | + /// straddle the region with a mix of skip/non-skip blocks). | |
| 455 | + fn is_skip_txfm(&self, mi_r: usize, mi_c: usize) -> bool { | |
| 456 | + (0..2).all(|rr| { | |
| 457 | + (0..2).all(|cc| { | |
| 458 | + self.skip_grid | |
| 459 | + .get((mi_r + rr) * self.skip_grid_cols_mi + (mi_c + cc)) | |
| 460 | + .copied() | |
| 461 | + .unwrap_or(false) | |
| 462 | + }) | |
| 463 | + }) | |
| 464 | + } | |
| 465 | + | |
| 420 | 466 | fn start_row(&mut self) { |
| 421 | 467 | self.left.iter_mut().for_each(|l| *l = Default::default()); |
| 422 | 468 | self.left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| @@ -989,6 +1035,7 @@ fn decode_block( | ||
| 989 | 1035 | )?; |
| 990 | 1036 | } |
| 991 | 1037 | neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]); |
| 1038 | + neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip); | |
| 992 | 1039 | Ok(()) |
| 993 | 1040 | } |
| 994 | 1041 | |
| @@ -1125,9 +1172,300 @@ fn decode_leaf8( | ||
| 1125 | 1172 | )?; |
| 1126 | 1173 | } |
| 1127 | 1174 | neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); |
| 1175 | + neighbours.fill_skip_grid(leaf_mi, 2, skip); | |
| 1128 | 1176 | Ok(mode) |
| 1129 | 1177 | } |
| 1130 | 1178 | |
| 1179 | +/// Spec 7.15.3's `Cdef_Directions`: `(row, col)` offsets for the two primary | |
| 1180 | +/// taps of each of the eight directions; the secondary taps at `dir+2`/`dir-2` | |
| 1181 | +/// (mod 8) reuse the same table. | |
| 1182 | +const CDEF_DIRECTIONS: [[(i32, i32); 2]; 8] = [ | |
| 1183 | + [(-1, 1), (-2, 2)], | |
| 1184 | + [(0, 1), (-1, 2)], | |
| 1185 | + [(0, 1), (0, 2)], | |
| 1186 | + [(0, 1), (1, 2)], | |
| 1187 | + [(1, 1), (2, 2)], | |
| 1188 | + [(1, 0), (2, 1)], | |
| 1189 | + [(1, 0), (2, 0)], | |
| 1190 | + [(1, 0), (2, -1)], | |
| 1191 | +]; | |
| 1192 | +/// A neighbour tap outside the frame's true extent -- `constrain` reads this | |
| 1193 | +/// as "so far away it never contributes" (spec/libaom `CDEF_VERY_LARGE`). | |
| 1194 | +const CDEF_VERY_LARGE: i32 = 0x4000; | |
| 1195 | +const CDEF_PRI_TAPS: [[i32; 2]; 2] = [[4, 2], [3, 3]]; | |
| 1196 | +const CDEF_SEC_TAPS: [i32; 2] = [2, 1]; | |
| 1197 | + | |
| 1198 | +fn cdef_msb(x: i32) -> i32 { | |
| 1199 | + 31 - x.leading_zeros() as i32 | |
| 1200 | +} | |
| 1201 | + | |
| 1202 | +/// Spec 7.15.3's `constrain`: how much of a neighbour's difference from the | |
| 1203 | +/// centre sample a filter tap is allowed to contribute. | |
| 1204 | +fn cdef_constrain(diff: i32, threshold: i32, damping: i32) -> i32 { | |
| 1205 | + if threshold == 0 { | |
| 1206 | + return 0; | |
| 1207 | + } | |
| 1208 | + let shift = (damping - cdef_msb(threshold)).max(0); | |
| 1209 | + diff.signum() * (threshold - (diff.abs() >> shift)).clamp(0, diff.abs()) | |
| 1210 | +} | |
| 1211 | + | |
| 1212 | +/// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant | |
| 1213 | +/// direction of an 8x8 luma window and the variance gap between it and its | |
| 1214 | +/// orthogonal, `sample(row, col)` reading that window's own pixels. | |
| 1215 | +fn cdef_find_dir(sample: impl Fn(usize, usize) -> i32) -> (usize, i32) { | |
| 1216 | + const DIV_TABLE: [i64; 9] = [0, 840, 420, 280, 210, 168, 140, 120, 105]; | |
| 1217 | + let mut partial = [[0i64; 15]; 8]; | |
| 1218 | + for i in 0..8i64 { | |
| 1219 | + for j in 0..8i64 { | |
| 1220 | + let x = i64::from(sample(i as usize, j as usize)) - 128; | |
| 1221 | + partial[0][(i + j) as usize] += x; | |
| 1222 | + partial[1][(i + j / 2) as usize] += x; | |
| 1223 | + partial[2][i as usize] += x; | |
| 1224 | + partial[3][(3 + i - j / 2) as usize] += x; | |
| 1225 | + partial[4][(7 + i - j) as usize] += x; | |
| 1226 | + partial[5][(3 - i / 2 + j) as usize] += x; | |
| 1227 | + partial[6][j as usize] += x; | |
| 1228 | + partial[7][(i / 2 + j) as usize] += x; | |
| 1229 | + } | |
| 1230 | + } | |
| 1231 | + let mut cost = [0i64; 8]; | |
| 1232 | + for i in 0..8usize { | |
| 1233 | + cost[2] += partial[2][i] * partial[2][i]; | |
| 1234 | + cost[6] += partial[6][i] * partial[6][i]; | |
| 1235 | + } | |
| 1236 | + cost[2] *= DIV_TABLE[8]; | |
| 1237 | + cost[6] *= DIV_TABLE[8]; | |
| 1238 | + for i in 0..7usize { | |
| 1239 | + cost[0] += (partial[0][i] * partial[0][i] + partial[0][14 - i] * partial[0][14 - i]) | |
| 1240 | + * DIV_TABLE[i + 1]; | |
| 1241 | + cost[4] += (partial[4][i] * partial[4][i] + partial[4][14 - i] * partial[4][14 - i]) | |
| 1242 | + * DIV_TABLE[i + 1]; | |
| 1243 | + } | |
| 1244 | + cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8]; | |
| 1245 | + cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8]; | |
| 1246 | + for i in (1..8).step_by(2) { | |
| 1247 | + for j in 0..5usize { | |
| 1248 | + cost[i] += partial[i][3 + j] * partial[i][3 + j]; | |
| 1249 | + } | |
| 1250 | + cost[i] *= DIV_TABLE[8]; | |
| 1251 | + for j in 0..3usize { | |
| 1252 | + cost[i] += (partial[i][j] * partial[i][j] + partial[i][10 - j] * partial[i][10 - j]) | |
| 1253 | + * DIV_TABLE[2 * j + 2]; | |
| 1254 | + } | |
| 1255 | + } | |
| 1256 | + let mut best_cost = 0i64; | |
| 1257 | + let mut best_dir = 0usize; | |
| 1258 | + for (i, &c) in cost.iter().enumerate() { | |
| 1259 | + if c > best_cost { | |
| 1260 | + best_cost = c; | |
| 1261 | + best_dir = i; | |
| 1262 | + } | |
| 1263 | + } | |
| 1264 | + let var = (best_cost - cost[(best_dir + 4) & 7]) >> 10; | |
| 1265 | + (best_dir, var as i32) | |
| 1266 | +} | |
| 1267 | + | |
| 1268 | +/// Spec 7.15.2's per-8x8 strength adjustment: a strongly directional 8x8 | |
| 1269 | +/// (high variance gap) gets more deringing than a flat or non-directional | |
| 1270 | +/// one, only ever applied to the luma primary strength. | |
| 1271 | +fn cdef_adjust_strength(strength: i32, var: i32) -> i32 { | |
| 1272 | + if var == 0 { | |
| 1273 | + return 0; | |
| 1274 | + } | |
| 1275 | + let i = if var >> 6 != 0 { | |
| 1276 | + cdef_msb(var >> 6).min(12) | |
| 1277 | + } else { | |
| 1278 | + 0 | |
| 1279 | + }; | |
| 1280 | + (strength * (4 + i) + 8) >> 4 | |
| 1281 | +} | |
| 1282 | + | |
| 1283 | +/// Spec 7.15.3's `cdef_filter_block`: filters one block (8x8 luma, 4x4 4:2:0 | |
| 1284 | +/// chroma) in place, `sample(row, col)` reading the *unfiltered* frame (a | |
| 1285 | +/// previously-filtered neighbour must never feed this block's own sum). | |
| 1286 | +#[allow(clippy::too_many_arguments)] | |
| 1287 | +fn cdef_filter_block( | |
| 1288 | + sample: impl Fn(i32, i32) -> i32, | |
| 1289 | + dst: &mut PlaneBuf, | |
| 1290 | + ox: usize, | |
| 1291 | + oy: usize, | |
| 1292 | + bw: usize, | |
| 1293 | + bh: usize, | |
| 1294 | + pri_strength: i32, | |
| 1295 | + sec_strength: i32, | |
| 1296 | + dir: usize, | |
| 1297 | + pri_damping: i32, | |
| 1298 | + sec_damping: i32, | |
| 1299 | + enable_primary: bool, | |
| 1300 | + enable_secondary: bool, | |
| 1301 | +) { | |
| 1302 | + let clipping_required = enable_primary && enable_secondary; | |
| 1303 | + let pri_taps = CDEF_PRI_TAPS[(pri_strength & 1) as usize]; | |
| 1304 | + for i in 0..bh { | |
| 1305 | + for j in 0..bw { | |
| 1306 | + let x = sample(i as i32, j as i32); | |
| 1307 | + let mut sum = 0i32; | |
| 1308 | + let mut max = x; | |
| 1309 | + let mut min = x; | |
| 1310 | + for k in 0..2usize { | |
| 1311 | + if enable_primary { | |
| 1312 | + let (tr, tc) = CDEF_DIRECTIONS[dir][k]; | |
| 1313 | + let p0 = sample(i as i32 + tr, j as i32 + tc); | |
| 1314 | + let p1 = sample(i as i32 - tr, j as i32 - tc); | |
| 1315 | + sum += pri_taps[k] * cdef_constrain(p0 - x, pri_strength, pri_damping); | |
| 1316 | + sum += pri_taps[k] * cdef_constrain(p1 - x, pri_strength, pri_damping); | |
| 1317 | + if clipping_required { | |
| 1318 | + if p0 != CDEF_VERY_LARGE { | |
| 1319 | + max = max.max(p0); | |
| 1320 | + } | |
| 1321 | + if p1 != CDEF_VERY_LARGE { | |
| 1322 | + max = max.max(p1); | |
| 1323 | + } | |
| 1324 | + min = min.min(p0).min(p1); | |
| 1325 | + } | |
| 1326 | + } | |
| 1327 | + if enable_secondary { | |
| 1328 | + let (tr0, tc0) = CDEF_DIRECTIONS[(dir + 2) & 7][k]; | |
| 1329 | + let (tr1, tc1) = CDEF_DIRECTIONS[(dir + 6) & 7][k]; | |
| 1330 | + let s0 = sample(i as i32 + tr0, j as i32 + tc0); | |
| 1331 | + let s1 = sample(i as i32 - tr0, j as i32 - tc0); | |
| 1332 | + let s2 = sample(i as i32 + tr1, j as i32 + tc1); | |
| 1333 | + let s3 = sample(i as i32 - tr1, j as i32 - tc1); | |
| 1334 | + if clipping_required { | |
| 1335 | + for s in [s0, s1, s2, s3] { | |
| 1336 | + if s != CDEF_VERY_LARGE { | |
| 1337 | + max = max.max(s); | |
| 1338 | + } | |
| 1339 | + } | |
| 1340 | + min = min.min(s0).min(s1).min(s2).min(s3); | |
| 1341 | + } | |
| 1342 | + sum += CDEF_SEC_TAPS[k] | |
| 1343 | + * (cdef_constrain(s0 - x, sec_strength, sec_damping) | |
| 1344 | + + cdef_constrain(s1 - x, sec_strength, sec_damping) | |
| 1345 | + + cdef_constrain(s2 - x, sec_strength, sec_damping) | |
| 1346 | + + cdef_constrain(s3 - x, sec_strength, sec_damping)); | |
| 1347 | + } | |
| 1348 | + } | |
| 1349 | + let mut y = x + ((8 + sum - i32::from(sum < 0)) >> 4); | |
| 1350 | + if clipping_required { | |
| 1351 | + y = y.clamp(min, max); | |
| 1352 | + } | |
| 1353 | + dst.data[(oy + i) * dst.width + (ox + j)] = y.clamp(0, 255) as u8; | |
| 1354 | + } | |
| 1355 | + } | |
| 1356 | +} | |
| 1357 | + | |
| 1358 | +/// Applies CDEF (spec 7.15) to the whole reconstructed frame, over its true | |
| 1359 | +/// (unpadded) `mi_cols`/`mi_rows` extent, before it is cropped to its display | |
| 1360 | +/// size. `cdef_bits == 0` is this crate's only supported case so far (see the | |
| 1361 | +/// refusal in `stream.rs`), so every 64x64 filter block always uses index 0's | |
| 1362 | +/// strengths -- no per-block `cdef_idx` selection is implemented yet. | |
| 1363 | +fn apply_cdef( | |
| 1364 | + y: &mut PlaneBuf, | |
| 1365 | + u: &mut PlaneBuf, | |
| 1366 | + v: &mut PlaneBuf, | |
| 1367 | + cdef: &CdefParams, | |
| 1368 | + skip_grid: &Neighbours, | |
| 1369 | +) { | |
| 1370 | + if cdef.y_pri_strength[0] == 0 | |
| 1371 | + && cdef.y_sec_strength[0] == 0 | |
| 1372 | + && cdef.uv_pri_strength[0] == 0 | |
| 1373 | + && cdef.uv_sec_strength[0] == 0 | |
| 1374 | + { | |
| 1375 | + return; | |
| 1376 | + } | |
| 1377 | + let src_y = y.data.clone(); | |
| 1378 | + let src_u = u.data.clone(); | |
| 1379 | + let src_v = v.data.clone(); | |
| 1380 | + let damping = i32::from(cdef.damping); | |
| 1381 | + let damping_uv = (damping - 1).max(0); | |
| 1382 | + let mut mi_r = 0usize; | |
| 1383 | + while mi_r < skip_grid.mi_rows { | |
| 1384 | + let mut mi_c = 0usize; | |
| 1385 | + while mi_c < skip_grid.mi_cols { | |
| 1386 | + if !skip_grid.is_skip_txfm(mi_r, mi_c) { | |
| 1387 | + let (ox, oy) = (mi_c * 4, mi_r * 4); | |
| 1388 | + let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height); | |
| 1389 | + let sample_y = |r: i32, c: i32| -> i32 { | |
| 1390 | + let (ny, nx) = (oy as i32 + r, ox as i32 + c); | |
| 1391 | + if ny < 0 || nx < 0 || ny as usize >= y_true_h || nx as usize >= y_true_w { | |
| 1392 | + CDEF_VERY_LARGE | |
| 1393 | + } else { | |
| 1394 | + i32::from(src_y[ny as usize * y_stride + nx as usize]) | |
| 1395 | + } | |
| 1396 | + }; | |
| 1397 | + let (dir, var) = cdef_find_dir(|r, c| { | |
| 1398 | + let (ny, nx) = ((oy + r).min(y_true_h - 1), (ox + c).min(y_true_w - 1)); | |
| 1399 | + i32::from(src_y[ny * y_stride + nx]) | |
| 1400 | + }); | |
| 1401 | + | |
| 1402 | + // libaom `av1_cdef_filter_fb` passes `pri_strength ? dir : 0` | |
| 1403 | + // -- each plane zeroes the shared direction when *its own* | |
| 1404 | + // frame-level primary strength is 0, independent of the | |
| 1405 | + // per-block adjusted `t` and of the other plane's strength. | |
| 1406 | + let y_dir = if cdef.y_pri_strength[0] != 0 { dir } else { 0 }; | |
| 1407 | + let t = cdef_adjust_strength(i32::from(cdef.y_pri_strength[0]), var); | |
| 1408 | + let enable_primary = t != 0; | |
| 1409 | + let enable_secondary = cdef.y_sec_strength[0] != 0; | |
| 1410 | + if enable_primary || enable_secondary { | |
| 1411 | + cdef_filter_block( | |
| 1412 | + sample_y, | |
| 1413 | + y, | |
| 1414 | + ox, | |
| 1415 | + oy, | |
| 1416 | + 8, | |
| 1417 | + 8, | |
| 1418 | + t, | |
| 1419 | + i32::from(cdef.y_sec_strength[0]), | |
| 1420 | + y_dir, | |
| 1421 | + damping, | |
| 1422 | + damping, | |
| 1423 | + enable_primary, | |
| 1424 | + enable_secondary, | |
| 1425 | + ); | |
| 1426 | + } | |
| 1427 | + | |
| 1428 | + let t_uv = i32::from(cdef.uv_pri_strength[0]); | |
| 1429 | + let uv_dir = if t_uv != 0 { dir } else { 0 }; | |
| 1430 | + let enable_primary_uv = t_uv != 0; | |
| 1431 | + let enable_secondary_uv = cdef.uv_sec_strength[0] != 0; | |
| 1432 | + if enable_primary_uv || enable_secondary_uv { | |
| 1433 | + let (cox, coy) = (ox / 2, oy / 2); | |
| 1434 | + for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] { | |
| 1435 | + let (tw, th) = (plane.true_width, plane.true_height); | |
| 1436 | + let stride = plane.width; | |
| 1437 | + let sample_uv = |r: i32, c: i32| -> i32 { | |
| 1438 | + let (ny, nx) = (coy as i32 + r, cox as i32 + c); | |
| 1439 | + if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw { | |
| 1440 | + CDEF_VERY_LARGE | |
| 1441 | + } else { | |
| 1442 | + i32::from(src_p[ny as usize * stride + nx as usize]) | |
| 1443 | + } | |
| 1444 | + }; | |
| 1445 | + cdef_filter_block( | |
| 1446 | + sample_uv, | |
| 1447 | + plane, | |
| 1448 | + cox, | |
| 1449 | + coy, | |
| 1450 | + 4, | |
| 1451 | + 4, | |
| 1452 | + t_uv, | |
| 1453 | + i32::from(cdef.uv_sec_strength[0]), | |
| 1454 | + uv_dir, | |
| 1455 | + damping_uv, | |
| 1456 | + damping_uv, | |
| 1457 | + enable_primary_uv, | |
| 1458 | + enable_secondary_uv, | |
| 1459 | + ); | |
| 1460 | + } | |
| 1461 | + } | |
| 1462 | + } | |
| 1463 | + mi_c += 2; | |
| 1464 | + } | |
| 1465 | + mi_r += 2; | |
| 1466 | + } | |
| 1467 | +} | |
| 1468 | + | |
| 1131 | 1469 | /// Decodes the payload [`crate::tile::sb_coeff_key_frame_tile`] writes, |
| 1132 | 1470 | /// returning the picture it reconstructs to. |
| 1133 | 1471 | /// |
| @@ -1154,6 +1492,7 @@ pub fn decode_key_frame_tile( | ||
| 1154 | 1492 | frame_width: u32, |
| 1155 | 1493 | frame_height: u32, |
| 1156 | 1494 | enable_filter_intra: bool, |
| 1495 | + cdef: &CdefParams, | |
| 1157 | 1496 | ) -> Result<Picture> { |
| 1158 | 1497 | if mi_cols == 0 || mi_rows == 0 { |
| 1159 | 1498 | return Err(unsupported("a frame with no mode-info grid")); |
| @@ -1488,6 +1827,8 @@ pub fn decode_key_frame_tile( | ||
| 1488 | 1827 | } |
| 1489 | 1828 | } |
| 1490 | 1829 | |
| 1830 | + apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); | |
| 1831 | + | |
| 1491 | 1832 | let (fw, fh) = (frame_width as usize, frame_height as usize); |
| 1492 | 1833 | if fw == width && fh == height { |
| 1493 | 1834 | return Ok(Picture { |
| @@ -2008,6 +2349,7 @@ fn decode_inter_block( | ||
| 2008 | 2349 | } |
| 2009 | 2350 | neighbours.record(at, side, mode_for_tx, &[luma_grid, u_grid, v_grid]); |
| 2010 | 2351 | neighbours.record_inter(at, side, skip, is_inter); |
| 2352 | + neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip); | |
| 2011 | 2353 | Ok(()) |
| 2012 | 2354 | } |
| 2013 | 2355 | |
| @@ -2032,6 +2374,7 @@ pub fn decode_inter_frame_tile( | ||
| 2032 | 2374 | frame_width: u32, |
| 2033 | 2375 | frame_height: u32, |
| 2034 | 2376 | reference: &Picture, |
| 2377 | + cdef: &CdefParams, | |
| 2035 | 2378 | ) -> Result<Picture> { |
| 2036 | 2379 | if mi_cols == 0 || mi_rows == 0 { |
| 2037 | 2380 | return Err(unsupported("a frame with no mode-info grid")); |
| @@ -2237,6 +2580,8 @@ pub fn decode_inter_frame_tile( | ||
| 2237 | 2580 | } |
| 2238 | 2581 | } |
| 2239 | 2582 | |
| 2583 | + apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); | |
| 2584 | + | |
| 2240 | 2585 | let (fw, fh) = (frame_width as usize, frame_height as usize); |
| 2241 | 2586 | if fw == width && fh == height { |
| 2242 | 2587 | return Ok(Picture { |
| @@ -2279,7 +2624,10 @@ mod tests { | ||
| 2279 | 2624 | |
| 2280 | 2625 | #[test] |
| 2281 | 2626 | fn a_frame_with_no_mode_info_grid_is_refused() { |
| 2282 | - assert!(decode_key_frame_tile(&[0u8; 4], 0, 32, 32, 0, 128, false).is_err()); | |
| 2627 | + assert!( | |
| 2628 | + decode_key_frame_tile(&[0u8; 4], 0, 32, 32, 0, 128, false, &CdefParams::default()) | |
| 2629 | + .is_err() | |
| 2630 | + ); | |
| 2283 | 2631 | } |
| 2284 | 2632 | |
| 2285 | 2633 | /// Encodes and decodes one picture with `modes`, asserting the decoder's |
| @@ -2308,7 +2656,14 @@ mod tests { | ||
| 2308 | 2656 | .. |
| 2309 | 2657 | }: Encoded = crate::encode::encode_key_frame_with_modes(&picture, 40, 0.0, modes).unwrap(); |
| 2310 | 2658 | let decoded = decode_key_frame_tile( |
| 2311 | - &tile, mi_cols, mi_rows, base_q_idx, w as u32, h as u32, false, | |
| 2659 | + &tile, | |
| 2660 | + mi_cols, | |
| 2661 | + mi_rows, | |
| 2662 | + base_q_idx, | |
| 2663 | + w as u32, | |
| 2664 | + h as u32, | |
| 2665 | + false, | |
| 2666 | + &CdefParams::default(), | |
| 2312 | 2667 | ) |
| 2313 | 2668 | .unwrap(); |
| 2314 | 2669 | assert_eq!(decoded.width, w, "{w}x{h}: decoded width"); |
| @@ -2380,7 +2735,9 @@ mod tests { | ||
| 2380 | 2735 | ); |
| 2381 | 2736 | enc.symbol(DC_PRED, &mut cdfs.uv_mode_no_cfl[DC_PRED]); |
| 2382 | 2737 | let data = enc.finish(); |
| 2383 | - let decoded = decode_key_frame_tile(&data, 16, 16, 40, 64, 64, false).unwrap(); | |
| 2738 | + let decoded = | |
| 2739 | + decode_key_frame_tile(&data, 16, 16, 40, 64, 64, false, &CdefParams::default()) | |
| 2740 | + .unwrap(); | |
| 2384 | 2741 | assert!( |
| 2385 | 2742 | decoded.y.iter().all(|&s| s == 128), |
| 2386 | 2743 | "a skipped DC_PRED block with no neighbours predicts flat mid-grey" |
| @@ -2560,6 +2917,7 @@ mod tests { | ||
| 2560 | 2917 | coded_w, |
| 2561 | 2918 | coded_h, |
| 2562 | 2919 | false, |
| 2920 | + &CdefParams::default(), | |
| 2563 | 2921 | ) |
| 2564 | 2922 | .unwrap(); |
| 2565 | 2923 | assert_eq!(ours.y, ffmpeg_decoded.y, "{width}x{height}: luma vs ffmpeg"); |
| @@ -2628,6 +2986,7 @@ mod tests { | ||
| 2628 | 2986 | width as u32, |
| 2629 | 2987 | height as u32, |
| 2630 | 2988 | false, |
| 2989 | + &CdefParams::default(), | |
| 2631 | 2990 | ) |
| 2632 | 2991 | .unwrap(); |
| 2633 | 2992 | assert_eq!( |
| @@ -2652,6 +3011,7 @@ mod tests { | ||
| 2652 | 3011 | width as u32, |
| 2653 | 3012 | height as u32, |
| 2654 | 3013 | &reference, |
| 3014 | + &CdefParams::default(), | |
| 2655 | 3015 | ) |
| 2656 | 3016 | .unwrap(); |
| 2657 | 3017 | assert_eq!( |
| @@ -2895,6 +3255,7 @@ mod tests { | ||
| 2895 | 3255 | coded_w, |
| 2896 | 3256 | coded_h, |
| 2897 | 3257 | false, |
| 3258 | + &CdefParams::default(), | |
| 2898 | 3259 | ) |
| 2899 | 3260 | .unwrap(); |
| 2900 | 3261 | assert_eq!(reference.y, ffmpeg_frames[0].y, "frame 0 luma vs ffmpeg"); |
| @@ -2908,6 +3269,7 @@ mod tests { | ||
| 2908 | 3269 | coded_w, |
| 2909 | 3270 | coded_h, |
| 2910 | 3271 | &reference, |
| 3272 | + &CdefParams::default(), | |
| 2911 | 3273 | ) |
| 2912 | 3274 | .unwrap(); |
| 2913 | 3275 | assert_eq!(decoded.y, ffmpeg_frames[i].y, "frame {i} luma vs ffmpeg"); |
crates/ec-av1/src/stream.rsmodified+63 -24
| @@ -10,27 +10,12 @@ | ||
| 10 | 10 | //! are `pub(crate)`, and rightly so: the wire is `stream`, everything else is |
| 11 | 11 | //! an implementation detail of how this crate happens to build it). |
| 12 | 12 | |
| 13 | -use ec_av1_syntax::{Av1Parser, CdefParams, FrameType, ObuKind, TxMode}; | |
| 13 | +use ec_av1_syntax::{Av1Parser, FrameType, ObuKind, TxMode}; | |
| 14 | 14 | use ec_core::{Error, Result}; |
| 15 | 15 | |
| 16 | 16 | use crate::decode::{decode_inter_frame_tile, decode_key_frame_tile}; |
| 17 | 17 | use crate::encode::Picture; |
| 18 | 18 | |
| 19 | -/// Whether a frame's CDEF parameters would actually filter any samples — | |
| 20 | -/// `1 << bits` strength pairs are declared, but only the used ones (any | |
| 21 | -/// non-zero primary/secondary strength) matter; `read_cdef_params` already | |
| 22 | -/// zeroes every field when CDEF is off for the frame (lossless, intrabc, or | |
| 23 | -/// the sequence header disabling it), so a plain field check is enough. | |
| 24 | -fn cdef_is_active(cdef: &CdefParams) -> bool { | |
| 25 | - let n = 1usize << cdef.bits; | |
| 26 | - (0..n).any(|i| { | |
| 27 | - cdef.y_pri_strength[i] != 0 | |
| 28 | - || cdef.y_sec_strength[i] != 0 | |
| 29 | - || cdef.uv_pri_strength[i] != 0 | |
| 30 | - || cdef.uv_sec_strength[i] != 0 | |
| 31 | - }) | |
| 32 | -} | |
| 33 | - | |
| 34 | 19 | /// Decode every frame in a raw AV1 OBU stream, in coding order. |
| 35 | 20 | /// |
| 36 | 21 | /// A key frame becomes the reference for the inter frames that follow it, |
| @@ -63,16 +48,19 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 63 | 48 | let tile = tiles.first().ok_or_else(|| { |
| 64 | 49 | Error::unsupported("AV1 decode_stream", "a frame OBU with no tile group") |
| 65 | 50 | })?; |
| 66 | - // Neither tile decoder applies the in-loop CDEF filter (spec 7.15): | |
| 67 | - // its `cdef_idx` syntax needs no bits when `cdef_bits == 0` (this | |
| 68 | - // crate's own writer's only case), so a real libaom stream with an | |
| 69 | - // active strength decodes with the bitstream in sync but the wrong | |
| 70 | - // pixels — decodes-but-wrong, worse than a refusal. Refuse before | |
| 71 | - // that happens rather than let it through silently. | |
| 72 | - if cdef_is_active(&header.cdef) { | |
| 51 | + // `read_cdef` (spec `decodeframe.c`, called at the first non-skip | |
| 52 | + // block of each 64x64) only reads a literal `cdef_idx` when | |
| 53 | + // `cdef_bits > 0`; at `cdef_bits == 0` there is nothing to read (this | |
| 54 | + // crate's own writer's only case, and both `lane-av1cdef` gate | |
| 55 | + // streams'), so the per-block symbol this crate's tile readers never | |
| 56 | + // consume is a true no-op there. A `cdef_bits > 0` stream would | |
| 57 | + // desync the moment it hit a real strength selector this decoder | |
| 58 | + // never reads — refuse that by name rather than silently miscode, | |
| 59 | + // the same pattern as the other round-2 refusals below. | |
| 60 | + if header.cdef.bits != 0 { | |
| 73 | 61 | return Err(Error::unsupported( |
| 74 | 62 | "AV1 decode_stream", |
| 75 | - "a frame with an active CDEF filter (not yet applied by this decoder)", | |
| 63 | + "a frame with cdef_bits > 0 (this decoder never reads the per-64x64 cdef_idx symbol)", | |
| 76 | 64 | )); |
| 77 | 65 | } |
| 78 | 66 | // Every `decode_block` call below assumes the block's transform is |
| @@ -139,6 +127,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 139 | 127 | header.frame_width, |
| 140 | 128 | header.frame_height, |
| 141 | 129 | enable_filter_intra, |
| 130 | + &header.cdef, | |
| 142 | 131 | )? |
| 143 | 132 | } else { |
| 144 | 133 | let reference = reference.as_ref().ok_or_else(|| { |
| @@ -155,6 +144,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 155 | 144 | header.frame_width, |
| 156 | 145 | header.frame_height, |
| 157 | 146 | reference, |
| 147 | + &header.cdef, | |
| 158 | 148 | )? |
| 159 | 149 | }; |
| 160 | 150 | reference = Some(picture.clone()); |
| @@ -221,6 +211,7 @@ mod tests { | ||
| 221 | 211 | width as u32, |
| 222 | 212 | height as u32, |
| 223 | 213 | false, |
| 214 | + &ec_av1_syntax::CdefParams::default(), | |
| 224 | 215 | ) |
| 225 | 216 | .unwrap(); |
| 226 | 217 | let via_stream = decode_stream(&encoded.stream).unwrap(); |
| @@ -578,6 +569,54 @@ mod tests { | ||
| 578 | 569 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); |
| 579 | 570 | } |
| 580 | 571 | |
| 572 | + /// As [`a_real_libaom_gradients_stream_decodes_pixel_exact`], but leaves | |
| 573 | + /// CDEF *on* (no `-enable-cdef 0`) -- the whole point of `lane-av1cdef`: | |
| 574 | + /// proves `apply_cdef` itself, not just that this decoder still works | |
| 575 | + /// when the filter never fires. Gradient content at a low crf reliably | |
| 576 | + /// gives libaom something to dering. If a run happens to land on | |
| 577 | + /// `cdef_bits > 0` (per-64x64 `cdef_idx`, still unimplemented -- see the | |
| 578 | + /// refusal in `decode_stream`) the stream is skipped rather than failed: | |
| 579 | + /// that gap is named by the refusal's own error text, not silently | |
| 580 | + /// swallowed here. | |
| 581 | + #[test] | |
| 582 | + fn a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact() { | |
| 583 | + if !have_ffmpeg() { | |
| 584 | + eprintln!( | |
| 585 | + "SKIP a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact: no ffmpeg" | |
| 586 | + ); | |
| 587 | + return; | |
| 588 | + } | |
| 589 | + let (width, height) = (64, 64); | |
| 590 | + let stream = libaom_encode_with( | |
| 591 | + "gradients=size=64x64:c0=red:c1=blue:c2=green:rate=1:seed=42", | |
| 592 | + width, | |
| 593 | + height, | |
| 594 | + 30, | |
| 595 | + &[ | |
| 596 | + "-threads", | |
| 597 | + "1", | |
| 598 | + "-row-mt", | |
| 599 | + "0", | |
| 600 | + "-tile-columns", | |
| 601 | + "0", | |
| 602 | + "-tile-rows", | |
| 603 | + "0", | |
| 604 | + ], | |
| 605 | + ) | |
| 606 | + .expect("ffmpeg encode"); | |
| 607 | + match decode_stream(&stream) { | |
| 608 | + Ok(frames) => { | |
| 609 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); | |
| 610 | + assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg (CDEF on)"); | |
| 611 | + assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg (CDEF on)"); | |
| 612 | + assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (CDEF on)"); | |
| 613 | + } | |
| 614 | + Err(e) => { | |
| 615 | + eprintln!("SKIP a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact: {e}"); | |
| 616 | + } | |
| 617 | + } | |
| 618 | + } | |
| 619 | + | |
| 581 | 620 | /// scratch: isolate a pinned mismatching stream's first divergent pixel. |
| 582 | 621 | #[test] |
| 583 | 622 | #[ignore] |