diff --git a/av/dictionary.pxd b/av/dictionary.pxd index 1c59df448..47c100adc 100644 --- a/av/dictionary.pxd +++ b/av/dictionary.pxd @@ -2,9 +2,7 @@ cimport libav as lib cdef class _Dictionary: - cdef lib.AVDictionary *ptr - cpdef _Dictionary copy(self) diff --git a/av/dictionary.py b/av/dictionary.py new file mode 100644 index 000000000..2080266ed --- /dev/null +++ b/av/dictionary.py @@ -0,0 +1,62 @@ +from collections.abc import MutableMapping + +import cython +from cython.cimports.av.error import err_check + + +@cython.cclass +class _Dictionary: + def __cinit__(self, *args, **kwargs): + for arg in args: + self.update(arg) + if kwargs: + self.update(kwargs) + + def __dealloc__(self): + if self.ptr != cython.NULL: + lib.av_dict_free(cython.address(self.ptr)) + + def __getitem__(self, key: cython.str): + element = cython.declare( + cython.pointer[lib.AVDictionaryEntry], + lib.av_dict_get(self.ptr, key, cython.NULL, 0), + ) + if element == cython.NULL: + raise KeyError(key) + return element.value + + def __setitem__(self, key: cython.str, value: cython.str): + err_check(lib.av_dict_set(cython.address(self.ptr), key, value, 0)) + + def __delitem__(self, key: cython.str): + err_check(lib.av_dict_set(cython.address(self.ptr), key, cython.NULL, 0)) + + def __len__(self): + return err_check(lib.av_dict_count(self.ptr)) + + def __iter__(self): + element = cython.declare(cython.pointer[lib.AVDictionaryEntry], cython.NULL) + while True: + element = lib.av_dict_get(self.ptr, "", element, lib.AV_DICT_IGNORE_SUFFIX) + if element == cython.NULL: + break + yield element.key + + def __repr__(self): + return f"bv.Dictionary({dict(self)!r})" + + def copy(self): + other = cython.declare(_Dictionary, Dictionary()) + lib.av_dict_copy(cython.address(other.ptr), self.ptr, 0) + return other + + +class Dictionary(_Dictionary, MutableMapping): + pass + + +@cython.cfunc +def wrap_dictionary(input_: cython.pointer[lib.AVDictionary]) -> _Dictionary: + output = cython.declare(_Dictionary, Dictionary()) + output.ptr = input_ + return output diff --git a/av/dictionary.pyx b/av/dictionary.pyx deleted file mode 100644 index 15de38381..000000000 --- a/av/dictionary.pyx +++ /dev/null @@ -1,57 +0,0 @@ -from collections.abc import MutableMapping - -from av.error cimport err_check - - -cdef class _Dictionary: - def __cinit__(self, *args, **kwargs): - for arg in args: - self.update(arg) - if kwargs: - self.update(kwargs) - - def __dealloc__(self): - if self.ptr != NULL: - lib.av_dict_free(&self.ptr) - - def __getitem__(self, str key): - cdef lib.AVDictionaryEntry *element = lib.av_dict_get(self.ptr, key, NULL, 0) - if element != NULL: - return element.value - else: - raise KeyError(key) - - def __setitem__(self, str key, str value): - err_check(lib.av_dict_set(&self.ptr, key, value, 0)) - - def __delitem__(self, str key): - err_check(lib.av_dict_set(&self.ptr, key, NULL, 0)) - - def __len__(self): - return err_check(lib.av_dict_count(self.ptr)) - - def __iter__(self): - cdef lib.AVDictionaryEntry *element = NULL - while True: - element = lib.av_dict_get(self.ptr, "", element, lib.AV_DICT_IGNORE_SUFFIX) - if element == NULL: - break - yield element.key - - def __repr__(self): - return f"av.Dictionary({dict(self)!r})" - - cpdef _Dictionary copy(self): - cdef _Dictionary other = Dictionary() - lib.av_dict_copy(&other.ptr, self.ptr, 0) - return other - - -class Dictionary(_Dictionary, MutableMapping): - pass - - -cdef _Dictionary wrap_dictionary(lib.AVDictionary *input_): - cdef _Dictionary output = Dictionary() - output.ptr = input_ - return output diff --git a/av/plane.pxd b/av/plane.pxd index df3847d7b..2066bfea8 100644 --- a/av/plane.pxd +++ b/av/plane.pxd @@ -3,9 +3,7 @@ from av.frame cimport Frame cdef class Plane(Buffer): - cdef Frame frame cdef int index - cdef size_t _buffer_size(self) cdef void* _buffer_ptr(self) diff --git a/av/plane.pyx b/av/plane.py similarity index 72% rename from av/plane.pyx rename to av/plane.py index c733b20a7..f84a99c3d 100644 --- a/av/plane.pyx +++ b/av/plane.py @@ -1,12 +1,15 @@ +import cython -cdef class Plane(Buffer): + +@cython.cclass +class Plane(Buffer): """ Base class for audio and video planes. See also :class:`~av.audio.plane.AudioPlane` and :class:`~av.video.plane.VideoPlane`. """ - def __cinit__(self, Frame frame, int index): + def __cinit__(self, frame: Frame, index: cython.int): self.frame = frame self.index = index @@ -16,5 +19,6 @@ def __repr__(self): f"buffer_ptr=0x{self.buffer_ptr:x}; at 0x{id(self):x}>" ) - cdef void* _buffer_ptr(self): + @cython.cfunc + def _buffer_ptr(self) -> cython.p_void: return self.frame.ptr.extended_data[self.index]