| 1 |
# type: ignore |
| 2 |
import cython |
| 3 |
from cython import NULL |
| 4 |
from cython.cimports import libav as lib |
| 5 |
from cython.cimports.av.error import stash_exception |
| 6 |
from cython.cimports.libc.stdint import int64_t, uint8_t |
| 7 |
from cython.cimports.libc.string import memcpy |
| 8 |
|
| 9 |
Buf = cython.typedef(cython.pointer[uint8_t]) |
| 10 |
BufC = cython.typedef(cython.pointer[cython.const[uint8_t]]) |
| 11 |
|
| 12 |
seek_func_t = cython.typedef( |
| 13 |
"int64_t (*seek_func_t)(void *opaque, int64_t offset, int whence) noexcept nogil" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
@cython.final |
| 18 |
@cython.cclass |
| 19 |
class PyIOFile: |
| 20 |
def __cinit__(self, file, buffer_size, writeable=None): |
| 21 |
self.file = file |
| 22 |
|
| 23 |
seek_func: seek_func_t = NULL |
| 24 |
|
| 25 |
readable = getattr(self.file, "readable", None) |
| 26 |
writable = getattr(self.file, "writable", None) |
| 27 |
seekable = getattr(self.file, "seekable", None) |
| 28 |
self.fread = getattr(self.file, "read", None) |
| 29 |
self.fwrite = getattr(self.file, "write", None) |
| 30 |
self.fseek = getattr(self.file, "seek", None) |
| 31 |
self.ftell = getattr(self.file, "tell", None) |
| 32 |
self.fclose = getattr(self.file, "close", None) |
| 33 |
|
| 34 |
# To be seekable, the file object must have `seek` and `tell` methods. |
| 35 |
# If it also has a `seekable` method, it must return True. |
| 36 |
if ( |
| 37 |
self.fseek is not None |
| 38 |
and self.ftell is not None |
| 39 |
and (seekable is None or seekable()) |
| 40 |
): |
| 41 |
seek_func: seek_func_t = pyio_seek |
| 42 |
|
| 43 |
if writeable is None: |
| 44 |
writeable = self.fwrite is not None |
| 45 |
|
| 46 |
if writeable: |
| 47 |
if self.fwrite is None or (writable is not None and not writable()): |
| 48 |
raise ValueError( |
| 49 |
"File object has no write() method, or writable() returned False." |
| 50 |
) |
| 51 |
else: |
| 52 |
if self.fread is None or (readable is not None and not readable()): |
| 53 |
raise ValueError( |
| 54 |
"File object has no read() method, or readable() returned False." |
| 55 |
) |
| 56 |
|
| 57 |
self.pos = 0 |
| 58 |
self.pos_is_valid = True |
| 59 |
|
| 60 |
# This is effectively the maximum size of reads. |
| 61 |
self.buffer = cython.cast(cython.p_uchar, lib.av_malloc(buffer_size)) |
| 62 |
|
| 63 |
self.iocontext = lib.avio_alloc_context( |
| 64 |
self.buffer, |
| 65 |
buffer_size, |
| 66 |
writeable, |
| 67 |
cython.cast(cython.p_void, self), # User data. |
| 68 |
pyio_read, |
| 69 |
pyio_write, |
| 70 |
seek_func, |
| 71 |
) |
| 72 |
|
| 73 |
if seek_func: |
| 74 |
self.iocontext.seekable = lib.AVIO_SEEKABLE_NORMAL |
| 75 |
self.iocontext.max_packet_size = buffer_size |
| 76 |
|
| 77 |
def __dealloc__(self): |
| 78 |
with cython.nogil: |
| 79 |
# FFmpeg will not release custom input, so it's up to us to free it. |
| 80 |
# Do not touch our original buffer as it may have been freed and replaced. |
| 81 |
if self.iocontext: |
| 82 |
lib.av_freep(cython.address(self.iocontext.buffer)) |
| 83 |
lib.av_freep(cython.address(self.iocontext)) |
| 84 |
|
| 85 |
# We likely errored badly if we got here, and so we are still responsible. |
| 86 |
else: |
| 87 |
lib.av_freep(cython.address(self.buffer)) |
| 88 |
|
| 89 |
|
| 90 |
@cython.cfunc |
| 91 |
@cython.nogil |
| 92 |
@cython.exceptval(check=False) |
| 93 |
def pyio_read(opaque: cython.p_void, buf: Buf, buf_size: cython.int) -> cython.int: |
| 94 |
with cython.gil: |
| 95 |
return pyio_read_gil(opaque, buf, buf_size) |
| 96 |
|
| 97 |
|
| 98 |
@cython.cfunc |
| 99 |
@cython.exceptval(check=False) |
| 100 |
def pyio_read_gil(opaque: cython.p_void, buf: Buf, buf_size: cython.int) -> cython.int: |
| 101 |
self: PyIOFile |
| 102 |
res: bytes |
| 103 |
try: |
| 104 |
self = cython.cast(PyIOFile, opaque) |
| 105 |
res = self.fread(buf_size) |
| 106 |
memcpy( |
| 107 |
buf, cython.cast(cython.p_void, cython.cast(cython.p_char, res)), len(res) |
| 108 |
) |
| 109 |
self.pos += len(res) |
| 110 |
if not res: |
| 111 |
return lib.AVERROR_EOF |
| 112 |
return len(res) |
| 113 |
except Exception: |
| 114 |
return stash_exception() |
| 115 |
|
| 116 |
|
| 117 |
@cython.cfunc |
| 118 |
@cython.nogil |
| 119 |
@cython.exceptval(check=False) |
| 120 |
def pyio_write(opaque: cython.p_void, buf: BufC, buf_size: cython.int) -> cython.int: |
| 121 |
with cython.gil: |
| 122 |
return pyio_write_gil(opaque, buf, buf_size) |
| 123 |
|
| 124 |
|
| 125 |
@cython.cfunc |
| 126 |
@cython.exceptval(check=False) |
| 127 |
def pyio_write_gil( |
| 128 |
opaque: cython.p_void, buf: BufC, buf_size: cython.int |
| 129 |
) -> cython.int: |
| 130 |
self: PyIOFile |
| 131 |
bytes_to_write: bytes |
| 132 |
bytes_written: cython.int |
| 133 |
try: |
| 134 |
self = cython.cast(PyIOFile, opaque) |
| 135 |
bytes_to_write = buf[:buf_size] |
| 136 |
ret_value = self.fwrite(bytes_to_write) |
| 137 |
bytes_written = ret_value if isinstance(ret_value, int) else buf_size |
| 138 |
self.pos += bytes_written |
| 139 |
return bytes_written |
| 140 |
except Exception: |
| 141 |
return stash_exception() |
| 142 |
|
| 143 |
|
| 144 |
@cython.cfunc |
| 145 |
@cython.nogil |
| 146 |
@cython.exceptval(check=False) |
| 147 |
def pyio_seek(opaque: cython.p_void, offset: int64_t, whence: cython.int) -> int64_t: |
| 148 |
# Seek takes the standard flags, but also a ad-hoc one which means that the library |
| 149 |
# wants to know how large the file is. We are generally allowed to ignore this. |
| 150 |
if whence == lib.AVSEEK_SIZE: |
| 151 |
return -1 |
| 152 |
with cython.gil: |
| 153 |
return pyio_seek_gil(opaque, offset, whence) |
| 154 |
|
| 155 |
|
| 156 |
@cython.cfunc |
| 157 |
def pyio_seek_gil( |
| 158 |
opaque: cython.p_void, offset: int64_t, whence: cython.int |
| 159 |
) -> int64_t: |
| 160 |
self: PyIOFile |
| 161 |
try: |
| 162 |
self = cython.cast(PyIOFile, opaque) |
| 163 |
res = self.fseek(offset, whence) |
| 164 |
|
| 165 |
# Track the position for the user. |
| 166 |
if whence == 0: |
| 167 |
self.pos = offset |
| 168 |
elif whence == 1: |
| 169 |
self.pos += offset |
| 170 |
else: |
| 171 |
self.pos_is_valid = False |
| 172 |
if res is None: |
| 173 |
if self.pos_is_valid: |
| 174 |
res = self.pos |
| 175 |
else: |
| 176 |
res = self.ftell() |
| 177 |
return res |
| 178 |
except Exception: |
| 179 |
return stash_exception() |
| 180 |
|
| 181 |
|
| 182 |
@cython.cfunc |
| 183 |
def pyio_close_gil(pb: cython.pointer[lib.AVIOContext]) -> cython.int: |
| 184 |
try: |
| 185 |
return lib.avio_close(pb) |
| 186 |
except Exception: |
| 187 |
return stash_exception() |
| 188 |
|
| 189 |
|
| 190 |
@cython.cfunc |
| 191 |
def pyio_close_custom_gil(pb: cython.pointer[lib.AVIOContext]) -> cython.int: |
| 192 |
self: PyIOFile |
| 193 |
try: |
| 194 |
self = cython.cast(PyIOFile, pb.opaque) |
| 195 |
|
| 196 |
# Flush bytes in the AVIOContext buffers to the custom I/O |
| 197 |
lib.avio_flush(pb) |
| 198 |
|
| 199 |
if self.fclose is not None: |
| 200 |
self.fclose() |
| 201 |
|
| 202 |
return 0 |
| 203 |
except Exception: |
| 204 |
stash_exception() |