diff --git a/av/codec/context.pxd b/av/codec/context.pxd index f247655ff..58bc6a07d 100644 --- a/av/codec/context.pxd +++ b/av/codec/context.pxd @@ -37,6 +37,8 @@ cdef class CodecContext: cpdef encode(self, Frame frame=?) cpdef decode(self, Packet packet=?) + cpdef flush_buffers(self) + # Used by both transcode APIs to setup user-land objects. # TODO: Remove the `Packet` from `_setup_decoded_frame` (because flushing # packets are bogus). It should take all info it needs from the context and/or stream. diff --git a/av/codec/context.pyi b/av/codec/context.pyi index ee81daae5..0477aeb93 100644 --- a/av/codec/context.pyi +++ b/av/codec/context.pyi @@ -1,6 +1,7 @@ from typing import Any, Literal from av.enum import EnumFlag, EnumItem +from av.frame import Frame from av.packet import Packet from .codec import Codec @@ -78,3 +79,6 @@ class CodecContext: def parse( self, raw_input: bytes | bytearray | memoryview | None = None ) -> list[Packet]: ... + def encode(self, frame: Frame | None) -> list[Packet]: ... + def decode(self, packet: Packet | None) -> list[Frame]: ... + def flush_buffers(self) -> None: ... diff --git a/av/codec/context.pyx b/av/codec/context.pyx index e2557e702..d94a08015 100644 --- a/av/codec/context.pyx +++ b/av/codec/context.pyx @@ -510,6 +510,17 @@ cdef class CodecContext: res.append(frame) return res + cpdef flush_buffers(self): + """Reset the internal codec state and discard all internal buffers. + + Should be called before you start decoding from a new position e.g. + when seeking or when switching to a different stream. + + """ + if self.is_open: + with nogil: + lib.avcodec_flush_buffers(self.ptr) + cdef _setup_decoded_frame(self, Frame frame, Packet packet): # Propagate our manual times. diff --git a/av/container/input.pyx b/av/container/input.pyx index acf02fbab..88cc95ee0 100644 --- a/av/container/input.pyx +++ b/av/container/input.pyx @@ -278,6 +278,5 @@ cdef class InputContainer(Container): for stream in self.streams: codec_context = stream.codec_context - if codec_context and codec_context.is_open: - with nogil: - lib.avcodec_flush_buffers(codec_context.ptr) + if codec_context: + codec_context.flush_buffers()