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
26 BOOL InitMP3(struct mpstr *mp)
30 memset(mp,0,sizeof(struct mpstr));
35 mp->head = mp->tail = NULL;
43 make_decode_tables(32767);
51 void ExitMP3(struct mpstr *mp)
64 static struct buf *addbuf(struct mpstr *mp,const unsigned char *buf,int size)
68 nbuf = malloc( sizeof(struct buf) );
70 fprintf(stderr,"Out of memory!\n");
73 nbuf->pnt = malloc(size);
79 memcpy(nbuf->pnt,buf,size);
81 nbuf->prev = mp->head;
88 mp->head->next = nbuf;
97 static void remove_buf(struct mpstr *mp)
99 struct buf *buf = mp->tail;
101 mp->tail = buf->next;
103 mp->tail->prev = NULL;
105 mp->tail = mp->head = NULL;
113 static int read_buf_byte(struct mpstr *mp)
120 while(pos >= mp->tail->size) {
125 b = mp->tail->pnt[pos];
133 static void read_head(struct mpstr *mp)
137 head = read_buf_byte(mp);
139 head |= read_buf_byte(mp);
141 head |= read_buf_byte(mp);
143 head |= read_buf_byte(mp);
148 int decodeMP3(struct mpstr *mp,const unsigned char *in,int isize,unsigned char *out,
154 fprintf(stderr,"To less out space\n");
159 if(addbuf(mp,in,isize) == NULL) {
164 /* First decode header */
165 if(mp->framesize == 0) {
167 return MP3_NEED_MORE;
170 if (decode_header(&mp->fr,mp->header) == 0) {
173 mp->framesize = mp->fr.framesize;
176 if(mp->fr.framesize > mp->bsize)
177 return MP3_NEED_MORE;
179 wordpointer = mp->bsspace[mp->bsnum] + 512;
180 mp->bsnum = (mp->bsnum + 1) & 0x1;
184 while(len < mp->framesize) {
186 int blen = mp->tail->size - mp->tail->pos;
187 if( (mp->framesize - len) <= blen) {
188 nlen = mp->framesize-len;
193 memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
195 mp->tail->pos += nlen;
197 if(mp->tail->pos == mp->tail->size) {
203 if(mp->fr.error_protection)
207 do_layer1(&mp->fr,(unsigned char *) out,done);
210 do_layer2(&mp->fr,(unsigned char *) out,done);
213 do_layer3(&mp->fr,(unsigned char *) out,done);
217 mp->fsizeold = mp->framesize;
223 int set_pointer(struct mpstr *mp, long backstep)
225 unsigned char *bsbufold;
226 if(mp->fsizeold < 0 && backstep > 0) {
227 fprintf(stderr,"Can't step back %ld!\n",backstep);
230 bsbufold = mp->bsspace[mp->bsnum] + 512;
231 wordpointer -= backstep;
233 memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep);