Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions av/opaque.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
cimport libav as lib
from libc.stdint cimport uint8_t


cdef void noop_free(void *opaque, uint8_t *data) noexcept nogil


cdef class OpaqueContainer:
Expand Down
4 changes: 4 additions & 0 deletions av/opaque.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ 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:
Expand Down
27 changes: 16 additions & 11 deletions av/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import cython
from cython.cimports import libav as lib
from cython.cimports.av.buffer import Buffer
from cython.cimports.av.bytesource import bytesource
from cython.cimports.av.bytesource import ByteSource, bytesource
from cython.cimports.av.error import err_check
from cython.cimports.av.opaque import opaque_container
from cython.cimports.av.opaque import noop_free, opaque_container
from cython.cimports.av.utils import avrational_to_fraction, to_avrational
from cython.cimports.libc.string import memcpy

Expand Down Expand Up @@ -213,24 +213,29 @@ def __dealloc__(self):
def __init__(self, input=None):
size: cython.size_t = 0
source: ByteSource = None
buf: cython.pointer[lib.AVBufferRef]

if input is None:
return

if isinstance(input, int):
size = input
if size:
err_check(lib.av_new_packet(self.ptr, size))
else:
source = bytesource(input)
size = source.length

if size:
err_check(lib.av_new_packet(self.ptr, size))

if source is not None:
self.update(source)
# TODO: Hold onto the source, and copy its pointer
# instead of its data.
# self.source = source
if size:
# Create a buffer that references the source data directly.
# The noop_free callback is used because Python manages the memory
# via self.source keeping the ByteSource alive.
buf = lib.av_buffer_create(source.ptr, size, noop_free, cython.NULL, 0)
if buf == cython.NULL:
raise MemoryError("Could not allocate AVBufferRef")
self.ptr.buf = buf
self.ptr.data = source.ptr
self.ptr.size = size
self.source = source

def __repr__(self):
stream = self._stream.index if self._stream else 0
Expand Down
Loading