Skip to content

Commit

Permalink
Expose CodecContext flush_buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
skeskinen authored and WyattBlue committed Apr 25, 2024
1 parent 19afbf2 commit 7f357af
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions av/codec/context.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions av/codec/context.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: ...
11 changes: 11 additions & 0 deletions av/codec/context.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions av/container/input.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 7f357af

Please sign in to comment.