Forgot to remove wps_data.remove_wps in checkwps too.
[kugel-rb.git] / apps / codecs / mpc.c
blob250a03fbfd63dc11d21683ddb39ba8405037c619
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 static 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 static 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 static mpc_int32_t tell_impl(void *data)
48 struct codec_api *ci = (struct codec_api *)data;
50 return ci->curpos;
53 static mpc_int32_t get_size_impl(void *data)
55 struct codec_api *ci = (struct codec_api *)data;
57 return ci->filesize;
60 static 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 uint32_t frequency; /* 0.1 kHz accuracy */
76 uint32_t elapsed_time; /* milliseconds */
77 unsigned status;
78 mpc_reader reader;
79 mpc_streaminfo info;
80 int retval = CODEC_OK;
82 /* musepack's sample representation is 18.14
83 * DSP_SET_SAMPLE_DEPTH = 14 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 29 */
84 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
86 /* Create a decoder instance */
87 reader.read = read_impl;
88 reader.seek = seek_impl;
89 reader.tell = tell_impl;
90 reader.get_size = get_size_impl;
91 reader.canseek = canseek_impl;
92 reader.data = ci;
94 next_track:
95 if (codec_init()) {
96 retval = CODEC_ERROR;
97 goto exit;
100 while (!*ci->taginfo_ready && !ci->stop_codec)
101 ci->sleep(1);
103 samplesdone = ci->id3->offset;
105 /* read file's streaminfo data */
106 mpc_streaminfo_init(&info);
107 if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) {
108 retval = CODEC_ERROR;
109 goto done;
111 frequency = info.sample_freq / 100; /* 0.1 kHz accuracy */
112 ci->configure(DSP_SWITCH_FREQUENCY, info.sample_freq);
114 /* set playback engine up for correct number of channels */
115 /* NOTE: current musepack format only allows for stereo files
116 but code is here to handle other configurations anyway */
117 if (info.channels == 2)
118 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
119 else if (info.channels == 1)
120 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
121 else {
122 retval = CODEC_ERROR;
123 goto done;
126 codec_set_replaygain(ci->id3);
127 /* instantiate a decoder with our file reader */
128 mpc_decoder_setup(&decoder, &reader);
129 if (!mpc_decoder_initialize(&decoder, &info)) {
130 retval = CODEC_ERROR;
131 goto done;
134 /* Resume to saved sample offset. */
135 if (samplesdone > 0) {
136 /* hack to improve seek time if filebuf goes empty */
137 if (mpc_decoder_seek_sample(&decoder, samplesdone)) {
138 elapsed_time = (samplesdone*10)/frequency;
139 ci->set_elapsed(elapsed_time);
140 } else {
141 samplesdone = 0;
143 /* reset chunksize */
146 /* This is the decoding loop. */
147 do {
148 /* Complete seek handler. */
149 if (ci->seek_time) {
150 /* hack to improve seek time if filebuf goes empty */
151 mpc_int64_t new_offset = ((ci->seek_time - 1)/10)*frequency;
152 if (mpc_decoder_seek_sample(&decoder, new_offset)) {
153 samplesdone = new_offset;
154 ci->set_elapsed(ci->seek_time);
156 ci->seek_complete();
157 /* reset chunksize */
159 if (ci->stop_codec || ci->new_track)
160 break;
162 status = mpc_decoder_decode(&decoder, sample_buffer, NULL, NULL);
163 ci->yield();
164 if (status == 0) /* end of file reached */
165 goto done;
166 if (status == (unsigned)(-1)) { /* decode error */
167 retval = CODEC_ERROR;
168 goto done;
169 } else {
170 ci->pcmbuf_insert(sample_buffer,
171 sample_buffer + MPC_FRAME_LENGTH,
172 status);
173 samplesdone += status;
174 elapsed_time = (samplesdone*10)/frequency;
175 ci->set_elapsed(elapsed_time);
176 ci->set_offset(samplesdone);
178 } while (status != 0);
179 retval = CODEC_OK;
181 done:
182 if (ci->request_next_track())
183 goto next_track;
185 exit:
186 return retval;