76 #define OFFSET(x) offsetof(CompandContext, x)
77 #define A AV_OPT_FLAG_AUDIO_PARAM
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 },
86 {
"delay",
"set delay for samples before sending them to volume adjuster",
OFFSET(delay),
AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, 20,
A },
145 for (p = item_str; *p; p++) {
164 float in_log, out_log;
167 if (in_lin < s->in_min_lin)
170 in_log = logf(in_lin);
173 if (in_log <= s->segments[i].x)
177 out_log = cs->
y + in_log * (cs->
a * in_log + cs->
b);
179 return expf(out_log);
208 for (chan = 0; chan < channels; chan++) {
213 for (i = 0; i < nb_samples; i++) {
220 if (frame != out_frame)
226 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
234 int chan, i, dindex = 0, oindex, count = 0;
242 for (chan = 0; chan < channels; chan++) {
251 for (i = 0, oindex = 0; i < nb_samples; i++) {
252 const float in = src[i];
275 dst[oindex++] = av_clipf(dbuf[dindex] *
309 for (chan = 0; chan < channels; chan++) {
310 AVFrame *delay_frame = s->delay_frame;
312 float *dst = (
float *)frame->extended_data[chan];
315 dindex = s->delay_index;
316 for (i = 0; i < frame->nb_samples; i++) {
319 dindex =
MOD(dindex + 1, s->delay_samples);
322 s->delay_count -= frame->nb_samples;
323 s->delay_index = dindex;
333 double radius = s->
curve_dB * M_LN10 / 20.0;
337 int nb_attacks, nb_decays, nb_points;
338 int new_nb_items, num;
352 if (nb_attacks > channels || nb_decays > channels) {
354 "Number of attacks/decays bigger than number of channels.\n");
371 for (i = 0, new_nb_items = 0; i < nb_attacks; i++) {
385 nb_attacks = new_nb_items;
388 for (i = 0, new_nb_items = 0; i < nb_decays; i++) {
392 new_nb_items += sscanf(tstr,
"%f", &s->
channels[i].
decay) == 1;
401 nb_decays = new_nb_items;
403 if (nb_attacks != nb_decays) {
405 "Number of attacks %d differs from number of decays %d.\n",
406 nb_attacks, nb_decays);
411 #define S(x) s->segments[2 * ((x) + 1)]
413 for (i = 0, new_nb_items = 0; i < nb_points; i++) {
418 err = sscanf(tstr,
"%f/%f", &
S(i).x, &
S(i).y);
422 "Invalid and/or missing input/output value.\n");
426 if (i &&
S(i - 1).x >
S(i).x) {
428 "Transfer function input values must be increasing.\n");
441 if (num == 0 ||
S(num - 1).x)
445 #define S(x) s->segments[2 * (x)]
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);
461 for (j = --i; j < num; j++)
465 for (i = 0; !i || s->
segments[i - 2].
x; i += 2) {
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;
512 for (i = 0; i < 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));
595 "Compress or expand audio dynamic range."),
598 .priv_class = &compand_class,