2009-07-25 15:47:13 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2009-11-18 01:00:43 +00:00
|
|
|
* $Id$
|
2009-07-25 15:47:13 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Mohamed Tarek
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "codeclib.h"
|
|
|
|
#include "librm/rm.h"
|
|
|
|
#include "libfaad/common.h"
|
|
|
|
#include "libfaad/structs.h"
|
|
|
|
#include "libfaad/decoder.h"
|
2011-06-02 14:59:15 +00:00
|
|
|
/* rockbox: not used
|
2009-07-25 15:47:13 +00:00
|
|
|
#include "libfaad/output.h"
|
2011-06-02 14:59:15 +00:00
|
|
|
*/
|
2009-07-25 15:47:13 +00:00
|
|
|
|
|
|
|
CODEC_HEADER
|
2009-08-04 06:10:03 +00:00
|
|
|
|
2009-07-25 15:47:13 +00:00
|
|
|
static void init_rm(RMContext *rmctx)
|
|
|
|
{
|
|
|
|
memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
|
|
|
|
}
|
|
|
|
|
2010-07-25 22:24:53 +00:00
|
|
|
static RMContext rmctx;
|
|
|
|
static RMPacket pkt;
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2009-07-25 15:47:13 +00:00
|
|
|
/* this is the codec entry point */
|
2011-04-27 03:08:23 +00:00
|
|
|
enum codec_status codec_main(enum codec_entry_call_reason reason)
|
|
|
|
{
|
|
|
|
if (reason == CODEC_LOAD) {
|
|
|
|
/* Generic codec initialisation */
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(void)
|
2009-07-25 15:47:13 +00:00
|
|
|
{
|
|
|
|
static NeAACDecFrameInfo frame_info;
|
|
|
|
NeAACDecHandle decoder;
|
2010-07-06 19:20:55 +00:00
|
|
|
size_t n;
|
2009-07-25 15:47:13 +00:00
|
|
|
unsigned int i;
|
|
|
|
unsigned char* buffer;
|
|
|
|
int err, consumed, pkt_offset, skipped = 0;
|
|
|
|
uint32_t s = 0; /* sample rate */
|
|
|
|
unsigned char c = 0; /* channels */
|
2009-10-03 00:18:42 +00:00
|
|
|
int playback_on = -1;
|
2011-02-20 15:27:10 +00:00
|
|
|
size_t resume_offset;
|
2017-12-07 18:21:57 +00:00
|
|
|
long action;
|
2011-04-27 03:08:23 +00:00
|
|
|
intptr_t param;
|
2009-07-25 15:47:13 +00:00
|
|
|
|
|
|
|
if (codec_init()) {
|
|
|
|
DEBUGF("FAAD: Codec init error\n");
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-07-14 11:59:39 +00:00
|
|
|
action = CODEC_ACTION_NULL;
|
|
|
|
param = ci->id3->elapsed;
|
2011-02-20 15:27:10 +00:00
|
|
|
resume_offset = ci->id3->offset;
|
2009-07-25 15:47:13 +00:00
|
|
|
|
|
|
|
ci->memset(&rmctx,0,sizeof(RMContext));
|
|
|
|
ci->memset(&pkt,0,sizeof(RMPacket));
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
ci->seek_buffer(0);
|
|
|
|
|
2009-07-25 15:47:13 +00:00
|
|
|
init_rm(&rmctx);
|
2013-05-05 04:48:40 +00:00
|
|
|
ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
|
2009-07-25 15:47:13 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
|
|
|
|
|
|
|
/* initialise the sound converter */
|
|
|
|
decoder = NeAACDecOpen();
|
|
|
|
|
|
|
|
if (!decoder) {
|
|
|
|
DEBUGF("FAAD: Decode open error\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-25 15:47:13 +00:00
|
|
|
NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
|
2010-07-06 19:20:55 +00:00
|
|
|
conf->outputFormat = FAAD_FMT_16BIT; /* irrelevant, we don't convert */
|
2009-07-25 15:47:13 +00:00
|
|
|
NeAACDecSetConfiguration(decoder, conf);
|
|
|
|
|
|
|
|
decoder->config.defObjectType = rmctx.codec_extradata[0];
|
|
|
|
decoder->config.defSampleRate = rmctx.sample_rate;
|
|
|
|
err = NeAACDecInit(decoder, NULL, 0, &s, &c);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
DEBUGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-07-06 19:20:55 +00:00
|
|
|
}
|
|
|
|
|
2009-11-18 00:41:46 +00:00
|
|
|
/* check for a mid-track resume and force a seek time accordingly */
|
2013-07-14 11:59:39 +00:00
|
|
|
if (resume_offset) {
|
|
|
|
resume_offset -= MIN(resume_offset, rmctx.data_offset + DATA_HEADER_SIZE);
|
2009-11-18 00:41:46 +00:00
|
|
|
/* put number of subpackets to skip in resume_offset */
|
2018-12-23 01:04:28 +00:00
|
|
|
resume_offset /= (rmctx.block_align + PACKET_HEADER_SIZE +
|
|
|
|
((rmctx.flags & RM_PKT_V1) ? 1 : 0));
|
2011-04-27 03:08:23 +00:00
|
|
|
param = (int)resume_offset * ((rmctx.block_align * 8 * 1000)/rmctx.bit_rate);
|
2013-07-14 11:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (param > 0) {
|
2011-04-27 03:08:23 +00:00
|
|
|
action = CODEC_ACTION_SEEK_TIME;
|
2009-11-18 00:41:46 +00:00
|
|
|
}
|
2013-07-14 11:59:39 +00:00
|
|
|
else {
|
|
|
|
/* Seek to the first packet */
|
|
|
|
ci->set_elapsed(0);
|
|
|
|
ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
|
|
|
|
}
|
2009-07-25 15:47:13 +00:00
|
|
|
|
|
|
|
/* The main decoding loop */
|
|
|
|
while (1) {
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_NULL)
|
|
|
|
action = ci->get_command(¶m);
|
|
|
|
|
|
|
|
if (action == CODEC_ACTION_HALT)
|
2009-07-25 15:47:13 +00:00
|
|
|
break;
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_SEEK_TIME) {
|
2009-07-25 15:47:13 +00:00
|
|
|
/* Do not allow seeking beyond the file's length */
|
2013-07-14 11:59:39 +00:00
|
|
|
if ((unsigned long)param > ci->id3->length) {
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->set_elapsed(ci->id3->length);
|
2009-07-25 15:47:13 +00:00
|
|
|
ci->seek_complete();
|
2011-04-27 03:08:23 +00:00
|
|
|
break;
|
2009-07-25 15:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
|
|
|
|
|
|
|
|
/* Seek to the start of the track */
|
2011-04-27 03:08:23 +00:00
|
|
|
if (param == 0) {
|
2009-07-25 15:47:13 +00:00
|
|
|
ci->set_elapsed(0);
|
|
|
|
ci->seek_complete();
|
2011-04-27 03:08:23 +00:00
|
|
|
action = CODEC_ACTION_NULL;
|
|
|
|
continue;
|
2009-07-25 15:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
skipped = 0;
|
|
|
|
while(1) {
|
|
|
|
buffer = ci->request_buffer(&n,rmctx.audio_framesize + 1000);
|
|
|
|
pkt_offset = skipped - pkt.length;
|
|
|
|
consumed = rm_get_packet(&buffer, &rmctx, &pkt);
|
2009-10-03 00:18:42 +00:00
|
|
|
if(consumed < 0 && playback_on != 0) {
|
|
|
|
if(playback_on == -1) {
|
|
|
|
/* Error only if packet-parsing failed and playback hadn't started */
|
|
|
|
DEBUGF("rm_get_packet failed\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->seek_complete();
|
2009-10-03 00:18:42 +00:00
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
else {
|
|
|
|
ci->seek_complete();
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
2009-07-25 15:47:13 +00:00
|
|
|
}
|
|
|
|
skipped += pkt.length;
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
if(pkt.timestamp > (unsigned)param)
|
|
|
|
break;
|
|
|
|
|
2009-07-25 15:47:13 +00:00
|
|
|
ci->advance_buffer(pkt.length);
|
|
|
|
}
|
2013-07-14 11:59:39 +00:00
|
|
|
|
2009-07-25 15:47:13 +00:00
|
|
|
ci->seek_buffer(pkt_offset + rmctx.data_offset + DATA_HEADER_SIZE);
|
|
|
|
buffer = ci->request_buffer(&n,rmctx.audio_framesize + 1000);
|
2011-04-24 19:13:08 +00:00
|
|
|
NeAACDecPostSeekReset(decoder, decoder->frame);
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->set_elapsed(pkt.timestamp);
|
2009-07-25 15:47:13 +00:00
|
|
|
ci->seek_complete();
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
action = CODEC_ACTION_NULL;
|
|
|
|
|
2009-07-25 15:47:13 +00:00
|
|
|
/* Request the required number of bytes from the input buffer */
|
|
|
|
buffer=ci->request_buffer(&n,rmctx.audio_framesize + 1000);
|
|
|
|
consumed = rm_get_packet(&buffer, &rmctx, &pkt);
|
|
|
|
|
2009-10-03 00:18:42 +00:00
|
|
|
if(consumed < 0 && playback_on != 0) {
|
|
|
|
if(playback_on == -1) {
|
|
|
|
/* Error only if packet-parsing failed and playback hadn't started */
|
|
|
|
DEBUGF("rm_get_packet failed\n");
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
|
|
|
else
|
2011-04-27 03:08:23 +00:00
|
|
|
break;
|
2009-10-03 00:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
playback_on = 1;
|
2009-07-25 15:47:13 +00:00
|
|
|
if (pkt.timestamp >= ci->id3->length)
|
2011-04-27 03:08:23 +00:00
|
|
|
break;
|
|
|
|
|
2009-07-25 15:47:13 +00:00
|
|
|
/* Decode one block - returned samples will be host-endian */
|
|
|
|
for(i = 0; i < rmctx.sub_packet_cnt; i++) {
|
2011-05-01 13:48:28 +00:00
|
|
|
NeAACDecDecode(decoder, &frame_info, buffer, rmctx.sub_packet_lengths[i]);
|
2009-07-25 15:47:13 +00:00
|
|
|
buffer += rmctx.sub_packet_lengths[i];
|
|
|
|
if (frame_info.error > 0) {
|
|
|
|
DEBUGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2009-07-25 15:47:13 +00:00
|
|
|
}
|
2010-07-06 19:20:55 +00:00
|
|
|
ci->pcmbuf_insert(decoder->time_out[0],
|
|
|
|
decoder->time_out[1],
|
|
|
|
decoder->frameLength);
|
2009-07-25 15:47:13 +00:00
|
|
|
ci->set_elapsed(pkt.timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
ci->advance_buffer(pkt.length);
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2009-07-25 15:47:13 +00:00
|
|
|
}
|