From patchwork Mon Jul 31 11:31:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 18912 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 CEB98C32A9 for ; Mon, 31 Jul 2023 11:31:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0AB35627F4; Mon, 31 Jul 2023 13:31:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1690803100; bh=Lcsp5bxXyF6F9amD/MIYkbupCUgOZQJ9MVE6UGRCTYs=; 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=C/ATEv87n9GPCgjKcjUnefbh0XcbpUpSXQ6F2qiIBc+Fc8Nlvz3SHoA6BTdSnr3GF b81R774yw2OSDWsuR42ppG9cFWc5fm6HEybS14Mnd/h12bfkW0Bp1vYI2JQm0SHQno 4QaI1w9I5De+XqHQjKXUa9gXZLk3txZatgJqCX6Df3iLmnlNTNtm38ygztVmK6wtNx +ggAdj69TLn7zVcrOYhvGplQwYIlWviCdiKrVn8wihThbjsJP6nbr3TbWhHRQcvNED 6Zsa5qdJqzEQ1572zMSzmN0x2/4gIe1od+jIgPCZ1TnoIhXLvP8c5y02hMcDl9+4xg m10lBIH9CyrqA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0F5AB627EF for ; Mon, 31 Jul 2023 13:31:36 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="jRGw6xZ1"; dkim-atps=neutral Received: from uno.localdomain (mob-5-90-53-43.net.vodafone.it [5.90.53.43]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 9B3D012B1; Mon, 31 Jul 2023 13:30:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1690803033; bh=Lcsp5bxXyF6F9amD/MIYkbupCUgOZQJ9MVE6UGRCTYs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jRGw6xZ1K9WEq4DJiu5E5wrS7WoQ3/oEpHUt2Txl1HLrcAo++ndYRiaYayzYeLjDg /D1ywMjMixeA1RTsG8sjxRysSJ9IeIAbiNSowpmkI9v8jfh4F7twbXIen9cPXOyn9o 83OyFjWSrTxATZHlpPB7QCjQrm0HAd695eh68f5w= To: libcamera-devel@lists.libcamera.org Date: Mon, 31 Jul 2023 13:31:14 +0200 Message-Id: <20230731113115.5915-4-jacopo.mondi@ideasonboard.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230731113115.5915-1-jacopo.mondi@ideasonboard.com> References: <20230731113115.5915-1-jacopo.mondi@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/4] libcamera: camera_sensor: Add function to apply a config 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: Jacopo Mondi via libcamera-devel From: Jacopo Mondi Reply-To: Jacopo Mondi Cc: Jacopo Mondi Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add to the CameraSensor class a function to apply to the sensor a full configuration. The configuration shall be fully populated and shall apply without modifications to the sensor. Signed-off-by: Jacopo Mondi Reviewed-by: Naushir Patuck Reviewed-by: Kieran Bingham --- include/libcamera/internal/camera_sensor.h | 5 ++ src/libcamera/camera_sensor.cpp | 86 ++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h index 02c77ab037da..06791c3c6425 100644 --- a/include/libcamera/internal/camera_sensor.h +++ b/include/libcamera/internal/camera_sensor.h @@ -29,6 +29,7 @@ namespace libcamera { class BayerFormat; class CameraLens; class MediaEntity; +class SensorConfiguration; struct CameraSensorProperties; @@ -58,6 +59,10 @@ public: Transform transform = Transform::Identity); int tryFormat(V4L2SubdeviceFormat *format) const; + int applyConfiguration(const SensorConfiguration &config, + Transform transform = Transform::Identity, + V4L2SubdeviceFormat *sensorFormat = nullptr); + const ControlInfoMap &controls() const; ControlList getControls(const std::vector &ids); int setControls(ControlList *ctrls); diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp index f3a5aa37149f..96b618572a3d 100644 --- a/src/libcamera/camera_sensor.cpp +++ b/src/libcamera/camera_sensor.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -822,6 +823,91 @@ int CameraSensor::tryFormat(V4L2SubdeviceFormat *format) const V4L2Subdevice::Whence::TryFormat); } +/** + * \brief Apply a sensor configuration to the camera sensor + * \param[in] config The sensor configuration + * \param[in] transform The transform to be applied on the sensor. + * Defaults to Identity + * \param[out] sensorFormat Optional output parameter where the format + * actually applied to the sensor is returned to the caller + * + * Apply to the camera sensor the configuration \a config. + * + * \todo The configuration shall be fully populated and if any of the fields + * there specified cannot be applied as it is, an error code is returned. + * + * \return 0 if \a config is applied to the camera sensor, a negative error code + * otherwise + */ +int CameraSensor::applyConfiguration(const SensorConfiguration &config, + Transform transform, + V4L2SubdeviceFormat *sensorFormat) +{ + if (!config) { + LOG(CameraSensor, Error) << "Invalid sensor configuration"; + return -EINVAL; + } + + std::vector filteredCodes; + std::copy_if(mbusCodes_.begin(), mbusCodes_.end(), + std::back_inserter(filteredCodes), + [&config](unsigned int mbusCode) { + BayerFormat bayer = BayerFormat::fromMbusCode(mbusCode); + if (bayer.bitDepth == config.bitDepth) + return true; + return false; + }); + if (filteredCodes.empty()) { + LOG(CameraSensor, Error) + << "Cannot find any format with bit depth " + << config.bitDepth; + return -EINVAL; + } + + /* + * Compute the sensor's data frame size by applying the cropping + * rectangle, subsampling and output crop to the sensor's pixel array + * size. + * + * \todo The actual size computation is for now ignored and only the + * output size is considered. This implies that resolutions obtained + * with two different cropping/subsampling will look identical and + * only the first found one will be considered. + */ + V4L2SubdeviceFormat subdevFormat = {}; + for (unsigned int code : filteredCodes) { + for (const Size &size : sizes(code)) { + if (size.width != config.outputSize.width || + size.height != config.outputSize.height) + continue; + + subdevFormat.mbus_code = code; + subdevFormat.size = size; + break; + } + } + if (!subdevFormat.mbus_code) { + LOG(CameraSensor, Error) << "Invalid output size in sensor configuration"; + return -EINVAL; + } + + int ret = setFormat(&subdevFormat, transform); + if (ret) + return ret; + + /* + * Return to the caller the format actually applied to the sensor. + * This is relevant if transform has changed the bayer pattern order. + */ + if (sensorFormat) + *sensorFormat = subdevFormat; + + /* \todo Handle AnalogCrop. Most sensors do not support set_selection */ + /* \todo Handle scaling in the digital domain. */ + + return 0; +} + /** * \brief Retrieve the supported V4L2 controls and their information *