make usb_serial work again. Also know as "make sure arrays are allocated at their...
[kugel-rb.git] / apps / codecs / mpc.c
blobc74c10ee644719ec5475a5859d9e023dda77bcfe
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Thom Johansen
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "codeclib.h"
21 #include <codecs/libmusepack/musepack.h>
23 CODEC_HEADER
25 mpc_decoder decoder IBSS_ATTR;
27 /* Our implementations of the mpc_reader callback functions. */
28 mpc_int32_t read_impl(void *data, void *ptr, mpc_int32_t size)
30 struct codec_api *ci = (struct codec_api *)data;
32 return ((mpc_int32_t)(ci->read_filebuf(ptr, size)));
35 mpc_bool_t seek_impl(void *data, mpc_int32_t offset)
37 struct codec_api *ci = (struct codec_api *)data;
39 /* WARNING: assumes we don't need to skip too far into the past,
40 this might not be supported by the buffering layer yet */
41 return ci->seek_buffer(offset);
44 mpc_int32_t tell_impl(void *data)
46 struct codec_api *ci = (struct codec_api *)data;
48 return ci->curpos;
51 mpc_int32_t get_size_impl(void *data)
53 struct codec_api *ci = (struct codec_api *)data;
55 return ci->filesize;
58 mpc_bool_t canseek_impl(void *data)
60 (void)data;
62 /* doesn't much matter, libmusepack ignores this anyway */
63 return true;
66 MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH]
67 IBSS_ATTR_MPC_SAMPLE_BUF;
68 mpc_uint32_t seek_table[10000];
70 /* this is the codec entry point */
71 enum codec_status codec_main(void)
73 mpc_int64_t samplesdone;
74 unsigned long frequency;
75 unsigned status;
76 mpc_reader reader;
77 mpc_streaminfo info;
78 int retval = CODEC_OK;
80 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
82 /* Create a decoder instance */
83 reader.read = read_impl;
84 reader.seek = seek_impl;
85 reader.tell = tell_impl;
86 reader.get_size = get_size_impl;
87 reader.canseek = canseek_impl;
88 reader.data = ci;
90 /* Ensure that SeekTable is clear since decoder is reused */
91 decoder.SeekTable = NULL;
93 mpc_decoder_set_seek_table(&decoder, seek_table, sizeof(seek_table));
95 next_track:
96 if (codec_init()) {
97 retval = CODEC_ERROR;
98 goto exit;
101 while (!*ci->taginfo_ready && !ci->stop_codec)
102 ci->sleep(1);
104 samplesdone = ci->id3->offset;
106 /* read file's streaminfo data */
107 mpc_streaminfo_init(&info);
108 if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) {
109 retval = CODEC_ERROR;
110 goto done;
112 frequency = info.sample_freq / 1000;
113 ci->configure(DSP_SWITCH_FREQUENCY, info.sample_freq);
115 /* set playback engine up for correct number of channels */
116 /* NOTE: current musepack format only allows for stereo files
117 but code is here to handle other configurations anyway */
118 if (info.channels == 2)
119 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
120 else if (info.channels == 1)
121 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
122 else {
123 retval = CODEC_ERROR;
124 goto done;
127 codec_set_replaygain(ci->id3);
128 /* instantiate a decoder with our file reader */
129 mpc_decoder_setup(&decoder, &reader);
130 if (!mpc_decoder_initialize(&decoder, &info)) {
131 retval = CODEC_ERROR;
132 goto done;
135 /* Resume to saved sample offset. */
136 if(samplesdone > 0) {
137 /* hack to improve seek time if filebuf goes empty */
138 if (mpc_decoder_seek_sample(&decoder, samplesdone)) {
139 ci->set_elapsed(samplesdone/frequency);
140 } else {
141 samplesdone = 0;
143 /* reset chunksize */
146 /* This is the decoding loop. */
147 do {
148 #if 1
149 /* Complete seek handler. */
150 if (ci->seek_time) {
151 /* hack to improve seek time if filebuf goes empty */
152 mpc_int64_t new_offset = (ci->seek_time - 1)*frequency;
153 if (mpc_decoder_seek_sample(&decoder, new_offset)) {
154 samplesdone = new_offset;
155 ci->set_elapsed(ci->seek_time);
157 ci->seek_complete();
158 /* reset chunksize */
161 #else
162 /* Seek to start of track handler. */
163 if (ci->seek_time) {
164 if (ci->seek_time == 1 && mpc_decoder_seek_sample(&decoder, 0)) {
165 samplesdone = 0;
166 ci->set_elapsed(0);
168 ci->seek_complete();
170 #endif
171 if (ci->stop_codec || ci->new_track)
172 break;
174 status = mpc_decoder_decode(&decoder, sample_buffer, NULL, NULL);
175 ci->yield();
176 if (status == 0) /* end of file reached */
177 goto done;
178 if (status == (unsigned)(-1)) { /* decode error */
179 retval = CODEC_ERROR;
180 goto done;
181 } else {
182 ci->pcmbuf_insert(sample_buffer,
183 sample_buffer + MPC_FRAME_LENGTH,
184 status);
185 samplesdone += status;
186 ci->set_elapsed(samplesdone/frequency);
187 ci->set_offset(samplesdone);
189 } while (status != 0);
190 retval = CODEC_OK;
192 done:
193 if (ci->request_next_track())
194 goto next_track;
196 exit:
197 mpc_decoder_destroy(&decoder);
198 return retval;