* [FFmpeg-devel] [RFC PATCH] lavd/v4l2: cover all bufer if bytesperline is set by driver
@ 2022-11-09 19:36 Dima Buzdyk
2022-12-08 7:50 ` Dmitry Buzdyk
0 siblings, 1 reply; 2+ messages in thread
From: Dima Buzdyk @ 2022-11-09 19:36 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Dima Buzdyk
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".
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [FFmpeg-devel] [RFC PATCH] lavd/v4l2: cover all bufer if bytesperline is set by driver
2022-11-09 19:36 [FFmpeg-devel] [RFC PATCH] lavd/v4l2: cover all bufer if bytesperline is set by driver Dima Buzdyk
@ 2022-12-08 7:50 ` Dmitry Buzdyk
0 siblings, 0 replies; 2+ messages in thread
From: Dmitry Buzdyk @ 2022-12-08 7:50 UTC (permalink / raw)
To: ffmpeg-devel
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@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;
>
_______________________________________________
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".
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-12-08 7:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-09 19:36 [FFmpeg-devel] [RFC PATCH] lavd/v4l2: cover all bufer if bytesperline is set by driver Dima Buzdyk
2022-12-08 7:50 ` Dmitry Buzdyk
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