11import cython
2- from cython .cimports .av .bytesource import ByteSource , bytesource
32from cython .cimports .cpython import PyBUF_WRITABLE , PyBuffer_FillInfo
3+ from cython .cimports .cpython .buffer import (
4+ PyBUF_SIMPLE ,
5+ PyBuffer_Release ,
6+ PyObject_CheckBuffer ,
7+ PyObject_GetBuffer ,
8+ )
49from cython .cimports .libc .string import memcpy
510
611
12+ @cython .cclass
13+ class ByteSource :
14+ def __cinit__ (self , owner ):
15+ self .owner = owner
16+
17+ try :
18+ self .ptr = owner
19+ except TypeError :
20+ pass
21+ else :
22+ self .length = len (owner )
23+ return
24+
25+ if PyObject_CheckBuffer (owner ):
26+ # Can very likely use PyBUF_ND instead of PyBUF_SIMPLE
27+ res = PyObject_GetBuffer (owner , cython .address (self .view ), PyBUF_SIMPLE )
28+ if not res :
29+ self .has_view = True
30+ self .ptr = cython .cast (cython .p_uchar , self .view .buf )
31+ self .length = self .view .len
32+ return
33+
34+ raise TypeError ("expected bytes, bytearray or memoryview" )
35+
36+ def __dealloc__ (self ):
37+ if self .has_view :
38+ PyBuffer_Release (cython .address (self .view ))
39+
40+
41+ @cython .cfunc
42+ def bytesource (obj , allow_none : cython .bint = False ) -> ByteSource | None :
43+ if allow_none and obj is None :
44+ return None
45+ elif isinstance (obj , ByteSource ):
46+ return obj
47+ else :
48+ return ByteSource (obj )
49+
50+
751@cython .cclass
852class Buffer :
953 """A base class for PyAV objects which support the buffer protocol, such
@@ -29,7 +73,6 @@ def __getbuffer__(self, view: cython.pointer[Py_buffer], flags: cython.int):
2973
3074 @property
3175 def buffer_size (self ):
32- """The size of the buffer in bytes."""
3376 return self ._buffer_size ()
3477
3578 @property
0 commit comments