annotate 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
rev   line source
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 * SDL_sound -- An abstract sound format decoding API.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 * Copyright (C) 2001 Ryan C. Gordon.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 * version 2.1 of the License, or (at your option) any later version.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 * Lesser General Public License for more details.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 /*
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 * FLAC decoder for SDL_sound.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 * This driver handles FLAC audio, that is to say the Free Lossless Audio
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 * Codec. It depends on libFLAC for decoding, which can be grabbed from:
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 * http://flac.sourceforge.net
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 * Please see the file LICENSE in the source's root directory.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 * This file written by Torbjörn Andersson. (d91tan@Update.UU.SE)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 #if HAVE_CONFIG_H
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 # include <config.h>
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34 #endif
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 #ifdef SOUND_SUPPORTS_FLAC
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 #include <stdio.h>
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 #include <stdlib.h>
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 #include <string.h>
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 #include <assert.h>
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 #include "SDL_sound.h"
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45 #define __SDL_SOUND_INTERNAL__
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 #include "SDL_sound_internal.h"
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 #include "FLAC/all.h"
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 static int FLAC_init(void);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52 static void FLAC_quit(void);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 static int FLAC_open(Sound_Sample *sample, const char *ext);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 static void FLAC_close(Sound_Sample *sample);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 static Uint32 FLAC_read(Sound_Sample *sample);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 static const char *extensions_flac[] = { "FLAC", "FLA", NULL };
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 const Sound_DecoderFunctions __Sound_DecoderFunctions_FLAC =
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 extensions_flac,
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 "Free Lossless Audio Codec",
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 "Torbjörn Andersson <d91tan@Update.UU.SE>",
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65 "http://flac.sourceforge.net/"
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 },
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 FLAC_init, /* init() method */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69 FLAC_quit, /* quit() method */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 FLAC_open, /* open() method */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 FLAC_close, /* close() method */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 FLAC_read /* read() method */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 };
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75 /* This is what we store in our internal->decoder_private field. */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76 typedef struct
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 FLAC__StreamDecoder *decoder;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 SDL_RWops *rw;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80 Sound_Sample *sample;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 Uint32 frame_size;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 } flac_t;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 static FLAC__StreamDecoderReadStatus FLAC_read_callback(
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 unsigned int *bytes, void *client_data)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 flac_t *f = (flac_t *) client_data;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 Uint32 retval;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 #if 0
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 SNDDBG(("FLAC: Read callback\n"));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 #endif
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 retval = SDL_RWread(f->rw, (Uint8 *) buffer, 1, *bytes);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 if (retval == 0)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 SNDDBG(("FLAC: End of file\n"));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101 *bytes = 0;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 f->sample->flags |= SOUND_SAMPLEFLAG_EOF;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 return(FLAC__STREAM_DECODER_READ_END_OF_STREAM);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 if (retval == -1)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 *bytes = 0;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110 return(FLAC__STREAM_DECODER_READ_ABORT);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 if (retval < *bytes)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 *bytes = retval;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 f->sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 return(FLAC__STREAM_DECODER_READ_CONTINUE);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 } /* FLAC_read_callback */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 static FLAC__StreamDecoderWriteStatus FLAC_write_callback(
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 const FLAC__int32 *buffer[], void *client_data)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 flac_t *f = (flac_t *) client_data;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128 Uint32 i, j;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129 Uint8 *dst;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 #if 0
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132 SNDDBG(("FLAC: Write callback.\n"));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133 #endif
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 f->frame_size = frame->header.channels * frame->header.blocksize
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136 * frame->header.bits_per_sample / 8;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
137
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138 if (f->frame_size > f->sample->buffer_size)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139 Sound_SetBufferSize(f->sample, f->frame_size);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
141 dst = f->sample->buffer;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 if (frame->header.bits_per_sample == 8)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145 for (i = 0; i < frame->header.blocksize; i++)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146 for (j = 0; j < frame->header.channels; j++)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 *dst++ = buffer[j][i] & 0x000000ff;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 else
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151 for (i = 0; i < frame->header.blocksize; i++)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 for (j = 0; j < frame->header.channels; j++)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 *dst++ = (buffer[j][i] & 0x0000ff00) >> 8;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155 *dst++ = buffer[j][i] & 0x000000ff;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 } /* for */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157 } /* else */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 return(FLAC__STREAM_DECODER_WRITE_CONTINUE);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 } /* FLAC_write_callback */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163 void FLAC_metadata_callback(
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164 const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata,
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 void *client_data)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 flac_t *f = (flac_t *) client_data;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169 SNDDBG(("FLAC: Metadata callback.\n"));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
171 if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 SNDDBG(("FLAC: Metadata is streaminfo.\n"));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174 f->sample->actual.channels = metadata->data.stream_info.channels;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 f->sample->actual.rate = metadata->data.stream_info.sample_rate;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177 /* !!! FIXME: I believe bits_per_sample may be anywhere between
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 * 4 and 24. We can only handle 8 and 16 at present.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 switch (metadata->data.stream_info.bits_per_sample)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 case 8:
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 f->sample->actual.format = AUDIO_S8;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 break;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185 case 16:
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186 f->sample->actual.format = AUDIO_S16MSB;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 break;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188 default:
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 Sound_SetError("FLAC: Unsupported sample width.\n");
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 f->sample->actual.format = 0;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 break;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 } /* switch */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 } /* FLAC_metadata_callback */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 void FLAC_error_callback(
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status,
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 void *client_data)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 flac_t *f = (flac_t *) client_data;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203 SNDDBG(("FLAC: Error callback.\n"));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205 } /* FLAC_error_callback */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 static int FLAC_init(void)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210 return(1); /* always succeeds. */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 } /* FLAC_init */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 static void FLAC_quit(void)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 /* it's a no-op. */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217 } /* FLAC_quit */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 static int FLAC_open(Sound_Sample *sample, const char *ext)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223 SDL_RWops *rw = internal->rw;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 FLAC__StreamDecoder *decoder;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225 Uint8 flac_magic[4];
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 flac_t *f;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
228 if (SDL_RWread(rw, flac_magic, sizeof (flac_magic), 1) != 1)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230 Sound_SetError("FLAC: Could not read FLAC magic.");
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 return(0);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 if (strncmp(flac_magic, "fLaC", sizeof (flac_magic)) != 0)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236 Sound_SetError("FLAC: Not a FLAC stream.");
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 return(0);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 SDL_RWseek(internal->rw, -sizeof (flac_magic), SEEK_CUR);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 f = (flac_t *) malloc(sizeof (flac_t));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245 decoder = FLAC__stream_decoder_new();
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246 if (decoder == NULL)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 Sound_SetError(ERR_OUT_OF_MEMORY);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 free(f);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 return(0);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 FLAC__stream_decoder_set_read_callback(decoder, FLAC_read_callback);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254 FLAC__stream_decoder_set_write_callback(decoder, FLAC_write_callback);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
255 FLAC__stream_decoder_set_metadata_callback(decoder, FLAC_metadata_callback);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256 FLAC__stream_decoder_set_error_callback(decoder, FLAC_error_callback);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257 FLAC__stream_decoder_set_client_data(decoder, f);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
259 f->rw = internal->rw;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
260 f->sample = sample;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
261 f->decoder = decoder;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
262
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
263 FLAC__stream_decoder_init(decoder);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264 internal->decoder_private = f;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
265
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
266 SNDDBG(("FLAC: Accepting data stream.\n"));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
267
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
268 FLAC__stream_decoder_process_metadata(decoder);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
269
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
270 if (f->sample->actual.format == 0)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
271 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
272 FLAC__stream_decoder_finish(decoder);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
273 FLAC__stream_decoder_delete(decoder);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
274 free(f);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
275 return(0);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
276 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
277
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
278 sample->flags = SOUND_SAMPLEFLAG_NONE;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
279 return(1);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
280 } /* FLAC_open */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
281
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
282
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
283 static void FLAC_close(Sound_Sample *sample)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
284 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
285 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
286 flac_t *f = (flac_t *) internal->decoder_private;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
287
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
288 FLAC__stream_decoder_finish(f->decoder);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
289 FLAC__stream_decoder_delete(f->decoder);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
290 free(f);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
291 } /* FLAC_close */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
294 static Uint32 FLAC_read(Sound_Sample *sample)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
297 flac_t *f = (flac_t *) internal->decoder_private;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
298 Uint32 len;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
299
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
300 if (FLAC__stream_decoder_get_state(f->decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
301 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
302 sample->flags |= SOUND_SAMPLEFLAG_EOF;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
303 return(0);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
304 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
305
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
306 if (!FLAC__stream_decoder_process_one_frame(f->decoder))
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
307 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
308 SNDDBG(("FLAC: Couldn't decode frame.\n"));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
309 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
310 return(0);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
311 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
312
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
313 return(f->frame_size);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
314 } /* FLAC_read */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
315
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
316 #endif /* SOUND_SUPPORTS_FLAC */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
317
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
318
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
319 /* end of flac.c ... */