|
6 | 6 | from cython.cimports.av.packet import Packet |
7 | 7 | from cython.cimports.av.subtitles.subtitle import SubtitleProxy, SubtitleSet |
8 | 8 | 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 |
10 | 10 |
|
11 | 11 |
|
12 | 12 | @cython.cclass |
@@ -65,35 +65,19 @@ def encode_subtitle(self, subtitle: SubtitleSet) -> Packet: |
65 | 65 |
|
66 | 66 | self.open(strict=False) |
67 | 67 |
|
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) |
82 | 69 |
|
83 | 70 | ret: cython.int = lib.avcodec_encode_subtitle( |
84 | 71 | self.ptr, |
85 | | - buf, |
86 | | - buf_size, |
| 72 | + packet.ptr.data, |
| 73 | + packet.ptr.size, |
87 | 74 | cython.address(subtitle.proxy.struct), |
88 | 75 | ) |
89 | 76 |
|
90 | 77 | if ret < 0: |
91 | | - lib.av_free(buf) |
92 | 78 | err_check(ret, "avcodec_encode_subtitle()") |
93 | 79 |
|
94 | | - packet: Packet = Packet(ret) |
95 | | - memcpy(packet.ptr.data, buf, ret) |
96 | | - lib.av_free(buf) |
| 80 | + packet.ptr.size = ret |
97 | 81 |
|
98 | 82 | packet.ptr.pts = subtitle.proxy.struct.pts |
99 | 83 | packet.ptr.dts = subtitle.proxy.struct.pts |
|
0 commit comments