| 1 |
import cython |
| 2 |
import cython.cimports.libav as lib |
| 3 |
from cython.cimports.av.error import err_check |
| 4 |
from cython.cimports.av.video.format import ( |
| 5 |
VideoFormatComponent, |
| 6 |
get_pix_fmt, |
| 7 |
get_video_format, |
| 8 |
) |
| 9 |
from cython.cimports.av.video.frame import VideoFrame |
| 10 |
from cython.cimports.cpython import PyBUF_WRITABLE, PyBuffer_FillInfo |
| 11 |
from cython.cimports.cpython.buffer import Py_buffer |
| 12 |
from cython.cimports.cpython.pycapsule import ( |
| 13 |
PyCapsule_GetPointer, |
| 14 |
PyCapsule_IsValid, |
| 15 |
PyCapsule_New, |
| 16 |
) |
| 17 |
from cython.cimports.libc.stdlib import free, malloc |
| 18 |
|
| 19 |
|
| 20 |
@cython.final |
| 21 |
@cython.cclass |
| 22 |
class VideoPlane(Plane): |
| 23 |
def __cinit__(self, frame: VideoFrame, index: cython.int): |
| 24 |
# The palette plane has no associated component or linesize; set fields manually |
| 25 |
fmt = frame.format |
| 26 |
if frame.ptr.hw_frames_ctx: |
| 27 |
frames_ctx: cython.pointer[lib.AVHWFramesContext] = cython.cast( |
| 28 |
cython.pointer[lib.AVHWFramesContext], frame.ptr.hw_frames_ctx.data |
| 29 |
) |
| 30 |
fmt = get_video_format( |
| 31 |
frames_ctx.sw_format, frame.ptr.width, frame.ptr.height |
| 32 |
) |
| 33 |
|
| 34 |
if index == 1 and fmt.name == "pal8": |
| 35 |
self.width = 256 |
| 36 |
self.height = 1 |
| 37 |
self.buffer_size = 256 * 4 |
| 38 |
return |
| 39 |
|
| 40 |
for i in range(fmt.ptr.nb_components): |
| 41 |
if fmt.ptr.comp[i].plane == index: |
| 42 |
component = VideoFormatComponent(fmt, i) |
| 43 |
self.width = component.width |
| 44 |
self.height = component.height |
| 45 |
break |
| 46 |
else: |
| 47 |
raise RuntimeError(f"could not find plane {index} of {fmt!r}") |
| 48 |
|
| 49 |
# Sometimes, linesize is negative (and that is meaningful). We are only |
| 50 |
# insisting that the buffer size be based on the extent of linesize, and |
| 51 |
# ignore it's direction. |
| 52 |
self.buffer_size = abs(self.frame.ptr.linesize[self.index]) * self.height |
| 53 |
|
| 54 |
@cython.cfunc |
| 55 |
def _buffer_size(self) -> cython.size_t: |
| 56 |
return self.buffer_size |
| 57 |
|
| 58 |
@property |
| 59 |
def line_size(self): |
| 60 |
""" |
| 61 |
Bytes per horizontal line in this plane. |
| 62 |
|
| 63 |
:type: int |
| 64 |
""" |
| 65 |
return self.frame.ptr.linesize[self.index] |
| 66 |
|
| 67 |
@cython.cfunc |
| 68 |
def _buffer_writable(self) -> cython.bint: |
| 69 |
if self.frame.ptr.hw_frames_ctx: |
| 70 |
return False |
| 71 |
return True |
| 72 |
|
| 73 |
def __getbuffer__(self, view: cython.pointer[Py_buffer], flags: cython.int): |
| 74 |
if self.frame.ptr.hw_frames_ctx: |
| 75 |
raise TypeError( |
| 76 |
"Hardware frame planes do not support the Python buffer protocol. " |
| 77 |
"Use DLPack (__dlpack__) or download to a software frame." |
| 78 |
) |
| 79 |
if flags & PyBUF_WRITABLE and not self._buffer_writable(): |
| 80 |
raise ValueError("buffer is not writable") |
| 81 |
|
| 82 |
ptr: cython.p_void = self._buffer_ptr() |
| 83 |
line_size: cython.int = self.frame.ptr.linesize[self.index] |
| 84 |
if line_size < 0: |
| 85 |
height: cython.int = self.height |
| 86 |
ptr = cython.cast(cython.p_char, ptr) + (height - 1) * line_size |
| 87 |
|
| 88 |
PyBuffer_FillInfo(view, self, ptr, self._buffer_size(), 0, flags) |
| 89 |
|
| 90 |
def __dlpack_device__(self): |
| 91 |
if self.frame.ptr.hw_frames_ctx: |
| 92 |
if cython.cast(lib.AVPixelFormat, self.frame.ptr.format) != get_pix_fmt( |
| 93 |
b"cuda" |
| 94 |
): |
| 95 |
raise NotImplementedError( |
| 96 |
"DLPack export is only implemented for CUDA hw frames" |
| 97 |
) |
| 98 |
return (kCuda, self.frame._device_id) |
| 99 |
return (kCPU, 0) |
| 100 |
|
| 101 |
def __dlpack__(self, *, stream: int | None = None): |
| 102 |
if self.frame.ptr.buf[0] == cython.NULL: |
| 103 |
raise TypeError( |
| 104 |
"DLPack export requires a refcounted AVFrame (frame.buf[0] is NULL)" |
| 105 |
) |
| 106 |
|
| 107 |
sw_fmt: lib.AVPixelFormat |
| 108 |
device_type: cython.int |
| 109 |
device_id: cython.int |
| 110 |
|
| 111 |
if self.frame.ptr.hw_frames_ctx: |
| 112 |
if cython.cast(lib.AVPixelFormat, self.frame.ptr.format) != get_pix_fmt( |
| 113 |
b"cuda" |
| 114 |
): |
| 115 |
raise NotImplementedError( |
| 116 |
"DLPack export is only implemented for CUDA hw frames" |
| 117 |
) |
| 118 |
if stream is not None: |
| 119 |
raise NotImplementedError( |
| 120 |
"CUDA stream synchronization is not supported. " |
| 121 |
"Pass stream=None and synchronize before calling __dlpack__." |
| 122 |
) |
| 123 |
|
| 124 |
frames_ctx: cython.pointer[lib.AVHWFramesContext] = cython.cast( |
| 125 |
cython.pointer[lib.AVHWFramesContext], self.frame.ptr.hw_frames_ctx.data |
| 126 |
) |
| 127 |
sw_fmt = frames_ctx.sw_format |
| 128 |
device_type = kCuda |
| 129 |
device_id = self.frame._device_id |
| 130 |
else: |
| 131 |
sw_fmt = cython.cast(lib.AVPixelFormat, self.frame.ptr.format) |
| 132 |
device_type = kCPU |
| 133 |
device_id = 0 |
| 134 |
|
| 135 |
line_size = self.line_size |
| 136 |
if line_size < 0: |
| 137 |
raise NotImplementedError( |
| 138 |
"negative linesize is not supported for DLPack export" |
| 139 |
) |
| 140 |
|
| 141 |
nv12 = get_pix_fmt(b"nv12") |
| 142 |
p010le = get_pix_fmt(b"p010le") |
| 143 |
p016le = get_pix_fmt(b"p016le") |
| 144 |
|
| 145 |
ndim, bits, itemsize = cython.declare(cython.int) |
| 146 |
s0, s1, s2 = cython.declare(int64_t) |
| 147 |
st0, st1, st2 = cython.declare(int64_t) |
| 148 |
|
| 149 |
if sw_fmt == nv12: |
| 150 |
itemsize = 1 |
| 151 |
bits = 8 |
| 152 |
if self.index == 0: |
| 153 |
ndim = 2 |
| 154 |
s0 = self.frame.ptr.height |
| 155 |
s1 = self.frame.ptr.width |
| 156 |
st0 = line_size |
| 157 |
st1 = 1 |
| 158 |
elif self.index == 1: |
| 159 |
ndim = 3 |
| 160 |
s0 = self.frame.ptr.height // 2 |
| 161 |
s1 = self.frame.ptr.width // 2 |
| 162 |
s2 = 2 |
| 163 |
st0 = line_size |
| 164 |
st1 = 2 |
| 165 |
st2 = 1 |
| 166 |
else: |
| 167 |
raise ValueError("invalid plane index for NV12") |
| 168 |
elif sw_fmt == p010le or sw_fmt == p016le: |
| 169 |
itemsize = 2 |
| 170 |
bits = 16 |
| 171 |
if line_size % itemsize: |
| 172 |
raise ValueError("linesize is not aligned to dtype") |
| 173 |
if self.index == 0: |
| 174 |
ndim = 2 |
| 175 |
s0 = self.frame.ptr.height |
| 176 |
s1 = self.frame.ptr.width |
| 177 |
st0 = line_size // itemsize |
| 178 |
st1 = 1 |
| 179 |
elif self.index == 1: |
| 180 |
ndim = 3 |
| 181 |
s0 = self.frame.ptr.height // 2 |
| 182 |
s1 = self.frame.ptr.width // 2 |
| 183 |
s2 = 2 |
| 184 |
st0 = line_size // itemsize |
| 185 |
st1 = 2 |
| 186 |
st2 = 1 |
| 187 |
else: |
| 188 |
raise ValueError("invalid plane index for P010/P016") |
| 189 |
else: |
| 190 |
raise NotImplementedError("unsupported sw_format for DLPack export") |
| 191 |
|
| 192 |
frame_ref: cython.pointer[lib.AVFrame] = lib.av_frame_alloc() |
| 193 |
if frame_ref == cython.NULL: |
| 194 |
raise MemoryError("av_frame_alloc() failed") |
| 195 |
err_check(lib.av_frame_ref(frame_ref, self.frame.ptr)) |
| 196 |
|
| 197 |
shape = cython.cast( |
| 198 |
cython.pointer[int64_t], malloc(ndim * cython.sizeof(int64_t)) |
| 199 |
) |
| 200 |
strides = cython.cast( |
| 201 |
cython.pointer[int64_t], malloc(ndim * cython.sizeof(int64_t)) |
| 202 |
) |
| 203 |
if shape == cython.NULL or strides == cython.NULL: |
| 204 |
if shape != cython.NULL: |
| 205 |
free(shape) |
| 206 |
if strides != cython.NULL: |
| 207 |
free(strides) |
| 208 |
lib.av_frame_free(cython.address(frame_ref)) |
| 209 |
raise MemoryError("malloc() failed") |
| 210 |
|
| 211 |
if ndim == 2: |
| 212 |
shape[0] = s0 |
| 213 |
shape[1] = s1 |
| 214 |
strides[0] = st0 |
| 215 |
strides[1] = st1 |
| 216 |
else: |
| 217 |
shape[0] = s0 |
| 218 |
shape[1] = s1 |
| 219 |
shape[2] = s2 |
| 220 |
strides[0] = st0 |
| 221 |
strides[1] = st1 |
| 222 |
strides[2] = st2 |
| 223 |
|
| 224 |
ctx = cython.cast( |
| 225 |
cython.pointer[cython.p_void], malloc(3 * cython.sizeof(cython.p_void)) |
| 226 |
) |
| 227 |
if ctx == cython.NULL: |
| 228 |
free(shape) |
| 229 |
free(strides) |
| 230 |
lib.av_frame_free(cython.address(frame_ref)) |
| 231 |
raise MemoryError("malloc() failed") |
| 232 |
|
| 233 |
ctx[0] = cython.cast(cython.p_void, frame_ref) |
| 234 |
ctx[1] = cython.cast(cython.p_void, shape) |
| 235 |
ctx[2] = cython.cast(cython.p_void, strides) |
| 236 |
|
| 237 |
managed = cython.cast( |
| 238 |
cython.pointer[DLManagedTensor], malloc(cython.sizeof(DLManagedTensor)) |
| 239 |
) |
| 240 |
if managed == cython.NULL: |
| 241 |
free(ctx) |
| 242 |
free(shape) |
| 243 |
free(strides) |
| 244 |
lib.av_frame_free(cython.address(frame_ref)) |
| 245 |
raise MemoryError("malloc() failed") |
| 246 |
|
| 247 |
managed.dl_tensor.data = cython.cast(cython.p_void, frame_ref.data[self.index]) |
| 248 |
managed.dl_tensor.device_type = device_type |
| 249 |
managed.dl_tensor.device_id = device_id |
| 250 |
managed.dl_tensor.ndim = ndim |
| 251 |
managed.dl_tensor.dtype = DLDataType(code=1, bits=bits, lanes=1) |
| 252 |
managed.dl_tensor.shape = shape |
| 253 |
managed.dl_tensor.strides = strides |
| 254 |
managed.dl_tensor.byte_offset = 0 |
| 255 |
managed.manager_ctx = cython.cast(cython.p_void, ctx) |
| 256 |
managed.deleter = _dlpack_managed_tensor_deleter |
| 257 |
|
| 258 |
try: |
| 259 |
capsule = PyCapsule_New( |
| 260 |
cython.cast(cython.p_void, managed), |
| 261 |
b"dltensor", |
| 262 |
_dlpack_capsule_destructor, |
| 263 |
) |
| 264 |
except Exception: |
| 265 |
_dlpack_managed_tensor_deleter(managed) |
| 266 |
raise |
| 267 |
|
| 268 |
return capsule |
| 269 |
|
| 270 |
|
| 271 |
@cython.cfunc |
| 272 |
@cython.nogil |
| 273 |
@cython.exceptval(check=False) |
| 274 |
def _dlpack_managed_tensor_deleter( |
| 275 |
managed: cython.pointer[DLManagedTensor], |
| 276 |
) -> cython.void: |
| 277 |
if managed == cython.NULL: |
| 278 |
return |
| 279 |
ctx = cython.cast(cython.pointer[cython.p_void], managed.manager_ctx) |
| 280 |
if ctx != cython.NULL: |
| 281 |
frame_ref = cython.cast(cython.pointer[lib.AVFrame], ctx[0]) |
| 282 |
shape = cython.cast(cython.pointer[int64_t], ctx[1]) |
| 283 |
strides = cython.cast(cython.pointer[int64_t], ctx[2]) |
| 284 |
|
| 285 |
if frame_ref != cython.NULL: |
| 286 |
lib.av_frame_free(cython.address(frame_ref)) |
| 287 |
if shape != cython.NULL: |
| 288 |
free(shape) |
| 289 |
if strides != cython.NULL: |
| 290 |
free(strides) |
| 291 |
free(ctx) |
| 292 |
|
| 293 |
free(managed) |
| 294 |
|
| 295 |
|
| 296 |
@cython.cfunc |
| 297 |
@cython.exceptval(check=False) |
| 298 |
def _dlpack_capsule_destructor(capsule: object) -> cython.void: |
| 299 |
if PyCapsule_IsValid(capsule, b"dltensor"): |
| 300 |
managed = cython.cast( |
| 301 |
cython.pointer[DLManagedTensor], |
| 302 |
PyCapsule_GetPointer(capsule, b"dltensor"), |
| 303 |
) |
| 304 |
if managed != cython.NULL: |
| 305 |
managed.deleter(managed) |