From 182d6b4edeecb14ea0564823bd7f6f613abca46c Mon Sep 17 00:00:00 2001 From: dave Date: Wed, 1 Feb 2006 16:42:02 +0000 Subject: [PATCH] Patch #1421483 - AIFF codec by Jvo Studer git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8524 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/Makefile | 1 + apps/codecs/SOURCES | 1 + apps/codecs/aiff.c | 314 ++++++++++++++++++++++++++++++++++++++++++++++++++ apps/metadata.c | 81 ++++++++++++- apps/playback.c | 5 + apps/tree.c | 4 +- docs/CREDITS | 1 + firmware/export/id3.h | 1 + firmware/id3.c | 1 + 9 files changed, 407 insertions(+), 2 deletions(-) create mode 100644 apps/codecs/aiff.c diff --git a/apps/codecs/Makefile b/apps/codecs/Makefile index d0cd97db9..915e92227 100644 --- a/apps/codecs/Makefile +++ b/apps/codecs/Makefile @@ -63,6 +63,7 @@ $(OBJDIR)/wavpack.elf: $(OBJDIR)/wavpack.o $(CODECDEPS) $(BUILDDIR)/libwavpack.a $(OBJDIR)/alac.elf: $(OBJDIR)/alac.o $(CODECDEPS) $(BUILDDIR)/libalac.a $(BUILDDIR)/libm4a.a $(OBJDIR)/aac.elf: $(OBJDIR)/aac.o $(CODECDEPS) $(BUILDDIR)/libfaad.a $(BUILDDIR)/libm4a.a $(OBJDIR)/shorten.elf: $(OBJDIR)/shorten.o $(CODECDEPS) $(BUILDDIR)/libffmpegFLAC.a +$(OBJDIR)/aiff.elf: $(OBJDIR)/aiff.o $(CODECDEPS) $(OBJDIR)/%.elf: $(OBJDIR)/%.o $(CODECDEPS) $(ELFIT) diff --git a/apps/codecs/SOURCES b/apps/codecs/SOURCES index 911ee3b70..05172d6da 100644 --- a/apps/codecs/SOURCES +++ b/apps/codecs/SOURCES @@ -11,4 +11,5 @@ alac.c aac.c #endif shorten.c +aiff.c #endif diff --git a/apps/codecs/aiff.c b/apps/codecs/aiff.c new file mode 100644 index 000000000..091e621bb --- /dev/null +++ b/apps/codecs/aiff.c @@ -0,0 +1,314 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (c) 2005 Jvo Studer + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include "codeclib.h" +#include "inttypes.h" + +CODEC_HEADER + +struct codec_api* rb; + +/* This codec supports AIFF files with the following formats: + * - PCM, 8 and 16 bits, mono or stereo + */ + +enum +{ + AIFF_FORMAT_PCM = 0x0001, /* AIFF PCM Format (big endian) */ + IEEE_FORMAT_FLOAT = 0x0003, /* IEEE Float */ + AIFF_FORMAT_ALAW = 0x0004, /* AIFC ALaw compressed */ + AIFF_FORMAT_ULAW = 0x0005 /* AIFC uLaw compressed */ +}; + +/* Maximum number of bytes to process in one iteration */ +/* for 44.1kHz stereo 16bits, this represents 0.023s ~= 1/50s */ +#define AIF_CHUNK_SIZE (1024*2) + +#ifdef USE_IRAM +extern char iramcopy[]; +extern char iramstart[]; +extern char iramend[]; +extern char iedata[]; +extern char iend[]; +#endif + +static int16_t int16_samples[AIF_CHUNK_SIZE] IBSS_ATTR; + + +/* this is the codec entry point */ +enum codec_status codec_start(struct codec_api* api) +{ + struct codec_api* ci; + uint32_t numbytes, bytesdone; + uint16_t numChannels = 0; + uint32_t numSampleFrames = 0; + uint16_t sampleSize = 0; + uint32_t sampleRate = 0; + uint32_t i; + size_t n, aifbufsize; + int endofstream; + unsigned char* buf; + uint16_t* aifbuf; + long chunksize; + uint32_t offset2snd = 0; + uint16_t blockSize = 0; + uint32_t avgbytespersec = 0; + off_t firstblockposn; /* position of the first block in file */ + int shortorlong = 1; /* do we output shorts (1) or longs (2)? */ + int32_t * const int32_samples = (int32_t*)int16_samples; + + /* Generic codec initialisation */ + rb = api; + ci = api; + +#ifdef USE_IRAM + ci->memcpy(iramstart, iramcopy, iramend-iramstart); + ci->memset(iedata, 0, iend - iedata); +#endif + + ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512)); + ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*256)); + + ci->configure(DSP_DITHER, (bool *)false); + + next_track: + + if (codec_init(api)) { + i = CODEC_ERROR; + goto exit; + } + + while (!*ci->taginfo_ready) + ci->yield(); + + /* assume the AIFF header is less than 1024 bytes */ + buf=ci->request_buffer((long *)&n,1024); + if (n<44) { + i = CODEC_ERROR; + goto exit; + } + if ((memcmp(buf,"FORM",4)!=0) || (memcmp(&buf[8],"AIFF",4)!=0)) { + i = CODEC_ERROR; + goto exit; + } + + buf += 12; + n -= 12; + numbytes = 0; + + /* read until 'SSND' chunk, which typically is last */ + while(numbytes == 0 && n >= 8) { + /* chunkSize */ + i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]); + if (memcmp(buf,"COMM",4)==0) { + if (i != 18) { + DEBUGF("CODEC_ERROR: 'COMM' chunk size=%lu != 18\n",i); + i = CODEC_ERROR; + goto exit; + } + /* numChannels */ + numChannels = ((buf[8]<<8)|buf[9]); + /* numSampleFrames */ + numSampleFrames = ((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)|buf[13]); + /* sampleSize */ + sampleSize = ((buf[14]<<8)|buf[15]); + /* sampleRate (don't use last 4 bytes, only integer fs) */ + if (buf[16] != 0x40) { + DEBUGF("CODEC_ERROR: wierd sampling rate (no @)\n",i); + i = CODEC_ERROR; + goto exit; + } + sampleRate = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21])+1; + sampleRate = sampleRate >> (16+14-buf[17]); + /* calc average bytes per second */ + avgbytespersec = sampleRate*numChannels*sampleSize/8; + } + else if (memcmp(buf,"SSND",4)==0) { + if (sampleSize == 0) { + DEBUGF("CODEC_ERROR: unsupported chunk order\n"); + i = CODEC_ERROR; + goto exit; + } + /* offset2snd */ + offset2snd = ((buf[8]<<8)|buf[9]); + /* blockSize */ + blockSize = ((buf[10]<<8)|buf[11]); + if (blockSize == 0) + blockSize = numChannels*sampleSize; + numbytes = i-8-offset2snd; + i = 8+offset2snd; /* advance to the beginning of data */ + } + else { + DEBUGF("unsupported AIFF chunk: '%c%c%c%c', size=%lu\n", + buf[0], buf[1], buf[2], buf[3], i); + } + + if (i & 0x01) /* odd chunk sizes must be padded */ + i++; + buf += i+8; + if (n < (i+8)) { + DEBUGF("CODEC_ERROR: AIFF header size > 1024\n"); + i = CODEC_ERROR; + goto exit; + } + n -= i+8; + } /* while 'SSND' */ + + if (numChannels == 0) { + DEBUGF("CODEC_ERROR: 'COMM' chunk not found or 0-channels file\n"); + i = CODEC_ERROR; + goto exit; + } + if (numbytes == 0) { + DEBUGF("CODEC_ERROR: 'SSND' chunk not found or has zero length\n"); + i = CODEC_ERROR; + goto exit; + } + if (sampleSize > 24) { + DEBUGF("CODEC_ERROR: PCM with more than 24 bits per sample " + "is unsupported\n"); + i = CODEC_ERROR; + goto exit; + } + + ci->configure(CODEC_DSP_ENABLE, (bool *)true); + ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency)); + + if (sampleSize <= 16) { + ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(16)); + } else { + shortorlong = 2; + ci->configure(DSP_DITHER, (bool *)false); + ci->configure(DSP_SET_SAMPLE_DEPTH, (long *) (32)); + ci->configure(DSP_SET_CLIP_MAX, (long *) (2147483647)); + ci->configure(DSP_SET_CLIP_MIN, (long *) (-2147483647-1)); + } + + if (numChannels == 2) { + ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED); + } else if (numChannels == 1) { + ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_MONO); + } else { + DEBUGF("CODEC_ERROR: more than 2 channels unsupported\n"); + i = CODEC_ERROR; + goto exit; + } + + firstblockposn = (1024-n); + ci->advance_buffer(firstblockposn); + + /* The main decoder loop */ + + bytesdone=0; + ci->set_elapsed(0); + endofstream=0; + /* chunksize is computed so that one chunk is about 1/50s. + * this make 4096 for 44.1kHz 16bits stereo. + * It also has to be a multiple of blockalign */ + chunksize = (1 + avgbytespersec / (50*blockSize)) * blockSize; + /* check that the output buffer is big enough (convert to samplespersec, + then round to the blockSize multiple below) */ + if (((uint64_t)chunksize*ci->id3->frequency*numChannels*shortorlong) + / (uint64_t)avgbytespersec >= AIF_CHUNK_SIZE) { + chunksize = ((uint64_t)AIF_CHUNK_SIZE * avgbytespersec + / ((uint64_t)ci->id3->frequency * numChannels * shortorlong + * blockSize)) * blockSize; + } + + while (!endofstream) { + uint8_t *aifbuf8; + + ci->yield(); + if (ci->stop_codec || ci->reload_codec) { + 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*blockSize)) * blockSize; + if (newpos > numbytes) + break; + if (ci->seek_buffer(firstblockposn + newpos)) { + bytesdone = newpos; + } + ci->seek_complete(); + } + aifbuf=ci->request_buffer((long *)&n,chunksize); + aifbuf8 = (uint8_t*)aifbuf; + + if (n==0) + break; /* End of stream */ + + if (bytesdone + n > numbytes) { + n = numbytes - bytesdone; + endofstream = 1; + } + + aifbufsize = sizeof(int16_samples); + + if (sampleSize > 24) { + for (i=0;i 16) { + for (i=0;i 8) { + /* copy data. */ + for (i=0;ipcmbuf_insert((char*)int16_samples, aifbufsize)) { + ci->yield(); + } + + ci->advance_buffer(n); + bytesdone += n; + if (bytesdone >= numbytes) { + endofstream=1; + } + + ci->set_elapsed(bytesdone*1000LL/avgbytespersec); + } + + if (ci->request_next_track()) + goto next_track; + + i = CODEC_OK; +exit: + return i; +} + diff --git a/apps/metadata.c b/apps/metadata.c index 41ea0196c..531969b8a 100644 --- a/apps/metadata.c +++ b/apps/metadata.c @@ -78,6 +78,8 @@ static const struct format_list formats[] = { AFMT_ALAC, "m4a" }, { AFMT_AAC, "mp4" }, { AFMT_SHN, "shn" }, + { AFMT_AIFF, "aif" }, + { AFMT_AIFF, "aiff" }, }; static const unsigned short a52_bitrates[] = @@ -894,7 +896,6 @@ static bool get_wave_metadata(int fd, struct mp3entry* id3) } - static bool get_m4a_metadata(int fd, struct mp3entry* id3) { unsigned char* buf; @@ -1245,6 +1246,76 @@ static bool get_musepack_metadata(int fd, struct mp3entry *id3) return true; } +static bool get_aiff_metadata(int fd, struct mp3entry* id3) +{ + /* Use the trackname part of the id3 structure as a temporary buffer */ + unsigned char* buf = id3->path; + unsigned long numChannels = 0; + unsigned long numSampleFrames = 0; + unsigned long sampleSize = 0; + unsigned long sampleRate = 0; + unsigned long numbytes = 0; + int read_bytes; + int i; + + if ((lseek(fd, 0, SEEK_SET) < 0) + || ((read_bytes = read(fd, buf, sizeof(id3->path))) < 44)) + { + return false; + } + + if ((memcmp(buf, "FORM",4) != 0) + || (memcmp(&buf[8], "AIFF", 4) !=0 )) + { + return false; + } + + buf += 12; + read_bytes -= 12; + + while ((numbytes == 0) && (read_bytes >= 8)) + { + /* chunkSize */ + i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]); + + if (memcmp(buf, "COMM", 4) == 0) + { + /* numChannels */ + numChannels = ((buf[8]<<8)|buf[9]); + /* numSampleFrames */ + numSampleFrames =((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)|buf[13]); + /* sampleSize */ + sampleSize = ((buf[14]<<8)|buf[15]); + /* sampleRate */ + sampleRate = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21]); + sampleRate = sampleRate >> (16+14-buf[17]); + /* save format infos */ + id3->bitrate = (sampleSize * numChannels * sampleRate) / 1000; + id3->frequency = sampleRate; + id3->length = (numSampleFrames / id3->frequency) * 1000; + id3->vbr = false; /* AIFF files are CBR */ + id3->filesize = filesize(fd); + } + else if (memcmp(buf, "SSND", 4) == 0) + { + numbytes = i - 8; + } + + if (i & 0x01) + { + i++; /* odd chunk sizes must be padded */ + } + buf += i + 8; + read_bytes -= i + 8; + } + + if ((numbytes == 0) || (numChannels == 0)) + { + return false; + } + return true; +} + /* Simple file type probing by looking at the filename extension. */ static unsigned int probe_file_format(const char *filename) { @@ -1448,6 +1519,14 @@ bool get_metadata(struct track_info* track, int fd, const char* trackname, /* TODO: read the id3v2 header if it exists */ break; + case AFMT_AIFF: + if (!get_aiff_metadata(fd, &(track->id3))) + { + return false; + } + + break; + default: /* If we don't know how to read the metadata, assume we can't play the file */ diff --git a/apps/playback.c b/apps/playback.c index 22ee3362c..5ed6c5e00 100644 --- a/apps/playback.c +++ b/apps/playback.c @@ -81,6 +81,7 @@ static volatile bool paused; #define CODEC_ALAC "/.rockbox/codecs/alac.codec" #define CODEC_AAC "/.rockbox/codecs/aac.codec" #define CODEC_SHN "/.rockbox/codecs/shorten.codec" +#define CODEC_AIFF "/.rockbox/codecs/aiff.codec" #define AUDIO_DEFAULT_FIRST_LIMIT (1024*1024*10) #define AUDIO_FILL_CYCLE (1024*256) @@ -950,6 +951,10 @@ static bool loadcodec(bool start_play) logf("Codec: SHN"); codec_path = CODEC_SHN; break; + case AFMT_AIFF: + logf("Codec: PCM AIFF"); + codec_path = CODEC_AIFF; + break; default: logf("Codec: Unsupported"); codec_path = NULL; diff --git a/apps/tree.c b/apps/tree.c index 15624d761..74c1059a6 100644 --- a/apps/tree.c +++ b/apps/tree.c @@ -85,7 +85,7 @@ const struct filetype filetypes[] = { { "ogg", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, { "wma", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, { "wav", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, - { "flac", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, + { "flac",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, { "ac3", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, { "a52", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, { "mpc", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, @@ -93,6 +93,8 @@ const struct filetype filetypes[] = { { "m4a", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, { "mp4", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, { "shn", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, + { "aif", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, + { "aiff",TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA }, #endif { "m3u", TREE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, { "cfg", TREE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG }, diff --git a/docs/CREDITS b/docs/CREDITS index 6343447ca..76fd64935 100644 --- a/docs/CREDITS +++ b/docs/CREDITS @@ -161,3 +161,4 @@ Tomasz Malesinski Andrew Pilley Matt v.d. Westhuizen Tim Crist +Jvo Studer diff --git a/firmware/export/id3.h b/firmware/export/id3.h index 5090534e9..38494044f 100644 --- a/firmware/export/id3.h +++ b/firmware/export/id3.h @@ -41,6 +41,7 @@ enum { AFMT_ALAC, /* Apple Lossless Audio Codec */ AFMT_AAC, /* Advanced Audio Coding (AAC) in M4A container */ AFMT_SHN, /* Shorten */ + AFMT_AIFF, /* Audio Interchange File Format */ /* New formats must be added to the end of this list */ diff --git a/firmware/id3.c b/firmware/id3.c index a2b6521b6..fcca99266 100644 --- a/firmware/id3.c +++ b/firmware/id3.c @@ -102,6 +102,7 @@ static const char* const codec_labels[] = { "ALAC", /* Apple Lossless Audio Codec */ "AAC", /* Advanced Audio Coding in M4A container */ "SHN", /* Shorten */ + "AIFF", /* Audio Interchange File Format */ #endif }; -- 2.11.4.GIT