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 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3
);
32 BOOL
InitMP3(struct mpstr
*mp
)
36 memset(mp
,0,sizeof(struct mpstr
));
41 mp
->head
= mp
->tail
= NULL
;
49 make_decode_tables(32767);
57 void ClearMP3Buffer(struct mpstr
*mp
)
63 HeapFree(GetProcessHeap(), 0, b
->pnt
);
65 HeapFree(GetProcessHeap(), 0, b
);
73 static struct buf
*addbuf(struct mpstr
*mp
,const unsigned char *buf
,int size
)
77 nbuf
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct buf
));
79 WARN("Out of memory!\n");
82 nbuf
->pnt
= HeapAlloc(GetProcessHeap(), 0, size
);
84 HeapFree(GetProcessHeap(), 0, nbuf
);
85 WARN("Out of memory!\n");
89 memcpy(nbuf
->pnt
,buf
,size
);
91 nbuf
->prev
= mp
->head
;
98 mp
->head
->next
= nbuf
;
107 static void remove_buf(struct mpstr
*mp
)
109 struct buf
*buf
= mp
->tail
;
111 mp
->tail
= buf
->next
;
113 mp
->tail
->prev
= NULL
;
115 mp
->tail
= mp
->head
= NULL
;
118 HeapFree(GetProcessHeap(), 0, buf
->pnt
);
119 HeapFree(GetProcessHeap(), 0, buf
);
123 static int read_buf_byte(struct mpstr
*mp
)
130 while(pos
>= mp
->tail
->size
) {
135 b
= mp
->tail
->pnt
[pos
];
143 static void read_head(struct mpstr
*mp
)
147 head
= read_buf_byte(mp
);
149 head
|= read_buf_byte(mp
);
151 head
|= read_buf_byte(mp
);
153 head
|= read_buf_byte(mp
);
158 int decodeMP3(struct mpstr
*mp
,const unsigned char *in
,int isize
,unsigned char *out
,
164 ERR("Output buffer too small\n");
169 if(addbuf(mp
,in
,isize
) == NULL
) {
174 /* First decode header */
175 if(mp
->framesize
== 0) {
178 return MP3_NEED_MORE
;
181 while (!(ret
= decode_header(&mp
->fr
,mp
->header
)) && mp
->bsize
)
183 mp
->header
= mp
->header
<< 8;
184 mp
->header
|= read_buf_byte(mp
);
188 return MP3_NEED_MORE
;
190 mp
->framesize
= mp
->fr
.framesize
;
193 if(mp
->fr
.framesize
> mp
->bsize
)
194 return MP3_NEED_MORE
;
196 wordpointer
= mp
->bsspace
[mp
->bsnum
] + 512;
197 mp
->bsnum
= (mp
->bsnum
+ 1) & 0x1;
201 while(len
< mp
->framesize
) {
203 int blen
= mp
->tail
->size
- mp
->tail
->pos
;
204 if( (mp
->framesize
- len
) <= blen
) {
205 nlen
= mp
->framesize
-len
;
210 memcpy(wordpointer
+len
,mp
->tail
->pnt
+mp
->tail
->pos
,nlen
);
212 mp
->tail
->pos
+= nlen
;
214 if(mp
->tail
->pos
== mp
->tail
->size
) {
220 if(mp
->fr
.error_protection
)
224 do_layer1(&mp
->fr
,out
,done
);
227 do_layer2(&mp
->fr
,out
,done
);
230 do_layer3(&mp
->fr
,out
,done
);
234 mp
->fsizeold
= mp
->framesize
;
240 int set_pointer(struct mpstr
*mp
, long backstep
)
242 unsigned char *bsbufold
;
243 if(mp
->fsizeold
< 0 && backstep
> 0) {
244 /* This is not a bug if we just did seeking, the first frame is dropped then */
245 WARN("Can't step back %ld!\n",backstep
);
248 bsbufold
= mp
->bsspace
[mp
->bsnum
] + 512;
249 wordpointer
-= backstep
;
251 memcpy(wordpointer
,bsbufold
+mp
->fsizeold
-backstep
,backstep
);