Skip to content
Merged
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
21 changes: 4 additions & 17 deletions av/subtitles/codeccontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from cython.cimports.av.packet import Packet
from cython.cimports.av.subtitles.subtitle import SubtitleProxy, SubtitleSet
from cython.cimports.cpython.bytes import PyBytes_FromStringAndSize
from cython.cimports.libc.string import memcpy, strlen
from cython.cimports.libc.string import memcpy


@cython.cclass
Expand Down Expand Up @@ -65,36 +65,23 @@ def encode_subtitle(self, subtitle: SubtitleSet) -> Packet:

self.open(strict=False)

# Calculate buffer size from subtitle text length
buf_size: cython.size_t = 0
i: cython.uint
for i in range(subtitle.proxy.struct.num_rects):
rect = subtitle.proxy.struct.rects[i]
if rect.ass != cython.NULL:
buf_size += strlen(rect.ass)
if rect.text != cython.NULL:
buf_size += strlen(rect.text)
buf_size += 1024 # padding for format overhead

buf: cython.p_uchar = cython.cast(cython.p_uchar, lib.av_malloc(buf_size))
buf: cython.p_uchar = cython.cast(cython.p_uchar, lib.av_malloc(1024 * 1024))
if buf == cython.NULL:
raise MemoryError("Failed to allocate subtitle encode buffer")
raise MemoryError()

ret: cython.int = lib.avcodec_encode_subtitle(
self.ptr,
buf,
buf_size,
1024 * 1024,
cython.address(subtitle.proxy.struct),
)

if ret < 0:
lib.av_free(buf)
err_check(ret, "avcodec_encode_subtitle()")

packet: Packet = Packet(ret)
memcpy(packet.ptr.data, buf, ret)
lib.av_free(buf)

packet.ptr.pts = subtitle.proxy.struct.pts
packet.ptr.dts = subtitle.proxy.struct.pts
packet.ptr.duration = (
Expand Down
Loading