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
This commit is contained in:
Mohamed Tarek 2009-08-14 17:36:57 +00:00
parent 4f26112b1b
commit 685628cf18
10 changed files with 227 additions and 12 deletions

View file

@ -12,6 +12,7 @@ alac.c
cook.c
raac.c
a52_rm.c
atrac3_rm.c
mpc.c
wma.c
sid.c

173
apps/codecs/atrac3_rm.c Normal file
View file

@ -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 <string.h>
#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;
}

View file

@ -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

View file

@ -0,0 +1,3 @@
atrac3.c
fixp_math.c
bitstream.c

View file

@ -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;

View file

@ -28,7 +28,7 @@
#ifndef AVCODEC_ATRAC3DATA_H
#define AVCODEC_ATRAC3DATA_H
#include <stdint.h>
#include <inttypes.h>
/* VLC tables */

View file

@ -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

View file

@ -124,6 +124,9 @@ const struct afmt_entry audio_formats[AFMT_NUM_CODECS] =
/* AC3 in RM/RA */
[AFMT_RM_AC3] =
AFMT_ENTRY("AC3", "a52_rm", NULL, "rm\0ra\0rmvb\0" ),
/* ATRAC3 in RM/RA */
[AFMT_RM_ATRAC3] =
AFMT_ENTRY("ATRAC3","atrac3_rm", NULL, "rm\0ra\0rmvb\0" ),
#endif
};

View file

@ -64,6 +64,7 @@ enum
AFMT_RM_COOK, /* Cook in RM/RA */
AFMT_RM_AAC, /* AAC in RM/RA */
AFMT_RM_AC3, /* AC3 in RM/RA */
AFMT_RM_ATRAC3, /* ATRAC3 in RM/RA */
#endif
/* add new formats at any index above this line to have a sensible order -

View file

@ -182,6 +182,14 @@ static inline int real_read_audio_stream_info(int fd, RMContext *rmctx)
rmctx->codec_type = CODEC_AC3;
break;
case FOURCC('a','t','r','c'):
rmctx->codec_type = CODEC_ATRAC;
read_uint32be(fd, &rmctx->extradata_size);
skipped += 4;
read(fd, rmctx->codec_extradata, rmctx->extradata_size);
skipped += rmctx->extradata_size;
break;
default: /* Not a supported codec */
return -1;
}
@ -421,7 +429,7 @@ bool get_rm_metadata(int fd, struct mp3entry* id3)
break;
case CODEC_ATRAC:
/* Not yet supported in rockbox, parsing fails before reaching here */
id3->codectype = AFMT_RM_ATRAC3;
break;
}