Show a patch.

GET /api/1.1/patches/18813/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 18813,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/18813/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/18813/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20230712105510.14658-3-naush@raspberrypi.com>",
    "date": "2023-07-12T10:55:09",
    "name": "[libcamera-devel,v1,2/3] libcamera: rpi: pipeline_base: Move findBestFormat to CameraData",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "57a9ef31c76533a388a0a5b9fb83aab4e4e31930",
    "submitter": {
        "id": 34,
        "url": "https://patchwork.libcamera.org/api/1.1/people/34/?format=api",
        "name": "Naushir Patuck",
        "email": "naush@raspberrypi.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/18813/mbox/",
    "series": [
        {
            "id": 3968,
            "url": "https://patchwork.libcamera.org/api/1.1/series/3968/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3968",
            "date": "2023-07-12T10:55:07",
            "name": "Raspberry Pi: Configuration simplifications",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/3968/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/18813/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/18813/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 E21AEC3243\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 12 Jul 2023 10:55:22 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EFC9C628C0;\n\tWed, 12 Jul 2023 12:55:19 +0200 (CEST)",
            "from mail-wm1-x330.google.com (mail-wm1-x330.google.com\n\t[IPv6:2a00:1450:4864:20::330])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A4FBC628BF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 12 Jul 2023 12:55:17 +0200 (CEST)",
            "by mail-wm1-x330.google.com with SMTP id\n\t5b1f17b1804b1-3fbf1b82dc7so69572455e9.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 12 Jul 2023 03:55:17 -0700 (PDT)",
            "from localhost.localdomain ([93.93.133.154])\n\tby smtp.gmail.com with ESMTPSA id\n\th14-20020a05600c260e00b003fc05b89e5bsm4729236wma.34.2023.07.12.03.55.16\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 12 Jul 2023 03:55:16 -0700 (PDT)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1689159320;\n\tbh=1sWGanjV4RzWljAseBrrIQv/Co7vS/CBO3bYxB5L+B0=;\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=GPdIVcZFtCh2AH7zNn+fZAGx3hCDYmcrPqYgwd3eJPU3N4kzSrtvDJj66wN/1JXZ4\n\t0zvi1Z3CQ27IZEiQl8LYpkH5wwrdNIwi5geSqV9eec0bfhzZsuM5m2PyZnd6Og0ygd\n\ty3XNtzhWCE7oP85rlteXS3dlFdOOrMv166LxX/nfTnFAqrcWzfgzzj+04O/uIV/LNR\n\tS46//1/W/YOG9jpZW0YNJSX706KXiG7ucwO9aw7TMw/sNSck22cHTCNI4hWyWkNCJd\n\tYzxrU3Fn+UHcvWiUWTh3GeE6i+YzFBvu50lt2ILDaEe+mPXOmSRTY0W89O4eiHXQwM\n\tsRM3GT4GLDHqw==",
            "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1689159317; x=1691751317;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=2uSSj325O/nrrSzfl57V3hKln92+IihZFGi4yGkEac8=;\n\tb=DfFHV2DBnJ8tI1kWaocJuS+FFKm31Q4tSpKVI6f66P8qzCNa6rmVlD0ZVnSumTOWMR\n\tDV0chxKq15ZPX5xJZ74hQsznFRJNaap4J95trotJjjNWhuRD+jGxdrTcbDzull7n2HF4\n\tLCgRpHnvT/kA/6/khTAJ5fRrMDZ2vW9EOSefU55QXNcVJ7gtsJRp7HlEURPNdEPZkEYm\n\ttdfgbGFmy/9F74Tgsok44zvFYH25UuQdAdOiqbT8s9MjIQp/PcqEG2ZvH3sWWzW7Dk2A\n\t2ZaOdOPDIND80Wua9nBCk4YN6uvGvETGFhVIhXYPLDw1e8/K8I7eRTtY3nqwA5P8lkFB\n\tlSjg=="
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"DfFHV2DB\"; dkim-atps=neutral",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20221208; t=1689159317; x=1691751317;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=2uSSj325O/nrrSzfl57V3hKln92+IihZFGi4yGkEac8=;\n\tb=BYkdSfElbXUmAYq4EwDkLfE94YAjRyjzRVuNcskWD3aq5d1QbEmNngRyVTTDyrfQhT\n\t2JKdy7bu/TjC5h901GByOpybLTNqpfkoDsbSApP2YaK3ATkpnsEq4nvbXKFitKd1LnuQ\n\trFRViJWZkTig5KlUUFnxmHpoAeVOM6g/Kn+L4JMB+fbUmVCt5nnKLs+0zqrkxBN3VxNM\n\tSy4Z2kkgTbdYTvLW82Oq/YZ/xTxFz1JNub+fMq/6he0ITpG3oWcVBxjrsxeHBuIGfF2n\n\tueaDdVrXLEiFNFh4Vxhp6h7ye+kHjVll7iALL0pxvprDWHToQczJFhxhI0iD6LyuTNXt\n\tHdCQ==",
        "X-Gm-Message-State": "ABy/qLby0XOdUrGuegbLR5onrQ90woqEIZDGBhLCiQ68+Je0c2iNGWa9\n\tdcQ+mSJkahrtZM012nzq7BbXd6edEtllv79QMzB1eA==",
        "X-Google-Smtp-Source": "APBJJlFIbWrnD+bB3UJ0/mMof1q/0bwTldQ3z9WVNuLOf4HecftypdONOEjpamFjiLuXY2A46Q3KrQ==",
        "X-Received": "by 2002:a05:600c:2902:b0:3f9:b430:199b with SMTP id\n\ti2-20020a05600c290200b003f9b430199bmr17202733wmd.15.1689159316999; \n\tWed, 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",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v1 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": "Naushir Patuck via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>",
        "Reply-To": "Naushir Patuck <naush@raspberrypi.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": "From: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\nThe 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>\n---\n .../pipeline/rpi/common/pipeline_base.cpp     | 140 ++++++++++--------\n .../pipeline/rpi/common/pipeline_base.h       |   3 +\n 2 files changed, 79 insertions(+), 64 deletions(-)",
    "diff": "diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\nindex fb3756a47590..725c1db0d527 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@@ -265,6 +208,17 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n \t\t  [](auto &l, auto &r) { return l.cfg->size > r.cfg->size; });\n \n \t/* Do any platform specific fixups. */\n+\tunsigned int bitDepth = defaultRawBitDepth;\n+\tif (!rawStreams.empty()) {\n+\t\tBayerFormat bayerFormat = BayerFormat::fromPixelFormat(rawStreams[0].cfg->pixelFormat);\n+\t\tbitDepth = bayerFormat.bitDepth;\n+\t}\n+\n+\tV4L2SubdeviceFormat sensorFormat =\n+\t\tdata_->findBestFormat(rawStreams.empty() ? outStreams[0].cfg->size\n+\t\t\t\t\t\t\t : rawStreams[0].cfg->size,\n+\t\t\t\t      bitDepth);\n+\n \tstatus = data_->platformValidate(rawStreams, outStreams);\n \tif (status == Invalid)\n \t\treturn Invalid;\n@@ -275,8 +229,8 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n \t\tV4L2DeviceFormat rawFormat;\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\tbitDepth = info.isValid() ? info.bitsPerPixel : defaultRawBitDepth;\n+\t\tsensorFormat = 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 +345,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 +485,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 +909,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",
        "v1",
        "2/3"
    ]
}