1
/* $Id: interface.c,v 1.46 2004/03/10 00:50:54 robert Exp $ */
12
#include "interface.h"
16
#include "decode_i386.h"
30
BOOL InitMP3( PMPSTR mp)
32
memset(mp,0,sizeof(MPSTR));
43
mp->old_free_format=0;
48
mp->head = mp->tail = NULL;
51
wordpointer = mp->bsspace[mp->bsnum] + 512;
53
mp->sync_bitstream = 1;
55
make_decode_tables(32767);
66
void ExitMP3( PMPSTR mp)
79
static struct buf *addbuf( PMPSTR mp, unsigned char *buf,int size)
83
nbuf = (struct buf*) malloc( sizeof(struct buf) );
85
fprintf(stderr,"Out of memory!\n");
88
nbuf->pnt = (unsigned char*) malloc((size_t)size);
94
memcpy(nbuf->pnt,buf,(size_t)size);
96
nbuf->prev = mp->head;
103
mp->head->next = nbuf;
112
void remove_buf(PMPSTR mp)
114
struct buf *buf = mp->tail;
116
mp->tail = buf->next;
118
mp->tail->prev = NULL;
120
mp->tail = mp->head = NULL;
128
static int read_buf_byte(PMPSTR mp)
136
while(pos >= mp->tail->size) {
139
fprintf(stderr,"Fatal error! tried to read past mp buffer\n");
145
b = mp->tail->pnt[pos];
155
static void read_head(PMPSTR mp)
159
head = read_buf_byte(mp);
161
head |= read_buf_byte(mp);
163
head |= read_buf_byte(mp);
165
head |= read_buf_byte(mp);
175
void copy_mp(PMPSTR mp,int size,unsigned char *ptr)
179
while(len < size && mp->tail) {
181
int blen = mp->tail->size - mp->tail->pos;
182
if( (size - len) <= blen) {
188
memcpy(ptr+len,mp->tail->pnt+mp->tail->pos,(size_t)nlen);
190
mp->tail->pos += nlen;
192
if(mp->tail->pos == mp->tail->size) {
198
/* number of bytes needed by GetVbrTag to parse header */
199
#define XING_HEADER_SIZE 194
201
/* traverse mp data structure without changing it */
202
/* (just like sync_buffer) */
203
/* pull out Xing bytes */
204
/* call vbr header check code from LAME */
205
/* if we find a header, parse it and also compute the VBR header size */
206
/* if no header, do nothing. */
208
/* bytes = number of bytes before MPEG header. skip this many bytes */
209
/* before starting to read */
210
/* return value: number of bytes in VBR header, including syncword */
211
int check_vbr_header(PMPSTR mp,int bytes)
223
int sync_buffer(PMPSTR mp,int free_match)
225
/* traverse mp structure without modifing pointers, looking
226
* for a frame valid header.
227
* if free_format, valid header must also have the same
229
* return number of bytes in mp, before the header
230
* return -1 if header is not found
232
unsigned int b[4]={0,0,0,0};
234
struct buf *buf=mp->tail;
238
for (i=0; i<mp->bsize; i++) {
241
b[0]=b[1]; b[1]=b[2]; b[2]=b[3];
242
while(pos >= buf->size) {
247
/* not enough data to read 4 bytes */
250
b[3] = buf->pnt[pos];
254
struct frame *fr = &mp->fr;
264
h = head_check(head,fr->lay);
266
if (h && free_match) {
267
/* just to be even more thorough, match the sample rate */
268
int mode,stereo,sampling_frequency,mpeg25,lsf;
270
if( head & (1<<20) ) {
271
lsf = (head & (1<<19)) ? 0x0 : 0x1;
279
mode = ((head>>6)&0x3);
280
stereo = (mode == MPG_MD_MONO) ? 1 : 2;
283
sampling_frequency = 6 + ((head>>10)&0x3);
285
sampling_frequency = ((head>>10)&0x3) + (lsf*3);
286
h = ((stereo==fr->stereo) && (lsf==fr->lsf) && (mpeg25==fr->mpeg25) &&
287
(sampling_frequency == fr->sampling_frequency));
302
int decodeMP3_clipchoice( PMPSTR mp,unsigned char *in,int isize,char *out,
304
int (*synth_1to1_mono_ptr)(PMPSTR,real *,unsigned char *,int *),
305
int (*synth_1to1_ptr)(PMPSTR,real *,int,unsigned char *, int *) )
307
int i,iret,bits,bytes;
309
if (in && isize && addbuf(mp,in,isize) == NULL)
312
/* First decode header */
313
if(!mp->header_parsed) {
315
if (mp->fsizeold==-1 || mp->sync_bitstream) {
317
mp->sync_bitstream=0;
319
/* This is the very first call. sync with anything */
320
/* bytes= number of bytes before header */
321
bytes=sync_buffer(mp,0);
323
/* now look for Xing VBR header */
324
if (mp->bsize >= bytes+XING_HEADER_SIZE ) {
325
/* vbrbytes = number of bytes in entire vbr header */
326
vbrbytes=check_vbr_header(mp,bytes);
328
/* not enough data to look for Xing header */
329
return MP3_NEED_MORE;
332
if (mp->vbr_header) {
333
/* do we have enough data to parse entire Xing header? */
334
if (bytes+vbrbytes > mp->bsize) return MP3_NEED_MORE;
336
/* read in Xing header. Buffer data in case it
337
* is used by a non zero main_data_begin for the next
338
* frame, but otherwise dont decode Xing header */
339
/*fprintf(stderr,"found xing header, skipping %i bytes\n",vbrbytes+bytes);*/
340
for (i=0; i<vbrbytes+bytes; ++i) read_buf_byte(mp);
341
/* now we need to find another syncword */
342
/* just return and make user send in more data */
343
return MP3_NEED_MORE;
346
/* match channels, samplerate, etc, when syncing */
347
bytes=sync_buffer(mp,1);
350
if (bytes<0) return MP3_NEED_MORE;
352
/* there were some extra bytes in front of header.
353
* bitstream problem, but we are now resynced
354
* should try to buffer previous data in case new
355
* frame has nonzero main_data_begin, but we need
356
* to make sure we do not overflow buffer
359
// fprintf(stderr,"bitstream problem: resyncing...\n");
360
mp->old_free_format=0;
361
mp->sync_bitstream=1;
363
/* skip some bytes, buffer the rest */
364
size = (int) (wordpointer - (mp->bsspace[mp->bsnum]+512));
366
if (size > MAXFRAMESIZE) {
367
/* wordpointer buffer is trashed. probably cant recover, but try anyway */
368
fprintf(stderr,"mpglib: wordpointer trashed. size=%i (%i) bytes=%i \n",
369
size,MAXFRAMESIZE,bytes);
371
wordpointer = mp->bsspace[mp->bsnum]+512;
374
/* buffer contains 'size' data right now
375
we want to add 'bytes' worth of data, but do not
376
exceed MAXFRAMESIZE, so we through away 'i' bytes */
377
i = (size+bytes)-MAXFRAMESIZE;
383
copy_mp(mp,bytes,wordpointer);
384
mp->fsizeold += bytes;
388
decode_header(&mp->fr,mp->header);
390
mp->framesize = mp->fr.framesize;
391
mp->free_format = (mp->framesize==0);
394
mp->ssize = (mp->fr.stereo == 1) ? 9 : 17;
396
mp->ssize = (mp->fr.stereo == 1) ? 17 : 32;
397
if (mp->fr.error_protection)
400
mp->bsnum = 1-mp->bsnum; /* toggle buffer */
401
wordpointer = mp->bsspace[mp->bsnum] + 512;
404
/* for very first header, never parse rest of data */
405
if (mp->fsizeold==-1)
406
return MP3_NEED_MORE;
409
/* now decode side information */
410
if (!mp->side_parsed) {
415
if (mp->bsize < mp->ssize)
416
return MP3_NEED_MORE;
418
copy_mp(mp,mp->ssize,wordpointer);
420
if(mp->fr.error_protection)
422
bits=do_layer3_sideinfo(&mp->fr);
423
/* bits = actual number of bits needed to parse this frame */
424
/* can be negative, if all bits needed are in the reservoir */
427
/* read just as many bytes as necessary before decoding */
428
mp->dsize = (bits+7)/8;
430
/* this will force mpglib to read entire frame before decoding */
431
/* mp->dsize= mp->framesize - mp->ssize;*/
439
/* check if there is enough input data */
440
if(mp->fr.framesize > mp->bsize)
441
return MP3_NEED_MORE;
443
/* takes care that the right amount of data is copied into wordpointer */
444
mp->dsize=mp->fr.framesize;
451
/* now decode main data */
453
if (!mp->data_parsed ) {
454
if(mp->dsize > mp->bsize) {
455
return MP3_NEED_MORE;
458
copy_mp(mp,mp->dsize,wordpointer);
462
/*do_layer3(&mp->fr,(unsigned char *) out,done); */
467
if(mp->fr.error_protection)
470
do_layer1(mp,(unsigned char *) out,done);
475
if(mp->fr.error_protection)
478
do_layer2(mp,(unsigned char *) out,done);
482
do_layer3(mp,(unsigned char *) out,done, synth_1to1_mono_ptr, synth_1to1_ptr);
485
// fprintf(stderr,"invalid layer %d\n",mp->fr.lay);
489
wordpointer = mp->bsspace[mp->bsnum] + 512 + mp->ssize + mp->dsize;
496
/* remaining bits are ancillary data, or reservoir for next frame
497
* If free format, scan stream looking for next frame to determine
499
if (mp->free_format) {
500
if (mp->old_free_format) {
501
/* free format. bitrate must not vary */
502
mp->framesize=mp->fsizeold_nopadding + (mp->fr.padding);
504
bytes=sync_buffer(mp,1);
505
if (bytes<0) return iret;
506
mp->framesize = bytes + mp->ssize+mp->dsize;
507
mp->fsizeold_nopadding= mp->framesize - mp->fr.padding;
509
fprintf(stderr,"freeformat bitstream: estimated bitrate=%ikbs \n",
510
8*(4+mp->framesize)*freqs[mp->fr.sampling_frequency]/
511
(1000*576*(2-mp->fr.lsf)));
516
/* buffer the ancillary data and reservoir for next frame */
517
bytes = mp->framesize-(mp->ssize+mp->dsize);
518
if (bytes > mp->bsize) {
524
copy_mp(mp,bytes,wordpointer);
525
wordpointer += bytes;
527
size = (int) (wordpointer - (mp->bsspace[mp->bsnum]+512));
528
if (size > MAXFRAMESIZE) {
529
fprintf(stderr,"fatal error. MAXFRAMESIZE not large enough.\n");
534
/* the above frame is completey parsed. start looking for next frame */
535
mp->fsizeold = mp->framesize;
536
mp->old_free_format = mp->free_format;
545
int decodeMP3( PMPSTR mp,unsigned char *in,int isize,char *out,
549
// fprintf(stderr,"To less out space\n");
553
/* passing pointers to the functions which clip the samples */
554
return decodeMP3_clipchoice(mp, in, isize, out, osize, done, synth_1to1_mono, synth_1to1);
557
int decodeMP3_unclipped( PMPSTR mp,unsigned char *in,int isize,char *out,
560
/* we forbid input with more than 1152 samples per channel for output in unclipped mode */
561
if(osize < 1152 * 2 * sizeof(real) ) {
562
// fprintf(stderr,"To less out space\n");
566
/* passing pointers to the functions which don't clip the samples */
567
return decodeMP3_clipchoice(mp, in, isize, out, osize, done, synth_1to1_mono_unclipped, synth_1to1_unclipped);