2009-07-06 22:40:45 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2009-11-18 01:00:43 +00:00
|
|
|
* $Id$
|
|
|
|
*
|
2009-07-06 22:40:45 +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 "codeclib.h"
|
|
|
|
#include "inttypes.h"
|
|
|
|
#include "libcook/cook.h"
|
|
|
|
|
|
|
|
CODEC_HEADER
|
|
|
|
|
2011-05-22 19:03:58 +00:00
|
|
|
static RMContext rmctx IBSS_ATTR_COOK_LARGE_IRAM;
|
|
|
|
static RMPacket pkt IBSS_ATTR_COOK_LARGE_IRAM;
|
|
|
|
static COOKContext q IBSS_ATTR;
|
|
|
|
static int32_t rm_outbuf[2048] IBSS_ATTR_COOK_LARGE_IRAM MEM_ALIGN_ATTR;
|
2009-07-06 22:40:45 +00:00
|
|
|
|
|
|
|
static void init_rm(RMContext *rmctx)
|
|
|
|
{
|
2009-07-08 17:18:59 +00:00
|
|
|
memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext));
|
2009-07-06 22:40:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-23 01:04:28 +00:00
|
|
|
static int request_packet(int size)
|
|
|
|
{
|
|
|
|
int consumed = 0;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
uint8_t *buffer = ci->request_buffer((size_t *)(&consumed), size);
|
|
|
|
if (!consumed)
|
|
|
|
break;
|
|
|
|
consumed = rm_get_packet(&buffer, &rmctx, &pkt);
|
|
|
|
if (consumed < 0 || consumed == size)
|
|
|
|
break;
|
|
|
|
ci->advance_buffer(size);
|
|
|
|
}
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
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-07-13 10:06:19 +00:00
|
|
|
int datasize, res, consumed, i, time_offset;
|
2009-07-06 22:40:45 +00:00
|
|
|
uint16_t fs,sps,h;
|
|
|
|
uint32_t packet_count;
|
2018-12-23 01:04:28 +00:00
|
|
|
int spn, packet_header_size, scrambling_unit_size, num_units;
|
2011-02-20 15:27:10 +00:00
|
|
|
size_t resume_offset;
|
2013-07-14 11:59:39 +00:00
|
|
|
intptr_t param;
|
2017-12-07 18:21:57 +00:00
|
|
|
long action;
|
2009-07-06 22:40:45 +00:00
|
|
|
|
|
|
|
if (codec_init()) {
|
|
|
|
DEBUGF("codec init failed\n");
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
2011-02-20 15:27:10 +00:00
|
|
|
|
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-06 22:40:45 +00:00
|
|
|
|
|
|
|
codec_set_replaygain(ci->id3);
|
|
|
|
ci->memset(&rmctx,0,sizeof(RMContext));
|
|
|
|
ci->memset(&pkt,0,sizeof(RMPacket));
|
|
|
|
ci->memset(&q,0,sizeof(COOKContext));
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->seek_buffer(0);
|
|
|
|
|
2009-07-06 22:40:45 +00:00
|
|
|
init_rm(&rmctx);
|
|
|
|
|
|
|
|
ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
|
2010-02-21 19:47:05 +00:00
|
|
|
/* cook's sample representation is 21.11
|
|
|
|
* DSP_SET_SAMPLE_DEPTH = 11 (FRACT) + 16 (NATIVE) - 1 (SIGN) = 26 */
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 26);
|
2009-07-06 22:40:45 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
|
2010-02-21 19:47:05 +00:00
|
|
|
STEREO_MONO : STEREO_NONINTERLEAVED);
|
2009-07-06 22:40:45 +00:00
|
|
|
|
2018-12-23 01:04:28 +00:00
|
|
|
packet_header_size = PACKET_HEADER_SIZE +
|
|
|
|
((rmctx.flags & RM_PKT_V1) ? 1 : 0);
|
2009-07-06 22:40:45 +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;
|
2018-12-23 01:04:28 +00:00
|
|
|
scrambling_unit_size = h * (fs + packet_header_size);
|
|
|
|
spn = h * fs / sps;
|
|
|
|
|
2009-07-06 22:40:45 +00:00
|
|
|
res =cook_decode_init(&rmctx, &q);
|
|
|
|
if(res < 0) {
|
|
|
|
DEBUGF("failed to initialize cook decoder\n");
|
|
|
|
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 */
|
2013-07-14 11:59:39 +00:00
|
|
|
if(resume_offset) {
|
|
|
|
resume_offset -= MIN(resume_offset, rmctx.data_offset + DATA_HEADER_SIZE);
|
2018-12-23 01:04:28 +00:00
|
|
|
num_units = (int)resume_offset / scrambling_unit_size;
|
|
|
|
/* put number of packets to skip in resume_offset */
|
|
|
|
resume_offset = num_units * h;
|
|
|
|
param = (int)resume_offset * ((8000LL * fs)/rmctx.bit_rate);
|
2013-07-14 11:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (param) {
|
2011-04-27 03:08:23 +00:00
|
|
|
action = CODEC_ACTION_SEEK_TIME;
|
2009-11-18 00:41:46 +00:00
|
|
|
}
|
2011-08-28 07:45:35 +00:00
|
|
|
else {
|
|
|
|
ci->set_elapsed(0);
|
|
|
|
}
|
2009-07-06 22:40:45 +00:00
|
|
|
|
|
|
|
ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE);
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* The main decoder loop */
|
2009-07-08 20:23:24 +00:00
|
|
|
seek_start :
|
|
|
|
while(packet_count)
|
|
|
|
{
|
2018-12-23 01:04:28 +00:00
|
|
|
consumed = request_packet(scrambling_unit_size);
|
|
|
|
if (!consumed)
|
|
|
|
break;
|
2009-07-08 20:23:24 +00:00
|
|
|
if(consumed < 0) {
|
|
|
|
DEBUGF("rm_get_packet failed\n");
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
2018-12-23 01:04:28 +00:00
|
|
|
|
|
|
|
for (i = 0; i < spn; i++)
|
2011-04-27 03:08:23 +00:00
|
|
|
{
|
|
|
|
if (action == CODEC_ACTION_NULL)
|
|
|
|
action = ci->get_command(¶m);
|
2009-07-12 14:36:06 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_HALT)
|
|
|
|
return CODEC_OK;
|
2009-07-13 10:06:19 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_SEEK_TIME) {
|
2009-07-13 10:06:19 +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-07-13 10:06:19 +00:00
|
|
|
ci->seek_complete();
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2009-07-13 10:06:19 +00:00
|
|
|
}
|
|
|
|
|
2009-07-08 20:23:24 +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;
|
2009-07-13 10:06:19 +00:00
|
|
|
|
|
|
|
/* Seek to the start of the track */
|
2011-04-27 03:08:23 +00:00
|
|
|
if (param == 0) {
|
2009-07-12 14:36:06 +00:00
|
|
|
ci->set_elapsed(0);
|
|
|
|
ci->seek_complete();
|
2011-04-27 03:08:23 +00:00
|
|
|
action = CODEC_ACTION_NULL;
|
2009-07-12 14:36:06 +00:00
|
|
|
goto seek_start;
|
2009-07-13 10:06:19 +00:00
|
|
|
}
|
2018-12-23 01:04:28 +00:00
|
|
|
num_units = (param/(sps*1000*8/rmctx.bit_rate))/spn;
|
|
|
|
ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + scrambling_unit_size * num_units);
|
|
|
|
consumed = request_packet(scrambling_unit_size);
|
|
|
|
if (!consumed) {
|
|
|
|
ci->seek_complete();
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
2009-07-13 10:06:19 +00:00
|
|
|
if(consumed < 0) {
|
2018-12-23 01:04:28 +00:00
|
|
|
DEBUGF("rm_get_packet failed\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->seek_complete();
|
|
|
|
return CODEC_ERROR;
|
2009-07-13 10:06:19 +00:00
|
|
|
}
|
2018-12-23 01:04:28 +00:00
|
|
|
|
|
|
|
packet_count = rmctx.nb_packets - h * num_units;
|
2011-04-27 03:08:23 +00:00
|
|
|
rmctx.frame_number = (param/(sps*1000*8/rmctx.bit_rate));
|
2018-12-23 01:04:28 +00:00
|
|
|
while(rmctx.audiotimestamp > (unsigned)param && num_units-- > 0) {
|
2009-07-12 14:36:06 +00:00
|
|
|
rmctx.audio_pkt_cnt = 0;
|
2018-12-23 01:04:28 +00:00
|
|
|
ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + scrambling_unit_size * num_units);
|
|
|
|
consumed = request_packet(scrambling_unit_size);
|
|
|
|
if (!consumed) {
|
|
|
|
ci->seek_complete();
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
|
|
|
if(consumed < 0) {
|
|
|
|
ci->seek_complete();
|
|
|
|
DEBUGF("rm_get_packet failed\n");
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet_count += h;
|
2009-07-12 14:36:06 +00:00
|
|
|
}
|
2018-12-23 01:04:28 +00:00
|
|
|
|
|
|
|
if (num_units < 0)
|
|
|
|
rmctx.audiotimestamp = 0;
|
2011-04-27 03:08:23 +00:00
|
|
|
time_offset = param - rmctx.audiotimestamp;
|
2009-07-13 10:06:19 +00:00
|
|
|
i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate));
|
2018-12-23 01:04:28 +00:00
|
|
|
ci->set_elapsed(param);
|
2009-07-13 10:06:19 +00:00
|
|
|
ci->seek_complete();
|
2011-04-27 03:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
action = CODEC_ACTION_NULL;
|
|
|
|
|
2018-12-23 01:04:28 +00:00
|
|
|
res = cook_decode_frame(&rmctx,&q, rm_outbuf, &datasize, pkt.frames[i], sps);
|
2009-07-08 20:23:24 +00:00
|
|
|
|
2018-12-23 01:04:28 +00:00
|
|
|
if (res != sps) {
|
2009-07-08 20:23:24 +00:00
|
|
|
DEBUGF("codec error\n");
|
2009-07-06 22:40:45 +00:00
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
2009-07-08 20:23:24 +00:00
|
|
|
|
2018-12-23 01:04:28 +00:00
|
|
|
if(datasize)
|
|
|
|
ci->pcmbuf_insert(rm_outbuf,
|
|
|
|
rm_outbuf+q.samples_per_channel,
|
|
|
|
q.samples_per_channel);
|
2009-07-12 14:36:06 +00:00
|
|
|
ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i);
|
2018-12-23 01:04:28 +00:00
|
|
|
rmctx.frame_number++;
|
2009-07-06 22:40:45 +00:00
|
|
|
}
|
2018-12-23 01:04:28 +00:00
|
|
|
packet_count -= h;
|
2009-07-08 20:23:24 +00:00
|
|
|
rmctx.audio_pkt_cnt = 0;
|
2018-12-23 01:04:28 +00:00
|
|
|
ci->advance_buffer(scrambling_unit_size);
|
2009-07-06 22:40:45 +00:00
|
|
|
}
|
2009-07-08 20:23:24 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2009-07-06 22:40:45 +00:00
|
|
|
}
|