Skip to content

Commit 5213cdd

Browse files
committed
Add typestubs and change names
1 parent c3bf776 commit 5213cdd

File tree

4 files changed

+117
-63
lines changed

4 files changed

+117
-63
lines changed

av/packet.pxd

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,4 @@ cdef class Packet(Buffer):
2121
cdef size_t _buffer_size(self)
2222
cdef void* _buffer_ptr(self)
2323

24-
cdef PacketSideData _packet_side_data_from_packet(
25-
lib.AVPacket* packet, lib.AVPacketSideDataType data_type
26-
)
24+
cdef PacketSideData _packet_sidedata_from_packet(lib.AVPacket* packet, lib.AVPacketSideDataType data_type)

av/packet.py

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from cython.cimports.av.utils import avrational_to_fraction, to_avrational
99
from cython.cimports.libc.string import memcpy
1010

11-
# check https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/packet.h#L41 for
12-
# new additions in the future FFmpeg releases
13-
# note: the order must follow that of the AVPacketSideDataType enum def
14-
PacketSideDataTypeLiteral = Literal[
11+
# Check https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/packet.h#L41
12+
# for new additions in the future ffmpeg releases
13+
# Note: the order must follow that of the AVPacketSideDataType enum def
14+
PktSideDataT = Literal[
1515
"palette",
1616
"new_extradata",
1717
"param_change",
@@ -55,33 +55,26 @@
5555
]
5656

5757

58-
def _packet_side_data_type_to_literal(
59-
data_type: lib.AVPacketSideDataType,
60-
) -> PacketSideDataTypeLiteral:
61-
return get_args(PacketSideDataTypeLiteral)[cython.cast(int, data_type)]
58+
def packet_sidedata_type_to_literal(dtype: lib.AVPacketSideDataType) -> PktSideDataT:
59+
return get_args(PktSideDataT)[cython.cast(int, dtype)]
6260

6361

64-
def _packet_side_data_type_from_literal(
65-
data_type: PacketSideDataTypeLiteral,
66-
) -> lib.AVPacketSideDataType:
67-
i = cython.declare(cython.int, get_args(PacketSideDataTypeLiteral).index(data_type))
62+
def packet_sidedata_type_from_literal(dtype: PktSideDataT) -> lib.AVPacketSideDataType:
63+
return get_args(PktSideDataT).index(dtype)
6864

69-
return cython.cast(lib.AVPacketSideDataType, i)
7065

71-
72-
def packet_side_data_type_desc(data_type: PacketSideDataTypeLiteral) -> str | None:
66+
@cython.ccall
67+
def sidedata_typedesc(dtype: PktSideDataT) -> str | None:
7368
"""FFmpeg description of packet side data type"""
74-
dtype = _packet_side_data_type_from_literal(data_type)
75-
res = lib.av_packet_side_data_name(dtype)
69+
dtype2: lib.AVPacketSideDataType = packet_sidedata_type_from_literal(dtype)
70+
res: cython.pointer[cython.char] = lib.av_packet_side_data_name(dtype2)
7671
return res if res != cython.NULL else None
7772

7873

7974
@cython.cclass
8075
class PacketSideData:
8176
@staticmethod
82-
def from_packet(
83-
packet: "Packet", data_type: PacketSideDataTypeLiteral
84-
) -> "PacketSideData":
77+
def from_packet(packet: Packet, data_type: PktSideDataT) -> PacketSideData:
8578
"""create new PacketSideData by copying an existing packet's side data
8679
8780
:param packet: Source packet
@@ -92,8 +85,8 @@ def from_packet(
9285
:rtype: :class:`~av.packet.PacketSideData`
9386
"""
9487

95-
dtype = _packet_side_data_type_from_literal(data_type)
96-
return _packet_side_data_from_packet(packet.ptr, dtype)
88+
dtype = packet_sidedata_type_from_literal(data_type)
89+
return _packet_sidedata_from_packet(packet.ptr, dtype)
9790

9891
def __cinit__(self, dtype: lib.AVPacketSideDataType, size: cython.size_t):
9992
self.dtype = dtype
@@ -110,7 +103,7 @@ def __dealloc__(self):
110103
with cython.nogil:
111104
lib.av_freep(cython.address(self.data))
112105

113-
def to_packet(self, packet: "Packet", move: cython.bint = False):
106+
def to_packet(self, packet: Packet, move: cython.bint = False):
114107
"""copy or move side data to the specified packet
115108
116109
:param packet: Target packet
@@ -146,7 +139,7 @@ def data_type(self) -> str:
146139
147140
:type: str
148141
"""
149-
return _packet_side_data_type_to_literal(self.dtype)
142+
return packet_sidedata_type_to_literal(self.dtype)
150143

151144
@property
152145
def data_desc(self) -> str:
@@ -177,17 +170,16 @@ def __bool__(self) -> bool:
177170

178171

179172
@cython.cfunc
180-
def _packet_side_data_from_packet(
181-
packet: cython.pointer[lib.AVPacket], data_type: lib.AVPacketSideDataType
173+
def _packet_sidedata_from_packet(
174+
packet: cython.pointer[lib.AVPacket], dtype: lib.AVPacketSideDataType
182175
) -> PacketSideData:
183176
with cython.nogil:
184177
c_ptr = lib.av_packet_side_data_get(
185-
packet.side_data, packet.side_data_elems, data_type
178+
packet.side_data, packet.side_data_elems, dtype
186179
)
180+
found: cython.bint = c_ptr != cython.NULL
187181

188-
found = cython.declare(cython.bint, c_ptr != cython.NULL)
189-
190-
sdata = PacketSideData(data_type, c_ptr.size if found else 0)
182+
sdata = PacketSideData(dtype, c_ptr.size if found else 0)
191183

192184
with cython.nogil:
193185
if found:
@@ -426,48 +418,47 @@ def opaque(self, v):
426418
return
427419
self.ptr.opaque_ref = opaque_container.add(v)
428420

429-
def has_side_data(self, data_type: str) -> bool:
421+
def has_sidedata(self, dtype: str) -> bool:
430422
"""True if this packet has the specified side data
431423
432-
:param data_type: side data type
433-
:type data_type: str
424+
:param dtype: side data type
425+
:type dtype: str
434426
"""
435427

436-
dtype = _packet_side_data_type_from_literal(data_type)
428+
dtype2 = packet_sidedata_type_from_literal(dtype)
437429
return (
438430
lib.av_packet_side_data_get(
439-
self.ptr.side_data, self.ptr.side_data_elems, dtype
431+
self.ptr.side_data, self.ptr.side_data_elems, dtype2
440432
)
441433
!= cython.NULL
442434
)
443435

444-
def get_side_data(self, data_type: str) -> PacketSideData:
436+
def get_sidedata(self, dtype: str) -> PacketSideData:
445437
"""get a copy of the side data
446438
447-
:param data_type: side data type (:method:`~av.packet.PacketSideData.side_data_types` for the full list of options)
448-
:type data_type: str
439+
:param dtype: side data type (:method:`~av.packet.PacketSideData.sidedata_types` for the full list of options)
440+
:type dtype: str
449441
:return: newly created copy of the side data if the side data of the
450442
requested type is found in the packet, else an empty object
451443
:rtype: :class:`~av.packet.PacketSideData`
452444
"""
453-
return PacketSideData.from_packet(self, data_type)
445+
return PacketSideData.from_packet(self, dtype)
454446

455-
def set_side_data(self, side_data: PacketSideData, move: bool = False):
447+
def set_sidedata(self, sidedata: PacketSideData, move: cython.bint = False):
456448
"""copy or move side data to this packet
457449
458-
:param side_data: Source packet side data
459-
:type side_data: :class:`~av.packet.PacketSideData`
460-
:param move: True to move the data from `side_data` object,
461-
defaults to False.
450+
:param sidedata: Source packet side data
451+
:type sidedata: :class:`~av.packet.PacketSideData`
452+
:param move: If True, move the data from `sidedata` object, defaults to False
462453
:type move: bool
463454
"""
464-
side_data.to_packet(self, move)
455+
sidedata.to_packet(self, move)
465456

466-
def iter_side_data(self) -> Iterator[PacketSideData]:
457+
def iter_sidedata(self) -> Iterator[PacketSideData]:
467458
"""iterate over side data of this packet.
468459
469460
:yield: :class:`~av.packet.PacketSideData` object
470461
"""
471462

472463
for i in range(self.ptr.side_data_elems):
473-
yield _packet_side_data_from_packet(self.ptr, self.ptr.side_data[i].type)
464+
yield _packet_sidedata_from_packet(self.ptr, self.ptr.side_data[i].type)

av/packet.pyi

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,71 @@
11
from fractions import Fraction
2+
from typing import Iterator, Literal
23

34
from av.subtitles.subtitle import SubtitleSet
45

56
from .buffer import Buffer
67
from .stream import Stream
78

9+
# Sync with definition in 'packet.py'
10+
PktSideDataT = Literal[
11+
"palette",
12+
"new_extradata",
13+
"param_change",
14+
"h263_mb_info",
15+
"replay_gain",
16+
"display_matrix",
17+
"stereo_3d",
18+
"audio_service_type",
19+
"quality_stats",
20+
"fallback_track",
21+
"cpb_properties",
22+
"skip_samples",
23+
"jp_dual_mono",
24+
"strings_metadata",
25+
"subtitle_position",
26+
"matroska_block_additional",
27+
"webvtt_identifier",
28+
"webvtt_settings",
29+
"metadata_update",
30+
"mpegts_stream_id",
31+
"mastering_display_metadata",
32+
"spherical",
33+
"content_light_level",
34+
"a53_cc",
35+
"encryption_init_info",
36+
"encryption_info",
37+
"afd",
38+
"prft",
39+
"icc_profile",
40+
"dovi_conf",
41+
"s12m_timecode",
42+
"dynamic_hdr10_plus",
43+
"iamf_mix_gain_param",
44+
"iamf_info_param",
45+
"iamf_recon_gain_info_param",
46+
"ambient_viewing_environment",
47+
"frame_cropping",
48+
"lcevc",
49+
"3d_reference_displays",
50+
"rtcp_sr",
51+
]
52+
53+
class PacketSideData:
54+
@staticmethod
55+
def from_packet(packet: Packet, dtype: PktSideDataT) -> PacketSideData: ...
56+
def to_packet(self, packet: Packet, move: bool = False): ...
57+
@property
58+
def data_type(self) -> str: ...
59+
@property
60+
def data_desc(self) -> str: ...
61+
@property
62+
def data_size(self) -> int: ...
63+
def __bool__(self) -> bool: ...
64+
65+
def packet_sidedata_type_to_literal(dtype: int) -> PktSideDataT: ...
66+
def packet_sidedata_type_from_literal(dtype: PktSideDataT) -> int: ...
67+
def sidedata_typedesc(dtype: PktSideDataT) -> str | None: ...
68+
869
class Packet(Buffer):
970
stream: Stream
1071
stream_index: int
@@ -23,3 +84,7 @@ class Packet(Buffer):
2384

2485
def __init__(self, input: int | bytes | None = None) -> None: ...
2586
def decode(self) -> list[SubtitleSet]: ...
87+
def has_sidedata(self, dtype: PktSideDataT) -> bool: ...
88+
def get_sidedata(self, dtype: PktSideDataT) -> PacketSideData: ...
89+
def set_sidedata(self, sidedata: PacketSideData, move: bool = False) -> None: ...
90+
def iter_sidedata(self) -> Iterator[PacketSideData]: ...

tests/test_packet.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,44 +53,44 @@ def test_set_duration(self) -> None:
5353

5454

5555
class TestPacketSideData:
56-
def test_data_types(self):
57-
dtypes = get_args(av.packet.PacketSideDataTypeLiteral)
56+
def test_data_types(self) -> None:
57+
dtypes = get_args(av.packet.PktSideDataT)
5858
ffmpeg_ver = [int(v) for v in av.ffmpeg_version_info.split(".", 2)[:2]]
5959
for dtype in dtypes:
60-
av_enum = av.packet._packet_side_data_type_from_literal(dtype)
61-
assert dtype == av.packet._packet_side_data_type_to_literal(av_enum)
62-
assert av.packet.packet_side_data_type_desc(dtype) is not None
60+
av_enum = av.packet.packet_sidedata_type_from_literal(dtype)
61+
assert dtype == av.packet.packet_sidedata_type_to_literal(av_enum)
62+
assert av.packet.sidedata_typedesc(dtype) is not None
6363

6464
if (ffmpeg_ver[0] < 8 and dtype == "lcevc") or (
6565
ffmpeg_ver[0] < 9 and dtype == "rtcp_sr"
6666
):
6767
break
6868

69-
def test_iter(self):
69+
def test_iter(self) -> None:
7070
with av.open(fate_suite("h264/extradata-reload-multi-stsd.mov")) as container:
7171
for pkt in container.demux():
72-
for sdata in pkt.iter_side_data():
72+
for sdata in pkt.iter_sidedata():
7373
assert pkt.dts == 2 and sdata.data_type == "new_extradata"
7474

75-
def test_palette(self):
75+
def test_palette(self) -> None:
7676
with av.open(fate_suite("h264/extradata-reload-multi-stsd.mov")) as container:
7777
iterpackets = container.demux()
78-
pkt = next(pkt for pkt in iterpackets if pkt.has_side_data("new_extradata"))
78+
pkt = next(pkt for pkt in iterpackets if pkt.has_sidedata("new_extradata"))
7979

80-
sdata = pkt.get_side_data("new_extradata")
80+
sdata = pkt.get_sidedata("new_extradata")
8181
assert sdata.data_type == "new_extradata"
8282
assert bool(sdata)
8383
assert sdata.data_size > 0
8484
assert sdata.data_desc == "New Extradata"
8585

8686
nxt = next(iterpackets) # has no palette
8787

88-
assert not nxt.has_side_data("new_extradata")
88+
assert not nxt.has_sidedata("new_extradata")
8989

90-
sdata1 = nxt.get_side_data("new_extradata")
90+
sdata1 = nxt.get_sidedata("new_extradata")
9191
assert sdata1.data_type == "new_extradata"
9292
assert not bool(sdata1)
9393
assert sdata1.data_size == 0
9494

95-
nxt.set_side_data(sdata, move=True)
95+
nxt.set_sidedata(sdata, move=True)
9696
assert not bool(sdata)

0 commit comments

Comments
 (0)