From 042ff8056281df0b374c2b22faff393ec9ea68db Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Sun, 8 Mar 2026 22:55:37 +0800 Subject: [PATCH] doc/examples/qsv_decode: fix raw frame dump for chroma planes The output loop used sw_frame->width as the write size for all planes. This is only correct for NV12 where the interleaved UV plane happens to have the same byte width as the Y plane. For other pixel formats (e.g. YUV420P where U/V planes are half width, or P010 where samples are 2 bytes), the output would be corrupted. Use av_image_get_linesize() to compute the correct byte width for each plane based on the actual pixel format. Signed-off-by: Jun Zhao --- doc/examples/qsv_decode.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/examples/qsv_decode.c b/doc/examples/qsv_decode.c index ec91109480..cdfde89652 100644 --- a/doc/examples/qsv_decode.c +++ b/doc/examples/qsv_decode.c @@ -39,6 +39,7 @@ #include #include #include +#include #include static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts) @@ -88,9 +89,16 @@ static int decode_packet(AVCodecContext *decoder_ctx, goto fail; } - for (i = 0; i < FF_ARRAY_ELEMS(sw_frame->data) && sw_frame->data[i]; i++) - for (j = 0; j < (sw_frame->height >> (i > 0)); j++) - avio_write(output_ctx, sw_frame->data[i] + j * sw_frame->linesize[i], sw_frame->width); + for (i = 0; i < FF_ARRAY_ELEMS(sw_frame->data) && sw_frame->data[i]; i++) { + int h = sw_frame->height >> (i > 0); + int linesize = av_image_get_linesize(sw_frame->format, sw_frame->width, i); + if (linesize < 0) { + ret = linesize; + goto fail; + } + for (j = 0; j < h; j++) + avio_write(output_ctx, sw_frame->data[i] + j * sw_frame->linesize[i], linesize); + } fail: av_frame_unref(sw_frame);