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