autodetection: convert path to native separators before displaying it.
[Rockbox.git] / apps / codecs / mpc.c
blobc734915f685b2067041a1869573e6f54ef963d9d
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;
69 /* this is the codec entry point */
70 enum codec_status codec_main(void)
72 mpc_int64_t samplesdone;
73 unsigned long frequency;
74 unsigned status;
75 mpc_reader reader;
76 mpc_streaminfo info;
77 int retval = CODEC_OK;
79 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
81 /* Create a decoder instance */
82 reader.read = read_impl;
83 reader.seek = seek_impl;
84 reader.tell = tell_impl;
85 reader.get_size = get_size_impl;
86 reader.canseek = canseek_impl;
87 reader.data = ci;
89 next_track:
90 if (codec_init()) {
91 retval = CODEC_ERROR;
92 goto exit;
95 while (!*ci->taginfo_ready && !ci->stop_codec)
96 ci->sleep(1);
98 samplesdone = ci->id3->offset;
100 /* read file's streaminfo data */
101 mpc_streaminfo_init(&info);
102 if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) {
103 retval = CODEC_ERROR;
104 goto done;
106 frequency = info.sample_freq / 1000;
107 ci->configure(DSP_SWITCH_FREQUENCY, info.sample_freq);
109 /* set playback engine up for correct number of channels */
110 /* NOTE: current musepack format only allows for stereo files
111 but code is here to handle other configurations anyway */
112 if (info.channels == 2)
113 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
114 else if (info.channels == 1)
115 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
116 else {
117 retval = CODEC_ERROR;
118 goto done;
121 codec_set_replaygain(ci->id3);
122 /* instantiate a decoder with our file reader */
123 mpc_decoder_setup(&decoder, &reader);
124 if (!mpc_decoder_initialize(&decoder, &info)) {
125 retval = CODEC_ERROR;
126 goto done;
129 /* Resume to saved sample offset. */
130 if(samplesdone > 0) {
131 /* hack to improve seek time if filebuf goes empty */
132 if (mpc_decoder_seek_sample(&decoder, samplesdone)) {
133 ci->set_elapsed(samplesdone/frequency);
134 } else {
135 samplesdone = 0;
137 /* reset chunksize */
140 /* This is the decoding loop. */
141 do {
142 #if 1
143 /* Complete seek handler. */
144 if (ci->seek_time) {
145 /* hack to improve seek time if filebuf goes empty */
146 mpc_int64_t new_offset = (ci->seek_time - 1)*frequency;
147 if (mpc_decoder_seek_sample(&decoder, new_offset)) {
148 samplesdone = new_offset;
149 ci->set_elapsed(ci->seek_time);
151 ci->seek_complete();
152 /* reset chunksize */
155 #else
156 /* Seek to start of track handler. */
157 if (ci->seek_time) {
158 if (ci->seek_time == 1 && mpc_decoder_seek_sample(&decoder, 0)) {
159 samplesdone = 0;
160 ci->set_elapsed(0);
162 ci->seek_complete();
164 #endif
165 if (ci->stop_codec || ci->new_track)
166 break;
168 status = mpc_decoder_decode(&decoder, sample_buffer, NULL, NULL);
169 ci->yield();
170 if (status == 0) /* end of file reached */
171 goto done;
172 if (status == (unsigned)(-1)) { /* decode error */
173 retval = CODEC_ERROR;
174 goto done;
175 } else {
176 ci->pcmbuf_insert(sample_buffer,
177 sample_buffer + MPC_FRAME_LENGTH,
178 status);
179 samplesdone += status;
180 ci->set_elapsed(samplesdone/frequency);
181 ci->set_offset(samplesdone);
183 } while (status != 0);
184 retval = CODEC_OK;
186 done:
187 if (ci->request_next_track())
188 goto next_track;
190 exit:
191 mpc_decoder_destroy(&decoder);
192 return retval;