[Asterisk-cvs] asterisk-addons/format_mp3 decode_ntom.c, NONE, 1.1 Makefile, 1.1, 1.2 README, 1.1, 1.2 common.c, 1.1, 1.2 dct64_i386.c, 1.1, 1.2 format_mp3.c, 1.1, 1.2 interface.c, 1.1, 1.2 layer3.c, 1.1, 1.2 mpg123.h, 1.1, 1.2 mpglib.h, 1.1, 1.2 tabinit.c, 1.1, 1.2

anthm at lists.digium.com anthm at lists.digium.com
Wed Sep 8 12:15:13 CDT 2004


Update of /usr/cvsroot/asterisk-addons/format_mp3
In directory mongoose.digium.com:/tmp/cvs-serv1055

Modified Files:
	Makefile README common.c dct64_i386.c format_mp3.c interface.c 
	layer3.c mpg123.h mpglib.h tabinit.c 
Added Files:
	decode_ntom.c 
Log Message:
Add changes by Chris Stenton to support stereo and non-8khz mp3

--- NEW FILE: decode_ntom.c ---
/* 
 * Mpeg Layer-1,2,3 audio decoder 
 * ------------------------------
 * copyright (c) 1995,1996,1997 by Michael Hipp, All rights reserved.
 * See also 'README'
 *
 * N->M down/up sampling. Not optimized for speed.
 */

#include <asterisk/logger.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "mpg123.h"
#include "mpglib.h"

#define WRITE_SAMPLE(samples,sum,clip) \
  if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
  else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
  else { *(samples) = sum; }

#define NTOM_MUL (32768)
static unsigned long ntom_val[2] = { NTOM_MUL>>1,NTOM_MUL>>1 };
static unsigned long ntom_step = NTOM_MUL;


int synth_ntom_set_step(long m,long n)
{
	if(param.verbose > 1)
		ast_log(LOG_WARNING,"Init rate converter: %ld->%ld\n",m,n);

	if(n >= 96000 || m >= 96000 || m == 0 || n == 0) {
		ast_log(LOG_WARNING,"NtoM converter: illegal rates\n");
		return (1);
	}

	n *= NTOM_MUL;
	ntom_step = n / m;

	if(ntom_step > 8*NTOM_MUL) {
		ast_log(LOG_WARNING,"max. 1:8 conversion allowed!\n");
		return (1);
	}

	ntom_val[0] = ntom_val[1] = NTOM_MUL>>1;

	return (0);
	
}


int synth_ntom_mono (struct mpstr *mp, real *bandPtr,unsigned char *samples,int *pnt)
{
  short samples_tmp[8*64];
  short *tmp1 = samples_tmp;
  int i,ret;
  int pnt1 = 0;

  ret = synth_ntom(mp, bandPtr,0,(unsigned char *) samples_tmp,&pnt1);
  samples += *pnt;

  for(i=0;i<(pnt1>>2);i++) {
    *( (short *)samples) = *tmp1;
    samples += 2;
    tmp1 += 2;
  }
  *pnt += pnt1 >> 1;

  return ret;
}



int synth_ntom(struct mpstr *mp, real *bandPtr,int channel,unsigned char *out,int *pnt)
{
  static const int step = 2;
  int bo;
  short *samples = (short *) (out + *pnt);

  real *b0,(*buf)[0x110];
  int clip = 0; 
  int bo1;
  int ntom;

  bo = mp->synth_bo;
  
  if(!channel) {
    bo--;
    bo &= 0xf;
    buf = mp->synth_buffs[0];
    ntom = ntom_val[1] = ntom_val[0];
  }
  else {
    samples++;
    out += 2; /* to compute the right *pnt value */
    buf = mp->synth_buffs[1];
    ntom = ntom_val[1];
  }

  if(bo & 0x1) {
    b0 = buf[0];
    bo1 = bo;
    dct64(buf[1]+((bo+1)&0xf),buf[0]+bo,bandPtr);
  }
  else {
    b0 = buf[1];
    bo1 = bo+1;
    dct64(buf[0]+bo,buf[1]+bo+1,bandPtr);
  }

  mp->synth_bo = bo;
  
  {
    register int j;
    real *window = (mp->decwin) + 16 - bo1;
 
    for (j=16;j;j--,window+=0x10)
    {
      real sum;

      ntom += ntom_step;
      if(ntom < NTOM_MUL) {
        window += 16;
        b0 += 16;
        continue;
      }

      sum  = *window++ * *b0++;
      sum -= *window++ * *b0++;
      sum += *window++ * *b0++;
      sum -= *window++ * *b0++;
      sum += *window++ * *b0++;
      sum -= *window++ * *b0++;
      sum += *window++ * *b0++;
      sum -= *window++ * *b0++;
      sum += *window++ * *b0++;
      sum -= *window++ * *b0++;
      sum += *window++ * *b0++;
      sum -= *window++ * *b0++;
      sum += *window++ * *b0++;
      sum -= *window++ * *b0++;
      sum += *window++ * *b0++;
      sum -= *window++ * *b0++;

      while(ntom >= NTOM_MUL) {
        WRITE_SAMPLE(samples,sum,clip);
        samples += step;
        ntom -= NTOM_MUL;
      }
    }

    ntom += ntom_step;
    if(ntom >= NTOM_MUL)
    {
      real sum;
      sum  = window[0x0] * b0[0x0];
      sum += window[0x2] * b0[0x2];
      sum += window[0x4] * b0[0x4];
      sum += window[0x6] * b0[0x6];
      sum += window[0x8] * b0[0x8];
      sum += window[0xA] * b0[0xA];
      sum += window[0xC] * b0[0xC];
      sum += window[0xE] * b0[0xE];

      while(ntom >= NTOM_MUL) {
        WRITE_SAMPLE(samples,sum,clip);
        samples += step;
        ntom -= NTOM_MUL;
      }
    }

    b0-=0x10,window-=0x20;
    window += bo1<<1;

    for (j=15;j;j--,b0-=0x20,window-=0x10)
    {
      real sum;

      ntom += ntom_step;
      if(ntom < NTOM_MUL) {
        window -= 16;
        b0 += 16;
        continue;
      }

      sum = -*(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;
      sum -= *(--window) * *b0++;

      while(ntom >= NTOM_MUL) {
        WRITE_SAMPLE(samples,sum,clip);
        samples += step;
        ntom -= NTOM_MUL;
      }
    }
  }

  ntom_val[channel] = ntom;
  *pnt = ((unsigned char *) samples - out);

  return clip;
}



Index: Makefile
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/format_mp3/Makefile,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Makefile	5 Sep 2004 18:28:03 -0000	1.1
+++ Makefile	8 Sep 2004 16:17:25 -0000	1.2
@@ -1,10 +1,71 @@
+INSTALLTO=/usr/lib/asterisk/modules
 MODS=format_mp3.so
-MP3OBJS=common.o dct64_i386.o decode_i386.o layer3.o tabinit.o interface.o format_mp3.o
+MP3OBJS=common.o dct64_i386.o decode_ntom.o layer3.o tabinit.o interface.o format_mp3.o
+
+CFLAGS=-pipe  -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations $(DEBUG) $(INCLUDE) -D_REENTRANT -D_GNU_SOURCE #-DMAKE_VALGRIND_HAPPY
+
+OSARCH=$(shell uname -s)
+
+ifeq (${OSARCH},Linux)
+PROC=$(shell uname -m)
+ifeq ($(PROC),x86_64)
+# You must have GCC 3.4 to use k8, otherwise use athlon
+PROC=k8
+#PROC=athlon
+OPTIONS+=-m64
+endif
+ifeq ($(PROC),sparc64)
+#The problem with sparc is the best stuff is in newer versions of gcc (post 3.0) only.
+#This works for even old (2.96) versions of gcc and provides a small boost either way.
+#A ultrasparc cpu is really v9 but the stock debian stable 3.0 gcc doesn't support it.
+#So we go lowest common available by gcc and go a step down, still a step up from
+#the default as we now have a better instruction set to work with. - Belgarath
+PROC=ultrasparc
+OPTIONS+=$(shell if $(CC) -mtune=$(PROC) -S -o /dev/null -xc /dev/null >/dev/null 2>&1; then echo "-mtune=$(PROC)"; fi)
+OPTIONS+=$(shell if $(CC) -mcpu=v8 -S -o /dev/null -xc /dev/null >/dev/null 2>&1; then echo "-mcpu=v8"; fi)
+OPTIONS+=-fomit-frame-pointer
+endif
+
+endif
+
+ifeq (${OSARCH},FreeBSD)
+OSVERSION=$(shell make -V OSVERSION -f /usr/share/mk/bsd.port.subdir.mk)
+CFLAGS+=$(if ${OSVERSION}<500016,-D_THREAD_SAFE)
+LIBS+=$(if ${OSVERSION}<502102,-lc_r,-pthread)
+INCLUDE+=-I/usr/local/include
+CFLAGS+=$(shell if [ -d /usr/local/include/spandsp ]; then echo "-I/usr/local/include/spandsp"; fi)
+endif # FreeBSD
+
+ifeq (${OSARCH},NetBSD)
+CFLAGS+=-pthread
+INCLUDE+=-I/usr/local/include
+endif
+
+ifeq (${OSARCH},OpenBSD)
+CFLAGS+=-pthread
+endif
+
+#Tell gcc to optimize the asterisk's code
+OPTIMIZE+=-O6
+
+CFLAGS+=$(OPTIMIZE)
+CFLAGS+= $(OPTIONS)
+
+CC=gcc
+
 
 format_mp3.so: $(MP3OBJS)
-	$(CC) -shared -Xlinker -x -o $@ $(MP3OBJS)
+	$(CC) $(CFLAGS) -shared -Xlinker -x -o $@ $(MP3OBJS)
 
 all: $(MODS)
 
 clean:
 	rm -f *.o *.so *~
+
+install: all
+	/bin/cp -f format_mp3.so $(INSTALLTO)
+
+autoload: all
+	asterisk -rx "unload format_mp3.so"
+	/bin/cp -f format_mp3.so $(INSTALLTO)
+	asterisk -rx "load format_mp3.so"

Index: README
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/format_mp3/README,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- README	5 Sep 2004 18:28:03 -0000	1.1
+++ README	8 Sep 2004 16:17:25 -0000	1.2
@@ -1,18 +1,24 @@
-This is a module for asterisk to play mp3 natively
-They *MUST* be already at 8khz and *MUST* be mono.
+This is a module for asterisk to play mp3 natively.
+They *SHOULD* be already at 8khz and *SHOULD* be mono.
+otherwise they will be consuming CPU alot more than need be.
 
+Convert them to 8k mono like this:
 lame -q -p --mp3input -a --preset 8 in.mp3 8kout.mp3
 
-This probably sucks still but it's a start.
-
 just run 
-#make clean install 
-to intsall 
+# make clean install 
+to install 
 
 or
-#make clean autoload
-
+# make clean autoload
 to install and autoload at the same time
 
 
+Comments or improvements
+Anthony Minessale <anthmct at yahoo.com>
+
+Donations Welcomed at paypal:jillkm3 at yahoo.com
+
+
+
 

Index: common.c
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/format_mp3/common.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- common.c	5 Sep 2004 18:28:03 -0000	1.1
+++ common.c	8 Sep 2004 16:17:25 -0000	1.2
@@ -1,3 +1,5 @@
+#include <asterisk/logger.h>
+
 #include <ctype.h>
 #include <stdlib.h>
 #include <signal.h>
@@ -6,6 +8,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
+#include "mpg123.h"
 #include "mpglib.h"
 
 struct parameter param = { 1 , 1 , 0 , 0 };
@@ -24,11 +27,6 @@
                   22050, 24000, 16000 ,
                   11025 , 12000 , 8000 };
 
-int bitindex;
-unsigned char *wordpointer;
-unsigned char *pcm_sample;
-int pcm_point = 0;
-
 
 #if 0
 static void get_II_stuff(struct frame *fr)
@@ -92,8 +90,8 @@
     
     fr->lay = 4-((newhead>>17)&3);
     if( ((newhead>>10)&0x3) == 0x3) {
-      ast_log(LOG_ERROR,"Stream error\n");
-	  return(0);
+      ast_log(LOG_WARNING,"Stream error\n");
+      return (0);
     }
     if(fr->mpeg25) {
       fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
@@ -118,7 +116,7 @@
 
     if(!fr->bitrate_index)
     {
-      ast_log(LOG_ERROR,"Free format not supported.\n");
+      ast_log(LOG_WARNING,"Free format not supported.\n");
       return (0);
     }
 
@@ -133,7 +131,7 @@
         fr->framesize /= freqs[fr->sampling_frequency];
         fr->framesize  = ((fr->framesize+fr->padding)<<2)-4;
 #else
-        ast_log(LOG_ERROR,"Not supported!\n");
+        ast_log(LOG_WARNING,"Layer 1 not supported!\n");
 #endif
         break;
       case 2:
@@ -146,7 +144,7 @@
         fr->framesize /= freqs[fr->sampling_frequency];
         fr->framesize += fr->padding - 4;
 #else
-        ast_log(LOG_ERROR,"Not supported!\n");
+        ast_log(LOG_WARNING,"Layer 2 not supported!\n");
 #endif
         break;
       case 3:
@@ -167,7 +165,7 @@
           fr->framesize = fr->framesize + fr->padding - 4;
         break; 
       default:
-        ast_log(LOG_ERROR,"Sorry, unknown layer type.\n"); 
+        ast_log(LOG_WARNING,"Sorry, unknown layer type.\n"); 
         return (0);
     }
     return 1;
@@ -179,15 +177,15 @@
 	static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
 	static char *layers[4] = { "Unknown" , "I", "II", "III" };
 
-	ast_log(LOG_ERROR,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n", 
+	ast_log(LOG_WARNING,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n", 
 		fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
 		layers[fr->lay],freqs[fr->sampling_frequency],
 		modes[fr->mode],fr->mode_ext,fr->framesize+4);
-	ast_log(LOG_ERROR,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
+	ast_log(LOG_WARNING,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
 		fr->stereo,fr->copyright?"Yes":"No",
 		fr->original?"Yes":"No",fr->error_protection?"Yes":"No",
 		fr->emphasis);
-	ast_log(LOG_ERROR,"Bitrate: %d Kbits/s, Extension value: %d\n",
+	ast_log(LOG_WARNING,"Bitrate: %d Kbits/s, Extension value: %d\n",
 		tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension);
 }
 
@@ -196,7 +194,7 @@
 	static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
 	static char *layers[4] = { "Unknown" , "I", "II", "III" };
  
-	ast_log(LOG_ERROR,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
+	ast_log(LOG_WARNING,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
 		fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
 		layers[fr->lay],
 		tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
@@ -205,7 +203,7 @@
 
 #endif
 
-unsigned int getbits(int number_of_bits)
+unsigned int getbits(struct mpstr *mp, int number_of_bits)
 {
   unsigned long rval;
 
@@ -213,52 +211,53 @@
     return 0;
 
   {
-    rval = wordpointer[0];
+    rval = (mp->worksample).wordpointer[0];
     rval <<= 8;
-    rval |= wordpointer[1];
+    rval |= (mp->worksample).wordpointer[1];
     rval <<= 8;
-    rval |= wordpointer[2];
-    rval <<= bitindex;
+    rval |= (mp->worksample).wordpointer[2];
+    rval <<= (mp->worksample).bitindex;
     rval &= 0xffffff;
 
-    bitindex += number_of_bits;
+    (mp->worksample).bitindex += number_of_bits;
 
     rval >>= (24-number_of_bits);
 
-    wordpointer += (bitindex>>3);
-    bitindex &= 7;
+    (mp->worksample).wordpointer += ((mp->worksample).bitindex>>3);
+    (mp->worksample).bitindex &= 7;
   }
   return rval;
 }
 
-unsigned int getbits_fast(int number_of_bits)
+unsigned int getbits_fast(struct mpstr *mp, int number_of_bits)
 {
   unsigned long rval;
 
   {
-    rval = wordpointer[0];
+    rval = (mp->worksample).wordpointer[0];
     rval <<= 8;	
-    rval |= wordpointer[1];
-    rval <<= bitindex;
+    rval |= (mp->worksample).wordpointer[1];
+    rval <<= (mp->worksample).bitindex;
     rval &= 0xffff;
-    bitindex += number_of_bits;
+    (mp->worksample).bitindex += number_of_bits;
 
     rval >>= (16-number_of_bits);
 
-    wordpointer += (bitindex>>3);
-    bitindex &= 7;
+    (mp->worksample).wordpointer += ((mp->worksample).bitindex>>3);
+    (mp->worksample).bitindex &= 7;
   }
   return rval;
 }
 
-unsigned int get1bit(void)
+unsigned int get1bit(struct mpstr *mp)
 {
   unsigned char rval;
-  rval = *wordpointer << bitindex;
 
-  bitindex++;
-  wordpointer += (bitindex>>3);
-  bitindex &= 7;
+  rval = *((mp->worksample).wordpointer) << (mp->worksample).bitindex;
+
+  (mp->worksample).bitindex++;
+  (mp->worksample).wordpointer += ((mp->worksample).bitindex>>3);
+  (mp->worksample).bitindex &= 7;
 
   return rval>>7;
 }

Index: dct64_i386.c
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/format_mp3/dct64_i386.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- dct64_i386.c	5 Sep 2004 18:28:03 -0000	1.1
+++ dct64_i386.c	8 Sep 2004 16:17:25 -0000	1.2
@@ -7,61 +7,59 @@
  * even for Intel processors.
  */
 
-#include "mpglib.h"
+#include "mpg123.h"
+
+/*
+ * the call via dct64 is a trick to force GCC to use
+ * (new) registers for the b1,b2 pointer to the bufs[xx] field
+ */
+void dct64(real *a,real *b,real *c);
 
 static void dct64_1(real *out0,real *out1,real *b1,real *b2,real *samples)
 {
-
  {
   register real *costab = pnts[0];
 
   b1[0x00] = samples[0x00] + samples[0x1F];
-  b1[0x1F] = (samples[0x00] - samples[0x1F]) * costab[0x0];
-
   b1[0x01] = samples[0x01] + samples[0x1E];
+  b1[0x1F] = (samples[0x00] - samples[0x1F]) * costab[0x0];
   b1[0x1E] = (samples[0x01] - samples[0x1E]) * costab[0x1];
 
   b1[0x02] = samples[0x02] + samples[0x1D];
-  b1[0x1D] = (samples[0x02] - samples[0x1D]) * costab[0x2];
-
   b1[0x03] = samples[0x03] + samples[0x1C];
+  b1[0x1D] = (samples[0x02] - samples[0x1D]) * costab[0x2];
   b1[0x1C] = (samples[0x03] - samples[0x1C]) * costab[0x3];
 
   b1[0x04] = samples[0x04] + samples[0x1B];
-  b1[0x1B] = (samples[0x04] - samples[0x1B]) * costab[0x4];
-
   b1[0x05] = samples[0x05] + samples[0x1A];
+  b1[0x1B] = (samples[0x04] - samples[0x1B]) * costab[0x4];
   b1[0x1A] = (samples[0x05] - samples[0x1A]) * costab[0x5];
 
   b1[0x06] = samples[0x06] + samples[0x19];
-  b1[0x19] = (samples[0x06] - samples[0x19]) * costab[0x6];
-
   b1[0x07] = samples[0x07] + samples[0x18];
+  b1[0x19] = (samples[0x06] - samples[0x19]) * costab[0x6];
   b1[0x18] = (samples[0x07] - samples[0x18]) * costab[0x7];
 
   b1[0x08] = samples[0x08] + samples[0x17];
-  b1[0x17] = (samples[0x08] - samples[0x17]) * costab[0x8];
-
   b1[0x09] = samples[0x09] + samples[0x16];
+  b1[0x17] = (samples[0x08] - samples[0x17]) * costab[0x8];
   b1[0x16] = (samples[0x09] - samples[0x16]) * costab[0x9];
 
   b1[0x0A] = samples[0x0A] + samples[0x15];
-  b1[0x15] = (samples[0x0A] - samples[0x15]) * costab[0xA];
-
   b1[0x0B] = samples[0x0B] + samples[0x14];
+  b1[0x15] = (samples[0x0A] - samples[0x15]) * costab[0xA];
   b1[0x14] = (samples[0x0B] - samples[0x14]) * costab[0xB];
 
   b1[0x0C] = samples[0x0C] + samples[0x13];
-  b1[0x13] = (samples[0x0C] - samples[0x13]) * costab[0xC];
-
   b1[0x0D] = samples[0x0D] + samples[0x12];
+  b1[0x13] = (samples[0x0C] - samples[0x13]) * costab[0xC];
   b1[0x12] = (samples[0x0D] - samples[0x12]) * costab[0xD];
 
   b1[0x0E] = samples[0x0E] + samples[0x11];
-  b1[0x11] = (samples[0x0E] - samples[0x11]) * costab[0xE];
-
   b1[0x0F] = samples[0x0F] + samples[0x10];
+  b1[0x11] = (samples[0x0E] - samples[0x11]) * costab[0xE];
   b1[0x10] = (samples[0x0F] - samples[0x10]) * costab[0xF];
+
  }
 
 
@@ -69,37 +67,45 @@
   register real *costab = pnts[1];
 
   b2[0x00] = b1[0x00] + b1[0x0F]; 
-  b2[0x0F] = (b1[0x00] - b1[0x0F]) * costab[0];
   b2[0x01] = b1[0x01] + b1[0x0E]; 
+  b2[0x0F] = (b1[0x00] - b1[0x0F]) * costab[0];
   b2[0x0E] = (b1[0x01] - b1[0x0E]) * costab[1];
+
   b2[0x02] = b1[0x02] + b1[0x0D]; 
-  b2[0x0D] = (b1[0x02] - b1[0x0D]) * costab[2];
   b2[0x03] = b1[0x03] + b1[0x0C]; 
+  b2[0x0D] = (b1[0x02] - b1[0x0D]) * costab[2];
   b2[0x0C] = (b1[0x03] - b1[0x0C]) * costab[3];
+
   b2[0x04] = b1[0x04] + b1[0x0B]; 
-  b2[0x0B] = (b1[0x04] - b1[0x0B]) * costab[4];
   b2[0x05] = b1[0x05] + b1[0x0A]; 
+  b2[0x0B] = (b1[0x04] - b1[0x0B]) * costab[4];
   b2[0x0A] = (b1[0x05] - b1[0x0A]) * costab[5];
+
   b2[0x06] = b1[0x06] + b1[0x09]; 
-  b2[0x09] = (b1[0x06] - b1[0x09]) * costab[6];
   b2[0x07] = b1[0x07] + b1[0x08]; 
+  b2[0x09] = (b1[0x06] - b1[0x09]) * costab[6];
   b2[0x08] = (b1[0x07] - b1[0x08]) * costab[7];
 
+  /* */
+
   b2[0x10] = b1[0x10] + b1[0x1F];
-  b2[0x1F] = (b1[0x1F] - b1[0x10]) * costab[0];
   b2[0x11] = b1[0x11] + b1[0x1E];
+  b2[0x1F] = (b1[0x1F] - b1[0x10]) * costab[0];
   b2[0x1E] = (b1[0x1E] - b1[0x11]) * costab[1];
+
   b2[0x12] = b1[0x12] + b1[0x1D];
-  b2[0x1D] = (b1[0x1D] - b1[0x12]) * costab[2];
   b2[0x13] = b1[0x13] + b1[0x1C];
+  b2[0x1D] = (b1[0x1D] - b1[0x12]) * costab[2];
   b2[0x1C] = (b1[0x1C] - b1[0x13]) * costab[3];
+
   b2[0x14] = b1[0x14] + b1[0x1B];
-  b2[0x1B] = (b1[0x1B] - b1[0x14]) * costab[4];
   b2[0x15] = b1[0x15] + b1[0x1A];
+  b2[0x1B] = (b1[0x1B] - b1[0x14]) * costab[4];
   b2[0x1A] = (b1[0x1A] - b1[0x15]) * costab[5];
+
   b2[0x16] = b1[0x16] + b1[0x19];
-  b2[0x19] = (b1[0x19] - b1[0x16]) * costab[6];
   b2[0x17] = b1[0x17] + b1[0x18];
+  b2[0x19] = (b1[0x19] - b1[0x16]) * costab[6];
   b2[0x18] = (b1[0x18] - b1[0x17]) * costab[7];
  }
 
@@ -262,6 +268,16 @@
  out1[0x10* 8] = b1[0x03];
  out1[0x10*12] = b1[0x07];
 
+#if 1
+ out0[0x10*14] = b1[0x08] + b1[0x0C];
+ out0[0x10*10] = b1[0x0C] + b1[0x0a];
+ out0[0x10* 6] = b1[0x0A] + b1[0x0E];
+ out0[0x10* 2] = b1[0x0E] + b1[0x09];
+ out1[0x10* 2] = b1[0x09] + b1[0x0D];
+ out1[0x10* 6] = b1[0x0D] + b1[0x0B];
+ out1[0x10*10] = b1[0x0B] + b1[0x0F];
+ out1[0x10*14] = b1[0x0F];
+#else
  b1[0x08] += b1[0x0C];
  out0[0x10*14] = b1[0x08];
  b1[0x0C] += b1[0x0a];
@@ -277,30 +293,34 @@
  b1[0x0B] += b1[0x0F];
  out1[0x10*10] = b1[0x0B];
  out1[0x10*14] = b1[0x0F];
+#endif
 
- b1[0x18] += b1[0x1C];
- out0[0x10*15] = b1[0x10] + b1[0x18];
- out0[0x10*13] = b1[0x18] + b1[0x14];
- b1[0x1C] += b1[0x1a];
- out0[0x10*11] = b1[0x14] + b1[0x1C];
- out0[0x10* 9] = b1[0x1C] + b1[0x12];
- b1[0x1A] += b1[0x1E];
- out0[0x10* 7] = b1[0x12] + b1[0x1A];
- out0[0x10* 5] = b1[0x1A] + b1[0x16];
- b1[0x1E] += b1[0x19];
- out0[0x10* 3] = b1[0x16] + b1[0x1E];
- out0[0x10* 1] = b1[0x1E] + b1[0x11];
- b1[0x19] += b1[0x1D];
- out1[0x10* 1] = b1[0x11] + b1[0x19];
- out1[0x10* 3] = b1[0x19] + b1[0x15];
- b1[0x1D] += b1[0x1B];
- out1[0x10* 5] = b1[0x15] + b1[0x1D];
- out1[0x10* 7] = b1[0x1D] + b1[0x13];
- b1[0x1B] += b1[0x1F];
- out1[0x10* 9] = b1[0x13] + b1[0x1B];
- out1[0x10*11] = b1[0x1B] + b1[0x17];
+ { 
+ real tmp;
+ tmp = b1[0x18] + b1[0x1C];
+ out0[0x10*15] = tmp + b1[0x10];
+ out0[0x10*13] = tmp + b1[0x14];
+ tmp = b1[0x1C] + b1[0x1A];
+ out0[0x10*11] = tmp + b1[0x14];
+ out0[0x10* 9] = tmp + b1[0x12];
+ tmp = b1[0x1A] + b1[0x1E];
+ out0[0x10* 7] = tmp + b1[0x12];
+ out0[0x10* 5] = tmp + b1[0x16];
+ tmp = b1[0x1E] + b1[0x19];
+ out0[0x10* 3] = tmp + b1[0x16];
+ out0[0x10* 1] = tmp + b1[0x11];
+ tmp = b1[0x19] + b1[0x1D];
+ out1[0x10* 1] = tmp + b1[0x11];
+ out1[0x10* 3] = tmp + b1[0x15]; 
+ tmp = b1[0x1D] + b1[0x1B];
+ out1[0x10* 5] = tmp + b1[0x15];
+ out1[0x10* 7] = tmp + b1[0x13];
+ tmp = b1[0x1B] + b1[0x1F];
+ out1[0x10* 9] = tmp + b1[0x13];
+ out1[0x10*11] = tmp + b1[0x17];
  out1[0x10*13] = b1[0x17] + b1[0x1F];
  out1[0x10*15] = b1[0x1F];
+ }
 }
 
 /*

Index: format_mp3.c
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/format_mp3/format_mp3.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- format_mp3.c	5 Sep 2004 18:28:03 -0000	1.1
+++ format_mp3.c	8 Sep 2004 16:17:25 -0000	1.2
@@ -1,16 +1,21 @@
 /*
  * Asterisk -- A telephony toolkit for Linux.
  *
- * Work with MP3 in the proprietary Microsoft format.
- * 
- * Copyright (C) 1999, Mark Spencer
+ * format_mp3.c
+ * Anthony Minessale <anthmct at yahoo.com>
  *
+ * Derived from other asterisk sound formats by
  * Mark Spencer <markster at linux-support.net>
  *
+ * Thanks to mpglib from http://www.mpg123.org/
+ * and Chris Stenton [jacs at gnome.co.uk]
+ * for coding the ability to play stereo and non-8khz files
+ *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
  */
 
+#include "mpg123.h" 
 #include "mpglib.h" 
 #include <asterisk/lock.h>
 #include <asterisk/channel.h>
@@ -67,12 +72,13 @@
 static int glistcnt = 0;
 
 static char *name = "mp3";
-static char *desc = "MP3 format (8000hz)";
+static char *desc = "MP3 format [Any rate but 8000hz mono optimal]";
 static char *exts = "mp3";
 
 #define BLOCKSIZE 160
+#define OUTSCALE 4096
 
-#define GAIN 2		/* 2^GAIN is the multiple to increase the volume by */
+#define GAIN -4		/* 2^GAIN is the multiple to increase the volume by */
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #define htoll(b) (b)
@@ -107,7 +113,7 @@
 			free(tmp);
 			return NULL;
 		}
-		InitMP3(&tmp->mp);
+		InitMP3(&tmp->mp, OUTSCALE);
 		tmp->dbuflen = 0;
 		tmp->fd = fd;
 		tmp->fr.data = tmp->buf;
@@ -170,7 +176,7 @@
 	int res = 0, bytes = 0;
 	if(s->seek) {
 		ExitMP3(&s->mp);
-		InitMP3(&s->mp);
+		InitMP3(&s->mp, OUTSCALE);
 		lseek(s->fd, 0, SEEK_SET);
 		s->sbuflen = s->dbuflen = s->offset = 0;
 		while(s->offset < s->seek) {
@@ -312,6 +318,7 @@
 
 int load_module()
 {
+	InitMP3Constants();
 	return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
 							   mp3_open,
 							   mp3_rewrite,

Index: interface.c
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/format_mp3/interface.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- interface.c	5 Sep 2004 18:28:03 -0000	1.1
+++ interface.c	8 Sep 2004 16:17:25 -0000	1.2
@@ -1,22 +1,38 @@
+#include <asterisk/logger.h>
 
 #include <stdlib.h>
 #include <stdio.h>
+
+#include "mpg123.h"
 #include "mpglib.h"
 
-BOOL InitMP3(struct mpstr *mp) 
+
+void InitMP3Constants(void)
 {
+	init_layer3_const();
+	make_decode_tables_const();
+	
+}
+
+
+BOOL InitMP3(struct mpstr *mp, long outscale) 
+{
+	/* quiet 4096 med 8192 */
+
 	memset(mp,0,sizeof(struct mpstr));
 
 	mp->framesize = 0;
 	mp->fsizeold = -1;
 	mp->bsize = 0;
 	mp->head = mp->tail = NULL;
-	mp->fr.single = -1;
+	mp->fr.single = 3; /* force mono */
 	mp->bsnum = 0;
 	mp->synth_bo = 1;
+	mp->outsamplerate = 8000;
 
-	make_decode_tables(32767);
-	init_layer3(SBLIMIT);
+	make_decode_tables_scale(mp, outscale);
+
+	init_layer3_sample_limits(mp, SBLIMIT);
 
 	return !0;
 }
@@ -32,7 +48,6 @@
 		free(b);
 		b = bn;
 	}
-	mp->tail = NULL;
 }
 
 static struct buf *addbuf(struct mpstr *mp,char *buf,int size)
@@ -41,7 +56,7 @@
 
 	nbuf = malloc( sizeof(struct buf) );
 	if(!nbuf) {
-		ast_log(LOG_ERROR,"Out of memory!\n");
+		ast_log(LOG_WARNING,"Out of memory!\n");
 		return NULL;
 	}
 	nbuf->pnt = malloc(size);
@@ -84,19 +99,20 @@
 
 }
 
-static int read_buf_byte(struct mpstr *mp)
+static int read_buf_byte(int *error, struct mpstr *mp)
 {
-	unsigned int b;
-
-	int pos;
+	unsigned int b;int pos;
 
 	pos = mp->tail->pos;
 	while(pos >= mp->tail->size) {
 		remove_buf(mp);
 		pos = mp->tail->pos;
 		if(!mp->tail) {
-			ast_log(LOG_ERROR,"Fatal error!\n");
-			return -1;
+			/* We may pick up this error a few times*/
+			/* But things have gone pear shaped */
+			ast_log(LOG_WARNING,"Fatal Buffer error!\n");
+			*error = 1;
+			return (0);		
 		}
 	}
 
@@ -108,28 +124,74 @@
 	return b;
 }
 
-static void read_head(struct mpstr *mp)
+static int  read_head(struct mpstr *mp)
 {
 	unsigned long head;
+	int error=0;
+	
 
-	head = read_buf_byte(mp);
+	head = read_buf_byte(&error, mp);
 	head <<= 8;
-	head |= read_buf_byte(mp);
+	head |= read_buf_byte(&error, mp);
 	head <<= 8;
-	head |= read_buf_byte(mp);
+	head |= read_buf_byte(&error, mp);
 	head <<= 8;
-	head |= read_buf_byte(mp);
+	head |= read_buf_byte(&error, mp);
+
+	mp->header = head;
+
+	if(error){
+		return (1);
+	}else
+		return (0);
+		
+}
+
+static int head_check(unsigned long head)
+{
+    if( (head & 0xffe00000) != 0xffe00000)
+	return FALSE;
+    if(!((head>>17)&3))
+	return FALSE;
+    if( ((head>>12)&0xf) == 0xf || ((head>>12)&0xf) == 0)
+	return FALSE;
+    if( ((head>>10)&0x3) == 0x3 )
+	return FALSE;
+    if ((head & 0xffff0000) == 0xfffe0000)
+      return FALSE;
 
+    return TRUE;
+}
+
+static int head_shift(struct mpstr *mp)
+{
+	unsigned long head;
+	int error = 0;
+	
+	head = mp->header;
+	
+	head <<= 8;
+	head |= read_buf_byte(&error, mp);
+	
 	mp->header = head;
+
+	if (error){
+		return (1);
+	}else
+		return (0);
+	
 }
 
+
 int decodeMP3(struct mpstr *mp,char *in,int isize,char *out,
-			  int osize,int *done)
+		int osize,int *done)
 {
 	int len;
+	long n,m;
+	int down_sample_sblimit;
 
 	if(osize < 4608) {
-		ast_log(LOG_ERROR,"To less out space\n");
+		ast_log(LOG_WARNING,"To less out space\n");
 		return MP3_ERR;
 	}
 
@@ -144,18 +206,69 @@
 		if(mp->bsize < 4) {
 			return MP3_NEED_MORE;
 		}
-		read_head(mp);
+		if (read_head(mp))
+			return MP3_ERR;
+		
+		if(!head_check(mp->header) ) {
+			int i;
+
+			ast_log(LOG_WARNING,"Junk at the beginning of frame %08lx\n",mp->header);
+			
+			/* step in byte steps through next 64K */
+			for(i=0;i<65536;i++) {
+				if(!mp->bsize)
+					return MP3_NEED_MORE;
+				
+				if(head_shift(mp))
+					return MP3_ERR;
+				
+				if(head_check(mp->header))
+					break;
+			}
+			if(i == 65536) {
+				ast_log(LOG_WARNING,"Giving up searching valid MPEG header\n");
+				return MP3_ERR;
+			}
+		}
+
 		decode_header(&mp->fr,mp->header);
 		mp->framesize = mp->fr.framesize;
+
+		if (!mp->initmp3){
+			mp->initmp3 = 1;
+			
+			n = freqs[mp->fr.sampling_frequency];
+			if (mp->outsamplerate) {
+				m = mp->outsamplerate;
+			}
+			else {
+				m =n;
+			}
+		
+			if (synth_ntom_set_step(n,m))
+				return MP3_ERR;
+			
+			
+			if(n>m) {
+				down_sample_sblimit = SBLIMIT * m;
+				down_sample_sblimit /= n;
+			}
+			else {
+				down_sample_sblimit = SBLIMIT;
+			}
+			
+			init_layer3_sample_limits(mp, down_sample_sblimit);
+	
+		}
 	}
+	
 
-	if(mp->fr.framesize > mp->bsize) {
+	if(mp->fr.framesize > mp->bsize)
 		return MP3_NEED_MORE;
-	}
 
-	wordpointer = mp->bsspace[mp->bsnum] + 512;
+	(mp->worksample).wordpointer = mp->bsspace[mp->bsnum] + 512;
 	mp->bsnum = (mp->bsnum + 1) & 0x1;
-	bitindex = 0;
+	(mp->worksample).bitindex = 0;
 
 	len = 0;
 	while(len < mp->framesize) {
@@ -167,7 +280,7 @@
 		else {
                   nlen = blen;
                 }
-		memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
+		memcpy((mp->worksample).wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
                 len += nlen;
                 mp->tail->pos += nlen;
 		mp->bsize -= nlen;
@@ -178,13 +291,14 @@
 
 	*done = 0;
 	if(mp->fr.error_protection)
-           getbits(16);
-
-	if((do_layer3(mp,(unsigned char *) out,done)) < 0)
+           getbits(mp, 16);
+	
+	if (do_layer3(mp,(unsigned char *) out,done))
 		return MP3_ERR;
 
 	mp->fsizeold = mp->framesize;
 	mp->framesize = 0;
+
 	return MP3_OK;
 }
 
@@ -192,14 +306,14 @@
 {
   unsigned char *bsbufold;
   if(mp->fsizeold < 0 && backstep > 0) {
-    ast_log(LOG_ERROR,"Can't step back %ld!\n",backstep);
+    ast_log(LOG_WARNING,"Can't step back %ld!\n",backstep);
     return MP3_ERR;
   }
   bsbufold = mp->bsspace[mp->bsnum] + 512;
-  wordpointer -= backstep;
+  (mp->worksample).wordpointer -= backstep;
   if (backstep)
-    memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep);
-  bitindex = 0;
+    memcpy((mp->worksample).wordpointer,bsbufold+mp->fsizeold-backstep,backstep);
+  (mp->worksample).bitindex = 0;
   return MP3_OK;
 }
 

Index: layer3.c
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/format_mp3/layer3.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- layer3.c	5 Sep 2004 18:28:03 -0000	1.1
+++ layer3.c	8 Sep 2004 16:17:25 -0000	1.2
@@ -5,15 +5,15 @@
  * All rights reserved. See also 'README'
  */ 
 
+#include <asterisk/logger.h>
 #include <stdlib.h>
+#include "mpg123.h"
 #include "mpglib.h"
 #include "huffman.h"
 
-
-
 #define MPEG1
 
-
+/* These should all be constants setup once using init_layer3_const */
 static real ispow[8207];
 static real aa_ca[8],aa_cs[8];
 static real COS1[12][6];
@@ -24,7 +24,6 @@
 static real COS6_1,COS6_2;
 static real tfcos36[9];
 static real tfcos12[3];
-static int ready=0;
 
 struct bandInfoStruct {
   short longIdx[23];
@@ -33,8 +32,6 @@
   short shortDiff[13];
 };
 
-int longLimit[9][23];
-int shortLimit[9][14];
 
 struct bandInfoStruct bandInfo[9] = { 
 
@@ -97,13 +94,12 @@
 static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];
 
 /* 
- * init tables for layer-3 
+ * init constant tables for layer-3 
  */
-void init_layer3(int down_sample_sblimit)
+void init_layer3_const(void)
 {
   int i,j,k,l;
-  if(ready)
-	  return;
+
   for(i=-256;i<118+4;i++)
     gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) );
 
@@ -234,19 +230,6 @@
 
   }
 
-  for(j=0;j<9;j++) {
-    for(i=0;i<23;i++) {
-      longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
-      if(longLimit[j][i] > (down_sample_sblimit) )
-        longLimit[j][i] = down_sample_sblimit;
-    }
-    for(i=0;i<14;i++) {
-      shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
-      if(shortLimit[j][i] > (down_sample_sblimit) )
-        shortLimit[j][i] = down_sample_sblimit;
-    }
-  }
-
   for(i=0;i<5;i++) {
     for(j=0;j<6;j++) {
       for(k=0;k<6;k++) {
@@ -289,28 +272,47 @@
       }
     }
   }
-  ready=1;
 }
 
+/* MP3 file specific rates */
+void init_layer3_sample_limits(struct mpstr *mp, int down_sample_sblimit)
+{
+	int i,j;	
+	for(j=0;j<9;j++) {
+		for(i=0;i<23;i++) {
+			(mp->longLimit)[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
+			if((mp->longLimit)[j][i] > (down_sample_sblimit) )
+				(mp->longLimit)[j][i] = down_sample_sblimit;
+		}
+		for(i=0;i<14;i++) {
+			(mp->shortLimit)[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
+			if((mp->shortLimit)[j][i] > (down_sample_sblimit) )
+				(mp->shortLimit)[j][i] = down_sample_sblimit;
+		}
+	}
+}
+
+
+
 /*
  * read additional side information
  */
 #ifdef MPEG1 
-static void III_get_side_info_1(struct III_sideinfo *si,int stereo,
+static int III_get_side_info_1(struct mpstr *mp, struct III_sideinfo *si,int stereo,
  int ms_stereo,long sfreq,int single)
 {
    int ch, gr;
    int powdiff = (single == 3) ? 4 : 0;
 
-   si->main_data_begin = getbits(9);
+   si->main_data_begin = getbits(mp, 9);
    if (stereo == 1)
-     si->private_bits = getbits_fast(5);
+     si->private_bits = getbits_fast(mp, 5);
    else 
-     si->private_bits = getbits_fast(3);
+     si->private_bits = getbits_fast(mp, 3);
 
    for (ch=0; ch<stereo; ch++) {
        si->ch[ch].gr[0].scfsi = -1;
-       si->ch[ch].gr[1].scfsi = getbits_fast(4);
+       si->ch[ch].gr[1].scfsi = getbits_fast(mp, 4);
    }
 
    for (gr=0; gr<2; gr++) 
@@ -319,35 +321,35 @@
      {
        register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
 
-       gr_info->part2_3_length = getbits(12);
-       gr_info->big_values = getbits_fast(9);
+       gr_info->part2_3_length = getbits(mp, 12);
+       gr_info->big_values = getbits_fast(mp, 9);
        if(gr_info->big_values > 288) {
-          ast_log(LOG_ERROR,"big_values too large 1!\n");
+          ast_log(LOG_WARNING,"big_values too large!\n");
           gr_info->big_values = 288;
        }
-       gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
+       gr_info->pow2gain = gainpow2+256 - getbits_fast(mp, 8) + powdiff;
        if(ms_stereo)
          gr_info->pow2gain += 2;
-       gr_info->scalefac_compress = getbits_fast(4);
+       gr_info->scalefac_compress = getbits_fast(mp, 4);
 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
-       if(get1bit()) 
+       if(get1bit(mp)) 
        {
          int i;
-         gr_info->block_type = getbits_fast(2);
-         gr_info->mixed_block_flag = get1bit();
-         gr_info->table_select[0] = getbits_fast(5);
-         gr_info->table_select[1] = getbits_fast(5);
+         gr_info->block_type = getbits_fast(mp, 2);
+         gr_info->mixed_block_flag = get1bit(mp);
+         gr_info->table_select[0] = getbits_fast(mp, 5);
+         gr_info->table_select[1] = getbits_fast(mp, 5);
          /*
           * table_select[2] not needed, because there is no region2,
           * but to satisfy some verifications tools we set it either.
           */
          gr_info->table_select[2] = 0;
          for(i=0;i<3;i++)
-           gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
+           gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(mp, 3)<<3);
 
          if(gr_info->block_type == 0) {
-           ast_log(LOG_ERROR,"Blocktype == 0 and window-switching == 1 not allowed.\n");
-		   return;
+           ast_log(LOG_WARNING,"Blocktype == 0 and window-switching == 1 not allowed.\n");
+           return (1);
          }
          /* region_count/start parameters are implicit in this case. */       
          gr_info->region1start = 36>>1;
@@ -357,70 +359,72 @@
        {
          int i,r0c,r1c;
          for (i=0; i<3; i++)
-           gr_info->table_select[i] = getbits_fast(5);
-         r0c = getbits_fast(4);
-         r1c = getbits_fast(3);
+           gr_info->table_select[i] = getbits_fast(mp, 5);
+         r0c = getbits_fast(mp, 4);
+         r1c = getbits_fast(mp, 3);
          gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
          gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
          gr_info->block_type = 0;
          gr_info->mixed_block_flag = 0;
        }
-       gr_info->preflag = get1bit();
-       gr_info->scalefac_scale = get1bit();
-       gr_info->count1table_select = get1bit();
+       gr_info->preflag = get1bit(mp);
+       gr_info->scalefac_scale = get1bit(mp);
+       gr_info->count1table_select = get1bit(mp);
      }
    }
+   return (0);
+   
 }
 #endif
 
 /*
  * Side Info for MPEG 2.0 / LSF
  */
-static void III_get_side_info_2(struct III_sideinfo *si,int stereo,
+static int III_get_side_info_2(struct mpstr *mp, struct III_sideinfo *si,int stereo,
  int ms_stereo,long sfreq,int single)
 {
    int ch;
    int powdiff = (single == 3) ? 4 : 0;
 
-   si->main_data_begin = getbits(8);
+   si->main_data_begin = getbits(mp, 8);
    if (stereo == 1)
-     si->private_bits = get1bit();
+     si->private_bits = get1bit(mp);
    else 
-     si->private_bits = getbits_fast(2);
+     si->private_bits = getbits_fast(mp, 2);
 
    for (ch=0; ch<stereo; ch++) 
    {
        register struct gr_info_s *gr_info = &(si->ch[ch].gr[0]);
 
-       gr_info->part2_3_length = getbits(12);
-       gr_info->big_values = getbits_fast(9);
+       gr_info->part2_3_length = getbits(mp, 12);
+       gr_info->big_values = getbits_fast(mp, 9);
        if(gr_info->big_values > 288) {
-         ast_log(LOG_ERROR,"big_values too large 2!\n");
+         ast_log(LOG_WARNING,"big_values too large!\n");
          gr_info->big_values = 288;
        }
-       gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
+       gr_info->pow2gain = gainpow2+256 - getbits_fast(mp, 8) + powdiff;
        if(ms_stereo)
          gr_info->pow2gain += 2;
-       gr_info->scalefac_compress = getbits(9);
+       gr_info->scalefac_compress = getbits(mp, 9);
 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
-       if(get1bit()) 
+       if(get1bit(mp)) 
        {
          int i;
-         gr_info->block_type = getbits_fast(2);
-         gr_info->mixed_block_flag = get1bit();
-         gr_info->table_select[0] = getbits_fast(5);
-         gr_info->table_select[1] = getbits_fast(5);
+         gr_info->block_type = getbits_fast(mp, 2);
+         gr_info->mixed_block_flag = get1bit(mp);
+         gr_info->table_select[0] = getbits_fast(mp, 5);
+         gr_info->table_select[1] = getbits_fast(mp, 5);
          /*
           * table_select[2] not needed, because there is no region2,
           * but to satisfy some verifications tools we set it either.
           */
          gr_info->table_select[2] = 0;
          for(i=0;i<3;i++)
-           gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
+           gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(mp, 3)<<3);
 
          if(gr_info->block_type == 0) {
-           ast_log(LOG_ERROR,"Blocktype == 0 and window-switching == 1 not allowed.\n");
-		   return;
+           ast_log(LOG_WARNING,"Blocktype == 0 and window-switching == 1 not allowed.\n");
+           return (1);
          }
          /* region_count/start parameters are implicit in this case. */       
 /* check this again! */
@@ -437,24 +441,25 @@
        {
          int i,r0c,r1c;
          for (i=0; i<3; i++)
-           gr_info->table_select[i] = getbits_fast(5);
-         r0c = getbits_fast(4);
-         r1c = getbits_fast(3);
+           gr_info->table_select[i] = getbits_fast(mp, 5);
+         r0c = getbits_fast(mp, 4);
+         r1c = getbits_fast(mp, 3);
          gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
          gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
          gr_info->block_type = 0;
          gr_info->mixed_block_flag = 0;
        }
-       gr_info->scalefac_scale = get1bit();
-       gr_info->count1table_select = get1bit();
+       gr_info->scalefac_scale = get1bit(mp);
+       gr_info->count1table_select = get1bit(mp);
    }
+   return (0);
 }
 
 /*
  * read scalefactors
  */
 #ifdef MPEG1
-static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info)
+static int III_get_scale_factors_1(struct mpstr *mp, int *scf,struct gr_info_s *gr_info)
 {
    static unsigned char slen[2][16] = {
      {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
@@ -471,15 +476,15 @@
 
       if (gr_info->mixed_block_flag) {
          for (i=8;i;i--)
-           *scf++ = getbits_fast(num0);
+           *scf++ = getbits_fast(mp, num0);
          i = 9;
          numbits -= num0; /* num0 * 17 + num1 * 18 */
       }
 
       for (;i;i--)
-        *scf++ = getbits_fast(num0);
+        *scf++ = getbits_fast(mp, num0);
       for (i = 18; i; i--)
-        *scf++ = getbits_fast(num1);
+        *scf++ = getbits_fast(mp, num1);
       *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
     }
     else 
@@ -489,16 +494,16 @@
 
       if(scfsi < 0) { /* scfsi < 0 => granule == 0 */
          for(i=11;i;i--)
-           *scf++ = getbits_fast(num0);
+           *scf++ = getbits_fast(mp, num0);
          for(i=10;i;i--)
-           *scf++ = getbits_fast(num1);
+           *scf++ = getbits_fast(mp, num1);
          numbits = (num0 + num1) * 10 + num0;
       }
       else {
         numbits = 0;
         if(!(scfsi & 0x8)) {
           for (i=6;i;i--)
-            *scf++ = getbits_fast(num0);
+            *scf++ = getbits_fast(mp, num0);
           numbits += num0 * 6;
         }
         else {
@@ -508,7 +513,7 @@
 
         if(!(scfsi & 0x4)) {
           for (i=5;i;i--)
-            *scf++ = getbits_fast(num0);
+            *scf++ = getbits_fast(mp, num0);
           numbits += num0 * 5;
         }
         else {
@@ -518,7 +523,7 @@
 
         if(!(scfsi & 0x2)) {
           for(i=5;i;i--)
-            *scf++ = getbits_fast(num1);
+            *scf++ = getbits_fast(mp, num1);
           numbits += num1 * 5;
         }
         else {
@@ -528,7 +533,7 @@
 
         if(!(scfsi & 0x1)) {
           for (i=5;i;i--)
-            *scf++ = getbits_fast(num1);
+            *scf++ = getbits_fast(mp, num1);
           numbits += num1 * 5;
         }
         else {
@@ -543,7 +548,7 @@
 }
 #endif
 
-static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo)
+static int III_get_scale_factors_2(struct mpstr *mp, int *scf,struct gr_info_s *gr_info,int i_stereo)
 {
   unsigned char *pnt;
   int i,j;
@@ -580,7 +585,7 @@
     slen >>= 3;
     if(num) {
       for(j=0;j<(int)(pnt[i]);j++)
-        *scf++ = getbits_fast(num);
+        *scf++ = getbits_fast(mp, num);
       numbits += pnt[i] * num;
     }
     else {
@@ -602,7 +607,7 @@
 /*
  * don't forget to apply the same changes to III_dequantize_sample_ms() !!! 
  */
-static int III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int *scf,
+static int III_dequantize_sample(struct mpstr *mp, real xr[SBLIMIT][SSLIMIT],int *scf,
    struct gr_info_s *gr_info,int sfreq,int part2bits)
 {
   int shift = 1 + gr_info->scalefac_scale;
@@ -680,7 +685,7 @@
         {
           register short *val = h->table;
           while((y=*val++)<0) {
-            if (get1bit())
+            if (get1bit(mp))
               val -= y;
             part2remain--;
           }
@@ -690,15 +695,15 @@
         if(x == 15) {
           max[lwin] = cb;
           part2remain -= h->linbits+1;
-          x += getbits(h->linbits);
-          if(get1bit())
+          x += getbits(mp, h->linbits);
+          if(get1bit(mp))
             *xrpnt = -ispow[x] * v;
           else
             *xrpnt =  ispow[x] * v;
         }
         else if(x) {
           max[lwin] = cb;
-          if(get1bit())
+          if(get1bit(mp))
             *xrpnt = -ispow[x] * v;
           else
             *xrpnt =  ispow[x] * v;
@@ -710,15 +715,15 @@
         if(y == 15) {
           max[lwin] = cb;
           part2remain -= h->linbits+1;
-          y += getbits(h->linbits);
-          if(get1bit())
+          y += getbits(mp, h->linbits);
+          if(get1bit(mp))
             *xrpnt = -ispow[y] * v;
           else
             *xrpnt =  ispow[y] * v;
         }
         else if(y) {
           max[lwin] = cb;
-          if(get1bit())
+          if(get1bit(mp))
             *xrpnt = -ispow[y] * v;
           else
             *xrpnt =  ispow[y] * v;
@@ -740,7 +745,7 @@
           a = 0;
           break;
         }
-        if (get1bit())
+        if (get1bit(mp))
           val -= a;
       }
 
@@ -769,7 +774,7 @@
             part2remain++;
             break;
           }
-          if(get1bit()) 
+          if(get1bit(mp)) 
             *xrpnt = -v;
           else
             *xrpnt = v;
@@ -810,7 +815,7 @@
     {
       int rmax = max[0] > max[1] ? max[0] : max[1];
       rmax = (rmax > max[2] ? rmax : max[2]) + 1;
-      gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
+      gr_info->maxb = rmax ? (mp->shortLimit)[sfreq][rmax] : (mp->longLimit)[sfreq][max[3]+1];
     }
 
   }
@@ -846,7 +851,7 @@
         {
           register short *val = h->table;
           while((y=*val++)<0) {
-            if (get1bit())
+            if (get1bit(mp))
               val -= y;
             part2remain--;
           }
@@ -856,15 +861,15 @@
         if (x == 15) {
           max = cb;
           part2remain -= h->linbits+1;
-          x += getbits(h->linbits);
-          if(get1bit())
+          x += getbits(mp, h->linbits);
+          if(get1bit(mp))
             *xrpnt++ = -ispow[x] * v;
           else
             *xrpnt++ =  ispow[x] * v;
         }
         else if(x) {
           max = cb;
-          if(get1bit())
+          if(get1bit(mp))
             *xrpnt++ = -ispow[x] * v;
           else
             *xrpnt++ =  ispow[x] * v;
@@ -876,15 +881,15 @@
         if (y == 15) {
           max = cb;
           part2remain -= h->linbits+1;
-          y += getbits(h->linbits);
-          if(get1bit())
+          y += getbits(mp, h->linbits);
+          if(get1bit(mp))
             *xrpnt++ = -ispow[y] * v;
           else
             *xrpnt++ =  ispow[y] * v;
         }
         else if(y) {
           max = cb;
-          if(get1bit())
+          if(get1bit(mp))
             *xrpnt++ = -ispow[y] * v;
           else
             *xrpnt++ =  ispow[y] * v;
@@ -909,7 +914,7 @@
           a = 0;
           break;
         }
-        if (get1bit())
+        if (get1bit(mp))
           val -= a;
       }
 
@@ -929,7 +934,7 @@
             part2remain++;
             break;
           }
-          if(get1bit())
+          if(get1bit(mp))
             *xrpnt++ = -v;
           else
             *xrpnt++ = v;
@@ -948,17 +953,17 @@
     }
 
     gr_info->maxbandl = max+1;
-    gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
+    gr_info->maxb = (mp->longLimit)[sfreq][gr_info->maxbandl];
   }
 
   while( part2remain > 16 ) {
-    getbits(16); /* Dismiss stuffing Bits */
+    getbits(mp, 16); /* Dismiss stuffing Bits */
     part2remain -= 16;
   }
   if(part2remain > 0)
-    getbits(part2remain);
+    getbits(mp, part2remain);
   else if(part2remain < 0) {
-    ast_log(LOG_ERROR,"mpg123: Can't rewind stream by %d bits!\n",-part2remain);
+    ast_log(LOG_WARNING,"mpg123: Can't rewind stream by %d bits!\n",-part2remain);
     return 1; /* -> error */
   }
   return 0;
@@ -1042,7 +1047,7 @@
         {
           register short *val = h->table;
           while((y=*val++)<0) {
-            if (get1bit())
+            if (get1bit(mp))
               val -= y;
             part2remain--;
           }
@@ -1052,8 +1057,8 @@
         if(x == 15) {
           max[lwin] = cb;
           part2remain -= h->linbits+1;
-          x += getbits(h->linbits);
-          if(get1bit()) {
+          x += getbits(mp, h->linbits);
+          if(get1bit(mp)) {
             real a = ispow[x] * v;
             *xrpnt = *xr0pnt + a;
             *xr0pnt -= a;
@@ -1066,7 +1071,7 @@
         }
         else if(x) {
           max[lwin] = cb;
-          if(get1bit()) {
+          if(get1bit(mp)) {
             real a = ispow[x] * v;
             *xrpnt = *xr0pnt + a;
             *xr0pnt -= a;
@@ -1086,8 +1091,8 @@
         if(y == 15) {
           max[lwin] = cb;
           part2remain -= h->linbits+1;
-          y += getbits(h->linbits);
-          if(get1bit()) {
+          y += getbits(mp, h->linbits);
+          if(get1bit(mp)) {
             real a = ispow[y] * v;
             *xrpnt = *xr0pnt + a;
             *xr0pnt -= a;
@@ -1100,7 +1105,7 @@
         }
         else if(y) {
           max[lwin] = cb;
-          if(get1bit()) {
+          if(get1bit(mp)) {
             real a = ispow[y] * v;
             *xrpnt = *xr0pnt + a;
             *xr0pnt -= a;
@@ -1130,7 +1135,7 @@
           a = 0;
           break;
         }
-        if (get1bit())
+        if (get1bit(mp))
           val -= a;
       }
 
@@ -1160,7 +1165,7 @@
             part2remain++;
             break;
           }
-          if(get1bit()) {
+          if(get1bit(mp)) {
             *xrpnt = *xr0pnt + v;
             *xr0pnt -= v;
           }
@@ -1209,7 +1214,7 @@
     {
       int rmax = max[0] > max[1] ? max[0] : max[1];
       rmax = (rmax > max[2] ? rmax : max[2]) + 1;
-      gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
+      gr_info->maxb = rmax ? (mp->shortLimit)[sfreq][rmax] : (mp->longLimit)[sfreq][max[3]+1];
     }
   }
   else {
@@ -1236,7 +1241,7 @@
         {
           register short *val = h->table;
           while((y=*val++)<0) {
-            if (get1bit())
+            if (get1bit(mp))
               val -= y;
             part2remain--;
           }
@@ -1246,8 +1251,8 @@
         if (x == 15) {
           max = cb;
           part2remain -= h->linbits+1;
-          x += getbits(h->linbits);
-          if(get1bit()) {
+          x += getbits(mp, h->linbits);
+          if(get1bit(mp)) {
             real a = ispow[x] * v;
             *xrpnt++ = *xr0pnt + a;
             *xr0pnt++ -= a;
@@ -1260,7 +1265,7 @@
         }
         else if(x) {
           max = cb;
-          if(get1bit()) {
+          if(get1bit(mp)) {
             real a = ispow[x] * v;
             *xrpnt++ = *xr0pnt + a;
             *xr0pnt++ -= a;
@@ -1278,8 +1283,8 @@
         if (y == 15) {
           max = cb;
           part2remain -= h->linbits+1;
-          y += getbits(h->linbits);
-          if(get1bit()) {
+          y += getbits(mp, h->linbits);
+          if(get1bit(mp)) {
             real a = ispow[y] * v;
             *xrpnt++ = *xr0pnt + a;
             *xr0pnt++ -= a;
@@ -1292,7 +1297,7 @@
         }
         else if(y) {
           max = cb;
-          if(get1bit()) {
+          if(get1bit(mp)) {
             real a = ispow[y] * v;
             *xrpnt++ = *xr0pnt + a;
             *xr0pnt++ -= a;
@@ -1320,7 +1325,7 @@
           a = 0;
           break;
         }
-        if (get1bit())
+        if (get1bit(mp))
           val -= a;
       }
 
@@ -1340,7 +1345,7 @@
             part2remain++;
             break;
           }
-          if(get1bit()) {
+          if(get1bit(mp)) {
             *xrpnt++ = *xr0pnt + v;
             *xr0pnt++ -= v;
           }
@@ -1363,13 +1368,13 @@
   }
 
   while ( part2remain > 16 ) {
-    getbits(16); /* Dismiss stuffing Bits */
+    getbits(mp, 16); /* Dismiss stuffing Bits */
     part2remain -= 16;
   }
   if(part2remain > 0 )
-    getbits(part2remain);
+    getbits(mp, part2remain);
   else if(part2remain < 0) {
-    ast_log(LOG_ERROR,"mpg123_ms: Can't rewind stream by %d bits!\n",-part2remain);
+    ast_log(LOG_WARNING,"mpg123_ms: Can't rewind stream by %d bits!\n",-part2remain);
     return 1; /* -> error */
   }
   return 0;
@@ -1869,19 +1874,24 @@
 /*
  * main layer3 handler
  */
-int do_layer3(struct mpstr *mp, unsigned char *pcm_sample,int *pcm_point)
+int do_layer3(struct mpstr *mp,unsigned char *pcm_sample,int *pcm_point)
 {
-  struct frame *fr = &mp->fr;
+
   int gr, ch, ss,clip=0;
   int scalefacs[39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
   struct III_sideinfo sideinfo;
-  int stereo = fr->stereo;
-  int single = fr->single;
+  struct frame *fr;
+  int stereo, single, sfreq;
   int ms_stereo,i_stereo;
-  int sfreq = fr->sampling_frequency;
   int stereo1,granules;
 
+  fr = &(mp->fr);
+  stereo = fr->stereo;
+  single = fr->single;
+  sfreq = fr->sampling_frequency;
 
+  
+  
   if(stereo == 1) { /* stream is mono */
     stereo1 = 1;
     single = 0;
@@ -1900,18 +1910,21 @@
 
   if(fr->lsf) {
     granules = 1;
-    III_get_side_info_2(&sideinfo,stereo,ms_stereo,sfreq,single);
+    if (III_get_side_info_2(mp, &sideinfo,stereo,ms_stereo,sfreq,single))
+	    return (MP3_ERR);
   }
   else {
     granules = 2;
 #ifdef MPEG1
-    III_get_side_info_1(&sideinfo,stereo,ms_stereo,sfreq,single);
+   if ( III_get_side_info_1(mp, &sideinfo,stereo,ms_stereo,sfreq,single))
+	   return (MP3_ERR);
+   
 #else
-    ast_log(LOG_ERROR,"Not supported\n");
+    ast_log(LOG_WARNING,"Not supported\n");
 #endif
   }
 
-  if(set_pointer(mp,sideinfo.main_data_begin) == MP3_ERR)
+  if(set_pointer(mp, sideinfo.main_data_begin) == MP3_ERR)
     return 0;
 
   for (gr=0;gr<granules;gr++) 
@@ -1923,32 +1936,32 @@
       struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
       long part2bits;
       if(fr->lsf)
-        part2bits = III_get_scale_factors_2(scalefacs,gr_info,0);
+        part2bits = III_get_scale_factors_2(mp, scalefacs,gr_info,0);
       else {
 #ifdef MPEG1
-        part2bits = III_get_scale_factors_1(scalefacs,gr_info);
+        part2bits = III_get_scale_factors_1(mp, scalefacs,gr_info);
 #else
-	ast_log(LOG_ERROR,"Not supported\n");
+	ast_log(LOG_WARNING,"Not supported\n");
 #endif
       }
-      if(III_dequantize_sample(hybridIn[0], scalefacs,gr_info,sfreq,part2bits))
-        return -1;
+      if(III_dequantize_sample(mp, hybridIn[0], scalefacs,gr_info,sfreq,part2bits))
+        return clip;
     }
     if(stereo == 2) {
       struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
       long part2bits;
       if(fr->lsf) 
-        part2bits = III_get_scale_factors_2(scalefacs,gr_info,i_stereo);
+        part2bits = III_get_scale_factors_2(mp, scalefacs,gr_info,i_stereo);
       else {
 #ifdef MPEG1
-        part2bits = III_get_scale_factors_1(scalefacs,gr_info);
+        part2bits = III_get_scale_factors_1(mp, scalefacs,gr_info);
 #else
-	ast_log(LOG_ERROR,"Not supported\n");
+	ast_log(LOG_WARNING,"Not supported\n");
 #endif
       }
 
-      if(III_dequantize_sample(hybridIn[1],scalefacs,gr_info,sfreq,part2bits))
-          return -1;
+      if(III_dequantize_sample(mp, hybridIn[1],scalefacs,gr_info,sfreq,part2bits))
+          return clip;
 
       if(ms_stereo) {
         int i;
@@ -1994,22 +2007,22 @@
     for(ch=0;ch<stereo1;ch++) {
       struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
       III_antialias(hybridIn[ch],gr_info);
-      III_hybrid(mp,hybridIn[ch], hybridOut[ch], ch,gr_info);
+      III_hybrid(mp, hybridIn[ch], hybridOut[ch], ch,gr_info);
     }
 
     for(ss=0;ss<SSLIMIT;ss++) {
       if(single >= 0) {
-        clip += synth_1to1_mono(mp,hybridOut[0][ss],pcm_sample,pcm_point);
+        clip += synth_ntom_mono(mp,hybridOut[0][ss],pcm_sample,pcm_point);
       }
       else {
         int p1 = *pcm_point;
-        clip += synth_1to1(mp,hybridOut[0][ss],0,pcm_sample,&p1);
-        clip += synth_1to1(mp,hybridOut[1][ss],1,pcm_sample,pcm_point);
+        clip += synth_ntom(mp,hybridOut[0][ss],0,pcm_sample,&p1);
+        clip += synth_ntom(mp,hybridOut[1][ss],1,pcm_sample,pcm_point);
       }
     }
   }
   
-  return clip;
+  return 0;
 }
 
 

Index: mpg123.h
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/format_mp3/mpg123.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- mpg123.h	5 Sep 2004 18:28:03 -0000	1.1
+++ mpg123.h	8 Sep 2004 16:17:25 -0000	1.2
@@ -1,8 +1,6 @@
 #include        <stdio.h>
 #include        <string.h>
 #include        <signal.h>
-#include        "mpglib.h"
-
 
 #ifndef WIN32
 #include        <sys/signal.h>
@@ -87,16 +85,6 @@
 	int checkrange;
 };
 
-extern unsigned int   get1bit(void);
-extern unsigned int   getbits(int);
-extern unsigned int   getbits_fast(int);
-extern int set_pointer(long);
-
-extern unsigned char *wordpointer;
-extern int bitindex;
-
-extern void make_decode_tables(long scaleval);
-extern int do_layer3(struct mpstr *mp,unsigned char *,int *);
 extern int decode_header(struct frame *fr,unsigned long newhead);
 
 
@@ -131,52 +119,14 @@
   } ch[2];
 };
 
-extern int synth_1to1 (real *,int,unsigned char *,int *);
-extern int synth_1to1_8bit (real *,int,unsigned char *,int *);
-extern int synth_1to1_mono (real *,unsigned char *,int *);
-extern int synth_1to1_mono2stereo (real *,unsigned char *,int *);
-extern int synth_1to1_8bit_mono (real *,unsigned char *,int *);
-extern int synth_1to1_8bit_mono2stereo (real *,unsigned char *,int *);
-
-extern int synth_2to1 (real *,int,unsigned char *,int *);
-extern int synth_2to1_8bit (real *,int,unsigned char *,int *);
-extern int synth_2to1_mono (real *,unsigned char *,int *);
-extern int synth_2to1_mono2stereo (real *,unsigned char *,int *);
-extern int synth_2to1_8bit_mono (real *,unsigned char *,int *);
-extern int synth_2to1_8bit_mono2stereo (real *,unsigned char *,int *);
-
-extern int synth_4to1 (real *,int,unsigned char *,int *);
-extern int synth_4to1_8bit (real *,int,unsigned char *,int *);
-extern int synth_4to1_mono (real *,unsigned char *,int *);
-extern int synth_4to1_mono2stereo (real *,unsigned char *,int *);
-extern int synth_4to1_8bit_mono (real *,unsigned char *,int *);
-extern int synth_4to1_8bit_mono2stereo (real *,unsigned char *,int *);
-
-extern int synth_ntom (real *,int,unsigned char *,int *);
-extern int synth_ntom_8bit (real *,int,unsigned char *,int *);
-extern int synth_ntom_mono (real *,unsigned char *,int *);
-extern int synth_ntom_mono2stereo (real *,unsigned char *,int *);
-extern int synth_ntom_8bit_mono (real *,unsigned char *,int *);
-extern int synth_ntom_8bit_mono2stereo (real *,unsigned char *,int *);
-
-extern void rewindNbits(int bits);
-extern int  hsstell(void);
-extern int get_songlen(struct frame *fr,int no);
-
-extern void init_layer3(int);
-extern void init_layer2(void);
-extern void make_decode_tables(long scale);
-extern void make_conv16to8_table(int);
-extern void dct64(real *,real *,real *);
+struct pcm_workingsample
+{
+	int bitindex;
+	unsigned char *wordpointer;
+};
 
-extern void synth_ntom_set_step(long,long);
 
-extern unsigned char *conv16to8;
 extern long freqs[9];
-extern real muls[27][64];
-extern real decwin[512+32];
-extern real *pnts[5];
-
 extern struct parameter param;
-
+extern real *pnts[5];
 

Index: mpglib.h
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/format_mp3/mpglib.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- mpglib.h	5 Sep 2004 18:28:03 -0000	1.1
+++ mpglib.h	8 Sep 2004 16:17:25 -0000	1.2
@@ -1,99 +1,3 @@
-#ifndef __MPGLIB_H
-#define __MPGLIB_H      
-
-#include        <stdio.h>
-#include        <string.h>
-#include        <signal.h>
-#include        <asterisk/logger.h>
-
-
-#ifndef WIN32
-#include        <sys/signal.h>
-#include        <unistd.h>
-#endif
-
-#include        <math.h>
-
-#ifdef _WIN32
-# undef WIN32
-# define WIN32
-
-
-# define M_PI       3.14159265358979323846
-# define M_SQRT2	1.41421356237309504880
-# define REAL_IS_FLOAT
-# define NEW_DCT9
-
-# define random rand
-# define srandom srand
-
-#endif
-
-#ifdef REAL_IS_FLOAT
-#  define real float
-#elif defined(REAL_IS_LONG_DOUBLE)
-#  define real long double
-#else
-#  define real double
-#endif
-
-#ifdef __GNUC__
-#define INLINE inline
-#else
-#define INLINE
-#endif
-
-/* AUDIOBUFSIZE = n*64 with n=1,2,3 ...  */
-#define		AUDIOBUFSIZE		16384
-
-#define         FALSE                   0
-#define         TRUE                    1
-
-#define         SBLIMIT                 32
-#define         SSLIMIT                 18
-
-#define         MPG_MD_STEREO           0
-#define         MPG_MD_JOINT_STEREO     1
-#define         MPG_MD_DUAL_CHANNEL     2
-#define         MPG_MD_MONO             3
-
-#define MAXFRAMESIZE 1792
-
-
-
-
-
-
-/* Pre Shift fo 16 to 8 bit converter table */
-#define AUSHIFT (3)
-
-struct frame {
-    int stereo;
-    int jsbound;
-    int single;
-    int lsf;
-    int mpeg25;
-    int header_change;
-    int lay;
-    int error_protection;
-    int bitrate_index;
-    int sampling_frequency;
-    int padding;
-    int extension;
-    int mode;
-    int mode_ext;
-    int copyright;
-    int original;
-    int emphasis;
-    int framesize; /* computed framesize */
-};
-
-struct parameter {
-	int quiet;	/* shut up! */
-	int tryresync;  /* resync stream after error */
-	int verbose;    /* verbose level */
-	int checkrange;
-};
 
 struct buf {
         unsigned char *pnt;
@@ -123,115 +27,49 @@
 	int bsnum;
 	real synth_buffs[2][2][0x110];
         int  synth_bo;
-};
-
-
-extern unsigned int   get1bit(void);
-extern unsigned int   getbits(int);
-extern unsigned int   getbits_fast(int);
-extern int set_pointer(struct mpstr *, long);
-
-extern unsigned char *wordpointer;
-extern int bitindex;
-
-extern void make_decode_tables(long scaleval);
-extern int do_layer3(struct mpstr *mp,unsigned char *,int *);
-extern int decode_header(struct frame *fr,unsigned long newhead);
-
-
-
-struct gr_info_s {
-      int scfsi;
-      unsigned part2_3_length;
-      unsigned big_values;
-      unsigned scalefac_compress;
-      unsigned block_type;
-      unsigned mixed_block_flag;
-      unsigned table_select[3];
-      unsigned subblock_gain[3];
-      unsigned maxband[3];
-      unsigned maxbandl;
-      unsigned maxb;
-      unsigned region1start;
-      unsigned region2start;
-      unsigned preflag;
-      unsigned scalefac_scale;
-      unsigned count1table_select;
-      real *full_gain[3];
-      real *pow2gain;
-};
+	long outscale; /* volume control default value 32768 */
+	long outsamplerate; /* raw output rate default same as mp3 sample rate*/
+	struct pcm_workingsample worksample; /* keep the state of the working sample for threads */
+	int initmp3; /* flag for first initialisation */
+	int longLimit[9][23]; /*sample limits re setting volume */
+	int shortLimit[9][14];
+	real decwin[512+32]; /* scale table */
+	
+	};
 
-struct III_sideinfo
-{
-  unsigned main_data_begin;
-  unsigned private_bits;
-  struct {
-    struct gr_info_s gr[2];
-  } ch[2];
-};
+#define BOOL int
 
-extern int synth_1to1 (struct mpstr *mp,real *,int,unsigned char *,int *);
-extern int synth_1to1_8bit (real *,int,unsigned char *,int *);
-extern int synth_1to1_mono (struct mpstr *mp, real *,unsigned char *,int *);
-extern int synth_1to1_mono2stereo (real *,unsigned char *,int *);
-extern int synth_1to1_8bit_mono (real *,unsigned char *,int *);
-extern int synth_1to1_8bit_mono2stereo (real *,unsigned char *,int *);
+#define MP3_ERR -1
+#define MP3_OK  0
+#define MP3_NEED_MORE 1
 
-extern int synth_2to1 (real *,int,unsigned char *,int *);
-extern int synth_2to1_8bit (real *,int,unsigned char *,int *);
-extern int synth_2to1_mono (real *,unsigned char *,int *);
-extern int synth_2to1_mono2stereo (real *,unsigned char *,int *);
-extern int synth_2to1_8bit_mono (real *,unsigned char *,int *);
-extern int synth_2to1_8bit_mono2stereo (real *,unsigned char *,int *);
 
-extern int synth_4to1 (real *,int,unsigned char *,int *);
-extern int synth_4to1_8bit (real *,int,unsigned char *,int *);
-extern int synth_4to1_mono (real *,unsigned char *,int *);
-extern int synth_4to1_mono2stereo (real *,unsigned char *,int *);
-extern int synth_4to1_8bit_mono (real *,unsigned char *,int *);
-extern int synth_4to1_8bit_mono2stereo (real *,unsigned char *,int *);
+void InitMP3Constants(void);
+BOOL InitMP3(struct mpstr *mp, long outscale);
+int decodeMP3(struct mpstr *mp,char *inmemory,int inmemsize,
+     char *outmemory,int outmemsize,int *done);
+void ExitMP3(struct mpstr *mp);
 
-extern int synth_ntom (real *,int,unsigned char *,int *);
+extern int synth_ntom_set_step(long,long);
+extern int synth_ntom(struct mpstr *mp, real *bandPtr,int channel,unsigned char *out,int *pnt);
+extern int synth_ntom_mono (struct mpstr *mp, real *bandPtr,unsigned char *samples,int *pnt);
 extern int synth_ntom_8bit (real *,int,unsigned char *,int *);
-extern int synth_ntom_mono (real *,unsigned char *,int *);
 extern int synth_ntom_mono2stereo (real *,unsigned char *,int *);
 extern int synth_ntom_8bit_mono (real *,unsigned char *,int *);
 extern int synth_ntom_8bit_mono2stereo (real *,unsigned char *,int *);
 
-extern void rewindNbits(int bits);
-extern int  hsstell(void);
-extern int get_songlen(struct frame *fr,int no);
+extern void init_layer3_sample_limits(struct mpstr *mp, int down_sample_sblimit);
+extern void init_layer3_const(void);
+extern int do_layer3(struct mpstr *mp,unsigned char *,int *);
 
-extern void init_layer3(int);
-extern void init_layer2(void);
-extern void make_decode_tables(long scale);
+extern void make_decode_tables_scale(struct mpstr *mp, long scaleval);
+extern void make_decode_tables_const(void);
 extern void make_conv16to8_table(int);
-extern void dct64(real *,real *,real *);
-
-extern void synth_ntom_set_step(long,long);
-
-extern unsigned char *conv16to8;
-extern long freqs[9];
-extern real muls[27][64];
-extern real decwin[512+32];
-extern real *pnts[5];
-
-extern struct parameter param;
-
-
-
-
-
-#define BOOL int
-
-#define MP3_ERR -1
-#define MP3_OK  0
-#define MP3_NEED_MORE 1
 
+extern void dct64(real *,real *,real *);
 
-BOOL InitMP3(struct mpstr *mp);
-int decodeMP3(struct mpstr *mp,char *inmemory,int inmemsize,
-     char *outmemory,int outmemsize,int *done);
-void ExitMP3(struct mpstr *mp);
+extern unsigned int   get1bit(struct mpstr *mp);
+extern unsigned int   getbits(struct mpstr *mp, int);
+extern unsigned int   getbits_fast(struct mpstr *mp, int);
+extern int set_pointer(struct mpstr *mp, long backstep);
 
-#endif

Index: tabinit.c
===================================================================
RCS file: /usr/cvsroot/asterisk-addons/format_mp3/tabinit.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- tabinit.c	5 Sep 2004 18:28:03 -0000	1.1
+++ tabinit.c	8 Sep 2004 16:17:25 -0000	1.2
@@ -1,18 +1,12 @@
 
 #include <stdlib.h>
 
+#include "mpg123.h"
 #include "mpglib.h"
 
-real decwin[512+32];
 static real cos64[16],cos32[8],cos16[4],cos8[2],cos4[1];
 real *pnts[] = { cos64,cos32,cos16,cos8,cos4 };
 
-#if 0
-static unsigned char *conv16to8_buf = NULL;
-unsigned char *conv16to8;
-#endif
-static int ready=0;
-
 static long intwinbase[] = {
      0,    -1,    -1,    -1,    -1,    -1,    -1,    -2,    -2,    -2,
     -2,    -3,    -3,    -4,    -4,    -5,    -5,    -6,    -7,    -7,
@@ -41,26 +35,31 @@
  64019, 65290, 66494, 67629, 68692, 69679, 70590, 71420, 72169, 72835,
  73415, 73908, 74313, 74630, 74856, 74992, 75038 };
 
-void make_decode_tables(long scaleval)
+void make_decode_tables_const(void)
 {
-  int i,j,k,kr,divv;
-  real *table,*costab;
-  if(ready)
-	  return;
-  
-  for(i=0;i<5;i++)
-  {
-    kr=0x10>>i; divv=0x40>>i;
-    costab = pnts[i];
-    for(k=0;k<kr;k++)
-      costab[k] = 1.0 / (2.0 * cos(M_PI * ((double) k * 2.0 + 1.0) / (double) divv));
-  }
+	int i,k,kr,divv;
+	real *costab;
+		
+	for(i=0;i<5;i++)
+	{
+		kr=0x10>>i; divv=0x40>>i;
+		costab = pnts[i];
+		for(k=0;k<kr;k++)
+			costab[k] = 1.0 / (2.0 * cos(M_PI * ((double) k * 2.0 + 1.0) / (double) divv));
+	}
 
-  table = decwin;
+}
+
+void make_decode_tables_scale(struct mpstr *mp, long scaleval)
+{
+  int i,j;
+  real *table;
+  
+  table = mp->decwin;
   scaleval = -scaleval;
   for(i=0,j=0;i<256;i++,j++,table+=32)
   {
-    if(table < decwin+512+16)
+    if(table < (mp->decwin)+512+16)
       table[16] = table[0] = (double) intwinbase[j] / 65536.0 * (double) scaleval;
     if(i % 32 == 31)
       table -= 1023;
@@ -70,14 +69,13 @@
 
   for( /* i=256 */ ;i<512;i++,j--,table+=32)
   {
-    if(table < decwin+512+16)
+    if(table < (mp->decwin)+512+16)
       table[16] = table[0] = (double) intwinbase[j] / 65536.0 * (double) scaleval;
     if(i % 32 == 31)
       table -= 1023;
     if(i % 64 == 63)
       scaleval = - scaleval;
   }
-  ready=1;
 }
 
 




More information about the svn-commits mailing list