From patchwork Tue Aug 9 14:35:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rishikesh Donadkar X-Patchwork-Id: 17041 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id EF8E4C3272 for ; Tue, 9 Aug 2022 14:36:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AAC3863326; Tue, 9 Aug 2022 16:36:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1660055786; bh=T0WmI2RWLr2c6dBiNrTdJ4MTRV+QU5vFXoEu5EEgxBs=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=s/zaw+dvJpx0JImHOqX28ogYeQSXxpP4GKbyl4kTLmtlVWUsWzIsTHxi5e3P7m/KY H5eQFynZHZSVJfSb2Q3GJskwamLYKPjP98JcDJ3+k40e/TAz7IKkqh8HzTI1Yk7X0S x4zF/7FHzTubtxxOoc41mxfTEBRolgSbY+ZTz5YS13SjpooPCivCwtGs7F9xnvozFj A1w0O4FRVL7FpOfgd9f5WlZTbt8LccML0h1ifraZYI1yD3ZDsXnCWtKy4rH5RbbBC+ bMZVtbtqJ5eLgSOJhuGpqPcIHqyLzoBjvEhC8+w9FbF2w9+7WQQvi17ZpNgVwdJRAD iEzfcskeQ77qw== Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0732A63326 for ; Tue, 9 Aug 2022 16:36:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="MaUYhful"; dkim-atps=neutral Received: by mail-pj1-x102f.google.com with SMTP id w11-20020a17090a380b00b001f73f75a1feso6981206pjb.2 for ; Tue, 09 Aug 2022 07:36:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=3w3bA9gQ8JT3rGEZQtYRbMOQfEpMY/08pD8JogJ972M=; b=MaUYhful8PdKqsAHkLoW/w031SD/VvcBfeCUmQdcp/SaGCevl0LMT9mo3Aog4EPSrZ UpZHTFXSNChUEtJWmEMv/TAO171QGXafPjJ5OLe5/BGVro5LeuAvRz3jc0dBswY2VSOn 7dfjydR+VUfAEdyzZlyZhhUUmgecs/r+aY1tVymn3BiW/HJJ4z7FiHI4AYjVCLiY31o9 4LM541stl6Sw+vGkgO9oXQ0QtJRWss7FKPtctSFR/YrUtASkMsKfLxya2U8ra+os6YQD HAtGj7VtgGw+pk5M7AfvJbtNv41VGAgsY3b/s76RlPnr3C57McjFHQAgscJT9CotcQci 3fSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=3w3bA9gQ8JT3rGEZQtYRbMOQfEpMY/08pD8JogJ972M=; b=EWAF09JpcBFVYuGEEZj8ljOEaCz0wvQTUQEfoLvISxIwOTNeOPMPuhcGnhK5l4F+RG Pzzo9CtmsFsoCRL/EDDKXrDACvx6Q/e7Zd3ha3Bxws4Nzdd1Vstw76K5B05JvqXPJDzW wzpTeneXkkXUIUQsDpgQyYecV/JufvdCMVMbK1JYCLiflqMAAB8C+rSIpb2vXnuE/Mt0 D0l6Umm0+XDJf7S+rNFQDmPAMrGxWOoFfuwaUyizocZMn8rAvcZPJzhOH/loNcvolefp gpqg9Mmk3koAJ0t2vsVkg44auQe1VTZrPE+jWyfxaX3qDLqmtkPHymzkLYzYbSSCkSIE EM3Q== X-Gm-Message-State: ACgBeo0LefRmd2hGURGMDSPIvSzQjMS2A+eF8A3xS1ULyYUE84uIQ9H/ 9H4BcKSQbkmjWvNIPEMV1qnptCARPtLMiiXa X-Google-Smtp-Source: AA6agR5+xHt/bgRsE6pLfqn+qRCwhrN4SrtSXTF4bXfaGD03Zacnh4T74y1Q7usPPTQb3NzBf7sVTA== X-Received: by 2002:a17:90b:4c12:b0:1f5:958:c313 with SMTP id na18-20020a17090b4c1200b001f50958c313mr35038462pjb.6.1660055783288; Tue, 09 Aug 2022 07:36:23 -0700 (PDT) Received: from localhost.localdomain ([49.36.99.220]) by smtp.googlemail.com with ESMTPSA id h12-20020a17090ac38c00b001f4fb21c11asm10269264pjt.21.2022.08.09.07.36.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 07:36:23 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Tue, 9 Aug 2022 20:05:31 +0530 Message-Id: <20220809143531.7473-3-rishikeshdonadkar@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220809143531.7473-1-rishikeshdonadkar@gmail.com> References: <20220809143531.7473-1-rishikeshdonadkar@gmail.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/2] gstreamer: Add multiple colorimetry support X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Rishikesh Donadkar via libcamera-devel From: Rishikesh Donadkar Reply-To: Rishikesh Donadkar Cc: Rishikesh Donadkar , nicolas.dufresne@collabora.com, vedantparanjape160201@gmail.com Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This patch provides the following: - Add support to handle colorimetry in the GStreamer->libcamera direction. - Add support for multiple colorimetry. The function colorspace_from_colorimetry() takes in a GstVideoColorimetry and returns libcamera::ColorSpace Append copy of the structure with best suitable resolution into a fresh new caps and normalize the caps using gst_caps_normalize(). This will return caps where the colorimetry list is expanded. The ncaps will contain as many structures as the number of colorimetry specified in the GStreamer pipeline. Iterate over each structure in the ncaps, retrieve the colorimetry string, convert to colorspace using the function colorspace_from_colorimetry() and validate the camera configuration.Retrieve the colorspace after validation, convert to colorimetry and check if the it is same as the colorimetry requested. If none of the colorimetry requested is supported by the camera (i.e. not the same after validation) then set the stream_cfg to the previous configuration that was present before trying new colorimetry. Signed-off-by: Rishikesh Donadkar Signed-off-by: Umang Jain --- src/gstreamer/gstlibcamera-utils.cpp | 152 ++++++++++++++++++++++++++- src/gstreamer/gstlibcamera-utils.h | 4 +- src/gstreamer/gstlibcamerasrc.cpp | 3 +- 3 files changed, 153 insertions(+), 6 deletions(-) diff --git a/src/gstreamer/gstlibcamera-utils.cpp b/src/gstreamer/gstlibcamera-utils.cpp index dbb47c5a..078b9343 100644 --- a/src/gstreamer/gstlibcamera-utils.cpp +++ b/src/gstreamer/gstlibcamera-utils.cpp @@ -104,6 +104,93 @@ colorimetry_from_colorspace(const ColorSpace &colorSpace) return colorimetry; } +static ColorSpace +colorspace_from_colorimetry(const GstVideoColorimetry &colorimetry) +{ + ColorSpace colorspace = ColorSpace::Raw; + + switch (colorimetry.primaries) { + case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN: + /* Unknown primaries map to raw colorspace in GStreamer */ + return ColorSpace::Raw; + case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M: + colorspace.primaries = ColorSpace::Primaries::Smpte170m; + break; + case GST_VIDEO_COLOR_PRIMARIES_BT709: + colorspace.primaries = ColorSpace::Primaries::Rec709; + break; + case GST_VIDEO_COLOR_PRIMARIES_BT2020: + colorspace.primaries = ColorSpace::Primaries::Rec2020; + break; + default: + GST_WARNING("Colorimetry primaries %d not mapped in gstlibcamera", + colorimetry.primaries); + return ColorSpace::Raw; + } + + switch (colorimetry.transfer) { + /* Transfer function mappings inspired from v4l2src plugin */ + case GST_VIDEO_TRANSFER_GAMMA18: + case GST_VIDEO_TRANSFER_GAMMA20: + case GST_VIDEO_TRANSFER_GAMMA22: + case GST_VIDEO_TRANSFER_GAMMA28: + GST_WARNING("GAMMA 18, 20, 22, 28 transfer functions not supported"); + /* fallthrough */ + case GST_VIDEO_TRANSFER_GAMMA10: + colorspace.transferFunction = ColorSpace::TransferFunction::Linear; + break; + case GST_VIDEO_TRANSFER_SRGB: + colorspace.transferFunction = ColorSpace::TransferFunction::Srgb; + break; + case GST_VIDEO_TRANSFER_BT601: + case GST_VIDEO_TRANSFER_BT2020_12: + case GST_VIDEO_TRANSFER_BT2020_10: + case GST_VIDEO_TRANSFER_BT709: + colorspace.transferFunction = ColorSpace::TransferFunction::Rec709; + break; + default: + GST_WARNING("Colorimetry transfer function %d not mapped in gstlibcamera", + colorimetry.transfer); + return ColorSpace::Raw; + } + + switch (colorimetry.matrix) { + case GST_VIDEO_COLOR_MATRIX_RGB: + colorspace.ycbcrEncoding = ColorSpace::YcbcrEncoding::None; + break; + /* FCC is about the same as BT601 with less digits */ + case GST_VIDEO_COLOR_MATRIX_FCC: + case GST_VIDEO_COLOR_MATRIX_BT601: + colorspace.ycbcrEncoding = ColorSpace::YcbcrEncoding::Rec601; + break; + case GST_VIDEO_COLOR_MATRIX_BT709: + colorspace.ycbcrEncoding = ColorSpace::YcbcrEncoding::Rec709; + break; + case GST_VIDEO_COLOR_MATRIX_BT2020: + colorspace.ycbcrEncoding = ColorSpace::YcbcrEncoding::Rec2020; + break; + default: + GST_WARNING("Colorimetry matrix %d not mapped in gstlibcamera", + colorimetry.matrix); + return ColorSpace::Raw; + } + + switch (colorimetry.range) { + case GST_VIDEO_COLOR_RANGE_0_255: + colorspace.range = ColorSpace::Range::Full; + break; + case GST_VIDEO_COLOR_RANGE_16_235: + colorspace.range = ColorSpace::Range::Limited; + break; + default: + GST_WARNING("Colorimetry range %d not mapped in gstlibcamera", + colorimetry.range); + return ColorSpace::Raw; + } + + return colorspace; +} + static GstVideoFormat pixel_format_to_gst_format(const PixelFormat &format) { @@ -215,13 +302,47 @@ gst_libcamera_stream_configuration_to_caps(const StreamConfiguration &stream_cfg return caps; } +static void +configure_colorspace_from_caps(StreamConfiguration &stream_cfg, + GstStructure *s) +{ + if (gst_structure_has_field(s, "colorimetry")) { + const gchar *colorimetry_str = gst_structure_get_string(s, "colorimetry"); + GstVideoColorimetry colorimetry; + + if (!gst_video_colorimetry_from_string(&colorimetry, colorimetry_str)) + g_critical("Invalid colorimetry %s", colorimetry_str); + + stream_cfg.colorSpace = colorspace_from_colorimetry(colorimetry); + /* Check if colorimetry had any identifiers which did not map */ + if (colorimetry.primaries != GST_VIDEO_COLOR_PRIMARIES_UNKNOWN && + stream_cfg.colorSpace == ColorSpace::Raw) { + GST_ERROR("One or more identifiers could not be mapped for %s colorimetry", + colorimetry_str); + stream_cfg.colorSpace = std::nullopt; + } + } +} + +static gboolean +check_colorspace(const ColorSpace colorSpace, const gchar *colorimetry_old) +{ + GstVideoColorimetry colorimetry = colorimetry_from_colorspace(colorSpace); + g_autofree gchar *colorimetry_new = gst_video_colorimetry_to_string(&colorimetry); + if (!g_strcmp0(colorimetry_old, colorimetry_new)) { + return true; + } + return false; +} + void -gst_libcamera_configure_stream_from_caps(StreamConfiguration &stream_cfg, +gst_libcamera_configure_stream_from_caps(CameraConfiguration &cam_cfg, + StreamConfiguration &stream_cfg, GstCaps *caps) { GstVideoFormat gst_format = pixel_format_to_gst_format(stream_cfg.pixelFormat); guint i; - gint best_fixed = -1, best_in_range = -1; + gint best_fixed = -1, best_in_range = -1, colorimetry_index = -1; GstStructure *s; /* @@ -267,10 +388,13 @@ gst_libcamera_configure_stream_from_caps(StreamConfiguration &stream_cfg, } /* Prefer reliable fixed value over ranges */ - if (best_fixed >= 0) + if (best_fixed >= 0) { s = gst_caps_get_structure(caps, best_fixed); - else + colorimetry_index = best_fixed; + } else { s = gst_caps_get_structure(caps, best_in_range); + colorimetry_index = best_in_range; + } if (gst_structure_has_name(s, "video/x-raw")) { const gchar *format = gst_video_format_to_string(gst_format); @@ -293,6 +417,26 @@ gst_libcamera_configure_stream_from_caps(StreamConfiguration &stream_cfg, gst_structure_get_int(s, "height", &height); stream_cfg.size.width = width; stream_cfg.size.height = height; + + /* Create new caps, copy the structure with best resolutions + * and normalize the caps. + */ + GstCaps *ncaps = gst_caps_copy_nth(caps, colorimetry_index); + ncaps = gst_caps_normalize(ncaps); + + /* Configure Colorimetry */ + StreamConfiguration dup_stream_cfg = stream_cfg; + for (i = 0; i < gst_caps_get_size(ncaps); i++) { + GstStructure *ns = gst_caps_get_structure(ncaps, i); + configure_colorspace_from_caps(stream_cfg, ns); + g_autofree const gchar *colorimetry_old = gst_structure_get_string(ns, "colorimetry"); + if (cam_cfg.validate() != CameraConfiguration::Invalid) { + if (check_colorspace(stream_cfg.colorSpace.value(), colorimetry_old)) + break; + else + stream_cfg = dup_stream_cfg; + } + } } #if !GST_CHECK_VERSION(1, 17, 1) diff --git a/src/gstreamer/gstlibcamera-utils.h b/src/gstreamer/gstlibcamera-utils.h index 164189a2..90be6abe 100644 --- a/src/gstreamer/gstlibcamera-utils.h +++ b/src/gstreamer/gstlibcamera-utils.h @@ -8,6 +8,7 @@ #pragma once +#include #include #include @@ -16,7 +17,8 @@ GstCaps *gst_libcamera_stream_formats_to_caps(const libcamera::StreamFormats &formats); GstCaps *gst_libcamera_stream_configuration_to_caps(const libcamera::StreamConfiguration &stream_cfg); -void gst_libcamera_configure_stream_from_caps(libcamera::StreamConfiguration &stream_cfg, +void gst_libcamera_configure_stream_from_caps(libcamera::CameraConfiguration &cam_cfg, + libcamera::StreamConfiguration &stream_cfg, GstCaps *caps); #if !GST_CHECK_VERSION(1, 17, 1) gboolean gst_task_resume(GstTask *task); diff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp index 16d70fea..3617170e 100644 --- a/src/gstreamer/gstlibcamerasrc.cpp +++ b/src/gstreamer/gstlibcamerasrc.cpp @@ -492,6 +492,7 @@ gst_libcamera_src_task_enter(GstTask *task, [[maybe_unused]] GThread *thread, for (gsize i = 0; i < state->srcpads_.size(); i++) { GstPad *srcpad = state->srcpads_[i]; StreamConfiguration &stream_cfg = state->config_->at(i); + CameraConfiguration &cam_cfg = *(state->config_); /* Retrieve the supported caps. */ g_autoptr(GstCaps) filter = gst_libcamera_stream_formats_to_caps(stream_cfg.formats()); @@ -503,7 +504,7 @@ gst_libcamera_src_task_enter(GstTask *task, [[maybe_unused]] GThread *thread, /* Fixate caps and configure the stream. */ caps = gst_caps_make_writable(caps); - gst_libcamera_configure_stream_from_caps(stream_cfg, caps); + gst_libcamera_configure_stream_from_caps(cam_cfg, stream_cfg, caps); } if (flow_ret != GST_FLOW_OK)