~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/transcode/mpglib/mpglib/interface.c

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2007-12-31 09:49:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071231094954-ix1amvcsj9pk61ya
Tags: 1:1.4.1.57486.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #180254), remaining changes:
  - debian/rules;
    - Added dh_icons
  - Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: interface.c,v 1.46 2004/03/10 00:50:54 robert 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
#include "decode_i386.h"
 
17
 
 
18
#ifdef USE_LAYER_1
 
19
        #include "layer1.h"
 
20
#endif
 
21
 
 
22
#ifdef USE_LAYER_2
 
23
        #include "layer2.h"
 
24
#endif
 
25
 
 
26
#ifdef WITH_DMALLOC
 
27
#include <dmalloc.h>
 
28
#endif
 
29
 
 
30
BOOL InitMP3( PMPSTR mp) 
 
31
{
 
32
        memset(mp,0,sizeof(MPSTR));
 
33
 
 
34
        mp->framesize = 0;
 
35
        mp->num_frames = 0;
 
36
        mp->enc_delay = -1;
 
37
        mp->enc_padding = -1;
 
38
        mp->vbr_header=0;
 
39
        mp->header_parsed=0;
 
40
        mp->side_parsed=0;
 
41
        mp->data_parsed=0;
 
42
        mp->free_format=0;
 
43
        mp->old_free_format=0;
 
44
        mp->ssize = 0;
 
45
        mp->dsize=0;
 
46
        mp->fsizeold = -1;
 
47
        mp->bsize = 0;
 
48
        mp->head = mp->tail = NULL;
 
49
        mp->fr.single = -1;
 
50
        mp->bsnum = 0;
 
51
        wordpointer = mp->bsspace[mp->bsnum] + 512;
 
52
        mp->synth_bo = 1;
 
53
        mp->sync_bitstream = 1;
 
54
 
 
55
        make_decode_tables(32767);
 
56
 
 
57
        init_layer3(SBLIMIT);
 
58
 
 
59
#ifdef USE_LAYER_2
 
60
        init_layer2();
 
61
#endif
 
62
 
 
63
        return !0;
 
64
}
 
65
 
 
66
void ExitMP3( PMPSTR mp)
 
67
{
 
68
        struct buf *b,*bn;
 
69
        
 
70
        b = mp->tail;
 
71
        while(b) {
 
72
                free(b->pnt);
 
73
                bn = b->next;
 
74
                free(b);
 
75
                b = bn;
 
76
        }
 
77
}
 
78
 
 
79
static struct buf *addbuf( PMPSTR mp, unsigned char *buf,int size)
 
80
{
 
81
        struct buf *nbuf;
 
82
 
 
83
        nbuf = (struct buf*) malloc( sizeof(struct buf) );
 
84
        if(!nbuf) {
 
85
                fprintf(stderr,"Out of memory!\n");
 
86
                return NULL;
 
87
        }
 
88
        nbuf->pnt = (unsigned char*) malloc((size_t)size);
 
89
        if(!nbuf->pnt) {
 
90
                free(nbuf);
 
91
                return NULL;
 
92
        }
 
93
        nbuf->size = size;
 
94
        memcpy(nbuf->pnt,buf,(size_t)size);
 
95
        nbuf->next = NULL;
 
96
        nbuf->prev = mp->head;
 
97
        nbuf->pos = 0;
 
98
 
 
99
        if(!mp->tail) {
 
100
                mp->tail = nbuf;
 
101
        }
 
102
        else {
 
103
          mp->head->next = nbuf;
 
104
        }
 
105
 
 
106
        mp->head = nbuf;
 
107
        mp->bsize += size;
 
108
 
 
109
        return nbuf;
 
110
}
 
111
 
 
112
void remove_buf(PMPSTR mp)
 
113
{
 
114
  struct buf *buf = mp->tail;
 
115
  
 
116
  mp->tail = buf->next;
 
117
  if(mp->tail)
 
118
    mp->tail->prev = NULL;
 
119
  else {
 
120
    mp->tail = mp->head = NULL;
 
121
  }
 
122
  
 
123
  free(buf->pnt);
 
124
  free(buf);
 
125
 
 
126
}
 
127
 
 
128
static int read_buf_byte(PMPSTR mp)
 
129
{
 
130
        unsigned int b;
 
131
 
 
132
        int pos;
 
133
 
 
134
        
 
135
        pos = mp->tail->pos;
 
136
        while(pos >= mp->tail->size) {
 
137
                remove_buf(mp);
 
138
                if(!mp->tail) {
 
139
                        fprintf(stderr,"Fatal error! tried to read past mp buffer\n");
 
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
 
 
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
 
 
175
void copy_mp(PMPSTR mp,int size,unsigned char *ptr) 
 
176
{
 
177
  int len = 0;
 
178
 
 
179
  while(len < size && mp->tail) {
 
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
/* number of bytes needed by GetVbrTag to parse header */
 
199
#define XING_HEADER_SIZE 194
 
200
 
 
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. */
 
207
/* */
 
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)
 
212
{
 
213
    return 0;
 
214
 
 
215
}
 
216
 
 
217
 
 
218
 
 
219
 
 
220
 
 
221
 
 
222
 
 
223
int sync_buffer(PMPSTR mp,int free_match) 
 
224
{
 
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
 
228
   * samplerate.   
 
229
   * return number of bytes in mp, before the header
 
230
   * return -1 if header is not found
 
231
   */
 
232
  unsigned int b[4]={0,0,0,0};
 
233
  int i,h,pos;
 
234
  struct buf *buf=mp->tail;
 
235
  if (!buf) return -1;
 
236
 
 
237
  pos = buf->pos;
 
238
  for (i=0; i<mp->bsize; i++) {
 
239
    /* get 4 bytes */
 
240
    
 
241
    b[0]=b[1]; b[1]=b[2]; b[2]=b[3];
 
242
    while(pos >= buf->size) {
 
243
      buf  = buf->next;
 
244
      pos = buf->pos;
 
245
      if(!buf) {
 
246
        return -1;
 
247
        /* not enough data to read 4 bytes */
 
248
      }
 
249
    }
 
250
    b[3] = buf->pnt[pos];
 
251
    ++pos;
 
252
 
 
253
    if (i>=3) {
 
254
        struct frame *fr = &mp->fr;
 
255
        unsigned long head;
 
256
 
 
257
        head = b[0];
 
258
        head <<= 8;
 
259
        head |= b[1];
 
260
        head <<= 8;
 
261
        head |= b[2];
 
262
        head <<= 8;
 
263
        head |= b[3];
 
264
        h = head_check(head,fr->lay);
 
265
 
 
266
        if (h && free_match) {
 
267
          /* just to be even more thorough, match the sample rate */
 
268
          int mode,stereo,sampling_frequency,mpeg25,lsf;
 
269
 
 
270
          if( head & (1<<20) ) {
 
271
            lsf = (head & (1<<19)) ? 0x0 : 0x1;
 
272
            mpeg25 = 0;
 
273
          }
 
274
          else {
 
275
            lsf = 1;
 
276
            mpeg25 = 1;
 
277
          }
 
278
 
 
279
          mode      = ((head>>6)&0x3);
 
280
          stereo    = (mode == MPG_MD_MONO) ? 1 : 2;
 
281
 
 
282
          if(mpeg25) 
 
283
            sampling_frequency = 6 + ((head>>10)&0x3);
 
284
          else
 
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));
 
288
        }
 
289
 
 
290
        if (h) {
 
291
          return i-3;
 
292
        }
 
293
    }
 
294
  }
 
295
  return -1;
 
296
}
 
297
 
 
298
 
 
299
 
 
300
 
 
301
 
 
302
int decodeMP3_clipchoice( PMPSTR mp,unsigned char *in,int isize,char *out,
 
303
                           int osize,int *done,      
 
304
                           int (*synth_1to1_mono_ptr)(PMPSTR,real *,unsigned char *,int *),
 
305
                           int (*synth_1to1_ptr)(PMPSTR,real *,int,unsigned char *, int *) )
 
306
{
 
307
        int i,iret,bits,bytes;
 
308
 
 
309
        if (in && isize && addbuf(mp,in,isize) == NULL)
 
310
            return MP3_ERR;
 
311
 
 
312
        /* First decode header */
 
313
        if(!mp->header_parsed) {
 
314
 
 
315
            if (mp->fsizeold==-1 || mp->sync_bitstream) {
 
316
                int vbrbytes;
 
317
                mp->sync_bitstream=0;
 
318
 
 
319
                /* This is the very first call.   sync with anything */
 
320
                /* bytes= number of bytes before header */
 
321
                bytes=sync_buffer(mp,0); 
 
322
 
 
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);
 
327
                } else {
 
328
                    /* not enough data to look for Xing header */
 
329
                    return MP3_NEED_MORE;
 
330
                }
 
331
 
 
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;
 
335
                    
 
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;
 
344
                }
 
345
            }else{
 
346
                /* match channels, samplerate, etc, when syncing */
 
347
                bytes=sync_buffer(mp,1);
 
348
            }
 
349
 
 
350
            if (bytes<0) return MP3_NEED_MORE;
 
351
            if (bytes>0) {
 
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
 
357
                 */
 
358
                int size;
 
359
//              fprintf(stderr,"bitstream problem: resyncing...\n");
 
360
                mp->old_free_format=0;
 
361
                mp->sync_bitstream=1;
 
362
                
 
363
                /* skip some bytes, buffer the rest */
 
364
                size = (int) (wordpointer - (mp->bsspace[mp->bsnum]+512));
 
365
                
 
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);             
 
370
                    size=0;
 
371
                    wordpointer = mp->bsspace[mp->bsnum]+512;
 
372
                }
 
373
                
 
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;
 
378
                for (; i>0; --i) {
 
379
                    --bytes;
 
380
                    read_buf_byte(mp);
 
381
                }
 
382
                
 
383
                copy_mp(mp,bytes,wordpointer);
 
384
                mp->fsizeold += bytes;
 
385
            }
 
386
            
 
387
            read_head(mp);
 
388
            decode_header(&mp->fr,mp->header);
 
389
            mp->header_parsed=1;
 
390
            mp->framesize = mp->fr.framesize;
 
391
            mp->free_format = (mp->framesize==0);
 
392
            
 
393
            if(mp->fr.lsf)
 
394
                mp->ssize = (mp->fr.stereo == 1) ? 9 : 17;
 
395
            else
 
396
                mp->ssize = (mp->fr.stereo == 1) ? 17 : 32;
 
397
            if (mp->fr.error_protection) 
 
398
                mp->ssize += 2;
 
399
            
 
400
            mp->bsnum = 1-mp->bsnum; /* toggle buffer */
 
401
            wordpointer = mp->bsspace[mp->bsnum] + 512;
 
402
            bitindex = 0;
 
403
            
 
404
            /* for very first header, never parse rest of data */
 
405
            if (mp->fsizeold==-1)
 
406
                return MP3_NEED_MORE;
 
407
        }
 
408
        
 
409
        /* now decode side information */
 
410
        if (!mp->side_parsed) {
 
411
 
 
412
                /* Layer 3 only */
 
413
                if (mp->fr.lay==3)
 
414
                {
 
415
                if (mp->bsize < mp->ssize) 
 
416
                  return MP3_NEED_MORE;
 
417
 
 
418
                copy_mp(mp,mp->ssize,wordpointer);
 
419
 
 
420
                if(mp->fr.error_protection)
 
421
                  getbits(16);
 
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 */
 
425
                if (bits<0) bits=0;
 
426
 
 
427
                /* read just as many bytes as necessary before decoding */
 
428
                mp->dsize = (bits+7)/8;
 
429
 
 
430
                /* this will force mpglib to read entire frame before decoding */
 
431
                /* mp->dsize= mp->framesize - mp->ssize;*/
 
432
 
 
433
                }
 
434
 
 
435
                else
 
436
                {
 
437
                        /* Layers 1 and 2 */
 
438
 
 
439
                        /* check if there is enough input data */
 
440
                        if(mp->fr.framesize > mp->bsize)
 
441
                                return MP3_NEED_MORE;
 
442
 
 
443
                        /* takes care that the right amount of data is copied into wordpointer */
 
444
                        mp->dsize=mp->fr.framesize;
 
445
                        mp->ssize=0;
 
446
                }
 
447
 
 
448
                mp->side_parsed=1;
 
449
        }
 
450
 
 
451
        /* now decode main data */
 
452
        iret=MP3_NEED_MORE;
 
453
        if (!mp->data_parsed ) {
 
454
                if(mp->dsize > mp->bsize) {
 
455
                                return MP3_NEED_MORE;
 
456
                }
 
457
 
 
458
                copy_mp(mp,mp->dsize,wordpointer);
 
459
 
 
460
                *done = 0;
 
461
 
 
462
                /*do_layer3(&mp->fr,(unsigned char *) out,done); */
 
463
                switch (mp->fr.lay)
 
464
                {
 
465
#ifdef USE_LAYER_1
 
466
                        case 1:
 
467
                                if(mp->fr.error_protection)
 
468
                                        getbits(16);
 
469
 
 
470
                                do_layer1(mp,(unsigned char *) out,done);
 
471
                        break;
 
472
#endif
 
473
#ifdef USE_LAYER_2
 
474
                        case 2:
 
475
                                if(mp->fr.error_protection)
 
476
                                        getbits(16);
 
477
 
 
478
                                do_layer2(mp,(unsigned char *) out,done);
 
479
                        break;
 
480
#endif
 
481
                        case 3:
 
482
                                do_layer3(mp,(unsigned char *) out,done, synth_1to1_mono_ptr, synth_1to1_ptr);
 
483
                        break;
 
484
                        default:
 
485
//                              fprintf(stderr,"invalid layer %d\n",mp->fr.lay);
 
486
                        break;
 
487
                }
 
488
 
 
489
                wordpointer = mp->bsspace[mp->bsnum] + 512 + mp->ssize + mp->dsize;
 
490
 
 
491
                mp->data_parsed=1;
 
492
                iret=MP3_OK;
 
493
        }
 
494
 
 
495
 
 
496
        /* remaining bits are ancillary data, or reservoir for next frame 
 
497
         * If free format, scan stream looking for next frame to determine
 
498
         * mp->framesize */
 
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);
 
503
          }else{
 
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;
 
508
            /*
 
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)));
 
512
            */
 
513
          }
 
514
        }
 
515
 
 
516
        /* buffer the ancillary data and reservoir for next frame */
 
517
        bytes = mp->framesize-(mp->ssize+mp->dsize);
 
518
        if (bytes > mp->bsize) {
 
519
          return iret;
 
520
        }
 
521
 
 
522
        if (bytes>0) {
 
523
          int size;
 
524
          copy_mp(mp,bytes,wordpointer);
 
525
          wordpointer += bytes;
 
526
 
 
527
          size = (int) (wordpointer - (mp->bsspace[mp->bsnum]+512));
 
528
          if (size > MAXFRAMESIZE) {
 
529
            fprintf(stderr,"fatal error.  MAXFRAMESIZE not large enough.\n");
 
530
          }
 
531
 
 
532
        }
 
533
 
 
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;
 
537
        mp->framesize =0;
 
538
        mp->header_parsed=0;
 
539
        mp->side_parsed=0;
 
540
        mp->data_parsed=0;
 
541
 
 
542
        return iret;
 
543
}
 
544
 
 
545
int decodeMP3( PMPSTR mp,unsigned char *in,int isize,char *out,
 
546
                int osize,int *done)
 
547
{
 
548
        if(osize < 4608) {
 
549
//              fprintf(stderr,"To less out space\n");
 
550
                return MP3_ERR;
 
551
        }
 
552
 
 
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);
 
555
}       
 
556
 
 
557
int decodeMP3_unclipped( PMPSTR mp,unsigned char *in,int isize,char *out,
 
558
                          int osize,int *done)
 
559
{
 
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");
 
563
                return MP3_ERR;
 
564
        }
 
565
 
 
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);
 
568
}       
 
569
 
 
570
 
 
571
        
 
572
 
 
573