comparison decoders/flac.c @ 312:498240aa76f1

Seek implementations.
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 24 Apr 2002 20:47:21 +0000
parents c97be6e1bd27
children cbb15ecf423a
comparison
equal deleted inserted replaced
311:d62c05322a3e 312:498240aa76f1
44 44
45 #define __SDL_SOUND_INTERNAL__ 45 #define __SDL_SOUND_INTERNAL__
46 #include "SDL_sound_internal.h" 46 #include "SDL_sound_internal.h"
47 47
48 /* 48 /*
49 * libFLAC 1.0.1 added a seekable stream decoder, but if I understand the 49 * FLAC 1.0.1 added a seekable stream decoder. To be able to reuse as much as
50 * documentation correctly it's still much easier for us to handle the rewind 50 * possible of the non-seekable FLAC decoder, we define a set of wrapper
51 * method ourselves. 51 * macros and typedefs to map onto the right set of functions and data types.
52 *
53 * An added benefit is that we get identifiers of manageable length.
52 */ 54 */
53 55
56 #if SOUND_SUPPORTS_SEEKABLE_FLAC
57
58 #include "FLAC/seekable_stream_decoder.h"
59
60 #define D_END_OF_STREAM FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM
61
62 #define d_new() FLAC__seekable_stream_decoder_new()
63 #define d_init(x) FLAC__seekable_stream_decoder_init(x)
64 #define d_process_metadata(x) FLAC__seekable_stream_decoder_process_metadata(x)
65 #define d_process_one_frame(x) FLAC__seekable_stream_decoder_process_one_frame(x)
66 #define d_get_state(x) FLAC__seekable_stream_decoder_get_state(x)
67 #define d_finish(x) FLAC__seekable_stream_decoder_finish(x)
68 #define d_delete(x) FLAC__seekable_stream_decoder_delete(x)
69 #define d_set_read_callback(x, y) FLAC__seekable_stream_decoder_set_read_callback(x, y)
70 #define d_set_write_callback(x, y) FLAC__seekable_stream_decoder_set_write_callback(x, y)
71 #define d_set_metadata_callback(x, y) FLAC__seekable_stream_decoder_set_metadata_callback(x, y)
72 #define d_set_error_callback(x, y) FLAC__seekable_stream_decoder_set_error_callback(x, y)
73 #define d_set_client_data(x, y) FLAC__seekable_stream_decoder_set_client_data(x, y)
74
75 typedef FLAC__SeekableStreamDecoder decoder_t;
76 typedef FLAC__SeekableStreamDecoderReadStatus d_read_status_t;
77
78 /* Only in the seekable decoder */
79
80 #define D_SEEK_STATUS_OK FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK
81 #define D_SEEK_STATUS_ERROR FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR
82 #define D_TELL_STATUS_OK FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK
83 #define D_TELL_STATUS_ERROR FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR
84 #define D_LENGTH_STATUS_OK FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK
85 #define D_LENGTH_STATUS_ERROR FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR
86
87 #define d_set_seek_callback(x, y) FLAC__seekable_stream_decoder_set_seek_callback(x, y)
88 #define d_set_tell_callback(x, y) FLAC__seekable_stream_decoder_set_tell_callback(x, y)
89 #define d_set_length_callback(x, y) FLAC__seekable_stream_decoder_set_length_callback(x, y)
90 #define d_set_eof_callback(x, y) FLAC__seekable_stream_decoder_set_eof_callback(x, y)
91 #define d_seek_absolute(x, y) FLAC__seekable_stream_decoder_seek_absolute(x, y)
92
93 typedef FLAC__SeekableStreamDecoderSeekStatus d_seek_status_t;
94 typedef FLAC__SeekableStreamDecoderTellStatus d_tell_status_t;
95 typedef FLAC__SeekableStreamDecoderLengthStatus d_length_status_t;
96
97 #else
98
54 #include "FLAC/stream_decoder.h" 99 #include "FLAC/stream_decoder.h"
100
101 #define D_END_OF_STREAM FLAC__STREAM_DECODER_END_OF_STREAM
102
103 #define d_new() FLAC__stream_decoder_new()
104 #define d_init(x) FLAC__stream_decoder_init(x)
105 #define d_process_metadata(x) FLAC__stream_decoder_process_metadata(x)
106 #define d_process_one_frame(x) FLAC__stream_decoder_process_one_frame(x)
107 #define d_get_state(x) FLAC__stream_decoder_get_state(x)
108 #define d_finish(x) FLAC__stream_decoder_finish(x)
109 #define d_delete(x) FLAC__stream_decoder_delete(x)
110 #define d_set_read_callback(x, y) FLAC__stream_decoder_set_read_callback(x, y)
111 #define d_set_write_callback(x, y) FLAC__stream_decoder_set_write_callback(x, y)
112 #define d_set_metadata_callback(x, y) FLAC__stream_decoder_set_metadata_callback(x, y)
113 #define d_set_error_callback(x, y) FLAC__stream_decoder_set_error_callback(x, y)
114 #define d_set_client_data(x, y) FLAC__stream_decoder_set_client_data(x, y)
115
116 typedef FLAC__StreamDecoder decoder_t;
117 typedef FLAC__StreamDecoderReadStatus d_read_status_t;
118
119 /* Only in the non-seekable decoder */
120
121 #define d_reset(x) FLAC__stream_decoder_reset(x)
122
123 #endif
124
125 /* These are the same for both decoders, so they're just cosmetics. */
126
127 #define D_WRITE_CONTINUE FLAC__STREAM_DECODER_WRITE_CONTINUE
128 #define D_READ_END_OF_STREAM FLAC__STREAM_DECODER_READ_END_OF_STREAM
129 #define D_READ_ABORT FLAC__STREAM_DECODER_READ_ABORT
130 #define D_READ_CONTINUE FLAC__STREAM_DECODER_READ_CONTINUE
131
132 #define d_error_status_string FLAC__StreamDecoderErrorStatusString
133
134 typedef FLAC__StreamDecoderWriteStatus d_write_status_t;
135 typedef FLAC__StreamDecoderErrorStatus d_error_status_t;
136 typedef FLAC__StreamMetaData d_metadata_t;
55 137
56 138
57 static int FLAC_init(void); 139 static int FLAC_init(void);
58 static void FLAC_quit(void); 140 static void FLAC_quit(void);
59 static int FLAC_open(Sound_Sample *sample, const char *ext); 141 static int FLAC_open(Sound_Sample *sample, const char *ext);
83 }; 165 };
84 166
85 /* This is what we store in our internal->decoder_private field. */ 167 /* This is what we store in our internal->decoder_private field. */
86 typedef struct 168 typedef struct
87 { 169 {
88 FLAC__StreamDecoder *decoder; 170 decoder_t *decoder;
89 SDL_RWops *rw; 171 SDL_RWops *rw;
90 Sound_Sample *sample; 172 Sound_Sample *sample;
91 Uint32 data_starting_offset;
92 Uint32 frame_size; 173 Uint32 frame_size;
93 Uint8 is_flac; 174 Uint8 is_flac;
175
176 #if !SOUND_SUPPORTS_SEEKABLE_FLAC
177 Uint32 data_offset;
178 #else
179 Uint32 stream_length;
180 #endif
94 } flac_t; 181 } flac_t;
95 182
96 183
97 static void free_flac(flac_t *f) 184 static void free_flac(flac_t *f)
98 { 185 {
99 FLAC__stream_decoder_finish(f->decoder); 186 d_finish(f->decoder);
100 FLAC__stream_decoder_delete(f->decoder); 187 d_delete(f->decoder);
101 free(f); 188 free(f);
102 } /* free_flac */ 189 } /* free_flac */
103 190
104 191
105 static FLAC__StreamDecoderReadStatus FLAC_read_callback( 192 static d_read_status_t read_callback(
106 const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], 193 const decoder_t *decoder, FLAC__byte buffer[],
107 unsigned int *bytes, void *client_data) 194 unsigned int *bytes, void *client_data)
108 { 195 {
109 flac_t *f = (flac_t *) client_data; 196 flac_t *f = (flac_t *) client_data;
110 Uint32 retval; 197 Uint32 retval;
111 198
112 #if 0
113 SNDDBG(("FLAC: Read callback.\n"));
114 #endif
115
116 retval = SDL_RWread(f->rw, (Uint8 *) buffer, 1, *bytes); 199 retval = SDL_RWread(f->rw, (Uint8 *) buffer, 1, *bytes);
117 200
118 if (retval == 0) 201 if (retval == 0)
119 { 202 {
120 *bytes = 0; 203 *bytes = 0;
121 f->sample->flags |= SOUND_SAMPLEFLAG_EOF; 204 f->sample->flags |= SOUND_SAMPLEFLAG_EOF;
122 return(FLAC__STREAM_DECODER_READ_END_OF_STREAM); 205 return(D_READ_END_OF_STREAM);
123 } /* if */ 206 } /* if */
124 207
125 if (retval == -1) 208 if (retval == -1)
126 { 209 {
127 *bytes = 0; 210 *bytes = 0;
128 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; 211 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR;
129 return(FLAC__STREAM_DECODER_READ_ABORT); 212 return(D_READ_ABORT);
130 } /* if */ 213 } /* if */
131 214
132 if (retval < *bytes) 215 if (retval < *bytes)
133 { 216 {
134 *bytes = retval; 217 *bytes = retval;
135 f->sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; 218 f->sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
136 } /* if */ 219 } /* if */
137 220
138 return(FLAC__STREAM_DECODER_READ_CONTINUE); 221 return(D_READ_CONTINUE);
139 } /* FLAC_read_callback */ 222 } /* read_callback */
140 223
141 224
142 static FLAC__StreamDecoderWriteStatus FLAC_write_callback( 225 static d_write_status_t write_callback(
143 const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, 226 const decoder_t *decoder, const FLAC__Frame *frame,
144 const FLAC__int32 *buffer[], void *client_data) 227 const FLAC__int32 *buffer[], void *client_data)
145 { 228 {
146 flac_t *f = (flac_t *) client_data; 229 flac_t *f = (flac_t *) client_data;
147 Uint32 i, j; 230 Uint32 i, j;
148 Uint32 sample; 231 Uint32 sample;
149 Uint8 *dst; 232 Uint8 *dst;
150
151 #if 0
152 SNDDBG(("FLAC: Write callback.\n"));
153 #endif
154 233
155 f->frame_size = frame->header.channels * frame->header.blocksize 234 f->frame_size = frame->header.channels * frame->header.blocksize
156 * frame->header.bits_per_sample / 8; 235 * frame->header.bits_per_sample / 8;
157 236
158 if (f->frame_size > f->sample->buffer_size) 237 if (f->frame_size > f->sample->buffer_size)
189 *dst++ = (sample & 0xff00) >> 8; 268 *dst++ = (sample & 0xff00) >> 8;
190 *dst++ = sample & 0x00ff; 269 *dst++ = sample & 0x00ff;
191 } /* for */ 270 } /* for */
192 } /* else */ 271 } /* else */
193 272
194 return(FLAC__STREAM_DECODER_WRITE_CONTINUE); 273 return(D_WRITE_CONTINUE);
195 } /* FLAC_write_callback */ 274 } /* write_callback */
196 275
197 276
198 void FLAC_metadata_callback( 277 static void metadata_callback(
199 const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, 278 const decoder_t *decoder,
279 const d_metadata_t *metadata,
200 void *client_data) 280 void *client_data)
201 { 281 {
202 flac_t *f = (flac_t *) client_data; 282 flac_t *f = (flac_t *) client_data;
203 283
204 SNDDBG(("FLAC: Metadata callback.\n")); 284 SNDDBG(("FLAC: Metadata callback.\n"));
205 285
206 /* There are several kinds of metadata, but STREAMINFO is the only 286 /* There are several kinds of metadata, but STREAMINFO is the only
207 * one that always has to be there. 287 * one that always has to be there.
208 */ 288 */
217 if (metadata->data.stream_info.bits_per_sample > 8) 297 if (metadata->data.stream_info.bits_per_sample > 8)
218 f->sample->actual.format = AUDIO_S16MSB; 298 f->sample->actual.format = AUDIO_S16MSB;
219 else 299 else
220 f->sample->actual.format = AUDIO_S8; 300 f->sample->actual.format = AUDIO_S8;
221 } /* if */ 301 } /* if */
222 } /* FLAC_metadata_callback */ 302 } /* metadata_callback */
223 303
224 304
225 void FLAC_error_callback( 305 static void error_callback(
226 const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, 306 const decoder_t *decoder,
307 d_error_status_t status,
227 void *client_data) 308 void *client_data)
228 { 309 {
229 flac_t *f = (flac_t *) client_data; 310 flac_t *f = (flac_t *) client_data;
230 311
231 /* !!! FIXME: Is every error really fatal? I don't know... */ 312 /* !!! FIXME: Is every error really fatal? I don't know... */
232 Sound_SetError(FLAC__StreamDecoderErrorStatusString[status]); 313 Sound_SetError(d_error_status_string[status]);
233 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; 314 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR;
234 } /* FLAC_error_callback */ 315 } /* error_callback */
235 316
317
318 #if SOUND_SUPPORTS_SEEKABLE_FLAC
319
320 static d_seek_status_t seek_callback(
321 const decoder_t *decoder,
322 FLAC__uint64 absolute_byte_offset,
323 void *client_data)
324 {
325 flac_t *f = (flac_t *) client_data;
326
327 if (SDL_RWseek(f->rw, absolute_byte_offset, SEEK_SET) >= 0)
328 {
329 return(D_SEEK_STATUS_OK);
330 } /* if */
331
332 return(D_SEEK_STATUS_ERROR);
333 } /* seek_callback*/
334
335
336 static d_tell_status_t tell_callback(
337 const decoder_t *decoder,
338 FLAC__uint64 *absolute_byte_offset,
339 void *client_data)
340 {
341 flac_t *f = (flac_t *) client_data;
342 int pos; /* !!! FIXME: int? Really? */
343
344 pos = SDL_RWtell(f->rw);
345
346 if (pos < 0)
347 {
348 return(D_TELL_STATUS_ERROR);
349 } /* if */
350
351 *absolute_byte_offset = pos;
352 return(D_TELL_STATUS_OK);
353 } /* tell_callback */
354
355
356 static d_length_status_t length_callback(
357 const decoder_t *decoder,
358 FLAC__uint64 *stream_length,
359 void *client_data)
360 {
361 flac_t *f = (flac_t *) client_data;
362
363 if (f->sample->flags & SOUND_SAMPLEFLAG_CANSEEK)
364 {
365 *stream_length = f->stream_length;
366 return(D_LENGTH_STATUS_OK);
367 } /* if */
368
369 return(D_LENGTH_STATUS_ERROR);
370 } /* length_callback */
371
372
373 static FLAC__bool eof_callback(
374 const decoder_t *decoder,
375 void *client_data)
376 {
377 flac_t *f = (flac_t *) client_data;
378 int pos; /* !!! FIXME: int? Really? */
379
380 /* Maybe we could check for SOUND_SAMPLEFLAG_EOF here instead? */
381 pos = SDL_RWtell(f->rw);
382
383 if (pos >= 0 && pos >= f->stream_length)
384 {
385 return(true);
386 } /* if */
387
388 return(false);
389 } /* eof_callback */
390
391 #endif
236 392
237 static int FLAC_init(void) 393 static int FLAC_init(void)
238 { 394 {
239 return(1); /* always succeeds. */ 395 return(1); /* always succeeds. */
240 } /* FLAC_init */ 396 } /* FLAC_init */
250 406
251 static int FLAC_open(Sound_Sample *sample, const char *ext) 407 static int FLAC_open(Sound_Sample *sample, const char *ext)
252 { 408 {
253 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; 409 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
254 SDL_RWops *rw = internal->rw; 410 SDL_RWops *rw = internal->rw;
255 FLAC__StreamDecoder *decoder; 411 decoder_t *decoder;
256 flac_t *f; 412 flac_t *f;
257 int i; 413 int i;
258 int has_extension = 0; 414 int has_extension = 0;
415
416 #if SOUND_SUPPORTS_SEEKABLE_FLAC
417 Uint32 pos;
418 #endif
259 419
260 /* 420 /*
261 * If the extension is "flac", we'll believe that this is really meant 421 * If the extension is "flac", we'll believe that this is really meant
262 * to be a FLAC stream, and will try to grok it from existing metadata. 422 * to be a FLAC stream, and will try to grok it from existing metadata.
263 * metadata searching can be a very expensive operation, however, so 423 * metadata searching can be a very expensive operation, however, so
285 } /* if */ 445 } /* if */
286 446
287 f = (flac_t *) malloc(sizeof (flac_t)); 447 f = (flac_t *) malloc(sizeof (flac_t));
288 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0); 448 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0);
289 449
290 decoder = FLAC__stream_decoder_new(); 450 decoder = d_new();
291 if (decoder == NULL) 451 if (decoder == NULL)
292 { 452 {
293 Sound_SetError(ERR_OUT_OF_MEMORY); 453 Sound_SetError(ERR_OUT_OF_MEMORY);
294 free(f); 454 free(f);
295 return(0); 455 return(0);
296 } /* if */ 456 } /* if */
297 457
298 FLAC__stream_decoder_set_read_callback(decoder, FLAC_read_callback); 458 d_set_read_callback(decoder, read_callback);
299 FLAC__stream_decoder_set_write_callback(decoder, FLAC_write_callback); 459 d_set_write_callback(decoder, write_callback);
300 FLAC__stream_decoder_set_metadata_callback(decoder, FLAC_metadata_callback); 460 d_set_metadata_callback(decoder, metadata_callback);
301 FLAC__stream_decoder_set_error_callback(decoder, FLAC_error_callback); 461 d_set_error_callback(decoder, error_callback);
302 FLAC__stream_decoder_set_client_data(decoder, f); 462
463 #if SOUND_SUPPORTS_SEEKABLE_FLAC
464 d_set_seek_callback(decoder, seek_callback);
465 d_set_tell_callback(decoder, tell_callback);
466 d_set_length_callback(decoder, length_callback);
467 d_set_eof_callback(decoder, eof_callback);
468 #endif
469
470 d_set_client_data(decoder, f);
303 471
304 f->rw = internal->rw; 472 f->rw = internal->rw;
305 f->sample = sample; 473 f->sample = sample;
306 f->decoder = decoder; 474 f->decoder = decoder;
307 f->sample->actual.format = 0; 475 f->sample->actual.format = 0;
308 f->is_flac = 0 /* !!! FIXME: should be "has_extension", not "0". */; 476 f->is_flac = 0 /* !!! FIXME: should be "has_extension", not "0". */;
309 477
310 internal->decoder_private = f; 478 internal->decoder_private = f;
311 FLAC__stream_decoder_init(decoder); 479 d_init(decoder);
312 480
481 #if !SOUND_SUPPORTS_SEEKABLE_FLAC
313 /* 482 /*
314 * Annoyingly, the rewind method will put the FLAC decoder in a state 483 * Annoyingly, the rewind method will put the FLAC decoder in a state
315 * where it expects to read metadata, so we have to set this marker 484 * where it expects to read metadata, so we have to set this marker
316 * before the metadata block. 485 * before the metadata block.
317 */ 486 */
318 f->data_starting_offset = SDL_RWtell(f->rw); 487 f->data_offset = SDL_RWtell(f->rw);
488 #endif
319 489
490 sample->flags = SOUND_SAMPLEFLAG_NONE;
491
492 #if SOUND_SUPPORTS_SEEKABLE_FLAC
493 /*
494 * FIXME?: For the seekable stream decoder to work, we need to know
495 * the length of the stream. This is so ugly...
496 */
497 pos = SDL_RWtell(f->rw);
498 if (SDL_RWseek(f->rw, 0, SEEK_END))
499 {
500 f->stream_length = SDL_RWtell(f->rw);
501 SDL_RWseek(f->rw, pos, SEEK_SET);
502 sample->flags = SOUND_SAMPLEFLAG_CANSEEK;
503 } /* if */
504 #endif
505
320 /* 506 /*
321 * If we are not sure this is a FLAC stream, check for the STREAMINFO 507 * If we are not sure this is a FLAC stream, check for the STREAMINFO
322 * metadata block. If not, we'd have to peek at the first audio frame 508 * metadata block. If not, we'd have to peek at the first audio frame
323 * and get the sound format from there, but that is not yet 509 * and get the sound format from there, but that is not yet
324 * implemented. 510 * implemented.
325 */ 511 */
326 if (!f->is_flac) 512 if (!f->is_flac)
327 { 513 {
328 FLAC__stream_decoder_process_metadata(decoder); 514 d_process_metadata(decoder);
329 515
330 /* Still not FLAC? Give up. */ 516 /* Still not FLAC? Give up. */
331 if (!f->is_flac) 517 if (!f->is_flac)
332 { 518 {
333 Sound_SetError("FLAC: No metadata found. Not a FLAC stream?"); 519 Sound_SetError("FLAC: No metadata found. Not a FLAC stream?");
335 return(0); 521 return(0);
336 } /* if */ 522 } /* if */
337 } /* if */ 523 } /* if */
338 524
339 SNDDBG(("FLAC: Accepting data stream.\n")); 525 SNDDBG(("FLAC: Accepting data stream.\n"));
340
341 sample->flags = SOUND_SAMPLEFLAG_NONE;
342 return(1); 526 return(1);
343 } /* FLAC_open */ 527 } /* FLAC_open */
344 528
345 529
346 static void FLAC_close(Sound_Sample *sample) 530 static void FLAC_close(Sound_Sample *sample)
356 { 540 {
357 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; 541 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
358 flac_t *f = (flac_t *) internal->decoder_private; 542 flac_t *f = (flac_t *) internal->decoder_private;
359 Uint32 len; 543 Uint32 len;
360 544
361 if (!FLAC__stream_decoder_process_one_frame(f->decoder)) 545 if (!d_process_one_frame(f->decoder))
362 { 546 {
363 Sound_SetError("FLAC: Couldn't decode frame."); 547 Sound_SetError("FLAC: Couldn't decode frame.");
364 sample->flags |= SOUND_SAMPLEFLAG_ERROR; 548 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
365 return(0); 549 return(0);
366 } /* if */ 550 } /* if */
367 551
368 if (FLAC__stream_decoder_get_state(f->decoder) == FLAC__STREAM_DECODER_END_OF_STREAM) 552 if (d_get_state(f->decoder) == D_END_OF_STREAM)
369 { 553 {
370 sample->flags |= SOUND_SAMPLEFLAG_EOF; 554 sample->flags |= SOUND_SAMPLEFLAG_EOF;
371 return(0); 555 return(0);
372 } /* if */ 556 } /* if */
373 557
379 } /* FLAC_read */ 563 } /* FLAC_read */
380 564
381 565
382 static int FLAC_rewind(Sound_Sample *sample) 566 static int FLAC_rewind(Sound_Sample *sample)
383 { 567 {
568 #if SOUND_SUPPORTS_SEEKABLE_FLAC
569 return FLAC_seek(sample, 0);
570 #else
384 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; 571 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
385 flac_t *f = (flac_t *) internal->decoder_private; 572 flac_t *f = (flac_t *) internal->decoder_private;
386 int rc = SDL_RWseek(f->rw, f->data_starting_offset, SEEK_SET); 573 int rc = SDL_RWseek(f->rw, f->data_offset, SEEK_SET);
387 574
388 BAIL_IF_MACRO(rc != f->data_starting_offset, ERR_IO_ERROR, 0); 575 BAIL_IF_MACRO(rc != f->data_offset, ERR_IO_ERROR, 0);
389 BAIL_IF_MACRO(!FLAC__stream_decoder_reset(f->decoder), 576 BAIL_IF_MACRO(!d_reset(f->decoder), "FLAC: could not reset decoder", 0);
390 "FLAC: could not reset decoder", 0); 577 d_process_metadata(f->decoder);
391 FLAC__stream_decoder_process_metadata(f->decoder);
392 return(1); 578 return(1);
579 #endif
393 } /* FLAC_rewind */ 580 } /* FLAC_rewind */
394 581
395 582
396 static int FLAC_seek(Sound_Sample *sample, Uint32 ms) 583 static int FLAC_seek(Sound_Sample *sample, Uint32 ms)
397 { 584 {
398 BAIL_MACRO("!!! FIXME: Not implemented", 0); 585 #if SOUND_SUPPORTS_SEEKABLE_FLAC
586 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
587 flac_t *f = (flac_t *) internal->decoder_private;
588
589 d_seek_absolute(f->decoder, (ms * sample->actual.rate) / 1000);
590 return(1);
591 #else
592 BAIL_MACRO("FLAC: This is the non-seekable version of the decoder!", 0);
593 #endif
399 } /* FLAC_seek */ 594 } /* FLAC_seek */
400 595
401 596
402 #endif /* SOUND_SUPPORTS_FLAC */ 597 #endif /* SOUND_SUPPORTS_FLAC */
403 598