2 * Copyright (c) 1999 Chris Bagwell
3 * Copyright (c) 1999 Nick Bailey
4 * Copyright (c) 2007 Rob Sykes <robs@users.sourceforge.net>
5 * Copyright (c) 2013 Paul B Mahol
6 * Copyright (c) 2014 Andrew Kelley
8 * This file is part of libav.
10 * Libav is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * Libav is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * audio compand filter
32 #include "libavutil/avstring.h"
33 #include "libavutil/channel_layout.h"
34 #include "libavutil/common.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/opt.h"
43 typedef struct ChanParam {
49 typedef struct CompandSegment {
54 typedef struct CompandContext {
58 char *attacks, *decays, *points;
59 CompandSegment *segments;
65 double initial_volume;
73 int (*compand)(AVFilterContext *ctx, AVFrame *frame);
76 #define OFFSET(x) offsetof(CompandContext, x)
77 #define A AV_OPT_FLAG_AUDIO_PARAM
79 static const AVOption compand_options[] = {
80 { "attacks", "set time over which increase of volume is determined", OFFSET(attacks), AV_OPT_TYPE_STRING, { .str = "0.3" }, 0, 0, A },
81 { "decays", "set time over which decrease of volume is determined", OFFSET(decays), AV_OPT_TYPE_STRING, { .str = "0.8" }, 0, 0, A },
82 { "points", "set points of transfer function", OFFSET(points), AV_OPT_TYPE_STRING, { .str = "-70/-70|-60/-20" }, 0, 0, A },
83 { "soft-knee", "set soft-knee", OFFSET(curve_dB), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.01, 900, A },
84 { "gain", "set output gain", OFFSET(gain_dB), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -900, 900, A },
85 { "volume", "set initial volume", OFFSET(initial_volume), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -900, 0, A },
86 { "delay", "set delay for samples before sending them to volume adjuster", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, 20, A },
90 static const AVClass compand_class = {
91 .class_name = "compand filter",
92 .item_name = av_default_item_name,
93 .option = compand_options,
94 .version = LIBAVUTIL_VERSION_INT,
97 static av_cold int init(AVFilterContext *ctx)
99 CompandContext *s = ctx->priv;
100 s->pts = AV_NOPTS_VALUE;
104 static av_cold void uninit(AVFilterContext *ctx)
106 CompandContext *s = ctx->priv;
108 av_freep(&s->channels);
109 av_freep(&s->segments);
110 av_frame_free(&s->delay_frame);
113 static int query_formats(AVFilterContext *ctx)
115 AVFilterChannelLayouts *layouts;
116 AVFilterFormats *formats;
117 static const enum AVSampleFormat sample_fmts[] = {
122 layouts = ff_all_channel_layouts();
124 return AVERROR(ENOMEM);
125 ff_set_common_channel_layouts(ctx, layouts);
127 formats = ff_make_format_list(sample_fmts);
129 return AVERROR(ENOMEM);
130 ff_set_common_formats(ctx, formats);
132 formats = ff_all_samplerates();
134 return AVERROR(ENOMEM);
135 ff_set_common_samplerates(ctx, formats);
140 static void count_items(char *item_str, int *nb_items)
145 for (p = item_str; *p; p++) {
151 static void update_volume(ChanParam *cp, float in)
153 float delta = in - cp->volume;
156 cp->volume += delta * cp->attack;
158 cp->volume += delta * cp->decay;
161 static float get_volume(CompandContext *s, float in_lin)
164 float in_log, out_log;
167 if (in_lin < s->in_min_lin)
168 return s->out_min_lin;
170 in_log = logf(in_lin);
172 for (i = 1; i < s->nb_segments; i++)
173 if (in_log <= s->segments[i].x)
175 cs = &s->segments[i - 1];
177 out_log = cs->y + in_log * (cs->a * in_log + cs->b);
179 return expf(out_log);
182 static int compand_nodelay(AVFilterContext *ctx, AVFrame *frame)
184 CompandContext *s = ctx->priv;
185 AVFilterLink *inlink = ctx->inputs[0];
186 const int channels = s->nb_channels;
187 const int nb_samples = frame->nb_samples;
192 if (av_frame_is_writable(frame)) {
195 out_frame = ff_get_audio_buffer(inlink, nb_samples);
197 av_frame_free(&frame);
198 return AVERROR(ENOMEM);
200 err = av_frame_copy_props(out_frame, frame);
202 av_frame_free(&out_frame);
203 av_frame_free(&frame);
208 for (chan = 0; chan < channels; chan++) {
209 const float *src = (float *)frame->extended_data[chan];
210 float *dst = (float *)out_frame->extended_data[chan];
211 ChanParam *cp = &s->channels[chan];
213 for (i = 0; i < nb_samples; i++) {
214 update_volume(cp, fabs(src[i]));
216 dst[i] = av_clipf(src[i] * get_volume(s, cp->volume), -1.0f, 1.0f);
220 if (frame != out_frame)
221 av_frame_free(&frame);
223 return ff_filter_frame(ctx->outputs[0], out_frame);
226 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
228 static int compand_delay(AVFilterContext *ctx, AVFrame *frame)
230 CompandContext *s = ctx->priv;
231 AVFilterLink *inlink = ctx->inputs[0];
232 const int channels = s->nb_channels;
233 const int nb_samples = frame->nb_samples;
234 int chan, i, dindex = 0, oindex, count = 0;
235 AVFrame *out_frame = NULL;
238 if (s->pts == AV_NOPTS_VALUE) {
239 s->pts = (frame->pts == AV_NOPTS_VALUE) ? 0 : frame->pts;
242 for (chan = 0; chan < channels; chan++) {
243 AVFrame *delay_frame = s->delay_frame;
244 const float *src = (float *)frame->extended_data[chan];
245 float *dbuf = (float *)delay_frame->extended_data[chan];
246 ChanParam *cp = &s->channels[chan];
249 count = s->delay_count;
250 dindex = s->delay_index;
251 for (i = 0, oindex = 0; i < nb_samples; i++) {
252 const float in = src[i];
253 update_volume(cp, fabs(in));
255 if (count >= s->delay_samples) {
257 out_frame = ff_get_audio_buffer(inlink, nb_samples - i);
259 av_frame_free(&frame);
260 return AVERROR(ENOMEM);
262 err = av_frame_copy_props(out_frame, frame);
264 av_frame_free(&out_frame);
265 av_frame_free(&frame);
268 out_frame->pts = s->pts;
269 s->pts += av_rescale_q(nb_samples - i,
270 (AVRational){ 1, inlink->sample_rate },
274 dst = (float *)out_frame->extended_data[chan];
275 dst[oindex++] = av_clipf(dbuf[dindex] *
276 get_volume(s, cp->volume), -1.0f, 1.0f);
282 dindex = MOD(dindex + 1, s->delay_samples);
286 s->delay_count = count;
287 s->delay_index = dindex;
289 av_frame_free(&frame);
290 return out_frame ? ff_filter_frame(ctx->outputs[0], out_frame) : 0;
293 static int compand_drain(AVFilterLink *outlink)
295 AVFilterContext *ctx = outlink->src;
296 CompandContext *s = ctx->priv;
297 const int channels = s->nb_channels;
298 AVFrame *frame = NULL;
301 /* 2048 is to limit output frame size during drain */
302 frame = ff_get_audio_buffer(outlink, FFMIN(2048, s->delay_count));
304 return AVERROR(ENOMEM);
306 s->pts += av_rescale_q(frame->nb_samples,
307 (AVRational){ 1, outlink->sample_rate }, outlink->time_base);
309 for (chan = 0; chan < channels; chan++) {
310 AVFrame *delay_frame = s->delay_frame;
311 float *dbuf = (float *)delay_frame->extended_data[chan];
312 float *dst = (float *)frame->extended_data[chan];
313 ChanParam *cp = &s->channels[chan];
315 dindex = s->delay_index;
316 for (i = 0; i < frame->nb_samples; i++) {
317 dst[i] = av_clipf(dbuf[dindex] * get_volume(s, cp->volume),
319 dindex = MOD(dindex + 1, s->delay_samples);
322 s->delay_count -= frame->nb_samples;
323 s->delay_index = dindex;
325 return ff_filter_frame(outlink, frame);
328 static int config_output(AVFilterLink *outlink)
330 AVFilterContext *ctx = outlink->src;
331 CompandContext *s = ctx->priv;
332 const int sample_rate = outlink->sample_rate;
333 double radius = s->curve_dB * M_LN10 / 20.0;
336 av_get_channel_layout_nb_channels(outlink->channel_layout);
337 int nb_attacks, nb_decays, nb_points;
338 int new_nb_items, num;
343 count_items(s->attacks, &nb_attacks);
344 count_items(s->decays, &nb_decays);
345 count_items(s->points, &nb_points);
348 av_log(ctx, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
349 return AVERROR(EINVAL);
352 if (nb_attacks > channels || nb_decays > channels) {
353 av_log(ctx, AV_LOG_ERROR,
354 "Number of attacks/decays bigger than number of channels.\n");
355 return AVERROR(EINVAL);
360 s->nb_channels = channels;
361 s->channels = av_mallocz_array(channels, sizeof(*s->channels));
362 s->nb_segments = (nb_points + 4) * 2;
363 s->segments = av_mallocz_array(s->nb_segments, sizeof(*s->segments));
365 if (!s->channels || !s->segments) {
367 return AVERROR(ENOMEM);
371 for (i = 0, new_nb_items = 0; i < nb_attacks; i++) {
372 char *tstr = av_get_token(&p, "|");
374 return AVERROR(ENOMEM);
376 new_nb_items += sscanf(tstr, "%f", &s->channels[i].attack) == 1;
378 if (s->channels[i].attack < 0) {
380 return AVERROR(EINVAL);
385 nb_attacks = new_nb_items;
388 for (i = 0, new_nb_items = 0; i < nb_decays; i++) {
389 char *tstr = av_get_token(&p, "|");
391 return AVERROR(ENOMEM);
392 new_nb_items += sscanf(tstr, "%f", &s->channels[i].decay) == 1;
394 if (s->channels[i].decay < 0) {
396 return AVERROR(EINVAL);
401 nb_decays = new_nb_items;
403 if (nb_attacks != nb_decays) {
404 av_log(ctx, AV_LOG_ERROR,
405 "Number of attacks %d differs from number of decays %d.\n",
406 nb_attacks, nb_decays);
408 return AVERROR(EINVAL);
411 #define S(x) s->segments[2 * ((x) + 1)]
413 for (i = 0, new_nb_items = 0; i < nb_points; i++) {
414 char *tstr = av_get_token(&p, "|");
416 return AVERROR(ENOMEM);
418 err = sscanf(tstr, "%f/%f", &S(i).x, &S(i).y);
421 av_log(ctx, AV_LOG_ERROR,
422 "Invalid and/or missing input/output value.\n");
424 return AVERROR(EINVAL);
426 if (i && S(i - 1).x > S(i).x) {
427 av_log(ctx, AV_LOG_ERROR,
428 "Transfer function input values must be increasing.\n");
430 return AVERROR(EINVAL);
433 av_log(ctx, AV_LOG_DEBUG, "%d: x=%f y=%f\n", i, S(i).x, S(i).y);
440 /* Add 0,0 if necessary */
441 if (num == 0 || S(num - 1).x)
445 #define S(x) s->segments[2 * (x)]
446 /* Add a tail off segment at the start */
447 S(0).x = S(1).x - 2 * s->curve_dB;
451 /* Join adjacent colinear segments */
452 for (i = 2; i < num; i++) {
453 double g1 = (S(i - 1).y - S(i - 2).y) * (S(i - 0).x - S(i - 1).x);
454 double g2 = (S(i - 0).y - S(i - 1).y) * (S(i - 1).x - S(i - 2).x);
457 /* here we purposefully lose precision so that we can compare floats */
461 for (j = --i; j < num; j++)
465 for (i = 0; !i || s->segments[i - 2].x; i += 2) {
466 s->segments[i].y += s->gain_dB;
467 s->segments[i].x *= M_LN10 / 20;
468 s->segments[i].y *= M_LN10 / 20;
471 #define L(x) s->segments[i - (x)]
472 for (i = 4; s->segments[i - 2].x; i += 2) {
473 double x, y, cx, cy, in1, in2, out1, out2, theta, len, r;
476 L(4).b = (L(2).y - L(4).y) / (L(2).x - L(4).x);
479 L(2).b = (L(0).y - L(2).y) / (L(0).x - L(2).x);
481 theta = atan2(L(2).y - L(4).y, L(2).x - L(4).x);
482 len = sqrt(pow(L(2).x - L(4).x, 2.) + pow(L(2).y - L(4).y, 2.));
483 r = FFMIN(radius, len);
484 L(3).x = L(2).x - r * cos(theta);
485 L(3).y = L(2).y - r * sin(theta);
487 theta = atan2(L(0).y - L(2).y, L(0).x - L(2).x);
488 len = sqrt(pow(L(0).x - L(2).x, 2.) + pow(L(0).y - L(2).y, 2.));
489 r = FFMIN(radius, len / 2);
490 x = L(2).x + r * cos(theta);
491 y = L(2).y + r * sin(theta);
493 cx = (L(3).x + L(2).x + x) / 3;
494 cy = (L(3).y + L(2).y + y) / 3;
501 in2 = L(2).x - L(3).x;
502 out2 = L(2).y - L(3).y;
503 L(3).a = (out2 / in2 - out1 / in1) / (in2 - in1);
504 L(3).b = out1 / in1 - L(3).a * in1;
509 s->in_min_lin = exp(s->segments[1].x);
510 s->out_min_lin = exp(s->segments[1].y);
512 for (i = 0; i < channels; i++) {
513 ChanParam *cp = &s->channels[i];
515 if (cp->attack > 1.0 / sample_rate)
516 cp->attack = 1.0 - exp(-1.0 / (sample_rate * cp->attack));
519 if (cp->decay > 1.0 / sample_rate)
520 cp->decay = 1.0 - exp(-1.0 / (sample_rate * cp->decay));
523 cp->volume = pow(10.0, s->initial_volume / 20);
526 s->delay_samples = s->delay * sample_rate;
527 if (s->delay_samples <= 0) {
528 s->compand = compand_nodelay;
532 s->delay_frame = av_frame_alloc();
533 if (!s->delay_frame) {
535 return AVERROR(ENOMEM);
538 s->delay_frame->format = outlink->format;
539 s->delay_frame->nb_samples = s->delay_samples;
540 s->delay_frame->channel_layout = outlink->channel_layout;
542 err = av_frame_get_buffer(s->delay_frame, 32);
546 s->compand = compand_delay;
550 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
552 AVFilterContext *ctx = inlink->dst;
553 CompandContext *s = ctx->priv;
555 return s->compand(ctx, frame);
558 static int request_frame(AVFilterLink *outlink)
560 AVFilterContext *ctx = outlink->src;
561 CompandContext *s = ctx->priv;
564 ret = ff_request_frame(ctx->inputs[0]);
566 if (ret == AVERROR_EOF && s->delay_count)
567 ret = compand_drain(outlink);
572 static const AVFilterPad compand_inputs[] = {
575 .type = AVMEDIA_TYPE_AUDIO,
576 .filter_frame = filter_frame,
581 static const AVFilterPad compand_outputs[] = {
584 .request_frame = request_frame,
585 .config_props = config_output,
586 .type = AVMEDIA_TYPE_AUDIO,
592 AVFilter ff_af_compand = {
594 .description = NULL_IF_CONFIG_SMALL(
595 "Compress or expand audio dynamic range."),
596 .query_formats = query_formats,
597 .priv_size = sizeof(CompandContext),
598 .priv_class = &compand_class,
601 .inputs = compand_inputs,
602 .outputs = compand_outputs,