From patchwork Tue Mar 16 15:52:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marian Cichy X-Patchwork-Id: 11594 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 F144ABD80C for ; Tue, 16 Mar 2021 15:52:32 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A947368D55; Tue, 16 Mar 2021 16:52:31 +0100 (CET) Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BF1B068D4A for ; Tue, 16 Mar 2021 16:52:27 +0100 (CET) Received: from dude02.hi.pengutronix.de ([2001:67c:670:100:1d::28]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lMBze-0000V1-TI; Tue, 16 Mar 2021 16:52:26 +0100 Received: from mci by dude02.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1lMBze-0003DS-He; Tue, 16 Mar 2021 16:52:26 +0100 From: Marian Cichy To: libcamera-devel@lists.libcamera.org Date: Tue, 16 Mar 2021 16:52:06 +0100 Message-Id: <20210316155211.6679-2-m.cichy@pengutronix.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210316155211.6679-1-m.cichy@pengutronix.de> References: <20210316155211.6679-1-m.cichy@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::28 X-SA-Exim-Mail-From: mci@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [RFC PATCH 1/6] libcamera: Add fraction.h 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: , Cc: graphics@pengutronix.de, Marian Cichy Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a new class that represents a fraction. A structure like this is also often used in linux camera drivers, e.g. v4l2_fract to represent a frame interval or in applications like Gstreamer to represent frame rates. Adding this class helps to interface frame intervals and frame rates in video streams. Signed-off-by: Marian Cichy --- include/libcamera/fraction.h | 34 ++++++++++++++++++++ src/libcamera/fraction.cpp | 60 ++++++++++++++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 3 files changed, 95 insertions(+) create mode 100644 include/libcamera/fraction.h create mode 100644 src/libcamera/fraction.cpp diff --git a/include/libcamera/fraction.h b/include/libcamera/fraction.h new file mode 100644 index 00000000..aa6a1abb --- /dev/null +++ b/include/libcamera/fraction.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Pengutronix, Marian Cichy + * + * fraction.h - A fraction consisting of two integers + */ + +#ifndef __LIBCAMERA_FRACTION_H__ +#define __LIBCAMERA_FRACTION_H__ + +namespace libcamera { + +class Fraction { +public: + constexpr Fraction() : + numerator(0), denominator(0) + { + } + + constexpr Fraction(unsigned int num, unsigned int den) : + numerator(num), denominator(den) + { + } + + unsigned int numerator; + unsigned int denominator; + + const std::string toString() const; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_FRACTION_H__ */ diff --git a/src/libcamera/fraction.cpp b/src/libcamera/fraction.cpp new file mode 100644 index 00000000..76c373aa --- /dev/null +++ b/src/libcamera/fraction.cpp @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Pengutronix, Marian Cichy + * + * fraction.h - A fraction consisting of two integers + */ + +#include +#include + +#include + +/** + * \file fraction.h + * \brief A fraction consisting of two integers. + */ + +namespace libcamera { + +/** + * \class Fraction + * \brief Represents a fraction with a nominator and a denominator. + */ + +/** + * \fn Fraction::Fraction() + * \brief Construct a Fraction with value 0/0. This should be interpreted + * as invalid or not-used Fraction. + */ + +/** + * \fn Fraction::Fraction(unsigned int num, unsigned int den) + * \brief Construct a Fraction with value n/d. + */ + +/** + * \var Fraction::numerator + * \brief The numerator of the fraction. + */ + +/** + * \var Fraction::denominator + * \brief The denominator of the fraction. + */ + +/** + * \brief Assemble and return a string describing the fraction + * \return A string describing the fraction. + */ +const std::string Fraction::toString() const +{ + std::stringstream ss; + + ss << numerator << "/" << denominator; + + return ss.str(); +} + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 815629db..7927481f 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -22,6 +22,7 @@ libcamera_sources = files([ 'file.cpp', 'file_descriptor.cpp', 'formats.cpp', + 'fraction.cpp', 'framebuffer_allocator.cpp', 'geometry.cpp', 'ipa_controls.cpp',