shithub: sox

Download patch

ref: 58e2503190a598aa2d74f633cd90f295a544a089
parent: bc3e181d1f2a0f35f001745e02b28911b3f4035d
author: jching <jching>
date: Sat Apr 6 03:36:43 EST 2002

Clip sample to prevent overflow plus other cleanups.

--- a/src/mp3.c
+++ b/src/mp3.c
@@ -26,7 +26,7 @@
 #define MIN(s1,s2) ((s1)<(s2)?(s1):(s2))
 #endif
 
-#define INPUT_BUFFER_SIZE	(5*8192)
+#define INPUT_BUFFER_SIZE	(5*ST_BUFSIZ)
 
 /* Private data */
 struct mp3priv {
@@ -35,13 +35,13 @@
 	struct mad_frame	*Frame;
 	struct mad_synth	*Synth;
 	mad_timer_t		*Timer;
-        unsigned char		*InputBuffer;
-        st_ssize_t              cursamp;
+	unsigned char		*InputBuffer;
+	st_ssize_t		cursamp;
 	unsigned long		FrameCount;
-        int                     eof;
+	int			eof;
 #endif /*HAVE_LIBMAD*/
 #if defined(HAVE_LAME)
-        lame_global_flags       *gfp;
+	lame_global_flags	*gfp;
 #endif /*HAVE_LAME*/
 };
 
@@ -55,9 +55,8 @@
 
 int tagtype(const unsigned char *data, int length)
 {
-  if (length >= 3 &&
-      data[0] == 'T' && data[1] == 'A' && data[2] == 'G')
-    return 128; /* ID3V1 */
+  if (length >= 3 && data[0] == 'T' && data[1] == 'A' && data[2] == 'G')
+	return 128; /* ID3V1 */
 
   if (length >= 10 &&
       (data[0] == 'I' && data[1] == 'D' && data[2] == '3') &&
@@ -90,6 +89,7 @@
 	p->Frame=malloc(sizeof(struct mad_frame));
 	if (p->Frame == NULL){
 	  st_fail_errno(ft, ST_ENOMEM, "Could not allocate memory");
+	  free(p->Stream);
 	  return ST_EOF;
 	}
 	
@@ -96,6 +96,8 @@
 	p->Synth=malloc(sizeof(struct mad_synth));
 	if (p->Synth == NULL){
 	  st_fail_errno(ft, ST_ENOMEM, "Could not allocate memory");
+	  free(p->Stream);
+	  free(p->Frame);
 	  return ST_EOF;
 	}
 	
@@ -102,6 +104,9 @@
 	p->Timer=malloc(sizeof(mad_timer_t));
 	if (p->Timer == NULL){
 	  st_fail_errno(ft, ST_ENOMEM, "Could not allocate memory");
+	  free(p->Stream);
+	  free(p->Frame);
+	  free(p->Synth);
 	  return ST_EOF;
 	}
 	
@@ -108,6 +113,10 @@
 	p->InputBuffer=malloc(INPUT_BUFFER_SIZE);
 	if (p->InputBuffer == NULL){
 	  st_fail_errno(ft, ST_ENOMEM, "Could not allocate memory");
+	  free(p->Stream);
+	  free(p->Frame);
+	  free(p->Synth);
+	  free(p->Timer);
 	  return ST_EOF;
 	}
 	
@@ -182,6 +191,7 @@
 {
   struct mp3priv *p = (struct mp3priv *) ft->priv;
   st_ssize_t donow,i,done=0;
+  mad_fixed_t sample;
   int chan;
 
   do{
@@ -189,8 +199,12 @@
     i=0;
     while(i<donow){
       for(chan=0;chan<ft->info.channels;chan++){
-
-	*buf++=(st_sample_t)p->Synth->pcm.samples[chan][p->cursamp]<<(8*sizeof(st_sample_t)-1-MAD_F_FRACBITS);
+	sample=p->Synth->pcm.samples[chan][p->cursamp];
+	if (sample < -MAD_F_ONE)
+	  sample=-MAD_F_ONE;
+	else if (sample >= MAD_F_ONE)
+	  sample=MAD_F_ONE-1;
+	*buf++=(st_sample_t)(sample<<(32-1-MAD_F_FRACBITS));
 	i++;
       }
       p->cursamp++;
@@ -238,20 +252,23 @@
 	  int tagsize;
 	  if ( (tagsize=tagtype(p->Stream->this_frame, p->Stream->bufend - p->Stream->this_frame)) == 0){
 	    if (!p->eof)
-	      st_warn("recoverable frame level error (%s)\n");
+	      st_warn("recoverable frame level error (%s).\n",
+		      mad_stream_errorstr(p->Stream));
 	  }
 	  else mad_stream_skip(p->Stream,tagsize);
 	  continue;
       	}
       else
-	if(p->Stream->error==MAD_ERROR_BUFLEN)
-	  continue;
-	else
-	  {
-	    st_warn("unrecoverable frame level error (%s).\n",
-		    mad_stream_errorstr(p->Stream));
-	    return done;
-	  }
+	{
+	  if(p->Stream->error==MAD_ERROR_BUFLEN)
+	    continue;
+	  else
+	    {
+	      st_warn("unrecoverable frame level error (%s).\n",
+		      mad_stream_errorstr(p->Stream));
+	      return done;
+	    }
+	}
     }
     p->FrameCount++;
     mad_timer_add(p->Timer,p->Frame->header.duration);