diff --git a/apps/SOURCES b/apps/SOURCES index 8166dbe..0346931 100644 --- a/apps/SOURCES +++ b/apps/SOURCES @@ -168,6 +168,7 @@ metadata/wavpack.c metadata/a52.c metadata/asap.c metadata/rm.c +metadata/snd.c #endif #ifdef HAVE_TAGCACHE tagcache.c diff --git a/apps/codecs/SOURCES b/apps/codecs/SOURCES index 44a8498..264a7b8 100644 --- a/apps/codecs/SOURCES +++ b/apps/codecs/SOURCES @@ -23,6 +23,7 @@ shorten.c aiff.c speex.c adx.c +snd.c #if defined(HAVE_RECORDING) && !defined(SIMULATOR) /* encoders */ aiff_enc.c diff --git a/apps/codecs/aiff.c b/apps/codecs/aiff.c index 9a67541..6987514 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,68 @@ #include "codeclib.h" #include +#include "codecs/libpcms/support_formats.h" CODEC_HEADER -/* Macro that sign extends an unsigned byte */ -#define SE(x) ((int32_t)((int8_t)(x))) - -/* This codec supports AIFF files with the following formats: - * - PCM, 8, 16 and 24 bits, mono or stereo - */ +#define FOURCC(c1, c2, c3, c4) \ +((((uint32_t)c1)<<24)|(((uint32_t)c2)<<16)|(((uint32_t)c3)<<8)|((uint32_t)c4)) + +/* 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 */ + AIFC_FORMAT_IEEE_FLOAT32 = FOURCC('f', 'l', '3', '2'), /* AIFC IEEE float 32 bit */ + AIFC_FORMAT_IEEE_FLOAT64 = FOURCC('f', 'l', '6', '4'), /* AIFC IEEE float 64 bit */ + AIFC_FORMAT_QT_IMA_ADPCM = FOURCC('i', 'm', 'a', '4'), /* AIFC QuickTime IMA ADPCM */ +}; -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 }, + { AIFC_FORMAT_IEEE_FLOAT32, get_ieee_float_codec }, + { AIFC_FORMAT_IEEE_FLOAT64, get_ieee_float_codec }, + { AIFC_FORMAT_QT_IMA_ADPCM, get_qt_ima_adpcm_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 6 + +static int32_t samples[PCM_CHUNK_SIZE] IBSS_ATTR; -static int32_t samples[AIF_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 +105,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 +186,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 +210,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%lx\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 +254,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 +261,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 a8c0085..4c8902b 100644 --- a/apps/codecs/codecs.make +++ b/apps/codecs/codecs.make @@ -34,6 +34,7 @@ include $(APPSDIR)/codecs/libtremor/libtremor.make include $(APPSDIR)/codecs/libwavpack/libwavpack.make include $(APPSDIR)/codecs/libwma/libwma.make include $(APPSDIR)/codecs/libcook/libcook.make +include $(APPSDIR)/codecs/libpcms/libpcms.make # compile flags for codecs CODECFLAGS = $(CFLAGS) -I$(APPSDIR)/codecs -I$(APPSDIR)/codecs/lib \ @@ -75,6 +76,9 @@ $(CODECDIR)/wma.codec : $(CODECDIR)/libwma.a $(CODECDIR)/wavpack_enc.codec: $(CODECDIR)/libwavpack.a $(CODECDIR)/asap.codec : $(CODECDIR)/libasap.a $(CODECDIR)/cook.codec : $(CODECDIR)/libcook.a +$(CODECDIR)/aiff.codec : $(CODECDIR)/libpcms.a +$(CODECDIR)/wav.codec : $(CODECDIR)/libpcms.a +$(CODECDIR)/snd.codec : $(CODECDIR)/libpcms.a $(CODECS): $(CODECLIB) # this must be last in codec dependency list diff --git a/apps/codecs/wav.c b/apps/codecs/wav.c index b3efbc1..509c699 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/libpcms/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,86 @@ 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 */ - + WAVE_FORMAT_XBOX_ADPCM = 0x0069, /* XBOX 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_SWF_ADPCM = 0x5346, /* Adobe SWF ADPCM */ 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_ADPCM, get_ms_adpcm_codec }, + { WAVE_FORMAT_IEEE_FLOAT, get_ieee_float_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 }, + { WAVE_FORMAT_DIALOGIC_OKI_ADPCM, get_dialogic_oki_adpcm_codec }, + { WAVE_FORMAT_YAMAHA_ADPCM, get_yamaha_adpcm_codec }, + { WAVE_FORMAT_XBOX_ADPCM, get_dvi_adpcm_codec }, + { IBM_FORMAT_MULAW, get_itut_g711_mulaw_codec }, + { IBM_FORMAT_ALAW, get_itut_g711_alaw_codec }, + { WAVE_FORMAT_SWF_ADPCM, get_swf_adpcm_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 13 + +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 +130,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 +143,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 +167,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) " + DEBUGF("CODEC_WARNING: non-PCM WAVE (formattag=0x%lx) " "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 %lx\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 +238,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 +252,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 +296,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 +304,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/filetypes.c b/apps/filetypes.c index 1772cac..4fb6462 100644 --- a/apps/filetypes.c +++ b/apps/filetypes.c @@ -85,6 +85,8 @@ static const struct filetype inbuilt_filetypes[] = { { "sap" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, { "rm", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, { "ra", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, + { "au", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, + { "snd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, #endif { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, diff --git a/apps/metadata.c b/apps/metadata.c index b995e11..a123175 100644 --- a/apps/metadata.c +++ b/apps/metadata.c @@ -118,6 +118,9 @@ const struct afmt_entry audio_formats[AFMT_NUM_CODECS] = /* Cook in RM/RA */ [AFMT_COOK] = AFMT_ENTRY("Cook", "cook", NULL, "rm\0ra\0" ), + /* Sun Audio file */ + [AFMT_SND] = + AFMT_ENTRY("snd", "snd", NULL, "au\0snd\0" ), #endif }; @@ -383,6 +386,14 @@ bool get_metadata(struct mp3entry* id3, int fd, const char* trackname) return false; } break; + + case AFMT_SND: + if (!get_snd_metadata(fd, id3)) + { + DEBUGF("get_snd_metadata error\n"); + return false; + } + break; #endif /* CONFIG_CODEC == SWCODEC */ diff --git a/apps/metadata.h b/apps/metadata.h index 6c02017..367c66f 100644 --- a/apps/metadata.h +++ b/apps/metadata.h @@ -62,6 +62,7 @@ enum AFMT_MOD, /* Amiga MOD File Format */ AFMT_SAP, /* Amiga 8Bit SAP Format */ AFMT_COOK, /* Cook in RM/RA */ + AFMT_SND, /* Sun Audio file */ #endif /* add new formats at any index above this line to have a sensible order - diff --git a/apps/metadata/aiff.c b/apps/metadata/aiff.c index cb18e92..aba327f 100644 --- a/apps/metadata/aiff.c +++ b/apps/metadata/aiff.c @@ -29,6 +29,9 @@ #include "metadata_common.h" #include "metadata_parsers.h" +/* compressionType: AIFC QuickTime IMA ADPCM */ +#define AIFC_FORMAT_QT_IMA_ADPCM "ima4" + bool get_aiff_metadata(int fd, struct mp3entry* id3) { /* Use the trackname part of the id3 structure as a temporary buffer */ @@ -40,6 +43,7 @@ bool get_aiff_metadata(int fd, struct mp3entry* id3) unsigned long numbytes = 0; int read_bytes; int i; + bool is_aifc = false; if ((lseek(fd, 0, SEEK_SET) < 0) || ((read_bytes = read(fd, buf, sizeof(id3->path))) < 54)) @@ -47,10 +51,15 @@ bool get_aiff_metadata(int fd, struct mp3entry* id3) return false; } - if ((memcmp(buf, "FORM",4) != 0) - || (memcmp(&buf[8], "AIFF", 4) !=0 )) - { + if (memcmp(buf, "FORM",4) != 0) return false; + + if (memcmp(&buf[8], "AIFF", 4) != 0) + { + if (memcmp(&buf[8], "AIFC", 4) != 0) + return false; + + is_aifc = true; } buf += 12; @@ -75,7 +84,13 @@ bool get_aiff_metadata(int fd, struct mp3entry* id3) /* save format infos */ id3->bitrate = (sampleSize * numChannels * sampleRate) / 1000; id3->frequency = sampleRate; - id3->length = ((int64_t) numSampleFrames * 1000) / id3->frequency; + if (!is_aifc || memcmp(&buf[26], AIFC_FORMAT_QT_IMA_ADPCM, 4) != 0) + id3->length = ((int64_t) numSampleFrames * 1000) / id3->frequency; + else + { + /* QuickTime IMA ADPCM is 1block = 64 data for each channel */ + id3->length = (int64_t)(numSampleFrames * 64000LL) / id3->frequency; + } id3->vbr = false; /* AIFF files are CBR */ id3->filesize = filesize(fd); diff --git a/apps/metadata/metadata_parsers.h b/apps/metadata/metadata_parsers.h index 760d9a0..6625b1b 100644 --- a/apps/metadata/metadata_parsers.h +++ b/apps/metadata/metadata_parsers.h @@ -40,3 +40,4 @@ bool get_a52_metadata(int fd, struct mp3entry* id3); bool get_asf_metadata(int fd, struct mp3entry* id3); bool get_asap_metadata(int fd, struct mp3entry* id3); bool get_rm_metadata(int fd, struct mp3entry* id3); +bool get_snd_metadata(int fd, struct mp3entry* id3); diff --git a/apps/metadata/wave.c b/apps/metadata/wave.c index cf676f8..311555c 100644 --- a/apps/metadata/wave.c +++ b/apps/metadata/wave.c @@ -29,17 +29,89 @@ #include "metadata_common.h" #include "metadata_parsers.h" +enum +{ + WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */ + WAVE_FORMAT_ADPCM = 0x0002, /* Microsoft ADPCM Format */ + WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* IEEE Float */ + WAVE_FORMAT_ALAW = 0x0006, /* Microsoft ALAW */ + WAVE_FORMAT_MULAW = 0x0007, /* Microsoft MULAW */ + WAVE_FORMAT_DVI_ADPCM = 0x0011, /* Intel's DVI ADPCM */ + WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic OKI ADPCM */ + WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha ADPCM */ + WAVE_FORMAT_XBOX_ADPCM = 0x0069, /* XBOX ADPCM */ + IBM_FORMAT_MULAW = 0x0101, /* same as WAVE_FORMAT_MULAW */ + IBM_FORMAT_ALAW = 0x0102, /* same as WAVE_FORMAT_ALAW */ + WAVE_FORMAT_SWF_ADPCM = 0x5346, /* Adobe SWF ADPCM */ +}; + +struct wave_fmt { + unsigned int formattag; + unsigned long channels; + unsigned int blockalign; + unsigned long bitspersample; + unsigned int samplesperblock; + unsigned long numbytes; +}; + +static unsigned long get_totalsamples(struct wave_fmt *fmt, struct mp3entry* id3) +{ + unsigned long totalsamples = 0; + + switch (fmt->formattag) + { + case WAVE_FORMAT_PCM: + case WAVE_FORMAT_IEEE_FLOAT: + case WAVE_FORMAT_ALAW: + case WAVE_FORMAT_MULAW: + case IBM_FORMAT_ALAW: + case IBM_FORMAT_MULAW: + totalsamples = + fmt->numbytes / ((((fmt->bitspersample - 1) / 8) + 1) * fmt->channels); + break; + case WAVE_FORMAT_ADPCM: + case WAVE_FORMAT_DVI_ADPCM: + case WAVE_FORMAT_XBOX_ADPCM: + totalsamples = (fmt->numbytes / fmt->blockalign) * fmt->samplesperblock; + break; + case WAVE_FORMAT_YAMAHA_ADPCM: + if (fmt->samplesperblock == 0) + { + if (fmt->blockalign == ((id3->frequency / 60) + 4) * fmt->channels) + fmt->samplesperblock = id3->frequency / 30; + else + fmt->samplesperblock = fmt->blockalign * 2 / fmt->channels; + } + totalsamples = (fmt->numbytes / fmt->blockalign) * fmt->samplesperblock; + break; + case WAVE_FORMAT_DIALOGIC_OKI_ADPCM: + totalsamples = 2 * fmt->numbytes; + break; + case WAVE_FORMAT_SWF_ADPCM: + if (fmt->samplesperblock == 0) + fmt->samplesperblock = (((fmt->blockalign << 3) - 2) / fmt->channels - 22) + / fmt->bitspersample; + + totalsamples = (fmt->numbytes / fmt->blockalign) * fmt->samplesperblock; + break; + default: + totalsamples = 0; + break; + } + return totalsamples; +} + bool get_wave_metadata(int fd, struct mp3entry* id3) { /* Use the trackname part of the id3 structure as a temporary buffer */ unsigned char* buf = (unsigned char *)id3->path; + struct wave_fmt fmt; unsigned long totalsamples = 0; - unsigned long channels = 0; - unsigned long bitspersample = 0; - unsigned long numbytes = 0; int read_bytes; int i; + memset(&fmt, 0, sizeof(struct wave_fmt)); + /* get RIFF chunk header */ if ((lseek(fd, 0, SEEK_SET) < 0) || ((read_bytes = read(fd, buf, 12)) < 12)) @@ -66,25 +138,31 @@ bool get_wave_metadata(int fd, struct mp3entry* id3) if (memcmp(buf, "fmt ", 4) == 0) { /* get rest of chunk */ - if ((read_bytes = read(fd, buf, 16)) < 16) + if (i < 16 || (read_bytes = read(fd, buf, i)) < i) return false; - i -= 16; - - /* skipping wFormatTag */ + /* wFormatTag */ + fmt.formattag = buf[0] | (buf[1] << 8); /* wChannels */ - channels = buf[2] | (buf[3] << 8); + fmt.channels = buf[2] | (buf[3] << 8); /* dwSamplesPerSec */ id3->frequency = get_long_le(&buf[4]); /* dwAvgBytesPerSec */ id3->bitrate = (get_long_le(&buf[8]) * 8) / 1000; - /* skipping wBlockAlign */ + /* wBlockAlign */ + fmt.blockalign = buf[12] | (buf[13] << 8); /* wBitsPerSample */ - bitspersample = buf[14] | (buf[15] << 8); + fmt.bitspersample = buf[14] | (buf[15] << 8); + if (i > 19) + { + /* wSamplesPerBlock */ + fmt.samplesperblock = buf[18] | (buf[19] << 8); + } + i = 0; } else if (memcmp(buf, "data", 4) == 0) { - numbytes = i; + fmt.numbytes = i; break; } else if (memcmp(buf, "fact", 4) == 0) @@ -109,16 +187,14 @@ bool get_wave_metadata(int fd, struct mp3entry* id3) return false; } - if ((numbytes == 0) || (channels == 0)) + if ((fmt.numbytes == 0) || (fmt.channels == 0) || (fmt.blockalign == 0)) { return false; } if (totalsamples == 0) { - /* for PCM only */ - totalsamples = numbytes - / ((((bitspersample - 1) / 8) + 1) * channels); + totalsamples = get_totalsamples(&fmt, id3); } id3->vbr = false; /* All WAV files are CBR */ diff --git a/apps/codecs/libpcms/SOURCES b/apps/codecs/libpcms/SOURCES new file mode 100644 index 0000000..c2e5871 --- /dev/null +++ b/apps/codecs/libpcms/SOURCES @@ -0,0 +1,10 @@ +linear_pcm.c +itut_g711.c +dvi_adpcm.c +ieee_float.c +dialogic_oki_adpcm.c +ms_adpcm.c +yamaha_adpcm.c +ima_adpcm_common.c +qt_ima_adpcm.c +swf_adpcm.c diff --git a/apps/codecs/libpcms/dialogic_oki_adpcm.c b/apps/codecs/libpcms/dialogic_oki_adpcm.c new file mode 100644 index 0000000..eebce96 --- /dev/null +++ b/apps/codecs/libpcms/dialogic_oki_adpcm.c @@ -0,0 +1,138 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ +#include "codeclib.h" +#include "pcm_common.h" + +/* + * Dialogic OKI ADPCM + * + * References + * [1] Dialogic Corporation, Dialogic ADPCM Algorithm, 1988 + * [2] MultimediaWiki, Dialogic IMA ADPCM, URL:http://wiki.multimedia.cx/index.php?title=Dialogic_IMA_ADPCM + * [3] sox source code, src/adpcms.c + * [4] Tetsuya Isaki, NetBSD:/sys/dev/audio.c, http://www.tri-tree.gr.jp/~isaki/NetBSD/src/sys/dev/ic/msm6258.c.html + */ + +static const uint16_t step_table[] ICONST_ATTR = { + 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, +}; + +static const int index_table[] ICONST_ATTR = { + -1, -1, -1, -1, 2, 4, 6, 8 +}; + +static int8_t index; +static int16_t pcmdata; + +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) + { + DEBUGF("CODEC_ERROR: dialogic oki adpcm must be 4 bitspersample %d\n", fmt->bitspersample); + return false; + } + + if (fmt->channels != 1) + { + DEBUGF("CODEC_ERROR: dialogic oki adpcm must be monaural\n"); + return false; + } + + /* blockalign = 2 samples */ + fmt->blockalign = 8; + + /* chunksize = about 1/50[sec] data */ + fmt->chunksize = (ci->id3->frequency / 100); + + return true; +} + +static uint32_t get_seek_pos(long seek_time) +{ + return ((uint64_t)(seek_time * ci->id3->frequency)) / 250LL; +} + +static int16_t create_pcmdata(uint8_t nibble) +{ + int16_t delta; + int16_t step = step_table[index]; + + delta = (step >> 3); + if (nibble & 4) delta += step; + if (nibble & 2) delta += (step >> 1); + if (nibble & 1) delta += (step >> 2); + + if (nibble & 0x08) + pcmdata -= delta; + else + pcmdata += delta; + + CLIP(pcmdata, -2048, 2047); + + index += index_table[nibble & 0x07]; + CLIP(index, 0, 48); + + return pcmdata; +} + +static int decode(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufcount) +{ + size_t nsamples = 0; + + while (inbufsize) + { + *outbuf++ = create_pcmdata(*inbuf >> 4) << 16; + *outbuf++ = create_pcmdata(*inbuf ) << 16; + nsamples += 2; + + inbuf++; + inbufsize--; + } + + *outbufcount = nsamples; + + return CODEC_OK; +} + +static const struct pcm_codec codec = { + set_format, + get_seek_pos, + decode, + }; + +const struct pcm_codec *get_dialogic_oki_adpcm_codec(void) +{ + /* initialize first pcm data, step index */ + pcmdata = 0; + index = 0; + + return &codec; +} diff --git a/apps/codecs/libpcms/dvi_adpcm.c b/apps/codecs/libpcms/dvi_adpcm.c new file mode 100644 index 0000000..9008fb6 --- /dev/null +++ b/apps/codecs/libpcms/dvi_adpcm.c @@ -0,0 +1,276 @@ +/*************************************************************************** + * __________ __ ___. + * 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" +#include "ima_adpcm_common.h" + +/* + * Intel DVI ADPCM (IMA ADPCM) + * + * References + * [1] The IMA Digital Audio Focus and Technical Working Groups, + * Recommended Practices for Enhancing Digital Audio Compatibility in Multimedia Systems + * Revision 3.00, 1992 + * [2] Microsoft Corporation, New Multimedia Data Types and Data Techniques, Revision:3 .0, 1994 + * [3] ffmpeg source code, libavcodec/adpcm.c + */ + +static struct pcm_format *fmt; + +static bool set_format(struct pcm_format *format, const unsigned char *fmtpos) +{ + fmt = format; + + (void)fmtpos; + + if (fmt->bitspersample < 2 || fmt->bitspersample > 5) + { + DEBUGF("CODEC_ERROR: dvi adpcm must be 2, 3, 4 or 5 bitspersample: %d\n", fmt->bitspersample); + 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; + + init_ima_adpcm_decoder(fmt->bitspersample, NULL); + return true; +} + +static uint32_t get_seek_pos(long seek_time) +{ + /* use avgbytespersec to round to the closest blockalign multiple, + add firstblockposn. 64-bit casts to avoid overflows. */ + return (((uint64_t)fmt->avgbytespersec*(seek_time - 1)) + / (1000LL*fmt->blockalign))*fmt->blockalign; +} + +static int decode(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufsize) +{ + int ch; + unsigned int i, j; + int samples; + unsigned int nblocks = fmt->chunksize / fmt->blockalign; + int32_t init_pcmdata[2]; + int8_t init_index[2]; + int32_t *pcmbuf; + const uint8_t *adpcmbuf; + + (void)inbufsize; + + *outbufsize = 0; + for (i = 0; i < nblocks; i++) + { + for (ch = 0; ch < fmt->channels; ch++) + { + init_pcmdata[ch] = inbuf[0] | (inbuf[1] << 8); + if (init_pcmdata[ch] > 32767) + init_pcmdata[ch] -= 65536; + + init_index[ch] = inbuf[2]; + if (init_index[ch] > 88 || init_index[ch] < 0) + { + DEBUGF("CODEC_ERROR: dvi adpcm illegal step index=%d > 88\n", init_index[ch]); + return CODEC_ERROR; + } + inbuf += 4; + + *outbuf++ = init_pcmdata[ch] << 13; + } + + *outbufsize += 1; + set_decode_parameters(fmt->channels, init_pcmdata, init_index); + + if (fmt->bitspersample == 4) + { + samples = fmt->blockalign / (4 * fmt->channels) - 1; + *outbufsize += (samples << 3); + while (samples-- > 0) + { + for (ch = 0; ch < fmt->channels; ch++) + { + pcmbuf = outbuf + ch; + for (j = 0; j < 4; j++) + { + *pcmbuf = create_pcmdata_adpcm_data_size4(ch, *inbuf ) << 13; + pcmbuf += fmt->channels; + *pcmbuf = create_pcmdata_adpcm_data_size4(ch, *inbuf >> 4) << 13; + pcmbuf += fmt->channels; + inbuf++; + } + } + outbuf += 8 * fmt->channels; + } + } + else if (fmt->bitspersample == 3) + { + uint32_t adpcms; + + samples = (fmt->blockalign - 4 * fmt->channels) / (12 * fmt->channels); + *outbufsize += (samples << 5); + while (samples--) + { + for (ch = 0; ch < fmt->channels; ch++) + { + adpcmbuf = inbuf + ch * 4; + pcmbuf = outbuf + ch; + adpcms = *adpcmbuf++; + adpcms |= (*adpcmbuf++) << 8; + adpcms |= (*adpcmbuf++) << 16; + for (j = 0; j < 8; j++) + { + *pcmbuf = create_pcmdata(ch, adpcms >> (3 * j)) << 13; + pcmbuf += fmt->channels; + } + adpcms = *adpcmbuf++; + adpcmbuf += (fmt->channels - 1) * 4; + adpcms |= (*adpcmbuf++) << 8; + adpcms |= (*adpcmbuf++) << 16; + for (j = 0; j < 8; j++) + { + *pcmbuf = create_pcmdata(ch, adpcms >> (3 * j)) << 13; + pcmbuf += fmt->channels; + } + adpcms = *adpcmbuf++; + adpcms |= (*adpcmbuf++) << 8; + adpcmbuf += (fmt->channels - 1) * 4; + adpcms |= (*adpcmbuf++) << 16; + for (j = 0; j < 8; j++) + { + *pcmbuf = create_pcmdata(ch, adpcms >> (3 * j)) << 13; + pcmbuf += fmt->channels; + } + adpcms = *adpcmbuf++; + adpcms |= (*adpcmbuf++) << 8; + adpcms |= (*adpcmbuf++) << 16; + for (j = 0; j < 8; j++) + { + *pcmbuf = create_pcmdata(ch, adpcms >> (3 * j)) << 13; + pcmbuf += fmt->channels; + } + } + outbuf += 32 * fmt->channels; + inbuf += 12 * fmt->channels; + } + } + else if (fmt->bitspersample == 5) + { + uint64_t adpcms; + + samples = (fmt->blockalign - 4 * fmt->channels) / (20 * fmt->channels); + *outbufsize += (samples << 5); + while (samples--) + { + for (ch = 0; ch < fmt->channels; ch++) + { + adpcmbuf = inbuf + ch * 4; + pcmbuf = outbuf + ch; + adpcms = *adpcmbuf++; + adpcms |= (*adpcmbuf++) << 8; + adpcms |= (*adpcmbuf++) << 16; + adpcms |= (uint64_t)(*adpcmbuf++) << 24; + adpcmbuf += (fmt->channels - 1) * 4; + adpcms |= (uint64_t)(*adpcmbuf++) << 32; + for (j = 0; j < 8; j++) + { + *pcmbuf = create_pcmdata(ch, adpcms >> (5 * j)) << 13; + pcmbuf += fmt->channels; + } + adpcms = *adpcmbuf++; + adpcms |= (*adpcmbuf++) << 8; + adpcms |= (*adpcmbuf++) << 16; + adpcmbuf += (fmt->channels - 1) * 4; + adpcms |= (uint64_t)(*adpcmbuf++) << 24; + adpcms |= (uint64_t)(*adpcmbuf++) << 32; + for (j = 0; j < 8; j++) + { + *pcmbuf = create_pcmdata(ch, adpcms >> (5 * j)) << 13; + pcmbuf += fmt->channels; + } + adpcms = *adpcmbuf++; + adpcms |= (*adpcmbuf++) << 8; + adpcmbuf += (fmt->channels - 1) * 4; + adpcms |= (*adpcmbuf++) << 16; + adpcms |= (uint64_t)(*adpcmbuf++) << 24; + adpcms |= (uint64_t)(*adpcmbuf++) << 32; + for (j = 0; j < 8; j++) + { + *pcmbuf = create_pcmdata(ch, adpcms >> (5 * j)) << 13; + pcmbuf += fmt->channels; + } + adpcms = *adpcmbuf++; + adpcmbuf += (fmt->channels - 1) * 4; + adpcms |= (*adpcmbuf++) << 8; + adpcms |= (*adpcmbuf++) << 16; + adpcms |= (uint64_t)(*adpcmbuf++) << 24; + adpcms |= (uint64_t)(*adpcmbuf++) << 32; + for (j = 0; j < 8; j++) + { + *pcmbuf = create_pcmdata(ch, adpcms >> (5 * j)) << 13; + pcmbuf += fmt->channels; + } + } + outbuf += 32 * fmt->channels; + inbuf += 20 * fmt->channels; + } + } + else /* fmt->bitspersample == 2 */ + { + samples = fmt->blockalign / (4 * fmt->channels) - 1; + *outbufsize += (samples << 4); + while (samples-- > 0) + { + for (ch = 0; ch < fmt->channels; ch++) + { + pcmbuf = outbuf + ch; + for (j = 0; j < 4; j++) + { + *pcmbuf = create_pcmdata(ch, *inbuf ) << 13; + pcmbuf += fmt->channels; + *pcmbuf = create_pcmdata(ch, *inbuf >> 2) << 13; + pcmbuf += fmt->channels; + *pcmbuf = create_pcmdata(ch, *inbuf >> 4) << 13; + pcmbuf += fmt->channels; + *pcmbuf = create_pcmdata(ch, *inbuf >> 6) << 13; + pcmbuf += fmt->channels; + inbuf++; + } + } + outbuf += 16 * fmt->channels; + } + } + } + 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/libpcms/ieee_float.c b/apps/codecs/libpcms/ieee_float.c new file mode 100644 index 0000000..a5c50e8 --- /dev/null +++ b/apps/codecs/libpcms/ieee_float.c @@ -0,0 +1,147 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ +#include "codeclib.h" +#include "pcm_common.h" + +/* + * IEEE float + */ + +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 && fmt->bitspersample != 64) + { + DEBUGF("CODEC_ERROR: ieee float must be 32 or 64 bitspersample %d\n", fmt->bitspersample); + return false; + } + + fmt->bytespersample = fmt->bitspersample >> 3; + fmt->blockalign = fmt->bytespersample; + + /* chunksize is computed so that one chunk is about 1/50s. */ + fmt->chunksize = (ci->id3->frequency * fmt->channels / 50) * fmt->blockalign; + + return true; +} + +static uint32_t get_seek_pos(long seek_time) +{ + uint32_t newpos; + + newpos = ((uint64_t)(seek_time * ci->id3->frequency * fmt->channels / 1000LL))*fmt->blockalign; + return newpos; +} + +static int decode(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufsize) +{ + uint32_t i; + int32_t pcm; + int16_t exp; + int sgn; + + if (fmt->bitspersample == 32) + { + for (i = 0; i < inbufsize; i += 4) + { + if (fmt->is_little_endian) + { + pcm = (inbuf[0]<<5)|(inbuf[1]<<13)|((inbuf[2]|0x80)<<21); + exp = ((inbuf[2]>>7)|((inbuf[3]&0x7f)<<1)) - 127; + sgn = inbuf[3] & 0x80; + } + else + { + pcm = (inbuf[3]<<5)|(inbuf[2]<<13)|((inbuf[1]|0x80)<<21); + exp = ((inbuf[1]>>7)|((inbuf[0]&0x7f)<<1)) - 127; + sgn = inbuf[0] & 0x80; + } + if (exp > -29 && exp < 0) + { + pcm >>= -exp; + if (sgn) + pcm = -pcm; + } + else if (exp < -28) + pcm = 0; + else + pcm = (sgn)?-(1<<28):(1<<28)-1; + + outbuf[i/4] = pcm; + inbuf += 4; + } + *outbufsize = inbufsize >> 2; + } + else + { + for (i = 0; i < inbufsize; i += 8) + { + if (fmt->is_little_endian) + { + pcm = inbuf[3]|(inbuf[4]<<8)|(inbuf[5]<<16)|(((inbuf[6]&0x0f)|0x10)<<24); + exp = (((inbuf[6]&0xf0)>>4)|((inbuf[7]&0x7f)<<4)) - 1023; + sgn = inbuf[7] & 0x80; + } + else + { + pcm = inbuf[4]|(inbuf[3]<<8)|(inbuf[2]<<16)|(((inbuf[1]&0x0f)|0x10)<<24); + exp = (((inbuf[1]&0xf0)>>4)|((inbuf[0]&0x7f)<<4)) - 1023; + sgn = inbuf[0] & 0x80; + } + if (exp > -29 && exp < 0) + { + pcm >>= -exp; + if (sgn) + pcm = -pcm; + } + else if (exp < -28) + pcm = 0; + else + pcm = (sgn)?-(1<<28):(1<<28)-1; + + outbuf[i/8] = pcm; + inbuf += 8; + } + *outbufsize = inbufsize >> 3; + } + + 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_ieee_float_codec(void) +{ + return &codec; +} diff --git a/apps/codecs/libpcms/ima_adpcm_common.c b/apps/codecs/libpcms/ima_adpcm_common.c new file mode 100644 index 0000000..62fec26 --- /dev/null +++ b/apps/codecs/libpcms/ima_adpcm_common.c @@ -0,0 +1,169 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ +#include "codeclib.h" +#include "pcm_common.h" +#include "ima_adpcm_common.h" + +/* + * Functions for IMA ADPCM and IMA ADPCM series format + * + * References + * [1] The IMA Digital Audio Focus and Technical Working Groups, + * Recommended Practices for Enhancing Digital Audio Compatibility in Multimedia Systems + * Revision 3.00, 1992 + * [2] Microsoft Corporation, New Multimedia Data Types and Data Techniques, Revision:3 .0, 1994 + * [3] ffmpeg source code, libavcodec/adpcm.c + */ + +/* step table */ +static const uint16_t step_table[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 +}; + +/* step index tables */ +static const int index_tables[4][16] ICONST_ATTR = { + /* adpcm data size is 2 */ + { -1, 2 }, + /* adpcm data size is 3 */ + { -1, -1, 1, 2 }, + /* adpcm data size is 4 */ + { -1, -1, -1, -1, 2, 4, 6, 8 }, + /* adpcm data size is 5 */ + { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }, +}; + +static int32_t pcmdata[2]; +static int8_t index[2]; + +static int adpcm_data_size; +static uint8_t step_mask; +static uint8_t step_sign_mask; +static int8_t step_shift; +static const int *use_index_table; + +/* + * Before first decoding, this function must be executed. + * + * params + * bit: adpcm data size (2 <= bit <= 5). + * index_table: step index table + * if index_table is null, then step index table is used index_tables[bit - 2]. + */ +void init_ima_adpcm_decoder(int bit, const int *index_table) +{ + adpcm_data_size = bit; + step_sign_mask = 1 << (adpcm_data_size - 1); + step_mask = step_sign_mask - 1; + step_shift = adpcm_data_size - 2; + if (index_table) + use_index_table = index_table; + else + use_index_table = index_tables[adpcm_data_size - 2]; +} + +/* + * When starting decoding for each block, this function must be executed. + * + * params + * channels: channel count + * init_pcmdata: array of init pcmdata + * init_index: array of init step indexes + */ +void set_decode_parameters(int channels, int32_t *init_pcmdata, int8_t *init_index) +{ + int ch; + + for (ch = 0; ch < channels; ch++) + { + pcmdata[ch] = init_pcmdata[ch]; + index[ch] = init_index[ch]; + } +} + +/* + * convert ADPCM to PCM for any adpcm data size. + * + * If adpcm_data_size is 4, then you use create_pcmdata_adpcm_data4() + * in place of this functon. + */ +int16_t create_pcmdata(int ch, uint8_t nibble) +{ + int check_bit = 1 << step_shift; + int32_t delta = 0; + int16_t step = step_table[index[ch]]; + + do { + if (nibble & check_bit) + delta += step; + step >>= 1; + check_bit >>= 1; + } while (check_bit); + delta += step; + + if (nibble & step_sign_mask) + pcmdata[ch] -= delta; + else + pcmdata[ch] += delta; + + index[ch] += use_index_table[nibble & step_mask]; + CLIP(index[ch], 0, 88); + + CLIP(pcmdata[ch], -32768, 32767); + + return (int16_t)pcmdata[ch]; +} + +/* + * convert ADPCM to PCM when adpcm data size is 4. + */ +int16_t create_pcmdata_adpcm_data_size4(int ch, uint8_t nibble) +{ + int32_t delta; + int16_t step = step_table[index[ch]]; + + delta = (step >> 3); + if (nibble & 4) delta += step; + if (nibble & 2) delta += (step >> 1); + if (nibble & 1) delta += (step >> 2); + + if (nibble & 0x08) + pcmdata[ch] -= delta; + else + pcmdata[ch] += delta; + + index[ch] += use_index_table[nibble & 0x07]; + CLIP(index[ch], 0, 88); + + CLIP(pcmdata[ch], -32768, 32767); + + return (int16_t)pcmdata[ch]; +} diff --git a/apps/codecs/libpcms/ima_adpcm_common.h b/apps/codecs/libpcms/ima_adpcm_common.h new file mode 100644 index 0000000..f6f4361 --- /dev/null +++ b/apps/codecs/libpcms/ima_adpcm_common.h @@ -0,0 +1,32 @@ +/*************************************************************************** + * __________ __ ___. + * 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_IMA_ADPCM_COMMON_H +#define CODEC_LIBPCMS_IMA_ADPCM_COMMON_H + +#include +#include +#include + +void init_ima_adpcm_decoder(int bit, const int *index_table); +void set_decode_parameters(int channels, int32_t *init_pcmdata, int8_t *init_index); +int16_t create_pcmdata(int ch, uint8_t nibble); +int16_t create_pcmdata_adpcm_data_size4(int ch, uint8_t nibble); +#endif diff --git a/apps/codecs/libpcms/itut_g711.c b/apps/codecs/libpcms/itut_g711.c new file mode 100644 index 0000000..1f23594 --- /dev/null +++ b/apps/codecs/libpcms/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/libpcms/libpcms.make b/apps/codecs/libpcms/libpcms.make new file mode 100644 index 0000000..d99dccf --- /dev/null +++ b/apps/codecs/libpcms/libpcms.make @@ -0,0 +1,25 @@ +# __________ __ ___. +# Open \______ \ ____ ____ | | _\_ |__ _______ ___ +# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +# \/ \/ \/ \/ \/ +# $Id$ +# + +# libpcms +PCMSLIB := $(CODECDIR)/libpcms.a +PCMSLIB_SRC := $(call preprocess, $(APPSDIR)/codecs/libpcms/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)/libpcms/%.o: $(ROOTDIR)/apps/codecs/libpcms/%.c + $(SILENT)mkdir -p $(dir $@) + $(call PRINTS,CC $(subst $(ROOTDIR)/,,$<))$(CC) $(PCMSFLAGS) -c $< -o $@ diff --git a/apps/codecs/libpcms/linear_pcm.c b/apps/codecs/libpcms/linear_pcm.c new file mode 100644 index 0000000..2c766bd --- /dev/null +++ b/apps/codecs/libpcms/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/libpcms/ms_adpcm.c b/apps/codecs/libpcms/ms_adpcm.c new file mode 100644 index 0000000..addcdf3 --- /dev/null +++ b/apps/codecs/libpcms/ms_adpcm.c @@ -0,0 +1,193 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ +#include "codeclib.h" +#include "pcm_common.h" + +/* + * Microsoft ADPCM + * + * References + * [1] Microsoft, New Multimedia Data Types and Data Techniques Revision 3.0, 1994 + * [2] MulitimediaWiki, Microsoft ADPCM, 2006 (http://wiki.multimedia.cx/index.php?title=Microsoft_ADPCM) + * [3] ffmpeg source code, libavcodec/adpcm.c + */ + +#define NUM_COEFF 7 +static int16_t coeffs[NUM_COEFF][2]; + +static int16_t dec_coeff[2][2]; +static uint16_t delta[2]; +static int16_t sample[2][2]; + +static struct pcm_format *fmt; + +static const int16_t adaptation_table[] ICONST_ATTR = { + 230, 230, 230, 230, 307, 409, 512, 614, + 768, 614, 512, 409, 307, 230, 230, 230 +}; + +static bool set_format(struct pcm_format *format, const unsigned char *fmtpos) +{ + int i; + int num; + int size; + + fmt = format; + + if (fmt->bitspersample != 4) + { + DEBUGF("CODEC_ERROR: microsoft adpcm must be 4 bitspersample %d\n", fmt->bitspersample); + return false; + } + + fmt->chunksize = fmt->blockalign; + + fmtpos += 4; /* skip 'fmt ' */ + size = fmtpos[0] | (fmtpos[1] << 8) | (fmtpos[1] << 16) | (fmtpos[1] << 24); + if (size < 50) + { + DEBUGF("CODEC_ERROR: microsoft adpcm 'fmt ' chunk size=%lu < 50\n", (unsigned long)size); + return false; + } + + /* get nNumCoef */ + fmtpos += 24; + num = fmtpos[0] | (fmtpos[1] << 8); + + /* + * In many case, nNumCoef is 7. + * Depending upon the encoder, as for this value there is a possibility of + * increasing more. + * If you found the file where this value exceeds 7, please report. + */ + if (num != NUM_COEFF) + { + DEBUGF("CODEC_ERROR: microsoft adpcm nNumCoef=%d != 7\n", num); + return false; + } + + /* get aCoeffs */ + fmtpos += 2; + for (i = 0; i < NUM_COEFF; i++) + { + coeffs[i][0] = fmtpos[0] | (SE(fmtpos[1]) << 8); + coeffs[i][1] = fmtpos[2] | (SE(fmtpos[3]) << 8); + fmtpos += 4; + } + + return true; +} + +static uint32_t get_seek_pos(long seek_time) +{ + return ((uint64_t)(seek_time * ci->id3->frequency * fmt->blockalign)) / + (2000LL * fmt->samplesperblock); +} + +static int16_t create_pcmdata(int ch, uint8_t nibble) +{ + int32_t pcmdata; + + pcmdata = (sample[ch][0] * dec_coeff[ch][0] + sample[ch][1] * dec_coeff[ch][1]) / 256; + pcmdata += (delta[ch] * (nibble - ((nibble & 0x8) << 1))); + + CLIP(pcmdata, -32768, 32767); + + sample[ch][1] = sample[ch][0]; + sample[ch][0] = pcmdata; + + delta[ch] = (adaptation_table[nibble] * delta[ch]) >> 8; + if (delta[ch] < 16) + delta[ch] = 16; + + return (int16_t)pcmdata; +} + +static int decode(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufcount) +{ + int ch; + size_t nsamples = 0; + int size = fmt->samplesperblock; + + /* read block header */ + for (ch = 0; ch < fmt->channels; ch++) + { + if (*inbuf >= NUM_COEFF) + { + DEBUGF("CODEC_ERROR: microsoft adpcm illegal initial coeff=%d > 7\n", *inbuf); + return CODEC_ERROR; + } + dec_coeff[ch][0] = coeffs[*inbuf][0]; + dec_coeff[ch][1] = coeffs[*inbuf][1]; + inbuf++; + } + + for (ch = 0; ch < fmt->channels; ch++) + { + delta[ch] = inbuf[0] | (SE(inbuf[1]) << 8); + inbuf += 2; + } + + for (ch = 0; ch < fmt->channels; ch++) + { + sample[ch][0] = inbuf[0] | (SE(inbuf[1]) << 8); + inbuf += 2; + } + + for (ch = 0; ch < fmt->channels; ch++) + { + sample[ch][1] = inbuf[0] | (SE(inbuf[1]) << 8); + inbuf += 2; + } + + inbufsize -= 7 * fmt->channels; + ch = fmt->channels - 1; + + while (size-- > 0) + { + *outbuf++ = create_pcmdata(0, *inbuf >> 4 ) << 13; + *outbuf++ = create_pcmdata(ch, *inbuf & 0xf) << 13; + nsamples += 2; + + inbuf++; + inbufsize--; + if (inbufsize <= 0) + break; + } + + if (fmt->channels == 2) + nsamples >>= 1; + *outbufcount = nsamples; + + return CODEC_OK; +} + +static const struct pcm_codec codec = { + set_format, + get_seek_pos, + decode, + }; + +const struct pcm_codec *get_ms_adpcm_codec(void) +{ + return &codec; +} diff --git a/apps/codecs/libpcms/pcm_common.h b/apps/codecs/libpcms/pcm_common.h new file mode 100644 index 0000000..a920c93 --- /dev/null +++ b/apps/codecs/libpcms/pcm_common.h @@ -0,0 +1,125 @@ +/*************************************************************************** + * __________ __ ___. + * 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) + +/* Macro that clipping data */ +#define CLIP(data, min, max) \ +if ((data) > (max)) data = max; \ +else if ((data) < (min)) data = min; + +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/libpcms/qt_ima_adpcm.c b/apps/codecs/libpcms/qt_ima_adpcm.c new file mode 100644 index 0000000..21dcba4 --- /dev/null +++ b/apps/codecs/libpcms/qt_ima_adpcm.c @@ -0,0 +1,118 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ +#include "codeclib.h" +#include "pcm_common.h" +#include "ima_adpcm_common.h" + +/* + * Apple QuickTime IMA ADPCM + * + * References + * [1] Multimedia Wiki, Apple QuickTime IMA ADPCM + * URL:http://wiki.multimedia.cx/index.php?title=Apple_QuickTime_IMA_ADPCM + * [2] Apple Inc., Technical Note TN1081 Understanding the Differences Between + * Apple and Windows IMA-ADPCM Compressed Sound Files, 1996 + * [3] ffmpeg source code, libavcodec/adpcm.c + */ + +static struct pcm_format *fmt; + +static bool set_format(struct pcm_format *format, const unsigned char *fmtpos) +{ + fmt = format; + + (void)fmtpos; + + /* 1 chunksize = about 1/50[s] data */ + fmt->blockalign = 34 * fmt->channels; + fmt->chunksize = (ci->id3->frequency/3200) * fmt->blockalign; + + init_ima_adpcm_decoder(4, NULL); + return true; +} + +static uint32_t get_seek_pos(long seek_time) +{ + return (((uint64_t)(seek_time * ci->id3->frequency)) / 64000LL) * fmt->blockalign; +} + +static int decode(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufcount) +{ + int ch; + size_t nsamples = 0; + int block_size; + int32_t *pcmbuf; + int32_t init_pcmdata; + int8_t init_index; + + while (inbufsize > 0) + { + /* read block header */ + for (ch = 0; ch < fmt->channels; ch++) + { + init_pcmdata = (inbuf[0] << 8)|(inbuf[1] & 0x80); + if (init_pcmdata > 32767) + init_pcmdata -= 65536; + + init_index = inbuf[1] & 0x7f; + if (init_index > 88) + { + DEBUGF("CODEC_ERROR: quicktime ima adpcm illegal step index=%d > 88\n", init_index); + return CODEC_ERROR; + } + + inbuf += 2; + inbufsize -= 2; + + set_decode_parameters(1, &init_pcmdata, &init_index); + + pcmbuf = outbuf + ch; + for (block_size = 32; block_size > 0 && inbufsize > 0; block_size--, inbufsize--) + { + *pcmbuf = create_pcmdata_adpcm_data_size4(ch, *inbuf ) << 13; + pcmbuf += fmt->channels; + *pcmbuf = create_pcmdata_adpcm_data_size4(ch, *inbuf >> 4) << 13; + pcmbuf += fmt->channels; + nsamples += 2; + inbuf++; + } + } + outbuf += 64 * fmt->channels; + } + + if (fmt->channels == 2) + nsamples >>= 1; + *outbufcount = nsamples; + + return CODEC_OK; +} + +static const struct pcm_codec codec = { + set_format, + get_seek_pos, + decode, + }; + +const struct pcm_codec *get_qt_ima_adpcm_codec(void) +{ + return &codec; +} diff --git a/apps/codecs/libpcms/support_formats.h b/apps/codecs/libpcms/support_formats.h new file mode 100644 index 0000000..d0faf27 --- /dev/null +++ b/apps/codecs/libpcms/support_formats.h @@ -0,0 +1,55 @@ +/*************************************************************************** + * __________ __ ___. + * 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 (IMA ADPCM) */ +const struct pcm_codec *get_dvi_adpcm_codec(void); + +/* IEEE float */ +const struct pcm_codec *get_ieee_float_codec(void); + +/* Microsoft ADPCM */ +const struct pcm_codec *get_ms_adpcm_codec(void); + +/* Dialogic OKI ADPCM */ +const struct pcm_codec *get_dialogic_oki_adpcm_codec(void); + +/* YAMAHA ADPCM */ +const struct pcm_codec *get_yamaha_adpcm_codec(void); + +/* Apple QuickTime IMA ADPCM */ +const struct pcm_codec *get_qt_ima_adpcm_codec(void); + +/* Adobe SWF ADPCM */ +const struct pcm_codec *get_swf_adpcm_codec(void); +#endif diff --git a/apps/codecs/libpcms/swf_adpcm.c b/apps/codecs/libpcms/swf_adpcm.c new file mode 100644 index 0000000..cfbc63b --- /dev/null +++ b/apps/codecs/libpcms/swf_adpcm.c @@ -0,0 +1,198 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ +#include "codeclib.h" +#include "pcm_common.h" +#include "ima_adpcm_common.h" + +/* + * Adobe SWF ADPCM + * + * References + * [1] Adobe, SWF File Format Specification Version 10, 2008 + * [2] Jack Jansen, adpcm.c in adpcm.zip + * [3] ffmpeg source code, libavcodec/adpcm.c + */ + +/* step index table when bitspersample is 3. + * (when bitspersample is 2, 4, 5, step index table uses the table + * which is defined ima_adpcm_common.c.) + */ +static const int index_table[4] ICONST_ATTR = { + -1, -1, 2, 4, +}; + +static int validity_bits = 8; +static bool first_block = true; +static int blockbits = 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 < 2 || fmt->bitspersample > 5) + { + DEBUGF("CODEC_ERROR: swf adpcm must be 2, 3, 4 or 5 bitspersample %d\n", fmt->bitspersample); + return false; + } + + if (fmt->samplesperblock == 0) + fmt->samplesperblock = (((fmt->blockalign << 3) - 2) / fmt->channels - 22) + / fmt->bitspersample + 1; + + if (fmt->samplesperblock > 4096) + { + DEBUGF("CODEC_ERROR: swf adpcm samplesperblock is too large: %d\n", fmt->samplesperblock); + return false; + } + + blockbits = ((fmt->samplesperblock - 1) * fmt->bitspersample + 22) * fmt->channels; + + /* 1 chunksize = about 93 [ms] data (frequency:44.1kHz, 4096 [sample/block]) */ + fmt->chunksize = (blockbits + 9) >> 3; + + /* initialize for ima adpcm common functions */ + if (fmt->bitspersample == 3) + init_ima_adpcm_decoder(fmt->bitspersample, index_table); + else + init_ima_adpcm_decoder(fmt->bitspersample, NULL); + + return true; +} + +static uint32_t get_seek_pos(long seek_time) +{ + return ((((uint64_t)(seek_time * ci->id3->frequency * fmt->blockalign)) / + (1000LL * fmt->samplesperblock)) * blockbits + 9) >> 3; +} + +static uint8_t get_data(const uint8_t **buf, int bit) +{ + uint8_t res = 0; + uint8_t mask = (1 << bit) - 1; + + if (validity_bits >= bit) + { + validity_bits -= bit; + return (**buf >> validity_bits) & mask; + } + + if (validity_bits > 0) + res = **buf << (bit - validity_bits); + + validity_bits += 8 - bit; + res = (res | (*(++(*buf)) >> validity_bits)) & mask; + return res; +} + +static int decode(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufcount) +{ + int ch; + int adpcm_code_size; + int count = fmt->samplesperblock; + int32_t init_pcmdata[2]; + int8_t init_index[2]; + + static uint8_t lastbyte = 0; + static int lastbytebits = 0; + + (void)inbufsize; + + validity_bits = 8; + + /* read block header */ + ch = fmt->channels - 1; + if (first_block) + { + adpcm_code_size = get_data(&inbuf, 2) + 2; + if (fmt->bitspersample != adpcm_code_size) + { + DEBUGF("CODEC_ERROR: swf adpcm different adpcm code size=%d != %d\n", + adpcm_code_size, fmt->bitspersample); + return CODEC_ERROR; + } + init_pcmdata[0] = (get_data(&inbuf, 8) << 8) | get_data(&inbuf, 8); + + lastbytebits = 0; + first_block = false; + } + else + { + if (lastbytebits > 0) + init_pcmdata[0] = ((lastbyte << (8 + lastbytebits)) | + (get_data(&inbuf, 8) << lastbytebits) | + get_data(&inbuf, lastbytebits)) & 65535; + else + init_pcmdata[0] = (get_data(&inbuf, 8) << 8) | get_data(&inbuf, 8); + } + + init_index[0] = get_data(&inbuf, 6); + if (init_pcmdata[0] > 32767) + init_pcmdata[0] -= 65536; + + if (ch > 0) + { + init_pcmdata[1] = (get_data(&inbuf, 8) << 8) | get_data(&inbuf, 8); + init_index[1] = get_data(&inbuf, 6); + if (init_pcmdata[1] > 32767) + init_pcmdata[1] -= 65536; + } + + *outbuf++ = init_pcmdata[0] << 13; + if (ch > 0) + *outbuf++ = init_pcmdata[1] << 13; + + set_decode_parameters(fmt->channels, init_pcmdata, init_index); + + while (--count > 0) + { + *outbuf++ = create_pcmdata(0, get_data(&inbuf, fmt->bitspersample)) << 13; + if (ch > 0) + *outbuf++ = create_pcmdata(ch, get_data(&inbuf, fmt->bitspersample)) << 13; + } + + *outbufcount = fmt->samplesperblock; + + lastbyte = *inbuf; + lastbytebits = (8 - validity_bits) & 0x07; + + /* calculates next read bytes */ + fmt->chunksize = (blockbits - validity_bits + 7) >> 3; + + return CODEC_OK; +} + +static const struct pcm_codec codec = { + set_format, + get_seek_pos, + decode, + }; + +const struct pcm_codec *get_swf_adpcm_codec(void) +{ + first_block = true; + + return &codec; +} diff --git a/apps/codecs/libpcms/yamaha_adpcm.c b/apps/codecs/libpcms/yamaha_adpcm.c new file mode 100644 index 0000000..ed539e3 --- /dev/null +++ b/apps/codecs/libpcms/yamaha_adpcm.c @@ -0,0 +1,204 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ +#include "codeclib.h" +#include "pcm_common.h" + +/* + * YAMAHA ADPCM + * + * References + * [1] YAMAHA, YAMAHA ADPCM ACM Driver Version 1.0.0.0, 2005 + * [2] BlendWorks, YM2608 ADPCM, http://web.archive.org/web/20050208190547/www.memb.jp/~dearna/ma/ym2608/adpcm.html + * [3] Naoyuki Sawa, ADPCM no shikumi #1, http://www.piece-me.org/piece-lab/adpcm/adpcm1.html + * [4] ffmpeg source code, libavcodec/adpcm.c + */ + +/* ADPCM data block layout + * + * encoding by YAMAHA ADPCM ACM Driver + * blockAlign = (frequency / 60 + 4) * channels. + * + * block + * (channels = 1) + * int16_t first value (Little endian) + * uint16_t first predictor (Little endian) + * uint8_t ADPCM data (1st data: 0-3 bit, 2nd data: 4-7 bit) + * .... + * + * (channels = 2) + * int16_t Left channel first value (Little endian) + * uint16_t Left channel first predictor (Little endian) + * int16_t Right channel first value (Little endian) + * uint16_t Right channel first predictor (Little endian) + * uint8_t ADPCM data (Left channel data: 0-3 bit, Right channel data: 4-7 bit) + * .... + * + * encoding by ffmpeg + * blockAlign = 8000 + * + * block + * (channels = 1) + * uint8_t ADPCM data (1st data: 0-3 bit, 2nd data: 4-7 bit) + * .... + * + * (channels = 2) + * uint8_t ADPCM data (Left channel data: 0-3 bit, Right channel data: 4-7 bit) + * .... + */ + +static const int amplification_table[] ICONST_ATTR = { + 230, 230, 230, 230, 307, 409, 512, 614, 230, 230, 230, 230, 307, 409, 512, 614 +}; + +static uint32_t step[2] = {127, 127}; +static int32_t pcmdata[2] = {0, 0}; + +static bool has_block_header = false; + +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) + { + DEBUGF("CODEC_ERROR: yamaha adpcm must be 4 bitspersample %d\n", fmt->bitspersample); + return false; + } + + /* check exists block header */ + if (fmt->blockalign == ((ci->id3->frequency / 60) + 4) * fmt->channels) + { + has_block_header = true; + + /* chunksize = about 1/30 [sec] data */ + fmt->chunksize = fmt->blockalign; + } + else + { + has_block_header = false; + + /* blockalign = 2 * channels samples */ + fmt->blockalign = 8 * fmt->channels; + + /* chunksize = about 1/50[sec] data */ + fmt->chunksize = (ci->id3->frequency / 100) * fmt->channels; + } + + return true; +} + +static uint32_t get_seek_pos(long seek_time) +{ + uint32_t newpos = 0; + + if (has_block_header) + { + /* blockalign = 1/30 [sec] data = (frequency / 30) samples */ + newpos = (seek_time * 3/100) * fmt->blockalign; + } + else + { + /* blockalign = 2 * channels samples */ + newpos = ((uint64_t)(seek_time * ci->id3->frequency * fmt->channels)) / 250LL; + } + + return newpos; +} + +static int16_t create_pcmdata(int ch, uint8_t nibble) +{ + int32_t delta = 0; + + delta = step[ch] >> 3; + if (nibble & 4) delta += step[ch]; + if (nibble & 2) delta += (step[ch] >> 1); + if (nibble & 1) delta += (step[ch] >> 2); + + if (nibble & 0x08) + pcmdata[ch] -= delta; + else + pcmdata[ch] += delta; + + CLIP(pcmdata[ch], -32768, 32767); + + step[ch] = (step[ch] * amplification_table[nibble & 0x07]) >> 8; + CLIP(step[ch], 127, 24576); + + return (int16_t)pcmdata[ch]; +} + +static int decode(const uint8_t *inbuf, size_t inbufsize, + int32_t *outbuf, int *outbufcount) +{ + int ch; + size_t nsamples = 0; + + /* parse block header */ + if (has_block_header) + { + for (ch = 0; ch < fmt->channels; ch++) + { + pcmdata[ch] = inbuf[0] | (SE(inbuf[1]) << 8); + step[ch] = inbuf[2] | (inbuf[3] << 8); + + inbuf += 4; + inbufsize -= 4; + } + } + + ch = fmt->channels - 1; + while (inbufsize) + { + *outbuf++ = create_pcmdata(0, *inbuf ) << 13; + *outbuf++ = create_pcmdata(ch, *inbuf >> 4) << 13; + nsamples += 2; + + inbuf++; + inbufsize--; + } + + if (fmt->channels == 2) + nsamples >>= 1; + *outbufcount = nsamples; + + return CODEC_OK; +} + +static const struct pcm_codec codec = { + set_format, + get_seek_pos, + decode, + }; + +const struct pcm_codec *get_yamaha_adpcm_codec(void) +{ + /* initialize first step, pcm data */ + step[0] = 127; + step[1] = 127; + pcmdata[0] = 0; + pcmdata[1] = 0; + + return &codec; +} diff --git a/apps/codecs/snd.c b/apps/codecs/snd.c new file mode 100644 index 0000000..3ff5f3d --- /dev/null +++ b/apps/codecs/snd.c @@ -0,0 +1,297 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ + +#include "codeclib.h" +#include "inttypes.h" +#include "codecs/libpcms/support_formats.h" + +CODEC_HEADER + +/* Sun Audio file (Au file format) codec + * + * References + * [1] Sun Microsystems, Inc., Header file for Audio, .au, 1992 + * URL http://www.opengroup.org/public/pubs/external/auformat.html + * [2] Wikipedia, Au file format, URL: http://en.wikipedia.org/wiki/Sun_Audio + */ + +static int32_t samples[PCM_CHUNK_SIZE] IBSS_ATTR; + +enum +{ + SND_FORMAT_UNSUPPORT = 0, /* unsupported format */ + SND_FORMAT_MULAW, /* G.711 MULAW */ + SND_FORMAT_PCM, /* Linear PCM */ + SND_FORMAT_IEEE_FLOAT, /* IEEE float */ + SND_FORMAT_ALAW, /* G.711 ALAW */ +}; + +static int support_formats[28][2] = { + { SND_FORMAT_UNSUPPORT, 0 }, + { SND_FORMAT_MULAW, 8 }, /* G.711 MULAW */ + { SND_FORMAT_PCM, 8 }, /* 8bit */ + { SND_FORMAT_PCM, 16 }, /* 16bit */ + { SND_FORMAT_PCM, 24 }, /* 24bit */ + { SND_FORMAT_PCM, 32 }, /* 32bit */ + { SND_FORMAT_IEEE_FLOAT, 32 }, /* 32bit */ + { SND_FORMAT_IEEE_FLOAT, 64 }, /* 64bit */ + { SND_FORMAT_UNSUPPORT, 0 }, /* Fragmented sample data */ + { SND_FORMAT_UNSUPPORT, 0 }, /* DSP program */ + { SND_FORMAT_UNSUPPORT, 0 }, /* 8bit fixed point */ + { SND_FORMAT_UNSUPPORT, 0 }, /* 16bit fixed point */ + { SND_FORMAT_UNSUPPORT, 0 }, /* 24bit fixed point */ + { SND_FORMAT_UNSUPPORT, 0 }, /* 32bit fixed point */ + { SND_FORMAT_UNSUPPORT, 0 }, + { SND_FORMAT_UNSUPPORT, 0 }, + { SND_FORMAT_UNSUPPORT, 0 }, + { SND_FORMAT_UNSUPPORT, 0 }, + { SND_FORMAT_UNSUPPORT, 0 }, /* 16bit linear with emphasis */ + { SND_FORMAT_UNSUPPORT, 0 }, /* 16bit linear compressed */ + { SND_FORMAT_UNSUPPORT, 0 }, /* 16bit linear with emphasis and compression */ + { SND_FORMAT_UNSUPPORT, 0 }, /* Music kit DSP commands */ + { SND_FORMAT_UNSUPPORT, 0 }, + { SND_FORMAT_UNSUPPORT, 0 }, /* G.721 MULAW */ + { SND_FORMAT_UNSUPPORT, 0 }, /* G.722 */ + { SND_FORMAT_UNSUPPORT, 0 }, /* G.723 3bit */ + { SND_FORMAT_UNSUPPORT, 0 }, /* G.723 5bit */ + { SND_FORMAT_ALAW, 8 }, /* G.711 ALAW */ +}; + +const struct pcm_entry snd_codecs[] = { + { SND_FORMAT_MULAW, get_itut_g711_mulaw_codec }, + { SND_FORMAT_PCM, get_linear_pcm_codec }, + { SND_FORMAT_IEEE_FLOAT, get_ieee_float_codec }, + { SND_FORMAT_ALAW, get_itut_g711_alaw_codec }, +}; + +#define NUM_FORMATS 4 + +static const struct pcm_codec *get_snd_codec(uint32_t formattag) +{ + int i; + + for (i = 0; i < NUM_FORMATS; i++) + { + if (snd_codecs[i].format_tag == formattag) + { + if (snd_codecs[i].get_codec) + return snd_codecs[i].get_codec(); + return 0; + } + } + return 0; +} + +static unsigned int get_be32(uint8_t *buf) +{ + return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; +} + +static int convert_snd_format(unsigned int encoding, struct pcm_format *fmt) +{ + if (encoding > 27) + { + fmt->formattag = SND_FORMAT_UNSUPPORT; + fmt->bitspersample = 0; + } + else + { + fmt->formattag = support_formats[encoding][0]; + fmt->bitspersample = support_formats[encoding][1]; + } + + return fmt->formattag; +} + +/* this is the codec entry point */ +enum codec_status codec_main(void) +{ + int status = CODEC_OK; + struct pcm_format format; + uint32_t bytesdone, decodedbytes; + size_t n; + int bufcount; + int endofstream; + unsigned char *buf; + uint8_t *sndbuf; + off_t firstblockposn; /* position of the first block in file */ + const struct pcm_codec *codec; + int offset = 0; + + /* Generic codec initialisation */ + ci->configure(DSP_SET_SAMPLE_DEPTH, 28); + +next_track: + if (codec_init()) { + DEBUGF("codec_init() error\n"); + status = CODEC_ERROR; + goto exit; + } + + while (!*ci->taginfo_ready && !ci->stop_codec) + ci->sleep(1); + + codec_set_replaygain(ci->id3); + + ci->memset(&format, 0, sizeof(struct pcm_format)); + format.is_signed = true; + format.is_little_endian = false; + + /* get header */ + buf = ci->request_buffer(&n, 24); + if (n < 24 || (memcmp(buf, ".snd", 4) != 0)) + { + /* no header */ + offset = 0; + format.formattag = SND_FORMAT_MULAW; + format.channels = 1; + format.bitspersample = 8; + format.numbytes = ci->id3->filesize; + } + else + { + /* data offset */ + offset = get_be32(buf + 4); + if (offset < 24) + { + DEBUGF("CODEC_ERROR: snd offset size is small %d\n", offset); + status = CODEC_ERROR; + goto done; + } + /* data size */ + format.numbytes = get_be32(buf + 8); + if (format.numbytes == (uint32_t)0xffffffff) + format.numbytes = ci->id3->filesize - offset; + /* encoding */ + format.formattag = convert_snd_format(get_be32(buf + 12), &format); + if (format.formattag == SND_FORMAT_UNSUPPORT) + { + DEBUGF("CODEC_ERROR: snd unsupport format %d\n", get_be32(buf + 12)); + status = CODEC_ERROR; + goto done; + } + /* skip sample rate */ + format.channels = get_be32(buf + 20); + if (format.channels == 0) { + DEBUGF("CODEC_ERROR: 0-channels file\n"); + status = CODEC_ERROR; + goto done; + } + } + + /* advance to first WAVE chunk */ + ci->advance_buffer(offset); + + firstblockposn = offset; + + decodedbytes = 0; + codec = 0; + bytesdone = 0; + + format.avgbytespersec = ci->id3->frequency * format.bitspersample * format.channels / 8; + format.blockalign = format.bitspersample * format.channels / 8; + + /* get codec */ + codec = get_snd_codec(format.formattag); + if (!codec) + { + DEBUGF("CODEC_ERROR: unsupport snd format %lx\n", format.formattag); + status = CODEC_ERROR; + goto done; + } + + if (!codec->set_format(&format, 0)) + { + status = CODEC_ERROR; + goto done; + } + + if (format.numbytes == 0) { + DEBUGF("CODEC_ERROR: data size is 0\n"); + status = CODEC_ERROR; + goto done; + } + + ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); + if (format.channels == 2) { + ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); + } else if (format.channels == 1) { + ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); + } else { + DEBUGF("CODEC_ERROR: more than 2 channels\n"); + status = CODEC_ERROR; + goto done; + } + + /* The main decoder loop */ + endofstream = 0; + + while (!endofstream) { + ci->yield(); + if (ci->stop_codec || ci->new_track) { + break; + } + + if (ci->seek_time) { + 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(); + } + + sndbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize); + if (n == 0) + break; /* End of stream */ + if (bytesdone + n > format.numbytes) { + n = format.numbytes - bytesdone; + endofstream = 1; + } + + status = codec->decode(sndbuf, 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; + decodedbytes += bufcount; + + if (bytesdone >= format.numbytes) + endofstream = 1; + ci->set_elapsed(decodedbytes*1000LL/ci->id3->frequency); + } + status = CODEC_OK; + +done: + if (ci->request_next_track()) + goto next_track; + +exit: + return status; +} diff --git a/apps/metadata/snd.c b/apps/metadata/snd.c new file mode 100644 index 0000000..983a53d --- /dev/null +++ b/apps/metadata/snd.c @@ -0,0 +1,122 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ +#include +#include +#include +#include +#include + +#include "system.h" +#include "metadata.h" +#include "metadata_common.h" +#include "metadata_parsers.h" +#include "logf.h" + +static int bitspersamples[28] = { + 0, + 8, /* G.711 MULAW */ + 8, /* 8bit */ + 16, /* 16bit */ + 24, /* 24bit */ + 32, /* 32bit */ + 32, /* 32bit */ + 64, /* 64bit */ + 0, /* Fragmented sample data */ + 0, /* DSP program */ + 0, /* 8bit fixed point */ + 0, /* 16bit fixed point */ + 0, /* 24bit fixed point */ + 0, /* 32bit fixed point */ + 0, + 0, + 0, + 0, + 0, /* 16bit linear with emphasis */ + 0, /* 16bit linear compressed */ + 0, /* 16bit linear with emphasis and compression */ + 0, /* Music kit DSP commands */ + 0, + 0, /* G.721 MULAW */ + 0, /* G.722 */ + 0, /* G.723 3bit */ + 0, /* G.723 5bit */ + 8, /* G.711 ALAW */ +}; + +static int get_snd_bitspersample(unsigned int encoding) +{ + if (encoding > 27) + return 0; + return bitspersamples[encoding]; +} + +bool get_snd_metadata(int fd, struct mp3entry* id3) +{ + /* Use the trackname part of the id3 structure as a temporary buffer */ + unsigned char* buf = (unsigned char *)id3->path; + unsigned long totalsamples = 0; + unsigned long channels = 0; + unsigned long bitspersample = 0; + unsigned long numbytes = 0; + int read_bytes; + int offset; + + id3->vbr = false; /* All WAV files are CBR */ + id3->filesize = filesize(fd); + + if ((lseek(fd, 0, SEEK_SET) < 0) || ((read_bytes = read(fd, buf, 24)) < 0)) + return false; + + if (read_bytes < 24 || (memcmp(buf, ".snd", 4) != 0)) + { + /* no header */ + numbytes = id3->filesize; + bitspersample = 8; + id3->frequency = 8000; + channels = 1; + } + else + { + /* data offset */ + offset = get_long_be(buf + 4); + if (offset < 24) + { + DEBUGF("CODEC_ERROR: snd offset size is small %d\n", offset); + return false; + } + /* data size */ + numbytes = get_long_be(buf + 8); + if (numbytes == (uint32_t)0xffffffff) + numbytes = id3->filesize - offset; + /* bitspersample */ + bitspersample = get_snd_bitspersample(get_long_be(buf + 12)); + /* sample rate */ + id3->frequency = get_long_be(buf + 16); + channels = get_long_be(buf + 20); + } + + totalsamples = numbytes / ((((bitspersample - 1) / 8) + 1) * channels); + + /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */ + id3->length = ((int64_t) totalsamples * 1000) / id3->frequency; + + return true; +}