Skip to content

Commit 5080752

Browse files
authored
Merge pull request #2385 from awxkee/yuv-stride-fix
Fix YUV 4:2:0, 4:2:2 large strides
2 parents 2c986d3 + 45767ee commit 5080752

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/codecs/avif/yuv.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,23 @@ fn process_halved_chroma_row<
521521

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

524+
// If the stride is larger than the plane size,
525+
// it might contain junk data beyond the actual valid region.
526+
// To avoid processing artifacts when working with odd-sized images,
527+
// the buffer is reshaped to its actual size,
528+
// preventing accidental use of invalid values from the trailing region.
529+
530+
let y_plane = &image.y_plane[0..image.width];
531+
let chroma_size = image.width.div_ceil(2);
532+
let u_plane = &image.u_plane[0..chroma_size];
533+
let v_plane = &image.v_plane[0..chroma_size];
534+
let rgba = &mut rgba[0..image.width * CHANNELS];
535+
524536
let bias_y = range.bias_y as i32;
525537
let bias_uv = range.bias_uv as i32;
526-
let y_iter = image.y_plane.chunks_exact(2);
538+
let y_iter = y_plane.chunks_exact(2);
527539
let rgb_chunks = rgba.chunks_exact_mut(CHANNELS * 2);
528-
for (((y_src, &u_src), &v_src), rgb_dst) in
529-
y_iter.zip(image.u_plane).zip(image.v_plane).zip(rgb_chunks)
530-
{
540+
for (((y_src, &u_src), &v_src), rgb_dst) in y_iter.zip(u_plane).zip(v_plane).zip(rgb_chunks) {
531541
let y_value: i32 = (y_src[0].as_() - bias_y) * y_coef;
532542
let cb_value: i32 = u_src.as_() - bias_uv;
533543
let cr_value: i32 = v_src.as_() - bias_uv;
@@ -571,13 +581,13 @@ fn process_halved_chroma_row<
571581

572582
// Process remainder if width is odd.
573583
if image.width & 1 != 0 {
574-
let y_left = image.y_plane.chunks_exact(2).remainder();
584+
let y_left = y_plane.chunks_exact(2).remainder();
575585
let rgb_chunks = rgba
576586
.chunks_exact_mut(CHANNELS * 2)
577587
.into_remainder()
578588
.chunks_exact_mut(CHANNELS);
579-
let u_iter = image.u_plane.iter().rev();
580-
let v_iter = image.v_plane.iter().rev();
589+
let u_iter = u_plane.iter().rev();
590+
let v_iter = v_plane.iter().rev();
581591

582592
for (((y_src, u_src), v_src), rgb_dst) in
583593
y_left.iter().zip(u_iter).zip(v_iter).zip(rgb_chunks)

0 commit comments

Comments
 (0)