27#include "config_components.h"
70#define OFFSET(x) offsetof(SidechainCompressContext, x)
71#define A AV_OPT_FLAG_AUDIO_PARAM
72#define F AV_OPT_FLAG_FILTERING_PARAM
73#define R AV_OPT_FLAG_RUNTIME_PARAM
98 "acompressor/sidechaincompress",
102#define FAKE_INFINITY (65536.0 * 65536.0)
105#define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
107static double output_gain(
double lin_slope,
double ratio,
double thres,
108 double knee,
double knee_start,
double knee_stop,
109 double compressed_knee_start,
110 double compressed_knee_stop,
111 int detection,
int mode)
113 double slope = log(lin_slope);
124 gain = (slope - thres) / ratio + thres;
129 if (knee > 1.0 && slope > knee_start)
131 knee_stop, compressed_knee_start,
134 if (knee > 1.0 && slope < knee_stop)
136 knee_start, compressed_knee_stop,
140 return exp(gain - slope);
148 s->thres = log(
s->threshold);
149 s->lin_knee_start =
s->threshold / sqrt(
s->knee);
150 s->lin_knee_stop =
s->threshold * sqrt(
s->knee);
151 s->adj_knee_start =
s->lin_knee_start *
s->lin_knee_start;
152 s->adj_knee_stop =
s->lin_knee_stop *
s->lin_knee_stop;
153 s->knee_start = log(
s->lin_knee_start);
154 s->knee_stop = log(
s->lin_knee_stop);
155 s->compressed_knee_start = (
s->knee_start -
s->thres) /
s->ratio +
s->thres;
156 s->compressed_knee_stop = (
s->knee_stop -
s->thres) /
s->ratio +
s->thres;
165 const double *
src,
double *
dst,
const double *scsrc,
int nb_samples,
166 double level_in,
double level_sc,
169 const double makeup =
s->makeup;
170 const double mix =
s->mix;
173 for (
i = 0;
i < nb_samples;
i++) {
174 double abs_sample, gain = 1.0;
178 abs_sample =
fabs(scsrc[0] * level_sc);
182 abs_sample =
FFMAX(
fabs(scsrc[
c] * level_sc), abs_sample);
185 abs_sample +=
fabs(scsrc[
c] * level_sc);
191 abs_sample *= abs_sample;
193 s->lin_slope += (abs_sample -
s->lin_slope) * (abs_sample >
s->lin_slope ?
s->attack_coeff :
s->release_coeff);
196 detector = (
s->detection ?
s->adj_knee_stop :
s->lin_knee_stop);
197 detected =
s->lin_slope < detector;
199 detector = (
s->detection ?
s->adj_knee_start :
s->lin_knee_start);
200 detected =
s->lin_slope > detector;
203 if (
s->lin_slope > 0.0 && detected)
205 s->knee_start,
s->knee_stop,
206 s->compressed_knee_start,
207 s->compressed_knee_stop,
208 s->detection,
s->mode);
220 char *res,
int res_len,
int flags)
233#if CONFIG_SIDECHAINCOMPRESS_FILTER
238 int ret,
i, nb_samples;
262 for (
i = 0;
i < 2;
i++) {
273 dst = (
double *)
out->data[0];
278 (
double *)in[1]->
data[0], nb_samples,
279 s->level_in,
s->level_sc,
280 ctx->inputs[0],
ctx->inputs[1]);
332 if (!
s->fifo[0] || !
s->fifo[1])
348static const AVFilterPad sidechaincompress_inputs[] = {
358static const AVFilterPad sidechaincompress_outputs[] = {
367 .p.name =
"sidechaincompress",
369 .p.priv_class = &sidechaincompress_acompressor_class,
380#if CONFIG_ACOMPRESSOR_FILTER
383 const double *
src = (
const double *)in->
data[0];
400 dst = (
double *)
out->data[0];
403 s->level_in,
s->level_in,
415 .filter_frame = acompressor_filter_frame,
428 .p.name =
"acompressor",
430 .p.priv_class = &sidechaincompress_acompressor_class,
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static enum AVSampleFormat sample_fmts[]
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static double output_gain(double lin_slope, double ratio, double thres, double knee, double knee_start, double knee_stop, double compressed_knee_start, double compressed_knee_stop, int detection, int mode)
static void compressor(SidechainCompressContext *s, const double *src, double *dst, const double *scsrc, int nb_samples, double level_in, double level_sc, AVFilterLink *inlink, AVFilterLink *sclink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int compressor_config_output(AVFilterLink *outlink)
#define IS_FAKE_INFINITY(value)
const FFFilter ff_af_acompressor
const FFFilter ff_af_sidechaincompress
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
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.
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Main libavfilter public API header.
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
Public libavutil channel layout APIs header.
common internal and external API header
static __device__ float fabs(float a)
static const uint16_t channel_layouts[7]
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples)
Read data from an AVAudioFifo.
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_DBL
double
static double hermite_interpolation(double x, double x0, double x1, double p0, double p1, double m0, double m1)
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
static int mix(int c0, int c1)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
#define FILTER_QUERY_FUNC2(func)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Context for an Audio FIFO Buffer.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
A link between two filters.
AVFilterContext * src
source filter
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
int sample_rate
samples per second
AVChannelLayout ch_layout
channel layout of current buffer (see libavutil/channel_layout.h)
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Rational number (pair of numerator and denominator).
double compressed_knee_stop
double compressed_knee_start
static AVFormatContext * ctx