78 #define OFFSET(x) offsetof(CompandContext, x)
79 #define A AV_OPT_FLAG_AUDIO_PARAM
82 {
"attacks",
"set time over which increase of volume is determined",
OFFSET(attacks),
AV_OPT_TYPE_STRING, { .str =
"0.3" }, 0, 0,
A },
83 {
"decays",
"set time over which decrease of volume is determined",
OFFSET(decays),
AV_OPT_TYPE_STRING, { .str =
"0.8" }, 0, 0,
A },
84 {
"points",
"set points of transfer function",
OFFSET(points),
AV_OPT_TYPE_STRING, { .str =
"-70/-70|-60/-20" }, 0, 0,
A },
88 {
"delay",
"set delay for samples before sending them to volume adjuster",
OFFSET(delay),
AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, 20,
A },
147 for (p = item_str; *p; p++) {
166 float in_log, out_log;
169 if (in_lin < s->in_min_lin)
172 in_log = logf(in_lin);
175 if (in_log <= s->segments[i].x)
179 out_log = cs->
y + in_log * (cs->
a * in_log + cs->
b);
181 return expf(out_log);
210 for (chan = 0; chan < channels; chan++) {
215 for (i = 0; i < nb_samples; i++) {
222 if (frame != out_frame)
228 #define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
236 int chan, i, dindex = 0, oindex, count = 0;
244 for (chan = 0; chan < channels; chan++) {
253 for (i = 0, oindex = 0; i < nb_samples; i++) {
254 const float in = src[i];
277 dst[oindex++] = av_clipf(dbuf[dindex] *
319 for (chan = 0; chan < channels; chan++) {
320 AVFrame *delay_frame = s->delay_frame;
322 float *dst = (
float *)frame->extended_data[chan];
325 dindex = s->delay_index;
326 for (i = 0; i < frame->nb_samples; i++) {
329 dindex =
MOD(dindex + 1, s->delay_samples);
332 s->delay_count -= frame->nb_samples;
333 s->delay_index = dindex;
343 double radius = s->
curve_dB * M_LN10 / 20.0;
347 int nb_attacks, nb_decays, nb_points;
348 int new_nb_items, num;
362 if (nb_attacks > channels || nb_decays > channels) {
364 "Number of attacks/decays bigger than number of channels.\n");
381 for (i = 0, new_nb_items = 0; i < nb_attacks; i++) {
395 nb_attacks = new_nb_items;
398 for (i = 0, new_nb_items = 0; i < nb_decays; i++) {
402 new_nb_items += sscanf(tstr,
"%f", &s->
channels[i].
decay) == 1;
411 nb_decays = new_nb_items;
413 if (nb_attacks != nb_decays) {
415 "Number of attacks %d differs from number of decays %d.\n",
416 nb_attacks, nb_decays);
421 #define S(x) s->segments[2 * ((x) + 1)]
423 for (i = 0, new_nb_items = 0; i < nb_points; i++) {
428 err = sscanf(tstr,
"%f/%f", &
S(i).x, &
S(i).y);
432 "Invalid and/or missing input/output value.\n");
436 if (i &&
S(i - 1).x >
S(i).x) {
438 "Transfer function input values must be increasing.\n");
451 if (num == 0 ||
S(num - 1).x)
455 #define S(x) s->segments[2 * (x)]
462 for (i = 2; i < num; i++) {
463 double g1 = (
S(i - 1).y -
S(i - 2).y) * (
S(i - 0).x -
S(i - 1).x);
464 double g2 = (
S(i - 0).y -
S(i - 1).y) * (
S(i - 1).x -
S(i - 2).x);
471 for (j = --i; j < num; j++)
475 for (i = 0; !i || s->
segments[i - 2].
x; i += 2) {
481 #define L(x) s->segments[i - (x)]
482 for (i = 4; s->
segments[i - 2].
x; i += 2) {
483 double x, y, cx, cy, in1, in2, out1, out2, theta,
len,
r;
486 L(4).b = (
L(2).y -
L(4).y) / (
L(2).x -
L(4).x);
489 L(2).b = (
L(0).y -
L(2).y) / (
L(0).x -
L(2).x);
491 theta = atan2(
L(2).y -
L(4).y,
L(2).x -
L(4).x);
492 len = sqrt(pow(
L(2).x -
L(4).x, 2.) + pow(
L(2).y -
L(4).y, 2.));
493 r =
FFMIN(radius, len);
494 L(3).x =
L(2).x - r * cos(theta);
495 L(3).y =
L(2).y - r * sin(theta);
497 theta = atan2(
L(0).y -
L(2).y,
L(0).x -
L(2).x);
498 len = sqrt(pow(
L(0).x -
L(2).x, 2.) + pow(
L(0).y -
L(2).y, 2.));
499 r =
FFMIN(radius, len / 2);
500 x =
L(2).x + r * cos(theta);
501 y =
L(2).y + r * sin(theta);
503 cx = (
L(3).x +
L(2).x + x) / 3;
504 cy = (
L(3).y +
L(2).y + y) / 3;
511 in2 =
L(2).x -
L(3).x;
512 out2 =
L(2).y -
L(3).y;
513 L(3).a = (out2 / in2 - out1 / in1) / (in2 - in1);
514 L(3).b = out1 / in1 -
L(3).a * in1;
522 for (i = 0; i < channels; i++) {
525 if (cp->
attack > 1.0 / sample_rate)
526 cp->
attack = 1.0 - exp(-1.0 / (sample_rate * cp->
attack));
529 if (cp->
decay > 1.0 / sample_rate)
530 cp->
decay = 1.0 - exp(-1.0 / (sample_rate * cp->
decay));
607 "Compress or expand audio dynamic range."),
610 .priv_class = &compand_class,