| 1 |
import cython |
| 2 |
from 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 |
) |
| 9 |
from cython.cimports.libc.string import memcpy |
| 10 |
|
| 11 |
|
| 12 |
@cython.final |
| 13 |
@cython.cclass |
| 14 |
class ByteSource: |
| 15 |
def __cinit__(self, owner): |
| 16 |
self.owner = owner |
| 17 |
|
| 18 |
try: |
| 19 |
self.ptr = owner |
| 20 |
except TypeError: |
| 21 |
pass |
| 22 |
else: |
| 23 |
self.length = len(owner) |
| 24 |
return |
| 25 |
|
| 26 |
if PyObject_CheckBuffer(owner): |
| 27 |
# Can very likely use PyBUF_ND instead of PyBUF_SIMPLE |
| 28 |
res = PyObject_GetBuffer(owner, cython.address(self.view), PyBUF_SIMPLE) |
| 29 |
if not res: |
| 30 |
self.has_view = True |
| 31 |
self.ptr = cython.cast(cython.p_uchar, self.view.buf) |
| 32 |
self.length = self.view.len |
| 33 |
return |
| 34 |
|
| 35 |
raise TypeError("expected bytes, bytearray or memoryview") |
| 36 |
|
| 37 |
def __dealloc__(self): |
| 38 |
if self.has_view: |
| 39 |
PyBuffer_Release(cython.address(self.view)) |
| 40 |
|
| 41 |
|
| 42 |
@cython.cfunc |
| 43 |
def bytesource(obj, allow_none: cython.bint = False) -> ByteSource | None: |
| 44 |
if allow_none and obj is None: |
| 45 |
return None |
| 46 |
elif isinstance(obj, ByteSource): |
| 47 |
return obj |
| 48 |
else: |
| 49 |
return ByteSource(obj) |
| 50 |
|
| 51 |
|
| 52 |
@cython.cclass |
| 53 |
class Buffer: |
| 54 |
"""A base class for PyAV objects which support the buffer protocol, such |
| 55 |
as :class:`.Packet` and :class:`.Plane`. |
| 56 |
|
| 57 |
""" |
| 58 |
|
| 59 |
@cython.cfunc |
| 60 |
def _buffer_size(self) -> cython.size_t: |
| 61 |
return 0 |
| 62 |
|
| 63 |
def _buffer_ptr(self) -> cython.p_void: |
| 64 |
return cython.NULL |
| 65 |
|
| 66 |
def _buffer_writable(self) -> cython.bint: |
| 67 |
return True |
| 68 |
|
| 69 |
def __getbuffer__(self, view: cython.pointer[Py_buffer], flags: cython.int): |
| 70 |
if flags & PyBUF_WRITABLE and not self._buffer_writable(): |
| 71 |
raise ValueError("buffer is not writable") |
| 72 |
|
| 73 |
PyBuffer_FillInfo(view, self, self._buffer_ptr(), self._buffer_size(), 0, flags) |
| 74 |
|
| 75 |
@property |
| 76 |
def buffer_size(self): |
| 77 |
return self._buffer_size() |
| 78 |
|
| 79 |
@property |
| 80 |
def buffer_ptr(self): |
| 81 |
"""The memory address of the buffer.""" |
| 82 |
return cython.cast(cython.size_t, self._buffer_ptr()) |
| 83 |
|
| 84 |
def update(self, input): |
| 85 |
"""Replace the data in this object with the given buffer. |
| 86 |
|
| 87 |
Accepts anything that supports the `buffer protocol <https://docs.python.org/3/c-api/buffer.html>`_, |
| 88 |
e.g. bytes, NumPy arrays, other :class:`Buffer` objects, etc.. |
| 89 |
|
| 90 |
""" |
| 91 |
if not self._buffer_writable(): |
| 92 |
raise ValueError("buffer is not writable") |
| 93 |
|
| 94 |
source: ByteSource = bytesource(input) |
| 95 |
size: cython.size_t = self._buffer_size() |
| 96 |
|
| 97 |
if source.length != size: |
| 98 |
raise ValueError(f"got {source.length} bytes; need {size} bytes") |
| 99 |
|
| 100 |
memcpy(self._buffer_ptr(), source.ptr, size) |