quartz: Silence requests for ipin on filters.
[wine] / dlls / winemp3.acm / interface.c
1 /*
2  * Copyright (c) Michael Hipp and other authors of the mpglib project.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21
22 #include "mpg123.h"
23 #include "mpglib.h"
24
25
26 BOOL InitMP3(struct mpstr *mp)
27 {
28         static int init = 0;
29
30         memset(mp,0,sizeof(struct mpstr));
31
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;
40
41         if(!init) {
42                 init = 1;
43                 make_decode_tables(32767);
44                 init_layer2();
45                 init_layer3(SBLIMIT);
46         }
47
48         return !0;
49 }
50
51 void ClearMP3Buffer(struct mpstr *mp)
52 {
53         struct buf *b,*bn;
54
55         b = mp->tail;
56         while(b) {
57                 free(b->pnt);
58                 bn = b->next;
59                 free(b);
60                 b = bn;
61         }
62         mp->tail = NULL;
63         mp->head = NULL;
64         mp->bsize = 0;
65 }
66
67 static struct buf *addbuf(struct mpstr *mp,const unsigned char *buf,int size)
68 {
69         struct buf *nbuf;
70
71         nbuf = malloc( sizeof(struct buf) );
72         if(!nbuf) {
73                 fprintf(stderr,"Out of memory!\n");
74                 return NULL;
75         }
76         nbuf->pnt = malloc(size);
77         if(!nbuf->pnt) {
78                 free(nbuf);
79                 return NULL;
80         }
81         nbuf->size = size;
82         memcpy(nbuf->pnt,buf,size);
83         nbuf->next = NULL;
84         nbuf->prev = mp->head;
85         nbuf->pos = 0;
86
87         if(!mp->tail) {
88                 mp->tail = nbuf;
89         }
90         else {
91           mp->head->next = nbuf;
92         }
93
94         mp->head = nbuf;
95         mp->bsize += size;
96
97         return nbuf;
98 }
99
100 static void remove_buf(struct mpstr *mp)
101 {
102   struct buf *buf = mp->tail;
103
104   mp->tail = buf->next;
105   if(mp->tail)
106     mp->tail->prev = NULL;
107   else {
108     mp->tail = mp->head = NULL;
109   }
110
111   free(buf->pnt);
112   free(buf);
113
114 }
115
116 static int read_buf_byte(struct mpstr *mp)
117 {
118         unsigned int b;
119
120         int pos;
121
122         pos = mp->tail->pos;
123         while(pos >= mp->tail->size) {
124                 remove_buf(mp);
125                 pos = mp->tail->pos;
126         }
127
128         b = mp->tail->pnt[pos];
129         mp->bsize--;
130         mp->tail->pos++;
131
132
133         return b;
134 }
135
136 static void read_head(struct mpstr *mp)
137 {
138         unsigned long head;
139
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);
147
148         mp->header = head;
149 }
150
151 int decodeMP3(struct mpstr *mp,const unsigned char *in,int isize,unsigned char *out,
152                 int osize,int *done)
153 {
154         int len;
155
156         if(osize < 4608) {
157                 fprintf(stderr,"To less out space\n");
158                 return MP3_ERR;
159         }
160
161         if(in) {
162                 if(addbuf(mp,in,isize) == NULL) {
163                         return MP3_ERR;
164                 }
165         }
166
167         /* First decode header */
168         if(mp->framesize == 0) {
169                 if(mp->bsize < 4) {
170                         return MP3_NEED_MORE;
171                 }
172                 read_head(mp);
173                 if (decode_header(&mp->fr,mp->header) == 0) {
174                         return MP3_ERR;
175                 }
176                 mp->framesize = mp->fr.framesize;
177         }
178
179         if(mp->fr.framesize > mp->bsize)
180                 return MP3_NEED_MORE;
181
182         wordpointer = mp->bsspace[mp->bsnum] + 512;
183         mp->bsnum = (mp->bsnum + 1) & 0x1;
184         bitindex = 0;
185
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;
192                 }
193                 else {
194                   nlen = blen;
195                 }
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);
202                 }
203         }
204
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;
218         }
219
220         mp->fsizeold = mp->framesize;
221         mp->framesize = 0;
222
223         return MP3_OK;
224 }
225
226 int set_pointer(struct mpstr *mp, long backstep)
227 {
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;
232   }
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;
239 }