MythTV master
mythavrational.h
Go to the documentation of this file.
1#ifndef MYTH_AV_RATIONAL_H
2#define MYTH_AV_RATIONAL_H
3
4extern "C"
5{
6#include "libavutil/rational.h"
7}
8
9#include <QString>
10
15{
16 public:
17 explicit MythAVRational(int n = 0, int d = 1) : m_q({n, d}) {}
18 explicit MythAVRational(AVRational q) : m_q(q) {}
25 static MythAVRational fromDouble(double d, int64_t max)
26 {
27 return MythAVRational(av_d2q(d, max));
28 }
29
30 AVRational get_q() const { return m_q; }
32 long long toFixed(long long base) const
33 {
34 return (base * m_q.num) / m_q.den;
35 }
36 double toDouble() const { return av_q2d(m_q); }
37 QString toString() const
38 {
39 return QStringLiteral("%1/%2").arg(QString::number(m_q.num), QString::number(m_q.den));
40 }
41
42 bool isNonzero() const { return m_q.num != 0; }
44 bool isValid() const { return m_q.den != 0; }
45
46 [[nodiscard]] MythAVRational reduce(int64_t max) const
47 {
48 bool ignore {false};
49 return reduce(max, ignore);
50 }
51 [[nodiscard]] MythAVRational reduce(int64_t max, bool& exact) const
52 {
54 exact = av_reduce(&q.m_q.num, &q.m_q.den, m_q.num, m_q.den, max);
55 return q;
56 }
57 [[nodiscard]] MythAVRational invert() const { return MythAVRational(av_inv_q(m_q)); }
58
59 static int cmp(MythAVRational a, MythAVRational b) { return av_cmp_q(a.m_q, b.m_q); }
60
65 private:
66 AVRational m_q {.num = 0, .den = 1};
67};
68
69inline bool operator==(MythAVRational lhs, MythAVRational rhs) { return MythAVRational::cmp(lhs, rhs) == 0; }
70inline bool operator!=(MythAVRational lhs, MythAVRational rhs) { return MythAVRational::cmp(lhs, rhs) != 0; }
71inline bool operator< (MythAVRational lhs, MythAVRational rhs) { return MythAVRational::cmp(lhs, rhs) < 0; }
72inline bool operator> (MythAVRational lhs, MythAVRational rhs) { return MythAVRational::cmp(lhs, rhs) > 0; }
73inline bool operator<=(MythAVRational lhs, MythAVRational rhs) { return MythAVRational::cmp(lhs, rhs) <= 0; }
74inline bool operator>=(MythAVRational lhs, MythAVRational rhs) { return MythAVRational::cmp(lhs, rhs) >= 0; }
75
77{
78 return MythAVRational(av_add_q(lhs.get_q(), rhs.get_q()));
79}
80
82{
83 return MythAVRational(av_sub_q(lhs.get_q(), rhs.get_q()));
84}
85
87{
88 return MythAVRational(av_mul_q(lhs.get_q(), rhs.get_q()));
89}
90
92{
93 return MythAVRational(av_div_q(lhs.get_q(), rhs.get_q()));
94}
95
96inline MythAVRational& MythAVRational::operator+=(MythAVRational rhs) { return *this = operator+(*this, rhs); }
97inline MythAVRational& MythAVRational::operator-=(MythAVRational rhs) { return *this = operator-(*this, rhs); }
98inline MythAVRational& MythAVRational::operator*=(MythAVRational rhs) { return *this = operator*(*this, rhs); }
99inline MythAVRational& MythAVRational::operator/=(MythAVRational rhs) { return *this = operator/(*this, rhs); }
100#endif // MYTH_AV_RATIONAL_H
C++ wrapper for FFmpeg libavutil AVRational.
MythAVRational reduce(int64_t max) const
static int cmp(MythAVRational a, MythAVRational b)
long long toFixed(long long base) const
Convert the rational number to fixed point.
bool isValid() const
double toDouble() const
static MythAVRational fromDouble(double d, int64_t max)
Convert a double to a MythAVRational.
AVRational get_q() const
MythAVRational & operator/=(MythAVRational rhs)
bool isNonzero() const
MythAVRational & operator-=(MythAVRational rhs)
QString toString() const
MythAVRational reduce(int64_t max, bool &exact) const
MythAVRational & operator*=(MythAVRational rhs)
MythAVRational(int n=0, int d=1)
AVRational m_q
MythAVRational(AVRational q)
MythAVRational & operator+=(MythAVRational rhs)
MythAVRational invert() const
static const iso6937table * d
bool operator<(MythAVRational lhs, MythAVRational rhs)
bool operator==(MythAVRational lhs, MythAVRational rhs)
MythAVRational operator/(MythAVRational lhs, MythAVRational rhs)
bool operator<=(MythAVRational lhs, MythAVRational rhs)
MythAVRational operator+(MythAVRational lhs, MythAVRational rhs)
bool operator>(MythAVRational lhs, MythAVRational rhs)
MythAVRational operator*(MythAVRational lhs, MythAVRational rhs)
bool operator!=(MythAVRational lhs, MythAVRational rhs)
MythAVRational operator-(MythAVRational lhs, MythAVRational rhs)
bool operator>=(MythAVRational lhs, MythAVRational rhs)