diff options
| author | Linus Nielsen Feltzing <linus@haxx.se> | 2005-06-05 23:05:10 +0000 |
|---|---|---|
| committer | Linus Nielsen Feltzing <linus@haxx.se> | 2005-06-05 23:05:10 +0000 |
| commit | 1c497e60457a2e125a3e4fc839d6453bfe585834 (patch) | |
| tree | 4688a25afda658a88e900a0c5a6d4f6f755c3268 /apps/plugins | |
| parent | b1e1e44041f7c078a8a1e4f31ab0cde03efd1b2a (diff) | |
| download | rockbox-1c497e60457a2e125a3e4fc839d6453bfe585834.zip rockbox-1c497e60457a2e125a3e4fc839d6453bfe585834.tar.gz rockbox-1c497e60457a2e125a3e4fc839d6453bfe585834.tar.bz2 rockbox-1c497e60457a2e125a3e4fc839d6453bfe585834.tar.xz | |
First audio codec playback attempt by Miikka Pekkarinen
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6574 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins')
| -rw-r--r-- | apps/plugins/SOURCES | 4 | ||||
| -rw-r--r-- | apps/plugins/codecmpa.c | 404 | ||||
| -rw-r--r-- | apps/plugins/codecvorbis.c | 160 | ||||
| -rw-r--r-- | apps/plugins/lib/SOURCES | 3 | ||||
| -rw-r--r-- | apps/plugins/lib/codeclib.c | 36 | ||||
| -rw-r--r-- | apps/plugins/lib/codeclib.h | 46 | ||||
| -rw-r--r-- | apps/plugins/lib/xxx2wav.c | 9 | ||||
| -rw-r--r-- | apps/plugins/lib/xxx2wav.h | 1 |
8 files changed, 661 insertions, 2 deletions
diff --git a/apps/plugins/SOURCES b/apps/plugins/SOURCES index 64f85b1..6ed0e7c 100644 --- a/apps/plugins/SOURCES +++ b/apps/plugins/SOURCES @@ -72,6 +72,10 @@ mpa2wav.c a52towav.c flac2wav.c vorbis2wav.c +#ifdef IRIVER_H100 +codecvorbis.c +codecmpa.c +#endif wv2wav.c mpc2wav.c midi2wav.c diff --git a/apps/plugins/codecmpa.c b/apps/plugins/codecmpa.c new file mode 100644 index 0000000..1125b4b --- /dev/null +++ b/apps/plugins/codecmpa.c @@ -0,0 +1,404 @@ +/*************************************************************************** + * __________ __ ___. + * 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 "plugin.h" + +#include <codecs/libmad/mad.h> + +#include "playback.h" +#include "mp3data.h" +#include "lib/codeclib.h" + +static struct plugin_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; + +mad_fixed_t mad_frame_overlap[2][32][18] IDATA_ATTR; +unsigned char mad_main_data[MAD_BUFFER_MDLEN] IDATA_ATTR; + +#ifdef USE_IRAM +extern char iramcopy[]; +extern char iramstart[]; +extern char iramend[]; +#endif + +#undef DEBUG_GAPLESS + +/* this is the plugin entry point */ +enum plugin_status plugin_start(struct plugin_api* api, void* parm) +{ + struct codec_api *ci = (struct codec_api *)parm; + 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; + + /* Generic plugin inititialisation */ + + TEST_PLUGIN_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, ci)) { + return PLUGIN_ERROR; + } + + /* Create a decoder instance */ + + next_track: + + 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; + +#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 + + samplesdone = 0; + 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); + + samplecount = ci->id3->length * (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); */ + + /* 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)) { + ci->seek_time = 0; + if (sample_loc >= samplecount + samplesdone) + break ; + samplecount += samplesdone - sample_loc; + samplesdone = sample_loc; + } + } + + /* 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 */ + for(i=0;i<Synth.pcm.length;i++) + { + 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(Synth.pcm.samples[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(Synth.pcm.samples[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; + } + + /* 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 PLUGIN_OK; +} diff --git a/apps/plugins/codecvorbis.c b/apps/plugins/codecvorbis.c new file mode 100644 index 0000000..41db223 --- /dev/null +++ b/apps/plugins/codecvorbis.c @@ -0,0 +1,160 @@ +/*************************************************************************** + * __________ __ ___. + * 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 "plugin.h" + +#include <codecs/Tremor/ivorbisfile.h> + +#include "playback.h" +#include "lib/codeclib.h" + +static struct plugin_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) { + struct codec_api *p = (struct codec_api *) datasource; + + return p->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) { + struct codec_api *p = (struct codec_api *) datasource; + return p->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 plugin entry point */ +enum plugin_status plugin_start(struct plugin_api* api, void* parm) +{ + struct codec_api *ci = (struct codec_api *)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_PLUGIN_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 + + /* This function sets up the buffers and reads the file into RAM */ + + if (codec_init(api, ci)) { + return PLUGIN_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; + + next_track: + error=ov_open_callbacks(ci,&vf,NULL,0,callbacks); + + vi=ov_info(&vf,-1); + + if (vi==NULL) { + // rb->splash(HZ*2, true, "Vorbis Error"); + return PLUGIN_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 (ci->stop_codec || ci->reload_codec) + break ; + while (!ci->audiobuffer_insert(pcmbuf, n)) + rb->yield(); + +#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 (ci->request_next_track()) + goto next_track; + + return PLUGIN_OK; +} + diff --git a/apps/plugins/lib/SOURCES b/apps/plugins/lib/SOURCES index 0e8e14c..58356af 100644 --- a/apps/plugins/lib/SOURCES +++ b/apps/plugins/lib/SOURCES @@ -35,4 +35,7 @@ playergfx.c #endif #if CONFIG_HWCODEC == MASNONE /* software codec platforms */ xxx2wav.c +#ifdef IRIVER_H100 +codeclib.c +#endif #endif diff --git a/apps/plugins/lib/codeclib.c b/apps/plugins/lib/codeclib.c new file mode 100644 index 0000000..d9866ef --- /dev/null +++ b/apps/plugins/lib/codeclib.c @@ -0,0 +1,36 @@ +/*************************************************************************** + * __________ __ ___. + * 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 */ + +#include "plugin.h" +#include "playback.h" +#include "codeclib.h" + +struct plugin_api* local_rb; + +int codec_init(struct plugin_api* rb, struct codec_api* ci) { + local_rb = rb; + + xxx2wav_set_api(rb); + mem_ptr = 0; + mallocbuf = (unsigned char *)ci->get_codec_memory((size_t *)&bufsize); + + return 0; +} diff --git a/apps/plugins/lib/codeclib.h b/apps/plugins/lib/codeclib.h new file mode 100644 index 0000000..876e69b --- /dev/null +++ b/apps/plugins/lib/codeclib.h @@ -0,0 +1,46 @@ +/*************************************************************************** + * __________ __ ___. + * 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 + +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 plugin_api* rb, struct codec_api* ci); + diff --git a/apps/plugins/lib/xxx2wav.c b/apps/plugins/lib/xxx2wav.c index d864527..ed67184 100644 --- a/apps/plugins/lib/xxx2wav.c +++ b/apps/plugins/lib/xxx2wav.c @@ -40,14 +40,14 @@ void* codec_malloc(size_t size) { 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)) { 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); } @@ -162,6 +162,11 @@ static unsigned char wav_header[44]={'R','I','F','F', // 0 - ChunkID }; +void xxx2wav_set_api(struct plugin_api* rb) +{ + local_rb = rb; +} + int local_init(char* infilename, char* outfilename, file_info_struct* file_info, struct plugin_api* rb) { char s[32]; int i,n,bytesleft; diff --git a/apps/plugins/lib/xxx2wav.h b/apps/plugins/lib/xxx2wav.h index 7e3afce..7e1b942 100644 --- a/apps/plugins/lib/xxx2wav.h +++ b/apps/plugins/lib/xxx2wav.h @@ -64,3 +64,4 @@ 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 plugin_api* rb); void close_wav(file_info_struct* file_info); +void xxx2wav_set_api(struct plugin_api* rb); |