summaryrefslogtreecommitdiff
path: root/apps/codecs/libpcm
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libpcm')
-rw-r--r--apps/codecs/libpcm/SOURCES3
-rwxr-xr-xapps/codecs/libpcm/dvi_adpcm.c309
-rw-r--r--apps/codecs/libpcm/itut_g711.c203
-rw-r--r--apps/codecs/libpcm/libpcm.make25
-rw-r--r--apps/codecs/libpcm/linear_pcm.c163
-rw-r--r--apps/codecs/libpcm/pcm_common.h120
-rw-r--r--apps/codecs/libpcm/support_formats.h37
7 files changed, 860 insertions, 0 deletions
diff --git a/apps/codecs/libpcm/SOURCES b/apps/codecs/libpcm/SOURCES
new file mode 100644
index 0000000..e921584
--- /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 0000000..9118906
--- /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]<<bitsread;
+ bitsread += 8;
+ inbufsize--;
+ inbuf++;
+ }
+ samplecode[i][c] = bitstream & 7;
+ bitstream = bitstream>>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 0000000..1f23594
--- /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 0000000..ce7b6b5
--- /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 0000000..2c766bd
--- /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 0000000..757d0ad
--- /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 <sys/types.h>
+#include <stdbool.h>
+#include <inttypes.h>
+
+/*
+ * 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 0000000..9a1f4f7
--- /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