Mercurial > SDL_sound_CoreAudio
annotate decoders/mpglib/interface.c @ 279:52b9f37998db
Removed global state variable; should be thread safe now.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Thu, 14 Mar 2002 16:38:59 +0000 |
parents | 9e7f9e09ea0e |
children | ad4c8f34136a |
rev | line source |
---|---|
261 | 1 |
2 #include <stdlib.h> | |
3 #include <stdio.h> | |
4 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
5 #include "SDL_sound.h" |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
6 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
7 #define __SDL_SOUND_INTERNAL__ |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
8 #include "SDL_sound_internal.h" |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
9 |
261 | 10 #include "mpg123_sdlsound.h" |
11 #include "mpglib_sdlsound.h" | |
12 | |
13 /* Global mp .. it's a hack */ | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
14 /*struct mpstr *gmp;*/ |
261 | 15 |
16 | |
17 BOOL InitMP3(struct mpstr *mp) | |
18 { | |
19 static int init = 0; | |
20 | |
21 memset(mp,0,sizeof(struct mpstr)); | |
22 | |
23 mp->framesize = 0; | |
24 mp->fsizeold = -1; | |
25 mp->bsize = 0; | |
26 mp->head = mp->tail = NULL; | |
27 mp->fr.single = -1; | |
28 mp->bsnum = 0; | |
29 mp->synth_bo = 1; | |
30 | |
31 if(!init) { | |
32 init = 1; | |
33 make_decode_tables(32767); | |
34 init_layer2(); | |
35 init_layer3(SBLIMIT); | |
36 } | |
37 | |
38 return !0; | |
39 } | |
40 | |
41 void ExitMP3(struct mpstr *mp) | |
42 { | |
43 struct buf *b,*bn; | |
44 | |
45 b = mp->tail; | |
46 while(b) { | |
47 free(b->pnt); | |
48 bn = b->next; | |
49 free(b); | |
50 b = bn; | |
51 } | |
52 } | |
53 | |
54 static struct buf *addbuf(struct mpstr *mp,char *buf,int size) | |
55 { | |
56 struct buf *nbuf; | |
57 | |
58 nbuf = malloc( sizeof(struct buf) ); | |
59 if(!nbuf) { | |
274
9e7f9e09ea0e
Removed fprintf() calls. Replaced with SNDDBG() and Sound_SetError() calls.
Ryan C. Gordon <icculus@icculus.org>
parents:
273
diff
changeset
|
60 Sound_SetError("MPGLIB: Out of memory!"); |
261 | 61 return NULL; |
62 } | |
63 nbuf->pnt = malloc(size); | |
64 if(!nbuf->pnt) { | |
65 free(nbuf); | |
66 return NULL; | |
67 } | |
68 nbuf->size = size; | |
69 memcpy(nbuf->pnt,buf,size); | |
70 nbuf->next = NULL; | |
71 nbuf->prev = mp->head; | |
72 nbuf->pos = 0; | |
73 | |
74 if(!mp->tail) { | |
75 mp->tail = nbuf; | |
76 } | |
77 else { | |
78 mp->head->next = nbuf; | |
79 } | |
80 | |
81 mp->head = nbuf; | |
82 mp->bsize += size; | |
83 | |
84 return nbuf; | |
85 } | |
86 | |
87 static void remove_buf(struct mpstr *mp) | |
88 { | |
89 struct buf *buf = mp->tail; | |
90 | |
91 mp->tail = buf->next; | |
92 if(mp->tail) | |
93 mp->tail->prev = NULL; | |
94 else { | |
95 mp->tail = mp->head = NULL; | |
96 } | |
97 | |
98 free(buf->pnt); | |
99 free(buf); | |
100 | |
101 } | |
102 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
103 static int read_buf_byte(struct mpstr *mp, unsigned long *retval) |
261 | 104 { |
105 int pos; | |
106 | |
107 pos = mp->tail->pos; | |
108 while(pos >= mp->tail->size) { | |
109 remove_buf(mp); | |
110 pos = mp->tail->pos; | |
111 if(!mp->tail) { | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
112 Sound_SetError("MPGLIB: Fatal error! Short read in read_buf_byte()!"); |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
113 return 0; |
261 | 114 } |
115 } | |
116 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
117 if (retval != NULL) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
118 *retval = mp->tail->pnt[pos]; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
119 |
261 | 120 mp->bsize--; |
121 mp->tail->pos++; | |
122 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
123 return 1; |
261 | 124 } |
125 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
126 static int read_head(struct mpstr *mp) |
261 | 127 { |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
128 unsigned long val; |
261 | 129 unsigned long head; |
130 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
131 if (!read_buf_byte(mp, &val)) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
132 return 0; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
133 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
134 head = val << 8; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
135 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
136 if (!read_buf_byte(mp, &val)) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
137 return 0; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
138 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
139 head |= val; |
261 | 140 head <<= 8; |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
141 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
142 if (!read_buf_byte(mp, &val)) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
143 return 0; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
144 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
145 head |= val; |
261 | 146 head <<= 8; |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
147 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
148 if (!read_buf_byte(mp, &val)) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
149 return 0; |
261 | 150 |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
151 head |= val; |
261 | 152 mp->header = head; |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
153 return 1; |
261 | 154 } |
155 | |
156 int decodeMP3(struct mpstr *mp,char *in,int isize,char *out, | |
157 int osize,int *done) | |
158 { | |
159 int len; | |
160 | |
161 if(osize < 4608) { | |
274
9e7f9e09ea0e
Removed fprintf() calls. Replaced with SNDDBG() and Sound_SetError() calls.
Ryan C. Gordon <icculus@icculus.org>
parents:
273
diff
changeset
|
162 Sound_SetError("MPGLIB: Not enough output space for decoding!"); |
261 | 163 return MP3_ERR; |
164 } | |
165 | |
166 if(in) { | |
167 if(addbuf(mp,in,isize) == NULL) { | |
168 return MP3_ERR; | |
169 } | |
170 } | |
171 | |
172 /* First decode header */ | |
173 if(mp->framesize == 0) { | |
174 if(mp->bsize < 4) { | |
175 return MP3_NEED_MORE; | |
176 } | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
177 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
178 if (!read_head(mp)) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
179 return MP3_ERR; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
180 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
181 if (!decode_header(&mp->fr,mp->header)) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
182 return MP3_ERR; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
183 |
261 | 184 mp->framesize = mp->fr.framesize; |
185 } | |
186 | |
187 if(mp->fr.framesize > mp->bsize) | |
188 return MP3_NEED_MORE; | |
189 | |
190 wordpointer = mp->bsspace[mp->bsnum] + 512; | |
191 mp->bsnum = (mp->bsnum + 1) & 0x1; | |
192 bitindex = 0; | |
193 | |
194 len = 0; | |
195 while(len < mp->framesize) { | |
196 int nlen; | |
197 int blen = mp->tail->size - mp->tail->pos; | |
198 if( (mp->framesize - len) <= blen) { | |
199 nlen = mp->framesize-len; | |
200 } | |
201 else { | |
202 nlen = blen; | |
203 } | |
204 memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen); | |
205 len += nlen; | |
206 mp->tail->pos += nlen; | |
207 mp->bsize -= nlen; | |
208 if(mp->tail->pos == mp->tail->size) { | |
209 remove_buf(mp); | |
210 } | |
211 } | |
212 | |
213 *done = 0; | |
214 if(mp->fr.error_protection) | |
215 getbits(16); | |
216 switch(mp->fr.lay) { | |
217 case 1: | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
218 do_layer1(&mp->fr,(unsigned char *) out,done,mp); |
261 | 219 break; |
220 case 2: | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
221 do_layer2(&mp->fr,(unsigned char *) out,done,mp); |
261 | 222 break; |
223 case 3: | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
224 do_layer3(&mp->fr,(unsigned char *) out,done,mp); |
261 | 225 break; |
226 } | |
227 | |
228 mp->fsizeold = mp->framesize; | |
229 mp->framesize = 0; | |
230 | |
231 return MP3_OK; | |
232 } | |
233 | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
234 int set_pointer(long backstep, struct mpstr *mp) |
261 | 235 { |
236 unsigned char *bsbufold; | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
237 if(mp->fsizeold < 0 && backstep > 0) { |
274
9e7f9e09ea0e
Removed fprintf() calls. Replaced with SNDDBG() and Sound_SetError() calls.
Ryan C. Gordon <icculus@icculus.org>
parents:
273
diff
changeset
|
238 Sound_SetError("MPGLIB: Can't step back!"); /* FIXME: need formatting: %ld!\n",backstep); */ |
261 | 239 return MP3_ERR; |
240 } | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
241 bsbufold = mp->bsspace[mp->bsnum] + 512; |
261 | 242 wordpointer -= backstep; |
243 if (backstep) | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
244 memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep); |
261 | 245 bitindex = 0; |
246 return MP3_OK; | |
247 } | |
248 | |
249 | |
250 | |
251 |