[FFmpeg-devel] [RFC PATCH] lavd/v4l2: cover all bufer if bytesperline is set by driver
Dmitry Buzdyk
dima.buzdyk at gmail.com
Thu Dec 8 09:50:14 EET 2022
Reminder
On Thu, 2022-11-10 at 01:36 +0600, Dima Buzdyk wrote:
> 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 at 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;
>
More information about the ffmpeg-devel
mailing list