2013-07-17 00:39:06 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1999 Chris Bagwell
|
|
|
|
* Copyright (c) 1999 Nick Bailey
|
|
|
|
* Copyright (c) 2007 Rob Sykes <robs@users.sourceforge.net>
|
|
|
|
* Copyright (c) 2013 Paul B Mahol
|
2014-02-26 14:40:33 +03:00
|
|
|
* Copyright (c) 2014 Andrew Kelley
|
2013-07-17 00:39:06 +03:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2014-02-26 14:42:47 +03:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* audio compand filter
|
|
|
|
*/
|
|
|
|
|
2013-09-07 04:07:34 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2013-07-17 00:39:06 +03:00
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/samplefmt.h"
|
|
|
|
#include "audio.h"
|
2014-02-26 15:09:30 +03:00
|
|
|
#include "avfilter.h"
|
2013-07-17 00:39:06 +03:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct ChanParam {
|
|
|
|
double attack;
|
|
|
|
double decay;
|
|
|
|
double volume;
|
|
|
|
} ChanParam;
|
|
|
|
|
|
|
|
typedef struct CompandSegment {
|
|
|
|
double x, y;
|
|
|
|
double a, b;
|
|
|
|
} CompandSegment;
|
|
|
|
|
|
|
|
typedef struct CompandContext {
|
|
|
|
const AVClass *class;
|
2014-02-26 15:09:30 +03:00
|
|
|
int nb_segments;
|
2013-07-17 00:39:06 +03:00
|
|
|
char *attacks, *decays, *points;
|
|
|
|
CompandSegment *segments;
|
|
|
|
ChanParam *channels;
|
|
|
|
double in_min_lin;
|
|
|
|
double out_min_lin;
|
|
|
|
double curve_dB;
|
|
|
|
double gain_dB;
|
|
|
|
double initial_volume;
|
|
|
|
double delay;
|
2014-02-26 14:29:22 +03:00
|
|
|
AVFrame *delay_frame;
|
2013-07-17 00:39:06 +03:00
|
|
|
int delay_samples;
|
|
|
|
int delay_count;
|
|
|
|
int delay_index;
|
|
|
|
int64_t pts;
|
|
|
|
|
|
|
|
int (*compand)(AVFilterContext *ctx, AVFrame *frame);
|
|
|
|
} CompandContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(CompandContext, x)
|
|
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption compand_options[] = {
|
2014-02-26 14:53:37 +03:00
|
|
|
{ "attacks", "set time over which increase of volume is determined", OFFSET(attacks), AV_OPT_TYPE_STRING, { .str = "0.3" }, 0, 0, A },
|
|
|
|
{ "decays", "set time over which decrease of volume is determined", OFFSET(decays), AV_OPT_TYPE_STRING, { .str = "0.8" }, 0, 0, A },
|
|
|
|
{ "points", "set points of transfer function", OFFSET(points), AV_OPT_TYPE_STRING, { .str = "-70/-70|-60/-20" }, 0, 0, A },
|
2014-02-26 13:41:36 +03:00
|
|
|
{ "soft-knee", "set soft-knee", OFFSET(curve_dB), AV_OPT_TYPE_DOUBLE, { .dbl = 0.01 }, 0.01, 900, A },
|
|
|
|
{ "gain", "set output gain", OFFSET(gain_dB), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -900, 900, A },
|
|
|
|
{ "volume", "set initial volume", OFFSET(initial_volume), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -900, 0, A },
|
|
|
|
{ "delay", "set delay for samples before sending them to volume adjuster", OFFSET(delay), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, 20, A },
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2013-07-17 00:39:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(compand);
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
CompandContext *s = ctx->priv;
|
2014-02-26 14:44:01 +03:00
|
|
|
s->pts = AV_NOPTS_VALUE;
|
2013-07-17 00:39:06 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
CompandContext *s = ctx->priv;
|
|
|
|
|
|
|
|
av_freep(&s->channels);
|
|
|
|
av_freep(&s->segments);
|
2014-02-26 14:29:22 +03:00
|
|
|
av_frame_free(&s->delay_frame);
|
2013-07-17 00:39:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterChannelLayouts *layouts;
|
|
|
|
AVFilterFormats *formats;
|
|
|
|
static const enum AVSampleFormat sample_fmts[] = {
|
|
|
|
AV_SAMPLE_FMT_DBLP,
|
|
|
|
AV_SAMPLE_FMT_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
layouts = ff_all_channel_layouts();
|
|
|
|
if (!layouts)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ff_set_common_channel_layouts(ctx, layouts);
|
|
|
|
|
|
|
|
formats = ff_make_format_list(sample_fmts);
|
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ff_set_common_formats(ctx, formats);
|
|
|
|
|
|
|
|
formats = ff_all_samplerates();
|
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ff_set_common_samplerates(ctx, formats);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void count_items(char *item_str, int *nb_items)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
*nb_items = 1;
|
|
|
|
for (p = item_str; *p; p++) {
|
2014-02-26 14:04:00 +03:00
|
|
|
if (*p == ' ' || *p == '|')
|
2013-07-17 00:39:06 +03:00
|
|
|
(*nb_items)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_volume(ChanParam *cp, double in)
|
|
|
|
{
|
|
|
|
double delta = in - cp->volume;
|
|
|
|
|
|
|
|
if (delta > 0.0)
|
|
|
|
cp->volume += delta * cp->attack;
|
|
|
|
else
|
|
|
|
cp->volume += delta * cp->decay;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double get_volume(CompandContext *s, double in_lin)
|
|
|
|
{
|
|
|
|
CompandSegment *cs;
|
|
|
|
double in_log, out_log;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (in_lin < s->in_min_lin)
|
|
|
|
return s->out_min_lin;
|
|
|
|
|
|
|
|
in_log = log(in_lin);
|
|
|
|
|
2014-02-25 12:37:26 +03:00
|
|
|
for (i = 1; i < s->nb_segments; i++)
|
|
|
|
if (in_log <= s->segments[i].x)
|
2013-07-17 00:39:06 +03:00
|
|
|
break;
|
2014-02-25 12:37:26 +03:00
|
|
|
cs = &s->segments[i - 1];
|
2013-07-17 00:39:06 +03:00
|
|
|
in_log -= cs->x;
|
|
|
|
out_log = cs->y + in_log * (cs->a * in_log + cs->b);
|
|
|
|
|
|
|
|
return exp(out_log);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compand_nodelay(AVFilterContext *ctx, AVFrame *frame)
|
|
|
|
{
|
2014-02-26 13:41:36 +03:00
|
|
|
CompandContext *s = ctx->priv;
|
2013-07-17 00:39:06 +03:00
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
2014-02-26 18:03:09 +03:00
|
|
|
const int channels = inlink->channels;
|
2013-07-17 00:39:06 +03:00
|
|
|
const int nb_samples = frame->nb_samples;
|
|
|
|
AVFrame *out_frame;
|
|
|
|
int chan, i;
|
2014-02-26 14:49:04 +03:00
|
|
|
int err;
|
2013-07-17 00:39:06 +03:00
|
|
|
|
|
|
|
if (av_frame_is_writable(frame)) {
|
|
|
|
out_frame = frame;
|
|
|
|
} else {
|
|
|
|
out_frame = ff_get_audio_buffer(inlink, nb_samples);
|
2014-02-15 14:38:32 +03:00
|
|
|
if (!out_frame) {
|
|
|
|
av_frame_free(&frame);
|
2013-07-17 00:39:06 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2014-02-15 14:38:32 +03:00
|
|
|
}
|
2014-02-26 14:49:04 +03:00
|
|
|
err = av_frame_copy_props(out_frame, frame);
|
|
|
|
if (err < 0) {
|
|
|
|
av_frame_free(&out_frame);
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return err;
|
|
|
|
}
|
2013-07-17 00:39:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (chan = 0; chan < channels; chan++) {
|
2013-09-15 15:10:12 +03:00
|
|
|
const double *src = (double *)frame->extended_data[chan];
|
|
|
|
double *dst = (double *)out_frame->extended_data[chan];
|
2013-07-17 00:39:06 +03:00
|
|
|
ChanParam *cp = &s->channels[chan];
|
|
|
|
|
|
|
|
for (i = 0; i < nb_samples; i++) {
|
|
|
|
update_volume(cp, fabs(src[i]));
|
|
|
|
|
|
|
|
dst[i] = av_clipd(src[i] * get_volume(s, cp->volume), -1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame != out_frame)
|
|
|
|
av_frame_free(&frame);
|
|
|
|
|
|
|
|
return ff_filter_frame(ctx->outputs[0], out_frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
|
|
|
|
|
|
|
|
static int compand_delay(AVFilterContext *ctx, AVFrame *frame)
|
|
|
|
{
|
2014-02-26 13:41:36 +03:00
|
|
|
CompandContext *s = ctx->priv;
|
2013-07-17 00:39:06 +03:00
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
const int channels = inlink->channels;
|
|
|
|
const int nb_samples = frame->nb_samples;
|
2013-09-07 04:07:34 +03:00
|
|
|
int chan, i, av_uninit(dindex), oindex, av_uninit(count);
|
2014-02-26 13:41:36 +03:00
|
|
|
AVFrame *out_frame = NULL;
|
2014-02-26 14:49:04 +03:00
|
|
|
int err;
|
2013-07-17 00:39:06 +03:00
|
|
|
|
2014-02-26 14:44:01 +03:00
|
|
|
if (s->pts == AV_NOPTS_VALUE) {
|
|
|
|
s->pts = (frame->pts == AV_NOPTS_VALUE) ? 0 : frame->pts;
|
|
|
|
}
|
|
|
|
|
2013-09-07 04:07:34 +03:00
|
|
|
av_assert1(channels > 0); /* would corrupt delay_count and delay_index */
|
|
|
|
|
2013-07-17 00:39:06 +03:00
|
|
|
for (chan = 0; chan < channels; chan++) {
|
2014-02-26 14:29:22 +03:00
|
|
|
AVFrame *delay_frame = s->delay_frame;
|
2014-02-26 18:03:09 +03:00
|
|
|
const double *src = (double *)frame->extended_data[chan];
|
|
|
|
double *dbuf = (double *)delay_frame->extended_data[chan];
|
2014-02-26 14:42:47 +03:00
|
|
|
ChanParam *cp = &s->channels[chan];
|
2013-07-17 00:39:06 +03:00
|
|
|
double *dst;
|
|
|
|
|
|
|
|
count = s->delay_count;
|
|
|
|
dindex = s->delay_index;
|
|
|
|
for (i = 0, oindex = 0; i < nb_samples; i++) {
|
|
|
|
const double in = src[i];
|
|
|
|
update_volume(cp, fabs(in));
|
|
|
|
|
|
|
|
if (count >= s->delay_samples) {
|
|
|
|
if (!out_frame) {
|
|
|
|
out_frame = ff_get_audio_buffer(inlink, nb_samples - i);
|
2014-02-15 14:38:32 +03:00
|
|
|
if (!out_frame) {
|
|
|
|
av_frame_free(&frame);
|
2013-07-17 00:39:06 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2014-02-15 14:38:32 +03:00
|
|
|
}
|
2014-02-26 14:49:04 +03:00
|
|
|
err = av_frame_copy_props(out_frame, frame);
|
|
|
|
if (err < 0) {
|
|
|
|
av_frame_free(&out_frame);
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return err;
|
|
|
|
}
|
2013-07-17 00:39:06 +03:00
|
|
|
out_frame->pts = s->pts;
|
2014-02-26 13:41:36 +03:00
|
|
|
s->pts += av_rescale_q(nb_samples - i,
|
|
|
|
(AVRational){ 1, inlink->sample_rate },
|
|
|
|
inlink->time_base);
|
2013-07-17 00:39:06 +03:00
|
|
|
}
|
|
|
|
|
2013-09-15 15:10:12 +03:00
|
|
|
dst = (double *)out_frame->extended_data[chan];
|
2014-02-26 13:41:36 +03:00
|
|
|
dst[oindex++] = av_clipd(dbuf[dindex] *
|
|
|
|
get_volume(s, cp->volume), -1, 1);
|
2013-07-17 00:39:06 +03:00
|
|
|
} else {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbuf[dindex] = in;
|
|
|
|
dindex = MOD(dindex + 1, s->delay_samples);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->delay_count = count;
|
|
|
|
s->delay_index = dindex;
|
|
|
|
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return out_frame ? ff_filter_frame(ctx->outputs[0], out_frame) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compand_drain(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2014-02-26 13:41:36 +03:00
|
|
|
CompandContext *s = ctx->priv;
|
2014-02-26 18:03:09 +03:00
|
|
|
const int channels = outlink->channels;
|
|
|
|
AVFrame *frame = NULL;
|
2013-07-17 00:39:06 +03:00
|
|
|
int chan, i, dindex;
|
|
|
|
|
2014-02-26 14:59:31 +03:00
|
|
|
/* 2048 is to limit output frame size during drain */
|
2013-07-17 00:39:06 +03:00
|
|
|
frame = ff_get_audio_buffer(outlink, FFMIN(2048, s->delay_count));
|
|
|
|
if (!frame)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
frame->pts = s->pts;
|
2014-02-26 13:41:36 +03:00
|
|
|
s->pts += av_rescale_q(frame->nb_samples,
|
|
|
|
(AVRational){ 1, outlink->sample_rate }, outlink->time_base);
|
2013-07-17 00:39:06 +03:00
|
|
|
|
2014-05-08 07:53:16 +03:00
|
|
|
av_assert0(channels > 0);
|
2013-07-17 00:39:06 +03:00
|
|
|
for (chan = 0; chan < channels; chan++) {
|
2014-02-26 14:29:22 +03:00
|
|
|
AVFrame *delay_frame = s->delay_frame;
|
|
|
|
double *dbuf = (double *)delay_frame->extended_data[chan];
|
2013-09-15 15:10:12 +03:00
|
|
|
double *dst = (double *)frame->extended_data[chan];
|
2013-07-17 00:39:06 +03:00
|
|
|
ChanParam *cp = &s->channels[chan];
|
|
|
|
|
|
|
|
dindex = s->delay_index;
|
|
|
|
for (i = 0; i < frame->nb_samples; i++) {
|
2014-02-26 14:59:31 +03:00
|
|
|
dst[i] = av_clipd(dbuf[dindex] * get_volume(s, cp->volume),
|
2014-02-26 18:03:09 +03:00
|
|
|
-1, 1);
|
2013-07-17 00:39:06 +03:00
|
|
|
dindex = MOD(dindex + 1, s->delay_samples);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->delay_count -= frame->nb_samples;
|
|
|
|
s->delay_index = dindex;
|
|
|
|
|
|
|
|
return ff_filter_frame(outlink, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
2014-02-26 13:41:36 +03:00
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
CompandContext *s = ctx->priv;
|
2013-07-17 00:39:06 +03:00
|
|
|
const int sample_rate = outlink->sample_rate;
|
2014-02-26 14:59:31 +03:00
|
|
|
double radius = s->curve_dB * M_LN10 / 20.0;
|
2014-02-26 15:09:30 +03:00
|
|
|
char *p, *saveptr = NULL;
|
2014-02-26 14:49:04 +03:00
|
|
|
const int channels = outlink->channels;
|
2014-02-26 15:09:30 +03:00
|
|
|
int nb_attacks, nb_decays, nb_points;
|
2013-07-17 00:39:06 +03:00
|
|
|
int new_nb_items, num;
|
|
|
|
int i;
|
2014-02-26 14:29:22 +03:00
|
|
|
int err;
|
|
|
|
|
2013-07-17 00:39:06 +03:00
|
|
|
|
|
|
|
count_items(s->attacks, &nb_attacks);
|
|
|
|
count_items(s->decays, &nb_decays);
|
|
|
|
count_items(s->points, &nb_points);
|
|
|
|
|
2014-02-26 14:49:04 +03:00
|
|
|
if (channels <= 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2014-02-26 14:59:31 +03:00
|
|
|
if (nb_attacks > channels || nb_decays > channels) {
|
2014-02-26 15:09:30 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Number of attacks/decays bigger than number of channels.\n");
|
2013-07-17 00:39:06 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
uninit(ctx);
|
|
|
|
|
2014-02-26 14:50:54 +03:00
|
|
|
s->channels = av_mallocz_array(channels, sizeof(*s->channels));
|
2014-02-25 12:37:26 +03:00
|
|
|
s->nb_segments = (nb_points + 4) * 2;
|
|
|
|
s->segments = av_mallocz_array(s->nb_segments, sizeof(*s->segments));
|
2013-07-17 00:39:06 +03:00
|
|
|
|
2014-02-26 14:11:33 +03:00
|
|
|
if (!s->channels || !s->segments) {
|
|
|
|
uninit(ctx);
|
2013-07-17 00:39:06 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2014-02-26 14:11:33 +03:00
|
|
|
}
|
2013-07-17 00:39:06 +03:00
|
|
|
|
|
|
|
p = s->attacks;
|
|
|
|
for (i = 0, new_nb_items = 0; i < nb_attacks; i++) {
|
2014-02-26 14:04:00 +03:00
|
|
|
char *tstr = av_strtok(p, " |", &saveptr);
|
2013-07-17 00:39:06 +03:00
|
|
|
p = NULL;
|
|
|
|
new_nb_items += sscanf(tstr, "%lf", &s->channels[i].attack) == 1;
|
2014-02-26 14:11:33 +03:00
|
|
|
if (s->channels[i].attack < 0) {
|
|
|
|
uninit(ctx);
|
2013-07-17 00:39:06 +03:00
|
|
|
return AVERROR(EINVAL);
|
2014-02-26 14:11:33 +03:00
|
|
|
}
|
2013-07-17 00:39:06 +03:00
|
|
|
}
|
|
|
|
nb_attacks = new_nb_items;
|
|
|
|
|
|
|
|
p = s->decays;
|
|
|
|
for (i = 0, new_nb_items = 0; i < nb_decays; i++) {
|
2014-02-26 14:04:00 +03:00
|
|
|
char *tstr = av_strtok(p, " |", &saveptr);
|
2013-07-17 00:39:06 +03:00
|
|
|
p = NULL;
|
|
|
|
new_nb_items += sscanf(tstr, "%lf", &s->channels[i].decay) == 1;
|
2014-02-26 14:11:33 +03:00
|
|
|
if (s->channels[i].decay < 0) {
|
|
|
|
uninit(ctx);
|
2013-07-17 00:39:06 +03:00
|
|
|
return AVERROR(EINVAL);
|
2014-02-26 14:11:33 +03:00
|
|
|
}
|
2013-07-17 00:39:06 +03:00
|
|
|
}
|
|
|
|
nb_decays = new_nb_items;
|
|
|
|
|
|
|
|
if (nb_attacks != nb_decays) {
|
2014-02-26 13:41:36 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Number of attacks %d differs from number of decays %d.\n",
|
|
|
|
nb_attacks, nb_decays);
|
2014-02-26 14:11:33 +03:00
|
|
|
uninit(ctx);
|
2013-07-17 00:39:06 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define S(x) s->segments[2 * ((x) + 1)]
|
|
|
|
p = s->points;
|
|
|
|
for (i = 0, new_nb_items = 0; i < nb_points; i++) {
|
2014-02-26 14:04:00 +03:00
|
|
|
char *tstr = av_strtok(p, " |", &saveptr);
|
2013-07-17 00:39:06 +03:00
|
|
|
p = NULL;
|
|
|
|
if (sscanf(tstr, "%lf/%lf", &S(i).x, &S(i).y) != 2) {
|
2014-02-26 13:41:36 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Invalid and/or missing input/output value.\n");
|
2014-02-26 14:11:33 +03:00
|
|
|
uninit(ctx);
|
2013-07-17 00:39:06 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
if (i && S(i - 1).x > S(i).x) {
|
2014-02-26 13:41:36 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Transfer function input values must be increasing.\n");
|
2014-02-26 14:11:33 +03:00
|
|
|
uninit(ctx);
|
2013-07-17 00:39:06 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
S(i).y -= S(i).x;
|
2013-07-30 21:29:46 +03:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "%d: x=%f y=%f\n", i, S(i).x, S(i).y);
|
2013-07-17 00:39:06 +03:00
|
|
|
new_nb_items++;
|
|
|
|
}
|
|
|
|
num = new_nb_items;
|
|
|
|
|
|
|
|
/* Add 0,0 if necessary */
|
|
|
|
if (num == 0 || S(num - 1).x)
|
|
|
|
num++;
|
|
|
|
|
|
|
|
#undef S
|
|
|
|
#define S(x) s->segments[2 * (x)]
|
|
|
|
/* Add a tail off segment at the start */
|
|
|
|
S(0).x = S(1).x - 2 * s->curve_dB;
|
|
|
|
S(0).y = S(1).y;
|
|
|
|
num++;
|
|
|
|
|
|
|
|
/* Join adjacent colinear segments */
|
|
|
|
for (i = 2; i < num; i++) {
|
|
|
|
double g1 = (S(i - 1).y - S(i - 2).y) * (S(i - 0).x - S(i - 1).x);
|
|
|
|
double g2 = (S(i - 0).y - S(i - 1).y) * (S(i - 1).x - S(i - 2).x);
|
|
|
|
int j;
|
|
|
|
|
|
|
|
if (fabs(g1 - g2))
|
|
|
|
continue;
|
|
|
|
num--;
|
|
|
|
for (j = --i; j < num; j++)
|
|
|
|
S(j) = S(j + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; !i || s->segments[i - 2].x; i += 2) {
|
|
|
|
s->segments[i].y += s->gain_dB;
|
|
|
|
s->segments[i].x *= M_LN10 / 20;
|
|
|
|
s->segments[i].y *= M_LN10 / 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define L(x) s->segments[i - (x)]
|
|
|
|
for (i = 4; s->segments[i - 2].x; i += 2) {
|
|
|
|
double x, y, cx, cy, in1, in2, out1, out2, theta, len, r;
|
|
|
|
|
|
|
|
L(4).a = 0;
|
|
|
|
L(4).b = (L(2).y - L(4).y) / (L(2).x - L(4).x);
|
|
|
|
|
|
|
|
L(2).a = 0;
|
|
|
|
L(2).b = (L(0).y - L(2).y) / (L(0).x - L(2).x);
|
|
|
|
|
|
|
|
theta = atan2(L(2).y - L(4).y, L(2).x - L(4).x);
|
|
|
|
len = sqrt(pow(L(2).x - L(4).x, 2.) + pow(L(2).y - L(4).y, 2.));
|
|
|
|
r = FFMIN(radius, len);
|
|
|
|
L(3).x = L(2).x - r * cos(theta);
|
|
|
|
L(3).y = L(2).y - r * sin(theta);
|
|
|
|
|
|
|
|
theta = atan2(L(0).y - L(2).y, L(0).x - L(2).x);
|
|
|
|
len = sqrt(pow(L(0).x - L(2).x, 2.) + pow(L(0).y - L(2).y, 2.));
|
|
|
|
r = FFMIN(radius, len / 2);
|
|
|
|
x = L(2).x + r * cos(theta);
|
|
|
|
y = L(2).y + r * sin(theta);
|
|
|
|
|
|
|
|
cx = (L(3).x + L(2).x + x) / 3;
|
|
|
|
cy = (L(3).y + L(2).y + y) / 3;
|
|
|
|
|
|
|
|
L(2).x = x;
|
|
|
|
L(2).y = y;
|
|
|
|
|
2014-02-26 13:41:36 +03:00
|
|
|
in1 = cx - L(3).x;
|
2013-07-17 00:39:06 +03:00
|
|
|
out1 = cy - L(3).y;
|
2014-02-26 13:41:36 +03:00
|
|
|
in2 = L(2).x - L(3).x;
|
2013-07-17 00:39:06 +03:00
|
|
|
out2 = L(2).y - L(3).y;
|
2014-02-26 13:41:36 +03:00
|
|
|
L(3).a = (out2 / in2 - out1 / in1) / (in2 - in1);
|
2013-07-17 00:39:06 +03:00
|
|
|
L(3).b = out1 / in1 - L(3).a * in1;
|
|
|
|
}
|
|
|
|
L(3).x = 0;
|
|
|
|
L(3).y = L(2).y;
|
|
|
|
|
|
|
|
s->in_min_lin = exp(s->segments[1].x);
|
|
|
|
s->out_min_lin = exp(s->segments[1].y);
|
|
|
|
|
2014-02-26 14:50:54 +03:00
|
|
|
for (i = 0; i < channels; i++) {
|
2013-07-17 00:39:06 +03:00
|
|
|
ChanParam *cp = &s->channels[i];
|
|
|
|
|
|
|
|
if (cp->attack > 1.0 / sample_rate)
|
|
|
|
cp->attack = 1.0 - exp(-1.0 / (sample_rate * cp->attack));
|
|
|
|
else
|
|
|
|
cp->attack = 1.0;
|
|
|
|
if (cp->decay > 1.0 / sample_rate)
|
|
|
|
cp->decay = 1.0 - exp(-1.0 / (sample_rate * cp->decay));
|
|
|
|
else
|
|
|
|
cp->decay = 1.0;
|
|
|
|
cp->volume = pow(10.0, s->initial_volume / 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->delay_samples = s->delay * sample_rate;
|
2014-02-26 14:29:22 +03:00
|
|
|
if (s->delay_samples <= 0) {
|
2013-07-17 00:39:06 +03:00
|
|
|
s->compand = compand_nodelay;
|
2014-02-26 14:29:22 +03:00
|
|
|
return 0;
|
2013-07-17 00:39:06 +03:00
|
|
|
}
|
2014-02-26 14:29:22 +03:00
|
|
|
|
|
|
|
s->delay_frame = av_frame_alloc();
|
|
|
|
if (!s->delay_frame) {
|
|
|
|
uninit(ctx);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->delay_frame->format = outlink->format;
|
|
|
|
s->delay_frame->nb_samples = s->delay_samples;
|
|
|
|
s->delay_frame->channel_layout = outlink->channel_layout;
|
|
|
|
|
|
|
|
err = av_frame_get_buffer(s->delay_frame, 32);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
|
|
|
|
s->compand = compand_delay;
|
2013-07-17 00:39:06 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2014-02-26 13:41:36 +03:00
|
|
|
CompandContext *s = ctx->priv;
|
2013-07-17 00:39:06 +03:00
|
|
|
|
|
|
|
return s->compand(ctx, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2014-02-26 13:41:36 +03:00
|
|
|
CompandContext *s = ctx->priv;
|
2013-07-17 00:39:06 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_request_frame(ctx->inputs[0]);
|
|
|
|
|
|
|
|
if (ret == AVERROR_EOF && !ctx->is_disabled && s->delay_count)
|
|
|
|
ret = compand_drain(outlink);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad compand_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2013-07-17 00:39:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad compand_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.request_frame = request_frame,
|
|
|
|
.config_props = config_output,
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2013-07-17 00:39:06 +03:00
|
|
|
};
|
|
|
|
|
2014-02-26 13:41:36 +03:00
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
AVFilter ff_af_compand = {
|
2014-02-26 13:41:36 +03:00
|
|
|
.name = "compand",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL(
|
|
|
|
"Compress or expand audio dynamic range."),
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.priv_size = sizeof(CompandContext),
|
|
|
|
.priv_class = &compand_class,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.inputs = compand_inputs,
|
|
|
|
.outputs = compand_outputs,
|
2013-07-17 00:39:06 +03:00
|
|
|
};
|