]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/resample.c
use #ifdef
[ffmpeg] / libavcodec / resample.c
index 29445964f61816959ebabf685e42bb29822b0d4c..ad857e220c4d2407f458c31f5ee3a7b7ecf1c9b4 100644 (file)
@@ -1,30 +1,23 @@
 /*
  * Sample rate convertion for both audio and video
- * Copyright (c) 2000 Gerard Lantau.
+ * Copyright (c) 2000 Fabrice Bellard.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library 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 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library 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 General Public License for more details.
+ * 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 General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <math.h>
 #include "avcodec.h"
 
-#define NDEBUG
-#include <assert.h>
-
 typedef struct {
     /* fractional resampling */
     UINT32 incr; /* fractional increment */
@@ -54,7 +47,7 @@ static void init_mono_resample(ReSampleChannelContext *s, float ratio)
     if (s->iratio == 0)
         s->iratio = 1;
     s->incr = (int)((ratio / s->iratio) * FRAC);
-    s->frac = 0;
+    s->frac = FRAC;
     s->last_sample = 0;
     s->icount = s->iratio;
     s->isum = 0;
@@ -196,9 +189,11 @@ static void stereo_mux(short *output, short *input1, short *input2, int n)
 
 static int mono_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
 {
-    short buf1[nb_samples];
+    short *buf1;
     short *buftmp;
 
+    buf1= (short*)av_malloc( nb_samples * sizeof(short) );
+
     /* first downsample by an integer factor with averaging filter */
     if (s->iratio > 1) {
         buftmp = buf1;
@@ -213,6 +208,7 @@ static int mono_resample(ReSampleChannelContext *s, short *output, short *input,
     } else {
         memcpy(output, buftmp, nb_samples * sizeof(short));
     }
+    av_free(buf1);
     return nb_samples;
 }
 
@@ -251,9 +247,10 @@ ReSampleContext *audio_resample_init(int output_channels, int input_channels,
 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
 {
     int i, nb_samples1;
-    short bufin[2][nb_samples];
-    short bufout[2][(int)(nb_samples * s->ratio) + 16]; /* make some zoom to avoid round pb */
+    short *bufin[2];
+    short *bufout[2];
     short *buftmp2[2], *buftmp3[2];
+    int lenout;
 
     if (s->input_channels == s->output_channels && s->ratio == 1.0) {
         /* nothing to do */
@@ -261,6 +258,15 @@ int audio_resample(ReSampleContext *s, short *output, short *input, int nb_sampl
         return nb_samples;
     }
 
+    /* XXX: move those malloc to resample init code */
+    bufin[0]= (short*) av_malloc( nb_samples * sizeof(short) );
+    bufin[1]= (short*) av_malloc( nb_samples * sizeof(short) );
+    
+    /* make some zoom to avoid round pb */
+    lenout= (int)(nb_samples * s->ratio) + 16;
+    bufout[0]= (short*) av_malloc( lenout * sizeof(short) );
+    bufout[1]= (short*) av_malloc( lenout * sizeof(short) );
+
     if (s->input_channels == 2 &&
         s->output_channels == 1) {
         buftmp2[0] = bufin[0];
@@ -292,10 +298,15 @@ int audio_resample(ReSampleContext *s, short *output, short *input, int nb_sampl
         stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
     }
 
+    av_free(bufin[0]);
+    av_free(bufin[1]);
+
+    av_free(bufout[0]);
+    av_free(bufout[1]);
     return nb_samples1;
 }
 
 void audio_resample_close(ReSampleContext *s)
 {
-    free(s);
+    av_free(s);
 }