-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathopaque.pyx
More file actions
50 lines (34 loc) · 1.39 KB
/
opaque.pyx
File metadata and controls
50 lines (34 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
cimport libav as lib
from libc.stdint cimport uint8_t, uintptr_t
from libc.string cimport memcpy
cdef void noop_free(void *opaque, uint8_t *data) noexcept nogil:
pass
cdef void key_free(void *opaque, uint8_t *data) noexcept nogil:
cdef char *name = <char *>data
with gil:
opaque_container.pop(name)
cdef class OpaqueContainer:
def __cinit__(self):
self._objects = {}
cdef lib.AVBufferRef *add(self, object v):
# Use object's memory address as key
cdef uintptr_t key = <uintptr_t><long long>id(v)
self._objects[key] = v
cdef uint8_t *data = <uint8_t *>lib.av_malloc(sizeof(uintptr_t))
if data == NULL:
raise MemoryError("Failed to allocate memory for key")
memcpy(data, &key, sizeof(uintptr_t))
# Create the buffer with our free callback
cdef lib.AVBufferRef *buffer_ref = lib.av_buffer_create(
data, sizeof(uintptr_t), key_free, NULL, 0
)
if buffer_ref == NULL:
raise MemoryError("Failed to create AVBufferRef")
return buffer_ref
cdef object get(self, char *name):
cdef uintptr_t key = (<uintptr_t *>name)[0]
return self._objects.get(key)
cdef object pop(self, char *name):
cdef uintptr_t key = (<uintptr_t *>name)[0]
return self._objects.pop(key, None)
cdef OpaqueContainer opaque_container = OpaqueContainer()