| 1 |
import cython |
| 2 |
from cython.cimports import libav as lib |
| 3 |
from cython.cimports.av.buffer import ByteSource, bytesource |
| 4 |
from cython.cimports.av.codec.context import CodecContext |
| 5 |
from cython.cimports.av.error import err_check |
| 6 |
from cython.cimports.av.packet import Packet |
| 7 |
from cython.cimports.av.subtitles.subtitle import SubtitleProxy, SubtitleSet |
| 8 |
from cython.cimports.cpython.bytes import PyBytes_FromStringAndSize |
| 9 |
from cython.cimports.libc.string import memcpy |
| 10 |
|
| 11 |
|
| 12 |
@cython.final |
| 13 |
@cython.cclass |
| 14 |
class SubtitleCodecContext(CodecContext): |
| 15 |
@property |
| 16 |
def subtitle_header(self) -> bytes | None: |
| 17 |
"""Get the subtitle header data (ASS/SSA format for text subtitles).""" |
| 18 |
if ( |
| 19 |
self.ptr.subtitle_header == cython.NULL |
| 20 |
or self.ptr.subtitle_header_size <= 0 |
| 21 |
): |
| 22 |
return None |
| 23 |
return PyBytes_FromStringAndSize( |
| 24 |
cython.cast(cython.p_char, self.ptr.subtitle_header), |
| 25 |
self.ptr.subtitle_header_size, |
| 26 |
) |
| 27 |
|
| 28 |
@subtitle_header.setter |
| 29 |
def subtitle_header(self, data: bytes | None) -> None: |
| 30 |
"""Set the subtitle header data.""" |
| 31 |
source: ByteSource |
| 32 |
if data is None: |
| 33 |
lib.av_freep(cython.address(self.ptr.subtitle_header)) |
| 34 |
self.ptr.subtitle_header_size = 0 |
| 35 |
else: |
| 36 |
source = bytesource(data) |
| 37 |
self.ptr.subtitle_header = cython.cast( |
| 38 |
cython.p_uchar, |
| 39 |
lib.av_realloc( |
| 40 |
self.ptr.subtitle_header, |
| 41 |
source.length + lib.AV_INPUT_BUFFER_PADDING_SIZE, |
| 42 |
), |
| 43 |
) |
| 44 |
if not self.ptr.subtitle_header: |
| 45 |
raise MemoryError("Cannot allocate subtitle_header") |
| 46 |
memcpy(self.ptr.subtitle_header, source.ptr, source.length) |
| 47 |
self.ptr.subtitle_header_size = source.length |
| 48 |
self.subtitle_header_set = True |
| 49 |
|
| 50 |
def __dealloc__(self) -> None: |
| 51 |
if self.ptr and self.subtitle_header_set: |
| 52 |
lib.av_freep(cython.address(self.ptr.subtitle_header)) |
| 53 |
|
| 54 |
def encode_subtitle(self, subtitle: SubtitleSet) -> Packet: |
| 55 |
""" |
| 56 |
Encode a SubtitleSet into a Packet. |
| 57 |
|
| 58 |
Args: |
| 59 |
subtitle: The SubtitleSet to encode |
| 60 |
|
| 61 |
Returns: |
| 62 |
A Packet containing the encoded subtitle data |
| 63 |
""" |
| 64 |
if not self.codec.ptr: |
| 65 |
raise ValueError("Cannot encode with unknown codec") |
| 66 |
|
| 67 |
self.open(strict=False) |
| 68 |
|
| 69 |
buf: cython.p_uchar = cython.cast(cython.p_uchar, lib.av_malloc(1024 * 1024)) |
| 70 |
if buf == cython.NULL: |
| 71 |
raise MemoryError() |
| 72 |
|
| 73 |
ret: cython.int = lib.avcodec_encode_subtitle( |
| 74 |
self.ptr, |
| 75 |
buf, |
| 76 |
1024 * 1024, |
| 77 |
cython.address(subtitle.proxy.struct), |
| 78 |
) |
| 79 |
if ret < 0: |
| 80 |
lib.av_free(buf) |
| 81 |
err_check(ret, "avcodec_encode_subtitle()") |
| 82 |
|
| 83 |
packet: Packet = Packet(ret) |
| 84 |
memcpy(packet.ptr.data, buf, ret) |
| 85 |
lib.av_free(buf) |
| 86 |
packet.ptr.pts = subtitle.proxy.struct.pts |
| 87 |
packet.ptr.dts = subtitle.proxy.struct.pts |
| 88 |
packet.ptr.duration = ( |
| 89 |
subtitle.proxy.struct.end_display_time |
| 90 |
- subtitle.proxy.struct.start_display_time |
| 91 |
) |
| 92 |
|
| 93 |
return packet |
| 94 |
|
| 95 |
@cython.ccall |
| 96 |
def decode(self, packet: Packet | None = None): |
| 97 |
"""Decode a subtitle packet, returning a list of :class:`.Subtitle` objects |
| 98 |
if a subtitle was decoded, or an empty list otherwise.""" |
| 99 |
if not self.codec.ptr: |
| 100 |
raise ValueError("cannot decode unknown codec") |
| 101 |
|
| 102 |
if packet is None: |
| 103 |
raise RuntimeError("packet cannot be None") |
| 104 |
|
| 105 |
self.open(strict=False) |
| 106 |
proxy: SubtitleProxy = SubtitleProxy() |
| 107 |
got_frame: cython.int = 0 |
| 108 |
|
| 109 |
err_check( |
| 110 |
lib.avcodec_decode_subtitle2( |
| 111 |
self.ptr, |
| 112 |
cython.address(proxy.struct), |
| 113 |
cython.address(got_frame), |
| 114 |
packet.ptr, |
| 115 |
) |
| 116 |
) |
| 117 |
|
| 118 |
if got_frame: |
| 119 |
return list(SubtitleSet(proxy)) |
| 120 |
return [] |
| 121 |
|
| 122 |
@cython.ccall |
| 123 |
def decode2(self, packet: Packet): |
| 124 |
""" |
| 125 |
Returns SubtitleSet if you really need it. |
| 126 |
""" |
| 127 |
if not self.codec.ptr: |
| 128 |
raise ValueError("cannot decode unknown codec") |
| 129 |
|
| 130 |
self.open(strict=False) |
| 131 |
|
| 132 |
proxy: SubtitleProxy = SubtitleProxy() |
| 133 |
got_frame: cython.int = 0 |
| 134 |
|
| 135 |
err_check( |
| 136 |
lib.avcodec_decode_subtitle2( |
| 137 |
self.ptr, |
| 138 |
cython.address(proxy.struct), |
| 139 |
cython.address(got_frame), |
| 140 |
packet.ptr, |
| 141 |
) |
| 142 |
) |
| 143 |
|
| 144 |
if got_frame: |
| 145 |
return SubtitleSet(proxy) |
| 146 |
return None |