rpcrt4/tests: Make tests run on win9x/NT4 again.
[wine/multimedia.git] / dlls / winemp3.acm / interface.c
blob6ed8562d1b3a3964a22b39e39b95b1ef2965d8f9
1 /*
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
19 #include <stdlib.h>
20 #include <stdio.h>
22 #include "mpg123.h"
23 #include "mpglib.h"
26 BOOL InitMP3(struct mpstr *mp)
28 static int init = 0;
30 memset(mp,0,sizeof(struct mpstr));
32 mp->framesize = 0;
33 mp->fsizeold = -1;
34 mp->bsize = 0;
35 mp->head = mp->tail = NULL;
36 mp->fr.single = -1;
37 mp->bsnum = 0;
38 mp->synth_bo = 1;
39 mp->fr.mp = mp;
41 if(!init) {
42 init = 1;
43 make_decode_tables(32767);
44 init_layer2();
45 init_layer3(SBLIMIT);
48 return !0;
51 void ClearMP3Buffer(struct mpstr *mp)
53 struct buf *b,*bn;
55 b = mp->tail;
56 while(b) {
57 free(b->pnt);
58 bn = b->next;
59 free(b);
60 b = bn;
62 mp->tail = NULL;
63 mp->head = NULL;
64 mp->bsize = 0;
67 static struct buf *addbuf(struct mpstr *mp,const unsigned char *buf,int size)
69 struct buf *nbuf;
71 nbuf = malloc( sizeof(struct buf) );
72 if(!nbuf) {
73 fprintf(stderr,"Out of memory!\n");
74 return NULL;
76 nbuf->pnt = malloc(size);
77 if(!nbuf->pnt) {
78 free(nbuf);
79 return NULL;
81 nbuf->size = size;
82 memcpy(nbuf->pnt,buf,size);
83 nbuf->next = NULL;
84 nbuf->prev = mp->head;
85 nbuf->pos = 0;
87 if(!mp->tail) {
88 mp->tail = nbuf;
90 else {
91 mp->head->next = nbuf;
94 mp->head = nbuf;
95 mp->bsize += size;
97 return nbuf;
100 static void remove_buf(struct mpstr *mp)
102 struct buf *buf = mp->tail;
104 mp->tail = buf->next;
105 if(mp->tail)
106 mp->tail->prev = NULL;
107 else {
108 mp->tail = mp->head = NULL;
111 free(buf->pnt);
112 free(buf);
116 static int read_buf_byte(struct mpstr *mp)
118 unsigned int b;
120 int pos;
122 pos = mp->tail->pos;
123 while(pos >= mp->tail->size) {
124 remove_buf(mp);
125 pos = mp->tail->pos;
128 b = mp->tail->pnt[pos];
129 mp->bsize--;
130 mp->tail->pos++;
133 return b;
136 static void read_head(struct mpstr *mp)
138 unsigned long head;
140 head = read_buf_byte(mp);
141 head <<= 8;
142 head |= read_buf_byte(mp);
143 head <<= 8;
144 head |= read_buf_byte(mp);
145 head <<= 8;
146 head |= read_buf_byte(mp);
148 mp->header = head;
151 int decodeMP3(struct mpstr *mp,const unsigned char *in,int isize,unsigned char *out,
152 int osize,int *done)
154 int len;
156 if(osize < 4608) {
157 fprintf(stderr,"To less out space\n");
158 return MP3_ERR;
161 if(in) {
162 if(addbuf(mp,in,isize) == NULL) {
163 return MP3_ERR;
167 /* First decode header */
168 if(mp->framesize == 0) {
169 if(mp->bsize < 4) {
170 return MP3_NEED_MORE;
172 read_head(mp);
173 if (decode_header(&mp->fr,mp->header) == 0) {
174 return MP3_ERR;
176 mp->framesize = mp->fr.framesize;
179 if(mp->fr.framesize > mp->bsize)
180 return MP3_NEED_MORE;
182 wordpointer = mp->bsspace[mp->bsnum] + 512;
183 mp->bsnum = (mp->bsnum + 1) & 0x1;
184 bitindex = 0;
186 len = 0;
187 while(len < mp->framesize) {
188 int nlen;
189 int blen = mp->tail->size - mp->tail->pos;
190 if( (mp->framesize - len) <= blen) {
191 nlen = mp->framesize-len;
193 else {
194 nlen = blen;
196 memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
197 len += nlen;
198 mp->tail->pos += nlen;
199 mp->bsize -= nlen;
200 if(mp->tail->pos == mp->tail->size) {
201 remove_buf(mp);
205 *done = 0;
206 if(mp->fr.error_protection)
207 getbits(16);
208 switch(mp->fr.lay) {
209 case 1:
210 do_layer1(&mp->fr,out,done);
211 break;
212 case 2:
213 do_layer2(&mp->fr,out,done);
214 break;
215 case 3:
216 do_layer3(&mp->fr,out,done);
217 break;
220 mp->fsizeold = mp->framesize;
221 mp->framesize = 0;
223 return MP3_OK;
226 int set_pointer(struct mpstr *mp, long backstep)
228 unsigned char *bsbufold;
229 if(mp->fsizeold < 0 && backstep > 0) {
230 fprintf(stderr,"Can't step back %ld!\n",backstep);
231 return MP3_ERR;
233 bsbufold = mp->bsspace[mp->bsnum] + 512;
234 wordpointer -= backstep;
235 if (backstep)
236 memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep);
237 bitindex = 0;
238 return MP3_OK;