From patchwork Fri Jul 14 14:26:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 18834 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 1532AC32AA for ; Fri, 14 Jul 2023 14:26:59 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 89C16628CB; Fri, 14 Jul 2023 16:26:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1689344818; bh=SJPvSZ3bvmhraCsC8eN2BA+aYYspDXZ2Jec5wa5heGs=; 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=dW1Hbs5VJnBIo4qBy/IrAXAgW4Wi9UGA0DxAicpq1rAYPco7pyWe0Vg5ox2xIYVCl Hd+2ebYQuWXW+Jdeg+Q+j2wxrGvicBcyvZ08om29QWXIBV5IrzIg4TjcTEcsehgTtv AXo7AubpsU3q9dOzc8p5FDwq8B/Etxvl/l5r0E52YTH4QtfkNEw6j7UMwDSMgURUia lXPHhfqjDdHGEZzS6a9XtSE5l+jz6kUUxhZwgIxXfuOdR5LV42VETYKzzVqyw/SkpA pqHh2xsN0NPbkbM+pw50Ji34ecmRGjtPNcjLdHTB8SWaQ8chJy3CuJz8twM9as+3Eb T3PsT5iOK/aSg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 80D4C60382 for ; Fri, 14 Jul 2023 16:26:55 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="WorixpT4"; dkim-atps=neutral Received: from uno.localdomain (mob-5-90-9-92.net.vodafone.it [5.90.9.92]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5B196D01; Fri, 14 Jul 2023 16:26:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1689344764; bh=SJPvSZ3bvmhraCsC8eN2BA+aYYspDXZ2Jec5wa5heGs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WorixpT4a/7o4ZuvHQ65hIqBVlb+jy+brk7W9aMQcWkvU/yRc0r+HFAC1jsJrfTj0 0oi0Ye1CXYRD6JvI0P2/CZyebN4LLf9qlZzCVo3Wbij56PRw0t+jbMB+qfKu5n07uf FN78Op79cr896XH8VOD7yjmpHxMJOR+5QSMxhNEk= To: libcamera-devel@lists.libcamera.org Date: Fri, 14 Jul 2023 16:26:33 +0200 Message-Id: <20230714142634.13134-3-jacopo.mondi@ideasonboard.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230714142634.13134-1-jacopo.mondi@ideasonboard.com> References: <20230714142634.13134-1-jacopo.mondi@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 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: 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 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 | 139 ++++++++++-------- .../pipeline/rpi/common/pipeline_base.h | 3 + 2 files changed, 78 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..a71715a6aff0 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,7 @@ 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; BayerFormat::Packing packing = BayerFormat::fromPixelFormat(cfg.pixelFormat).packing; rawFormat = PipelineHandlerBase::toV4L2DeviceFormat(raw.dev, sensorFormat, packing); @@ -391,7 +344,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 +484,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 +908,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;