From: Zhao Zhili via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: Wenjie Yin <wenjyin@qti.qualcomm.com>,
"wangao.wang@oss.qualcomm.com" <wangao.wang@oss.qualcomm.com>,
"Qiwei Liu (QUIC)" <quic_qiweil@quicinc.com>,
"renjiang.han@oss.qualcomm.com" <renjiang.han@oss.qualcomm.com>,
Zhao Zhili <quinkblack@foxmail.com>
Subject: [FFmpeg-devel] Re: [PATCH] libavcodec/v4l2_buffers: fixing buffer alignment issue
Date: Thu, 13 Nov 2025 14:56:26 +0800
Message-ID: <tencent_AF284AB3E0F37BF5C1E90B8EA24541731C05@qq.com> (raw)
In-Reply-To: <DS4PR02MB10842C9A33E392ACB7BD189C59ACDA@DS4PR02MB10842.namprd02.prod.outlook.com>
> On Nov 13, 2025, at 11:29, Wenjie Yin via ffmpeg-devel <ffmpeg-devel@ffmpeg.org> wrote:
>
> V4l2 drivers may return buffer width & height with aligned values, which
> may be different from frame width & height. Bytesperlines and strides
> should be check for aligned buffers when copying pixel data. When width
> aligned, copy with planar; otherwise, copy line-by-line to dest buffer.
>
> Co-authored-by: Renjiang Han <renjiang.han@oss.qualcomm.com>
> Signed-off-by: Wenjie.Yin <quic_wenjyin@quicinc.com>
> Signed-off-by: Wenjie Yin <wenjyin@qti.qualcomm.com>
> ---
> libavcodec/v4l2_buffers.c | 68 +++++++++++++++++++++++++++++++++++----
> 1 file changed, 62 insertions(+), 6 deletions(-)
>
> diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
> index d869ce9e77..5f5a8aefd5 100644
> --- a/libavcodec/v4l2_buffers.c
> +++ b/libavcodec/v4l2_buffers.c
> @@ -356,6 +356,33 @@ static int v4l2_buffer_buf_to_swframe(AVFrame *frame, V4L2Buffer *avbuf)
> return 0;
> }
>
> +static int v4l2_plane_to_plane_align(V4L2Buffer *out, int plane, const uint8_t* data, int size, int offset, int width, int height, int stride)
> +{
> + unsigned int bytesused, length;
> +
> + if (plane >= out->num_planes)
> + return AVERROR(EINVAL);
> +
> + length = out->plane_info[plane].length;
> + bytesused = FFMIN(size+offset, length);
> +
> + for (int i = 0; i < height; i++) {
> + memcpy((uint8_t*)out->plane_info[plane].mm_addr + offset, data, width);
> + offset += stride;
> + data += width;
> + }
> +
> + if (V4L2_TYPE_IS_MULTIPLANAR(out->buf.type)) {
> + out->planes[plane].bytesused = bytesused;
> + out->planes[plane].length = length;
> + } else {
> + out->buf.bytesused = bytesused;
> + out->buf.length = length;
> + }
> +
> + return 0;
> +}
> +
> static int v4l2_buffer_swframe_to_buf(const AVFrame *frame, V4L2Buffer *out)
> {
> int i, ret;
> @@ -364,7 +391,11 @@ static int v4l2_buffer_swframe_to_buf(const AVFrame *frame, V4L2Buffer *out)
> fmt.fmt.pix_mp.pixelformat : fmt.fmt.pix.pixelformat;
> int height = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ?
> fmt.fmt.pix_mp.height : fmt.fmt.pix.height;
> - int is_planar_format = 0;
> + int bytesperline = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ?
> + (fmt.fmt.pix_mp.num_planes ? fmt.fmt.pix_mp.plane_fmt[0].bytesperline: 0) : fmt.fmt.pix.bytesperline;
The doc says bytesperline can be zero
__u32 bytesperline; /* for padding, zero if unused */
> + int sizeimage = V4L2_TYPE_IS_MULTIPLANAR(fmt.type) ?
> + (fmt.fmt.pix_mp.num_planes ? fmt.fmt.pix_mp.plane_fmt[0].sizeimage: 0) : fmt.fmt.pix.sizeimage;
> + int is_planar_format = 0, scanline_y = 0, scanline_uv = 0;
>
> switch (pixel_format) {
> case V4L2_PIX_FMT_YUV420M:
> @@ -398,16 +429,41 @@ static int v4l2_buffer_swframe_to_buf(const AVFrame *frame, V4L2Buffer *out)
> for (i = 0; i < desc->nb_components; i++)
> planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
>
> + switch (pixel_format)
> + {
> + /* Currently only NV12 & NV21 are addressed for buffer alignment issue */
> + case V4L2_PIX_FMT_NV12:
> + case V4L2_PIX_FMT_NV21:
> + scanline_y = sizeimage / bytesperline * 2 / 3;
> + scanline_uv = scanline_y / 2;
> + break;
> + default:
> + /* By default, no buffer alignment issues are expected */
> + scanline_y = height;
> + scanline_uv = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
> + bytesperline = frame->linesize[0];
> + break;
> + }
I think you can use the information in const AVPixFmtDescriptor *desc to handle
all formats than switch case for each one.
Please send a PR to https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls
> +
> for (i = 0; i < planes_nb; i++) {
> int size, h = height;
> if (i == 1 || i == 2) {
> h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
> }
> - size = frame->linesize[i] * h;
> - ret = v4l2_bufref_to_buf(out, 0, frame->data[i], size, offset);
> - if (ret)
> - return ret;
> - offset += size;
> +
> + if (bytesperline == frame->linesize[0]) {
> + size = frame->linesize[i] * h;
> + ret = v4l2_bufref_to_buf(out, 0, frame->data[i], size, offset);
> + if (ret)
> + return ret;
> + offset += frame->linesize[i] * (i ? scanline_uv : scanline_y);
> + } else {
> + size = bytesperline * (i ? scanline_uv : scanline_y);
> + ret = v4l2_plane_to_plane_align(out, 0, frame->data[i], size, offset, frame->linesize[i], h, bytesperline);
> + if (ret)
> + return ret;
> + offset += size;
> + }
> }
> return 0;
> }
> --
> 2.34.1
>
> _______________________________________________
> ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
> To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
next prev parent reply other threads:[~2025-11-13 7:01 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-13 3:29 [FFmpeg-devel] " Wenjie Yin via ffmpeg-devel
2025-11-13 6:56 ` Zhao Zhili via ffmpeg-devel [this message]
[not found] <DS4PR02MB10842923BAC71ECD23E9C0D899AFAA@DS4PR02MB10842.namprd02.prod.outlook.com>
2025-11-04 10:37 ` [FFmpeg-devel] " Wenjie Yin via ffmpeg-devel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_AF284AB3E0F37BF5C1E90B8EA24541731C05@qq.com \
--to=ffmpeg-devel@ffmpeg.org \
--cc=quic_qiweil@quicinc.com \
--cc=quinkblack@foxmail.com \
--cc=renjiang.han@oss.qualcomm.com \
--cc=wangao.wang@oss.qualcomm.com \
--cc=wenjyin@qti.qualcomm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git