1 /* $Id: interface.c,v 1.5 2005/01/18 15:56:45 snelg Exp $ */
2
3 #ifdef HAVE_CONFIG_H
4 # include <config.h>
5 #endif
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "common.h"
12 #include "interface.h"
13 #include "tabinit.h"
14 #include "layer3.h"
15 #include "VbrTag.h"
16
17 #ifdef USE_LAYER_1
18 #include "layer1.h"
19 #endif
20
21 #ifdef USE_LAYER_2
22 #include "layer2.h"
23 #endif
24
25 #ifdef WITH_DMALLOC
26 #include <dmalloc.h>
27 #endif
28
29
InitMP3(PMPSTR mp)30 BOOL InitMP3( PMPSTR mp)
31 {
32 memset(mp,0,sizeof(MPSTR));
33
34 mp->framesize = 0;
35 mp->num_frames = 0;
36 mp->vbr_header=0;
37 mp->header_parsed=0;
38 mp->side_parsed=0;
39 mp->data_parsed=0;
40 mp->free_format=0;
41 mp->old_free_format=0;
42 mp->ssize = 0;
43 mp->dsize=0;
44 mp->fsizeold = -1;
45 mp->bsize = 0;
46 mp->head = mp->tail = NULL;
47 mp->fr.single = -1;
48 mp->bsnum = 0;
49 wordpointer = mp->bsspace[mp->bsnum] + 512;
50 mp->synth_bo = 1;
51 mp->sync_bitstream = 1;
52
53 make_decode_tables(32767);
54
55 init_layer3(SBLIMIT);
56
57 #ifdef USE_LAYER_2
58 init_layer2();
59 #endif
60
61 return !0;
62 }
63
ExitMP3(PMPSTR mp)64 void ExitMP3( PMPSTR mp)
65 {
66 struct buf *b,*bn;
67
68 b = mp->tail;
69 while(b) {
70 free(b->pnt);
71 bn = b->next;
72 free(b);
73 b = bn;
74 }
75 }
76
addbuf(PMPSTR mp,unsigned char * ibuf,int size)77 static struct buf *addbuf( PMPSTR mp, unsigned char *ibuf,int size)
78 {
79 struct buf *nbuf;
80
81 nbuf = (struct buf*) malloc( sizeof(struct buf) );
82 if(!nbuf) {
83 fprintf(stderr,"Out of memory!\n");
84 return NULL;
85 }
86 nbuf->pnt = (unsigned char*) malloc((size_t)size);
87 if(!nbuf->pnt) {
88 free(nbuf);
89 return NULL;
90 }
91 nbuf->size = size;
92 memcpy(nbuf->pnt,ibuf,(size_t)size);
93 nbuf->next = NULL;
94 nbuf->prev = mp->head;
95 nbuf->pos = 0;
96
97 if(!mp->tail) {
98 mp->tail = nbuf;
99 }
100 else {
101 mp->head->next = nbuf;
102 }
103
104 mp->head = nbuf;
105 mp->bsize += size;
106
107 return nbuf;
108 }
109
remove_buf(PMPSTR mp)110 void remove_buf(PMPSTR mp)
111 {
112 struct buf *buff = mp->tail;
113
114 mp->tail = buff->next;
115 if(mp->tail)
116 mp->tail->prev = NULL;
117 else {
118 mp->tail = mp->head = NULL;
119 }
120
121 free(buff->pnt);
122 free(buff);
123
124 }
125
read_buf_byte(PMPSTR mp)126 static int read_buf_byte(PMPSTR mp)
127 {
128 unsigned int b;
129
130 int pos;
131
132
133 pos = mp->tail->pos;
134 while(pos >= mp->tail->size) {
135 remove_buf(mp);
136 if(!mp->tail) {
137 fprintf(stderr,"Fatal error! tried to read past mp buffer\n");
138 fclose(stdout);
139 fclose(stderr);
140 exit(1);
141 }
142 pos = mp->tail->pos;
143 }
144
145 b = mp->tail->pnt[pos];
146 mp->bsize--;
147 mp->tail->pos++;
148
149
150 return b;
151 }
152
153
154
read_head(PMPSTR mp)155 static void read_head(PMPSTR mp)
156 {
157 unsigned long head;
158
159 head = read_buf_byte(mp);
160 head <<= 8;
161 head |= read_buf_byte(mp);
162 head <<= 8;
163 head |= read_buf_byte(mp);
164 head <<= 8;
165 head |= read_buf_byte(mp);
166
167 mp->header = head;
168 }
169
170
171
172
173
174
copy_mp(PMPSTR mp,int size,unsigned char * ptr)175 void copy_mp(PMPSTR mp,int size,unsigned char *ptr)
176 {
177 int len = 0;
178
179 while(len < size) {
180 int nlen;
181 int blen = mp->tail->size - mp->tail->pos;
182 if( (size - len) <= blen) {
183 nlen = size-len;
184 }
185 else {
186 nlen = blen;
187 }
188 memcpy(ptr+len,mp->tail->pnt+mp->tail->pos,(size_t)nlen);
189 len += nlen;
190 mp->tail->pos += nlen;
191 mp->bsize -= nlen;
192 if(mp->tail->pos == mp->tail->size) {
193 remove_buf(mp);
194 }
195 }
196 }
197
198 char VBRTag[5] = "Xing";
199
ExtractI4(unsigned char * buf)200 static int ExtractI4(unsigned char *buf)
201 {
202 int x;
203 /* big endian extract */
204 x = buf[0];
205 x <<= 8;
206 x |= buf[1];
207 x <<= 8;
208 x |= buf[2];
209 x <<= 8;
210 x |= buf[3];
211 return x;
212 }
213
214 const int bitrate_table [3] [16] = {
215 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, -1 },
216 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1 },
217 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, -1 },
218 };
219
220 const int samplerate_table [3] [4] = {
221 { 22050, 24000, 16000, -1 },
222 { 44100, 48000, 32000, -1 },
223 { 11025, 12000, 8000, -1 },
224 };
225
GetVbrTag(VBRTAGDATA * pTagData,unsigned char * buf)226 int GetVbrTag(VBRTAGDATA *pTagData, unsigned char *buf)
227 {
228 int i, head_flags;
229 int h_bitrate,h_id, h_mode, h_sr_index;
230
231 /* get Vbr header data */
232 pTagData->flags = 0;
233
234 /* get selected MPEG header data */
235 h_id = (buf[1] >> 3) & 1;
236 h_sr_index = (buf[2] >> 2) & 3;
237 h_mode = (buf[3] >> 6) & 3;
238 h_bitrate = ((buf[2]>>4)&0xf);
239 h_bitrate = bitrate_table[h_id][h_bitrate];
240
241
242 /* determine offset of header */
243 if( h_id )
244 {
245 /* mpeg1 */
246 if( h_mode != 3 ) buf+=(32+4);
247 else buf+=(17+4);
248 }
249 else
250 {
251 /* mpeg2 */
252 if( h_mode != 3 ) buf+=(17+4);
253 else buf+=(9+4);
254 }
255
256 if( buf[0] != VBRTag[0] ) return 0; /* fail */
257 if( buf[1] != VBRTag[1] ) return 0; /* header not found*/
258 if( buf[2] != VBRTag[2] ) return 0;
259 if( buf[3] != VBRTag[3] ) return 0;
260
261 buf+=4;
262
263 pTagData->h_id = h_id;
264 pTagData->samprate = samplerate_table[h_id][h_sr_index];
265
266 if( h_id == 0 )
267 pTagData->samprate >>= 1;
268
269 head_flags = pTagData->flags = ExtractI4(buf); buf+=4; /* get flags */
270
271 if( head_flags & FRAMES_FLAG )
272 {
273 pTagData->frames = ExtractI4(buf); buf+=4;
274 }
275
276 if( head_flags & BYTES_FLAG )
277 {
278 pTagData->bytes = ExtractI4(buf); buf+=4;
279 }
280
281 if( head_flags & TOC_FLAG )
282 {
283 if( pTagData->toc != NULL )
284 {
285 for(i=0;i<NUMTOCENTRIES;i++)
286 pTagData->toc[i] = buf[i];
287 }
288 buf+=NUMTOCENTRIES;
289 }
290
291 pTagData->vbr_scale = -1;
292
293 if( head_flags & VBR_SCALE_FLAG )
294 {
295 pTagData->vbr_scale = ExtractI4(buf); buf+=4;
296 }
297
298 pTagData->headersize =
299 ((h_id+1)*72000*h_bitrate) / pTagData->samprate;
300
301
302 #ifdef DEBUG_VBRTAG
303 DEBUGF("\n\n********************* VBR TAG INFO *****************\n");
304 DEBUGF("tag :%s\n",VBRTag);
305 DEBUGF("head_flags :%d\n",head_flags);
306 DEBUGF("bytes :%d\n",pTagData->bytes);
307 DEBUGF("frames :%d\n",pTagData->frames);
308 DEBUGF("VBR Scale :%d\n",pTagData->vbr_scale);
309 DEBUGF("toc:\n");
310 if( pTagData->toc != NULL )
311 {
312 for(i=0;i<NUMTOCENTRIES;i++)
313 {
314 if( (i%10) == 0 ) DEBUGF("\n");
315 DEBUGF(" %3d", (int)(pTagData->toc[i]));
316 }
317 }
318 DEBUGF("\n***************** END OF VBR TAG INFO ***************\n");
319 #endif
320 return 1; /* success */
321 }
322
323
324
325 // traverse mp data structure without changing it
326 // (just like sync_buffer)
327 // pull out 48 bytes
328 // call vbr header check code from LAME
329 // if we find a header, parse it and also compute the VBR header size
330 // if no header, do nothing.
331 //
332 // bytes = number of bytes before MPEG header. skip this many bytes
333 // before starting to read
334 // return value: number of bytes in VBR header, including syncword
check_vbr_header(PMPSTR mp,int bytes)335 int check_vbr_header(PMPSTR mp,int bytes)
336 {
337 int i,pos;
338 struct buf *ibuf=mp->tail;
339 unsigned char xing[48];
340 VBRTAGDATA pTagData;
341
342 pos = ibuf->pos;
343 // skip to valid header
344 for (i=0; i<bytes; ++i) {
345 while(pos >= ibuf->size) {
346 ibuf = ibuf->next;
347 pos = ibuf->pos;
348 if(!ibuf) return -1; /* fatal error */
349 }
350 ++pos;
351 }
352 // now read 48 bytes
353 for (i=0; i<48; ++i) {
354 while(pos >= ibuf->size) {
355 ibuf = ibuf->next;
356 pos = ibuf->pos;
357 if(!ibuf) return -1; /* fatal error */
358 }
359 xing[i] = ibuf->pnt[pos];
360 ++pos;
361 }
362
363 /* check first 48 bytes for Xing header */
364 mp->vbr_header = GetVbrTag(&pTagData,xing);
365 if (mp->vbr_header) {
366 mp->num_frames=pTagData.frames;
367 // fprintf(stderr,"\rmpglib: Xing VBR header dectected. MP3 file has %i frames\n", pTagData.frames);
368 return pTagData.headersize;
369 }
370 return 0;
371 }
372
373
374
375
376
377
378
sync_buffer(PMPSTR mp,int free_match)379 int sync_buffer(PMPSTR mp,int free_match)
380 {
381 /* traverse mp structure without modifing pointers, looking
382 * for a frame valid header.
383 * if free_format, valid header must also have the same
384 * samplerate.
385 * return number of bytes in mp, before the header
386 * return -1 if header is not found
387 */
388 unsigned int b[4]={0,0,0,0};
389 int i,h,pos;
390 struct buf *buff=mp->tail;
391
392 pos = buff->pos;
393 for (i=0; i<mp->bsize; i++) {
394 /* get 4 bytes */
395
396 b[0]=b[1]; b[1]=b[2]; b[2]=b[3];
397 while(pos >= buff->size) {
398 buff = buff->next;
399 pos = buff->pos;
400 if(!buff) {
401 return -1;
402 /* not enough data to read 4 bytes */
403 }
404 }
405 b[3] = buff->pnt[pos];
406 ++pos;
407
408 if (i>=3) {
409 struct frame *fr = &mp->fr;
410 unsigned long head;
411
412 head = b[0];
413 head <<= 8;
414 head |= b[1];
415 head <<= 8;
416 head |= b[2];
417 head <<= 8;
418 head |= b[3];
419 h = head_check(head,fr->lay);
420
421 if (h && free_match) {
422 /* just to be even more thorough, match the sample rate */
423 int mode,stereo,sampling_frequency,mpeg25,lsf;
424
425 if( head & (1<<20) ) {
426 lsf = (head & (1<<19)) ? 0x0 : 0x1;
427 mpeg25 = 0;
428 }
429 else {
430 lsf = 1;
431 mpeg25 = 1;
432 }
433
434 mode = ((head>>6)&0x3);
435 stereo = (mode == MPG_MD_MONO) ? 1 : 2;
436
437 if(mpeg25)
438 sampling_frequency = 6 + ((head>>10)&0x3);
439 else
440 sampling_frequency = ((head>>10)&0x3) + (lsf*3);
441 h = ((stereo==fr->stereo) && (lsf==fr->lsf) && (mpeg25==fr->mpeg25) &&
442 (sampling_frequency == fr->sampling_frequency));
443 }
444
445 if (h) {
446 return i-3;
447 }
448 }
449 }
450 return -1;
451 }
452
453
454
455
456
decodeMP3(PMPSTR mp,unsigned char * in,int isize,int * done)457 int decodeMP3( PMPSTR mp,unsigned char *in,int isize,int *done)
458 {
459 int i,iret,bits,bytes;
460
461 if(in) {
462 if(addbuf(mp,in,isize) == NULL) {
463 return MP3_ERR;
464 }
465 }
466
467
468 /* First decode header */
469 if(!mp->header_parsed) {
470
471 if (mp->fsizeold==-1 || mp->sync_bitstream) {
472 int vbrbytes;
473 mp->sync_bitstream=0;
474
475 /* This is the very first call. sync with anything */
476 /* bytes= number of bytes before header */
477 bytes=sync_buffer(mp,0);
478
479 /* now look for Xing VBR header */
480 if (mp->bsize >= bytes+48 ) {
481 /* vbrbytes = number of bytes in entire vbr header */
482 vbrbytes=check_vbr_header(mp,bytes);
483 } else {
484 /* not enough data to look for Xing header */
485 return MP3_NEED_MORE;
486 }
487
488 if (mp->vbr_header) {
489 /* do we have enough data to parse entire Xing header? */
490 if (bytes+vbrbytes > mp->bsize) return MP3_NEED_MORE;
491
492 /* read in Xing header. Buffer data in case it
493 * is used by a non zero main_data_begin for the next
494 * frame, but otherwise dont decode Xing header */
495 for (i=0; i<vbrbytes+bytes; ++i) read_buf_byte(mp);
496 /* now we need to find another syncword */
497 /* just return and make user send in more data */
498 return MP3_NEED_MORE;
499 }
500 }else{
501 /* match channels, samplerate, etc, when syncing */
502 bytes=sync_buffer(mp,1);
503 }
504
505 if (bytes<0) return MP3_NEED_MORE;
506 if (bytes>0) {
507 /* there were some extra bytes in front of header.
508 * bitstream problem, but we are now resynced
509 * should try to buffer previous data in case new
510 * frame has nonzero main_data_begin, but we need
511 * to make sure we do not overflow buffer
512 */
513 int size;
514 // fprintf(stderr,"bitstream problem: resyncing...\n");
515 mp->old_free_format=0;
516 mp->sync_bitstream=1;
517
518 /* skip some bytes, buffer the rest */
519 size = (int) (wordpointer - (mp->bsspace[mp->bsnum]+512));
520
521 if (size > MAXFRAMESIZE) {
522 /* wordpointer buffer is trashed. probably cant recover, but try anyway */
523 // fprintf(stderr,"mpglib: wordpointer trashed. size=%i (%i) bytes=%i \n",
524 // size,MAXFRAMESIZE,bytes);
525 size=0;
526 wordpointer = mp->bsspace[mp->bsnum]+512;
527 }
528
529 /* buffer contains 'size' data right now
530 we want to add 'bytes' worth of data, but do not
531 exceed MAXFRAMESIZE, so we through away 'i' bytes */
532 i = (size+bytes)-MAXFRAMESIZE;
533 for (; i>0; --i) {
534 --bytes;
535 read_buf_byte(mp);
536 }
537
538 copy_mp(mp,bytes,wordpointer);
539 mp->fsizeold += bytes;
540 }
541
542 read_head(mp);
543 decode_header(&mp->fr,mp->header);
544 mp->header_parsed=1;
545 mp->framesize = mp->fr.framesize;
546 mp->free_format = (mp->framesize==0);
547
548 if(mp->fr.lsf)
549 mp->ssize = (mp->fr.stereo == 1) ? 9 : 17;
550 else
551 mp->ssize = (mp->fr.stereo == 1) ? 17 : 32;
552 if (mp->fr.error_protection)
553 mp->ssize += 2;
554
555 mp->bsnum = 1-mp->bsnum; /* toggle buffer */
556 wordpointer = mp->bsspace[mp->bsnum] + 512;
557 bitindex = 0;
558
559 /* for very first header, never parse rest of data */
560 if (mp->fsizeold==-1)
561 return MP3_NEED_MORE;
562 }
563
564 /* now decode side information */
565 if (!mp->side_parsed) {
566
567 /* Layer 3 only */
568 if (mp->fr.lay==3)
569 {
570 if (mp->bsize < mp->ssize)
571 return MP3_NEED_MORE;
572
573 copy_mp(mp,mp->ssize,wordpointer);
574
575 if(mp->fr.error_protection)
576 getbits(16);
577 bits=do_layer3_sideinfo(&mp->fr);
578 if (bits == -32767) {
579 ExitMP3(mp);
580 InitMP3(mp);
581 return MP3_ERR;
582 }
583 /* bits = actual number of bits needed to parse this frame */
584 /* can be negative, if all bits needed are in the reservoir */
585 if (bits<0) bits=0;
586
587 /* read just as many bytes as necessary before decoding */
588 mp->dsize = (bits+7)/8;
589
590 /* this will force mpglib to read entire frame before decoding */
591 /* mp->dsize= mp->framesize - mp->ssize;*/
592
593 }
594
595 else
596 {
597 /* Layers 1 and 2 */
598
599 /* check if there is enough input data */
600 if(mp->fr.framesize > mp->bsize)
601 return MP3_NEED_MORE;
602
603 /* takes care that the right amount of data is copied into wordpointer */
604 mp->dsize=mp->fr.framesize;
605 mp->ssize=0;
606 }
607
608 mp->side_parsed=1;
609 }
610
611 /* now decode main data */
612 iret=MP3_NEED_MORE;
613 if (!mp->data_parsed ) {
614 if(mp->dsize > mp->bsize) {
615 return MP3_NEED_MORE;
616 }
617
618 copy_mp(mp,mp->dsize,wordpointer);
619
620 *done = 0;
621
622 //do_layer3(&mp->fr,(unsigned char *) out,done);
623 iret=MP3_OK;
624 switch (mp->fr.lay)
625 {
626 #ifdef USE_LAYER_1
627 case 1:
628 if(mp->fr.error_protection)
629 getbits(16);
630
631 do_layer1(mp,(unsigned char *) out,done);
632 break;
633 #endif
634 #ifdef USE_LAYER_2
635 case 2:
636 if(mp->fr.error_protection)
637 getbits(16);
638
639 do_layer2(mp,(unsigned char *) out,done);
640 break;
641 #endif
642 case 3:
643 if (do_layer3(mp,done)) {
644 iret = MP3_ERR;
645 }
646 break;
647 default:
648 fprintf(stderr,"invalid layer %d\n",mp->fr.lay);
649 }
650
651 wordpointer = mp->bsspace[mp->bsnum] + 512 + mp->ssize + mp->dsize;
652
653 mp->data_parsed=1;
654 }
655
656
657 /* remaining bits are ancillary data, or reservoir for next frame
658 * If free format, scan stream looking for next frame to determine
659 * mp->framesize */
660 if (mp->free_format) {
661 if (mp->old_free_format) {
662 /* free format. bitrate must not vary */
663 mp->framesize=mp->fsizeold_nopadding + (mp->fr.padding);
664 }else{
665 bytes=sync_buffer(mp,1);
666 if (bytes<0) return iret;
667 mp->framesize = bytes + mp->ssize+mp->dsize;
668 mp->fsizeold_nopadding= mp->framesize - mp->fr.padding;
669 /*
670 fprintf(stderr,"freeformat bitstream: estimated bitrate=%ikbs \n",
671 8*(4+mp->framesize)*freqs[mp->fr.sampling_frequency]/
672 (1000*576*(2-mp->fr.lsf)));
673 */
674 }
675 }
676
677 /* buffer the ancillary data and reservoir for next frame */
678 bytes = mp->framesize-(mp->ssize+mp->dsize);
679 if (bytes > mp->bsize) {
680 return iret;
681 }
682
683 if (bytes>0) {
684 int size;
685 copy_mp(mp,bytes,wordpointer);
686 wordpointer += bytes;
687
688 size = (int) (wordpointer - (mp->bsspace[mp->bsnum]+512));
689 if (size > MAXFRAMESIZE) {
690 fprintf(stderr,"fatal error. MAXFRAMESIZE not large enough.\n");
691 }
692
693 }
694
695 /* the above frame is completey parsed. start looking for next frame */
696 mp->fsizeold = mp->framesize;
697 mp->old_free_format = mp->free_format;
698 mp->framesize =0;
699 mp->header_parsed=0;
700 mp->side_parsed=0;
701 mp->data_parsed=0;
702
703 return iret;
704 }
705
706
707
708