[{"id":26927,"web_url":"https://patchwork.libcamera.org/comment/26927/","msgid":"<20230424060040.GB9864@pendragon.ideasonboard.com>","date":"2023-04-24T06:00:40","subject":"Re: [libcamera-devel] [PATCH v2 1/6] libcamera: imx8-isi: Break out\n\tRAW format selection","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Mon, Mar 13, 2023 at 10:19:39AM +0100, Jacopo Mondi wrote:\n> The current implementation of the ISI pipeline handler handles\n> translation of PixelFormat to media bus formats from the sensor\n> through a centralized map.\n> \n> As the criteria to select the correct media bus code depends on if the\n> output PixelFormat is a RAW Bayer format or not, start by splitting\n> the RAW media bus code procedure selection out by adding a function\n> for such purpose to the ISICameraData class.\n> \n> Add the function to the ISICameraData and not to the\n> ISICameraConfiguration because:\n> - The sensor is a property of CameraData\n> - The same function will be re-used by the ISIPipelineHandler\n>   during CameraConfiguration generation.\n> \n> Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n> Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>\n> Tested-by: Daniel Scally <dan.scally@ideasonboard.com>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  src/libcamera/pipeline/imx8-isi/imx8-isi.cpp | 130 +++++++++++++------\n>  1 file changed, 90 insertions(+), 40 deletions(-)\n> \n> diff --git a/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp b/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp\n> index 0c67e35dde73..ffb96e6284f0 100644\n> --- a/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp\n> +++ b/src/libcamera/pipeline/imx8-isi/imx8-isi.cpp\n> @@ -59,6 +59,8 @@ public:\n>  \t\treturn stream - &*streams_.begin();\n>  \t}\n>  \n> +\tunsigned int getRawMediaBusFormat(PixelFormat *pixelFormat) const;\n> +\n>  \tstd::unique_ptr<CameraSensor> sensor_;\n>  \tstd::unique_ptr<V4L2Subdevice> csis_;\n>  \n> @@ -174,6 +176,85 @@ int ISICameraData::init()\n>  \treturn 0;\n>  }\n>  \n> +/*\n> + * Get a RAW Bayer media bus format compatible with the requested pixelFormat.\n> + *\n> + * If the requested pixelFormat cannot be produced by the sensor adjust it to\n> + * the one corresponding to the media bus format with the largest bit-depth.\n> + */\n> +unsigned int ISICameraData::getRawMediaBusFormat(PixelFormat *pixelFormat) const\n> +{\n> +\tstd::vector<unsigned int> mbusCodes = sensor_->mbusCodes();\n> +\n> +\tstatic const std::map<PixelFormat, unsigned int> rawFormats = {\n> +\t\t{ formats::SBGGR8, MEDIA_BUS_FMT_SBGGR8_1X8 },\n> +\t\t{ formats::SGBRG8, MEDIA_BUS_FMT_SGBRG8_1X8 },\n> +\t\t{ formats::SGRBG8, MEDIA_BUS_FMT_SGRBG8_1X8 },\n> +\t\t{ formats::SRGGB8, MEDIA_BUS_FMT_SRGGB8_1X8 },\n> +\t\t{ formats::SBGGR10, MEDIA_BUS_FMT_SBGGR10_1X10 },\n> +\t\t{ formats::SGBRG10, MEDIA_BUS_FMT_SGBRG10_1X10 },\n> +\t\t{ formats::SGRBG10, MEDIA_BUS_FMT_SGRBG10_1X10 },\n> +\t\t{ formats::SRGGB10, MEDIA_BUS_FMT_SRGGB10_1X10 },\n> +\t\t{ formats::SBGGR12, MEDIA_BUS_FMT_SBGGR12_1X12 },\n> +\t\t{ formats::SGBRG12, MEDIA_BUS_FMT_SGBRG12_1X12 },\n> +\t\t{ formats::SGRBG12, MEDIA_BUS_FMT_SGRBG12_1X12 },\n> +\t\t{ formats::SRGGB12, MEDIA_BUS_FMT_SRGGB12_1X12 },\n\nNow that libcamera has RAW14 support, could you add those formats here ?\n\n> +\t};\n> +\n> +\t/*\n> +\t * Make sure the requested PixelFormat is supported in the above\n> +\t * map and the sensor can produce the compatible mbus code.\n> +\t */\n> +\tauto it = rawFormats.find(*pixelFormat);\n> +\tif (it != rawFormats.end() &&\n> +\t    std::count(mbusCodes.begin(), mbusCodes.end(), it->second))\n> +\t\treturn it->second;\n> +\n> +\tif (it == rawFormats.end())\n> +\t\tLOG(ISI, Warning) << pixelFormat\n> +\t\t\t\t  << \" not supported in ISI formats map.\";\n> +\n> +\t/*\n> +\t * The desired pixel format cannot be produced. Adjust it to the one\n> +\t * corresponding to the raw media bus format with the largest bit-depth\n> +\t * the sensor provides.\n> +\t */\n> +\tunsigned int sensorCode = 0;\n> +\tunsigned int maxDepth = 0;\n> +\t*pixelFormat = {};\n> +\n> +\tfor (unsigned int code : mbusCodes) {\n> +\t\t/* Make sure the media bus format is RAW Bayer. */\n> +\t\tconst BayerFormat &bayerFormat = BayerFormat::fromMbusCode(code);\n> +\t\tif (!bayerFormat.isValid())\n> +\t\t\tcontinue;\n> +\n> +\t\t/* Make sure the media format is supported. */\n> +\t\tit = std::find_if(rawFormats.begin(), rawFormats.end(),\n> +\t\t\t\t  [code](auto &rawFormat) {\n> +\t\t\t\t\t  return rawFormat.second == code;\n> +\t\t\t\t  });\n> +\n> +\t\tif (it == rawFormats.end()) {\n> +\t\t\tLOG(ISI, Warning) << bayerFormat\n> +\t\t\t\t\t  << \" not supported in ISI formats map.\";\n> +\t\t\tcontinue;\n> +\t\t}\n> +\n> +\t\t/* Pick the one with the largest bit depth. */\n> +\t\tif (bayerFormat.bitDepth > maxDepth) {\n> +\t\t\tmaxDepth = bayerFormat.bitDepth;\n> +\t\t\t*pixelFormat = it->first;\n> +\t\t\tsensorCode = code;\n> +\t\t}\n> +\t}\n> +\n> +\tif (!pixelFormat->isValid())\n> +\t\tLOG(ISI, Error) << \"Cannot find a supported RAW format\";\n> +\n> +\treturn sensorCode;\n> +}\n> +\n>  /* -----------------------------------------------------------------------------\n>   * Camera Configuration\n>   */\n> @@ -311,50 +392,19 @@ ISICameraConfiguration::validateRaw(std::set<Stream *> &availableStreams,\n>  \t */\n>  \tstd::vector<unsigned int> mbusCodes = data_->sensor_->mbusCodes();\n>  \tStreamConfiguration &rawConfig = config_[0];\n> +\tPixelFormat rawFormat = rawConfig.pixelFormat;\n>  \n> -\tbool supported = false;\n> -\tauto it = formatsMap_.find(rawConfig.pixelFormat);\n> -\tif (it != formatsMap_.end()) {\n> -\t\tunsigned int mbusCode = it->second.sensorCode;\n> -\n> -\t\tif (std::count(mbusCodes.begin(), mbusCodes.end(), mbusCode))\n> -\t\t\tsupported = true;\n> +\tunsigned int sensorCode = data_->getRawMediaBusFormat(&rawFormat);\n> +\tif (!sensorCode) {\n> +\t\tLOG(ISI, Error) << \"Cannot adjust RAW pixelformat \"\n> +\t\t\t\t<< rawConfig.pixelFormat;\n> +\t\treturn Invalid;\n>  \t}\n>  \n> -\tif (!supported) {\n> -\t\t/*\n> -\t\t * Adjust to the first mbus code supported by both the\n> -\t\t * sensor and the pipeline.\n> -\t\t */\n> -\t\tconst FormatMap::value_type *pipeConfig = nullptr;\n> -\t\tfor (unsigned int code : mbusCodes) {\n> -\t\t\tconst BayerFormat &bayerFormat = BayerFormat::fromMbusCode(code);\n> -\t\t\tif (!bayerFormat.isValid())\n> -\t\t\t\tcontinue;\n> -\n> -\t\t\tauto fmt = std::find_if(ISICameraConfiguration::formatsMap_.begin(),\n> -\t\t\t\t\t\tISICameraConfiguration::formatsMap_.end(),\n> -\t\t\t\t\t\t[code](const auto &isiFormat) {\n> -\t\t\t\t\t\t\tconst auto &pipe = isiFormat.second;\n> -\t\t\t\t\t\t\treturn pipe.sensorCode == code;\n> -\t\t\t\t\t\t});\n> -\n> -\t\t\tif (fmt == ISICameraConfiguration::formatsMap_.end())\n> -\t\t\t\tcontinue;\n> -\n> -\t\t\tpipeConfig = &(*fmt);\n> -\t\t\tbreak;\n> -\t\t}\n> -\n> -\t\tif (!pipeConfig) {\n> -\t\t\tLOG(ISI, Error) << \"Cannot adjust RAW format \"\n> -\t\t\t\t\t<< rawConfig.pixelFormat;\n> -\t\t\treturn Invalid;\n> -\t\t}\n> -\n> -\t\trawConfig.pixelFormat = pipeConfig->first;\n> +\tif (rawFormat != rawConfig.pixelFormat) {\n>  \t\tLOG(ISI, Debug) << \"RAW pixelformat adjusted to \"\n> -\t\t\t\t<< pipeConfig->first;\n> +\t\t\t\t<< rawFormat;\n> +\t\trawConfig.pixelFormat = rawFormat;\n>  \t\tstatus = Adjusted;\n>  \t}\n>","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 6225FBE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 24 Apr 2023 06:00:29 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id CB75D627D5;\n\tMon, 24 Apr 2023 08:00:28 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 320B361EB1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 24 Apr 2023 08:00:28 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(133-32-181-51.west.xps.vectant.ne.jp [133.32.181.51])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5CE7824E2;\n\tMon, 24 Apr 2023 08:00:17 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1682316028;\n\tbh=Gmi1LOTimGUVB05YlsLLGbUc25LeeOea6UGDfLpth84=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=mMpKs0DHWOQMy6eAKhzEA9kumLDJaCMd9PcRhNlQtIaTNhACXEEF4uGTRP+Ytr5d2\n\tzubJN1tQUvUzbut6FXI9L4lAu3c4mI/+5djgwsxdTpKzK/C+GbNBwqoJxshKZKAS1C\n\tWnmVJAD0HjjZE0cBQ3pYS4r5QlXh5/FnuKpkPcY48JaXhWDMFCWc/CP3k7/HV3PUTf\n\ttvgMhmgBx0t6A+A3BDnqIxeVGESp0ozz6GB8fsH23+cX1mEUGVWLh4O4WgjKk3068P\n\tDk1NvEjWSPY9SePiPTmwEiPyqrikhRaUo6kR0CD34zfNZrv1SpAQ0MnCTd84CuCYVP\n\tJjFR9RTYxDzUQ==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1682316018;\n\tbh=Gmi1LOTimGUVB05YlsLLGbUc25LeeOea6UGDfLpth84=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=YFRlsfRwAcRbzgMfS9b2gTY77CSZsFG/njutV7my9eu77PQWyhIZh86KBT5+ycr4S\n\tsr77CN453CXKrc3QQ+/xyCp9TrKNnO89lGTellqrLaNy9bm06UOs5EonEVP//EfxGL\n\tIzCXQVgFettFyfcDYB7eS7ltdzaxOZHwNzS9WmNo="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"YFRlsfRw\"; dkim-atps=neutral","Date":"Mon, 24 Apr 2023 09:00:40 +0300","To":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Message-ID":"<20230424060040.GB9864@pendragon.ideasonboard.com>","References":"<20230313091944.9530-1-jacopo.mondi@ideasonboard.com>\n\t<20230313091944.9530-2-jacopo.mondi@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20230313091944.9530-2-jacopo.mondi@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v2 1/6] libcamera: imx8-isi: Break out\n\tRAW format selection","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":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]