comparison decoders/flac.c @ 182:3849438b735e

Checks magic number again, unless the file extension is recognized.
author Ryan C. Gordon <icculus@icculus.org>
date Mon, 17 Dec 2001 00:26:10 +0000
parents d8904267d23c
children 47cc2de2ae36
comparison
equal deleted inserted replaced
181:993dd477eb4c 182:3849438b735e
233 { 233 {
234 /* it's a no-op. */ 234 /* it's a no-op. */
235 } /* FLAC_quit */ 235 } /* FLAC_quit */
236 236
237 237
238 #define FLAC_MAGIC 0x43614C66 /* "fLaC" in ASCII. */
239
238 static int FLAC_open(Sound_Sample *sample, const char *ext) 240 static int FLAC_open(Sound_Sample *sample, const char *ext)
239 { 241 {
240 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; 242 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
241 SDL_RWops *rw = internal->rw; 243 SDL_RWops *rw = internal->rw;
242 FLAC__StreamDecoder *decoder; 244 FLAC__StreamDecoder *decoder;
243 flac_t *f; 245 flac_t *f;
244 int i; 246 int i;
247 int has_extension = 0;
248
249 /*
250 * If the extension is "flac", we'll believe that this is really meant
251 * to be a FLAC stream, and will try to grok it from existing metadata.
252 * metadata searching can be a very expensive operation, however, so
253 * unless the user swears that it is a FLAC stream through the extension,
254 * we decide what to do based on the existance of a 32-bit magic number.
255 */
256 for (i = 0; extensions_flac[i] != NULL; i++)
257 {
258 if (__Sound_strcasecmp(ext, extensions_flac[i]) == 0)
259 {
260 has_extension = 1;
261 break;
262 } /* if */
263 } /* for */
264
265 if (!has_extension)
266 {
267 int rc;
268 Uint32 flac_magic = SDL_ReadLE32(rw);
269 BAIL_IF_MACRO(flac_magic != FLAC_MAGIC, "FLAC: Not a FLAC stream.", 0);
270
271 /* move back over magic number for metadata scan... */
272 rc = SDL_RWseek(internal->rw, -sizeof (flac_magic), SEEK_CUR);
273 BAIL_IF_MACRO(rc < 0, ERR_IO_ERROR, 0);
274 } /* if */
245 275
246 f = (flac_t *) malloc(sizeof (flac_t)); 276 f = (flac_t *) malloc(sizeof (flac_t));
247 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0); 277 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0);
248 278
249 decoder = FLAC__stream_decoder_new(); 279 decoder = FLAC__stream_decoder_new();
262 292
263 f->rw = internal->rw; 293 f->rw = internal->rw;
264 f->sample = sample; 294 f->sample = sample;
265 f->decoder = decoder; 295 f->decoder = decoder;
266 f->sample->actual.format = 0; 296 f->sample->actual.format = 0;
267 f->is_flac = 0; 297 f->is_flac = 0 /* !!! FIXME: should be "has_extension", not "0". */;
268
269 #if 0
270 /* !!! FIXME:
271 *
272 * It should be possible to play a FLAC stream starting at any frame,
273 * but we can only do that if we know for sure that it is a FLAC
274 * stream. Otherwise we have to check for metadata, and then we need
275 * the entire stream, from the beginning.
276 *
277 * But getting this to work right seems to be an enormous pain in the
278 * butt for what is, at the moment, a very small gain. Maybe later.
279 */
280 for (i = 0; extensions_flac[i] != NULL; i++)
281 if (__Sound_strcasecmp(ext, extensions_flac[i]) == 0)
282 {
283 f->is_flac = 1;
284 break;
285 } /* if */
286 #endif
287 298
288 internal->decoder_private = f; 299 internal->decoder_private = f;
289 FLAC__stream_decoder_init(decoder); 300 FLAC__stream_decoder_init(decoder);
290 301
291 /* If we are not sure this is a FLAC stream, check for the STREAMINFO 302 /*
303 * If we are not sure this is a FLAC stream, check for the STREAMINFO
292 * metadata block. If not, we'd have to peek at the first audio frame 304 * metadata block. If not, we'd have to peek at the first audio frame
293 * and get the sound format from there but, as stated above, that is 305 * and get the sound format from there, but that is not yet
294 * not yet implemented. 306 * implemented.
295 */ 307 */
296 if (!f->is_flac) 308 if (!f->is_flac)
297 { 309 {
298 FLAC__stream_decoder_process_metadata(decoder); 310 FLAC__stream_decoder_process_metadata(decoder);
299 311