Skip to content

Commit 7f05ad4

Browse files
committed
Make av/format pure
1 parent 744b613 commit 7f05ad4

1 file changed

Lines changed: 43 additions & 33 deletions

File tree

av/format.pyx renamed to av/format.py

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
cimport libav as lib
2-
3-
from av.descriptor cimport wrap_avclass
4-
51
from enum import Flag
62

3+
import cython
4+
import cython.cimports.libav as lib
5+
from cython.cimports.av.descriptor import wrap_avclass
76

8-
cdef object _cinit_bypass_sentinel = object()
7+
_cinit_bypass_sentinel = cython.declare(object, object())
98

10-
cdef ContainerFormat build_container_format(lib.AVInputFormat* iptr, lib.AVOutputFormat* optr):
9+
10+
@cython.cfunc
11+
def build_container_format(
12+
iptr: cython.pointer[lib.AVInputFormat], optr: cython.pointer[lib.AVOutputFormat]
13+
) -> ContainerFormat:
1114
if not iptr and not optr:
1215
raise ValueError("needs input format or output format")
13-
cdef ContainerFormat format = ContainerFormat.__new__(ContainerFormat, _cinit_bypass_sentinel)
16+
format: ContainerFormat = ContainerFormat.__new__(
17+
ContainerFormat, _cinit_bypass_sentinel
18+
)
1419
format.iptr = iptr
1520
format.optr = optr
1621
format.name = optr.name if optr else iptr.name
1722
return format
1823

1924

25+
# fmt: off
2026
class Flags(Flag):
2127
no_file = lib.AVFMT_NOFILE
2228
need_number: "Needs '%d' in filename." = lib.AVFMT_NEEDNUMBER
@@ -36,8 +42,11 @@ class Flags(Flag):
3642
# If not set the timestamp will be shifted in `av_write_frame()` and `av_interleaved_write_frame()`
3743
# so they start from 0. The user or muxer can override this through AVFormatContext.avoid_negative_ts
3844
seek_to_pts: "Seeking is based on PTS" = lib.AVFMT_SEEK_TO_PTS
45+
# fmt: on
46+
3947

40-
cdef class ContainerFormat:
48+
@cython.cclass
49+
class ContainerFormat:
4150
"""Descriptor of a container format.
4251
4352
:param str name: The name of the format.
@@ -59,7 +68,7 @@ def __cinit__(self, name, mode=None):
5968
self.iptr = lib.av_find_input_format(name)
6069

6170
if mode is None or mode == "w":
62-
self.optr = lib.av_guess_format(name, NULL, NULL)
71+
self.optr = lib.av_guess_format(name, cython.NULL, cython.NULL)
6372

6473
if not self.iptr and not self.optr:
6574
raise ValueError(f"no container format {name!r}")
@@ -81,30 +90,30 @@ def options(self):
8190
@property
8291
def input(self):
8392
"""An input-only view of this format."""
84-
if self.iptr == NULL:
93+
if self.iptr == cython.NULL:
8594
return None
86-
elif self.optr == NULL:
95+
elif self.optr == cython.NULL:
8796
return self
8897
else:
89-
return build_container_format(self.iptr, NULL)
98+
return build_container_format(self.iptr, cython.NULL)
9099

91100
@property
92101
def output(self):
93102
"""An output-only view of this format."""
94-
if self.optr == NULL:
103+
if self.optr == cython.NULL:
95104
return None
96-
elif self.iptr == NULL:
105+
elif self.iptr == cython.NULL:
97106
return self
98107
else:
99-
return build_container_format(NULL, self.optr)
108+
return build_container_format(cython.NULL, self.optr)
100109

101110
@property
102111
def is_input(self):
103-
return self.iptr != NULL
112+
return self.iptr != cython.NULL
104113

105114
@property
106115
def is_output(self):
107-
return self.optr != NULL
116+
return self.optr != cython.NULL
108117

109118
@property
110119
def long_name(self):
@@ -114,7 +123,7 @@ def long_name(self):
114123

115124
@property
116125
def extensions(self):
117-
cdef set exts = set()
126+
exts: set = set()
118127
if self.iptr and self.iptr.extensions:
119128
exts.update(self.iptr.extensions.split(","))
120129
if self.optr and self.optr.extensions:
@@ -128,42 +137,43 @@ def flags(self):
128137
129138
:rtype: int
130139
"""
131-
return (
132-
(self.iptr.flags if self.iptr else 0) |
133-
(self.optr.flags if self.optr else 0)
140+
return (self.iptr.flags if self.iptr else 0) | (
141+
self.optr.flags if self.optr else 0
134142
)
135143

136144
@property
137145
def no_file(self):
138146
return bool(self.flags & lib.AVFMT_NOFILE)
139147

140148

141-
cdef get_output_format_names():
142-
names = set()
143-
cdef const lib.AVOutputFormat *ptr
144-
cdef void *opaque = NULL
149+
@cython.cfunc
150+
def get_output_format_names() -> set:
151+
names: set = set()
152+
ptr: cython.pointer[cython.const[lib.AVOutputFormat]]
153+
opaque: cython.p_void = cython.NULL
145154
while True:
146-
ptr = lib.av_muxer_iterate(&opaque)
155+
ptr = lib.av_muxer_iterate(cython.address(opaque))
147156
if ptr:
148157
names.add(ptr.name)
149158
else:
150159
break
151160
return names
152161

153-
cdef get_input_format_names():
154-
names = set()
155-
cdef const lib.AVInputFormat *ptr
156-
cdef void *opaque = NULL
162+
163+
@cython.cfunc
164+
def get_input_format_names() -> set:
165+
names: set = set()
166+
ptr: cython.pointer[cython.const[lib.AVInputFormat]]
167+
opaque: cython.p_void = cython.NULL
157168
while True:
158-
ptr = lib.av_demuxer_iterate(&opaque)
169+
ptr = lib.av_demuxer_iterate(cython.address(opaque))
159170
if ptr:
160171
names.add(ptr.name)
161172
else:
162173
break
163174
return names
164175

176+
165177
formats_available = get_output_format_names()
166178
formats_available.update(get_input_format_names())
167-
168-
169179
format_descriptor = wrap_avclass(lib.avformat_get_class())

0 commit comments

Comments
 (0)