From patchwork Wed Jul 12 10:55:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 18813 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 E21AEC3243 for ; Wed, 12 Jul 2023 10:55:22 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EFC9C628C0; Wed, 12 Jul 2023 12:55:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1689159320; bh=1sWGanjV4RzWljAseBrrIQv/Co7vS/CBO3bYxB5L+B0=; 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=GPdIVcZFtCh2AH7zNn+fZAGx3hCDYmcrPqYgwd3eJPU3N4kzSrtvDJj66wN/1JXZ4 0zvi1Z3CQ27IZEiQl8LYpkH5wwrdNIwi5geSqV9eec0bfhzZsuM5m2PyZnd6Og0ygd y3XNtzhWCE7oP85rlteXS3dlFdOOrMv166LxX/nfTnFAqrcWzfgzzj+04O/uIV/LNR S46//1/W/YOG9jpZW0YNJSX706KXiG7ucwO9aw7TMw/sNSck22cHTCNI4hWyWkNCJd YzxrU3Fn+UHcvWiUWTh3GeE6i+YzFBvu50lt2ILDaEe+mPXOmSRTY0W89O4eiHXQwM sRM3GT4GLDHqw== Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A4FBC628BF for ; Wed, 12 Jul 2023 12:55:17 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="DfFHV2DB"; dkim-atps=neutral Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3fbf1b82dc7so69572455e9.2 for ; Wed, 12 Jul 2023 03:55:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1689159317; x=1691751317; 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=2uSSj325O/nrrSzfl57V3hKln92+IihZFGi4yGkEac8=; b=DfFHV2DBnJ8tI1kWaocJuS+FFKm31Q4tSpKVI6f66P8qzCNa6rmVlD0ZVnSumTOWMR DV0chxKq15ZPX5xJZ74hQsznFRJNaap4J95trotJjjNWhuRD+jGxdrTcbDzull7n2HF4 LCgRpHnvT/kA/6/khTAJ5fRrMDZ2vW9EOSefU55QXNcVJ7gtsJRp7HlEURPNdEPZkEYm tdfgbGFmy/9F74Tgsok44zvFYH25UuQdAdOiqbT8s9MjIQp/PcqEG2ZvH3sWWzW7Dk2A 2ZaOdOPDIND80Wua9nBCk4YN6uvGvETGFhVIhXYPLDw1e8/K8I7eRTtY3nqwA5P8lkFB lSjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689159317; x=1691751317; 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=2uSSj325O/nrrSzfl57V3hKln92+IihZFGi4yGkEac8=; b=BYkdSfElbXUmAYq4EwDkLfE94YAjRyjzRVuNcskWD3aq5d1QbEmNngRyVTTDyrfQhT 2JKdy7bu/TjC5h901GByOpybLTNqpfkoDsbSApP2YaK3ATkpnsEq4nvbXKFitKd1LnuQ rFRViJWZkTig5KlUUFnxmHpoAeVOM6g/Kn+L4JMB+fbUmVCt5nnKLs+0zqrkxBN3VxNM Sy4Z2kkgTbdYTvLW82Oq/YZ/xTxFz1JNub+fMq/6he0ITpG3oWcVBxjrsxeHBuIGfF2n ueaDdVrXLEiFNFh4Vxhp6h7ye+kHjVll7iALL0pxvprDWHToQczJFhxhI0iD6LyuTNXt HdCQ== X-Gm-Message-State: ABy/qLby0XOdUrGuegbLR5onrQ90woqEIZDGBhLCiQ68+Je0c2iNGWa9 dcQ+mSJkahrtZM012nzq7BbXd6edEtllv79QMzB1eA== X-Google-Smtp-Source: APBJJlFIbWrnD+bB3UJ0/mMof1q/0bwTldQ3z9WVNuLOf4HecftypdONOEjpamFjiLuXY2A46Q3KrQ== X-Received: by 2002:a05:600c:2902:b0:3f9:b430:199b with SMTP id i2-20020a05600c290200b003f9b430199bmr17202733wmd.15.1689159316999; Wed, 12 Jul 2023 03:55:16 -0700 (PDT) Received: from localhost.localdomain ([93.93.133.154]) by smtp.gmail.com with ESMTPSA id h14-20020a05600c260e00b003fc05b89e5bsm4729236wma.34.2023.07.12.03.55.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 03:55:16 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 12 Jul 2023 11:55:09 +0100 Message-Id: <20230712105510.14658-3-naush@raspberrypi.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230712105510.14658-1-naush@raspberrypi.com> References: <20230712105510.14658-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v1 2/3] libcamera: rpi: pipeline_base: Move findBestFormat to CameraData 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: Naushir Patuck via libcamera-devel From: Naushir Patuck Reply-To: Naushir Patuck Cc: Jacopo Mondi Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Jacopo Mondi The findBestFormat() helper operates on the list of sensor formats, which is owned by the CameraData class. Move the function to that class as well to: 1) Avoid passing the list of formats to the function 2) Remove a static helper in favour of a class function 3) Allow subclasses with access to CameraData to call the function Move to the CameraData class the scoreFormat helper as well. Signed-off-by: Jacopo Mondi Reviewed-by: Naushir Patuck --- .../pipeline/rpi/common/pipeline_base.cpp | 140 ++++++++++-------- .../pipeline/rpi/common/pipeline_base.h | 3 + 2 files changed, 79 insertions(+), 64 deletions(-) diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp index fb3756a47590..725c1db0d527 100644 --- a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp +++ b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp @@ -74,63 +74,6 @@ bool isMonoSensor(std::unique_ptr &sensor) return bayer.order == BayerFormat::Order::MONO; } -double scoreFormat(double desired, double actual) -{ - double score = desired - actual; - /* Smaller desired dimensions are preferred. */ - if (score < 0.0) - score = (-score) / 8; - /* Penalise non-exact matches. */ - if (actual != desired) - score *= 2; - - return score; -} - -V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size &req, unsigned int bitDepth) -{ - double bestScore = std::numeric_limits::max(), score; - V4L2SubdeviceFormat bestFormat; - bestFormat.colorSpace = ColorSpace::Raw; - - constexpr float penaltyAr = 1500.0; - constexpr float penaltyBitDepth = 500.0; - - /* Calculate the closest/best mode from the user requested size. */ - for (const auto &iter : formatsMap) { - const unsigned int mbusCode = iter.first; - const PixelFormat format = mbusCodeToPixelFormat(mbusCode, - BayerFormat::Packing::None); - const PixelFormatInfo &info = PixelFormatInfo::info(format); - - for (const Size &size : iter.second) { - double reqAr = static_cast(req.width) / req.height; - double fmtAr = static_cast(size.width) / size.height; - - /* Score the dimensions for closeness. */ - score = scoreFormat(req.width, size.width); - score += scoreFormat(req.height, size.height); - score += penaltyAr * scoreFormat(reqAr, fmtAr); - - /* Add any penalties... this is not an exact science! */ - score += utils::abs_diff(info.bitsPerPixel, bitDepth) * penaltyBitDepth; - - if (score <= bestScore) { - bestScore = score; - bestFormat.mbus_code = mbusCode; - bestFormat.size = size; - } - - LOG(RPI, Debug) << "Format: " << size - << " fmt " << format - << " Score: " << score - << " (best " << bestScore << ")"; - } - } - - return bestFormat; -} - const std::vector validColorSpaces = { ColorSpace::Sycc, ColorSpace::Smpte170m, @@ -265,6 +208,17 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() [](auto &l, auto &r) { return l.cfg->size > r.cfg->size; }); /* Do any platform specific fixups. */ + unsigned int bitDepth = defaultRawBitDepth; + if (!rawStreams.empty()) { + BayerFormat bayerFormat = BayerFormat::fromPixelFormat(rawStreams[0].cfg->pixelFormat); + bitDepth = bayerFormat.bitDepth; + } + + V4L2SubdeviceFormat sensorFormat = + data_->findBestFormat(rawStreams.empty() ? outStreams[0].cfg->size + : rawStreams[0].cfg->size, + bitDepth); + status = data_->platformValidate(rawStreams, outStreams); if (status == Invalid) return Invalid; @@ -275,8 +229,8 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() V4L2DeviceFormat rawFormat; const PixelFormatInfo &info = PixelFormatInfo::info(cfg.pixelFormat); - unsigned int bitDepth = info.isValid() ? info.bitsPerPixel : defaultRawBitDepth; - V4L2SubdeviceFormat sensorFormat = findBestFormat(data_->sensorFormats_, cfg.size, bitDepth); + bitDepth = info.isValid() ? info.bitsPerPixel : defaultRawBitDepth; + sensorFormat = data_->findBestFormat(cfg.size, bitDepth); BayerFormat::Packing packing = BayerFormat::fromPixelFormat(cfg.pixelFormat).packing; rawFormat = PipelineHandlerBase::toV4L2DeviceFormat(raw.dev, sensorFormat, packing); @@ -391,7 +345,7 @@ PipelineHandlerBase::generateConfiguration(Camera *camera, SpansensorFormats_, size, defaultRawBitDepth); + sensorFormat = data->findBestFormat(size, defaultRawBitDepth); pixelFormat = mbusCodeToPixelFormat(sensorFormat.mbus_code, BayerFormat::Packing::CSI2); ASSERT(pixelFormat.isValid()); @@ -531,10 +485,11 @@ int PipelineHandlerBase::configure(Camera *camera, CameraConfiguration *config) bitDepth = bayerFormat.bitDepth; } - V4L2SubdeviceFormat sensorFormat = findBestFormat(data->sensorFormats_, - rawStreams.empty() ? ispStreams[0].cfg->size - : rawStreams[0].cfg->size, - bitDepth); + V4L2SubdeviceFormat sensorFormat = + data->findBestFormat(rawStreams.empty() ? ispStreams[0].cfg->size + : rawStreams[0].cfg->size, + bitDepth); + /* Apply any cached transform. */ const RPiCameraConfiguration *rpiConfig = static_cast(config); @@ -954,6 +909,63 @@ int PipelineHandlerBase::queueAllBuffers(Camera *camera) return 0; } +double CameraData::scoreFormat(double desired, double actual) const +{ + double score = desired - actual; + /* Smaller desired dimensions are preferred. */ + if (score < 0.0) + score = (-score) / 8; + /* Penalise non-exact matches. */ + if (actual != desired) + score *= 2; + + return score; +} + +V4L2SubdeviceFormat CameraData::findBestFormat(const Size &req, unsigned int bitDepth) const +{ + double bestScore = std::numeric_limits::max(), score; + V4L2SubdeviceFormat bestFormat; + bestFormat.colorSpace = ColorSpace::Raw; + + constexpr float penaltyAr = 1500.0; + constexpr float penaltyBitDepth = 500.0; + + /* Calculate the closest/best mode from the user requested size. */ + for (const auto &iter : sensorFormats_) { + const unsigned int mbusCode = iter.first; + const PixelFormat format = mbusCodeToPixelFormat(mbusCode, + BayerFormat::Packing::None); + const PixelFormatInfo &info = PixelFormatInfo::info(format); + + for (const Size &size : iter.second) { + double reqAr = static_cast(req.width) / req.height; + double fmtAr = static_cast(size.width) / size.height; + + /* Score the dimensions for closeness. */ + score = scoreFormat(req.width, size.width); + score += scoreFormat(req.height, size.height); + score += penaltyAr * scoreFormat(reqAr, fmtAr); + + /* Add any penalties... this is not an exact science! */ + score += utils::abs_diff(info.bitsPerPixel, bitDepth) * penaltyBitDepth; + + if (score <= bestScore) { + bestScore = score; + bestFormat.mbus_code = mbusCode; + bestFormat.size = size; + } + + LOG(RPI, Debug) << "Format: " << size + << " fmt " << format + << " Score: " << score + << " (best " << bestScore << ")"; + } + } + + return bestFormat; +} + void CameraData::freeBuffers() { if (ipa_) { diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.h b/src/libcamera/pipeline/rpi/common/pipeline_base.h index f648e81054bb..2eda3cd89812 100644 --- a/src/libcamera/pipeline/rpi/common/pipeline_base.h +++ b/src/libcamera/pipeline/rpi/common/pipeline_base.h @@ -81,6 +81,9 @@ public: virtual void platformStart() = 0; virtual void platformStop() = 0; + double scoreFormat(double desired, double actual) const; + V4L2SubdeviceFormat findBestFormat(const Size &req, unsigned int bitDepth) const; + void freeBuffers(); virtual void platformFreeBuffers() = 0;