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 @@ -52,7 +52,6 @@ cdef class CodecContext:
# send/recv buffer may be limited to a single frame. Ergo, we need to flush
# the buffer as often as possible.
cdef _recv_packet(self)
cdef _send_packet_and_recv(self, Packet packet)
cdef _recv_frame(self)

cdef _transfer_hwframe(self, Frame frame)
Expand Down
39 changes: 14 additions & 25 deletions av/codec/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,25 +362,6 @@ def _send_frame_and_recv(self, frame: Frame | None):
yield packet
packet = self._recv_packet()

@cython.cfunc
def _send_packet_and_recv(self, packet: Packet | None):
frame: Frame
res: cython.int
with cython.nogil:
res = lib.avcodec_send_packet(
self.ptr, packet.ptr if packet is not None else cython.NULL
)
err_check(res, "avcodec_send_packet()")

out: list = []
while True:
frame = self._recv_frame()
if frame:
out.append(frame)
else:
break
return out

@cython.cfunc
def _prepare_frames_for_encode(self, frame: Frame | None) -> list:
return [frame]
Expand Down Expand Up @@ -487,12 +468,20 @@ def decode(self, packet: Packet | None = None):

self.open(strict=False)

res: list = []
for frame in self._send_packet_and_recv(packet):
if isinstance(frame, Frame):
self._setup_decoded_frame(frame, packet)
res.append(frame)
return res
res: cython.int
with cython.nogil:
res = lib.avcodec_send_packet(
self.ptr, packet.ptr if packet is not None else cython.NULL
)
err_check(res, "avcodec_send_packet()")

out: list = []
frame = self._recv_frame()
while frame:
self._setup_decoded_frame(frame, packet)
out.append(frame)
frame = self._recv_frame()
return out

@cython.ccall
def flush_buffers(self):
Expand Down
13 changes: 10 additions & 3 deletions av/subtitles/codeccontext.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cython
from cython.cimports import libav as lib
from cython.cimports.av.buffer import ByteSource, bytesource
from cython.cimports.av.codec.context import CodecContext
from cython.cimports.av.error import err_check
from cython.cimports.av.packet import Packet
from cython.cimports.av.subtitles.subtitle import SubtitleProxy, SubtitleSet
Expand Down Expand Up @@ -103,11 +104,17 @@ def encode_subtitle(self, subtitle: SubtitleSet) -> Packet:

return packet

@cython.cfunc
def _send_packet_and_recv(self, packet: Packet | None):
@cython.ccall
def decode(self, packet: Packet | None = None):
"""Decode a subtitle packet, returning a list of :class:`.Subtitle` objects
if a subtitle was decoded, or an empty list otherwise."""
if not self.codec.ptr:
raise ValueError("cannot decode unknown codec")

if packet is None:
raise RuntimeError("packet cannot be None")

self.open(strict=False)
proxy: SubtitleProxy = SubtitleProxy()
got_frame: cython.int = 0

Expand All @@ -121,7 +128,7 @@ def _send_packet_and_recv(self, packet: Packet | None):
)

if got_frame:
return SubtitleSet(proxy)
return list(SubtitleSet(proxy))
return []

@cython.ccall
Expand Down
Loading