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
1 change: 0 additions & 1 deletion av/codec/context.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ cdef class CodecContext:
cdef _assert_not_open(self, name)

# Public API.
cdef readonly bint is_open
cdef readonly Codec codec
cdef readonly HWAccel hwaccel
cdef public dict options
Expand Down
20 changes: 12 additions & 8 deletions av/codec/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ def __cinit__(self, sentinel=None, *args, **kwargs):

self.options = {}
self.stream_index = -1 # This is set by the container immediately.
self.is_open = False

@property
def supported_options(self):
Expand Down Expand Up @@ -289,7 +288,7 @@ def _init(

@cython.cfunc
def _assert_not_open(self, name):
if self.is_open:
if lib.avcodec_is_open(self.ptr):
raise RuntimeError(f"Cannot change {name} after codec is open.")

@property
Expand Down Expand Up @@ -392,9 +391,15 @@ def is_decoder(self):
return False
return lib.av_codec_is_decoder(self.ptr.codec)

@property
def is_open(self):
if self.ptr is cython.NULL:
return False
return bool(lib.avcodec_is_open(self.ptr))

@cython.ccall
def open(self, strict: cython.bint = True):
if self.is_open:
if lib.avcodec_is_open(self.ptr):
if strict:
raise ValueError("CodecContext is already open.")
return
Expand All @@ -419,7 +424,6 @@ def open(self, strict: cython.bint = True):
lib.avcodec_open2(self.ptr, self.codec.ptr, cython.address(options.ptr)),
f'avcodec_open2("{self.codec.name}", {self.options})',
)
self.is_open = True
self.options = dict(options)

def __dealloc__(self):
Expand Down Expand Up @@ -659,7 +663,7 @@ def _prepare_and_time_rebase_frames_for_encode(self, frame: Frame):
# context. Encoders like h264_nvenc require hw_frames_ctx to be set before
# avcodec_open2, so adopt the frame's if we don't already have one.
if (
not self.is_open
not lib.avcodec_is_open(self.ptr)
and frame is not None
and frame.ptr.hw_frames_ctx != cython.NULL
and self.ptr.hw_frames_ctx == cython.NULL
Expand Down Expand Up @@ -760,7 +764,7 @@ def flush_buffers(self):
when seeking or when switching to a different stream.

"""
if self.is_open:
if lib.avcodec_is_open(self.ptr):
with cython.nogil:
lib.avcodec_flush_buffers(self.ptr)

Expand Down Expand Up @@ -923,7 +927,7 @@ def thread_count(self):

@thread_count.setter
def thread_count(self, value: cython.int):
if self.is_open:
if lib.avcodec_is_open(self.ptr):
raise RuntimeError("Cannot change thread_count after codec is open.")
self.ptr.thread_count = value

Expand All @@ -938,7 +942,7 @@ def thread_type(self):

@thread_type.setter
def thread_type(self, value):
if self.is_open:
if lib.avcodec_is_open(self.ptr):
raise RuntimeError("Cannot change thread_type after codec is open.")
if type(value) is int:
self.ptr.thread_type = value
Expand Down
1 change: 1 addition & 0 deletions include/avcodec.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ cdef extern from "libavcodec/avcodec.h" nogil:
cdef const AVCodecDescriptor* avcodec_descriptor_get_by_name(const char *name)
cdef const char* avcodec_get_name(AVCodecID id)
cdef int avcodec_open2(AVCodecContext *ctx, const AVCodec *codec, AVDictionary **options)
cdef int avcodec_is_open(AVCodecContext *ctx)
cdef enum AVPacketSideDataType:
AV_PKT_DATA_NEW_EXTRADATA
AV_PKT_DATA_DISPLAYMATRIX
Expand Down
Loading