{"id":13156,"url":"https://patchwork.libcamera.org/api/patches/13156/?format=json","web_url":"https://patchwork.libcamera.org/patch/13156/","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":"<20210730082832.152626-3-umang.jain@ideasonboard.com>","date":"2021-07-30T08:28:27","name":"[libcamera-devel,RFC,2/3] ipu3: cio2: Replicate CameraSensor::getFormats() to a member function","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"9076d3ed8e23d9f683c8d22d503a518f942f2d3e","submitter":{"id":86,"url":"https://patchwork.libcamera.org/api/people/86/?format=json","name":"Umang Jain","email":"umang.jain@ideasonboard.com"},"delegate":{"id":12,"url":"https://patchwork.libcamera.org/api/users/12/?format=json","username":"uajain","first_name":"Umang","last_name":"Jain","email":"umang.jain@ideasonboard.com"},"mbox":"https://patchwork.libcamera.org/patch/13156/mbox/","series":[{"id":2290,"url":"https://patchwork.libcamera.org/api/series/2290/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2290","date":"2021-07-30T08:28:25","name":"IPU3: Tweak sensor format selection","version":1,"mbox":"https://patchwork.libcamera.org/series/2290/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13156/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13156/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 38F8DC322E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 30 Jul 2021 08:28:53 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EF651687C7;\n\tFri, 30 Jul 2021 10:28:52 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 717CE687BD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 30 Jul 2021 10:28:51 +0200 (CEST)","from perceval.ideasonboard.com (unknown [103.251.226.16])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 6AD7989B;\n\tFri, 30 Jul 2021 10:28:50 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"IGu8KJt/\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1627633731;\n\tbh=m/bt9Xd2CfewM3NefRaUavg/Zat0rXEt+xHTqV/EO1k=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=IGu8KJt/MEsuA6yd+J+vvrk2bGVywb3iF0ByQO6fRqgMtk6XM7hTV2WeuSsOb6o97\n\t4iEQjcpLtiziI5ccyVXAfeNrUktUNZS0udTTiQTo52aPOrMj1ztYP3053FMoRXck1A\n\tScLBjEFkTp96rH0o84eH4uhf/rFilUaENPvKNII4=","From":"Umang Jain <umang.jain@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 30 Jul 2021 13:58:27 +0530","Message-Id":"<20210730082832.152626-3-umang.jain@ideasonboard.com>","X-Mailer":"git-send-email 2.31.1","In-Reply-To":"<20210730082832.152626-1-umang.jain@ideasonboard.com>","References":"<20210730082832.152626-1-umang.jain@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC PATCH 2/3] 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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"This prepares a base to introduce custom selection of sensor format\nbased on platform(soraka or nautilus) constraints. The changes in\nselection policy will be introduced in a subsequent patch.\n\nNo functional changes in this patch.\n\nSigned-off-by: Umang Jain <umang.jain@ideasonboard.com>\n---\n src/libcamera/pipeline/ipu3/cio2.cpp | 101 ++++++++++++++++++++++++++-\n src/libcamera/pipeline/ipu3/cio2.h   |   5 ++\n 2 files changed, 104 insertions(+), 2 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/ipu3/cio2.cpp b/src/libcamera/pipeline/ipu3/cio2.cpp\nindex 1bcd580e..aef88afd 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@@ -120,6 +122,14 @@ int CIO2Device::init(const MediaDevice *media, unsigned int index)\n \tif (ret)\n \t\treturn ret;\n \n+\tformats_ = sensor_->formats();\n+\tif (formats_.empty()) {\n+\t\tLOG(IPU3, Error)\n+\t\t\t<< \"Sensor \" << sensor_->entity()->name()\n+\t\t\t<< \"has empty sensor format list\";\n+\t\treturn -EINVAL;\n+\t}\n+\n \t/*\n \t * Make sure the sensor produces at least one format compatible with\n \t * the CIO2 requirements.\n@@ -168,7 +178,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@@ -206,7 +216,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@@ -219,6 +229,93 @@ 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+\tfloat desiredRatio = static_cast<float>(size.width) / size.height;\n+\tfloat bestRatio = FLT_MAX;\n+\tconst Size *bestSize = nullptr;\n+\tuint32_t bestCode = 0;\n+\n+\tfor (unsigned int code : mbusCodes) {\n+\t\tconst auto formats = formats_.find(code);\n+\t\tif (formats == formats_.end())\n+\t\t\tcontinue;\n+\n+\t\tfor (const SizeRange &range : formats->second) {\n+\t\t\tconst Size &sz = range.max;\n+\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) {\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 {\ndiff --git a/src/libcamera/pipeline/ipu3/cio2.h b/src/libcamera/pipeline/ipu3/cio2.h\nindex f28e9f1d..f3f12706 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+\n \tint start();\n \tint stop();\n \n@@ -67,6 +70,8 @@ private:\n \tstd::unique_ptr<V4L2Subdevice> csi2_;\n \tstd::unique_ptr<V4L2VideoDevice> output_;\n \n+\tV4L2Subdevice::Formats formats_;\n+\n \tstd::vector<std::unique_ptr<FrameBuffer>> buffers_;\n \tstd::queue<FrameBuffer *> availableBuffers_;\n };\n","prefixes":["libcamera-devel","RFC","2/3"]}