<div dir="ltr"><div>Hi everyone</div><div><br></div><div>Thanks for this interesting proposal. I don't normally venture too many opinions on stuff that doesn't really affect the Pi, but I did have just a couple of comments on this one, if that's all right!</div><div><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 19 Mar 2021 at 00:15, Laurent Pinchart <<a href="mailto:laurent.pinchart@ideasonboard.com">laurent.pinchart@ideasonboard.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Marian,<br>
<br>
On Wed, Mar 17, 2021 at 06:19:26PM +0100, Marian Cichy wrote:<br>
> On 3/17/21 2:48 PM, Kieran Bingham wrote:<br>
> > On 17/03/2021 12:41, Marian Cichy wrote:<br>
> >> On 3/17/21 12:33 PM, Kieran Bingham wrote:<br>
> >>> On 16/03/2021 15:52, Marian Cichy wrote:<br>
> >>>> Add a new class that represents a fraction. A structure like this is<br>
> >>>> also often used in linux camera drivers, e.g. v4l2_fract to represent a<br>
> >>>> frame interval or in applications like Gstreamer to represent frame<br>
> >>>> rates.<br>
> >>>><br>
> >>>> Adding this class helps to interface frame intervals and frame<br>
> >>>> rates in video streams.<br>
> >>> This indeed would be helpful in any area that handles time.<br>
> >>><br>
> >>> I think this is normally called a rational though?<br></blockquote><div><br></div><div><div>1. I think "fraction" is not an ideal name. The very fact that we've seen some discussion about what, exactly, a "fraction" is kind of demonstrates the problem.</div><div><br></div><div>Personally, I would be inclined to go with "rational". It has a cast iron and straightforward mathematical definition. (Numbers of the form p/q, where p and q are both integers, q != 0).</div><div><br></div><div>2. Following the idea of using rationals, the numerator would need to become signed.</div><div><br></div><div>In particular, this makes rationals closed under the usual arithmetic operations (+ - * /). There may be no plans to overload them, but you could imagine, in a C++ world, someone wanting to do that and finding the lack of negative numbers rather awkward! With rationals, you at least get to do whatever calculation you want, and test if the result is ">= 0" or not at the end.</div><div><br></div><div></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> >> Well .. this is probably mathematical nit-picking, but a fraction is<br>
> >> always a construct that consists of two integers, numerator and<br>
> >> denominator. A rational number is any number that *can* be displayed as<br>
> >> a fraction. For example, "5" is in the set of rational numbers, because<br>
> >> we can display it as the fraction "5/1".<br>
> > Indeed, and wouldn't we use such a representation in the case of long<br>
> > exposures if we use this for frame rates?<br>
> <br>
> But as the frame rate is configurable to be, for example 30 or 29.97, it <br>
> is only sometimes representable by single integers but always by <br>
> fractions. Gstreamer also reports 30/1 for a frame rate of 30.<br>
<br>
This would be true if the frame rate was really 30 or 29.97 fps, but<br>
that's rarely the case in practice with cameras. For instance, there's<br>
an Intel camera HAL implementation that produces ~29.94 fps, and reports<br>
an exact 30 fps to Android as the Android camera framework wants at<br>
least 29.97 fps for the corresponding use case.<br>
<br>
I'm not entirely opposed to using fractions, but we need to consider the<br>
implications carefully, looking at the big picture. It's a non-trivial<br>
topic, and lots of assumptions from the TV world don't apply as-is to<br>
cameras.<br>
<br>
> > Isn't 5/1 the frame rate for a 5 second exposure?<br>
> <br>
> 5/1 is the frame rate of 0.2 seconds exposure<br>
<br>
For 0.2s of frame duration. The exposure time can be (and usually is)<br>
shorter.<br>
<br>
> 5 frames per 1 second<br>
> <br>
> 5/1 is the frame interval of 5 seconds exposure<br>
> <br>
> 5 seconds interval for 1 frame<br>
> <br>
> I know, numbers are confusing :-(<br>
> <br>
> > However I don't think 5/1 could easily be called a fraction...<br>
> ><br>
> > <Ok, after some googling, it looks like they can be called 'improper<br>
> > fractions'><br>
> ><br>
> >> Also, since it is called v4l2_fract in V4L2 and GST_FRACTION in<br>
> >> Gstreamer, I went for consistency in the linux media universe.<br>
> ><br></blockquote><div><br></div><div><div><div>I appreciate that "rationals" don't 100% match the v4l2_fract any more, but the inability to represent some of those rationals where you need full 32-bit positive denominators seems negligible to me. However, I'd be delighted to be educated on that if anyone can think of any examples!</div><div><br></div><div>Thanks and best regards</div><div>David</div></div><div> </div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> > Ah, yes I see that reference later in the series now which I hadn't before.<br>
> ><br>
> >>> We're really building up a math library in here aren't we with our<br>
> >>> sizes, geometry, and number classes...<br>
> >>><br>
> >>>> Signed-off-by: Marian Cichy <<a href="mailto:m.cichy@pengutronix.de" target="_blank">m.cichy@pengutronix.de</a>><br>
> >>>> ---<br>
> >>>> include/libcamera/fraction.h | 34 ++++++++++++++++++++<br>
> >>>> src/libcamera/fraction.cpp | 60 ++++++++++++++++++++++++++++++++++++<br>
> >>>> src/libcamera/meson.build | 1 +<br>
> >>>> 3 files changed, 95 insertions(+)<br>
> >>>> create mode 100644 include/libcamera/fraction.h<br>
> >>>> create mode 100644 src/libcamera/fraction.cpp<br>
> >>>><br>
> >>>> diff --git a/include/libcamera/fraction.h b/include/libcamera/fraction.h<br>
> >>>> new file mode 100644<br>
> >>>> index 00000000..aa6a1abb<br>
> >>>> --- /dev/null<br>
> >>>> +++ b/include/libcamera/fraction.h<br>
> >>>> @@ -0,0 +1,34 @@<br>
> >>>> +/* SPDX-License-Identifier: LGPL-2.1-or-later */<br>
> >>>> +/*<br>
> >>>> + * Copyright (C) 2021, Pengutronix, Marian Cichy<br>
> >>>> <<a href="mailto:entwicklung@pengutronix.de" target="_blank">entwicklung@pengutronix.de</a>><br>
> >>>> + *<br>
> >>>> + * fraction.h - A fraction consisting of two integers<br>
> >>>> + */<br>
> >>>> +<br>
> >>>> +#ifndef __LIBCAMERA_FRACTION_H__<br>
> >>>> +#define __LIBCAMERA_FRACTION_H__<br>
> >>>> +<br>
> >>>> +namespace libcamera {<br>
> >>>> +<br>
> >>>> +class Fraction {<br>
> >>>> +public:<br>
> >>>> + constexpr Fraction() :<br>
> >>>> + numerator(0), denominator(0)<br>
> >>>> + {<br>
> >>>> + }<br>
> >>>> +<br>
> >>>> + constexpr Fraction(unsigned int num, unsigned int den) :<br>
> >>>> + numerator(num), denominator(den)<br>
> >>>> + {<br>
> >>>> + }<br>
> >>>> +<br>
> >>>> + unsigned int numerator;<br>
> >>>> + unsigned int denominator;<br>
> >>> Generically - Fractions/(Rational) numbers could be negative.<br>
> >>> But given our use cases, I presume we wouldn't expect negative values ?<br>
> >>><br>
> >>> Well ... not unless we start needing to do things like clock recovery<br>
> >>> .... I don't think we'll need to do anything like that ...<br>
> >>><br>
> >>> Would you expect further math functions in here to be able to add /<br>
> >>> multiple two fractions/rational numbers together?<br>
> >>><br>
> >>> In fact - I wonder if this would also tie into the geometry functions<br>
> >>> for scaling or such?<br>
> >>><br>
> >>> Size(640, 480) * Rational(1/2) == Size(320, 240);<br>
> >>><br>
> >>> I'm not saying the extra functionality possible here is required for<br>
> >>> this patch though - just exploring what the goals would be for this<br>
> >>> class.<br>
> >>><br>
> >>> -- <br>
> >>> Kieran<br>
> >>><br>
> >>><br>
> >>><br>
> >>>> +<br>
> >>>> + const std::string toString() const;<br>
> >>>> +};<br>
> >>>> +<br>
> >>>> +} /* namespace libcamera */<br>
> >>>> +<br>
> >>>> +#endif /* __LIBCAMERA_FRACTION_H__ */<br>
> >>>> diff --git a/src/libcamera/fraction.cpp b/src/libcamera/fraction.cpp<br>
> >>>> new file mode 100644<br>
> >>>> index 00000000..76c373aa<br>
> >>>> --- /dev/null<br>
> >>>> +++ b/src/libcamera/fraction.cpp<br>
> >>>> @@ -0,0 +1,60 @@<br>
> >>>> +/* SPDX-License-Identifier: LGPL-2.1-or-later */<br>
> >>>> +/*<br>
> >>>> + * Copyright (C) 2021, Pengutronix, Marian Cichy<br>
> >>>> <<a href="mailto:entwicklung@pengutronix.de" target="_blank">entwicklung@pengutronix.de</a>><br>
> >>>> + *<br>
> >>>> + * fraction.h - A fraction consisting of two integers<br>
> >>>> + */<br>
> >>>> +<br>
> >>>> +#include <string><br>
> >>>> +#include <sstream><br>
> >>>> +<br>
> >>>> +#include <libcamera/fraction.h><br>
> >>>> +<br>
> >>>> +/**<br>
> >>>> + * \file fraction.h<br>
> >>>> + * \brief A fraction consisting of two integers.<br>
> >>>> + */<br>
> >>>> +<br>
> >>>> +namespace libcamera {<br>
> >>>> +<br>
> >>>> +/**<br>
> >>>> + * \class Fraction<br>
> >>>> + * \brief Represents a fraction with a nominator and a denominator.<br>
> >>>> + */<br>
> >>>> +<br>
> >>>> +/**<br>
> >>>> + * \fn Fraction::Fraction()<br>
> >>>> + * \brief Construct a Fraction with value 0/0. This should be<br>
> >>>> interpreted<br>
> >>>> + * as invalid or not-used Fraction.<br>
> >>>> + */<br>
> >>>> +<br>
> >>>> +/**<br>
> >>>> + * \fn Fraction::Fraction(unsigned int num, unsigned int den)<br>
> >>>> + * \brief Construct a Fraction with value n/d.<br>
> >>>> + */<br>
> >>>> +<br>
> >>>> +/**<br>
> >>>> + * \var Fraction::numerator<br>
> >>>> + * \brief The numerator of the fraction.<br>
> >>>> + */<br>
> >>>> +<br>
> >>>> +/**<br>
> >>>> + * \var Fraction::denominator<br>
> >>>> + * \brief The denominator of the fraction.<br>
> >>>> + */<br>
> >>>> +<br>
> >>>> +/**<br>
> >>>> + * \brief Assemble and return a string describing the fraction<br>
> >>>> + * \return A string describing the fraction.<br>
> >>>> + */<br>
> >>>> +const std::string Fraction::toString() const<br>
> >>>> +{<br>
> >>>> + std::stringstream ss;<br>
> >>>> +<br>
> >>>> + ss << numerator << "/" << denominator;<br>
> >>>> +<br>
> >>>> + return ss.str();<br>
> >>>> +}<br>
> >>>> +<br>
> >>>> +} /* namespace libcamera */<br>
> >>>> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build<br>
> >>>> index 815629db..7927481f 100644<br>
> >>>> --- a/src/libcamera/meson.build<br>
> >>>> +++ b/src/libcamera/meson.build<br>
> >>>> @@ -22,6 +22,7 @@ libcamera_sources = files([<br>
> >>>> 'file.cpp',<br>
> >>>> 'file_descriptor.cpp',<br>
> >>>> 'formats.cpp',<br>
> >>>> + 'fraction.cpp',<br>
> >>>> 'framebuffer_allocator.cpp',<br>
> >>>> 'geometry.cpp',<br>
> >>>> 'ipa_controls.cpp',<br>
<br>
-- <br>
Regards,<br>
<br>
Laurent Pinchart<br>
_______________________________________________<br>
libcamera-devel mailing list<br>
<a href="mailto:libcamera-devel@lists.libcamera.org" target="_blank">libcamera-devel@lists.libcamera.org</a><br>
<a href="https://lists.libcamera.org/listinfo/libcamera-devel" rel="noreferrer" target="_blank">https://lists.libcamera.org/listinfo/libcamera-devel</a><br>
</blockquote></div></div>