Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * This file is part of FFmpeg. | ||
3 | * | ||
4 | * FFmpeg is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) any later version. | ||
8 | * | ||
9 | * FFmpeg is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Lesser General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public | ||
15 | * License along with FFmpeg; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file | ||
21 | * Audio join filter | ||
22 | * | ||
23 | * Join multiple audio inputs as different channels in | ||
24 | * a single output | ||
25 | */ | ||
26 | |||
27 | #include "libavutil/avassert.h" | ||
28 | #include "libavutil/avstring.h" | ||
29 | #include "libavutil/channel_layout.h" | ||
30 | #include "libavutil/common.h" | ||
31 | #include "libavutil/mem.h" | ||
32 | #include "libavutil/opt.h" | ||
33 | |||
34 | #include "audio.h" | ||
35 | #include "avfilter.h" | ||
36 | #include "formats.h" | ||
37 | #include "filters.h" | ||
38 | #include "internal.h" | ||
39 | |||
40 | typedef struct ChannelMap { | ||
41 | int input; ///< input stream index | ||
42 | int in_channel_idx; ///< index of in_channel in the input stream data | ||
43 | enum AVChannel in_channel; | ||
44 | enum AVChannel out_channel; | ||
45 | } ChannelMap; | ||
46 | |||
47 | typedef struct JoinContext { | ||
48 | const AVClass *class; | ||
49 | |||
50 | int inputs; | ||
51 | char *map; | ||
52 | AVChannelLayout ch_layout; | ||
53 | |||
54 | int64_t eof_pts; | ||
55 | int eof; | ||
56 | |||
57 | ChannelMap *channels; | ||
58 | |||
59 | /** | ||
60 | * Temporary storage for input frames, until we get one on each input. | ||
61 | */ | ||
62 | AVFrame **input_frames; | ||
63 | |||
64 | /** | ||
65 | * Temporary storage for buffer references, for assembling the output frame. | ||
66 | */ | ||
67 | AVBufferRef **buffers; | ||
68 | } JoinContext; | ||
69 | |||
70 | #define OFFSET(x) offsetof(JoinContext, x) | ||
71 | #define A AV_OPT_FLAG_AUDIO_PARAM | ||
72 | #define F AV_OPT_FLAG_FILTERING_PARAM | ||
73 | static const AVOption join_options[] = { | ||
74 | { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A|F }, | ||
75 | { "channel_layout", "Channel layout of the " | ||
76 | "output stream.", OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = "stereo"}, 0, 0, A|F }, | ||
77 | { "map", "A comma-separated list of channels maps in the format " | ||
78 | "'input_stream.input_channel-output_channel.", | ||
79 | OFFSET(map), AV_OPT_TYPE_STRING, .flags = A|F }, | ||
80 | { NULL } | ||
81 | }; | ||
82 | |||
83 | #define MAP_SEPARATOR '|' | ||
84 | |||
85 | AVFILTER_DEFINE_CLASS(join); | ||
86 | |||
87 | 2 | static int parse_maps(AVFilterContext *ctx) | |
88 | { | ||
89 | 2 | JoinContext *s = ctx->priv; | |
90 | 2 | char *cur = s->map; | |
91 | |||
92 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | while (cur && *cur) { |
93 | ChannelMap *map; | ||
94 | char *sep, *next, *p; | ||
95 | int input_idx, out_ch_idx; | ||
96 | |||
97 | ✗ | next = strchr(cur, MAP_SEPARATOR); | |
98 | ✗ | if (next) | |
99 | ✗ | *next++ = 0; | |
100 | |||
101 | /* split the map into input and output parts */ | ||
102 | ✗ | if (!(sep = strchr(cur, '-'))) { | |
103 | ✗ | av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel " | |
104 | "map '%s'\n", cur); | ||
105 | ✗ | return AVERROR(EINVAL); | |
106 | } | ||
107 | ✗ | *sep++ = 0; | |
108 | |||
109 | /* parse output channel */ | ||
110 | ✗ | out_ch_idx = av_channel_layout_index_from_string(&s->ch_layout, sep); | |
111 | ✗ | if (out_ch_idx < 0) { | |
112 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid output channel: %s.\n", sep); | |
113 | ✗ | return AVERROR(EINVAL); | |
114 | } | ||
115 | |||
116 | ✗ | map = &s->channels[out_ch_idx]; | |
117 | |||
118 | ✗ | if (map->input >= 0) { | |
119 | ✗ | av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel " | |
120 | "'%s'.\n", sep); | ||
121 | ✗ | return AVERROR(EINVAL); | |
122 | } | ||
123 | |||
124 | /* parse input channel */ | ||
125 | ✗ | input_idx = strtol(cur, &cur, 0); | |
126 | ✗ | if (input_idx < 0 || input_idx >= s->inputs) { | |
127 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n", | |
128 | input_idx); | ||
129 | ✗ | return AVERROR(EINVAL); | |
130 | } | ||
131 | |||
132 | ✗ | if (*cur) | |
133 | ✗ | cur++; | |
134 | |||
135 | ✗ | map->input = input_idx; | |
136 | ✗ | map->in_channel = AV_CHAN_NONE; | |
137 | ✗ | map->in_channel_idx = strtol(cur, &p, 0); | |
138 | ✗ | if (p == cur) { | |
139 | /* channel specifier is not a number, handle as channel name */ | ||
140 | ✗ | map->in_channel = av_channel_from_string(cur); | |
141 | ✗ | if (map->in_channel < 0) { | |
142 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid input channel: %s.\n", cur); | |
143 | ✗ | return AVERROR(EINVAL); | |
144 | } | ||
145 | ✗ | } else if (map->in_channel_idx < 0) { | |
146 | ✗ | av_log(ctx, AV_LOG_ERROR, "Invalid input channel index: %d\n", map->in_channel_idx); | |
147 | ✗ | return AVERROR(EINVAL); | |
148 | } | ||
149 | |||
150 | ✗ | cur = next; | |
151 | } | ||
152 | 2 | return 0; | |
153 | } | ||
154 | |||
155 | 2 | static av_cold int join_init(AVFilterContext *ctx) | |
156 | { | ||
157 | 2 | JoinContext *s = ctx->priv; | |
158 | int ret, i; | ||
159 | |||
160 | 2 | s->channels = av_calloc(s->ch_layout.nb_channels, sizeof(*s->channels)); | |
161 | 2 | s->buffers = av_calloc(s->ch_layout.nb_channels, sizeof(*s->buffers)); | |
162 | 2 | s->input_frames = av_calloc(s->inputs, sizeof(*s->input_frames)); | |
163 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
2 | if (!s->channels || !s->buffers|| !s->input_frames) |
164 | ✗ | return AVERROR(ENOMEM); | |
165 | |||
166 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
167 | 10 | s->channels[i].out_channel = av_channel_layout_channel_from_index(&s->ch_layout, i); | |
168 | 10 | s->channels[i].input = -1; | |
169 | 10 | s->channels[i].in_channel_idx = -1; | |
170 | 10 | s->channels[i].in_channel = AV_CHAN_NONE; | |
171 | } | ||
172 | |||
173 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if ((ret = parse_maps(ctx)) < 0) |
174 | ✗ | return ret; | |
175 | |||
176 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (i = 0; i < s->inputs; i++) { |
177 | 4 | AVFilterPad pad = { 0 }; | |
178 | |||
179 | 4 | pad.type = AVMEDIA_TYPE_AUDIO; | |
180 | 4 | pad.name = av_asprintf("input%d", i); | |
181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!pad.name) |
182 | ✗ | return AVERROR(ENOMEM); | |
183 | |||
184 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0) |
185 | ✗ | return ret; | |
186 | } | ||
187 | |||
188 | 2 | return 0; | |
189 | } | ||
190 | |||
191 | 2 | static av_cold void join_uninit(AVFilterContext *ctx) | |
192 | { | ||
193 | 2 | JoinContext *s = ctx->priv; | |
194 | int i; | ||
195 | |||
196 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
6 | for (i = 0; i < s->inputs && s->input_frames; i++) { |
197 | 4 | av_frame_free(&s->input_frames[i]); | |
198 | } | ||
199 | |||
200 | 2 | av_freep(&s->channels); | |
201 | 2 | av_freep(&s->buffers); | |
202 | 2 | av_freep(&s->input_frames); | |
203 | 2 | } | |
204 | |||
205 | 1 | static int join_query_formats(AVFilterContext *ctx) | |
206 | { | ||
207 | 1 | JoinContext *s = ctx->priv; | |
208 | 1 | AVFilterChannelLayouts *layouts = NULL; | |
209 | int i, ret; | ||
210 | |||
211 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
2 | if ((ret = ff_add_channel_layout(&layouts, &s->ch_layout)) < 0 || |
212 | 1 | (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->incfg.channel_layouts)) < 0) | |
213 | ✗ | return ret; | |
214 | |||
215 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 0; i < ctx->nb_inputs; i++) { |
216 | 2 | layouts = ff_all_channel_layouts(); | |
217 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->outcfg.channel_layouts)) < 0) |
218 | ✗ | return ret; | |
219 | } | ||
220 | |||
221 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
2 | if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 || |
222 | 1 | (ret = ff_set_common_all_samplerates(ctx)) < 0) | |
223 | ✗ | return ret; | |
224 | |||
225 | 1 | return 0; | |
226 | } | ||
227 | |||
228 | typedef struct ChannelList { | ||
229 | enum AVChannel *ch; | ||
230 | int nb_ch; | ||
231 | } ChannelList; | ||
232 | |||
233 | 5 | static enum AVChannel channel_list_pop(ChannelList *chl, int idx) | |
234 | { | ||
235 | 5 | enum AVChannel ret = chl->ch[idx]; | |
236 | 5 | memmove(chl->ch + idx, chl->ch + idx + 1, | |
237 | 5 | (chl->nb_ch - idx - 1) * sizeof(*chl->ch)); | |
238 | 5 | chl->nb_ch--; | |
239 | 5 | return ret; | |
240 | } | ||
241 | |||
242 | /* | ||
243 | * If ch is present in chl, remove it from the list and return it. | ||
244 | * Otherwise return AV_CHAN_NONE. | ||
245 | */ | ||
246 | 8 | static enum AVChannel channel_list_pop_ch(ChannelList *chl, enum AVChannel ch) | |
247 | { | ||
248 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6 times.
|
17 | for (int i = 0; i < chl->nb_ch; i++) |
249 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
|
11 | if (chl->ch[i] == ch) |
250 | 2 | return channel_list_pop(chl, i); | |
251 | 6 | return AV_CHAN_NONE; | |
252 | } | ||
253 | |||
254 | 5 | static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch, | |
255 | ChannelList *inputs) | ||
256 | { | ||
257 | int i; | ||
258 | |||
259 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3 times.
|
11 | for (i = 0; i < ctx->nb_inputs; i++) { |
260 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6 times.
|
8 | if (channel_list_pop_ch(&inputs[i], ch->out_channel) != AV_CHAN_NONE) { |
261 | 2 | ch->input = i; | |
262 | 2 | ch->in_channel = ch->out_channel; | |
263 | 2 | return; | |
264 | } | ||
265 | } | ||
266 | } | ||
267 | |||
268 | 3 | static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch, | |
269 | ChannelList *inputs) | ||
270 | { | ||
271 | int i; | ||
272 | |||
273 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | for (i = 0; i < ctx->nb_inputs; i++) { |
274 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (inputs[i].nb_ch) { |
275 | 3 | ch->input = i; | |
276 | 3 | ch->in_channel = channel_list_pop(&inputs[i], 0); | |
277 | 3 | return; | |
278 | } | ||
279 | } | ||
280 | } | ||
281 | |||
282 | 1 | static int join_config_output(AVFilterLink *outlink) | |
283 | { | ||
284 | 1 | AVFilterContext *ctx = outlink->src; | |
285 | 1 | JoinContext *s = ctx->priv; | |
286 | // unused channels from each input | ||
287 | ChannelList *inputs_unused; | ||
288 | char inbuf[64], outbuf[64]; | ||
289 | 1 | int i, ret = 0; | |
290 | |||
291 | /* initialize unused channel list for each input */ | ||
292 | 1 | inputs_unused = av_calloc(ctx->nb_inputs, sizeof(*inputs_unused)); | |
293 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!inputs_unused) |
294 | ✗ | return AVERROR(ENOMEM); | |
295 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 0; i < ctx->nb_inputs; i++) { |
296 | 2 | AVFilterLink *inlink = ctx->inputs[i]; | |
297 | 2 | AVChannelLayout *chl = &inlink->ch_layout; | |
298 | 2 | ChannelList *iu = &inputs_unused[i]; | |
299 | |||
300 | 2 | iu->nb_ch = chl->nb_channels; | |
301 | 2 | iu->ch = av_malloc_array(iu->nb_ch, sizeof(*iu->ch)); | |
302 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!iu->ch) { |
303 | ✗ | ret = AVERROR(ENOMEM); | |
304 | ✗ | goto fail; | |
305 | } | ||
306 | |||
307 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
|
7 | for (int ch_idx = 0; ch_idx < iu->nb_ch; ch_idx++) { |
308 | 5 | iu->ch[ch_idx] = av_channel_layout_channel_from_index(chl, ch_idx); | |
309 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (iu->ch[ch_idx] < 0) { |
310 | /* no channel ordering information in this input, | ||
311 | * so don't auto-map from it */ | ||
312 | ✗ | iu->nb_ch = 0; | |
313 | ✗ | break; | |
314 | } | ||
315 | } | ||
316 | } | ||
317 | |||
318 | /* process user-specified maps */ | ||
319 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
320 | 5 | ChannelMap *ch = &s->channels[i]; | |
321 | AVFilterLink *inlink; | ||
322 | AVChannelLayout *ichl; | ||
323 | ChannelList *iu; | ||
324 | |||
325 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (ch->input < 0) |
326 | 5 | continue; | |
327 | |||
328 | ✗ | inlink = ctx->inputs[ch->input]; | |
329 | ✗ | ichl = &inlink->ch_layout; | |
330 | ✗ | iu = &inputs_unused[ch->input]; | |
331 | |||
332 | /* get the index for the channels defined by name */ | ||
333 | ✗ | if (ch->in_channel != AV_CHAN_NONE) { | |
334 | ✗ | ch->in_channel_idx = av_channel_layout_index_from_channel(ichl, ch->in_channel); | |
335 | ✗ | if (ch->in_channel_idx < 0) { | |
336 | ✗ | av_channel_name(inbuf, sizeof(inbuf), ch->in_channel); | |
337 | ✗ | av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in " | |
338 | "input stream #%d.\n", inbuf, | ||
339 | ch->input); | ||
340 | ✗ | ret = AVERROR(EINVAL); | |
341 | ✗ | goto fail; | |
342 | } | ||
343 | } | ||
344 | |||
345 | /* make sure channels specified by index actually exist */ | ||
346 | ✗ | if (ch->in_channel_idx >= ichl->nb_channels) { | |
347 | ✗ | av_log(ctx, AV_LOG_ERROR, "Requested channel with index %d is not " | |
348 | "present in input stream #%d.\n", ch->in_channel_idx, ch->input); | ||
349 | ✗ | ret = AVERROR(EINVAL); | |
350 | ✗ | goto fail; | |
351 | } | ||
352 | |||
353 | ✗ | channel_list_pop_ch(iu, av_channel_layout_channel_from_index(ichl, ch->in_channel_idx)); | |
354 | } | ||
355 | |||
356 | /* guess channel maps when not explicitly defined */ | ||
357 | /* first try unused matching channels */ | ||
358 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
359 | 5 | ChannelMap *ch = &s->channels[i]; | |
360 | |||
361 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (ch->input < 0) |
362 | 5 | guess_map_matching(ctx, ch, inputs_unused); | |
363 | } | ||
364 | |||
365 | /* if the above failed, try to find _any_ unused input channel */ | ||
366 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
367 | 5 | ChannelMap *ch = &s->channels[i]; | |
368 | |||
369 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
|
5 | if (ch->input < 0) |
370 | 3 | guess_map_any(ctx, ch, inputs_unused); | |
371 | |||
372 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (ch->input < 0) { |
373 | ✗ | av_channel_name(outbuf, sizeof(outbuf), ch->out_channel); | |
374 | ✗ | av_log(ctx, AV_LOG_ERROR, "Could not find input channel for " | |
375 | "output channel '%s'.\n", | ||
376 | outbuf); | ||
377 | ✗ | ret = AVERROR(EINVAL); | |
378 | ✗ | goto fail; | |
379 | } | ||
380 | |||
381 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (ch->in_channel != AV_CHAN_NONE) { |
382 | 5 | ch->in_channel_idx = av_channel_layout_index_from_channel( | |
383 | 5 | &ctx->inputs[ch->input]->ch_layout, ch->in_channel); | |
384 | } | ||
385 | |||
386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | av_assert0(ch->in_channel_idx >= 0); |
387 | } | ||
388 | |||
389 | /* print mappings */ | ||
390 | 1 | av_log(ctx, AV_LOG_VERBOSE, "mappings: "); | |
391 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
392 | 5 | ChannelMap *ch = &s->channels[i]; | |
393 | 5 | AVFilterLink *inlink = ctx->inputs[ch->input]; | |
394 | 5 | AVChannelLayout *ichl = &inlink->ch_layout; | |
395 | 5 | enum AVChannel in_ch = av_channel_layout_channel_from_index( | |
396 | 5 | ichl, ch->in_channel_idx); | |
397 | |||
398 | 5 | av_channel_name(inbuf, sizeof(inbuf), in_ch); | |
399 | 5 | av_channel_name(outbuf, sizeof(outbuf), ch->out_channel); | |
400 | 5 | av_log(ctx, AV_LOG_VERBOSE, "%d.%s(%d) => %s(%d) ", ch->input, | |
401 | inbuf, ch->in_channel_idx, | ||
402 | outbuf, i); | ||
403 | } | ||
404 | 1 | av_log(ctx, AV_LOG_VERBOSE, "\n"); | |
405 | |||
406 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 0; i < ctx->nb_inputs; i++) { |
407 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (inputs_unused[i].nb_ch == ctx->inputs[i]->ch_layout.nb_channels) |
408 | ✗ | av_log(ctx, AV_LOG_WARNING, "No channels are used from input " | |
409 | "stream %d.\n", i); | ||
410 | } | ||
411 | |||
412 | 1 | fail: | |
413 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 0; i < ctx->nb_inputs; i++) |
414 | 2 | av_freep(&inputs_unused[i].ch); | |
415 | 1 | av_freep(&inputs_unused); | |
416 | 1 | return ret; | |
417 | } | ||
418 | |||
419 | 66 | static int try_push_frame(AVFilterContext *ctx) | |
420 | { | ||
421 | 66 | AVFilterLink *outlink = ctx->outputs[0]; | |
422 | 66 | JoinContext *s = ctx->priv; | |
423 | AVFrame *frame; | ||
424 | 66 | int linesize = INT_MAX; | |
425 | 66 | int nb_samples = INT_MAX; | |
426 | 66 | int nb_buffers = 0; | |
427 | int i, j, ret; | ||
428 | |||
429 |
2/2✓ Branch 0 taken 131 times.
✓ Branch 1 taken 65 times.
|
196 | for (i = 0; i < ctx->nb_inputs; i++) { |
430 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 130 times.
|
131 | if (!s->input_frames[i]) { |
431 | 1 | nb_samples = 0; | |
432 | 1 | break; | |
433 | } else { | ||
434 | 130 | nb_samples = FFMIN(nb_samples, s->input_frames[i]->nb_samples); | |
435 | } | ||
436 | } | ||
437 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 65 times.
|
66 | if (!nb_samples) |
438 | 1 | goto eof; | |
439 | |||
440 | /* setup the output frame */ | ||
441 | 65 | frame = av_frame_alloc(); | |
442 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (!frame) |
443 | ✗ | return AVERROR(ENOMEM); | |
444 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (s->ch_layout.nb_channels > FF_ARRAY_ELEMS(frame->data)) { |
445 | ✗ | frame->extended_data = av_calloc(s->ch_layout.nb_channels, | |
446 | sizeof(*frame->extended_data)); | ||
447 | ✗ | if (!frame->extended_data) { | |
448 | ✗ | ret = AVERROR(ENOMEM); | |
449 | ✗ | goto fail; | |
450 | } | ||
451 | } | ||
452 | |||
453 | /* copy the data pointers */ | ||
454 |
2/2✓ Branch 0 taken 325 times.
✓ Branch 1 taken 65 times.
|
390 | for (i = 0; i < s->ch_layout.nb_channels; i++) { |
455 | 325 | ChannelMap *ch = &s->channels[i]; | |
456 | 325 | AVFrame *cur = s->input_frames[ch->input]; | |
457 | AVBufferRef *buf; | ||
458 | |||
459 | 325 | frame->extended_data[i] = cur->extended_data[ch->in_channel_idx]; | |
460 | 325 | linesize = FFMIN(linesize, cur->linesize[0]); | |
461 | |||
462 | /* add the buffer where this plan is stored to the list if it's | ||
463 | * not already there */ | ||
464 | 325 | buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx); | |
465 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
|
325 | if (!buf) { |
466 | ✗ | ret = AVERROR(EINVAL); | |
467 | ✗ | goto fail; | |
468 | } | ||
469 |
2/2✓ Branch 0 taken 650 times.
✓ Branch 1 taken 325 times.
|
975 | for (j = 0; j < nb_buffers; j++) |
470 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
|
650 | if (s->buffers[j]->buffer == buf->buffer) |
471 | ✗ | break; | |
472 |
1/2✓ Branch 0 taken 325 times.
✗ Branch 1 not taken.
|
325 | if (j == i) |
473 | 325 | s->buffers[nb_buffers++] = buf; | |
474 | } | ||
475 | |||
476 | /* create references to the buffers we copied to output */ | ||
477 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) { |
478 | ✗ | frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf); | |
479 | ✗ | frame->extended_buf = av_calloc(frame->nb_extended_buf, | |
480 | sizeof(*frame->extended_buf)); | ||
481 | ✗ | if (!frame->extended_buf) { | |
482 | ✗ | frame->nb_extended_buf = 0; | |
483 | ✗ | ret = AVERROR(ENOMEM); | |
484 | ✗ | goto fail; | |
485 | } | ||
486 | } | ||
487 |
3/4✓ Branch 0 taken 390 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 325 times.
✓ Branch 3 taken 65 times.
|
390 | for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) { |
488 | 325 | frame->buf[i] = av_buffer_ref(s->buffers[i]); | |
489 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 325 times.
|
325 | if (!frame->buf[i]) { |
490 | ✗ | ret = AVERROR(ENOMEM); | |
491 | ✗ | goto fail; | |
492 | } | ||
493 | } | ||
494 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | for (i = 0; i < frame->nb_extended_buf; i++) { |
495 | ✗ | frame->extended_buf[i] = av_buffer_ref(s->buffers[i + | |
496 | FF_ARRAY_ELEMS(frame->buf)]); | ||
497 | ✗ | if (!frame->extended_buf[i]) { | |
498 | ✗ | ret = AVERROR(ENOMEM); | |
499 | ✗ | goto fail; | |
500 | } | ||
501 | } | ||
502 | |||
503 | 65 | frame->nb_samples = nb_samples; | |
504 | 65 | frame->duration = av_rescale_q(frame->nb_samples, | |
505 | av_make_q(1, outlink->sample_rate), | ||
506 | outlink->time_base); | ||
507 | |||
508 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
|
65 | if ((ret = av_channel_layout_copy(&frame->ch_layout, &outlink->ch_layout)) < 0) |
509 | ✗ | goto fail; | |
510 | 65 | frame->sample_rate = outlink->sample_rate; | |
511 | 65 | frame->format = outlink->format; | |
512 | 65 | frame->pts = s->input_frames[0]->pts; | |
513 | 65 | frame->linesize[0] = linesize; | |
514 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (frame->data != frame->extended_data) { |
515 | ✗ | memcpy(frame->data, frame->extended_data, sizeof(*frame->data) * | |
516 | ✗ | FFMIN(FF_ARRAY_ELEMS(frame->data), s->ch_layout.nb_channels)); | |
517 | } | ||
518 | |||
519 | 65 | s->eof_pts = frame->pts + av_rescale_q(frame->nb_samples, | |
520 | av_make_q(1, outlink->sample_rate), | ||
521 | outlink->time_base); | ||
522 | 65 | ret = ff_filter_frame(outlink, frame); | |
523 | |||
524 |
2/2✓ Branch 0 taken 130 times.
✓ Branch 1 taken 65 times.
|
195 | for (i = 0; i < ctx->nb_inputs; i++) |
525 | 130 | av_frame_free(&s->input_frames[i]); | |
526 | |||
527 | 65 | return ret; | |
528 | |||
529 | ✗ | fail: | |
530 | ✗ | av_frame_free(&frame); | |
531 | ✗ | return ret; | |
532 | 1 | eof: | |
533 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | for (i = 0; i < ctx->nb_inputs; i++) { |
534 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | if (s->eof && |
535 | 1 | ff_inlink_queued_samples(ctx->inputs[i]) <= 0 && | |
536 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | !s->input_frames[i]) { |
537 | 1 | ff_outlink_set_status(outlink, AVERROR_EOF, s->eof_pts); | |
538 | 1 | break; | |
539 | } | ||
540 | } | ||
541 | |||
542 | 1 | return 0; | |
543 | } | ||
544 | |||
545 | 196 | static int activate(AVFilterContext *ctx) | |
546 | { | ||
547 | 196 | JoinContext *s = ctx->priv; | |
548 | int i, ret, status; | ||
549 | 196 | int nb_samples = 0; | |
550 | int64_t pts; | ||
551 | |||
552 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 196 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
196 | FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx); |
553 | |||
554 |
2/2✓ Branch 0 taken 140 times.
✓ Branch 1 taken 56 times.
|
196 | if (!s->input_frames[0]) { |
555 | 140 | ret = ff_inlink_consume_frame(ctx->inputs[0], &s->input_frames[0]); | |
556 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
|
140 | if (ret < 0) { |
557 | ✗ | return ret; | |
558 |
3/4✓ Branch 0 taken 75 times.
✓ Branch 1 taken 65 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
|
140 | } else if (ret == 0 && ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) { |
559 | ✗ | s->eof |= status == AVERROR_EOF; | |
560 | } | ||
561 | |||
562 |
5/6✓ Branch 0 taken 139 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 65 times.
✓ Branch 5 taken 74 times.
✗ Branch 6 not taken.
|
140 | if (!s->eof && !s->input_frames[0] && ff_outlink_frame_wanted(ctx->outputs[0])) { |
563 | 74 | ff_inlink_request_frame(ctx->inputs[0]); | |
564 | 74 | return 0; | |
565 | } | ||
566 | } | ||
567 | |||
568 |
2/2✓ Branch 0 taken 121 times.
✓ Branch 1 taken 1 times.
|
122 | if (s->input_frames[0]) |
569 | 121 | nb_samples = s->input_frames[0]->nb_samples; | |
570 | |||
571 |
4/4✓ Branch 0 taken 122 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 121 times.
✓ Branch 3 taken 1 times.
|
187 | for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) { |
572 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 121 times.
|
121 | if (s->input_frames[i]) |
573 | ✗ | continue; | |
574 | 121 | ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->input_frames[i]); | |
575 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 121 times.
|
121 | if (ret < 0) { |
576 | ✗ | return ret; | |
577 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 120 times.
|
121 | } else if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) { |
578 | 1 | s->eof |= status == AVERROR_EOF; | |
579 | } | ||
580 | |||
581 |
4/4✓ Branch 0 taken 120 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 64 times.
|
121 | if (!s->eof && !s->input_frames[i]) { |
582 | 56 | ff_inlink_request_frame(ctx->inputs[i]); | |
583 | 56 | return 0; | |
584 | } | ||
585 | } | ||
586 | |||
587 | 66 | return try_push_frame(ctx); | |
588 | } | ||
589 | |||
590 | static const AVFilterPad avfilter_af_join_outputs[] = { | ||
591 | { | ||
592 | .name = "default", | ||
593 | .type = AVMEDIA_TYPE_AUDIO, | ||
594 | .config_props = join_config_output, | ||
595 | }, | ||
596 | }; | ||
597 | |||
598 | const AVFilter ff_af_join = { | ||
599 | .name = "join", | ||
600 | .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into " | ||
601 | "multi-channel output."), | ||
602 | .priv_size = sizeof(JoinContext), | ||
603 | .priv_class = &join_class, | ||
604 | .init = join_init, | ||
605 | .uninit = join_uninit, | ||
606 | .activate = activate, | ||
607 | .inputs = NULL, | ||
608 | FILTER_OUTPUTS(avfilter_af_join_outputs), | ||
609 | FILTER_QUERY_FUNC(join_query_formats), | ||
610 | .flags = AVFILTER_FLAG_DYNAMIC_INPUTS, | ||
611 | }; | ||
612 |