From patchwork Wed Jul 8 13:44:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 8675 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id E52E9BD792 for ; Wed, 8 Jul 2020 13:44:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B3EBF61124; Wed, 8 Jul 2020 15:44:43 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="izfWgpg4"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DC9FE610B2 for ; Wed, 8 Jul 2020 15:44:42 +0200 (CEST) Received: from pyrite.rasen.tech (unknown [IPv6:2400:4051:61:600:2c71:1b79:d06d:5032]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 70D4D51B; Wed, 8 Jul 2020 15:44:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1594215882; bh=AyDMzbKkI5i3eRGnjzMm2HtvG29+dqw181TtVpwUvIA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=izfWgpg4tYjOZKFJcpyfSrznTaSXh+V9OJY8tbmP9h44dkgbPlQHMQHY0YFcuxIAY ck5Fi7awq6GSuIsCLXsu43VHBFPfJlNptO5+Wd41GB8nmdgtKeMsQHSwdPLqkgO3FY IBucZLnVnIphuAu3Ue9hmQpyGIV7VCdyzTG8Y7mE= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Wed, 8 Jul 2020 22:44:02 +0900 Message-Id: <20200708134417.67747-7-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200708134417.67747-1-paul.elder@ideasonboard.com> References: <20200708134417.67747-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 06/21] libcamera: PixelFormatInfo: Add functions stride and frameSize X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add member functions to PixelFormatInfo for calculating stride and frame size. This will simplify existing code that calculates these things. Signed-off-by: Paul Elder Reviewed-by: Laurent Pinchart --- Changes in v4: - add overloaded frameSize() that takes array of strides - add optional parameter to stride() for byte alignment Changes in v3: - rename functions to stride and frameSize, from bytesPerLine and imageSize, respectively Changes in v2: - make these functions const - add documentation - inline DIV_ROUND_UP --- include/libcamera/internal/formats.h | 6 +++ src/libcamera/formats.cpp | 73 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/include/libcamera/internal/formats.h b/include/libcamera/internal/formats.h index 054be37..e347a46 100644 --- a/include/libcamera/internal/formats.h +++ b/include/libcamera/internal/formats.h @@ -52,6 +52,12 @@ public: static const PixelFormatInfo &info(const PixelFormat &format); static const PixelFormatInfo &info(const V4L2PixelFormat &format); + unsigned int stride(unsigned int width, unsigned int plane, + unsigned int align = 0) const; + unsigned int frameSize(const Size &size) const; + unsigned int frameSize(const Size &size, + const std::array &strides) const; + /* \todo Add support for non-contiguous memory planes */ const char *name; PixelFormat format; diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp index 6d96055..c355e57 100644 --- a/src/libcamera/formats.cpp +++ b/src/libcamera/formats.cpp @@ -732,4 +732,77 @@ const PixelFormatInfo &PixelFormatInfo::info(const V4L2PixelFormat &format) return info->second; } +/** + * \brief Compute the stride + * \param[in] width The width of the line, in pixels + * \param[in] plane The index of the plane whose stride is to be computed + * \param[in] align The number of bytes to align to (0 for default alignment) + * \return The number of bytes necessary to store a line, or 0 if the + * PixelFormatInfo instance or the \a plane is not valid + */ +unsigned int PixelFormatInfo::stride(unsigned int width, unsigned int plane, + unsigned int align) const +{ + if (!isValid()) + return 0; + + if (plane > planes.size() || !planes[plane].bytesPerGroup) + return 0; + + /* ceil(width / pixelsPerGroup) * bytesPerGroup */ + unsigned int stride = (width + pixelsPerGroup - 1) / pixelsPerGroup + * planes[plane].bytesPerGroup; + + if (!align) + return stride; + + /* ceil(stride / align) * align */ + return (stride + align - 1) / align * align; +} + +/** + * \brief Compute the bytes necessary to store the frame + * \param[in] size The size of the frame, in pixels + * \return The number of bytes necessary to store the frame, or 0 if the + * PixelFormatInfo instance is not valid + */ +unsigned int PixelFormatInfo::frameSize(const Size &size) const +{ + /* stride * ceil(height / verticalSubSampling) */ + unsigned int sum = 0; + for (unsigned int i = 0; i < 3; i++) { + unsigned int vertSubSample = planes[i].verticalSubSampling; + if (!vertSubSample) + continue; + sum += stride(size.width, i) + * ((size.height + vertSubSample - 1) / vertSubSample); + } + + return sum; +} + +/** + * \brief Compute the bytes necessary to store the frame + * \param[in] size The size of the frame, in pixels + * \param[in] strides The strides to use for each plane + * \return The number of bytes necessary to store the frame, or 0 if the + * PixelFormatInfo instance is not valid + */ +unsigned int +PixelFormatInfo::frameSize(const Size &size, + const std::array &strides) const +{ + /* stride * ceil(height / verticalSubSampling) */ + unsigned int sum = 0; + for (unsigned int i = 0; i < 3; i++) { + unsigned int vertSubSample = planes[i].verticalSubSampling; + if (!vertSubSample) + continue; + sum += strides[i] + * ((size.height + vertSubSample - 1) / vertSubSample); + } + + return sum; +} + } /* namespace libcamera */