| 1 |
import cython |
| 2 |
from cython.cimports.av.error import err_check |
| 3 |
from cython.cimports.av.opaque import opaque_container |
| 4 |
from cython.cimports.av.utils import avrational_to_fraction, to_avrational |
| 5 |
|
| 6 |
from av.sidedata.sidedata import SideDataContainer |
| 7 |
|
| 8 |
|
| 9 |
@cython.cclass |
| 10 |
class Frame: |
| 11 |
""" |
| 12 |
Base class for audio and video frames. |
| 13 |
|
| 14 |
See also :class:`~av.audio.frame.AudioFrame` and :class:`~av.video.frame.VideoFrame`. |
| 15 |
""" |
| 16 |
|
| 17 |
def __cinit__(self, *args, **kwargs): |
| 18 |
with cython.nogil: |
| 19 |
self.ptr = lib.av_frame_alloc() |
| 20 |
|
| 21 |
def __dealloc__(self): |
| 22 |
with cython.nogil: |
| 23 |
lib.av_frame_free(cython.address(self.ptr)) |
| 24 |
|
| 25 |
def __repr__(self): |
| 26 |
return f"<av.{self.__class__.__name__} pts={self.pts} at 0x{id(self):x}>" |
| 27 |
|
| 28 |
@cython.cfunc |
| 29 |
def _copy_internal_attributes(self, source: Frame, data_layout: cython.bint = True): |
| 30 |
# Mimic another frame |
| 31 |
self._time_base = source._time_base |
| 32 |
lib.av_frame_copy_props(self.ptr, source.ptr) |
| 33 |
if data_layout: |
| 34 |
# TODO: Assert we don't have any data yet. |
| 35 |
self.ptr.format = source.ptr.format |
| 36 |
self.ptr.width = source.ptr.width |
| 37 |
self.ptr.height = source.ptr.height |
| 38 |
self.ptr.ch_layout = source.ptr.ch_layout |
| 39 |
|
| 40 |
@cython.cfunc |
| 41 |
def _init_user_attributes(self): |
| 42 |
pass # Dummy to match the API of the others. |
| 43 |
|
| 44 |
@cython.cfunc |
| 45 |
def _rebase_time(self, dst: lib.AVRational): |
| 46 |
if not dst.num: |
| 47 |
raise ValueError("Cannot rebase to zero time.") |
| 48 |
|
| 49 |
if not self._time_base.num: |
| 50 |
self._time_base = dst |
| 51 |
return |
| 52 |
|
| 53 |
if self._time_base.num == dst.num and self._time_base.den == dst.den: |
| 54 |
return |
| 55 |
|
| 56 |
if self.ptr.pts != lib.AV_NOPTS_VALUE: |
| 57 |
self.ptr.pts = lib.av_rescale_q(self.ptr.pts, self._time_base, dst) |
| 58 |
|
| 59 |
if self.ptr.duration != 0: |
| 60 |
self.ptr.duration = lib.av_rescale_q( |
| 61 |
self.ptr.duration, self._time_base, dst |
| 62 |
) |
| 63 |
|
| 64 |
self._time_base = dst |
| 65 |
|
| 66 |
@property |
| 67 |
def dts(self): |
| 68 |
""" |
| 69 |
The decoding timestamp copied from the :class:`~av.packet.Packet` that triggered returning this frame in :attr:`time_base` units. |
| 70 |
|
| 71 |
(if frame threading isn't used) This is also the Presentation time of this frame calculated from only :attr:`.Packet.dts` values without pts values. |
| 72 |
|
| 73 |
:type: int | None |
| 74 |
""" |
| 75 |
if self.ptr.pkt_dts == lib.AV_NOPTS_VALUE: |
| 76 |
return None |
| 77 |
return self.ptr.pkt_dts |
| 78 |
|
| 79 |
@dts.setter |
| 80 |
def dts(self, value): |
| 81 |
if value is None: |
| 82 |
self.ptr.pkt_dts = lib.AV_NOPTS_VALUE |
| 83 |
else: |
| 84 |
self.ptr.pkt_dts = value |
| 85 |
|
| 86 |
@property |
| 87 |
def pts(self): |
| 88 |
""" |
| 89 |
The presentation timestamp in :attr:`time_base` units for this frame. |
| 90 |
|
| 91 |
This is the time at which the frame should be shown to the user. |
| 92 |
|
| 93 |
:type: int | None |
| 94 |
""" |
| 95 |
if self.ptr.pts == lib.AV_NOPTS_VALUE: |
| 96 |
return None |
| 97 |
return self.ptr.pts |
| 98 |
|
| 99 |
@pts.setter |
| 100 |
def pts(self, value): |
| 101 |
if value is None: |
| 102 |
self.ptr.pts = lib.AV_NOPTS_VALUE |
| 103 |
else: |
| 104 |
self.ptr.pts = value |
| 105 |
|
| 106 |
@property |
| 107 |
def duration(self): |
| 108 |
""" |
| 109 |
The duration of the frame in :attr:`time_base` units |
| 110 |
|
| 111 |
:type: int |
| 112 |
""" |
| 113 |
return self.ptr.duration |
| 114 |
|
| 115 |
@duration.setter |
| 116 |
def duration(self, value): |
| 117 |
self.ptr.duration = value |
| 118 |
|
| 119 |
@property |
| 120 |
@cython.cdivision(True) |
| 121 |
def time(self): |
| 122 |
""" |
| 123 |
The presentation time in seconds for this frame. |
| 124 |
|
| 125 |
This is the time at which the frame should be shown to the user. |
| 126 |
|
| 127 |
:type: float | None |
| 128 |
""" |
| 129 |
if self.ptr.pts == lib.AV_NOPTS_VALUE: |
| 130 |
return None |
| 131 |
return float(self.ptr.pts) * self._time_base.num / self._time_base.den |
| 132 |
|
| 133 |
@property |
| 134 |
def time_base(self): |
| 135 |
""" |
| 136 |
The unit of time (in fractional seconds) in which timestamps are expressed. |
| 137 |
|
| 138 |
:type: fractions.Fraction | None |
| 139 |
""" |
| 140 |
if self._time_base.num: |
| 141 |
return avrational_to_fraction(cython.address(self._time_base)) |
| 142 |
|
| 143 |
@time_base.setter |
| 144 |
def time_base(self, value): |
| 145 |
to_avrational(value, cython.address(self._time_base)) |
| 146 |
|
| 147 |
@property |
| 148 |
def is_corrupt(self): |
| 149 |
""" |
| 150 |
Is this frame corrupt? |
| 151 |
|
| 152 |
:type: bool |
| 153 |
""" |
| 154 |
return self.ptr.decode_error_flags != 0 or bool( |
| 155 |
self.ptr.flags & lib.AV_FRAME_FLAG_CORRUPT |
| 156 |
) |
| 157 |
|
| 158 |
@property |
| 159 |
def key_frame(self): |
| 160 |
"""Is this frame a key frame? |
| 161 |
|
| 162 |
Wraps :ffmpeg:`AVFrame.key_frame`. |
| 163 |
|
| 164 |
""" |
| 165 |
return bool(self.ptr.flags & lib.AV_FRAME_FLAG_KEY) |
| 166 |
|
| 167 |
@key_frame.setter |
| 168 |
def key_frame(self, v): |
| 169 |
# PyAV makes no guarantees this does anything. |
| 170 |
if v: |
| 171 |
self.ptr.flags |= lib.AV_FRAME_FLAG_KEY |
| 172 |
else: |
| 173 |
self.ptr.flags &= ~lib.AV_FRAME_FLAG_KEY |
| 174 |
|
| 175 |
@property |
| 176 |
def side_data(self): |
| 177 |
if self._side_data is None: |
| 178 |
self._side_data = SideDataContainer(self) |
| 179 |
return self._side_data |
| 180 |
|
| 181 |
def make_writable(self): |
| 182 |
""" |
| 183 |
Ensures that the frame data is writable. Copy the data to new buffer if it is not. |
| 184 |
This is a wrapper around :ffmpeg:`av_frame_make_writable`. |
| 185 |
""" |
| 186 |
ret: cython.int = lib.av_frame_make_writable(self.ptr) |
| 187 |
err_check(ret) |
| 188 |
|
| 189 |
@property |
| 190 |
def opaque(self): |
| 191 |
if self.ptr.opaque_ref is not cython.NULL: |
| 192 |
return opaque_container.get( |
| 193 |
cython.cast(cython.p_char, self.ptr.opaque_ref.data) |
| 194 |
) |
| 195 |
|
| 196 |
@opaque.setter |
| 197 |
def opaque(self, v): |
| 198 |
lib.av_buffer_unref(cython.address(self.ptr.opaque_ref)) |
| 199 |
|
| 200 |
if v is not None: |
| 201 |
self.ptr.opaque_ref = opaque_container.add(v) |