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
36 changes: 8 additions & 28 deletions av/container/core.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@ from av.dictionary cimport _Dictionary
from av.format cimport ContainerFormat
from av.stream cimport Stream

# Since there are multiple objects that need to refer to a valid context, we
# need this intermediate proxy object so that there aren't any reference cycles
# and the pointer can be freed when everything that depends upon it is deleted.
cdef class ContainerProxy(object):

cdef bint writeable
cdef lib.AVFormatContext *ptr
cdef class Container(object):

cdef seek(self, int stream_index, offset, str whence, bint backward, bint any_frame)
cdef flush_buffers(self)
cdef readonly bint writeable
cdef lib.AVFormatContext *ptr

# Copies from Container.
cdef object name
cdef str metadata_encoding
cdef str metadata_errors
cdef readonly object name
cdef readonly str metadata_encoding
cdef readonly str metadata_errors

# File-like source.
cdef object file
cdef readonly object file
cdef object fread
cdef object fwrite
cdef object fseek
Expand All @@ -36,27 +30,13 @@ cdef class ContainerProxy(object):
cdef bint pos_is_valid
cdef bint input_was_opened

cdef int err_check(self, int value) except -1


cdef class Container(object):

cdef readonly object name
cdef readonly object file

cdef readonly bint writeable

cdef readonly ContainerFormat format

cdef readonly dict options
cdef readonly dict container_options
cdef readonly list stream_options

cdef ContainerProxy proxy
cdef object __weakref__

cdef readonly StreamContainer streams
cdef readonly dict metadata

cdef readonly str metadata_encoding
cdef readonly str metadata_errors
cdef int err_check(self, int value) except -1
134 changes: 40 additions & 94 deletions av/container/core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,37 @@ ctypedef int64_t (*seek_func_t)(void *opaque, int64_t offset, int whence) nogil
cdef object _cinit_sentinel = object()


cdef class ContainerProxy(object):

def __init__(self, sentinel, Container container):
cdef class Container(object):

self.input_was_opened = False
cdef int res
def __cinit__(self, sentinel, file_, format_name, options, container_options, stream_options, metadata_encoding, metadata_errors):

if sentinel is not _cinit_sentinel:
raise RuntimeError('cannot construct ContainerProxy')
raise RuntimeError('cannot construct base Container')

self.writeable = isinstance(self, OutputContainer)
if not self.writeable and not isinstance(self, InputContainer):
raise RuntimeError('Container cannot be directly extended.')

if isinstance(file_, basestring):
self.name = file_
else:
self.name = getattr(file_, 'name', '<none>')
if not isinstance(self.name, basestring):
raise TypeError("File's name attribute must be string-like.")
self.file = file_

self.options = dict(options or ())
self.container_options = dict(container_options or ())
self.stream_options = [dict(x) for x in stream_options or ()]

self.metadata_encoding = metadata_encoding
self.metadata_errors = metadata_errors

if format_name is not None:
self.format = ContainerFormat(format_name)

# Copy key attributes.
self.file = container.file
self.metadata_encoding = container.metadata_encoding
self.metadata_errors = container.metadata_errors
self.name = container.name
self.writeable = container.writeable
self.input_was_opened = False
cdef int res

cdef bytes name_obj = fsencode(self.name) if isinstance(self.name, unicode) else self.name
cdef char *name = name_obj
Expand All @@ -54,7 +69,7 @@ cdef class ContainerProxy(object):
cdef lib.AVOutputFormat *ofmt
if self.writeable:

ofmt = container.format.optr if container.format else lib.av_guess_format(NULL, name, NULL)
ofmt = self.format.optr if self.format else lib.av_guess_format(NULL, name, NULL)
if ofmt == NULL:
raise ValueError("Could not determine output format")

Expand Down Expand Up @@ -115,22 +130,27 @@ cdef class ContainerProxy(object):
self.ptr.pb = self.iocontext

cdef lib.AVInputFormat *ifmt
cdef _Dictionary options
cdef _Dictionary c_options
if not self.writeable:
ifmt = container.format.iptr if container.format else NULL

options = Dictionary(container.options, container.container_options)
ifmt = self.format.iptr if self.format else NULL

c_options = Dictionary(self.options, self.container_options)
with nogil:
res = lib.avformat_open_input(
&self.ptr,
name,
ifmt,
&options.ptr
&c_options.ptr
)
self.err_check(res)
self.input_was_opened = True

if format_name is None:
self.format = build_container_format(self.ptr.iformat, self.ptr.oformat)

def __dealloc__(self):

with nogil:

# Let FFmpeg close input if it was fully opened.
Expand All @@ -151,89 +171,15 @@ cdef class ContainerProxy(object):
# Finish releasing the whole structure.
lib.avformat_free_context(self.ptr)

cdef seek(self, int stream_index, offset, str whence, bint backward, bint any_frame):

# We used to take floats here and assume they were in seconds. This
# was super confusing, so lets go in the complete opposite direction.
if not isinstance(offset, (int, long)):
raise TypeError('Container.seek only accepts integer offset.', type(offset))
cdef int64_t c_offset = offset

cdef int flags = 0
cdef int ret

if whence == 'frame':
flags |= lib.AVSEEK_FLAG_FRAME
elif whence == 'byte':
flags |= lib.AVSEEK_FLAG_BYTE
elif whence != 'time':
raise ValueError("whence must be one of 'frame', 'byte', or 'time'.", whence)

if backward:
flags |= lib.AVSEEK_FLAG_BACKWARD

if any_frame:
flags |= lib.AVSEEK_FLAG_ANY

with nogil:
ret = lib.av_seek_frame(self.ptr, stream_index, c_offset, flags)
err_check(ret)

self.flush_buffers()

cdef flush_buffers(self):
cdef unsigned int i
cdef lib.AVStream *stream

with nogil:
for i in range(self.ptr.nb_streams):
stream = self.ptr.streams[i]
if stream.codec and stream.codec.codec and stream.codec.codec_id != lib.AV_CODEC_ID_NONE:
lib.avcodec_flush_buffers(stream.codec)
def __repr__(self):
return '<av.%s %r>' % (self.__class__.__name__, self.file or self.name)

cdef int err_check(self, int value) except -1:
return err_check(value, filename=self.name)


cdef class Container(object):

def __cinit__(self, sentinel, file_, format_name, options, container_options, stream_options, metadata_encoding, metadata_errors):

if sentinel is not _cinit_sentinel:
raise RuntimeError('cannot construct base Container')

self.writeable = isinstance(self, OutputContainer)
if not self.writeable and not isinstance(self, InputContainer):
raise RuntimeError('Container cannot be extended except')

if isinstance(file_, basestring):
self.name = file_
else:
self.name = getattr(file_, 'name', '<none>')
if not isinstance(self.name, basestring):
raise TypeError("File's name attribute must be string-like.")
self.file = file_

if format_name is not None:
self.format = ContainerFormat(format_name)

self.options = dict(options or ())
self.container_options = dict(container_options or ())
self.stream_options = [dict(x) for x in stream_options or ()]

self.metadata_encoding = metadata_encoding
self.metadata_errors = metadata_errors
self.proxy = ContainerProxy(_cinit_sentinel, self)

if format_name is None:
self.format = build_container_format(self.proxy.ptr.iformat, self.proxy.ptr.oformat)

def __repr__(self):
return '<av.%s %r>' % (self.__class__.__name__, self.file or self.name)

def dumps_format(self):
with LogCapture() as logs:
lib.av_dump_format(self.proxy.ptr, 0, "", isinstance(self, OutputContainer))
lib.av_dump_format(self.ptr, 0, "", isinstance(self, OutputContainer))
return ''.join(log[2] for log in logs)


Expand Down
3 changes: 2 additions & 1 deletion av/container/input.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ from av.stream cimport Stream


cdef class InputContainer(Container):
pass

cdef flush_buffers(self)
Loading