avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
av::Rational Class Reference

Rational number: a numerator and a denominator. More...

#include <rational.h>

Public Member Functions

 Rational () noexcept
 Create zero, in the sense of 0/1.
 Rational (int numerator, int denominator=1) noexcept
 Create a rational number.
 Rational (const AVRational &value) noexcept
 Wrap an FFmpeg rational.
 Rational (double value, int maxPrecision=RationalMaxPrecision) noexcept
 Approximate a floating point value as a rational.
AVRational & getValue () noexcept
 Mutable access to the underlying FFmpeg value.
const AVRational & getValue () const noexcept
 The underlying FFmpeg value.
void setValue (const AVRational &newValue) noexcept
 Replace the value.
void setNumerator (int numerator) noexcept
 Set the numerator.
void setDenominator (int denominator) noexcept
 Set the denominator.
int getNumerator () const noexcept
 The numerator.
int getDenominator () const noexcept
 The denominator.
double getDouble () const noexcept
 The value as a floating point number.
int64_t rescale (int64_t srcValue, const Rational &dstBase) const noexcept
 Rescale a value from this base to another.
void dump () const noexcept
 Print the value through the FFmpeg log system.
Rational & operator= (const AVRational &value) noexcept
 Assign an FFmpeg rational.
Rational & operator= (double value) noexcept
 Assign an approximation of a floating point value.
bool operator== (const Rational &other) const noexcept
 Compare two rationals by value, not by representation.
std::strong_ordering operator<=> (const Rational &other) const noexcept
 Order two rationals by value.
Rational operator+ (const Rational &value) const noexcept
 Add another rational.
Rational operator- (const Rational &value) const noexcept
 Subtract another rational.
Rational operator* (const Rational &value) const noexcept
 Multiply by another rational.
Rational operator/ (const Rational &value) const noexcept
 Divide by another rational.
double operator() () const noexcept
 The value as a floating point number.
 operator const AVRational & () const noexcept
 Implicit conversion to the FFmpeg type.

Static Public Member Functions

static Rational fromDouble (double value, int maxPrecision=RationalMaxPrecision) noexcept
 Approximate a floating point value as a rational.

Detailed Description

Widths, heights and frame rates in FFmpeg are rationals, and so are the time bases that give meaning to timestamps. The class keeps the FFmpeg representation and adds comparison and arithmetic that account for different denominators, so 1/25 and 2/50 compare equal.

A denominator of zero is preserved rather than corrected: it is what FFmpeg uses for "unknown", and isValid() reports it.

Examples
api2-decode-encode-audio.cpp, api2-decode-encode-video.cpp, api2-decode-filter-encode.cpp, api2-decode-overlay-encode.cpp, api2-generate-subtitles.cpp, api2-hw-encode.cpp, api2-hw-scale.cpp, api2-hw-transcode.cpp, and api2-scale-video.cpp.

Constructor & Destructor Documentation

◆ Rational() [1/3]

av::Rational::Rational ( int numerator,
int denominator = 1 )
noexcept
Parameters
numeratorthe numerator
denominatorthe denominator; zero means "unknown"

◆ Rational() [2/3]

av::Rational::Rational ( const AVRational & value)
noexcept
Parameters
valuethe value to copy

◆ Rational() [3/3]

av::Rational::Rational ( double value,
int maxPrecision = RationalMaxPrecision )
noexcept
Parameters
valuethe number to approximate
maxPrecisionmaximum denominator to try, in decimal digits

Member Function Documentation

◆ fromDouble()

Rational av::Rational::fromDouble ( double value,
int maxPrecision = RationalMaxPrecision )
staticnoexcept
Parameters
valuethe number to approximate
maxPrecisionmaximum denominator to try, in decimal digits
Returns
the closest rational within the precision limit

◆ getDenominator()

int av::Rational::getDenominator ( ) const
inlinenoexcept
Returns
the stored denominator
Examples
api2-generate-subtitles.cpp.

◆ getDouble()

double av::Rational::getDouble ( ) const
inlinenoexcept
Returns
numerator divided by denominator; infinity for a zero denominator

◆ getNumerator()

int av::Rational::getNumerator ( ) const
inlinenoexcept
Returns
the stored numerator
Examples
api2-generate-subtitles.cpp.

◆ getValue() [1/2]

const AVRational & av::Rational::getValue ( ) const
inlinenoexcept
Returns
a reference to the stored rational

◆ getValue() [2/2]

AVRational & av::Rational::getValue ( )
inlinenoexcept
Returns
a reference to the stored rational

◆ operator const AVRational &()

av::Rational::operator const AVRational & ( ) const
inlinenoexcept
Returns
a reference to the stored value, so it can be passed to FFmpeg

◆ operator()()

double av::Rational::operator() ( ) const
noexcept
Returns
numerator divided by denominator

◆ operator*()

Rational av::Rational::operator* ( const Rational & value) const
noexcept
Parameters
valuethe factor
Returns
the product

◆ operator+()

Rational av::Rational::operator+ ( const Rational & value) const
noexcept
Parameters
valuethe value to add
Returns
the sum, reduced to a common denominator

◆ operator-()

Rational av::Rational::operator- ( const Rational & value) const
noexcept
Parameters
valuethe value to subtract
Returns
the difference

◆ operator/()

Rational av::Rational::operator/ ( const Rational & value) const
noexcept
Parameters
valuethe divisor; a zero rational produces an invalid result
Returns
the quotient

◆ operator<=>()

std::strong_ordering av::Rational::operator<=> ( const Rational & other) const
inlinenoexcept
Parameters
otherthe rational to compare with
Returns
the three-way comparison of the values

◆ operator=() [1/2]

Rational & av::Rational::operator= ( const AVRational & value)
noexcept
Parameters
valuethe value to store
Returns
this rational

◆ operator=() [2/2]

Rational & av::Rational::operator= ( double value)
noexcept
Parameters
valuethe number to approximate
Returns
this rational

◆ operator==()

bool av::Rational::operator== ( const Rational & other) const
noexcept
Parameters
otherthe rational to compare with
Returns
true when the values are equal, so 1/25 equals 2/50

◆ rescale()

int64_t av::Rational::rescale ( int64_t srcValue,
const Rational & dstBase ) const
noexcept
Parameters
srcValuethe value in this time base
dstBasethe time base to convert to
Returns
the value in dstBase, rounded the way FFmpeg rounds

◆ setDenominator()

void av::Rational::setDenominator ( int denominator)
inlinenoexcept
Parameters
denominatorthe new denominator; zero means "unknown"

◆ setNumerator()

void av::Rational::setNumerator ( int numerator)
inlinenoexcept
Parameters
numeratorthe new numerator

◆ setValue()

void av::Rational::setValue ( const AVRational & newValue)
inlinenoexcept
Parameters
newValuethe rational to store

The documentation for this class was generated from the following file: