Skip to content

Commit 1c66e17

Browse files
committed
Combine pad and link
1 parent 2f6c495 commit 1c66e17

File tree

11 files changed

+97
-147
lines changed

11 files changed

+97
-147
lines changed

av/filter/context.pyi

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from av.filter import Graph
22
from av.frame import Frame
33

4-
from .pad import FilterContextPad
5-
64
class FilterContext:
75
name: str | None
8-
inputs: tuple[FilterContextPad, ...]
9-
outputs: tuple[FilterContextPad, ...]
106

117
def init(self, args: str | None = None, **kwargs: str | None) -> None: ...
128
def link_to(

av/filter/context.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from av.audio.frame cimport alloc_audio_frame
44
from av.dictionary cimport _Dictionary
55
from av.dictionary import Dictionary
66
from av.error cimport err_check
7-
from av.filter.pad cimport alloc_filter_pads
7+
from av.filter.link cimport alloc_filter_pads
88
from av.frame cimport Frame
99
from av.utils cimport avrational_to_fraction
1010
from av.video.frame cimport alloc_video_frame

av/filter/filter.pyi

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
from av.descriptor import Descriptor
22
from av.option import Option
33

4-
from .pad import FilterPad
5-
64
class Filter:
75
name: str
86
description: str
9-
107
descriptor: Descriptor
118
options: tuple[Option, ...] | None
129
flags: int
@@ -15,8 +12,6 @@ class Filter:
1512
timeline_support: bool
1613
slice_threads: bool
1714
command_support: bool
18-
inputs: tuple[FilterPad, ...]
19-
outputs: tuple[FilterPad, ...]
2015

2116
def __init__(self, name: str) -> None: ...
2217

av/filter/filter.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cimport libav as lib
22

33
from av.descriptor cimport wrap_avclass
4-
from av.filter.pad cimport alloc_filter_pads
4+
from av.filter.link cimport alloc_filter_pads
55

66

77
cdef object _cinit_sentinel = object()

av/filter/link.pxd

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
cimport libav as lib
22

3+
from av.filter.context cimport FilterContext
4+
from av.filter.filter cimport Filter
35
from av.filter.graph cimport Graph
4-
from av.filter.pad cimport FilterContextPad
6+
from av.filter.link cimport FilterContextPad, FilterLink
57

68

79
cdef class FilterLink:
8-
910
cdef readonly Graph graph
1011
cdef lib.AVFilterLink *ptr
1112

@@ -14,3 +15,18 @@ cdef class FilterLink:
1415

1516

1617
cdef FilterLink wrap_filter_link(Graph graph, lib.AVFilterLink *ptr)
18+
19+
cdef class FilterPad:
20+
cdef readonly Filter filter
21+
cdef readonly FilterContext context
22+
cdef readonly bint is_input
23+
cdef readonly int index
24+
25+
cdef const lib.AVFilterPad *base_ptr
26+
27+
28+
cdef class FilterContextPad(FilterPad):
29+
cdef FilterLink _link
30+
31+
32+
cdef tuple alloc_filter_pads(Filter, const lib.AVFilterPad *ptr, bint is_input, FilterContext context=?)

av/filter/link.pyi

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
from .pad import FilterContextPad
2-
31
class FilterLink:
4-
input: FilterContextPad
5-
output: FilterContextPad
2+
pass

av/filter/link.pyx

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ cdef _cinit_sentinel = object()
77

88

99
cdef class FilterLink:
10-
1110
def __cinit__(self, sentinel):
1211
if sentinel is not _cinit_sentinel:
1312
raise RuntimeError("cannot instantiate FilterLink")
@@ -51,3 +50,79 @@ cdef FilterLink wrap_filter_link(Graph graph, lib.AVFilterLink *ptr):
5150
link.graph = graph
5251
link.ptr = ptr
5352
return link
53+
54+
55+
56+
cdef class FilterPad:
57+
def __cinit__(self, sentinel):
58+
if sentinel is not _cinit_sentinel:
59+
raise RuntimeError("cannot construct FilterPad")
60+
61+
def __repr__(self):
62+
_filter = self.filter.name
63+
_io = "inputs" if self.is_input else "outputs"
64+
65+
return f"<av.FilterPad {_filter}.{_io}[{self.index}]: {self.name} ({self.type})>"
66+
67+
@property
68+
def is_output(self):
69+
return not self.is_input
70+
71+
@property
72+
def name(self):
73+
return lib.avfilter_pad_get_name(self.base_ptr, self.index)
74+
75+
76+
cdef class FilterContextPad(FilterPad):
77+
def __repr__(self):
78+
_filter = self.filter.name
79+
_io = "inputs" if self.is_input else "outputs"
80+
context = self.context.name
81+
82+
return f"<av.FilterContextPad {_filter}.{_io}[{self.index}] of {context}: {self.name} ({self.type})>"
83+
84+
@property
85+
def link(self):
86+
if self._link:
87+
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]
90+
if not link:
91+
return
92+
self._link = wrap_filter_link(self.context.graph, link)
93+
return self._link
94+
95+
@property
96+
def linked(self):
97+
cdef FilterLink link = self.link
98+
if link:
99+
return link.input if self.is_input else link.output
100+
101+
102+
cdef tuple alloc_filter_pads(Filter filter, const lib.AVFilterPad *ptr, bint is_input, FilterContext context=None):
103+
if not ptr:
104+
return ()
105+
106+
pads = []
107+
108+
# We need to be careful and check our bounds if we know what they are,
109+
# since the arrays on a AVFilterContext are not NULL terminated.
110+
cdef int i = 0
111+
cdef int count
112+
if context is None:
113+
count = lib.avfilter_filter_pad_count(filter.ptr, not is_input)
114+
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)
120+
pads.append(pad)
121+
pad.filter = filter
122+
pad.context = context
123+
pad.is_input = is_input
124+
pad.base_ptr = ptr
125+
pad.index = i
126+
i += 1
127+
128+
return tuple(pads)

av/filter/pad.pxd

Lines changed: 0 additions & 23 deletions
This file was deleted.

av/filter/pad.pyi

Lines changed: 0 additions & 10 deletions
This file was deleted.

av/filter/pad.pyx

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)