[libcamera-devel] [RFC PATCH 1/6] libcamera: Add fraction.h

Marian Cichy m.cichy at pengutronix.de
Tue Mar 16 16:52:06 CET 2021


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 <m.cichy at pengutronix.de>
---
 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 <entwicklung at pengutronix.de>
+ *
+ * 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 <entwicklung at pengutronix.de>
+ *
+ * fraction.h - A fraction consisting of two integers
+ */
+
+#include <string>
+#include <sstream>
+
+#include <libcamera/fraction.h>
+
+/**
+ * \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',
-- 
2.29.2



More information about the libcamera-devel mailing list