diff options
author | Dave Chapman <dave@dchapman.com> | 2005-06-07 18:53:01 +0000 |
---|---|---|
committer | Dave Chapman <dave@dchapman.com> | 2005-06-07 18:53:01 +0000 |
commit | 6c082a857af4200a4b1c2631deef9c01501ad04a (patch) | |
tree | ec5bc164f4a70502b884d5e72d4e643c6a45d7f7 /apps/plugins/codecflac.c | |
parent | 2258946855c8839f159596dfc60a63b6ccfc0347 (diff) | |
download | rockbox-6c082a857af4200a4b1c2631deef9c01501ad04a.tar.gz rockbox-6c082a857af4200a4b1c2631deef9c01501ad04a.zip |
First attempt at codecflac.c
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6597 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/codecflac.c')
-rw-r--r-- | apps/plugins/codecflac.c | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/apps/plugins/codecflac.c b/apps/plugins/codecflac.c new file mode 100644 index 0000000000..f17be3560a --- /dev/null +++ b/apps/plugins/codecflac.c | |||
@@ -0,0 +1,219 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 Björn Stenberg | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #include "plugin.h" | ||
21 | |||
22 | #include <codecs/libFLAC/include/FLAC/seekable_stream_decoder.h> | ||
23 | #include "playback.h" | ||
24 | #include "lib/codeclib.h" | ||
25 | |||
26 | #define FLAC_MAX_SUPPORTED_BLOCKSIZE 4608 | ||
27 | #define FLAC_MAX_SUPPORTED_CHANNELS 2 | ||
28 | |||
29 | static struct plugin_api* rb; | ||
30 | |||
31 | /* Called when the FLAC decoder needs some FLAC data to decode */ | ||
32 | FLAC__SeekableStreamDecoderReadStatus flac_read_handler(const FLAC__SeekableStreamDecoder *dec, | ||
33 | FLAC__byte buffer[], unsigned *bytes, void *data) | ||
34 | { struct codec_api* ci = (struct codec_api*)data; | ||
35 | (void)dec; | ||
36 | |||
37 | *bytes=(unsigned)(ci->read_filebuf(buffer,*bytes)); | ||
38 | |||
39 | /* QUESTION: How do I detect the end of the stream? */ | ||
40 | if (ci->curpos >= ci->filesize) { | ||
41 | return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; | ||
42 | } | ||
43 | |||
44 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; | ||
45 | } | ||
46 | |||
47 | static unsigned char pcmbuf[FLAC_MAX_SUPPORTED_BLOCKSIZE*FLAC_MAX_SUPPORTED_CHANNELS*2] IDATA_ATTR; | ||
48 | |||
49 | /* Called when the FLAC decoder has some decoded PCM data to write */ | ||
50 | FLAC__StreamDecoderWriteStatus flac_write_handler(const FLAC__SeekableStreamDecoder *dec, | ||
51 | const FLAC__Frame *frame, | ||
52 | const FLAC__int32 * const buf[], | ||
53 | void *data) | ||
54 | { | ||
55 | struct codec_api* ci = (struct codec_api*)data; | ||
56 | (void)dec; | ||
57 | unsigned int c_samp, c_chan, d_samp; | ||
58 | uint32_t data_size = frame->header.blocksize * frame->header.channels * 2; /* Assume 16-bit words */ | ||
59 | uint32_t samples = frame->header.blocksize; | ||
60 | |||
61 | |||
62 | if (samples*frame->header.channels > (FLAC_MAX_SUPPORTED_BLOCKSIZE*FLAC_MAX_SUPPORTED_CHANNELS)) { | ||
63 | // ERROR!!! | ||
64 | DEBUGF("ERROR: samples*frame->header.channels=%d\n",samples*frame->header.channels); | ||
65 | return(FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE); | ||
66 | } | ||
67 | |||
68 | (void)dec; | ||
69 | for(c_samp = d_samp = 0; c_samp < samples; c_samp++) { | ||
70 | for(c_chan = 0; c_chan < frame->header.channels; c_chan++, d_samp++) { | ||
71 | pcmbuf[d_samp*2] = (buf[c_chan][c_samp]&0xff00)>>8; | ||
72 | pcmbuf[(d_samp*2)+1] = buf[c_chan][c_samp]&0xff; | ||
73 | } | ||
74 | } | ||
75 | |||
76 | while (!ci->audiobuffer_insert(pcmbuf, data_size)) | ||
77 | rb->yield(); | ||
78 | |||
79 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; | ||
80 | } | ||
81 | |||
82 | void flac_metadata_handler(const FLAC__SeekableStreamDecoder *dec, | ||
83 | const FLAC__StreamMetadata *meta, void *data) | ||
84 | { | ||
85 | /* Ignore metadata for now... */ | ||
86 | (void)dec; | ||
87 | (void)meta; | ||
88 | (void)data; | ||
89 | } | ||
90 | |||
91 | |||
92 | void flac_error_handler(const FLAC__SeekableStreamDecoder *dec, | ||
93 | FLAC__StreamDecoderErrorStatus status, void *data) | ||
94 | { | ||
95 | (void)dec; | ||
96 | (void)status; | ||
97 | (void)data; | ||
98 | } | ||
99 | |||
100 | FLAC__SeekableStreamDecoderSeekStatus flac_seek_handler (const FLAC__SeekableStreamDecoder *decoder, | ||
101 | FLAC__uint64 absolute_byte_offset, | ||
102 | void *client_data) | ||
103 | { | ||
104 | (void)decoder; | ||
105 | struct codec_api* ci = (struct codec_api*)client_data; | ||
106 | |||
107 | if (ci->seek_buffer(absolute_byte_offset)) { | ||
108 | return(FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK); | ||
109 | } else { | ||
110 | return(FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR); | ||
111 | } | ||
112 | } | ||
113 | |||
114 | FLAC__SeekableStreamDecoderTellStatus flac_tell_handler (const FLAC__SeekableStreamDecoder *decoder, | ||
115 | FLAC__uint64 *absolute_byte_offset, void *client_data) | ||
116 | { | ||
117 | struct codec_api* ci = (struct codec_api*)client_data; | ||
118 | |||
119 | (void)decoder; | ||
120 | *absolute_byte_offset=ci->curpos; | ||
121 | return(FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK); | ||
122 | } | ||
123 | |||
124 | FLAC__SeekableStreamDecoderLengthStatus flac_length_handler (const FLAC__SeekableStreamDecoder *decoder, | ||
125 | FLAC__uint64 *stream_length, void *client_data) | ||
126 | { | ||
127 | struct codec_api* ci = (struct codec_api*)client_data; | ||
128 | |||
129 | (void)decoder; | ||
130 | *stream_length=ci->filesize; | ||
131 | return(FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK); | ||
132 | } | ||
133 | |||
134 | FLAC__bool flac_eof_handler (const FLAC__SeekableStreamDecoder *decoder, | ||
135 | void *client_data) | ||
136 | { | ||
137 | struct codec_api* ci = (struct codec_api*)client_data; | ||
138 | |||
139 | (void)decoder; | ||
140 | if (ci->curpos >= ci->filesize) { | ||
141 | return(true); | ||
142 | } else { | ||
143 | return(false); | ||
144 | } | ||
145 | } | ||
146 | |||
147 | #ifndef SIMULATOR | ||
148 | extern char iramcopy[]; | ||
149 | extern char iramstart[]; | ||
150 | extern char iramend[]; | ||
151 | #endif | ||
152 | |||
153 | /* this is the plugin entry point */ | ||
154 | enum plugin_status plugin_start(struct plugin_api* api, void* parm) | ||
155 | { | ||
156 | struct codec_api* ci = (struct codec_api*)parm; | ||
157 | FLAC__SeekableStreamDecoder* flacDecoder; | ||
158 | |||
159 | /* Generic plugin initialisation */ | ||
160 | TEST_PLUGIN_API(api); | ||
161 | |||
162 | /* if you are using a global api pointer, don't forget to copy it! | ||
163 | otherwise you will get lovely "I04: IllInstr" errors... :-) */ | ||
164 | rb = api; | ||
165 | |||
166 | #ifndef SIMULATOR | ||
167 | rb->memcpy(iramstart, iramcopy, iramend-iramstart); | ||
168 | #endif | ||
169 | |||
170 | /* This function sets up the buffers and reads the file into RAM */ | ||
171 | |||
172 | if (codec_init(api, ci)) { | ||
173 | return PLUGIN_ERROR; | ||
174 | } | ||
175 | |||
176 | /* Create a decoder instance */ | ||
177 | |||
178 | next_track: | ||
179 | flacDecoder=FLAC__seekable_stream_decoder_new(); | ||
180 | |||
181 | /* Set up the decoder and the callback functions - this must be done before init */ | ||
182 | |||
183 | /* The following are required for stream_decoder and higher */ | ||
184 | FLAC__seekable_stream_decoder_set_client_data(flacDecoder,ci); | ||
185 | FLAC__seekable_stream_decoder_set_write_callback(flacDecoder, flac_write_handler); | ||
186 | FLAC__seekable_stream_decoder_set_read_callback(flacDecoder, flac_read_handler); | ||
187 | FLAC__seekable_stream_decoder_set_metadata_callback(flacDecoder, flac_metadata_handler); | ||
188 | FLAC__seekable_stream_decoder_set_error_callback(flacDecoder, flac_error_handler); | ||
189 | FLAC__seekable_stream_decoder_set_metadata_respond(flacDecoder, FLAC__METADATA_TYPE_STREAMINFO); | ||
190 | |||
191 | /* The following are only for the seekable_stream_decoder */ | ||
192 | FLAC__seekable_stream_decoder_set_seek_callback(flacDecoder, flac_seek_handler); | ||
193 | FLAC__seekable_stream_decoder_set_tell_callback(flacDecoder, flac_tell_handler); | ||
194 | FLAC__seekable_stream_decoder_set_length_callback(flacDecoder, flac_length_handler); | ||
195 | FLAC__seekable_stream_decoder_set_eof_callback(flacDecoder, flac_eof_handler); | ||
196 | |||
197 | |||
198 | /* QUESTION: What do we do when the init fails? */ | ||
199 | if (FLAC__seekable_stream_decoder_init(flacDecoder)) { | ||
200 | return PLUGIN_ERROR; | ||
201 | } | ||
202 | |||
203 | /* The first thing to do is to parse the metadata */ | ||
204 | FLAC__seekable_stream_decoder_process_until_end_of_metadata(flacDecoder); | ||
205 | |||
206 | /* The main decoder loop */ | ||
207 | while (FLAC__seekable_stream_decoder_get_state(flacDecoder)!=2) { | ||
208 | rb->yield(); | ||
209 | if (ci->stop_codec || ci->reload_codec) { | ||
210 | break; | ||
211 | } | ||
212 | FLAC__seekable_stream_decoder_process_single(flacDecoder); | ||
213 | } | ||
214 | |||
215 | if (ci->request_next_track()) | ||
216 | goto next_track; | ||
217 | |||
218 | return PLUGIN_OK; | ||
219 | } | ||