2006-02-01 16:42:02 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Jvo Studer
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "codeclib.h"
|
2006-03-20 20:19:40 +00:00
|
|
|
#include <inttypes.h>
|
2006-02-01 16:42:02 +00:00
|
|
|
|
|
|
|
CODEC_HEADER
|
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
/* Macro that sign extends an unsigned byte */
|
|
|
|
#define SE(x) ((int32_t)((int8_t)(x)))
|
|
|
|
|
2006-02-01 16:42:02 +00:00
|
|
|
/* This codec supports AIFF files with the following formats:
|
2006-03-20 20:19:40 +00:00
|
|
|
* - PCM, 8, 16 and 24 bits, mono or stereo
|
2006-02-01 16:42:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
AIFF_FORMAT_PCM = 0x0001, /* AIFF PCM Format (big endian) */
|
|
|
|
IEEE_FORMAT_FLOAT = 0x0003, /* IEEE Float */
|
|
|
|
AIFF_FORMAT_ALAW = 0x0004, /* AIFC ALaw compressed */
|
|
|
|
AIFF_FORMAT_ULAW = 0x0005 /* AIFC uLaw compressed */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Maximum number of bytes to process in one iteration */
|
|
|
|
/* for 44.1kHz stereo 16bits, this represents 0.023s ~= 1/50s */
|
|
|
|
#define AIF_CHUNK_SIZE (1024*2)
|
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
static int32_t samples[AIF_CHUNK_SIZE] IBSS_ATTR;
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-11-26 18:31:41 +00:00
|
|
|
enum codec_status codec_main(void)
|
2006-02-01 16:42:02 +00:00
|
|
|
{
|
2006-03-20 20:19:40 +00:00
|
|
|
uint32_t numbytes, bytesdone;
|
|
|
|
uint16_t num_channels = 0;
|
|
|
|
uint32_t num_sample_frames = 0;
|
|
|
|
uint16_t sample_size = 0;
|
|
|
|
uint32_t sample_rate = 0;
|
|
|
|
uint32_t i;
|
2007-02-07 00:51:50 +00:00
|
|
|
size_t n;
|
|
|
|
int bufcount;
|
2006-03-20 20:19:40 +00:00
|
|
|
int endofstream;
|
|
|
|
unsigned char *buf;
|
|
|
|
uint8_t *aifbuf;
|
|
|
|
long chunksize;
|
|
|
|
uint32_t offset2snd = 0;
|
|
|
|
uint16_t block_size = 0;
|
|
|
|
uint32_t avgbytespersec = 0;
|
|
|
|
off_t firstblockposn; /* position of the first block in file */
|
|
|
|
|
|
|
|
/* Generic codec initialisation */
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
|
|
|
|
ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
next_track:
|
2006-11-26 18:31:41 +00:00
|
|
|
if (codec_init()) {
|
2006-03-20 20:19:40 +00:00
|
|
|
i = CODEC_ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
while (!*ci->taginfo_ready)
|
|
|
|
ci->yield();
|
2007-02-26 17:15:04 +00:00
|
|
|
|
|
|
|
codec_set_replaygain(ci->id3);
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
/* assume the AIFF header is less than 1024 bytes */
|
2006-03-24 14:02:27 +00:00
|
|
|
buf = ci->request_buffer(&n, 1024);
|
2006-11-25 21:27:46 +00:00
|
|
|
if (n < 54) {
|
2006-03-20 20:19:40 +00:00
|
|
|
i = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-03-20 20:19:40 +00:00
|
|
|
}
|
|
|
|
if ((memcmp(buf, "FORM", 4) != 0) || (memcmp(&buf[8], "AIFF", 4) != 0)) {
|
|
|
|
i = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-02-01 16:42:02 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
buf += 12;
|
|
|
|
n -= 12;
|
|
|
|
numbytes = 0;
|
|
|
|
|
|
|
|
/* read until 'SSND' chunk, which typically is last */
|
|
|
|
while (numbytes == 0 && n >= 8) {
|
|
|
|
/* chunkSize */
|
|
|
|
i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
|
|
|
|
if (memcmp(buf, "COMM", 4) == 0) {
|
2007-04-23 23:11:33 +00:00
|
|
|
if (i < 18) {
|
|
|
|
DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < 18\n",
|
2007-03-17 10:50:58 +00:00
|
|
|
(unsigned long)i);
|
2006-03-20 20:19:40 +00:00
|
|
|
i = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-03-20 20:19:40 +00:00
|
|
|
}
|
|
|
|
/* num_channels */
|
|
|
|
num_channels = ((buf[8]<<8)|buf[9]);
|
|
|
|
/* num_sample_frames */
|
|
|
|
num_sample_frames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)
|
|
|
|
|buf[13]);
|
|
|
|
/* sample_size */
|
|
|
|
sample_size = ((buf[14]<<8)|buf[15]);
|
|
|
|
/* sample_rate (don't use last 4 bytes, only integer fs) */
|
|
|
|
if (buf[16] != 0x40) {
|
2007-03-17 09:54:28 +00:00
|
|
|
DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n");
|
2006-03-20 20:19:40 +00:00
|
|
|
i = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-03-20 20:19:40 +00:00
|
|
|
}
|
|
|
|
sample_rate = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1;
|
|
|
|
sample_rate = sample_rate >> (16 + 14 - buf[17]);
|
|
|
|
/* calc average bytes per second */
|
|
|
|
avgbytespersec = sample_rate*num_channels*sample_size/8;
|
|
|
|
} else if (memcmp(buf, "SSND", 4)==0) {
|
|
|
|
if (sample_size == 0) {
|
|
|
|
DEBUGF("CODEC_ERROR: unsupported chunk order\n");
|
|
|
|
i = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-03-20 20:19:40 +00:00
|
|
|
}
|
|
|
|
/* offset2snd */
|
2006-11-25 21:27:46 +00:00
|
|
|
offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11];
|
2006-03-20 20:19:40 +00:00
|
|
|
/* block_size */
|
2006-11-25 21:27:46 +00:00
|
|
|
block_size = (buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15];
|
2006-03-20 20:19:40 +00:00
|
|
|
if (block_size == 0)
|
|
|
|
block_size = num_channels*sample_size;
|
|
|
|
numbytes = i - 8 - offset2snd;
|
|
|
|
i = 8 + offset2snd; /* advance to the beginning of data */
|
|
|
|
} else {
|
|
|
|
DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n",
|
2007-03-17 10:50:58 +00:00
|
|
|
buf[0], buf[1], buf[2], buf[3], (unsigned long)i);
|
2006-03-20 20:19:40 +00:00
|
|
|
}
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
if (i & 0x01) /* odd chunk sizes must be padded */
|
|
|
|
i++;
|
|
|
|
buf += i + 8;
|
|
|
|
if (n < (i + 8)) {
|
|
|
|
DEBUGF("CODEC_ERROR: AIFF header size > 1024\n");
|
|
|
|
i = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-02-01 16:42:02 +00:00
|
|
|
}
|
2006-03-20 20:19:40 +00:00
|
|
|
n -= i + 8;
|
|
|
|
} /* while 'SSND' */
|
|
|
|
|
|
|
|
if (num_channels == 0) {
|
|
|
|
DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n");
|
|
|
|
i = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-02-01 16:42:02 +00:00
|
|
|
}
|
2006-03-20 20:19:40 +00:00
|
|
|
if (numbytes == 0) {
|
|
|
|
DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n");
|
|
|
|
i = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-03-20 20:19:40 +00:00
|
|
|
}
|
|
|
|
if (sample_size > 24) {
|
|
|
|
DEBUGF("CODEC_ERROR: PCM with more than 24 bits per sample "
|
|
|
|
"is unsupported\n");
|
|
|
|
i = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-03-20 20:19:40 +00:00
|
|
|
}
|
|
|
|
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
if (num_channels == 2) {
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
|
2006-03-20 20:19:40 +00:00
|
|
|
} else if (num_channels == 1) {
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
|
2006-03-20 20:19:40 +00:00
|
|
|
} else {
|
|
|
|
DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n");
|
|
|
|
i = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2006-03-20 20:19:40 +00:00
|
|
|
}
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
firstblockposn = 1024 - n;
|
|
|
|
ci->advance_buffer(firstblockposn);
|
|
|
|
|
|
|
|
/* The main decoder loop */
|
|
|
|
bytesdone = 0;
|
|
|
|
ci->set_elapsed(0);
|
|
|
|
endofstream = 0;
|
|
|
|
/* chunksize is computed so that one chunk is about 1/50s.
|
|
|
|
* this make 4096 for 44.1kHz 16bits stereo.
|
|
|
|
* It also has to be a multiple of blockalign */
|
|
|
|
chunksize = (1 + avgbytespersec/(50*block_size))*block_size;
|
|
|
|
/* check that the output buffer is big enough (convert to samplespersec,
|
|
|
|
then round to the block_size multiple below) */
|
|
|
|
if (((uint64_t)chunksize*ci->id3->frequency*num_channels*2)
|
|
|
|
/(uint64_t)avgbytespersec >= AIF_CHUNK_SIZE) {
|
|
|
|
chunksize = ((uint64_t)AIF_CHUNK_SIZE*avgbytespersec
|
|
|
|
/((uint64_t)ci->id3->frequency*num_channels*2
|
|
|
|
*block_size))*block_size;
|
2006-02-01 16:42:02 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
while (!endofstream) {
|
|
|
|
ci->yield();
|
2006-04-15 02:03:11 +00:00
|
|
|
if (ci->stop_codec || ci->new_track)
|
2006-03-20 20:19:40 +00:00
|
|
|
break;
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
if (ci->seek_time) {
|
|
|
|
uint32_t newpos;
|
|
|
|
|
|
|
|
/* use avgbytespersec to round to the closest blockalign multiple,
|
|
|
|
add firstblockposn. 64-bit casts to avoid overflows. */
|
|
|
|
newpos = (((uint64_t)avgbytespersec*(ci->seek_time - 1))
|
|
|
|
/(1000LL*block_size))*block_size;
|
|
|
|
if (newpos > numbytes)
|
|
|
|
break;
|
|
|
|
if (ci->seek_buffer(firstblockposn + newpos))
|
|
|
|
bytesdone = newpos;
|
|
|
|
ci->seek_complete();
|
2006-02-01 16:42:02 +00:00
|
|
|
}
|
2006-03-24 14:02:27 +00:00
|
|
|
aifbuf = (uint8_t *)ci->request_buffer(&n, chunksize);
|
2006-03-20 20:19:40 +00:00
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
break; /* End of stream */
|
|
|
|
|
|
|
|
if (bytesdone + n > numbytes) {
|
|
|
|
n = numbytes - bytesdone;
|
|
|
|
endofstream = 1;
|
2006-02-01 16:42:02 +00:00
|
|
|
}
|
2006-03-20 20:19:40 +00:00
|
|
|
|
|
|
|
if (sample_size > 24) {
|
|
|
|
for (i = 0; i < n; i += 4) {
|
|
|
|
samples[i/4] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13)
|
|
|
|
|(aifbuf[i + 2]<<5)|(aifbuf[i + 3]>>3);
|
|
|
|
}
|
2007-02-07 00:51:50 +00:00
|
|
|
bufcount = n >> 2;
|
2006-03-20 20:19:40 +00:00
|
|
|
} else if (sample_size > 16) {
|
|
|
|
for (i = 0; i < n; i += 3) {
|
|
|
|
samples[i/3] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13)
|
|
|
|
|(aifbuf[i + 2]<<5);
|
|
|
|
}
|
2007-02-07 00:51:50 +00:00
|
|
|
bufcount = n/3;
|
2006-03-20 20:19:40 +00:00
|
|
|
} else if (sample_size > 8) {
|
|
|
|
for (i = 0; i < n; i += 2)
|
|
|
|
samples[i/2] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13);
|
2007-02-07 00:51:50 +00:00
|
|
|
bufcount = n >> 1;
|
2006-03-20 20:19:40 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
samples[i] = SE(aifbuf[i]) << 21;
|
2007-02-07 00:51:50 +00:00
|
|
|
bufcount = n;
|
2006-02-01 16:42:02 +00:00
|
|
|
}
|
|
|
|
|
2007-02-07 00:51:50 +00:00
|
|
|
if (num_channels == 2)
|
|
|
|
bufcount >>= 1;
|
|
|
|
|
|
|
|
ci->pcmbuf_insert(samples, NULL, bufcount);
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
ci->advance_buffer(n);
|
|
|
|
bytesdone += n;
|
|
|
|
if (bytesdone >= numbytes)
|
|
|
|
endofstream = 1;
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-03-20 20:19:40 +00:00
|
|
|
ci->set_elapsed(bytesdone*1000LL/avgbytespersec);
|
|
|
|
}
|
2006-04-22 14:40:13 +00:00
|
|
|
i = CODEC_OK;
|
2006-02-01 16:42:02 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
done:
|
2006-03-20 20:19:40 +00:00
|
|
|
if (ci->request_next_track())
|
|
|
|
goto next_track;
|
2006-02-01 16:42:02 +00:00
|
|
|
|
|
|
|
exit:
|
2006-03-20 20:19:40 +00:00
|
|
|
return i;
|
2006-02-01 16:42:02 +00:00
|
|
|
}
|
2006-03-20 20:19:40 +00:00
|
|
|
|