Skip to content

Commit 714be34

Browse files
committed
Make container/input pure
1 parent 8d4028e commit 714be34

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed
Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
cimport libav as lib
1+
import cython
2+
import cython.cimports.libav as lib
3+
from cython.cimports.av.filter.graph import Graph
24

3-
from av.filter.graph cimport Graph
5+
_cinit_sentinel = cython.declare(object, object())
46

57

6-
cdef _cinit_sentinel = object()
7-
8-
9-
cdef class FilterLink:
8+
@cython.cclass
9+
class FilterLink:
1010
def __cinit__(self, sentinel):
1111
if sentinel is not _cinit_sentinel:
1212
raise RuntimeError("cannot instantiate FilterLink")
@@ -15,45 +15,48 @@ def __cinit__(self, sentinel):
1515
def input(self):
1616
if self._input:
1717
return self._input
18-
cdef lib.AVFilterContext *cctx = self.ptr.src
19-
cdef unsigned int i
18+
cctx: cython.pointer[lib.AVFilterContext] = self.ptr.src
19+
i: cython.Py_ssize_t
2020
for i in range(cctx.nb_outputs):
2121
if self.ptr == cctx.outputs[i]:
2222
break
23-
else:
23+
else: # nobreak
2424
raise RuntimeError("could not find link in context")
25-
ctx = self.graph._context_by_ptr[<long>cctx]
25+
ctx = self.graph._context_by_ptr[cython.cast(cython.long, cctx)]
2626
self._input = ctx.outputs[i]
2727
return self._input
2828

2929
@property
3030
def output(self):
3131
if self._output:
3232
return self._output
33-
cdef lib.AVFilterContext *cctx = self.ptr.dst
34-
cdef unsigned int i
33+
cctx: cython.pointer[lib.AVFilterContext] = self.ptr.dst
34+
i: cython.Py_ssize_t
3535
for i in range(cctx.nb_inputs):
3636
if self.ptr == cctx.inputs[i]:
3737
break
3838
else:
3939
raise RuntimeError("could not find link in context")
4040
try:
41-
ctx = self.graph._context_by_ptr[<long>cctx]
41+
ctx = self.graph._context_by_ptr[cython.cast(cython.long, cctx)]
4242
except KeyError:
43-
raise RuntimeError("could not find context in graph", (cctx.name, cctx.filter.name))
43+
raise RuntimeError(
44+
"could not find context in graph", (cctx.name, cctx.filter.name)
45+
)
4446
self._output = ctx.inputs[i]
4547
return self._output
4648

4749

48-
cdef FilterLink wrap_filter_link(Graph graph, lib.AVFilterLink *ptr):
49-
cdef FilterLink link = FilterLink(_cinit_sentinel)
50+
@cython.cfunc
51+
def wrap_filter_link(graph: Graph, ptr: cython.pointer[lib.AVFilterLink]) -> FilterLink:
52+
link: FilterLink = FilterLink(_cinit_sentinel)
5053
link.graph = graph
5154
link.ptr = ptr
5255
return link
5356

5457

55-
56-
cdef class FilterPad:
58+
@cython.cclass
59+
class FilterPad:
5760
def __cinit__(self, sentinel):
5861
if sentinel is not _cinit_sentinel:
5962
raise RuntimeError("cannot construct FilterPad")
@@ -62,7 +65,9 @@ def __repr__(self):
6265
_filter = self.filter.name
6366
_io = "inputs" if self.is_input else "outputs"
6467

65-
return f"<av.FilterPad {_filter}.{_io}[{self.index}]: {self.name} ({self.type})>"
68+
return (
69+
f"<av.FilterPad {_filter}.{_io}[{self.index}]: {self.name} ({self.type})>"
70+
)
6671

6772
@property
6873
def is_output(self):
@@ -73,7 +78,8 @@ def name(self):
7378
return lib.avfilter_pad_get_name(self.base_ptr, self.index)
7479

7580

76-
cdef class FilterContextPad(FilterPad):
81+
@cython.cclass
82+
class FilterContextPad(FilterPad):
7783
def __repr__(self):
7884
_filter = self.filter.name
7985
_io = "inputs" if self.is_input else "outputs"
@@ -85,38 +91,50 @@ def __repr__(self):
8591
def link(self):
8692
if self._link:
8793
return self._link
88-
cdef lib.AVFilterLink **links = self.context.ptr.inputs if self.is_input else self.context.ptr.outputs
89-
cdef lib.AVFilterLink *link = links[self.index]
94+
links: cython.pointer[cython.pointer[lib.AVFilterLink]] = (
95+
self.context.ptr.inputs if self.is_input else self.context.ptr.outputs
96+
)
97+
link: cython.pointer[lib.AVFilterLink] = links[self.index]
9098
if not link:
9199
return
92100
self._link = wrap_filter_link(self.context.graph, link)
93101
return self._link
94102

95103
@property
96104
def linked(self):
97-
cdef FilterLink link = self.link
105+
link: FilterLink = self.link
98106
if link:
99107
return link.input if self.is_input else link.output
100108

101109

102-
cdef tuple alloc_filter_pads(Filter filter, const lib.AVFilterPad *ptr, bint is_input, FilterContext context=None):
110+
@cython.cfunc
111+
def alloc_filter_pads(
112+
filter: Filter,
113+
ptr: cython.pointer[cython.const[lib.AVFilterPad]],
114+
is_input: cython.bint,
115+
context: FilterContext | None = None,
116+
) -> tuple:
103117
if not ptr:
104118
return ()
105119

106-
pads = []
120+
pads: list = []
107121

108122
# We need to be careful and check our bounds if we know what they are,
109123
# since the arrays on a AVFilterContext are not NULL terminated.
110-
cdef int i = 0
111-
cdef int count
124+
i: cython.int = 0
125+
count: cython.int
112126
if context is None:
113127
count = lib.avfilter_filter_pad_count(filter.ptr, not is_input)
114128
else:
115-
count = (context.ptr.nb_inputs if is_input else context.ptr.nb_outputs)
116-
117-
cdef FilterPad pad
118-
while (i < count):
119-
pad = FilterPad(_cinit_sentinel) if context is None else FilterContextPad(_cinit_sentinel)
129+
count = context.ptr.nb_inputs if is_input else context.ptr.nb_outputs
130+
131+
pad: FilterPad
132+
while i < count:
133+
pad = (
134+
FilterPad(_cinit_sentinel)
135+
if context is None
136+
else FilterContextPad(_cinit_sentinel)
137+
)
120138
pads.append(pad)
121139
pad.filter = filter
122140
pad.context = context

0 commit comments

Comments
 (0)