94#define OFFSET(x) offsetof(MCompandContext, x)
95#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
98 {
"args",
"set parameters for each band",
OFFSET(args),
AV_OPT_TYPE_STRING, { .str =
"0.005,0.1 6 -47/-40,-34/-34,-17/-33 100 | 0.003,0.05 6 -47/-40,-34/-34,-17/-33 400 | 0.000625,0.0125 6 -47/-40,-34/-34,-15/-33 1600 | 0.0001,0.025 6 -47/-40,-34/-34,-31/-31,-0/-30 6400 | 0,0.025 6 -38/-31,-28/-28,-0/-25 22000" }, 0, 0,
A },
114 for (
i = 0;
i <
s->nb_bands;
i++) {
126static void count_items(
char *item_str,
int *nb_items,
char delimiter)
131 for (p = item_str; *p; p++) {
139 double delta = in -
cb->volume[ch];
142 cb->volume[ch] +=
delta *
cb->attack_rate[ch];
144 cb->volume[ch] +=
delta *
cb->decay_rate[ch];
150 double in_log, out_log;
153 if (in_lin <= s->in_min_lin)
154 return s->out_min_lin;
156 in_log = log(in_lin);
158 for (
i = 1;
i <
s->nb_segments;
i++)
159 if (in_log <= s->segments[
i].x)
161 cs = &
s->segments[
i - 1];
163 out_log = cs->
y + in_log * (cs->
a * in_log + cs->
b);
171 int new_nb_items, num;
172 char *saveptr =
NULL;
176#define S(x) s->segments[2 * ((x) + 1)]
177 for (
i = 0, new_nb_items = 0;
i < nb_points;
i++) {
178 char *tstr =
av_strtok(p,
",", &saveptr);
180 if (!tstr || sscanf(tstr,
"%lf/%lf", &
S(
i).x, &
S(
i).y) != 2) {
182 "Invalid and/or missing input/output value.\n");
185 if (
i &&
S(
i - 1).x >
S(
i).x) {
187 "Transfer function input values must be increasing.\n");
197 if (num == 0 ||
S(num - 1).x)
201#define S(x) s->segments[2 * (x)]
203 S(0).x =
S(1).x - 2 *
s->curve_dB;
208 for (
i = 2;
i < num;
i++) {
209 double g1 = (
S(
i - 1).y -
S(
i - 2).y) * (
S(
i - 0).x -
S(
i - 1).x);
210 double g2 = (
S(
i - 0).y -
S(
i - 1).y) * (
S(
i - 1).x -
S(
i - 2).x);
216 for (j = --
i; j < num; j++)
220 for (
i = 0;
i <
s->nb_segments;
i += 2) {
221 s->segments[
i].y +=
s->gain_dB;
226#define L(x) s->segments[i - (x)]
227 for (
i = 4;
i <
s->nb_segments;
i += 2) {
228 double x, y, cx, cy, in1, in2, out1, out2, theta,
len,
r;
231 L(4).b = (
L(2).y -
L(4).y) / (
L(2).x -
L(4).x);
234 L(2).b = (
L(0).y -
L(2).y) / (
L(0).x -
L(2).x);
236 theta = atan2(
L(2).y -
L(4).y,
L(2).x -
L(4).x);
239 L(3).x =
L(2).x -
r * cos(theta);
240 L(3).y =
L(2).y -
r * sin(theta);
242 theta = atan2(
L(0).y -
L(2).y,
L(0).x -
L(2).x);
245 x =
L(2).x +
r * cos(theta);
246 y =
L(2).y +
r * sin(theta);
248 cx = (
L(3).x +
L(2).x + x) / 3;
249 cy = (
L(3).y +
L(2).y + y) / 3;
256 in2 =
L(2).x -
L(3).x;
257 out2 =
L(2).y -
L(3).y;
258 L(3).a = (out2 / in2 - out1 / in1) / (in2 - in1);
259 L(3).b = out1 / in1 -
L(3).a * in1;
264 s->in_min_lin =
exp(
s->segments[1].x);
265 s->out_min_lin =
exp(
s->segments[1].y);
273 y[1] = 2 * x[0] * x[1];
274 y[2] = 2 * x[0] * x[2] + x[1] * x[1];
275 y[3] = 2 * x[1] * x[2];
282 double Q = sqrt(.5),
alpha = sin(w0) / (2*
Q);
289 x[0] = (1 - cos(w0))/2;
291 x[2] = (1 - cos(w0))/2;
292 x[3] = (1 + cos(w0))/2;
293 x[4] = -(1 + cos(w0));
294 x[5] = (1 + cos(w0))/2;
299 for (norm = x[6],
i = 0;
i < 9; ++
i)
317 int ret, ch,
i, k, new_nb_items, nb_bands;
318 char *p =
s->args, *saveptr =
NULL;
319 int max_delay_size = 0;
322 s->nb_bands =
FFMAX(1, nb_bands);
328 for (
i = 0, new_nb_items = 0;
i < nb_bands;
i++) {
329 int nb_points, nb_attacks, nb_items = 0;
330 char *tstr2, *tstr =
av_strtok(p,
"|", &saveptr);
331 char *p2, *p3, *saveptr2 =
NULL, *saveptr3 =
NULL;
349 if (!nb_attacks || nb_attacks & 1) {
357 if (!
s->bands[
i].attack_rate || !
s->bands[
i].decay_rate || !
s->bands[
i].volume)
361 char *tstr3 =
av_strtok(p3,
",", &saveptr3);
364 sscanf(tstr3,
"%lf", &
s->bands[
i].attack_rate[k]);
366 sscanf(tstr3,
"%lf", &
s->bands[
i].decay_rate[k]);
368 if (
s->bands[
i].attack_rate[k] > 1.0 / outlink->
sample_rate) {
369 s->bands[
i].attack_rate[k] = 1.0 -
exp(-1.0 / (outlink->
sample_rate *
s->bands[
i].attack_rate[k]));
371 s->bands[
i].attack_rate[k] = 1.0;
374 if (
s->bands[
i].decay_rate[k] > 1.0 / outlink->
sample_rate) {
375 s->bands[
i].decay_rate[k] = 1.0 -
exp(-1.0 / (outlink->
sample_rate *
s->bands[
i].decay_rate[k]));
377 s->bands[
i].decay_rate[k] = 1.0;
382 s->bands[
i].attack_rate[ch] =
s->bands[
i].attack_rate[k - 1];
383 s->bands[
i].decay_rate[ch] =
s->bands[
i].decay_rate[k - 1];
391 sscanf(tstr2,
"%lf", &
s->bands[
i].transfer_fn.curve_dB);
393 radius =
s->bands[
i].transfer_fn.curve_dB *
M_LN10 / 20.0;
402 s->bands[
i].transfer_fn.nb_segments = (nb_points + 4) * 2;
403 s->bands[
i].transfer_fn.segments =
av_calloc(
s->bands[
i].transfer_fn.nb_segments,
405 if (!
s->bands[
i].transfer_fn.segments)
420 new_nb_items += sscanf(tstr2,
"%lf", &
s->bands[
i].topfreq) == 1;
421 if (
s->bands[
i].topfreq < 0 ||
s->bands[
i].topfreq >= outlink->
sample_rate / 2.0) {
426 if (
s->bands[
i].topfreq != 0) {
434 sscanf(tstr2,
"%lf", &
s->bands[
i].delay);
439 double initial_volume;
441 sscanf(tstr2,
"%lf", &initial_volume);
442 initial_volume = pow(10.0, initial_volume / 20);
445 s->bands[
i].volume[k] = initial_volume;
450 sscanf(tstr2,
"%lf", &
s->bands[
i].transfer_fn.gain_dB);
455 s->nb_bands = new_nb_items;
457 for (
i = 0; max_delay_size > 0 &&
i <
s->nb_bands;
i++) {
459 if (!
s->bands[
i].delay_buf)
462 s->delay_buf_size = max_delay_size;
467#define CONVOLVE _ _ _ _
470 double *ibuf,
double *obuf_low,
471 double *obuf_high,
size_t len)
473 double out_low, out_high;
476 p->pos = p->pos ? p->pos - 1 :
N - 1;
477#define _ out_low += p->coefs[j] * p->previous[ch][p->pos + j].in \
478 - p->coefs[2*N+2 + j] * p->previous[ch][p->pos + j].out_low, j++;
481 out_low = p->coefs[0] * *ibuf;
483 *obuf_low++ = out_low;
486#define _ out_high += p->coefs[j+N+1] * p->previous[ch][p->pos + j].in \
487 - p->coefs[2*N+2 + j] * p->previous[ch][p->pos + j].out_high, j++;
490 out_high = p->coefs[
N+1] * *ibuf;
492 *obuf_high++ = out_high;
494 p->previous[ch][p->pos +
N].in = p->previous[ch][p->pos].in = *ibuf++;
495 p->previous[ch][p->pos +
N].out_low = p->previous[ch][p->pos].out_low = out_low;
496 p->previous[ch][p->pos +
N].out_high = p->previous[ch][p->pos].out_high = out_high;
504 for (
i = 0;
i <
len;
i++) {
505 double level_in_lin, level_out_lin, checkbuf;
510 level_in_lin = l->
volume[ch];
513 if (
c->delay_buf_size <= 0) {
514 checkbuf = ibuf[
i] * level_out_lin;
536 l->
delay_size) %
c->delay_buf_size] * level_out_lin;
579 double *
a, *
dst = (
double *)
out->extended_data[ch];
581 for (band = 0, abuf = in, bbuf =
s->band_buf2, cbuf =
s->band_buf1; band < s->nb_bands; band++) {
596 for (
i = 0;
i <
out->nb_samples;
i++) {
638 .p.name =
"mcompand",
640 "Multiband Compress or expand audio dynamic range."),
641 .p.priv_class = &mcompand_class,
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static int request_frame(AVFilterLink *outlink)
static const AVFilterPad mcompand_outputs[]
static void square_quadratic(double const *x, double *y)
static void update_volume(CompBand *cb, double in, int ch)
static int mcompand_channel(MCompandContext *c, CompBand *l, double *ibuf, double *obuf, int len, int ch)
static int parse_points(char *points, int nb_points, double radius, CompandT *s, AVFilterContext *ctx)
static int crossover_setup(AVFilterLink *outlink, Crossover *p, double frequency)
static int request_frame(AVFilterLink *outlink)
static const AVFilterPad mcompand_inputs[]
const FFFilter ff_af_mcompand
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static void count_items(char *item_str, int *nb_items, char delimiter)
static double get_volume(CompandT *s, double in_lin)
static av_cold void uninit(AVFilterContext *ctx)
static void crossover(int ch, Crossover *p, double *ibuf, double *obuf_low, double *obuf_high, size_t len)
static int config_output(AVFilterLink *outlink)
static const AVOption mcompand_options[]
static AVFormatContext * ctx
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
static __device__ float fabs(float a)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
internal math functions header
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
@ AV_SAMPLE_FMT_DBLP
double, planar
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
static const int16_t alpha[]
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
#define AVFILTER_DEFINE_CLASS(fname)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static av_const double hypot(double x, double y)
#define FFSWAP(type, a, b)
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
A link between two filters.
AVFilterContext * src
source filter
int sample_rate
samples per second
AVChannelLayout ch_layout
channel layout of current buffer (see libavutil/channel_layout.h)
AVFilterContext * dst
dest filter
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t ** extended_data
pointers to the data planes/channels.
CompandSegment * segments
static double cb(void *priv, double x, double y)