Loading...
Searching...
No Matches
AVRational
libavutil » Mathematics

Rational number calculation. More...

## Files

file | rational.h
| Utilties for rational number calculation.

## Data Structures

struct | AVRational
| Rational number (pair of numerator and denominator). More...

## Functions

static AVRational | av_make_q (int num, int den)
| Create an AVRational.
static int | av_cmp_q (AVRational a, AVRational b)
| Compare two rationals.
static double | av_q2d (AVRational a)
| Convert an AVRational to a `double`.
int | av_reduce (int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
| Reduce a fraction.
AVRational | av_mul_q (AVRational b, AVRational c) av_const
| Multiply two rationals.
AVRational | av_div_q (AVRational b, AVRational c) av_const
| Divide one rational by another.
AVRational | av_add_q (AVRational b, AVRational c) av_const
| Add two rationals.
AVRational | av_sub_q (AVRational b, AVRational c) av_const
| Subtract one rational from another.
static av_always_inline AVRational | av_inv_q (AVRational q)
| Invert a rational.
AVRational | av_d2q (double d, int max) av_const
| Convert a double precision floating point number to a rational.
int | av_nearer_q (AVRational q, AVRational q1, AVRational q2)
| Find which of the two rationals is closer to another rational.
int | av_find_nearest_q_idx (AVRational q, const AVRational *q_list)
| Find the value in a list of rationals nearest a given reference rational.
uint32_t | av_q2intfloat (AVRational q)
| Convert an AVRational to a IEEE 32-bit `float` expressed in fixed-point format.
AVRational | av_gcd_q (AVRational a, AVRational b, int max_den, AVRational def)
| Return the best rational so that a and b are multiple of it.

## Detailed Description

Rational number calculation.

While rational numbers can be expressed as floating-point numbers, the conversion process is a lossy one, so are floating-point operations. On the other hand, the nature of FFmpeg demands highly accurate calculation of timestamps. This set of rational number utilities serves as a generic interface for manipulating rational numbers as pairs of numerators and denominators.

Many of the functions that operate on AVRational's have the suffix `_q`, in reference to the mathematical symbol "ℚ" (Q) which denotes the set of all rational numbers.

## Function Documentation

## av_make_q()

static AVRational av_make_q | ( | int | num,
|  | int | den
| ) |  |

inline static

Create an AVRational.

Useful for compilers that do not support compound literals.

Note
  The return value is not reduced.

See also
  av_reduce()

Definition at line 71 of file rational.h.

## av_cmp_q()

static int av_cmp_q | ( | AVRational | a,
|  | AVRational | b
| ) |  |

inline static

Compare two rationals.

Parameters

  a | First rational
  b | Second rational

Returns
  One of the following values:
  - 0 if `a == b`
  - 1 if `a > b`
  - -1 if `a < b`
  - `INT_MIN` if one of the values is of the form `0 / 0`

Definition at line 89 of file rational.h.

## av_q2d()

static double av_q2d | ( | AVRational | a | ) |

inline static

Convert an AVRational to a `double`.

Parameters

  a | AVRational to convert

Returns
  `a` in floating-point form

See also
  av_d2q()

Definition at line 104 of file rational.h.

## av_reduce()

int av_reduce | ( | int * | dst_num,
|  | int * | dst_den,
|  | int64_t | num,
|  | int64_t | den,
|  | int64_t | max
| ) |  |

Reduce a fraction.

This is useful for framerate calculations.

Parameters

  [out] | dst_num | Destination numerator
  [out] | dst_den | Destination denominator
  [in] | num | Source numerator
  [in] | den | Source denominator
  [in] | max | Maximum allowed values for `dst_num` & `dst_den`

Returns
  1 if the operation is exact, 0 otherwise

## av_mul_q()

AVRational av_mul_q | ( | AVRational | b,
|  | AVRational | c
| ) |  | const

Multiply two rationals.

Parameters

  b | First rational
  c | Second rational

Returns
  b*c

## av_div_q()

AVRational av_div_q | ( | AVRational | b,
|  | AVRational | c
| ) |  | const

Divide one rational by another.

Parameters

  b | First rational
  c | Second rational

Returns
  b/c

## av_add_q()

AVRational av_add_q | ( | AVRational | b,
|  | AVRational | c
| ) |  | const

Add two rationals.

Parameters

  b | First rational
  c | Second rational

Returns
  b+c

## av_sub_q()

AVRational av_sub_q | ( | AVRational | b,
|  | AVRational | c
| ) |  | const

Subtract one rational from another.

Parameters

  b | First rational
  c | Second rational

Returns
  b-c

## av_inv_q()

static av_always_inline AVRational av_inv_q | ( | AVRational | q | ) |

static

Invert a rational.

Parameters

  q | value

Returns
  1 / q

Examples
  qsv_transcode.c, transcode.c, and vaapi_transcode.c.

Definition at line 159 of file rational.h.

Referenced by dec_enc(), dec_enc(), dynamic_set_parameter(), and open_output_file().

## av_d2q()

AVRational av_d2q | ( | double | d,
|  | int | max
| ) |  | const

Convert a double precision floating point number to a rational.

In case of infinity, the returned value is expressed as `{1, 0}` or `{-1, 0}` depending on the sign.

In general rational numbers with |num| <= 1<<26 && |den| <= 1<<26 can be recovered exactly from their double representation. (no exceptions were found within 1B random ones)

Parameters

  d | `double` to convert
  max | Maximum allowed numerator and denominator

Returns
  `d` in AVRational form

See also
  av_q2d()

Examples
  qsv_transcode.c.

Referenced by dec_enc(), and dynamic_set_parameter().

## av_nearer_q()

int av_nearer_q | ( | AVRational | q,
|  | AVRational | q1,
|  | AVRational | q2
| ) |  |

Find which of the two rationals is closer to another rational.

Parameters

  q | Rational to be compared against
  q1 | Rational to be tested
  q2 | Rational to be tested

Returns
  One of the following values:
  - 1 if `q1` is nearer to `q` than `q2`
  - -1 if `q2` is nearer to `q` than `q1`
  - 0 if they have the same distance

## av_find_nearest_q_idx()

int av_find_nearest_q_idx | ( | AVRational | q,
|  | const AVRational * | q_list
| ) |  |

Find the value in a list of rationals nearest a given reference rational.

Parameters

  q | Reference rational
  q_list | Array of rationals terminated by `{0, 0}`

Returns
  Index of the nearest value found in the array

## av_q2intfloat()

uint32_t av_q2intfloat | ( | AVRational | q | ) |

Convert an AVRational to a IEEE 32-bit `float` expressed in fixed-point format.

Parameters

  q | Rational to be converted

Returns
  Equivalent floating-point value, expressed as an unsigned 32-bit integer.

Note
  The returned value is platform-indepedant.

## av_gcd_q()

AVRational av_gcd_q | ( | AVRational | a,
|  | AVRational | b,
|  | int | max_den,
|  | AVRational | def
| ) |  |

Return the best rational so that a and b are multiple of it.

If the resulting denominator is larger than max_den, return def.