{"id":8721,"url":"https://patchwork.libcamera.org/api/1.1/patches/8721/?format=json","web_url":"https://patchwork.libcamera.org/patch/8721/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20200709132835.112593-7-paul.elder@ideasonboard.com>","date":"2020-07-09T13:28:18","name":"[libcamera-devel,v5,06/23] libcamera: PixelFormatInfo: Add functions stride and frameSize","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"04f6eabdce84197a91c4972c7b74ebef103f7920","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/8721/mbox/","series":[{"id":1093,"url":"https://patchwork.libcamera.org/api/1.1/series/1093/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1093","date":"2020-07-09T13:28:12","name":"Clean up formats in v4l2-compat and pipeline handlers","version":5,"mbox":"https://patchwork.libcamera.org/series/1093/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/8721/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/8721/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 B2D3ABD792\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  9 Jul 2020 13:29:05 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8202F61218;\n\tThu,  9 Jul 2020 15:29:05 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8C6EE61184\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  9 Jul 2020 15:29:04 +0200 (CEST)","from pyrite.rasen.tech (unknown\n\t[IPv6:2400:4051:61:600:2c71:1b79:d06d:5032])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B6F24525;\n\tThu,  9 Jul 2020 15:29:01 +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=\"urQQwHjS\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1594301343;\n\tbh=Myx76QvUqwfMypzOWC1GMNsc6zeXn6XAate9783uJ2E=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=urQQwHjSmGbCAXS6ux8D8Fw2Hun98M3eartk0sXS8PDJxsQO+Ru5mNS0FK806DqWW\n\taSnLUZgORUiFRjDyirlShwQiXCo08S54bWqnE03LazUjCfLhHCnpcXr4jhNMPy5lkz\n\ti6UrWrQuopFM0OLQN+IB/G79nsHr0AWnSFgT/WIk=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu,  9 Jul 2020 22:28:18 +0900","Message-Id":"<20200709132835.112593-7-paul.elder@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20200709132835.112593-1-paul.elder@ideasonboard.com>","References":"<20200709132835.112593-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v5 06/23] libcamera: PixelFormatInfo: Add\n\tfunctions stride and frameSize","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add member functions to PixelFormatInfo for calculating stride and frame\nsize. This will simplify existing code that calculates these things.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n---\nChanges in v5:\n- changed default align parameter for stride() to 1\n- added align parameter to frameSize()\n\nChanges in v4:\n- add overloaded frameSize() that takes array of strides\n- add optional parameter to stride() for byte alignment\n\nChanges in v3:\n- rename functions to stride and frameSize, from bytesPerLine and\n  imageSize, respectively\n\nChanges in v2:\n- make these functions const\n- add documentation\n- inline DIV_ROUND_UP\n---\n include/libcamera/internal/formats.h |  6 +++\n src/libcamera/formats.cpp            | 80 ++++++++++++++++++++++++++++\n 2 files changed, 86 insertions(+)","diff":"diff --git a/include/libcamera/internal/formats.h b/include/libcamera/internal/formats.h\nindex 211da4a..cad41ad 100644\n--- a/include/libcamera/internal/formats.h\n+++ b/include/libcamera/internal/formats.h\n@@ -53,6 +53,12 @@ public:\n \tstatic const PixelFormatInfo &info(const PixelFormat &format);\n \tstatic const PixelFormatInfo &info(const V4L2PixelFormat &format);\n \n+\tunsigned int stride(unsigned int width, unsigned int plane,\n+\t\t\t    unsigned int align = 1) const;\n+\tunsigned int frameSize(const Size &size, unsigned int align = 1) const;\n+\tunsigned int frameSize(const Size &size,\n+\t\t\t       const std::array<unsigned int, 3> &strides) const;\n+\n \t/* \\todo Add support for non-contiguous memory planes */\n \tconst char *name;\n \tPixelFormat format;\ndiff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp\nindex 6d96055..0f71061 100644\n--- a/src/libcamera/formats.cpp\n+++ b/src/libcamera/formats.cpp\n@@ -732,4 +732,84 @@ const PixelFormatInfo &PixelFormatInfo::info(const V4L2PixelFormat &format)\n \treturn info->second;\n }\n \n+/**\n+ * \\brief Compute the stride\n+ * \\param[in] width The width of the line, in pixels\n+ * \\param[in] plane The index of the plane whose stride is to be computed\n+ * \\param[in] align The number of bytes to align to (1 for default alignment)\n+ * \\return The number of bytes necessary to store a line, or 0 if the\n+ * PixelFormatInfo instance or the \\a plane is not valid\n+ */\n+unsigned int PixelFormatInfo::stride(unsigned int width, unsigned int plane,\n+\t\t\t\t     unsigned int align) const\n+{\n+\tif (!isValid()) {\n+\t\tLOG(Formats, Warning) << \"Invalid pixel format, stride is zero\";\n+\t\treturn 0;\n+\t}\n+\n+\tif (plane > planes.size() || !planes[plane].bytesPerGroup) {\n+\t\tLOG(Formats, Warning) << \"Invalid plane index, stride is zero\";\n+\t\treturn 0;\n+\t}\n+\n+\t/* ceil(width / pixelsPerGroup) * bytesPerGroup */\n+\tunsigned int stride = (width + pixelsPerGroup - 1) / pixelsPerGroup\n+\t\t\t    * planes[plane].bytesPerGroup;\n+\n+\t/* ceil(stride / align) * align */\n+\treturn (stride + align - 1) / align * align;\n+}\n+\n+/**\n+ * \\brief Compute the number of bytes necessary to store a frame\n+ * \\param[in] size The size of the frame, in pixels\n+ * \\param[in] align The number of bytes to align to (1 for default alignment)\n+ * \\return The number of bytes necessary to store the frame, or 0 if the\n+ * PixelFormatInfo instance is not valid\n+ */\n+unsigned int PixelFormatInfo::frameSize(const Size &size, unsigned int align) const\n+{\n+\t/* stride * ceil(height / verticalSubSampling) */\n+\tunsigned int sum = 0;\n+\tfor (unsigned int i = 0; i < 3; i++) {\n+\t\tunsigned int vertSubSample = planes[i].verticalSubSampling;\n+\t\tif (!vertSubSample)\n+\t\t\tcontinue;\n+\t\tsum += stride(size.width, i, align)\n+\t\t     * ((size.height + vertSubSample - 1) / vertSubSample);\n+\t}\n+\n+\treturn sum;\n+}\n+\n+/**\n+ * \\brief Compute the number of bytes necessary to store a frame\n+ * \\param[in] size The size of the frame, in pixels\n+ * \\param[in] strides The strides to use for each plane\n+ *\n+ * This function is an overloaded version that takes custom strides for each\n+ * plane, to be used when the device has custom alignment constraints that\n+ * can't be described by just an alignment value.\n+ *\n+ * \\return The number of bytes necessary to store the frame, or 0 if the\n+ * PixelFormatInfo instance is not valid\n+ */\n+unsigned int\n+PixelFormatInfo::frameSize(const Size &size,\n+\t\t\t   const std::array<unsigned int, 3> &strides) const\n+{\n+\t/* stride * ceil(height / verticalSubSampling) */\n+\tunsigned int sum = 0;\n+\tfor (unsigned int i = 0; i < 3; i++) {\n+\t\tunsigned int vertSubSample = planes[i].verticalSubSampling;\n+\t\tif (!vertSubSample)\n+\t\t\tcontinue;\n+\t\tsum += strides[i]\n+\t\t     * ((size.height + vertSubSample - 1) / vertSubSample);\n+\t}\n+\n+\treturn sum;\n+}\n+\n } /* namespace libcamera */\n","prefixes":["libcamera-devel","v5","06/23"]}