Mercurial > SDL_sound_CoreAudio
annotate decoders/flac.c @ 420:5b8a07b5162e
Latest altcvt from Frank.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Thu, 26 Sep 2002 04:34:35 +0000 |
parents | 9d0b5ec9cc26 |
children | 5b06e23d934e |
rev | line source |
---|---|
155 | 1 /* |
2 * SDL_sound -- An abstract sound format decoding API. | |
3 * Copyright (C) 2001 Ryan C. Gordon. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2.1 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /* | |
21 * FLAC decoder for SDL_sound. | |
22 * | |
23 * This driver handles FLAC audio, that is to say the Free Lossless Audio | |
24 * Codec. It depends on libFLAC for decoding, which can be grabbed from: | |
25 * http://flac.sourceforge.net | |
26 * | |
184
47cc2de2ae36
Changed reference to "LICENSE" file to "COPYING".
Ryan C. Gordon <icculus@icculus.org>
parents:
182
diff
changeset
|
27 * Please see the file COPYING in the source's root directory. |
155 | 28 * |
29 * This file written by Torbjörn Andersson. (d91tan@Update.UU.SE) | |
30 */ | |
31 | |
32 #if HAVE_CONFIG_H | |
33 # include <config.h> | |
34 #endif | |
35 | |
36 #ifdef SOUND_SUPPORTS_FLAC | |
37 | |
38 #include <stdio.h> | |
39 #include <stdlib.h> | |
40 #include <string.h> | |
41 | |
42 #include "SDL_sound.h" | |
43 | |
44 #define __SDL_SOUND_INTERNAL__ | |
45 #include "SDL_sound_internal.h" | |
46 | |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
47 /* |
312 | 48 * FLAC 1.0.1 added a seekable stream decoder. To be able to reuse as much as |
49 * possible of the non-seekable FLAC decoder, we define a set of wrapper | |
50 * macros and typedefs to map onto the right set of functions and data types. | |
51 * | |
52 * An added benefit is that we get identifiers of manageable length. | |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
53 */ |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
54 |
312 | 55 #if SOUND_SUPPORTS_SEEKABLE_FLAC |
56 | |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
57 #define FLAC_IS_SEEKABLE 1 |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
58 |
312 | 59 #include "FLAC/seekable_stream_decoder.h" |
60 | |
61 #define D_END_OF_STREAM FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM | |
62 | |
63 #define d_new() FLAC__seekable_stream_decoder_new() | |
64 #define d_init(x) FLAC__seekable_stream_decoder_init(x) | |
65 #define d_process_metadata(x) FLAC__seekable_stream_decoder_process_metadata(x) | |
66 #define d_process_one_frame(x) FLAC__seekable_stream_decoder_process_one_frame(x) | |
67 #define d_get_state(x) FLAC__seekable_stream_decoder_get_state(x) | |
68 #define d_finish(x) FLAC__seekable_stream_decoder_finish(x) | |
69 #define d_delete(x) FLAC__seekable_stream_decoder_delete(x) | |
70 #define d_set_read_callback(x, y) FLAC__seekable_stream_decoder_set_read_callback(x, y) | |
71 #define d_set_write_callback(x, y) FLAC__seekable_stream_decoder_set_write_callback(x, y) | |
72 #define d_set_metadata_callback(x, y) FLAC__seekable_stream_decoder_set_metadata_callback(x, y) | |
73 #define d_set_error_callback(x, y) FLAC__seekable_stream_decoder_set_error_callback(x, y) | |
74 #define d_set_client_data(x, y) FLAC__seekable_stream_decoder_set_client_data(x, y) | |
75 | |
76 typedef FLAC__SeekableStreamDecoder decoder_t; | |
77 typedef FLAC__SeekableStreamDecoderReadStatus d_read_status_t; | |
78 | |
79 /* Only in the seekable decoder */ | |
80 | |
81 #define D_SEEK_STATUS_OK FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK | |
82 #define D_SEEK_STATUS_ERROR FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR | |
83 #define D_TELL_STATUS_OK FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK | |
84 #define D_TELL_STATUS_ERROR FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR | |
85 #define D_LENGTH_STATUS_OK FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK | |
86 #define D_LENGTH_STATUS_ERROR FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR | |
87 | |
88 #define d_set_seek_callback(x, y) FLAC__seekable_stream_decoder_set_seek_callback(x, y) | |
89 #define d_set_tell_callback(x, y) FLAC__seekable_stream_decoder_set_tell_callback(x, y) | |
90 #define d_set_length_callback(x, y) FLAC__seekable_stream_decoder_set_length_callback(x, y) | |
91 #define d_set_eof_callback(x, y) FLAC__seekable_stream_decoder_set_eof_callback(x, y) | |
92 #define d_seek_absolute(x, y) FLAC__seekable_stream_decoder_seek_absolute(x, y) | |
93 | |
94 typedef FLAC__SeekableStreamDecoderSeekStatus d_seek_status_t; | |
95 typedef FLAC__SeekableStreamDecoderTellStatus d_tell_status_t; | |
96 typedef FLAC__SeekableStreamDecoderLengthStatus d_length_status_t; | |
97 | |
98 #else | |
99 | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
100 #include "FLAC/stream_decoder.h" |
155 | 101 |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
102 #define FLAC_IS_SEEKABLE 0 |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
103 |
312 | 104 #define D_END_OF_STREAM FLAC__STREAM_DECODER_END_OF_STREAM |
105 | |
106 #define d_new() FLAC__stream_decoder_new() | |
107 #define d_init(x) FLAC__stream_decoder_init(x) | |
108 #define d_process_metadata(x) FLAC__stream_decoder_process_metadata(x) | |
109 #define d_process_one_frame(x) FLAC__stream_decoder_process_one_frame(x) | |
110 #define d_get_state(x) FLAC__stream_decoder_get_state(x) | |
111 #define d_finish(x) FLAC__stream_decoder_finish(x) | |
112 #define d_delete(x) FLAC__stream_decoder_delete(x) | |
113 #define d_set_read_callback(x, y) FLAC__stream_decoder_set_read_callback(x, y) | |
114 #define d_set_write_callback(x, y) FLAC__stream_decoder_set_write_callback(x, y) | |
115 #define d_set_metadata_callback(x, y) FLAC__stream_decoder_set_metadata_callback(x, y) | |
116 #define d_set_error_callback(x, y) FLAC__stream_decoder_set_error_callback(x, y) | |
117 #define d_set_client_data(x, y) FLAC__stream_decoder_set_client_data(x, y) | |
118 | |
119 typedef FLAC__StreamDecoder decoder_t; | |
120 typedef FLAC__StreamDecoderReadStatus d_read_status_t; | |
121 | |
122 /* Only in the non-seekable decoder */ | |
123 | |
124 #define d_reset(x) FLAC__stream_decoder_reset(x) | |
125 | |
126 #endif | |
127 | |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
128 /* |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
129 * FLAC 1.0.3 changed some symbol names, so we need to change what we |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
130 * reference depending on what version of their headers we compile against. |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
131 * We check for a #define that was included in FLAC 1.0.3 but doesn't exist |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
132 * in 1.0.2 and earlier. Fun. --ryan. |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
133 */ |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
134 #if (defined FLAC__STREAM_SYNC_LENGTH) |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
135 #define FLAC_VERSION_102_OR_LESS 0 |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
136 #else |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
137 #define FLAC_VERSION_102_OR_LESS 1 |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
138 #endif |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
139 |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
140 |
312 | 141 /* These are the same for both decoders, so they're just cosmetics. */ |
142 | |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
143 #if FLAC_VERSION_102_OR_LESS |
312 | 144 #define D_WRITE_CONTINUE FLAC__STREAM_DECODER_WRITE_CONTINUE |
145 #define D_READ_END_OF_STREAM FLAC__STREAM_DECODER_READ_END_OF_STREAM | |
146 #define D_READ_ABORT FLAC__STREAM_DECODER_READ_ABORT | |
147 #define D_READ_CONTINUE FLAC__STREAM_DECODER_READ_CONTINUE | |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
148 #else |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
149 #define D_WRITE_CONTINUE FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
150 #define D_READ_END_OF_STREAM FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
151 #define D_READ_ABORT FLAC__STREAM_DECODER_READ_STATUS_ABORT |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
152 #define D_READ_CONTINUE FLAC__STREAM_DECODER_READ_STATUS_CONTINUE |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
153 #endif |
312 | 154 |
155 #define d_error_status_string FLAC__StreamDecoderErrorStatusString | |
156 | |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
157 typedef FLAC__StreamDecoderErrorStatus d_error_status_t; |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
158 #if FLAC_VERSION_102_OR_LESS |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
159 typedef FLAC__StreamMetaData d_metadata_t; |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
160 #else |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
161 typedef FLAC__StreamMetadata d_metadata_t; |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
162 #endif |
312 | 163 typedef FLAC__StreamDecoderWriteStatus d_write_status_t; |
164 | |
155 | 165 |
166 static int FLAC_init(void); | |
167 static void FLAC_quit(void); | |
168 static int FLAC_open(Sound_Sample *sample, const char *ext); | |
169 static void FLAC_close(Sound_Sample *sample); | |
170 static Uint32 FLAC_read(Sound_Sample *sample); | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
171 static int FLAC_rewind(Sound_Sample *sample); |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
172 static int FLAC_seek(Sound_Sample *sample, Uint32 ms); |
155 | 173 |
174 static const char *extensions_flac[] = { "FLAC", "FLA", NULL }; | |
175 | |
176 const Sound_DecoderFunctions __Sound_DecoderFunctions_FLAC = | |
177 { | |
178 { | |
179 extensions_flac, | |
180 "Free Lossless Audio Codec", | |
181 "Torbjörn Andersson <d91tan@Update.UU.SE>", | |
182 "http://flac.sourceforge.net/" | |
183 }, | |
184 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
185 FLAC_init, /* init() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
186 FLAC_quit, /* quit() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
187 FLAC_open, /* open() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
188 FLAC_close, /* close() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
189 FLAC_read, /* read() method */ |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
190 FLAC_rewind, /* rewind() method */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
191 FLAC_seek /* seek() method */ |
155 | 192 }; |
193 | |
194 /* This is what we store in our internal->decoder_private field. */ | |
195 typedef struct | |
196 { | |
312 | 197 decoder_t *decoder; |
155 | 198 SDL_RWops *rw; |
199 Sound_Sample *sample; | |
200 Uint32 frame_size; | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
201 Uint8 is_flac; |
312 | 202 |
203 #if !SOUND_SUPPORTS_SEEKABLE_FLAC | |
204 Uint32 data_offset; | |
205 #else | |
206 Uint32 stream_length; | |
207 #endif | |
155 | 208 } flac_t; |
209 | |
210 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
211 static void free_flac(flac_t *f) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
212 { |
312 | 213 d_finish(f->decoder); |
214 d_delete(f->decoder); | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
215 free(f); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
216 } /* free_flac */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
217 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
218 |
312 | 219 static d_read_status_t read_callback( |
220 const decoder_t *decoder, FLAC__byte buffer[], | |
155 | 221 unsigned int *bytes, void *client_data) |
222 { | |
223 flac_t *f = (flac_t *) client_data; | |
224 Uint32 retval; | |
225 | |
226 retval = SDL_RWread(f->rw, (Uint8 *) buffer, 1, *bytes); | |
227 | |
228 if (retval == 0) | |
229 { | |
230 *bytes = 0; | |
231 f->sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
312 | 232 return(D_READ_END_OF_STREAM); |
155 | 233 } /* if */ |
234 | |
235 if (retval == -1) | |
236 { | |
237 *bytes = 0; | |
238 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
312 | 239 return(D_READ_ABORT); |
155 | 240 } /* if */ |
241 | |
242 if (retval < *bytes) | |
243 { | |
244 *bytes = retval; | |
245 f->sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
246 } /* if */ | |
247 | |
312 | 248 return(D_READ_CONTINUE); |
249 } /* read_callback */ | |
155 | 250 |
251 | |
312 | 252 static d_write_status_t write_callback( |
253 const decoder_t *decoder, const FLAC__Frame *frame, | |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
254 #if FLAC_VERSION_102_OR_LESS |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
255 const FLAC__int32 * buffer[], |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
256 #else |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
257 const FLAC__int32 * const buffer[], |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
258 #endif |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
259 void *client_data) |
155 | 260 { |
261 flac_t *f = (flac_t *) client_data; | |
262 Uint32 i, j; | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
263 Uint32 sample; |
155 | 264 Uint8 *dst; |
265 | |
266 f->frame_size = frame->header.channels * frame->header.blocksize | |
267 * frame->header.bits_per_sample / 8; | |
268 | |
269 if (f->frame_size > f->sample->buffer_size) | |
270 Sound_SetBufferSize(f->sample, f->frame_size); | |
271 | |
272 dst = f->sample->buffer; | |
273 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
274 /* If the sample is neither exactly 8-bit nor 16-bit, it will have to |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
275 * be converted. Unfortunately the buffer is read-only, so we either |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
276 * have to check for each sample, or make a copy of the buffer. I'm |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
277 * not sure which way is best, so I've arbitrarily picked the former. |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
278 */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
279 if (f->sample->actual.format == AUDIO_S8) |
155 | 280 { |
281 for (i = 0; i < frame->header.blocksize; i++) | |
282 for (j = 0; j < frame->header.channels; j++) | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
283 { |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
284 sample = buffer[j][i]; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
285 if (frame->header.bits_per_sample < 8) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
286 sample <<= (8 - frame->header.bits_per_sample); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
287 *dst++ = sample & 0x00ff; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
288 } /* for */ |
155 | 289 } /* if */ |
290 else | |
291 { | |
292 for (i = 0; i < frame->header.blocksize; i++) | |
293 for (j = 0; j < frame->header.channels; j++) | |
294 { | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
295 sample = buffer[j][i]; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
296 if (frame->header.bits_per_sample < 16) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
297 sample <<= (16 - frame->header.bits_per_sample); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
298 else if (frame->header.bits_per_sample > 16) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
299 sample >>= (frame->header.bits_per_sample - 16); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
300 *dst++ = (sample & 0xff00) >> 8; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
301 *dst++ = sample & 0x00ff; |
155 | 302 } /* for */ |
303 } /* else */ | |
304 | |
312 | 305 return(D_WRITE_CONTINUE); |
306 } /* write_callback */ | |
155 | 307 |
308 | |
312 | 309 static void metadata_callback( |
310 const decoder_t *decoder, | |
311 const d_metadata_t *metadata, | |
155 | 312 void *client_data) |
313 { | |
314 flac_t *f = (flac_t *) client_data; | |
312 | 315 |
155 | 316 SNDDBG(("FLAC: Metadata callback.\n")); |
317 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
318 /* There are several kinds of metadata, but STREAMINFO is the only |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
319 * one that always has to be there. |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
320 */ |
155 | 321 if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) |
322 { | |
323 SNDDBG(("FLAC: Metadata is streaminfo.\n")); | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
324 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
325 f->is_flac = 1; |
155 | 326 f->sample->actual.channels = metadata->data.stream_info.channels; |
327 f->sample->actual.rate = metadata->data.stream_info.sample_rate; | |
328 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
329 if (metadata->data.stream_info.bits_per_sample > 8) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
330 f->sample->actual.format = AUDIO_S16MSB; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
331 else |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
332 f->sample->actual.format = AUDIO_S8; |
155 | 333 } /* if */ |
312 | 334 } /* metadata_callback */ |
155 | 335 |
336 | |
312 | 337 static void error_callback( |
338 const decoder_t *decoder, | |
339 d_error_status_t status, | |
155 | 340 void *client_data) |
341 { | |
342 flac_t *f = (flac_t *) client_data; | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
343 |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
377
diff
changeset
|
344 __Sound_SetError(d_error_status_string[status]); |
155 | 345 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; |
312 | 346 } /* error_callback */ |
347 | |
348 | |
349 #if SOUND_SUPPORTS_SEEKABLE_FLAC | |
350 | |
351 static d_seek_status_t seek_callback( | |
352 const decoder_t *decoder, | |
353 FLAC__uint64 absolute_byte_offset, | |
354 void *client_data) | |
355 { | |
356 flac_t *f = (flac_t *) client_data; | |
357 | |
358 if (SDL_RWseek(f->rw, absolute_byte_offset, SEEK_SET) >= 0) | |
359 { | |
360 return(D_SEEK_STATUS_OK); | |
361 } /* if */ | |
362 | |
363 return(D_SEEK_STATUS_ERROR); | |
364 } /* seek_callback*/ | |
365 | |
366 | |
367 static d_tell_status_t tell_callback( | |
368 const decoder_t *decoder, | |
369 FLAC__uint64 *absolute_byte_offset, | |
370 void *client_data) | |
371 { | |
372 flac_t *f = (flac_t *) client_data; | |
400 | 373 int pos; |
312 | 374 |
375 pos = SDL_RWtell(f->rw); | |
376 | |
377 if (pos < 0) | |
378 { | |
379 return(D_TELL_STATUS_ERROR); | |
380 } /* if */ | |
155 | 381 |
312 | 382 *absolute_byte_offset = pos; |
383 return(D_TELL_STATUS_OK); | |
384 } /* tell_callback */ | |
385 | |
386 | |
387 static d_length_status_t length_callback( | |
388 const decoder_t *decoder, | |
389 FLAC__uint64 *stream_length, | |
390 void *client_data) | |
391 { | |
392 flac_t *f = (flac_t *) client_data; | |
393 | |
394 if (f->sample->flags & SOUND_SAMPLEFLAG_CANSEEK) | |
395 { | |
396 *stream_length = f->stream_length; | |
397 return(D_LENGTH_STATUS_OK); | |
398 } /* if */ | |
399 | |
400 return(D_LENGTH_STATUS_ERROR); | |
401 } /* length_callback */ | |
402 | |
403 | |
404 static FLAC__bool eof_callback( | |
405 const decoder_t *decoder, | |
406 void *client_data) | |
407 { | |
408 flac_t *f = (flac_t *) client_data; | |
400 | 409 int pos; |
312 | 410 |
411 /* Maybe we could check for SOUND_SAMPLEFLAG_EOF here instead? */ | |
412 pos = SDL_RWtell(f->rw); | |
413 | |
414 if (pos >= 0 && pos >= f->stream_length) | |
415 { | |
416 return(true); | |
417 } /* if */ | |
418 | |
419 return(false); | |
420 } /* eof_callback */ | |
421 | |
422 #endif | |
155 | 423 |
424 static int FLAC_init(void) | |
425 { | |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
426 SNDDBG(("FLAC: we are using libFLAC version %s 1.0.2.\n", |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
427 FLAC_VERSION_102_OR_LESS ? "<=" : ">")); |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
428 SNDDBG(("FLAC: We %shave seeking support.\n", |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
429 FLAC_IS_SEEKABLE ? "" : "do NOT ")); |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
430 |
155 | 431 return(1); /* always succeeds. */ |
432 } /* FLAC_init */ | |
433 | |
434 | |
435 static void FLAC_quit(void) | |
436 { | |
437 /* it's a no-op. */ | |
438 } /* FLAC_quit */ | |
439 | |
440 | |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
441 #define FLAC_MAGIC 0x43614C66 /* "fLaC" in ASCII. */ |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
442 |
155 | 443 static int FLAC_open(Sound_Sample *sample, const char *ext) |
444 { | |
445 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
446 SDL_RWops *rw = internal->rw; | |
312 | 447 decoder_t *decoder; |
155 | 448 flac_t *f; |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
449 int i; |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
450 int has_extension = 0; |
312 | 451 |
452 #if SOUND_SUPPORTS_SEEKABLE_FLAC | |
453 Uint32 pos; | |
454 #endif | |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
455 |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
456 /* |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
457 * If the extension is "flac", we'll believe that this is really meant |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
458 * to be a FLAC stream, and will try to grok it from existing metadata. |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
459 * metadata searching can be a very expensive operation, however, so |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
460 * unless the user swears that it is a FLAC stream through the extension, |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
461 * we decide what to do based on the existance of a 32-bit magic number. |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
462 */ |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
463 for (i = 0; extensions_flac[i] != NULL; i++) |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
464 { |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
465 if (__Sound_strcasecmp(ext, extensions_flac[i]) == 0) |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
466 { |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
467 has_extension = 1; |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
468 break; |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
469 } /* if */ |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
470 } /* for */ |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
471 |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
472 if (!has_extension) |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
473 { |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
474 int rc; |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
475 Uint32 flac_magic = SDL_ReadLE32(rw); |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
476 BAIL_IF_MACRO(flac_magic != FLAC_MAGIC, "FLAC: Not a FLAC stream.", 0); |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
477 |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
478 /* move back over magic number for metadata scan... */ |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
479 rc = SDL_RWseek(internal->rw, -sizeof (flac_magic), SEEK_CUR); |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
480 BAIL_IF_MACRO(rc < 0, ERR_IO_ERROR, 0); |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
481 } /* if */ |
155 | 482 |
483 f = (flac_t *) malloc(sizeof (flac_t)); | |
484 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0); | |
485 | |
312 | 486 decoder = d_new(); |
155 | 487 if (decoder == NULL) |
488 { | |
489 free(f); | |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
377
diff
changeset
|
490 BAIL_MACRO(ERR_OUT_OF_MEMORY, 0); |
155 | 491 } /* if */ |
492 | |
312 | 493 d_set_read_callback(decoder, read_callback); |
494 d_set_write_callback(decoder, write_callback); | |
495 d_set_metadata_callback(decoder, metadata_callback); | |
496 d_set_error_callback(decoder, error_callback); | |
497 | |
498 #if SOUND_SUPPORTS_SEEKABLE_FLAC | |
499 d_set_seek_callback(decoder, seek_callback); | |
500 d_set_tell_callback(decoder, tell_callback); | |
501 d_set_length_callback(decoder, length_callback); | |
502 d_set_eof_callback(decoder, eof_callback); | |
503 #endif | |
504 | |
505 d_set_client_data(decoder, f); | |
155 | 506 |
507 f->rw = internal->rw; | |
508 f->sample = sample; | |
509 f->decoder = decoder; | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
510 f->sample->actual.format = 0; |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
511 f->is_flac = 0 /* !!! FIXME: should be "has_extension", not "0". */; |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
512 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
513 internal->decoder_private = f; |
312 | 514 d_init(decoder); |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
515 |
400 | 516 sample->flags = SOUND_SAMPLEFLAG_NONE; |
517 | |
518 #if SOUND_SUPPORTS_SEEKABLE_FLAC | |
519 | |
520 pos = SDL_RWtell(f->rw); | |
521 if (SDL_RWseek(f->rw, 0, SEEK_END) > 0) | |
522 { | |
523 f->stream_length = SDL_RWtell(f->rw); | |
524 if (SDL_RWseek(f->rw, pos, SEEK_SET) == -1) | |
525 { | |
526 free_flac(f); | |
527 BAIL_MACRO(ERR_IO_ERROR, 0); | |
528 } /* if */ | |
529 sample->flags = SOUND_SAMPLEFLAG_CANSEEK; | |
530 } /* if */ | |
531 | |
532 #else | |
533 | |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
534 /* |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
535 * Annoyingly, the rewind method will put the FLAC decoder in a state |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
536 * where it expects to read metadata, so we have to set this marker |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
537 * before the metadata block. |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
538 */ |
312 | 539 f->data_offset = SDL_RWtell(f->rw); |
400 | 540 |
312 | 541 #endif |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
542 |
312 | 543 |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
544 /* |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
545 * If we are not sure this is a FLAC stream, check for the STREAMINFO |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
546 * metadata block. If not, we'd have to peek at the first audio frame |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
547 * and get the sound format from there, but that is not yet |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
548 * implemented. |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
549 */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
550 if (!f->is_flac) |
155 | 551 { |
312 | 552 d_process_metadata(decoder); |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
553 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
554 /* Still not FLAC? Give up. */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
555 if (!f->is_flac) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
556 { |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
557 free_flac(f); |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
377
diff
changeset
|
558 BAIL_MACRO("FLAC: No metadata found. Not a FLAC stream?", 0); |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
559 } /* if */ |
155 | 560 } /* if */ |
561 | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
562 SNDDBG(("FLAC: Accepting data stream.\n")); |
155 | 563 return(1); |
564 } /* FLAC_open */ | |
565 | |
566 | |
567 static void FLAC_close(Sound_Sample *sample) | |
568 { | |
569 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
570 flac_t *f = (flac_t *) internal->decoder_private; | |
571 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
572 free_flac(f); |
155 | 573 } /* FLAC_close */ |
574 | |
575 | |
576 static Uint32 FLAC_read(Sound_Sample *sample) | |
577 { | |
578 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
579 flac_t *f = (flac_t *) internal->decoder_private; | |
580 Uint32 len; | |
581 | |
312 | 582 if (!d_process_one_frame(f->decoder)) |
155 | 583 { |
584 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
377
diff
changeset
|
585 BAIL_MACRO("FLAC: Couldn't decode frame.", 0); |
155 | 586 } /* if */ |
587 | |
312 | 588 if (d_get_state(f->decoder) == D_END_OF_STREAM) |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
589 { |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
590 sample->flags |= SOUND_SAMPLEFLAG_EOF; |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
591 return(0); |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
592 } /* if */ |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
593 |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
594 /* An error may have been signalled through the error callback. */ |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
595 if (sample->flags & SOUND_SAMPLEFLAG_ERROR) |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
596 return(0); |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
597 |
155 | 598 return(f->frame_size); |
599 } /* FLAC_read */ | |
600 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
601 |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
602 static int FLAC_rewind(Sound_Sample *sample) |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
603 { |
312 | 604 #if SOUND_SUPPORTS_SEEKABLE_FLAC |
605 return FLAC_seek(sample, 0); | |
606 #else | |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
607 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
608 flac_t *f = (flac_t *) internal->decoder_private; |
312 | 609 int rc = SDL_RWseek(f->rw, f->data_offset, SEEK_SET); |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
610 |
312 | 611 BAIL_IF_MACRO(rc != f->data_offset, ERR_IO_ERROR, 0); |
612 BAIL_IF_MACRO(!d_reset(f->decoder), "FLAC: could not reset decoder", 0); | |
613 d_process_metadata(f->decoder); | |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
614 return(1); |
312 | 615 #endif |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
616 } /* FLAC_rewind */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
617 |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
618 |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
619 static int FLAC_seek(Sound_Sample *sample, Uint32 ms) |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
620 { |
312 | 621 #if SOUND_SUPPORTS_SEEKABLE_FLAC |
622 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
623 flac_t *f = (flac_t *) internal->decoder_private; | |
624 | |
625 d_seek_absolute(f->decoder, (ms * sample->actual.rate) / 1000); | |
626 return(1); | |
627 #else | |
628 BAIL_MACRO("FLAC: This is the non-seekable version of the decoder!", 0); | |
629 #endif | |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
630 } /* FLAC_seek */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
631 |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
632 |
155 | 633 #endif /* SOUND_SUPPORTS_FLAC */ |
634 | |
635 | |
636 /* end of flac.c ... */ |