diff options
| author | Daniel Stenberg <daniel@haxx.se> | 2005-06-22 19:41:30 +0000 |
|---|---|---|
| committer | Daniel Stenberg <daniel@haxx.se> | 2005-06-22 19:41:30 +0000 |
| commit | 1dd672fe3226fa77113f35e4d72f50b863484c63 (patch) | |
| tree | 67b424ab990f160dbc8fb238b9fa3390ceba10ed /apps/codecs | |
| parent | b7aaa641b864628d76103b8c9d57c15747560ca7 (diff) | |
| download | rockbox-1dd672fe3226fa77113f35e4d72f50b863484c63.zip rockbox-1dd672fe3226fa77113f35e4d72f50b863484c63.tar.gz rockbox-1dd672fe3226fa77113f35e4d72f50b863484c63.tar.bz2 rockbox-1dd672fe3226fa77113f35e4d72f50b863484c63.tar.xz | |
moved and renamed the codecs, gave the codecs a new extension (.codec),
unified to a single codec-only API, made a new codeclib, disabled the building
of the *2wav plugins
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6812 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs')
| -rw-r--r-- | apps/codecs/Makefile | 81 | ||||
| -rw-r--r-- | apps/codecs/a52.c | 210 | ||||
| -rw-r--r-- | apps/codecs/flac.c | 248 | ||||
| -rw-r--r-- | apps/codecs/lib/Makefile | 46 | ||||
| -rw-r--r-- | apps/codecs/lib/codeclib.c | 38 | ||||
| -rw-r--r-- | apps/codecs/lib/codeclib.h | 49 | ||||
| -rw-r--r-- | apps/codecs/lib/xxx2wav.c | 270 | ||||
| -rw-r--r-- | apps/codecs/lib/xxx2wav.h | 69 | ||||
| -rw-r--r-- | apps/codecs/mpa.c | 521 | ||||
| -rw-r--r-- | apps/codecs/mpc.c | 214 | ||||
| -rw-r--r-- | apps/codecs/vorbis.c | 168 | ||||
| -rw-r--r-- | apps/codecs/wav.c | 136 | ||||
| -rw-r--r-- | apps/codecs/wavpack.c | 185 |
13 files changed, 2233 insertions, 2 deletions
diff --git a/apps/codecs/Makefile b/apps/codecs/Makefile index ffd8eab..970048e 100644 --- a/apps/codecs/Makefile +++ b/apps/codecs/Makefile @@ -10,20 +10,97 @@ INCLUDES = -I$(FIRMDIR)/include -I$(FIRMDIR)/export -I$(FIRMDIR)/common \ -I$(FIRMDIR)/drivers -I$(APPSDIR) -Ilib -I$(BUILDDIR) CFLAGS = $(GCCOPTS) $(INCLUDES) $(TARGET) $(EXTRA_DEFINES) \ - -DMEM=${MEMORYSIZE} + -DMEM=${MEMORYSIZE} -DCODEC ifdef APPEXTRA INCLUDES += -I$(APPSDIR)/$(APPEXTRA) endif +ifdef SOFTWARECODECS + CODECLIBS = -lmad -la52 -lFLAC -lTremor -lwavpack -lmusepack +endif + +# we "borrow" the plugin LDS file +LDS := $(APPSDIR)/plugins/plugin.lds + +LINKCODEC := $(OBJDIR)/codeclink.lds +DEPFILE = $(OBJDIR)/dep-codecs + +# This sets up 'SRC' based on the files mentioned in SOURCES +include $(TOOLSDIR)/makesrc.inc + +ROCKS := $(SRC:%.c=$(OBJDIR)/%.codec) +SOURCES = $(SRC) +ELFS := $(SRC:%.c=$(OBJDIR)/%.elf) +OBJS := $(SRC:%.c=$(OBJDIR)/%.o) +# as created by the cross-compiler for win32: +DEFS := $(SRC:%.c=$(OBJDIR)/%.def) +DIRS = . + .PHONY: libmad liba52 libFLAC libTremor libwavpack dumb libmusepack OUTPUT = $(SOFTWARECODECS) -all: $(OUTPUT) +all: $(OUTPUT) $(ROCKS) $(DEPFILE) + +ifndef SIMVER +$(OBJDIR)/%.elf: $(OBJDIR)/%.o $(LINKCODEC) + $(SILENT)(file=`basename $@`; \ + echo "LD $$file"; \ + $(CC) $(GCCOPTS) -O -nostdlib -o $@ $< -L$(BUILDDIR) $(CODECLIBS) -lcodec -lgcc -T$(LINKCODEC) -Wl,-Map,$(OBJDIR)/$*.map) + +$(OBJDIR)/%.codec : $(OBJDIR)/%.elf + @echo "OBJCOPY "`basename $@` + @$(OC) -O binary $< $@ +else + +ifeq ($(SIMVER), x11) +################################################### +# This is the X11 simulator version + +$(OBJDIR)/%.codec : $(OBJDIR)/%.o $(BUILDDIR)/libplugin.a + @echo "LD "`basename $@` + @$(CC) $(CFLAGS) -shared $< -L$(BUILDDIR) $(CODECLIBS) -lplugin -o $@ +ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN) +# 'x' must be kept or you'll have "Win32 error 5" +# $ fgrep 5 /usr/include/w32api/winerror.h | head -1 +# #define ERROR_ACCESS_DENIED 5L +else + @chmod -x $@ +endif + +else # end of x11-simulator +################################################### +# This is the win32 simulator version +DLLTOOLFLAGS = --export-all +DLLWRAPFLAGS = -s --entry _DllMain@12 --target=i386-mingw32 -mno-cygwin + +$(OBJDIR)/%.codec : $(OBJDIR)/%.o $(BUILDDIR)/libplugin.a + @echo "DLL "`basename $@` + @$(DLLTOOL) $(DLLTOOLFLAGS) -z $(OBJDIR)/$*.def $< + @$(DLLWRAP) $(DLLWRAPFLAGS) --def $(OBJDIR)/$*.def $< $(BUILDDIR)/libplugin.a \ + $(patsubst -l%,$(BUILDDIR)/lib%.a,$(CODECLIBS)) -o $@ +ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN) +# 'x' must be kept or you'll have "Win32 error 5" +# $ fgrep 5 /usr/include/w32api/winerror.h | head -1 +# #define ERROR_ACCESS_DENIED 5L +else + @chmod -x $@ +endif +endif # end of win32-simulator + +endif # end of simulator section include $(TOOLSDIR)/make.inc +$(BUILDDIR)/libcodec.a: + @echo "MAKE in codecs/lib" + @mkdir -p $(OBJDIR)/lib + @$(MAKE) -C lib OBJDIR=$(OBJDIR)/lib + +$(LINKCODEC): $(LDS) + @echo "build $@" + @cat $< | $(CC) -DMEMORYSIZE=$(MEMORYSIZE) -DCODEC $(INCLUDES) $(TARGET) $(DEFINES) -E -P - >$@ libmad: @echo "MAKE in libmad" diff --git a/apps/codecs/a52.c b/apps/codecs/a52.c new file mode 100644 index 0000000..d35854e --- /dev/null +++ b/apps/codecs/a52.c @@ -0,0 +1,210 @@ +/*************************************************************************** + * __________ __ ___. + * 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 <inttypes.h> /* Needed by a52.h */ +#include <codecs/liba52/config-a52.h> +#include <codecs/liba52/a52.h> + +#include "playback.h" +#include "lib/codeclib.h" + +#define BUFFER_SIZE 4096 + +struct codec_api* rb; +struct codec_api* ci; + +static float gain = 1; +static a52_state_t * state; +unsigned long samplesdone; +unsigned long frequency; + +/* Two buffers used outside liba52 */ +static uint8_t buf[3840] IDATA_ATTR; +static int16_t int16_samples[256*2] IDATA_ATTR; + +static inline int16_t convert (int32_t i) +{ + i >>= 15; + return (i > 32767) ? 32767 : ((i < -32768) ? -32768 : i); +} + +void output_audio(sample_t* samples,int flags) { + int i; + + flags &= A52_CHANNEL_MASK | A52_LFE; + + /* We may need to check the output format in flags - I'm not sure... */ + for (i = 0; i < 256; i++) { + int16_samples[2*i] = convert (samples[i]); + int16_samples[2*i+1] = convert (samples[i+256]); + } + + rb->yield(); + while(!ci->audiobuffer_insert((unsigned char*)int16_samples,256*2*2)) + rb->yield(); +} + + +void a52_decode_data (uint8_t * start, uint8_t * end) +{ + static uint8_t * bufptr = buf; + static uint8_t * bufpos = buf + 7; + + /* + * sample_rate and flags are static because this routine could + * exit between the a52_syncinfo() and the ao_setup(), and we want + * to have the same values when we get back ! + */ + + static int sample_rate; + static int flags; + int bit_rate; + int len; + + while (1) { + len = end - start; + if (!len) + break; + if (len > bufpos - bufptr) + len = bufpos - bufptr; + memcpy (bufptr, start, len); + bufptr += len; + start += len; + if (bufptr == bufpos) { + if (bufpos == buf + 7) { + int length; + + length = a52_syncinfo (buf, &flags, &sample_rate, &bit_rate); + if (!length) { + DEBUGF("skip\n"); + for (bufptr = buf; bufptr < buf + 6; bufptr++) + bufptr[0] = bufptr[1]; + continue; + } + bufpos = buf + length; + } else { + // The following two defaults are taken from audio_out_oss.c: + level_t level; + sample_t bias; + int i; + + /* This is the configuration for the downmixing: */ + flags=A52_STEREO|A52_ADJUST_LEVEL|A52_LFE; + level=(1 << 26); + bias=0; + + level = (level_t) (level * gain); + + if (a52_frame (state, buf, &flags, &level, bias)) { + goto error; + } + +// file_info->frames_decoded++; + +// /* We assume this never changes */ +// file_info->samplerate=sample_rate; + frequency=sample_rate; + + // An A52 frame consists of 6 blocks of 256 samples + // So we decode and output them one block at a time + for (i = 0; i < 6; i++) { + if (a52_block (state)) { + goto error; + } + + output_audio(a52_samples (state),flags); + samplesdone+=256; + } + ci->set_elapsed(samplesdone/(frequency/1000)); + bufptr = buf; + bufpos = buf + 7; + continue; + + error: + + //logf("Error decoding A52 stream\n"); + bufptr = buf; + bufpos = buf + 7; + } + } + } +} + +#ifndef SIMULATOR +extern char iramcopy[]; +extern char iramstart[]; +extern char iramend[]; +#endif + +/* this is the codec entry point */ +enum codec_status codec_start(struct codec_api* api, void* parm) +{ + size_t n; + unsigned char* filebuf; + + /* 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*2)); + ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*128)); + + next_track: + + if (codec_init(api)) { + return CODEC_ERROR; + } + + /* Intialise the A52 decoder and check for success */ + state = a52_init (0); // Parameter is "accel" + + /* The main decoding loop */ + + samplesdone=0; + while (1) { + if (ci->stop_codec || ci->reload_codec) { + break; + } + + filebuf=ci->request_buffer(&n,BUFFER_SIZE); + + if (n==0) { /* End of Stream */ + break; + } + + a52_decode_data(filebuf,filebuf+n); + + ci->advance_buffer(n); + } + + if (ci->request_next_track()) + goto next_track; + +//NOT NEEDED??: a52_free (state); + + return CODEC_OK; +} diff --git a/apps/codecs/flac.c b/apps/codecs/flac.c new file mode 100644 index 0000000..93134bb --- /dev/null +++ b/apps/codecs/flac.c @@ -0,0 +1,248 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 Björn Stenberg + * + * 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/libFLAC/include/FLAC/seekable_stream_decoder.h> +#include "playback.h" +#include "lib/codeclib.h" + +#define FLAC_MAX_SUPPORTED_BLOCKSIZE 4608 +#define FLAC_MAX_SUPPORTED_CHANNELS 2 + +static struct codec_api* rb; +static uint32_t samplesdone; + +/* Called when the FLAC decoder needs some FLAC data to decode */ +FLAC__SeekableStreamDecoderReadStatus flac_read_handler(const FLAC__SeekableStreamDecoder *dec, + FLAC__byte buffer[], unsigned *bytes, void *data) +{ struct codec_api* ci = (struct codec_api*)data; + (void)dec; + + *bytes=(unsigned)(ci->read_filebuf(buffer,*bytes)); + + if (*bytes==0) { + return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; + } else { + return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; + } +} + +static unsigned char pcmbuf[FLAC_MAX_SUPPORTED_BLOCKSIZE*FLAC_MAX_SUPPORTED_CHANNELS*2] IDATA_ATTR; + +/* Called when the FLAC decoder has some decoded PCM data to write */ +FLAC__StreamDecoderWriteStatus flac_write_handler(const FLAC__SeekableStreamDecoder *dec, + const FLAC__Frame *frame, + const FLAC__int32 * const buf[], + void *data) +{ + struct codec_api* ci = (struct codec_api*)data; + (void)dec; + unsigned int c_samp, c_chan, d_samp; + uint32_t data_size = frame->header.blocksize * frame->header.channels * 2; /* Assume 16-bit words */ + uint32_t samples = frame->header.blocksize; + int yieldcounter = 0; + + + if (samples*frame->header.channels > (FLAC_MAX_SUPPORTED_BLOCKSIZE*FLAC_MAX_SUPPORTED_CHANNELS)) { + // ERROR!!! + DEBUGF("ERROR: samples*frame->header.channels=%d\n",samples*frame->header.channels); + return(FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE); + } + + (void)dec; + for(c_samp = d_samp = 0; c_samp < samples; c_samp++) { + for(c_chan = 0; c_chan < frame->header.channels; c_chan++, d_samp++) { + pcmbuf[d_samp*2] = (buf[c_chan][c_samp]&0xff00)>>8; + pcmbuf[(d_samp*2)+1] = buf[c_chan][c_samp]&0xff; + if (yieldcounter++ == 100) { + rb->yield(); + yieldcounter = 0; + } + } + } + + samplesdone+=samples; + ci->set_elapsed(samplesdone/(ci->id3->frequency/1000)); + + rb->yield(); + while (!ci->audiobuffer_insert(pcmbuf, data_size)) + rb->yield(); + + return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; +} + +void flac_metadata_handler(const FLAC__SeekableStreamDecoder *dec, + const FLAC__StreamMetadata *meta, void *data) +{ + /* Ignore metadata for now... */ + (void)dec; + (void)meta; + (void)data; +} + + +void flac_error_handler(const FLAC__SeekableStreamDecoder *dec, + FLAC__StreamDecoderErrorStatus status, void *data) +{ + (void)dec; + (void)status; + (void)data; +} + +FLAC__SeekableStreamDecoderSeekStatus flac_seek_handler (const FLAC__SeekableStreamDecoder *decoder, + FLAC__uint64 absolute_byte_offset, + void *client_data) +{ + (void)decoder; + struct codec_api* ci = (struct codec_api*)client_data; + + if (ci->seek_buffer(absolute_byte_offset)) { + return(FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK); + } else { + return(FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR); + } +} + +FLAC__SeekableStreamDecoderTellStatus flac_tell_handler (const FLAC__SeekableStreamDecoder *decoder, + FLAC__uint64 *absolute_byte_offset, void *client_data) +{ + struct codec_api* ci = (struct codec_api*)client_data; + + (void)decoder; + *absolute_byte_offset=ci->curpos; + return(FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK); +} + +FLAC__SeekableStreamDecoderLengthStatus flac_length_handler (const FLAC__SeekableStreamDecoder *decoder, + FLAC__uint64 *stream_length, void *client_data) +{ + struct codec_api* ci = (struct codec_api*)client_data; + + (void)decoder; + *stream_length=ci->filesize; + return(FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK); +} + +FLAC__bool flac_eof_handler (const FLAC__SeekableStreamDecoder *decoder, + void *client_data) +{ + struct codec_api* ci = (struct codec_api*)client_data; + + (void)decoder; + if (ci->curpos >= ci->filesize) { + return(true); + } else { + return(false); + } +} + +#ifndef SIMULATOR +extern char iramcopy[]; +extern char iramstart[]; +extern char iramend[]; +#endif + +/* this is the codec entry point */ +enum codec_status codec_start(struct codec_api* api, void* parm) +{ + struct codec_api* ci = api; + FLAC__SeekableStreamDecoder* flacDecoder; + + /* Generic codec initialisation */ + TEST_CODEC_API(api); + + /* if you are using a global api pointer, don't forget to copy it! + otherwise you will get lovely "I04: IllInstr" errors... :-) */ + rb = 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)); + + next_track: + + if (codec_init(api)) { + return CODEC_ERROR; + } + + /* Create a decoder instance */ + + flacDecoder=FLAC__seekable_stream_decoder_new(); + + /* Set up the decoder and the callback functions - this must be done before init */ + + /* The following are required for stream_decoder and higher */ + FLAC__seekable_stream_decoder_set_client_data(flacDecoder,ci); + FLAC__seekable_stream_decoder_set_write_callback(flacDecoder, flac_write_handler); + FLAC__seekable_stream_decoder_set_read_callback(flacDecoder, flac_read_handler); + FLAC__seekable_stream_decoder_set_metadata_callback(flacDecoder, flac_metadata_handler); + FLAC__seekable_stream_decoder_set_error_callback(flacDecoder, flac_error_handler); + FLAC__seekable_stream_decoder_set_metadata_respond(flacDecoder, FLAC__METADATA_TYPE_STREAMINFO); + + /* The following are only for the seekable_stream_decoder */ + FLAC__seekable_stream_decoder_set_seek_callback(flacDecoder, flac_seek_handler); + FLAC__seekable_stream_decoder_set_tell_callback(flacDecoder, flac_tell_handler); + FLAC__seekable_stream_decoder_set_length_callback(flacDecoder, flac_length_handler); + FLAC__seekable_stream_decoder_set_eof_callback(flacDecoder, flac_eof_handler); + + + /* QUESTION: What do we do when the init fails? */ + if (FLAC__seekable_stream_decoder_init(flacDecoder)) { + return CODEC_ERROR; + } + + /* The first thing to do is to parse the metadata */ + FLAC__seekable_stream_decoder_process_until_end_of_metadata(flacDecoder); + + samplesdone=0; + ci->set_elapsed(0); + /* The main decoder loop */ + while (FLAC__seekable_stream_decoder_get_state(flacDecoder)!=FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM) { + rb->yield(); + if (ci->stop_codec || ci->reload_codec) { + break; + } + + if (ci->seek_time) { + int sample_loc; + + sample_loc = ci->seek_time/1000 * ci->id3->frequency; + if (FLAC__seekable_stream_decoder_seek_absolute(flacDecoder,sample_loc)) { + samplesdone=sample_loc; + ci->set_elapsed(samplesdone/(ci->id3->frequency/1000)); + } + ci->seek_time = 0; + } + + FLAC__seekable_stream_decoder_process_single(flacDecoder); + } + + /* Flush the libFLAC buffers */ + FLAC__seekable_stream_decoder_finish(flacDecoder); + + if (ci->request_next_track()) + goto next_track; + + return CODEC_OK; +} diff --git a/apps/codecs/lib/Makefile b/apps/codecs/lib/Makefile new file mode 100644 index 0000000..08dcd2a --- /dev/null +++ b/apps/codecs/lib/Makefile @@ -0,0 +1,46 @@ +# __________ __ ___. +# Open \______ \ ____ ____ | | _\_ |__ _______ ___ +# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +# \/ \/ \/ \/ \/ +# $Id$ +# + +# ../.. for the codec.h in the apps dir +# .. for stuff in the codecs dir +# . for stuff in the codeclib dir +INCLUDES=-I$(APPSDIR) -I.. -I. -I$(FIRMDIR)/include -I$(FIRMDIR)/export \ + -I$(FIRMDIR)/common -I$(BUILDDIR) + +ifdef APPEXTRA +INCLUDES += -I$(APPSDIR)/$(APPEXTRA) +endif + +CFLAGS = $(GCCOPTS) \ +$(INCLUDES) $(TARGET) $(EXTRA_DEFINES) -DMEM=${MEMORYSIZE} -DCODEC + +# This sets up 'SRC' based on the files mentioned in SOURCES +include $(TOOLSDIR)/makesrc.inc + +SOURCES = $(SRC) +OBJS := $(SRC:%.c=$(OBJDIR)/%.o) +DEPFILE = $(OBJDIR)/dep-codeclib +DIRS = . + +OUTPUT = $(BUILDDIR)/libcodec.a + +all: $(OUTPUT) + +$(OUTPUT): $(OBJS) + @echo "AR+RANLIB $@" + @$(AR) ruv $@ $+ >/dev/null 2>&1 + @$(RANLIB) $@ + +include $(TOOLSDIR)/make.inc + +clean: + @echo "cleaning lib" + @rm -f $(OBJS) $(OUTPUT) $(DEPFILE) + +-include $(DEPFILE) diff --git a/apps/codecs/lib/codeclib.c b/apps/codecs/lib/codeclib.c new file mode 100644 index 0000000..3aa6216 --- /dev/null +++ b/apps/codecs/lib/codeclib.c @@ -0,0 +1,38 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ + +/* "helper functions" common to all codecs */ + +#include "plugin.h" +#include "playback.h" +#include "codeclib.h" +#include "xxx2wav.h" + +struct codec_api *local_rb; + +int codec_init(struct codec_api* rb) +{ + local_rb = rb; + + xxx2wav_set_api(rb); + mem_ptr = 0; + mallocbuf = (unsigned char *)rb->get_codec_memory((size_t *)&bufsize); + + return 0; +} diff --git a/apps/codecs/lib/codeclib.h b/apps/codecs/lib/codeclib.h new file mode 100644 index 0000000..116f210 --- /dev/null +++ b/apps/codecs/lib/codeclib.h @@ -0,0 +1,49 @@ +/*************************************************************************** + * __________ __ ___. + * 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 "config.h" +#include "codecs.h" + +/* Various codec "helper functions" */ + +#if CONFIG_CPU == MCF5249 && !defined(SIMULATOR) +#define ICODE_ATTR __attribute__ ((section(".icode"))) +#define IDATA_ATTR __attribute__ ((section(".idata"))) +#define USE_IRAM 1 +#else +#define ICODE_ATTR +#define IDATA_ATTR +#endif + +extern int mem_ptr; +extern int bufsize; +extern unsigned char* mallocbuf; // 512K from the start of MP3 buffer + +void* codec_malloc(size_t size); +void* codec_calloc(size_t nmemb, size_t size); +void* codec_alloca(size_t size); +void* codec_realloc(void* ptr, size_t size); +void codec_free(void* ptr); +void *memcpy(void *dest, const void *src, size_t n); +void *memset(void *s, int c, size_t n); +int memcmp(const void *s1, const void *s2, size_t n); +void* memmove(const void *s1, const void *s2, size_t n); + +int codec_init(struct codec_api* rb); + diff --git a/apps/codecs/lib/xxx2wav.c b/apps/codecs/lib/xxx2wav.c new file mode 100644 index 0000000..1bc1837 --- /dev/null +++ b/apps/codecs/lib/xxx2wav.c @@ -0,0 +1,270 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ + +/* Various "helper functions" common to all the xxx2wav decoder plugins */ + +#if (CONFIG_HWCODEC == MASNONE) +/* software codec platforms, not for simulator */ + +#include "codecs.h" +#include "xxx2wav.h" + +static struct codec_api* local_rb; + +int mem_ptr; +int bufsize; +unsigned char* audiobuf; // The actual audio buffer from Rockbox +unsigned char* mallocbuf; // 512K from the start of audio buffer +unsigned char* filebuf; // The rest of the audio buffer + +void* codec_malloc(size_t size) +{ + void* x; + + x=&mallocbuf[mem_ptr]; + mem_ptr+=(size+3)&~3; // Keep memory 32-bit aligned (if it was already?) +/* + if(TIME_AFTER(*(local_rb->current_tick), last_tick + HZ)) { + char s[32]; + static long last_tick = 0; + local_rb->snprintf(s,30,"Memory used: %d",mem_ptr); + local_rb->lcd_putsxy(0,80,s); + + last_tick = *(local_rb->current_tick); + local_rb->lcd_update(); + }*/ + return(x); +} + +void* codec_calloc(size_t nmemb, size_t size) +{ + void* x; + x = codec_malloc(nmemb*size); + local_rb->memset(x,0,nmemb*size); + return(x); +} + +void* codec_alloca(size_t size) +{ + void* x; + x = codec_malloc(size); + return(x); +} + +void codec_free(void* ptr) { + (void)ptr; +} + +void* codec_realloc(void* ptr, size_t size) { + void* x; + (void)ptr; + x = codec_malloc(size); + return(x); +} + +void *memcpy(void *dest, const void *src, size_t n) { + return(local_rb->memcpy(dest,src,n)); +} + +void *memset(void *s, int c, size_t n) { + return(local_rb->memset(s,c,n)); +} + +int memcmp(const void *s1, const void *s2, size_t n) { + return(local_rb->memcmp(s1,s2,n)); +} + +void* memchr(const void *s, int c, size_t n) { + /* TO DO: Implement for Tremor */ + (void)s; + (void)c; + (void)n; + return(NULL); +} + +void* memmove(const void *s1, const void *s2, size_t n) { + char* dest=(char*)s1; + char* src=(char*)s2; + size_t i; + + for (i=0;i<n;i++) { dest[i]=src[i]; } + // while(n>0) { *(dest++)=*(src++); n--; } + return(dest); +} + +void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) { + local_rb->qsort(base,nmemb,size,compar); +} + +void display_status(file_info_struct* file_info) { + char s[32]; + unsigned long ticks_taken; + unsigned long long speed; + unsigned long xspeed; + static long last_tick = 0; + + if(TIME_AFTER(*(local_rb->current_tick), last_tick + HZ)) { + local_rb->snprintf(s,32,"Bytes read: %d",file_info->curpos); + local_rb->lcd_putsxy(0,0,s); + local_rb->snprintf(s,32,"Samples Decoded: %d",file_info->current_sample); + local_rb->lcd_putsxy(0,20,s); + local_rb->snprintf(s,32,"Frames Decoded: %d",file_info->frames_decoded); + local_rb->lcd_putsxy(0,40,s); + + ticks_taken=*(local_rb->current_tick)-file_info->start_tick; + + /* e.g.: + ticks_taken=500 + sam_fmt.rate=44,100 + samples_decoded=172,400 + (samples_decoded/sam_fmt.rate)*100=400 (time it should have taken) + % Speed=(400/500)*100=80% + */ + + if (ticks_taken==0) { ticks_taken=1; } // Avoid fp exception. + + speed=(100*file_info->current_sample)/file_info->samplerate; + xspeed=(speed*10000)/ticks_taken; + local_rb->snprintf(s,32,"Speed %ld.%02ld %% Secs: %d",(xspeed/100),(xspeed%100),ticks_taken/100); + local_rb->lcd_putsxy(0,60,s); + + last_tick = *(local_rb->current_tick); + local_rb->lcd_update(); + } +} + +#if 0 +static unsigned char wav_header[44]={'R','I','F','F', // 0 - ChunkID + 0,0,0,0, // 4 - ChunkSize (filesize-8) + 'W','A','V','E', // 8 - Format + 'f','m','t',' ', // 12 - SubChunkID + 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM + 1,0, // 20 - AudioFormat (1=16-bit) + 2,0, // 22 - NumChannels + 0,0,0,0, // 24 - SampleRate in Hz + 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8) + 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8) + 16,0, // 34 - BitsPerSample + 'd','a','t','a', // 36 - Subchunk2ID + 0,0,0,0 // 40 - Subchunk2Size + }; +#endif + +void xxx2wav_set_api(struct codec_api* rb) +{ + local_rb = rb; +} + +#if 0 +int local_init(char* infilename, char* outfilename, + file_info_struct* file_info, + struct codec_api* rb) +{ + char s[32]; + int i,n,bytesleft; + + local_rb=rb; + + mem_ptr=0; + audiobuf=local_rb->plugin_get_audio_buffer(&bufsize); + mallocbuf=audiobuf; + filebuf=&audiobuf[MALLOC_BUFSIZE]; + + local_rb->snprintf(s,32,"audio bufsize: %d",bufsize); + local_rb->lcd_putsxy(0,100,s); + local_rb->lcd_update(); + + file_info->infile=local_rb->open(infilename,O_RDONLY); + file_info->outfile=local_rb->creat(outfilename,O_WRONLY); + local_rb->write(file_info->outfile,wav_header,sizeof(wav_header)); + file_info->curpos=0; + file_info->current_sample=0; + file_info->frames_decoded=0; + file_info->filesize=local_rb->filesize(file_info->infile); + + local_rb->splash(HZ, true, "in: %d, size: %d", file_info->infile, file_info->filesize); + + if (file_info->filesize > (bufsize-MALLOC_BUFSIZE)) { + local_rb->close(file_info->infile); + local_rb->splash(HZ*2, true, "File too large"); + return(1); + } + + local_rb->snprintf(s,32,"Loading file..."); + local_rb->lcd_putsxy(0,0,s); + local_rb->lcd_update(); + + bytesleft=file_info->filesize; + i=0; + while (bytesleft > 0) { + n=local_rb->read(file_info->infile,&filebuf[i],bytesleft); + if (n < 0) { + local_rb->close(file_info->infile); + local_rb->splash(HZ*2, true, "ERROR READING FILE"); + return(1); + } + i+=n; bytesleft-=n; + } + local_rb->close(file_info->infile); + local_rb->lcd_clear_display(); + return(0); +} + +void close_wav(file_info_struct* file_info) +{ + int x; + int filesize=local_rb->filesize(file_info->outfile); + + /* We assume 16-bit, Stereo */ + + local_rb->lseek(file_info->outfile,0,SEEK_SET); + + // ChunkSize + x=filesize-8; + wav_header[4]=(x&0xff); + wav_header[5]=(x&0xff00)>>8; + wav_header[6]=(x&0xff0000)>>16; + wav_header[7]=(x&0xff000000)>>24; + + // Samplerate + wav_header[24]=file_info->samplerate&0xff; + wav_header[25]=(file_info->samplerate&0xff00)>>8; + wav_header[26]=(file_info->samplerate&0xff0000)>>16; + wav_header[27]=(file_info->samplerate&0xff000000)>>24; + + // ByteRate + x=file_info->samplerate*4; + wav_header[28]=(x&0xff); + wav_header[29]=(x&0xff00)>>8; + wav_header[30]=(x&0xff0000)>>16; + wav_header[31]=(x&0xff000000)>>24; + + // Subchunk2Size + x=filesize-44; + wav_header[40]=(x&0xff); + wav_header[41]=(x&0xff00)>>8; + wav_header[42]=(x&0xff0000)>>16; + wav_header[43]=(x&0xff000000)>>24; + + local_rb->write(file_info->outfile,wav_header,sizeof(wav_header)); + local_rb->close(file_info->outfile); +} +#endif /* 0 */ + +#endif /* CONFIG_HWCODEC == MASNONE */ diff --git a/apps/codecs/lib/xxx2wav.h b/apps/codecs/lib/xxx2wav.h new file mode 100644 index 0000000..1fa7dc9 --- /dev/null +++ b/apps/codecs/lib/xxx2wav.h @@ -0,0 +1,69 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ + +/* Various "helper functions" common to all the xxx2wav decoder plugins */ + +#if CONFIG_CPU == MCF5249 && !defined(SIMULATOR) +#define ICODE_ATTR __attribute__ ((section(".icode"))) +#define IDATA_ATTR __attribute__ ((section(".idata"))) +#define USE_IRAM 1 +#else +#define ICODE_ATTR +#define IDATA_ATTR +#endif + +/* the main data structure of the program */ +typedef struct { + int infile; + int outfile; + off_t curpos; + off_t filesize; + int samplerate; + int bitspersample; + int channels; + int frames_decoded; + unsigned long total_samples; + unsigned long current_sample; + unsigned long start_tick; +} file_info_struct; + +#define MALLOC_BUFSIZE (512*1024) + +extern int mem_ptr; +extern int bufsize; +extern unsigned char* mp3buf; // The actual MP3 buffer from Rockbox +extern unsigned char* mallocbuf; // 512K from the start of MP3 buffer +extern unsigned char* filebuf; // The rest of the MP3 buffer + +void* codec_malloc(size_t size); +void* codec_calloc(size_t nmemb, size_t size); +void* codec_alloca(size_t size); +void* codec_realloc(void* ptr, size_t size); +void codec_free(void* ptr); +void *memcpy(void *dest, const void *src, size_t n); +void *memset(void *s, int c, size_t n); +int memcmp(const void *s1, const void *s2, size_t n); +void* memmove(const void *s1, const void *s2, size_t n); + +void display_status(file_info_struct* file_info); +int local_init(char* infilename, char* outfilename, + file_info_struct* file_info, + struct codec_api* rb); +void close_wav(file_info_struct* file_info); +void xxx2wav_set_api(struct codec_api* rb); diff --git a/apps/codecs/mpa.c b/apps/codecs/mpa.c new file mode 100644 index 0000000..beb71d7 --- /dev/null +++ b/apps/codecs/mpa.c @@ -0,0 +1,521 @@ +/*************************************************************************** + * __________ __ ___. + * 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/libmad/mad.h> + +#include "playback.h" +#include "mp3data.h" +#include "lib/codeclib.h" + +static struct codec_api* rb; + +struct mad_stream Stream IDATA_ATTR; +struct mad_frame Frame IDATA_ATTR; +struct mad_synth Synth IDATA_ATTR; +mad_timer_t Timer; +struct dither d0, d1; + +/* The following function is used inside libmad - let's hope it's never + called. +*/ + +void abort(void) { +} + +/* The "dither" code to convert the 24-bit samples produced by libmad was + taken from the coolplayer project - coolplayer.sourceforge.net */ + +struct dither { + mad_fixed_t error[3]; + mad_fixed_t random; +}; + +# define SAMPLE_DEPTH 16 +# define scale(x, y) dither((x), (y)) + +/* + * NAME: prng() + * DESCRIPTION: 32-bit pseudo-random number generator + */ +static __inline +unsigned long prng(unsigned long state) +{ + return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL; +} + +/* + * NAME: dither() + * DESCRIPTION: dither and scale sample + */ +static __inline +signed int dither(mad_fixed_t sample, struct dither *dither) +{ + unsigned int scalebits; + mad_fixed_t output, mask, random; + + enum { + MIN = -MAD_F_ONE, + MAX = MAD_F_ONE - 1 + }; + + /* noise shape */ + sample += dither->error[0] - dither->error[1] + dither->error[2]; + + dither->error[2] = dither->error[1]; + dither->error[1] = dither->error[0] / 2; + + /* bias */ + output = sample + (1L << (MAD_F_FRACBITS + 1 - SAMPLE_DEPTH - 1)); + + scalebits = MAD_F_FRACBITS + 1 - SAMPLE_DEPTH; + mask = (1L << scalebits) - 1; + + /* dither */ + random = prng(dither->random); + output += (random & mask) - (dither->random & mask); + + //dither->random = random; + + /* clip */ + if (output > MAX) { + output = MAX; + + if (sample > MAX) + sample = MAX; + } + else if (output < MIN) { + output = MIN; + + if (sample < MIN) + sample = MIN; + } + + /* quantize */ + output &= ~mask; + + /* error feedback */ + dither->error[0] = sample - output; + + /* scale */ + return output >> scalebits; +} + +static __inline +signed int detect_silence(mad_fixed_t sample) +{ + unsigned int scalebits; + mad_fixed_t output, mask; + + enum { + MIN = -MAD_F_ONE, + MAX = MAD_F_ONE - 1 + }; + + /* bias */ + output = sample + (1L << (MAD_F_FRACBITS + 1 - SAMPLE_DEPTH - 1)); + + scalebits = MAD_F_FRACBITS + 1 - SAMPLE_DEPTH; + mask = (1L << scalebits) - 1; + + /* clip */ + if (output > MAX) { + output = MAX; + + if (sample > MAX) + sample = MAX; + } + else if (output < MIN) { + output = MIN; + + if (sample < MIN) + sample = MIN; + } + + /* quantize */ + output &= ~mask; + + /* scale */ + output >>= scalebits + 4; + + if (output == 0x00 || output == 0xff) + return 1; + + return 0; +} +#define SHRT_MAX 32767 + +#define INPUT_CHUNK_SIZE 8192 +#define OUTPUT_BUFFER_SIZE 65536 /* Must be an integer multiple of 4. */ + +unsigned char OutputBuffer[OUTPUT_BUFFER_SIZE]; +unsigned char *OutputPtr; +unsigned char *GuardPtr=NULL; +const unsigned char *OutputBufferEnd=OutputBuffer+OUTPUT_BUFFER_SIZE; +long resampled_data[2][5000]; /* enough to cope with 11khz upsampling */ + +mad_fixed_t mad_frame_overlap[2][32][18] IDATA_ATTR; +unsigned char mad_main_data[MAD_BUFFER_MDLEN] IDATA_ATTR; +/* TODO: what latency does layer 1 have? */ +int mpeg_latency[3] = { 0, 481, 529 }; +#ifdef USE_IRAM +extern char iramcopy[]; +extern char iramstart[]; +extern char iramend[]; +#endif + +#undef DEBUG_GAPLESS + +struct resampler { + long last_sample, phase, delta; +}; + +#if CONFIG_CPU==MCF5249 && !defined(SIMULATOR) + +#define INIT() asm volatile ("move.l #0xb0, %macsr") /* frac, round, clip */ +#define FRACMUL(x, y) \ +({ \ + long t; \ + asm volatile ("mac.l %[a], %[b], %%acc0\n\t" \ + "movclr.l %%acc0, %[t]\n\t" \ + : [t] "=r" (t) : [a] "r" (x), [b] "r" (y)); \ + t; \ +}) + +#else + +#define INIT() +#define FRACMUL(x, y) (long)(((long long)(x)*(long long)(y)) << 1) +#endif + +/* linear resampling, introduces one sample delay, because of our inability to + look into the future at the end of a frame */ +long downsample(long *in, long *out, int num, struct resampler *s) +{ + long i = 1, pos; + long last = s->last_sample; + + INIT(); + pos = s->phase >> 16; + /* check if we need last sample of previous frame for interpolation */ + if (pos > 0) + last = in[pos - 1]; + out[0] = last + FRACMUL((s->phase & 0xffff) << 15, in[pos] - last); + s->phase += s->delta; + while ((pos = s->phase >> 16) < num) { + out[i++] = in[pos - 1] + FRACMUL((s->phase & 0xffff) << 15, in[pos] - in[pos - 1]); + s->phase += s->delta; + } + /* wrap phase accumulator back to start of next frame */ + s->phase -= num << 16; + s->last_sample = in[num - 1]; + return i; +} + +long upsample(long *in, long *out, int num, struct resampler *s) +{ + long i = 0, pos; + + INIT(); + while ((pos = s->phase >> 16) == 0) { + out[i++] = s->last_sample + FRACMUL((s->phase & 0xffff) << 15, in[pos] - s->last_sample); + s->phase += s->delta; + } + while ((pos = s->phase >> 16) < num) { + out[i++] = in[pos - 1] + FRACMUL((s->phase & 0xffff) << 15, in[pos] - in[pos - 1]); + s->phase += s->delta; + } + /* wrap phase accumulator back to start of next frame */ + s->phase -= num << 16; + s->last_sample = in[num - 1]; + return i; +} + +long resample(long *in, long *out, int num, struct resampler *s) +{ + if (s->delta >= (1 << 16)) + return downsample(in, out, num, s); + else + return upsample(in, out, num, s); +} + +/* this is the codec entry point */ +enum codec_status codec_start(struct codec_api* api, void* parm) +{ + struct codec_api *ci = api; + struct mp3info *info; + int Status=0; + size_t size; + int file_end; + unsigned short Sample; + char *InputBuffer; + unsigned int samplecount; + unsigned int samplesdone; + bool first_frame; +#ifdef DEBUG_GAPLESS + bool first = true; + int fd; +#endif + int i; + int yieldcounter = 0; + int stop_skip, start_skip; + struct resampler lr = { 0, 0, 0 }, rr = { 0, 0, 0 }; + long length; + /* Generic codec inititialisation */ + (void)parm; + + TEST_CODEC_API(api); + rb = api; + +#ifdef USE_IRAM + rb->memcpy(iramstart, iramcopy, iramend-iramstart); +#endif + + /* This function sets up the buffers and reads the file into RAM */ + + if (codec_init(api)) { + return CODEC_ERROR; + } + + /* Create a decoder instance */ + + ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*2)); + ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*16)); + + memset(&Stream, 0, sizeof(struct mad_stream)); + memset(&Frame, 0, sizeof(struct mad_frame)); + memset(&Synth, 0, sizeof(struct mad_synth)); + memset(&Timer, 0, sizeof(mad_timer_t)); + + mad_stream_init(&Stream); + mad_frame_init(&Frame); + mad_synth_init(&Synth); + mad_timer_reset(&Timer); + + /* We do this so libmad doesn't try to call codec_calloc() */ + memset(mad_frame_overlap, 0, sizeof(mad_frame_overlap)); + Frame.overlap = &mad_frame_overlap; + Stream.main_data = &mad_main_data; + /* This label might need to be moved above all the init code, but I don't + think reiniting the codec is necessary for MPEG. It might even be unwanted + for gapless playback */ + next_track: + +#ifdef DEBUG_GAPLESS + if (first) + fd = rb->open("/first.pcm", O_WRONLY | O_CREAT); + else + fd = rb->open("/second.pcm", O_WRONLY | O_CREAT); + first = false; +#endif + + info = ci->mp3data; + first_frame = false; + file_end = 0; + OutputPtr = OutputBuffer; + + while (!*ci->taginfo_ready) + rb->yield(); + + ci->request_buffer(&size, ci->id3->first_frame_offset); + ci->advance_buffer(size); + + if (info->enc_delay >= 0 && info->enc_padding >= 0) { + stop_skip = info->enc_padding - mpeg_latency[info->layer]; + if (stop_skip < 0) stop_skip = 0; + start_skip = info->enc_delay + mpeg_latency[info->layer]; + } else { + stop_skip = 0; + /* We want to skip this amount anyway */ + start_skip = mpeg_latency[info->layer]; + } + + /* NOTE: currently this doesn't work, the below calculated samples_count + seems to be right, but sometimes libmad just can't supply us with + all the data we need... */ + if (info->frame_count) { + /* TODO: 1152 is the frame size in samples for MPEG1 layer 2 and layer 3, + it's probably not correct at all for MPEG2 and layer 1 */ + samplecount = info->frame_count*1152 - (start_skip + stop_skip); + samplesdone = ci->id3->elapsed * (ci->id3->frequency / 100) / 10; + } else { + samplecount = ci->id3->length * (ci->id3->frequency / 100) / 10; + samplesdone = ci->id3->elapsed * (ci->id3->frequency / 100) / 10; + } + /* rb->snprintf(buf2, sizeof(buf2), "sc: %d", samplecount); + rb->splash(0, true, buf2); + rb->snprintf(buf2, sizeof(buf2), "length: %d", ci->id3->length); + rb->splash(HZ*5, true, buf2); + rb->snprintf(buf2, sizeof(buf2), "frequency: %d", ci->id3->frequency); + rb->splash(HZ*5, true, buf2); */ + lr.delta = rr.delta = ci->id3->frequency*65536/44100; + /* This is the decoding loop. */ + while (1) { + rb->yield(); + if (ci->stop_codec || ci->reload_codec) { + break ; + } + + if (ci->seek_time) { + unsigned int sample_loc; + int newpos; + + sample_loc = ci->seek_time/1000 * ci->id3->frequency; + newpos = ci->mp3_get_filepos(ci->seek_time-1); + if (ci->seek_buffer(newpos)) { + if (sample_loc >= samplecount + samplesdone) + break ; + samplecount += samplesdone - sample_loc; + samplesdone = sample_loc; + } + ci->seek_time = 0; + } + + /* Lock buffers */ + if (Stream.error == 0) { + InputBuffer = ci->request_buffer(&size, INPUT_CHUNK_SIZE); + if (size == 0 || InputBuffer == NULL) + break ; + mad_stream_buffer(&Stream, InputBuffer, size); + } + + //if ((int)ci->curpos >= ci->id3->first_frame_offset) + //first_frame = true; + + if(mad_frame_decode(&Frame,&Stream)) + { + if (Stream.error == MAD_FLAG_INCOMPLETE || Stream.error == MAD_ERROR_BUFLEN) { + // rb->splash(HZ*1, true, "Incomplete"); + /* This makes the codec to support partially corrupted files too. */ + if (file_end == 30) + break ; + + /* Fill the buffer */ + Stream.error = 0; + file_end++; + continue ; + } + else if(MAD_RECOVERABLE(Stream.error)) + { + if(Stream.error!=MAD_ERROR_LOSTSYNC || Stream.this_frame!=GuardPtr) + { + // rb->splash(HZ*1, true, "Recoverable...!"); + } + continue; + } + else if(Stream.error==MAD_ERROR_BUFLEN) { + //rb->splash(HZ*1, true, "Buflen error"); + break ; + } else { + //rb->splash(HZ*1, true, "Unrecoverable error"); + Status=1; + break; + } + } + if (Stream.next_frame) + ci->advance_buffer_loc((void *)Stream.next_frame); + file_end = false; + /* ?? Do we need the timer module? */ + // mad_timer_add(&Timer,Frame.header.duration); + +/* DAVE: This can be used to attenuate the audio */ +// if(DoFilter) +// ApplyFilter(&Frame); + + mad_synth_frame(&Synth,&Frame); + + //if (!first_frame) { + //samplecount -= Synth.pcm.length; + //continue ; + //} + + /* Convert MAD's numbers to an array of 16-bit LE signed integers */ + /* We skip start_skip number of samples here, this should only happen for + very first frame in the stream. */ + /* TODO: possible for start_skip to exceed one frames worth of samples? */ + length = resample((long *)&Synth.pcm.samples[0][start_skip], resampled_data[0], Synth.pcm.length, &lr); + if (MAD_NCHANNELS(&Frame.header) == 2) + resample((long *)&Synth.pcm.samples[1][start_skip], resampled_data[1], Synth.pcm.length, &rr); + for (i = 0;i<length;i++) + { + start_skip = 0; /* not very elegant, and might want to keep this value */ + samplesdone++; + //if (ci->mp3data->padding > 0) { + // ci->mp3data->padding--; + // continue ; + //} + /*if (!first_frame) { + if (detect_silence(Synth.pcm.samples[0][i])) + continue ; + first_frame = true; + }*/ + + /* Left channel */ + Sample=scale(resampled_data[0][i],&d0); + *(OutputPtr++)=Sample>>8; + *(OutputPtr++)=Sample&0xff; + + /* Right channel. If the decoded stream is monophonic then + * the right output channel is the same as the left one. + */ + if(MAD_NCHANNELS(&Frame.header)==2) + Sample=scale(resampled_data[1][i],&d1); + *(OutputPtr++)=Sample>>8; + *(OutputPtr++)=Sample&0xff; + + samplecount--; + if (samplecount == 0) { +#ifdef DEBUG_GAPLESS + rb->write(fd, OutputBuffer, (int)OutputPtr-(int)OutputBuffer); +#endif + while (!ci->audiobuffer_insert(OutputBuffer, (int)OutputPtr-(int)OutputBuffer)) + rb->yield(); + goto song_end; + } + + if (yieldcounter++ == 200) { + rb->yield(); + yieldcounter = 0; + } + + /* Flush the buffer if it is full. */ + if(OutputPtr==OutputBufferEnd) + { +#ifdef DEBUG_GAPLESS + rb->write(fd, OutputBuffer, OUTPUT_BUFFER_SIZE); +#endif + while (!ci->audiobuffer_insert(OutputBuffer, OUTPUT_BUFFER_SIZE)) + rb->yield(); + OutputPtr=OutputBuffer; + } + } + ci->set_elapsed(samplesdone / (ci->id3->frequency/1000)); + } + + song_end: +#ifdef DEBUG_GAPLESS + rb->close(fd); +#endif + Stream.error = 0; + + if (ci->request_next_track()) + goto next_track; + return CODEC_OK; +} diff --git a/apps/codecs/mpc.c b/apps/codecs/mpc.c new file mode 100644 index 0000000..9b4a616 --- /dev/null +++ b/apps/codecs/mpc.c @@ -0,0 +1,214 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2005 Thom Johansen + * + * 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 "playback.h" +#include "lib/codeclib.h" +#include <codecs/libmusepack/musepack.h> + +static struct codec_api* rb; +mpc_decoder decoder; + +/* + Our implementations of the mpc_reader callback functions. +*/ +mpc_int32_t +read_impl(void *data, void *ptr, mpc_int32_t size) +{ + struct codec_api* ci = (struct codec_api*)data; + + return((mpc_int32_t)(ci->read_filebuf(ptr,size))); +} + +bool +seek_impl(void *data, mpc_int32_t offset) +{ + struct codec_api* ci = (struct codec_api*)data; + + /* WARNING: assumes we don't need to skip too far into the past, + this might not be supported by the buffering layer yet */ + return ci->seek_buffer(offset); +} + +mpc_int32_t +tell_impl(void *data) +{ + struct codec_api* ci = (struct codec_api*)data; + + return ci->curpos; +} + +mpc_int32_t +get_size_impl(void *data) +{ + struct codec_api* ci = (struct codec_api*)data; + return ci->filesize; +} + +bool +canseek_impl(void *data) +{ + (void)data; + return false; +} + +static int +shift_signed(MPC_SAMPLE_FORMAT val, int shift) +{ + if (shift > 0) + val <<= shift; + else if (shift < 0) + val >>= -shift; + return (int)val; +} + +#define OUTPUT_BUFFER_SIZE 65536 /* Must be an integer multiple of 4. */ + +unsigned char OutputBuffer[OUTPUT_BUFFER_SIZE]; +/* temporary, we probably have better use for iram than this */ +MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH] IDATA_ATTR; +unsigned char *OutputPtr=OutputBuffer; +const unsigned char *OutputBufferEnd=OutputBuffer+OUTPUT_BUFFER_SIZE; + +#ifdef USE_IRAM +extern char iramcopy[]; +extern char iramstart[]; +extern char iramend[]; +#endif + +/* this is the codec entry point */ +enum codec_status codec_start(struct codec_api* api, void* parm) +{ + struct codec_api* ci = api; + unsigned short Sample; + unsigned long samplesdone; + unsigned long frequency; + unsigned status = 1; + unsigned int i; + mpc_reader reader; + + /* Generic codec inititialisation */ + + TEST_CODEC_API(api); + rb = api; + +#ifndef SIMULATOR + rb->memcpy(iramstart, iramcopy, iramend-iramstart); +#endif + + ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*2)); + ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*16)); + + next_track: + + if (codec_init(api)) { + return CODEC_ERROR; + } + + /* Create a decoder instance */ + + reader.read = read_impl; + reader.seek = seek_impl; + reader.tell = tell_impl; + reader.get_size = get_size_impl; + reader.canseek = canseek_impl; + reader.data = ci; + + /* read file's streaminfo data */ + mpc_streaminfo info; + mpc_streaminfo_init(&info); + if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) { + return CODEC_ERROR; + } + frequency=info.sample_freq; + + /* instantiate a decoder with our file reader */ + mpc_decoder_setup(&decoder, &reader); + if (!mpc_decoder_initialize(&decoder, &info)) { + return CODEC_ERROR; + } + + /* Initialise the output buffer. */ + OutputPtr=OutputBuffer; + OutputBufferEnd=OutputBuffer+OUTPUT_BUFFER_SIZE; + + /* This is the decoding loop. */ + samplesdone=0; + while (status != 0) { + if (ci->stop_codec || ci->reload_codec) { + break; + } + + status = mpc_decoder_decode(&decoder, sample_buffer, 0, 0); + if (status == (unsigned)(-1)) { + //decode error + return CODEC_ERROR; + } + else //status>0 + { + // file_info.current_sample += status; + // file_info.frames_decoded++; + /* Convert musepack's numbers to an array of 16-bit BE signed integers */ + for(i = 0; i < status*info.channels; i += info.channels) + { + /* Left channel */ + Sample=shift_signed(sample_buffer[i], 16 - MPC_FIXED_POINT_SCALE_SHIFT); + *(OutputPtr++)=Sample>>8; + *(OutputPtr++)=Sample&0xff; + + /* Right channel. If the decoded stream is monophonic then + * the right output channel is the same as the left one. + */ + if(info.channels==2) { + Sample=shift_signed(sample_buffer[i + 1], 16 - MPC_FIXED_POINT_SCALE_SHIFT); + } + *(OutputPtr++)=Sample>>8; + *(OutputPtr++)=Sample&0xff; + + samplesdone++; + + /* Flush the buffer if it is full. */ + if(OutputPtr==OutputBufferEnd) + { + rb->yield(); + while (!ci->audiobuffer_insert(OutputBuffer, OUTPUT_BUFFER_SIZE)) { + rb->yield(); + } + + ci->set_elapsed(samplesdone/(frequency/1000)); + OutputPtr=OutputBuffer; + } + } + } + } + + /* Flush the remaining data in the output buffer */ + if (OutputPtr > OutputBuffer) { + rb->yield(); + while (!ci->audiobuffer_insert(OutputBuffer, OutputPtr-OutputBuffer)) { + rb->yield(); + } + } + + if (ci->request_next_track()) + goto next_track; + + return CODEC_OK; +} + diff --git a/apps/codecs/vorbis.c b/apps/codecs/vorbis.c new file mode 100644 index 0000000..969fdf3 --- /dev/null +++ b/apps/codecs/vorbis.c @@ -0,0 +1,168 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 Björn Stenberg + * + * 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 "kernel.h" +#include "codecs.h" + +#include "Tremor/ivorbisfile.h" +#include "playback.h" +#include "lib/codeclib.h" + +static struct codec_api* rb; + +/* Some standard functions and variables needed by Tremor */ + +int errno; + +size_t strlen(const char *s) +{ + return(rb->strlen(s)); +} + +char *strcpy(char *dest, const char *src) +{ + return(rb->strcpy(dest,src)); +} + +char *strcat(char *dest, const char *src) +{ + return(rb->strcat(dest,src)); +} + +size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource) +{ + return rb->read_filebuf(ptr, nmemb*size); +} + +int seek_handler(void *datasource, ogg_int64_t offset, int whence) +{ + /* We are not seekable at the moment */ + (void)datasource; + (void)offset; + (void)whence; + return -1; +} + +int close_handler(void *datasource) +{ + (void)datasource; + return 0; +} + +long tell_handler(void *datasource) +{ + return rb->curpos; +} + +#ifdef USE_IRAM +extern char iramcopy[]; +extern char iramstart[]; +extern char iramend[]; +#endif + + +/* reserve the PCM buffer in the IRAM area */ +static char pcmbuf[4096] IDATA_ATTR; + +/* this is the codec entry point */ +enum codec_status codec_start(struct codec_api* api, void* parm) +{ + ov_callbacks callbacks; + OggVorbis_File vf; + vorbis_info* vi; + + int error; + long n; + int current_section; + int eof; +#if BYTE_ORDER == BIG_ENDIAN + int i; + char x; +#endif + + TEST_CODEC_API(api); + + /* if you are using a global api pointer, don't forget to copy it! + otherwise you will get lovely "I04: IllInstr" errors... :-) */ + rb = api; + +#ifdef USE_IRAM + rb->memcpy(iramstart, iramcopy, iramend-iramstart); +#endif + + rb->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*2)); + rb->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*64)); + + /* We need to flush reserver memory every track load. */ + next_track: + if (codec_init(rb)) { + return CODEC_ERROR; + } + + + /* Create a decoder instance */ + + callbacks.read_func=read_handler; + callbacks.seek_func=seek_handler; + callbacks.tell_func=tell_handler; + callbacks.close_func=close_handler; + + error=ov_open_callbacks(rb,&vf,NULL,0,callbacks); + + vi=ov_info(&vf,-1); + + if (vi==NULL) { + // rb->splash(HZ*2, true, "Vorbis Error"); + return CODEC_ERROR; + } + + eof=0; + while (!eof) { + /* Read host-endian signed 16 bit PCM samples */ + n=ov_read(&vf,pcmbuf,sizeof(pcmbuf),¤t_section); + + if (n==0) { + eof=1; + } + else if (n < 0) { + DEBUGF("Error decoding frame\n"); + } else { + rb->yield(); + if (rb->stop_codec || rb->reload_codec) + break ; + + rb->yield(); + while (!rb->audiobuffer_insert(pcmbuf, n)) + rb->yield(); + + rb->set_elapsed(ov_time_tell(&vf)); + +#if BYTE_ORDER == BIG_ENDIAN + for (i=0;i<n;i+=2) { + x=pcmbuf[i]; pcmbuf[i]=pcmbuf[i+1]; pcmbuf[i+1]=x; + } +#endif + } + } + + if (rb->request_next_track()) + goto next_track; + + return CODEC_OK; +} + diff --git a/apps/codecs/wav.c b/apps/codecs/wav.c new file mode 100644 index 0000000..d750b64 --- /dev/null +++ b/apps/codecs/wav.c @@ -0,0 +1,136 @@ +/*************************************************************************** + * __________ __ ___. + * 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 "playback.h" +#include "lib/codeclib.h" + +#define BYTESWAP(x) (((x>>8) & 0xff) | ((x<<8) & 0xff00)) + +/* Number of bytes to process in one iteration */ +#define WAV_CHUNK_SIZE (1024*4) + +#ifndef SIMULATOR +extern char iramcopy[]; +extern char iramstart[]; +extern char iramend[]; +#endif + +/* this is the codec entry point */ +enum codec_status codec_start(struct codec_api* api, void* parm) +{ + struct codec_api* rb = api; + struct codec_api* ci = api; + unsigned long samplerate,numbytes,totalsamples,samplesdone,nsamples; + int channels,bytespersample,bitspersample; + unsigned int i; + size_t n; + int endofstream; + unsigned char* header; + unsigned short* wavbuf; + + /* Generic codec initialisation */ + TEST_CODEC_API(api); + + /* if you are using a global api pointer, don't forget to copy it! + otherwise you will get lovely "I04: IllInstr" errors... :-) */ + rb = 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*256)); + + next_track: + + if (codec_init(api)) { + return CODEC_ERROR; + } + + /* FIX: Correctly parse WAV header - we assume canonical 44-byte header */ + + header=ci->request_buffer(&n,44); + if (n!=44) { + return CODEC_ERROR; + } + if ((memcmp(header,"RIFF",4)!=0) || (memcmp(&header[8],"WAVEfmt",7)!=0)) { + return CODEC_ERROR; + } + + samplerate=header[24]|(header[25]<<8)|(header[26]<<16)|(header[27]<<24); + bitspersample=header[34]; + channels=header[22]; + bytespersample=((bitspersample/8)*channels); + numbytes=(header[40]|(header[41]<<8)|(header[42]<<16)|(header[43]<<24)); + totalsamples=numbytes/bytespersample; + + if ((bitspersample!=16) || (channels != 2)) { + return CODEC_ERROR; + } + + ci->advance_buffer(44); + + /* The main decoder loop */ + + samplesdone=0; + ci->set_elapsed(0); + endofstream=0; + while (!endofstream) { + if (ci->stop_codec || ci->reload_codec) { + break; + } + + wavbuf=ci->request_buffer(&n,WAV_CHUNK_SIZE); + + if (n==0) break; /* End of stream */ + + nsamples=(n/bytespersample); + + /* WAV files can contain extra data at the end - so we can't just + process until the end of the file */ + + if (samplesdone+nsamples > totalsamples) { + nsamples=(totalsamples-samplesdone); + n=nsamples*bytespersample; + endofstream=1; + } + + /* Byte-swap data */ + for (i=0;i<n/2;i++) { + wavbuf[i]=BYTESWAP(wavbuf[i]); + } + + samplesdone+=nsamples; + ci->set_elapsed(samplesdone/(ci->id3->frequency/1000)); + + rb->yield(); + while (!ci->audiobuffer_insert((unsigned char*)wavbuf, n)) + rb->yield(); + + ci->advance_buffer(n); + } + + if (ci->request_next_track()) + goto next_track; + + return CODEC_OK; +} diff --git a/apps/codecs/wavpack.c b/apps/codecs/wavpack.c new file mode 100644 index 0000000..e18dac6 --- /dev/null +++ b/apps/codecs/wavpack.c @@ -0,0 +1,185 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2005 David Bryant + * + * 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/libwavpack/wavpack.h> +#include "playback.h" +#include "lib/codeclib.h" + +static struct codec_api *rb; +static struct codec_api *ci; + +#define BUFFER_SIZE 4096 + +static long temp_buffer [BUFFER_SIZE] IDATA_ATTR; + +static long read_callback (void *buffer, long bytes) +{ + return ci->read_filebuf (buffer, bytes); +} + +#ifndef SIMULATOR +extern char iramcopy[]; +extern char iramstart[]; +extern char iramend[]; +#endif + +/* this is the codec entry point */ +enum codec_status codec_start(struct codec_api* api, void* parm) +{ + WavpackContext *wpc; + char error [80]; + int bps, nchans; + + /* Generic codec initialisation */ + TEST_CODEC_API(api); + + rb = api; + ci = 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)); + + next_track: + + if (codec_init(api)) + return CODEC_ERROR; + + /* Create a decoder instance */ + + wpc = WavpackOpenFileInput (read_callback, error); + + if (!wpc) + return CODEC_ERROR; + + bps = WavpackGetBytesPerSample (wpc); + nchans = WavpackGetReducedChannels (wpc); + + ci->set_elapsed (0); + + /* The main decoder loop */ + + while (1) { + long nsamples; + + if (ci->seek_time && ci->taginfo_ready && ci->id3->length) { + int curpos_ms = (WavpackGetSampleIndex (wpc) + 220) / 441 * 10; + int n, d, skip; + + if (ci->seek_time > curpos_ms) { + n = ci->seek_time - curpos_ms; + d = ci->id3->length - curpos_ms; + skip = (int)((long long)(ci->filesize - ci->curpos) * n / d); + ci->seek_buffer (ci->curpos + skip); + } + else { + n = curpos_ms - ci->seek_time; + d = curpos_ms; + skip = (int)((long long) ci->curpos * n / d); + ci->seek_buffer (ci->curpos - skip); + } + + wpc = WavpackOpenFileInput (read_callback, error); + ci->seek_time = 0; + + if (!wpc) + break; + + ci->set_elapsed ((int)((long long) WavpackGetSampleIndex (wpc) * 1000 / 44100)); + rb->yield (); + } + + nsamples = WavpackUnpackSamples (wpc, temp_buffer, BUFFER_SIZE / 2); + + if (!nsamples || ci->stop_codec || ci->reload_codec) + break; + + /* convert mono to stereo here, in place */ + + if (nchans == 1) { + long *dst = temp_buffer + (nsamples * 2); + long *src = temp_buffer + nsamples; + long count = nsamples; + + while (count--) { + *--dst = *--src; + *--dst = *src; + if (!(count & 0x7f)) + rb->yield (); + } + } + + if (bps == 1) { + short *dst = (short *) temp_buffer; + long *src = temp_buffer; + long count = nsamples; + + while (count--) { + *dst++ = *src++ << 8; + *dst++ = *src++ << 8; + if (!(count & 0x7f)) + rb->yield (); + } + } + else if (bps == 2) { + short *dst = (short *) temp_buffer; + long *src = temp_buffer; + long count = nsamples; + + while (count--) { + *dst++ = *src++; + *dst++ = *src++; + if (!(count & 0x7f)) + rb->yield (); + } + } + else { + short *dst = (short *) temp_buffer; + int shift = (bps - 2) * 8; + long *src = temp_buffer; + long count = nsamples; + + while (count--) { + *dst++ = *src++ >> shift; + *dst++ = *src++ >> shift; + if (!(count & 0x7f)) + rb->yield (); + } + } + + if (ci->stop_codec || ci->reload_codec) + break; + + while (!ci->audiobuffer_insert ((char *) temp_buffer, nsamples * 4)) + rb->yield (); + + ci->set_elapsed ((WavpackGetSampleIndex (wpc) + 220) / 441 * 10); + } + + if (ci->request_next_track()) + goto next_track; + + return CODEC_OK; +} |