From 170dcd58db4797fd32b81d6132d5ebd45d777da5 Mon Sep 17 00:00:00 2001 From: Nicolas Dufresne Date: Thu, 22 Jun 2023 10:10:43 -0400 Subject: [PATCH] v4l2: Fix support for left and top padding In the current implementation, we support for most pixel format left and top padding by changing the offset in the video meta. Though, to align driver bytesused to the offset, we recalculate the offset, which removed the modification we did before. Instead, save the plane size, and truncate the driver reported bytesused to the expected size, which ensures that the offsets still match. This should also fix issues were the buffer size ended up bigger then the pool size due to driver introduced padding. Part-of: --- .../sys/v4l2/gstv4l2bufferpool.c | 18 +++++++++++------- .../gst-plugins-good/sys/v4l2/gstv4l2object.c | 15 ++++----------- .../gst-plugins-good/sys/v4l2/gstv4l2object.h | 1 + 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/subprojects/gst-plugins-good/sys/v4l2/gstv4l2bufferpool.c b/subprojects/gst-plugins-good/sys/v4l2/gstv4l2bufferpool.c index 5a2d4a030a..e12ec10156 100644 --- a/subprojects/gst-plugins-good/sys/v4l2/gstv4l2bufferpool.c +++ b/subprojects/gst-plugins-good/sys/v4l2/gstv4l2bufferpool.c @@ -1240,8 +1240,7 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer, GstV4l2Object *obj = pool->obj; GstClockTime timestamp; GstV4l2MemoryGroup *group; - GstVideoMeta *vmeta; - gsize size; + const GstVideoInfo *info = &obj->info; gint i; gint old_buffer_state; @@ -1292,9 +1291,9 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer, timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp); - size = 0; - vmeta = gst_buffer_get_video_meta (outbuf); for (i = 0; i < group->n_mem; i++) { + const GstVideoFormatInfo *finfo = info->finfo; + GST_LOG_OBJECT (pool, "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %" GST_TIME_FORMAT ", pool-queued=%d, buffer=%p, previous-state=%i", @@ -1302,10 +1301,15 @@ gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool, GstBuffer ** buffer, group->planes[i].bytesused, i, group->buffer.flags, GST_TIME_ARGS (timestamp), pool->num_queued, outbuf, old_buffer_state); - if (vmeta) { - vmeta->offset[i] = size; - size += gst_memory_get_sizes (group->mem[i], NULL, NULL); + /* Ensure our offset matches the expected plane size, or image size if + * there is only one memory */ + if (group->n_mem == 1) { + gst_memory_resize (group->mem[0], 0, info->size + info->offset[0]); + break; } + + if (!GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) + gst_memory_resize (group->mem[i], 0, obj->plane_size[i]); } /* Ignore timestamp and field for OUTPUT device */ diff --git a/subprojects/gst-plugins-good/sys/v4l2/gstv4l2object.c b/subprojects/gst-plugins-good/sys/v4l2/gstv4l2object.c index 5679ef4226..ea5e1c9ff2 100644 --- a/subprojects/gst-plugins-good/sys/v4l2/gstv4l2object.c +++ b/subprojects/gst-plugins-good/sys/v4l2/gstv4l2object.c @@ -3576,18 +3576,11 @@ gst_v4l2_object_save_format (GstV4l2Object * v4l2object, if ((align->padding_left + align->padding_top) > 0) GST_WARNING_OBJECT (v4l2object->dbg_obj, "Left and top padding is not permitted for tiled formats"); + memset (v4l2object->plane_size, 0, + sizeof (v4l2object->plane_size[0] * GST_VIDEO_MAX_PLANES)); } else { - for (i = 0; i < finfo->n_planes; i++) { - gint vedge, hedge; - - /* FIXME we assume plane as component as this is true for all supported - * format we support. */ - - hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, i, align->padding_left); - vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, align->padding_top); - - info->offset[i] += (vedge * info->stride[i]) + - (hedge * GST_VIDEO_INFO_COMP_PSTRIDE (info, i)); + if (!gst_video_info_align_full (info, align, v4l2object->plane_size)) { + GST_WARNING_OBJECT (v4l2object->dbg_obj, "Failed to align video info"); } } diff --git a/subprojects/gst-plugins-good/sys/v4l2/gstv4l2object.h b/subprojects/gst-plugins-good/sys/v4l2/gstv4l2object.h index 13b6f91ad6..5223cbbaca 100644 --- a/subprojects/gst-plugins-good/sys/v4l2/gstv4l2object.h +++ b/subprojects/gst-plugins-good/sys/v4l2/gstv4l2object.h @@ -148,6 +148,7 @@ struct _GstV4l2Object { GstVideoInfo info; GstVideoAlignment align; GstVideoTransferFunction transfer; + gsize plane_size[GST_VIDEO_MAX_PLANES]; /* Features */ gboolean need_video_meta;