af_amix.c File Reference

Audio Mix Filter. More...

#include "libavutil/audio_fifo.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/float_dsp.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libavutil/samplefmt.h"
#include "audio.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
Include dependency graph for af_amix.c:

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_OFF   0
 input has reached EOF More...
 
#define INPUT_ON   1
 input is active More...
 
#define INPUT_INACTIVE   2
 input is on, but is currently inactive 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
 

Typedefs

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

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, int nb_samples)
 Read samples from the input FIFOs, mix, and write to the output link. More...
 
static int get_available_samples (MixContext *s)
 Returns the smallest number of samples available in the input FIFOs other than that of the first input. 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 request_frame (AVFilterLink *outlink)
 
static int filter_frame (AVFilterLink *inlink, AVFrame *buf)
 
static int init (AVFilterContext *ctx)
 
static void uninit (AVFilterContext *ctx)
 
static int query_formats (AVFilterContext *ctx)
 

Variables

static const AVOption amix_options []
 
static const AVFilterPad avfilter_af_amix_outputs []
 
AVFilter avfilter_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

#define A   AV_OPT_FLAG_AUDIO_PARAM

Definition at line 176 of file af_amix.c.

#define DURATION_FIRST   2

Definition at line 52 of file af_amix.c.

Referenced by calc_active_inputs().

#define DURATION_LONGEST   0

Definition at line 50 of file af_amix.c.

#define DURATION_SHORTEST   1

Definition at line 51 of file af_amix.c.

Referenced by calc_active_inputs().

Definition at line 177 of file af_amix.c.

#define INPUT_INACTIVE   2

input is on, but is currently inactive

Definition at line 48 of file af_amix.c.

#define INPUT_OFF   0

input has reached EOF

Definition at line 46 of file af_amix.c.

Referenced by calc_active_inputs(), get_available_samples(), request_frame(), and request_samples().

#define INPUT_ON   1

input is active

Definition at line 47 of file af_amix.c.

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

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

Definition at line 175 of file af_amix.c.

Typedef Documentation

typedef struct FrameInfo FrameInfo
typedef struct FrameList FrameList

Linked list used to store timestamps and frame sizes of all frames in the FIFO for the first input.

This is needed to keep timestamps synchronized for the case where multiple input frames are pushed to the filter for processing before a frame is requested by the output link.

typedef struct MixContext MixContext

Function Documentation

AVFILTER_DEFINE_CLASS ( amix  )
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 371 of file af_amix.c.

Referenced by request_frame().

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 201 of file af_amix.c.

Referenced by config_output(), and output_frame().

static int config_output ( AVFilterLink outlink)
static

Definition at line 218 of file af_amix.c.

static int filter_frame ( AVFilterLink inlink,
AVFrame buf 
)
static

Definition at line 453 of file af_amix.c.

Referenced by init().

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

Definition at line 131 of file af_amix.c.

Referenced by filter_frame().

static void frame_list_clear ( FrameList frame_list)
static

Definition at line 76 of file af_amix.c.

Referenced by frame_list_remove_samples(), and uninit().

static int frame_list_next_frame_size ( FrameList frame_list)
static

Definition at line 90 of file af_amix.c.

Referenced by request_frame().

static int64_t frame_list_next_pts ( FrameList frame_list)
static

Definition at line 97 of file af_amix.c.

Referenced by request_frame().

static void frame_list_remove_samples ( FrameList frame_list,
int  nb_samples 
)
static

Definition at line 104 of file af_amix.c.

Referenced by request_frame().

static int get_available_samples ( MixContext s)
static

Returns the smallest number of samples available in the input FIFOs other than that of the first input.

Definition at line 319 of file af_amix.c.

Referenced by request_frame().

static int init ( AVFilterContext ctx)
static

Definition at line 486 of file af_amix.c.

static int output_frame ( AVFilterLink outlink,
int  nb_samples 
)
static

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

Definition at line 269 of file af_amix.c.

Referenced by request_frame().

static int query_formats ( AVFilterContext ctx)
static

Definition at line 527 of file af_amix.c.

static int request_frame ( AVFilterLink outlink)
static

Definition at line 386 of file af_amix.c.

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 341 of file af_amix.c.

Referenced by request_frame().

static void uninit ( AVFilterContext ctx)
static

Definition at line 508 of file af_amix.c.

Variable Documentation

const AVOption amix_options[]
static
Initial value:
= {
{ "inputs", "Number of inputs.",
OFFSET(nb_inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, 32, A|F },
{ "duration", "How to determine the end-of-stream.",
OFFSET(duration_mode), AV_OPT_TYPE_INT, { .i64 = DURATION_LONGEST }, 0, 2, A|F, "duration" },
{ "longest", "Duration of longest input.", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_LONGEST }, INT_MIN, INT_MAX, A|F, "duration" },
{ "shortest", "Duration of shortest input.", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_SHORTEST }, INT_MIN, INT_MAX, A|F, "duration" },
{ "first", "Duration of first input.", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_FIRST }, INT_MIN, INT_MAX, A|F, "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 },
{ NULL },
}
#define DURATION_LONGEST
Definition: af_amix.c:50
#define A
Definition: af_amix.c:176
#define OFFSET(x)
Definition: af_amix.c:175
NULL
Definition: eval.c:55
#define DURATION_SHORTEST
Definition: af_amix.c:51
#define F
Definition: af_amix.c:177
#define DURATION_FIRST
Definition: af_amix.c:52

Definition at line 178 of file af_amix.c.

AVFilter avfilter_af_amix
Initial value:
= {
.name = "amix",
.description = NULL_IF_CONFIG_SMALL("Audio mixing."),
.priv_size = sizeof(MixContext),
.priv_class = &amix_class,
.init = init,
}
static void uninit(AVFilterContext *ctx)
Definition: af_amix.c:508
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:424
struct MixContext MixContext
static const AVFilterPad avfilter_af_amix_outputs[]
Definition: af_amix.c:538
static int query_formats(AVFilterContext *ctx)
Definition: af_amix.c:527
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
NULL
Definition: eval.c:55
static int flags
Definition: cpu.c:23
static int init(AVFilterContext *ctx)
Definition: af_amix.c:486
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 at line 548 of file af_amix.c.

const AVFilterPad avfilter_af_amix_outputs[]
static
Initial value:
= {
{
.name = "default",
.config_props = config_output,
.request_frame = request_frame
},
{ NULL }
}
NULL
Definition: eval.c:55
static int request_frame(AVFilterLink *outlink)
Definition: af_amix.c:386
static int config_output(AVFilterLink *outlink)
Definition: af_amix.c:218

Definition at line 538 of file af_amix.c.