From c9183bf15e56bbc795f9fb08027ee6285b2ebed4 Mon Sep 17 00:00:00 2001 From: Michael Giacomelli Date: Wed, 27 Jan 2010 17:25:10 +0000 Subject: Commit FS#10422 by Yoshihisa Uchida. Seperates WAV and AIFF parsing from PCM decoding by introducing libpcm, a library for decoding linear and non-uniform PCM independently of the container format. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24346 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/aiff.c | 213 +++++++----- apps/codecs/codecs.make | 3 + apps/codecs/libpcm/SOURCES | 3 + apps/codecs/libpcm/dvi_adpcm.c | 309 +++++++++++++++++ apps/codecs/libpcm/itut_g711.c | 203 +++++++++++ apps/codecs/libpcm/libpcm.make | 25 ++ apps/codecs/libpcm/linear_pcm.c | 163 +++++++++ apps/codecs/libpcm/pcm_common.h | 120 +++++++ apps/codecs/libpcm/support_formats.h | 37 ++ apps/codecs/wav.c | 648 +++++++---------------------------- apps/metadata/aiff.c | 2 +- 11 files changed, 1114 insertions(+), 612 deletions(-) create mode 100644 apps/codecs/libpcm/SOURCES create mode 100755 apps/codecs/libpcm/dvi_adpcm.c create mode 100644 apps/codecs/libpcm/itut_g711.c create mode 100644 apps/codecs/libpcm/libpcm.make create mode 100644 apps/codecs/libpcm/linear_pcm.c create mode 100644 apps/codecs/libpcm/pcm_common.h create mode 100644 apps/codecs/libpcm/support_formats.h diff --git a/apps/codecs/aiff.c b/apps/codecs/aiff.c index 9a675415e2..1b8c2da920 100644 --- a/apps/codecs/aiff.c +++ b/apps/codecs/aiff.c @@ -8,6 +8,7 @@ * $Id$ * * Copyright (c) 2005 Jvo Studer + * Copyright (c) 2009 Yoshihisa Uchida * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -21,48 +22,62 @@ #include "codeclib.h" #include +#include "codecs/libpcm/support_formats.h" CODEC_HEADER -/* Macro that sign extends an unsigned byte */ -#define SE(x) ((int32_t)((int8_t)(x))) +#define FOURCC(c1, c2, c3, c4) \ +((((uint32_t)c1)<<24)|(((uint32_t)c2)<<16)|(((uint32_t)c3)<<8)|((uint32_t)c4)) -/* This codec supports AIFF files with the following formats: - * - PCM, 8, 16 and 24 bits, mono or stereo - */ +/* This codec supports the following AIFC compressionType formats */ +enum { + AIFC_FORMAT_PCM = FOURCC('N', 'O', 'N', 'E'), /* AIFC PCM Format (big endian) */ + AIFC_FORMAT_ALAW = FOURCC('a', 'l', 'a', 'w'), /* AIFC ALaw compressed */ + AIFC_FORMAT_MULAW = FOURCC('u', 'l', 'a', 'w'), /* AIFC uLaw compressed */ +}; -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 */ +static const struct pcm_entry pcm_codecs[] = { + { AIFC_FORMAT_PCM, get_linear_pcm_codec }, + { AIFC_FORMAT_ALAW, get_itut_g711_alaw_codec }, + { AIFC_FORMAT_MULAW, get_itut_g711_mulaw_codec }, }; -/* 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) +#define NUM_FORMATS 3 -static int32_t samples[AIF_CHUNK_SIZE] IBSS_ATTR; +static int32_t samples[PCM_CHUNK_SIZE] IBSS_ATTR; + +static const struct pcm_codec *get_codec(uint32_t formattag) +{ + int i; + + for (i = 0; i < NUM_FORMATS; i++) + { + if (pcm_codecs[i].format_tag == formattag) + { + if (pcm_codecs[i].get_codec) + return pcm_codecs[i].get_codec(); + return 0; + } + } + return 0; +} enum codec_status codec_main(void) { - uint32_t numbytes, bytesdone; - uint16_t num_channels = 0; + int status = CODEC_OK; + struct pcm_format format; + uint32_t bytesdone, decodedbytes; uint32_t num_sample_frames = 0; - uint16_t sample_size = 0; - uint32_t sample_rate = 0; - uint32_t i; + uint32_t i = CODEC_OK; size_t n; int bufcount; 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 */ + bool is_aifc = false; + const struct pcm_codec *codec; /* Generic codec initialisation */ ci->configure(DSP_SET_SAMPLE_DEPTH, 28); @@ -84,45 +99,80 @@ next_track: i = CODEC_ERROR; goto done; } - if ((memcmp(buf, "FORM", 4) != 0) || (memcmp(&buf[8], "AIFF", 4) != 0)) { + + if (memcmp(buf, "FORM", 4) != 0) + { + DEBUGF("CODEC_ERROR: does not aiff format %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]); + i = CODEC_ERROR; + goto done; + } + if (memcmp(&buf[8], "AIFF", 4) == 0) + is_aifc = false; + else if (memcmp(&buf[8], "AIFC", 4) == 0) + is_aifc = true; + else + { + DEBUGF("CODEC_ERROR: does not aiff format %c%c%c%c\n", buf[8], buf[9], buf[10], buf[11]); i = CODEC_ERROR; goto done; } buf += 12; n -= 12; - numbytes = 0; + + ci->memset(&format, 0, sizeof(struct pcm_format)); + format.is_signed = true; + format.is_little_endian = false; + + decodedbytes = 0; + codec = 0; /* read until 'SSND' chunk, which typically is last */ - while (numbytes == 0 && n >= 8) { + while (format.numbytes == 0 && n >= 8) + { /* chunkSize */ i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]); if (memcmp(buf, "COMM", 4) == 0) { - if (i < 18) { - DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < 18\n", - (unsigned long)i); + if ((!is_aifc && i < 18) || (is_aifc && i < 22)) + { + DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu < %d\n", + (unsigned long)i, (is_aifc)?22:18); i = CODEC_ERROR; goto done; } /* num_channels */ - num_channels = ((buf[8]<<8)|buf[9]); + format.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]); + format.bitspersample = ((buf[14]<<8)|buf[15]); /* sample_rate (don't use last 4 bytes, only integer fs) */ if (buf[16] != 0x40) { DEBUGF("CODEC_ERROR: weird sampling rate (no @)\n"); i = CODEC_ERROR; goto done; } - sample_rate = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1; - sample_rate = sample_rate >> (16 + 14 - buf[17]); + format.samplespersec = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1; + format.samplespersec >>= (16 + 14 - buf[17]); + /* compressionType (AIFC only) */ + if (is_aifc) + { + format.formattag = (buf[26]<<24)|(buf[27]<<16)|(buf[28]<<8)|buf[29]; + + /* + * aiff's sample_size is uncompressed sound data size. + * But format.bitspersample is compressed sound data size. + */ + if (format.formattag == AIFC_FORMAT_ALAW || format.formattag == AIFC_FORMAT_MULAW) + format.bitspersample = 8; + } + else + format.formattag = AIFC_FORMAT_PCM; /* calc average bytes per second */ - avgbytespersec = sample_rate*num_channels*sample_size/8; + format.avgbytespersec = format.samplespersec*format.channels*format.bitspersample/8; } else if (memcmp(buf, "SSND", 4)==0) { - if (sample_size == 0) { + if (format.bitspersample == 0) { DEBUGF("CODEC_ERROR: unsupported chunk order\n"); i = CODEC_ERROR; goto done; @@ -130,11 +180,14 @@ next_track: /* offset2snd */ offset2snd = (buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11]; /* block_size */ - block_size = (buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15]; - if (block_size == 0) - block_size = num_channels*sample_size; - numbytes = i - 8 - offset2snd; + format.blockalign = (buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15]; + if (format.blockalign == 0) + format.blockalign = format.channels*format.bitspersample; + format.numbytes = i - 8 - offset2snd; i = 8 + offset2snd; /* advance to the beginning of data */ + } else if (is_aifc && (memcmp(buf, "FVER", 4)==0)) { + /* Format Version Chunk (AIFC only chunk) */ + /* skip this chunk */ } else { DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n", buf[0], buf[1], buf[2], buf[3], (unsigned long)i); @@ -151,28 +204,36 @@ next_track: n -= i + 8; } /* while 'SSND' */ - if (num_channels == 0) { + if (format.channels == 0) { DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n"); i = CODEC_ERROR; goto done; } - if (numbytes == 0) { + if (format.numbytes == 0) { DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n"); i = CODEC_ERROR; goto done; } - if (sample_size > 24) { - DEBUGF("CODEC_ERROR: PCM with more than 24 bits per sample " - "is unsupported\n"); + + codec = get_codec(format.formattag); + if (codec == 0) + { + DEBUGF("CODEC_ERROR: AIFC does not support compressionType: 0x%x\n", format.formattag); + i = CODEC_ERROR; + goto done; + } + + if (!codec->set_format(&format, 0)) + { i = CODEC_ERROR; goto done; } ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); - if (num_channels == 2) { + if (format.channels == 2) { ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); - } else if (num_channels == 1) { + } else if (format.channels == 1) { ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); } else { DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n"); @@ -187,18 +248,6 @@ next_track: 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; - } while (!endofstream) { ci->yield(); @@ -206,61 +255,39 @@ next_track: break; 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) + uint32_t newpos = codec->get_seek_pos(ci->seek_time); + if (newpos > format.numbytes) break; if (ci->seek_buffer(firstblockposn + newpos)) bytesdone = newpos; ci->seek_complete(); } - aifbuf = (uint8_t *)ci->request_buffer(&n, chunksize); + aifbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize); if (n == 0) break; /* End of stream */ - if (bytesdone + n > numbytes) { - n = numbytes - bytesdone; + if (bytesdone + n > format.numbytes) { + n = format.numbytes - bytesdone; endofstream = 1; } - 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); - } - bufcount = n >> 2; - } 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); - } - bufcount = n/3; - } else if (sample_size > 8) { - for (i = 0; i < n; i += 2) - samples[i/2] = (SE(aifbuf[i])<<21)|(aifbuf[i + 1]<<13); - bufcount = n >> 1; - } else { - for (i = 0; i < n; i++) - samples[i] = SE(aifbuf[i]) << 21; - bufcount = n; + status = codec->decode(aifbuf, n, samples, &bufcount); + if (status == CODEC_ERROR) + { + DEBUGF("codec error\n"); + goto done; } - if (num_channels == 2) - bufcount >>= 1; - ci->pcmbuf_insert(samples, NULL, bufcount); ci->advance_buffer(n); bytesdone += n; - if (bytesdone >= numbytes) + decodedbytes += bufcount; + if (bytesdone >= format.numbytes) endofstream = 1; - ci->set_elapsed(bytesdone*1000LL/avgbytespersec); + ci->set_elapsed(decodedbytes*1000LL/ci->id3->frequency); } i = CODEC_OK; diff --git a/apps/codecs/codecs.make b/apps/codecs/codecs.make index 391975d73c..9e1f909462 100644 --- a/apps/codecs/codecs.make +++ b/apps/codecs/codecs.make @@ -39,6 +39,7 @@ include $(APPSDIR)/codecs/libwma/libwma.make include $(APPSDIR)/codecs/libcook/libcook.make include $(APPSDIR)/codecs/librm/librm.make include $(APPSDIR)/codecs/libatrac/libatrac.make +include $(APPSDIR)/codecs/libpcm/libpcm.make # compile flags for codecs CODECFLAGS = $(filter-out -fno-strict-aliasing,$(CFLAGS)) -fstrict-aliasing \ @@ -85,6 +86,8 @@ $(CODECDIR)/cook.codec : $(CODECDIR)/libcook.a $(CODECDIR)/librm.a $(CODECDIR)/raac.codec : $(CODECDIR)/libfaad.a $(CODECDIR)/librm.a $(CODECDIR)/a52_rm.codec : $(CODECDIR)/liba52.a $(CODECDIR)/librm.a $(CODECDIR)/atrac3_rm.codec : $(CODECDIR)/libatrac.a $(CODECDIR)/librm.a +$(CODECDIR)/aiff.codec : $(CODECDIR)/libpcm.a +$(CODECDIR)/wav.codec : $(CODECDIR)/libpcm.a $(CODECS): $(CODECLIB) # this must be last in codec dependency list diff --git a/apps/codecs/libpcm/SOURCES b/apps/codecs/libpcm/SOURCES new file mode 100644 index 0000000000..e921584f7c --- /dev/null +++ b/apps/codecs/libpcm/SOURCES @@ -0,0 +1,3 @@ +linear_pcm.c +itut_g711.c +dvi_adpcm.c diff --git a/apps/codecs/libpcm/dvi_adpcm.c b/apps/codecs/libpcm/dvi_adpcm.c new file mode 100755 index 0000000000..9118906a93 --- /dev/null +++ b/apps/codecs/libpcm/dvi_adpcm.c @@ -0,0 +1,309 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2005 Dave Chapman + * Copyright (C) 2009 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 "pcm_common.h" + +/* + * Intel DVI ADPCM + */ + +static const uint16_t dvi_adpcm_steptab[89] ICONST_ATTR = { + 7, 8, 9, 10, 11, 12, 13, 14, + 16, 17, 19, 21, 23, 25, 28, 31, + 34, 37, 41, 45, 50, 55, 60, 66, + 73, 80, 88, 97, 107, 118, 130, 143, + 157, 173, 190, 209, 230, 253, 279, 307, + 337, 371, 408, 449, 494, 544, 598, 658, + 724, 796, 876, 963, 1060, 1166, 1282, 1411, + 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, + 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, + 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, + 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, + 32767 }; + +static const int dvi_adpcm_indextab4[8] ICONST_ATTR = { + -1, -1, -1, -1, 2, 4, 6, 8 }; + +static const int dvi_adpcm_indextab3[4] ICONST_ATTR = { -1, -1, 1, 2 }; + +static struct pcm_format *fmt; + +static bool set_format(struct pcm_format *format, const unsigned char *fmtpos) +{ + fmt = format; + + (void)fmtpos; + + if (fmt->bitspersample != 4 && fmt->bitspersample != 3) + { + DEBUGF("CODEC_ERROR: dvi_adpcm must have 3 or 4 bitspersample\n"); + return false; + } + + if (fmt->size < 2) { + DEBUGF("CODEC_ERROR: dvi_adpcm is missing SamplesPerBlock value\n"); + return false; + } + + /* 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 */ + fmt->chunksize = (1 + fmt->avgbytespersec / (50*fmt->blockalign))*fmt->blockalign; + + /* check that the output buffer is big enough (convert to samplespersec, + then round to the blockalign multiple below) */ + if ((((uint64_t)fmt->chunksize * ci->id3->frequency * fmt->channels * fmt->bitspersample)>>3) + /(uint64_t)fmt->avgbytespersec >= PCM_CHUNK_SIZE) + fmt->chunksize = ((uint64_t)PCM_CHUNK_SIZE * fmt->avgbytespersec + /((uint64_t)ci->id3->frequency * fmt->channels * 2 + * fmt->blockalign)) * fmt->blockalign; + + return true; +} + +static uint32_t get_seek_pos(long 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)fmt->avgbytespersec*(seek_time - 1)) + / (1000LL*fmt->blockalign))*fmt->blockalign; + return newpos; +} + +static int decode_dvi_adpcm(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, size_t *outbufcount) +{ + size_t nsamples = 0; + int sample[2]; + int samplecode[32][2]; + int i; + int stepindex[2]; + int c; + int diff; + int step; + int codem; + int code; + + if (fmt->bitspersample != 4 && fmt->bitspersample != 3) { + DEBUGF("decode_dvi_adpcm: wrong bitspersample\n"); + return CODEC_ERROR; + } + + /* decode block header */ + for (c = 0; c < fmt->channels && inbufsize >= 4; c++) { + /* decode + push first sample */ + sample[c] = (short)(inbuf[0]|(inbuf[1]<<8));/* need cast for sign-extend */ + outbuf[c] = sample[c] << 13; + nsamples++; + stepindex[c] = inbuf[2]; + /* check for step table index overflow */ + if (stepindex[c] > 88) { + DEBUGF("decode_dvi_adpcm: stepindex[%d]=%d>88\n",c,stepindex[c]); + return CODEC_ERROR; + } + + inbuf += 4; + inbufsize -= 4; + } + if (fmt->bitspersample == 4) { + while (inbufsize >= (size_t)(fmt->channels*4) && + (nsamples + (fmt->channels*8) <= *outbufcount)) + { + for (c = 0; c < fmt->channels; c++) + { + samplecode[0][c] = inbuf[0]&0xf; + samplecode[1][c] = inbuf[0]>>4; + samplecode[2][c] = inbuf[1]&0xf; + samplecode[3][c] = inbuf[1]>>4; + samplecode[4][c] = inbuf[2]&0xf; + samplecode[5][c] = inbuf[2]>>4; + samplecode[6][c] = inbuf[3]&0xf; + samplecode[7][c] = inbuf[3]>>4; + inbuf += 4; + inbufsize -= 4; + } + for (i = 0; i < 8; i++) + { + for (c = 0; c < fmt->channels; c++) + { + step = dvi_adpcm_steptab[stepindex[c]]; + codem = samplecode[i][c]; + code = codem & 0x07; + + /* adjust the step table index */ + stepindex[c] += dvi_adpcm_indextab4[code]; + /* check for step table index overflow and underflow */ + if (stepindex[c] > 88) + stepindex[c] = 88; + else if (stepindex[c] < 0) + stepindex[c] = 0; + /* calculate the difference */ +#ifdef STRICT_IMA + diff = 0; + if (code & 4) + diff += step; + step = step >> 1; + if (code & 2) + diff += step; + step = step >> 1; + if (code & 1) + diff += step; + step = step >> 1; + diff += step; +#else + diff = ((code + code + 1) * step) >> 3; /* faster */ +#endif + /* check the sign bit */ + /* check for overflow and underflow errors */ + if (code != codem) + { + sample[c] -= diff; + if (sample[c] < -32768) + sample[c] = -32768; + } + else + { + sample[c] += diff; + if (sample[c] > 32767) + sample[c] = 32767; + } + /* output the new sample */ + outbuf[nsamples] = sample[c] << 13; + nsamples++; + } + } + } + } else { /* bitspersample == 3 */ + while (inbufsize >= (uint32_t)(fmt->channels*12) && + (nsamples + 32*fmt->channels) <= *outbufcount) { + for (c = 0; c < fmt->channels; c++) { + uint16_t bitstream = 0; + int bitsread = 0; + for (i = 0; i < 32 && inbufsize > 0; i++) { + if (bitsread < 3) { + /* read 8 more bits */ + bitstream |= inbuf[0]<>3; + bitsread -= 3; + } + if (bitsread != 0) { + /* 32*3 = 3 words, so we should end with bitsread==0 */ + DEBUGF("decode_dvi_adpcm: error in implementation\n"); + return CODEC_ERROR; + } + } + + for (i = 0; i < 32; i++) { + for (c = 0; c < fmt->channels; c++) { + step = dvi_adpcm_steptab[stepindex[c]]; + codem = samplecode[i][c]; + code = codem & 0x03; + + /* adjust the step table index */ + stepindex[c] += dvi_adpcm_indextab3[code]; + /* check for step table index overflow and underflow */ + if (stepindex[c] > 88) + stepindex[c] = 88; + else if (stepindex[c] < 0) + stepindex[c] = 0; + /* calculate the difference */ +#ifdef STRICT_IMA + diff = 0; + if (code & 2) + diff += step; + step = step >> 1; + if (code & 1) + diff += step; + step = step >> 1; + diff += step; +#else + diff = ((code + code + 1) * step) >> 3; /* faster */ +#endif + /* check the sign bit */ + /* check for overflow and underflow errors */ + if (code != codem) { + sample[c] -= diff; + if (sample[c] < -32768) + sample[c] = -32768; + } + else { + sample[c] += diff; + if (sample[c] > 32767) + sample[c] = 32767; + } + /* output the new sample */ + outbuf[nsamples] = sample[c] << 13; + nsamples++; + } + } + } + } + + if (nsamples > *outbufcount) { + DEBUGF("decode_dvi_adpcm: output buffer overflow!\n"); + return CODEC_ERROR; + } + *outbufcount = nsamples; + if (inbufsize != 0) { + DEBUGF("decode_dvi_adpcm: n=%d unprocessed bytes\n", (int)inbufsize); + } + return CODEC_OK; +} + +static int decode(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufsize) +{ + unsigned int i; + unsigned int nblocks = fmt->chunksize / fmt->blockalign; + + (void)inbufsize; + + for (i = 0; i < nblocks; i++) + { + size_t decodedsize = fmt->samplesperblock * fmt->channels; + if (decode_dvi_adpcm(inbuf + i * fmt->blockalign, fmt->blockalign, + outbuf + i * fmt->samplesperblock * fmt->channels, + &decodedsize) != CODEC_OK) { + return CODEC_ERROR; + } + } + *outbufsize = nblocks * fmt->samplesperblock; + return CODEC_OK; +} + +static const struct pcm_codec codec = { + set_format, + get_seek_pos, + decode, + }; + +const struct pcm_codec *get_dvi_adpcm_codec(void) +{ + return &codec; +} diff --git a/apps/codecs/libpcm/itut_g711.c b/apps/codecs/libpcm/itut_g711.c new file mode 100644 index 0000000000..1f235943ad --- /dev/null +++ b/apps/codecs/libpcm/itut_g711.c @@ -0,0 +1,203 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2005 Dave Chapman + * Copyright (C) 2009 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 "pcm_common.h" + +/* + * ITU-T G.711 A-law mu-law + */ + +static const int16_t alaw2linear16[256] ICONST_ATTR = { + -5504, -5248, -6016, -5760, -4480, -4224, -4992, + -4736, -7552, -7296, -8064, -7808, -6528, -6272, + -7040, -6784, -2752, -2624, -3008, -2880, -2240, + -2112, -2496, -2368, -3776, -3648, -4032, -3904, + -3264, -3136, -3520, -3392, -22016, -20992, -24064, + -23040, -17920, -16896, -19968, -18944, -30208, -29184, + -32256, -31232, -26112, -25088, -28160, -27136, -11008, + -10496, -12032, -11520, -8960, -8448, -9984, -9472, + -15104, -14592, -16128, -15616, -13056, -12544, -14080, + -13568, -344, -328, -376, -360, -280, -264, + -312, -296, -472, -456, -504, -488, -408, + -392, -440, -424, -88, -72, -120, -104, + -24, -8, -56, -40, -216, -200, -248, + -232, -152, -136, -184, -168, -1376, -1312, + -1504, -1440, -1120, -1056, -1248, -1184, -1888, + -1824, -2016, -1952, -1632, -1568, -1760, -1696, + -688, -656, -752, -720, -560, -528, -624, + -592, -944, -912, -1008, -976, -816, -784, + -880, -848, 5504, 5248, 6016, 5760, 4480, + 4224, 4992, 4736, 7552, 7296, 8064, 7808, + 6528, 6272, 7040, 6784, 2752, 2624, 3008, + 2880, 2240, 2112, 2496, 2368, 3776, 3648, + 4032, 3904, 3264, 3136, 3520, 3392, 22016, + 20992, 24064, 23040, 17920, 16896, 19968, 18944, + 30208, 29184, 32256, 31232, 26112, 25088, 28160, + 27136, 11008, 10496, 12032, 11520, 8960, 8448, + 9984, 9472, 15104, 14592, 16128, 15616, 13056, + 12544, 14080, 13568, 344, 328, 376, 360, + 280, 264, 312, 296, 472, 456, 504, + 488, 408, 392, 440, 424, 88, 72, + 120, 104, 24, 8, 56, 40, 216, + 200, 248, 232, 152, 136, 184, 168, + 1376, 1312, 1504, 1440, 1120, 1056, 1248, + 1184, 1888, 1824, 2016, 1952, 1632, 1568, + 1760, 1696, 688, 656, 752, 720, 560, + 528, 624, 592, 944, 912, 1008, 976, + 816, 784, 880, 848 +}; + +static const int16_t ulaw2linear16[256] ICONST_ATTR = { + -32124, -31100, -30076, -29052, -28028, -27004, -25980, + -24956, -23932, -22908, -21884, -20860, -19836, -18812, + -17788, -16764, -15996, -15484, -14972, -14460, -13948, + -13436, -12924, -12412, -11900, -11388, -10876, -10364, + -9852, -9340, -8828, -8316, -7932, -7676, -7420, + -7164, -6908, -6652, -6396, -6140, -5884, -5628, + -5372, -5116, -4860, -4604, -4348, -4092, -3900, + -3772, -3644, -3516, -3388, -3260, -3132, -3004, + -2876, -2748, -2620, -2492, -2364, -2236, -2108, + -1980, -1884, -1820, -1756, -1692, -1628, -1564, + -1500, -1436, -1372, -1308, -1244, -1180, -1116, + -1052, -988, -924, -876, -844, -812, -780, + -748, -716, -684, -652, -620, -588, -556, + -524, -492, -460, -428, -396, -372, -356, + -340, -324, -308, -292, -276, -260, -244, + -228, -212, -196, -180, -164, -148, -132, + -120, -112, -104, -96, -88, -80, -72, + -64, -56, -48, -40, -32, -24, -16, + -8, 0, 32124, 31100, 30076, 29052, 28028, + 27004, 25980, 24956, 23932, 22908, 21884, 20860, + 19836, 18812, 17788, 16764, 15996, 15484, 14972, + 14460, 13948, 13436, 12924, 12412, 11900, 11388, + 10876, 10364, 9852, 9340, 8828, 8316, 7932, + 7676, 7420, 7164, 6908, 6652, 6396, 6140, + 5884, 5628, 5372, 5116, 4860, 4604, 4348, + 4092, 3900, 3772, 3644, 3516, 3388, 3260, + 3132, 3004, 2876, 2748, 2620, 2492, 2364, + 2236, 2108, 1980, 1884, 1820, 1756, 1692, + 1628, 1564, 1500, 1436, 1372, 1308, 1244, + 1180, 1116, 1052, 988, 924, 876, 844, + 812, 780, 748, 716, 684, 652, 620, + 588, 556, 524, 492, 460, 428, 396, + 372, 356, 340, 324, 308, 292, 276, + 260, 244, 228, 212, 196, 180, 164, + 148, 132, 120, 112, 104, 96, 88, + 80, 72, 64, 56, 48, 40, 32, + 24, 16, 8, 0 +}; + +static struct pcm_format *fmt; + +static bool set_format(struct pcm_format *format, const unsigned char *fmtpos) +{ + fmt = format; + + (void)fmtpos; + + if (fmt->bitspersample != 8) + { + DEBUGF("CODEC_ERROR: alaw and mulaw must have 8 bitspersample\n"); + return false; + } + + if (fmt->totalsamples == 0) + { + fmt->bytespersample = fmt->channels; + fmt->totalsamples = fmt->numbytes/fmt->bytespersample; + } + + /* 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 */ + fmt->chunksize = (1 + fmt->avgbytespersec / (50*fmt->blockalign))*fmt->blockalign; + + /* check that the output buffer is big enough (convert to samplespersec, + then round to the blockalign multiple below) */ + if ((((uint64_t)fmt->chunksize * ci->id3->frequency * fmt->channels * fmt->bitspersample)>>3) + /(uint64_t)fmt->avgbytespersec >= PCM_CHUNK_SIZE) + fmt->chunksize = ((uint64_t)PCM_CHUNK_SIZE * fmt->avgbytespersec + /((uint64_t)ci->id3->frequency * fmt->channels * 2 + * fmt->blockalign)) * fmt->blockalign; + + return true; +} + +static uint32_t get_seek_pos(long 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)fmt->avgbytespersec*(seek_time - 1)) + / (1000LL*fmt->blockalign))*fmt->blockalign; + return newpos; +} + +static int decode_alaw(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufsize) +{ + uint32_t i; + + for (i = 0; i < inbufsize; i++) + outbuf[i] = alaw2linear16[inbuf[i]] << 13; + + *outbufsize = (fmt->channels == 2) ? (inbufsize >> 1) : inbufsize; + + return CODEC_OK; +} + +static int decode_mulaw(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufsize) +{ + uint32_t i; + + for (i = 0; i < inbufsize; i++) + outbuf[i] = ulaw2linear16[inbuf[i]] << 13; + + *outbufsize = (fmt->channels == 2) ? (inbufsize >> 1) : inbufsize; + + return CODEC_OK; +} + +static const struct pcm_codec alaw_codec = { + set_format, + get_seek_pos, + decode_alaw, + }; + +static const struct pcm_codec mulaw_codec = { + set_format, + get_seek_pos, + decode_mulaw, + }; + +const struct pcm_codec *get_itut_g711_alaw_codec(void) +{ + return &alaw_codec; +} + +const struct pcm_codec *get_itut_g711_mulaw_codec(void) +{ + return &mulaw_codec; +} + diff --git a/apps/codecs/libpcm/libpcm.make b/apps/codecs/libpcm/libpcm.make new file mode 100644 index 0000000000..ce7b6b5083 --- /dev/null +++ b/apps/codecs/libpcm/libpcm.make @@ -0,0 +1,25 @@ +# __________ __ ___. +# Open \______ \ ____ ____ | | _\_ |__ _______ ___ +# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +# \/ \/ \/ \/ \/ +# $Id$ +# + +# libpcm +PCMSLIB := $(CODECDIR)/libpcm.a +PCMSLIB_SRC := $(call preprocess, $(APPSDIR)/codecs/libpcm/SOURCES) +PCMSLIB_OBJ := $(call c2obj, $(PCMSLIB_SRC)) +OTHER_SRC += $(PCMSLIB_SRC) + +$(PCMSLIB): $(PCMSLIB_OBJ) + $(SILENT)$(shell rm -f $@) + $(call PRINTS,AR $(@F))$(AR) rcs $@ $^ >/dev/null + +PCMSFLAGS = $(filter-out -O%,$(CODECFLAGS)) +PCMSFLAGS += -O1 + +$(CODECDIR)/libpcm/%.o: $(ROOTDIR)/apps/codecs/libpcm/%.c + $(SILENT)mkdir -p $(dir $@) + $(call PRINTS,CC $(subst $(ROOTDIR)/,,$<))$(CC) $(PCMSFLAGS) -c $< -o $@ diff --git a/apps/codecs/libpcm/linear_pcm.c b/apps/codecs/libpcm/linear_pcm.c new file mode 100644 index 0000000000..2c766bd741 --- /dev/null +++ b/apps/codecs/libpcm/linear_pcm.c @@ -0,0 +1,163 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2005 Dave Chapman + * Copyright (C) 2009 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 "pcm_common.h" + +/* + * Linear PCM + */ + +static struct pcm_format *fmt; + +static bool set_format(struct pcm_format *format, const unsigned char *fmtpos) +{ + fmt = format; + + (void)fmtpos; + + if (fmt->bitspersample > 32) + { + DEBUGF("CODEC_ERROR: pcm with more than 32 bitspersample " + "is unsupported\n"); + return false; + } + + if (fmt->totalsamples == 0) + { + fmt->bytespersample = (((fmt->bitspersample - 1)/8 + 1)*fmt->channels); + fmt->totalsamples = fmt->numbytes/fmt->bytespersample; + } + + /* 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 */ + fmt->chunksize = (1 + fmt->avgbytespersec / (50*fmt->blockalign))*fmt->blockalign; + + return true; +} + +static uint32_t get_seek_pos(long 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)fmt->avgbytespersec*(seek_time - 1)) + / (1000LL*fmt->blockalign))*fmt->blockalign; + return newpos; +} + +static int decode(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufsize) +{ + uint32_t i; + + if (fmt->bitspersample > 24) + { + for (i = 0; i < inbufsize; i += 4) + { + if (fmt->is_little_endian) + { + if (fmt->is_signed) + outbuf[i/4] = (inbuf[i]>>3)|(inbuf[i+1]<<5)|(inbuf[i+2]<<13)|(SE(inbuf[i+3])<<21); + else + outbuf[i/4] = (inbuf[i]>>3)|(inbuf[i+1]<<5)|(inbuf[i+2]<<13)|(SFT(inbuf[i+3])<<21); + } + else + { + if (fmt->is_signed) + outbuf[i/4] = (inbuf[i+3]>>3)|(inbuf[i+2]<<5)|(inbuf[i+1]<<13)|(SE(inbuf[i])<<21); + else + outbuf[i/4] = (inbuf[i+3]>>3)|(inbuf[i+2]<<5)|(inbuf[i+1]<<13)|(SFT(inbuf[i])<<21); + } + } + *outbufsize = inbufsize >> 2; + } + else if (fmt->bitspersample > 16) + { + for (i = 0; i < inbufsize; i += 3) + { + if (fmt->is_little_endian) + { + if (fmt->is_signed) + outbuf[i/3] = (inbuf[i]<<5)|(inbuf[i+1]<<13)|(SE(inbuf[i+2])<<21); + else + outbuf[i/3] = (inbuf[i]<<5)|(inbuf[i+1]<<13)|(SFT(inbuf[i+2])<<21); + } + else + { + if (fmt->is_signed) + outbuf[i/3] = (inbuf[i+2]<<5)|(inbuf[i+1]<<13)|(SE(inbuf[i])<<21); + else + outbuf[i/3] = (inbuf[i+2]<<5)|(inbuf[i+1]<<13)|(SFT(inbuf[i])<<21); + } + } + *outbufsize = inbufsize/3; + } + else if (fmt->bitspersample > 8) + { + for (i = 0; i < inbufsize; i += 2) + { + if (fmt->is_little_endian) + { + if (fmt->is_signed) + outbuf[i/2] = (inbuf[i]<<13)|(SE(inbuf[i+1])<<21); + else + outbuf[i/2] = (inbuf[i]<<13)|(SFT(inbuf[i+1])<<21); + } + else + { + if (fmt->is_signed) + outbuf[i/2] = (inbuf[i+1]<<13)|(SE(inbuf[i])<<21); + else + outbuf[i/2] = (inbuf[i+1]<<13)|(SFT(inbuf[i])<<21); + } + } + *outbufsize = inbufsize >> 1; + } + else + { + for (i = 0; i < inbufsize; i++) { + if (fmt->is_signed) + outbuf[i] = SE(inbuf[i])<<21; + else + outbuf[i] = SFT(inbuf[i])<<21; + } + *outbufsize = inbufsize; + } + + if (fmt->channels == 2) + *outbufsize >>= 1; + + return CODEC_OK; +} + +static const struct pcm_codec codec = { + set_format, + get_seek_pos, + decode, + }; + +const struct pcm_codec *get_linear_pcm_codec(void) +{ + return &codec; +} diff --git a/apps/codecs/libpcm/pcm_common.h b/apps/codecs/libpcm/pcm_common.h new file mode 100644 index 0000000000..757d0ad5d9 --- /dev/null +++ b/apps/codecs/libpcm/pcm_common.h @@ -0,0 +1,120 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2009 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. + * + ****************************************************************************/ +#ifndef CODEC_LIBPCMS_PCM_COMMON_H +#define CODEC_LIBPCMS_PCM_COMMON_H + +#include +#include +#include + +/* + * PCM_CHUNK_SIZE has the size only of storing the sample at 1/50 seconds. + * But it might not be 1/50 seconds according to the format. + * Please confirm the source file of each format. + */ +#define PCM_CHUNK_SIZE (4096*2) + +/* Macro that sign extends an unsigned byte */ +#define SE(x) ((int32_t)((int8_t)(x))) + +/* Macro that shift to -0x80. (0 .. 127 to -128 .. -1, 128 .. 255 to 0 .. 127) */ +#define SFT(x) ((int32_t)x-0x80) + +struct pcm_format { + /* + * RIFF: wFormatTag (in 'fmt ' chunk) + * AIFF: compressionType (in 'COMM' chunk) + */ + uint32_t formattag; + + /* + * RIFF: wChannels (in 'fmt ' chunk) + * AIFF: numChannels (in 'COMM' chunk) + */ + uint16_t channels; + + /* + * RIFF: dwSamplesPerSec (in 'fmt ' chunk) + * AIFF: sampleRate (in 'COMM' chunk) + */ + uint32_t samplespersec; + + /* RIFF: dwAvgBytesPerSec (in 'fmt ' chunk) */ + uint32_t avgbytespersec; + + /* + * RIFF: wBlockAlign (in 'fmt ' chunk) + * AIFF: blockSize (in 'SSND' chunk) + */ + uint16_t blockalign; + + /* + * RIFF: wBitsPerSample (in 'fmt ' chunk) + * AIFF: sampleSize (in 'COMM' chunk) + */ + uint16_t bitspersample; + + /* RIFF: wSize (in 'fmt ' chunk) */ + uint16_t size; + + /* RIFF: dSamplesPerBlock (in 'fmt ' chunk) */ + uint16_t samplesperblock; + + /* RIFF: wTotalSamples (in 'fact' chunk) */ + uint16_t totalsamples; + + /* the following values are not RIFF/AIFF chunk values */ + + /* bytes per sample */ + int bytespersample; + + /* chunk size */ + long chunksize; + + /* data size */ + uint32_t numbytes; + + /* + * data endian + * true: little endian, false: big endian + */ + bool is_little_endian; + + /* + * data signess + * true: signed, false: unsigned + */ + bool is_signed; +}; + +struct pcm_codec { + bool (*set_format)(struct pcm_format *format, const unsigned char *fmtpos); + uint32_t (*get_seek_pos)(long seek_time); + int (*decode)(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufsize); +}; + +struct pcm_entry { + uint32_t format_tag; + const struct pcm_codec *(*get_codec)(void); +}; + +#endif diff --git a/apps/codecs/libpcm/support_formats.h b/apps/codecs/libpcm/support_formats.h new file mode 100644 index 0000000000..9a1f4f764d --- /dev/null +++ b/apps/codecs/libpcm/support_formats.h @@ -0,0 +1,37 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2009 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. + * + ****************************************************************************/ +#ifndef CODEC_LIBPCMS_SUPPORT_FORMATS_H +#define CODEC_LIBPCMS_SUPPORT_FORMATS_H + +#include "pcm_common.h" + +/* Linear PCM */ +const struct pcm_codec *get_linear_pcm_codec(void); + +/* ITU-T G.711 A-law */ +const struct pcm_codec *get_itut_g711_alaw_codec(void); + +/* ITU-T G.711 mu-law */ +const struct pcm_codec *get_itut_g711_mulaw_codec(void); + +/* Intel DVI ADPCM */ +const struct pcm_codec *get_dvi_adpcm_codec(void); +#endif diff --git a/apps/codecs/wav.c b/apps/codecs/wav.c index b3efbc10ce..1f210a99bf 100644 --- a/apps/codecs/wav.c +++ b/apps/codecs/wav.c @@ -8,6 +8,7 @@ * $Id$ * * Copyright (C) 2005 Dave Chapman + * Copyright (C) 2009 Yoshihisa Uchida * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -21,16 +22,11 @@ #include "codeclib.h" #include "inttypes.h" +#include "codecs/libpcm/support_formats.h" CODEC_HEADER -/* Macro that sign extends an unsigned byte */ -#define SE(x) ((int32_t)((int8_t)(x))) - -/* This codec support WAVE files with the following formats: - * - PCM, up to 32 bits, supporting 32 bits playback when useful. - * - ALAW and MULAW (16 bits compressed on 8 bits). - * - DVI_ADPCM (16 bits compressed on 3 or 4 bits). +/* WAVE (RIFF) codec: * * For a good documentation on WAVE files, see: * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/WAVE.html @@ -40,198 +36,74 @@ CODEC_HEADER * For sample WAV files, see: * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/Samples.html * - * The most common formats seem to be PCM, ADPCM, DVI_ADPCM, IEEE_FLOAT, - * ALAW and MULAW */ -/* These constants are from RFC 2361. */ +static int32_t samples[PCM_CHUNK_SIZE] IBSS_ATTR; + +/* This codec support WAVE files with the following formats: */ enum { WAVE_FORMAT_UNKNOWN = 0x0000, /* Microsoft Unknown Wave Format */ WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */ - WAVE_FORMAT_ADPCM = 0x0002, /* Microsoft ADPCM Format */ - WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* IEEE Float */ - WAVE_FORMAT_VSELP = 0x0004, /* Compaq Computer's VSELP */ - WAVE_FORMAT_IBM_CVSD = 0x0005, /* IBM CVSD */ WAVE_FORMAT_ALAW = 0x0006, /* Microsoft ALAW */ WAVE_FORMAT_MULAW = 0x0007, /* Microsoft MULAW */ - WAVE_FORMAT_OKI_ADPCM = 0x0010, /* OKI ADPCM */ WAVE_FORMAT_DVI_ADPCM = 0x0011, /* Intel's DVI ADPCM */ - WAVE_FORMAT_MEDIASPACE_ADPCM = 0x0012, /* Videologic's MediaSpace ADPCM */ - WAVE_FORMAT_SIERRA_ADPCM = 0x0013, /* Sierra ADPCM */ - WAVE_FORMAT_G723_ADPCM = 0x0014, /* G.723 ADPCM */ - WAVE_FORMAT_DIGISTD = 0x0015, /* DSP Solutions' DIGISTD */ - WAVE_FORMAT_DIGIFIX = 0x0016, /* DSP Solutions' DIGIFIX */ - WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic OKI ADPCM */ - WAVE_FORMAT_MEDIAVISION_ADPCM = 0x0018, /* MediaVision ADPCM */ - WAVE_FORMAT_CU_CODEC = 0x0019, /* HP CU */ - WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha ADPCM */ - WAVE_FORMAT_SONARC = 0x0021, /* Speech Compression's Sonarc */ - WAVE_FORMAT_DSP_TRUESPEECH = 0x0022, /* DSP Group's True Speech */ - WAVE_FORMAT_ECHOSC1 = 0x0023, /* Echo Speech's EchoSC1 */ - WAVE_FORMAT_AUDIOFILE_AF36 = 0x0024, /* Audiofile AF36 */ - WAVE_FORMAT_APTX = 0x0025, /* APTX */ - WAVE_FORMAT_DOLBY_AC2 = 0x0030, /* Dolby AC2 */ - WAVE_FORMAT_GSM610 = 0x0031, /* GSM610 */ - WAVE_FORMAT_MSNAUDIO = 0x0032, /* MSNAudio */ - WAVE_FORMAT_ANTEX_ADPCME = 0x0033, /* Antex ADPCME */ - - WAVE_FORMAT_MPEG = 0x0050, /* MPEG */ - WAVE_FORMAT_MPEGLAYER3 = 0x0055, /* MPEG layer 3 */ - WAVE_FORMAT_LUCENT_G723 = 0x0059, /* Lucent G.723 */ - WAVE_FORMAT_G726_ADPCM = 0x0064, /* G.726 ADPCM */ - WAVE_FORMAT_G722_ADPCM = 0x0065, /* G.722 ADPCM */ - IBM_FORMAT_MULAW = 0x0101, /* same as WAVE_FORMAT_MULAW */ IBM_FORMAT_ALAW = 0x0102, /* same as WAVE_FORMAT_ALAW */ - IBM_FORMAT_ADPCM = 0x0103, - - WAVE_FORMAT_CREATIVE_ADPCM = 0x0200, - WAVE_FORMAT_EXTENSIBLE = 0xFFFE }; -/* Maximum number of bytes to process in one iteration */ -/* for 44.1kHz stereo 16bits, this represents 0.023s ~= 1/50s */ -#define WAV_CHUNK_SIZE (1024*2) - -static const int16_t alaw2linear16[256] ICONST_ATTR = { - -5504, -5248, -6016, -5760, -4480, -4224, -4992, - -4736, -7552, -7296, -8064, -7808, -6528, -6272, - -7040, -6784, -2752, -2624, -3008, -2880, -2240, - -2112, -2496, -2368, -3776, -3648, -4032, -3904, - -3264, -3136, -3520, -3392, -22016, -20992, -24064, - -23040, -17920, -16896, -19968, -18944, -30208, -29184, - -32256, -31232, -26112, -25088, -28160, -27136, -11008, - -10496, -12032, -11520, -8960, -8448, -9984, -9472, - -15104, -14592, -16128, -15616, -13056, -12544, -14080, - -13568, -344, -328, -376, -360, -280, -264, - -312, -296, -472, -456, -504, -488, -408, - -392, -440, -424, -88, -72, -120, -104, - -24, -8, -56, -40, -216, -200, -248, - -232, -152, -136, -184, -168, -1376, -1312, - -1504, -1440, -1120, -1056, -1248, -1184, -1888, - -1824, -2016, -1952, -1632, -1568, -1760, -1696, - -688, -656, -752, -720, -560, -528, -624, - -592, -944, -912, -1008, -976, -816, -784, - -880, -848, 5504, 5248, 6016, 5760, 4480, - 4224, 4992, 4736, 7552, 7296, 8064, 7808, - 6528, 6272, 7040, 6784, 2752, 2624, 3008, - 2880, 2240, 2112, 2496, 2368, 3776, 3648, - 4032, 3904, 3264, 3136, 3520, 3392, 22016, - 20992, 24064, 23040, 17920, 16896, 19968, 18944, - 30208, 29184, 32256, 31232, 26112, 25088, 28160, - 27136, 11008, 10496, 12032, 11520, 8960, 8448, - 9984, 9472, 15104, 14592, 16128, 15616, 13056, - 12544, 14080, 13568, 344, 328, 376, 360, - 280, 264, 312, 296, 472, 456, 504, - 488, 408, 392, 440, 424, 88, 72, - 120, 104, 24, 8, 56, 40, 216, - 200, 248, 232, 152, 136, 184, 168, - 1376, 1312, 1504, 1440, 1120, 1056, 1248, - 1184, 1888, 1824, 2016, 1952, 1632, 1568, - 1760, 1696, 688, 656, 752, 720, 560, - 528, 624, 592, 944, 912, 1008, 976, - 816, 784, 880, 848 +const struct pcm_entry wave_codecs[] = { + { WAVE_FORMAT_UNKNOWN, 0 }, + { WAVE_FORMAT_PCM, get_linear_pcm_codec }, + { WAVE_FORMAT_ALAW, get_itut_g711_alaw_codec }, + { WAVE_FORMAT_MULAW, get_itut_g711_mulaw_codec }, + { WAVE_FORMAT_DVI_ADPCM, get_dvi_adpcm_codec }, + { IBM_FORMAT_MULAW, get_itut_g711_mulaw_codec }, + { IBM_FORMAT_ALAW, get_itut_g711_alaw_codec }, }; -static const int16_t ulaw2linear16[256] ICONST_ATTR = { - -32124, -31100, -30076, -29052, -28028, -27004, -25980, - -24956, -23932, -22908, -21884, -20860, -19836, -18812, - -17788, -16764, -15996, -15484, -14972, -14460, -13948, - -13436, -12924, -12412, -11900, -11388, -10876, -10364, - -9852, -9340, -8828, -8316, -7932, -7676, -7420, - -7164, -6908, -6652, -6396, -6140, -5884, -5628, - -5372, -5116, -4860, -4604, -4348, -4092, -3900, - -3772, -3644, -3516, -3388, -3260, -3132, -3004, - -2876, -2748, -2620, -2492, -2364, -2236, -2108, - -1980, -1884, -1820, -1756, -1692, -1628, -1564, - -1500, -1436, -1372, -1308, -1244, -1180, -1116, - -1052, -988, -924, -876, -844, -812, -780, - -748, -716, -684, -652, -620, -588, -556, - -524, -492, -460, -428, -396, -372, -356, - -340, -324, -308, -292, -276, -260, -244, - -228, -212, -196, -180, -164, -148, -132, - -120, -112, -104, -96, -88, -80, -72, - -64, -56, -48, -40, -32, -24, -16, - -8, 0, 32124, 31100, 30076, 29052, 28028, - 27004, 25980, 24956, 23932, 22908, 21884, 20860, - 19836, 18812, 17788, 16764, 15996, 15484, 14972, - 14460, 13948, 13436, 12924, 12412, 11900, 11388, - 10876, 10364, 9852, 9340, 8828, 8316, 7932, - 7676, 7420, 7164, 6908, 6652, 6396, 6140, - 5884, 5628, 5372, 5116, 4860, 4604, 4348, - 4092, 3900, 3772, 3644, 3516, 3388, 3260, - 3132, 3004, 2876, 2748, 2620, 2492, 2364, - 2236, 2108, 1980, 1884, 1820, 1756, 1692, - 1628, 1564, 1500, 1436, 1372, 1308, 1244, - 1180, 1116, 1052, 988, 924, 876, 844, - 812, 780, 748, 716, 684, 652, 620, - 588, 556, 524, 492, 460, 428, 396, - 372, 356, 340, 324, 308, 292, 276, - 260, 244, 228, 212, 196, 180, 164, - 148, 132, 120, 112, 104, 96, 88, - 80, 72, 64, 56, 48, 40, 32, - 24, 16, 8, 0 -}; +#define NUM_FORMATS 7 + +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; +} -static const uint16_t dvi_adpcm_steptab[89] ICONST_ATTR = { - 7, 8, 9, 10, 11, 12, 13, 14, - 16, 17, 19, 21, 23, 25, 28, 31, - 34, 37, 41, 45, 50, 55, 60, 66, - 73, 80, 88, 97, 107, 118, 130, 143, - 157, 173, 190, 209, 230, 253, 279, 307, - 337, 371, 408, 449, 494, 544, 598, 658, - 724, 796, 876, 963, 1060, 1166, 1282, 1411, - 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, - 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, - 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, - 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, - 32767 }; - -static const int dvi_adpcm_indextab4[8] ICONST_ATTR = { - -1, -1, -1, -1, 2, 4, 6, 8 }; - -static const int dvi_adpcm_indextab3[4] ICONST_ATTR = { -1, -1, 1, 2 }; - -static int32_t samples[WAV_CHUNK_SIZE] IBSS_ATTR; - -static enum codec_status -decode_dvi_adpcm(struct codec_api *ci, - const uint8_t *buf, - int n, - uint16_t channels, uint16_t bitspersample, - int32_t *pcmout, - size_t *pcmoutsize); /* this is the codec entry point */ enum codec_status codec_main(void) { - uint32_t numbytes, bytesdone; - uint32_t totalsamples = 0; - uint16_t channels = 0; - uint16_t samplesperblock = 0; - int bytespersample = 0; - uint16_t bitspersample; + int status = CODEC_OK; + struct pcm_format format; + uint32_t bytesdone, decodedbytes; uint32_t i; size_t n; int bufcount; int endofstream; unsigned char *buf; uint8_t *wavbuf; - long chunksize; - uint16_t formattag = 0; - uint16_t blockalign = 0; - uint32_t avgbytespersec = 0; off_t firstblockposn; /* position of the first block in file */ - - + const struct pcm_codec *codec; + /* Generic codec initialisation */ ci->configure(DSP_SET_SAMPLE_DEPTH, 28); next_track: if (codec_init()) { - i = CODEC_ERROR; + DEBUGF("codec_init() error\n"); + status = CODEC_ERROR; goto exit; } @@ -246,11 +118,12 @@ next_track: /* get RIFF chunk header */ buf = ci->request_buffer(&n, 12); if (n < 12) { - i = CODEC_ERROR; + DEBUGF("request_buffer error\n"); + status = CODEC_ERROR; goto done; } if ((memcmp(buf, "RIFF", 4) != 0) || (memcmp(&buf[8], "WAVE", 4) != 0)) { - i = CODEC_ERROR; + status = CODEC_ERROR; goto done; } @@ -258,17 +131,21 @@ next_track: ci->advance_buffer(12); firstblockposn = 12; - bitspersample = 0; - numbytes = 0; - totalsamples = 0; + ci->memset(&format, 0, sizeof(struct pcm_format)); + format.is_signed = true; + format.is_little_endian = true; + + decodedbytes = 0; + codec = 0; /* 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) { + DEBUGF("data chunk request_buffer error\n"); /* no more chunks, 'data' chunk must not have been found */ - i = CODEC_ERROR; + status = CODEC_ERROR; goto done; } @@ -278,54 +155,70 @@ next_track: if (i < 16) { DEBUGF("CODEC_ERROR: 'fmt ' chunk size=%lu < 16\n", (unsigned long)i); - i = CODEC_ERROR; + status = CODEC_ERROR; goto done; } /* wFormatTag */ - formattag=buf[8]|(buf[9]<<8); + format.formattag=buf[8]|(buf[9]<<8); /* wChannels */ - channels=buf[10]|(buf[11]<<8); + format.channels=buf[10]|(buf[11]<<8); /* skipping dwSamplesPerSec */ /* dwAvgBytesPerSec */ - avgbytespersec = buf[16]|(buf[17]<<8)|(buf[18]<<16)|(buf[19]<<24); + format.avgbytespersec = buf[16]|(buf[17]<<8)|(buf[18]<<16)|(buf[19]<<24); /* wBlockAlign */ - blockalign=buf[20]|(buf[21]<<8); + format.blockalign=buf[20]|(buf[21]<<8); /* wBitsPerSample */ - bitspersample=buf[22]|(buf[23]<<8); - if (formattag != WAVE_FORMAT_PCM) { - uint16_t size; + format.bitspersample=buf[22]|(buf[23]<<8); + if (format.formattag != WAVE_FORMAT_PCM) { if (i < 18) { /* 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) " "doesn't have ext. fmt descr (chunksize=%ld<18).\n", - formattag, (long)i); + format.formattag, (long)i); } - size = buf[24]|(buf[25]<<8); - if (formattag == WAVE_FORMAT_DVI_ADPCM) { - if (size < 2) { - DEBUGF("CODEC_ERROR: dvi_adpcm is missing " - "SamplesPerBlock value\n"); - i = CODEC_ERROR; - goto done; - } - samplesperblock = buf[26]|(buf[27]<<8); - } else if (formattag == WAVE_FORMAT_EXTENSIBLE) { - if (size < 22) { - DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is " - "missing extension\n"); - i = CODEC_ERROR; - goto done; + else + { + format.size = buf[24]|(buf[25]<<8); + if (format.formattag != WAVE_FORMAT_EXTENSIBLE) + format.samplesperblock = buf[26]|(buf[27]<<8); + else { + if (format.size < 22) { + DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is " + "missing extension\n"); + status = CODEC_ERROR; + goto done; + } + /* 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); } - /* wValidBitsPerSample */ - bitspersample = buf[26]|(buf[27]<<8); - /* skipping dwChannelMask (4bytes) */ - /* SubFormat (only get the first two bytes) */ - formattag = buf[32]|(buf[33]<<8); } } + + /* get codec */ + codec = get_wave_codec(format.formattag); + if (!codec) + { + DEBUGF("CODEC_ERROR: unsupport wave format %x\n", format.formattag); + status = CODEC_ERROR; + goto done; + } + + /* 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 */ + if (!codec->set_format(&format, buf)) + { + status = CODEC_ERROR; + goto done; + } } else if (memcmp(buf, "data", 4) == 0) { - numbytes = i; + format.numbytes = i; /* advance to start of data */ ci->advance_buffer(8); firstblockposn += 8; @@ -333,7 +226,8 @@ next_track: } else if (memcmp(buf, "fact", 4) == 0) { /* dwSampleLength */ if (i >= 4) - totalsamples = (buf[8]|(buf[9]<<8)|(buf[10]<<16)|(buf[11]<<24)); + format.totalsamples = + (buf[8]|(buf[9]<<8)|(buf[10]<<16)|(buf[11]<<24)); } else { DEBUGF("unknown WAVE chunk: '%c%c%c%c', size=%lu\n", buf[0], buf[1], buf[2], buf[3], (unsigned long)i); @@ -346,71 +240,40 @@ next_track: firstblockposn += i + 8; } - if (channels == 0) { - DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n"); - i = CODEC_ERROR; + if (!codec) + { + DEBUGF("CODEC_ERROR: 'fmt ' chunk not found\n"); + status = CODEC_ERROR; goto done; } - if (numbytes == 0) { - DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n"); - i = CODEC_ERROR; - goto done; - } - if (formattag != WAVE_FORMAT_PCM && totalsamples == 0) { - /* This is non-fatal for some formats */ - DEBUGF("CODEC_WARNING: non-PCM WAVE doesn't have a 'fact' chunk\n"); - } - if (formattag == WAVE_FORMAT_ALAW || formattag == WAVE_FORMAT_MULAW || - formattag == IBM_FORMAT_ALAW || formattag == IBM_FORMAT_MULAW) { - if (bitspersample != 8) { - DEBUGF("CODEC_ERROR: alaw and mulaw must have 8 bitspersample\n"); - i = CODEC_ERROR; - goto done; - } - bytespersample = channels; - } - if (formattag == WAVE_FORMAT_DVI_ADPCM - && bitspersample != 4 && bitspersample != 3) { - DEBUGF("CODEC_ERROR: dvi_adpcm must have 3 or 4 bitspersample\n"); - i = CODEC_ERROR; + + /* common format check */ + if (format.channels == 0) { + DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n"); + status = CODEC_ERROR; goto done; } - if (formattag == WAVE_FORMAT_PCM && bitspersample > 32) { - DEBUGF("CODEC_ERROR: pcm with more than 32 bitspersample " - "is unsupported\n"); - i = CODEC_ERROR; + if (format.numbytes == 0) { + DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n"); + status = CODEC_ERROR; goto done; } ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); - if (channels == 2) { + if (format.channels == 2) { ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); - } else if (channels == 1) { + } else if (format.channels == 1) { ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); } else { DEBUGF("CODEC_ERROR: more than 2 channels\n"); - i = CODEC_ERROR; + status = CODEC_ERROR; goto done; } - if (totalsamples == 0) { - if (formattag == WAVE_FORMAT_PCM || - formattag == WAVE_FORMAT_ALAW || formattag == WAVE_FORMAT_MULAW || - formattag == IBM_FORMAT_ALAW || formattag == IBM_FORMAT_MULAW) { - /* for PCM and derived formats only */ - bytespersample = (((bitspersample - 1)/8 + 1)*channels); - totalsamples = numbytes/bytespersample; - } else { - DEBUGF("CODEC_ERROR: cannot compute totalsamples\n"); - i = CODEC_ERROR; - goto done; - } - } - /* make sure we're at the correct offset */ if (bytesdone > (uint32_t) firstblockposn) { /* Round down to previous block */ - uint32_t offset = bytesdone - bytesdone % blockalign; + uint32_t offset = bytesdone - bytesdone % format.blockalign; ci->advance_buffer(offset-firstblockposn); bytesdone = offset - firstblockposn; @@ -421,18 +284,6 @@ next_track: /* The main decoder loop */ 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*blockalign))*blockalign; - /* check that the output buffer is big enough (convert to samplespersec, - then round to the blockalign multiple below) */ - if (((uint64_t)chunksize*ci->id3->frequency*channels*2) - /(uint64_t)avgbytespersec >= WAV_CHUNK_SIZE) { - chunksize = ((uint64_t)WAV_CHUNK_SIZE*avgbytespersec - /((uint64_t)ci->id3->frequency*channels*2 - *blockalign))*blockalign; - } while (!endofstream) { ci->yield(); @@ -441,286 +292,47 @@ next_track: } if (ci->seek_time) { - uint32_t newpos; + uint32_t newpos = codec->get_seek_pos(ci->seek_time); - /* 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*blockalign))*blockalign; - if (newpos > numbytes) + if (newpos > format.numbytes) break; if (ci->seek_buffer(firstblockposn + newpos)) + { bytesdone = newpos; + } ci->seek_complete(); } - wavbuf = (uint8_t *)ci->request_buffer(&n, chunksize); + wavbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize); if (n == 0) break; /* End of stream */ - - if (bytesdone + n > numbytes) { - n = numbytes - bytesdone; + if (bytesdone + n > format.numbytes) { + n = format.numbytes - bytesdone; endofstream = 1; } - if (formattag == WAVE_FORMAT_PCM) { - if (bitspersample > 24) { - for (i = 0; i < n; i += 4) { - samples[i/4] = (wavbuf[i] >> 3)| - (wavbuf[i + 1]<<5)|(wavbuf[i + 2]<<13)| - (SE(wavbuf[i + 3])<<21); - } - bufcount = n >> 2; - } else if (bitspersample > 16) { - for (i = 0; i < n; i += 3) { - samples[i/3] = (wavbuf[i]<<5)| - (wavbuf[i + 1]<<13)|(SE(wavbuf[i + 2])<<21); - } - bufcount = n/3; - } else if (bitspersample > 8) { - for (i = 0; i < n; i += 2) { - samples[i/2] = (wavbuf[i]<<13)|(SE(wavbuf[i + 1])<<21); - } - bufcount = n >> 1; - } else { - for (i = 0; i < n; i++) { - samples[i] = (wavbuf[i] - 0x80)<<21; - } - bufcount = n; - } - - if (channels == 2) - bufcount >>= 1; - } else if (formattag == WAVE_FORMAT_ALAW - || formattag == IBM_FORMAT_ALAW) { - for (i = 0; i < n; i++) - samples[i] = alaw2linear16[wavbuf[i]] << 13; - - bufcount = (channels == 2) ? (n >> 1) : n; - } else if (formattag == WAVE_FORMAT_MULAW - || formattag == IBM_FORMAT_MULAW) { - for (i = 0; i < n; i++) - samples[i] = ulaw2linear16[wavbuf[i]] << 13; - - bufcount = (channels == 2) ? (n >> 1) : n; - } - else if (formattag == WAVE_FORMAT_DVI_ADPCM) { - unsigned int nblocks = chunksize/blockalign; - - for (i = 0; i < nblocks; i++) { - size_t decodedsize = samplesperblock*channels; - if (decode_dvi_adpcm(ci, wavbuf + i*blockalign, - blockalign, channels, bitspersample, - samples + i*samplesperblock*channels, - &decodedsize) != CODEC_OK) { - i = CODEC_ERROR; - goto done; - } - } - bufcount = nblocks*samplesperblock; - } else { - DEBUGF("CODEC_ERROR: unsupported format %x\n", formattag); - i = CODEC_ERROR; + status = codec->decode(wavbuf, n, samples, &bufcount); + if (status == CODEC_ERROR) + { + DEBUGF("codec error\n"); goto done; } ci->pcmbuf_insert(samples, NULL, bufcount); - ci->advance_buffer(n); bytesdone += n; - if (bytesdone >= numbytes) + decodedbytes += bufcount; + + if (bytesdone >= format.numbytes) endofstream = 1; - ci->set_elapsed(bytesdone*1000LL/avgbytespersec); + ci->set_elapsed(decodedbytes*1000LL/ci->id3->frequency); } - i = CODEC_OK; + status = CODEC_OK; done: if (ci->request_next_track()) goto next_track; exit: - return i; + return status; } - -static enum codec_status -decode_dvi_adpcm(struct codec_api *ci, - const uint8_t *buf, - int n, - uint16_t channels, uint16_t bitspersample, - int32_t *pcmout, - size_t *pcmoutsize) -{ - size_t nsamples = 0; - int sample[2]; - int samplecode[32][2]; - int i; - int stepindex[2]; - int c; - int diff; - int step; - int codem; - int code; - - (void)ci; - if (bitspersample != 4 && bitspersample != 3) { - DEBUGF("decode_dvi_adpcm: wrong bitspersample\n"); - return CODEC_ERROR; - } - - /* decode block header */ - for (c = 0; c < channels && n >= 4; c++) { - /* decode + push first sample */ - sample[c] = (short)(buf[0]|(buf[1]<<8));/* need cast for sign-extend */ - pcmout[c] = sample[c] << 13; - nsamples++; - stepindex[c] = buf[2]; - /* check for step table index overflow */ - if (stepindex[c] > 88) { - DEBUGF("decode_dvi_adpcm: stepindex[%d]=%d>88\n",c,stepindex[c]); - return CODEC_ERROR; - } - - buf += 4; - n -= 4; - } - if (bitspersample == 4) { - while (n>= channels*4 && (nsamples + 8*channels) <= *pcmoutsize) { - for (c = 0; c < channels; c++) { - samplecode[0][c] = buf[0]&0xf; - samplecode[1][c] = buf[0]>>4; - samplecode[2][c] = buf[1]&0xf; - samplecode[3][c] = buf[1]>>4; - samplecode[4][c] = buf[2]&0xf; - samplecode[5][c] = buf[2]>>4; - samplecode[6][c] = buf[3]&0xf; - samplecode[7][c] = buf[3]>>4; - buf += 4; - n -= 4; - } - for (i = 0; i < 8; i++) { - for (c = 0; c < channels; c++) { - step = dvi_adpcm_steptab[stepindex[c]]; - codem = samplecode[i][c]; - code = codem & 0x07; - - /* adjust the step table index */ - stepindex[c] += dvi_adpcm_indextab4[code]; - /* check for step table index overflow and underflow */ - if (stepindex[c] > 88) - stepindex[c] = 88; - else if (stepindex[c] < 0) - stepindex[c] = 0; - /* calculate the difference */ -#ifdef STRICT_IMA - diff = 0; - if (code & 4) - diff += step; - step = step >> 1; - if (code & 2) - diff += step; - step = step >> 1; - if (code & 1) - diff += step; - step = step >> 1; - diff += step; -#else - diff = ((code + code + 1) * step) >> 3; /* faster */ -#endif - /* check the sign bit */ - /* check for overflow and underflow errors */ - if (code != codem) { - sample[c] -= diff; - if (sample[c] < -32768) - sample[c] = -32768; - } else { - sample[c] += diff; - if (sample[c] > 32767) - sample[c] = 32767; - } - /* output the new sample */ - pcmout[nsamples] = sample[c] << 13; - nsamples++; - } - } - } - } else { /* bitspersample == 3 */ - while (n >= channels*12 && (nsamples + 32*channels) <= *pcmoutsize) { - for (c = 0; c < channels; c++) { - uint16_t bitstream = 0; - int bitsread = 0; - for (i = 0; i < 32 && n > 0; i++) { - if (bitsread < 3) { - /* read 8 more bits */ - bitstream |= buf[0]<>3; - bitsread -= 3; - } - if (bitsread != 0) { - /* 32*3 = 3 words, so we should end with bitsread==0 */ - DEBUGF("decode_dvi_adpcm: error in implementation\n"); - return CODEC_ERROR; - } - } - - for (i = 0; i < 32; i++) { - for (c = 0; c < channels; c++) { - step = dvi_adpcm_steptab[stepindex[c]]; - codem = samplecode[i][c]; - code = codem & 0x03; - - /* adjust the step table index */ - stepindex[c] += dvi_adpcm_indextab3[code]; - /* check for step table index overflow and underflow */ - if (stepindex[c] > 88) - stepindex[c] = 88; - else if (stepindex[c] < 0) - stepindex[c] = 0; - /* calculate the difference */ -#ifdef STRICT_IMA - diff = 0; - if (code & 2) - diff += step; - step = step >> 1; - if (code & 1) - diff += step; - step = step >> 1; - diff += step; -#else - diff = ((code + code + 1) * step) >> 3; /* faster */ -#endif - /* check the sign bit */ - /* check for overflow and underflow errors */ - if (code != codem) { - sample[c] -= diff; - if (sample[c] < -32768) - sample[c] = -32768; - } - else { - sample[c] += diff; - if (sample[c] > 32767) - sample[c] = 32767; - } - /* output the new sample */ - pcmout[nsamples] = sample[c] << 13; - nsamples++; - } - } - } - } - - if (nsamples > *pcmoutsize) { - DEBUGF("decode_dvi_adpcm: output buffer overflow!\n"); - return CODEC_ERROR; - } - *pcmoutsize = nsamples; - if (n != 0) { - DEBUGF("decode_dvi_adpcm: n=%d unprocessed bytes\n", n); - } - return CODEC_OK; -} - diff --git a/apps/metadata/aiff.c b/apps/metadata/aiff.c index cb18e9249a..67fb43b8c6 100644 --- a/apps/metadata/aiff.c +++ b/apps/metadata/aiff.c @@ -48,7 +48,7 @@ bool get_aiff_metadata(int fd, struct mp3entry* id3) } if ((memcmp(buf, "FORM",4) != 0) - || (memcmp(&buf[8], "AIFF", 4) !=0 )) + || ((memcmp(&buf[8], "AIFF", 4) !=0) && (memcmp(&buf[8], "AIFC", 4) !=0))) { return false; } -- cgit v1.2.3