summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/plugins/SOURCES1
-rw-r--r--apps/plugins/codecflac.c219
-rw-r--r--apps/tree.c1
3 files changed, 221 insertions, 0 deletions
diff --git a/apps/plugins/SOURCES b/apps/plugins/SOURCES
index ebf261bf29..64b0b785f0 100644
--- a/apps/plugins/SOURCES
+++ b/apps/plugins/SOURCES
@@ -74,6 +74,7 @@ vorbis2wav.c
74#ifdef IRIVER_H100 74#ifdef IRIVER_H100
75codecvorbis.c 75codecvorbis.c
76codecmpa.c 76codecmpa.c
77codecflac.c
77#endif 78#endif
78wv2wav.c 79wv2wav.c
79mpc2wav.c 80mpc2wav.c
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
29static struct plugin_api* rb;
30
31/* Called when the FLAC decoder needs some FLAC data to decode */
32FLAC__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
47static 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 */
50FLAC__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
82void 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
92void 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
100FLAC__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
114FLAC__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
124FLAC__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
134FLAC__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
148extern char iramcopy[];
149extern char iramstart[];
150extern char iramend[];
151#endif
152
153/* this is the plugin entry point */
154enum 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}
diff --git a/apps/tree.c b/apps/tree.c
index a49fb9b3ab..176add2909 100644
--- a/apps/tree.c
+++ b/apps/tree.c
@@ -76,6 +76,7 @@ const struct filetype filetypes[] = {
76 { ".ogg", TREE_ATTR_MPA, File, VOICE_EXT_MPA }, 76 { ".ogg", TREE_ATTR_MPA, File, VOICE_EXT_MPA },
77 { ".wma", TREE_ATTR_MPA, File, VOICE_EXT_MPA }, 77 { ".wma", TREE_ATTR_MPA, File, VOICE_EXT_MPA },
78 { ".wav", TREE_ATTR_MPA, File, VOICE_EXT_MPA }, 78 { ".wav", TREE_ATTR_MPA, File, VOICE_EXT_MPA },
79 { ".flac", TREE_ATTR_MPA, File, VOICE_EXT_MPA },
79#endif 80#endif
80 { ".m3u", TREE_ATTR_M3U, Playlist, LANG_PLAYLIST }, 81 { ".m3u", TREE_ATTR_M3U, Playlist, LANG_PLAYLIST },
81 { ".cfg", TREE_ATTR_CFG, Config, VOICE_EXT_CFG }, 82 { ".cfg", TREE_ATTR_CFG, Config, VOICE_EXT_CFG },