2007-07-03 09:25:36 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2007-07-03 10:21:23 +00:00
|
|
|
* $Id$
|
2007-07-03 09:25:36 +00:00
|
|
|
*
|
2008-04-16 19:42:26 +00:00
|
|
|
* Copyright (C) 2007 Dave Chapman
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-07-03 09:25:36 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "codeclib.h"
|
2010-05-02 17:19:42 +00:00
|
|
|
#include "libasf/asf.h"
|
2007-07-03 09:25:36 +00:00
|
|
|
#include "libwma/wmadec.h"
|
|
|
|
|
|
|
|
CODEC_HEADER
|
|
|
|
|
2007-07-18 09:43:12 +00:00
|
|
|
/* NOTE: WMADecodeContext is 120152 bytes (on x86) */
|
2007-07-03 09:25:36 +00:00
|
|
|
static WMADecodeContext wmadec;
|
|
|
|
|
|
|
|
/* this is the codec entry point */
|
|
|
|
enum codec_status codec_main(void)
|
|
|
|
{
|
|
|
|
uint32_t elapsedtime;
|
|
|
|
int retval;
|
|
|
|
asf_waveformatex_t wfx;
|
|
|
|
size_t resume_offset;
|
2007-07-09 17:24:00 +00:00
|
|
|
int i;
|
2007-07-10 13:37:16 +00:00
|
|
|
int wmares, res;
|
|
|
|
uint8_t* audiobuf;
|
|
|
|
int audiobufsize;
|
2008-11-20 11:27:31 +00:00
|
|
|
int packetlength = 0;
|
2007-11-22 05:32:26 +00:00
|
|
|
int errcount = 0;
|
2007-07-03 09:25:36 +00:00
|
|
|
|
|
|
|
/* Generic codec initialisation */
|
2008-07-17 17:43:49 +00:00
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
|
2007-07-03 09:25:36 +00:00
|
|
|
|
2007-10-17 12:51:37 +00:00
|
|
|
next_track:
|
2007-07-03 09:25:36 +00:00
|
|
|
|
|
|
|
/* Wait for the metadata to be read */
|
|
|
|
while (!*ci->taginfo_ready && !ci->stop_codec)
|
|
|
|
ci->sleep(1);
|
|
|
|
|
|
|
|
retval = CODEC_OK;
|
|
|
|
|
|
|
|
/* Remember the resume position - when the codec is opened, the
|
|
|
|
playback engine will reset it. */
|
|
|
|
resume_offset = ci->id3->offset;
|
2008-04-29 19:00:42 +00:00
|
|
|
restart_track:
|
2007-07-03 09:25:36 +00:00
|
|
|
if (codec_init()) {
|
|
|
|
LOGF("WMA: Error initialising codec\n");
|
|
|
|
retval = CODEC_ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2007-07-11 16:58:11 +00:00
|
|
|
/* Copy the format metadata we've stored in the id3 TOC field. This
|
2007-07-03 09:25:36 +00:00
|
|
|
saves us from parsing it again here. */
|
|
|
|
memcpy(&wfx, ci->id3->toc, sizeof(wfx));
|
|
|
|
|
2007-07-11 23:07:41 +00:00
|
|
|
if (wma_decode_init(&wmadec,&wfx) < 0) {
|
|
|
|
LOGF("WMA: Unsupported or corrupt file\n");
|
|
|
|
retval = CODEC_ERROR;
|
|
|
|
goto exit;
|
2007-07-11 16:58:11 +00:00
|
|
|
}
|
2007-07-03 09:25:36 +00:00
|
|
|
|
2008-04-12 19:52:31 +00:00
|
|
|
if (resume_offset > ci->id3->first_frame_offset)
|
|
|
|
{
|
|
|
|
/* Get start of current packet */
|
2008-07-17 17:43:49 +00:00
|
|
|
int packet_offset = (resume_offset - ci->id3->first_frame_offset)
|
2008-04-12 19:52:31 +00:00
|
|
|
% wfx.packet_size;
|
|
|
|
ci->seek_buffer(resume_offset - packet_offset);
|
2010-05-02 20:07:11 +00:00
|
|
|
elapsedtime = asf_get_timestamp(&i);
|
2008-04-12 19:52:31 +00:00
|
|
|
ci->set_elapsed(elapsedtime);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Now advance the file position to the first frame */
|
|
|
|
ci->seek_buffer(ci->id3->first_frame_offset);
|
|
|
|
elapsedtime = 0;
|
|
|
|
}
|
2007-07-03 09:25:36 +00:00
|
|
|
|
2008-04-29 19:00:42 +00:00
|
|
|
resume_offset = 0;
|
2007-07-03 09:25:36 +00:00
|
|
|
ci->configure(DSP_SWITCH_FREQUENCY, wfx.rate);
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, wfx.channels == 1 ?
|
2010-07-28 12:45:21 +00:00
|
|
|
STEREO_MONO : STEREO_NONINTERLEAVED);
|
2007-07-03 09:25:36 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
|
|
|
|
|
|
|
/* The main decoding loop */
|
|
|
|
|
|
|
|
res = 1;
|
|
|
|
while (res >= 0)
|
|
|
|
{
|
|
|
|
ci->yield();
|
|
|
|
if (ci->stop_codec || ci->new_track) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2007-10-17 12:51:37 +00:00
|
|
|
/* Deal with any pending seek requests */
|
2007-11-22 05:32:26 +00:00
|
|
|
if (ci->seek_time){
|
|
|
|
|
2007-10-17 18:19:35 +00:00
|
|
|
if (ci->seek_time == 1) {
|
|
|
|
ci->seek_complete();
|
2008-04-29 19:00:42 +00:00
|
|
|
goto restart_track; /* Pretend you never saw this... */
|
2007-10-17 18:19:35 +00:00
|
|
|
}
|
2007-07-03 09:25:36 +00:00
|
|
|
|
2010-05-21 16:05:28 +00:00
|
|
|
elapsedtime = asf_seek(ci->seek_time, &wfx);
|
2007-12-01 02:46:31 +00:00
|
|
|
if (elapsedtime < 1){
|
2007-11-22 05:32:26 +00:00
|
|
|
ci->seek_complete();
|
|
|
|
goto next_track;
|
|
|
|
}
|
2008-04-07 02:32:48 +00:00
|
|
|
/*DEBUGF("Seek returned %d\n", (int)elapsedtime);*/
|
2007-11-22 05:32:26 +00:00
|
|
|
ci->set_elapsed(elapsedtime);
|
|
|
|
|
|
|
|
/*flush the wma decoder state*/
|
2007-11-30 00:46:32 +00:00
|
|
|
wmadec.last_superframe_len = 0;
|
|
|
|
wmadec.last_bitoffset = 0;
|
|
|
|
ci->seek_complete();
|
2007-11-22 05:32:26 +00:00
|
|
|
}
|
|
|
|
errcount = 0;
|
|
|
|
new_packet:
|
2010-05-02 20:07:11 +00:00
|
|
|
res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &wfx);
|
2007-11-22 05:32:26 +00:00
|
|
|
|
2007-12-01 02:46:31 +00:00
|
|
|
if (res < 0) {
|
|
|
|
/* We'll try to recover from a parse error a certain number of
|
|
|
|
* times. If we succeed, the error counter will be reset.
|
|
|
|
*/
|
2007-11-22 05:32:26 +00:00
|
|
|
|
2007-12-01 02:46:31 +00:00
|
|
|
errcount++;
|
2008-01-06 02:50:01 +00:00
|
|
|
DEBUGF("read_packet error %d, errcount %d\n",wmares, errcount);
|
2007-12-01 02:46:31 +00:00
|
|
|
if (errcount > 5) {
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
ci->advance_buffer(packetlength);
|
|
|
|
goto new_packet;
|
|
|
|
}
|
|
|
|
} else if (res > 0) {
|
|
|
|
wma_decode_superframe_init(&wmadec, audiobuf, audiobufsize);
|
2007-07-09 17:24:00 +00:00
|
|
|
|
|
|
|
for (i=0; i < wmadec.nb_frames; i++)
|
|
|
|
{
|
|
|
|
wmares = wma_decode_superframe_frame(&wmadec,
|
2007-07-10 13:37:16 +00:00
|
|
|
audiobuf, audiobufsize);
|
2007-11-30 00:46:32 +00:00
|
|
|
|
2007-07-09 17:24:00 +00:00
|
|
|
ci->yield ();
|
|
|
|
|
2007-12-01 02:46:31 +00:00
|
|
|
if (wmares < 0) {
|
|
|
|
/* Do the above, but for errors in decode. */
|
2007-11-22 05:32:26 +00:00
|
|
|
errcount++;
|
|
|
|
DEBUGF("WMA decode error %d, errcount %d\n",wmares, errcount);
|
|
|
|
if (errcount > 5) {
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
ci->advance_buffer(packetlength);
|
|
|
|
goto new_packet;
|
|
|
|
}
|
2007-07-09 17:24:00 +00:00
|
|
|
} else if (wmares > 0) {
|
2010-07-28 12:45:21 +00:00
|
|
|
ci->pcmbuf_insert((*wmadec.frame_out)[0], (*wmadec.frame_out)[1], wmares);
|
2007-11-22 05:32:26 +00:00
|
|
|
elapsedtime += (wmares*10)/(wfx.rate/100);
|
2007-07-09 17:24:00 +00:00
|
|
|
ci->set_elapsed(elapsedtime);
|
|
|
|
}
|
2007-11-22 05:32:26 +00:00
|
|
|
ci->yield();
|
2007-07-03 09:25:36 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-10 13:37:16 +00:00
|
|
|
|
|
|
|
ci->advance_buffer(packetlength);
|
2007-07-03 09:25:36 +00:00
|
|
|
}
|
|
|
|
retval = CODEC_OK;
|
|
|
|
|
|
|
|
done:
|
2008-04-07 02:32:48 +00:00
|
|
|
/*LOGF("WMA: Decoded %ld samples\n",elapsedtime*wfx.rate/1000);*/
|
2007-07-03 09:25:36 +00:00
|
|
|
|
|
|
|
if (ci->request_next_track())
|
|
|
|
goto next_track;
|
|
|
|
exit:
|
|
|
|
return retval;
|
|
|
|
}
|