... | ... |
@@ -133,6 +133,44 @@ For example to force the output to either unsigned 8-bit or signed 16-bit stereo |
133 | 133 |
aformat=sample_fmts\=u8\,s16:channel_layouts\=stereo |
134 | 134 |
@end example |
135 | 135 |
|
136 |
+@section amix |
|
137 |
+ |
|
138 |
+Mixes multiple audio inputs into a single output. |
|
139 |
+ |
|
140 |
+For example |
|
141 |
+@example |
|
142 |
+avconv -i INPUT1 -i INPUT2 -i INPUT3 -filter_complex amix=inputs=3:duration=first:dropout_transition=3 OUTPUT |
|
143 |
+@end example |
|
144 |
+will mix 3 input audio streams to a single output with the same duration as the |
|
145 |
+first input and a dropout transition time of 3 seconds. |
|
146 |
+ |
|
147 |
+The filter accepts the following named parameters: |
|
148 |
+@table @option |
|
149 |
+ |
|
150 |
+@item inputs |
|
151 |
+Number of inputs. If unspecified, it defaults to 2. |
|
152 |
+ |
|
153 |
+@item duration |
|
154 |
+How to determine the end-of-stream. |
|
155 |
+@table @option |
|
156 |
+ |
|
157 |
+@item longest |
|
158 |
+Duration of longest input. (default) |
|
159 |
+ |
|
160 |
+@item shortest |
|
161 |
+Duration of shortest input. |
|
162 |
+ |
|
163 |
+@item first |
|
164 |
+Duration of first input. |
|
165 |
+ |
|
166 |
+@end table |
|
167 |
+ |
|
168 |
+@item dropout_transition |
|
169 |
+Transition time, in seconds, for volume renormalization when an input |
|
170 |
+stream ends. The default value is 2 seconds. |
|
171 |
+ |
|
172 |
+@end table |
|
173 |
+ |
|
136 | 174 |
@section anull |
137 | 175 |
|
138 | 176 |
Pass the audio source unchanged to the output. |
... | ... |
@@ -25,6 +25,7 @@ OBJS = allfilters.o \ |
25 | 25 |
video.o \ |
26 | 26 |
|
27 | 27 |
OBJS-$(CONFIG_AFORMAT_FILTER) += af_aformat.o |
28 |
+OBJS-$(CONFIG_AMIX_FILTER) += af_amix.o |
|
28 | 29 |
OBJS-$(CONFIG_ANULL_FILTER) += af_anull.o |
29 | 30 |
OBJS-$(CONFIG_ASPLIT_FILTER) += split.o |
30 | 31 |
OBJS-$(CONFIG_ASYNCTS_FILTER) += af_asyncts.o |
31 | 32 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,545 @@ |
0 |
+/* |
|
1 |
+ * Audio Mix Filter |
|
2 |
+ * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com> |
|
3 |
+ * |
|
4 |
+ * This file is part of Libav. |
|
5 |
+ * |
|
6 |
+ * Libav is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * Libav is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with Libav; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+/** |
|
22 |
+ * @file |
|
23 |
+ * Audio Mix Filter |
|
24 |
+ * |
|
25 |
+ * Mixes audio from multiple sources into a single output. The channel layout, |
|
26 |
+ * sample rate, and sample format will be the same for all inputs and the |
|
27 |
+ * output. |
|
28 |
+ */ |
|
29 |
+ |
|
30 |
+#include "libavutil/audioconvert.h" |
|
31 |
+#include "libavutil/audio_fifo.h" |
|
32 |
+#include "libavutil/avassert.h" |
|
33 |
+#include "libavutil/avstring.h" |
|
34 |
+#include "libavutil/mathematics.h" |
|
35 |
+#include "libavutil/opt.h" |
|
36 |
+#include "libavutil/samplefmt.h" |
|
37 |
+ |
|
38 |
+#include "audio.h" |
|
39 |
+#include "avfilter.h" |
|
40 |
+#include "formats.h" |
|
41 |
+#include "internal.h" |
|
42 |
+ |
|
43 |
+#define INPUT_OFF 0 /**< input has reached EOF */ |
|
44 |
+#define INPUT_ON 1 /**< input is active */ |
|
45 |
+#define INPUT_INACTIVE 2 /**< input is on, but is currently inactive */ |
|
46 |
+ |
|
47 |
+#define DURATION_LONGEST 0 |
|
48 |
+#define DURATION_SHORTEST 1 |
|
49 |
+#define DURATION_FIRST 2 |
|
50 |
+ |
|
51 |
+ |
|
52 |
+typedef struct FrameInfo { |
|
53 |
+ int nb_samples; |
|
54 |
+ int64_t pts; |
|
55 |
+ struct FrameInfo *next; |
|
56 |
+} FrameInfo; |
|
57 |
+ |
|
58 |
+/** |
|
59 |
+ * Linked list used to store timestamps and frame sizes of all frames in the |
|
60 |
+ * FIFO for the first input. |
|
61 |
+ * |
|
62 |
+ * This is needed to keep timestamps synchronized for the case where multiple |
|
63 |
+ * input frames are pushed to the filter for processing before a frame is |
|
64 |
+ * requested by the output link. |
|
65 |
+ */ |
|
66 |
+typedef struct FrameList { |
|
67 |
+ int nb_frames; |
|
68 |
+ int nb_samples; |
|
69 |
+ FrameInfo *list; |
|
70 |
+ FrameInfo *end; |
|
71 |
+} FrameList; |
|
72 |
+ |
|
73 |
+static void frame_list_clear(FrameList *frame_list) |
|
74 |
+{ |
|
75 |
+ if (frame_list) { |
|
76 |
+ while (frame_list->list) { |
|
77 |
+ FrameInfo *info = frame_list->list; |
|
78 |
+ frame_list->list = info->next; |
|
79 |
+ av_free(info); |
|
80 |
+ } |
|
81 |
+ frame_list->nb_frames = 0; |
|
82 |
+ frame_list->nb_samples = 0; |
|
83 |
+ frame_list->end = NULL; |
|
84 |
+ } |
|
85 |
+} |
|
86 |
+ |
|
87 |
+static int frame_list_next_frame_size(FrameList *frame_list) |
|
88 |
+{ |
|
89 |
+ if (!frame_list->list) |
|
90 |
+ return 0; |
|
91 |
+ return frame_list->list->nb_samples; |
|
92 |
+} |
|
93 |
+ |
|
94 |
+static int64_t frame_list_next_pts(FrameList *frame_list) |
|
95 |
+{ |
|
96 |
+ if (!frame_list->list) |
|
97 |
+ return AV_NOPTS_VALUE; |
|
98 |
+ return frame_list->list->pts; |
|
99 |
+} |
|
100 |
+ |
|
101 |
+static void frame_list_remove_samples(FrameList *frame_list, int nb_samples) |
|
102 |
+{ |
|
103 |
+ if (nb_samples >= frame_list->nb_samples) { |
|
104 |
+ frame_list_clear(frame_list); |
|
105 |
+ } else { |
|
106 |
+ int samples = nb_samples; |
|
107 |
+ while (samples > 0) { |
|
108 |
+ FrameInfo *info = frame_list->list; |
|
109 |
+ av_assert0(info != NULL); |
|
110 |
+ if (info->nb_samples <= samples) { |
|
111 |
+ samples -= info->nb_samples; |
|
112 |
+ frame_list->list = info->next; |
|
113 |
+ if (!frame_list->list) |
|
114 |
+ frame_list->end = NULL; |
|
115 |
+ frame_list->nb_frames--; |
|
116 |
+ frame_list->nb_samples -= info->nb_samples; |
|
117 |
+ av_free(info); |
|
118 |
+ } else { |
|
119 |
+ info->nb_samples -= samples; |
|
120 |
+ info->pts += samples; |
|
121 |
+ frame_list->nb_samples -= samples; |
|
122 |
+ samples = 0; |
|
123 |
+ } |
|
124 |
+ } |
|
125 |
+ } |
|
126 |
+} |
|
127 |
+ |
|
128 |
+static int frame_list_add_frame(FrameList *frame_list, int nb_samples, int64_t pts) |
|
129 |
+{ |
|
130 |
+ FrameInfo *info = av_malloc(sizeof(*info)); |
|
131 |
+ if (!info) |
|
132 |
+ return AVERROR(ENOMEM); |
|
133 |
+ info->nb_samples = nb_samples; |
|
134 |
+ info->pts = pts; |
|
135 |
+ info->next = NULL; |
|
136 |
+ |
|
137 |
+ if (!frame_list->list) { |
|
138 |
+ frame_list->list = info; |
|
139 |
+ frame_list->end = info; |
|
140 |
+ } else { |
|
141 |
+ av_assert0(frame_list->end != NULL); |
|
142 |
+ frame_list->end->next = info; |
|
143 |
+ frame_list->end = info; |
|
144 |
+ } |
|
145 |
+ frame_list->nb_frames++; |
|
146 |
+ frame_list->nb_samples += nb_samples; |
|
147 |
+ |
|
148 |
+ return 0; |
|
149 |
+} |
|
150 |
+ |
|
151 |
+ |
|
152 |
+typedef struct MixContext { |
|
153 |
+ const AVClass *class; /**< class for AVOptions */ |
|
154 |
+ |
|
155 |
+ int nb_inputs; /**< number of inputs */ |
|
156 |
+ int active_inputs; /**< number of input currently active */ |
|
157 |
+ int duration_mode; /**< mode for determining duration */ |
|
158 |
+ float dropout_transition; /**< transition time when an input drops out */ |
|
159 |
+ |
|
160 |
+ int nb_channels; /**< number of channels */ |
|
161 |
+ int sample_rate; /**< sample rate */ |
|
162 |
+ AVAudioFifo **fifos; /**< audio fifo for each input */ |
|
163 |
+ uint8_t *input_state; /**< current state of each input */ |
|
164 |
+ float *input_scale; /**< mixing scale factor for each input */ |
|
165 |
+ float scale_norm; /**< normalization factor for all inputs */ |
|
166 |
+ int64_t next_pts; /**< calculated pts for next output frame */ |
|
167 |
+ FrameList *frame_list; /**< list of frame info for the first input */ |
|
168 |
+} MixContext; |
|
169 |
+ |
|
170 |
+#define OFFSET(x) offsetof(MixContext, x) |
|
171 |
+#define A AV_OPT_FLAG_AUDIO_PARAM |
|
172 |
+static const AVOption options[] = { |
|
173 |
+ { "inputs", "Number of inputs.", |
|
174 |
+ OFFSET(nb_inputs), AV_OPT_TYPE_INT, { 2 }, 1, 32, A }, |
|
175 |
+ { "duration", "How to determine the end-of-stream.", |
|
176 |
+ OFFSET(duration_mode), AV_OPT_TYPE_INT, { DURATION_LONGEST }, 0, 2, A, "duration" }, |
|
177 |
+ { "longest", "Duration of longest input.", 0, AV_OPT_TYPE_CONST, { DURATION_LONGEST }, INT_MIN, INT_MAX, A, "duration" }, |
|
178 |
+ { "shortest", "Duration of shortest input.", 0, AV_OPT_TYPE_CONST, { DURATION_SHORTEST }, INT_MIN, INT_MAX, A, "duration" }, |
|
179 |
+ { "first", "Duration of first input.", 0, AV_OPT_TYPE_CONST, { DURATION_FIRST }, INT_MIN, INT_MAX, A, "duration" }, |
|
180 |
+ { "dropout_transition", "Transition time, in seconds, for volume " |
|
181 |
+ "renormalization when an input stream ends.", |
|
182 |
+ OFFSET(dropout_transition), AV_OPT_TYPE_FLOAT, { 2.0 }, 0, INT_MAX, A }, |
|
183 |
+ { NULL }, |
|
184 |
+}; |
|
185 |
+ |
|
186 |
+static const AVClass amix_class = { |
|
187 |
+ .class_name = "amix filter", |
|
188 |
+ .item_name = av_default_item_name, |
|
189 |
+ .option = options, |
|
190 |
+ .version = LIBAVUTIL_VERSION_INT, |
|
191 |
+}; |
|
192 |
+ |
|
193 |
+ |
|
194 |
+/** |
|
195 |
+ * Update the scaling factors to apply to each input during mixing. |
|
196 |
+ * |
|
197 |
+ * This balances the full volume range between active inputs and handles |
|
198 |
+ * volume transitions when EOF is encountered on an input but mixing continues |
|
199 |
+ * with the remaining inputs. |
|
200 |
+ */ |
|
201 |
+static void calculate_scales(MixContext *s, int nb_samples) |
|
202 |
+{ |
|
203 |
+ int i; |
|
204 |
+ |
|
205 |
+ if (s->scale_norm > s->active_inputs) { |
|
206 |
+ s->scale_norm -= nb_samples / (s->dropout_transition * s->sample_rate); |
|
207 |
+ s->scale_norm = FFMAX(s->scale_norm, s->active_inputs); |
|
208 |
+ } |
|
209 |
+ |
|
210 |
+ for (i = 0; i < s->nb_inputs; i++) { |
|
211 |
+ if (s->input_state[i] == INPUT_ON) |
|
212 |
+ s->input_scale[i] = 1.0f / s->scale_norm; |
|
213 |
+ else |
|
214 |
+ s->input_scale[i] = 0.0f; |
|
215 |
+ } |
|
216 |
+} |
|
217 |
+ |
|
218 |
+static int config_output(AVFilterLink *outlink) |
|
219 |
+{ |
|
220 |
+ AVFilterContext *ctx = outlink->src; |
|
221 |
+ MixContext *s = ctx->priv; |
|
222 |
+ int i; |
|
223 |
+ char buf[64]; |
|
224 |
+ |
|
225 |
+ s->sample_rate = outlink->sample_rate; |
|
226 |
+ outlink->time_base = (AVRational){ 1, outlink->sample_rate }; |
|
227 |
+ s->next_pts = AV_NOPTS_VALUE; |
|
228 |
+ |
|
229 |
+ s->frame_list = av_mallocz(sizeof(*s->frame_list)); |
|
230 |
+ if (!s->frame_list) |
|
231 |
+ return AVERROR(ENOMEM); |
|
232 |
+ |
|
233 |
+ s->fifos = av_mallocz(s->nb_inputs * sizeof(*s->fifos)); |
|
234 |
+ if (!s->fifos) |
|
235 |
+ return AVERROR(ENOMEM); |
|
236 |
+ |
|
237 |
+ s->nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout); |
|
238 |
+ for (i = 0; i < s->nb_inputs; i++) { |
|
239 |
+ s->fifos[i] = av_audio_fifo_alloc(outlink->format, s->nb_channels, 1024); |
|
240 |
+ if (!s->fifos[i]) |
|
241 |
+ return AVERROR(ENOMEM); |
|
242 |
+ } |
|
243 |
+ |
|
244 |
+ s->input_state = av_malloc(s->nb_inputs); |
|
245 |
+ if (!s->input_state) |
|
246 |
+ return AVERROR(ENOMEM); |
|
247 |
+ memset(s->input_state, INPUT_ON, s->nb_inputs); |
|
248 |
+ s->active_inputs = s->nb_inputs; |
|
249 |
+ |
|
250 |
+ s->input_scale = av_mallocz(s->nb_inputs * sizeof(*s->input_scale)); |
|
251 |
+ if (!s->input_scale) |
|
252 |
+ return AVERROR(ENOMEM); |
|
253 |
+ s->scale_norm = s->active_inputs; |
|
254 |
+ calculate_scales(s, 0); |
|
255 |
+ |
|
256 |
+ av_get_channel_layout_string(buf, sizeof(buf), -1, outlink->channel_layout); |
|
257 |
+ |
|
258 |
+ av_log(ctx, AV_LOG_VERBOSE, |
|
259 |
+ "inputs:%d fmt:%s srate:%"PRId64" cl:%s\n", s->nb_inputs, |
|
260 |
+ av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf); |
|
261 |
+ |
|
262 |
+ return 0; |
|
263 |
+} |
|
264 |
+ |
|
265 |
+/* TODO: move optimized version from DSPContext to libavutil */ |
|
266 |
+static void vector_fmac_scalar(float *dst, const float *src, float mul, int len) |
|
267 |
+{ |
|
268 |
+ int i; |
|
269 |
+ for (i = 0; i < len; i++) |
|
270 |
+ dst[i] += src[i] * mul; |
|
271 |
+} |
|
272 |
+ |
|
273 |
+/** |
|
274 |
+ * Read samples from the input FIFOs, mix, and write to the output link. |
|
275 |
+ */ |
|
276 |
+static int output_frame(AVFilterLink *outlink, int nb_samples) |
|
277 |
+{ |
|
278 |
+ AVFilterContext *ctx = outlink->src; |
|
279 |
+ MixContext *s = ctx->priv; |
|
280 |
+ AVFilterBufferRef *out_buf, *in_buf; |
|
281 |
+ int i; |
|
282 |
+ |
|
283 |
+ calculate_scales(s, nb_samples); |
|
284 |
+ |
|
285 |
+ out_buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples); |
|
286 |
+ if (!out_buf) |
|
287 |
+ return AVERROR(ENOMEM); |
|
288 |
+ |
|
289 |
+ in_buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples); |
|
290 |
+ if (!in_buf) |
|
291 |
+ return AVERROR(ENOMEM); |
|
292 |
+ |
|
293 |
+ for (i = 0; i < s->nb_inputs; i++) { |
|
294 |
+ if (s->input_state[i] == INPUT_ON) { |
|
295 |
+ av_audio_fifo_read(s->fifos[i], (void **)in_buf->extended_data, |
|
296 |
+ nb_samples); |
|
297 |
+ vector_fmac_scalar((float *)out_buf->extended_data[0], |
|
298 |
+ (float *) in_buf->extended_data[0], |
|
299 |
+ s->input_scale[i], nb_samples * s->nb_channels); |
|
300 |
+ } |
|
301 |
+ } |
|
302 |
+ avfilter_unref_buffer(in_buf); |
|
303 |
+ |
|
304 |
+ out_buf->pts = s->next_pts; |
|
305 |
+ if (s->next_pts != AV_NOPTS_VALUE) |
|
306 |
+ s->next_pts += nb_samples; |
|
307 |
+ |
|
308 |
+ ff_filter_samples(outlink, out_buf); |
|
309 |
+ |
|
310 |
+ return 0; |
|
311 |
+} |
|
312 |
+ |
|
313 |
+/** |
|
314 |
+ * Returns the smallest number of samples available in the input FIFOs other |
|
315 |
+ * than that of the first input. |
|
316 |
+ */ |
|
317 |
+static int get_available_samples(MixContext *s) |
|
318 |
+{ |
|
319 |
+ int i; |
|
320 |
+ int available_samples = INT_MAX; |
|
321 |
+ |
|
322 |
+ av_assert0(s->nb_inputs > 1); |
|
323 |
+ |
|
324 |
+ for (i = 1; i < s->nb_inputs; i++) { |
|
325 |
+ int nb_samples; |
|
326 |
+ if (s->input_state[i] == INPUT_OFF) |
|
327 |
+ continue; |
|
328 |
+ nb_samples = av_audio_fifo_size(s->fifos[i]); |
|
329 |
+ available_samples = FFMIN(available_samples, nb_samples); |
|
330 |
+ } |
|
331 |
+ if (available_samples == INT_MAX) |
|
332 |
+ return 0; |
|
333 |
+ return available_samples; |
|
334 |
+} |
|
335 |
+ |
|
336 |
+/** |
|
337 |
+ * Requests a frame, if needed, from each input link other than the first. |
|
338 |
+ */ |
|
339 |
+static int request_samples(AVFilterContext *ctx, int min_samples) |
|
340 |
+{ |
|
341 |
+ MixContext *s = ctx->priv; |
|
342 |
+ int i, ret; |
|
343 |
+ |
|
344 |
+ av_assert0(s->nb_inputs > 1); |
|
345 |
+ |
|
346 |
+ for (i = 1; i < s->nb_inputs; i++) { |
|
347 |
+ ret = 0; |
|
348 |
+ if (s->input_state[i] == INPUT_OFF) |
|
349 |
+ continue; |
|
350 |
+ while (!ret && av_audio_fifo_size(s->fifos[i]) < min_samples) |
|
351 |
+ ret = avfilter_request_frame(ctx->inputs[i]); |
|
352 |
+ if (ret == AVERROR_EOF) { |
|
353 |
+ if (av_audio_fifo_size(s->fifos[i]) == 0) { |
|
354 |
+ s->input_state[i] = INPUT_OFF; |
|
355 |
+ continue; |
|
356 |
+ } |
|
357 |
+ } else if (ret) |
|
358 |
+ return ret; |
|
359 |
+ } |
|
360 |
+ return 0; |
|
361 |
+} |
|
362 |
+ |
|
363 |
+/** |
|
364 |
+ * Calculates the number of active inputs and determines EOF based on the |
|
365 |
+ * duration option. |
|
366 |
+ * |
|
367 |
+ * @return 0 if mixing should continue, or AVERROR_EOF if mixing should stop. |
|
368 |
+ */ |
|
369 |
+static int calc_active_inputs(MixContext *s) |
|
370 |
+{ |
|
371 |
+ int i; |
|
372 |
+ int active_inputs = 0; |
|
373 |
+ for (i = 0; i < s->nb_inputs; i++) |
|
374 |
+ active_inputs += !!(s->input_state[i] != INPUT_OFF); |
|
375 |
+ s->active_inputs = active_inputs; |
|
376 |
+ |
|
377 |
+ if (!active_inputs || |
|
378 |
+ (s->duration_mode == DURATION_FIRST && s->input_state[0] == INPUT_OFF) || |
|
379 |
+ (s->duration_mode == DURATION_SHORTEST && active_inputs != s->nb_inputs)) |
|
380 |
+ return AVERROR_EOF; |
|
381 |
+ return 0; |
|
382 |
+} |
|
383 |
+ |
|
384 |
+static int request_frame(AVFilterLink *outlink) |
|
385 |
+{ |
|
386 |
+ AVFilterContext *ctx = outlink->src; |
|
387 |
+ MixContext *s = ctx->priv; |
|
388 |
+ int ret; |
|
389 |
+ int wanted_samples, available_samples; |
|
390 |
+ |
|
391 |
+ if (s->input_state[0] == INPUT_OFF) { |
|
392 |
+ ret = request_samples(ctx, 1); |
|
393 |
+ if (ret < 0) |
|
394 |
+ return ret; |
|
395 |
+ |
|
396 |
+ ret = calc_active_inputs(s); |
|
397 |
+ if (ret < 0) |
|
398 |
+ return ret; |
|
399 |
+ |
|
400 |
+ available_samples = get_available_samples(s); |
|
401 |
+ if (!available_samples) |
|
402 |
+ return 0; |
|
403 |
+ |
|
404 |
+ return output_frame(outlink, available_samples); |
|
405 |
+ } |
|
406 |
+ |
|
407 |
+ if (s->frame_list->nb_frames == 0) { |
|
408 |
+ ret = avfilter_request_frame(ctx->inputs[0]); |
|
409 |
+ if (ret == AVERROR_EOF) { |
|
410 |
+ s->input_state[0] = INPUT_OFF; |
|
411 |
+ if (s->nb_inputs == 1) |
|
412 |
+ return AVERROR_EOF; |
|
413 |
+ else |
|
414 |
+ return AVERROR(EAGAIN); |
|
415 |
+ } else if (ret) |
|
416 |
+ return ret; |
|
417 |
+ } |
|
418 |
+ av_assert0(s->frame_list->nb_frames > 0); |
|
419 |
+ |
|
420 |
+ wanted_samples = frame_list_next_frame_size(s->frame_list); |
|
421 |
+ ret = request_samples(ctx, wanted_samples); |
|
422 |
+ if (ret < 0) |
|
423 |
+ return ret; |
|
424 |
+ |
|
425 |
+ ret = calc_active_inputs(s); |
|
426 |
+ if (ret < 0) |
|
427 |
+ return ret; |
|
428 |
+ |
|
429 |
+ if (s->active_inputs > 1) { |
|
430 |
+ available_samples = get_available_samples(s); |
|
431 |
+ if (!available_samples) |
|
432 |
+ return 0; |
|
433 |
+ available_samples = FFMIN(available_samples, wanted_samples); |
|
434 |
+ } else { |
|
435 |
+ available_samples = wanted_samples; |
|
436 |
+ } |
|
437 |
+ |
|
438 |
+ s->next_pts = frame_list_next_pts(s->frame_list); |
|
439 |
+ frame_list_remove_samples(s->frame_list, available_samples); |
|
440 |
+ |
|
441 |
+ return output_frame(outlink, available_samples); |
|
442 |
+} |
|
443 |
+ |
|
444 |
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf) |
|
445 |
+{ |
|
446 |
+ AVFilterContext *ctx = inlink->dst; |
|
447 |
+ MixContext *s = ctx->priv; |
|
448 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
449 |
+ int i; |
|
450 |
+ |
|
451 |
+ for (i = 0; i < ctx->input_count; i++) |
|
452 |
+ if (ctx->inputs[i] == inlink) |
|
453 |
+ break; |
|
454 |
+ if (i >= ctx->input_count) { |
|
455 |
+ av_log(ctx, AV_LOG_ERROR, "unknown input link\n"); |
|
456 |
+ return; |
|
457 |
+ } |
|
458 |
+ |
|
459 |
+ if (i == 0) { |
|
460 |
+ int64_t pts = av_rescale_q(buf->pts, inlink->time_base, |
|
461 |
+ outlink->time_base); |
|
462 |
+ frame_list_add_frame(s->frame_list, buf->audio->nb_samples, pts); |
|
463 |
+ } |
|
464 |
+ |
|
465 |
+ av_audio_fifo_write(s->fifos[i], (void **)buf->extended_data, |
|
466 |
+ buf->audio->nb_samples); |
|
467 |
+ |
|
468 |
+ avfilter_unref_buffer(buf); |
|
469 |
+} |
|
470 |
+ |
|
471 |
+static int init(AVFilterContext *ctx, const char *args, void *opaque) |
|
472 |
+{ |
|
473 |
+ MixContext *s = ctx->priv; |
|
474 |
+ int i, ret; |
|
475 |
+ |
|
476 |
+ s->class = &amix_class; |
|
477 |
+ av_opt_set_defaults(s); |
|
478 |
+ |
|
479 |
+ if ((ret = av_set_options_string(s, args, "=", ":")) < 0) { |
|
480 |
+ av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args); |
|
481 |
+ return ret; |
|
482 |
+ } |
|
483 |
+ av_opt_free(s); |
|
484 |
+ |
|
485 |
+ for (i = 0; i < s->nb_inputs; i++) { |
|
486 |
+ char name[32]; |
|
487 |
+ AVFilterPad pad = { 0 }; |
|
488 |
+ |
|
489 |
+ snprintf(name, sizeof(name), "input%d", i); |
|
490 |
+ pad.type = AVMEDIA_TYPE_AUDIO; |
|
491 |
+ pad.name = av_strdup(name); |
|
492 |
+ pad.filter_samples = filter_samples; |
|
493 |
+ |
|
494 |
+ avfilter_insert_inpad(ctx, i, &pad); |
|
495 |
+ } |
|
496 |
+ |
|
497 |
+ return 0; |
|
498 |
+} |
|
499 |
+ |
|
500 |
+static void uninit(AVFilterContext *ctx) |
|
501 |
+{ |
|
502 |
+ int i; |
|
503 |
+ MixContext *s = ctx->priv; |
|
504 |
+ |
|
505 |
+ if (s->fifos) { |
|
506 |
+ for (i = 0; i < s->nb_inputs; i++) |
|
507 |
+ av_audio_fifo_free(s->fifos[i]); |
|
508 |
+ av_freep(&s->fifos); |
|
509 |
+ } |
|
510 |
+ frame_list_clear(s->frame_list); |
|
511 |
+ av_freep(&s->frame_list); |
|
512 |
+ av_freep(&s->input_state); |
|
513 |
+ av_freep(&s->input_scale); |
|
514 |
+ |
|
515 |
+ for (i = 0; i < ctx->input_count; i++) |
|
516 |
+ av_freep(&ctx->input_pads[i].name); |
|
517 |
+} |
|
518 |
+ |
|
519 |
+static int query_formats(AVFilterContext *ctx) |
|
520 |
+{ |
|
521 |
+ AVFilterFormats *formats = NULL; |
|
522 |
+ avfilter_add_format(&formats, AV_SAMPLE_FMT_FLT); |
|
523 |
+ avfilter_set_common_formats(ctx, formats); |
|
524 |
+ ff_set_common_channel_layouts(ctx, ff_all_channel_layouts()); |
|
525 |
+ ff_set_common_samplerates(ctx, ff_all_samplerates()); |
|
526 |
+ return 0; |
|
527 |
+} |
|
528 |
+ |
|
529 |
+AVFilter avfilter_af_amix = { |
|
530 |
+ .name = "amix", |
|
531 |
+ .description = NULL_IF_CONFIG_SMALL("Audio mixing."), |
|
532 |
+ .priv_size = sizeof(MixContext), |
|
533 |
+ |
|
534 |
+ .init = init, |
|
535 |
+ .uninit = uninit, |
|
536 |
+ .query_formats = query_formats, |
|
537 |
+ |
|
538 |
+ .inputs = (const AVFilterPad[]) {{ .name = NULL}}, |
|
539 |
+ .outputs = (const AVFilterPad[]) {{ .name = "default", |
|
540 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
541 |
+ .config_props = config_output, |
|
542 |
+ .request_frame = request_frame }, |
|
543 |
+ { .name = NULL}}, |
|
544 |
+}; |
... | ... |
@@ -35,6 +35,7 @@ void avfilter_register_all(void) |
35 | 35 |
initialized = 1; |
36 | 36 |
|
37 | 37 |
REGISTER_FILTER (AFORMAT, aformat, af); |
38 |
+ REGISTER_FILTER (AMIX, amix, af); |
|
38 | 39 |
REGISTER_FILTER (ANULL, anull, af); |
39 | 40 |
REGISTER_FILTER (ASPLIT, asplit, af); |
40 | 41 |
REGISTER_FILTER (ASYNCTS, asyncts, af); |
... | ... |
@@ -29,7 +29,7 @@ |
29 | 29 |
#include "libavutil/avutil.h" |
30 | 30 |
|
31 | 31 |
#define LIBAVFILTER_VERSION_MAJOR 2 |
32 |
-#define LIBAVFILTER_VERSION_MINOR 19 |
|
32 |
+#define LIBAVFILTER_VERSION_MINOR 20 |
|
33 | 33 |
#define LIBAVFILTER_VERSION_MICRO 0 |
34 | 34 |
|
35 | 35 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |