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
23 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
29 BOOL InitMP3(struct mpstr *mp)
33 memset(mp,0,sizeof(struct mpstr));
38 mp->head = mp->tail = NULL;
46 make_decode_tables(32767);
54 void ClearMP3Buffer(struct mpstr *mp)
70 static struct buf *addbuf(struct mpstr *mp,const unsigned char *buf,int size)
74 nbuf = malloc( sizeof(struct buf) );
76 WARN("Out of memory!\n");
79 nbuf->pnt = malloc(size);
82 WARN("Out of memory!\n");
86 memcpy(nbuf->pnt,buf,size);
88 nbuf->prev = mp->head;
95 mp->head->next = nbuf;
104 static void remove_buf(struct mpstr *mp)
106 struct buf *buf = mp->tail;
108 mp->tail = buf->next;
110 mp->tail->prev = NULL;
112 mp->tail = mp->head = NULL;
120 static int read_buf_byte(struct mpstr *mp)
127 while(pos >= mp->tail->size) {
132 b = mp->tail->pnt[pos];
140 static void read_head(struct mpstr *mp)
144 head = read_buf_byte(mp);
146 head |= read_buf_byte(mp);
148 head |= read_buf_byte(mp);
150 head |= read_buf_byte(mp);
155 int decodeMP3(struct mpstr *mp,const unsigned char *in,int isize,unsigned char *out,
161 ERR("Output buffer too small\n");
166 if(addbuf(mp,in,isize) == NULL) {
171 /* First decode header */
172 if(mp->framesize == 0) {
175 return MP3_NEED_MORE;
178 while (!(ret = decode_header(&mp->fr,mp->header)) && mp->bsize)
180 mp->header = mp->header << 8;
181 mp->header |= read_buf_byte(mp);
185 return MP3_NEED_MORE;
187 mp->framesize = mp->fr.framesize;
190 if(mp->fr.framesize > mp->bsize)
191 return MP3_NEED_MORE;
193 wordpointer = mp->bsspace[mp->bsnum] + 512;
194 mp->bsnum = (mp->bsnum + 1) & 0x1;
198 while(len < mp->framesize) {
200 int blen = mp->tail->size - mp->tail->pos;
201 if( (mp->framesize - len) <= blen) {
202 nlen = mp->framesize-len;
207 memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
209 mp->tail->pos += nlen;
211 if(mp->tail->pos == mp->tail->size) {
217 if(mp->fr.error_protection)
221 do_layer1(&mp->fr,out,done);
224 do_layer2(&mp->fr,out,done);
227 do_layer3(&mp->fr,out,done);
231 mp->fsizeold = mp->framesize;
237 int set_pointer(struct mpstr *mp, long backstep)
239 unsigned char *bsbufold;
240 if(mp->fsizeold < 0 && backstep > 0) {
241 /* This is not a bug if we just did seeking, the first frame is dropped then */
242 WARN("Can't step back %ld!\n",backstep);
245 bsbufold = mp->bsspace[mp->bsnum] + 512;
246 wordpointer -= backstep;
248 memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep);