2005-06-13 06:15:05 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 David Bryant
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2005-10-13 11:32:52 +00:00
|
|
|
#include "codeclib.h"
|
|
|
|
#include "libwavpack/wavpack.h"
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
static struct codec_api *ci;
|
|
|
|
|
2005-09-30 21:11:31 +00:00
|
|
|
#define FORCE_DSP_USE /* fixes some WavPack bugs; adds about 12% to boost ratio
|
|
|
|
(when DSP would not have been used) */
|
|
|
|
|
2005-06-13 06:15:05 +00:00
|
|
|
#define BUFFER_SIZE 4096
|
|
|
|
|
2005-10-27 11:32:02 +00:00
|
|
|
static long temp_buffer [BUFFER_SIZE] IBSS_ATTR;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
static long read_callback (void *buffer, long bytes)
|
|
|
|
{
|
2005-06-30 06:09:59 +00:00
|
|
|
long retval = ci->read_filebuf (buffer, bytes);
|
|
|
|
ci->id3->offset = ci->curpos;
|
|
|
|
return retval;
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
|
2005-09-23 00:12:02 +00:00
|
|
|
#ifdef USE_IRAM
|
2005-06-13 06:15:05 +00:00
|
|
|
extern char iramcopy[];
|
|
|
|
extern char iramstart[];
|
|
|
|
extern char iramend[];
|
|
|
|
#endif
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* this is the codec entry point */
|
2005-06-22 19:55:09 +00:00
|
|
|
enum codec_status codec_start(struct codec_api* api)
|
2005-06-13 06:15:05 +00:00
|
|
|
{
|
|
|
|
WavpackContext *wpc;
|
|
|
|
char error [80];
|
2005-06-27 00:12:40 +00:00
|
|
|
int bps, nchans, sr_100;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* Generic codec initialisation */
|
|
|
|
TEST_CODEC_API(api);
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
ci = api;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-09-23 00:12:02 +00:00
|
|
|
#ifdef USE_IRAM
|
|
|
|
ci->memcpy(iramstart, iramcopy, iramend-iramstart);
|
2005-06-13 06:15:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*10));
|
|
|
|
ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512));
|
|
|
|
ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*128));
|
2005-06-26 19:41:29 +00:00
|
|
|
|
|
|
|
ci->configure(DSP_DITHER, (bool *)false);
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED);
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(16));
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
next_track:
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
if (codec_init(api))
|
|
|
|
return CODEC_ERROR;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-09-23 00:12:02 +00:00
|
|
|
while (!*ci->taginfo_ready && !ci->stop_codec)
|
2005-07-11 18:47:47 +00:00
|
|
|
ci->sleep(1);
|
2005-06-26 19:41:29 +00:00
|
|
|
|
2005-09-30 21:11:31 +00:00
|
|
|
#ifdef FORCE_DSP_USE
|
|
|
|
ci->configure(CODEC_DSP_ENABLE, (bool *)true);
|
|
|
|
ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency));
|
|
|
|
codec_set_replaygain(ci->id3);
|
|
|
|
#else
|
2005-07-25 03:34:25 +00:00
|
|
|
if (ci->id3->frequency != NATIVE_FREQUENCY ||
|
|
|
|
ci->global_settings->replaygain) {
|
|
|
|
ci->configure(CODEC_DSP_ENABLE, (bool *)true);
|
|
|
|
ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency));
|
2005-09-23 00:12:02 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
2005-06-26 19:41:29 +00:00
|
|
|
}
|
2005-07-25 03:34:25 +00:00
|
|
|
else
|
|
|
|
ci->configure(CODEC_DSP_ENABLE, (bool *)false);
|
2005-09-30 21:11:31 +00:00
|
|
|
#endif
|
|
|
|
|
2005-06-13 06:15:05 +00:00
|
|
|
/* Create a decoder instance */
|
|
|
|
wpc = WavpackOpenFileInput (read_callback, error);
|
|
|
|
|
|
|
|
if (!wpc)
|
2005-06-22 19:41:30 +00:00
|
|
|
return CODEC_ERROR;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
bps = WavpackGetBytesPerSample (wpc);
|
|
|
|
nchans = WavpackGetReducedChannels (wpc);
|
2005-06-27 00:12:40 +00:00
|
|
|
sr_100 = ci->id3->frequency / 100;
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
ci->set_elapsed (0);
|
|
|
|
|
|
|
|
/* The main decoder loop */
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
long nsamples;
|
|
|
|
|
|
|
|
if (ci->seek_time && ci->taginfo_ready && ci->id3->length) {
|
2005-11-06 19:18:04 +00:00
|
|
|
ci->seek_time--;
|
2005-06-27 00:12:40 +00:00
|
|
|
int curpos_ms = WavpackGetSampleIndex (wpc) / sr_100 * 10;
|
2005-06-13 06:15:05 +00:00
|
|
|
int n, d, skip;
|
|
|
|
|
|
|
|
if (ci->seek_time > curpos_ms) {
|
|
|
|
n = ci->seek_time - curpos_ms;
|
|
|
|
d = ci->id3->length - curpos_ms;
|
|
|
|
skip = (int)((long long)(ci->filesize - ci->curpos) * n / d);
|
|
|
|
ci->seek_buffer (ci->curpos + skip);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = curpos_ms - ci->seek_time;
|
|
|
|
d = curpos_ms;
|
|
|
|
skip = (int)((long long) ci->curpos * n / d);
|
|
|
|
ci->seek_buffer (ci->curpos - skip);
|
|
|
|
}
|
|
|
|
|
|
|
|
wpc = WavpackOpenFileInput (read_callback, error);
|
2005-11-02 00:09:42 +00:00
|
|
|
ci->seek_complete();
|
2005-06-13 06:15:05 +00:00
|
|
|
|
|
|
|
if (!wpc)
|
|
|
|
break;
|
|
|
|
|
2005-06-27 00:12:40 +00:00
|
|
|
ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
|
2005-09-23 00:12:02 +00:00
|
|
|
ci->yield ();
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsamples = WavpackUnpackSamples (wpc, temp_buffer, BUFFER_SIZE / 2);
|
|
|
|
|
|
|
|
if (!nsamples || ci->stop_codec || ci->reload_codec)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* convert mono to stereo here, in place */
|
|
|
|
|
|
|
|
if (nchans == 1) {
|
|
|
|
long *dst = temp_buffer + (nsamples * 2);
|
|
|
|
long *src = temp_buffer + nsamples;
|
|
|
|
long count = nsamples;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
*--dst = *--src;
|
|
|
|
*--dst = *src;
|
|
|
|
if (!(count & 0x7f))
|
2005-09-23 00:12:02 +00:00
|
|
|
ci->yield ();
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bps == 1) {
|
|
|
|
short *dst = (short *) temp_buffer;
|
|
|
|
long *src = temp_buffer;
|
|
|
|
long count = nsamples;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
*dst++ = *src++ << 8;
|
|
|
|
*dst++ = *src++ << 8;
|
|
|
|
if (!(count & 0x7f))
|
2005-09-23 00:12:02 +00:00
|
|
|
ci->yield ();
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (bps == 2) {
|
|
|
|
short *dst = (short *) temp_buffer;
|
|
|
|
long *src = temp_buffer;
|
|
|
|
long count = nsamples;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
*dst++ = *src++;
|
|
|
|
*dst++ = *src++;
|
|
|
|
if (!(count & 0x7f))
|
2005-09-23 00:12:02 +00:00
|
|
|
ci->yield ();
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
short *dst = (short *) temp_buffer;
|
|
|
|
int shift = (bps - 2) * 8;
|
|
|
|
long *src = temp_buffer;
|
|
|
|
long count = nsamples;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
*dst++ = *src++ >> shift;
|
|
|
|
*dst++ = *src++ >> shift;
|
|
|
|
if (!(count & 0x7f))
|
2005-09-23 00:12:02 +00:00
|
|
|
ci->yield ();
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ci->stop_codec || ci->reload_codec)
|
|
|
|
break;
|
|
|
|
|
2005-07-13 12:48:22 +00:00
|
|
|
while (!ci->pcmbuf_insert ((char *) temp_buffer, nsamples * 4))
|
2005-09-23 00:12:02 +00:00
|
|
|
ci->sleep (1);
|
2005-06-13 06:15:05 +00:00
|
|
|
|
2005-06-27 00:12:40 +00:00
|
|
|
ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ci->request_next_track())
|
|
|
|
goto next_track;
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
return CODEC_OK;
|
2005-06-13 06:15:05 +00:00
|
|
|
}
|