Mercurial > SDL_sound_CoreAudio
comparison decoders/flac.c @ 155:72ff7d3a25b6
Initial add.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Fri, 09 Nov 2001 21:43:11 +0000 |
parents | |
children | 77482005beb6 |
comparison
equal
deleted
inserted
replaced
154:feff6dab2278 | 155:72ff7d3a25b6 |
---|---|
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 | |
48 #include "FLAC/all.h" | |
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; | |
82 } flac_t; | |
83 | |
84 | |
85 static FLAC__StreamDecoderReadStatus FLAC_read_callback( | |
86 const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], | |
87 unsigned int *bytes, void *client_data) | |
88 { | |
89 flac_t *f = (flac_t *) client_data; | |
90 Uint32 retval; | |
91 | |
92 #if 0 | |
93 SNDDBG(("FLAC: Read callback\n")); | |
94 #endif | |
95 | |
96 retval = SDL_RWread(f->rw, (Uint8 *) buffer, 1, *bytes); | |
97 | |
98 if (retval == 0) | |
99 { | |
100 SNDDBG(("FLAC: End of file\n")); | |
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")); | |
174 f->sample->actual.channels = metadata->data.stream_info.channels; | |
175 f->sample->actual.rate = metadata->data.stream_info.sample_rate; | |
176 | |
177 /* !!! FIXME: I believe bits_per_sample may be anywhere between | |
178 * 4 and 24. We can only handle 8 and 16 at present. | |
179 */ | |
180 switch (metadata->data.stream_info.bits_per_sample) | |
181 { | |
182 case 8: | |
183 f->sample->actual.format = AUDIO_S8; | |
184 break; | |
185 case 16: | |
186 f->sample->actual.format = AUDIO_S16MSB; | |
187 break; | |
188 default: | |
189 Sound_SetError("FLAC: Unsupported sample width.\n"); | |
190 f->sample->actual.format = 0; | |
191 break; | |
192 } /* switch */ | |
193 } /* if */ | |
194 } /* FLAC_metadata_callback */ | |
195 | |
196 | |
197 void FLAC_error_callback( | |
198 const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, | |
199 void *client_data) | |
200 { | |
201 flac_t *f = (flac_t *) client_data; | |
202 | |
203 SNDDBG(("FLAC: Error callback.\n")); | |
204 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
205 } /* FLAC_error_callback */ | |
206 | |
207 | |
208 static int FLAC_init(void) | |
209 { | |
210 return(1); /* always succeeds. */ | |
211 } /* FLAC_init */ | |
212 | |
213 | |
214 static void FLAC_quit(void) | |
215 { | |
216 /* it's a no-op. */ | |
217 } /* FLAC_quit */ | |
218 | |
219 | |
220 static int FLAC_open(Sound_Sample *sample, const char *ext) | |
221 { | |
222 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
223 SDL_RWops *rw = internal->rw; | |
224 FLAC__StreamDecoder *decoder; | |
225 Uint8 flac_magic[4]; | |
226 flac_t *f; | |
227 | |
228 if (SDL_RWread(rw, flac_magic, sizeof (flac_magic), 1) != 1) | |
229 { | |
230 Sound_SetError("FLAC: Could not read FLAC magic."); | |
231 return(0); | |
232 } /* if */ | |
233 | |
234 if (strncmp(flac_magic, "fLaC", sizeof (flac_magic)) != 0) | |
235 { | |
236 Sound_SetError("FLAC: Not a FLAC stream."); | |
237 return(0); | |
238 } /* if */ | |
239 | |
240 SDL_RWseek(internal->rw, -sizeof (flac_magic), SEEK_CUR); | |
241 | |
242 f = (flac_t *) malloc(sizeof (flac_t)); | |
243 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0); | |
244 | |
245 decoder = FLAC__stream_decoder_new(); | |
246 if (decoder == NULL) | |
247 { | |
248 Sound_SetError(ERR_OUT_OF_MEMORY); | |
249 free(f); | |
250 return(0); | |
251 } /* if */ | |
252 | |
253 FLAC__stream_decoder_set_read_callback(decoder, FLAC_read_callback); | |
254 FLAC__stream_decoder_set_write_callback(decoder, FLAC_write_callback); | |
255 FLAC__stream_decoder_set_metadata_callback(decoder, FLAC_metadata_callback); | |
256 FLAC__stream_decoder_set_error_callback(decoder, FLAC_error_callback); | |
257 FLAC__stream_decoder_set_client_data(decoder, f); | |
258 | |
259 f->rw = internal->rw; | |
260 f->sample = sample; | |
261 f->decoder = decoder; | |
262 | |
263 FLAC__stream_decoder_init(decoder); | |
264 internal->decoder_private = f; | |
265 | |
266 SNDDBG(("FLAC: Accepting data stream.\n")); | |
267 | |
268 FLAC__stream_decoder_process_metadata(decoder); | |
269 | |
270 if (f->sample->actual.format == 0) | |
271 { | |
272 FLAC__stream_decoder_finish(decoder); | |
273 FLAC__stream_decoder_delete(decoder); | |
274 free(f); | |
275 return(0); | |
276 } /* if */ | |
277 | |
278 sample->flags = SOUND_SAMPLEFLAG_NONE; | |
279 return(1); | |
280 } /* FLAC_open */ | |
281 | |
282 | |
283 static void FLAC_close(Sound_Sample *sample) | |
284 { | |
285 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
286 flac_t *f = (flac_t *) internal->decoder_private; | |
287 | |
288 FLAC__stream_decoder_finish(f->decoder); | |
289 FLAC__stream_decoder_delete(f->decoder); | |
290 free(f); | |
291 } /* FLAC_close */ | |
292 | |
293 | |
294 static Uint32 FLAC_read(Sound_Sample *sample) | |
295 { | |
296 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
297 flac_t *f = (flac_t *) internal->decoder_private; | |
298 Uint32 len; | |
299 | |
300 if (FLAC__stream_decoder_get_state(f->decoder) == FLAC__STREAM_DECODER_END_OF_STREAM) | |
301 { | |
302 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
303 return(0); | |
304 } /* if */ | |
305 | |
306 if (!FLAC__stream_decoder_process_one_frame(f->decoder)) | |
307 { | |
308 SNDDBG(("FLAC: Couldn't decode frame.\n")); | |
309 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
310 return(0); | |
311 } /* if */ | |
312 | |
313 return(f->frame_size); | |
314 } /* FLAC_read */ | |
315 | |
316 #endif /* SOUND_SUPPORTS_FLAC */ | |
317 | |
318 | |
319 /* end of flac.c ... */ |