push 88671f85dcf7a7cd89c63d6e9f34ad6b6ad2ae64
[wine/hacks.git] / dlls / winemp3.acm / interface.c
blob336a4f801e91058eb7947516168ef24bcbcdcb99
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 ExitMP3(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;
64 static struct buf *addbuf(struct mpstr *mp,const unsigned char *buf,int size)
66 struct buf *nbuf;
68 nbuf = malloc( sizeof(struct buf) );
69 if(!nbuf) {
70 fprintf(stderr,"Out of memory!\n");
71 return NULL;
73 nbuf->pnt = malloc(size);
74 if(!nbuf->pnt) {
75 free(nbuf);
76 return NULL;
78 nbuf->size = size;
79 memcpy(nbuf->pnt,buf,size);
80 nbuf->next = NULL;
81 nbuf->prev = mp->head;
82 nbuf->pos = 0;
84 if(!mp->tail) {
85 mp->tail = nbuf;
87 else {
88 mp->head->next = nbuf;
91 mp->head = nbuf;
92 mp->bsize += size;
94 return nbuf;
97 static void remove_buf(struct mpstr *mp)
99 struct buf *buf = mp->tail;
101 mp->tail = buf->next;
102 if(mp->tail)
103 mp->tail->prev = NULL;
104 else {
105 mp->tail = mp->head = NULL;
108 free(buf->pnt);
109 free(buf);
113 static int read_buf_byte(struct mpstr *mp)
115 unsigned int b;
117 int pos;
119 pos = mp->tail->pos;
120 while(pos >= mp->tail->size) {
121 remove_buf(mp);
122 pos = mp->tail->pos;
125 b = mp->tail->pnt[pos];
126 mp->bsize--;
127 mp->tail->pos++;
130 return b;
133 static void read_head(struct mpstr *mp)
135 unsigned long head;
137 head = read_buf_byte(mp);
138 head <<= 8;
139 head |= read_buf_byte(mp);
140 head <<= 8;
141 head |= read_buf_byte(mp);
142 head <<= 8;
143 head |= read_buf_byte(mp);
145 mp->header = head;
148 int decodeMP3(struct mpstr *mp,const unsigned char *in,int isize,unsigned char *out,
149 int osize,int *done)
151 int len;
153 if(osize < 4608) {
154 fprintf(stderr,"To less out space\n");
155 return MP3_ERR;
158 if(in) {
159 if(addbuf(mp,in,isize) == NULL) {
160 return MP3_ERR;
164 /* First decode header */
165 if(mp->framesize == 0) {
166 if(mp->bsize < 4) {
167 return MP3_NEED_MORE;
169 read_head(mp);
170 if (decode_header(&mp->fr,mp->header) == 0) {
171 return MP3_ERR;
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;
181 bitindex = 0;
183 len = 0;
184 while(len < mp->framesize) {
185 int nlen;
186 int blen = mp->tail->size - mp->tail->pos;
187 if( (mp->framesize - len) <= blen) {
188 nlen = mp->framesize-len;
190 else {
191 nlen = blen;
193 memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
194 len += nlen;
195 mp->tail->pos += nlen;
196 mp->bsize -= nlen;
197 if(mp->tail->pos == mp->tail->size) {
198 remove_buf(mp);
202 *done = 0;
203 if(mp->fr.error_protection)
204 getbits(16);
205 switch(mp->fr.lay) {
206 case 1:
207 do_layer1(&mp->fr,out,done);
208 break;
209 case 2:
210 do_layer2(&mp->fr,out,done);
211 break;
212 case 3:
213 do_layer3(&mp->fr,out,done);
214 break;
217 mp->fsizeold = mp->framesize;
218 mp->framesize = 0;
220 return MP3_OK;
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);
228 return MP3_ERR;
230 bsbufold = mp->bsspace[mp->bsnum] + 512;
231 wordpointer -= backstep;
232 if (backstep)
233 memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep);
234 bitindex = 0;
235 return MP3_OK;