{"id":18875,"url":"https://patchwork.libcamera.org/api/1.1/patches/18875/?format=json","web_url":"https://patchwork.libcamera.org/patch/18875/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20230724095925.20877-3-jacopo.mondi@ideasonboard.com>","date":"2023-07-24T09:59:24","name":"[libcamera-devel,v3,2/3] libcamera: rpi: pipeline_base: Move findBestFormat to CameraData","commit_ref":"4edc162e3d36119f4a63fe8b43c73bdecffacfa1","pull_url":null,"state":"superseded","archived":false,"hash":"51c4f2c5f86d9f7f739349e1d22d51cca24e492b","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/1.1/people/143/?format=json","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18875/mbox/","series":[{"id":3982,"url":"https://patchwork.libcamera.org/api/1.1/series/3982/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3982","date":"2023-07-24T09:59:22","name":"[libcamera-devel,v3,1/3] libcamera: rpi: pipeline_base: Remove populateSensorFormats()","version":3,"mbox":"https://patchwork.libcamera.org/series/3982/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18875/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18875/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 4C018BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 24 Jul 2023 09:59:44 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0BEC3628C1;\n\tMon, 24 Jul 2023 11:59:43 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id ADFED628C2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 24 Jul 2023 11:59:39 +0200 (CEST)","from uno.localdomain (mob-5-91-20-233.net.vodafone.it\n\t[5.91.20.233])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id F23DF735;\n\tMon, 24 Jul 2023 11:58:41 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1690192783;\n\tbh=BJgz2A0FbhIgFokyI5u9AQHq3W92s4WU+W3DdmM1Dg0=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=dCu2YrrYiC6rRToA37q16wH3hxsm09allQYHz7jh89Nh6uVcy5C5iH/s01W//dMK+\n\tgBo82BDuwkzRD9qzWcdYSwB9tZEbR2Rx8GdgyGYGeRx2mOcIJXqLipZdbaDwZ0wK1o\n\tS1Vc5b8pf/eIeAWpoNMszYCeZdALVzpIElb5xVBMJgBZgJxRuLrlLhOC3nz4bWPR9U\n\tYtOLDNqFaAfuSjy/+Bm/bRWUn9ALGC/W4RIRdT4kZk1iIWIip5MAHg5sX05ddp3q2Q\n\t9+c/qLL5MtbmkyJp+UA0taKUnaZM4tJ1hNP/09t4rt58xw+61+yrK+jzI6mh4iiycI\n\tUtrqmyWSf1p/Q==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1690192722;\n\tbh=BJgz2A0FbhIgFokyI5u9AQHq3W92s4WU+W3DdmM1Dg0=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=hpWJninX2nbtxF1WzOv0Hc+mC/Rbr1pkdu18vyL8IdknulXz4p4tDDJLKexWeATHZ\n\toYBKYZj0rMMlpR50aErv+BvWQi/WPehduAIZfZcyAd1Zf7J6a2tebpu86E07wz1ik7\n\tjyLIIVQv9IJQtUWbBUldo/z5Fvjaaf7egMPerqVc="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"hpWJninX\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 24 Jul 2023 11:59:24 +0200","Message-Id":"<20230724095925.20877-3-jacopo.mondi@ideasonboard.com>","X-Mailer":"git-send-email 2.40.1","In-Reply-To":"<20230724095925.20877-1-jacopo.mondi@ideasonboard.com>","References":"<20230724095925.20877-1-jacopo.mondi@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 2/3] libcamera: rpi: pipeline_base:\n\tMove findBestFormat to CameraData","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The findBestFormat() helper operates on the list of sensor formats,\nwhich is owned by the CameraData class. Move the function to that class\nas well to:\n\n1) Avoid passing the list of formats to the function\n2) Remove a static helper in favour of a class function\n3) Allow subclasses with access to CameraData to call the function\n\nMove to the CameraData class the scoreFormat helper as well.\n\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\nReviewed-by: Naushir Patuck <naush@raspberrypi.com>\n---\n .../pipeline/rpi/common/pipeline_base.cpp     | 127 +++++++++---------\n .../pipeline/rpi/common/pipeline_base.h       |   3 +\n 2 files changed, 67 insertions(+), 63 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\nindex fb3756a47590..d794e9fc429f 100644\n--- a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\n+++ b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\n@@ -74,63 +74,6 @@ bool isMonoSensor(std::unique_ptr<CameraSensor> &sensor)\n \treturn bayer.order == BayerFormat::Order::MONO;\n }\n \n-double scoreFormat(double desired, double actual)\n-{\n-\tdouble score = desired - actual;\n-\t/* Smaller desired dimensions are preferred. */\n-\tif (score < 0.0)\n-\t\tscore = (-score) / 8;\n-\t/* Penalise non-exact matches. */\n-\tif (actual != desired)\n-\t\tscore *= 2;\n-\n-\treturn score;\n-}\n-\n-V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size &req, unsigned int bitDepth)\n-{\n-\tdouble bestScore = std::numeric_limits<double>::max(), score;\n-\tV4L2SubdeviceFormat bestFormat;\n-\tbestFormat.colorSpace = ColorSpace::Raw;\n-\n-\tconstexpr float penaltyAr = 1500.0;\n-\tconstexpr float penaltyBitDepth = 500.0;\n-\n-\t/* Calculate the closest/best mode from the user requested size. */\n-\tfor (const auto &iter : formatsMap) {\n-\t\tconst unsigned int mbusCode = iter.first;\n-\t\tconst PixelFormat format = mbusCodeToPixelFormat(mbusCode,\n-\t\t\t\t\t\t\t\t BayerFormat::Packing::None);\n-\t\tconst PixelFormatInfo &info = PixelFormatInfo::info(format);\n-\n-\t\tfor (const Size &size : iter.second) {\n-\t\t\tdouble reqAr = static_cast<double>(req.width) / req.height;\n-\t\t\tdouble fmtAr = static_cast<double>(size.width) / size.height;\n-\n-\t\t\t/* Score the dimensions for closeness. */\n-\t\t\tscore = scoreFormat(req.width, size.width);\n-\t\t\tscore += scoreFormat(req.height, size.height);\n-\t\t\tscore += penaltyAr * scoreFormat(reqAr, fmtAr);\n-\n-\t\t\t/* Add any penalties... this is not an exact science! */\n-\t\t\tscore += utils::abs_diff(info.bitsPerPixel, bitDepth) * penaltyBitDepth;\n-\n-\t\t\tif (score <= bestScore) {\n-\t\t\t\tbestScore = score;\n-\t\t\t\tbestFormat.mbus_code = mbusCode;\n-\t\t\t\tbestFormat.size = size;\n-\t\t\t}\n-\n-\t\t\tLOG(RPI, Debug) << \"Format: \" << size\n-\t\t\t\t\t<< \" fmt \" << format\n-\t\t\t\t\t<< \" Score: \" << score\n-\t\t\t\t\t<< \" (best \" << bestScore << \")\";\n-\t\t}\n-\t}\n-\n-\treturn bestFormat;\n-}\n-\n const std::vector<ColorSpace> validColorSpaces = {\n \tColorSpace::Sycc,\n \tColorSpace::Smpte170m,\n@@ -276,7 +219,7 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n \n \t\tconst PixelFormatInfo &info = PixelFormatInfo::info(cfg.pixelFormat);\n \t\tunsigned int bitDepth = info.isValid() ? info.bitsPerPixel : defaultRawBitDepth;\n-\t\tV4L2SubdeviceFormat sensorFormat = findBestFormat(data_->sensorFormats_, cfg.size, bitDepth);\n+\t\tV4L2SubdeviceFormat sensorFormat = data_->findBestFormat(cfg.size, bitDepth);\n \n \t\tBayerFormat::Packing packing = BayerFormat::fromPixelFormat(cfg.pixelFormat).packing;\n \t\trawFormat = PipelineHandlerBase::toV4L2DeviceFormat(raw.dev, sensorFormat, packing);\n@@ -391,7 +334,7 @@ PipelineHandlerBase::generateConfiguration(Camera *camera, Span<const StreamRole\n \t\tswitch (role) {\n \t\tcase StreamRole::Raw:\n \t\t\tsize = sensorSize;\n-\t\t\tsensorFormat = findBestFormat(data->sensorFormats_, size, defaultRawBitDepth);\n+\t\t\tsensorFormat = data->findBestFormat(size, defaultRawBitDepth);\n \t\t\tpixelFormat = mbusCodeToPixelFormat(sensorFormat.mbus_code,\n \t\t\t\t\t\t\t    BayerFormat::Packing::CSI2);\n \t\t\tASSERT(pixelFormat.isValid());\n@@ -531,10 +474,11 @@ int PipelineHandlerBase::configure(Camera *camera, CameraConfiguration *config)\n \t\tbitDepth = bayerFormat.bitDepth;\n \t}\n \n-\tV4L2SubdeviceFormat sensorFormat = findBestFormat(data->sensorFormats_,\n-\t\t\t\t\t\t\t  rawStreams.empty() ? ispStreams[0].cfg->size\n-\t\t\t\t\t\t\t\t\t     : rawStreams[0].cfg->size,\n-\t\t\t\t\t\t\t  bitDepth);\n+\tV4L2SubdeviceFormat sensorFormat =\n+\t\tdata->findBestFormat(rawStreams.empty() ? ispStreams[0].cfg->size\n+\t\t\t\t\t\t\t: rawStreams[0].cfg->size,\n+\t\t\t\t     bitDepth);\n+\n \t/* Apply any cached transform. */\n \tconst RPiCameraConfiguration *rpiConfig = static_cast<const RPiCameraConfiguration *>(config);\n \n@@ -954,6 +898,63 @@ int PipelineHandlerBase::queueAllBuffers(Camera *camera)\n \treturn 0;\n }\n \n+double CameraData::scoreFormat(double desired, double actual) const\n+{\n+\tdouble score = desired - actual;\n+\t/* Smaller desired dimensions are preferred. */\n+\tif (score < 0.0)\n+\t\tscore = (-score) / 8;\n+\t/* Penalise non-exact matches. */\n+\tif (actual != desired)\n+\t\tscore *= 2;\n+\n+\treturn score;\n+}\n+\n+V4L2SubdeviceFormat CameraData::findBestFormat(const Size &req, unsigned int bitDepth) const\n+{\n+\tdouble bestScore = std::numeric_limits<double>::max(), score;\n+\tV4L2SubdeviceFormat bestFormat;\n+\tbestFormat.colorSpace = ColorSpace::Raw;\n+\n+\tconstexpr float penaltyAr = 1500.0;\n+\tconstexpr float penaltyBitDepth = 500.0;\n+\n+\t/* Calculate the closest/best mode from the user requested size. */\n+\tfor (const auto &iter : sensorFormats_) {\n+\t\tconst unsigned int mbusCode = iter.first;\n+\t\tconst PixelFormat format = mbusCodeToPixelFormat(mbusCode,\n+\t\t\t\t\t\t\t\t BayerFormat::Packing::None);\n+\t\tconst PixelFormatInfo &info = PixelFormatInfo::info(format);\n+\n+\t\tfor (const Size &size : iter.second) {\n+\t\t\tdouble reqAr = static_cast<double>(req.width) / req.height;\n+\t\t\tdouble fmtAr = static_cast<double>(size.width) / size.height;\n+\n+\t\t\t/* Score the dimensions for closeness. */\n+\t\t\tscore = scoreFormat(req.width, size.width);\n+\t\t\tscore += scoreFormat(req.height, size.height);\n+\t\t\tscore += penaltyAr * scoreFormat(reqAr, fmtAr);\n+\n+\t\t\t/* Add any penalties... this is not an exact science! */\n+\t\t\tscore += utils::abs_diff(info.bitsPerPixel, bitDepth) * penaltyBitDepth;\n+\n+\t\t\tif (score <= bestScore) {\n+\t\t\t\tbestScore = score;\n+\t\t\t\tbestFormat.mbus_code = mbusCode;\n+\t\t\t\tbestFormat.size = size;\n+\t\t\t}\n+\n+\t\t\tLOG(RPI, Debug) << \"Format: \" << size\n+\t\t\t\t\t<< \" fmt \" << format\n+\t\t\t\t\t<< \" Score: \" << score\n+\t\t\t\t\t<< \" (best \" << bestScore << \")\";\n+\t\t}\n+\t}\n+\n+\treturn bestFormat;\n+}\n+\n void CameraData::freeBuffers()\n {\n \tif (ipa_) {\ndiff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.h b/src/libcamera/pipeline/rpi/common/pipeline_base.h\nindex f648e81054bb..2eda3cd89812 100644\n--- a/src/libcamera/pipeline/rpi/common/pipeline_base.h\n+++ b/src/libcamera/pipeline/rpi/common/pipeline_base.h\n@@ -81,6 +81,9 @@ public:\n \tvirtual void platformStart() = 0;\n \tvirtual void platformStop() = 0;\n \n+\tdouble scoreFormat(double desired, double actual) const;\n+\tV4L2SubdeviceFormat findBestFormat(const Size &req, unsigned int bitDepth) const;\n+\n \tvoid freeBuffers();\n \tvirtual void platformFreeBuffers() = 0;\n \n","prefixes":["libcamera-devel","v3","2/3"]}