[FFmpeg-devel] [PATCH] avfilter: add adelay filter

Paul B Mahol onemda at gmail.com
Sun Sep 15 13:28:30 CEST 2013


On 9/15/13, Stefano Sabatini <stefasab at gmail.com> wrote:
> On date Friday 2013-09-13 17:42:08 +0000, Paul B Mahol encoded:
>> Signed-off-by: Paul B Mahol <onemda at gmail.com>
>> ---
>>  doc/filters.texi         |  15 +++
>>  libavfilter/Makefile     |   1 +
>>  libavfilter/af_adelay.c  | 296
>> +++++++++++++++++++++++++++++++++++++++++++++++
>>  libavfilter/allfilters.c |   1 +
>>  4 files changed, 313 insertions(+)
>>  create mode 100644 libavfilter/af_adelay.c
>>
>> diff --git a/doc/filters.texi b/doc/filters.texi
>> index 7f8d1b2..d4cec8a 100644
>> --- a/doc/filters.texi
>> +++ b/doc/filters.texi
>> @@ -347,6 +347,21 @@ aconvert=u8:auto
>>  @end example
>>  @end itemize
>>
>> + at section adelay
>> +
>> +Delay one or more audio channels.
>> +
>> +The filter accepts the following option:
>> +
>
> Please specify what happens when an audio channels is delayed (I
> suppose it is filled with silence).

Added.

>
>> + at table @option
>> + at item delays
>> +Set list of delays in milliseconds for each channel.
>> +At least one delay greater than 0 should be provided.
>> +Unused delays will be silently ignored. If number
>> +of given delays is smaller than numer of channels all
>> +remaining channels will be un-delayed.
>
> Missing separator declaration. Also I wonder if it makes sense to
> specify time durations instead.


Well, usage is to use reasonably small delays.
Minutes/hours needs memory.

>
>> + at end table
>> +
>>  @section aecho
>>
>>  Apply echoing to the input audio.
>> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
>> index b57d4c9..5a82c84 100644
>> --- a/libavfilter/Makefile
>> +++ b/libavfilter/Makefile
>> @@ -52,6 +52,7 @@ OBJS-$(CONFIG_AVFORMAT)                      +=
>> lavfutils.o
>>  OBJS-$(CONFIG_SWSCALE)                       += lswsutils.o
>>
>>  OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
>> +OBJS-$(CONFIG_ADELAY_FILTER)                 += af_adelay.o
>>  OBJS-$(CONFIG_AECHO_FILTER)                  += af_aecho.o
>>  OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
>>  OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
>> diff --git a/libavfilter/af_adelay.c b/libavfilter/af_adelay.c
>> new file mode 100644
>> index 0000000..b74ddaf
>> --- /dev/null
>> +++ b/libavfilter/af_adelay.c
>> @@ -0,0 +1,296 @@
>> +/*
>> + * Copyright (c) 2013 Paul B Mahol
>> + *
>> + * 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
>> + *
>> + */
>> +
>> +#include "libavutil/avstring.h"
>> +#include "libavutil/opt.h"
>> +#include "libavutil/samplefmt.h"
>> +#include "avfilter.h"
>> +#include "audio.h"
>> +#include "internal.h"
>> +
>> +typedef struct ChanDelay {
>> +    int delay;
>> +    unsigned delay_index;
>> +    unsigned index;
>> +    uint8_t *samples;
>> +} ChanDelay;
>> +
>> +typedef struct AudioDelayContext {
>> +    const AVClass *class;
>> +    char *delays;
>> +    ChanDelay *chandelay;
>> +    int nb_delays;
>> +    int block_align;
>> +    unsigned max_delay;
>> +    int64_t next_pts;
>> +
>> +    void (*delay_channel)(ChanDelay *d, int nb_samples,
>> +                          const uint8_t *src, uint8_t *dst);
>> +} AudioDelayContext;
>> +
>> +#define OFFSET(x) offsetof(AudioDelayContext, x)
>> +#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
>> +
>> +static const AVOption adelay_options[] = {
>> +    { "delays", "set list of delays for each channel", OFFSET(delays),
>> AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
>> +    { NULL }
>> +};
>> +
>> +AVFILTER_DEFINE_CLASS(adelay);
>> +
>> +static av_cold int init(AVFilterContext *ctx)
>> +{
>> +    AudioDelayContext *s = ctx->priv;
>> +
>
>> +    if (!s->delays) {
>> +        av_log(ctx, AV_LOG_ERROR, "Missing delays.\n");
>
> Nit: no need for final point (no complete sentence)
>
>> +        return AVERROR(EINVAL);
>
> or maybe it could work a as a no-op (simplify scripting sometimes).

It was trivial change, thus changed that way (also less lines).

>
>> +    }
>> +
>> +    return 0;
>> +}
>> +
>> +static int query_formats(AVFilterContext *ctx)
>> +{
>> +    AVFilterChannelLayouts *layouts;
>> +    AVFilterFormats *formats;
>> +    static const enum AVSampleFormat sample_fmts[] = {
>> +        AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
>> +        AV_SAMPLE_FMT_FLTP, 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;
>> +}
>> +
>> +#define DELAY(name, type, fill)
>> \
>> +static void delay_channel_## name ##p(ChanDelay *d, int nb_samples,
>> \
>> +                                      const uint8_t *ssrc, uint8_t *ddst)
>> \
>> +{
>> \
>> +    const type *src = (type *)ssrc;
>> \
>> +    type *dst = (type *)ddst;
>> \
>> +    type *samples = (type *)d->samples;
>> \
>> +
>> \
>> +    while (nb_samples) {
>> \
>> +        if (d->delay_index < d->delay) {
>> \
>> +            const int len = FFMIN(nb_samples, d->delay - d->delay_index);
>> \
>> +
>> \
>> +            memcpy(&samples[d->delay_index], src, len * sizeof(type));
>> \
>> +            memset(dst, fill, len * sizeof(type));
>> \
>> +            d->delay_index += len;
>> \
>> +            src += len;
>> \
>> +            dst += len;
>> \
>> +            nb_samples -= len;
>> \
>> +        } else {
>> \
>> +            *dst = samples[d->index];
>> \
>> +            samples[d->index] = *src;
>> \
>> +            nb_samples--;
>> \
>> +            d->index++;
>> \
>> +            src++, dst++;
>> \
>> +            d->index %= d->delay;
>> \
>> +        }
>> \
>> +    }
>> \
>> +}
>> +
>> +DELAY(u8,  uint8_t, 0x80)
>> +DELAY(s16, int16_t, 0)
>> +DELAY(s32, int32_t, 0)
>> +DELAY(flt, float,   0)
>> +DELAY(dbl, double,  0)
>> +
>> +static int config_input(AVFilterLink *inlink)
>> +{
>> +    AVFilterContext *ctx = inlink->dst;
>> +    AudioDelayContext *s = ctx->priv;
>> +    char *p, *arg, *saveptr = NULL;
>> +    int i;
>> +
>> +    s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
>> +    if (!s->chandelay)
>> +        return AVERROR(ENOMEM);
>> +    s->nb_delays = inlink->channels;
>> +    s->block_align = av_get_bytes_per_sample(inlink->format);
>> +
>> +    p = s->delays;
>> +    for (i = 0; i < s->nb_delays; i++) {
>> +        ChanDelay *d = &s->chandelay[i];
>> +        float delay;
>> +
>> +        if (!(arg = av_strtok(p, "|", &saveptr)))
>> +            break;
>> +
>> +        p = NULL;
>> +        sscanf(arg, "%f", &delay);
>> +
>> +        d->delay = delay * inlink->sample_rate / 1000.0;
>> +        if (d->delay < 0) {
>> +            av_log(ctx, AV_LOG_ERROR, "Delay must be non-negative
>> number.\n");
>
> Nit: a non negative number
>
> Same remark about milliseconds vs. time duration specification.
>
> Also: would it make sense to specify a negative delay?

That would make code more complicated. Feel free to send patch.

>
>> +            return AVERROR(EINVAL);
>> +        }
>> +    }
>> +
>> +    for (i = 0; i < s->nb_delays; i++) {
>> +        ChanDelay *d = &s->chandelay[i];
>> +
>> +        if (!d->delay)
>> +            continue;
>> +
>> +        d->samples = av_malloc_array(d->delay, s->block_align);
>> +        if (!d->samples)
>> +            return AVERROR(ENOMEM);
>> +
>> +        s->max_delay = FFMAX(s->max_delay, d->delay);
>> +    }
>> +
>> +    if (!s->max_delay) {
>
>> +        av_log(ctx, AV_LOG_ERROR, "At least one delay >0 needed.\n");
>
> Nit: is needed / must be specified.

Done.

>
> [...]
> --
> FFmpeg = Fiendish and Fancy Murdering Power Exxagerate Goblin
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>


More information about the ffmpeg-devel mailing list