From patchwork Thu Nov 3 10:40:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 17750 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 469BAC3285 for ; Thu, 3 Nov 2022 10:40:37 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 75A7163080; Thu, 3 Nov 2022 11:40:35 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1667472035; bh=PPn+BaSabQ56UJEYn4f/uNz4wcJVOI+k1B0UOdn8sHU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=pJgnHzzEmI+ML/YXjg5SiqbuMUMIsozO6kZ+34Pz2qMSqEOB/H/xlQNSLjO1JTG1M Q+4SGe/aazUQiAf843jQcHv5dtyu8pAa9EXmKqo5F8IHblsLQvTXUCeOlM/N8b4mFR 9ojiENXdB9F13PZYOdRo+EgC4EB0YVtSGkAZYivuylz5sA//W2V3RTdPioeXUovy3J WjNbmyAfs5QMYyu9EIeZsEChOtSqnuy6YHHfSdjYSkgAyzzqa4WCw+svMTy3Evqffn pyjQeNm0u2sLAQP7IXulpi3HcoZ3HblTrX6uiIP1rQttJzjRtyFrkRf6kWjz7mRko8 gsVTyTK1Jo0zw== Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 872E763037 for ; Thu, 3 Nov 2022 11:40:33 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="Ila0xqbC"; dkim-atps=neutral Received: by mail-wm1-x32a.google.com with SMTP id p13-20020a05600c468d00b003cf8859ed1bso914506wmo.1 for ; Thu, 03 Nov 2022 03:40:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+brAMRKPod66CqD4qVrWYNSEjZMifCSp+0eKyFT0UZY=; b=Ila0xqbClBgK5QEI4YXp8HnDhKcRn40pOhm2acnsb8t4TPR+a7ukePjTUUYrRiX1oS VuOw47mHvHcn0f0sWawz3zagsgNV8aPK0sRmVBMuAP5PZoCzm9993pl0oj/KnnaFc8nf NJv8jSeLulwD2FOkSQYgU7jf88G7AJBZYj8fswlwqxZZirE9KoAOIBsvD9gAfe2Q6j9T TqGaa2ZlFAy44xMof6eDqpB2opJliwZ4MTiPG0KWwz+36TjJBqJ1ciOsCMwKF0BaQtec mtK2j0FSXwf3b2NhBegHMhZAFlk5UfppLjy2rc1TmJpwMmu1wqSRmERGvWig+1nyG8aZ lvRw== 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 :subject:date:message-id:reply-to; bh=+brAMRKPod66CqD4qVrWYNSEjZMifCSp+0eKyFT0UZY=; b=WIBsHJzV/Z7SpcNdxH1jluepGaTQJkFWuiqcc+YoASiqXwHcq3XsaIHTa2NfO/aVXc vi++Jc80L4ZL/Vn35AZB9DnTqjtQVcsBdLMyfgxRiQ5ltPVS8s3xqS5QDF1cKMMHzsNT 7hyi5ksSQA77MsjYwU4JwNqhwhtL2SDnuPYn9Rdy0UhzectU13ogqgsomGOUkatsL0fx BT0hdikRYSRD4k5eBwSg80o6624Z8FTpyUrC9lxkK1pC4oZ1LsFACAMHdY5lpPY8Hq43 CFyo1pwVAuRbybEiE9pGZizGJMkF+SK/h0w6nvP5utiqst5aocuyDM+qJco0/d05cGEn 8Zaw== X-Gm-Message-State: ACrzQf04Yc5R9WDmeBY26vB1lc+a8JgT8ThVF5qAYqXvghrURkDLVewK BqmMk32DajiGjpeTdkpZUo9DfEbgnDScPQ== X-Google-Smtp-Source: AMsMyM57U7R5FqeLGyieu8PDE9O+XpM4+Q8NCT+wsxKNwgF5Hy2W0HfTJCZ5yVScpJ3yLCIaZFIxYw== X-Received: by 2002:a05:600c:500e:b0:3cf:89c5:c233 with SMTP id n14-20020a05600c500e00b003cf89c5c233mr2825163wmr.28.1667472032836; Thu, 03 Nov 2022 03:40:32 -0700 (PDT) Received: from pi4-davidp.lan (plowpeople3.plus.com. [80.229.223.72]) by smtp.gmail.com with ESMTPSA id f15-20020a5d50cf000000b0022e36c1113fsm516363wrt.13.2022.11.03.03.40.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 03:40:32 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 3 Nov 2022 10:40:26 +0000 Message-Id: <20221103104027.4197-2-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221103104027.4197-1-david.plowman@raspberrypi.com> References: <20221103104027.4197-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 1/2] libcamera: bayer_format: Add toMbusCode method 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: David Plowman via libcamera-devel From: David Plowman Reply-To: David Plowman Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This makes it easier to perform transformations on Bayer type mbus codes by converting them to a BayerFormat, doing the transform, and then converting them back again. Signed-off-by: David Plowman --- include/libcamera/internal/bayer_format.h | 1 + src/libcamera/bayer_format.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/include/libcamera/internal/bayer_format.h b/include/libcamera/internal/bayer_format.h index 78ba3969..09f756c4 100644 --- a/include/libcamera/internal/bayer_format.h +++ b/include/libcamera/internal/bayer_format.h @@ -47,6 +47,7 @@ public: } static const BayerFormat &fromMbusCode(unsigned int mbusCode); + unsigned int toMbusCode(bool &valid) const; bool isValid() const { return bitDepth != 0; } std::string toString() const; diff --git a/src/libcamera/bayer_format.cpp b/src/libcamera/bayer_format.cpp index f27cc166..a03c58c0 100644 --- a/src/libcamera/bayer_format.cpp +++ b/src/libcamera/bayer_format.cpp @@ -226,6 +226,18 @@ const BayerFormat &BayerFormat::fromMbusCode(unsigned int mbusCode) return it->second; } +/** + * \brief Retrieve the media bus code corresponding this this BayerFormat + * \param[out] valid Set to true if a matching media bus code was found, else false + */ +unsigned int BayerFormat::toMbusCode(bool &valid) const +{ + auto it = std::find_if(mbusCodeToBayer.begin(), mbusCodeToBayer.end(), + [this](const auto &i) { return i.second == *this; }); + valid = it != mbusCodeToBayer.end(); + return valid ? it->first : 0; +} + /** * \fn BayerFormat::isValid() * \brief Return whether a BayerFormat is valid From patchwork Thu Nov 3 10:40:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 17751 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 C12FABD16B for ; Thu, 3 Nov 2022 10:40:38 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 355796307E; Thu, 3 Nov 2022 11:40:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1667472038; bh=LaIhUXXzGrnIPHHMkC4s4z6uzHZMYuLgbu2UnjfMX2M=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=iLAMuaq3fre2wmwCsgRncaMJgVzeFKnf0/qszZ7fobRMNyFjP9Q+urPUnOsuEO1K+ tgIEqFy0spqVoJGXUIF8EIIlUXo9LMnvorLkrtzPwXboRlRnpTusGMWUWt9ME2YDh7 h1hfNKh4bG/ZEim1pPCf3qna/3TuZ/Zi0h7enKZcvSKwzSV4tUlSpTrm1/ztEjcbQO XjqFt1BnFkrYFn5MTwZ2fwlrzrOzVycuIHZq2t2025vxDRCnDF4IVyU7oJGoVdYMYM 96HVYPJmuwgJT/uZe10qgNxrQ/T6wj0C4CZ94RgmiSzDDouxSYcA8qj9BPSdAhcFbv QNVS71vI2rKzQ== Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 97A4963079 for ; Thu, 3 Nov 2022 11:40:34 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="S/gV4A4m"; dkim-atps=neutral Received: by mail-wr1-x433.google.com with SMTP id k8so2149189wrh.1 for ; Thu, 03 Nov 2022 03:40:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4oWVlNVD+BhXaNoojUjft82Db7px57l5q5o4NalLCOU=; b=S/gV4A4mnys4csuvqcw+/zWhqKSmt1MMQl8dGXPGtJiCE2K8r1i3qhc5bYNJhadVKI Mhkg7j04y39+xiYLCIh6m/DRGjR9DwFOFsp2Prx4XapjdN73aCfkDL0b+m/4JzBxLb4a iVAdTsZm8b5uxfg65EhP+cOXM4KqhfXbg1oRN5oP6Q+PyHd1Fo0EFVTumbw8oMOsl2nl lkGisv82mxo8aX4oRpj1lc90Pevkir1nvsVEwcFgw31L9lV4LWQSVtW4ZJrZmoN7F5z9 wvYIiqjLBH8xqMYVyHTMdqUItY5+y2bNxlGFMd93HOpURwpf5wyFiHjfix+8lJNXUNYU C8VA== 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 :subject:date:message-id:reply-to; bh=4oWVlNVD+BhXaNoojUjft82Db7px57l5q5o4NalLCOU=; b=IUhUI5bxG+tbJI++YFFn6D+h78jYL7BXyYv/lYM3tyca7PonxBF2ImDwGUC6glwKOT 2Vj0Y5LeHs7fjCda099jpTce1WHomwqJnBp5Uz64m81sJp01iTBVERB164jfWvyks0Bi O6TglgyxAqgAdFsCNqextQkr1m2FK3bHHvN3qp0zePefbUfAkSt2OXUmgzoRXf2T7u44 Jqh5HisI2BzcNe3yIT0/x4MBX58rWb1JLQrRiedYgieDD8d4hrWQ3KaUFH6iTfcD29RB Wcly/PjLk1pTUdW6/Vyl5plfEZjAUeAH704+nGNszpWbhh7OTWRgENamzqkU10NVzmEX +bbA== X-Gm-Message-State: ACrzQf0R2rER89NC/FfaVnBnU0lPuRaZ/saYZI9kmZ7Y0t1ta/3esG3l THQLJCYJFuCmqTgtBo3BT7VWbcYDuiMhUA== X-Google-Smtp-Source: AMsMyM7RQlK85eyj6IuOjngut6Q+KBcr97ekiXL6FZvhg+dcXIzfIlAGMvNOQzL/j+P5oPPtXyZLcg== X-Received: by 2002:a5d:6b03:0:b0:236:d441:8f4e with SMTP id v3-20020a5d6b03000000b00236d4418f4emr11806556wrw.123.1667472033923; Thu, 03 Nov 2022 03:40:33 -0700 (PDT) Received: from pi4-davidp.lan (plowpeople3.plus.com. [80.229.223.72]) by smtp.gmail.com with ESMTPSA id f15-20020a5d50cf000000b0022e36c1113fsm516363wrt.13.2022.11.03.03.40.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 03:40:33 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 3 Nov 2022 10:40:27 +0000 Message-Id: <20221103104027.4197-3-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221103104027.4197-1-david.plowman@raspberrypi.com> References: <20221103104027.4197-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 2/2] libcamera: camera_sensor: Do not clear camera flips when listing formats 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: David Plowman via libcamera-devel From: David Plowman Reply-To: David Plowman Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Previously the code used to clear the camnera's h and v flip bits when enumerating the supported formats so as to obtain any Bayer formats in the sensor's native (untransformed) orientation. However this fails when the camera is already in use elsewhere. Instead, we query the current state of the flip bits and transform the formats - which we obtain in their flipped orientation - back into their native orientation to be stored. Signed-off-by: David Plowman --- src/libcamera/camera_sensor.cpp | 51 +++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp index 572a313a..6670dfb9 100644 --- a/src/libcamera/camera_sensor.cpp +++ b/src/libcamera/camera_sensor.cpp @@ -19,6 +19,8 @@ #include +#include + #include "libcamera/internal/bayer_format.h" #include "libcamera/internal/camera_lens.h" #include "libcamera/internal/camera_sensor_properties.h" @@ -108,18 +110,51 @@ int CameraSensor::init() return ret; /* - * Clear any flips to be sure we get the "native" Bayer order. This is - * harmless for sensors where the flips don't affect the Bayer order. + * We want to get the native mbus codes for the sensor, without any flips. + * We can't clear any flips here, so we have to read the current values + * (if the flip controls exist), decide whether they actually modify any + * output Bayer pattern, and finally undo their effect on the formats. + * + * First, check if the flip controls exist and if so read them. */ ControlList ctrls(subdev_->controls()); - if (subdev_->controls().find(V4L2_CID_HFLIP) != subdev_->controls().end()) - ctrls.set(V4L2_CID_HFLIP, 0); - if (subdev_->controls().find(V4L2_CID_VFLIP) != subdev_->controls().end()) - ctrls.set(V4L2_CID_VFLIP, 0); - subdev_->setControls(&ctrls); + std::vector flipCtrlIds; + bool hasHflip = subdev_->controls().find(V4L2_CID_HFLIP) != subdev_->controls().end(); + bool hasVflip = subdev_->controls().find(V4L2_CID_VFLIP) != subdev_->controls().end(); + if (hasHflip) + flipCtrlIds.push_back(V4L2_CID_HFLIP); + if (hasVflip) + flipCtrlIds.push_back(V4L2_CID_VFLIP); + ControlList flipCtrls = subdev_->getControls(flipCtrlIds); + + /* Now construct a transform that would undo any flips. */ + Transform transform = Transform::Identity; + if (hasHflip && flipCtrls.get(V4L2_CID_HFLIP).get()) { + const struct v4l2_query_ext_ctrl *extCtrl = subdev_->controlInfo(V4L2_CID_HFLIP); + if (extCtrl->flags & V4L2_CTRL_FLAG_MODIFY_LAYOUT) + transform |= Transform::HFlip; + } + if (hasVflip && flipCtrls.get(V4L2_CID_VFLIP).get()) { + const struct v4l2_query_ext_ctrl *extCtrl = subdev_->controlInfo(V4L2_CID_VFLIP); + if (extCtrl->flags & V4L2_CTRL_FLAG_MODIFY_LAYOUT) + transform |= Transform::VFlip; + } + + /* Finally get the formats, and apply the transform to the mbus codes. */ + auto formats = subdev_->formats(pad_); + for (const auto &format : formats) { + unsigned int mbusCode = format.first; + BayerFormat bayerFormat = BayerFormat::fromMbusCode(mbusCode); + bool valid = true; + + if (bayerFormat.isValid()) + mbusCode = bayerFormat.transform(transform).toMbusCode(valid); + + if (valid) + formats_[mbusCode] = std::move(format.second); + } /* Enumerate, sort and cache media bus codes and sizes. */ - formats_ = subdev_->formats(pad_); if (formats_.empty()) { LOG(CameraSensor, Error) << "No image format found"; return -EINVAL;