Debian Codegraph @

av/filter/link.py

raw
1 import weakref
2
3 import cython
4 import cython.cimports.libav as lib
5 from cython.cimports.av.filter.graph import Graph
6
7 _cinit_sentinel = cython.declare(object, object())
8
9
10 @cython.final
11 @cython.cclass
12 class FilterLink:
13 def __cinit__(self, sentinel):
14 if sentinel is not _cinit_sentinel:
15 raise RuntimeError("cannot instantiate FilterLink")
16
17 @property
18 def graph(self) -> Graph:
19 if g := self._graph():
20 return g
21 else:
22 raise RuntimeError("graph is unallocated")
23
24 @property
25 def input(self):
26 if self._input:
27 return self._input
28 cctx: cython.pointer[lib.AVFilterContext] = self.ptr.src
29 i: cython.Py_ssize_t
30 for i in range(cctx.nb_outputs):
31 if self.ptr == cctx.outputs[i]:
32 break
33 else: # nobreak
34 raise RuntimeError("could not find link in context")
35 graph: Graph = self.graph
36 ctx = graph._context_by_ptr[cython.cast(cython.long, cctx)]
37 self._input = ctx.outputs[i]
38 return self._input
39
40 @property
41 def output(self):
42 if self._output:
43 return self._output
44 cctx: cython.pointer[lib.AVFilterContext] = self.ptr.dst
45 i: cython.Py_ssize_t
46 for i in range(cctx.nb_inputs):
47 if self.ptr == cctx.inputs[i]:
48 break
49 else:
50 raise RuntimeError("could not find link in context")
51 try:
52 graph: Graph = self.graph
53 ctx = graph._context_by_ptr[cython.cast(cython.long, cctx)]
54 except KeyError:
55 raise RuntimeError(
56 "could not find context in graph", (cctx.name, cctx.filter.name)
57 )
58 self._output = ctx.inputs[i]
59 return self._output
60
61
62 @cython.cfunc
63 def wrap_filter_link(graph: Graph, ptr: cython.pointer[lib.AVFilterLink]) -> FilterLink:
64 link: FilterLink = FilterLink(_cinit_sentinel)
65 link._graph = weakref.ref(graph)
66 link.ptr = ptr
67 return link
68
69
70 @cython.cclass
71 class FilterPad:
72 def __cinit__(self, sentinel):
73 if sentinel is not _cinit_sentinel:
74 raise RuntimeError("cannot construct FilterPad")
75
76 def __repr__(self):
77 _filter = self.filter.name
78 _io = "inputs" if self.is_input else "outputs"
79
80 return (
81 f"<av.FilterPad {_filter}.{_io}[{self.index}]: {self.name} ({self.type})>"
82 )
83
84 @property
85 def is_output(self):
86 return not self.is_input
87
88 @property
89 def name(self):
90 return lib.avfilter_pad_get_name(self.base_ptr, self.index)
91
92
93 @cython.final
94 @cython.cclass
95 class FilterContextPad(FilterPad):
96 def __repr__(self):
97 _filter = self.filter.name
98 _io = "inputs" if self.is_input else "outputs"
99 context = self.context.name
100
101 return f"<av.FilterContextPad {_filter}.{_io}[{self.index}] of {context}: {self.name} ({self.type})>"
102
103 @property
104 def link(self):
105 if self._link:
106 return self._link
107 links: cython.pointer[cython.pointer[lib.AVFilterLink]] = (
108 self.context.ptr.inputs if self.is_input else self.context.ptr.outputs
109 )
110 link: cython.pointer[lib.AVFilterLink] = links[self.index]
111 if not link:
112 return
113 self._link = wrap_filter_link(self.context.graph, link)
114 return self._link
115
116 @property
117 def linked(self):
118 link: FilterLink = self.link
119 if link:
120 return link.input if self.is_input else link.output
121
122
123 @cython.cfunc
124 def alloc_filter_pads(
125 filter: Filter,
126 ptr: cython.pointer[cython.const[lib.AVFilterPad]],
127 is_input: cython.bint,
128 context: FilterContext | None = None,
129 ) -> tuple:
130 if not ptr:
131 return ()
132
133 pads: list = []
134
135 # We need to be careful and check our bounds if we know what they are,
136 # since the arrays on a AVFilterContext are not NULL terminated.
137 i: cython.int = 0
138 count: cython.int
139 if context is None:
140 count = lib.avfilter_filter_pad_count(filter.ptr, not is_input)
141 else:
142 count = context.ptr.nb_inputs if is_input else context.ptr.nb_outputs
143
144 pad: FilterPad
145 while i < count:
146 pad = (
147 FilterPad(_cinit_sentinel)
148 if context is None
149 else FilterContextPad(_cinit_sentinel)
150 )
151 pads.append(pad)
152 pad.filter = filter
153 pad.context = context
154 pad.is_input = is_input
155 pad.base_ptr = ptr
156 pad.index = i
157 i += 1
158
159 return tuple(pads)