2005-10-31 20:56:29 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "codeclib.h"
|
|
|
|
#include "libm4a/m4a.h"
|
|
|
|
#include "libfaad/common.h"
|
|
|
|
#include "libfaad/structs.h"
|
|
|
|
#include "libfaad/decoder.h"
|
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2005-10-31 20:56:29 +00:00
|
|
|
#ifndef SIMULATOR
|
|
|
|
extern char iramcopy[];
|
|
|
|
extern char iramstart[];
|
|
|
|
extern char iramend[];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct codec_api* rb;
|
|
|
|
struct codec_api* ci;
|
|
|
|
|
|
|
|
/* this is the codec entry point */
|
|
|
|
enum codec_status codec_start(struct codec_api* api)
|
|
|
|
{
|
2005-10-31 23:34:14 +00:00
|
|
|
size_t n;
|
|
|
|
static demux_res_t demux_res;
|
|
|
|
stream_t input_stream;
|
|
|
|
uint32_t samplesdone;
|
|
|
|
uint32_t elapsedtime;
|
|
|
|
uint32_t sample_duration;
|
|
|
|
uint32_t sample_byte_size;
|
|
|
|
int samplesdecoded;
|
|
|
|
unsigned int i;
|
|
|
|
unsigned char* buffer;
|
|
|
|
static NeAACDecFrameInfo frameInfo;
|
|
|
|
NeAACDecHandle hDecoder;
|
|
|
|
int err;
|
|
|
|
int16_t* decodedbuffer;
|
|
|
|
|
|
|
|
/* Generic codec initialisation */
|
|
|
|
rb = api;
|
2006-01-18 00:05:14 +00:00
|
|
|
ci = api;
|
2005-10-31 20:56:29 +00:00
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
2005-10-31 23:34:14 +00:00
|
|
|
rb->memcpy(iramstart, iramcopy, iramend-iramstart);
|
2005-10-31 20:56:29 +00:00
|
|
|
#endif
|
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*16));
|
|
|
|
ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512));
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
ci->configure(DSP_DITHER, (bool *)false);
|
2005-11-02 19:43:52 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_NONINTERLEAVED);
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(29));
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-04-15 02:03:11 +00:00
|
|
|
next_track:
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
if (codec_init(api)) {
|
|
|
|
LOGF("FAAD: Error initialising codec\n");
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
|
|
|
goto exit;
|
2005-10-31 20:56:29 +00:00
|
|
|
}
|
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
while (!rb->taginfo_ready)
|
|
|
|
rb->yield();
|
|
|
|
|
|
|
|
ci->configure(DSP_SET_FREQUENCY, (long *)(rb->id3->frequency));
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
stream_create(&input_stream,ci);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
/* if qtmovie_read returns successfully, the stream is up to
|
|
|
|
* the movie data, which can be used directly by the decoder */
|
|
|
|
if (!qtmovie_read(&input_stream, &demux_res)) {
|
|
|
|
LOGF("FAAD: Error initialising file\n");
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
/* initialise the sound converter */
|
|
|
|
hDecoder = NULL;
|
|
|
|
hDecoder = NeAACDecOpen();
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
if (!hDecoder) {
|
|
|
|
LOGF("FAAD: Error opening decoder\n");
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2005-10-31 20:56:29 +00:00
|
|
|
}
|
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(hDecoder);
|
2005-11-02 19:43:52 +00:00
|
|
|
conf->outputFormat = FAAD_FMT_24BIT; /* irrelevant, we don't convert */
|
2005-10-31 23:34:14 +00:00
|
|
|
NeAACDecSetConfiguration(hDecoder, conf);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-03-03 02:09:58 +00:00
|
|
|
uint32_t s=0;
|
2005-10-31 23:34:14 +00:00
|
|
|
unsigned char c=0;
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
err = NeAACDecInit2(hDecoder, demux_res.codecdata,demux_res.codecdata_len, &s, &c);
|
|
|
|
if (err) {
|
|
|
|
LOGF("FAAD: Error initialising decoder: %d, type=%d\n", err,hDecoder->object_type);
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
ci->id3->frequency=s;
|
|
|
|
|
|
|
|
i=0;
|
|
|
|
samplesdone=0;
|
|
|
|
/* The main decoding loop */
|
|
|
|
while (i < demux_res.num_sample_byte_sizes) {
|
|
|
|
rb->yield();
|
2006-04-15 02:03:11 +00:00
|
|
|
if (ci->stop_codec || ci->new_track) {
|
2005-10-31 23:34:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deal with any pending seek requests */
|
|
|
|
if (ci->seek_time) {
|
2005-12-02 08:42:48 +00:00
|
|
|
if (alac_seek(&demux_res, &input_stream,
|
2005-11-06 19:18:04 +00:00
|
|
|
((ci->seek_time-1)/10) * (ci->id3->frequency/100),
|
2005-12-02 08:42:48 +00:00
|
|
|
&samplesdone, (int *)&i)) {
|
2005-10-31 23:34:14 +00:00
|
|
|
elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
|
|
|
|
ci->set_elapsed(elapsedtime);
|
|
|
|
}
|
2005-11-02 00:09:42 +00:00
|
|
|
ci->seek_complete();
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup the length (in samples and bytes) of block i */
|
|
|
|
if (!get_sample_info(&demux_res, i, &sample_duration,
|
|
|
|
&sample_byte_size)) {
|
|
|
|
LOGF("AAC: Error in get_sample_info\n");
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Request the required number of bytes from the input buffer */
|
2006-03-24 14:02:27 +00:00
|
|
|
buffer=ci->request_buffer(&n,sample_byte_size);
|
2005-10-31 23:34:14 +00:00
|
|
|
|
|
|
|
/* Decode one block - returned samples will be host-endian */
|
|
|
|
rb->yield();
|
|
|
|
decodedbuffer = NeAACDecDecode(hDecoder, &frameInfo, buffer, n);
|
2005-11-02 19:43:52 +00:00
|
|
|
/* ignore decodedbuffer return value, we access samples in the
|
|
|
|
decoder struct directly */
|
2005-10-31 23:34:14 +00:00
|
|
|
if (frameInfo.error > 0) {
|
|
|
|
LOGF("FAAD: decoding error \"%s\"\n", NeAACDecGetErrorMessage(frameInfo.error));
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the number of decoded samples */
|
|
|
|
samplesdecoded=frameInfo.samples;
|
|
|
|
|
|
|
|
/* Advance codec buffer */
|
|
|
|
ci->advance_buffer(n);
|
|
|
|
|
|
|
|
/* Output the audio */
|
|
|
|
rb->yield();
|
2005-11-02 19:43:52 +00:00
|
|
|
while (!rb->pcmbuf_insert_split(hDecoder->time_out[0],
|
|
|
|
hDecoder->time_out[1],
|
|
|
|
frameInfo.samples*2))
|
2005-10-31 23:34:14 +00:00
|
|
|
rb->yield();
|
|
|
|
|
|
|
|
/* Update the elapsed-time indicator */
|
|
|
|
samplesdone+=sample_duration;
|
|
|
|
elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
|
|
|
|
ci->set_elapsed(elapsedtime);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
/* Keep track of current position - for resuming */
|
|
|
|
ci->set_offset(elapsedtime);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
i++;
|
|
|
|
}
|
2006-04-22 14:40:13 +00:00
|
|
|
err = CODEC_OK;
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
done:
|
2005-10-31 23:34:14 +00:00
|
|
|
LOGF("AAC: Decoded %d samples\n",samplesdone);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
if (ci->request_next_track())
|
|
|
|
goto next_track;
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-01-18 20:22:03 +00:00
|
|
|
exit:
|
|
|
|
return err;
|
2005-10-31 20:56:29 +00:00
|
|
|
}
|