|
| 1 | +# type: ignore |
| 2 | +from fractions import Fraction |
| 3 | +from numbers import Rational |
| 4 | + |
| 5 | +import cython |
| 6 | +from cython.cimports import libav as lib |
| 7 | + |
| 8 | +_INT32_MAX: cython.longlong = 2147483647 |
| 9 | + |
| 10 | + |
| 11 | +@cython.cclass |
| 12 | +class AVRational: |
| 13 | + """ |
| 14 | + An exact rational number stored as two int32s, mirroring FFmpeg's |
| 15 | + ``AVRational``. |
| 16 | +
|
| 17 | + Values are always reduced to lowest terms with a positive denominator. |
| 18 | + Arithmetic between two :class:`AVRational` uses FFmpeg's ``av_mul_q`` |
| 19 | + family: intermediates are computed in int64, then reduced back to int32, |
| 20 | + **approximating** the result if it does not fit. Arithmetic with other |
| 21 | + numeric types promotes to :class:`fractions.Fraction` (exact). |
| 22 | +
|
| 23 | + Following FFmpeg, a zero denominator is allowed: ``1/0``, ``-1/0`` |
| 24 | + (infinities) and ``0/0`` (undefined) exist and, like the unset value |
| 25 | + ``AVRational(0, 1)``, are falsy — so ``if rate:`` covers every |
| 26 | + not-a-real-value case that used to be ``None``. |
| 27 | +
|
| 28 | + Every PyAV setter that accepts a :class:`fractions.Fraction` (e.g. |
| 29 | + ``stream.time_base``, ``codec_context.framerate``) also accepts an |
| 30 | + :class:`AVRational`. |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self, num=0, den=1): |
| 34 | + if den == 1 and isinstance(num, Rational): |
| 35 | + num, den = num.numerator, num.denominator |
| 36 | + n64: cython.longlong = num |
| 37 | + d64: cython.longlong = den |
| 38 | + n: cython.int |
| 39 | + d: cython.int |
| 40 | + if not lib.av_reduce( |
| 41 | + cython.address(n), cython.address(d), n64, d64, _INT32_MAX |
| 42 | + ): |
| 43 | + raise OverflowError(f"{num}/{den} cannot be reduced to fit in int32") |
| 44 | + self.num = n |
| 45 | + self.den = d |
| 46 | + |
| 47 | + @cython.cfunc |
| 48 | + def _q(self) -> lib.AVRational: |
| 49 | + q: lib.AVRational |
| 50 | + q.num = self.num |
| 51 | + q.den = self.den |
| 52 | + return q |
| 53 | + |
| 54 | + @property |
| 55 | + def numerator(self): |
| 56 | + return self.num |
| 57 | + |
| 58 | + @property |
| 59 | + def denominator(self): |
| 60 | + return self.den |
| 61 | + |
| 62 | + def __repr__(self): |
| 63 | + return f"AVRational({self.num}, {self.den})" |
| 64 | + |
| 65 | + def __str__(self): |
| 66 | + return f"{self.num}/{self.den}" |
| 67 | + |
| 68 | + def __bool__(self): |
| 69 | + return self.num != 0 and self.den != 0 |
| 70 | + |
| 71 | + def __float__(self): |
| 72 | + if self.den == 0: |
| 73 | + return float("nan") if self.num == 0 else self.num * float("inf") |
| 74 | + return self.num / self.den |
| 75 | + |
| 76 | + def __hash__(self): |
| 77 | + if self.den == 0: |
| 78 | + return hash((self.num, 0)) |
| 79 | + return hash(Fraction(self.num, self.den)) |
| 80 | + |
| 81 | + def __reduce__(self): |
| 82 | + return (AVRational, (self.num, self.den)) |
| 83 | + |
| 84 | + def __eq__(self, other): |
| 85 | + if isinstance(other, AVRational): |
| 86 | + o: AVRational = other |
| 87 | + return self.num == o.num and self.den == o.den |
| 88 | + if self.den == 0: |
| 89 | + return False |
| 90 | + return Fraction(self.num, self.den) == other |
| 91 | + |
| 92 | + def __lt__(self, other): |
| 93 | + return Fraction(self.num, self.den) < other |
| 94 | + |
| 95 | + def __le__(self, other): |
| 96 | + return Fraction(self.num, self.den) <= other |
| 97 | + |
| 98 | + def __gt__(self, other): |
| 99 | + return Fraction(self.num, self.den) > other |
| 100 | + |
| 101 | + def __ge__(self, other): |
| 102 | + return Fraction(self.num, self.den) >= other |
| 103 | + |
| 104 | + def __neg__(self): |
| 105 | + return AVRational(-self.num, self.den) |
| 106 | + |
| 107 | + def __mul__(self, other): |
| 108 | + if isinstance(other, AVRational): |
| 109 | + o: AVRational = other |
| 110 | + return from_avrational(lib.av_mul_q(self._q(), o._q())) |
| 111 | + return Fraction(self.num, self.den) * other |
| 112 | + |
| 113 | + def __rmul__(self, other): |
| 114 | + return other * Fraction(self.num, self.den) |
| 115 | + |
| 116 | + def __truediv__(self, other): |
| 117 | + if isinstance(other, AVRational): |
| 118 | + o: AVRational = other |
| 119 | + if o.num == 0: |
| 120 | + raise ZeroDivisionError(f"{self} / {other}") |
| 121 | + return from_avrational(lib.av_div_q(self._q(), o._q())) |
| 122 | + return Fraction(self.num, self.den) / other |
| 123 | + |
| 124 | + def __rtruediv__(self, other): |
| 125 | + return other / Fraction(self.num, self.den) |
| 126 | + |
| 127 | + def __add__(self, other): |
| 128 | + if isinstance(other, AVRational): |
| 129 | + o: AVRational = other |
| 130 | + return from_avrational(lib.av_add_q(self._q(), o._q())) |
| 131 | + return Fraction(self.num, self.den) + other |
| 132 | + |
| 133 | + def __radd__(self, other): |
| 134 | + return other + Fraction(self.num, self.den) |
| 135 | + |
| 136 | + def __sub__(self, other): |
| 137 | + if isinstance(other, AVRational): |
| 138 | + o: AVRational = other |
| 139 | + return from_avrational(lib.av_sub_q(self._q(), o._q())) |
| 140 | + return Fraction(self.num, self.den) - other |
| 141 | + |
| 142 | + def __rsub__(self, other): |
| 143 | + return other - Fraction(self.num, self.den) |
| 144 | + |
| 145 | + |
| 146 | +@cython.cfunc |
| 147 | +def from_avrational(q: lib.AVRational) -> AVRational: |
| 148 | + obj: AVRational = AVRational.__new__(AVRational) |
| 149 | + # FFmpeg does not guarantee reduced form; our invariant requires it. |
| 150 | + lib.av_reduce( |
| 151 | + cython.address(obj.num), cython.address(obj.den), q.num, q.den, _INT32_MAX |
| 152 | + ) |
| 153 | + return obj |
| 154 | + |
| 155 | + |
| 156 | +Rational.register(AVRational) |
0 commit comments