home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Sound / LAME / src / mpglib / interface.c < prev    next >
C/C++ Source or Header  |  2000-06-18  |  8KB  |  418 lines

  1. #ifdef HAVEMPGLIB
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #include "mpg123.h"
  6. #include "mpglib.h"
  7.  
  8.  
  9. /* Global mp .. it's a hack */
  10. struct mpstr *gmp;
  11.  
  12.  
  13. BOOL InitMP3(struct mpstr *mp) 
  14. {
  15.     memset(mp,0,sizeof(struct mpstr));
  16.  
  17.     mp->framesize = 0;
  18.     mp->header_parsed=0;
  19.     mp->side_parsed=0;
  20.     mp->data_parsed=0;
  21.     mp->free_format=0;
  22.     mp->old_free_format=0;
  23.     mp->ssize = 0;
  24.     mp->dsize=0;
  25.     mp->fsizeold = -1;
  26.     mp->bsize = 0;
  27.     mp->head = mp->tail = NULL;
  28.     mp->fr.single = -1;
  29.     mp->bsnum = 0;
  30.     wordpointer = mp->bsspace[mp->bsnum] + 512;
  31.     mp->synth_bo = 1;
  32.  
  33.     make_decode_tables(32767);
  34.     init_layer3(SBLIMIT);
  35.  
  36.     return !0;
  37. }
  38.  
  39. void ExitMP3(struct mpstr *mp)
  40. {
  41.     struct buf *b,*bn;
  42.     
  43.     b = mp->tail;
  44.     while(b) {
  45.         free(b->pnt);
  46.         bn = b->next;
  47.         free(b);
  48.         b = bn;
  49.     }
  50. }
  51.  
  52. static struct buf *addbuf(struct mpstr *mp,char *buf,int size)
  53. {
  54.     struct buf *nbuf;
  55.  
  56.     nbuf = (struct buf*) malloc( sizeof(struct buf) );
  57.     if(!nbuf) {
  58.         fprintf(stderr,"Out of memory!\n");
  59.         return NULL;
  60.     }
  61.     nbuf->pnt = (unsigned char*) malloc((size_t)size);
  62.     if(!nbuf->pnt) {
  63.         free(nbuf);
  64.         return NULL;
  65.     }
  66.     nbuf->size = size;
  67.     memcpy(nbuf->pnt,buf,(size_t)size);
  68.     nbuf->next = NULL;
  69.     nbuf->prev = mp->head;
  70.     nbuf->pos = 0;
  71.  
  72.     if(!mp->tail) {
  73.         mp->tail = nbuf;
  74.     }
  75.     else {
  76.       mp->head->next = nbuf;
  77.     }
  78.  
  79.     mp->head = nbuf;
  80.     mp->bsize += size;
  81.  
  82.     return nbuf;
  83. }
  84.  
  85. static void remove_buf(struct mpstr *mp)
  86. {
  87.   struct buf *buf = mp->tail;
  88.   
  89.   mp->tail = buf->next;
  90.   if(mp->tail)
  91.     mp->tail->prev = NULL;
  92.   else {
  93.     mp->tail = mp->head = NULL;
  94.   }
  95.   
  96.   free(buf->pnt);
  97.   free(buf);
  98.  
  99. }
  100.  
  101. static int read_buf_byte(struct mpstr *mp)
  102. {
  103.     unsigned int b;
  104.  
  105.     int pos;
  106.  
  107.     pos = mp->tail->pos;
  108.     while(pos >= mp->tail->size) {
  109.             remove_buf(mp);
  110.         pos = mp->tail->pos;
  111.         if(!mp->tail) {
  112.             fprintf(stderr,"Fatal error!\n");
  113.             exit(1);
  114.         }
  115.     }
  116.  
  117.     b = mp->tail->pnt[pos];
  118.     mp->bsize--;
  119.     mp->tail->pos++;
  120.     
  121.  
  122.     return b;
  123. }
  124.  
  125.  
  126.  
  127. static void read_head(struct mpstr *mp)
  128. {
  129.     unsigned long head;
  130.  
  131.     head = read_buf_byte(mp);
  132.     head <<= 8;
  133.     head |= read_buf_byte(mp);
  134.     head <<= 8;
  135.     head |= read_buf_byte(mp);
  136.     head <<= 8;
  137.     head |= read_buf_byte(mp);
  138.  
  139.     mp->header = head;
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. void copy_mp(struct mpstr *mp,int size,unsigned char *ptr) 
  148. {
  149.   int len = 0;
  150.   while(len < size) {
  151.     int nlen;
  152.     int blen = mp->tail->size - mp->tail->pos;
  153.     if( (size - len) <= blen) {
  154.       nlen = size-len;
  155.     }
  156.     else {
  157.       nlen = blen;
  158.     }
  159.     memcpy(ptr+len,mp->tail->pnt+mp->tail->pos,(size_t)nlen);
  160.     len += nlen;
  161.     mp->tail->pos += nlen;
  162.     mp->bsize -= nlen;
  163.     if(mp->tail->pos == mp->tail->size) {
  164.       remove_buf(mp);
  165.     }
  166.   }
  167. }
  168.  
  169.  
  170.  
  171. int sync_buffer(struct mpstr *mp,int free_match) 
  172. {
  173.   /* traverse mp structure without modifing pointers, looking
  174.    * for a frame valid header.
  175.    * if free_format, valid header must also have the same
  176.    * samplerate.   
  177.    * return number of bytes in mp, before the header
  178.    * return -1 if header is not found
  179.    */
  180.   unsigned int b[4]={0,0,0,0};
  181.   int i,h,pos;
  182.   struct buf *buf=mp->tail;
  183.  
  184.   pos = buf->pos;
  185.   for (i=0; i<mp->bsize; i++) {
  186.     /* get 4 bytes */
  187.     
  188.     b[0]=b[1]; b[1]=b[2]; b[2]=b[3];
  189.     while(pos >= buf->size) {
  190.       buf  = buf->next;
  191.       pos = buf->pos;
  192.       if(!buf) {
  193.     return -1;
  194.     /* not enough data to read 4 bytes */
  195.       }
  196.     }
  197.     b[3] = buf->pnt[pos];
  198.     ++pos;
  199.  
  200.     if (i>=3) {
  201.     unsigned long head;
  202.  
  203.     head = b[0];
  204.     head <<= 8;
  205.     head |= b[1];
  206.     head <<= 8;
  207.     head |= b[2];
  208.     head <<= 8;
  209.     head |= b[3];
  210.     h = head_check(head);
  211.  
  212.     if (h && free_match) {
  213.       /* just to be even more thorough, match the sample rate */
  214.       struct frame *fr = &mp->fr;
  215.       int mode,stereo,sampling_frequency,mpeg25,lsf;
  216.  
  217.       if( head & (1<<20) ) {
  218.         lsf = (head & (1<<19)) ? 0x0 : 0x1;
  219.         mpeg25 = 0;
  220.       }
  221.       else {
  222.         lsf = 1;
  223.         mpeg25 = 1;
  224.       }
  225.  
  226.       mode      = ((head>>6)&0x3);
  227.       stereo    = (mode == MPG_MD_MONO) ? 1 : 2;
  228.  
  229.       if(mpeg25) 
  230.         sampling_frequency = 6 + ((head>>10)&0x3);
  231.       else
  232.         sampling_frequency = ((head>>10)&0x3) + (lsf*3);
  233.       h = ((stereo==fr->stereo) && (lsf==fr->lsf) && (mpeg25==fr->mpeg25) && 
  234.                  (sampling_frequency == fr->sampling_frequency));
  235.     }
  236.  
  237.     if (h) {
  238.       return i-3;
  239.     }
  240.     }
  241.   }
  242.   return -1;
  243. }
  244.  
  245.  
  246.  
  247.  
  248.  
  249. int decodeMP3(struct mpstr *mp,char *in,int isize,char *out,
  250.         int osize,int *done)
  251. {
  252.     int i,iret,bits,bytes;
  253.     gmp = mp;
  254.  
  255.     if(osize < 4608) {
  256.         fprintf(stderr,"To less out space\n");
  257.         return MP3_ERR;
  258.     }
  259.  
  260.     if(in) {
  261.         if(addbuf(mp,in,isize) == NULL) {
  262.             return MP3_ERR;
  263.         }
  264.     }
  265.  
  266.  
  267.     /* First decode header */
  268.     if(!mp->header_parsed) {
  269.  
  270.       if (mp->fsizeold==-1) 
  271.         /* first call.   sync with anything */
  272.         bytes=sync_buffer(mp,0);
  273.       else
  274.         /* match channels, samplerate, etc, when syncing */
  275.         bytes=sync_buffer(mp,1);
  276.  
  277.         if (bytes<0) return MP3_NEED_MORE;
  278.         if (bytes>0) {
  279.           /* bitstream problem, but we are now resynced 
  280.            * should try to buffer previous data in case new
  281.            * frame has nonzero main_data_begin, but we need
  282.            * to make sure we do not overflow buffer
  283.           */
  284.           int size;
  285.           fprintf(stderr,"bitstream problem: resyncing...\n");
  286.           mp->old_free_format=0;
  287.  
  288.           /* skip some bytes, buffer the rest */
  289.           size = (int) (wordpointer - mp->bsspace[mp->bsnum]+512);
  290.           i = (size+bytes)-MAXFRAMESIZE;
  291.           for (; i>0; --i) {
  292.             --bytes;
  293.             read_buf_byte(mp);
  294.           }
  295.           copy_mp(mp,bytes,wordpointer);
  296.           mp->fsizeold += bytes;
  297.  
  298.         }
  299.  
  300.         read_head(mp);
  301.         decode_header(&mp->fr,mp->header);
  302.         mp->header_parsed=1;
  303.         mp->framesize = mp->fr.framesize;
  304.         mp->free_format = (mp->framesize==0);
  305.  
  306.         if(mp->fr.lsf)
  307.           mp->ssize = (mp->fr.stereo == 1) ? 9 : 17;
  308.         else
  309.           mp->ssize = (mp->fr.stereo == 1) ? 17 : 32;
  310.         if (mp->fr.error_protection) 
  311.           mp->ssize += 2;
  312.  
  313.         mp->bsnum = 1-mp->bsnum; /* toggle buffer */
  314.         wordpointer = mp->bsspace[mp->bsnum] + 512;
  315.         bitindex = 0;
  316.     }
  317.  
  318.     /* now decode side information */
  319.     if (!mp->side_parsed ) {
  320.                 if (mp->bsize < mp->ssize) 
  321.           return MP3_NEED_MORE;
  322.  
  323.         copy_mp(mp,mp->ssize,wordpointer);
  324.  
  325.         if(mp->fr.error_protection)
  326.           getbits(16);
  327.         bits=do_layer3_sideinfo(&mp->fr);
  328.         /* bits = actual number of bits needed to parse this frame */
  329.         /* can be negative, if all bits needed are in the reservoir */
  330.         if (bits<0) bits=0;
  331.  
  332.         /* read just as many bytes as necessary before decoding */
  333.         mp->dsize = (bits+7)/8;
  334.  
  335.         /* this will force mpglib to read entire frame before decoding */
  336.         /* mp->dsize= mp->framesize - mp->ssize;*/
  337.  
  338.         mp->side_parsed=1;
  339.     }
  340.  
  341.     /* now decode main data */
  342.     iret=MP3_NEED_MORE;
  343.     if (!mp->data_parsed) {
  344.             if(mp->dsize > mp->bsize) {
  345.           return MP3_NEED_MORE;
  346.         }
  347.         copy_mp(mp,mp->dsize,wordpointer);
  348.         *done = 0;
  349.         do_layer3(&mp->fr,(unsigned char *) out,done);
  350.         wordpointer = mp->bsspace[mp->bsnum] + 512 + mp->ssize + mp->dsize;
  351.         mp->data_parsed=1;
  352.         iret=MP3_OK;
  353.     }
  354.  
  355.  
  356.     /* remaining bits are ancillary data, or reservoir for next frame 
  357.      * If free format, scan stream looking for next frame to determine
  358.      * mp->framesize */
  359.     if (mp->free_format) {
  360.       if (mp->old_free_format) {
  361.         /* free format.  bitrate must not vary */
  362.         mp->framesize=mp->fsizeold_nopadding + (mp->fr.padding);
  363.       }else{
  364.         bytes=sync_buffer(mp,1);
  365.         if (bytes<0) return iret;
  366.         mp->framesize = bytes + mp->ssize+mp->dsize;
  367.         mp->fsizeold_nopadding= mp->framesize - mp->fr.padding;
  368.         /*
  369.         fprintf(stderr,"freeformat bitstream:  estimated bitrate=%ikbs  \n",
  370.             8*(4+mp->framesize)*freqs[mp->fr.sampling_frequency]/
  371.             (1000*576*(2-mp->fr.lsf)));
  372.         */
  373.       }
  374.     }
  375.  
  376.     /* buffer the ancillary data and reservoir for next frame */
  377.     bytes = mp->framesize-(mp->ssize+mp->dsize);
  378.     if (bytes > mp->bsize) {
  379.       return iret;
  380.     }
  381.     if (bytes>0) {
  382.       copy_mp(mp,bytes,wordpointer);
  383.       wordpointer += bytes;
  384.     }
  385.  
  386.     /* the above frame is completey parsed.  start looking for next frame */
  387.     mp->fsizeold = mp->framesize;
  388.     mp->old_free_format = mp->free_format;
  389.     mp->framesize =0;
  390.     mp->header_parsed=0;
  391.     mp->side_parsed=0;
  392.     mp->data_parsed=0;
  393.     return iret;
  394. }
  395.  
  396.     
  397. int set_pointer(long backstep)
  398. {
  399.   unsigned char *bsbufold;
  400.  
  401.   if(gmp->fsizeold < 0 && backstep > 0) {
  402.     fprintf(stderr,"Can't step back %ld!\n",backstep);
  403.     return MP3_ERR; 
  404.   }
  405.   bsbufold = gmp->bsspace[1-gmp->bsnum] + 512;
  406.   wordpointer -= backstep;
  407.   if (backstep)
  408.     memcpy(wordpointer,bsbufold+gmp->fsizeold-backstep,(size_t)backstep);
  409.   bitindex = 0;
  410.   return MP3_OK;
  411. }
  412.  
  413.  
  414.  
  415.  
  416.  
  417. #endif
  418.