Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose CodecContext flush_buffers #1382

Merged
merged 1 commit into from
Apr 25, 2024
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
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()
Loading