2 * Copyright (c) Michael Hipp and other authors of the mpglib project.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 /* Global mp .. it's a hack */
29 BOOL InitMP3(struct mpstr *mp)
33 memset(mp,0,sizeof(struct mpstr));
38 mp->head = mp->tail = NULL;
45 make_decode_tables(32767);
53 void ExitMP3(struct mpstr *mp)
66 static struct buf *addbuf(struct mpstr *mp,const unsigned char *buf,int size)
70 nbuf = malloc( sizeof(struct buf) );
72 fprintf(stderr,"Out of memory!\n");
75 nbuf->pnt = malloc(size);
81 memcpy(nbuf->pnt,buf,size);
83 nbuf->prev = mp->head;
90 mp->head->next = nbuf;
99 static void remove_buf(struct mpstr *mp)
101 struct buf *buf = mp->tail;
103 mp->tail = buf->next;
105 mp->tail->prev = NULL;
107 mp->tail = mp->head = NULL;
115 static int read_buf_byte(struct mpstr *mp)
122 while(pos >= mp->tail->size) {
126 fprintf(stderr,"Fatal error!\n");
131 b = mp->tail->pnt[pos];
139 static void read_head(struct mpstr *mp)
143 head = read_buf_byte(mp);
145 head |= read_buf_byte(mp);
147 head |= read_buf_byte(mp);
149 head |= read_buf_byte(mp);
154 int decodeMP3(struct mpstr *mp,const unsigned char *in,int isize,unsigned char *out,
162 fprintf(stderr,"To less out space\n");
167 if(addbuf(mp,in,isize) == NULL) {
172 /* First decode header */
173 if(mp->framesize == 0) {
175 return MP3_NEED_MORE;
178 if (decode_header(&mp->fr,mp->header) == 0) {
181 mp->framesize = mp->fr.framesize;
184 if(mp->fr.framesize > mp->bsize)
185 return MP3_NEED_MORE;
187 wordpointer = mp->bsspace[mp->bsnum] + 512;
188 mp->bsnum = (mp->bsnum + 1) & 0x1;
192 while(len < mp->framesize) {
194 int blen = mp->tail->size - mp->tail->pos;
195 if( (mp->framesize - len) <= blen) {
196 nlen = mp->framesize-len;
201 memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
203 mp->tail->pos += nlen;
205 if(mp->tail->pos == mp->tail->size) {
211 if(mp->fr.error_protection)
215 do_layer1(&mp->fr,(unsigned char *) out,done);
218 do_layer2(&mp->fr,(unsigned char *) out,done);
221 do_layer3(&mp->fr,(unsigned char *) out,done);
225 mp->fsizeold = mp->framesize;
231 int set_pointer(long backstep)
233 unsigned char *bsbufold;
234 if(gmp->fsizeold < 0 && backstep > 0) {
235 fprintf(stderr,"Can't step back %ld!\n",backstep);
238 bsbufold = gmp->bsspace[gmp->bsnum] + 512;
239 wordpointer -= backstep;
241 memcpy(wordpointer,bsbufold+gmp->fsizeold-backstep,backstep);