FFmpeg: libavfilter/af_amix.c Source File
Go to the documentation of this file. 1 /*
2 * Audio Mix Filter
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Audio Mix Filter
25 *
26 * Mixes audio from multiple sources into a single output. The channel layout,
27 * sample rate, and sample format will be the same for all inputs and the
28 * output.
29 */
30
43
47
48 #define INPUT_ON 1 /**< input is active */
49 #define INPUT_EOF 2 /**< input has reached EOF (may still be active) */
50
51 #define DURATION_LONGEST 0
52 #define DURATION_SHORTEST 1
53 #define DURATION_FIRST 2
54
55
61
62 /**
63 * Linked list used to store timestamps and frame sizes of all frames in the
64 * FIFO for the first input.
65 *
66 * This is needed to keep timestamps synchronized for the case where multiple
67 * input frames are pushed to the filter for processing before a frame is
68 * requested by the output link.
69 */
76
78 {
79 if (frame_list) {
80 while (frame_list->
list) {
84 }
88 }
89 }
90
92 {
93 if (!frame_list->
list)
94 return 0;
96 }
97
99 {
100 if (!frame_list->
list)
103 }
104
106 {
109 } else {
117 if (!frame_list->
list)
122 } else {
127 }
128 }
129 }
130 }
131
133 {
137 info->nb_samples = nb_samples;
140
141 if (!frame_list->
list) {
144 } else {
148 }
151
152 return 0;
153 }
154
155 /* FIXME: use directly links fifo */
156
158 const AVClass *
class;
/**< class for AVOptions */
160
165 char *
weights_str;
/**< string for custom weights for every input */
167
174 float *
weights;
/**< custom weights for every input */
175 float weight_sum;
/**< sum of custom weights for every input */
176 float *
scale_norm;
/**< normalization factor for every input */
180
181 #define OFFSET(x) offsetof(MixContext, x)
182 #define A AV_OPT_FLAG_AUDIO_PARAM
183 #define F AV_OPT_FLAG_FILTERING_PARAM
184 #define T AV_OPT_FLAG_RUNTIME_PARAM
186 { "inputs", "Number of inputs.",
188 { "duration", "How to determine the end-of-stream.",
193 { "dropout_transition", "Transition time, in seconds, for volume "
194 "renormalization when an input stream ends.",
196 { "weights", "Set weight for each input.",
198 { "normalize", "Scale inputs",
201 };
202
204
205 /**
206 * Update the scaling factors to apply to each input during mixing.
207 *
208 * This balances the full volume range between active inputs and handles
209 * volume transitions when EOF is encountered on an input but mixing continues
210 * with the remaining inputs.
211 */
213 {
214 float weight_sum = 0.f;
216
217 for (
i = 0;
i <
s->nb_inputs;
i++)
219 weight_sum +=
FFABS(
s->weights[
i]);
220
221 for (
i = 0;
i <
s->nb_inputs;
i++) {
223 if (
s->scale_norm[
i] > weight_sum /
FFABS(
s->weights[
i])) {
224 s->scale_norm[
i] -= ((
s->weight_sum /
FFABS(
s->weights[
i])) /
s->nb_inputs) *
225 nb_samples / (
s->dropout_transition *
s->sample_rate);
226 s->scale_norm[
i] =
FFMAX(
s->scale_norm[
i], weight_sum /
FFABS(
s->weights[
i]));
227 }
228 }
229 }
230
231 for (
i = 0;
i <
s->nb_inputs;
i++) {
234 s->input_scale[
i] =
FFABS(
s->weights[
i]);
235 else
236 s->input_scale[
i] = 1.0f /
s->scale_norm[
i] *
FFSIGN(
s->weights[
i]);
237 } else {
238 s->input_scale[
i] = 0.0f;
239 }
240 }
241 }
242
244 {
248 char buf[64];
249
254
258
262
264 for (
i = 0;
i <
s->nb_inputs;
i++) {
268 }
269
273 memset(
s->input_state,
INPUT_ON,
s->nb_inputs);
274 s->active_inputs =
s->nb_inputs;
275
276 s->input_scale =
av_calloc(
s->nb_inputs,
sizeof(*
s->input_scale));
277 s->scale_norm =
av_calloc(
s->nb_inputs,
sizeof(*
s->scale_norm));
278 if (!
s->input_scale || !
s->scale_norm)
280 for (
i = 0;
i <
s->nb_inputs;
i++)
281 s->scale_norm[
i] =
s->weight_sum /
FFABS(
s->weights[
i]);
283
285
287 "inputs:%d fmt:%s srate:%d cl:%s\n",
s->nb_inputs,
289
290 return 0;
291 }
292
293 /**
294 * Read samples from the input FIFOs, mix, and write to the output link.
295 */
297 {
301 int nb_samples,
ns,
i;
302
304 /* first input live: use the corresponding frame size */
306 for (
i = 1;
i <
s->nb_inputs;
i++) {
309 if (
ns < nb_samples) {
311 /* unclosed input with not enough samples */
312 return 0;
313 /* closed input to drain */
315 }
316 }
317 }
318
320 } else {
321 /* first input closed: use the available samples */
322 nb_samples = INT_MAX;
323 for (
i = 1;
i <
s->nb_inputs;
i++) {
326 nb_samples =
FFMIN(nb_samples,
ns);
327 }
328 }
329 if (nb_samples == INT_MAX) {
331 return 0;
332 }
333 }
334
336
338
339 if (nb_samples == 0)
340 return 0;
341
343 if (!out_buf)
345
347 if (!in_buf) {
350 }
351
352 for (
i = 0;
i <
s->nb_inputs;
i++) {
355
357 nb_samples);
358
359 planes =
s->planar ?
s->nb_channels : 1;
360 plane_size = nb_samples * (
s->planar ? 1 :
s->nb_channels);
361 plane_size =
FFALIGN(plane_size, 16);
362
368 s->input_scale[
i], plane_size);
369 }
370 } else {
374 s->input_scale[
i], plane_size);
375 }
376 }
377 }
378 }
380
381 out_buf->
pts =
s->next_pts;
384
386 s->next_pts += nb_samples;
387
389 }
390
391 /**
392 * Requests a frame, if needed, from each input link other than the first.
393 */
395 {
398
402
403 for (
i = 1;
i <
s->nb_inputs;
i++) {
406 continue;
408 continue;
410 return 0;
411 }
413 }
414
415 /**
416 * Calculates the number of active inputs and determines EOF based on the
417 * duration option.
418 *
419 * @return 0 if mixing should continue, or AVERROR_EOF if mixing should stop.
420 */
422 {
424 int active_inputs = 0;
425 for (
i = 0;
i <
s->nb_inputs;
i++)
426 active_inputs += !!(
s->input_state[
i] &
INPUT_ON);
427 s->active_inputs = active_inputs;
428
429 if (!active_inputs ||
433 return 0;
434 }
435
437 {
442
444
445 for (
i = 0;
i <
s->nb_inputs;
i++) {
447
456 }
457 }
458
464 }
465
467
471 }
472 }
473
474 for (
i = 0;
i <
s->nb_inputs;
i++) {
477
483 if (
s->nb_inputs == 1) {
485 return 0;
486 }
487 }
488 }
489 }
490 }
491
494 return 0;
495 }
496
498 int wanted_samples;
499
502
503 if (
s->frame_list->nb_frames == 0) {
505 return 0;
506 }
508
510
512 }
513
514 return 0;
515 }
516
518 {
520 float last_weight = 1.f;
523
526 for (
i = 0;
i <
s->nb_inputs;
i++) {
528 s->weights[
i] = last_weight;
529 s->weight_sum +=
FFABS(last_weight);
532 } else {
534 break;
535 }
536 }
537
538 for (;
i <
s->nb_inputs;
i++) {
539 s->weights[
i] = last_weight;
540 s->weight_sum +=
FFABS(last_weight);
541 }
542 }
543
545 {
548
549 for (
i = 0;
i <
s->nb_inputs;
i++) {
551
556
559 }
560
564
565 s->weights =
av_calloc(
s->nb_inputs,
sizeof(*
s->weights));
568
570
571 return 0;
572 }
573
575 {
578
580 for (
i = 0;
i <
s->nb_inputs;
i++)
583 }
591 }
592
594 char *res,
int res_len,
int flags)
595 {
598
602
604 for (
int i = 0;
i <
s->nb_inputs;
i++)
605 s->scale_norm[
i] =
s->weight_sum /
FFABS(
s->weights[
i]);
607
608 return 0;
609 }
610
612 {
616 },
617 };
618
622 .p.priv_class = &amix_class,
633 };
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
@ AV_SAMPLE_FMT_FLTP
float, planar
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int64_t duration
Duration of the frame, in the same units as pts.
#define AVERROR_EOF
End of file.
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
char * av_asprintf(const char *fmt,...)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
static const struct @532 planes[]
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
#define AV_LOG_VERBOSE
Detailed information.
const char * name
Filter name.
int nb_channels
Number of channels in this layout.
A link between two filters.
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
#define INPUT_ON
input is active
float * input_scale
mixing scale factor for each input
const FFFilter ff_af_amix
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
#define INPUT_EOF
input has reached EOF (may still be active)
int sample_rate
sample rate
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Context for an Audio FIFO Buffer.
FrameList * frame_list
list of frame info for the first input
int normalize
if inputs are scaled
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
A filter pad used for either input or output.
static const AVFilterPad avfilter_af_amix_outputs[]
static const AVOption amix_options[]
#define FILTER_SAMPLEFMTS(...)
static void calculate_scales(MixContext *s, int nb_samples)
Update the scaling factors to apply to each input during mixing.
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int active_inputs
number of input currently active
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
#define FILTER_OUTPUTS(array)
int duration_mode
mode for determining duration
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Describe the class of an AVClass context structure.
int nb_channels
number of channels
float dropout_transition
transition time when an input drops out
Rational number (pair of numerator and denominator).
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p)
AVAudioFifo ** fifos
audio fifo for each input
Linked list used to store timestamps and frame sizes of all frames in the FIFO for the first input.
uint8_t * input_state
current state of each input
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
static int frame_list_add_frame(FrameList *frame_list, int nb_samples, int64_t pts)
static int config_output(AVFilterLink *outlink)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static av_cold void uninit(AVFilterContext *ctx)
int format
agreed upon media format
int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples)
Read data from an AVAudioFifo.
static AVRational av_make_q(int num, int den)
Create an AVRational.
#define AV_NOPTS_VALUE
Undefined timestamp value.
float * scale_norm
normalization factor for every input
char * weights_str
string for custom weights for every input
static int output_frame(AVFilterLink *outlink)
Read samples from the input FIFOs, mix, and write to the output link.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
static int activate(AVFilterContext *ctx)
AVFilterContext * src
source filter
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
static int64_t frame_list_next_pts(FrameList *frame_list)
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
float weight_sum
sum of custom weights for every input
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
int64_t next_pts
calculated pts for next output frame
int sample_rate
samples per second
int nb_samples
number of audio samples (per channel) described by this frame
#define i(width, name, range_min, range_max)
uint8_t ** extended_data
pointers to the data planes/channels.
static int frame_list_next_frame_size(FrameList *frame_list)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const char * name
Pad name.
void * av_calloc(size_t nmemb, size_t size)
enum AVMediaType type
AVFilterPad type.
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
float * weights
custom weights for every input
static int calc_active_inputs(MixContext *s)
Calculates the number of active inputs and determines EOF based on the duration option.
@ AV_OPT_TYPE_INT
Underlying C type is int.
AVFILTER_DEFINE_CLASS(amix)
@ AV_SAMPLE_FMT_DBLP
double, planar
Filter the word "frame" indicates either a video frame or a group of audio samples
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
AVFilter p
The public AVFilter.
static int request_samples(AVFilterContext *ctx, int min_samples)
Requests a frame, if needed, from each input link other than the first.
#define DURATION_SHORTEST
AVChannelLayout ch_layout
channel layout of current buffer (see libavutil/channel_layout.h)
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
static av_cold int init(AVFilterContext *ctx)
static void frame_list_clear(FrameList *frame_list)
static void parse_weights(AVFilterContext *ctx)
#define ns(max_value, name, subs,...)
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
@ AV_SAMPLE_FMT_DBL
double
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
static void frame_list_remove_samples(FrameList *frame_list, int nb_samples)
int nb_inputs
number of inputs
Generated on Sat Oct 18 2025 19:22:53 for FFmpeg by
doxygen
1.8.17