{"id":3008,"url":"https://patchwork.libcamera.org/api/1.1/patches/3008/?format=json","web_url":"https://patchwork.libcamera.org/patch/3008/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200306202637.525587-3-nicolas@ndufresne.ca>","date":"2020-03-06T20:26:12","name":"[libcamera-devel,v3,02/27] gst: Add utility to convert StreamFormats to GstCaps","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"6776c66cf31266b93c7c5e6655f987c8f4fbc420","submitter":{"id":30,"url":"https://patchwork.libcamera.org/api/1.1/people/30/?format=json","name":"Nicolas Dufresne","email":"nicolas@ndufresne.ca"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/3008/mbox/","series":[{"id":705,"url":"https://patchwork.libcamera.org/api/1.1/series/705/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=705","date":"2020-03-06T20:26:10","name":"GStreamer Element for libcamera","version":3,"mbox":"https://patchwork.libcamera.org/series/705/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3008/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3008/checks/","tags":{},"headers":{"Return-Path":"<nicolas@ndufresne.ca>","Received":["from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BFD9660424\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  6 Mar 2020 21:26:59 +0100 (CET)","from nicolas-tpx395.localdomain (unknown [IPv6:2610:98:8005::527])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256\n\tbits))\n\t(No client certificate requested) (Authenticated sender: nicolas)\n\tby bhuna.collabora.co.uk (Postfix) with ESMTPSA id 3D2E22970E7;\n\tFri,  6 Mar 2020 20:26:59 +0000 (GMT)"],"From":"Nicolas Dufresne <nicolas@ndufresne.ca>","To":"libcamera-devel@lists.libcamera.org","Cc":"Nicolas Dufresne <nicolas.dufresne@collabora.com>","Date":"Fri,  6 Mar 2020 15:26:12 -0500","Message-Id":"<20200306202637.525587-3-nicolas@ndufresne.ca>","X-Mailer":"git-send-email 2.24.1","In-Reply-To":"<20200306202637.525587-1-nicolas@ndufresne.ca>","References":"<20200306202637.525587-1-nicolas@ndufresne.ca>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[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:27:00 -0000"},"content":"From: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n\nThis transforms the basic information found in StreamFormats to GstCaps.\nThis can be handy to reply to early caps query or inside a device\nprovider. Note that we ignored generated range as they are harmful to\ncaps negotiation. We also don't simplify the caps for readability\nreasons, so some of the discrete value may be included in a range.\n\nSigned-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>\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","diff":"diff --git a/src/gstreamer/gstlibcamera-utils.cpp b/src/gstreamer/gstlibcamera-utils.cpp\nnew file mode 100644\nindex 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+}\ndiff --git a/src/gstreamer/gstlibcamera-utils.h b/src/gstreamer/gstlibcamera-utils.h\nnew file mode 100644\nindex 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__ */\ndiff --git a/src/gstreamer/meson.build b/src/gstreamer/meson.build\nindex 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 ]\n","prefixes":["libcamera-devel","v3","02/27"]}