]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/resample.c
moved the tables into header files (and applied the 'static' patch). Nick: why do...
[ffmpeg] / libavcodec / resample.c
index 78b4ad812667e7f91b16447d460a6a3cf4e87329..86bed847c4b6ec78a7a9d4b43467deff2fac1609 100644 (file)
@@ -1,28 +1,34 @@
 /*
  * 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
  */
+
+/**
+ * @file resample.c
+ * Sample rate convertion for both audio and video.
+ */
+
 #include "avcodec.h"
-#include <math.h>
+#include "os_support.h"
 
 typedef struct {
     /* fractional resampling */
-    UINT32 incr; /* fractional increment */
-    UINT32 frac;
+    uint32_t incr; /* fractional increment */
+    uint32_t frac;
     int last_sample;
     /* integer down sample */
     int iratio;  /* integer divison ratio */
@@ -44,7 +50,7 @@ struct ReSampleContext {
 static void init_mono_resample(ReSampleChannelContext *s, float ratio)
 {
     ratio = 1.0 / ratio;
-    s->iratio = (int)floor(ratio);
+    s->iratio = (int)floorf(ratio);
     if (s->iratio == 0)
         s->iratio = 1;
     s->incr = (int)((ratio / s->iratio) * FRAC);
@@ -76,9 +82,9 @@ static int fractional_resample(ReSampleChannelContext *s, short *output, short *
         *q++ = (l0 * (FRAC - frac) + l1 * frac) >> FRAC_BITS;
         frac = frac + s->incr;
         while (frac >= FRAC) {
+            frac -= FRAC;
             if (p >= pend)
                 goto the_end;
-            frac -= FRAC;
             l0 = l1;
             l1 = *p++;
         }
@@ -193,7 +199,7 @@ static int mono_resample(ReSampleChannelContext *s, short *output, short *input,
     short *buf1;
     short *buftmp;
 
-    buf1= (short*) malloc( nb_samples * sizeof(short) );
+    buf1= (short*)av_malloc( nb_samples * sizeof(short) );
 
     /* first downsample by an integer factor with averaging filter */
     if (s->iratio > 1) {
@@ -209,7 +215,7 @@ static int mono_resample(ReSampleChannelContext *s, short *output, short *input,
     } else {
         memcpy(output, buftmp, nb_samples * sizeof(short));
     }
-    free(buf1);
+    av_free(buf1);
     return nb_samples;
 }
 
@@ -260,13 +266,13 @@ int audio_resample(ReSampleContext *s, short *output, short *input, int nb_sampl
     }
 
     /* XXX: move those malloc to resample init code */
-    bufin[0]= (short*) malloc( nb_samples * sizeof(short) );
-    bufin[1]= (short*) malloc( nb_samples * sizeof(short) );
+    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*) malloc( lenout * sizeof(short) );
-    bufout[1]= (short*) malloc( lenout * sizeof(short) );
+    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) {
@@ -299,15 +305,15 @@ int audio_resample(ReSampleContext *s, short *output, short *input, int nb_sampl
         stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
     }
 
-    free(bufin[0]);
-    free(bufin[1]);
+    av_free(bufin[0]);
+    av_free(bufin[1]);
 
-    free(bufout[0]);
-    free(bufout[1]);
+    av_free(bufout[0]);
+    av_free(bufout[1]);
     return nb_samples1;
 }
 
 void audio_resample_close(ReSampleContext *s)
 {
-    free(s);
+    av_free(s);
 }