From patchwork Sat Jul 4 13:31:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 8627 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 5E992BD792 for ; Sat, 4 Jul 2020 13:32:08 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2803860E45; Sat, 4 Jul 2020 15:32:08 +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="uDiydM4u"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 35AB9609C7 for ; Sat, 4 Jul 2020 15:32:07 +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 B9D2D296; Sat, 4 Jul 2020 15:32:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1593869526; bh=D+f6lceFHOA535RfPSvxnxGwzZdlZQCwTfdtlgJkm+E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uDiydM4uo7/uQWdE4mcztGS4E4MFV61UknnfYge8oCCooymTWnuF/2BhtsWR20+xg q9ZgwSHHg/KfVwK9ZzS+gRTbWPsktTnwxGV2skL4EBugCO9cMOR7/GHXFVmVLH7MW7 RFkzl6SnPVLYZP1amUKjsQ5W5jK+c8sGSVMwqrHc= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Sat, 4 Jul 2020 22:31:24 +0900 Message-Id: <20200704133140.1738660-7-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200704133140.1738660-1-paul.elder@ideasonboard.com> References: <20200704133140.1738660-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 06/22] 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 --- 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 | 3 +++ src/libcamera/formats.cpp | 40 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/libcamera/internal/formats.h b/include/libcamera/internal/formats.h index cf00814..8957059 100644 --- a/include/libcamera/internal/formats.h +++ b/include/libcamera/internal/formats.h @@ -52,6 +52,9 @@ public: static const PixelFormatInfo &info(const PixelFormat &format); static const PixelFormatInfo &info(const V4L2PixelFormat &format); + unsigned int stride(unsigned int width, unsigned int plane) const; + unsigned int frameSize(const Size &size) const; + const char *name; PixelFormat format; V4L2PixelFormat v4l2Format; diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp index f96bf1f..6d558f2 100644 --- a/src/libcamera/formats.cpp +++ b/src/libcamera/formats.cpp @@ -710,4 +710,44 @@ 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 + * \return The number of bytes necessary to store a line, or 0 if the + * PixelFormatInfo instance not valid + */ +unsigned int PixelFormatInfo::stride(unsigned int width, unsigned int plane) const +{ + if (!isValid()) + return 0; + + if (plane > planes.size() || !planes[plane].bytesPerGroup) + return 0; + + /* ceil(width / pixelsPerGroup) * bytesPerGroup */ + return ((width + pixelsPerGroup - 1) / pixelsPerGroup) * planes[plane].bytesPerGroup; +} + +/** + * \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 (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; +} + } /* namespace libcamera */