Skip to content

Commit e3a0cbb

Browse files
committed
Make codec/codec pure
1 parent dc4d6b4 commit e3a0cbb

File tree

1 file changed

+68
-51
lines changed

1 file changed

+68
-51
lines changed
Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
cimport libav as lib
2-
3-
from av.audio.format cimport get_audio_format
4-
from av.codec.hwaccel cimport wrap_hwconfig
5-
from av.descriptor cimport wrap_avclass
6-
from av.utils cimport avrational_to_fraction
7-
from av.video.format cimport VideoFormat, get_pix_fmt, get_video_format
8-
91
from enum import Flag, IntEnum
10-
from libc.stdlib cimport free, malloc
112

3+
import cython
4+
from cython.cimports import libav as lib
5+
from cython.cimports.av.audio.format import get_audio_format
6+
from cython.cimports.av.codec.hwaccel import wrap_hwconfig
7+
from cython.cimports.av.descriptor import wrap_avclass
8+
from cython.cimports.av.utils import avrational_to_fraction
9+
from cython.cimports.av.video.format import VideoFormat, get_pix_fmt, get_video_format
10+
from cython.cimports.libc.stdlib import free, malloc
11+
12+
_cinit_sentinel = cython.declare(object, object())
1213

13-
cdef object _cinit_sentinel = object()
1414

15-
cdef Codec wrap_codec(const lib.AVCodec *ptr):
16-
cdef Codec codec = Codec(_cinit_sentinel)
15+
@cython.cfunc
16+
def wrap_codec(ptr: cython.pointer[cython.const[lib.AVCodec]]) -> Codec:
17+
codec: Codec = Codec(_cinit_sentinel)
1718
codec.ptr = ptr
1819
codec.is_encoder = lib.av_codec_is_encoder(ptr)
1920
codec._init()
2021
return codec
2122

23+
2224
class Properties(Flag):
2325
NONE = 0
2426
INTRA_ONLY = lib.AV_CODEC_PROP_INTRA_ONLY
@@ -57,7 +59,8 @@ class UnknownCodecError(ValueError):
5759
pass
5860

5961

60-
cdef class Codec:
62+
@cython.cclass
63+
class Codec:
6164
"""Codec(name, mode='r')
6265
6366
:param str name: The codec name.
@@ -105,7 +108,8 @@ def __cinit__(self, name, mode="r"):
105108
if (mode == "w") != self.is_encoder:
106109
raise RuntimeError("Found codec does not match mode.", name, mode)
107110

108-
cdef _init(self, name=None):
111+
@cython.cfunc
112+
def _init(self, name=None):
109113
if not self.ptr:
110114
raise UnknownCodecError(name)
111115

@@ -124,12 +128,13 @@ def __repr__(self):
124128
mode = self.mode
125129
return f"<av.{self.__class__.__name__} {self.name} {mode=}>"
126130

127-
def create(self, kind = None):
131+
def create(self, kind=None):
128132
"""Create a :class:`.CodecContext` for this codec.
129133
130134
:param str kind: Gives a hint to static type checkers for what exact CodecContext is used.
131135
"""
132136
from .context import CodecContext
137+
133138
return CodecContext.create(self)
134139

135140
@property
@@ -141,10 +146,12 @@ def is_decoder(self):
141146
return not self.is_encoder
142147

143148
@property
144-
def descriptor(self): return wrap_avclass(self.ptr.priv_class)
149+
def descriptor(self):
150+
return wrap_avclass(self.ptr.priv_class)
145151

146152
@property
147-
def name(self): return self.ptr.name or ""
153+
def name(self):
154+
return self.ptr.name or ""
148155

149156
@property
150157
def canonical_name(self):
@@ -154,7 +161,8 @@ def canonical_name(self):
154161
return lib.avcodec_get_name(self.ptr.id)
155162

156163
@property
157-
def long_name(self): return self.ptr.long_name or ""
164+
def long_name(self):
165+
return self.ptr.long_name or ""
158166

159167
@property
160168
def type(self):
@@ -167,18 +175,21 @@ def type(self):
167175
return lib.av_get_media_type_string(self.ptr.type)
168176

169177
@property
170-
def id(self): return self.ptr.id
178+
def id(self):
179+
return self.ptr.id
171180

172181
@property
173182
def frame_rates(self):
174183
"""A list of supported frame rates (:class:`fractions.Fraction`), or ``None``."""
175184
if not self.ptr.supported_framerates:
176185
return
177186

178-
ret = []
179-
cdef int i = 0
187+
ret: list = []
188+
i: cython.int = 0
180189
while self.ptr.supported_framerates[i].denum:
181-
ret.append(avrational_to_fraction(&self.ptr.supported_framerates[i]))
190+
ret.append(
191+
avrational_to_fraction(cython.address(self.ptr.supported_framerates[i]))
192+
)
182193
i += 1
183194
return ret
184195

@@ -188,8 +199,8 @@ def audio_rates(self):
188199
if not self.ptr.supported_samplerates:
189200
return
190201

191-
ret = []
192-
cdef int i = 0
202+
ret: list = []
203+
i: cython.int = 0
193204
while self.ptr.supported_samplerates[i]:
194205
ret.append(self.ptr.supported_samplerates[i])
195206
i += 1
@@ -201,8 +212,8 @@ def video_formats(self):
201212
if not self.ptr.pix_fmts:
202213
return
203214

204-
ret = []
205-
cdef int i = 0
215+
ret: list = []
216+
i: cython.int = 0
206217
while self.ptr.pix_fmts[i] != -1:
207218
ret.append(get_video_format(self.ptr.pix_fmts[i], 0, 0))
208219
i += 1
@@ -214,8 +225,8 @@ def audio_formats(self):
214225
if not self.ptr.sample_fmts:
215226
return
216227

217-
ret = []
218-
cdef int i = 0
228+
ret: list = []
229+
i: cython.int = 0
219230
while self.ptr.sample_fmts[i] != -1:
220231
ret.append(get_audio_format(self.ptr.sample_fmts[i]))
221232
i += 1
@@ -225,9 +236,9 @@ def audio_formats(self):
225236
def hardware_configs(self):
226237
if self._hardware_configs:
227238
return self._hardware_configs
228-
ret = []
229-
cdef int i = 0
230-
cdef const lib.AVCodecHWConfig *ptr
239+
ret: list = []
240+
i: cython.int = 0
241+
ptr: cython.pointer[cython.const[lib.AVCodecHWConfig]]
231242
while True:
232243
ptr = lib.avcodec_get_hw_config(self.ptr, i)
233244
if not ptr:
@@ -309,12 +320,14 @@ def delay(self):
309320
"""
310321
return bool(self.ptr.capabilities & lib.AV_CODEC_CAP_DELAY)
311322

312-
cdef get_codec_names():
313-
names = set()
314-
cdef const lib.AVCodec *ptr
315-
cdef void *opaque = NULL
323+
324+
@cython.cfunc
325+
def get_codec_names():
326+
names: cython.set = set()
327+
ptr = cython.declare(cython.pointer[cython.const[lib.AVCodec]])
328+
opaque: cython.p_void = cython.NULL
316329
while True:
317-
ptr = lib.av_codec_iterate(&opaque)
330+
ptr = lib.av_codec_iterate(cython.address(opaque))
318331
if ptr:
319332
names.add(ptr.name)
320333
else:
@@ -373,6 +386,7 @@ def dump_codecs():
373386
except Exception as e:
374387
print(f"...... {codec.name:<18} ERROR: {e}")
375388

389+
376390
def dump_hwconfigs():
377391
print("Hardware configs:")
378392
for name in sorted(codecs_available):
@@ -402,13 +416,13 @@ def find_best_pix_fmt_of_list(pix_fmts, src_pix_fmt, has_alpha=False):
402416
:return: (best_format, loss)
403417
:rtype: (VideoFormat | None, int)
404418
"""
405-
cdef lib.AVPixelFormat src
406-
cdef lib.AVPixelFormat best
407-
cdef lib.AVPixelFormat *c_list = NULL
408-
cdef Py_ssize_t n
409-
cdef Py_ssize_t i
410-
cdef object item
411-
cdef int c_loss
419+
src: lib.AVPixelFormat
420+
best: lib.AVPixelFormat
421+
c_list: cython.pointer[lib.AVPixelFormat] = cython.NULL
422+
n: cython.Py_ssize_t
423+
i: cython.Py_ssize_t
424+
item: object
425+
c_loss: cython.int
412426

413427
if pix_fmts is None:
414428
raise TypeError("pix_fmts must not be None")
@@ -418,29 +432,32 @@ def find_best_pix_fmt_of_list(pix_fmts, src_pix_fmt, has_alpha=False):
418432
return None, 0
419433

420434
if isinstance(src_pix_fmt, VideoFormat):
421-
src = (<VideoFormat>src_pix_fmt).pix_fmt
435+
src = cython.cast(VideoFormat, src_pix_fmt).pix_fmt
422436
else:
423-
src = get_pix_fmt(<str>src_pix_fmt)
437+
src = get_pix_fmt(cython.cast(str, src_pix_fmt))
424438

425439
n = len(pix_fmts)
426-
c_list = <lib.AVPixelFormat *>malloc((n + 1) * sizeof(lib.AVPixelFormat))
427-
if c_list == NULL:
440+
c_list = cython.cast(
441+
cython.pointer[lib.AVPixelFormat],
442+
malloc((n + 1) * cython.sizeof(lib.AVPixelFormat)),
443+
)
444+
if c_list == cython.NULL:
428445
raise MemoryError()
429446

430447
try:
431448
for i in range(n):
432449
item = pix_fmts[i]
433450
if isinstance(item, VideoFormat):
434-
c_list[i] = (<VideoFormat>item).pix_fmt
451+
c_list[i] = cython.cast(VideoFormat, item).pix_fmt
435452
else:
436-
c_list[i] = get_pix_fmt(<str>item)
453+
c_list[i] = get_pix_fmt(cython.cast(str, item))
437454
c_list[n] = lib.AV_PIX_FMT_NONE
438455

439456
c_loss = 0
440457
best = lib.avcodec_find_best_pix_fmt_of_list(
441-
c_list, src, 1 if has_alpha else 0, &c_loss
458+
c_list, src, 1 if has_alpha else 0, cython.address(c_loss)
442459
)
443460
return get_video_format(best, 0, 0), c_loss
444461
finally:
445-
if c_list != NULL:
462+
if c_list != cython.NULL:
446463
free(c_list)

0 commit comments

Comments
 (0)