Skip to content

Commit 30b02f9

Browse files
committed
Remove strlen usage by using a fixed buffer
1 parent b251531 commit 30b02f9

1 file changed

Lines changed: 5 additions & 21 deletions

File tree

av/subtitles/codeccontext.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from cython.cimports.av.packet import Packet
77
from cython.cimports.av.subtitles.subtitle import SubtitleProxy, SubtitleSet
88
from cython.cimports.cpython.bytes import PyBytes_FromStringAndSize
9-
from cython.cimports.libc.string import memcpy, strlen
9+
from cython.cimports.libc.string import memcpy
1010

1111

1212
@cython.cclass
@@ -65,35 +65,19 @@ def encode_subtitle(self, subtitle: SubtitleSet) -> Packet:
6565

6666
self.open(strict=False)
6767

68-
# Calculate buffer size from subtitle text length
69-
buf_size: cython.size_t = 0
70-
i: cython.uint
71-
for i in range(subtitle.proxy.struct.num_rects):
72-
rect = subtitle.proxy.struct.rects[i]
73-
if rect.ass != cython.NULL:
74-
buf_size += strlen(rect.ass)
75-
if rect.text != cython.NULL:
76-
buf_size += strlen(rect.text)
77-
buf_size += 1024 # padding for format overhead
78-
79-
buf: cython.p_uchar = cython.cast(cython.p_uchar, lib.av_malloc(buf_size))
80-
if buf == cython.NULL:
81-
raise MemoryError("Failed to allocate subtitle encode buffer")
68+
packet: Packet = Packet(1024 * 1024)
8269

8370
ret: cython.int = lib.avcodec_encode_subtitle(
8471
self.ptr,
85-
buf,
86-
buf_size,
72+
packet.ptr.data,
73+
packet.ptr.size,
8774
cython.address(subtitle.proxy.struct),
8875
)
8976

9077
if ret < 0:
91-
lib.av_free(buf)
9278
err_check(ret, "avcodec_encode_subtitle()")
9379

94-
packet: Packet = Packet(ret)
95-
memcpy(packet.ptr.data, buf, ret)
96-
lib.av_free(buf)
80+
packet.ptr.size = ret
9781

9882
packet.ptr.pts = subtitle.proxy.struct.pts
9983
packet.ptr.dts = subtitle.proxy.struct.pts

0 commit comments

Comments
 (0)