From patchwork Mon Mar 13 09:19:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 18384 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 EE6F8C3263 for ; Mon, 13 Mar 2023 09:20:06 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5A17462710; Mon, 13 Mar 2023 10:20:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1678699204; bh=dEH2tY6MGiTWeiifQ9gHQ0FCPbwnK1BxHd+7Oyw+XvY=; 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=OFJPy9+wWbybNiUn77B171WMAFjqAmm1o8FPbeXZSwy1xNiL5GmZ7atV8Q7eqj+ZX 6kPYqu3A5nvpM6VM5193jFPnQJZSwR/PkVMhIuqr2pTNP5cn9F/CiE8wn87Lw+ghab 6XHCeoSLvI7eVVfbgQoaO+0C/1Yz/GDVzCDM16aZIF2xH52fYVQ2EQvlXeqn07J6z9 0JIzdTsd/h0XDUjvGhNuKT/o8NrhBuiiX+EDyWCDtoaKxvdeIKTIG4E+OQ+ZD/knpu fdnXDOW3M1hHM+drC1XGKAGrGyGpVNgi2POg/MiGX6cYIud6N1QpZPTLR97L6CyxzX Ni1/waO1uxwFQ== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 48C59626B2 for ; Mon, 13 Mar 2023 10:20:02 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="qDgqmQgt"; dkim-atps=neutral Received: from uno.homenet.telecomitalia.it (host-79-33-55-183.retail.telecomitalia.it [79.33.55.183]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 62F3E8CC; Mon, 13 Mar 2023 10:20:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1678699202; bh=dEH2tY6MGiTWeiifQ9gHQ0FCPbwnK1BxHd+7Oyw+XvY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qDgqmQgtJ02Sy+MUKIwqx1hNG8o17QdKPzhAfUqIXXhE+b8Ocab5jpY8ZSzqv98Jx ANcprLchiXuN71/MuNIdGA3osu85en431hykPPxcnuVZsxP3NXx6S58d3j8gl31WNN Qy7MDxwbN+KGSOjpyCIm0LwufV036bGxkT0seT8Y= To: libcamera-devel@lists.libcamera.org Date: Mon, 13 Mar 2023 10:19:39 +0100 Message-Id: <20230313091944.9530-2-jacopo.mondi@ideasonboard.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230313091944.9530-1-jacopo.mondi@ideasonboard.com> References: <20230313091944.9530-1-jacopo.mondi@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/6] libcamera: imx8-isi: Break out RAW format selection 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" The current implementation of the ISI pipeline handler handles translation of PixelFormat to media bus formats from the sensor through a centralized map. As the criteria to select the correct media bus code depends on if the output PixelFormat is a RAW Bayer format or not, start by splitting the RAW media bus code procedure selection out by adding a function for such purpose to the ISICameraData class. Add the function to the ISICameraData and not to the ISICameraConfiguration because: - The sensor is a property of CameraData - The same function will be re-used by the ISIPipelineHandler during CameraConfiguration generation. Signed-off-by: Jacopo Mondi Reviewed-by: Paul Elder Reviewed-by: Daniel Scally Tested-by: Daniel Scally Reviewed-by: Laurent Pinchart --- src/libcamera/pipeline/imx8-isi/imx8-isi.cpp | 130 +++++++++++++------ 1 file changed, 90 insertions(+), 40 deletions(-) diff --git a/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp b/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp index 0c67e35dde73..ffb96e6284f0 100644 --- a/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp +++ b/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp @@ -59,6 +59,8 @@ public: return stream - &*streams_.begin(); } + unsigned int getRawMediaBusFormat(PixelFormat *pixelFormat) const; + std::unique_ptr sensor_; std::unique_ptr csis_; @@ -174,6 +176,85 @@ int ISICameraData::init() return 0; } +/* + * Get a RAW Bayer media bus format compatible with the requested pixelFormat. + * + * If the requested pixelFormat cannot be produced by the sensor adjust it to + * the one corresponding to the media bus format with the largest bit-depth. + */ +unsigned int ISICameraData::getRawMediaBusFormat(PixelFormat *pixelFormat) const +{ + std::vector mbusCodes = sensor_->mbusCodes(); + + static const std::map rawFormats = { + { formats::SBGGR8, MEDIA_BUS_FMT_SBGGR8_1X8 }, + { formats::SGBRG8, MEDIA_BUS_FMT_SGBRG8_1X8 }, + { formats::SGRBG8, MEDIA_BUS_FMT_SGRBG8_1X8 }, + { formats::SRGGB8, MEDIA_BUS_FMT_SRGGB8_1X8 }, + { formats::SBGGR10, MEDIA_BUS_FMT_SBGGR10_1X10 }, + { formats::SGBRG10, MEDIA_BUS_FMT_SGBRG10_1X10 }, + { formats::SGRBG10, MEDIA_BUS_FMT_SGRBG10_1X10 }, + { formats::SRGGB10, MEDIA_BUS_FMT_SRGGB10_1X10 }, + { formats::SBGGR12, MEDIA_BUS_FMT_SBGGR12_1X12 }, + { formats::SGBRG12, MEDIA_BUS_FMT_SGBRG12_1X12 }, + { formats::SGRBG12, MEDIA_BUS_FMT_SGRBG12_1X12 }, + { formats::SRGGB12, MEDIA_BUS_FMT_SRGGB12_1X12 }, + }; + + /* + * Make sure the requested PixelFormat is supported in the above + * map and the sensor can produce the compatible mbus code. + */ + auto it = rawFormats.find(*pixelFormat); + if (it != rawFormats.end() && + std::count(mbusCodes.begin(), mbusCodes.end(), it->second)) + return it->second; + + if (it == rawFormats.end()) + LOG(ISI, Warning) << pixelFormat + << " not supported in ISI formats map."; + + /* + * The desired pixel format cannot be produced. Adjust it to the one + * corresponding to the raw media bus format with the largest bit-depth + * the sensor provides. + */ + unsigned int sensorCode = 0; + unsigned int maxDepth = 0; + *pixelFormat = {}; + + for (unsigned int code : mbusCodes) { + /* Make sure the media bus format is RAW Bayer. */ + const BayerFormat &bayerFormat = BayerFormat::fromMbusCode(code); + if (!bayerFormat.isValid()) + continue; + + /* Make sure the media format is supported. */ + it = std::find_if(rawFormats.begin(), rawFormats.end(), + [code](auto &rawFormat) { + return rawFormat.second == code; + }); + + if (it == rawFormats.end()) { + LOG(ISI, Warning) << bayerFormat + << " not supported in ISI formats map."; + continue; + } + + /* Pick the one with the largest bit depth. */ + if (bayerFormat.bitDepth > maxDepth) { + maxDepth = bayerFormat.bitDepth; + *pixelFormat = it->first; + sensorCode = code; + } + } + + if (!pixelFormat->isValid()) + LOG(ISI, Error) << "Cannot find a supported RAW format"; + + return sensorCode; +} + /* ----------------------------------------------------------------------------- * Camera Configuration */ @@ -311,50 +392,19 @@ ISICameraConfiguration::validateRaw(std::set &availableStreams, */ std::vector mbusCodes = data_->sensor_->mbusCodes(); StreamConfiguration &rawConfig = config_[0]; + PixelFormat rawFormat = rawConfig.pixelFormat; - bool supported = false; - auto it = formatsMap_.find(rawConfig.pixelFormat); - if (it != formatsMap_.end()) { - unsigned int mbusCode = it->second.sensorCode; - - if (std::count(mbusCodes.begin(), mbusCodes.end(), mbusCode)) - supported = true; + unsigned int sensorCode = data_->getRawMediaBusFormat(&rawFormat); + if (!sensorCode) { + LOG(ISI, Error) << "Cannot adjust RAW pixelformat " + << rawConfig.pixelFormat; + return Invalid; } - if (!supported) { - /* - * Adjust to the first mbus code supported by both the - * sensor and the pipeline. - */ - const FormatMap::value_type *pipeConfig = nullptr; - for (unsigned int code : mbusCodes) { - const BayerFormat &bayerFormat = BayerFormat::fromMbusCode(code); - if (!bayerFormat.isValid()) - continue; - - auto fmt = std::find_if(ISICameraConfiguration::formatsMap_.begin(), - ISICameraConfiguration::formatsMap_.end(), - [code](const auto &isiFormat) { - const auto &pipe = isiFormat.second; - return pipe.sensorCode == code; - }); - - if (fmt == ISICameraConfiguration::formatsMap_.end()) - continue; - - pipeConfig = &(*fmt); - break; - } - - if (!pipeConfig) { - LOG(ISI, Error) << "Cannot adjust RAW format " - << rawConfig.pixelFormat; - return Invalid; - } - - rawConfig.pixelFormat = pipeConfig->first; + if (rawFormat != rawConfig.pixelFormat) { LOG(ISI, Debug) << "RAW pixelformat adjusted to " - << pipeConfig->first; + << rawFormat; + rawConfig.pixelFormat = rawFormat; status = Adjusted; }