1 /*
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
7 *
8 * This file is part of FFmpeg.
9 *
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.
14 *
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.
19 *
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
23 */
24
25 /**
26 * @file
27 * audio compand filter
28 */
29
38
44
49
67
70
71 #define OFFSET(x) offsetof(CompandContext, x)
72 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
73
75 {
"attacks",
"set time over which increase of volume is determined",
OFFSET(attacks),
AV_OPT_TYPE_STRING, { .str =
"0" }, 0, 0,
A },
76 {
"decays",
"set time over which decrease of volume is determined",
OFFSET(decays),
AV_OPT_TYPE_STRING, { .str =
"0.8" }, 0, 0,
A },
77 {
"points",
"set points of transfer function",
OFFSET(points),
AV_OPT_TYPE_STRING, { .str =
"-70/-70|-60/-20|1/0" }, 0, 0,
A },
81 {
"delay",
"set delay for samples before sending them to volume adjuster",
OFFSET(delay),
AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, 20,
A },
83 };
84
86
88 {
91 return 0;
92 }
93
95 {
97
101 }
102
104 {
105 char *p;
106
107 *nb_items = 1;
108 for (p = item_str; *p; p++) {
109 if (*p == ' ' || *p == '|')
110 (*nb_items)++;
111 }
112 }
113
115 {
117
120 else
122 }
123
125 {
127 double in_log, out_log;
129
130 if (in_lin < s->in_min_lin)
131 return s->out_min_lin;
132
133 in_log = log(in_lin);
134
135 for (
i = 1;
i <
s->nb_segments;
i++)
136 if (in_log <= s->segments[
i].x)
137 break;
138 cs = &
s->segments[
i - 1];
140 out_log = cs->
y + in_log * (cs->
a * in_log + cs->
b);
141
143 }
144
146 {
150 const int nb_samples =
frame->nb_samples;
153 int err;
154
157 } else {
159 if (!out_frame) {
162 }
164 if (err < 0) {
167 return err;
168 }
169 }
170
171 for (chan = 0; chan <
channels; chan++) {
172 const double *
src = (
double *)
frame->extended_data[chan];
175
176 for (
i = 0;
i < nb_samples;
i++) {
178
180 }
181 }
182
183 if (
frame != out_frame)
185
187 }
188
189 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
190
192 {
196 const int nb_samples =
frame->nb_samples;
199 int err;
200
203 }
204
206
207 for (chan = 0; chan <
channels; chan++) {
208 AVFrame *delay_frame =
s->delay_frame;
209 const double *
src = (
double *)
frame->extended_data[chan];
212 double *dst;
213
214 count =
s->delay_count;
215 dindex =
s->delay_index;
216 for (
i = 0, oindex = 0;
i < nb_samples;
i++) {
217 const double in =
src[
i];
219
220 if (count >=
s->delay_samples) {
221 if (!out_frame) {
223 if (!out_frame) {
226 }
228 if (err < 0) {
231 return err;
232 }
233 out_frame->
pts =
s->pts;
237 }
238
241 } else {
242 count++;
243 }
244
245 dbuf[dindex] = in;
246 dindex =
MOD(dindex + 1,
s->delay_samples);
247 }
248 }
249
250 s->delay_count = count;
251 s->delay_index = dindex;
252
254
255 if (out_frame) {
257 return err;
258 }
259
260 return 0;
261 }
262
264 {
270
271 /* 2048 is to limit output frame size during drain */
278
280 for (chan = 0; chan <
channels; chan++) {
281 AVFrame *delay_frame =
s->delay_frame;
283 double *dst = (
double *)
frame->extended_data[chan];
285
286 dindex =
s->delay_index;
287 for (
i = 0;
i <
frame->nb_samples;
i++) {
289 dindex =
MOD(dindex + 1,
s->delay_samples);
290 }
291 }
292 s->delay_count -=
frame->nb_samples;
293 s->delay_index = dindex;
294
296 }
297
299 {
303 double radius =
s->curve_dB *
M_LN10 / 20.0;
304 char *p, *saveptr =
NULL;
306 int nb_attacks, nb_decays, nb_points;
307 int new_nb_items, num;
309 int err;
310
311
315
319 }
320
323 "Number of attacks/decays bigger than number of channels. Ignoring rest of entries.\n");
326 }
327
329
331 s->nb_segments = (nb_points + 4) * 2;
332 s->segments =
av_calloc(
s->nb_segments,
sizeof(*
s->segments));
333
334 if (!
s->channels || !
s->segments) {
337 }
338
340 for (
i = 0, new_nb_items = 0;
i < nb_attacks;
i++) {
341 char *tstr =
av_strtok(p,
" |", &saveptr);
342 if (!tstr) {
345 }
347 new_nb_items += sscanf(tstr,
"%lf", &
s->channels[
i].attack) == 1;
348 if (
s->channels[
i].attack < 0) {
351 }
352 }
353 nb_attacks = new_nb_items;
354
356 for (
i = 0, new_nb_items = 0;
i < nb_decays;
i++) {
357 char *tstr =
av_strtok(p,
" |", &saveptr);
358 if (!tstr) {
361 }
363 new_nb_items += sscanf(tstr,
"%lf", &
s->channels[
i].decay) == 1;
364 if (
s->channels[
i].decay < 0) {
367 }
368 }
369 nb_decays = new_nb_items;
370
371 if (nb_attacks != nb_decays) {
373 "Number of attacks %d differs from number of decays %d.\n",
374 nb_attacks, nb_decays);
377 }
378
380 s->channels[
i].attack =
s->channels[nb_decays - 1].attack;
381 s->channels[
i].decay =
s->channels[nb_decays - 1].decay;
382 }
383
384 #define S(x) s->segments[2 * ((x) + 1)]
386 for (
i = 0, new_nb_items = 0;
i < nb_points;
i++) {
387 char *tstr =
av_strtok(p,
" |", &saveptr);
389 if (!tstr || sscanf(tstr,
"%lf/%lf", &
S(
i).x, &
S(
i).y) != 2) {
391 "Invalid and/or missing input/output value.\n");
394 }
395 if (
i &&
S(
i - 1).x >
S(
i).x) {
397 "Transfer function input values must be increasing.\n");
400 }
403 new_nb_items++;
404 }
405 num = new_nb_items;
406
407 /* Add 0,0 if necessary */
408 if (num == 0 ||
S(num - 1).x)
409 num++;
410
411 #undef S
412 #define S(x) s->segments[2 * (x)]
413 /* Add a tail off segment at the start */
414 S(0).x =
S(1).x - 2 *
s->curve_dB;
416 num++;
417
418 /* Join adjacent colinear segments */
419 for (
i = 2;
i < num;
i++) {
420 double g1 = (
S(
i - 1).y -
S(
i - 2).y) * (
S(
i - 0).x -
S(
i - 1).x);
421 double g2 = (
S(
i - 0).y -
S(
i - 1).y) * (
S(
i - 1).x -
S(
i - 2).x);
422 int j;
423
425 continue;
426 num--;
427 for (j = --
i; j < num; j++)
429 }
430
431 for (
i = 0;
i <
s->nb_segments;
i += 2) {
432 s->segments[
i].y +=
s->gain_dB;
435 }
436
437 #define L(x) s->segments[i - (x)]
438 for (
i = 4;
i <
s->nb_segments;
i += 2) {
439 double x, y, cx, cy, in1, in2, out1, out2, theta,
len,
r;
440
442 L(4).b = (
L(2).y -
L(4).y) / (
L(2).x -
L(4).x);
443
445 L(2).b = (
L(0).y -
L(2).y) / (
L(0).x -
L(2).x);
446
447 theta = atan2(
L(2).y -
L(4).y,
L(2).x -
L(4).x);
450 L(3).x =
L(2).x -
r * cos(theta);
451 L(3).y =
L(2).y -
r * sin(theta);
452
453 theta = atan2(
L(0).y -
L(2).y,
L(0).x -
L(2).x);
456 x =
L(2).x +
r * cos(theta);
457 y =
L(2).y +
r * sin(theta);
458
459 cx = (
L(3).x +
L(2).x + x) / 3;
460 cy = (
L(3).y +
L(2).y + y) / 3;
461
464
467 in2 =
L(2).x -
L(3).x;
468 out2 =
L(2).y -
L(3).y;
469 L(3).a = (out2 / in2 - out1 / in1) / (in2 - in1);
470 L(3).b = out1 / in1 -
L(3).a * in1;
471 }
474
475 s->in_min_lin =
exp(
s->segments[1].x);
476 s->out_min_lin =
exp(
s->segments[1].y);
477
480
483 else
487 else
490 }
491
493 if (
s->delay_samples <= 0) {
495 return 0;
496 }
497
499 if (!
s->delay_frame) {
502 }
503
504 s->delay_frame->format = outlink->
format;
505 s->delay_frame->nb_samples =
s->delay_samples;
506 #if FF_API_OLD_CHANNEL_LAYOUT
508 s->delay_frame->channel_layout = outlink->channel_layout;
510 #endif
512 return err;
513
515 if (err)
516 return err;
517
519 return 0;
520 }
521
523 {
526
528 }
529
531 {
535
537
540
542 }
543
545 {
549 },
550 };
551
553 {
558 },
559 };
560
561
565 "Compress or expand audio dynamic range."),
567 .priv_class = &compand_class,
573 };