2005-06-10 18:08:08 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
2010-01-27 17:25:10 +00:00
|
|
|
* Copyright (C) 2009 Yoshihisa Uchida
|
2005-06-10 18:08:08 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2005-06-10 18:08:08 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2005-10-13 11:32:52 +00:00
|
|
|
#include "codeclib.h"
|
2010-01-27 17:25:10 +00:00
|
|
|
#include "codecs/libpcm/support_formats.h"
|
2005-06-10 18:08:08 +00:00
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2010-01-27 17:25:10 +00:00
|
|
|
/* WAVE (RIFF) codec:
|
2005-09-22 19:36:25 +00:00
|
|
|
*
|
|
|
|
* For a good documentation on WAVE files, see:
|
|
|
|
* http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/WAVE.html
|
|
|
|
* and
|
|
|
|
* http://www.sonicspot.com/guide/wavefiles.html
|
|
|
|
*
|
|
|
|
* For sample WAV files, see:
|
|
|
|
* http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/Samples.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-02-24 11:30:16 +00:00
|
|
|
#define PCM_SAMPLE_SIZE (4096*2)
|
|
|
|
|
|
|
|
static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
|
2010-01-27 17:25:10 +00:00
|
|
|
|
|
|
|
/* This codec support WAVE files with the following formats: */
|
2005-09-22 19:36:25 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
WAVE_FORMAT_UNKNOWN = 0x0000, /* Microsoft Unknown Wave Format */
|
|
|
|
WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */
|
2010-02-20 02:04:56 +00:00
|
|
|
WAVE_FORMAT_ADPCM = 0x0002, /* Microsoft ADPCM Format */
|
2010-01-27 18:10:08 +00:00
|
|
|
WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* IEEE Float */
|
2005-09-22 19:36:25 +00:00
|
|
|
WAVE_FORMAT_ALAW = 0x0006, /* Microsoft ALAW */
|
|
|
|
WAVE_FORMAT_MULAW = 0x0007, /* Microsoft MULAW */
|
|
|
|
WAVE_FORMAT_DVI_ADPCM = 0x0011, /* Intel's DVI ADPCM */
|
2010-02-20 02:04:56 +00:00
|
|
|
WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic OKI ADPCM */
|
|
|
|
WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha ADPCM */
|
|
|
|
WAVE_FORMAT_XBOX_ADPCM = 0x0069, /* XBOX ADPCM */
|
2005-09-22 19:36:25 +00:00
|
|
|
IBM_FORMAT_MULAW = 0x0101, /* same as WAVE_FORMAT_MULAW */
|
|
|
|
IBM_FORMAT_ALAW = 0x0102, /* same as WAVE_FORMAT_ALAW */
|
2010-02-20 02:04:56 +00:00
|
|
|
WAVE_FORMAT_SWF_ADPCM = 0x5346, /* Adobe SWF ADPCM */
|
2005-09-22 19:36:25 +00:00
|
|
|
WAVE_FORMAT_EXTENSIBLE = 0xFFFE
|
|
|
|
};
|
|
|
|
|
2011-06-01 10:28:26 +00:00
|
|
|
static const struct pcm_entry wave_codecs[] = {
|
2010-01-27 17:25:10 +00:00
|
|
|
{ WAVE_FORMAT_UNKNOWN, 0 },
|
|
|
|
{ WAVE_FORMAT_PCM, get_linear_pcm_codec },
|
2010-02-20 02:04:56 +00:00
|
|
|
{ WAVE_FORMAT_ADPCM, get_ms_adpcm_codec },
|
2010-01-27 18:10:08 +00:00
|
|
|
{ WAVE_FORMAT_IEEE_FLOAT, get_ieee_float_codec },
|
2010-01-27 17:25:10 +00:00
|
|
|
{ WAVE_FORMAT_ALAW, get_itut_g711_alaw_codec },
|
|
|
|
{ WAVE_FORMAT_MULAW, get_itut_g711_mulaw_codec },
|
|
|
|
{ WAVE_FORMAT_DVI_ADPCM, get_dvi_adpcm_codec },
|
2010-02-20 02:04:56 +00:00
|
|
|
{ WAVE_FORMAT_DIALOGIC_OKI_ADPCM, get_dialogic_oki_adpcm_codec },
|
|
|
|
{ WAVE_FORMAT_YAMAHA_ADPCM, get_yamaha_adpcm_codec },
|
|
|
|
{ WAVE_FORMAT_XBOX_ADPCM, get_dvi_adpcm_codec },
|
2010-01-27 17:25:10 +00:00
|
|
|
{ IBM_FORMAT_MULAW, get_itut_g711_mulaw_codec },
|
|
|
|
{ IBM_FORMAT_ALAW, get_itut_g711_alaw_codec },
|
2010-02-20 02:04:56 +00:00
|
|
|
{ WAVE_FORMAT_SWF_ADPCM, get_swf_adpcm_codec },
|
2005-09-22 19:36:25 +00:00
|
|
|
};
|
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
#define NUM_FORMATS 13
|
2010-01-27 17:25:10 +00:00
|
|
|
|
|
|
|
static const struct pcm_codec *get_wave_codec(uint32_t formattag)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_FORMATS; i++)
|
|
|
|
{
|
|
|
|
if (wave_codecs[i].format_tag == formattag)
|
|
|
|
{
|
|
|
|
if (wave_codecs[i].get_codec)
|
|
|
|
return wave_codecs[i].get_codec();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-22 19:36:25 +00:00
|
|
|
|
2017-11-07 01:06:08 +00:00
|
|
|
static struct libpcm_pcm_format format;
|
2010-02-20 02:04:56 +00:00
|
|
|
static uint32_t bytesdone;
|
|
|
|
|
|
|
|
static bool set_msadpcm_coeffs(const uint8_t *buf)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int num;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
buf += 4; /* skip 'fmt ' */
|
|
|
|
size = buf[0] | (buf[1] << 8) | (buf[1] << 16) | (buf[1] << 24);
|
|
|
|
if (size < 50)
|
|
|
|
{
|
|
|
|
DEBUGF("CODEC_ERROR: microsoft adpcm 'fmt ' chunk size=%lu < 50\n",
|
|
|
|
(unsigned long)size);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get nNumCoef */
|
|
|
|
buf += 24;
|
|
|
|
num = buf[0] | (buf[1] << 8);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In many case, nNumCoef is 7.
|
|
|
|
* Depending upon the encoder, as for this value there is a possibility of
|
|
|
|
* increasing more.
|
|
|
|
* If you found the file where this value exceeds 7, please report.
|
|
|
|
*/
|
|
|
|
if (num != MSADPCM_NUM_COEFF)
|
|
|
|
{
|
|
|
|
DEBUGF("CODEC_ERROR: microsoft adpcm nNumCoef=%d != 7\n", num);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get aCoeffs */
|
|
|
|
buf += 2;
|
|
|
|
for (i = 0; i < MSADPCM_NUM_COEFF; i++)
|
|
|
|
{
|
|
|
|
format.coeffs[i][0] = buf[0] | (SE(buf[1]) << 8);
|
|
|
|
format.coeffs[i][1] = buf[2] | (SE(buf[3]) << 8);
|
|
|
|
buf += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2005-09-22 19:36:25 +00:00
|
|
|
|
2005-06-22 19:41:30 +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_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(void)
|
2005-06-10 18:08:08 +00:00
|
|
|
{
|
2010-02-20 02:04:56 +00:00
|
|
|
uint32_t decodedsamples;
|
2007-02-07 00:51:50 +00:00
|
|
|
size_t n;
|
|
|
|
int bufcount;
|
2006-03-20 20:32:19 +00:00
|
|
|
int endofstream;
|
|
|
|
unsigned char *buf;
|
|
|
|
uint8_t *wavbuf;
|
|
|
|
off_t firstblockposn; /* position of the first block in file */
|
2010-01-27 17:25:10 +00:00
|
|
|
const struct pcm_codec *codec;
|
2010-03-07 07:15:21 +00:00
|
|
|
uint32_t size;
|
2011-04-27 03:08:23 +00:00
|
|
|
intptr_t param;
|
2011-02-20 15:27:10 +00:00
|
|
|
|
2006-11-26 18:31:41 +00:00
|
|
|
if (codec_init()) {
|
2010-01-27 17:25:10 +00:00
|
|
|
DEBUGF("codec_init() error\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2006-03-20 20:32:19 +00:00
|
|
|
}
|
2005-06-10 18:08:08 +00:00
|
|
|
|
2007-02-26 17:15:04 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
2005-06-26 19:41:29 +00:00
|
|
|
|
2013-07-14 11:59:39 +00:00
|
|
|
/* Need to save resume for later use (cleared indirectly by advance_buffer) */
|
|
|
|
param = ci->id3->elapsed;
|
2006-08-23 08:19:29 +00:00
|
|
|
bytesdone = ci->id3->offset;
|
|
|
|
|
2006-06-27 22:27:21 +00:00
|
|
|
/* get RIFF chunk header */
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->seek_buffer(0);
|
2006-06-27 22:27:21 +00:00
|
|
|
buf = ci->request_buffer(&n, 12);
|
|
|
|
if (n < 12) {
|
2010-01-27 17:25:10 +00:00
|
|
|
DEBUGF("request_buffer error\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2006-03-20 20:32:19 +00:00
|
|
|
}
|
|
|
|
if ((memcmp(buf, "RIFF", 4) != 0) || (memcmp(&buf[8], "WAVE", 4) != 0)) {
|
2010-02-20 02:04:56 +00:00
|
|
|
DEBUGF("CODEC_ERROR: missing riff header\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2005-06-10 18:08:08 +00:00
|
|
|
}
|
|
|
|
|
2006-06-27 22:27:21 +00:00
|
|
|
/* advance to first WAVE chunk */
|
|
|
|
ci->advance_buffer(12);
|
|
|
|
|
|
|
|
firstblockposn = 12;
|
2017-11-07 01:06:08 +00:00
|
|
|
ci->memset(&format, 0, sizeof(struct libpcm_pcm_format));
|
2010-01-27 17:25:10 +00:00
|
|
|
format.is_signed = true;
|
|
|
|
format.is_little_endian = true;
|
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
decodedsamples = 0;
|
2010-01-27 17:25:10 +00:00
|
|
|
codec = 0;
|
2006-06-27 22:27:21 +00:00
|
|
|
|
|
|
|
/* iterate over WAVE chunks until the 'data' chunk, which should be after the 'fmt ' chunk */
|
|
|
|
while (true) {
|
|
|
|
/* get WAVE chunk header */
|
|
|
|
buf = ci->request_buffer(&n, 1024);
|
|
|
|
if (n < 8) {
|
2010-01-27 17:25:10 +00:00
|
|
|
DEBUGF("data chunk request_buffer error\n");
|
2006-06-27 22:27:21 +00:00
|
|
|
/* no more chunks, 'data' chunk must not have been found */
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2006-06-27 22:27:21 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 20:32:19 +00:00
|
|
|
/* chunkSize */
|
2010-03-07 07:15:21 +00:00
|
|
|
size = (buf[4]|(buf[5]<<8)|(buf[6]<<16)|(buf[7]<<24));
|
2006-03-20 20:32:19 +00:00
|
|
|
if (memcmp(buf, "fmt ", 4) == 0) {
|
2010-03-07 07:15:21 +00:00
|
|
|
if (size < 16) {
|
2007-03-17 10:50:58 +00:00
|
|
|
DEBUGF("CODEC_ERROR: 'fmt ' chunk size=%lu < 16\n",
|
2010-03-07 07:15:21 +00:00
|
|
|
(unsigned long)size);
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2006-03-20 20:32:19 +00:00
|
|
|
}
|
|
|
|
/* wFormatTag */
|
2010-01-27 17:25:10 +00:00
|
|
|
format.formattag=buf[8]|(buf[9]<<8);
|
2006-03-20 20:32:19 +00:00
|
|
|
/* wChannels */
|
2010-01-27 17:25:10 +00:00
|
|
|
format.channels=buf[10]|(buf[11]<<8);
|
2006-03-20 20:32:19 +00:00
|
|
|
/* skipping dwSamplesPerSec */
|
2010-03-07 07:15:21 +00:00
|
|
|
/* skipping dwAvgBytesPerSec */
|
2006-03-20 20:32:19 +00:00
|
|
|
/* wBlockAlign */
|
2010-01-27 17:25:10 +00:00
|
|
|
format.blockalign=buf[20]|(buf[21]<<8);
|
2006-03-20 20:32:19 +00:00
|
|
|
/* wBitsPerSample */
|
2010-01-27 17:25:10 +00:00
|
|
|
format.bitspersample=buf[22]|(buf[23]<<8);
|
|
|
|
if (format.formattag != WAVE_FORMAT_PCM) {
|
2010-03-07 07:15:21 +00:00
|
|
|
if (size < 18) {
|
2006-03-20 20:32:19 +00:00
|
|
|
/* this is not a fatal error with some formats,
|
|
|
|
* we'll see later if we can't decode it */
|
|
|
|
DEBUGF("CODEC_WARNING: non-PCM WAVE (formattag=0x%x) "
|
2010-01-27 18:05:28 +00:00
|
|
|
"doesn't have ext. fmt descr (chunksize=%d<18).\n",
|
2010-03-07 07:15:21 +00:00
|
|
|
(unsigned int)format.formattag, (int)size);
|
2006-03-20 20:32:19 +00:00
|
|
|
}
|
2010-01-27 17:25:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (format.formattag != WAVE_FORMAT_EXTENSIBLE)
|
|
|
|
format.samplesperblock = buf[26]|(buf[27]<<8);
|
2010-03-07 07:15:21 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
format.size = buf[24]|(buf[25]<<8);
|
2010-01-27 17:25:10 +00:00
|
|
|
if (format.size < 22) {
|
|
|
|
DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is "
|
|
|
|
"missing extension\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
|
|
|
/* wValidBitsPerSample */
|
|
|
|
format.bitspersample = buf[26]|(buf[27]<<8);
|
|
|
|
/* skipping dwChannelMask (4bytes) */
|
|
|
|
/* SubFormat (only get the first two bytes) */
|
|
|
|
format.formattag = buf[32]|(buf[33]<<8);
|
2006-03-20 20:32:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-27 17:25:10 +00:00
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
/* msadpcm specific */
|
|
|
|
if (format.formattag == WAVE_FORMAT_ADPCM)
|
|
|
|
{
|
|
|
|
if (!set_msadpcm_coeffs(buf))
|
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-02-20 02:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-27 17:25:10 +00:00
|
|
|
/* get codec */
|
|
|
|
codec = get_wave_codec(format.formattag);
|
|
|
|
if (!codec)
|
|
|
|
{
|
2010-02-20 02:04:56 +00:00
|
|
|
DEBUGF("CODEC_ERROR: unsupported wave format 0x%x\n",
|
2010-01-27 18:05:28 +00:00
|
|
|
(unsigned int) format.formattag);
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* riff 8bit linear pcm is unsigned */
|
|
|
|
if (format.formattag == WAVE_FORMAT_PCM && format.bitspersample == 8)
|
|
|
|
format.is_signed = false;
|
|
|
|
|
|
|
|
/* set format, parse codec specific tag, check format, and calculate chunk size */
|
2010-02-20 02:04:56 +00:00
|
|
|
if (!codec->set_format(&format))
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
2006-03-20 20:32:19 +00:00
|
|
|
} else if (memcmp(buf, "data", 4) == 0) {
|
2010-03-07 07:15:21 +00:00
|
|
|
format.numbytes = size;
|
2006-06-27 22:27:21 +00:00
|
|
|
/* advance to start of data */
|
|
|
|
ci->advance_buffer(8);
|
|
|
|
firstblockposn += 8;
|
|
|
|
break;
|
2006-03-20 20:32:19 +00:00
|
|
|
} else if (memcmp(buf, "fact", 4) == 0) {
|
|
|
|
/* dwSampleLength */
|
2010-03-07 07:15:21 +00:00
|
|
|
if (size >= 4)
|
2010-01-27 17:25:10 +00:00
|
|
|
format.totalsamples =
|
|
|
|
(buf[8]|(buf[9]<<8)|(buf[10]<<16)|(buf[11]<<24));
|
2006-03-20 20:32:19 +00:00
|
|
|
} else {
|
|
|
|
DEBUGF("unknown WAVE chunk: '%c%c%c%c', size=%lu\n",
|
2010-03-07 07:15:21 +00:00
|
|
|
buf[0], buf[1], buf[2], buf[3], (unsigned long)size);
|
2006-03-20 20:32:19 +00:00
|
|
|
}
|
2006-06-27 22:27:21 +00:00
|
|
|
|
2006-03-20 20:32:19 +00:00
|
|
|
/* go to next chunk (even chunk sizes must be padded) */
|
2010-03-07 07:15:21 +00:00
|
|
|
size += 8 + (size & 0x01);
|
|
|
|
|
|
|
|
ci->advance_buffer(size);
|
|
|
|
firstblockposn += size;
|
2006-03-20 20:32:19 +00:00
|
|
|
}
|
2005-10-10 16:02:52 +00:00
|
|
|
|
2010-01-27 17:25:10 +00:00
|
|
|
if (!codec)
|
|
|
|
{
|
|
|
|
DEBUGF("CODEC_ERROR: 'fmt ' chunk not found\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2006-03-20 20:32:19 +00:00
|
|
|
}
|
2010-01-27 17:25:10 +00:00
|
|
|
|
|
|
|
/* common format check */
|
|
|
|
if (format.channels == 0) {
|
|
|
|
DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2006-03-20 20:32:19 +00:00
|
|
|
}
|
2010-02-20 02:04:56 +00:00
|
|
|
if (format.samplesperblock == 0) {
|
|
|
|
DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-02-20 02:04:56 +00:00
|
|
|
}
|
|
|
|
if (format.blockalign == 0)
|
|
|
|
{
|
|
|
|
DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-02-20 02:04:56 +00:00
|
|
|
}
|
2010-01-27 17:25:10 +00:00
|
|
|
if (format.numbytes == 0) {
|
|
|
|
DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2005-09-22 19:36:25 +00:00
|
|
|
}
|
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
/* check chunksize */
|
|
|
|
if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
|
2010-02-24 11:30:16 +00:00
|
|
|
> PCM_SAMPLE_SIZE)
|
|
|
|
format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
|
2010-02-20 02:04:56 +00:00
|
|
|
if (format.chunksize == 0)
|
|
|
|
{
|
|
|
|
DEBUGF("CODEC_ERROR: chunksize is 0\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2010-02-20 02:04:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-05 04:48:40 +00:00
|
|
|
ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
|
2010-01-27 17:25:10 +00:00
|
|
|
if (format.channels == 2) {
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
|
2010-01-27 17:25:10 +00:00
|
|
|
} else if (format.channels == 1) {
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
|
2006-03-20 20:32:19 +00:00
|
|
|
} else {
|
|
|
|
DEBUGF("CODEC_ERROR: more than 2 channels\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2006-03-20 20:32:19 +00:00
|
|
|
}
|
2005-06-10 18:08:08 +00:00
|
|
|
|
2006-06-27 22:27:21 +00:00
|
|
|
/* make sure we're at the correct offset */
|
2013-07-14 11:59:39 +00:00
|
|
|
if (bytesdone > (uint32_t) firstblockposn || param) {
|
|
|
|
uint32_t seek_val;
|
|
|
|
int seek_mode;
|
|
|
|
|
|
|
|
if (bytesdone) {
|
|
|
|
seek_val = bytesdone - MIN((uint32_t) firstblockposn, bytesdone);
|
|
|
|
seek_mode = PCM_SEEK_POS;
|
|
|
|
} else {
|
|
|
|
seek_val = param;
|
|
|
|
seek_mode = PCM_SEEK_TIME;
|
|
|
|
}
|
|
|
|
|
2006-06-04 15:04:03 +00:00
|
|
|
/* Round down to previous block */
|
2013-07-14 11:59:39 +00:00
|
|
|
struct pcm_pos *newpos = codec->get_seek_pos(seek_val, seek_mode,
|
|
|
|
&read_buffer);
|
2006-06-04 15:04:03 +00:00
|
|
|
|
2010-03-22 10:02:05 +00:00
|
|
|
if (newpos->pos > format.numbytes)
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2010-03-22 10:02:05 +00:00
|
|
|
if (ci->seek_buffer(firstblockposn + newpos->pos))
|
|
|
|
{
|
|
|
|
bytesdone = newpos->pos;
|
|
|
|
decodedsamples = newpos->samples;
|
|
|
|
}
|
2006-06-04 15:04:03 +00:00
|
|
|
} else {
|
2006-06-27 22:27:21 +00:00
|
|
|
/* already where we need to be */
|
2006-06-04 15:04:03 +00:00
|
|
|
bytesdone = 0;
|
|
|
|
}
|
2006-03-20 20:32:19 +00:00
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
|
|
|
|
|
2006-03-20 20:32:19 +00:00
|
|
|
/* The main decoder loop */
|
|
|
|
endofstream = 0;
|
2005-06-10 18:08:08 +00:00
|
|
|
|
2006-03-20 20:32:19 +00:00
|
|
|
while (!endofstream) {
|
2017-12-07 18:21:57 +00:00
|
|
|
long action = ci->get_command(¶m);
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
if (action == CODEC_ACTION_HALT)
|
2006-03-20 20:32:19 +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-20 02:04:56 +00:00
|
|
|
if (newpos->pos > format.numbytes)
|
2011-04-27 03:08:23 +00:00
|
|
|
{
|
|
|
|
ci->set_elapsed(ci->id3->length);
|
|
|
|
ci->seek_complete();
|
2006-03-20 20:32:19 +00:00
|
|
|
break;
|
2011-04-27 03:08:23 +00:00
|
|
|
}
|
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
if (ci->seek_buffer(firstblockposn + newpos->pos))
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
2010-03-06 08:13:56 +00:00
|
|
|
bytesdone = newpos->pos;
|
|
|
|
decodedsamples = newpos->samples;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
|
2006-03-20 20:32:19 +00:00
|
|
|
ci->seek_complete();
|
2005-09-22 19:36:25 +00:00
|
|
|
}
|
2006-03-20 20:32:19 +00:00
|
|
|
|
2010-01-27 17:25:10 +00:00
|
|
|
wavbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
|
2006-03-20 20:32:19 +00:00
|
|
|
if (n == 0)
|
|
|
|
break; /* End of stream */
|
2010-01-27 17:25:10 +00:00
|
|
|
if (bytesdone + n > format.numbytes) {
|
|
|
|
n = format.numbytes - bytesdone;
|
2006-03-20 20:32:19 +00:00
|
|
|
endofstream = 1;
|
2005-09-22 19:36:25 +00:00
|
|
|
}
|
2006-03-20 20:32:19 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (codec->decode(wavbuf, n, samples, &bufcount) == CODEC_ERROR)
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
|
|
|
DEBUGF("codec error\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2005-09-22 19:36:25 +00:00
|
|
|
}
|
2005-06-10 18:08:08 +00:00
|
|
|
|
2007-02-07 00:51:50 +00:00
|
|
|
ci->pcmbuf_insert(samples, NULL, bufcount);
|
2006-03-20 20:32:19 +00:00
|
|
|
ci->advance_buffer(n);
|
|
|
|
bytesdone += n;
|
2010-02-20 02:04:56 +00:00
|
|
|
decodedsamples += bufcount;
|
2010-01-27 17:25:10 +00:00
|
|
|
|
|
|
|
if (bytesdone >= format.numbytes)
|
2006-03-20 20:32:19 +00:00
|
|
|
endofstream = 1;
|
2010-02-20 02:04:56 +00:00
|
|
|
ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
|
2005-09-22 19:36:25 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2005-06-10 18:08:08 +00:00
|
|
|
}
|