FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_sidechaincompress.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
3  * Copyright (c) 2015 Paul B Mahol
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  * Sidechain compressor filter
25  */
26 
27 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
31 
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 
37 typedef struct SidechainCompressContext {
38  const AVClass *class;
39 
42  double lin_slope;
43  double ratio;
44  double threshold;
45  double makeup;
46  double thres;
47  double knee;
48  double knee_start;
49  double knee_stop;
52  int link;
53  int detection;
54 
57 
58 #define OFFSET(x) offsetof(SidechainCompressContext, x)
59 #define A AV_OPT_FLAG_AUDIO_PARAM
60 #define F AV_OPT_FLAG_FILTERING_PARAM
61 
63  { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563, 1, A|F },
64  { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 20, A|F },
65  { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 2000, A|F },
66  { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A|F },
67  { "makeup", "set make up gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 64, A|F },
68  { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.82843}, 1, 8, A|F },
69  { "link", "set link type", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A|F, "link" },
70  { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "link" },
71  { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "link" },
72  { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, A|F, "detection" },
73  { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "detection" },
74  { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "detection" },
75  { NULL }
76 };
77 
78 AVFILTER_DEFINE_CLASS(sidechaincompress);
79 
80 static av_cold int init(AVFilterContext *ctx)
81 {
83 
84  s->thres = log(s->threshold);
85  s->lin_knee_start = s->threshold / sqrt(s->knee);
86  s->knee_start = log(s->lin_knee_start);
87  s->knee_stop = log(s->threshold * sqrt(s->knee));
88  s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
89 
90  return 0;
91 }
92 
93 static inline float hermite_interpolation(float x, float x0, float x1,
94  float p0, float p1,
95  float m0, float m1)
96 {
97  float width = x1 - x0;
98  float t = (x - x0) / width;
99  float t2, t3;
100  float ct0, ct1, ct2, ct3;
101 
102  m0 *= width;
103  m1 *= width;
104 
105  t2 = t*t;
106  t3 = t2*t;
107  ct0 = p0;
108  ct1 = m0;
109 
110  ct2 = -3 * p0 - 2 * m0 + 3 * p1 - m1;
111  ct3 = 2 * p0 + m0 - 2 * p1 + m1;
112 
113  return ct3 * t3 + ct2 * t2 + ct1 * t + ct0;
114 }
115 
116 // A fake infinity value (because real infinity may break some hosts)
117 #define FAKE_INFINITY (65536.0 * 65536.0)
118 
119 // Check for infinity (with appropriate-ish tolerance)
120 #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
121 
122 static double output_gain(double lin_slope, double ratio, double thres,
123  double knee, double knee_start, double knee_stop,
124  double compressed_knee_stop, int detection)
125 {
126  double slope = log(lin_slope);
127  double gain = 0.0;
128  double delta = 0.0;
129 
130  if (detection)
131  slope *= 0.5;
132 
133  if (IS_FAKE_INFINITY(ratio)) {
134  gain = thres;
135  delta = 0.0;
136  } else {
137  gain = (slope - thres) / ratio + thres;
138  delta = 1.0 / ratio;
139  }
140 
141  if (knee > 1.0 && slope < knee_stop)
142  gain = hermite_interpolation(slope, knee_start, knee_stop,
143  knee_start, compressed_knee_stop,
144  1.0, delta);
145 
146  return exp(gain - slope);
147 }
148 
150 {
151  AVFilterContext *ctx = link->dst;
153  AVFilterLink *sclink = ctx->inputs[1];
154  AVFilterLink *outlink = ctx->outputs[0];
155  const double makeup = s->makeup;
156  const double *scsrc;
157  double *sample;
158  int nb_samples;
159  int ret, i, c;
160 
161  for (i = 0; i < 2; i++)
162  if (link == ctx->inputs[i])
163  break;
164  av_assert0(i < 2 && !s->input_frame[i]);
165  s->input_frame[i] = frame;
166 
167  if (!s->input_frame[0] || !s->input_frame[1])
168  return 0;
169 
170  nb_samples = FFMIN(s->input_frame[0]->nb_samples,
171  s->input_frame[1]->nb_samples);
172 
173  sample = (double *)s->input_frame[0]->data[0];
174  scsrc = (const double *)s->input_frame[1]->data[0];
175 
176  for (i = 0; i < nb_samples; i++) {
177  double abs_sample, gain = 1.0;
178 
179  abs_sample = FFABS(scsrc[0]);
180 
181  if (s->link == 1) {
182  for (c = 1; c < sclink->channels; c++)
183  abs_sample = FFMAX(FFABS(scsrc[c]), abs_sample);
184  } else {
185  for (c = 1; c < sclink->channels; c++)
186  abs_sample += FFABS(scsrc[c]);
187 
188  abs_sample /= sclink->channels;
189  }
190 
191  if (s->detection)
192  abs_sample *= abs_sample;
193 
194  s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
195 
196  if (s->lin_slope > 0.0 && s->lin_slope > s->lin_knee_start)
197  gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
198  s->knee_start, s->knee_stop,
200 
201  for (c = 0; c < outlink->channels; c++)
202  sample[c] *= gain * makeup;
203 
204  sample += outlink->channels;
205  scsrc += sclink->channels;
206  }
207 
208  ret = ff_filter_frame(outlink, s->input_frame[0]);
209 
210  s->input_frame[0] = NULL;
211  av_frame_free(&s->input_frame[1]);
212 
213  return ret;
214 }
215 
216 static int request_frame(AVFilterLink *outlink)
217 {
218  AVFilterContext *ctx = outlink->src;
220  int i, ret;
221 
222  /* get a frame on each input */
223  for (i = 0; i < 2; i++) {
224  AVFilterLink *inlink = ctx->inputs[i];
225  if (!s->input_frame[i] &&
226  (ret = ff_request_frame(inlink)) < 0)
227  return ret;
228 
229  /* request the same number of samples on all inputs */
230  if (i == 0)
231  ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
232  }
233 
234  return 0;
235 }
236 
238 {
241  static const enum AVSampleFormat sample_fmts[] = {
244  };
245  int ret, i;
246 
247  if (!ctx->inputs[0]->in_channel_layouts ||
249  av_log(ctx, AV_LOG_WARNING,
250  "No channel layout for input 1\n");
251  return AVERROR(EAGAIN);
252  }
253 
255  if (!layouts)
256  return AVERROR(ENOMEM);
258 
259  for (i = 0; i < 2; i++) {
260  layouts = ff_all_channel_layouts();
261  if (!layouts)
262  return AVERROR(ENOMEM);
264  }
265 
266  formats = ff_make_format_list(sample_fmts);
267  if (!formats)
268  return AVERROR(ENOMEM);
269  ret = ff_set_common_formats(ctx, formats);
270  if (ret < 0)
271  return ret;
272 
273  formats = ff_all_samplerates();
274  if (!formats)
275  return AVERROR(ENOMEM);
276  return ff_set_common_samplerates(ctx, formats);
277 }
278 
279 static int config_output(AVFilterLink *outlink)
280 {
281  AVFilterContext *ctx = outlink->src;
283 
284  if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
285  av_log(ctx, AV_LOG_ERROR,
286  "Inputs must have the same sample rate "
287  "%d for in0 vs %d for in1\n",
288  ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
289  return AVERROR(EINVAL);
290  }
291 
292  outlink->sample_rate = ctx->inputs[0]->sample_rate;
293  outlink->time_base = ctx->inputs[0]->time_base;
294  outlink->channel_layout = ctx->inputs[0]->channel_layout;
295  outlink->channels = ctx->inputs[0]->channels;
296 
297  s->attack_coeff = FFMIN(1.f, 1.f / (s->attack * outlink->sample_rate / 4000.f));
298  s->release_coeff = FFMIN(1.f, 1.f / (s->release * outlink->sample_rate / 4000.f));
299 
300  return 0;
301 }
302 
304  {
305  .name = "main",
306  .type = AVMEDIA_TYPE_AUDIO,
307  .filter_frame = filter_frame,
308  .needs_writable = 1,
309  .needs_fifo = 1,
310  },{
311  .name = "sidechain",
312  .type = AVMEDIA_TYPE_AUDIO,
313  .filter_frame = filter_frame,
314  .needs_fifo = 1,
315  },
316  { NULL }
317 };
318 
320  {
321  .name = "default",
322  .type = AVMEDIA_TYPE_AUDIO,
323  .config_props = config_output,
324  .request_frame = request_frame,
325  },
326  { NULL }
327 };
328 
330  .name = "sidechaincompress",
331  .description = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
332  .priv_size = sizeof(SidechainCompressContext),
333  .priv_class = &sidechaincompress_class,
334  .init = init,
336  .inputs = sidechaincompress_inputs,
337  .outputs = sidechaincompress_outputs,
338 };
static const AVFilterPad sidechaincompress_outputs[]
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
static double output_gain(double lin_slope, double ratio, double thres, double knee, double knee_start, double knee_stop, double compressed_knee_stop, int detection)
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
static int filter_frame(AVFilterLink *link, AVFrame *frame)
#define F
static enum AVSampleFormat formats[]
#define sample
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
static int query_formats(AVFilterContext *ctx)
const char * name
Pad name.
Definition: internal.h:69
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:417
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1158
#define av_cold
Definition: attributes.h:74
float delta
AVOptions.
static AVFrame * frame
#define A
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:63
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:542
static int request_frame(AVFilterLink *outlink)
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:329
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
uint64_t * channel_layouts
list of channel layouts
Definition: formats.h:86
#define OFFSET(x)
simple assert() macros that are a bit more flexible than ISO C assert().
static const AVOption sidechaincompress_options[]
#define t3
Definition: regdef.h:31
#define FFMAX(a, b)
Definition: common.h:79
AVFILTER_DEFINE_CLASS(sidechaincompress)
static const AVFilterPad sidechaincompress_inputs[]
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:81
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:385
static float hermite_interpolation(float x, float x0, float x1, float p0, float p1, float m0, float m1)
A list of supported channel layouts.
Definition: formats.h:85
#define IS_FAKE_INFINITY(value)
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
const char * name
Filter name.
Definition: avfilter.h:474
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:379
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
common internal and external API header
int nb_channel_layouts
number of channel layouts
Definition: formats.h:87
static double c[64]
AVFilter ff_af_sidechaincompress
static av_cold int init(AVFilterContext *ctx)
static int config_output(AVFilterLink *outlink)
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:633
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:343
internal API functions
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:530
#define t2
Definition: regdef.h:30
static int width