Mercurial > SDL_sound_CoreAudio
annotate decoders/mpglib/interface.c @ 453:b9856ba6fb4e
Updated comments on supported files to include Speex.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Sat, 13 Sep 2003 20:35:24 +0000 |
parents | 9d0b5ec9cc26 |
children |
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) ); | |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
281
diff
changeset
|
56 BAIL_IF_MACRO(!nbuf, ERR_OUT_OF_MEMORY, NULL); |
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
281
diff
changeset
|
57 |
261 | 58 nbuf->pnt = malloc(size); |
59 if(!nbuf->pnt) { | |
60 free(nbuf); | |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
281
diff
changeset
|
61 BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL); |
261 | 62 } |
63 nbuf->size = size; | |
64 memcpy(nbuf->pnt,buf,size); | |
65 nbuf->next = NULL; | |
66 nbuf->prev = mp->head; | |
67 nbuf->pos = 0; | |
68 | |
69 if(!mp->tail) { | |
70 mp->tail = nbuf; | |
71 } | |
72 else { | |
73 mp->head->next = nbuf; | |
74 } | |
75 | |
76 mp->head = nbuf; | |
77 mp->bsize += size; | |
78 | |
79 return nbuf; | |
80 } | |
81 | |
82 static void remove_buf(struct mpstr *mp) | |
83 { | |
84 struct buf *buf = mp->tail; | |
85 | |
86 mp->tail = buf->next; | |
87 if(mp->tail) | |
88 mp->tail->prev = NULL; | |
89 else { | |
90 mp->tail = mp->head = NULL; | |
91 } | |
92 | |
93 free(buf->pnt); | |
94 free(buf); | |
95 | |
96 } | |
97 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
98 static int read_buf_byte(struct mpstr *mp, unsigned long *retval) |
261 | 99 { |
100 int pos; | |
101 | |
102 pos = mp->tail->pos; | |
103 while(pos >= mp->tail->size) { | |
104 remove_buf(mp); | |
105 pos = mp->tail->pos; | |
106 if(!mp->tail) { | |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
281
diff
changeset
|
107 BAIL_MACRO("MPGLIB: Short read in read_buf_byte()!", 0); |
261 | 108 } |
109 } | |
110 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
111 if (retval != NULL) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
112 *retval = mp->tail->pnt[pos]; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
113 |
261 | 114 mp->bsize--; |
115 mp->tail->pos++; | |
116 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
117 return 1; |
261 | 118 } |
119 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
120 static int read_head(struct mpstr *mp) |
261 | 121 { |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
122 unsigned long val; |
261 | 123 unsigned long head; |
124 | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
125 if (!read_buf_byte(mp, &val)) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
126 return 0; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
127 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
128 head = val << 8; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
129 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
130 if (!read_buf_byte(mp, &val)) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
131 return 0; |
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 head |= val; |
261 | 134 head <<= 8; |
273
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; |
261 | 144 |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
145 head |= val; |
261 | 146 mp->header = head; |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
147 return 1; |
261 | 148 } |
149 | |
150 int decodeMP3(struct mpstr *mp,char *in,int isize,char *out, | |
151 int osize,int *done) | |
152 { | |
153 int len; | |
154 | |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
281
diff
changeset
|
155 BAIL_IF_MACRO(osize < 4608, "MPGLIB: Output buffer too small", MP3_ERR); |
261 | 156 |
157 if(in) { | |
158 if(addbuf(mp,in,isize) == NULL) { | |
159 return MP3_ERR; | |
160 } | |
161 } | |
162 | |
163 /* First decode header */ | |
164 if(mp->framesize == 0) { | |
165 if(mp->bsize < 4) { | |
166 return MP3_NEED_MORE; | |
167 } | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
168 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
169 if (!read_head(mp)) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
170 return MP3_ERR; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
171 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
172 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
|
173 return MP3_ERR; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
174 |
261 | 175 mp->framesize = mp->fr.framesize; |
176 } | |
177 | |
178 if(mp->fr.framesize > mp->bsize) | |
179 return MP3_NEED_MORE; | |
180 | |
181 wordpointer = mp->bsspace[mp->bsnum] + 512; | |
182 mp->bsnum = (mp->bsnum + 1) & 0x1; | |
183 bitindex = 0; | |
184 | |
185 len = 0; | |
186 while(len < mp->framesize) { | |
187 int nlen; | |
188 int blen = mp->tail->size - mp->tail->pos; | |
189 if( (mp->framesize - len) <= blen) { | |
190 nlen = mp->framesize-len; | |
191 } | |
192 else { | |
193 nlen = blen; | |
194 } | |
195 memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen); | |
196 len += nlen; | |
197 mp->tail->pos += nlen; | |
198 mp->bsize -= nlen; | |
199 if(mp->tail->pos == mp->tail->size) { | |
200 remove_buf(mp); | |
201 } | |
202 } | |
203 | |
204 *done = 0; | |
205 if(mp->fr.error_protection) | |
206 getbits(16); | |
207 switch(mp->fr.lay) { | |
208 case 1: | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
209 do_layer1(&mp->fr,(unsigned char *) out,done,mp); |
261 | 210 break; |
211 case 2: | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
212 do_layer2(&mp->fr,(unsigned char *) out,done,mp); |
261 | 213 break; |
214 case 3: | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
215 do_layer3(&mp->fr,(unsigned char *) out,done,mp); |
261 | 216 break; |
217 } | |
218 | |
219 mp->fsizeold = mp->framesize; | |
220 mp->framesize = 0; | |
221 | |
222 return MP3_OK; | |
223 } | |
224 | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
225 int set_pointer(long backstep, struct mpstr *mp) |
261 | 226 { |
227 unsigned char *bsbufold; | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
228 if(mp->fsizeold < 0 && backstep > 0) { |
400 | 229 char err[128]; |
230 snprintf(err, sizeof (err), "MPGLIB: Can't step back! %ld!", backstep); | |
231 BAIL_MACRO(err, MP3_ERR); | |
261 | 232 } |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
233 bsbufold = mp->bsspace[mp->bsnum] + 512; |
261 | 234 wordpointer -= backstep; |
235 if (backstep) | |
279
52b9f37998db
Removed global state variable; should be thread safe now.
Ryan C. Gordon <icculus@icculus.org>
parents:
274
diff
changeset
|
236 memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep); |
261 | 237 bitindex = 0; |
238 return MP3_OK; | |
239 } | |
240 | |
241 | |
242 | |
243 |