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

Axel Holzinger aholzinger
Wed Oct 31 16:38:57 CET 2007


Benoit Fouet wrote:
> 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 ?

It's ints, what is the difference?

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

The correct way to handle this is to follow the ITU matrix:

Lo = L + 0.7 * C + k * Ls
Ro = R + 0.7 * C + k * Rs with default k = 0.7

You see: No division! Instead you have to implement clipping.

Why no division?

Imagine, you have a 5.1 signal, but only L + R hold any signal. If you
divide, the stereo level will be decreased and you will loose dynamic.

> > +{
> > +    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
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at mplayerhq.hu
> http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-devel

Cheers
Axel





More information about the ffmpeg-devel mailing list