On Fri, 14 Feb 2014 02:56:46 -0500, Andrew Kelley <superjo...@gmail.com> wrote: > This patch adds the `compand` audio filter from ffmpeg master branch > (currently at 7f0f47b3df) adapted to work with libav. > > The following changes are made: > > * use float instead of double > * use strtok_r instead of av_strtok > * remove asserts > * use AVFrame instead of manually allocating memory > --- > Changelog | 1 + > doc/filters.texi | 74 +++++++ > libavfilter/Makefile | 1 + > libavfilter/af_compand.c | 548 > +++++++++++++++++++++++++++++++++++++++++++++++ > libavfilter/allfilters.c | 1 + > libavfilter/version.h | 2 +- > 6 files changed, 626 insertions(+), 1 deletion(-) > create mode 100644 libavfilter/af_compand.c > > diff --git a/Changelog b/Changelog > index bed6c31..74b7f1a 100644 > --- a/Changelog > +++ b/Changelog > @@ -58,6 +58,7 @@ version 10: > - Mirillis FIC video decoder > - Support DNx444 > - libx265 encoder > +- compand audio filter > > > version 9: > diff --git a/doc/filters.texi b/doc/filters.texi > index 8c83b4e..8863384 100644 > --- a/doc/filters.texi > +++ b/doc/filters.texi > @@ -467,6 +467,80 @@ To fix a 5.1 WAV improperly encoded in AAC's native > channel order > avconv -i in.wav -filter 'channelmap=1|2|0|5|3|4:channel_layout=5.1' out.wav > @end example > > +@section compand > +Compress or expand audio dynamic range. > + > +A description of the accepted options follows. > + > +@table @option > + > +@item attacks > +@item decays > +Set list of times in seconds for each channel over which the instantaneous > level > +of the input signal is averaged to determine its volume. @var{attacks} > refers to > +increase of volume and @var{decays} refers to decrease of volume. For most > +situations, the attack time (response to the audio getting louder) should be > +shorter than the decay time because the human ear is more sensitive to sudden > +loud audio than sudden soft audio. Typical value for attack is 0.3 seconds > and > +for decay 0.8 seconds. > + > +@item points > +Set list of points for transfer function, specified in dB relative to maximum > +possible signal amplitude. Each key points list need to be defined using the > +following syntax: @code{x0/y0 x1/y1 x2/y2 ....} > + > +The input values must be in strictly increasing order but the transfer > function > +does not have to be monotonically rising. The point @code{0/0} is assumed but > +may be overridden (by @code{0/out-dBn}). Typical values for the transfer > +function are @code{-70/-70 -60/-20}. > + > +@item soft-knee > +Set amount for which the points at where adjacent line segments on the > transfer > +function meet will be rounded. Defaults is 0.01. > + > +@item gain > +Set additional gain in dB to be applied at all points on the transfer > function > +and allows easy adjustment of the overall gain. Default is 0. > + > +@item volume > +Set initial volume in dB to be assumed for each channel when filtering > starts. > +This permits the user to supply a nominal level initially, so that, for > +example, a very large gain is not applied to initial signal levels before the > +companding has begun to operate. A typical value for audio which is initially > +quiet is -90 dB. Default is 0. > + > +@item delay > +Set delay in seconds. Default is 0. The input audio is analysed immediately, > +but audio is delayed before being fed to the volume adjuster. Specifying a > +delay approximately equal to the attack/decay times allows the filter to > +effectively operate in predictive rather than reactive mode. > + > +@end table > + > +@subsection Examples > + > +@itemize > +@item > +Make music with both quiet and loud passages suitable for listening in a > noisy > +environment: > +@example > +compand=.3 .3:1 1:-90/-60 -60/-40 -40/-30 -20/-20:6:0:-90:0.2
I see the elements in the list are divided by spaces, which doesn't seem to be mentioned anywhere. Also, other filters usually use '|' for this. > +@end example > + > +@item > +Noise-gate for when the noise is at a lower level than the signal: > +@example > +compand=.1 .1:.2 .2:-900/-900 -50.1/-900 -50/-50:.01:0:-90:.1 > +@end example > + > +@item > +Here is another noise-gate, this time for when the noise is at a higher level > +than the signal (making it, in some ways, similar to squelch): > +@example > +compand=.1 .1:.1 .1:-45.1/-45.1 -45/-900 0/-900:.01:45:-90:.1 > +@end example > +@end itemize > + > @section join > Join multiple input streams into one multi-channel stream. > > diff --git a/libavfilter/Makefile b/libavfilter/Makefile > index 92c1561..2badb3e 100644 > --- a/libavfilter/Makefile > +++ b/libavfilter/Makefile > @@ -37,6 +37,7 @@ OBJS-$(CONFIG_CHANNELSPLIT_FILTER) += > af_channelsplit.o > OBJS-$(CONFIG_JOIN_FILTER) += af_join.o > OBJS-$(CONFIG_RESAMPLE_FILTER) += af_resample.o > OBJS-$(CONFIG_VOLUME_FILTER) += af_volume.o > +OBJS-$(CONFIG_COMPAND_FILTER) += af_compand.o > > OBJS-$(CONFIG_ANULLSRC_FILTER) += asrc_anullsrc.o > > diff --git a/libavfilter/af_compand.c b/libavfilter/af_compand.c > new file mode 100644 > index 0000000..d46dfa4 > --- /dev/null > +++ b/libavfilter/af_compand.c > @@ -0,0 +1,548 @@ > +/* > + * Copyright (c) 1999 Chris Bagwell > + * Copyright (c) 1999 Nick Bailey > + * Copyright (c) 2007 Rob Sykes <r...@users.sourceforge.net> > + * Copyright (c) 2013 Paul B Mahol > + * Copyright (c) 2014 Andrew Kelley > + * > + * This file is part of libav. > + * > + * Libav 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. > + * > + * Libav 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 Libav; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 > USA > + * > + */ > + > +/** > + * @file > + * audio compand filter > + */ > + > +#include <string.h> > + > +#include "libavutil/channel_layout.h" > +#include "libavutil/common.h" > +#include "libavutil/mathematics.h" > +#include "libavutil/mem.h" > +#include "libavutil/opt.h" > +#include "audio.h" > +#include "avfilter.h" > +#include "formats.h" > +#include "internal.h" > + > +typedef struct ChanParam { > + float attack; > + float decay; > + float volume; > +} ChanParam; > + > +typedef struct CompandSegment { > + float x, y; > + float a, b; > +} CompandSegment; > + > +typedef struct CompandContext { > + const AVClass *class; > + int nb_channels; > + char *attacks, *decays, *points; > + CompandSegment *segments; > + ChanParam *channels; > + float in_min_lin; > + float out_min_lin; > + float curve_dB; > + float gain_dB; > + float initial_volume; > + float delay; > + AVFrame *delay_frame; > + 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 > + > +static const AVOption compand_options[] = { > + { "attacks", "set time over which increase of volume is determined", > OFFSET(attacks), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A }, > + { "decays", "set time over which decrease of volume is determined", > OFFSET(decays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A }, > + { "points", "set points of transfer function", OFFSET(points), > AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A }, > + { "soft-knee", "set soft-knee", OFFSET(curve_dB), AV_OPT_TYPE_FLOAT, > {.dbl=0.01}, 0.01, 900, A }, > + { "gain", "set output gain", OFFSET(gain_dB), AV_OPT_TYPE_FLOAT, > {.dbl=0}, -900, 900, A }, > + { "volume", "set initial volume", OFFSET(initial_volume), > AV_OPT_TYPE_FLOAT, {.dbl=0}, -900, 0, A }, > + { "delay", "set delay for samples before sending them to volume > adjuster", OFFSET(delay), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 20, A }, > + { NULL } > +}; > + > +static const AVClass compand_class = { > + .class_name = "compand filter", > + .item_name = av_default_item_name, > + .option = compand_options, > + .version = LIBAVUTIL_VERSION_INT, > +}; > + > +static av_cold int init(AVFilterContext *ctx) > +{ > + CompandContext *s = ctx->priv; > + > + if (!s->attacks || !s->decays || !s->points) { > + av_log(ctx, AV_LOG_ERROR, "Missing attacks and/or decays and/or > points.\n"); > + return AVERROR(EINVAL); > + } > + > + return 0; > +} > + > +static av_cold void uninit(AVFilterContext *ctx) > +{ > + CompandContext *s = ctx->priv; > + > + av_freep(&s->channels); > + av_freep(&s->segments); > + av_frame_free(&s->delay_frame); > +} > + > +static int query_formats(AVFilterContext *ctx) > +{ > + AVFilterChannelLayouts *layouts; > + AVFilterFormats *formats; > + static const enum AVSampleFormat sample_fmts[] = { > + AV_SAMPLE_FMT_FLTP, > + 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++) { > + if (*p == ' ') > + (*nb_items)++; > + } > + > +} > + > +static void update_volume(ChanParam *cp, float in) > +{ > + float delta = in - cp->volume; > + > + if (delta > 0.0) > + cp->volume += delta * cp->attack; > + else > + cp->volume += delta * cp->decay; > +} > + > +static float get_volume(CompandContext *s, float in_lin) > +{ > + CompandSegment *cs; > + float in_log, out_log; > + int i; > + > + if (in_lin < s->in_min_lin) > + return s->out_min_lin; > + > + in_log = log(in_lin); > + > + for (i = 1;; i++) > + if (in_log <= s->segments[i + 1].x) > + break; > + > + cs = &s->segments[i]; > + 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) > +{ > + CompandContext *s = ctx->priv; > + AVFilterLink *inlink = ctx->inputs[0]; > + const int channels = s->nb_channels; > + const int nb_samples = frame->nb_samples; > + AVFrame *out_frame; > + int chan, i; > + > + if (av_frame_is_writable(frame)) { > + out_frame = frame; > + } else { > + out_frame = ff_get_audio_buffer(inlink, nb_samples); > + if (!out_frame) > + return AVERROR(ENOMEM); Leaking frame here. > + av_frame_copy_props(out_frame, frame); This should be checked for errors. > + } > + > + for (chan = 0; chan < channels; chan++) { > + const float *src = (float *)frame->extended_data[chan]; > + float *dst = (float *)out_frame->extended_data[chan]; > + ChanParam *cp = &s->channels[chan]; > + > + for (i = 0; i < nb_samples; i++) { > + update_volume(cp, fabs(src[i])); > + > + dst[i] = av_clipf(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) > +{ > + CompandContext *s = ctx->priv; > + AVFilterLink *inlink = ctx->inputs[0]; > + const int channels = s->nb_channels; > + const int nb_samples = frame->nb_samples; > + int chan, i, av_uninit(dindex), oindex, av_uninit(count); > + AVFrame *out_frame = NULL; > + > + for (chan = 0; chan < channels; chan++) { > + AVFrame *delay_frame = s->delay_frame; > + const float *src = (float *)frame->extended_data[chan]; > + float *dbuf = (float *)delay_frame->extended_data[chan]; > + ChanParam *cp = &s->channels[chan]; > + float *dst; > + > + count = s->delay_count; > + dindex = s->delay_index; > + for (i = 0, oindex = 0; i < nb_samples; i++) { > + const float 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); > + if (!out_frame) > + return AVERROR(ENOMEM); > + av_frame_copy_props(out_frame, frame); Same as above -- leaking frame + error check needed -- Anton Khirnov _______________________________________________ libav-devel mailing list libav-devel@libav.org https://lists.libav.org/mailman/listinfo/libav-devel