1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include <codecs/libmusepack/musepack.h>
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
;
53 mpc_int32_t
get_size_impl(void *data
)
55 struct codec_api
*ci
= (struct codec_api
*)data
;
60 mpc_bool_t
canseek_impl(void *data
)
64 /* doesn't much matter, libmusepack ignores this anyway */
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
;
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
;
99 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
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
;
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
);
121 retval
= CODEC_ERROR
;
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
;
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
);
141 /* reset chunksize */
144 /* This is the decoding loop. */
147 /* Complete seek handler. */
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
);
156 /* reset chunksize */
160 /* Seek to start of track handler. */
162 if (ci
->seek_time
== 1 && mpc_decoder_seek_sample(&decoder
, 0)) {
169 if (ci
->stop_codec
|| ci
->new_track
)
172 status
= mpc_decoder_decode(&decoder
, sample_buffer
, NULL
, NULL
);
174 if (status
== 0) /* end of file reached */
176 if (status
== (unsigned)(-1)) { /* decode error */
177 retval
= CODEC_ERROR
;
180 ci
->pcmbuf_insert(sample_buffer
,
181 sample_buffer
+ MPC_FRAME_LENGTH
,
183 samplesdone
+= status
;
184 ci
->set_elapsed(samplesdone
/frequency
);
185 ci
->set_offset(samplesdone
);
187 } while (status
!= 0);
191 if (ci
->request_next_track())