Mercurial > SDL_sound_CoreAudio
annotate decoders/mpglib/interface.c @ 274:9e7f9e09ea0e
Removed fprintf() calls. Replaced with SNDDBG() and Sound_SetError() calls.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Sun, 10 Mar 2002 19:15:25 +0000 |
parents | e1429f96aded |
children | 52b9f37998db |
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 */ | |
14 struct mpstr *gmp; | |
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 gmp = mp; | |
162 | |
163 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
|
164 Sound_SetError("MPGLIB: Not enough output space for decoding!"); |
261 | 165 return MP3_ERR; |
166 } | |
167 | |
168 if(in) { | |
169 if(addbuf(mp,in,isize) == NULL) { | |
170 return MP3_ERR; | |
171 } | |
172 } | |
173 | |
174 /* First decode header */ | |
175 if(mp->framesize == 0) { | |
176 if(mp->bsize < 4) { | |
177 return MP3_NEED_MORE; | |
178 } | |
273
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
179 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
180 if (!read_head(mp)) |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
181 return MP3_ERR; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
182 |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
183 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
|
184 return MP3_ERR; |
e1429f96aded
Replaced exit() calls with proper error reporting.
Ryan C. Gordon <icculus@icculus.org>
parents:
261
diff
changeset
|
185 |
261 | 186 mp->framesize = mp->fr.framesize; |
187 } | |
188 | |
189 if(mp->fr.framesize > mp->bsize) | |
190 return MP3_NEED_MORE; | |
191 | |
192 wordpointer = mp->bsspace[mp->bsnum] + 512; | |
193 mp->bsnum = (mp->bsnum + 1) & 0x1; | |
194 bitindex = 0; | |
195 | |
196 len = 0; | |
197 while(len < mp->framesize) { | |
198 int nlen; | |
199 int blen = mp->tail->size - mp->tail->pos; | |
200 if( (mp->framesize - len) <= blen) { | |
201 nlen = mp->framesize-len; | |
202 } | |
203 else { | |
204 nlen = blen; | |
205 } | |
206 memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen); | |
207 len += nlen; | |
208 mp->tail->pos += nlen; | |
209 mp->bsize -= nlen; | |
210 if(mp->tail->pos == mp->tail->size) { | |
211 remove_buf(mp); | |
212 } | |
213 } | |
214 | |
215 *done = 0; | |
216 if(mp->fr.error_protection) | |
217 getbits(16); | |
218 switch(mp->fr.lay) { | |
219 case 1: | |
220 do_layer1(&mp->fr,(unsigned char *) out,done); | |
221 break; | |
222 case 2: | |
223 do_layer2(&mp->fr,(unsigned char *) out,done); | |
224 break; | |
225 case 3: | |
226 do_layer3(&mp->fr,(unsigned char *) out,done); | |
227 break; | |
228 } | |
229 | |
230 mp->fsizeold = mp->framesize; | |
231 mp->framesize = 0; | |
232 | |
233 return MP3_OK; | |
234 } | |
235 | |
236 int set_pointer(long backstep) | |
237 { | |
238 unsigned char *bsbufold; | |
239 if(gmp->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
|
240 Sound_SetError("MPGLIB: Can't step back!"); /* FIXME: need formatting: %ld!\n",backstep); */ |
261 | 241 return MP3_ERR; |
242 } | |
243 bsbufold = gmp->bsspace[gmp->bsnum] + 512; | |
244 wordpointer -= backstep; | |
245 if (backstep) | |
246 memcpy(wordpointer,bsbufold+gmp->fsizeold-backstep,backstep); | |
247 bitindex = 0; | |
248 return MP3_OK; | |
249 } | |
250 | |
251 | |
252 | |
253 |