| 1 |
import weakref |
| 2 |
|
| 3 |
import cython |
| 4 |
import cython.cimports.libav as lib |
| 5 |
from cython.cimports.av.audio.frame import alloc_audio_frame |
| 6 |
from cython.cimports.av.dictionary import Dictionary |
| 7 |
from cython.cimports.av.error import err_check |
| 8 |
from cython.cimports.av.filter.link import alloc_filter_pads |
| 9 |
from cython.cimports.av.frame import Frame |
| 10 |
from cython.cimports.av.utils import avrational_to_fraction |
| 11 |
from cython.cimports.av.video.frame import alloc_video_frame |
| 12 |
|
| 13 |
_cinit_sentinel = cython.declare(object, object()) |
| 14 |
|
| 15 |
|
| 16 |
@cython.cfunc |
| 17 |
def wrap_filter_context( |
| 18 |
graph: Graph, filter: Filter, ptr: cython.pointer[lib.AVFilterContext] |
| 19 |
) -> FilterContext: |
| 20 |
self: FilterContext = FilterContext(_cinit_sentinel) |
| 21 |
self._graph = weakref.ref(graph) |
| 22 |
self.filter = filter |
| 23 |
self.ptr = ptr |
| 24 |
return self |
| 25 |
|
| 26 |
|
| 27 |
@cython.final |
| 28 |
@cython.cclass |
| 29 |
class FilterContext: |
| 30 |
def __cinit__(self, sentinel): |
| 31 |
if sentinel is not _cinit_sentinel: |
| 32 |
raise RuntimeError("cannot construct FilterContext") |
| 33 |
|
| 34 |
def __repr__(self): |
| 35 |
if self.ptr != cython.NULL: |
| 36 |
name = repr(self.ptr.name) if self.ptr.name != cython.NULL else "<NULL>" |
| 37 |
else: |
| 38 |
name = "None" |
| 39 |
|
| 40 |
parent = ( |
| 41 |
self.filter.ptr.name |
| 42 |
if self.filter and self.filter.ptr != cython.NULL |
| 43 |
else None |
| 44 |
) |
| 45 |
return f"<av.FilterContext {name} of {parent!r} at 0x{id(self):x}>" |
| 46 |
|
| 47 |
@property |
| 48 |
def name(self): |
| 49 |
if self.ptr.name != cython.NULL: |
| 50 |
return self.ptr.name |
| 51 |
|
| 52 |
@property |
| 53 |
def inputs(self): |
| 54 |
if self._inputs is None: |
| 55 |
self._inputs = alloc_filter_pads( |
| 56 |
self.filter, self.ptr.input_pads, True, self |
| 57 |
) |
| 58 |
return self._inputs |
| 59 |
|
| 60 |
@property |
| 61 |
def outputs(self): |
| 62 |
if self._outputs is None: |
| 63 |
self._outputs = alloc_filter_pads( |
| 64 |
self.filter, self.ptr.output_pads, False, self |
| 65 |
) |
| 66 |
return self._outputs |
| 67 |
|
| 68 |
def init(self, args=None, **kwargs): |
| 69 |
if self.inited: |
| 70 |
raise ValueError("already inited") |
| 71 |
if args and kwargs: |
| 72 |
raise ValueError("cannot init from args and kwargs") |
| 73 |
|
| 74 |
dict_: Dictionary = None |
| 75 |
c_args: cython.p_char = cython.NULL |
| 76 |
if args or not kwargs: |
| 77 |
if args: |
| 78 |
c_args = args |
| 79 |
err_check(lib.avfilter_init_str(self.ptr, c_args)) |
| 80 |
else: |
| 81 |
dict_ = Dictionary(kwargs) |
| 82 |
err_check(lib.avfilter_init_dict(self.ptr, cython.address(dict_.ptr))) |
| 83 |
|
| 84 |
self.inited = True |
| 85 |
if dict_: |
| 86 |
raise ValueError(f"unused config: {', '.join(sorted(dict_))}") |
| 87 |
|
| 88 |
def link_to( |
| 89 |
self, |
| 90 |
input_: FilterContext, |
| 91 |
output_idx: cython.int = 0, |
| 92 |
input_idx: cython.int = 0, |
| 93 |
): |
| 94 |
err_check(lib.avfilter_link(self.ptr, output_idx, input_.ptr, input_idx)) |
| 95 |
|
| 96 |
@property |
| 97 |
def graph(self): |
| 98 |
if graph := self._graph(): |
| 99 |
return graph |
| 100 |
else: |
| 101 |
raise RuntimeError("graph is unallocated") |
| 102 |
|
| 103 |
def push(self, frame: Frame | None): |
| 104 |
res: cython.int |
| 105 |
|
| 106 |
if frame is None: |
| 107 |
with cython.nogil: |
| 108 |
res = lib.av_buffersrc_write_frame(self.ptr, cython.NULL) |
| 109 |
err_check(res) |
| 110 |
return |
| 111 |
elif self.filter.name in ("abuffer", "buffer"): |
| 112 |
with cython.nogil: |
| 113 |
res = lib.av_buffersrc_write_frame(self.ptr, frame.ptr) |
| 114 |
err_check(res) |
| 115 |
return |
| 116 |
|
| 117 |
# Delegate to the input. |
| 118 |
if len(self.inputs) != 1: |
| 119 |
raise ValueError( |
| 120 |
f"cannot delegate push without single input; found {len(self.inputs)}" |
| 121 |
) |
| 122 |
if not self.inputs[0].link: |
| 123 |
raise ValueError("cannot delegate push without linked input") |
| 124 |
self.inputs[0].linked.context.push(frame) |
| 125 |
|
| 126 |
def pull(self): |
| 127 |
frame: Frame |
| 128 |
res: cython.int |
| 129 |
if self.filter.name == "buffersink": |
| 130 |
frame = alloc_video_frame() |
| 131 |
elif self.filter.name == "abuffersink": |
| 132 |
frame = alloc_audio_frame() |
| 133 |
else: |
| 134 |
# Delegate to the output. |
| 135 |
if len(self.outputs) != 1: |
| 136 |
raise ValueError( |
| 137 |
f"cannot delegate pull without single output; found {len(self.outputs)}" |
| 138 |
) |
| 139 |
if not self.outputs[0].link: |
| 140 |
raise ValueError("cannot delegate pull without linked output") |
| 141 |
return self.outputs[0].linked.context.pull() |
| 142 |
|
| 143 |
self.graph.configure() |
| 144 |
|
| 145 |
with cython.nogil: |
| 146 |
res = lib.av_buffersink_get_frame(self.ptr, frame.ptr) |
| 147 |
err_check(res) |
| 148 |
|
| 149 |
frame._init_user_attributes() |
| 150 |
frame.time_base = avrational_to_fraction( |
| 151 |
cython.address(self.ptr.inputs[0].time_base) |
| 152 |
) |
| 153 |
return frame |
| 154 |
|
| 155 |
def process_command( |
| 156 |
self, cmd, arg=None, res_len: cython.int = 1024, flags: cython.int = 0 |
| 157 |
): |
| 158 |
if not cmd: |
| 159 |
raise ValueError("Invalid cmd") |
| 160 |
|
| 161 |
c_arg: cython.p_char = cython.NULL |
| 162 |
c_cmd: cython.p_char = cmd |
| 163 |
if arg is not None: |
| 164 |
c_arg = arg |
| 165 |
|
| 166 |
c_res: cython.p_char = cython.NULL |
| 167 |
ret: cython.int |
| 168 |
res_buf: bytearray = None |
| 169 |
view: cython.uchar[:] |
| 170 |
b: bytes |
| 171 |
nul: cython.int |
| 172 |
|
| 173 |
if res_len > 0: |
| 174 |
res_buf = bytearray(res_len) |
| 175 |
view = res_buf |
| 176 |
c_res = cython.cast(cython.p_char, cython.address(view[0])) |
| 177 |
|
| 178 |
with cython.nogil: |
| 179 |
ret = lib.avfilter_process_command( |
| 180 |
self.ptr, c_cmd, c_arg, c_res, res_len, flags |
| 181 |
) |
| 182 |
err_check(ret) |
| 183 |
|
| 184 |
if res_buf is not None: |
| 185 |
b = bytes(res_buf) |
| 186 |
nul = b.find(b"\x00") |
| 187 |
if nul >= 0: |
| 188 |
b = b[:nul] |
| 189 |
if b: |
| 190 |
return b.decode("utf-8", "strict") |
| 191 |
return None |