Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / apps / codecs / mpc.c
blob36a7469088274f404ca58d308435081859189c4d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Thom Johansen
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "codeclib.h"
23 #include <codecs/libmusepack/musepack.h>
25 CODEC_HEADER
27 mpc_decoder decoder IBSS_ATTR;
29 /* Our implementations of the mpc_reader callback functions. */
30 mpc_int32_t read_impl(void *data, void *ptr, mpc_int32_t size)
32 struct codec_api *ci = (struct codec_api *)data;
34 return ((mpc_int32_t)(ci->read_filebuf(ptr, size)));
37 mpc_bool_t seek_impl(void *data, mpc_int32_t offset)
39 struct codec_api *ci = (struct codec_api *)data;
41 /* WARNING: assumes we don't need to skip too far into the past,
42 this might not be supported by the buffering layer yet */
43 return ci->seek_buffer(offset);
46 mpc_int32_t tell_impl(void *data)
48 struct codec_api *ci = (struct codec_api *)data;
50 return ci->curpos;
53 mpc_int32_t get_size_impl(void *data)
55 struct codec_api *ci = (struct codec_api *)data;
57 return ci->filesize;
60 mpc_bool_t canseek_impl(void *data)
62 (void)data;
64 /* doesn't much matter, libmusepack ignores this anyway */
65 return true;
68 MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH]
69 IBSS_ATTR_MPC_SAMPLE_BUF;
71 /* this is the codec entry point */
72 enum codec_status codec_main(void)
74 mpc_int64_t samplesdone;
75 unsigned long frequency;
76 unsigned status;
77 mpc_reader reader;
78 mpc_streaminfo info;
79 int retval = CODEC_OK;
81 /* musepack's sample representation is 18.14
82 * DSP_SET_SAMPLE_DEPTH = 14 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 29 */
83 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
85 /* Create a decoder instance */
86 reader.read = read_impl;
87 reader.seek = seek_impl;
88 reader.tell = tell_impl;
89 reader.get_size = get_size_impl;
90 reader.canseek = canseek_impl;
91 reader.data = ci;
93 next_track:
94 if (codec_init()) {
95 retval = CODEC_ERROR;
96 goto exit;
99 while (!*ci->taginfo_ready && !ci->stop_codec)
100 ci->sleep(1);
102 samplesdone = ci->id3->offset;
104 /* read file's streaminfo data */
105 mpc_streaminfo_init(&info);
106 if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) {
107 retval = CODEC_ERROR;
108 goto done;
110 frequency = info.sample_freq / 1000;
111 ci->configure(DSP_SWITCH_FREQUENCY, info.sample_freq);
113 /* set playback engine up for correct number of channels */
114 /* NOTE: current musepack format only allows for stereo files
115 but code is here to handle other configurations anyway */
116 if (info.channels == 2)
117 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
118 else if (info.channels == 1)
119 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
120 else {
121 retval = CODEC_ERROR;
122 goto done;
125 codec_set_replaygain(ci->id3);
126 /* instantiate a decoder with our file reader */
127 mpc_decoder_setup(&decoder, &reader);
128 if (!mpc_decoder_initialize(&decoder, &info)) {
129 retval = CODEC_ERROR;
130 goto done;
133 /* Resume to saved sample offset. */
134 if(samplesdone > 0) {
135 /* hack to improve seek time if filebuf goes empty */
136 if (mpc_decoder_seek_sample(&decoder, samplesdone)) {
137 ci->set_elapsed(samplesdone/frequency);
138 } else {
139 samplesdone = 0;
141 /* reset chunksize */
144 /* This is the decoding loop. */
145 do {
146 #if 1
147 /* Complete seek handler. */
148 if (ci->seek_time) {
149 /* hack to improve seek time if filebuf goes empty */
150 mpc_int64_t new_offset = (ci->seek_time - 1)*frequency;
151 if (mpc_decoder_seek_sample(&decoder, new_offset)) {
152 samplesdone = new_offset;
153 ci->set_elapsed(ci->seek_time);
155 ci->seek_complete();
156 /* reset chunksize */
159 #else
160 /* Seek to start of track handler. */
161 if (ci->seek_time) {
162 if (ci->seek_time == 1 && mpc_decoder_seek_sample(&decoder, 0)) {
163 samplesdone = 0;
164 ci->set_elapsed(0);
166 ci->seek_complete();
168 #endif
169 if (ci->stop_codec || ci->new_track)
170 break;
172 status = mpc_decoder_decode(&decoder, sample_buffer, NULL, NULL);
173 ci->yield();
174 if (status == 0) /* end of file reached */
175 goto done;
176 if (status == (unsigned)(-1)) { /* decode error */
177 retval = CODEC_ERROR;
178 goto done;
179 } else {
180 ci->pcmbuf_insert(sample_buffer,
181 sample_buffer + MPC_FRAME_LENGTH,
182 status);
183 samplesdone += status;
184 ci->set_elapsed(samplesdone/frequency);
185 ci->set_offset(samplesdone);
187 } while (status != 0);
188 retval = CODEC_OK;
190 done:
191 if (ci->request_next_track())
192 goto next_track;
194 exit:
195 return retval;