{"id":2884,"url":"https://patchwork.libcamera.org/api/patches/2884/?format=json","web_url":"https://patchwork.libcamera.org/patch/2884/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20200227200407.490616-3-nicolas.dufresne@collabora.com>","date":"2020-02-27T20:03:42","name":"[libcamera-devel,v2,02/27] gst: Add utility to convert StreamFormats to GstCaps","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"d0b4ea206983d8a0aa7ee13a9fc172004d7cbff7","submitter":{"id":31,"url":"https://patchwork.libcamera.org/api/people/31/?format=json","name":"Nicolas Dufresne","email":"nicolas.dufresne@collabora.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2884/mbox/","series":[{"id":693,"url":"https://patchwork.libcamera.org/api/series/693/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=693","date":"2020-02-27T20:03:40","name":"GStreamer Element for libcamera","version":2,"mbox":"https://patchwork.libcamera.org/series/693/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2884/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2884/checks/","tags":{},"headers":{"Return-Path":"<nicolas.dufresne@collabora.com>","Received":["from bhuna.collabora.co.uk (bhuna.collabora.co.uk\n\t[IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C8E9C626D0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 27 Feb 2020 21:04:18 +0100 (CET)","from [127.0.0.1] (localhost [127.0.0.1])\n\t(Authenticated sender: nicolas) with ESMTPSA id 576652949DF"],"From":"Nicolas Dufresne <nicolas.dufresne@collabora.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 27 Feb 2020 15:03:42 -0500","Message-Id":"<20200227200407.490616-3-nicolas.dufresne@collabora.com>","X-Mailer":"git-send-email 2.24.1","In-Reply-To":"<20200227200407.490616-1-nicolas.dufresne@collabora.com>","References":"<20200227200407.490616-1-nicolas.dufresne@collabora.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 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":"Thu, 27 Feb 2020 20:04:19 -0000"},"content":"This 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 | 98 ++++++++++++++++++++++++++++\n src/gstreamer/gstlibcamera-utils.h   | 18 +++++\n src/gstreamer/meson.build            |  1 +\n 3 files changed, 117 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..dc129c3\n--- /dev/null\n+++ b/src/gstreamer/gstlibcamera-utils.cpp\n@@ -0,0 +1,98 @@\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+#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 inline 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+\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\tgst_structure_set(s,\n+\t\t\t\t\t  \"width\", GST_TYPE_INT_RANGE, range.min.width, range.max.width, range.hStep,\n+\t\t\t\t\t  \"height\", GST_TYPE_INT_RANGE, range.min.height, range.max.height, range.vStep,\n+\t\t\t\t\t  nullptr);\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..33160b8\n--- /dev/null\n+++ b/src/gstreamer/gstlibcamera-utils.h\n@@ -0,0 +1,18 @@\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+\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 832b8a5..f409107 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","v2","02/27"]}