Patch Detail
Show a patch.
GET /api/patches/27601/?format=api
{ "id": 27601, "url": "https://patchwork.libcamera.org/api/patches/27601/?format=api", "web_url": "https://patchwork.libcamera.org/patch/27601/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20260803131616.11292-2-david.plowman@raspberrypi.com>", "date": "2026-08-03T13:07:47", "name": "[v3,1/1] gstreamer: Prefer non-raw (i.e. non-Bayer) formats in caps negotiation", "commit_ref": null, "pull_url": null, "state": "new", "archived": false, "hash": "13ee77bcaafce7ddba4f7822b8f516f38e0555d6", "submitter": { "id": 42, "url": "https://patchwork.libcamera.org/api/people/42/?format=api", "name": "David Plowman", "email": "david.plowman@raspberrypi.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/27601/mbox/", "series": [ { "id": 6104, "url": "https://patchwork.libcamera.org/api/series/6104/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=6104", "date": "2026-08-03T13:07:46", "name": "Make Gstreamer prefer non-raw caps", "version": 3, "mbox": "https://patchwork.libcamera.org/series/6104/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/27601/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/27601/checks/", "tags": {}, "headers": { "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>", "X-Original-To": "parsemail@patchwork.libcamera.org", "Delivered-To": "parsemail@patchwork.libcamera.org", "Received": [ "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 7D974C32CE\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 3 Aug 2026 13:16:21 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E0A9E680FC;\n\tMon, 3 Aug 2026 15:16:20 +0200 (CEST)", "from mail-wr1-x432.google.com (mail-wr1-x432.google.com\n\t[IPv6:2a00:1450:4864:20::432])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3BD9B68067\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 3 Aug 2026 15:16:19 +0200 (CEST)", "by mail-wr1-x432.google.com with SMTP id\n\tffacd0b85a97d-47c6e9a694bso2215183f8f.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 03 Aug 2026 06:16:19 -0700 (PDT)", "from davidp-pi5.pitowers.org\n\t([2a00:1098:3142:1f:4706:89ee:c47e:1087])\n\tby smtp.gmail.com with ESMTPSA id\n\tffacd0b85a97d-47fd45895b6sm33725944f8f.26.2026.08.03.06.16.18\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 03 Aug 2026 06:16:18 -0700 (PDT)" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"gg/Bag1Z\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1785762979; x=1786367779;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to:content-type;\n\tbh=0Xjvh+J+Hv9zC8LKMx3w8YUUSZW2rg9SjCfV5TpeCGw=;\n\tb=gg/Bag1Zonya94+r7wBX32hx1oyklEMSxPIzLyrKlmkFUyDVKMoL+naSrprQl7N9hw\n\tpJWO/jxPVaIrpwixsw7TXNmoopd2U6qQhXwWpuKEiv1u4KJxy4LtgtJYznIl2CRuE0+t\n\tG2FIVNONTTrPlBBnyvcLnmgMl7sk7L29wPeDVui1ZSkug8XqYH+po/Gaz+9UJMkgQa17\n\t0fzrFjDj6Uuotmh+WGz5u7ojnqwL9E2wJ/cBtZb0kdtGlJFCHpXHlua0LnQgJE1FmS7J\n\tizDXH27YaPHPwN2RcJQZyAeDPbrP6fusDQcfxT6jPBGVBZO/J+xSTuCe+yXFqoBNcXg3\n\tWH4w==", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20251104; t=1785762979; x=1786367779;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from\n\t:to:cc:subject:date:message-id:reply-to:content-type;\n\tbh=0Xjvh+J+Hv9zC8LKMx3w8YUUSZW2rg9SjCfV5TpeCGw=;\n\tb=kiKspdy8kG2QuUv/B2GVzLIcN+h7rec5nf+oZd/MDfxJP6BPDYX7V6rkKFjAUJFqDz\n\t4HmX7LffBSTl2juDhyTcYQxMW4KM7pjkoY/yDzlE9sQlwE9tIHl95eHTXLWGfqMxtXa0\n\tejr9Lh48UjM1sBHyFKH9lohsXaaxgHlBiVCXGNHbmM1gBQFAtxVUdB+u6OcOnDW9W64o\n\tOlZzFUnFWR+GXJMAly5+ZfLlaom1SeKTUBUyGTf4S5lUj33mnXjHWuCATAG/aI8sWmmr\n\tG7dZrbEb17Rp91xtDxtOqrssRuQ7grj2IlPMfF3EKA6YMmm0hFgZJz/yVVc0eMtSNRSx\n\tWAxQ==", "X-Gm-Message-State": "AOJu0YxcsmcHyEKNmGFGOvlB6w7irjlihU/+uEAcLbQeHp5QH32N4a5a\n\tgXcF/bs+iHRVbbLD3bAANOV7h6uAwyiDOjh8LTzn7DzBV7WdvAStL9Km+NAkDK0BmqC4gWDplBC\n\ti0JiZ", "X-Gm-Gg": "AR+sD13UfXz/Wklsjvw8kLTEblyKeYuHD/oS1D8qw/AiS4fQ8yuG+wnzJj2kzyFDPyd\n\toPIBWDTfkiQVbz3ICbtv3D5fQC/6wIrGehiKheXzeAzIZlVirUFBSrzvPsItGFtuQcxQUdFQ8Sx\n\th6ZVpQxlSa2uSeFtPpSvvtqB/lO3sFoviMixR+40SbJLmO1Yv7E8vYRf/qQ3pA9dbYl12BtJb28\n\tQe2BCutIwOfVztgtjM3HI3TnGQ6tdNtXKLdjan46cZ6QVo3YKqopICKEq8MGi0XHUUhJbfF1yP8\n\tXxh23l7OA41hUru8WJ11Elp1zSw+o+NSWutyoejsefsac+ApFbBpOZI0QN5pWuHHeqczfGTVCU8\n\tBzCfVRwvbf1VuGhX86iQPjGzR6PpZK085HGrff3ZCdnXij6cjCZhsV/lq/PollzaEATOhnPCO+8\n\twqCoss42qyCXbjKVsvapRhq9c5IzpI/SOuoA+s3FG/ltSU4EK2FTfLX5eJPOZ7DG/kLOell22Rm\n\tfEGxkCAJtVXkKTltoNChsbQoldBAJsZ7Zhku47GL6cYzQoK5szOaWf3ekdkbh+bjDN1J7LPEYfc\n\tX4Qu", "X-Received": "by 2002:a05:6000:2c08:b0:47f:8d71:210a with SMTP id\n\tffacd0b85a97d-47fd72b8dc4mr26597063f8f.15.1785762978543; \n\tMon, 03 Aug 2026 06:16:18 -0700 (PDT)", "From": "David Plowman <david.plowman@raspberrypi.com>", "To": "libcamera-devel@lists.libcamera.org", "Cc": "David Plowman <david.plowman@raspberrypi.com>", "Subject": "[PATCH v3 1/1] gstreamer: Prefer non-raw (i.e. non-Bayer) formats in\n\tcaps negotiation", "Date": "Mon, 3 Aug 2026 14:07:47 +0100", "Message-ID": "<20260803131616.11292-2-david.plowman@raspberrypi.com>", "X-Mailer": "git-send-email 2.47.3", "In-Reply-To": "<20260803131616.11292-1-david.plowman@raspberrypi.com>", "References": "<20260803131616.11292-1-david.plowman@raspberrypi.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "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>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "Existing code was preferring fixed size outputs over ones with ranges,\nresulting in raw output formats being preferred even when applications\n(\"cheese\" is one such example) aren't expecting them and subsequently\nfail.\n\nThe revised version still prefers fixed size outputs over ones with\nranges, but will prefer non-raw (meaning non-Bayer) formats where\nthese are available. This still allows applications that explicitly\nrequest raw formats to get them, but other applications will normally\nget non-raw formats which they handle better.\n\nFixes, for example, \"cheese\" on Raspberry Pi.\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi.com>\n---\n src/gstreamer/gstlibcamera-utils.cpp | 115 +++++++++++++++++++--------\n src/gstreamer/gstlibcamera-utils.h | 2 +-\n src/gstreamer/gstlibcamerasrc.cpp | 3 +-\n 3 files changed, 87 insertions(+), 33 deletions(-)", "diff": "diff --git a/src/gstreamer/gstlibcamera-utils.cpp b/src/gstreamer/gstlibcamera-utils.cpp\nindex 6541d478c..996467ef4 100644\n--- a/src/gstreamer/gstlibcamera-utils.cpp\n+++ b/src/gstreamer/gstlibcamera-utils.cpp\n@@ -8,6 +8,7 @@\n \n #include \"gstlibcamera-utils.h\"\n \n+#include <optional>\n #include <string>\n \n #include <libcamera/control_ids.h>\n@@ -435,61 +436,111 @@ gst_libcamera_stream_configuration_to_caps(const StreamConfiguration &stream_cfg\n \treturn caps;\n }\n \n-void gst_libcamera_configure_stream_from_caps(StreamConfiguration &stream_cfg,\n+/*\n+ * We will want to distinguish between caps structures corresponding to\n+ * raw and non-raw (processed) output images, as this will help inform\n+ * our choice of preferred format.\n+ *\n+ * We identify Bayer as being the principle \"raw\" format here, but note\n+ * that we are considering greyscale (images with one component) to be\n+ * raw too (e.g. a raw monochrome sensor).\n+ */\n+static bool\n+gst_libcamera_structure_is_raw_capture(const GstStructure *s)\n+{\n+\tif (gst_structure_has_name(s, \"video/x-bayer\"))\n+\t\treturn true;\n+\n+\tif (gst_structure_has_name(s, \"video/x-raw\")) {\n+\t\tconst gchar *format = gst_structure_get_string(s, \"format\");\n+\t\tif (!format)\n+\t\t\treturn false;\n+\n+\t\tconst GstVideoFormatInfo *finfo =\n+\t\t\tgst_video_format_get_info(gst_video_format_from_string(format));\n+\n+\t\treturn finfo->n_components == 1;\n+\t}\n+\n+\treturn false;\n+}\n+\n+/*\n+ * Data recorded for each caps structure candidate, used to rank them\n+ * against each other via operator<().\n+ */\n+struct CapsCandidate {\n+\tguint index;\n+\tbool is_raw;\n+\tbool is_fixed;\n+\tguint delta;\n+\n+\tbool operator<(const CapsCandidate &other) const\n+\t{\n+\t\t/*\n+\t\t * Raw formats are likely to be useful only to applications that\n+\t\t * specifically ask for them. Other applications will typically be\n+\t\t * unable to handle them and fail, so prefer non-raw candidates.\n+\t\t */\n+\t\tif (is_raw != other.is_raw)\n+\t\t\treturn !is_raw;\n+\n+\t\t/* Prefer a reliable fixed value over a range. */\n+\t\tif (is_fixed != other.is_fixed)\n+\t\t\treturn is_fixed;\n+\n+\t\t/* Otherwise the closest size match wins. */\n+\t\treturn delta < other.delta;\n+\t}\n+};\n+\n+bool gst_libcamera_configure_stream_from_caps(StreamConfiguration &stream_cfg,\n \t\t\t\t\t GstCaps *caps, GstVideoTransferFunction *transfer)\n {\n \tGstVideoFormat gst_format = pixel_format_to_gst_format(stream_cfg.pixelFormat);\n \tguint i;\n-\tgint best_fixed = -1, best_in_range = -1;\n \tGstStructure *s;\n \n-\t/*\n-\t * These are delta weight computed from:\n-\t * ABS(width - stream_cfg.size.width) * ABS(height - stream_cfg.size.height)\n-\t */\n-\tguint best_fixed_delta = G_MAXUINT;\n-\tguint best_in_range_delta = G_MAXUINT;\n-\n \t/* First fixate the caps using default configuration value. */\n \tg_assert(gst_caps_is_writable(caps));\n \n-\t/* Lookup the structure for a close match to the stream_cfg.size */\n+\t/*\n+\t * Build a candidate for every available caps structure, and keep\n+\t * track of the best one seen so far, as ranked by\n+\t * CapsCandidate::operator<().\n+\t */\n+\tstd::optional<CapsCandidate> best;\n+\n \tfor (i = 0; i < gst_caps_get_size(caps); i++) {\n \t\ts = gst_caps_get_structure(caps, i);\n \t\tgint width, height;\n-\t\tguint delta;\n+\t\tbool is_fixed = gst_structure_has_field_typed(s, \"width\", G_TYPE_INT) &&\n+\t\t\t\tgst_structure_has_field_typed(s, \"height\", G_TYPE_INT);\n \n-\t\tif (gst_structure_has_field_typed(s, \"width\", G_TYPE_INT) &&\n-\t\t gst_structure_has_field_typed(s, \"height\", G_TYPE_INT)) {\n+\t\tif (is_fixed) {\n \t\t\tgst_structure_get_int(s, \"width\", &width);\n \t\t\tgst_structure_get_int(s, \"height\", &height);\n-\n-\t\t\tdelta = ABS(width - (gint)stream_cfg.size.width) * ABS(height - (gint)stream_cfg.size.height);\n-\n-\t\t\tif (delta < best_fixed_delta) {\n-\t\t\t\tbest_fixed_delta = delta;\n-\t\t\t\tbest_fixed = i;\n-\t\t\t}\n \t\t} else {\n \t\t\tgst_structure_fixate_field_nearest_int(s, \"width\", stream_cfg.size.width);\n \t\t\tgst_structure_fixate_field_nearest_int(s, \"height\", stream_cfg.size.height);\n \t\t\tgst_structure_get_int(s, \"width\", &width);\n \t\t\tgst_structure_get_int(s, \"height\", &height);\n+\t\t}\n \n-\t\t\tdelta = ABS(width - (gint)stream_cfg.size.width) * ABS(height - (gint)stream_cfg.size.height);\n+\t\tguint delta = ABS(width - (gint)stream_cfg.size.width) * ABS(height - (gint)stream_cfg.size.height);\n \n-\t\t\tif (delta < best_in_range_delta) {\n-\t\t\t\tbest_in_range_delta = delta;\n-\t\t\t\tbest_in_range = i;\n-\t\t\t}\n-\t\t}\n+\t\tCapsCandidate candidate{ i, gst_libcamera_structure_is_raw_capture(s), is_fixed, delta };\n+\n+\t\tif (!best || candidate < *best)\n+\t\t\tbest = candidate;\n+\t}\n+\n+\tif (!best) {\n+\t\tGST_WARNING(\"Failed to find a suitable caps structure to configure the stream\");\n+\t\treturn false;\n \t}\n \n-\t/* Prefer reliable fixed value over ranges */\n-\tif (best_fixed >= 0)\n-\t\ts = gst_caps_get_structure(caps, best_fixed);\n-\telse\n-\t\ts = gst_caps_get_structure(caps, best_in_range);\n+\ts = gst_caps_get_structure(caps, best->index);\n \n \tif (gst_structure_has_name(s, \"video/x-raw\")) {\n \t\tconst gchar *format = gst_video_format_to_string(gst_format);\n@@ -529,6 +580,8 @@ void gst_libcamera_configure_stream_from_caps(StreamConfiguration &stream_cfg,\n \n \t\tstream_cfg.colorSpace = colorspace_from_colorimetry(colorimetry, transfer);\n \t}\n+\n+\treturn true;\n }\n \n void gst_libcamera_get_framerate_from_caps(GstCaps *caps,\ndiff --git a/src/gstreamer/gstlibcamera-utils.h b/src/gstreamer/gstlibcamera-utils.h\nindex 35df56fb9..5ce0e839c 100644\n--- a/src/gstreamer/gstlibcamera-utils.h\n+++ b/src/gstreamer/gstlibcamera-utils.h\n@@ -18,7 +18,7 @@\n GstCaps *gst_libcamera_stream_formats_to_caps(const libcamera::StreamFormats &formats);\n GstCaps *gst_libcamera_stream_configuration_to_caps(const libcamera::StreamConfiguration &stream_cfg,\n \t\t\t\t\t\t GstVideoTransferFunction transfer);\n-void gst_libcamera_configure_stream_from_caps(libcamera::StreamConfiguration &stream_cfg,\n+bool gst_libcamera_configure_stream_from_caps(libcamera::StreamConfiguration &stream_cfg,\n \t\t\t\t\t GstCaps *caps, GstVideoTransferFunction *transfer);\n void gst_libcamera_get_framerate_from_caps(GstCaps *caps, GstStructure *element_caps);\n void gst_libcamera_clamp_and_set_frameduration(libcamera::ControlList &controls,\ndiff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp\nindex 9061f9163..66b31ce82 100644\n--- a/src/gstreamer/gstlibcamerasrc.cpp\n+++ b/src/gstreamer/gstlibcamerasrc.cpp\n@@ -605,7 +605,8 @@ gst_libcamera_src_negotiate(GstLibcameraSrc *self)\n \n \t\t/* Fixate caps and configure the stream. */\n \t\tcaps = gst_caps_make_writable(caps);\n-\t\tgst_libcamera_configure_stream_from_caps(stream_cfg, caps, &transfer[i]);\n+\t\tif (!gst_libcamera_configure_stream_from_caps(stream_cfg, caps, &transfer[i]))\n+\t\t\treturn false;\n \t\tgst_libcamera_get_framerate_from_caps(caps, element_caps);\n \t}\n \n", "prefixes": [ "v3", "1/1" ] }