1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Thom Johansen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 #include <codecs/libmusepack/musepack.h>
25 mpc_decoder decoder IBSS_ATTR
;
27 /* Our implementations of the mpc_reader callback functions. */
28 mpc_int32_t
read_impl(void *data
, void *ptr
, mpc_int32_t size
)
30 struct codec_api
*ci
= (struct codec_api
*)data
;
32 return ((mpc_int32_t
)(ci
->read_filebuf(ptr
, size
)));
35 mpc_bool_t
seek_impl(void *data
, mpc_int32_t offset
)
37 struct codec_api
*ci
= (struct codec_api
*)data
;
39 /* WARNING: assumes we don't need to skip too far into the past,
40 this might not be supported by the buffering layer yet */
41 return ci
->seek_buffer(offset
);
44 mpc_int32_t
tell_impl(void *data
)
46 struct codec_api
*ci
= (struct codec_api
*)data
;
51 mpc_int32_t
get_size_impl(void *data
)
53 struct codec_api
*ci
= (struct codec_api
*)data
;
58 mpc_bool_t
canseek_impl(void *data
)
62 /* doesn't much matter, libmusepack ignores this anyway */
66 MPC_SAMPLE_FORMAT sample_buffer
[MPC_DECODER_BUFFER_LENGTH
]
67 IBSS_ATTR_MPC_SAMPLE_BUF
;
69 /* this is the codec entry point */
70 enum codec_status
codec_main(void)
72 mpc_int64_t samplesdone
;
73 unsigned long frequency
;
77 int retval
= CODEC_OK
;
79 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 28);
81 /* Create a decoder instance */
82 reader
.read
= read_impl
;
83 reader
.seek
= seek_impl
;
84 reader
.tell
= tell_impl
;
85 reader
.get_size
= get_size_impl
;
86 reader
.canseek
= canseek_impl
;
95 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
98 samplesdone
= ci
->id3
->offset
;
100 /* read file's streaminfo data */
101 mpc_streaminfo_init(&info
);
102 if (mpc_streaminfo_read(&info
, &reader
) != ERROR_CODE_OK
) {
103 retval
= CODEC_ERROR
;
106 frequency
= info
.sample_freq
/ 1000;
107 ci
->configure(DSP_SWITCH_FREQUENCY
, info
.sample_freq
);
109 /* set playback engine up for correct number of channels */
110 /* NOTE: current musepack format only allows for stereo files
111 but code is here to handle other configurations anyway */
112 if (info
.channels
== 2)
113 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
114 else if (info
.channels
== 1)
115 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_MONO
);
117 retval
= CODEC_ERROR
;
121 codec_set_replaygain(ci
->id3
);
122 /* instantiate a decoder with our file reader */
123 mpc_decoder_setup(&decoder
, &reader
);
124 if (!mpc_decoder_initialize(&decoder
, &info
)) {
125 retval
= CODEC_ERROR
;
129 /* Resume to saved sample offset. */
130 if(samplesdone
> 0) {
131 /* hack to improve seek time if filebuf goes empty */
132 if (mpc_decoder_seek_sample(&decoder
, samplesdone
)) {
133 ci
->set_elapsed(samplesdone
/frequency
);
137 /* reset chunksize */
140 /* This is the decoding loop. */
143 /* Complete seek handler. */
145 /* hack to improve seek time if filebuf goes empty */
146 mpc_int64_t new_offset
= (ci
->seek_time
- 1)*frequency
;
147 if (mpc_decoder_seek_sample(&decoder
, new_offset
)) {
148 samplesdone
= new_offset
;
149 ci
->set_elapsed(ci
->seek_time
);
152 /* reset chunksize */
156 /* Seek to start of track handler. */
158 if (ci
->seek_time
== 1 && mpc_decoder_seek_sample(&decoder
, 0)) {
165 if (ci
->stop_codec
|| ci
->new_track
)
168 status
= mpc_decoder_decode(&decoder
, sample_buffer
, NULL
, NULL
);
170 if (status
== 0) /* end of file reached */
172 if (status
== (unsigned)(-1)) { /* decode error */
173 retval
= CODEC_ERROR
;
176 ci
->pcmbuf_insert(sample_buffer
,
177 sample_buffer
+ MPC_FRAME_LENGTH
,
179 samplesdone
+= status
;
180 ci
->set_elapsed(samplesdone
/frequency
);
181 ci
->set_offset(samplesdone
);
183 } while (status
!= 0);
187 if (ci
->request_next_track())
191 mpc_decoder_destroy(&decoder
);