comparison decoders/au.c @ 217:9bab949e2318

Fixed memory leak I introduced, mangled coding style some more. :)
author Ryan C. Gordon <icculus@icculus.org>
date Sun, 13 Jan 2002 21:34:46 +0000
parents 948b62500a54
children c9772a9f5271
comparison
equal deleted inserted replaced
216:07d0939d40e7 217:9bab949e2318
73 73
74 /* no init/deinit needed */ 74 /* no init/deinit needed */
75 static int AU_init(void) 75 static int AU_init(void)
76 { 76 {
77 return(1); 77 return(1);
78 } 78 } /* AU_init */
79 79
80 static void AU_quit(void) 80 static void AU_quit(void)
81 { 81 {
82 /* no-op. */ 82 /* no-op. */
83 } 83 } /* AU_quit */
84 84
85 struct au_file_hdr 85 struct au_file_hdr
86 { 86 {
87 Uint32 magic; 87 Uint32 magic;
88 Uint32 hdr_size; 88 Uint32 hdr_size;
136 r = SDL_RWread(rw, &hdr, 1, HDR_SIZE); 136 r = SDL_RWread(rw, &hdr, 1, HDR_SIZE);
137 if (r < HDR_SIZE) 137 if (r < HDR_SIZE)
138 { 138 {
139 Sound_SetError("No .au file (bad header)"); 139 Sound_SetError("No .au file (bad header)");
140 free(dec); 140 free(dec);
141 return 0; 141 return(0);
142 } 142 } /* if */
143 143
144 if (SDL_SwapLE32(hdr.magic) == AU_MAGIC) 144 if (SDL_SwapLE32(hdr.magic) == AU_MAGIC)
145 { 145 {
146 /* valid magic */ 146 /* valid magic */
147 dec->encoding = SDL_SwapBE32(hdr.encoding); 147 dec->encoding = SDL_SwapBE32(hdr.encoding);
164 164
165 default: 165 default:
166 Sound_SetError("Unsupported .au encoding"); 166 Sound_SetError("Unsupported .au encoding");
167 free(dec); 167 free(dec);
168 return 0; 168 return 0;
169 } 169 } /* switch */
170 170
171 sample->actual.rate = SDL_SwapBE32(hdr.sample_rate); 171 sample->actual.rate = SDL_SwapBE32(hdr.sample_rate);
172 sample->actual.channels = SDL_SwapBE32(hdr.channels); 172 sample->actual.channels = SDL_SwapBE32(hdr.channels);
173 dec->remaining = SDL_SwapBE32(hdr.data_size); 173 dec->remaining = SDL_SwapBE32(hdr.data_size);
174 hsize = SDL_SwapBE32(hdr.hdr_size); 174 hsize = SDL_SwapBE32(hdr.hdr_size);
175 175
176 /* skip remaining part of header (input may be unseekable) */ 176 /* skip remaining part of header (input may be unseekable) */
177 for (i = HDR_SIZE; i < hsize; i++) 177 for (i = HDR_SIZE; i < hsize; i++)
178 SDL_RWread(rw, &c, 1, 1); 178 SDL_RWread(rw, &c, 1, 1);
179 } 179 } /* if */
180 180
181 else if (__Sound_strcasecmp(ext, "au") == 0) 181 else if (__Sound_strcasecmp(ext, "au") == 0)
182 { 182 {
183 /* 183 /*
184 * A number of files in the wild have the .au extension but no valid 184 * A number of files in the wild have the .au extension but no valid
192 dec->encoding = AU_ENC_ULAW_8; 192 dec->encoding = AU_ENC_ULAW_8;
193 dec->remaining = (Uint32)-1; /* no limit */ 193 dec->remaining = (Uint32)-1; /* no limit */
194 sample->actual.format = AUDIO_S16SYS; 194 sample->actual.format = AUDIO_S16SYS;
195 sample->actual.rate = 8000; 195 sample->actual.rate = 8000;
196 sample->actual.channels = 1; 196 sample->actual.channels = 1;
197 } 197 } /* else if */
198
199 else
200 {
201 SNDDBG(("AU: Not an .AU stream.\n"));
202 free(dec);
203 return(0);
204 } /* else */
198 205
199 sample->flags = SOUND_SAMPLEFLAG_NONE; 206 sample->flags = SOUND_SAMPLEFLAG_NONE;
200 207
201 SNDDBG(("AU: Accepting data stream.\n")); 208 SNDDBG(("AU: Accepting data stream.\n"));
202 return 1; 209 return(1);
203 } 210 } /* AU_open */
204 211
205 212
206 static void AU_close(Sound_Sample *sample) 213 static void AU_close(Sound_Sample *sample)
207 { 214 {
208 Sound_SampleInternal *internal = sample->opaque; 215 Sound_SampleInternal *internal = sample->opaque;
209 free(internal->decoder_private); 216 free(internal->decoder_private);
210 } 217 } /* AU_close */
218
211 219
212 /* table to convert from µ-law encoding to signed 16-bit samples, 220 /* table to convert from µ-law encoding to signed 16-bit samples,
213 generated by a throwaway perl script */ 221 generated by a throwaway perl script */
214 static Sint16 ulaw_to_linear[256] = { 222 static Sint16 ulaw_to_linear[256] = {
215 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956, 223 -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
244 244, 228, 212, 196, 180, 164, 148, 132, 252 244, 228, 212, 196, 180, 164, 148, 132,
245 120, 112, 104, 96, 88, 80, 72, 64, 253 120, 112, 104, 96, 88, 80, 72, 64,
246 56, 48, 40, 32, 24, 16, 8, 0 254 56, 48, 40, 32, 24, 16, 8, 0
247 }; 255 };
248 256
257
249 static Uint32 AU_read(Sound_Sample *sample) 258 static Uint32 AU_read(Sound_Sample *sample)
250 { 259 {
251 int ret; 260 int ret;
252 Sound_SampleInternal *internal = sample->opaque; 261 Sound_SampleInternal *internal = sample->opaque;
253 struct audec *dec = internal->decoder_private; 262 struct audec *dec = internal->decoder_private;
260 { 269 {
261 /* We read µ-law samples into the second half of the buffer, so 270 /* We read µ-law samples into the second half of the buffer, so
262 we can expand them to 16-bit samples afterwards */ 271 we can expand them to 16-bit samples afterwards */
263 maxlen >>= 1; 272 maxlen >>= 1;
264 buf += maxlen; 273 buf += maxlen;
265 } 274 } /* if */
266 275
267 if(maxlen > dec->remaining) 276 if(maxlen > dec->remaining)
268 maxlen = dec->remaining; 277 maxlen = dec->remaining;
269 ret = SDL_RWread(internal->rw, buf, 1, maxlen); 278 ret = SDL_RWread(internal->rw, buf, 1, maxlen);
270 if (ret == 0) 279 if (ret == 0)
282 int i; 291 int i;
283 Sint16 *dst = internal->buffer; 292 Sint16 *dst = internal->buffer;
284 for (i = 0; i < ret; i++) 293 for (i = 0; i < ret; i++)
285 dst[i] = ulaw_to_linear[buf[i]]; 294 dst[i] = ulaw_to_linear[buf[i]];
286 ret <<= 1; /* return twice as much as read */ 295 ret <<= 1; /* return twice as much as read */
287 } 296 } /* if */
288 } 297 } /* else */
289 298
290 return ret; 299 return(ret);
291 } 300 } /* AU_read */
292 301
293 #endif /* SOUND_SUPPORTS_AU */ 302 #endif /* SOUND_SUPPORTS_AU */
294 303