Mercurial > SDL_sound_CoreAudio
annotate decoders/flac.c @ 164:77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
stream handling; uses metadata check to see if stream is valid instead of
magic number.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 19 Nov 2001 16:39:42 +0000 |
parents | 72ff7d3a25b6 |
children | d8904267d23c |
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 * | |
27 * Please see the file LICENSE in the source's root directory. | |
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 #include <assert.h> | |
42 | |
43 #include "SDL_sound.h" | |
44 | |
45 #define __SDL_SOUND_INTERNAL__ | |
46 #include "SDL_sound_internal.h" | |
47 | |
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
|
48 #include "FLAC/stream_decoder.h" |
155 | 49 |
50 | |
51 static int FLAC_init(void); | |
52 static void FLAC_quit(void); | |
53 static int FLAC_open(Sound_Sample *sample, const char *ext); | |
54 static void FLAC_close(Sound_Sample *sample); | |
55 static Uint32 FLAC_read(Sound_Sample *sample); | |
56 | |
57 static const char *extensions_flac[] = { "FLAC", "FLA", NULL }; | |
58 | |
59 const Sound_DecoderFunctions __Sound_DecoderFunctions_FLAC = | |
60 { | |
61 { | |
62 extensions_flac, | |
63 "Free Lossless Audio Codec", | |
64 "Torbjörn Andersson <d91tan@Update.UU.SE>", | |
65 "http://flac.sourceforge.net/" | |
66 }, | |
67 | |
68 FLAC_init, /* init() method */ | |
69 FLAC_quit, /* quit() method */ | |
70 FLAC_open, /* open() method */ | |
71 FLAC_close, /* close() method */ | |
72 FLAC_read /* read() method */ | |
73 }; | |
74 | |
75 /* This is what we store in our internal->decoder_private field. */ | |
76 typedef struct | |
77 { | |
78 FLAC__StreamDecoder *decoder; | |
79 SDL_RWops *rw; | |
80 Sound_Sample *sample; | |
81 Uint32 frame_size; | |
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
|
82 Uint8 metadata_found; |
155 | 83 } flac_t; |
84 | |
85 | |
86 static FLAC__StreamDecoderReadStatus FLAC_read_callback( | |
87 const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], | |
88 unsigned int *bytes, void *client_data) | |
89 { | |
90 flac_t *f = (flac_t *) client_data; | |
91 Uint32 retval; | |
92 | |
93 #if 0 | |
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
|
94 SNDDBG(("FLAC: Read callback.\n")); |
155 | 95 #endif |
96 | |
97 retval = SDL_RWread(f->rw, (Uint8 *) buffer, 1, *bytes); | |
98 | |
99 if (retval == 0) | |
100 { | |
101 *bytes = 0; | |
102 f->sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
103 return(FLAC__STREAM_DECODER_READ_END_OF_STREAM); | |
104 } /* if */ | |
105 | |
106 if (retval == -1) | |
107 { | |
108 *bytes = 0; | |
109 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
110 return(FLAC__STREAM_DECODER_READ_ABORT); | |
111 } /* if */ | |
112 | |
113 if (retval < *bytes) | |
114 { | |
115 *bytes = retval; | |
116 f->sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
117 } /* if */ | |
118 | |
119 return(FLAC__STREAM_DECODER_READ_CONTINUE); | |
120 } /* FLAC_read_callback */ | |
121 | |
122 | |
123 static FLAC__StreamDecoderWriteStatus FLAC_write_callback( | |
124 const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, | |
125 const FLAC__int32 *buffer[], void *client_data) | |
126 { | |
127 flac_t *f = (flac_t *) client_data; | |
128 Uint32 i, j; | |
129 Uint8 *dst; | |
130 | |
131 #if 0 | |
132 SNDDBG(("FLAC: Write callback.\n")); | |
133 #endif | |
134 | |
135 f->frame_size = frame->header.channels * frame->header.blocksize | |
136 * frame->header.bits_per_sample / 8; | |
137 | |
138 if (f->frame_size > f->sample->buffer_size) | |
139 Sound_SetBufferSize(f->sample, f->frame_size); | |
140 | |
141 dst = f->sample->buffer; | |
142 | |
143 if (frame->header.bits_per_sample == 8) | |
144 { | |
145 for (i = 0; i < frame->header.blocksize; i++) | |
146 for (j = 0; j < frame->header.channels; j++) | |
147 *dst++ = buffer[j][i] & 0x000000ff; | |
148 } /* if */ | |
149 else | |
150 { | |
151 for (i = 0; i < frame->header.blocksize; i++) | |
152 for (j = 0; j < frame->header.channels; j++) | |
153 { | |
154 *dst++ = (buffer[j][i] & 0x0000ff00) >> 8; | |
155 *dst++ = buffer[j][i] & 0x000000ff; | |
156 } /* for */ | |
157 } /* else */ | |
158 | |
159 return(FLAC__STREAM_DECODER_WRITE_CONTINUE); | |
160 } /* FLAC_write_callback */ | |
161 | |
162 | |
163 void FLAC_metadata_callback( | |
164 const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, | |
165 void *client_data) | |
166 { | |
167 flac_t *f = (flac_t *) client_data; | |
168 | |
169 SNDDBG(("FLAC: Metadata callback.\n")); | |
170 | |
171 if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) | |
172 { | |
173 SNDDBG(("FLAC: Metadata is streaminfo.\n")); | |
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
|
174 f->metadata_found = 1; |
155 | 175 f->sample->actual.channels = metadata->data.stream_info.channels; |
176 f->sample->actual.rate = metadata->data.stream_info.sample_rate; | |
177 | |
178 /* !!! FIXME: I believe bits_per_sample may be anywhere between | |
179 * 4 and 24. We can only handle 8 and 16 at present. | |
180 */ | |
181 switch (metadata->data.stream_info.bits_per_sample) | |
182 { | |
183 case 8: | |
184 f->sample->actual.format = AUDIO_S8; | |
185 break; | |
186 case 16: | |
187 f->sample->actual.format = AUDIO_S16MSB; | |
188 break; | |
189 default: | |
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
|
190 Sound_SetError("FLAC: Unsupported sample width."); |
155 | 191 f->sample->actual.format = 0; |
192 break; | |
193 } /* switch */ | |
194 } /* if */ | |
195 } /* FLAC_metadata_callback */ | |
196 | |
197 | |
198 void FLAC_error_callback( | |
199 const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, | |
200 void *client_data) | |
201 { | |
202 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
|
203 |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
204 Sound_SetError(FLAC__StreamDecoderErrorStatusString[status]); |
155 | 205 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; |
206 } /* FLAC_error_callback */ | |
207 | |
208 | |
209 static int FLAC_init(void) | |
210 { | |
211 return(1); /* always succeeds. */ | |
212 } /* FLAC_init */ | |
213 | |
214 | |
215 static void FLAC_quit(void) | |
216 { | |
217 /* it's a no-op. */ | |
218 } /* FLAC_quit */ | |
219 | |
220 | |
221 static int FLAC_open(Sound_Sample *sample, const char *ext) | |
222 { | |
223 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
224 SDL_RWops *rw = internal->rw; | |
225 FLAC__StreamDecoder *decoder; | |
226 flac_t *f; | |
227 | |
228 f = (flac_t *) malloc(sizeof (flac_t)); | |
229 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0); | |
230 | |
231 decoder = FLAC__stream_decoder_new(); | |
232 if (decoder == NULL) | |
233 { | |
234 Sound_SetError(ERR_OUT_OF_MEMORY); | |
235 free(f); | |
236 return(0); | |
237 } /* if */ | |
238 | |
239 FLAC__stream_decoder_set_read_callback(decoder, FLAC_read_callback); | |
240 FLAC__stream_decoder_set_write_callback(decoder, FLAC_write_callback); | |
241 FLAC__stream_decoder_set_metadata_callback(decoder, FLAC_metadata_callback); | |
242 FLAC__stream_decoder_set_error_callback(decoder, FLAC_error_callback); | |
243 FLAC__stream_decoder_set_client_data(decoder, f); | |
244 | |
245 f->rw = internal->rw; | |
246 f->sample = sample; | |
247 f->decoder = decoder; | |
248 | |
249 FLAC__stream_decoder_init(decoder); | |
250 internal->decoder_private = f; | |
251 | |
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
|
252 f->metadata_found = 0; |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
253 f->sample->actual.format = 0; |
155 | 254 FLAC__stream_decoder_process_metadata(decoder); |
255 | |
256 if (f->sample->actual.format == 0) | |
257 { | |
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
|
258 if (f->metadata_found == 0) |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
259 Sound_SetError("FLAC: No metadata found."); |
155 | 260 FLAC__stream_decoder_finish(decoder); |
261 FLAC__stream_decoder_delete(decoder); | |
262 free(f); | |
263 return(0); | |
264 } /* if */ | |
265 | |
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
|
266 SNDDBG(("FLAC: Accepting data stream.\n")); |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
267 |
155 | 268 sample->flags = SOUND_SAMPLEFLAG_NONE; |
269 return(1); | |
270 } /* FLAC_open */ | |
271 | |
272 | |
273 static void FLAC_close(Sound_Sample *sample) | |
274 { | |
275 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
276 flac_t *f = (flac_t *) internal->decoder_private; | |
277 | |
278 FLAC__stream_decoder_finish(f->decoder); | |
279 FLAC__stream_decoder_delete(f->decoder); | |
280 free(f); | |
281 } /* FLAC_close */ | |
282 | |
283 | |
284 static Uint32 FLAC_read(Sound_Sample *sample) | |
285 { | |
286 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
287 flac_t *f = (flac_t *) internal->decoder_private; | |
288 Uint32 len; | |
289 | |
290 if (FLAC__stream_decoder_get_state(f->decoder) == FLAC__STREAM_DECODER_END_OF_STREAM) | |
291 { | |
292 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
293 return(0); | |
294 } /* if */ | |
295 | |
296 if (!FLAC__stream_decoder_process_one_frame(f->decoder)) | |
297 { | |
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
|
298 Sound_SetError("FLAC: Couldn't decode frame."); |
155 | 299 sample->flags |= SOUND_SAMPLEFLAG_ERROR; |
300 return(0); | |
301 } /* if */ | |
302 | |
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
|
303 /* 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
|
304 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
|
305 return(0); |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
306 |
155 | 307 return(f->frame_size); |
308 } /* FLAC_read */ | |
309 | |
310 #endif /* SOUND_SUPPORTS_FLAC */ | |
311 | |
312 | |
313 /* end of flac.c ... */ |