From patchwork Tue Nov 22 11:30:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 17836 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 71491BDE6B for ; Tue, 22 Nov 2022 11:30:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 68B9D6331F; Tue, 22 Nov 2022 12:30:54 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1669116654; bh=QeE2XrkMAXBq+xIM6/xevcr7Oh2p3OxWkukLjNSxJy0=; 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=GJbZOISVbPWXddqETRyGwB8uKEb5oKt8Et1/WaLolo8Jpqu2s1HshCidYavg74USN IOUuZ21avNdrDJ99b9cmn9rSMCv4dl/ZsRnF2MNQ9daqOWUJmbtYpA8HZyr8LLV/Fe tJhzO/KSDhmh/ddNODEZdpo85BGp8EEuitu/FmSjyMSaJts5IB1ErhvkoPBBJx/8NZ qQwL/u49ZGSV2uzO1NBh1Mw3B01DSdYKzgAUgbpCDtzKT9Xa9cnWJ0Ja2TJrx1jIfV mEeXnLJStc+6wPUYaUNuH2xwpfft+iRrBAskdBHY6A8yIRn1AM6GqVJbx3oCCFQgiK THDoGpwMryg2g== Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BFA80603CE for ; Tue, 22 Nov 2022 12:30:52 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="M/9yL6q4"; dkim-atps=neutral Received: by mail-wr1-x434.google.com with SMTP id x17so10789125wrn.6 for ; Tue, 22 Nov 2022 03:30:52 -0800 (PST) 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=sEHYM3NRf6pMdIrl/YwUGcydrfNxZgsax8Tl7eXR3Tw=; b=M/9yL6q45d7y25MiWKMrOI2BDQ/huTa6N7P4Q6l3nabuqTyAW/pqQj/7K9fFcaa/wX +XNsFhqK99ccKoGTG60xhsNJCNX7NQAc0mHSY5rIMt1sBPS96FtMfPozulvyVQLbcWP+ nv17JzItCZGq8dRZ2NI25Ao1zN2KVW0WYmmueF/71amRxtqGD1mO8WVw2aWeXIGdW3i1 J/4mO/CYRdzfqAjGut68k+J89kHUp+oEXCTpUdTLDC4pLhxpU3c5VneCOgervaLAE/Ls UB5S10CQ2uIzfzutDwfZ8np/aOgRm8stMcuLYbpbjrKqGG4sJq1iWx8Gn/vqlgioEcPA wpjA== 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=sEHYM3NRf6pMdIrl/YwUGcydrfNxZgsax8Tl7eXR3Tw=; b=wafOOsq0SdJDORwreTLRyKor0BVgprnRC1CCP887iYBvNgyrA0DjL/K355tfcuoCbK WW31G8F5TRPDQEXS4CRTWk1GgEZ3WjnvlyE/ACw4ntP/8VGHlT/CqnBqwMxTsQf/ABN7 2XUd+UOy5xstlPNswpkipPKY3aTWsdvYt+bpOukp5KdyUL/kskk6P/yTQeBxgzGLUBVI WS3aR+PdxA+oCVGHnWmzmtrboQ7HxtsZ9pGNy1qfWPUBwEjCSTn4pE0Yrao67aRf845e PduDBW/u7YU5a6Z+qqp9HicbMwFmpy/K0HWy+MswZKrMmJBz4cZNulTXycQ8H9PMoKMT yTYA== X-Gm-Message-State: ANoB5pkTeU26cepZGeuCZo1kF/ZHvMIzxfuT8w/iVgxthF0BMWAs845j 1+0b7M1ysuxeS1MYXKhhPL9zEkA4YMh5bA== X-Google-Smtp-Source: AA0mqf6C1SD+eXX8is+8hoaGV4b+wlI28NewVQtc7w28UdlX+1UYqZTre3NE42T1vkuvx9CuwgnSaQ== X-Received: by 2002:a5d:4984:0:b0:236:4e72:23ba with SMTP id r4-20020a5d4984000000b002364e7223bamr14343438wrq.385.1669116652146; Tue, 22 Nov 2022 03:30:52 -0800 (PST) Received: from pi4-davidp.pitowers.org ([2a00:1098:3142:14:e4a2:3070:eea4:e434]) by smtp.gmail.com with ESMTPSA id i6-20020a05600c354600b003cf894c05e4sm22713522wmq.22.2022.11.22.03.30.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 03:30:51 -0800 (PST) To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Nov 2022 11:30:48 +0000 Message-Id: <20221122113049.3951-2-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221122113049.3951-1-david.plowman@raspberrypi.com> References: <20221122113049.3951-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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 Reviewed-by: Jacopo Mondi --- include/libcamera/internal/bayer_format.h | 1 + src/libcamera/bayer_format.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/include/libcamera/internal/bayer_format.h b/include/libcamera/internal/bayer_format.h index 78ba3969..3601dccb 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() 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..fdbc4af1 100644 --- a/src/libcamera/bayer_format.cpp +++ b/src/libcamera/bayer_format.cpp @@ -226,6 +226,17 @@ const BayerFormat &BayerFormat::fromMbusCode(unsigned int mbusCode) return it->second; } +/** + * \brief Retrieve the media bus code corresponding this this BayerFormat + * \return The corresponding media bus code, or zero if none was found + */ +unsigned int BayerFormat::toMbusCode() const +{ + auto it = std::find_if(mbusCodeToBayer.begin(), mbusCodeToBayer.end(), + [this](const auto &i) { return i.second == *this; }); + return it != mbusCodeToBayer.end() ? it->first : 0; +} + /** * \fn BayerFormat::isValid() * \brief Return whether a BayerFormat is valid From patchwork Tue Nov 22 11:30:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 17837 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 11F20BDE6B for ; Tue, 22 Nov 2022 11:30:57 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C3259632F8; Tue, 22 Nov 2022 12:30:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1669116656; bh=ElKufu7CMYATkuoiRAVdVvcfz+UvtRZPr92j7IB7HfU=; 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=3BthTiot0FTJzzVSKtWhTMjsvDzK9TgdjovcCYejydMqz0uuEa1VrA1JPHy4dUgVn Iisgm6LuFy124/Bj1mzUVT3kKrlCbfm79a2YWjYXORvEppJ4IcmCYMhXHJGTLe/bLI qb3XyUjTpib0jSyMDzfvX5FpfIOC1VYsahYkBbfoUbYzgelG+bk/eXxUAxhECPk/h5 BqdXskrNcRWHk7ZD77Z96N9mm/t9WhjLeeLg+O8ZTwdOQ3U0fvbXjxEKoPrPMEQljS an75RGjAAmsvhO8Ao9x2D57anYmNAGsILOl/DWMFvmnDGEFLYRGq8FIILgS0gdYhE0 Ao9wGN2xwJjxg== Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DD3D0603CE for ; Tue, 22 Nov 2022 12:30:53 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="SYZzwtzV"; dkim-atps=neutral Received: by mail-wr1-x434.google.com with SMTP id z4so9659979wrr.3 for ; Tue, 22 Nov 2022 03:30:53 -0800 (PST) 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=eZWGHVFGF7J1lGK7ZZU9Tff95PuQzoZ9l1symSVcbtA=; b=SYZzwtzVuxpXs9Os45iE9YIytU8LCNBNL4i/GEgTXkaLQ3cPm/X6s6+rjdGD6ry+l0 0/+6fOr930hu3rXRmt9ClCE25oUHB/RhHTOGZeA5dlbJ6XG3TN8Wjy5M/9bW+QaWK9oa K2RD3JKHrYwl5UKseW/xunRQDQcvId8Zi83vz5jKZ2nWhGvNbWFfv3sFv0GZ0Rst3Zs4 NNfzVBgUvowlyiEmLOUU3y9hk9DJ3+lEXTdL3h2LdWEWQYgNYsFq0Ce97K3I4dyXeDxQ N5ssvpxx/rYBFGrXyThPuxVCRrLicjcVtcUtJaDmrPlvu8kQTL4p4ZKPqNbtRNw62M58 pzEg== 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=eZWGHVFGF7J1lGK7ZZU9Tff95PuQzoZ9l1symSVcbtA=; b=jVptGpQLEcvJ7ohbqn5Dl82rGwHnGuiSSTavqTqlo/EKxoVX7ta5PKblrplsUWYrDV evFJpt8UwqPCH0RLs6Yh9JhplIQI7ASpCEguadFYUTEo+a4xN/JJPVlP9BBO7ln3exdk MLb228gDjvQBYmIPwysUmgjMkUCcAC4gdwYS+YBzo6ltCts3NhVvHVJCGeZ34QVyc7Pp u3weGe+t2Ms+s85Ya5xk40A7+4FIbuvvgJVAErSjGjdZutyj1v/WjlhxVQYfvnQqp/DY TOifwpvFrysi0uKRRaXmDWIJjfTaMQVQMPcmmxn2v+87DueTlmz9YDZWzyr10/SZl9RI 9Wkw== X-Gm-Message-State: ANoB5pnbiXOwCFGb7tH7FXocsdgpXZgDJCmeJ5liadinXHVtRqXbz+P/ 2i2QXLUPs+jRD1dQPxm1ClwC+Zz5Ydccug== X-Google-Smtp-Source: AA0mqf6d5jILmg+Y/lwrA+KcPUtOq+dHsFyMzApcBjdhNX8gQU/6OpnhXW8nzx8XhgVLPAdXG61lVA== X-Received: by 2002:a5d:4c48:0:b0:241:bd2c:bef with SMTP id n8-20020a5d4c48000000b00241bd2c0befmr8002458wrt.304.1669116653116; Tue, 22 Nov 2022 03:30:53 -0800 (PST) Received: from pi4-davidp.pitowers.org ([2a00:1098:3142:14:e4a2:3070:eea4:e434]) by smtp.gmail.com with ESMTPSA id i6-20020a05600c354600b003cf894c05e4sm22713522wmq.22.2022.11.22.03.30.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Nov 2022 03:30:52 -0800 (PST) To: libcamera-devel@lists.libcamera.org Date: Tue, 22 Nov 2022 11:30:49 +0000 Message-Id: <20221122113049.3951-3-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221122113049.3951-1-david.plowman@raspberrypi.com> References: <20221122113049.3951-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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 Reviewed-by: Jacopo Mondi --- src/libcamera/camera_sensor.cpp | 49 ++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp index 572a313a..cbac9e78 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,45 @@ 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; + const struct v4l2_query_ext_ctrl *hflipInfo = subdev_->controlInfo(V4L2_CID_HFLIP); + const struct v4l2_query_ext_ctrl *vflipInfo = subdev_->controlInfo(V4L2_CID_VFLIP); + if (hflipInfo) + flipCtrlIds.push_back(V4L2_CID_HFLIP); + if (vflipInfo) + 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 (hflipInfo && flipCtrls.get(V4L2_CID_HFLIP).get() && + (hflipInfo->flags & V4L2_CTRL_FLAG_MODIFY_LAYOUT)) + transform |= Transform::HFlip; + if (vflipInfo && flipCtrls.get(V4L2_CID_VFLIP).get() && + (vflipInfo->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); + + if (bayerFormat.isValid()) + mbusCode = bayerFormat.transform(transform).toMbusCode(); + + if (mbusCode) + 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; @@ -189,7 +218,7 @@ int CameraSensor::init() * \todo The control API ought to have a flag to specify if a control * is read-only which could be used below. */ - const ControlInfo hblank = ctrls.infoMap()->at(V4L2_CID_HBLANK); + const ControlInfo hblank = subdev_->controls().at(V4L2_CID_HBLANK); const int32_t hblankMin = hblank.min().get(); const int32_t hblankMax = hblank.max().get();