| 1 |
from typing import Iterator, Literal, get_args |
| 2 |
|
| 3 |
import cython |
| 4 |
from cython.cimports import libav as lib |
| 5 |
from cython.cimports.av.buffer import Buffer, ByteSource, bytesource |
| 6 |
from cython.cimports.av.error import err_check |
| 7 |
from cython.cimports.av.opaque import opaque_container |
| 8 |
from cython.cimports.av.utils import avrational_to_fraction, to_avrational |
| 9 |
from cython.cimports.cpython.ref import Py_DECREF, Py_INCREF |
| 10 |
from cython.cimports.libc.stdint import uint8_t |
| 11 |
from cython.cimports.libc.string import memcpy |
| 12 |
|
| 13 |
# Check https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/packet.h#L41 |
| 14 |
# for new additions in the future ffmpeg releases |
| 15 |
# Note: the order must follow that of the AVPacketSideDataType enum def |
| 16 |
PktSideDataT = Literal[ |
| 17 |
"palette", |
| 18 |
"new_extradata", |
| 19 |
"param_change", |
| 20 |
"h263_mb_info", |
| 21 |
"replay_gain", |
| 22 |
"display_matrix", |
| 23 |
"stereo_3d", |
| 24 |
"audio_service_type", |
| 25 |
"quality_stats", |
| 26 |
"fallback_track", |
| 27 |
"cpb_properties", |
| 28 |
"skip_samples", |
| 29 |
"jp_dual_mono", |
| 30 |
"strings_metadata", |
| 31 |
"subtitle_position", |
| 32 |
"matroska_block_additional", |
| 33 |
"webvtt_identifier", |
| 34 |
"webvtt_settings", |
| 35 |
"metadata_update", |
| 36 |
"mpegts_stream_id", |
| 37 |
"mastering_display_metadata", |
| 38 |
"spherical", |
| 39 |
"content_light_level", |
| 40 |
"a53_cc", |
| 41 |
"encryption_init_info", |
| 42 |
"encryption_info", |
| 43 |
"afd", |
| 44 |
"prft", |
| 45 |
"icc_profile", |
| 46 |
"dovi_conf", |
| 47 |
"s12m_timecode", |
| 48 |
"dynamic_hdr10_plus", |
| 49 |
"iamf_mix_gain_param", |
| 50 |
"iamf_info_param", |
| 51 |
"iamf_recon_gain_info_param", |
| 52 |
"ambient_viewing_environment", |
| 53 |
"frame_cropping", |
| 54 |
"lcevc", |
| 55 |
"3d_reference_displays", |
| 56 |
"rtcp_sr", |
| 57 |
] |
| 58 |
|
| 59 |
|
| 60 |
def packet_sidedata_type_to_literal(dtype: lib.AVPacketSideDataType) -> PktSideDataT: |
| 61 |
return get_args(PktSideDataT)[cython.cast(int, dtype)] |
| 62 |
|
| 63 |
|
| 64 |
def packet_sidedata_type_from_literal(dtype: PktSideDataT) -> lib.AVPacketSideDataType: |
| 65 |
return get_args(PktSideDataT).index(dtype) |
| 66 |
|
| 67 |
|
| 68 |
@cython.final |
| 69 |
@cython.cclass |
| 70 |
class PacketSideData(Buffer): |
| 71 |
@staticmethod |
| 72 |
def from_packet(packet: Packet, data_type: PktSideDataT) -> PacketSideData: |
| 73 |
"""create new PacketSideData by copying an existing packet's side data |
| 74 |
|
| 75 |
:param packet: Source packet |
| 76 |
:type packet: :class:`~av.packet.Packet` |
| 77 |
:param data_type: side data type |
| 78 |
:return: newly created copy of the side data if the side data of the |
| 79 |
requested type is found in the packet, else an empty object |
| 80 |
:rtype: :class:`~av.packet.PacketSideData` |
| 81 |
""" |
| 82 |
|
| 83 |
dtype = packet_sidedata_type_from_literal(data_type) |
| 84 |
return _packet_sidedata_from_packet(packet.ptr, dtype) |
| 85 |
|
| 86 |
def __cinit__(self, dtype: lib.AVPacketSideDataType, size: cython.size_t): |
| 87 |
self.dtype = dtype |
| 88 |
with cython.nogil: |
| 89 |
if size: |
| 90 |
self.data = cython.cast(cython.p_uchar, lib.av_malloc(size)) |
| 91 |
if self.data == cython.NULL: |
| 92 |
raise MemoryError("Failed to allocate memory") |
| 93 |
else: |
| 94 |
self.data = cython.NULL |
| 95 |
self.size = size |
| 96 |
|
| 97 |
def __dealloc__(self): |
| 98 |
with cython.nogil: |
| 99 |
lib.av_freep(cython.address(self.data)) |
| 100 |
|
| 101 |
def to_packet(self, packet: Packet, move: cython.bint = False): |
| 102 |
"""copy or move side data to the specified packet |
| 103 |
|
| 104 |
:param packet: Target packet |
| 105 |
:type packet: :class:`~av.packet.Packet` |
| 106 |
:param move: True to move the data from this object to the packet, |
| 107 |
defaults to False. |
| 108 |
:type move: bool |
| 109 |
""" |
| 110 |
if self.size == 0: |
| 111 |
# nothing to add, should clear existing side_data in packet? |
| 112 |
return |
| 113 |
|
| 114 |
data = self.data |
| 115 |
|
| 116 |
with cython.nogil: |
| 117 |
if not move: |
| 118 |
data = cython.cast(cython.p_uchar, lib.av_malloc(self.size)) |
| 119 |
if data == cython.NULL: |
| 120 |
raise MemoryError("Failed to allocate memory") |
| 121 |
memcpy(data, self.data, self.size) |
| 122 |
|
| 123 |
res = lib.av_packet_add_side_data(packet.ptr, self.dtype, data, self.size) |
| 124 |
err_check(res) |
| 125 |
|
| 126 |
if move: |
| 127 |
self.data = cython.NULL |
| 128 |
self.size = 0 |
| 129 |
|
| 130 |
@property |
| 131 |
def data_type(self) -> str: |
| 132 |
""" |
| 133 |
The type of this packet side data. |
| 134 |
|
| 135 |
:type: str |
| 136 |
""" |
| 137 |
return packet_sidedata_type_to_literal(self.dtype) |
| 138 |
|
| 139 |
@property |
| 140 |
def data_desc(self) -> str: |
| 141 |
""" |
| 142 |
The description of this packet side data type. |
| 143 |
|
| 144 |
:type: str |
| 145 |
""" |
| 146 |
|
| 147 |
return lib.av_packet_side_data_name(self.dtype) |
| 148 |
|
| 149 |
@property |
| 150 |
def data_size(self) -> int: |
| 151 |
""" |
| 152 |
The size in bytes of this packet side data. |
| 153 |
|
| 154 |
:type: int |
| 155 |
""" |
| 156 |
return self.size |
| 157 |
|
| 158 |
# Buffer protocol implementation |
| 159 |
@cython.cfunc |
| 160 |
def _buffer_size(self) -> cython.size_t: |
| 161 |
return self.size |
| 162 |
|
| 163 |
@cython.cfunc |
| 164 |
def _buffer_ptr(self) -> cython.p_void: |
| 165 |
return self.data |
| 166 |
|
| 167 |
@cython.cfunc |
| 168 |
def _buffer_writable(self) -> cython.bint: |
| 169 |
return True |
| 170 |
|
| 171 |
def __bool__(self) -> bool: |
| 172 |
""" |
| 173 |
True if this object holds side data. |
| 174 |
|
| 175 |
:type: bool |
| 176 |
""" |
| 177 |
return self.data != cython.NULL |
| 178 |
|
| 179 |
|
| 180 |
@cython.cfunc |
| 181 |
def _packet_sidedata_from_packet( |
| 182 |
packet: cython.pointer[lib.AVPacket], dtype: lib.AVPacketSideDataType |
| 183 |
) -> PacketSideData: |
| 184 |
with cython.nogil: |
| 185 |
c_ptr = lib.av_packet_side_data_get( |
| 186 |
packet.side_data, packet.side_data_elems, dtype |
| 187 |
) |
| 188 |
found: cython.bint = c_ptr != cython.NULL |
| 189 |
|
| 190 |
sdata = PacketSideData(dtype, c_ptr.size if found else 0) |
| 191 |
|
| 192 |
with cython.nogil: |
| 193 |
if found: |
| 194 |
memcpy(sdata.data, c_ptr.data, c_ptr.size) |
| 195 |
|
| 196 |
return sdata |
| 197 |
|
| 198 |
|
| 199 |
@cython.cfunc |
| 200 |
@cython.nogil |
| 201 |
@cython.exceptval(check=False) |
| 202 |
def _python_free( |
| 203 |
opaque: cython.p_void, |
| 204 |
data: cython.pointer[uint8_t], |
| 205 |
) -> cython.void: |
| 206 |
if opaque != cython.NULL: |
| 207 |
with cython.gil: |
| 208 |
Py_DECREF(cython.cast(object, opaque)) |
| 209 |
|
| 210 |
|
| 211 |
@cython.final |
| 212 |
@cython.cclass |
| 213 |
class Packet(Buffer): |
| 214 |
"""A packet of encoded data within a :class:`~av.format.Stream`. |
| 215 |
|
| 216 |
This may, or may not include a complete object within a stream. |
| 217 |
:meth:`decode` must be called to extract encoded data. |
| 218 |
""" |
| 219 |
|
| 220 |
def __cinit__(self, input=None): |
| 221 |
with cython.nogil: |
| 222 |
self.ptr = lib.av_packet_alloc() |
| 223 |
|
| 224 |
def __dealloc__(self): |
| 225 |
with cython.nogil: |
| 226 |
lib.av_packet_free(cython.address(self.ptr)) |
| 227 |
|
| 228 |
def __init__(self, input=None): |
| 229 |
size: cython.size_t = 0 |
| 230 |
source: ByteSource = None |
| 231 |
buf: cython.pointer[lib.AVBufferRef] |
| 232 |
|
| 233 |
if input is None: |
| 234 |
return |
| 235 |
|
| 236 |
if isinstance(input, int): |
| 237 |
size = input |
| 238 |
if size: |
| 239 |
err_check(lib.av_new_packet(self.ptr, size)) |
| 240 |
else: |
| 241 |
source = bytesource(input) |
| 242 |
size = source.length |
| 243 |
if size: |
| 244 |
# Create a buffer that references the source data directly. |
| 245 |
Py_INCREF(source) |
| 246 |
buf = lib.av_buffer_create( |
| 247 |
source.ptr, |
| 248 |
size, |
| 249 |
_python_free, |
| 250 |
cython.cast(cython.p_void, source), |
| 251 |
0, |
| 252 |
) |
| 253 |
if buf == cython.NULL: |
| 254 |
Py_DECREF(source) |
| 255 |
raise MemoryError("Could not allocate AVBufferRef") |
| 256 |
self.ptr.buf = buf |
| 257 |
self.ptr.data = source.ptr |
| 258 |
self.ptr.size = size |
| 259 |
|
| 260 |
def __repr__(self): |
| 261 |
stream = self._stream.index if self._stream else 0 |
| 262 |
return ( |
| 263 |
f"av.{self.__class__.__name__} of #{stream}, dts={self.dts}," |
| 264 |
f" pts={self.pts}; {self.ptr.size} bytes at 0x{id(self):x}>" |
| 265 |
) |
| 266 |
|
| 267 |
# Buffer protocol. |
| 268 |
@cython.cfunc |
| 269 |
def _buffer_size(self) -> cython.size_t: |
| 270 |
return self.ptr.size |
| 271 |
|
| 272 |
@cython.cfunc |
| 273 |
def _buffer_ptr(self) -> cython.p_void: |
| 274 |
return self.ptr.data |
| 275 |
|
| 276 |
@cython.cfunc |
| 277 |
def _rebase_time(self, dst: lib.AVRational): |
| 278 |
if not dst.num: |
| 279 |
raise ValueError("Cannot rebase to zero time.") |
| 280 |
|
| 281 |
if not self.ptr.time_base.num: |
| 282 |
self.ptr.time_base = dst |
| 283 |
return |
| 284 |
|
| 285 |
if self.ptr.time_base.num == dst.num and self.ptr.time_base.den == dst.den: |
| 286 |
return |
| 287 |
|
| 288 |
lib.av_packet_rescale_ts(self.ptr, self.ptr.time_base, dst) |
| 289 |
self.ptr.time_base = dst |
| 290 |
|
| 291 |
def decode(self): |
| 292 |
""" |
| 293 |
Send the packet's data to the decoder and return a list of |
| 294 |
:class:`.AudioFrame`, :class:`.VideoFrame` or :class:`.SubtitleSet`. |
| 295 |
""" |
| 296 |
return self._stream.decode(self) |
| 297 |
|
| 298 |
@property |
| 299 |
def stream_index(self): |
| 300 |
return self.ptr.stream_index |
| 301 |
|
| 302 |
@property |
| 303 |
def stream(self): |
| 304 |
""" |
| 305 |
The :class:`Stream` this packet was demuxed from. |
| 306 |
""" |
| 307 |
return self._stream |
| 308 |
|
| 309 |
@stream.setter |
| 310 |
def stream(self, stream: Stream): |
| 311 |
self._stream = stream |
| 312 |
self.ptr.stream_index = stream.ptr.index |
| 313 |
|
| 314 |
@property |
| 315 |
def time_base(self): |
| 316 |
""" |
| 317 |
The unit of time (in fractional seconds) in which timestamps are expressed. |
| 318 |
|
| 319 |
:type: fractions.Fraction |
| 320 |
""" |
| 321 |
return avrational_to_fraction(cython.address(self.ptr.time_base)) |
| 322 |
|
| 323 |
@time_base.setter |
| 324 |
def time_base(self, value): |
| 325 |
to_avrational(value, cython.address(self.ptr.time_base)) |
| 326 |
|
| 327 |
@property |
| 328 |
def pts(self): |
| 329 |
""" |
| 330 |
The presentation timestamp in :attr:`time_base` units for this packet. |
| 331 |
|
| 332 |
This is the time at which the packet should be shown to the user. |
| 333 |
|
| 334 |
:type: int | None |
| 335 |
""" |
| 336 |
if self.ptr.pts != lib.AV_NOPTS_VALUE: |
| 337 |
return self.ptr.pts |
| 338 |
|
| 339 |
@pts.setter |
| 340 |
def pts(self, v): |
| 341 |
if v is None: |
| 342 |
self.ptr.pts = lib.AV_NOPTS_VALUE |
| 343 |
else: |
| 344 |
self.ptr.pts = v |
| 345 |
|
| 346 |
@property |
| 347 |
def dts(self): |
| 348 |
""" |
| 349 |
The decoding timestamp in :attr:`time_base` units for this packet. |
| 350 |
|
| 351 |
:type: int | None |
| 352 |
""" |
| 353 |
if self.ptr.dts != lib.AV_NOPTS_VALUE: |
| 354 |
return self.ptr.dts |
| 355 |
|
| 356 |
@dts.setter |
| 357 |
def dts(self, v): |
| 358 |
if v is None: |
| 359 |
self.ptr.dts = lib.AV_NOPTS_VALUE |
| 360 |
else: |
| 361 |
self.ptr.dts = v |
| 362 |
|
| 363 |
@property |
| 364 |
def pos(self): |
| 365 |
""" |
| 366 |
The byte position of this packet within the :class:`.Stream`. |
| 367 |
|
| 368 |
Returns `None` if it is not known. |
| 369 |
|
| 370 |
:type: int | None |
| 371 |
""" |
| 372 |
if self.ptr.pos != -1: |
| 373 |
return self.ptr.pos |
| 374 |
|
| 375 |
@property |
| 376 |
def size(self): |
| 377 |
""" |
| 378 |
The size in bytes of this packet's data. |
| 379 |
|
| 380 |
:type: int |
| 381 |
""" |
| 382 |
return self.ptr.size |
| 383 |
|
| 384 |
@property |
| 385 |
def duration(self): |
| 386 |
""" |
| 387 |
The duration in :attr:`time_base` units for this packet. |
| 388 |
|
| 389 |
Returns `None` if it is not known. |
| 390 |
|
| 391 |
:type: int |
| 392 |
""" |
| 393 |
if self.ptr.duration != lib.AV_NOPTS_VALUE: |
| 394 |
return self.ptr.duration |
| 395 |
|
| 396 |
@duration.setter |
| 397 |
def duration(self, v): |
| 398 |
if v is None: |
| 399 |
self.ptr.duration = lib.AV_NOPTS_VALUE |
| 400 |
else: |
| 401 |
self.ptr.duration = v |
| 402 |
|
| 403 |
@property |
| 404 |
def is_keyframe(self): |
| 405 |
return bool(self.ptr.flags & lib.AV_PKT_FLAG_KEY) |
| 406 |
|
| 407 |
@is_keyframe.setter |
| 408 |
def is_keyframe(self, v): |
| 409 |
if v: |
| 410 |
self.ptr.flags |= lib.AV_PKT_FLAG_KEY |
| 411 |
else: |
| 412 |
self.ptr.flags &= ~(lib.AV_PKT_FLAG_KEY) |
| 413 |
|
| 414 |
@property |
| 415 |
def is_corrupt(self): |
| 416 |
return bool(self.ptr.flags & lib.AV_PKT_FLAG_CORRUPT) |
| 417 |
|
| 418 |
@is_corrupt.setter |
| 419 |
def is_corrupt(self, v): |
| 420 |
if v: |
| 421 |
self.ptr.flags |= lib.AV_PKT_FLAG_CORRUPT |
| 422 |
else: |
| 423 |
self.ptr.flags &= ~(lib.AV_PKT_FLAG_CORRUPT) |
| 424 |
|
| 425 |
@property |
| 426 |
def is_discard(self): |
| 427 |
return bool(self.ptr.flags & lib.AV_PKT_FLAG_DISCARD) |
| 428 |
|
| 429 |
@property |
| 430 |
def is_trusted(self): |
| 431 |
return bool(self.ptr.flags & lib.AV_PKT_FLAG_TRUSTED) |
| 432 |
|
| 433 |
@property |
| 434 |
def is_disposable(self): |
| 435 |
return bool(self.ptr.flags & lib.AV_PKT_FLAG_DISPOSABLE) |
| 436 |
|
| 437 |
@property |
| 438 |
def opaque(self): |
| 439 |
if self.ptr.opaque_ref is not cython.NULL: |
| 440 |
return opaque_container.get( |
| 441 |
cython.cast(cython.p_char, self.ptr.opaque_ref.data) |
| 442 |
) |
| 443 |
|
| 444 |
@opaque.setter |
| 445 |
def opaque(self, v): |
| 446 |
lib.av_buffer_unref(cython.address(self.ptr.opaque_ref)) |
| 447 |
|
| 448 |
if v is None: |
| 449 |
return |
| 450 |
self.ptr.opaque_ref = opaque_container.add(v) |
| 451 |
|
| 452 |
def has_sidedata(self, dtype: str) -> bool: |
| 453 |
"""True if this packet has the specified side data |
| 454 |
|
| 455 |
:param dtype: side data type |
| 456 |
:type dtype: str |
| 457 |
""" |
| 458 |
|
| 459 |
dtype2 = packet_sidedata_type_from_literal(dtype) |
| 460 |
return ( |
| 461 |
lib.av_packet_side_data_get( |
| 462 |
self.ptr.side_data, self.ptr.side_data_elems, dtype2 |
| 463 |
) |
| 464 |
!= cython.NULL |
| 465 |
) |
| 466 |
|
| 467 |
def get_sidedata(self, dtype: str) -> PacketSideData: |
| 468 |
"""get a copy of the side data |
| 469 |
|
| 470 |
:param dtype: side data type (:meth:`~av.packet.PacketSideData.sidedata_types` for the full list of options) |
| 471 |
:type dtype: str |
| 472 |
:return: newly created copy of the side data if the side data of the |
| 473 |
requested type is found in the packet, else an empty object |
| 474 |
:rtype: :class:`~av.packet.PacketSideData` |
| 475 |
""" |
| 476 |
return PacketSideData.from_packet(self, dtype) |
| 477 |
|
| 478 |
def set_sidedata(self, sidedata: PacketSideData, move: cython.bint = False): |
| 479 |
"""copy or move side data to this packet |
| 480 |
|
| 481 |
:param sidedata: Source packet side data |
| 482 |
:type sidedata: :class:`~av.packet.PacketSideData` |
| 483 |
:param move: If True, move the data from `sidedata` object, defaults to False |
| 484 |
:type move: bool |
| 485 |
""" |
| 486 |
sidedata.to_packet(self, move) |
| 487 |
|
| 488 |
def iter_sidedata(self) -> Iterator[PacketSideData]: |
| 489 |
"""iterate over side data of this packet. |
| 490 |
|
| 491 |
:yield: :class:`~av.packet.PacketSideData` object |
| 492 |
""" |
| 493 |
|
| 494 |
for i in range(self.ptr.side_data_elems): |
| 495 |
yield _packet_sidedata_from_packet(self.ptr, self.ptr.side_data[i].type) |