Skip to content

Commit df1b032

Browse files
committed
Combine av/buffer and av/bytesource
1 parent f462b75 commit df1b032

File tree

9 files changed

+61
-67
lines changed

9 files changed

+61
-67
lines changed

av/buffer.pxd

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
from cpython.buffer cimport Py_buffer
12

2-
cdef class Buffer:
33

4+
cdef class ByteSource:
5+
cdef object owner
6+
cdef bint has_view
7+
cdef Py_buffer view
8+
cdef unsigned char *ptr
9+
cdef size_t length
10+
11+
cdef ByteSource bytesource(object, bint allow_none=*)
12+
13+
cdef class Buffer:
414
cdef size_t _buffer_size(self)
515
cdef void* _buffer_ptr(self)
616
cdef bint _buffer_writable(self)

av/buffer.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
11
import cython
2-
from cython.cimports.av.bytesource import ByteSource, bytesource
32
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+
)
49
from 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
852
class 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

av/bytesource.pxd

Lines changed: 0 additions & 14 deletions
This file was deleted.

av/bytesource.pyx

Lines changed: 0 additions & 43 deletions
This file was deleted.

av/codec/context.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cimport libav as lib
22
from libc.stdint cimport int64_t
33

4-
from av.bytesource cimport ByteSource
4+
from av.buffer cimport ByteSource
55
from av.codec.codec cimport Codec
66
from av.codec.hwaccel cimport HWAccel
77
from av.frame cimport Frame

av/codec/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import cython
44
from cython.cimports import libav as lib
5-
from cython.cimports.av.bytesource import ByteSource, bytesource
5+
from cython.cimports.av.buffer import ByteSource, bytesource
66
from cython.cimports.av.codec.codec import Codec, wrap_codec
77
from cython.cimports.av.dictionary import _Dictionary
88
from cython.cimports.av.error import err_check

av/packet.pxd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ from cython.cimports.libc.stdint import uint8_t
22

33
cimport libav as lib
44

5-
from av.buffer cimport Buffer
6-
from av.bytesource cimport ByteSource
5+
from av.buffer cimport Buffer, ByteSource
76
from av.stream cimport Stream
87

98

av/packet.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import cython
44
from cython.cimports import libav as lib
5-
from cython.cimports.av.buffer import Buffer
6-
from cython.cimports.av.bytesource import ByteSource, bytesource
5+
from cython.cimports.av.buffer import Buffer, ByteSource, bytesource
76
from cython.cimports.av.error import err_check
87
from cython.cimports.av.opaque import noop_free, opaque_container
98
from cython.cimports.av.utils import avrational_to_fraction, to_avrational

av/subtitles/codeccontext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import cython
22
from cython.cimports import libav as lib
3-
from cython.cimports.av.bytesource import ByteSource, bytesource
3+
from cython.cimports.av.buffer import ByteSource, bytesource
44
from cython.cimports.av.error import err_check
55
from cython.cimports.av.packet import Packet
66
from cython.cimports.av.subtitles.subtitle import SubtitleProxy, SubtitleSet

0 commit comments

Comments
 (0)