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 FFmpeg.
10 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * audio compand filter
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/samplefmt.h"
38 typedef struct ChanParam
{
44 typedef struct CompandSegment
{
49 typedef struct CompandContext
{
52 char *attacks
, *decays
, *points
;
53 CompandSegment
*segments
;
59 double initial_volume
;
67 int (*compand
)(AVFilterContext
*ctx
, AVFrame
*frame
);
70 #define OFFSET(x) offsetof(CompandContext, x)
71 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
73 static const AVOption compand_options
[] = {
74 { "attacks", "set time over which increase of volume is determined", OFFSET(attacks
), AV_OPT_TYPE_STRING
, { .str
= "0.3" }, 0, 0, A
},
75 { "decays", "set time over which decrease of volume is determined", OFFSET(decays
), AV_OPT_TYPE_STRING
, { .str
= "0.8" }, 0, 0, A
},
76 { "points", "set points of transfer function", OFFSET(points
), AV_OPT_TYPE_STRING
, { .str
= "-70/-70|-60/-20" }, 0, 0, A
},
77 { "soft-knee", "set soft-knee", OFFSET(curve_dB
), AV_OPT_TYPE_DOUBLE
, { .dbl
= 0.01 }, 0.01, 900, A
},
78 { "gain", "set output gain", OFFSET(gain_dB
), AV_OPT_TYPE_DOUBLE
, { .dbl
= 0 }, -900, 900, A
},
79 { "volume", "set initial volume", OFFSET(initial_volume
), AV_OPT_TYPE_DOUBLE
, { .dbl
= 0 }, -900, 0, A
},
80 { "delay", "set delay for samples before sending them to volume adjuster", OFFSET(delay
), AV_OPT_TYPE_DOUBLE
, { .dbl
= 0 }, 0, 20, A
},
84 AVFILTER_DEFINE_CLASS(compand
);
86 static av_cold
int init(AVFilterContext
*ctx
)
88 CompandContext
*s
= ctx
->priv
;
89 s
->pts
= AV_NOPTS_VALUE
;
93 static av_cold
void uninit(AVFilterContext
*ctx
)
95 CompandContext
*s
= ctx
->priv
;
97 av_freep(&s
->channels
);
98 av_freep(&s
->segments
);
99 av_frame_free(&s
->delay_frame
);
102 static int query_formats(AVFilterContext
*ctx
)
104 AVFilterChannelLayouts
*layouts
;
105 AVFilterFormats
*formats
;
106 static const enum AVSampleFormat sample_fmts
[] = {
111 layouts
= ff_all_channel_layouts();
113 return AVERROR(ENOMEM
);
114 ff_set_common_channel_layouts(ctx
, layouts
);
116 formats
= ff_make_format_list(sample_fmts
);
118 return AVERROR(ENOMEM
);
119 ff_set_common_formats(ctx
, formats
);
121 formats
= ff_all_samplerates();
123 return AVERROR(ENOMEM
);
124 ff_set_common_samplerates(ctx
, formats
);
129 static void count_items(char *item_str
, int *nb_items
)
134 for (p
= item_str
; *p
; p
++) {
135 if (*p
== ' ' || *p
== '|')
140 static void update_volume(ChanParam
*cp
, double in
)
142 double delta
= in
- cp
->volume
;
145 cp
->volume
+= delta
* cp
->attack
;
147 cp
->volume
+= delta
* cp
->decay
;
150 static double get_volume(CompandContext
*s
, double in_lin
)
153 double in_log
, out_log
;
156 if (in_lin
< s
->in_min_lin
)
157 return s
->out_min_lin
;
159 in_log
= log(in_lin
);
161 for (i
= 1; i
< s
->nb_segments
; i
++)
162 if (in_log
<= s
->segments
[i
].x
)
164 cs
= &s
->segments
[i
- 1];
166 out_log
= cs
->y
+ in_log
* (cs
->a
* in_log
+ cs
->b
);
171 static int compand_nodelay(AVFilterContext
*ctx
, AVFrame
*frame
)
173 CompandContext
*s
= ctx
->priv
;
174 AVFilterLink
*inlink
= ctx
->inputs
[0];
175 const int channels
= inlink
->channels
;
176 const int nb_samples
= frame
->nb_samples
;
181 if (av_frame_is_writable(frame
)) {
184 out_frame
= ff_get_audio_buffer(inlink
, nb_samples
);
186 av_frame_free(&frame
);
187 return AVERROR(ENOMEM
);
189 err
= av_frame_copy_props(out_frame
, frame
);
191 av_frame_free(&out_frame
);
192 av_frame_free(&frame
);
197 for (chan
= 0; chan
< channels
; chan
++) {
198 const double *src
= (double *)frame
->extended_data
[chan
];
199 double *dst
= (double *)out_frame
->extended_data
[chan
];
200 ChanParam
*cp
= &s
->channels
[chan
];
202 for (i
= 0; i
< nb_samples
; i
++) {
203 update_volume(cp
, fabs(src
[i
]));
205 dst
[i
] = av_clipd(src
[i
] * get_volume(s
, cp
->volume
), -1, 1);
209 if (frame
!= out_frame
)
210 av_frame_free(&frame
);
212 return ff_filter_frame(ctx
->outputs
[0], out_frame
);
215 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
217 static int compand_delay(AVFilterContext
*ctx
, AVFrame
*frame
)
219 CompandContext
*s
= ctx
->priv
;
220 AVFilterLink
*inlink
= ctx
->inputs
[0];
221 const int channels
= inlink
->channels
;
222 const int nb_samples
= frame
->nb_samples
;
223 int chan
, i
, av_uninit(dindex
), oindex
, av_uninit(count
);
224 AVFrame
*out_frame
= NULL
;
227 if (s
->pts
== AV_NOPTS_VALUE
) {
228 s
->pts
= (frame
->pts
== AV_NOPTS_VALUE
) ? 0 : frame
->pts
;
231 av_assert1(channels
> 0); /* would corrupt delay_count and delay_index */
233 for (chan
= 0; chan
< channels
; chan
++) {
234 AVFrame
*delay_frame
= s
->delay_frame
;
235 const double *src
= (double *)frame
->extended_data
[chan
];
236 double *dbuf
= (double *)delay_frame
->extended_data
[chan
];
237 ChanParam
*cp
= &s
->channels
[chan
];
240 count
= s
->delay_count
;
241 dindex
= s
->delay_index
;
242 for (i
= 0, oindex
= 0; i
< nb_samples
; i
++) {
243 const double in
= src
[i
];
244 update_volume(cp
, fabs(in
));
246 if (count
>= s
->delay_samples
) {
248 out_frame
= ff_get_audio_buffer(inlink
, nb_samples
- i
);
250 av_frame_free(&frame
);
251 return AVERROR(ENOMEM
);
253 err
= av_frame_copy_props(out_frame
, frame
);
255 av_frame_free(&out_frame
);
256 av_frame_free(&frame
);
259 out_frame
->pts
= s
->pts
;
260 s
->pts
+= av_rescale_q(nb_samples
- i
,
261 (AVRational
){ 1, inlink
->sample_rate
},
265 dst
= (double *)out_frame
->extended_data
[chan
];
266 dst
[oindex
++] = av_clipd(dbuf
[dindex
] *
267 get_volume(s
, cp
->volume
), -1, 1);
273 dindex
= MOD(dindex
+ 1, s
->delay_samples
);
277 s
->delay_count
= count
;
278 s
->delay_index
= dindex
;
280 av_frame_free(&frame
);
283 err
= ff_filter_frame(ctx
->outputs
[0], out_frame
);
290 static int compand_drain(AVFilterLink
*outlink
)
292 AVFilterContext
*ctx
= outlink
->src
;
293 CompandContext
*s
= ctx
->priv
;
294 const int channels
= outlink
->channels
;
295 AVFrame
*frame
= NULL
;
298 /* 2048 is to limit output frame size during drain */
299 frame
= ff_get_audio_buffer(outlink
, FFMIN(2048, s
->delay_count
));
301 return AVERROR(ENOMEM
);
303 s
->pts
+= av_rescale_q(frame
->nb_samples
,
304 (AVRational
){ 1, outlink
->sample_rate
}, outlink
->time_base
);
306 av_assert0(channels
> 0);
307 for (chan
= 0; chan
< channels
; chan
++) {
308 AVFrame
*delay_frame
= s
->delay_frame
;
309 double *dbuf
= (double *)delay_frame
->extended_data
[chan
];
310 double *dst
= (double *)frame
->extended_data
[chan
];
311 ChanParam
*cp
= &s
->channels
[chan
];
313 dindex
= s
->delay_index
;
314 for (i
= 0; i
< frame
->nb_samples
; i
++) {
315 dst
[i
] = av_clipd(dbuf
[dindex
] * get_volume(s
, cp
->volume
),
317 dindex
= MOD(dindex
+ 1, s
->delay_samples
);
320 s
->delay_count
-= frame
->nb_samples
;
321 s
->delay_index
= dindex
;
323 return ff_filter_frame(outlink
, frame
);
326 static int config_output(AVFilterLink
*outlink
)
328 AVFilterContext
*ctx
= outlink
->src
;
329 CompandContext
*s
= ctx
->priv
;
330 const int sample_rate
= outlink
->sample_rate
;
331 double radius
= s
->curve_dB
* M_LN10
/ 20.0;
332 char *p
, *saveptr
= NULL
;
333 const int channels
= outlink
->channels
;
334 int nb_attacks
, nb_decays
, nb_points
;
335 int new_nb_items
, num
;
340 count_items(s
->attacks
, &nb_attacks
);
341 count_items(s
->decays
, &nb_decays
);
342 count_items(s
->points
, &nb_points
);
345 av_log(ctx
, AV_LOG_ERROR
, "Invalid number of channels: %d\n", channels
);
346 return AVERROR(EINVAL
);
349 if (nb_attacks
> channels
|| nb_decays
> channels
) {
350 av_log(ctx
, AV_LOG_ERROR
,
351 "Number of attacks/decays bigger than number of channels.\n");
352 return AVERROR(EINVAL
);
357 s
->channels
= av_mallocz_array(channels
, sizeof(*s
->channels
));
358 s
->nb_segments
= (nb_points
+ 4) * 2;
359 s
->segments
= av_mallocz_array(s
->nb_segments
, sizeof(*s
->segments
));
361 if (!s
->channels
|| !s
->segments
) {
363 return AVERROR(ENOMEM
);
367 for (i
= 0, new_nb_items
= 0; i
< nb_attacks
; i
++) {
368 char *tstr
= av_strtok(p
, " |", &saveptr
);
370 new_nb_items
+= sscanf(tstr
, "%lf", &s
->channels
[i
].attack
) == 1;
371 if (s
->channels
[i
].attack
< 0) {
373 return AVERROR(EINVAL
);
376 nb_attacks
= new_nb_items
;
379 for (i
= 0, new_nb_items
= 0; i
< nb_decays
; i
++) {
380 char *tstr
= av_strtok(p
, " |", &saveptr
);
382 new_nb_items
+= sscanf(tstr
, "%lf", &s
->channels
[i
].decay
) == 1;
383 if (s
->channels
[i
].decay
< 0) {
385 return AVERROR(EINVAL
);
388 nb_decays
= new_nb_items
;
390 if (nb_attacks
!= nb_decays
) {
391 av_log(ctx
, AV_LOG_ERROR
,
392 "Number of attacks %d differs from number of decays %d.\n",
393 nb_attacks
, nb_decays
);
395 return AVERROR(EINVAL
);
398 #define S(x) s->segments[2 * ((x) + 1)]
400 for (i
= 0, new_nb_items
= 0; i
< nb_points
; i
++) {
401 char *tstr
= av_strtok(p
, " |", &saveptr
);
403 if (sscanf(tstr
, "%lf/%lf", &S(i
).x
, &S(i
).y
) != 2) {
404 av_log(ctx
, AV_LOG_ERROR
,
405 "Invalid and/or missing input/output value.\n");
407 return AVERROR(EINVAL
);
409 if (i
&& S(i
- 1).x
> S(i
).x
) {
410 av_log(ctx
, AV_LOG_ERROR
,
411 "Transfer function input values must be increasing.\n");
413 return AVERROR(EINVAL
);
416 av_log(ctx
, AV_LOG_DEBUG
, "%d: x=%f y=%f\n", i
, S(i
).x
, S(i
).y
);
421 /* Add 0,0 if necessary */
422 if (num
== 0 || S(num
- 1).x
)
426 #define S(x) s->segments[2 * (x)]
427 /* Add a tail off segment at the start */
428 S(0).x
= S(1).x
- 2 * s
->curve_dB
;
432 /* Join adjacent colinear segments */
433 for (i
= 2; i
< num
; i
++) {
434 double g1
= (S(i
- 1).y
- S(i
- 2).y
) * (S(i
- 0).x
- S(i
- 1).x
);
435 double g2
= (S(i
- 0).y
- S(i
- 1).y
) * (S(i
- 1).x
- S(i
- 2).x
);
441 for (j
= --i
; j
< num
; j
++)
445 for (i
= 0; !i
|| s
->segments
[i
- 2].x
; i
+= 2) {
446 s
->segments
[i
].y
+= s
->gain_dB
;
447 s
->segments
[i
].x
*= M_LN10
/ 20;
448 s
->segments
[i
].y
*= M_LN10
/ 20;
451 #define L(x) s->segments[i - (x)]
452 for (i
= 4; s
->segments
[i
- 2].x
; i
+= 2) {
453 double x
, y
, cx
, cy
, in1
, in2
, out1
, out2
, theta
, len
, r
;
456 L(4).b
= (L(2).y
- L(4).y
) / (L(2).x
- L(4).x
);
459 L(2).b
= (L(0).y
- L(2).y
) / (L(0).x
- L(2).x
);
461 theta
= atan2(L(2).y
- L(4).y
, L(2).x
- L(4).x
);
462 len
= sqrt(pow(L(2).x
- L(4).x
, 2.) + pow(L(2).y
- L(4).y
, 2.));
463 r
= FFMIN(radius
, len
);
464 L(3).x
= L(2).x
- r
* cos(theta
);
465 L(3).y
= L(2).y
- r
* sin(theta
);
467 theta
= atan2(L(0).y
- L(2).y
, L(0).x
- L(2).x
);
468 len
= sqrt(pow(L(0).x
- L(2).x
, 2.) + pow(L(0).y
- L(2).y
, 2.));
469 r
= FFMIN(radius
, len
/ 2);
470 x
= L(2).x
+ r
* cos(theta
);
471 y
= L(2).y
+ r
* sin(theta
);
473 cx
= (L(3).x
+ L(2).x
+ x
) / 3;
474 cy
= (L(3).y
+ L(2).y
+ y
) / 3;
481 in2
= L(2).x
- L(3).x
;
482 out2
= L(2).y
- L(3).y
;
483 L(3).a
= (out2
/ in2
- out1
/ in1
) / (in2
- in1
);
484 L(3).b
= out1
/ in1
- L(3).a
* in1
;
489 s
->in_min_lin
= exp(s
->segments
[1].x
);
490 s
->out_min_lin
= exp(s
->segments
[1].y
);
492 for (i
= 0; i
< channels
; i
++) {
493 ChanParam
*cp
= &s
->channels
[i
];
495 if (cp
->attack
> 1.0 / sample_rate
)
496 cp
->attack
= 1.0 - exp(-1.0 / (sample_rate
* cp
->attack
));
499 if (cp
->decay
> 1.0 / sample_rate
)
500 cp
->decay
= 1.0 - exp(-1.0 / (sample_rate
* cp
->decay
));
503 cp
->volume
= pow(10.0, s
->initial_volume
/ 20);
506 s
->delay_samples
= s
->delay
* sample_rate
;
507 if (s
->delay_samples
<= 0) {
508 s
->compand
= compand_nodelay
;
512 s
->delay_frame
= av_frame_alloc();
513 if (!s
->delay_frame
) {
515 return AVERROR(ENOMEM
);
518 s
->delay_frame
->format
= outlink
->format
;
519 s
->delay_frame
->nb_samples
= s
->delay_samples
;
520 s
->delay_frame
->channel_layout
= outlink
->channel_layout
;
522 err
= av_frame_get_buffer(s
->delay_frame
, 32);
526 outlink
->flags
|= FF_LINK_FLAG_REQUEST_LOOP
;
527 s
->compand
= compand_delay
;
531 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*frame
)
533 AVFilterContext
*ctx
= inlink
->dst
;
534 CompandContext
*s
= ctx
->priv
;
536 return s
->compand(ctx
, frame
);
539 static int request_frame(AVFilterLink
*outlink
)
541 AVFilterContext
*ctx
= outlink
->src
;
542 CompandContext
*s
= ctx
->priv
;
545 ret
= ff_request_frame(ctx
->inputs
[0]);
547 if (ret
== AVERROR_EOF
&& !ctx
->is_disabled
&& s
->delay_count
)
548 ret
= compand_drain(outlink
);
553 static const AVFilterPad compand_inputs
[] = {
556 .type
= AVMEDIA_TYPE_AUDIO
,
557 .filter_frame
= filter_frame
,
562 static const AVFilterPad compand_outputs
[] = {
565 .request_frame
= request_frame
,
566 .config_props
= config_output
,
567 .type
= AVMEDIA_TYPE_AUDIO
,
573 AVFilter ff_af_compand
= {
575 .description
= NULL_IF_CONFIG_SMALL(
576 "Compress or expand audio dynamic range."),
577 .query_formats
= query_formats
,
578 .priv_size
= sizeof(CompandContext
),
579 .priv_class
= &compand_class
,
582 .inputs
= compand_inputs
,
583 .outputs
= compand_outputs
,