| 1 |
import weakref |
| 2 |
from enum import IntEnum |
| 3 |
|
| 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 |
| 10 |
|
| 11 |
|
| 12 |
class HWDeviceType(IntEnum): |
| 13 |
none = lib.AV_HWDEVICE_TYPE_NONE |
| 14 |
vdpau = lib.AV_HWDEVICE_TYPE_VDPAU |
| 15 |
cuda = lib.AV_HWDEVICE_TYPE_CUDA |
| 16 |
vaapi = lib.AV_HWDEVICE_TYPE_VAAPI |
| 17 |
dxva2 = lib.AV_HWDEVICE_TYPE_DXVA2 |
| 18 |
qsv = lib.AV_HWDEVICE_TYPE_QSV |
| 19 |
videotoolbox = lib.AV_HWDEVICE_TYPE_VIDEOTOOLBOX |
| 20 |
d3d11va = lib.AV_HWDEVICE_TYPE_D3D11VA |
| 21 |
drm = lib.AV_HWDEVICE_TYPE_DRM |
| 22 |
opencl = lib.AV_HWDEVICE_TYPE_OPENCL |
| 23 |
mediacodec = lib.AV_HWDEVICE_TYPE_MEDIACODEC |
| 24 |
vulkan = lib.AV_HWDEVICE_TYPE_VULKAN |
| 25 |
d3d12va = lib.AV_HWDEVICE_TYPE_D3D12VA |
| 26 |
amf = 13 # FFmpeg >=8 |
| 27 |
ohcodec = 14 |
| 28 |
# TODO: When ffmpeg major is changed, check this enum. |
| 29 |
|
| 30 |
|
| 31 |
class HWConfigMethod(IntEnum): |
| 32 |
none = 0 |
| 33 |
hw_device_ctx = ( |
| 34 |
lib.AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX |
| 35 |
) # This is the only one we support. |
| 36 |
hw_frame_ctx = lib.AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX |
| 37 |
internal = lib.AV_CODEC_HW_CONFIG_METHOD_INTERNAL |
| 38 |
ad_hoc = lib.AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
| 39 |
|
| 40 |
|
| 41 |
_cinit_sentinel = cython.declare(object, object()) |
| 42 |
_singletons = cython.declare(object, weakref.WeakValueDictionary()) |
| 43 |
|
| 44 |
|
| 45 |
@cython.cfunc |
| 46 |
def wrap_hwconfig(ptr: cython.pointer[cython.const[lib.AVCodecHWConfig]]) -> HWConfig: |
| 47 |
try: |
| 48 |
return _singletons[cython.cast(cython.Py_ssize_t, ptr)] |
| 49 |
except KeyError: |
| 50 |
pass |
| 51 |
config: HWConfig = HWConfig(_cinit_sentinel) |
| 52 |
config._init(ptr) |
| 53 |
_singletons[cython.cast(cython.Py_ssize_t, ptr)] = config |
| 54 |
return config |
| 55 |
|
| 56 |
|
| 57 |
@cython.final |
| 58 |
@cython.cclass |
| 59 |
class HWConfig: |
| 60 |
def __init__(self, sentinel): |
| 61 |
if sentinel is not _cinit_sentinel: |
| 62 |
raise RuntimeError("Cannot instantiate CodecContext") |
| 63 |
|
| 64 |
@cython.cfunc |
| 65 |
def _init( |
| 66 |
self, ptr: cython.pointer[cython.const[lib.AVCodecHWConfig]] |
| 67 |
) -> cython.void: |
| 68 |
self.ptr = ptr |
| 69 |
|
| 70 |
def __repr__(self): |
| 71 |
return ( |
| 72 |
f"<av.{self.__class__.__name__} " |
| 73 |
f"device_type={lib.av_hwdevice_get_type_name(self.device_type)} " |
| 74 |
f"format={self.format.name if self.format else None} " |
| 75 |
f"is_supported={self.is_supported} at 0x{cython.cast(cython.Py_ssize_t, self.ptr):x}>" |
| 76 |
) |
| 77 |
|
| 78 |
@property |
| 79 |
def device_type(self): |
| 80 |
return HWDeviceType(self.ptr.device_type) |
| 81 |
|
| 82 |
@property |
| 83 |
def format(self): |
| 84 |
return get_video_format(self.ptr.pix_fmt, 0, 0) |
| 85 |
|
| 86 |
@property |
| 87 |
def methods(self): |
| 88 |
return HWConfigMethod(self.ptr.methods) |
| 89 |
|
| 90 |
@property |
| 91 |
def is_supported(self): |
| 92 |
return bool(self.ptr.methods & lib.AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) |
| 93 |
|
| 94 |
|
| 95 |
@cython.ccall |
| 96 |
def hwdevices_available(): |
| 97 |
result: list = [] |
| 98 |
x: lib.AVHWDeviceType = lib.AV_HWDEVICE_TYPE_NONE |
| 99 |
while True: |
| 100 |
x = lib.av_hwdevice_iterate_types(x) |
| 101 |
if x == lib.AV_HWDEVICE_TYPE_NONE: |
| 102 |
break |
| 103 |
result.append(lib.av_hwdevice_get_type_name(HWDeviceType(x))) |
| 104 |
return result |
| 105 |
|
| 106 |
|
| 107 |
@cython.final |
| 108 |
@cython.cclass |
| 109 |
class HWAccel: |
| 110 |
def __init__( |
| 111 |
self, |
| 112 |
device_type, |
| 113 |
device=None, |
| 114 |
allow_software_fallback=True, |
| 115 |
options=None, |
| 116 |
flags=None, |
| 117 |
is_hw_owned=False, |
| 118 |
): |
| 119 |
if isinstance(device_type, HWDeviceType): |
| 120 |
self._device_type = device_type |
| 121 |
elif isinstance(device_type, str): |
| 122 |
self._device_type = int(lib.av_hwdevice_find_type_by_name(device_type)) |
| 123 |
elif isinstance(device_type, int): |
| 124 |
self._device_type = device_type |
| 125 |
else: |
| 126 |
raise ValueError("Unknown type for device_type") |
| 127 |
|
| 128 |
self.is_hw_owned = is_hw_owned |
| 129 |
self.device_id = 0 |
| 130 |
if self._device_type == HWDeviceType.cuda and device: |
| 131 |
self.device_id = int(device) |
| 132 |
|
| 133 |
self._device = None if device is None else f"{device}" |
| 134 |
self.allow_software_fallback = allow_software_fallback |
| 135 |
|
| 136 |
self.options = {} if not options else dict(options) |
| 137 |
if self._device_type == HWDeviceType.cuda and self.is_hw_owned: |
| 138 |
self.options.setdefault("primary_ctx", "1") |
| 139 |
self.flags = 0 if not flags else flags |
| 140 |
self.ptr = cython.NULL |
| 141 |
self.config = None |
| 142 |
|
| 143 |
def _initialize_hw_context(self, codec: Codec): |
| 144 |
config: HWConfig |
| 145 |
for config in codec.hardware_configs: |
| 146 |
if not (config.ptr.methods & lib.AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX): |
| 147 |
continue |
| 148 |
if self._device_type and config.device_type != self._device_type: |
| 149 |
continue |
| 150 |
break |
| 151 |
else: # nobreak |
| 152 |
raise NotImplementedError(f"No supported hardware config for {codec}") |
| 153 |
|
| 154 |
self.config = config |
| 155 |
c_device: cython.p_char = cython.NULL |
| 156 |
if self._device: |
| 157 |
device_bytes = self._device.encode() |
| 158 |
c_device = device_bytes |
| 159 |
c_options: Dictionary = Dictionary(self.options) |
| 160 |
|
| 161 |
err_check( |
| 162 |
lib.av_hwdevice_ctx_create( |
| 163 |
cython.address(self.ptr), |
| 164 |
config.ptr.device_type, |
| 165 |
c_device, |
| 166 |
c_options.ptr, |
| 167 |
self.flags, |
| 168 |
) |
| 169 |
) |
| 170 |
|
| 171 |
def create(self, codec: Codec) -> HWAccel: |
| 172 |
"""Create a new hardware accelerator context with the given codec""" |
| 173 |
if self.ptr: |
| 174 |
raise RuntimeError("Hardware context already initialized") |
| 175 |
|
| 176 |
ret = HWAccel( |
| 177 |
device_type=self._device_type, |
| 178 |
device=self._device, |
| 179 |
allow_software_fallback=self.allow_software_fallback, |
| 180 |
options=self.options, |
| 181 |
is_hw_owned=self.is_hw_owned, |
| 182 |
) |
| 183 |
ret._initialize_hw_context(codec) |
| 184 |
return ret |
| 185 |
|
| 186 |
def __dealloc__(self): |
| 187 |
if self.ptr: |
| 188 |
lib.av_buffer_unref(cython.address(self.ptr)) |