Skip to content

Commit d89e4a2

Browse files
committed
Make codec/hwaccel pure
1 parent d83393f commit d89e4a2

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed
Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import weakref
22
from enum import IntEnum
33

4-
cimport libav as lib
5-
6-
from av.codec.codec cimport Codec
7-
from av.dictionary cimport _Dictionary
8-
from av.error cimport err_check
9-
from av.video.format cimport get_video_format
4+
import cython
5+
import cython.cimports.libav as lib
6+
from cython.cimports.av.codec.codec import Codec
7+
from cython.cimports.av.dictionary import _Dictionary
8+
from cython.cimports.av.error import err_check
9+
from cython.cimports.av.video.format import get_video_format
1010

1111
from av.dictionary import Dictionary
1212

@@ -29,42 +29,49 @@ class HWDeviceType(IntEnum):
2929
ohcodec = 14
3030
# TODO: When ffmpeg major is changed, check this enum.
3131

32+
3233
class HWConfigMethod(IntEnum):
3334
none = 0
34-
hw_device_ctx = lib.AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX # This is the only one we support.
35+
hw_device_ctx = (
36+
lib.AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
37+
) # This is the only one we support.
3538
hw_frame_ctx = lib.AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
3639
internal = lib.AV_CODEC_HW_CONFIG_METHOD_INTERNAL
3740
ad_hoc = lib.AV_CODEC_HW_CONFIG_METHOD_AD_HOC
3841

3942

40-
cdef object _cinit_sentinel = object()
41-
cdef object _singletons = weakref.WeakValueDictionary()
43+
_cinit_sentinel = cython.declare(object, object())
44+
_singletons = cython.declare(object, weakref.WeakValueDictionary())
45+
4246

43-
cdef HWConfig wrap_hwconfig(lib.AVCodecHWConfig *ptr):
47+
@cython.cfunc
48+
def wrap_hwconfig(ptr: cython.pointer[lib.AVCodecHWConfig]) -> HWConfig:
4449
try:
45-
return _singletons[<int>ptr]
50+
return _singletons[cython.cast(cython.int, ptr)]
4651
except KeyError:
4752
pass
48-
cdef HWConfig config = HWConfig(_cinit_sentinel)
53+
config: HWConfig = HWConfig(_cinit_sentinel)
4954
config._init(ptr)
50-
_singletons[<int>ptr] = config
55+
_singletons[cython.cast(cython.int, ptr)] = config
5156
return config
5257

5358

54-
cdef class HWConfig:
59+
@cython.cclass
60+
class HWConfig:
5561
def __init__(self, sentinel):
5662
if sentinel is not _cinit_sentinel:
5763
raise RuntimeError("Cannot instantiate CodecContext")
5864

59-
cdef void _init(self, lib.AVCodecHWConfig *ptr):
65+
@cython.cfunc
66+
def _init(self, ptr: cython.pointer[lib.AVCodecHWConfig]) -> cython.void:
6067
self.ptr = ptr
6168

6269
def __repr__(self):
6370
return (
6471
f"<av.{self.__class__.__name__} "
6572
f"device_type={lib.av_hwdevice_get_type_name(self.device_type)} "
6673
f"format={self.format.name if self.format else None} "
67-
f"is_supported={self.is_supported} at 0x{<int>self.ptr:x}>"
74+
f"is_supported={self.is_supported} at 0x{cython.cast(int, self.ptr):x}>"
6875
)
6976

7077
@property
@@ -84,21 +91,28 @@ def is_supported(self):
8491
return bool(self.ptr.methods & lib.AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
8592

8693

87-
cpdef hwdevices_available():
88-
result = []
89-
90-
cdef lib.AVHWDeviceType x = lib.AV_HWDEVICE_TYPE_NONE
94+
@cython.ccall
95+
def hwdevices_available():
96+
result: list = []
97+
x: lib.AVHWDeviceType = lib.AV_HWDEVICE_TYPE_NONE
9198
while True:
9299
x = lib.av_hwdevice_iterate_types(x)
93100
if x == lib.AV_HWDEVICE_TYPE_NONE:
94101
break
95102
result.append(lib.av_hwdevice_get_type_name(HWDeviceType(x)))
96-
97103
return result
98104

99105

100-
cdef class HWAccel:
101-
def __init__(self, device_type, device=None, allow_software_fallback=True, options=None, flags=None):
106+
@cython.cclass
107+
class HWAccel:
108+
def __init__(
109+
self,
110+
device_type,
111+
device=None,
112+
allow_software_fallback=True,
113+
options=None,
114+
flags=None,
115+
):
102116
if isinstance(device_type, HWDeviceType):
103117
self._device_type = device_type
104118
elif isinstance(device_type, str):
@@ -112,11 +126,11 @@ def __init__(self, device_type, device=None, allow_software_fallback=True, optio
112126
self.allow_software_fallback = allow_software_fallback
113127
self.options = {} if not options else dict(options)
114128
self.flags = 0 if not flags else flags
115-
self.ptr = NULL
129+
self.ptr = cython.NULL
116130
self.config = None
117131

118-
def _initialize_hw_context(self, Codec codec not None):
119-
cdef HWConfig config
132+
def _initialize_hw_context(self, codec: Codec):
133+
config: HWConfig
120134
for config in codec.hardware_configs:
121135
if not (config.ptr.methods & lib.AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX):
122136
continue
@@ -127,20 +141,23 @@ def _initialize_hw_context(self, Codec codec not None):
127141
raise NotImplementedError(f"No supported hardware config for {codec}")
128142

129143
self.config = config
130-
131-
cdef char *c_device = NULL
144+
c_device: cython.p_char = cython.NULL
132145
if self._device:
133146
device_bytes = self._device.encode()
134147
c_device = device_bytes
135-
cdef _Dictionary c_options = Dictionary(self.options)
148+
c_options: _Dictionary = Dictionary(self.options)
136149

137150
err_check(
138151
lib.av_hwdevice_ctx_create(
139-
&self.ptr, config.ptr.device_type, c_device, c_options.ptr, self.flags
152+
cython.address(self.ptr),
153+
config.ptr.device_type,
154+
c_device,
155+
c_options.ptr,
156+
self.flags,
140157
)
141158
)
142159

143-
def create(self, Codec codec not None):
160+
def create(self, codec: Codec):
144161
"""Create a new hardware accelerator context with the given codec"""
145162
if self.ptr:
146163
raise RuntimeError("Hardware context already initialized")
@@ -149,11 +166,11 @@ def create(self, Codec codec not None):
149166
device_type=self._device_type,
150167
device=self._device,
151168
allow_software_fallback=self.allow_software_fallback,
152-
options=self.options
169+
options=self.options,
153170
)
154171
ret._initialize_hw_context(codec)
155172
return ret
156173

157174
def __dealloc__(self):
158175
if self.ptr:
159-
lib.av_buffer_unref(&self.ptr)
176+
lib.av_buffer_unref(cython.address(self.ptr))

0 commit comments

Comments
 (0)