From 5a189866b74f3036dd0d719eb0517a3c9f61fc88 Mon Sep 17 00:00:00 2001 From: 94xhn <87560781+94xhn@users.noreply.github.com> Date: Sat, 18 Jul 2026 22:08:45 +0800 Subject: [PATCH] cpu encoder: fix chroma row advance timing in YUV420/YVU420 planar path _jpeg_write_scanlines_yuv_planar() advanced chroma1_data/chroma2_data after building the current luma row instead of before, so every even luma row (>=2) read the previous chroma-row-pair's samples instead of its own. Move the existing advance check to the top of the loop body, before the row is built, so it takes effect before the chroma pointers are read for that row. Condition and stride math are unchanged. --- src/ustreamer/encoders/cpu/encoder.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ustreamer/encoders/cpu/encoder.c b/src/ustreamer/encoders/cpu/encoder.c index 8639446..665b3b6 100644 --- a/src/ustreamer/encoders/cpu/encoder.c +++ b/src/ustreamer/encoders/cpu/encoder.c @@ -223,6 +223,11 @@ static void _jpeg_write_scanlines_yuv_planar(struct jpeg_compress_struct *jpeg, const u8 *chroma2_data = frame->data + luma_array_size + chroma_array_size; while (jpeg->next_scanline < frame->height) { + if (jpeg->next_scanline > 0 && jpeg->next_scanline % 2 == 0) { + chroma1_data += (frame->width + padding) >> 1; + chroma2_data += (frame->width + padding) >> 1; + } + u8 *ptr = line_buf; for (uint x = 0; x < frame->width; ++x) { @@ -253,11 +258,6 @@ static void _jpeg_write_scanlines_yuv_planar(struct jpeg_compress_struct *jpeg, data += frame->width + padding; - if (jpeg->next_scanline > 0 && jpeg->next_scanline % 2 == 0) { - chroma1_data += (frame->width + padding) >> 1; - chroma2_data += (frame->width + padding) >> 1; - } - JSAMPROW scanlines[1] = {line_buf}; jpeg_write_scanlines(jpeg, scanlines, 1); }