-
Notifications
You must be signed in to change notification settings - Fork 47
/
filter_graph_test.go
253 lines (235 loc) · 10.8 KB
/
filter_graph_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// TODO Fix https://github.com/asticode/go-astiav/actions/runs/5853322732/job/15867145888
//go:build !windows
package astiav
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestFilterGraph(t *testing.T) {
fg1 := AllocFilterGraph()
require.NotNil(t, fg1)
defer fg1.Free()
cl := fg1.Class()
require.NotNil(t, cl)
require.Equal(t, "AVFilterGraph", cl.Name())
fg1.SetThreadCount(2)
require.Equal(t, 2, fg1.ThreadCount())
fg1.SetThreadType(ThreadTypeSlice)
require.Equal(t, ThreadTypeSlice, fg1.ThreadType())
type command struct {
args string
cmd string
flags FilterCommandFlags
resp string
target string
withError bool
}
type buffersink struct {
channelLayout ChannelLayout
colorRange ColorRange
colorSpace ColorSpace
frameRate Rational
height int
mediaType MediaType
name string
pixelFormat PixelFormat
sampleAspectRatio Rational
sampleFormat SampleFormat
sampleRate int
timeBase Rational
width int
}
type buffersrc struct {
name string
}
type buffersrcParameters struct {
channelLayout ChannelLayout
height int
mediaType MediaType
pixelFormat PixelFormat
sampleAspectRatio Rational
sampleFormat SampleFormat
sampleRate int
timeBase Rational
width int
}
type graph struct {
buffersink buffersink
buffersrc buffersrc
commands []command
content string
s string
sources []buffersrcParameters
}
for _, v := range []graph{
{
buffersink: buffersink{
colorRange: ColorRangeUnspecified,
colorSpace: ColorSpaceUnspecified,
frameRate: NewRational(4, 1),
height: 8,
mediaType: MediaTypeVideo,
name: "buffersink",
pixelFormat: PixelFormatYuv420P,
sampleAspectRatio: NewRational(2, 1),
timeBase: NewRational(1, 4),
width: 4,
},
buffersrc: buffersrc{name: "buffer"},
commands: []command{
{
args: "a",
cmd: "invalid",
flags: NewFilterCommandFlags(),
target: "scale",
withError: true,
},
{
args: "4",
cmd: "width",
flags: NewFilterCommandFlags().Add(FilterCommandFlagOne),
target: "scale",
},
},
content: "[input_1]scale=4x8,settb=1/4,fps=fps=4/1,format=pix_fmts=yuv420p,setsar=2/1",
s: " +--------------+\nParsed_setsar_4:default--[4x8 2:1 yuv420p]--default| filter_out |\n | (buffersink) |\n +--------------+\n\n+-------------+\n| filter_in_1 |default--[2x4 1:2 rgba]--Parsed_scale_0:default\n| (buffer) |\n+-------------+\n\n +----------------+\nfilter_in_1:default--[2x4 1:2 rgba]--default| Parsed_scale_0 |default--[4x8 1:2 yuv420p]--Parsed_settb_1:default\n | (scale) |\n +----------------+\n\n +----------------+\nParsed_scale_0:default--[4x8 1:2 yuv420p]--default| Parsed_settb_1 |default--[4x8 1:2 yuv420p]--Parsed_fps_2:default\n | (settb) |\n +----------------+\n\n +--------------+\nParsed_settb_1:default--[4x8 1:2 yuv420p]--default| Parsed_fps_2 |default--[4x8 1:2 yuv420p]--Parsed_format_3:default\n | (fps) |\n +--------------+\n\n +-----------------+\nParsed_fps_2:default--[4x8 1:2 yuv420p]--default| Parsed_format_3 |default--[4x8 1:2 yuv420p]--Parsed_setsar_4:default\n | (format) |\n +-----------------+\n\n +-----------------+\nParsed_format_3:default--[4x8 1:2 yuv420p]--default| Parsed_setsar_4 |default--[4x8 2:1 yuv420p]--filter_out:default\n | (setsar) |\n +-----------------+\n\n",
sources: []buffersrcParameters{
{
height: 4,
mediaType: MediaTypeVideo,
pixelFormat: PixelFormatRgba,
sampleAspectRatio: NewRational(1, 2),
timeBase: NewRational(1, 2),
width: 2,
},
},
},
{
buffersink: buffersink{
channelLayout: ChannelLayoutStereo,
mediaType: MediaTypeAudio,
name: "abuffersink",
sampleFormat: SampleFormatS16,
sampleRate: 3,
timeBase: NewRational(1, 4),
},
buffersrc: buffersrc{name: "abuffer"},
content: "[input_1]aformat=sample_fmts=s16:channel_layouts=stereo:sample_rates=3,asettb=1/4",
s: " +---------------+\nParsed_asettb_1:default--[3Hz s16:stereo]--default| filter_out |\n | (abuffersink) |\n +---------------+\n\n+-------------+\n| filter_in_1 |default--[2Hz fltp:mono]--auto_aresample_0:default\n| (abuffer) |\n+-------------+\n\n +------------------+\nauto_aresample_0:default--[3Hz s16:stereo]--default| Parsed_aformat_0 |default--[3Hz s16:stereo]--Parsed_asettb_1:default\n | (aformat) |\n +------------------+\n\n +-----------------+\nParsed_aformat_0:default--[3Hz s16:stereo]--default| Parsed_asettb_1 |default--[3Hz s16:stereo]--filter_out:default\n | (asettb) |\n +-----------------+\n\n +------------------+\nfilter_in_1:default--[2Hz fltp:mono]--default| auto_aresample_0 |default--[3Hz s16:stereo]--Parsed_aformat_0:default\n | (aresample) |\n +------------------+\n\n",
sources: []buffersrcParameters{
{
channelLayout: ChannelLayoutMono,
mediaType: MediaTypeAudio,
sampleFormat: SampleFormatFltp,
sampleRate: 2,
timeBase: NewRational(1, 2),
},
},
},
} {
fg := AllocFilterGraph()
require.NotNil(t, fg)
defer fg.Free()
buffersrc := FindFilterByName(v.buffersrc.name)
require.NotNil(t, buffersrc)
buffersink := FindFilterByName(v.buffersink.name)
require.NotNil(t, buffersink)
buffersinkContext, err := fg.NewBuffersinkFilterContext(buffersink, "filter_out")
require.NoError(t, err)
require.Equal(t, buffersink, buffersinkContext.FilterContext().Filter())
cl = buffersinkContext.FilterContext().Class()
require.NotNil(t, cl)
require.Equal(t, "AVFilter", cl.Name())
inputs := AllocFilterInOut()
defer inputs.Free()
inputs.SetName("out")
inputs.SetFilterContext(buffersinkContext.FilterContext())
inputs.SetPadIdx(0)
inputs.SetNext(nil)
var outputs *FilterInOut
defer func() {
if outputs != nil {
outputs.Free()
}
}()
var buffersrcContexts []*BuffersrcFilterContext
for idx, src := range v.sources {
buffersrcContext, err := fg.NewBuffersrcFilterContext(buffersrc, fmt.Sprintf("filter_in_%d", idx+1))
require.NoError(t, err)
buffersrcContextParameters := AllocBuffersrcFilterContextParameters()
defer buffersrcContextParameters.Free()
switch src.mediaType {
case MediaTypeAudio:
buffersrcContextParameters.SetChannelLayout(src.channelLayout)
buffersrcContextParameters.SetSampleFormat(src.sampleFormat)
buffersrcContextParameters.SetSampleRate(src.sampleRate)
buffersrcContextParameters.SetTimeBase(src.timeBase)
default:
buffersrcContextParameters.SetHeight(src.height)
buffersrcContextParameters.SetPixelFormat(src.pixelFormat)
buffersrcContextParameters.SetSampleAspectRatio(src.sampleAspectRatio)
buffersrcContextParameters.SetTimeBase(src.timeBase)
buffersrcContextParameters.SetWidth(src.width)
}
buffersrcContext.SetParameters(buffersrcContextParameters)
require.NoError(t, buffersrcContext.Initialize(nil))
buffersrcContexts = append(buffersrcContexts, buffersrcContext)
o := AllocFilterInOut()
o.SetName(fmt.Sprintf("input_%d", idx+1))
o.SetFilterContext(buffersrcContext.FilterContext())
o.SetPadIdx(0)
o.SetNext(outputs)
outputs = o
}
require.NoError(t, fg.Parse(v.content, inputs, outputs))
require.NoError(t, fg.Configure())
require.Equal(t, v.buffersink.frameRate, buffersinkContext.FrameRate())
require.Equal(t, v.buffersink.mediaType, buffersinkContext.MediaType())
require.Equal(t, v.buffersink.timeBase, buffersinkContext.TimeBase())
switch v.buffersink.mediaType {
case MediaTypeAudio:
require.True(t, v.buffersink.channelLayout.Equal(buffersinkContext.ChannelLayout()))
require.Equal(t, v.buffersink.sampleFormat, buffersinkContext.SampleFormat())
require.Equal(t, v.buffersink.sampleRate, buffersinkContext.SampleRate())
default:
require.Equal(t, v.buffersink.colorRange, buffersinkContext.ColorRange())
require.Equal(t, v.buffersink.colorSpace, buffersinkContext.ColorSpace())
require.Equal(t, v.buffersink.height, buffersinkContext.Height())
require.Equal(t, v.buffersink.pixelFormat, buffersinkContext.PixelFormat())
require.Equal(t, v.buffersink.sampleAspectRatio, buffersinkContext.SampleAspectRatio())
require.Equal(t, v.buffersink.width, buffersinkContext.Width())
}
require.Equal(t, v.s, fg.String())
for _, command := range v.commands {
resp, err := fg.SendCommand(command.target, command.cmd, command.args, command.flags)
if command.withError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, command.resp, resp)
}
}
fg2 := AllocFilterGraph()
require.NotNil(t, fg2)
defer fg2.Free()
fgs, err := fg2.ParseSegment("anullsrc")
require.NoError(t, err)
defer fgs.Free()
require.Equal(t, 1, fgs.NbChains())
cs := fgs.Chains()
require.Equal(t, 1, len(cs))
require.Equal(t, 1, cs[0].NbFilters())
fs := cs[0].Filters()
require.Equal(t, 1, len(fs))
f := FindFilterByName(fs[0].FilterName())
require.NotNil(t, f)
require.Equal(t, 0, f.NbInputs())
require.Equal(t, 1, f.NbOutputs())
os := f.Outputs()
require.Equal(t, 1, len(os))
require.Equal(t, MediaTypeAudio, os[0].MediaType())
// TODO Test BuffersrcAddFrame
// TODO Test BuffersinkGetFrame
}