2010-02-28 08:13:30 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Yoshihisa Uchida
|
|
|
|
*
|
|
|
|
* 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 "codecs/libpcm/support_formats.h"
|
|
|
|
|
|
|
|
CODEC_HEADER
|
|
|
|
|
|
|
|
/* vox codec (Dialogic telephony file formats) */
|
|
|
|
|
|
|
|
#define PCM_SAMPLE_SIZE (2048)
|
|
|
|
|
|
|
|
static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
|
|
|
|
|
|
|
|
static struct pcm_format format;
|
|
|
|
static uint32_t bytesdone;
|
|
|
|
|
|
|
|
static uint8_t *read_buffer(size_t *realsize)
|
|
|
|
{
|
|
|
|
uint8_t *buffer = (uint8_t *)ci->request_buffer(realsize, format.chunksize);
|
|
|
|
if (bytesdone + (*realsize) > format.numbytes)
|
|
|
|
*realsize = format.numbytes - bytesdone;
|
|
|
|
bytesdone += *realsize;
|
|
|
|
ci->advance_buffer(*realsize);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(void)
|
2010-02-28 08:13:30 +00:00
|
|
|
{
|
|
|
|
uint32_t decodedsamples;
|
|
|
|
size_t n;
|
|
|
|
int bufcount;
|
|
|
|
int endofstream;
|
|
|
|
uint8_t *voxbuf;
|
2010-03-22 10:02:05 +00:00
|
|
|
off_t firstblockposn = 0; /* position of the first block in file */
|
2010-02-28 08:13:30 +00:00
|
|
|
const struct pcm_codec *codec;
|
2011-04-27 03:08:23 +00:00
|
|
|
intptr_t param;
|
2011-02-20 15:27:10 +00:00
|
|
|
|
2010-02-28 08:13:30 +00:00
|
|
|
if (codec_init()) {
|
|
|
|
DEBUGF("codec_init() error\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-02-28 08:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
codec_set_replaygain(ci->id3);
|
2010-03-22 10:02:05 +00:00
|
|
|
|
|
|
|
/* Need to save offset for later use (cleared indirectly by advance_buffer) */
|
|
|
|
bytesdone = ci->id3->offset;
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->seek_buffer(0);
|
2010-03-22 10:02:05 +00:00
|
|
|
|
2010-02-28 08:13:30 +00:00
|
|
|
ci->memset(&format, 0, sizeof(struct pcm_format));
|
|
|
|
|
|
|
|
/* set format */
|
|
|
|
format.channels = 1;
|
|
|
|
format.bitspersample = 4;
|
|
|
|
format.numbytes = ci->id3->filesize;
|
|
|
|
format.blockalign = 1;
|
|
|
|
|
|
|
|
/* advance to first WAVE chunk */
|
2010-03-22 10:02:05 +00:00
|
|
|
firstblockposn = 0;
|
2010-02-28 08:13:30 +00:00
|
|
|
decodedsamples = 0;
|
2010-03-22 10:02:05 +00:00
|
|
|
ci->advance_buffer(firstblockposn);
|
2010-02-28 08:13:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get codec
|
|
|
|
* supports dialogic oki adpcm only
|
|
|
|
*/
|
|
|
|
codec = get_dialogic_oki_adpcm_codec();
|
|
|
|
if (!codec)
|
|
|
|
{
|
|
|
|
DEBUGF("CODEC_ERROR: dialogic oki adpcm codec does not load.\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-02-28 08:13:30 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (!codec->set_format(&format)) {
|
|
|
|
return CODEC_ERROR;
|
2010-02-28 08:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format.numbytes == 0) {
|
|
|
|
DEBUGF("CODEC_ERROR: data size is 0\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-02-28 08:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check chunksize */
|
|
|
|
if (format.chunksize * 2 > PCM_SAMPLE_SIZE)
|
|
|
|
format.chunksize = PCM_SAMPLE_SIZE / 2;
|
|
|
|
if (format.chunksize == 0)
|
|
|
|
{
|
|
|
|
DEBUGF("CODEC_ERROR: chunksize is 0\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-02-28 08:13:30 +00:00
|
|
|
}
|
|
|
|
|
2013-05-05 04:48:40 +00:00
|
|
|
ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
|
2010-02-28 08:13:30 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
|
|
|
|
|
2010-03-22 10:02:05 +00:00
|
|
|
/* make sure we're at the correct offset */
|
|
|
|
if (bytesdone > (uint32_t) firstblockposn) {
|
|
|
|
/* Round down to previous block */
|
|
|
|
struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
|
|
|
|
PCM_SEEK_POS, &read_buffer);
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (newpos->pos > format.numbytes) {
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
2010-03-22 10:02:05 +00:00
|
|
|
if (ci->seek_buffer(firstblockposn + newpos->pos))
|
|
|
|
{
|
|
|
|
bytesdone = newpos->pos;
|
|
|
|
decodedsamples = newpos->samples;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* already where we need to be */
|
|
|
|
bytesdone = 0;
|
|
|
|
}
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
|
|
|
|
|
2010-02-28 08:13:30 +00:00
|
|
|
/* The main decoder loop */
|
|
|
|
endofstream = 0;
|
|
|
|
|
|
|
|
while (!endofstream) {
|
2011-04-27 03:08:23 +00:00
|
|
|
enum codec_command_action action = ci->get_command(¶m);
|
|
|
|
|
|
|
|
if (action == CODEC_ACTION_HALT)
|
2010-02-28 08:13:30 +00:00
|
|
|
break;
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_SEEK_TIME) {
|
|
|
|
struct pcm_pos *newpos = codec->get_seek_pos(param, PCM_SEEK_TIME,
|
2010-03-22 10:02:05 +00:00
|
|
|
&read_buffer);
|
2010-02-28 08:13:30 +00:00
|
|
|
|
|
|
|
if (newpos->pos > format.numbytes)
|
2011-04-27 03:08:23 +00:00
|
|
|
{
|
|
|
|
ci->set_elapsed(ci->id3->length);
|
|
|
|
ci->seek_complete();
|
2010-02-28 08:13:30 +00:00
|
|
|
break;
|
2011-04-27 03:08:23 +00:00
|
|
|
}
|
|
|
|
|
2010-02-28 08:13:30 +00:00
|
|
|
if (ci->seek_buffer(firstblockposn + newpos->pos))
|
|
|
|
{
|
2010-03-06 08:13:56 +00:00
|
|
|
bytesdone = newpos->pos;
|
|
|
|
decodedsamples = newpos->samples;
|
2010-02-28 08:13:30 +00:00
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
|
2010-02-28 08:13:30 +00:00
|
|
|
ci->seek_complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
voxbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
|
|
|
|
if (n == 0)
|
|
|
|
break; /* End of stream */
|
|
|
|
if (bytesdone + n > format.numbytes) {
|
|
|
|
n = format.numbytes - bytesdone;
|
|
|
|
endofstream = 1;
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (codec->decode(voxbuf, n, samples, &bufcount) == CODEC_ERROR)
|
2010-02-28 08:13:30 +00:00
|
|
|
{
|
|
|
|
DEBUGF("codec error\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-02-28 08:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ci->pcmbuf_insert(samples, NULL, bufcount);
|
|
|
|
ci->advance_buffer(n);
|
|
|
|
bytesdone += n;
|
|
|
|
decodedsamples += bufcount;
|
|
|
|
|
|
|
|
if (bytesdone >= format.numbytes)
|
|
|
|
endofstream = 1;
|
|
|
|
ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2010-02-28 08:13:30 +00:00
|
|
|
}
|