FFmpeg
Data Structures | Macros | Functions | Variables
af_amix.c File Reference
#include "libavutil/attributes.h"
#include "libavutil/audio_fifo.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/eval.h"
#include "libavutil/float_dsp.h"
#include "libavutil/mathematics.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/samplefmt.h"
#include "audio.h"
#include "avfilter.h"
#include "filters.h"

Go to the source code of this file.

Data Structures

struct  FrameInfo
 
struct  FrameList
 Linked list used to store timestamps and frame sizes of all frames in the FIFO for the first input. More...
 
struct  MixContext
 

Macros

#define INPUT_ON   1
 input is active More...
 
#define INPUT_EOF   2
 input has reached EOF (may still be active) More...
 
#define DURATION_LONGEST   0
 
#define DURATION_SHORTEST   1
 
#define DURATION_FIRST   2
 
#define OFFSET(x)   offsetof(MixContext, x)
 
#define A   AV_OPT_FLAG_AUDIO_PARAM
 
#define F   AV_OPT_FLAG_FILTERING_PARAM
 
#define T   AV_OPT_FLAG_RUNTIME_PARAM
 

Functions

static void frame_list_clear (FrameList *frame_list)
 
static int frame_list_next_frame_size (FrameList *frame_list)
 
static int64_t frame_list_next_pts (FrameList *frame_list)
 
static void frame_list_remove_samples (FrameList *frame_list, int nb_samples)
 
static int frame_list_add_frame (FrameList *frame_list, int nb_samples, int64_t pts)
 
 AVFILTER_DEFINE_CLASS (amix)
 
static void calculate_scales (MixContext *s, int nb_samples)
 Update the scaling factors to apply to each input during mixing. More...
 
static int config_output (AVFilterLink *outlink)
 
static int output_frame (AVFilterLink *outlink)
 Read samples from the input FIFOs, mix, and write to the output link. More...
 
static int request_samples (AVFilterContext *ctx, int min_samples)
 Requests a frame, if needed, from each input link other than the first. More...
 
static int calc_active_inputs (MixContext *s)
 Calculates the number of active inputs and determines EOF based on the duration option. More...
 
static int activate (AVFilterContext *ctx)
 
static void parse_weights (AVFilterContext *ctx)
 
static av_cold int init (AVFilterContext *ctx)
 
static av_cold void uninit (AVFilterContext *ctx)
 
static int process_command (AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
 

Variables

static const AVOption amix_options []
 
static const AVFilterPad avfilter_af_amix_outputs []
 
const AVFilter ff_af_amix
 

Detailed Description

Audio Mix Filter

Mixes audio from multiple sources into a single output. The channel layout, sample rate, and sample format will be the same for all inputs and the output.

Definition in file af_amix.c.

Macro Definition Documentation

◆ INPUT_ON

#define INPUT_ON   1

input is active

Definition at line 48 of file af_amix.c.

◆ INPUT_EOF

#define INPUT_EOF   2

input has reached EOF (may still be active)

Definition at line 49 of file af_amix.c.

◆ DURATION_LONGEST

#define DURATION_LONGEST   0

Definition at line 51 of file af_amix.c.

◆ DURATION_SHORTEST

#define DURATION_SHORTEST   1

Definition at line 52 of file af_amix.c.

◆ DURATION_FIRST

#define DURATION_FIRST   2

Definition at line 53 of file af_amix.c.

◆ OFFSET

#define OFFSET (   x)    offsetof(MixContext, x)

Definition at line 181 of file af_amix.c.

◆ A

#define A   AV_OPT_FLAG_AUDIO_PARAM

Definition at line 182 of file af_amix.c.

◆ F

Definition at line 183 of file af_amix.c.

◆ T

#define T   AV_OPT_FLAG_RUNTIME_PARAM

Definition at line 184 of file af_amix.c.

Function Documentation

◆ frame_list_clear()

static void frame_list_clear ( FrameList frame_list)
static

Definition at line 77 of file af_amix.c.

Referenced by frame_list_remove_samples(), and uninit().

◆ frame_list_next_frame_size()

static int frame_list_next_frame_size ( FrameList frame_list)
static

Definition at line 91 of file af_amix.c.

Referenced by activate(), and output_frame().

◆ frame_list_next_pts()

static int64_t frame_list_next_pts ( FrameList frame_list)
static

Definition at line 98 of file af_amix.c.

Referenced by output_frame().

◆ frame_list_remove_samples()

static void frame_list_remove_samples ( FrameList frame_list,
int  nb_samples 
)
static

Definition at line 105 of file af_amix.c.

Referenced by output_frame().

◆ frame_list_add_frame()

static int frame_list_add_frame ( FrameList frame_list,
int  nb_samples,
int64_t  pts 
)
static

Definition at line 132 of file af_amix.c.

Referenced by activate().

◆ AVFILTER_DEFINE_CLASS()

AVFILTER_DEFINE_CLASS ( amix  )

◆ calculate_scales()

static void calculate_scales ( MixContext s,
int  nb_samples 
)
static

Update the scaling factors to apply to each input during mixing.

This balances the full volume range between active inputs and handles volume transitions when EOF is encountered on an input but mixing continues with the remaining inputs.

Definition at line 212 of file af_amix.c.

Referenced by config_output(), output_frame(), and process_command().

◆ config_output()

static int config_output ( AVFilterLink outlink)
static

Definition at line 243 of file af_amix.c.

◆ output_frame()

static int output_frame ( AVFilterLink outlink)
static

Read samples from the input FIFOs, mix, and write to the output link.

Definition at line 296 of file af_amix.c.

Referenced by activate(), and request_samples().

◆ request_samples()

static int request_samples ( AVFilterContext ctx,
int  min_samples 
)
static

Requests a frame, if needed, from each input link other than the first.

Definition at line 394 of file af_amix.c.

Referenced by activate().

◆ calc_active_inputs()

static int calc_active_inputs ( MixContext s)
static

Calculates the number of active inputs and determines EOF based on the duration option.

Returns
0 if mixing should continue, or AVERROR_EOF if mixing should stop.

Definition at line 421 of file af_amix.c.

Referenced by activate().

◆ activate()

static int activate ( AVFilterContext ctx)
static

Definition at line 436 of file af_amix.c.

◆ parse_weights()

static void parse_weights ( AVFilterContext ctx)
static

Definition at line 517 of file af_amix.c.

Referenced by init(), and process_command().

◆ init()

static av_cold int init ( AVFilterContext ctx)
static

Definition at line 544 of file af_amix.c.

◆ uninit()

static av_cold void uninit ( AVFilterContext ctx)
static

Definition at line 574 of file af_amix.c.

◆ process_command()

static int process_command ( AVFilterContext ctx,
const char *  cmd,
const char *  args,
char *  res,
int  res_len,
int  flags 
)
static

Definition at line 593 of file af_amix.c.

Variable Documentation

◆ amix_options

const AVOption amix_options[]
static
Initial value:
= {
{ "inputs", "Number of inputs.",
OFFSET(nb_inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT16_MAX, A|F },
{ "duration", "How to determine the end-of-stream.",
OFFSET(duration_mode), AV_OPT_TYPE_INT, { .i64 = DURATION_LONGEST }, 0, 2, A|F, .unit = "duration" },
{ "longest", "Duration of longest input.", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_LONGEST }, 0, 0, A|F, .unit = "duration" },
{ "shortest", "Duration of shortest input.", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_SHORTEST }, 0, 0, A|F, .unit = "duration" },
{ "first", "Duration of first input.", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_FIRST }, 0, 0, A|F, .unit = "duration" },
{ "dropout_transition", "Transition time, in seconds, for volume "
"renormalization when an input stream ends.",
OFFSET(dropout_transition), AV_OPT_TYPE_FLOAT, { .dbl = 2.0 }, 0, INT_MAX, A|F },
{ "weights", "Set weight for each input.",
OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, A|F|T },
{ "normalize", "Scale inputs",
OFFSET(normalize), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A|F|T },
{ NULL }
}

Definition at line 185 of file af_amix.c.

◆ avfilter_af_amix_outputs

const AVFilterPad avfilter_af_amix_outputs[]
static
Initial value:
= {
{
.name = "default",
.config_props = config_output,
},
}

Definition at line 611 of file af_amix.c.

◆ ff_af_amix

const AVFilter ff_af_amix
Initial value:
= {
.name = "amix",
.description = NULL_IF_CONFIG_SMALL("Audio mixing."),
.priv_size = sizeof(MixContext),
.priv_class = &amix_class,
.init = init,
.process_command = process_command,
}

Definition at line 619 of file af_amix.c.

AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
DURATION_LONGEST
#define DURATION_LONGEST
Definition: af_amix.c:51
DURATION_FIRST
#define DURATION_FIRST
Definition: af_amix.c:53
MixContext
Definition: af_amix.c:157
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_amix.c:593
A
#define A
Definition: af_amix.c:182
AVFILTER_FLAG_DYNAMIC_INPUTS
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:141
avfilter_af_amix_outputs
static const AVFilterPad avfilter_af_amix_outputs[]
Definition: af_amix.c:611
FILTER_SAMPLEFMTS
#define FILTER_SAMPLEFMTS(...)
Definition: filters.h:250
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
NULL
#define NULL
Definition: coverity.c:32
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_amix.c:243
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_amix.c:574
activate
static int activate(AVFilterContext *ctx)
Definition: af_amix.c:436
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition: opt.h:271
normalize
Definition: normalize.py:1
F
#define F
Definition: af_amix.c:183
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
T
#define T
Definition: af_amix.c:184
DURATION_SHORTEST
#define DURATION_SHORTEST
Definition: af_amix.c:52
OFFSET
#define OFFSET(x)
Definition: af_amix.c:181
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_amix.c:544
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:61
AV_OPT_TYPE_STRING
@ 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...
Definition: opt.h:276
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60