Fix FS#11103. Resuming musepack files was handled wrong since ages. This change conve...
[kugel-rb.git] / apps / codecs / mpc.c
blobafda2871f94e12b601d19eaf92001ad933bc95a3
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/mpcdec.h>
24 #include <codecs/libmusepack/internal.h>
26 CODEC_HEADER
28 MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH] IBSS_ATTR;
30 /* Our implementations of the mpc_reader callback functions. */
31 static mpc_int32_t read_impl(mpc_reader *reader, void *ptr, mpc_int32_t size)
33 struct codec_api *ci = (struct codec_api *)(reader->data);
34 return ((mpc_int32_t)(ci->read_filebuf(ptr, size)));
37 static mpc_bool_t seek_impl(mpc_reader *reader, mpc_int32_t offset)
39 struct codec_api *ci = (struct codec_api *)(reader->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 static mpc_int32_t tell_impl(mpc_reader *reader)
48 struct codec_api *ci = (struct codec_api *)(reader->data);
50 return ci->curpos;
53 static mpc_int32_t get_size_impl(mpc_reader *reader)
55 struct codec_api *ci = (struct codec_api *)(reader->data);
57 return ci->filesize;
60 static mpc_bool_t canseek_impl(mpc_reader *reader)
62 (void)reader;
64 /* doesn't much matter, libmusepack ignores this anyway */
65 return true;
68 /* this is the codec entry point */
69 enum codec_status codec_main(void)
71 mpc_int64_t samplesdone;
72 uint32_t frequency; /* 0.1 kHz accuracy */
73 uint32_t elapsed_time; /* milliseconds */
74 uint32_t byterate; /* bytes per second */
75 mpc_status status;
76 mpc_reader reader;
77 mpc_streaminfo info;
78 mpc_frame_info frame;
79 mpc_demux *demux = NULL;
80 int retval = CODEC_OK;
82 frame.buffer = sample_buffer;
84 /* musepack's sample representation is 18.14
85 * DSP_SET_SAMPLE_DEPTH = 14 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 29 */
86 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
88 /* Create a decoder instance */
89 reader.read = read_impl;
90 reader.seek = seek_impl;
91 reader.tell = tell_impl;
92 reader.get_size = get_size_impl;
93 reader.canseek = canseek_impl;
94 reader.data = ci;
96 next_track:
97 if (codec_init())
99 retval = CODEC_ERROR;
100 goto exit;
103 while (!*ci->taginfo_ready && !ci->stop_codec)
104 ci->sleep(1);
106 /* initialize demux/decoder */
107 demux = mpc_demux_init(&reader);
108 if (NULL == demux)
110 retval = CODEC_ERROR;
111 goto done;
113 /* read file's streaminfo data */
114 mpc_demux_get_info(demux, &info);
116 byterate = (mpc_uint32_t)(info.average_bitrate) / 8;
117 frequency = info.sample_freq / 100; /* 0.1 kHz accuracy */
118 ci->configure(DSP_SWITCH_FREQUENCY, info.sample_freq);
120 /* Remark: rockbox offset is the file offset in bytes. So, estimate the
121 * sample seek position from the file offset, the sampling frequency and
122 * the bitrate. As the saved position is exactly calculated the reverse way
123 * there is no loss of information except rounding. */
124 samplesdone = 100 * ((mpc_uint64_t)(ci->id3->offset * frequency) / byterate);
126 /* set playback engine up for correct number of channels */
127 /* NOTE: current musepack format only allows for stereo files
128 but code is here to handle other configurations anyway */
129 if (info.channels == 2)
130 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
131 else if (info.channels == 1)
132 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
133 else
135 retval = CODEC_ERROR;
136 goto done;
139 codec_set_replaygain(ci->id3);
141 /* Resume to saved sample offset. */
142 if (samplesdone > 0) {
143 /* hack to improve seek time if filebuf goes empty */
144 if (mpc_demux_seek_sample(demux, samplesdone) == MPC_STATUS_OK)
146 elapsed_time = (samplesdone*10)/frequency;
147 ci->set_elapsed(elapsed_time);
149 else
151 samplesdone = 0;
153 /* reset chunksize */
156 /* This is the decoding loop. */
157 do {
158 /* Complete seek handler. */
159 if (ci->seek_time)
161 /* hack to improve seek time if filebuf goes empty */
162 mpc_int64_t new_offset = ((ci->seek_time - 1)/10)*frequency;
163 if (mpc_demux_seek_sample(demux, new_offset) == MPC_STATUS_OK)
165 samplesdone = new_offset;
166 ci->set_elapsed(ci->seek_time);
168 ci->seek_complete();
169 /* reset chunksize */
171 if (ci->stop_codec || ci->new_track)
172 break;
174 status = mpc_demux_decode(demux, &frame);
175 ci->yield();
176 if (frame.bits == -1) /* decoding stopped */
178 retval = (status == MPC_STATUS_OK) ? CODEC_OK : CODEC_ERROR;
179 goto done;
181 else
183 ci->pcmbuf_insert(frame.buffer,
184 frame.buffer + MPC_FRAME_LENGTH,
185 frame.samples);
186 samplesdone += frame.samples;
187 elapsed_time = (samplesdone*10)/frequency;
188 ci->set_elapsed(elapsed_time);
189 /* Remark: rockbox offset is the file offset in bytes. So estimate
190 * this offset from the samples, sampling frequency and bitrate */
191 ci->set_offset( (samplesdone * byterate)/(frequency*100) );
193 } while (true);
195 done:
196 if (ci->request_next_track())
197 goto next_track;
199 exit:
200 return retval;