[FFmpeg-devel] [PATCH] Support for reducing from 5.1 and 4.0 audio to stereo audio

Benoit Fouet benoit.fouet
Wed Oct 31 16:09:03 CET 2007


Hi,

Jonathan Wilson wrote:
> Attached patch adds support for reducing from 5.1 and 4.0 audio to
> stereo audio.
> It's based on the patch from Peter Ross.
> ------------------------------------------------------------------------
>
> Index: libavcodec/resample.c
> ===================================================================
> --- libavcodec/resample.c	(revision 10885)
> +++ libavcodec/resample.c	(working copy)
> @@ -62,6 +62,35 @@
>      }
>  }
>  
> +static void quad_to_stereo(short *output, short *input, int n)
> +{
> +    int i;
> +    for(i=0; i<n; i++) {
> +        output[0] = (input[0] + input[2]) >> 1;
> +        output[1] = (input[1] + input[3]) >> 1;
>   

shouldn't it be /2 instead of >>1 ?

> +        output += 2;
> +        input += 4;
> +    }
> +}
> +
> +
> +static void ac3_5p1_to_stereo(short *output, short *input, int n1)
> +{
> +    short *p, *q;
> +    int n = n1;
> +
>   

n is useless, why not having a for loop as in the first function ?

> +    p = input;
> +    q = output;
> +
> +    while(n > 0) {
> +        q[0] = (p[0] + p[1] + p[3] + p[5]) / 3;
> +        q[1] = (p[1] + p[2] + p[4] + p[5]) / 3;
> +        q+= 2;
> +        p+= 6;
> +        n--;
> +    }
> +}
> +
>  /* n1: number of samples */
>  static void mono_to_stereo(short *output, short *input, int n1)
>  {
> @@ -131,7 +160,7 @@
>  {
>      ReSampleContext *s;
>  
> -    if ( input_channels > 2)
> +    if ( input_channels > 2 && output_rate != input_rate)
>        {
>          av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.");
>          return NULL;
> @@ -177,6 +206,16 @@
>      short *buftmp2[2], *buftmp3[2];
>      int lenout;
>  
> +    if (s->input_channels == 4 && s->output_channels == 2) {
> +        quad_to_stereo(output, input, nb_samples);
> +        return nb_samples;
> +    }
> +
> +    if (s->input_channels == 6 && s->output_channels == 2) {
> +        ac3_5p1_to_stereo(output, input, nb_samples);
> +        return nb_samples;
> +    }
> +
>      if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
>          /* nothing to do */
>          memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
>   

-- 
Ben
Purple Labs S.A.
www.purplelabs.com




More information about the ffmpeg-devel mailing list