One film-grain kernel call per block, no scalar remainder
Browse files at this commit12e1d49a57b2a6a36f4784859241daa40e00d660Parent: 2426e3d- perf
- one film-grain kernel call per block, no scalar remainder
1 changed file +137 -50
- crates/ec-av1/src/film_grain.rs +137 -50
crates/ec-av1/src/film_grain.rsmodified+137 -50
| @@ -1098,6 +1098,42 @@ fn uninit_plane(n: usize) -> Vec<u16> { | |||
| 1098 | /// apply film grain to `picture`, returning a new grained picture. `picture` | 1098 | /// apply film grain to `picture`, returning a new grained picture. `picture` |
| 1099 | /// itself is unchanged -- it stays the clean reference-frame-bank copy. | 1099 | /// itself is unchanged -- it stays the clean reference-frame-bank copy. |
| 1100 | /// Returns `picture.clone()` untouched when `apply_grain` is unset. | 1100 | /// Returns `picture.clone()` untouched when `apply_grain` is unset. |
| 1101 | +/// lane-grainrow: one block's noise, gathered from the three sources spec | ||
| 1102 | +/// 7.18.3.5 reads for it -- the row above's blended line buffer (its `top` | ||
| 1103 | +/// rows), the column to the left's blended column buffer (`c_wide` columns) | ||
| 1104 | +/// and the grain template for the rest. `rows`/`cols` are the block's own | ||
| 1105 | +/// extent, already clipped at the picture edge by the caller. | ||
| 1106 | +#[allow(clippy::too_many_arguments)] | ||
| 1107 | +fn assemble_block_noise( | ||
| 1108 | + dst: &mut [i32], | ||
| 1109 | + d_stride: usize, | ||
| 1110 | + rows: usize, | ||
| 1111 | + cols: usize, | ||
| 1112 | + line: &[i32], | ||
| 1113 | + l_stride: usize, | ||
| 1114 | + l_col0: usize, | ||
| 1115 | + top: usize, | ||
| 1116 | + col: &[i32], | ||
| 1117 | + c_wide: usize, | ||
| 1118 | + tmpl: &[i32], | ||
| 1119 | + t_stride: usize, | ||
| 1120 | + t_row0: usize, | ||
| 1121 | + t_col0: usize, | ||
| 1122 | +) { | ||
| 1123 | + for r in 0..rows { | ||
| 1124 | + let d = &mut dst[r * d_stride..r * d_stride + cols]; | ||
| 1125 | + if r < top { | ||
| 1126 | + let o = r * l_stride + l_col0; | ||
| 1127 | + d.copy_from_slice(&line[o..o + cols]); | ||
| 1128 | + } else { | ||
| 1129 | + let o = (t_row0 + r) * t_stride + t_col0; | ||
| 1130 | + d.copy_from_slice(&tmpl[o..o + cols]); | ||
| 1131 | + let w = c_wide.min(cols); | ||
| 1132 | + d[..w].copy_from_slice(&col[r * c_wide..r * c_wide + w]); | ||
| 1133 | + } | ||
| 1134 | + } | ||
| 1135 | +} | ||
| 1136 | + | ||
| 1101 | pub(crate) fn apply_grain( | 1137 | pub(crate) fn apply_grain( |
| 1102 | picture: &Picture, | 1138 | picture: &Picture, |
| 1103 | fg: &FilmGrainParams, | 1139 | fg: &FilmGrainParams, |
| @@ -1151,6 +1187,9 @@ pub(crate) fn apply_grain( | |||
| 1151 | let scaling_cr = expand_scaling_lut(&scaling_cr, expand_bd); | 1187 | let scaling_cr = expand_scaling_lut(&scaling_cr, expand_bd); |
| 1152 | 1188 | ||
| 1153 | let overlap = fg.overlap_flag; | 1189 | let overlap = fg.overlap_flag; |
| 1190 | + let apply_y = fg.num_y_points > 0; | ||
| 1191 | + let apply_cb = fg.num_cb_points > 0 || fg.chroma_scaling_from_luma; | ||
| 1192 | + let apply_cr = fg.num_cr_points > 0 || fg.chroma_scaling_from_luma; | ||
| 1154 | 1193 | ||
| 1155 | // lane-filt1: 32-pixel block ROWS are the parallel axis. A row's noise | 1194 | // lane-filt1: 32-pixel block ROWS are the parallel axis. A row's noise |
| 1156 | // offsets come from `Rng::line(y * 2, grain_seed)` -- a function of the | 1195 | // offsets come from `Rng::line(y * 2, grain_seed)` -- a function of the |
| @@ -1215,6 +1254,9 @@ pub(crate) fn apply_grain( | |||
| 1215 | let mut y_col_buf = vec![0i32; ((LUMA_SUBBLOCK + 2) * 2) as usize]; | 1254 | let mut y_col_buf = vec![0i32; ((LUMA_SUBBLOCK + 2) * 2) as usize]; |
| 1216 | let mut cb_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | 1255 | let mut cb_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; |
| 1217 | let mut cr_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | 1256 | let mut cr_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; |
| 1257 | + let mut y_blk = vec![0i32; (LUMA_SUBBLOCK * LUMA_SUBBLOCK) as usize]; | ||
| 1258 | + let mut cb_blk = vec![0i32; (CHROMA_SUBBLOCK * CHROMA_SUBBLOCK) as usize]; | ||
| 1259 | + let mut cr_blk = vec![0i32; (CHROMA_SUBBLOCK * CHROMA_SUBBLOCK) as usize]; | ||
| 1218 | 1260 | ||
| 1219 | let mut y = y_first; | 1261 | let mut y = y_first; |
| 1220 | while y < y_hi { | 1262 | while y < y_hi { |
| @@ -1281,30 +1323,6 @@ pub(crate) fn apply_grain( | |||
| 1281 | hc_count, fctx, | 1323 | hc_count, fctx, |
| 1282 | ); | 1324 | ); |
| 1283 | 1325 | ||
| 1284 | - let i = if y != 0 { 1 } else { 0 }; | ||
| 1285 | - if !prelude { | ||
| 1286 | - add_noise_to_block( | ||
| 1287 | - fg, | ||
| 1288 | - mc_identity, | ||
| 1289 | - &scaling_y, | ||
| 1290 | - &scaling_cb, | ||
| 1291 | - &scaling_cr, | ||
| 1292 | - &mut *out, | ||
| 1293 | - (y + i) << 1, | ||
| 1294 | - x << 1, | ||
| 1295 | - &y_col_buf, | ||
| 1296 | - 2, | ||
| 1297 | - i * 2, | ||
| 1298 | - 0, | ||
| 1299 | - &cb_col_buf, | ||
| 1300 | - &cr_col_buf, | ||
| 1301 | - 1, | ||
| 1302 | - i, | ||
| 1303 | - 0, | ||
| 1304 | - (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, | ||
| 1305 | - 1, fctx, | ||
| 1306 | - ); | ||
| 1307 | - } | ||
| 1308 | } | 1326 | } |
| 1309 | 1327 | ||
| 1310 | if overlap && y != 0 { | 1328 | if overlap && y != 0 { |
| @@ -1387,7 +1405,14 @@ pub(crate) fn apply_grain( | |||
| 1387 | 1, fctx, | 1405 | 1, fctx, |
| 1388 | ); | 1406 | ); |
| 1389 | 1407 | ||
| 1408 | + } | ||
| 1409 | + | ||
| 1410 | + let i = if overlap && y != 0 { 1 } else { 0 }; | ||
| 1411 | + let j = if overlap && x != 0 { 1 } else { 0 }; | ||
| 1390 | if !prelude { | 1412 | if !prelude { |
| 1413 | + let bh = (LUMA_SUBBLOCK >> 1).min(height / 2 - y); | ||
| 1414 | + let bw = (LUMA_SUBBLOCK >> 1).min(width / 2 - x); | ||
| 1415 | + if i == 0 && j == 0 { | ||
| 1391 | add_noise_to_block( | 1416 | add_noise_to_block( |
| 1392 | fg, | 1417 | fg, |
| 1393 | mc_identity, | 1418 | mc_identity, |
| @@ -1397,24 +1422,85 @@ pub(crate) fn apply_grain( | |||
| 1397 | &mut *out, | 1422 | &mut *out, |
| 1398 | y << 1, | 1423 | y << 1, |
| 1399 | x << 1, | 1424 | x << 1, |
| 1425 | + &luma_template, | ||
| 1426 | + LUMA_BLOCK_W, | ||
| 1427 | + luma_off_y, | ||
| 1428 | + luma_off_x, | ||
| 1429 | + &cb_template, | ||
| 1430 | + &cr_template, | ||
| 1431 | + CHROMA_BLOCK_W, | ||
| 1432 | + chroma_off_y, | ||
| 1433 | + chroma_off_x, | ||
| 1434 | + bh, | ||
| 1435 | + bw, fctx, | ||
| 1436 | + ); | ||
| 1437 | + } else { | ||
| 1438 | + // lane-grainrow: the overlap line and the overlap | ||
| 1439 | + // column used to add their noise in calls of their own | ||
| 1440 | + // -- the column 1 chroma sample wide (one scalar call | ||
| 1441 | + // per row), and the block that was left 15 wide, whose | ||
| 1442 | + // AVX2 kernel handed the 3-sample tail of every row | ||
| 1443 | + // back to the scalar loop. A 4K cut spent 2.19e9 calls | ||
| 1444 | + // on 4.4e9 such samples. Assembling the three noise | ||
| 1445 | + // sources into one 32x32 block (16x16 chroma) first | ||
| 1446 | + // makes it one kernel call per block over a width that | ||
| 1447 | + // is a multiple of 8, with no scalar remainder. Every | ||
| 1448 | + // sample keeps the value its own source gave it, so the | ||
| 1449 | + // picture is bit-identical. | ||
| 1450 | + if apply_y { | ||
| 1451 | + assemble_block_noise( | ||
| 1452 | + &mut y_blk, | ||
| 1453 | + LUMA_SUBBLOCK as usize, | ||
| 1454 | + (bh * 2) as usize, | ||
| 1455 | + (bw * 2) as usize, | ||
| 1400 | &y_line_buf, | 1456 | &y_line_buf, |
| 1401 | - y_stride, | 1457 | + y_stride as usize, |
| 1402 | - 0, | 1458 | + (x << 1) as usize, |
| 1403 | - x << 1, | 1459 | + (i * 2) as usize, |
| 1460 | + &y_col_buf, | ||
| 1461 | + (j * 2) as usize, | ||
| 1462 | + &luma_template, | ||
| 1463 | + LUMA_BLOCK_W as usize, | ||
| 1464 | + luma_off_y as usize, | ||
| 1465 | + luma_off_x as usize, | ||
| 1466 | + ); | ||
| 1467 | + } | ||
| 1468 | + if apply_cb { | ||
| 1469 | + assemble_block_noise( | ||
| 1470 | + &mut cb_blk, | ||
| 1471 | + CHROMA_SUBBLOCK as usize, | ||
| 1472 | + bh as usize, | ||
| 1473 | + bw as usize, | ||
| 1404 | &cb_line_buf, | 1474 | &cb_line_buf, |
| 1405 | - &cr_line_buf, | 1475 | + c_stride as usize, |
| 1406 | - c_stride, | 1476 | + x as usize, |
| 1407 | - 0, | 1477 | + i as usize, |
| 1408 | - x, | 1478 | + &cb_col_buf, |
| 1409 | - 1, | 1479 | + j as usize, |
| 1410 | - (LUMA_SUBBLOCK >> 1).min(width / 2 - x), fctx, | 1480 | + &cb_template, |
| 1481 | + CHROMA_BLOCK_W as usize, | ||
| 1482 | + chroma_off_y as usize, | ||
| 1483 | + chroma_off_x as usize, | ||
| 1411 | ); | 1484 | ); |
| 1412 | } | 1485 | } |
| 1486 | + if apply_cr { | ||
| 1487 | + assemble_block_noise( | ||
| 1488 | + &mut cr_blk, | ||
| 1489 | + CHROMA_SUBBLOCK as usize, | ||
| 1490 | + bh as usize, | ||
| 1491 | + bw as usize, | ||
| 1492 | + &cr_line_buf, | ||
| 1493 | + c_stride as usize, | ||
| 1494 | + x as usize, | ||
| 1495 | + i as usize, | ||
| 1496 | + &cr_col_buf, | ||
| 1497 | + j as usize, | ||
| 1498 | + &cr_template, | ||
| 1499 | + CHROMA_BLOCK_W as usize, | ||
| 1500 | + chroma_off_y as usize, | ||
| 1501 | + chroma_off_x as usize, | ||
| 1502 | + ); | ||
| 1413 | } | 1503 | } |
| 1414 | - | ||
| 1415 | - let i = if overlap && y != 0 { 1 } else { 0 }; | ||
| 1416 | - let j = if overlap && x != 0 { 1 } else { 0 }; | ||
| 1417 | - if !prelude { | ||
| 1418 | add_noise_to_block( | 1504 | add_noise_to_block( |
| 1419 | fg, | 1505 | fg, |
| 1420 | mc_identity, | 1506 | mc_identity, |
| @@ -1422,21 +1508,22 @@ pub(crate) fn apply_grain( | |||
| 1422 | &scaling_cb, | 1508 | &scaling_cb, |
| 1423 | &scaling_cr, | 1509 | &scaling_cr, |
| 1424 | &mut *out, | 1510 | &mut *out, |
| 1425 | - (y + i) << 1, | 1511 | + y << 1, |
| 1426 | - (x + j) << 1, | 1512 | + x << 1, |
| 1427 | - &luma_template, | 1513 | + &y_blk, |
| 1428 | - LUMA_BLOCK_W, | 1514 | + LUMA_SUBBLOCK, |
| 1429 | - luma_off_y + (i << 1), | 1515 | + 0, |
| 1430 | - luma_off_x + (j << 1), | 1516 | + 0, |
| 1431 | - &cb_template, | 1517 | + &cb_blk, |
| 1432 | - &cr_template, | 1518 | + &cr_blk, |
| 1433 | - CHROMA_BLOCK_W, | 1519 | + CHROMA_SUBBLOCK, |
| 1434 | - chroma_off_y + i, | 1520 | + 0, |
| 1435 | - chroma_off_x + j, | 1521 | + 0, |
| 1436 | - (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, | 1522 | + bh, |
| 1437 | - (LUMA_SUBBLOCK >> 1).min(width / 2 - x) - j, fctx, | 1523 | + bw, fctx, |
| 1438 | ); | 1524 | ); |
| 1439 | } | 1525 | } |
| 1526 | + } | ||
| 1440 | 1527 | ||
| 1441 | if overlap { | 1528 | if overlap { |
| 1442 | if x != 0 { | 1529 | if x != 0 { |