{"id":14893,"url":"https://patchwork.libcamera.org/api/patches/14893/?format=json","web_url":"https://patchwork.libcamera.org/patch/14893/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20211130112259.18723-3-david.plowman@raspberrypi.com>","date":"2021-11-30T11:22:59","name":"[libcamera-devel,2/2] pipeline: raspberrypi: Choose bit depth and packing according to raw stream","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"061135332088a2c556c4fc6e053f1055c357d9a2","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/people/42/?format=json","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14893/mbox/","series":[{"id":2783,"url":"https://patchwork.libcamera.org/api/series/2783/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2783","date":"2021-11-30T11:22:57","name":"Buffer format and allocation fixes","version":1,"mbox":"https://patchwork.libcamera.org/series/2783/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14893/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14893/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 6E778C324F\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Nov 2021 11:23:20 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1503F605BA;\n\tTue, 30 Nov 2021 12:23:20 +0100 (CET)","from mail-wr1-x430.google.com (mail-wr1-x430.google.com\n\t[IPv6:2a00:1450:4864:20::430])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7541C60587\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 12:23:16 +0100 (CET)","by mail-wr1-x430.google.com with SMTP id j3so43647377wrp.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 03:23:16 -0800 (PST)","from pi4-davidp.pitowers.org\n\t([2a00:1098:3142:14:e4a2:3070:eea4:e434])\n\tby smtp.gmail.com with ESMTPSA id\n\tu2sm19054923wrs.17.2021.11.30.03.23.15\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 30 Nov 2021 03:23:15 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"E5ACehJh\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=2F2qAO8MKIglq4NnZW8/UIdKUY8Q5e13Dbp8ZkesB50=;\n\tb=E5ACehJh0qEMo9B01HrdDk/eLzKp0NymBawFVLS/bS9CX0TgzgB+MlsZXr6NcY8j2d\n\tGE+C8RipTDeAVybLrb9kG3zaJFDcEHke6ybiBx4sr82yc9OvieoDKVPrWOM6KMvky7K+\n\tK4DChkrT+kFomKDI9m+KIhUes5m3OxSjWyo0Y7twkrY6bCCpc797c2qNM3/Laag6f8Bh\n\tqO6f0EK9TRWoq3DMUDzrNs51Z7Q2wN7DqTLg4nl6szNjmtqMiS8k3jwM2rcC2eH4WMmt\n\tGR9NkoihdFhTOICtdT2u+v9ManDP1N8F7QY44bG3OzuInP9BzfG1V8zHkhCtjA/2Ecis\n\thc7g==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=2F2qAO8MKIglq4NnZW8/UIdKUY8Q5e13Dbp8ZkesB50=;\n\tb=XtdIPeC0qB/7ahdSNby03xyAjJa8/1apWiaS8C4EBABLdRg5r2Wa5leGvShWDZJPex\n\ttI6T67eoHXlcDoaIB93SO0PXyDIeBDX+2R9WmOGldRwtM9QnEDZ802THcM//254/CJZp\n\taLUm3mJ3kOPc2Yuv46RSq/Z24rmbdxstz/SwX4Ub42XlPB4UYcy+/7WGwuolxJKzMn9x\n\tV+xO5Yz42KYz1uhi0BT3rSYYnNcL0N3cSoKNjo6pldvKb03/+KaOnqwJWy6xwiw/xcJZ\n\tRqNXOs9QQFld057U8uhEha+0BLCwpsU2hc8GwWe6pcYPnK6lFO5JGkiNfg5KxmNGamtK\n\td7Dg==","X-Gm-Message-State":"AOAM532b2atFQd3f9kMJ+w77f7vauhetNpFR4/jSA9wk6snEj/NOq8rK\n\tX6tmvWoGTXxgU3PsRtE29UTfnAPsCm4k3Vcp","X-Google-Smtp-Source":"ABdhPJybUtltRbENGo2sXml7UXxsALol7mLq0AM1wwDqfOmfSeLQadSpPAdsQtmXJQ4yk78Rt283qg==","X-Received":"by 2002:adf:f1cc:: with SMTP id\n\tz12mr31470613wro.395.1638271395996; \n\tTue, 30 Nov 2021 03:23:15 -0800 (PST)","From":"David Plowman <david.plowman@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 30 Nov 2021 11:22:59 +0000","Message-Id":"<20211130112259.18723-3-david.plowman@raspberrypi.com>","X-Mailer":"git-send-email 2.30.2","In-Reply-To":"<20211130112259.18723-1-david.plowman@raspberrypi.com>","References":"<20211130112259.18723-1-david.plowman@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 2/2] pipeline: raspberrypi: Choose bit\n\tdepth and packing according to raw stream","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"When a raw stream is specified, the bit depth and packing requested\nshould influence our choice of camera mode to match (if possible).\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi.com>\n---\n .../pipeline/raspberrypi/raspberrypi.cpp      | 33 ++++++++++---------\n 1 file changed, 18 insertions(+), 15 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex 045725dd..03012e89 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -49,6 +49,8 @@ LOG_DEFINE_CATEGORY(RPI)\n \n namespace {\n \n+unsigned int DEFAULT_RAW_BIT_DEPTH = 12;\n+\n /* Map of mbus codes to supported sizes reported by the sensor. */\n using SensorFormats = std::map<unsigned int, std::vector<Size>>;\n \n@@ -125,15 +127,13 @@ double scoreFormat(double desired, double actual)\n \treturn score;\n }\n \n-V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size &req)\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 \n #define PENALTY_AR\t\t1500.0\n-#define PENALTY_8BIT\t\t2000.0\n-#define PENALTY_10BIT\t\t1000.0\n-#define PENALTY_12BIT\t\t   0.0\n+#define PENALTY_BIT_DEPTH\t500.0\n \n \t/* Calculate the closest/best mode from the user requested size. */\n \tfor (const auto &iter : formatsMap) {\n@@ -152,12 +152,7 @@ V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size &\n \t\t\tscore += PENALTY_AR * scoreFormat(reqAr, fmtAr);\n \n \t\t\t/* Add any penalties... this is not an exact science! */\n-\t\t\tif (info.bitsPerPixel == 12)\n-\t\t\t\tscore += PENALTY_12BIT;\n-\t\t\telse if (info.bitsPerPixel == 10)\n-\t\t\t\tscore += PENALTY_10BIT;\n-\t\t\telse if (info.bitsPerPixel == 8)\n-\t\t\t\tscore += PENALTY_8BIT;\n+\t\t\tscore += abs(info.bitsPerPixel - bitDepth) * PENALTY_BIT_DEPTH;\n \n \t\t\tif (score <= bestScore) {\n \t\t\t\tbestScore = score;\n@@ -397,9 +392,14 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n \t\t\t * Calculate the best sensor mode we can use based on\n \t\t\t * the user request.\n \t\t\t */\n-\t\t\tV4L2SubdeviceFormat sensorFormat = findBestFormat(data_->sensorFormats_, cfg.size);\n+\t\t\tconst PixelFormatInfo &info = PixelFormatInfo::info(cfg.pixelFormat);\n+\t\t\tunsigned int bitDepth = info.isValid() ? info.bitsPerPixel : DEFAULT_RAW_BIT_DEPTH;\n+\t\t\tV4L2SubdeviceFormat sensorFormat = findBestFormat(data_->sensorFormats_, cfg.size, bitDepth);\n+\t\t\tBayerFormat::Packing packing = BayerFormat::Packing::CSI2;\n+\t\t\tif (info.isValid() && !info.packed)\n+\t\t\t\tpacking = BayerFormat::Packing::None;\n \t\t\tV4L2DeviceFormat unicamFormat = toV4L2DeviceFormat(sensorFormat,\n-\t\t\t\t\t\t\t\t\t   BayerFormat::Packing::CSI2);\n+\t\t\t\t\t\t\t\t\t   packing);\n \t\t\tint ret = data_->unicam_[Unicam::Image].dev()->tryFormat(&unicamFormat);\n \t\t\tif (ret)\n \t\t\t\treturn Invalid;\n@@ -533,7 +533,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera,\n \t\tswitch (role) {\n \t\tcase StreamRole::Raw:\n \t\t\tsize = data->sensor_->resolution();\n-\t\t\tsensorFormat = findBestFormat(data->sensorFormats_, size);\n+\t\t\tsensorFormat = findBestFormat(data->sensorFormats_, size, DEFAULT_RAW_BIT_DEPTH);\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@@ -622,6 +622,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n \tSize maxSize, sensorSize;\n \tunsigned int maxIndex = 0;\n \tbool rawStream = false;\n+\tunsigned int bitDepth = DEFAULT_RAW_BIT_DEPTH;\n \n \t/*\n \t * Look for the RAW stream (if given) size as well as the largest\n@@ -638,7 +639,9 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n \t\t\tsensorSize = cfg.size;\n \t\t\trawStream = true;\n \t\t\t/* Check if the user has explicitly set an unpacked format. */\n-\t\t\tpacking = BayerFormat::fromPixelFormat(cfg.pixelFormat).packing;\n+\t\t\tBayerFormat bayerFormat = BayerFormat::fromPixelFormat(cfg.pixelFormat);\n+\t\t\tpacking = bayerFormat.packing;\n+\t\t\tbitDepth = bayerFormat.bitDepth;\n \t\t} else {\n \t\t\tif (cfg.size > maxSize) {\n \t\t\t\tmaxSize = config->at(i).size;\n@@ -664,7 +667,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n \t}\n \n \t/* First calculate the best sensor mode we can use based on the user request. */\n-\tV4L2SubdeviceFormat sensorFormat = findBestFormat(data->sensorFormats_, rawStream ? sensorSize : maxSize);\n+\tV4L2SubdeviceFormat sensorFormat = findBestFormat(data->sensorFormats_, rawStream ? sensorSize : maxSize, bitDepth);\n \tret = data->sensor_->setFormat(&sensorFormat);\n \tif (ret)\n \t\treturn ret;\n","prefixes":["libcamera-devel","2/2"]}