From: Dima Buzdyk <dima.buzdyk@gmail.com> To: ffmpeg-devel@ffmpeg.org Cc: Dima Buzdyk <dima.buzdyk@gmail.com> Subject: [FFmpeg-devel] [RFC PATCH] lavd/v4l2: cover all bufer if bytesperline is set by driver Date: Thu, 10 Nov 2022 01:36:03 +0600 Message-ID: <20221109193603.55080-1-dima.buzdyk@gmail.com> (raw) Some drivers may set bytesperline if hardware use padding bytes for alignment. In this case lavd/v4l2 will expect W*H bytes per frame, but driver will provide Pitch*H bytes which makes v4l2 unhappy. This change adjusts frame width to cover entire data buffer aligning lavd/v4l2 expectations with data provided by the driver. As a result user will be able to get image stream from device, albeit having garbage in padding bytes. Signed-off-by: Dima Buzdyk <dima.buzdyk@gmail.com> --- libavdevice/v4l2.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c index 5e85d1a2b3..b1e837f740 100644 --- a/libavdevice/v4l2.c +++ b/libavdevice/v4l2.c @@ -83,7 +83,7 @@ struct video_data { AVClass *class; int fd; int pixelformat; /* V4L2_PIX_FMT_* */ - int width, height; + int width, height, pitch; int frame_size; int interlaced; int top_field_first; @@ -202,7 +202,7 @@ fail: } static int device_init(AVFormatContext *ctx, int *width, int *height, - uint32_t pixelformat) + int *pitch, uint32_t pixelformat) { struct video_data *s = ctx->priv_data; struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE }; @@ -224,6 +224,7 @@ static int device_init(AVFormatContext *ctx, int *width, int *height, *width, *height, fmt.fmt.pix.width, fmt.fmt.pix.height); *width = fmt.fmt.pix.width; *height = fmt.fmt.pix.height; + *pitch = fmt.fmt.pix.bytesperline; } if (pixelformat != fmt.fmt.pix.pixelformat) { @@ -779,6 +780,7 @@ static int device_try_init(AVFormatContext *ctx, enum AVPixelFormat pix_fmt, int *width, int *height, + int *pitch, uint32_t *desired_format, enum AVCodecID *codec_id) { @@ -787,7 +789,7 @@ static int device_try_init(AVFormatContext *ctx, *desired_format = ff_fmt_ff2v4l(pix_fmt, ctx->video_codec_id); if (*desired_format) { - ret = device_init(ctx, width, height, *desired_format); + ret = device_init(ctx, width, height, pitch, *desired_format); if (ret < 0) { *desired_format = 0; if (ret != AVERROR(EINVAL)) @@ -804,7 +806,7 @@ static int device_try_init(AVFormatContext *ctx, (char *)av_x_if_null(av_get_pix_fmt_name(ff_fmt_conversion_table[i].ff_fmt), "none")); *desired_format = ff_fmt_conversion_table[i].v4l2_fmt; - ret = device_init(ctx, width, height, *desired_format); + ret = device_init(ctx, width, height, pitch, *desired_format); if (ret >= 0) break; else if (ret != AVERROR(EINVAL)) @@ -933,11 +935,13 @@ static int v4l2_read_header(AVFormatContext *ctx) s->width = fmt.fmt.pix.width; s->height = fmt.fmt.pix.height; + s->pitch = fmt.fmt.pix.bytesperline; av_log(ctx, AV_LOG_VERBOSE, "Setting frame size to %dx%d\n", s->width, s->height); } - res = device_try_init(ctx, pix_fmt, &s->width, &s->height, &desired_format, &codec_id); + res = device_try_init(ctx, pix_fmt, &s->width, &s->height, &s->pitch, + &desired_format, &codec_id); if (res < 0) goto fail; @@ -948,6 +952,24 @@ static int v4l2_read_header(AVFormatContext *ctx) if (codec_id != AV_CODEC_ID_NONE && ctx->video_codec_id == AV_CODEC_ID_NONE) ctx->video_codec_id = codec_id; + /* If bytesperpixel is set by driver then set width co cover full + * buffer area even if there are garbage data to be displayed. + * It is better to display padding bytes and give application ability + * to crop image later than fail to display image stream completely */ + if (s->pitch) { + int linesize; + + pix_fmt = ff_fmt_v4l2ff(desired_format, codec_id); + linesize = av_image_get_linesize(pix_fmt, s->width, 0); + if (linesize > 0) { + s->width = s->pitch * s->width / linesize; + + av_log(ctx, AV_LOG_INFO, + "Expand frame width to %dx%d to cover full buffer\n", + s->width, s->height); + } + } + if ((res = av_image_check_size(s->width, s->height, 0, ctx)) < 0) goto fail; -- 2.37.2 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next reply other threads:[~2022-11-09 19:36 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-11-09 19:36 Dima Buzdyk [this message] 2022-12-08 7:50 ` Dmitry Buzdyk
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=20221109193603.55080-1-dima.buzdyk@gmail.com \ --to=dima.buzdyk@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /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