home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Sound / LAME / src / mpglib / main.c < prev    next >
C/C++ Source or Header  |  2000-08-06  |  8KB  |  314 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 check_aid(char *header) {
  26.   int aid_header =
  27.     (header[0]=='A' && header[1]=='i' && header[2]=='D'
  28.      && header[3]== (char) 1);
  29.   return aid_header;
  30. }
  31.  
  32.  
  33. int is_syncword(char *header)
  34. {
  35.  
  36. /*
  37. unsigned int s0,s1;
  38. s0 = (unsigned char) header[0];
  39. s1 = (unsigned char) header[1] ;
  40. printf(" syncword:  %2X   %2X   \n ",s0, s1);
  41. */
  42.  
  43.   int mpeg1=((int) ( header[0] == (char) 0xFF)) &&
  44.     ((int) ( (header[1] & (char) 0xF0) == (char) 0xF0));
  45.   
  46.   int mpeg25=((int) ( header[0] == (char) 0xFF)) &&
  47.     ((int) ( (header[1] & (char) 0xF0) == (char) 0xE0));
  48.   
  49.   return (mpeg1 || mpeg25);
  50.  
  51.  
  52. }
  53.  
  54.  
  55. int lame_decode_initfile(FILE *fd, mp3data_struct *mp3data)
  56. {
  57.   extern int tabsel_123[2][3][16];
  58.   VBRTAGDATA pTagData;
  59.   int ret,size,framesize;
  60.   unsigned long num_frames=0;
  61.   size_t len,len2;
  62.   int xing_header,aid_header;
  63.  
  64.  
  65.   InitMP3(&mp);
  66.  
  67.  
  68.   memset(buf, 0, sizeof(buf));
  69.  
  70.  
  71.   len=4;
  72.   if (fread(&buf,1,len,fd) == 0) return -1;  /* failed */
  73.   aid_header = check_aid(buf);
  74.   if (aid_header) {
  75.     if (fread(&buf,1,2,fd) == 0) return -1;  /* failed */
  76.     aid_header = (unsigned char) buf[0] + 256*(unsigned char)buf[1];
  77.     fprintf(stderr,"Album ID found.  length=%i \n",aid_header);
  78.     /* skip rest of AID, except for 6 bytes we have already read */
  79.     fskip(fd,aid_header-6,1);
  80.  
  81.     /* read 2 more bytes to set up buffer for MP3 header check */
  82.     if (fread(&buf,1,2,fd) == 0) return -1;  /* failed */
  83.     len =2;
  84.   }
  85.  
  86.  
  87.  
  88.   /* look for sync word  FFF */
  89.   if (len<2) return -1;
  90.   while (!is_syncword(buf)) {
  91.     int i;
  92.     for (i=0; i<len-1; i++)
  93.       buf[i]=buf[i+1]; 
  94.     if (fread(&buf[len-1],1,1,fd) == 0) return -1;  /* failed */
  95.   }
  96.  
  97.  
  98.   
  99.   /* read the rest of header and enough bytes to check for Xing header */
  100.   len2 = fread(&buf[len],1,48-len,fd);
  101.   if (len2 ==0 ) return -1;
  102.   len +=len2;
  103.  
  104.  
  105.   /* check for Xing header */
  106.   xing_header = GetVbrTag(&pTagData,(unsigned char*)buf);
  107.   if (xing_header) {
  108.     num_frames=pTagData.frames;
  109.  
  110.     /* look for next sync word in buffer*/
  111.     len=2;
  112.     buf[0]=buf[1]=0;
  113.     while (!is_syncword(buf)) {
  114.       buf[0]=buf[1]; 
  115.       if (fread(&buf[1],1,1,fd) == 0) return -1;  /* fread failed */
  116.     }
  117.     /* read the rest of header */
  118.     len2 = fread(&buf[2],1,2,fd);
  119.     if (len2 ==0 ) return -1;
  120.     len +=len2;
  121.  
  122.  
  123.   } else {
  124.     /* rewind file back what we read looking for Xing headers */
  125.     if (fseek(fd, -44, SEEK_CUR) != 0) {
  126.       /* backwards fseek failed.  input is probably a pipe */
  127.     } else {
  128.       len=4;
  129.     }
  130.   }
  131.  
  132.   /* parse the buffer that was read looking for Xing header above */  
  133.   size=0;
  134.   ret = decodeMP3(&mp,buf,(int)len,out,FSIZE,&size);
  135.   if (ret==MP3_ERR) 
  136.     return -1;
  137.   if (ret==MP3_OK && size>0 && !xing_header) 
  138.     fprintf(stderr,"Oops: first frame of mpglib output will be lost \n"); 
  139.  
  140.   
  141.  
  142.   /* repeat until we decode a valid mp3 header */
  143.   while (!mp.header_parsed) {
  144.     len = fread(buf,1,2,fd);
  145.     if (len ==0 ) return -1;
  146.     ret = decodeMP3(&mp,buf,(int)len,out,FSIZE,&size);
  147.     if (ret==MP3_ERR) 
  148.       return -1;
  149.     if (ret==MP3_OK && !mp.header_parsed)
  150.       fprintf(stderr,"Oops: strange error in lame_decode_initfile \n");
  151.   }
  152.  
  153.  
  154.  
  155.   mp3data->stereo = mp.fr.stereo;
  156.   mp3data->samplerate = freqs[mp.fr.sampling_frequency];
  157.   mp3data->bitrate = tabsel_123[mp.fr.lsf][mp.fr.lay-1][mp.fr.bitrate_index];
  158.   mp3data->nsamp=MAX_U_32_NUM;
  159.  
  160.   framesize = (mp.fr.lsf == 0) ? 1152 : 576;
  161.   if (xing_header && num_frames) {
  162.     mp3data->nsamp=framesize * num_frames;
  163.   }
  164.  
  165.   /*
  166.   printf("ret = %i NEED_MORE=%i \n",ret,MP3_NEED_MORE);
  167.   printf("stereo = %i \n",mp.fr.stereo);
  168.   printf("samp = %i  \n",(int)freqs[mp.fr.sampling_frequency]);
  169.   printf("framesize = %i  \n",framesize);
  170.   printf("num frames = %i  \n",(int)num_frames);
  171.   printf("num samp = %i  \n",(int)*num_samples);
  172.   */
  173.   return 0;
  174. }
  175.  
  176.  
  177. int lame_decode_init(void)
  178. {
  179.   InitMP3(&mp);
  180.   memset(buf, 0, sizeof(buf));
  181.   return 0;
  182. }
  183.  
  184.  
  185. /*
  186. For lame_decode_fromfile:  return code
  187.   -1     error
  188.    0     ok, but need more data before outputing any samples
  189.    n     number of samples output.  either 576 or 1152 depending on MP3 file.
  190. */
  191. int lame_decode_fromfile(FILE *fd, short pcm_l[], short pcm_r[],mp3data_struct *mp3data)
  192. {
  193.   int size,stereo;
  194.   int outsize=0,j,i,ret;
  195.   size_t len;
  196.  
  197.   size=0;
  198.   len = fread(buf,1,64,fd);
  199.   if (len ==0 ) return 0;
  200.   ret = decodeMP3(&mp,buf,(int)len,out,FSIZE,&size);
  201.  
  202.   /* read more until we get a valid output frame */
  203.   while((ret == MP3_NEED_MORE) || !size) {
  204.     len = fread(buf,1,100,fd);
  205.     if (len ==0 ) return -1;
  206.     ret = decodeMP3(&mp,buf,(int)len,out,FSIZE,&size);
  207.     /* if (ret ==MP3_ERR) return -1;  lets ignore errors and keep reading... */
  208.     /*
  209.     printf("ret = %i size= %i  %i   %i  %i \n",ret,size,
  210.        MP3_NEED_MORE,MP3_ERR,MP3_OK); 
  211.     */
  212.   }
  213.  
  214.   stereo=mp.fr.stereo;
  215.  
  216.   if (ret == MP3_OK) 
  217.   {
  218.     mp3data->stereo = mp.fr.stereo;
  219.     mp3data->samplerate = freqs[mp.fr.sampling_frequency];
  220.     /* bitrate formula works for free bitrate also */
  221.     mp3data->bitrate = .5 + 8*(4+mp.fsizeold)*freqs[mp.fr.sampling_frequency]/
  222.       (1000.0*576*(2-mp.fr.lsf));
  223.     /*    write(1,out,size); */
  224.     outsize = size/(2*(stereo));
  225.     if ((outsize!=576) && (outsize!=1152)) {
  226.       fprintf(stderr,"Oops: mpg123 returned more than one frame!  Cant handle this... \n");
  227.     }
  228.  
  229.     for (j=0; j<stereo; j++)
  230.       for (i=0; i<outsize; i++) 
  231.     if (j==0) pcm_l[i] = ((short *) out)[mp.fr.stereo*i+j];
  232.     else pcm_r[i] = ((short *) out)[mp.fr.stereo*i+j];
  233.  
  234.   }
  235.   if (ret==MP3_ERR) return -1;
  236.   else return outsize;
  237. }
  238.  
  239.  
  240.  
  241.  
  242. /*
  243. For lame_decode:  return code
  244.   -1     error
  245.    0     ok, but need more data before outputing any samples
  246.    n     number of samples output.  either 576 or 1152 depending on MP3 file.
  247. */
  248. int lame_decode1(char *buffer,int len,short pcm_l[],short pcm_r[])
  249. {
  250.   int size;
  251.   int outsize=0,j,i,ret;
  252.  
  253.  
  254.   ret = decodeMP3(&mp,buffer,len,out,FSIZE,&size);
  255.   if (ret==MP3_ERR) return -1;
  256.   
  257.   if (ret==MP3_OK) {
  258.     outsize = size/(2*mp.fr.stereo);
  259.     
  260.     for (j=0; j<mp.fr.stereo; j++)
  261.       for (i=0; i<outsize; i++) 
  262.     if (j==0) pcm_l[i] = ((short *) out)[mp.fr.stereo*i+j];
  263.     else pcm_r[i] = ((short *) out)[mp.fr.stereo*i+j];
  264.   }
  265.   if (ret==MP3_NEED_MORE) 
  266.     outsize=0;
  267.   
  268.   /*
  269.   printf("ok, more, err:  %i %i %i  \n",MP3_OK, MP3_NEED_MORE, MP3_ERR);
  270.   printf("ret = %i out=%i \n",ret,totsize);
  271.   */
  272.   return outsize;
  273. }
  274.  
  275.  
  276.  
  277.  
  278. /*
  279. For lame_decode:  return code
  280.   -1     error
  281.    0     ok, but need more data before outputing any samples
  282.    n     number of samples output.  a multiple of 576 or 1152 depending on MP3 file.
  283. */
  284. int lame_decode(char *buffer,int len,short pcm_l[],short pcm_r[])
  285. {
  286.   int size,totsize=0;
  287.   int outsize=0,j,i,ret;
  288.  
  289.   do {
  290.     ret = decodeMP3(&mp,buffer,len,out,FSIZE,&size);
  291.     if (ret==MP3_ERR) return -1;
  292.  
  293.     if (ret==MP3_OK) {
  294.       outsize = size/(2*mp.fr.stereo);
  295.       
  296.       for (j=0; j<mp.fr.stereo; j++)
  297.     for (i=0; i<outsize; i++) 
  298.       if (j==0) pcm_l[totsize + i] = ((short *) out)[mp.fr.stereo*i+j];
  299.       else pcm_r[totsize + i] = ((short *) out)[mp.fr.stereo*i+j];
  300.  
  301.       totsize += outsize;
  302.     }
  303.     len=0;  /* future calls to decodeMP3 are just to flush buffers */
  304.   } while (ret!=MP3_NEED_MORE);
  305.   /*
  306.   printf("ok, more, err:  %i %i %i  \n",MP3_OK, MP3_NEED_MORE, MP3_ERR);
  307.   printf("ret = %i out=%i \n",ret,totsize);
  308.   */
  309.   return totsize;
  310. }
  311.  
  312. #endif /* HAVEMPGLIB */
  313.  
  314.