70 #define OFFSET(x) offsetof(CompandContext, x)
71 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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 },
80 {
"delay",
"set delay for samples before sending them to volume adjuster",
OFFSET(delay),
AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, 20,
A },
134 for (p = item_str; *p; p++) {
135 if (*p ==
' ' || *p ==
'|')
153 double in_log, out_log;
156 if (in_lin < s->in_min_lin)
159 in_log = log(in_lin);
162 if (in_log <= s->segments[i].x)
166 out_log = cs->
y + in_log * (cs->
a * in_log + cs->
b);
175 const int channels = inlink->
channels;
197 for (chan = 0; chan < channels; chan++) {
202 for (i = 0; i < nb_samples; i++) {
209 if (frame != out_frame)
215 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
221 const int channels = inlink->
channels;
233 for (chan = 0; chan < channels; chan++) {
242 for (i = 0, oindex = 0; i < nb_samples; i++) {
243 const double in = src[i];
266 dst[oindex++] = av_clipd(dbuf[dindex] *
288 const int channels = outlink->
channels;
300 for (chan = 0; chan < channels; chan++) {
301 AVFrame *delay_frame =
s->delay_frame;
306 dindex =
s->delay_index;
310 dindex =
MOD(dindex + 1,
s->delay_samples);
314 s->delay_index = dindex;
325 char *p, *saveptr = NULL;
326 const int channels = outlink->
channels;
327 int nb_attacks, nb_decays, nb_points;
328 int new_nb_items, num;
342 if (nb_attacks > channels || nb_decays > channels) {
344 "Number of attacks/decays bigger than number of channels.\n");
360 for (i = 0, new_nb_items = 0; i < nb_attacks; i++) {
361 char *tstr =
av_strtok(p,
" |", &saveptr);
369 nb_attacks = new_nb_items;
372 for (i = 0, new_nb_items = 0; i < nb_decays; i++) {
373 char *tstr =
av_strtok(p,
" |", &saveptr);
375 new_nb_items += sscanf(tstr,
"%lf", &s->
channels[i].
decay) == 1;
381 nb_decays = new_nb_items;
383 if (nb_attacks != nb_decays) {
385 "Number of attacks %d differs from number of decays %d.\n",
386 nb_attacks, nb_decays);
391 #define S(x) s->segments[2 * ((x) + 1)]
393 for (i = 0, new_nb_items = 0; i < nb_points; i++) {
394 char *tstr =
av_strtok(p,
" |", &saveptr);
396 if (sscanf(tstr,
"%lf/%lf", &
S(i).x, &
S(i).
y) != 2) {
398 "Invalid and/or missing input/output value.\n");
402 if (i &&
S(i - 1).x >
S(i).x) {
404 "Transfer function input values must be increasing.\n");
415 if (num == 0 ||
S(num - 1).x)
419 #define S(x) s->segments[2 * (x)]
426 for (i = 2; i < num; i++) {
427 double g1 = (
S(i - 1).y -
S(i - 2).y) * (
S(i - 0).x -
S(i - 1).x);
428 double g2 = (
S(i - 0).y -
S(i - 1).y) * (
S(i - 1).x -
S(i - 2).x);
434 for (j = --i; j < num; j++)
438 for (i = 0; !i || s->
segments[i - 2].
x; i += 2) {
444 #define L(x) s->segments[i - (x)]
445 for (i = 4; s->
segments[i - 2].
x; i += 2) {
446 double x,
y, cx, cy, in1, in2, out1, out2, theta,
len,
r;
449 L(4).b = (
L(2).y -
L(4).y) / (
L(2).x -
L(4).x);
452 L(2).b = (
L(0).y -
L(2).y) / (
L(0).x -
L(2).x);
454 theta = atan2(
L(2).y -
L(4).y,
L(2).x -
L(4).x);
455 len = sqrt(pow(
L(2).x -
L(4).x, 2.) + pow(
L(2).y -
L(4).y, 2.));
456 r =
FFMIN(radius, len);
457 L(3).x =
L(2).x - r * cos(theta);
458 L(3).y =
L(2).y - r * sin(theta);
460 theta = atan2(
L(0).y -
L(2).y,
L(0).x -
L(2).x);
461 len = sqrt(pow(
L(0).x -
L(2).x, 2.) + pow(
L(0).y -
L(2).y, 2.));
462 r =
FFMIN(radius, len / 2);
463 x =
L(2).x + r * cos(theta);
464 y =
L(2).y + r * sin(theta);
466 cx = (
L(3).x +
L(2).x + x) / 3;
467 cy = (
L(3).y +
L(2).y +
y) / 3;
474 in2 =
L(2).x -
L(3).x;
475 out2 =
L(2).y -
L(3).y;
476 L(3).a = (out2 / in2 - out1 / in1) / (in2 - in1);
477 L(3).b = out1 / in1 -
L(3).a * in1;
485 for (i = 0; i < channels; i++) {
488 if (cp->
attack > 1.0 / sample_rate)
489 cp->
attack = 1.0 - exp(-1.0 / (sample_rate * cp->
attack));
492 if (cp->
decay > 1.0 / sample_rate)
493 cp->
decay = 1.0 - exp(-1.0 / (sample_rate * cp->
decay));
569 "Compress or expand audio dynamic range."),
572 .priv_class = &compand_class,