summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Chapman <dave@dchapman.com>2005-09-22 21:55:37 +0000
committerDave Chapman <dave@dchapman.com>2005-09-22 21:55:37 +0000
commit139c1cb82491886f600ef5014b79acb49f2c510c (patch)
tree4d65d0037983b78007919268fe04b9c56438458e
parent8026f0fe05cafc2adfb75b90e9bd65b9ea1f02cf (diff)
downloadrockbox-139c1cb82491886f600ef5014b79acb49f2c510c.zip
rockbox-139c1cb82491886f600ef5014b79acb49f2c510c.tar.gz
rockbox-139c1cb82491886f600ef5014b79acb49f2c510c.tar.bz2
rockbox-139c1cb82491886f600ef5014b79acb49f2c510c.tar.xz
First version of ALAC (Apple Lossless) decoder
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7547 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/FILES1
-rw-r--r--apps/codecs/Makefile13
-rw-r--r--apps/codecs/SOURCES1
-rw-r--r--apps/codecs/alac.c388
-rw-r--r--apps/metadata.c338
-rw-r--r--apps/playback.c5
-rw-r--r--apps/tree.c1
7 files changed, 744 insertions, 3 deletions
diff --git a/apps/FILES b/apps/FILES
index c38ece1..07719a4 100644
--- a/apps/FILES
+++ b/apps/FILES
@@ -54,6 +54,7 @@ codecs/dumb/src/core/*
codecs/dumb/src/helpers/*
codecs/dumb/src/it/*
codecs/libmusepack/*
+codecs/libalac/*
codecs/lib/*.[ch]
codecs/lib/Makefile
codecs/lib/SOURCES
diff --git a/apps/codecs/Makefile b/apps/codecs/Makefile
index abd108f..8f869b3 100644
--- a/apps/codecs/Makefile
+++ b/apps/codecs/Makefile
@@ -17,7 +17,7 @@ ifdef APPEXTRA
endif
ifdef SOFTWARECODECS
- CODECLIBS = -lmad -la52 -lFLAC -lTremor -lwavpack -lmusepack
+ CODECLIBS = -lmad -la52 -lFLAC -lTremor -lwavpack -lmusepack -lalac
endif
# we "borrow" the plugin LDS file
@@ -39,7 +39,7 @@ DIRS = .
CODECDEPS = $(LINKCODEC) $(BUILDDIR)/libcodec.a
-.PHONY: libmad liba52 libFLAC libTremor libwavpack dumb libmusepack
+.PHONY: libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac
OUTPUT = $(SOFTWARECODECS)
@@ -60,6 +60,7 @@ $(OBJDIR)/vorbis.elf: $(OBJDIR)/vorbis.o $(CODECDEPS) $(BUILDDIR)/libTremor.a
$(OBJDIR)/mpc.elf: $(OBJDIR)/mpc.o $(CODECDEPS) $(BUILDDIR)/libmusepack.a
$(OBJDIR)/wav.elf: $(OBJDIR)/wav.o $(CODECDEPS)
$(OBJDIR)/wavpack.elf: $(OBJDIR)/wavpack.o $(CODECDEPS) $(BUILDDIR)/libwavpack.a
+$(OBJDIR)/alac.elf: $(OBJDIR)/alac.o $(CODECDEPS) $(BUILDDIR)/libalac.a
$(OBJDIR)/%.elf: $(OBJDIR)/%.o $(CODECDEPS)
$(ELFIT)
@@ -152,14 +153,20 @@ libmusepack:
@mkdir -p $(OBJDIR)/libmusepack
@$(MAKE) -C libmusepack OBJDIR=$(OBJDIR)/libmusepack OUTPUT=$(BUILDDIR)/libmusepack.a
+libalac:
+ @echo "MAKE in libalac"
+ @mkdir -p $(OBJDIR)/libalac
+ @$(MAKE) -C libalac OBJDIR=$(OBJDIR)/libalac OUTPUT=$(BUILDDIR)/libalac.a
+
clean:
@echo "cleaning codecs"
- $(SILENT)rm -fr $(OBJDIR)/libmad $(BUILDDIR)/libmad.a $(OBJDIR)/liba52 $(OBJDIR)/libFLAC $(OBJDIR)/Tremor $(OBJDIR)/libwavpack $(OBJDIR)/dumb $(BUILDDIR)/libdumb.a $(BUILDDIR)/libdumbd.a $(OBJDIR)/libmusepack $(BUILDDIR)/libmusepack.a
+ $(SILENT)rm -fr $(OBJDIR)/libmad $(BUILDDIR)/libmad.a $(OBJDIR)/liba52 $(OBJDIR)/libFLAC $(OBJDIR)/Tremor $(OBJDIR)/libwavpack $(OBJDIR)/dumb $(BUILDDIR)/libdumb.a $(BUILDDIR)/libdumbd.a $(OBJDIR)/libmusepack $(BUILDDIR)/libmusepack.a $(OBJDIR)/libalac $(BUILDDIR)/libalac.a
@$(MAKE) -C libmad clean OBJDIR=$(OBJDIR)/libmad
@$(MAKE) -C liba52 clean OBJDIR=$(OBJDIR)/liba52
@$(MAKE) -C libFLAC clean OBJDIR=$(OBJDIR)/libFLAC
@$(MAKE) -C Tremor clean OBJDIR=$(OBJDIR)/Tremor
@$(MAKE) -C libwavpack clean OBJDIR=$(OBJDIR)/libwavpack
@$(MAKE) -C libmusepack clean OBJDIR=$(OBJDIR)/libmusepack
+ @$(MAKE) -C libalac clean OBJDIR=$(OBJDIR)/libalac
@$(MAKE) -C dumb clean OBJDIR=$(OBJDIR)/dumb
@$(MAKE) -C lib clean OBJDIR=$(OBJDIR)/lib
diff --git a/apps/codecs/SOURCES b/apps/codecs/SOURCES
index 14a053b..14cf847 100644
--- a/apps/codecs/SOURCES
+++ b/apps/codecs/SOURCES
@@ -6,4 +6,5 @@ wav.c
a52.c
mpc.c
wavpack.c
+alac.c
#endif
diff --git a/apps/codecs/alac.c b/apps/codecs/alac.c
new file mode 100644
index 0000000..f00ae97
--- /dev/null
+++ b/apps/codecs/alac.c
@@ -0,0 +1,388 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2005 Dave Chapman
+ *
+ * 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 "codec.h"
+
+#include <codecs/libalac/demux.h>
+#include <codecs/libalac/decomp.h>
+#include <codecs/libalac/stream.h>
+
+#include "playback.h"
+#include "dsp.h"
+#include "lib/codeclib.h"
+
+#ifndef SIMULATOR
+extern char iramcopy[];
+extern char iramstart[];
+extern char iramend[];
+#endif
+
+#define destBufferSize (1024*16)
+
+char inputBuffer[1024*32]; /* Input buffer */
+size_t mdat_offset;
+struct codec_api* rb;
+struct codec_api* ci;
+
+/* Implementation of the stream.h functions used by libalac */
+
+#define _Swap32(v) do { \
+ v = (((v) & 0x000000FF) << 0x18) | \
+ (((v) & 0x0000FF00) << 0x08) | \
+ (((v) & 0x00FF0000) >> 0x08) | \
+ (((v) & 0xFF000000) >> 0x18); } while(0)
+
+#define _Swap16(v) do { \
+ v = (((v) & 0x00FF) << 0x08) | \
+ (((v) & 0xFF00) >> 0x08); } while (0)
+
+/* A normal read without any byte-swapping */
+void stream_read(stream_t *stream, size_t size, void *buf)
+{
+ ci->read_filebuf(buf,size);
+ if (ci->curpos >= ci->filesize) { stream->eof=1; }
+}
+
+int32_t stream_read_int32(stream_t *stream)
+{
+ int32_t v;
+ stream_read(stream, 4, &v);
+#ifdef ROCKBOX_LITTLE_ENDIAN
+ _Swap32(v);
+#endif
+ return v;
+}
+
+uint32_t stream_read_uint32(stream_t *stream)
+{
+ uint32_t v;
+ stream_read(stream, 4, &v);
+#ifdef ROCKBOX_LITTLE_ENDIAN
+ _Swap32(v);
+#endif
+ return v;
+}
+
+int16_t stream_read_int16(stream_t *stream)
+{
+ int16_t v;
+ stream_read(stream, 2, &v);
+#ifdef ROCKBOX_LITTLE_ENDIAN
+ _Swap16(v);
+#endif
+ return v;
+}
+
+uint16_t stream_read_uint16(stream_t *stream)
+{
+ uint16_t v;
+ stream_read(stream, 2, &v);
+#ifdef ROCKBOX_LITTLE_ENDIAN
+ _Swap16(v);
+#endif
+ return v;
+}
+
+int8_t stream_read_int8(stream_t *stream)
+{
+ int8_t v;
+ stream_read(stream, 1, &v);
+ return v;
+}
+
+uint8_t stream_read_uint8(stream_t *stream)
+{
+ uint8_t v;
+ stream_read(stream, 1, &v);
+ return v;
+}
+
+void stream_skip(stream_t *stream, size_t skip)
+{
+ (void)stream;
+ ci->advance_buffer(skip);
+}
+
+int stream_eof(stream_t *stream)
+{
+ return stream->eof;
+}
+
+void stream_create(stream_t *stream)
+{
+ stream->eof=0;
+}
+
+/* This function was part of the original alac decoder implementation */
+
+static int get_sample_info(demux_res_t *demux_res, uint32_t samplenum,
+ uint32_t *sample_duration,
+ uint32_t *sample_byte_size)
+{
+ unsigned int duration_index_accum = 0;
+ unsigned int duration_cur_index = 0;
+
+ if (samplenum >= demux_res->num_sample_byte_sizes) {
+ return 0;
+ }
+
+ if (!demux_res->num_time_to_samples) {
+ return 0;
+ }
+
+ while ((demux_res->time_to_sample[duration_cur_index].sample_count
+ + duration_index_accum) <= samplenum) {
+ duration_index_accum +=
+ demux_res->time_to_sample[duration_cur_index].sample_count;
+
+ duration_cur_index++;
+ if (duration_cur_index >= demux_res->num_time_to_samples) {
+ return 0;
+ }
+ }
+
+ *sample_duration =
+ demux_res->time_to_sample[duration_cur_index].sample_duration;
+ *sample_byte_size = demux_res->sample_byte_size[samplenum];
+
+ return 1;
+}
+
+/* Seek to sample_loc (or close to it). Return 1 on success (and
+ modify samplesdone and currentblock), 0 if failed
+
+ Seeking uses the following two arrays:
+
+ 1) the sample_byte_size array contains the length in bytes of
+ each block ("sample" in Applespeak).
+
+ 2) the time_to_sample array contains the duration (in samples) of
+ each block of data.
+
+ So we just find the block number we are going to seek to (using
+ time_to_sample) and then find the offset in the file (using
+ sample_byte_size).
+
+ Each ALAC block seems to be independent of all the others.
+ */
+
+static unsigned int alac_seek (demux_res_t* demux_res,
+ unsigned int sample_loc,
+ size_t* samplesdone, int* currentblock)
+{
+ int flag;
+ unsigned int i,j;
+ unsigned int newblock;
+ unsigned int newsample;
+ unsigned int newpos;
+
+ /* First check we have the appropriate metadata - we should always
+ have it. */
+ if ((demux_res->num_time_to_samples==0) ||
+ (demux_res->num_sample_byte_sizes==0)) { return 0; }
+
+ /* Find the destination block from time_to_sample array */
+ i=0;
+ newblock=0;
+ newsample=0;
+ flag=0;
+
+ while ((i<demux_res->num_time_to_samples) && (flag==0) &&
+ (newsample < sample_loc)) {
+ j=(sample_loc-newsample) /
+ demux_res->time_to_sample[i].sample_duration;
+
+ if (j <= demux_res->time_to_sample[i].sample_count) {
+ newblock+=j;
+ newsample+=j*demux_res->time_to_sample[i].sample_duration;
+ flag=1;
+ } else {
+ newsample+=(demux_res->time_to_sample[i].sample_duration
+ * demux_res->time_to_sample[i].sample_count);
+ newblock+=demux_res->time_to_sample[i].sample_count;
+ i++;
+ }
+ }
+
+ /* We know the new block, now calculate the file position */
+ newpos=mdat_offset;
+ for (i=0;i<newblock;i++) {
+ newpos+=demux_res->sample_byte_size[i];
+ }
+
+ /* We know the new file position, so let's try to seek to it */
+ if (ci->seek_buffer(newpos)) {
+ *samplesdone=newsample;
+ *currentblock=newblock;
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+/* this is the codec entry point */
+enum codec_status codec_start(struct codec_api* api)
+{
+ size_t n;
+ demux_res_t demux_res;
+ static stream_t input_stream;
+ uint32_t samplesdone;
+ uint32_t elapsedtime;
+ uint32_t sample_duration;
+ uint32_t sample_byte_size;
+ int outputBytes;
+ unsigned int i;
+ unsigned char* buffer;
+ alac_file alac;
+ int16_t* pDestBuffer;
+
+ /* Generic codec initialisation */
+ TEST_CODEC_API(api);
+
+ rb = api;
+ ci = (struct codec_api*)api;
+
+#ifndef SIMULATOR
+ rb->memcpy(iramstart, iramcopy, iramend-iramstart);
+#endif
+
+ ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*10));
+ ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512));
+ ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*128));
+
+ ci->configure(DSP_DITHER, (bool *)false);
+ ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED);
+ ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(16));
+
+ next_track:
+
+ if (codec_init(api)) {
+ LOGF("ALAC: Error initialising codec\n");
+ return CODEC_ERROR;
+ }
+
+ while (!rb->taginfo_ready)
+ rb->yield();
+
+ if (rb->id3->frequency != NATIVE_FREQUENCY) {
+ rb->configure(DSP_SET_FREQUENCY, (long *)(rb->id3->frequency));
+ rb->configure(CODEC_DSP_ENABLE, (bool *)true);
+ } else {
+ rb->configure(CODEC_DSP_ENABLE, (bool *)false);
+ }
+
+ stream_create(&input_stream);
+
+ /* if qtmovie_read returns successfully, the stream is up to
+ * the movie data, which can be used directly by the decoder */
+ if (!qtmovie_read(&input_stream, &demux_res)) {
+ LOGF("ALAC: Error initialising file\n");
+ return CODEC_ERROR;
+ }
+
+ /* Keep track of start of stream in file - used for seeking */
+ mdat_offset=ci->curpos;
+
+ /* initialise the sound converter */
+ create_alac(demux_res.sample_size, demux_res.num_channels,&alac);
+ alac_set_info(&alac, demux_res.codecdata);
+
+ i=0;
+ samplesdone=0;
+ /* The main decoding loop */
+ while (i < demux_res.num_sample_byte_sizes) {
+ rb->yield();
+ if (ci->stop_codec || ci->reload_codec) {
+ break;
+ }
+
+ /* Deal with any pending seek requests */
+ if (ci->seek_time) {
+ if (alac_seek(&demux_res,
+ (ci->seek_time/10) * (ci->id3->frequency/100),
+ &samplesdone, &i)) {
+ elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
+ ci->set_elapsed(elapsedtime);
+ }
+ ci->seek_time = 0;
+ }
+
+ /* Lookup the length (in samples and bytes) of block i */
+ if (!get_sample_info(&demux_res, i, &sample_duration,
+ &sample_byte_size)) {
+ LOGF("ALAC: Error in get_sample_info\n");
+ return CODEC_ERROR;
+ }
+
+ /* Request the required number of bytes from the input buffer */
+
+ buffer=ci->request_buffer(&n,sample_byte_size);
+ if (n!=sample_byte_size) {
+ /* The decode_frame function requires the whole frame, so if we
+ can't get it contiguously from the buffer, then we need to
+ copy it via a read - i.e. we are at the buffer wraparound
+ point */
+
+ /* Check we estimated the maximum buffer size correctly */
+ if (sample_byte_size > sizeof(inputBuffer)) {
+ LOGF("ALAC: Input buffer < %d bytes\n",sample_byte_size);
+ return CODEC_ERROR;
+ }
+
+ n=ci->read_filebuf(inputBuffer,sample_byte_size);
+ if (n!=sample_byte_size) {
+ LOGF("ALAC: Error reading data\n");
+ return CODEC_ERROR;
+ }
+ buffer=inputBuffer;
+ }
+
+ /* Decode one block - returned samples will be host-endian */
+ outputBytes = destBufferSize;
+ rb->yield();
+ pDestBuffer=decode_frame(&alac, buffer, &outputBytes);
+
+ /* Advance codec buffer - unless we did a read */
+ if ((char*)buffer!=(char*)inputBuffer) {
+ ci->advance_buffer(n);
+ }
+
+ /* Output the audio */
+ rb->yield();
+ while(!ci->pcmbuf_insert((char*)pDestBuffer,outputBytes))
+ rb->yield();
+
+ /* Update the elapsed-time indicator */
+ samplesdone+=sample_duration;
+ elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
+ ci->set_elapsed(elapsedtime);
+
+ /* Keep track of current position - for resuming */
+ ci->set_offset(elapsedtime);
+
+ i++;
+ }
+
+ LOGF("ALAC: Decoded %d samples\n",samplesdone);
+
+ if (ci->request_next_track())
+ goto next_track;
+
+ return CODEC_OK;
+}
diff --git a/apps/metadata.c b/apps/metadata.c
index 2883b01..b8fbd65 100644
--- a/apps/metadata.c
+++ b/apps/metadata.c
@@ -75,6 +75,7 @@ static const struct format_list formats[] =
{ AFMT_A52, "a52" },
{ AFMT_A52, "ac3" },
{ AFMT_WAVPACK, "wv" },
+ { AFMT_ALAC, "m4a" },
};
static const unsigned short a52_bitrates[] =
@@ -180,6 +181,30 @@ static void convert_endian(void *data, const char *format)
}
}
+/* read_uint32be() - read an unsigned integer from a big-endian
+ (e.g. Quicktime) file. This is used by the .m4a parser
+*/
+#ifdef ROCKBOX_BIG_ENDIAN
+#define read_uint32be(fd,buf) read((fd),(buf),4)
+#else
+int read_uint32be(int fd, unsigned int* buf) {
+ char tmp;
+ char* p=(char*)buf;
+ size_t n;
+
+ n=read(fd,tmp,4);
+ if (n==4) {
+ tmp=p[0];
+ p[0]=p[3];
+ p[1]=p[2];
+ p[2]=p[1];
+ p[3]=tmp;
+ }
+
+ return(n);
+}
+#endif
+
/* Read an unaligned 32-bit little endian long from buffer. */
static unsigned long get_long(void* buf)
{
@@ -264,6 +289,37 @@ static void convert_utf8(char* utf8)
*dest = 0;
}
+
+/* Read a string tag from an M4A file */
+void read_m4a_tag_string(int fd, int len,char** bufptr,size_t* bytes_remaining, char** dest)
+{
+ int data_length;
+
+ if (bytes_remaining==0) {
+ lseek(fd,len,SEEK_CUR); /* Skip everything */
+ } else {
+ /* Skip the data tag header - maybe we should parse it properly? */
+ lseek(fd,16,SEEK_CUR);
+ len-=16;
+
+ *dest=*bufptr;
+ if ((size_t)len+1 > *bytes_remaining) {
+ read(fd,*bufptr,*bytes_remaining-1);
+ lseek(fd,len-(*bytes_remaining-1),SEEK_CUR);
+ *bufptr+=(*bytes_remaining-1);
+ } else {
+ read(fd,*bufptr,len);
+ *bufptr+=len;
+ }
+ **bufptr=(char)0;
+
+ convert_utf8(*dest);
+ data_length = strlen(*dest)+1;
+ *bufptr=(*dest)+data_length;
+ *bytes_remaining-=data_length;
+ }
+}
+
/* Parse the tag (the name-value pair) and fill id3 and buffer accordingly.
* String values to keep are written to buf. Returns number of bytes written
* to buf (including end nil).
@@ -887,6 +943,280 @@ static bool get_wave_metadata(int fd, struct mp3entry* id3)
}
+
+static bool get_alac_metadata(int fd, struct mp3entry* id3)
+{
+ unsigned char* buf;
+ unsigned long totalsamples;
+ int i,j,k;
+ size_t n;
+ size_t bytes_remaining;
+ char* id3buf;
+ unsigned int compressedsize;
+ unsigned int sample_count;
+ unsigned int sample_duration;
+ int numentries;
+ int entry_size;
+ int size_remaining;
+ int chunk_len;
+ unsigned char chunk_id[4];
+ int sub_chunk_len;
+ unsigned char sub_chunk_id[4];
+
+ /* A simple parser to read vital metadata from an ALAC file.
+ This parser also works for AAC files - they are both stored in
+ a Quicktime M4A container. */
+
+ /* Use the trackname part of the id3 structure as a temporary buffer */
+ buf=id3->path;
+
+ lseek(fd, 0, SEEK_SET);
+
+ totalsamples=0;
+ compressedsize=0;
+ /* read the chunks - we stop when we find the mdat chunk and set compressedsize */
+ while (compressedsize==0) {
+ n=read_uint32be(fd,&chunk_len);
+
+ // This means it was a 64-bit file, so we have problems.
+ if (chunk_len == 1) {
+ logf("need 64bit support\n");
+ return false;
+ }
+
+ n=read(fd,&chunk_id,4);
+ if (memcmp(&chunk_id,"ftyp",4)==0) {
+ /* Check for M4A type */
+ n=read(fd,&chunk_id,4);
+ if (memcmp(&chunk_id,"M4A ",4)!=0) {
+ logf("Not an M4A file, aborting\n");
+ return false;
+ }
+ /* Skip rest of chunk */
+ lseek(fd, chunk_len - 8 - 4, SEEK_CUR); /* FIXME not 8 */
+ } else if (memcmp(&chunk_id,"moov",4)==0) {
+ size_remaining=chunk_len - 8; /* FIXME not 8 */
+
+ while (size_remaining > 0) {
+ n=read_uint32be(fd,&sub_chunk_len);
+ if ((sub_chunk_len < 1) || (sub_chunk_len > size_remaining)) {
+ logf("Strange sub_chunk_len value inside moov: %d (remaining: %d)\n",sub_chunk_len,size_remaining);
+ return false;
+ }
+ n=read(fd,&sub_chunk_id,4);
+ size_remaining-=8;
+
+ if (memcmp(&sub_chunk_id,"mvhd",4)==0) {
+ /* We don't need anything from here - skip */
+ lseek(fd, sub_chunk_len - 8, SEEK_CUR); /* FIXME not 8 */
+ size_remaining-=(sub_chunk_len-8);
+ } else if (memcmp(&sub_chunk_id,"udta",4)==0) {
+ /* The udta chunk contains the metadata - track, artist, album etc.
+ The format appears to be:
+ udta
+ meta
+ hdlr
+ ilst
+ .nam
+ [rest of tags]
+ free
+
+ NOTE: This code was written by examination of some .m4a files
+ produced by iTunes v4.9 - it may not therefore be 100%
+ compliant with all streams. But it should fail gracefully.
+ */
+ j=(sub_chunk_len-8);
+ size_remaining-=j;
+ n=read_uint32be(fd,&sub_chunk_len);
+ n=read(fd,&sub_chunk_id,4);
+ j-=8;
+ if (memcmp(&sub_chunk_id,"meta",4)==0) {
+ lseek(fd, 4, SEEK_CUR);
+ j-=4;
+ n=read_uint32be(fd,&sub_chunk_len);
+ n=read(fd,&sub_chunk_id,4);
+ j-=8;
+ if (memcmp(&sub_chunk_id,"hdlr",4)==0) {
+ lseek(fd, sub_chunk_len - 8, SEEK_CUR);
+ j-=(sub_chunk_len - 8);
+ n=read_uint32be(fd,&sub_chunk_len);
+ n=read(fd,&sub_chunk_id,4);
+ j-=8;
+ if (memcmp(&sub_chunk_id,"ilst",4)==0) {
+ /* Here are the actual tags. We use the id3v2 300-byte buffer
+ to store the string data */
+ bytes_remaining=sizeof(id3->id3v2buf);
+ id3->genre=255; /* Not every track is the Blues */
+ id3buf=id3->id3v2buf;
+ k=sub_chunk_len-8;
+ j-=k;
+ while (k > 0) {
+ n=read_uint32be(fd,&sub_chunk_len);
+ n=read(fd,&sub_chunk_id,4);
+ k-=8;
+ if (memcmp(sub_chunk_id,"\251nam",4)==0) {
+ read_m4a_tag_string(fd,sub_chunk_len-8,&id3buf,&bytes_remaining,&id3->title);
+ } else if (memcmp(sub_chunk_id,"\251ART",4)==0) {
+ read_m4a_tag_string(fd,sub_chunk_len-8,&id3buf,&bytes_remaining,&id3->artist);
+ } else if (memcmp(sub_chunk_id,"\251alb",4)==0) {
+ read_m4a_tag_string(fd,sub_chunk_len-8,&id3buf,&bytes_remaining,&id3->album);
+ } else if (memcmp(sub_chunk_id,"\251gen",4)==0) {
+ read_m4a_tag_string(fd,sub_chunk_len-8,&id3buf,&bytes_remaining,&id3->genre_string);
+ } else if (memcmp(sub_chunk_id,"\251day",4)==0) {
+ read_m4a_tag_string(fd,sub_chunk_len-8,&id3buf,&bytes_remaining,&id3->year_string);
+ } else if (memcmp(sub_chunk_id,"trkn",4)==0) {
+ if (sub_chunk_len==0x20) {
+ read(fd,buf,sub_chunk_len-8);
+ id3->tracknum=buf[19];
+ } else {
+ lseek(fd, sub_chunk_len-8,SEEK_CUR);
+ }
+ } else {
+ lseek(fd, sub_chunk_len-8,SEEK_CUR);
+ }
+ k-=(sub_chunk_len-8);
+ }
+ }
+ }
+ }
+ /* Skip any remaining data in udta chunk */
+ lseek(fd, j, SEEK_CUR);
+ } else if (memcmp(&sub_chunk_id,"trak",4)==0) {
+ /* Format of trak chunk:
+ tkhd
+ mdia
+ mdhd
+ hdlr
+ minf
+ smhd
+ dinf
+ stbl
+ stsd - Samplerate, Samplesize, Numchannels
+ stts - time_to_sample array - RLE'd table containing duration of each block
+ stsz - sample_byte_size array - ?Size in bytes of each compressed block
+ stsc - Seek table related?
+ stco - Seek table related?
+ */
+
+ /* Skip tkhd - not needed */
+ n=read_uint32be(fd,&sub_chunk_len);
+ n=read(fd,&sub_chunk_id,4);
+ if (memcmp(&sub_chunk_id,"tkhd",4)!=0) {
+ logf("Expecting tkhd\n");
+ return false;
+ }
+ lseek(fd, sub_chunk_len - 8, SEEK_CUR); /* FIXME not 8 */
+ size_remaining-=sub_chunk_len;
+
+ /* Process mdia */
+ n=read_uint32be(fd,&sub_chunk_len);
+ n=read(fd,&sub_chunk_id,4);
+ if (memcmp(&sub_chunk_id,"mdia",4)!=0) {
+ logf("Expecting mdia\n");
+ return false;
+ }
+ size_remaining-=sub_chunk_len;
+ j=sub_chunk_len-8;
+
+ while (j > 0) {
+ n=read_uint32be(fd,&sub_chunk_len);
+ n=read(fd,&sub_chunk_id,4);
+ j-=4;
+ if (memcmp(&sub_chunk_id,"minf",4)==0) {
+ j=sub_chunk_len-8;
+ } else if (memcmp(&sub_chunk_id,"stbl",4)==0) {
+ j=sub_chunk_len-8;
+ } else if (memcmp(&sub_chunk_id,"stsd",4)==0) {
+ n=read(fd,buf,sub_chunk_len-8);
+ j-=sub_chunk_len;
+ i=0;
+ /* Skip version and flags */
+ i+=4;
+
+ numentries=(buf[i]<<24)|(buf[i+1]<<16)|(buf[i+2]<<8)|buf[i+3];
+ i+=4;
+ if (numentries!=1) {
+ logf("ERROR: Expecting only one entry in stsd\n");
+ }
+
+ entry_size=(buf[i]<<24)|(buf[i+1]<<16)|(buf[i+2]<<8)|buf[i+3];
+ i+=4;
+
+ /* Check the codec type - 'alac' for ALAC, 'mp4a' for AAC */
+ if (memcmp(&buf[i],"alac",4)!=0) {
+ logf("Not an ALAC file\n");
+ return false;
+ }
+
+ //numchannels=(buf[i+20]<<8)|buf[i+21]; /* Not used - assume Stereo */
+ //samplesize=(buf[i+22]<<8)|buf[i+23]; /* Not used - assume 16-bit */
+
+ /* Samplerate is 32-bit fixed point, but this works for < 65536 Hz */
+ id3->frequency=(buf[i+28]<<8)|buf[i+29];
+ } else if (memcmp(&sub_chunk_id,"stts",4)==0) {
+ j-=sub_chunk_len;
+ i=8;
+ n=read(fd,buf,8);
+ i+=8;
+ numentries=(buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7];
+ for (k=0;k<numentries;k++) {
+ n=read_uint32be(fd,&sample_count);
+ n=read_uint32be(fd,&sample_duration);
+ totalsamples+=sample_count*sample_duration;
+ i+=8;
+ }
+ if (i > 0) lseek(fd, sub_chunk_len - i, SEEK_CUR);
+ } else if (memcmp(&sub_chunk_id,"stsz",4)==0) {
+ j-=sub_chunk_len;
+ i=8;
+ n=read(fd,buf,8);
+ i+=8;
+ numentries=(buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7];
+ for (k=0;k<numentries;k++) {
+ n=read_uint32be(fd,&sample_count);
+ n=read_uint32be(fd,&sample_duration);
+ totalsamples+=sample_count*sample_duration;
+ i+=8;
+ }
+ if (i > 0) lseek(fd, sub_chunk_len - i, SEEK_CUR);
+ } else {
+ lseek(fd, sub_chunk_len - 8, SEEK_CUR); /* FIXME not 8 */
+ j-=sub_chunk_len;
+ }
+ }
+ } else {
+ logf("Unexpected sub_chunk_id inside moov: %c%c%c%c\n",
+ sub_chunk_id[0],sub_chunk_id[1],sub_chunk_id[2],sub_chunk_id[3]);
+ return false;
+ }
+ }
+ } else if (memcmp(&chunk_id,"mdat",4)==0) {
+ /* once we hit mdat we stop reading and return.
+ * this is on the assumption that there is no furhter interesting
+ * stuff in the stream. if there is stuff will fail (:()).
+ * But we need the read pointer to be at the mdat stuff
+ * for the decoder. And we don't want to rely on fseek/ftell,
+ * as they may not always be avilable */
+ lseek(fd, chunk_len - 8, SEEK_CUR); /* FIXME not 8 */
+ compressedsize=chunk_len-8;
+ } else if (memcmp(&chunk_id,"free",4)==0) {
+ /* these following atoms can be skipped !!!! */
+ lseek(fd, chunk_len - 8, SEEK_CUR); /* FIXME not 8 */
+ } else {
+ logf("(top) unknown chunk id: %c%c%c%c\n", chunk_id[0],chunk_id[1],chunk_id[2],chunk_id[3]);
+ return false;
+ }
+ }
+
+ id3->vbr=true; /* All ALAC files are VBR */
+ id3->filesize=filesize(fd);
+ id3->samples=totalsamples;
+ id3->length=(10*totalsamples)/(id3->frequency/100);
+ id3->bitrate=(compressedsize*8)/id3->length;;
+
+ return true;
+}
+
/* Simple file type probing by looking at the filename extension. */
unsigned int probe_file_format(const char *filename)
{
@@ -1064,6 +1394,14 @@ bool get_metadata(struct track_info* track, int fd, const char* trackname,
track->id3.length = (totalsamples / track->id3.frequency) * 1000;
break;
+ case AFMT_ALAC:
+ if (!get_alac_metadata(fd, &(track->id3)))
+ {
+// return false;
+ }
+
+ break;
+
/* If we don't know how to read the metadata, just store the filename */
default:
break;
diff --git a/apps/playback.c b/apps/playback.c
index f05cb9e..0885bd4 100644
--- a/apps/playback.c
+++ b/apps/playback.c
@@ -74,6 +74,7 @@ static volatile bool paused;
#define CODEC_A52 "/.rockbox/codecs/a52.codec"
#define CODEC_MPC "/.rockbox/codecs/mpc.codec"
#define CODEC_WAVPACK "/.rockbox/codecs/wavpack.codec"
+#define CODEC_ALAC "/.rockbox/codecs/alac.codec"
#define AUDIO_FILL_CYCLE (1024*256)
#define AUDIO_DEFAULT_WATERMARK (1024*512)
@@ -881,6 +882,10 @@ bool loadcodec(const char *trackname, bool start_play)
logf("Codec: WAVPACK");
codec_path = CODEC_WAVPACK;
break;
+ case AFMT_ALAC:
+ logf("Codec: ALAC");
+ codec_path = CODEC_ALAC;
+ break;
default:
logf("Codec: Unsupported");
snprintf(msgbuf, sizeof(msgbuf)-1, "No codec for: %s", trackname);
diff --git a/apps/tree.c b/apps/tree.c
index e8bf46f..a52d453 100644
--- a/apps/tree.c
+++ b/apps/tree.c
@@ -82,6 +82,7 @@ const struct filetype filetypes[] = {
{ "a52", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
{ "mpc", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
{ "wv", TREE_ATTR_MPA, Icon_Audio, VOICE_EXT_MPA },
+ { "m4a", 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 },