[{"id":18630,"web_url":"https://patchwork.libcamera.org/comment/18630/","msgid":"<20210809155644.oovocxchjwh3zeou@uno.localdomain>","date":"2021-08-09T15:56:44","subject":"Re: [libcamera-devel] [PATCH 2/4] ipu3: cio2: Replicate\n\tCameraSensor::getFormats() to a member function","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Umang,\n\nOn Tue, Aug 03, 2021 at 07:02:03PM +0530, Umang Jain wrote:\n> This prepares a base to introduce custom selection of sensor format\n> based on platform(soraka or nautilus) constraints. The changes in\n> selection policy will be introduced in a subsequent patch.\n>\n> The replication of the function is not verbatim and has small changes.\n\nI would say that this copied CameraSensor::getFormat() in the PH code\nto be later changed on top. The \"the function\" here above is clear to\nus that know the context.\n\n> CameraSensor::getFormats() has accesss to all the supported sensor\n> formats via a V4L2Subdevice::Formats internally and use it directly.\n> We do not want to expose V4L2Subdevice::Formats directly to the\n> pipeline-handler hence, the patch is adapted to use the\n> CameraSensor::sizes(mbusCode) instead, to get the range of sensor sizes\n> supported for desired media bus codes.\n>\n> No functional changes in this patch.\n>\n> Signed-off-by: Umang Jain <umang.jain@ideasonboard.com>\n> ---\n>  src/libcamera/pipeline/ipu3/cio2.cpp | 91 +++++++++++++++++++++++++++-\n>  src/libcamera/pipeline/ipu3/cio2.h   |  3 +\n>  2 files changed, 92 insertions(+), 2 deletions(-)\n>\n> diff --git a/src/libcamera/pipeline/ipu3/cio2.cpp b/src/libcamera/pipeline/ipu3/cio2.cpp\n> index 6f2ef321..be063613 100644\n> --- a/src/libcamera/pipeline/ipu3/cio2.cpp\n> +++ b/src/libcamera/pipeline/ipu3/cio2.cpp\n> @@ -7,6 +7,8 @@\n>\n>  #include \"cio2.h\"\n>\n> +#include <float.h>\n> +\n>  #include <linux/media-bus-format.h>\n>\n>  #include <libcamera/formats.h>\n> @@ -170,7 +172,7 @@ int CIO2Device::configure(const Size &size, V4L2DeviceFormat *outputFormat)\n>  \t * the CIO2 output device.\n>  \t */\n>  \tstd::vector<unsigned int> mbusCodes = utils::map_keys(mbusCodesToPixelFormat);\n> -\tsensorFormat = sensor_->getFormat(mbusCodes, size);\n> +\tsensorFormat = getSensorFormat(mbusCodes, size);\n>  \tret = sensor_->setFormat(&sensorFormat);\n>  \tif (ret)\n>  \t\treturn ret;\n> @@ -208,7 +210,7 @@ StreamConfiguration CIO2Device::generateConfiguration(Size size) const\n>\n>  \t/* Query the sensor static information for closest match. */\n>  \tstd::vector<unsigned int> mbusCodes = utils::map_keys(mbusCodesToPixelFormat);\n> -\tV4L2SubdeviceFormat sensorFormat = sensor_->getFormat(mbusCodes, size);\n> +\tV4L2SubdeviceFormat sensorFormat = getSensorFormat(mbusCodes, size);\n>  \tif (!sensorFormat.mbus_code) {\n>  \t\tLOG(IPU3, Error) << \"Sensor does not support mbus code\";\n>  \t\treturn {};\n> @@ -221,6 +223,91 @@ StreamConfiguration CIO2Device::generateConfiguration(Size size) const\n>  \treturn cfg;\n>  }\n>\n> +/**\n> + * \\brief Retrieve the best sensor format for a desired output\n> + * \\param[in] mbusCodes The list of acceptable media bus codes\n> + * \\param[in] size The desired size\n> + *\n> + * Media bus codes are selected from \\a mbusCodes, which lists all acceptable\n> + * codes in decreasing order of preference. Media bus codes supported by the\n> + * sensor but not listed in \\a mbusCodes are ignored. If none of the desired\n> + * codes is supported, it returns an error.\n> + *\n> + * \\a size indicates the desired size at the output of the sensor. This method\n> + * selects the best media bus code and size supported by the sensor according\n> + * to the following criteria.\n> + *\n> + * - The desired \\a size shall fit in the sensor output size to avoid the need\n> + *   to up-scale.\n> + * - The sensor output size shall match the desired aspect ratio to avoid the\n> + *   need to crop the field of view.\n> + * - The sensor output size shall be as small as possible to lower the required\n> + *   bandwidth.\n> + * - The desired \\a size shall be supported by one of the media bus code listed\n> + *   in \\a mbusCodes.\n> + *\n> + * When multiple media bus codes can produce the same size, the code at the\n> + * lowest position in \\a mbusCodes is selected.\n> + *\n> + * The use of this method is optional, as the above criteria may not match the\n> + * needs of all pipeline handlers. Pipeline handlers may implement custom\n> + * sensor format selection when needed.\n> + *\n> + * The returned sensor output format is guaranteed to be acceptable by the\n> + * setFormat() method without any modification.\n> + *\n> + * \\return The best sensor output format matching the desired media bus codes\n> + * and size on success, or an empty format otherwise.\n> + */\n> +V4L2SubdeviceFormat CIO2Device::getSensorFormat(const std::vector<unsigned int> &mbusCodes,\n> +\t\t\t\t\t\tconst Size &size) const\n> +{\n> +\tunsigned int desiredArea = size.width * size.height;\n> +\tunsigned int bestArea = UINT_MAX;\n\nwe should drop the C versions and use std::numeric_limits<> instead\nMaybe on top ?\n\n> +\tfloat desiredRatio = static_cast<float>(size.width) / size.height;\n> +\tfloat bestRatio = FLT_MAX;\n> +\tSize bestSize;\n> +\tuint32_t bestCode = 0;\n> +\n> +\tfor (unsigned int code : mbusCodes) {\n> +\t\tconst auto sizes = sensor_->sizes(code);\n> +\t\tif (!sizes.size())\n> +\t\t\tcontinue;\n> +\n> +\t\tfor (const Size &sz : sizes) {\n> +\t\t\tif (sz.width < size.width || sz.height < size.height)\n> +\t\t\t\tcontinue;\n> +\n> +\t\t\tfloat ratio = static_cast<float>(sz.width) / sz.height;\n> +\t\t\tfloat ratioDiff = fabsf(ratio - desiredRatio);\n> +\t\t\tunsigned int area = sz.width * sz.height;\n> +\t\t\tunsigned int areaDiff = area - desiredArea;\n> +\n> +\t\t\tif (ratioDiff > bestRatio)\n> +\t\t\t\tcontinue;\n> +\n> +\t\t\tif (ratioDiff < bestRatio || areaDiff < bestArea) {\n> +\t\t\t\tbestRatio = ratioDiff;\n> +\t\t\t\tbestArea = areaDiff;\n> +\t\t\t\tbestSize = sz;\n> +\t\t\t\tbestCode = code;\n> +\t\t\t}\n> +\t\t}\n> +\t}\n> +\n> +\tif (bestSize.isNull()) {\n> +\t\tLOG(IPU3, Debug) << \"No supported format or size found\";\n> +\t\treturn {};\n> +\t}\n> +\n> +\tV4L2SubdeviceFormat format{\n> +\t\t.mbus_code = bestCode,\n> +\t\t.size = bestSize,\n> +\t};\n> +\n> +\treturn format;\n> +}\n> +\n>  int CIO2Device::exportBuffers(unsigned int count,\n>  \t\t\t      std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n>  {\n> diff --git a/src/libcamera/pipeline/ipu3/cio2.h b/src/libcamera/pipeline/ipu3/cio2.h\n> index f28e9f1d..da024d45 100644\n> --- a/src/libcamera/pipeline/ipu3/cio2.h\n> +++ b/src/libcamera/pipeline/ipu3/cio2.h\n> @@ -45,6 +45,9 @@ public:\n>  \tint exportBuffers(unsigned int count,\n>  \t\t\t  std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n>\n> +\tV4L2SubdeviceFormat getSensorFormat(const std::vector<unsigned int> &mbusCodes,\n> +\t\t\t\t\t    const Size &size) const;\n\nThe patch is good!\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n\nThanks\n   j\n\n> +\n>  \tint start();\n>  \tint stop();\n>\n> --\n> 2.31.0\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 62FB1C3240\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  9 Aug 2021 15:55:58 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id BC65968826;\n\tMon,  9 Aug 2021 17:55:57 +0200 (CEST)","from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net\n\t[217.70.183.200])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3C02960269\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  9 Aug 2021 17:55:56 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby relay7-d.mail.gandi.net (Postfix) with ESMTPSA id B183820004;\n\tMon,  9 Aug 2021 15:55:55 +0000 (UTC)"],"Date":"Mon, 9 Aug 2021 17:56:44 +0200","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"Umang Jain <umang.jain@ideasonboard.com>","Message-ID":"<20210809155644.oovocxchjwh3zeou@uno.localdomain>","References":"<20210803133205.6599-1-umang.jain@ideasonboard.com>\n\t<20210803133205.6599-3-umang.jain@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210803133205.6599-3-umang.jain@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH 2/4] ipu3: cio2: Replicate\n\tCameraSensor::getFormats() to a member function","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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]