[FFmpeg-devel] [PATCH] Add af_aconvert - sample fmt and channel layout conversion filter

Michael Niedermayer michaelni
Sat Jan 15 02:37:50 CET 2011


On Fri, Jan 14, 2011 at 10:08:53PM +0100, Stefano Sabatini wrote:
> On date Thursday 2011-01-13 03:27:40 +0100, Michael Niedermayer encoded:
> > On Wed, Jan 12, 2011 at 10:25:26PM +0100, Stefano Sabatini wrote:
> [...]
> > > Updated against latest SVN. Consider this a work in progress, many of
> > > the fixme need yet to be fixed, Michael please mark what you consider
> > > required for the patch to be applied.
> > 
> > the short types must be fixed IMHO
> 
> short -> int16_t
>  
> > and its (left+right)>>1 not (left>>1)+(right>>1); (trivial)
> 
> fixed
>  
> > The rest from my previous review can wait until after svn
> > 
> > Theres also another issue that i think must be fixed and that is the naming
> > There are 3 things
> > 
> > 1. Changing the sample rate
> > 2. Changing the sample format (int16<->int8<->float)
> > 3. Changing the channel layout (stereo<->mono<->5.1)
> > 
> > 1. is AFIAK called resampling
> > 2. is AFIAK called requantization or sample format convertion
> > 3. is AFIAK called rematrixing or up/down mixing
> 
> I changed the name of the filter resample -> aconvert and the variable
> names accordingly.
> 
> > If someone has some authorative (_NOT_ wikipedia) source that define these
> > terms that cant hurt ...
> -- 
> FFmpeg = Fast & Fancy Most Ponderous Elfic Game

>  Makefile      |    1 
>  af_aconvert.c |  465 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  allfilters.c  |    1 
>  3 files changed, 467 insertions(+)
> b3141782ec3f63f4c68edacf0a0316b9447fd2b0  0002-Add-af_aconvert-sample-fmt-and-channel-layout-conver.patch
> From 9b59d916568a885c5b2bceb4c61438a9ae1e7210 Mon Sep 17 00:00:00 2001
> From: Stefano Sabatini <stefano.sabatini-lala at poste.it>
> Date: Fri, 1 Oct 2010 14:58:22 +0200
> Subject: [PATCH] Add af_aconvert - sample fmt and channel layout conversion filter.
> 
> Based on a patch by "S.N. Hemanth Meenakshisundaram" 5m33nak5 at uc5d.3du.
> ---
>  libavfilter/Makefile      |    1 +
>  libavfilter/af_aconvert.c |  465 +++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/allfilters.c  |    1 +
>  3 files changed, 467 insertions(+), 0 deletions(-)
>  create mode 100644 libavfilter/af_aconvert.c
> 
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index fdb181e..ad10bbd 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -13,6 +13,7 @@ OBJS = allfilters.o                                                     \
>         formats.o                                                        \
>         graphparser.o                                                    \
>  
> +OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
>  OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
>  
>  OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
> diff --git a/libavfilter/af_aconvert.c b/libavfilter/af_aconvert.c
> new file mode 100644
> index 0000000..10e734a
> --- /dev/null
> +++ b/libavfilter/af_aconvert.c
> @@ -0,0 +1,465 @@
> +/*
> + * Copyright (C) 2010 S.N. Hemanth Meenakshisundaram <smeenaks at ucsd.edu>
> + *
> + * 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 Neidermayer
> + */
> +
> +#include "avfilter.h"
> +#include "libavcodec/audioconvert.h"
> +
> +typedef struct {
> +    int reconfig_channel_layout;            ///< flag set when channel layout of incoming buffer changes
> +    int reconfig_sample_fmt;                ///< flag set when sample format of incoming buffer changes
> +
> +    enum AVSampleFormat  in_sample_fmt;     ///< default incoming sample format expected
> +    enum AVSampleFormat out_sample_fmt;     ///< output sample format
> +    int64_t  in_channel_layout;             ///< default incoming channel layout expected
> +    int64_t out_channel_layout;             ///< output channel layout
> +
> +    int in_nb_samples;                      ///< stores number of samples in previous incoming buffer
> +    AVFilterBufferRef *s16_samples;         ///< stores temporary audio data in s16 sample format for channel layout conversions
> +    AVFilterBufferRef *s16_samples_ptr;     ///< duplicate pointer to audio data in s16 sample format
> +    AVFilterBufferRef *s16_mid_samples;     ///< stores temporary audio data in s16 sample format after channel layout conversions
> +    AVFilterBufferRef *s16_mid_samples_ptr; ///< duplicate pointer to audio data after channel layout conversions
> +    AVFilterBufferRef *out_samples;         ///< stores audio data after required sample format and channel layout conversions
> +    AVFilterBufferRef *out_samples_ptr;     ///< duplicate pointer to audio data after required conversions
> +
> +    AVAudioConvert *convert_to_s16_ctx;     ///< audio convert context for conversion to s16 sample format
> +    AVAudioConvert *convert_to_out_ctx;     ///< audio convert context for conversion to output sample format
> +
> +    /**
> +     * channel conversion routine, point to one of the routines below
> +     */
> +    void (*channel_conversion) (uint8_t *out[], uint8_t *in[], int , int);
> +} ConvertContext;
> +
> +/**
> + * All of the routines below are for packed audio data. SDL accepts packed data
> + * only and current ffplay also assumes packed data only at all times.
> + */
> +
> +/* Optimized stereo to mono and mono to stereo routines - common case */
> +static void stereo_to_mono(uint8_t *out[], uint8_t *in[], int nb_samples, int in_channels)
> +{
> +    uint16_t *input  = (uint16_t *) in[0];
> +    uint16_t *output = (uint16_t *) out[0];

most of these should probably be int16_t not uint16_t

[...]
> +/**
> + * This is for when we have more than 2 input channels, need to downmix to mono
> + * and do not have a conversion formula available.  We just use first two input
> + * channels - left and right. This is a placeholder until more conversion
> + * functions are written.
> + */
> +static void mono_downmix(uint8_t *out[], uint8_t *in[], int nb_samples, int in_channels)
> +{
> +    int i;

> +    uint16_t *input  = (short *) in[0];

this is definitly wrong


> +    uint16_t *output = (uint16_t *) out[0];

> +    uint16_t left, right;

int


> +
> +    for (i = 0; i < nb_samples; i++) {
> +        left = *input++;
> +        right = *input++;
> +        *output++ = (left+right)>>1;
> +        input += in_channels-2;
> +    }
> +}
> +
> +/* Stereo to 5.1 output */
> +static void ac3_5p1_mux(uint8_t *out[], uint8_t *in[], int nb_samples, int in_channels)
> +{
> +    int i;
> +    uint16_t *output = (uint16_t *) out[0];
> +    uint16_t *input = (uint16_t *) in[0];

> +    uint16_t left, right;

int


[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Its not that you shouldnt use gotos but rather that you should write
readable code and code with gotos often but not always is less readable
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20110115/af31a383/attachment.pgp>



More information about the ffmpeg-devel mailing list