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
;
68 mpc_uint32_t seek_table
[10000];
70 /* this is the codec entry point */
71 enum codec_status
codec_main(void)
73 mpc_int64_t samplesdone
;
74 unsigned long frequency
;
78 int retval
= CODEC_OK
;
80 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 28);
82 /* Create a decoder instance */
83 reader
.read
= read_impl
;
84 reader
.seek
= seek_impl
;
85 reader
.tell
= tell_impl
;
86 reader
.get_size
= get_size_impl
;
87 reader
.canseek
= canseek_impl
;
90 /* Ensure that SeekTable is clear since decoder is reused */
91 decoder
.SeekTable
= NULL
;
93 mpc_decoder_set_seek_table(&decoder
, seek_table
, sizeof(seek_table
));
101 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
104 samplesdone
= ci
->id3
->offset
;
106 /* read file's streaminfo data */
107 mpc_streaminfo_init(&info
);
108 if (mpc_streaminfo_read(&info
, &reader
) != ERROR_CODE_OK
) {
109 retval
= CODEC_ERROR
;
112 frequency
= info
.sample_freq
/ 1000;
113 ci
->configure(DSP_SWITCH_FREQUENCY
, info
.sample_freq
);
115 /* set playback engine up for correct number of channels */
116 /* NOTE: current musepack format only allows for stereo files
117 but code is here to handle other configurations anyway */
118 if (info
.channels
== 2)
119 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_NONINTERLEAVED
);
120 else if (info
.channels
== 1)
121 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_MONO
);
123 retval
= CODEC_ERROR
;
127 codec_set_replaygain(ci
->id3
);
128 /* instantiate a decoder with our file reader */
129 mpc_decoder_setup(&decoder
, &reader
);
130 if (!mpc_decoder_initialize(&decoder
, &info
)) {
131 retval
= CODEC_ERROR
;
135 /* Resume to saved sample offset. */
136 if(samplesdone
> 0) {
137 /* hack to improve seek time if filebuf goes empty */
138 if (mpc_decoder_seek_sample(&decoder
, samplesdone
)) {
139 ci
->set_elapsed(samplesdone
/frequency
);
143 /* reset chunksize */
146 /* This is the decoding loop. */
149 /* Complete seek handler. */
151 /* hack to improve seek time if filebuf goes empty */
152 mpc_int64_t new_offset
= (ci
->seek_time
- 1)*frequency
;
153 if (mpc_decoder_seek_sample(&decoder
, new_offset
)) {
154 samplesdone
= new_offset
;
155 ci
->set_elapsed(ci
->seek_time
);
158 /* reset chunksize */
162 /* Seek to start of track handler. */
164 if (ci
->seek_time
== 1 && mpc_decoder_seek_sample(&decoder
, 0)) {
171 if (ci
->stop_codec
|| ci
->new_track
)
174 status
= mpc_decoder_decode(&decoder
, sample_buffer
, NULL
, NULL
);
176 if (status
== 0) /* end of file reached */
178 if (status
== (unsigned)(-1)) { /* decode error */
179 retval
= CODEC_ERROR
;
182 ci
->pcmbuf_insert(sample_buffer
,
183 sample_buffer
+ MPC_FRAME_LENGTH
,
185 samplesdone
+= status
;
186 ci
->set_elapsed(samplesdone
/frequency
);
187 ci
->set_offset(samplesdone
);
189 } while (status
!= 0);
193 if (ci
->request_next_track())
197 mpc_decoder_destroy(&decoder
);