[{"id":3971,"web_url":"https://patchwork.libcamera.org/comment/3971/","msgid":"<20200306203806.GT4878@pendragon.ideasonboard.com>","date":"2020-03-06T20:38:06","subject":"Re: [libcamera-devel] [PATCH v3 02/27] gst: Add utility to convert\n\tStreamFormats to GstCaps","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Nicolas,\n\nThank you for the patch.\n\nOn Fri, Mar 06, 2020 at 03:26:12PM -0500, Nicolas Dufresne wrote:\n> From: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n> \n> This transforms the basic information found in StreamFormats to GstCaps.\n> This can be handy to reply to early caps query or inside a device\n> provider. Note that we ignored generated range as they are harmful to\n> caps negotiation. We also don't simplify the caps for readability\n> reasons, so some of the discrete value may be included in a range.\n> \n> Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  src/gstreamer/gstlibcamera-utils.cpp | 105 +++++++++++++++++++++++++++\n>  src/gstreamer/gstlibcamera-utils.h   |  19 +++++\n>  src/gstreamer/meson.build            |   1 +\n>  3 files changed, 125 insertions(+)\n>  create mode 100644 src/gstreamer/gstlibcamera-utils.cpp\n>  create mode 100644 src/gstreamer/gstlibcamera-utils.h\n> \n> diff --git a/src/gstreamer/gstlibcamera-utils.cpp b/src/gstreamer/gstlibcamera-utils.cpp\n> new file mode 100644\n> index 0000000..386ec36\n> --- /dev/null\n> +++ b/src/gstreamer/gstlibcamera-utils.cpp\n> @@ -0,0 +1,105 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2020, Collabora Ltd.\n> + *     Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n> + *\n> + * gstlibcamera-utils.c - GStreamer libcamera Utility Function\n> + */\n> +\n> +#include \"gstlibcamera-utils.h\"\n> +\n> +#include <linux/drm_fourcc.h>\n> +\n> +using namespace libcamera;\n> +\n> +static struct {\n> +\tGstVideoFormat gst_format;\n> +\tguint drm_fourcc;\n> +} format_map[] = {\n> +\t{ GST_VIDEO_FORMAT_ENCODED, DRM_FORMAT_MJPEG },\n> +\t{ GST_VIDEO_FORMAT_RGB, DRM_FORMAT_BGR888 },\n> +\t{ GST_VIDEO_FORMAT_BGR, DRM_FORMAT_RGB888 },\n> +\t{ GST_VIDEO_FORMAT_ARGB, DRM_FORMAT_BGRA8888 },\n> +\t{ GST_VIDEO_FORMAT_NV12, DRM_FORMAT_NV12 },\n> +\t{ GST_VIDEO_FORMAT_NV21, DRM_FORMAT_NV21 },\n> +\t{ GST_VIDEO_FORMAT_NV16, DRM_FORMAT_NV16 },\n> +\t{ GST_VIDEO_FORMAT_NV61, DRM_FORMAT_NV61 },\n> +\t{ GST_VIDEO_FORMAT_NV24, DRM_FORMAT_NV24 },\n> +\t{ GST_VIDEO_FORMAT_UYVY, DRM_FORMAT_UYVY },\n> +\t{ GST_VIDEO_FORMAT_VYUY, DRM_FORMAT_VYUY },\n> +\t{ GST_VIDEO_FORMAT_YUY2, DRM_FORMAT_YUYV },\n> +\t{ GST_VIDEO_FORMAT_YVYU, DRM_FORMAT_YVYU },\n> +\t/* \\todo NV42 is used in libcamera but is not mapped in GStreamer yet. */\n> +};\n> +\n> +static GstVideoFormat\n> +drm_to_gst_format(guint drm_fourcc)\n> +{\n> +\tfor (const auto &item : format_map) {\n> +\t\tif (item.drm_fourcc == drm_fourcc)\n> +\t\t\treturn item.gst_format;\n> +\t}\n> +\treturn GST_VIDEO_FORMAT_UNKNOWN;\n> +}\n> +\n> +static GstStructure *\n> +bare_structure_from_fourcc(guint fourcc)\n> +{\n> +\tGstVideoFormat gst_format = drm_to_gst_format(fourcc);\n> +\n> +\tif (gst_format == GST_VIDEO_FORMAT_UNKNOWN)\n> +\t\treturn nullptr;\n> +\n> +\tif (gst_format != GST_VIDEO_FORMAT_ENCODED)\n> +\t\treturn gst_structure_new(\"video/x-raw\", \"format\", G_TYPE_STRING,\n> +\t\t\t\t\t gst_video_format_to_string(gst_format), nullptr);\n> +\n> +\tswitch (fourcc) {\n> +\tcase DRM_FORMAT_MJPEG:\n> +\t\treturn gst_structure_new_empty(\"image/jpeg\");\n> +\tdefault:\n> +\t\treturn nullptr;\n> +\t}\n> +}\n> +\n> +GstCaps *\n> +gst_libcamera_stream_formats_to_caps(const StreamFormats &formats)\n> +{\n> +\tGstCaps *caps = gst_caps_new_empty();\n> +\n> +\tfor (unsigned int fourcc : formats.pixelformats()) {\n> +\t\tg_autoptr(GstStructure) bare_s = bare_structure_from_fourcc(fourcc);\n> +\n> +\t\tif (!bare_s) {\n> +\t\t\tGST_WARNING(\"Unsupported DRM format %\" GST_FOURCC_FORMAT,\n> +\t\t\t\t    GST_FOURCC_ARGS(fourcc));\n> +\t\t\tcontinue;\n> +\t\t}\n> +\n> +\t\tfor (const Size &size : formats.sizes(fourcc)) {\n> +\t\t\tGstStructure *s = gst_structure_copy(bare_s);\n> +\t\t\tgst_structure_set(s,\n> +\t\t\t\t\t  \"width\", G_TYPE_INT, size.width,\n> +\t\t\t\t\t  \"height\", G_TYPE_INT, size.height,\n> +\t\t\t\t\t  nullptr);\n> +\t\t\tgst_caps_append_structure(caps, s);\n> +\t\t}\n> +\n> +\t\tconst SizeRange &range = formats.range(fourcc);\n> +\t\tif (range.hStep && range.vStep) {\n> +\t\t\tGstStructure *s = gst_structure_copy(bare_s);\n> +\t\t\tGValue val = G_VALUE_INIT;\n> +\n> +\t\t\tg_value_init(&val, GST_TYPE_INT_RANGE);\n> +\t\t\tgst_value_set_int_range_step(&val, range.min.width, range.max.width, range.hStep);\n> +\t\t\tgst_structure_set_value(s, \"width\", &val);\n> +\t\t\tgst_value_set_int_range_step(&val, range.min.height, range.max.height, range.vStep);\n> +\t\t\tgst_structure_set_value(s, \"height\", &val);\n> +\t\t\tg_value_unset(&val);\n> +\n> +\t\t\tgst_caps_append_structure(caps, s);\n> +\t\t}\n> +\t}\n> +\n> +\treturn caps;\n> +}\n> diff --git a/src/gstreamer/gstlibcamera-utils.h b/src/gstreamer/gstlibcamera-utils.h\n> new file mode 100644\n> index 0000000..2e4e304\n> --- /dev/null\n> +++ b/src/gstreamer/gstlibcamera-utils.h\n> @@ -0,0 +1,19 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2020, Collabora Ltd.\n> + *     Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n> + *\n> + * gstlibcamera-utils.h - GStreamer libcamera Utility Functions\n> + */\n> +\n> +#ifndef __GST_LIBCAMERA_UTILS_H__\n> +#define __GST_LIBCAMERA_UTILS_H__\n> +\n> +#include <gst/gst.h>\n> +#include <gst/video/video.h>\n> +\n> +#include <libcamera/stream.h>\n> +\n> +GstCaps *gst_libcamera_stream_formats_to_caps(const libcamera::StreamFormats &formats);\n> +\n> +#endif /* __GST_LIBCAMERA_UTILS_H__ */\n> diff --git a/src/gstreamer/meson.build b/src/gstreamer/meson.build\n> index 25350f2..1539d9e 100644\n> --- a/src/gstreamer/meson.build\n> +++ b/src/gstreamer/meson.build\n> @@ -1,4 +1,5 @@\n>  libcamera_gst_sources = [\n> +    'gstlibcamera-utils.cpp',\n>      'gstlibcamera.c',\n>      'gstlibcamerasrc.cpp',\n>  ]","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2AC5360424\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  6 Mar 2020 21:38:10 +0100 (CET)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 94AC724B;\n\tFri,  6 Mar 2020 21:38:09 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1583527089;\n\tbh=MDkEUz63K8ocFUayV02pSjCf/rl1Mqvn+fTw71itLs0=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=SpNfotVQE8eklZlAmO1bg4309fiFbqx07ukoFvK2d346QdIVxwdZOGgYnmIlmIPyc\n\tPSTqQ5DGhz+EiDoJRCXmijSEf9Iz5kRJ3aDX3pgrHOhaPyLexiUA2np7c52u+dcQRx\n\tXMnxhUWDRqCyGafF3AskmBpR+S1YaYhV6/NwsMTo=","Date":"Fri, 6 Mar 2020 22:38:06 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Nicolas Dufresne <nicolas@ndufresne.ca>","Cc":"libcamera-devel@lists.libcamera.org,\n\tNicolas Dufresne <nicolas.dufresne@collabora.com>","Message-ID":"<20200306203806.GT4878@pendragon.ideasonboard.com>","References":"<20200306202637.525587-1-nicolas@ndufresne.ca>\n\t<20200306202637.525587-3-nicolas@ndufresne.ca>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20200306202637.525587-3-nicolas@ndufresne.ca>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v3 02/27] gst: Add utility to convert\n\tStreamFormats to GstCaps","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Fri, 06 Mar 2020 20:38:10 -0000"}}]