2009-08-14 17:36:57 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2009-11-18 01:00:43 +00:00
|
|
|
* $Id$
|
|
|
|
*
|
2009-08-14 17:36:57 +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 <string.h>
|
|
|
|
|
|
|
|
#include "logf.h"
|
|
|
|
#include "codeclib.h"
|
|
|
|
#include "inttypes.h"
|
|
|
|
#include "libatrac/atrac3.h"
|
|
|
|
|
|
|
|
CODEC_HEADER
|
|
|
|
|
2011-05-16 21:47:13 +00:00
|
|
|
static RMContext rmctx IBSS_ATTR_LARGE_IRAM;
|
|
|
|
static RMPacket pkt IBSS_ATTR_LARGE_IRAM;
|
|
|
|
static ATRAC3Context q IBSS_ATTR;
|
2009-08-14 17:36:57 +00:00
|
|
|
|
|
|
|
static void init_rm(RMContext *rmctx)
|
|
|
|
{
|
2010-02-16 01:47:22 +00:00
|
|
|
/* initialize the RMContext */
|
2009-08-14 17:36:57 +00:00
|
|
|
memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
|
2010-02-16 01:47:22 +00:00
|
|
|
|
|
|
|
/* and atrac3 expects extadata in id3v2buf, so we shall give it that */
|
|
|
|
memcpy(ci->id3->id3v2buf, (char*)rmctx->codec_extradata, rmctx->extradata_size*sizeof(char));
|
2009-08-14 17:36:57 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 12:52:11 +00:00
|
|
|
/* this is the codec entry point */
|
|
|
|
enum codec_status codec_main(enum codec_entry_call_reason reason)
|
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
return CODEC_OK;
|
|
|
|
(void)reason;
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(void)
|
|
|
|
{
|
2009-08-14 17:36:57 +00:00
|
|
|
static size_t buff_size;
|
|
|
|
int datasize, res, consumed, i, time_offset;
|
|
|
|
uint8_t *bit_buffer;
|
|
|
|
uint16_t fs,sps,h;
|
|
|
|
uint32_t packet_count;
|
|
|
|
int scrambling_unit_size, num_units, elapsed = 0;
|
2009-10-03 00:18:42 +00:00
|
|
|
int playback_on = -1;
|
2011-02-20 15:27:10 +00:00
|
|
|
size_t resume_offset;
|
2011-04-27 03:08:23 +00:00
|
|
|
intptr_t param;
|
|
|
|
enum codec_command_action action = CODEC_ACTION_NULL;
|
2009-08-14 17:36:57 +00:00
|
|
|
|
|
|
|
if (codec_init()) {
|
|
|
|
DEBUGF("codec init failed\n");
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
2011-02-20 15:27:10 +00:00
|
|
|
|
|
|
|
resume_offset = ci->id3->offset;
|
2009-08-14 17:36:57 +00:00
|
|
|
|
|
|
|
codec_set_replaygain(ci->id3);
|
|
|
|
ci->memset(&rmctx,0,sizeof(RMContext));
|
|
|
|
ci->memset(&pkt,0,sizeof(RMPacket));
|
|
|
|
ci->memset(&q,0,sizeof(ATRAC3Context));
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->seek_buffer(0);
|
2009-08-14 17:36:57 +00:00
|
|
|
init_rm(&rmctx);
|
|
|
|
|
|
|
|
ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
|
2009-08-30 14:14:22 +00:00
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */
|
2009-08-14 17:36:57 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
|
2009-08-30 14:14:22 +00:00
|
|
|
STEREO_MONO : STEREO_NONINTERLEAVED);
|
2009-08-14 17:36:57 +00:00
|
|
|
|
|
|
|
packet_count = rmctx.nb_packets;
|
|
|
|
rmctx.audio_framesize = rmctx.block_align;
|
|
|
|
rmctx.block_align = rmctx.sub_packet_size;
|
|
|
|
fs = rmctx.audio_framesize;
|
|
|
|
sps= rmctx.block_align;
|
|
|
|
h = rmctx.sub_packet_h;
|
2011-06-24 08:51:22 +00:00
|
|
|
scrambling_unit_size = h * (fs + PACKET_HEADER_SIZE);
|
2009-08-14 17:36:57 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
res = atrac3_decode_init(&q, ci->id3);
|
2009-08-14 17:36:57 +00:00
|
|
|
if(res < 0) {
|
2010-03-12 18:47:13 +00:00
|
|
|
DEBUGF("failed to initialize RM atrac decoder\n");
|
2009-08-14 17:36:57 +00:00
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2009-11-18 00:41:46 +00:00
|
|
|
/* check for a mid-track resume and force a seek time accordingly */
|
|
|
|
if(resume_offset > rmctx.data_offset + DATA_HEADER_SIZE) {
|
|
|
|
resume_offset -= rmctx.data_offset + DATA_HEADER_SIZE;
|
|
|
|
num_units = (int)resume_offset / scrambling_unit_size;
|
|
|
|
/* put number of subpackets to skip in resume_offset */
|
|
|
|
resume_offset /= (sps + PACKET_HEADER_SIZE);
|
2011-04-27 03:08:23 +00:00
|
|
|
param = (int)resume_offset * ((sps * 8 * 1000)/rmctx.bit_rate);
|
|
|
|
action = CODEC_ACTION_SEEK_TIME;
|
2009-11-18 00:41:46 +00:00
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
else {
|
|
|
|
ci->set_elapsed(0);
|
|
|
|
}
|
|
|
|
|
2009-08-14 17:36:57 +00:00
|
|
|
ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
|
|
|
|
|
|
|
|
/* The main decoder loop */
|
|
|
|
seek_start :
|
|
|
|
while((unsigned)elapsed < rmctx.duration)
|
|
|
|
{
|
|
|
|
bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
|
|
|
|
consumed = rm_get_packet(&bit_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
|
|
|
return CODEC_OK;
|
2009-08-14 17:36:57 +00:00
|
|
|
}
|
2009-11-18 00:41:46 +00:00
|
|
|
|
2009-08-14 17:36:57 +00:00
|
|
|
for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
|
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_NULL)
|
|
|
|
action = ci->get_command(¶m);
|
2009-08-14 17:36:57 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_HALT)
|
|
|
|
return CODEC_OK;
|
2009-08-14 17:36:57 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_SEEK_TIME) {
|
2009-08-14 17:36:57 +00:00
|
|
|
/* Do not allow seeking beyond the file's length */
|
2011-04-27 03:08:23 +00:00
|
|
|
if ((unsigned) param > ci->id3->length) {
|
|
|
|
ci->set_elapsed(ci->id3->length);
|
2009-08-14 17:36:57 +00:00
|
|
|
ci->seek_complete();
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2009-08-14 17:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
|
|
|
|
packet_count = rmctx.nb_packets;
|
|
|
|
rmctx.audio_pkt_cnt = 0;
|
|
|
|
rmctx.frame_number = 0;
|
|
|
|
|
|
|
|
/* Seek to the start of the track */
|
2011-04-27 03:08:23 +00:00
|
|
|
if (param == 0) {
|
2009-08-14 17:36:57 +00:00
|
|
|
ci->set_elapsed(0);
|
|
|
|
ci->seek_complete();
|
2011-04-27 03:08:23 +00:00
|
|
|
action = CODEC_ACTION_NULL;
|
2009-08-14 17:36:57 +00:00
|
|
|
goto seek_start;
|
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
num_units = (param/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps));
|
2009-08-14 17:36:57 +00:00
|
|
|
ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units);
|
|
|
|
bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
|
|
|
|
consumed = rm_get_packet(&bit_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
|
|
|
return CODEC_OK;
|
2009-10-03 00:18:42 +00:00
|
|
|
}
|
|
|
|
|
2009-08-14 17:36:57 +00:00
|
|
|
packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units;
|
2011-04-27 03:08:23 +00:00
|
|
|
rmctx.frame_number = (param/(sps*1000*8/rmctx.bit_rate));
|
|
|
|
while(rmctx.audiotimestamp > (unsigned) param) {
|
2009-08-14 17:36:57 +00:00
|
|
|
rmctx.audio_pkt_cnt = 0;
|
|
|
|
ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1));
|
|
|
|
bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size);
|
|
|
|
consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt);
|
|
|
|
packet_count += rmctx.audio_pkt_cnt;
|
|
|
|
num_units--;
|
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
time_offset = param - rmctx.audiotimestamp;
|
2009-08-14 17:36:57 +00:00
|
|
|
i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
|
|
|
|
elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i;
|
|
|
|
ci->set_elapsed(elapsed);
|
|
|
|
ci->seek_complete();
|
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
action = CODEC_ACTION_NULL;
|
|
|
|
|
2009-08-14 17:36:57 +00:00
|
|
|
if(pkt.length)
|
2010-02-16 01:47:22 +00:00
|
|
|
res = atrac3_decode_frame(rmctx.block_align, &q, &datasize, pkt.frames[i], rmctx.block_align);
|
2009-08-14 17:36:57 +00:00
|
|
|
else /* indicates that there are no remaining frames */
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2009-08-14 17:36:57 +00:00
|
|
|
|
|
|
|
if(res != rmctx.block_align) {
|
|
|
|
DEBUGF("codec error\n");
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(datasize)
|
2009-08-30 14:14:22 +00:00
|
|
|
ci->pcmbuf_insert(q.outSamples, q.outSamples + 1024, q.samples_per_frame / rmctx.nb_channels);
|
2009-10-03 00:18:42 +00:00
|
|
|
playback_on = 1;
|
2009-08-14 17:36:57 +00:00
|
|
|
elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i;
|
|
|
|
ci->set_elapsed(elapsed);
|
|
|
|
rmctx.frame_number++;
|
|
|
|
}
|
|
|
|
packet_count -= rmctx.audio_pkt_cnt;
|
|
|
|
rmctx.audio_pkt_cnt = 0;
|
|
|
|
ci->advance_buffer(consumed);
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2009-08-14 17:36:57 +00:00
|
|
|
}
|