File: opaque.pyx

package info (click to toggle)
python-av 14.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,664 kB
  • sloc: python: 4,712; sh: 175; ansic: 174; makefile: 123
file content (32 lines) | stat: -rw-r--r-- 833 bytes parent folder | download
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
cimport libav as lib
from libc.stdint cimport uint8_t

from uuid import uuid4


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:
    """A container that holds references to Python objects, indexed by uuid"""

    def __cinit__(self):
        self._by_name = {}

    cdef lib.AVBufferRef *add(self, v):
        cdef bytes uuid = str(uuid4()).encode("utf-8")
        cdef lib.AVBufferRef *ref = lib.av_buffer_create(uuid, len(uuid), &key_free, NULL, 0)
        self._by_name[uuid] = v
        return ref

    cdef object get(self, bytes name):
        return self._by_name.get(name)

    cdef object pop(self, bytes name):
        return self._by_name.pop(name)


cdef opaque_container = OpaqueContainer()