4 * Copyright (C) 1999-2010 The L.A.M.E. project
6 * Initially written by Michael Hipp, see also AUTHORS and README.
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.
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.
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.
23 /* $Id: interface.c,v 1.64 2010/03/22 14:30:19 robert Exp $ */
33 #include "interface.h"
39 #include "decode_i386.h"
48 extern void lame_report_def(const char* format, va_list args);
50 /* #define HIP_DEBUG */
55 hip_init_tables_layer1();
56 hip_init_tables_layer2();
57 hip_init_tables_layer3();
59 memset(mp, 0, sizeof(MPSTR));
66 mp->header_parsed = 0;
70 mp->old_free_format = 0;
75 mp->head = mp->tail = NULL;
78 mp->wordpointer = mp->bsspace[mp->bsnum] + 512;
81 mp->sync_bitstream = 1;
83 mp->report_dbg = &lame_report_def;
84 mp->report_err = &lame_report_def;
85 mp->report_msg = &lame_report_def;
87 make_decode_tables(32767);
107 addbuf(PMPSTR mp, unsigned char *buf, int size)
111 nbuf = (struct buf *) malloc(sizeof(struct buf));
113 lame_report_fnc(mp->report_err, "hip: addbuf() Out of memory!\n");
116 nbuf->pnt = (unsigned char *) malloc((size_t) size);
122 memcpy(nbuf->pnt, buf, (size_t) size);
124 nbuf->prev = mp->head;
131 mp->head->next = nbuf;
141 remove_buf(PMPSTR mp)
143 struct buf *buf = mp->tail;
145 mp->tail = buf->next;
147 mp->tail->prev = NULL;
149 mp->tail = mp->head = NULL;
158 read_buf_byte(PMPSTR mp)
166 while (pos >= mp->tail->size) {
169 lame_report_fnc(mp->report_err, "hip: Fatal error! tried to read past mp buffer\n");
175 b = mp->tail->pnt[pos];
190 head = read_buf_byte(mp);
192 head |= read_buf_byte(mp);
194 head |= read_buf_byte(mp);
196 head |= read_buf_byte(mp);
205 copy_mp(PMPSTR mp, int size, unsigned char *ptr)
209 while (len < size && mp->tail) {
211 int blen = mp->tail->size - mp->tail->pos;
212 if ((size - len) <= blen) {
218 memcpy(ptr + len, mp->tail->pnt + mp->tail->pos, (size_t) nlen);
220 mp->tail->pos += nlen;
222 if (mp->tail->pos == mp->tail->size) {
228 /* number of bytes needed by GetVbrTag to parse header */
229 #define XING_HEADER_SIZE 194
232 traverse mp data structure without changing it
233 (just like sync_buffer)
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.
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
244 check_vbr_header(PMPSTR mp, int bytes)
247 struct buf *buf = mp->tail;
248 unsigned char xing[XING_HEADER_SIZE];
252 /* skip to valid header */
253 for (i = 0; i < bytes; ++i) {
254 while (pos >= buf->size) {
257 return -1; /* fatal error */
262 /* now read header */
263 for (i = 0; i < XING_HEADER_SIZE; ++i) {
264 while (pos >= buf->size) {
267 return -1; /* fatal error */
270 xing[i] = buf->pnt[pos];
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;
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)
285 return pTagData.headersize;
295 sync_buffer(PMPSTR mp, int free_match)
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
301 * return number of bytes in mp, before the header
302 * return -1 if header is not found
304 unsigned int b[4] = { 0, 0, 0, 0 };
306 struct buf *buf = mp->tail;
311 for (i = 0; i < mp->bsize; i++) {
317 while (pos >= buf->size) {
322 /* not enough data to read 4 bytes */
325 b[3] = buf->pnt[pos];
329 struct frame *fr = &mp->fr;
339 h = head_check(head, fr->lay);
341 if (h && free_match) {
342 /* just to be even more thorough, match the sample rate */
343 int mode, stereo, sampling_frequency, mpeg25, lsf;
345 if (head & (1 << 20)) {
346 lsf = (head & (1 << 19)) ? 0x0 : 0x1;
354 mode = ((head >> 6) & 0x3);
355 stereo = (mode == MPG_MD_MONO) ? 1 : 2;
358 sampling_frequency = 6 + ((head >> 10) & 0x3);
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));
375 decode_reset(PMPSTR mp)
379 /* start looking for next frame */
380 /* mp->fsizeold = mp->framesize; */
382 mp->old_free_format = mp->free_format;
384 mp->header_parsed = 0;
387 mp->sync_bitstream = 1; /* TODO check if this is right */
389 InitMP3(mp); /* Less error prone to just to reinitialise. */
394 audiodata_precedesframes(PMPSTR mp)
397 return layer3_audiodata_precedesframes(mp);
399 return 0; /* For Layer 1 & 2 the audio data starts at the frame that describes it, so no audio data precedes. */
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 *))
407 int i, iret, bits, bytes;
409 if (in && isize && addbuf(mp, in, isize) == NULL)
412 /* First decode header */
413 if (!mp->header_parsed) {
415 if (mp->fsizeold == -1 || mp->sync_bitstream) {
417 mp->sync_bitstream = 0;
419 /* This is the very first call. sync with anything */
420 /* bytes= number of bytes before header */
421 bytes = sync_buffer(mp, 0);
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);
429 /* not enough data to look for Xing header */
431 lame_report_fnc(mp->report_dbg, "hip: not enough data to look for Xing header\n");
433 return MP3_NEED_MORE;
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;
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 */
447 lame_report_fnc(mp->report_dbg, "hip: found xing header, skipping %i bytes\n", vbrbytes + bytes);
449 for (i = 0; i < vbrbytes + bytes; ++i)
451 /* now we need to find another syncword */
452 /* just return and make user send in more data */
454 return MP3_NEED_MORE;
458 /* match channels, samplerate, etc, when syncing */
459 bytes = sync_buffer(mp, 1);
462 /* buffer now synchronized */
464 /* lame_report_fnc(mp->report_err,"hip: need more bytes %d\n", bytes); */
465 return MP3_NEED_MORE;
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
475 lame_report_fnc(mp->report_err, "hip: bitstream problem, resyncing skipping %d bytes...\n", bytes);
476 mp->old_free_format = 0;
478 /* FIXME: correct ??? */
479 mp->sync_bitstream = 1;
481 /* skip some bytes, buffer the rest */
482 size = (int) (mp->wordpointer - (mp->bsspace[mp->bsnum] + 512));
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);
489 mp->wordpointer = mp->bsspace[mp->bsnum] + 512;
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;
501 copy_mp(mp, bytes, mp->wordpointer);
502 mp->fsizeold += bytes;
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);
512 mp->ssize = (mp->fr.stereo == 1) ? 9 : 17;
514 mp->ssize = (mp->fr.stereo == 1) ? 17 : 32;
515 if (mp->fr.error_protection)
518 mp->bsnum = 1 - mp->bsnum; /* toggle buffer */
519 mp->wordpointer = mp->bsspace[mp->bsnum] + 512;
522 /* for very first header, never parse rest of data */
523 if (mp->fsizeold == -1) {
525 lame_report_fnc(mp->report_dbg, "hip: not parsing the rest of the data of the first header\n");
527 return MP3_NEED_MORE;
529 } /* end of header parsing block */
531 /* now decode side information */
532 if (!mp->side_parsed) {
535 if (mp->fr.lay == 3) {
536 if (mp->bsize < mp->ssize)
537 return MP3_NEED_MORE;
539 copy_mp(mp, mp->ssize, mp->wordpointer);
541 if (mp->fr.error_protection)
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 */
549 /* read just as many bytes as necessary before decoding */
550 mp->dsize = (bits + 7) / 8;
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;
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",
568 /* this will force mpglib to read entire frame before decoding */
569 /* mp->dsize= mp->framesize - mp->ssize; */
575 /* check if there is enough input data */
576 if (mp->fr.framesize > mp->bsize)
577 return MP3_NEED_MORE;
579 /* takes care that the right amount of data is copied into wordpointer */
580 mp->dsize = mp->fr.framesize;
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;
594 copy_mp(mp, mp->dsize, mp->wordpointer);
598 /*do_layer3(&mp->fr,(unsigned char *) out,done); */
599 switch (mp->fr.lay) {
601 if (mp->fr.error_protection)
604 decode_layer1_frame(mp, (unsigned char *) out, done);
608 if (mp->fr.error_protection)
611 decode_layer2_frame(mp, (unsigned char *) out, done);
615 decode_layer3_frame(mp, (unsigned char *) out, done, synth_1to1_mono_ptr, synth_1to1_ptr);
618 lame_report_fnc(mp->report_err, "hip: invalid layer %d\n", mp->fr.lay);
621 mp->wordpointer = mp->bsspace[mp->bsnum] + 512 + mp->ssize + mp->dsize;
628 /* remaining bits are ancillary data, or reservoir for next frame
629 * If free format, scan stream looking for next frame to determine
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);
637 bytes = sync_buffer(mp, 1);
640 mp->framesize = bytes + mp->ssize + mp->dsize;
641 mp->fsizeold_nopadding = mp->framesize - mp->fr.padding;
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)));
650 /* buffer the ancillary data and reservoir for next frame */
651 bytes = mp->framesize - (mp->ssize + mp->dsize);
652 if (bytes > mp->bsize) {
659 /* FIXME: while loop OK ??? */
660 while (bytes > 512) {
666 copy_mp(mp, bytes, mp->wordpointer);
667 mp->wordpointer += bytes;
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");
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;
680 mp->header_parsed = 0;
688 decodeMP3(PMPSTR mp, unsigned char *in, int isize, char *out, int osize, int *done)
691 lame_report_fnc(mp->report_err, "hip: Insufficient memory for decoding buffer %d\n", osize);
695 /* passing pointers to the functions which clip the samples */
696 return decodeMP3_clipchoice(mp, in, isize, out, done, synth_1to1_mono, synth_1to1);
700 decodeMP3_unclipped(PMPSTR mp, unsigned char *in, int isize, char *out, int osize, int *done)
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");
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);