[FFmpeg-devel] [ffmpeg-devel] [PATCH 3/4] lavfi: add audio convert filter

Stefano Sabatini stefano.sabatini-lala at poste.it
Tue Aug 2 16:09:26 CEST 2011


On date Monday 2011-08-01 11:52:21 +0300, Mina Nagy Zaki encoded:
> Previous patch had a type. 

> From d1d174123e8c470b570f51654121851dd60340da Mon Sep 17 00:00:00 2001
> From: Mina Nagy Zaki <mnzaki at gmail.com>
> Date: Mon, 4 Jul 2011 11:35:39 +0300
> Subject: [PATCH 04/11] lavfi: add audio convert filter
> 
> Add aconvert filter to perform sample format and channel layout conversion.
> 
> Based on code by Stefano Sabatini and "S.N. Hemanth Meenakshisundaram"
> smeenaks at ucsd.edu.
> ---
>  libavfilter/Makefile               |    2 +
>  libavfilter/af_aconvert.c          |  431 ++++++++++++++++++++++++++++++++++++
>  libavfilter/af_aconvert_rematrix.c |  184 +++++++++++++++
>  libavfilter/allfilters.c           |    1 +
>  4 files changed, 618 insertions(+), 0 deletions(-)
>  create mode 100644 libavfilter/af_aconvert.c
>  create mode 100644 libavfilter/af_aconvert_rematrix.c
> 
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 83b906d..ca54bd3 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -2,6 +2,7 @@ include $(SUBDIR)../config.mak
>  
>  NAME = avfilter
>  FFLIBS = avutil
> +FFLIBS-$(CONFIG_ACONVERT_FILTER) += avcodec
>  FFLIBS-$(CONFIG_MOVIE_FILTER) += avformat avcodec
>  FFLIBS-$(CONFIG_SCALE_FILTER) += swscale
>  FFLIBS-$(CONFIG_MP_FILTER) += avcodec
> @@ -18,6 +19,7 @@ OBJS = allfilters.o                                                     \
>  
>  OBJS-$(CONFIG_AVCODEC)                       += avcodec.o
>  
> +OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
>  OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
>  OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
>  
> diff --git a/libavfilter/af_aconvert.c b/libavfilter/af_aconvert.c
> new file mode 100644
> index 0000000..e95e360
> --- /dev/null
> +++ b/libavfilter/af_aconvert.c
> @@ -0,0 +1,431 @@
> +/*
> + * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram <smeenaks at ucsd.edu>
> + * Copyright (c) 2011 Stefano Sabatini
> + * Copyright (c) 2011 Mina Nagy Zaki
> + *
> + * 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
> + */
> +
> +/**
> + * @file
> + * sample format and channel layout conversion audio filter
> + * based on code in libavcodec/resample.c by Fabrice Bellard and
> + * libavcodec/audioconvert.c by Michael Niedermayer
> + */
> +
> +#include "avfilter.h"

> +#include "libavcodec/audioconvert.h"

libavutil

[...]
> +static void init_buffers(AVFilterLink *inlink, int nb_samples)

This may fail, better to return an int.

> +{
> +    AConvertContext *aconvert = inlink->dst->priv;
> +    AVFilterLink * const outlink = inlink->dst->outputs[0];
> +    int i, packed_stride = 0;
> +    int in_channels  =
> +            av_get_channel_layout_nb_channels(inlink->channel_layout),
> +        out_channels =
> +            av_get_channel_layout_nb_channels(outlink->channel_layout);
> +    const short
> +        stereo_downmix = inlink->channel_layout != outlink->channel_layout &&
> +                         !aconvert->convert_chlayout,
> +        format_conv    = inlink->format != outlink->format,
> +        packing_conv   = inlink->planar != outlink->planar &&
> +                         in_channels    != 1               &&
> +                         out_channels   != 1;
> +
> +    aconvert->nb_samples = nb_samples;
> +    uninit(inlink->dst);
> +
> +    // rematrixing
> +    if (aconvert->convert_chlayout) {
> +        aconvert->mix_samplesref =
> +                avfilter_get_audio_buffer(outlink,
> +                                          AV_PERM_WRITE | AV_PERM_REUSE2,
> +                                          inlink->format,
> +                                          nb_samples,
> +                                          outlink->channel_layout,
> +                                          inlink->planar);
> +        in_channels = out_channels;
> +    }
> +
> +    /* If there's any conversion left to do, we need a buffer */
> +    if (format_conv || packing_conv || stereo_downmix) {
> +        aconvert->out_samplesref = avfilter_get_audio_buffer(outlink,
> +                                          AV_PERM_WRITE | AV_PERM_REUSE2,
> +                                          outlink->format,
> +                                          nb_samples,
> +                                          outlink->channel_layout,
> +                                          outlink->planar);
> +    }
> +
> +    /* if there's a format/mode conversion or packed stereo downmixing,
> +     * we need an audio_convert context
> +     */
> +    if (format_conv || packing_conv || (stereo_downmix && !outlink->planar)) {
> +        aconvert->in_strides[0]  = av_get_bytes_per_sample(inlink->format);
> +        aconvert->out_strides[0] = av_get_bytes_per_sample(outlink->format);
> +
> +        aconvert->out_data = aconvert->out_samplesref->data;
> +        if (aconvert->mix_samplesref)
> +            aconvert->in_data  = aconvert->mix_samplesref->data;
> +
> +        if (packing_conv) {
> +            if (outlink->planar) {
> +                if (aconvert->mix_samplesref)
> +                    aconvert->packed_data[0] =
> +                        aconvert->mix_samplesref->data[0];
> +                aconvert->in_data         = aconvert->packed_data;
> +                packed_stride             = aconvert->in_strides[0];
> +                aconvert->in_strides[0]  *= in_channels;
> +            } else {
> +                aconvert->packed_data[0]  = aconvert->out_samplesref->data[0];
> +                aconvert->out_data        = aconvert->packed_data;
> +                packed_stride             = aconvert->out_strides[0];
> +                aconvert->out_strides[0] *= out_channels;
> +            }
> +        } else if (!outlink->planar) {
> +            out_channels = 1;
> +        }
> +
> +        for (i = 1; i < out_channels; i++) {
> +            aconvert->packed_data[i] = aconvert->packed_data[i-1] +
> +                                           packed_stride;
> +            aconvert->in_strides[i]  = aconvert->in_strides[0];
> +            aconvert->out_strides[i] = aconvert->out_strides[0];
> +        }
> +

> +        aconvert->audioconvert_ctx =
> +                av_audio_convert_alloc(outlink->format, out_channels,
> +                                       inlink->format,  out_channels, NULL, 0);

missing check, also you may avoid to free+reinitialize this each
time. Can you set this during the configuration phase? (also it may
fail in case the conversion is not supported, and the filter should
fail as soon as possible).

> +
> +    }
> +
> +}
> +
> +static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
> +{
> +    AConvertContext *aconvert = inlink->dst->priv;
> +    AVFilterBufferRef *curbuf = insamplesref;
> +    AVFilterLink * const outlink = inlink->dst->outputs[0];
> +    int nb_channels = av_get_channel_layout_nb_channels(
> +                          curbuf->audio->channel_layout);
> +
> +    if (!aconvert->nb_samples ||
> +        (curbuf->audio->nb_samples > aconvert->nb_samples))

I suggest aconvert->max_nb_samples (more intelligible to my eyes)

> +        init_buffers(inlink, curbuf->audio->nb_samples);
> +
> +    if (aconvert->mix_samplesref) {
> +        if (inlink->planar && nb_channels != 1)
> +            aconvert->convert_chlayout(aconvert->mix_samplesref->data,
> +                                       curbuf->data,
> +                                       curbuf->audio->nb_samples,
> +                                       nb_channels);
> +        else
> +            aconvert->convert_chlayout(aconvert->mix_samplesref->data[0],
> +                                       curbuf->data[0],
> +                                       curbuf->audio->nb_samples,
> +                                       nb_channels);
> +
> +        aconvert->mix_samplesref->audio->nb_samples =
> +            curbuf->audio->nb_samples;
> +        curbuf = aconvert->mix_samplesref;
> +
> +    }
> +
> +    if (aconvert->audioconvert_ctx) {
> +        if (!aconvert->mix_samplesref) {
> +            if (aconvert->in_data == aconvert->packed_data) {
> +                int i, packed_stride = av_get_bytes_per_sample(inlink->format);
> +                aconvert->packed_data[0] = curbuf->data[0];
> +                for (i = 1; i < nb_channels; i++)
> +                    aconvert->packed_data[i] =
> +                                aconvert->packed_data[i-1] + packed_stride;
> +            } else {
> +                aconvert->in_data = curbuf->data;
> +            }
> +        }
> +
> +        if (inlink->planar == outlink->planar && !outlink->planar)
> +            nb_channels = av_get_channel_layout_nb_channels(
> +                              curbuf->audio->channel_layout);
> +        else
> +            nb_channels = 1;
> +
> +        av_audio_convert(aconvert->audioconvert_ctx,
> +                         (void * const *) aconvert->out_data,
> +                         aconvert->out_strides,
> +                         (const void * const *) aconvert->in_data,
> +                         aconvert->in_strides,
> +                         curbuf->audio->nb_samples * nb_channels);
> +
> +        aconvert->out_samplesref->audio->nb_samples =
> +            curbuf->audio->nb_samples;
> +        curbuf = aconvert->out_samplesref;
> +    }
> +

> +    /* Handle generic planar stereo downmixing */
> +    if (!aconvert->convert_chlayout && !aconvert->audioconvert_ctx &&
> +        outlink->channel_layout == AV_CH_LAYOUT_STEREO) {
> +        int size =
> +          av_get_bytes_per_sample(inlink->format) * curbuf->audio->nb_samples;
> +        if (nb_channels == 1) curbuf->data[1] = curbuf->data[0];
> +        memcpy(aconvert->out_samplesref->data[0],curbuf->data[0], size);
> +        memcpy(aconvert->out_samplesref->data[1], curbuf->data[1], size);
> +        aconvert->out_samplesref->audio->nb_samples =
> +            curbuf->audio->nb_samples;
> +        curbuf = aconvert->out_samplesref;
> +    }

what's this exactly doing?

> +
> +    avfilter_filter_samples(inlink->dst->outputs[0],
> +                            avfilter_ref_buffer(curbuf, ~0));
> +    avfilter_unref_buffer(insamplesref);
> +}
-- 
FFmpeg = Formidable & Fundamental Mastodontic Peaceless Empowered Gnome


More information about the ffmpeg-devel mailing list