home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Dev / lame_src / mpglib / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-01  |  6.4 KB  |  270 lines

  1. #ifdef HAVEMPGLIB
  2.  
  3. #include "mpg123.h"
  4. #include "mpglib.h"
  5.  
  6. #ifdef PARENT_IS_SLASH
  7. #include "/lame.h"
  8. #include "/util.h"
  9. #include "/VbrTag.h"
  10. #else
  11. #include "../lame.h"
  12. #include "../util.h"
  13. #include "../VbrTag.h"
  14. #endif 
  15.  
  16. #include <stdlib.h>
  17.  
  18. static char buf[16384];
  19. #define FSIZE 8192  
  20. static char out[FSIZE];
  21. struct mpstr mp;
  22. plotting_data *mpg123_pinfo=NULL;
  23.  
  24.  
  25. int is_syncword(char *header)
  26. {
  27.  
  28. /*
  29. unsigned int s0,s1;
  30. s0 = (unsigned char) header[0];
  31. s1 = (unsigned char) header[1] ;
  32. printf(" syncword:  %2X   %2X   \n ",s0, s1);
  33. */
  34.  
  35.   int mpeg1=((int) ( header[0] == (char) 0xFF)) &&
  36.     ((int) ( (header[1] & (char) 0xF0) == (char) 0xF0));
  37.   
  38.   int mpeg25=((int) ( header[0] == (char) 0xFF)) &&
  39.     ((int) ( (header[1] & (char) 0xF0) == (char) 0xE0));
  40.   
  41.   return (mpeg1 || mpeg25);
  42.  
  43.  
  44. }
  45.  
  46.  
  47. int lame_decode_initfile(FILE *fd, mp3data_struct *mp3data)
  48. {
  49.   extern int tabsel_123[2][3][16];
  50.   VBRTAGDATA pTagData;
  51.   int ret,size,framesize;
  52.   unsigned long num_frames=0;
  53.   size_t len;
  54.   int xing_header;
  55.  
  56.  
  57.   InitMP3(&mp);
  58.   memset(buf, 0, sizeof(buf));
  59.  
  60.  
  61.   /* skip RIFF type proprietary headers  */
  62.   /* look for sync word  FFF */
  63.   while (!is_syncword(buf)) {
  64.     buf[0]=buf[1]; 
  65.     if (fread(&buf[1],1,1,fd) == 0) return -1;  /* failed */
  66.   }
  67.   
  68.   /* read the rest of header and enough bytes to check for Xing header */
  69.   len = fread(&buf[2],1,46,fd);
  70.   if (len ==0 ) return -1;
  71.   len +=2;
  72.   
  73.   /* check for Xing header */
  74.   xing_header = GetVbrTag(&pTagData,(unsigned char*)buf);
  75.   if (xing_header) {
  76.     num_frames=pTagData.frames;
  77.  
  78.     /* look for next sync word in buffer*/
  79.     buf[1]=0;
  80.     while (!is_syncword(buf)) {
  81.       buf[0]=buf[1]; 
  82.       if (fread(&buf[1],1,1,fd) == 0) return -1;  /* fread failed */
  83.     }
  84.     /* read the rest of header */
  85.     len = fread(&buf[2],1,2,fd);
  86.     if (len ==0 ) return -1;
  87.     len +=2;
  88.     
  89.  
  90.   } else {
  91.     /* rewind file back what we read looking for Xing headers */
  92.     if (fseek(fd, -44, SEEK_CUR) != 0) {
  93.       /* backwards fseek failed.  input is probably a pipe */
  94.     } else {
  95.       len=4;
  96.     }
  97.   }
  98.   
  99.   size=0;
  100.   ret = decodeMP3(&mp,buf,(int)len,out,FSIZE,&size);
  101.   if (ret==MP3_OK && size>0 && !xing_header) {
  102.     fprintf(stderr,"Oops: first frame of mpglib output will be lost \n");
  103.   }
  104.   
  105.   if (ret==MP3_ERR) 
  106.     return -1;
  107.   
  108.   if (!mp.header_parsed) 
  109.     return -1;
  110.  
  111.   mp3data->stereo = mp.fr.stereo;
  112.   mp3data->samplerate = freqs[mp.fr.sampling_frequency];
  113.   mp3data->bitrate = tabsel_123[mp.fr.lsf][mp.fr.lay-1][mp.fr.bitrate_index];
  114.   mp3data->nsamp=MAX_U_32_NUM;
  115.  
  116.   framesize = (mp.fr.lsf == 0) ? 1152 : 576;
  117.   if (xing_header && num_frames) {
  118.     mp3data->nsamp=framesize * num_frames;
  119.   }
  120.  
  121.   /*
  122.   printf("ret = %i NEED_MORE=%i \n",ret,MP3_NEED_MORE);
  123.   printf("stereo = %i \n",mp.fr.stereo);
  124.   printf("samp = %i  \n",(int)freqs[mp.fr.sampling_frequency]);
  125.   printf("framesize = %i  \n",framesize);
  126.   printf("num frames = %i  \n",(int)num_frames);
  127.   printf("num samp = %i  \n",(int)*num_samples);
  128.   */
  129.   return 0;
  130. }
  131.  
  132.  
  133. int lame_decode_init(void)
  134. {
  135.   InitMP3(&mp);
  136.   memset(buf, 0, sizeof(buf));
  137.   return 0;
  138. }
  139.  
  140.  
  141. /*
  142. For lame_decode_fromfile:  return code
  143.   -1     error
  144.    0     ok, but need more data before outputing any samples
  145.    n     number of samples output.  either 576 or 1152 depending on MP3 file.
  146. */
  147. int lame_decode_fromfile(FILE *fd, short pcm_l[], short pcm_r[],mp3data_struct *mp3data)
  148. {
  149.   int size,stereo;
  150.   int outsize=0,j,i,ret;
  151.   size_t len;
  152.  
  153.   size=0;
  154.   len = fread(buf,1,64,fd);
  155.   if (len ==0 ) return 0;
  156.   ret = decodeMP3(&mp,buf,(int)len,out,FSIZE,&size);
  157.  
  158.   /* read more until we get a valid output frame */
  159.   while((ret == MP3_NEED_MORE) || !size) {
  160.     len = fread(buf,1,100,fd);
  161.     if (len ==0 ) return -1;
  162.     ret = decodeMP3(&mp,buf,(int)len,out,FSIZE,&size);
  163.     /* if (ret ==MP3_ERR) return -1;  lets ignore errors and keep reading... */
  164.     /*
  165.     printf("ret = %i size= %i  %i   %i  %i \n",ret,size,
  166.        MP3_NEED_MORE,MP3_ERR,MP3_OK); 
  167.     */
  168.   }
  169.  
  170.   stereo=mp.fr.stereo;
  171.  
  172.   if (ret == MP3_OK) 
  173.   {
  174.     mp3data->stereo = mp.fr.stereo;
  175.     mp3data->samplerate = freqs[mp.fr.sampling_frequency];
  176.     /* bitrate formula works for free bitrate also */
  177.     mp3data->bitrate = .5 + 8*(4+mp.fsizeold)*freqs[mp.fr.sampling_frequency]/
  178.       (1000.0*576*(2-mp.fr.lsf));
  179.     /*    write(1,out,size); */
  180.     outsize = size/(2*(stereo));
  181.     if ((outsize!=576) && (outsize!=1152)) {
  182.       fprintf(stderr,"Oops: mpg123 returned more than one frame!  Cant handle this... \n");
  183.     }
  184.  
  185.     for (j=0; j<stereo; j++)
  186.       for (i=0; i<outsize; i++) 
  187.     if (j==0) pcm_l[i] = ((short *) out)[mp.fr.stereo*i+j];
  188.     else pcm_r[i] = ((short *) out)[mp.fr.stereo*i+j];
  189.  
  190.   }
  191.   if (ret==MP3_ERR) return -1;
  192.   else return outsize;
  193. }
  194.  
  195.  
  196.  
  197.  
  198. /*
  199. For lame_decode:  return code
  200.   -1     error
  201.    0     ok, but need more data before outputing any samples
  202.    n     number of samples output.  either 576 or 1152 depending on MP3 file.
  203. */
  204. int lame_decode1(char *buffer,int len,short pcm_l[],short pcm_r[])
  205. {
  206.   int size;
  207.   int outsize=0,j,i,ret;
  208.  
  209.  
  210.   ret = decodeMP3(&mp,buffer,len,out,FSIZE,&size);
  211.   if (ret==MP3_ERR) return -1;
  212.   
  213.   if (ret==MP3_OK) {
  214.     outsize = size/(2*mp.fr.stereo);
  215.     
  216.     for (j=0; j<mp.fr.stereo; j++)
  217.       for (i=0; i<outsize; i++) 
  218.     if (j==0) pcm_l[i] = ((short *) out)[mp.fr.stereo*i+j];
  219.     else pcm_r[i] = ((short *) out)[mp.fr.stereo*i+j];
  220.   }
  221.   if (ret==MP3_NEED_MORE) 
  222.     outsize=0;
  223.   
  224.   /*
  225.   printf("ok, more, err:  %i %i %i  \n",MP3_OK, MP3_NEED_MORE, MP3_ERR);
  226.   printf("ret = %i out=%i \n",ret,totsize);
  227.   */
  228.   return outsize;
  229. }
  230.  
  231.  
  232.  
  233.  
  234. /*
  235. For lame_decode:  return code
  236.   -1     error
  237.    0     ok, but need more data before outputing any samples
  238.    n     number of samples output.  a multiple of 576 or 1152 depending on MP3 file.
  239. */
  240. int lame_decode(char *buffer,int len,short pcm_l[],short pcm_r[])
  241. {
  242.   int size,totsize=0;
  243.   int outsize=0,j,i,ret;
  244.  
  245.   do {
  246.     ret = decodeMP3(&mp,buffer,len,out,FSIZE,&size);
  247.     if (ret==MP3_ERR) return -1;
  248.  
  249.     if (ret==MP3_OK) {
  250.       outsize = size/(2*mp.fr.stereo);
  251.       
  252.       for (j=0; j<mp.fr.stereo; j++)
  253.     for (i=0; i<outsize; i++) 
  254.       if (j==0) pcm_l[totsize + i] = ((short *) out)[mp.fr.stereo*i+j];
  255.       else pcm_r[totsize + i] = ((short *) out)[mp.fr.stereo*i+j];
  256.  
  257.       totsize += outsize;
  258.     }
  259.     len=0;  /* future calls to decodeMP3 are just to flush buffers */
  260.   } while (ret!=MP3_NEED_MORE);
  261.   /*
  262.   printf("ok, more, err:  %i %i %i  \n",MP3_OK, MP3_NEED_MORE, MP3_ERR);
  263.   printf("ret = %i out=%i \n",ret,totsize);
  264.   */
  265.   return totsize;
  266. }
  267.  
  268. #endif /* HAVEMPGLIB */
  269.  
  270.