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