Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix YUV 4:2:0, 4:2:2 large strides #2385

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/codecs/avif/yuv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,23 @@ fn process_halved_chroma_row<

let max_value = (1 << BIT_DEPTH) - 1;

// If the stride is larger than the plane size,
// it might contain junk data beyond the actual valid region.
// To avoid processing artifacts when working with odd-sized images,
// the buffer is reshaped to its actual size,
// preventing accidental use of invalid values from the trailing region.

let y_plane = &image.y_plane[0..image.width];
let chroma_size = image.width.div_ceil(2);
let u_plane = &image.u_plane[0..chroma_size];
let v_plane = &image.v_plane[0..chroma_size];
let rgba = &mut rgba[0..image.width * CHANNELS];

let bias_y = range.bias_y as i32;
let bias_uv = range.bias_uv as i32;
let y_iter = image.y_plane.chunks_exact(2);
let y_iter = y_plane.chunks_exact(2);
let rgb_chunks = rgba.chunks_exact_mut(CHANNELS * 2);
for (((y_src, &u_src), &v_src), rgb_dst) in
y_iter.zip(image.u_plane).zip(image.v_plane).zip(rgb_chunks)
{
for (((y_src, &u_src), &v_src), rgb_dst) in y_iter.zip(u_plane).zip(v_plane).zip(rgb_chunks) {
let y_value: i32 = (y_src[0].as_() - bias_y) * y_coef;
let cb_value: i32 = u_src.as_() - bias_uv;
let cr_value: i32 = v_src.as_() - bias_uv;
Expand Down Expand Up @@ -571,13 +581,13 @@ fn process_halved_chroma_row<

// Process remainder if width is odd.
if image.width & 1 != 0 {
let y_left = image.y_plane.chunks_exact(2).remainder();
let y_left = y_plane.chunks_exact(2).remainder();
let rgb_chunks = rgba
.chunks_exact_mut(CHANNELS * 2)
.into_remainder()
.chunks_exact_mut(CHANNELS);
let u_iter = image.u_plane.iter().rev();
let v_iter = image.v_plane.iter().rev();
let u_iter = u_plane.iter().rev();
let v_iter = v_plane.iter().rev();

for (((y_src, u_src), v_src), rgb_dst) in
y_left.iter().zip(u_iter).zip(v_iter).zip(rgb_chunks)
Expand Down
Loading