88from cython .cimports .av .sidedata .sidedata import get_display_rotation
99from cython .cimports .av .utils import check_ndarray
1010from cython .cimports .av .video .format import get_pix_fmt , get_video_format
11- from cython .cimports .av .video .plane import VideoPlane
11+ from cython .cimports .av .video .plane import DLManagedTensor , VideoPlane , kCPU , kCuda
1212from cython .cimports .cpython .exc import PyErr_Clear
1313from cython .cimports .cpython .pycapsule import (
1414 PyCapsule_GetPointer ,
1515 PyCapsule_IsValid ,
1616 PyCapsule_SetName ,
1717)
1818from cython .cimports .cpython .ref import Py_DECREF , Py_INCREF
19- from cython .cimports .dlpack import DLManagedTensor , kDLCPU , kDLCUDA , kDLUInt
2019from cython .cimports .libc .stdint import int64_t , uint8_t
2120
2221
2322@cython .cclass
2423class CudaContext :
25- def __cinit__ (self , device_id = 0 , primary_ctx = True ):
26- self ._device_id = int ( device_id )
27- self ._primary_ctx = bool ( primary_ctx )
24+ def __cinit__ (self , device_id : cython . int = 0 , primary_ctx : cython . bint = True ):
25+ self .device_id = device_id
26+ self .primary_ctx = primary_ctx
2827 self ._device_ref = cython .NULL
2928 self ._frames_cache = {}
3029
@@ -44,27 +43,18 @@ def __dealloc__(self):
4443 lib .av_buffer_unref (cython .address (ref ))
4544 self ._device_ref = cython .NULL
4645
47- @property
48- def device_id (self ) -> int :
49- return self ._device_id
50-
51- @property
52- def primary_ctx (self ) -> bool :
53- return self ._primary_ctx
54-
5546 @cython .cfunc
5647 def _get_device_ref (self ) -> cython .pointer [lib .AVBufferRef ]:
5748 device_ref : cython .pointer [lib .AVBufferRef ] = self ._device_ref
5849 if device_ref != cython .NULL :
5950 return device_ref
6051
6152 device_ref = cython .NULL
62- device_bytes = str ( int ( self .device_id )) .encode ()
53+ device_bytes = f" { self .device_id } " .encode ()
6354 c_device : cython .p_char = device_bytes
6455 options : Dictionary = Dictionary (
6556 {"primary_ctx" : "1" if self .primary_ctx else "0" }
6657 )
67-
6858 err_check (
6959 lib .av_hwdevice_ctx_create (
7060 cython .address (device_ref ),
@@ -74,7 +64,6 @@ def _get_device_ref(self) -> cython.pointer[lib.AVBufferRef]:
7464 0 ,
7565 )
7666 )
77-
7867 self ._device_ref = device_ref
7968 return device_ref
8069
@@ -426,10 +415,6 @@ def __repr__(self):
426415 f"{ self .width } x{ self .height } at 0x{ id (self ):x} >"
427416 )
428417
429- @property
430- def device_id (self ) -> int :
431- return self ._device_id
432-
433418 @property
434419 def planes (self ):
435420 """
@@ -1059,10 +1044,6 @@ def from_numpy_buffer(array, format="rgb24", width=0):
10591044 return frame
10601045
10611046 def _image_fill_pointers_numpy (self , buffer , width , height , linesizes , format ):
1062- c_data : cython .size_t = buffer .ctypes .data
1063- c_ptr : cython .pointer [uint8_t ] = cython .cast (cython .pointer [uint8_t ], c_data )
1064- c_format : lib .AVPixelFormat = get_pix_fmt (format )
1065-
10661047 # If you want to use the numpy notation, then you need to include the following lines at the top of the file:
10671048 # cimport numpy as cnp
10681049 # cnp.import_array()
@@ -1079,9 +1060,9 @@ def _image_fill_pointers_numpy(self, buffer, width, height, linesizes, format):
10791060 # Using buffer.ctypes.data helps avoid any kind of usage of the c-api from
10801061 # numpy, which avoid the need to add numpy as a compile time dependency.
10811062
1082- c_data = buffer .ctypes .data
1083- c_ptr = cython .cast (cython .pointer [uint8_t ], c_data )
1084- c_format = get_pix_fmt (format )
1063+ c_data : cython . Py_ssize_t = buffer .ctypes .data
1064+ c_ptr : cython . pointer [ uint8_t ] = cython .cast (cython .pointer [uint8_t ], c_data )
1065+ c_format : lib . AVPixelFormat = get_pix_fmt (format )
10851066 lib .av_frame_unref (self .ptr )
10861067 self ._np_buffer = None
10871068
@@ -1419,7 +1400,7 @@ def from_dlpack(
14191400 p010le = get_pix_fmt (b"p010le" )
14201401 p016le = get_pix_fmt (b"p016le" )
14211402
1422- if sw_fmt not in { nv12 , p010le , p016le } :
1403+ if sw_fmt not in ( nv12 , p010le , p016le ) :
14231404 raise NotImplementedError ("from_dlpack supports nv12, p010le, p016le only" )
14241405
14251406 expected_bits = 8 if sw_fmt == nv12 else 16
@@ -1437,7 +1418,7 @@ def from_dlpack(
14371418 dev_type1 = m1 .dl_tensor .device_type
14381419 if dev_type0 != dev_type1 :
14391420 raise ValueError ("plane tensors must have the same device_type" )
1440- if dev_type0 not in { kDLCUDA , kDLCPU } :
1421+ if dev_type0 not in ( kCuda , kCPU ) :
14411422 raise NotImplementedError (
14421423 "only CPU and CUDA DLPack tensors are supported"
14431424 )
@@ -1446,7 +1427,7 @@ def from_dlpack(
14461427 dev1 = m1 .dl_tensor .device_id
14471428 if dev0 != dev1 :
14481429 raise ValueError ("plane tensors must be on the same CUDA device" )
1449- if dev_type0 == kDLCUDA :
1430+ if dev_type0 == kCuda :
14501431 if device_id is None :
14511432 device_id = dev0
14521433 elif device_id != dev0 :
@@ -1457,18 +1438,17 @@ def from_dlpack(
14571438 if device_id not in (None , 0 ):
14581439 raise ValueError ("device_id must be 0 for CPU tensors" )
14591440 device_id = 0
1460- if dev_type0 == kDLCPU and (dev0 != 0 or dev1 != 0 ):
1441+ if dev_type0 == kCPU and (dev0 != 0 or dev1 != 0 ):
14611442 raise ValueError ("CPU DLPack tensors must have device_id == 0" )
14621443
14631444 if (
1464- m0 .dl_tensor .dtype .code != kDLUInt
1445+ m0 .dl_tensor .dtype .code != 1
14651446 or m0 .dl_tensor .dtype .bits != expected_bits
14661447 or m0 .dl_tensor .dtype .lanes != 1
14671448 ):
14681449 raise TypeError ("unexpected dtype for plane 0" )
1469-
14701450 if (
1471- m1 .dl_tensor .dtype .code != kDLUInt
1451+ m1 .dl_tensor .dtype .code != 1
14721452 or m1 .dl_tensor .dtype .bits != expected_bits
14731453 or m1 .dl_tensor .dtype .lanes != 1
14741454 ):
@@ -1541,13 +1521,9 @@ def from_dlpack(
15411521 frame = alloc_video_frame ()
15421522 frame .ptr .width = width
15431523 frame .ptr .height = height
1544- if dev_type0 == kDLCUDA :
1545- ctx = cython .declare (CudaContext )
1546- frames_ref = cython .declare (cython .pointer [lib .AVBufferRef ])
1547- if not isinstance (primary_ctx , (bool , int )):
1548- raise TypeError ("primary_ctx must be a bool" )
1549- primary_ctx = bool (primary_ctx )
1550-
1524+ if dev_type0 == kCuda :
1525+ ctx : CudaContext
1526+ frames_ref : cython .pointer [lib .AVBufferRef ]
15511527 if cuda_context is None :
15521528 ctx = CudaContext (device_id = device_id , primary_ctx = primary_ctx )
15531529 else :
@@ -1564,7 +1540,6 @@ def from_dlpack(
15641540 ctx = cython .cast (CudaContext , cuda_context )
15651541
15661542 frames_ref = ctx .get_frames_ctx (sw_fmt , width , height )
1567-
15681543 frame .ptr .format = get_pix_fmt (b"cuda" )
15691544 frame .ptr .hw_frames_ctx = frames_ref
15701545 else :
@@ -1600,7 +1575,6 @@ def from_dlpack(
16001575 m1 = cython .NULL
16011576
16021577 frame ._init_user_attributes ()
1603- frame ._device_id = device_id
16041578 return frame
16051579
16061580 except Exception :
0 commit comments