Film grain chroma noise walks contiguous rows
Browse files at this commit8567ed51a9d89685301ff546a1a9dd90540a9235Parent: bea828c- perf
- film grain chroma noise walks contiguous rows
1 changed file +45 -29
- crates/ec-av1/src/film_grain.rs +45 -29
crates/ec-av1/src/film_grain.rsmodified+45 -29
| @@ -387,35 +387,51 @@ fn add_noise_to_block( | ||
| 387 | 387 | |
| 388 | 388 | let cy0 = py0 / 2; |
| 389 | 389 | let cx0 = px0 / 2; |
| 390 | - for i in 0..half_h { | |
| 391 | - for j in 0..half_w { | |
| 392 | - let ly = py0 + i * 2; | |
| 393 | - let lx = px0 + j * 2; | |
| 394 | - let l0 = (ly * y_stride + lx) as usize; | |
| 395 | - let average_luma = (picture.y[l0] as i32 + picture.y[l0 + 1] as i32 + 1) >> 1; | |
| 396 | - let cidx = ((cy0 + i) * c_stride + (cx0 + j)) as usize; | |
| 397 | - let gidx = ((cg_row0 + i) * cg_stride + (cg_col0 + j)) as usize; | |
| 398 | - if apply_cb { | |
| 399 | - let scaled = (((average_luma * cb_luma_mult + cb_mult * picture.u[cidx] as i32) | |
| 400 | - >> 6) | |
| 401 | - + cb_offset) | |
| 402 | - .clamp(0, index_max); | |
| 403 | - let delta = | |
| 404 | - (scaling_cb[scaled as usize] * cb_grain[gidx] + rounding_offset) | |
| 405 | - >> scaling_shift; | |
| 406 | - picture.u[cidx] = | |
| 407 | - ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16; | |
| 408 | - } | |
| 409 | - if apply_cr { | |
| 410 | - let scaled = (((average_luma * cr_luma_mult + cr_mult * picture.v[cidx] as i32) | |
| 411 | - >> 6) | |
| 412 | - + cr_offset) | |
| 413 | - .clamp(0, index_max); | |
| 414 | - let delta = | |
| 415 | - (scaling_cr[scaled as usize] * cr_grain[gidx] + rounding_offset) | |
| 416 | - >> scaling_shift; | |
| 417 | - picture.v[cidx] = | |
| 418 | - ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16; | |
| 390 | + if apply_cb || apply_cr { | |
| 391 | + // lane-perf4: one row of each operand at a time. The 4:2:0 luma | |
| 392 | + // average (spec 7.18.3.5) reads a horizontally adjacent PAIR of the | |
| 393 | + // block's own luma row, so `chunks_exact(2)` walks exactly the pairs | |
| 394 | + // the per-sample `(ly * y_stride + lx)` form used to re-derive, and | |
| 395 | + // the U/V/grain rows advance in lockstep with it -- same integers, | |
| 396 | + // same rounding, no per-sample index arithmetic or bounds check. | |
| 397 | + let cw = half_w as usize; | |
| 398 | + // U and V are distinct fields, so a disjoint borrow of each is | |
| 399 | + // needed alongside the immutable read of Y. | |
| 400 | + let Picture { y: ref luma_plane, u: ref mut u_plane, v: ref mut v_plane, .. } = | |
| 401 | + *picture; | |
| 402 | + for i in 0..half_h { | |
| 403 | + let lrow = ((py0 + i * 2) * y_stride + px0) as usize; | |
| 404 | + let crow = ((cy0 + i) * c_stride + cx0) as usize; | |
| 405 | + let grow = ((cg_row0 + i) * cg_stride + cg_col0) as usize; | |
| 406 | + let luma = &luma_plane[lrow..lrow + cw * 2]; | |
| 407 | + let gcb = &cb_grain[grow..grow + cw]; | |
| 408 | + let gcr = &cr_grain[grow..grow + cw]; | |
| 409 | + let urow = &mut u_plane[crow..crow + cw]; | |
| 410 | + let vrow = &mut v_plane[crow..crow + cw]; | |
| 411 | + for ((((lpair, u), v), &gb), &gr) in luma | |
| 412 | + .chunks_exact(2) | |
| 413 | + .zip(urow.iter_mut()) | |
| 414 | + .zip(vrow.iter_mut()) | |
| 415 | + .zip(gcb.iter()) | |
| 416 | + .zip(gcr.iter()) | |
| 417 | + { | |
| 418 | + let average_luma = (lpair[0] as i32 + lpair[1] as i32 + 1) >> 1; | |
| 419 | + if apply_cb { | |
| 420 | + let scaled = (((average_luma * cb_luma_mult + cb_mult * *u as i32) >> 6) | |
| 421 | + + cb_offset) | |
| 422 | + .clamp(0, index_max); | |
| 423 | + let delta = | |
| 424 | + (scaling_cb[scaled as usize] * gb + rounding_offset) >> scaling_shift; | |
| 425 | + *u = ((*u as i32 + delta).clamp(min_chroma, max_chroma)) as u16; | |
| 426 | + } | |
| 427 | + if apply_cr { | |
| 428 | + let scaled = (((average_luma * cr_luma_mult + cr_mult * *v as i32) >> 6) | |
| 429 | + + cr_offset) | |
| 430 | + .clamp(0, index_max); | |
| 431 | + let delta = | |
| 432 | + (scaling_cr[scaled as usize] * gr + rounding_offset) >> scaling_shift; | |
| 433 | + *v = ((*v as i32 + delta).clamp(min_chroma, max_chroma)) as u16; | |
| 434 | + } | |
| 419 | 435 | } |
| 420 | 436 | } |
| 421 | 437 | } |