1
0
Fork 0
forked from len0rd/rockbox

Remove generic M4A parsing code from libalac and create a libm4a - so it can be used by other codecs

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7682 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Dave Chapman 2005-10-29 17:12:52 +00:00
parent e75cbdd2a8
commit 45f9e5d7b0
10 changed files with 400 additions and 300 deletions

View file

@ -0,0 +1,47 @@
# __________ __ ___.
# Open \______ \ ____ ____ | | _\_ |__ _______ ___
# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
# \/ \/ \/ \/ \/
# $Id$
#
INCLUDES=-I$(APPSDIR) -I.. -I. -I$(FIRMDIR)/include -I$(FIRMDIR)/export \
-I$(FIRMDIR)/common -I$(FIRMDIR)/drivers -I$(BUILDDIR)
ifdef APPEXTRA
INCLUDES += $(patsubst %,-I$(APPSDIR)/%,$(subst :, ,$(APPEXTRA)))
endif
M4AOPTS = -O3
CFLAGS = $(GCCOPTS) $(M4AOPTS) $(INCLUDES) $(TARGET) $(EXTRA_DEFINES) -DMEM=${MEMORYSIZE}
# This sets up 'SRC' based on the files mentioned in SOURCES
include $(TOOLSDIR)/makesrc.inc
SOURCES = $(SRC)
OBJS2 := $(SRC:%.c=$(OBJDIR)/%.o)
OBJS = $(patsubst %.S, $(OBJDIR)/%.o, $(OBJS2))
DEPFILE = $(OBJDIR)/dep-libm4a
DIRS =
OUTPUT = $(BUILDDIR)/libm4a.a
all: $(OUTPUT)
$(OUTPUT): $(OBJS)
@echo "AR $@"
@$(AR) ruv $@ $+ >/dev/null 2>&1
$(OBJDIR)/libm4a/%.o: $(APPSDIR)/codecs/libm4a/%.c
@echo "(libm4a) CC $<"
@$(CC) -c $(CFLAGS) -I$(APPSDIR)/codecs/libm4a/ $< -o $@
include $(TOOLSDIR)/make.inc
clean:
@echo "cleaning libm4a"
@rm -f $(OBJS) $(OUTPUT) $(DEPFILE)
-include $(DEPFILE)

View file

@ -0,0 +1,2 @@
m4a.c
demux.c

655
apps/codecs/libm4a/demux.c Normal file
View file

@ -0,0 +1,655 @@
/*
* ALAC (Apple Lossless Audio Codec) decoder
* Copyright (c) 2005 David Hammerton
* All rights reserved.
*
* This is the quicktime container demuxer.
*
* http://crazney.net/programs/itunes/alac.html
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <string.h>
#include <inttypes.h>
#include <stdlib.h>
#include "../codec.h"
#include "m4a.h"
typedef struct
{
stream_t *stream;
demux_res_t *res;
} qtmovie_t;
/* chunk handlers */
static void read_chunk_ftyp(qtmovie_t *qtmovie, size_t chunk_len)
{
fourcc_t type;
uint32_t minor_ver;
size_t size_remaining = chunk_len - 8; /* FIXME: can't hardcode 8, size may be 64bit */
type = stream_read_uint32(qtmovie->stream);
size_remaining-=4;
if (type != MAKEFOURCC('M','4','A',' '))
{
//fprintf(stderr, "not M4A file\n");
return;
}
minor_ver = stream_read_uint32(qtmovie->stream);
size_remaining-=4;
/* compatible brands */
while (size_remaining)
{
/* unused */
/*fourcc_t cbrand =*/ stream_read_uint32(qtmovie->stream);
size_remaining-=4;
}
}
static void read_chunk_tkhd(qtmovie_t *qtmovie, size_t chunk_len)
{
/* don't need anything from here atm, skip */
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
stream_skip(qtmovie->stream, size_remaining);
}
static void read_chunk_mdhd(qtmovie_t *qtmovie, size_t chunk_len)
{
/* don't need anything from here atm, skip */
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
stream_skip(qtmovie->stream, size_remaining);
}
/* media handler inside mdia */
static void read_chunk_hdlr(qtmovie_t *qtmovie, size_t chunk_len)
{
fourcc_t comptype, compsubtype;
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
int strlen;
char str[256] = {0};
/* version */
stream_read_uint8(qtmovie->stream);
size_remaining -= 1;
/* flags */
stream_read_uint8(qtmovie->stream);
stream_read_uint8(qtmovie->stream);
stream_read_uint8(qtmovie->stream);
size_remaining -= 3;
/* component type */
comptype = stream_read_uint32(qtmovie->stream);
compsubtype = stream_read_uint32(qtmovie->stream);
size_remaining -= 8;
/* component manufacturer */
stream_read_uint32(qtmovie->stream);
size_remaining -= 4;
/* flags */
stream_read_uint32(qtmovie->stream);
stream_read_uint32(qtmovie->stream);
size_remaining -= 8;
/* name */
strlen = stream_read_uint8(qtmovie->stream);
stream_read(qtmovie->stream, strlen, str);
size_remaining -= 1 + strlen;
if (size_remaining)
{
stream_skip(qtmovie->stream, size_remaining);
}
}
static bool read_chunk_stsd(qtmovie_t *qtmovie, size_t chunk_len)
{
unsigned int i;
uint32_t numentries;
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
/* version */
stream_read_uint8(qtmovie->stream);
size_remaining -= 1;
/* flags */
stream_read_uint8(qtmovie->stream);
stream_read_uint8(qtmovie->stream);
stream_read_uint8(qtmovie->stream);
size_remaining -= 3;
numentries = stream_read_uint32(qtmovie->stream);
size_remaining -= 4;
if (numentries != 1)
{
//fprintf(stderr, "only expecting one entry in sample description atom!\n");
return false;
}
for (i = 0; i < numentries; i++)
{
uint32_t entry_size;
uint16_t version;
uint32_t entry_remaining;
entry_size = stream_read_uint32(qtmovie->stream);
qtmovie->res->format = stream_read_uint32(qtmovie->stream);
entry_remaining = entry_size;
entry_remaining -= 8;
/* sound info: */
stream_skip(qtmovie->stream, 6); /* reserved */
entry_remaining -= 6;
version = stream_read_uint16(qtmovie->stream);
// if (version != 1)
//fprintf(stderr, "unknown version??\n");
entry_remaining -= 2;
/* revision level */
stream_read_uint16(qtmovie->stream);
/* vendor */
stream_read_uint32(qtmovie->stream);
entry_remaining -= 6;
/* EH?? spec doesn't say theres an extra 16 bits here.. but there is! */
stream_read_uint16(qtmovie->stream);
entry_remaining -= 2;
qtmovie->res->num_channels = stream_read_uint16(qtmovie->stream);
qtmovie->res->sample_size = stream_read_uint16(qtmovie->stream);
entry_remaining -= 4;
/* compression id */
stream_read_uint16(qtmovie->stream);
/* packet size */
stream_read_uint16(qtmovie->stream);
entry_remaining -= 4;
/* sample rate - 32bit fixed point = 16bit?? */
qtmovie->res->sample_rate = stream_read_uint16(qtmovie->stream);
entry_remaining -= 2;
/* skip 2 */
stream_skip(qtmovie->stream, 2);
entry_remaining -= 2;
/* remaining is codec data */
#if 0
qtmovie->res->codecdata_len = stream_read_uint32(qtmovie->stream);
if (qtmovie->res->codecdata_len != entry_remaining)
fprintf(stderr, "perhaps not? %i vs %i\n",
qtmovie->res->codecdata_len, entry_remaining);
entry_remaining -= 4;
stream_read_uint32(qtmovie->stream); /* 'alac' */
entry_remaining -= 4;
qtmovie->res->codecdata = malloc(qtmovie->res->codecdata_len - 8);
stream_read(qtmovie->stream,
entry_remaining,
qtmovie->res->codecdata);
entry_remaining = 0;
#else
/* 12 = audio format atom, 8 = padding */
qtmovie->res->codecdata_len = entry_remaining + 12 + 8;
qtmovie->res->codecdata = malloc(qtmovie->res->codecdata_len);
memset(qtmovie->res->codecdata, 0, qtmovie->res->codecdata_len);
/* audio format atom */
((unsigned int*)qtmovie->res->codecdata)[0] = 0x0c000000;
((unsigned int*)qtmovie->res->codecdata)[1] = MAKEFOURCC('a','m','r','f');
((unsigned int*)qtmovie->res->codecdata)[2] = MAKEFOURCC('c','a','l','a');
stream_read(qtmovie->stream,
entry_remaining,
((char*)qtmovie->res->codecdata) + 12);
entry_remaining -= entry_remaining;
#endif
if (entry_remaining)
stream_skip(qtmovie->stream, entry_remaining);
if (qtmovie->res->format != MAKEFOURCC('a','l','a','c'))
{
// fprintf(stderr, "expecting 'alac' data format, got %c%c%c%c\n",
// SPLITFOURCC(qtmovie->res->format));
return false;
}
}
return true;
}
static void read_chunk_stts(qtmovie_t *qtmovie, size_t chunk_len)
{
unsigned int i;
uint32_t numentries;
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
/* version */
stream_read_uint8(qtmovie->stream);
size_remaining -= 1;
/* flags */
stream_read_uint8(qtmovie->stream);
stream_read_uint8(qtmovie->stream);
stream_read_uint8(qtmovie->stream);
size_remaining -= 3;
numentries = stream_read_uint32(qtmovie->stream);
size_remaining -= 4;
qtmovie->res->num_time_to_samples = numentries;
qtmovie->res->time_to_sample = malloc(numentries * sizeof(*qtmovie->res->time_to_sample));
for (i = 0; i < numentries; i++)
{
qtmovie->res->time_to_sample[i].sample_count = stream_read_uint32(qtmovie->stream);
qtmovie->res->time_to_sample[i].sample_duration = stream_read_uint32(qtmovie->stream);
size_remaining -= 8;
}
if (size_remaining)
{
//fprintf(stderr, "ehm, size remianing?\n");
stream_skip(qtmovie->stream, size_remaining);
}
}
static void read_chunk_stsz(qtmovie_t *qtmovie, size_t chunk_len)
{
unsigned int i;
uint32_t numentries;
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
/* version */
stream_read_uint8(qtmovie->stream);
size_remaining -= 1;
/* flags */
stream_read_uint8(qtmovie->stream);
stream_read_uint8(qtmovie->stream);
stream_read_uint8(qtmovie->stream);
size_remaining -= 3;
/* default sample size */
if (stream_read_uint32(qtmovie->stream) != 0)
{
//fprintf(stderr, "i was expecting variable samples sizes\n");
stream_read_uint32(qtmovie->stream);
size_remaining -= 4;
return;
}
size_remaining -= 4;
numentries = stream_read_uint32(qtmovie->stream);
size_remaining -= 4;
qtmovie->res->num_sample_byte_sizes = numentries;
qtmovie->res->sample_byte_size = malloc(numentries * sizeof(*qtmovie->res->sample_byte_size));
for (i = 0; i < numentries; i++)
{
qtmovie->res->sample_byte_size[i] = stream_read_uint32(qtmovie->stream);
size_remaining -= 4;
}
if (size_remaining)
{
//fprintf(stderr, "ehm, size remianing?\n");
stream_skip(qtmovie->stream, size_remaining);
}
}
static bool read_chunk_stbl(qtmovie_t *qtmovie, size_t chunk_len)
{
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
while (size_remaining)
{
size_t sub_chunk_len;
fourcc_t sub_chunk_id;
sub_chunk_len = stream_read_uint32(qtmovie->stream);
if (sub_chunk_len <= 1 || sub_chunk_len > size_remaining)
{
//fprintf(stderr, "strange size for chunk inside stbl\n");
return false;
}
sub_chunk_id = stream_read_uint32(qtmovie->stream);
switch (sub_chunk_id)
{
case MAKEFOURCC('s','t','s','d'):
if (!read_chunk_stsd(qtmovie, sub_chunk_len)) {
return false;
}
break;
case MAKEFOURCC('s','t','t','s'):
read_chunk_stts(qtmovie, sub_chunk_len);
break;
case MAKEFOURCC('s','t','s','z'):
read_chunk_stsz(qtmovie, sub_chunk_len);
break;
case MAKEFOURCC('s','t','s','c'):
case MAKEFOURCC('s','t','c','o'):
/* skip these, no indexing for us! */
stream_skip(qtmovie->stream, sub_chunk_len - 8);
break;
default:
// fprintf(stderr, "(stbl) unknown chunk id: %c%c%c%c\n",
// SPLITFOURCC(sub_chunk_id));
return false;
}
size_remaining -= sub_chunk_len;
}
return true;
}
static bool read_chunk_minf(qtmovie_t *qtmovie, size_t chunk_len)
{
size_t dinf_size, stbl_size;
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
/**** SOUND HEADER CHUNK ****/
if (stream_read_uint32(qtmovie->stream) != 16)
{
//fprintf(stderr, "unexpected size in media info\n");
return false;
}
if (stream_read_uint32(qtmovie->stream) != MAKEFOURCC('s','m','h','d'))
{
//fprintf(stderr, "not a sound header! can't handle this.\n");
return false;
}
/* now skip the rest */
stream_skip(qtmovie->stream, 16 - 8);
size_remaining -= 16;
/****/
/**** DINF CHUNK ****/
dinf_size = stream_read_uint32(qtmovie->stream);
if (stream_read_uint32(qtmovie->stream) != MAKEFOURCC('d','i','n','f'))
{
//fprintf(stderr, "expected dinf, didn't get it.\n");
return false;
}
/* skip it */
stream_skip(qtmovie->stream, dinf_size - 8);
size_remaining -= dinf_size;
/****/
/**** SAMPLE TABLE ****/
stbl_size = stream_read_uint32(qtmovie->stream);
if (stream_read_uint32(qtmovie->stream) != MAKEFOURCC('s','t','b','l'))
{
//fprintf(stderr, "expected stbl, didn't get it.\n");
return false;
}
if (!read_chunk_stbl(qtmovie, stbl_size)) {
return false;
}
size_remaining -= stbl_size;
if (size_remaining)
{
//fprintf(stderr, "oops\n");
stream_skip(qtmovie->stream, size_remaining);
}
return true;
}
static bool read_chunk_mdia(qtmovie_t *qtmovie, size_t chunk_len)
{
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
while (size_remaining)
{
size_t sub_chunk_len;
fourcc_t sub_chunk_id;
sub_chunk_len = stream_read_uint32(qtmovie->stream);
if (sub_chunk_len <= 1 || sub_chunk_len > size_remaining)
{
//fprintf(stderr, "strange size for chunk inside mdia\n");
return false;
}
sub_chunk_id = stream_read_uint32(qtmovie->stream);
switch (sub_chunk_id)
{
case MAKEFOURCC('m','d','h','d'):
read_chunk_mdhd(qtmovie, sub_chunk_len);
break;
case MAKEFOURCC('h','d','l','r'):
read_chunk_hdlr(qtmovie, sub_chunk_len);
break;
case MAKEFOURCC('m','i','n','f'):
if (!read_chunk_minf(qtmovie, sub_chunk_len)) {
return false;
}
break;
default:
// fprintf(stderr, "(mdia) unknown chunk id: %c%c%c%c\n",
// SPLITFOURCC(sub_chunk_id));
return false;
}
size_remaining -= sub_chunk_len;
}
return true;
}
/* 'trak' - a movie track - contains other atoms */
static bool read_chunk_trak(qtmovie_t *qtmovie, size_t chunk_len)
{
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
while (size_remaining)
{
size_t sub_chunk_len;
fourcc_t sub_chunk_id;
sub_chunk_len = stream_read_uint32(qtmovie->stream);
if (sub_chunk_len <= 1 || sub_chunk_len > size_remaining)
{
//fprintf(stderr, "strange size for chunk inside trak\n");
return false;
}
sub_chunk_id = stream_read_uint32(qtmovie->stream);
switch (sub_chunk_id)
{
case MAKEFOURCC('t','k','h','d'):
read_chunk_tkhd(qtmovie, sub_chunk_len);
break;
case MAKEFOURCC('m','d','i','a'):
if (!read_chunk_mdia(qtmovie, sub_chunk_len)) {
return false;
}
break;
default:
// fprintf(stderr, "(trak) unknown chunk id: %c%c%c%c\n",
// SPLITFOURCC(sub_chunk_id));
stream_skip(qtmovie->stream, sub_chunk_len - 8);
break;
}
size_remaining -= sub_chunk_len;
}
return true;
}
/* 'mvhd' movie header atom */
static void read_chunk_mvhd(qtmovie_t *qtmovie, size_t chunk_len)
{
/* don't need anything from here atm, skip */
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
stream_skip(qtmovie->stream, size_remaining);
}
/* 'udta' user data.. contains tag info */
static void read_chunk_udta(qtmovie_t *qtmovie, size_t chunk_len)
{
/* don't need anything from here atm, skip */
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
stream_skip(qtmovie->stream, size_remaining);
}
/* 'moov' movie atom - contains other atoms */
static bool read_chunk_moov(qtmovie_t *qtmovie, size_t chunk_len)
{
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
while (size_remaining)
{
size_t sub_chunk_len;
fourcc_t sub_chunk_id;
sub_chunk_len = stream_read_uint32(qtmovie->stream);
if (sub_chunk_len <= 1 || sub_chunk_len > size_remaining)
{
//fprintf(stderr, "strange size for chunk inside moov\n");
return false;
}
sub_chunk_id = stream_read_uint32(qtmovie->stream);
switch (sub_chunk_id)
{
case MAKEFOURCC('m','v','h','d'):
read_chunk_mvhd(qtmovie, sub_chunk_len);
break;
case MAKEFOURCC('t','r','a','k'):
if (!read_chunk_trak(qtmovie, sub_chunk_len)) {
return false;
}
break;
case MAKEFOURCC('u','d','t','a'):
read_chunk_udta(qtmovie, sub_chunk_len);
break;
default:
// fprintf(stderr, "(moov) unknown chunk id: %c%c%c%c\n",
// SPLITFOURCC(sub_chunk_id));
return false;
}
size_remaining -= sub_chunk_len;
}
return true;
}
static void read_chunk_mdat(qtmovie_t *qtmovie, size_t chunk_len)
{
size_t size_remaining = chunk_len - 8; /* FIXME WRONG */
qtmovie->res->mdat_len = size_remaining;
#if 0
qtmovie->res->mdat = malloc(size_remaining);
stream_read(qtmovie->stream, size_remaining, qtmovie->res->mdat);
#endif
}
int qtmovie_read(stream_t *file, demux_res_t *demux_res)
{
qtmovie_t qtmovie;
/* construct the stream */
qtmovie.stream = file;
qtmovie.res = demux_res;
/* read the chunks */
while (1)
{
size_t chunk_len;
fourcc_t chunk_id;
chunk_len = stream_read_uint32(qtmovie.stream);
if (stream_eof(qtmovie.stream))
{
return 0;
}
if (chunk_len == 1)
{
//fprintf(stderr, "need 64bit support\n");
return 0;
}
chunk_id = stream_read_uint32(qtmovie.stream);
switch (chunk_id)
{
case MAKEFOURCC('f','t','y','p'):
read_chunk_ftyp(&qtmovie, chunk_len);
break;
case MAKEFOURCC('m','o','o','v'):
if (!read_chunk_moov(&qtmovie, chunk_len)) {
return 0;
}
break;
/* 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 */
case MAKEFOURCC('m','d','a','t'):
read_chunk_mdat(&qtmovie, chunk_len);
/* Keep track of start of stream in file - used for seeking */
qtmovie.res->mdat_offset=stream_tell(qtmovie.stream);
return 1;
/* these following atoms can be skipped !!!! */
case MAKEFOURCC('f','r','e','e'):
stream_skip(qtmovie.stream, chunk_len - 8); /* FIXME not 8 */
break;
default:
// fprintf(stderr, "(top) unknown chunk id: %c%c%c%c\n",
// SPLITFOURCC(chunk_id));
return 0;
}
}
return 0;
}

225
apps/codecs/libm4a/m4a.c Normal file
View file

@ -0,0 +1,225 @@
/***************************************************************************
* __________ __ ___.
* 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 <codecs.h>
#include <inttypes.h>
#include "m4a.h"
/* 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)
{
stream->ci->read_filebuf(buf,size);
if (stream->ci->curpos >= stream->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;
}
int32_t stream_tell(stream_t *stream)
{
return stream->ci->curpos;
}
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;
stream->ci->advance_buffer(skip);
}
int stream_eof(stream_t *stream)
{
return stream->eof;
}
void stream_create(stream_t *stream,struct codec_api* ci)
{
stream->ci=ci;
stream->eof=0;
}
/* This function was part of the original alac decoder implementation */
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.
*/
unsigned int alac_seek (demux_res_t* demux_res,
stream_t* stream,
unsigned int sample_loc,
uint32_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=demux_res->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 (stream->ci->seek_buffer(newpos)) {
*samplesdone=newsample;
*currentblock=newblock;
return 1;
} else {
return 0;
}
}

104
apps/codecs/libm4a/m4a.h Normal file
View file

@ -0,0 +1,104 @@
/***************************************************************************
* __________ __ ___.
* 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.
*
****************************************************************************/
#ifndef _M4A_H
#define _M4A_H
#include <codecs.h>
#include <inttypes.h>
typedef struct {
struct codec_api* ci;
int eof;
} stream_t;
typedef uint32_t fourcc_t;
typedef struct
{
uint16_t num_channels;
uint16_t sample_size;
uint32_t sample_rate;
fourcc_t format;
void *buf;
struct {
uint32_t sample_count;
uint32_t sample_duration;
} *time_to_sample;
uint32_t num_time_to_samples;
uint32_t *sample_byte_size;
uint32_t num_sample_byte_sizes;
uint32_t codecdata_len;
void *codecdata;
int mdat_offset;
uint32_t mdat_len;
#if 0
void *mdat;
#endif
} demux_res_t;
int qtmovie_read(stream_t *stream, demux_res_t *demux_res);
#ifndef MAKEFOURCC
#define MAKEFOURCC(ch0, ch1, ch2, ch3) ( \
( (int32_t)(char)(ch0) << 24 ) | \
( (int32_t)(char)(ch1) << 16 ) | \
( (int32_t)(char)(ch2) << 8 ) | \
( (int32_t)(char)(ch3) ) )
#endif
#ifndef SLPITFOURCC
/* splits it into ch0, ch1, ch2, ch3 - use for printf's */
#define SPLITFOURCC(code) \
(char)((int32_t)code >> 24), \
(char)((int32_t)code >> 16), \
(char)((int32_t)code >> 8), \
(char)code
#endif
void stream_read(stream_t *stream, size_t len, void *buf);
int32_t stream_tell(stream_t *stream);
int32_t stream_read_int32(stream_t *stream);
uint32_t stream_read_uint32(stream_t *stream);
int16_t stream_read_int16(stream_t *stream);
uint16_t stream_read_uint16(stream_t *stream);
int8_t stream_read_int8(stream_t *stream);
uint8_t stream_read_uint8(stream_t *stream);
void stream_skip(stream_t *stream, size_t skip);
int stream_eof(stream_t *stream);
void stream_create(stream_t *stream,struct codec_api* ci);
int get_sample_info(demux_res_t *demux_res, uint32_t samplenum,
uint32_t *sample_duration,
uint32_t *sample_byte_size);
unsigned int alac_seek (demux_res_t* demux_res,
stream_t* stream,
unsigned int sample_loc,
uint32_t* samplesdone, int* currentblock);
#endif /* STREAM_H */