From 685628cf18f815b1ccc80a84590037960b2b4ec1 Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Fri, 14 Aug 2009 17:36:57 +0000 Subject: Support for playback of atrac3 audio in rm, in sim. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22311 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/SOURCES | 1 + apps/codecs/atrac3_rm.c | 173 +++++++++++++++++++++++++++++++++++++ apps/codecs/codecs.make | 3 + apps/codecs/libatrac/SOURCES | 3 + apps/codecs/libatrac/atrac3.c | 25 +++--- apps/codecs/libatrac/atrac3data.h | 2 +- apps/codecs/libatrac/libatrac.make | 18 ++++ 7 files changed, 214 insertions(+), 11 deletions(-) create mode 100644 apps/codecs/atrac3_rm.c create mode 100644 apps/codecs/libatrac/SOURCES create mode 100644 apps/codecs/libatrac/libatrac.make (limited to 'apps/codecs') diff --git a/apps/codecs/SOURCES b/apps/codecs/SOURCES index 4e6c891d9a..75d74ab33b 100644 --- a/apps/codecs/SOURCES +++ b/apps/codecs/SOURCES @@ -12,6 +12,7 @@ alac.c cook.c raac.c a52_rm.c +atrac3_rm.c mpc.c wma.c sid.c diff --git a/apps/codecs/atrac3_rm.c b/apps/codecs/atrac3_rm.c new file mode 100644 index 0000000000..a9c1c49fea --- /dev/null +++ b/apps/codecs/atrac3_rm.c @@ -0,0 +1,173 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * Copyright (C) 2009 Mohamed Tarek + * + * 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 "logf.h" +#include "codeclib.h" +#include "inttypes.h" +#include "libatrac/atrac3.h" + +CODEC_HEADER + +RMContext rmctx; +RMPacket pkt; +ATRAC3Context q; + +static void init_rm(RMContext *rmctx) +{ + memcpy(rmctx, (void*)(( (intptr_t)ci->id3->id3v2buf + 3 ) &~ 3), sizeof(RMContext)); +} + +/* this is the codec entry point */ +enum codec_status codec_main(void) +{ + static size_t buff_size; + int datasize, res, consumed, i, time_offset; + uint8_t *bit_buffer; + int16_t outbuf[2048] __attribute__((aligned(32))); + uint16_t fs,sps,h; + uint32_t packet_count; + int scrambling_unit_size, num_units, elapsed = 0; + +next_track: + if (codec_init()) { + DEBUGF("codec init failed\n"); + return CODEC_ERROR; + } + while (!*ci->taginfo_ready && !ci->stop_codec) + ci->sleep(1); + + codec_set_replaygain(ci->id3); + ci->memset(&rmctx,0,sizeof(RMContext)); + ci->memset(&pkt,0,sizeof(RMPacket)); + ci->memset(&q,0,sizeof(ATRAC3Context)); + + init_rm(&rmctx); + + ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency); + ci->configure(DSP_SET_SAMPLE_DEPTH, 16); + ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ? + STEREO_MONO : STEREO_INTERLEAVED); + + packet_count = rmctx.nb_packets; + rmctx.audio_framesize = rmctx.block_align; + rmctx.block_align = rmctx.sub_packet_size; + fs = rmctx.audio_framesize; + sps= rmctx.block_align; + h = rmctx.sub_packet_h; + scrambling_unit_size = h*fs; + + res =atrac3_decode_init(&q, &rmctx); + if(res < 0) { + DEBUGF("failed to initialize atrac decoder\n"); + return CODEC_ERROR; + } + + ci->set_elapsed(0); + ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE); + + /* The main decoder loop */ +seek_start : + while((unsigned)elapsed < rmctx.duration) + { + bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size); + consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt); + if(consumed < 0) { + DEBUGF("rm_get_packet failed\n"); + return CODEC_ERROR; + } + + for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++) + { + ci->yield(); + if (ci->stop_codec || ci->new_track) + goto done; + + if (ci->seek_time) { + ci->set_elapsed(ci->seek_time); + + /* Do not allow seeking beyond the file's length */ + if ((unsigned) ci->seek_time > ci->id3->length) { + ci->seek_complete(); + goto done; + } + + ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE); + packet_count = rmctx.nb_packets; + rmctx.audio_pkt_cnt = 0; + rmctx.frame_number = 0; + + /* Seek to the start of the track */ + if (ci->seek_time == 1) { + ci->set_elapsed(0); + ci->seek_complete(); + goto seek_start; + } + num_units = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate))/(h*(fs/sps)); + ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * num_units); + bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size); + consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt); + if(consumed < 0) { + DEBUGF("rm_get_packet failed\n"); + return CODEC_ERROR; + } + packet_count = rmctx.nb_packets - rmctx.audio_pkt_cnt * num_units; + rmctx.frame_number = ((ci->seek_time)/(sps*1000*8/rmctx.bit_rate)); + while(rmctx.audiotimestamp > (unsigned) ci->seek_time) { + rmctx.audio_pkt_cnt = 0; + ci->seek_buffer(rmctx.data_offset + DATA_HEADER_SIZE + consumed * (num_units-1)); + bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size); + consumed = rm_get_packet(&bit_buffer, &rmctx, &pkt); + packet_count += rmctx.audio_pkt_cnt; + num_units--; + } + time_offset = ci->seek_time - rmctx.audiotimestamp; + i = (time_offset/((sps * 8 * 1000)/rmctx.bit_rate)); + elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i; + ci->set_elapsed(elapsed); + ci->seek_complete(); + } + if(pkt.length) + res = atrac3_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i], rmctx.block_align); + else /* indicates that there are no remaining frames */ + goto done; + + if(res != rmctx.block_align) { + DEBUGF("codec error\n"); + return CODEC_ERROR; + } + + if(datasize) + ci->pcmbuf_insert(outbuf, NULL, q.samples_per_frame / rmctx.nb_channels); + elapsed = rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i; + ci->set_elapsed(elapsed); + rmctx.frame_number++; + } + packet_count -= rmctx.audio_pkt_cnt; + rmctx.audio_pkt_cnt = 0; + ci->advance_buffer(consumed); + } + + done : + if (ci->request_next_track()) + goto next_track; + + return CODEC_OK; +} diff --git a/apps/codecs/codecs.make b/apps/codecs/codecs.make index ba5f0ed8a3..85c10158d7 100644 --- a/apps/codecs/codecs.make +++ b/apps/codecs/codecs.make @@ -35,6 +35,7 @@ include $(APPSDIR)/codecs/libwavpack/libwavpack.make include $(APPSDIR)/codecs/libwma/libwma.make include $(APPSDIR)/codecs/libcook/libcook.make include $(APPSDIR)/codecs/librm/librm.make +include $(APPSDIR)/codecs/libatrac/libatrac.make # compile flags for codecs CODECFLAGS = $(CFLAGS) -I$(APPSDIR)/codecs -I$(APPSDIR)/codecs/lib \ @@ -50,6 +51,7 @@ CODEC_CRT0 := $(CODECDIR)/codec_crt0.o CODECLIBS := $(DEMACLIB) $(A52LIB) $(ALACLIB) $(ASAPLIB) \ $(FAADLIB) $(FFMPEGFLACLIB) $(M4ALIB) $(MADLIB) $(MUSEPACKLIB) \ $(SPCLIB) $(SPEEXLIB) $(TREMORLIB) $(WAVPACKLIB) $(WMALIB) $(COOKLIB) \ + $(ATRACLIB) \ $(CODECLIB) $(CODECS): $(CODEC_CRT0) $(CODECLINK_LDS) @@ -78,6 +80,7 @@ $(CODECDIR)/asap.codec : $(CODECDIR)/libasap.a $(CODECDIR)/cook.codec : $(CODECDIR)/libcook.a $(CODECDIR)/librm.a $(CODECDIR)/raac.codec : $(CODECDIR)/libfaad.a $(CODECDIR)/librm.a $(CODECDIR)/a52_rm.codec : $(CODECDIR)/liba52.a $(CODECDIR)/librm.a +$(CODECDIR)/atrac3_rm.codec : $(CODECDIR)/libatrac.a $(CODECDIR)/librm.a $(CODECS): $(CODECLIB) # this must be last in codec dependency list diff --git a/apps/codecs/libatrac/SOURCES b/apps/codecs/libatrac/SOURCES new file mode 100644 index 0000000000..9db9ac3dab --- /dev/null +++ b/apps/codecs/libatrac/SOURCES @@ -0,0 +1,3 @@ +atrac3.c +fixp_math.c +bitstream.c diff --git a/apps/codecs/libatrac/atrac3.c b/apps/codecs/libatrac/atrac3.c index 9e61db1686..7f2cb57356 100644 --- a/apps/codecs/libatrac/atrac3.c +++ b/apps/codecs/libatrac/atrac3.c @@ -45,7 +45,10 @@ #define JOINT_STEREO 0x12 #define STEREO 0x2 -#define AVERROR(...) -1 +#ifdef ROCKBOX +#undef DEBUGF +#define DEBUGF(...) +#endif /* ROCKBOX */ /* FFMAX/MIN/SWAP and av_clip were taken from libavutil/common.h */ #define FFMAX(a,b) ((a) > (b) ? (a) : (b)) @@ -76,7 +79,7 @@ static channel_unit channel_units[2]; */ static void iqmf (int32_t *inlo, int32_t *inhi, unsigned int nIn, int32_t *pOut, int32_t *delayBuf, int32_t *temp) { - int i, j; + unsigned int i, j; int32_t *p1, *p3; memcpy(temp, delayBuf, 46*sizeof(int32_t)); @@ -163,7 +166,7 @@ static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){ const uint32_t* buf; uint32_t* obuf = (uint32_t*) out; -#ifdef TEST +#if ((defined(TEST) || defined(SIMULATOR)) && !defined(CPU_ARM)) off = 0; //no check for memory alignment of inbuffer #else off = (intptr_t)inbuffer & 3; @@ -175,14 +178,16 @@ static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){ for (i = 0; i < bytes/4; i++) obuf[i] = c ^ buf[i]; - if (off) + if (off) { DEBUGF("Offset of %d not handled, post sample on ffmpeg-dev.\n",off); + return off; + } return off; } -static void init_atrac3_transforms(ATRAC3Context *q) { +static void init_atrac3_transforms() { int32_t s; int i; @@ -630,7 +635,7 @@ static void channelWeighting (int32_t *su1, int32_t *su2, int *p3) */ -static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, int32_t *pOut, int channelNum, int codingMode) +static int decodeChannelSoundUnit (GetBitContext *gb, channel_unit *pSnd, int32_t *pOut, int channelNum, int codingMode) { int band, result=0, numSubbands, lastTonal, numBands; if (codingMode == JOINT_STEREO && channelNum == 1) { @@ -705,7 +710,7 @@ static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf) /* decode Sound Unit 1 */ init_get_bits(&q->gb,databuf,q->bits_per_frame); - result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO); + result = decodeChannelSoundUnit(&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO); if (result != 0) return (result); @@ -746,7 +751,7 @@ static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf) } /* Decode Sound Unit 2. */ - result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO); + result = decodeChannelSoundUnit(&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO); if (result != 0) return (result); @@ -763,7 +768,7 @@ static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf) /* Set the bitstream reader at the start of a channel sound unit. */ init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels); - result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode); + result = decodeChannelSoundUnit(&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode); if (result != 0) return (result); } @@ -943,7 +948,7 @@ int atrac3_decode_init(ATRAC3Context *q, RMContext *rmctx) } - init_atrac3_transforms(q); + init_atrac3_transforms(); /* init the joint-stereo decoding data */ q->weighting_delay[0] = 0; diff --git a/apps/codecs/libatrac/atrac3data.h b/apps/codecs/libatrac/atrac3data.h index e71dc35560..8e7fe97c32 100644 --- a/apps/codecs/libatrac/atrac3data.h +++ b/apps/codecs/libatrac/atrac3data.h @@ -28,7 +28,7 @@ #ifndef AVCODEC_ATRAC3DATA_H #define AVCODEC_ATRAC3DATA_H -#include +#include /* VLC tables */ diff --git a/apps/codecs/libatrac/libatrac.make b/apps/codecs/libatrac/libatrac.make new file mode 100644 index 0000000000..01606e1f15 --- /dev/null +++ b/apps/codecs/libatrac/libatrac.make @@ -0,0 +1,18 @@ +# __________ __ ___. +# Open \______ \ ____ ____ | | _\_ |__ _______ ___ +# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +# \/ \/ \/ \/ \/ +# $Id: libatrac.make 20151 2009-03-01 09:04:15Z amiconn $ +# + +# libatrac +ATRACLIB := $(CODECDIR)/libatrac.a +ATRACLIB_SRC := $(call preprocess, $(APPSDIR)/codecs/libatrac/SOURCES) +ATRACLIB_OBJ := $(call c2obj, $(ATRACLIB_SRC)) +OTHER_SRC += $(ATRACLIB_SRC) + +$(ATRACLIB): $(ATRACLIB_OBJ) + $(SILENT)$(shell rm -f $@) + $(call PRINTS,AR $(@F))$(AR) rcs $@ $^ >/dev/null -- cgit v1.2.3