interface.c

Go to the documentation of this file.
00001 /* $Id: interface.c,v 1.50 2007/07/01 20:05:52 robert Exp $ */
00002 
00003 #ifdef HAVE_CONFIG_H
00004 # include <config.h>
00005 #endif
00006 
00007 #include <stdlib.h>
00008 #include <stdio.h>
00009 
00010 #include "common.h"
00011 #include "interface.h"
00012 #include "tabinit.h"
00013 #include "layer3.h"
00014 #include "lame.h"
00015 #include "machine.h"
00016 #include "VbrTag.h"
00017 #include "decode_i386.h"
00018 
00019 #include "layer1.h"
00020 #include "layer2.h"
00021 
00022 #ifdef WITH_DMALLOC
00023 #include <dmalloc.h>
00024 #endif
00025 
00026 
00027 BOOL InitMP3( PMPSTR mp)
00028 {
00029         memset(mp,0,sizeof(MPSTR));
00030 
00031         mp->framesize = 0;
00032         mp->num_frames = 0;
00033         mp->enc_delay = -1;
00034         mp->enc_padding = -1;
00035         mp->vbr_header=0;
00036         mp->header_parsed=0;
00037         mp->side_parsed=0;
00038         mp->data_parsed=0;
00039         mp->free_format=0;
00040         mp->old_free_format=0;
00041         mp->ssize = 0;
00042         mp->dsize=0;
00043         mp->fsizeold = -1;
00044         mp->bsize = 0;
00045         mp->head = mp->tail = NULL;
00046         mp->fr.single = -1;
00047         mp->bsnum = 0;
00048         mp->wordpointer = mp->bsspace[mp->bsnum] + 512;
00049     mp->bitindex = 0;
00050         mp->synth_bo = 1;
00051         mp->sync_bitstream = 1;
00052 
00053         make_decode_tables(32767);
00054 
00055         init_layer3(SBLIMIT);
00056 
00057         init_layer2();
00058 
00059         return !0;
00060 }
00061 
00062 void ExitMP3( PMPSTR mp)
00063 {
00064         struct buf *b,*bn;
00065 
00066         b = mp->tail;
00067         while(b) {
00068                 free(b->pnt);
00069                 bn = b->next;
00070                 free(b);
00071                 b = bn;
00072         }
00073 }
00074 
00075 static struct buf *addbuf( PMPSTR mp, unsigned char *buf,int size)
00076 {
00077         struct buf *nbuf;
00078 
00079         nbuf = (struct buf*) malloc( sizeof(struct buf) );
00080         if(!nbuf) {
00081                 fprintf(stderr,"Out of memory!\n");
00082                 return NULL;
00083         }
00084         nbuf->pnt = (unsigned char*) malloc((size_t)size);
00085         if(!nbuf->pnt) {
00086                 free(nbuf);
00087                 return NULL;
00088         }
00089         nbuf->size = size;
00090         memcpy(nbuf->pnt,buf,(size_t)size);
00091         nbuf->next = NULL;
00092         nbuf->prev = mp->head;
00093         nbuf->pos = 0;
00094 
00095         if(!mp->tail) {
00096                 mp->tail = nbuf;
00097         }
00098         else {
00099           mp->head->next = nbuf;
00100         }
00101 
00102         mp->head = nbuf;
00103         mp->bsize += size;
00104 
00105         return nbuf;
00106 }
00107 
00108 void remove_buf(PMPSTR mp)
00109 {
00110   struct buf *buf = mp->tail;
00111 
00112   mp->tail = buf->next;
00113   if(mp->tail)
00114     mp->tail->prev = NULL;
00115   else {
00116     mp->tail = mp->head = NULL;
00117   }
00118 
00119   free(buf->pnt);
00120   free(buf);
00121 
00122 }
00123 
00124 static int read_buf_byte(PMPSTR mp)
00125 {
00126         unsigned int b;
00127 
00128         int pos;
00129 
00130 
00131         pos = mp->tail->pos;
00132         while(pos >= mp->tail->size) {
00133                 remove_buf(mp);
00134                 if(!mp->tail) {
00135                         fprintf(stderr,"Fatal error! tried to read past mp buffer\n");
00136                         exit(1);
00137                 }
00138                 pos = mp->tail->pos;
00139         }
00140 
00141         b = mp->tail->pnt[pos];
00142         mp->bsize--;
00143         mp->tail->pos++;
00144 
00145 
00146         return b;
00147 }
00148 
00149 
00150 
00151 static void read_head(PMPSTR mp)
00152 {
00153         unsigned long head;
00154 
00155         head = read_buf_byte(mp);
00156         head <<= 8;
00157         head |= read_buf_byte(mp);
00158         head <<= 8;
00159         head |= read_buf_byte(mp);
00160         head <<= 8;
00161         head |= read_buf_byte(mp);
00162 
00163         mp->header = head;
00164 }
00165 
00166 
00167 
00168 
00169 
00170 
00171 static void
00172 copy_mp(PMPSTR mp,int size,unsigned char *ptr)
00173 {
00174   int len = 0;
00175 
00176   while(len < size && mp->tail) {
00177     int nlen;
00178     int blen = mp->tail->size - mp->tail->pos;
00179     if( (size - len) <= blen) {
00180       nlen = size-len;
00181     }
00182     else {
00183       nlen = blen;
00184     }
00185     memcpy(ptr+len,mp->tail->pnt+mp->tail->pos,(size_t)nlen);
00186     len += nlen;
00187     mp->tail->pos += nlen;
00188     mp->bsize -= nlen;
00189     if(mp->tail->pos == mp->tail->size) {
00190       remove_buf(mp);
00191     }
00192   }
00193 }
00194 
00195 /* number of bytes needed by GetVbrTag to parse header */
00196 #define XING_HEADER_SIZE 194
00197 
00198 /* traverse mp data structure without changing it */
00199 /* (just like sync_buffer) */
00200 /* pull out Xing bytes */
00201 /* call vbr header check code from LAME */
00202 /* if we find a header, parse it and also compute the VBR header size */
00203 /* if no header, do nothing. */
00204 /* */
00205 /* bytes = number of bytes before MPEG header.  skip this many bytes */
00206 /* before starting to read */
00207 /* return value: number of bytes in VBR header, including syncword */
00208 static int
00209 check_vbr_header(PMPSTR mp,int bytes)
00210 {
00211   int i,pos;
00212   struct buf *buf=mp->tail;
00213   unsigned char xing[XING_HEADER_SIZE];
00214   VBRTAGDATA pTagData;
00215 
00216   pos = buf->pos;
00217   /* skip to valid header */
00218   for (i=0; i<bytes; ++i) {
00219     while(pos >= buf->size) {
00220       buf  = buf->next;
00221       pos = buf->pos;
00222       if(!buf)  return -1; /* fatal error */
00223     }
00224     ++pos;
00225   }
00226   /* now read header */
00227   for (i=0; i<XING_HEADER_SIZE; ++i) {
00228     while(pos >= buf->size) {
00229       buf  = buf->next;
00230       if(!buf)  return -1; /* fatal error */
00231       pos = buf->pos;
00232     }
00233     xing[i] = buf->pnt[pos];
00234     ++pos;
00235   }
00236 
00237   /* check first bytes for Xing header */
00238   mp->vbr_header = GetVbrTag(&pTagData,xing);
00239   if (mp->vbr_header) {
00240     mp->num_frames=pTagData.frames;
00241     mp->enc_delay=pTagData.enc_delay;
00242     mp->enc_padding=pTagData.enc_padding;
00243 
00244     /*fprintf(stderr,"\rmpglib: delays: %i %i \n",mp->enc_delay,mp->enc_padding); */
00245     /* fprintf(stderr,"\rmpglib: Xing VBR header dectected.  MP3 file has %i frames\n", pTagData.frames); */
00246     if ( pTagData.headersize < 1 ) return 1;
00247     return pTagData.headersize;
00248   }
00249   return 0;
00250 }
00251 
00252 
00253 
00254 
00255 
00256 
00257 
00258 static int
00259 sync_buffer(PMPSTR mp,int free_match)
00260 {
00261   /* traverse mp structure without modifing pointers, looking
00262    * for a frame valid header.
00263    * if free_format, valid header must also have the same
00264    * samplerate.
00265    * return number of bytes in mp, before the header
00266    * return -1 if header is not found
00267    */
00268   unsigned int b[4]={0,0,0,0};
00269   int i,h,pos;
00270   struct buf *buf=mp->tail;
00271   if (!buf) return -1;
00272 
00273   pos = buf->pos;
00274   for (i=0; i<mp->bsize; i++) {
00275     /* get 4 bytes */
00276 
00277     b[0]=b[1]; b[1]=b[2]; b[2]=b[3];
00278     while(pos >= buf->size) {
00279       buf  = buf->next;
00280       pos = buf->pos;
00281       if(!buf) {
00282         return -1;
00283         /* not enough data to read 4 bytes */
00284       }
00285     }
00286     b[3] = buf->pnt[pos];
00287     ++pos;
00288 
00289     if (i>=3) {
00290         struct frame *fr = &mp->fr;
00291         unsigned long head;
00292 
00293         head = b[0];
00294         head <<= 8;
00295         head |= b[1];
00296         head <<= 8;
00297         head |= b[2];
00298         head <<= 8;
00299         head |= b[3];
00300         h = head_check(head,fr->lay);
00301 
00302         if (h && free_match) {
00303           /* just to be even more thorough, match the sample rate */
00304           int mode,stereo,sampling_frequency,mpeg25,lsf;
00305 
00306           if( head & (1<<20) ) {
00307             lsf = (head & (1<<19)) ? 0x0 : 0x1;
00308             mpeg25 = 0;
00309           }
00310           else {
00311             lsf = 1;
00312             mpeg25 = 1;
00313           }
00314 
00315           mode      = ((head>>6)&0x3);
00316           stereo    = (mode == MPG_MD_MONO) ? 1 : 2;
00317 
00318           if(mpeg25)
00319             sampling_frequency = 6 + ((head>>10)&0x3);
00320           else
00321             sampling_frequency = ((head>>10)&0x3) + (lsf*3);
00322           h = ((stereo==fr->stereo) && (lsf==fr->lsf) && (mpeg25==fr->mpeg25) &&
00323                  (sampling_frequency == fr->sampling_frequency));
00324         }
00325 
00326         if (h) {
00327           return i-3;
00328         }
00329     }
00330   }
00331   return -1;
00332 }
00333 
00334 
00335 
00336 
00337 static int
00338 decodeMP3_clipchoice( PMPSTR mp,unsigned char *in,int isize,char *out,int *done,
00339                            int (*synth_1to1_mono_ptr)(PMPSTR,real *,unsigned char *,int *),
00340                            int (*synth_1to1_ptr)(PMPSTR,real *,int,unsigned char *, int *) )
00341 {
00342         int i,iret,bits,bytes;
00343 
00344         if (in && isize && addbuf(mp,in,isize) == NULL)
00345             return MP3_ERR;
00346 
00347         /* First decode header */
00348         if(!mp->header_parsed) {
00349 
00350             if (mp->fsizeold==-1 || mp->sync_bitstream) {
00351                 int vbrbytes;
00352                 mp->sync_bitstream=0;
00353 
00354                 /* This is the very first call.   sync with anything */
00355                 /* bytes= number of bytes before header */
00356                 bytes=sync_buffer(mp,0);
00357 
00358                 /* now look for Xing VBR header */
00359                 if (mp->bsize >= bytes+XING_HEADER_SIZE ) {
00360                     /* vbrbytes = number of bytes in entire vbr header */
00361                     vbrbytes=check_vbr_header(mp,bytes);
00362                 } else {
00363                     /* not enough data to look for Xing header */
00364                     return MP3_NEED_MORE;
00365                 }
00366 
00367                 if (mp->vbr_header) {
00368                     /* do we have enough data to parse entire Xing header? */
00369                     if (bytes+vbrbytes > mp->bsize) return MP3_NEED_MORE;
00370 
00371                     /* read in Xing header.  Buffer data in case it
00372                      * is used by a non zero main_data_begin for the next
00373                      * frame, but otherwise dont decode Xing header */
00374 /*fprintf(stderr,"found xing header, skipping %i bytes\n",vbrbytes+bytes);*/
00375                     for (i=0; i<vbrbytes+bytes; ++i) read_buf_byte(mp);
00376                     /* now we need to find another syncword */
00377                     /* just return and make user send in more data */
00378                     return MP3_NEED_MORE;
00379                 }
00380             }else{
00381                 /* match channels, samplerate, etc, when syncing */
00382                 bytes=sync_buffer(mp,1);
00383             }
00384 
00385             if (bytes<0) return MP3_NEED_MORE;
00386             if (bytes>0) {
00387                 /* there were some extra bytes in front of header.
00388                  * bitstream problem, but we are now resynced
00389                  * should try to buffer previous data in case new
00390                  * frame has nonzero main_data_begin, but we need
00391                  * to make sure we do not overflow buffer
00392                  */
00393                 int size;
00394                 fprintf(stderr,"bitstream problem: resyncing...\n");
00395                 mp->old_free_format=0;
00396                 mp->sync_bitstream=1;
00397 
00398                 /* skip some bytes, buffer the rest */
00399                 size = (int) (mp->wordpointer - (mp->bsspace[mp->bsnum]+512));
00400 
00401                 if (size > MAXFRAMESIZE) {
00402                     /* wordpointer buffer is trashed.  probably cant recover, but try anyway */
00403                     fprintf(stderr,"mpglib: wordpointer trashed.  size=%i (%i)  bytes=%i \n",
00404                             size,MAXFRAMESIZE,bytes);
00405                     size=0;
00406                     mp->wordpointer = mp->bsspace[mp->bsnum]+512;
00407                 }
00408 
00409                 /* buffer contains 'size' data right now
00410                    we want to add 'bytes' worth of data, but do not
00411                    exceed MAXFRAMESIZE, so we through away 'i' bytes */
00412                 i = (size+bytes)-MAXFRAMESIZE;
00413                 for (; i>0; --i) {
00414                     --bytes;
00415                     read_buf_byte(mp);
00416                 }
00417 
00418                 copy_mp(mp,bytes,mp->wordpointer);
00419                 mp->fsizeold += bytes;
00420             }
00421 
00422             read_head(mp);
00423             decode_header(&mp->fr,mp->header);
00424             mp->header_parsed=1;
00425             mp->framesize = mp->fr.framesize;
00426             mp->free_format = (mp->framesize==0);
00427 
00428             if(mp->fr.lsf)
00429                 mp->ssize = (mp->fr.stereo == 1) ? 9 : 17;
00430             else
00431                 mp->ssize = (mp->fr.stereo == 1) ? 17 : 32;
00432             if (mp->fr.error_protection)
00433                 mp->ssize += 2;
00434 
00435             mp->bsnum = 1-mp->bsnum; /* toggle buffer */
00436             mp->wordpointer = mp->bsspace[mp->bsnum] + 512;
00437             mp->bitindex = 0;
00438 
00439             /* for very first header, never parse rest of data */
00440             if (mp->fsizeold==-1)
00441                 return MP3_NEED_MORE;
00442         }
00443 
00444         /* now decode side information */
00445         if (!mp->side_parsed) {
00446 
00447                 /* Layer 3 only */
00448                 if (mp->fr.lay==3)
00449                 {
00450                 if (mp->bsize < mp->ssize)
00451                   return MP3_NEED_MORE;
00452 
00453                 copy_mp(mp,mp->ssize,mp->wordpointer);
00454 
00455                 if(mp->fr.error_protection)
00456                   getbits(mp,16);
00457                 bits=do_layer3_sideinfo(mp);
00458                 /* bits = actual number of bits needed to parse this frame */
00459                 /* can be negative, if all bits needed are in the reservoir */
00460                 if (bits<0) bits=0;
00461 
00462                 /* read just as many bytes as necessary before decoding */
00463                 mp->dsize = (bits+7)/8;
00464 
00465                 /* this will force mpglib to read entire frame before decoding */
00466                 /* mp->dsize= mp->framesize - mp->ssize;*/
00467 
00468                 }
00469 
00470                 else
00471                 {
00472                         /* Layers 1 and 2 */
00473 
00474                         /* check if there is enough input data */
00475                         if(mp->fr.framesize > mp->bsize)
00476                                 return MP3_NEED_MORE;
00477 
00478                         /* takes care that the right amount of data is copied into wordpointer */
00479                         mp->dsize=mp->fr.framesize;
00480                         mp->ssize=0;
00481                 }
00482 
00483                 mp->side_parsed=1;
00484         }
00485 
00486         /* now decode main data */
00487         iret=MP3_NEED_MORE;
00488         if (!mp->data_parsed ) {
00489                 if(mp->dsize > mp->bsize) {
00490                                 return MP3_NEED_MORE;
00491                 }
00492 
00493                 copy_mp(mp,mp->dsize,mp->wordpointer);
00494 
00495                 *done = 0;
00496 
00497                 /*do_layer3(&mp->fr,(unsigned char *) out,done); */
00498                 switch (mp->fr.lay)
00499                 {
00500                         case 1:
00501                                 if(mp->fr.error_protection)
00502                                         getbits(mp,16);
00503 
00504                                 do_layer1(mp,(unsigned char *) out,done);
00505                         break;
00506 
00507                         case 2:
00508                                 if(mp->fr.error_protection)
00509                                         getbits(mp,16);
00510 
00511                                 do_layer2(mp,(unsigned char *) out,done);
00512                         break;
00513 
00514                         case 3:
00515                                 do_layer3(mp,(unsigned char *) out,done, synth_1to1_mono_ptr, synth_1to1_ptr);
00516                         break;
00517                         default:
00518                                 fprintf(stderr,"invalid layer %d\n",mp->fr.lay);
00519                 }
00520 
00521                 mp->wordpointer = mp->bsspace[mp->bsnum] + 512 + mp->ssize + mp->dsize;
00522 
00523                 mp->data_parsed=1;
00524                 iret=MP3_OK;
00525         }
00526 
00527 
00528         /* remaining bits are ancillary data, or reservoir for next frame
00529          * If free format, scan stream looking for next frame to determine
00530          * mp->framesize */
00531         if (mp->free_format) {
00532           if (mp->old_free_format) {
00533             /* free format.  bitrate must not vary */
00534             mp->framesize=mp->fsizeold_nopadding + (mp->fr.padding);
00535           }else{
00536             bytes=sync_buffer(mp,1);
00537             if (bytes<0) return iret;
00538             mp->framesize = bytes + mp->ssize+mp->dsize;
00539             mp->fsizeold_nopadding= mp->framesize - mp->fr.padding;
00540             /*
00541             fprintf(stderr,"freeformat bitstream:  estimated bitrate=%ikbs  \n",
00542                 8*(4+mp->framesize)*freqs[mp->fr.sampling_frequency]/
00543                     (1000*576*(2-mp->fr.lsf)));
00544             */
00545           }
00546         }
00547 
00548         /* buffer the ancillary data and reservoir for next frame */
00549         bytes = mp->framesize-(mp->ssize+mp->dsize);
00550         if (bytes > mp->bsize) {
00551           return iret;
00552         }
00553 
00554         if (bytes>0) {
00555           int size;
00556           copy_mp(mp,bytes,mp->wordpointer);
00557           mp->wordpointer += bytes;
00558 
00559           size = (int) (mp->wordpointer - (mp->bsspace[mp->bsnum]+512));
00560           if (size > MAXFRAMESIZE) {
00561             fprintf(stderr,"fatal error.  MAXFRAMESIZE not large enough.\n");
00562           }
00563 
00564         }
00565 
00566         /* the above frame is completey parsed.  start looking for next frame */
00567         mp->fsizeold = mp->framesize;
00568         mp->old_free_format = mp->free_format;
00569         mp->framesize =0;
00570         mp->header_parsed=0;
00571         mp->side_parsed=0;
00572         mp->data_parsed=0;
00573 
00574         return iret;
00575 }
00576 
00577 int decodeMP3( PMPSTR mp,unsigned char *in,int isize,char *out,
00578                 int osize,int *done)
00579 {
00580         if(osize < 4608) {
00581                 fprintf(stderr,"To less out space\n");
00582                 return MP3_ERR;
00583         }
00584 
00585         /* passing pointers to the functions which clip the samples */
00586         return decodeMP3_clipchoice(mp, in, isize, out, done, synth_1to1_mono, synth_1to1);
00587 }
00588 
00589 int decodeMP3_unclipped( PMPSTR mp,unsigned char *in,int isize,char *out,
00590                           int osize,int *done)
00591 {
00592         /* we forbid input with more than 1152 samples per channel for output in unclipped mode */
00593         if(osize < 1152 * 2 * sizeof(real) ) {
00594                 fprintf(stderr,"To less out space\n");
00595                 return MP3_ERR;
00596         }
00597 
00598         /* passing pointers to the functions which don't clip the samples */
00599         return decodeMP3_clipchoice(mp, in, isize, out, done, synth_1to1_mono_unclipped, synth_1to1_unclipped);
00600 }
00601 
00602 
00603 
00604 
00605 

Generated on Sun Dec 2 11:34:21 2007 for LAME by  doxygen 1.5.2