Manual: Fix part 1 of FS#11874 - touchscreen region in the manual isn't 100% correct.
[kugel-rb.git] / apps / codecs / mpc.c
blob1d83449838ac55ee0600003abc7a02922d945b4e
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 static 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 (void)reader;
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 (void)reader;
40 return ci->seek_buffer(offset);
43 static mpc_int32_t tell_impl(mpc_reader *reader)
45 (void)reader;
46 return ci->curpos;
49 static mpc_int32_t get_size_impl(mpc_reader *reader)
51 (void)reader;
52 return ci->filesize;
55 /* This is the codec entry point. */
56 enum codec_status codec_main(void)
58 mpc_int64_t samplesdone;
59 uint32_t frequency; /* 0.1 kHz accuracy */
60 uint32_t elapsed_time; /* milliseconds */
61 uint32_t byterate; /* bytes per second */
62 mpc_status status;
63 mpc_reader reader;
64 mpc_streaminfo info;
65 mpc_frame_info frame;
66 mpc_demux *demux = NULL;
67 int retval = CODEC_OK;
69 frame.buffer = sample_buffer;
71 /* musepack's sample representation is 18.14
72 * DSP_SET_SAMPLE_DEPTH = 14 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 29 */
73 ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
75 /* Create a decoder instance */
76 reader.read = read_impl;
77 reader.seek = seek_impl;
78 reader.tell = tell_impl;
79 reader.get_size = get_size_impl;
81 next_track:
82 if (codec_init())
84 retval = CODEC_ERROR;
85 goto exit;
88 while (!*ci->taginfo_ready && !ci->stop_codec)
89 ci->sleep(1);
91 /* Initialize demux/decoder. */
92 demux = mpc_demux_init(&reader);
93 if (NULL == demux)
95 retval = CODEC_ERROR;
96 goto done;
98 /* Read file's streaminfo data. */
99 mpc_demux_get_info(demux, &info);
101 byterate = (mpc_uint32_t)(info.average_bitrate) / 8;
102 frequency = info.sample_freq / 100; /* 0.1 kHz accuracy */
103 ci->configure(DSP_SWITCH_FREQUENCY, info.sample_freq);
105 /* Remark: rockbox offset is the file offset in bytes. So, estimate the
106 * sample seek position from the file offset, the sampling frequency and
107 * the bitrate. As the saved position is exactly calculated the reverse way
108 * there is no loss of information except rounding. */
109 samplesdone = 100 * ((mpc_uint64_t)(ci->id3->offset * frequency) / byterate);
111 /* Set up digital signal processing for correct number of channels */
112 /* NOTE: current musepack format only allows for stereo files
113 but code is here to handle other configurations anyway */
114 if (info.channels == 2)
115 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
116 else if (info.channels == 1)
117 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
118 else
120 retval = CODEC_ERROR;
121 goto done;
124 codec_set_replaygain(ci->id3);
126 /* Resume to saved sample offset. */
127 if (samplesdone > 0)
129 if (mpc_demux_seek_sample(demux, samplesdone) == MPC_STATUS_OK)
131 elapsed_time = (samplesdone*10)/frequency;
132 ci->set_elapsed(elapsed_time);
134 else
136 samplesdone = 0;
140 /* This is the decoding loop. */
143 /* Complete seek handler. */
144 if (ci->seek_time)
146 mpc_int64_t new_offset = ((ci->seek_time - 1)/10)*frequency;
147 if (mpc_demux_seek_sample(demux, new_offset) == MPC_STATUS_OK)
149 samplesdone = new_offset;
150 ci->set_elapsed(ci->seek_time);
152 ci->seek_complete();
155 /* Stop or skip occured, exit decoding loop. */
156 if (ci->stop_codec || ci->new_track)
157 break;
159 /* Decode one frame. */
160 status = mpc_demux_decode(demux, &frame);
161 ci->yield();
162 if (frame.bits == -1)
164 /* Decoding error, exit decoding loop. */
165 retval = (status == MPC_STATUS_OK) ? CODEC_OK : CODEC_ERROR;
166 goto done;
168 else
170 /* Decoding passed, insert samples to PCM buffer. */
171 ci->pcmbuf_insert(frame.buffer,
172 frame.buffer + MPC_FRAME_LENGTH,
173 frame.samples);
174 samplesdone += frame.samples;
175 elapsed_time = (samplesdone*10)/frequency;
176 ci->set_elapsed(elapsed_time);
177 /* Remark: rockbox offset is the file offset in bytes. So estimate
178 * this offset from the samples, sampling frequency and bitrate */
179 ci->set_offset( (samplesdone * byterate)/(frequency*100) );
181 } while (true);
183 done:
184 if (ci->request_next_track())
185 goto next_track;
187 exit:
188 return retval;