From 273d2e81f72c7721447ab9c539877f6712faaecc Mon Sep 17 00:00:00 2001 From: Dave Chapman Date: Wed, 26 Oct 2005 12:35:58 +0000 Subject: New FLAC decoder from the ffmpeg project git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7656 a1c6a512-1295-4272-9138-f99709370657 --- apps/FILES | 6 +- apps/codecs/Makefile | 18 +- apps/codecs/flac.c | 405 +++++++-------------- apps/codecs/libffmpegFLAC/COPYING | 504 ++++++++++++++++++++++++++ apps/codecs/libffmpegFLAC/Makefile | 47 +++ apps/codecs/libffmpegFLAC/Makefile.test | 21 ++ apps/codecs/libffmpegFLAC/README | 19 + apps/codecs/libffmpegFLAC/README.rockbox | 46 +++ apps/codecs/libffmpegFLAC/SOURCES | 2 + apps/codecs/libffmpegFLAC/bitstream.c | 62 ++++ apps/codecs/libffmpegFLAC/bitstream.h | 333 ++++++++++++++++++ apps/codecs/libffmpegFLAC/decoder.c | 584 +++++++++++++++++++++++++++++++ apps/codecs/libffmpegFLAC/decoder.h | 46 +++ apps/codecs/libffmpegFLAC/golomb.h | 106 ++++++ apps/codecs/libffmpegFLAC/main.c | 292 ++++++++++++++++ apps/plugins/Makefile | 2 +- tools/configure | 8 +- 17 files changed, 2201 insertions(+), 300 deletions(-) create mode 100644 apps/codecs/libffmpegFLAC/COPYING create mode 100644 apps/codecs/libffmpegFLAC/Makefile create mode 100644 apps/codecs/libffmpegFLAC/Makefile.test create mode 100644 apps/codecs/libffmpegFLAC/README create mode 100644 apps/codecs/libffmpegFLAC/README.rockbox create mode 100644 apps/codecs/libffmpegFLAC/SOURCES create mode 100644 apps/codecs/libffmpegFLAC/bitstream.c create mode 100644 apps/codecs/libffmpegFLAC/bitstream.h create mode 100644 apps/codecs/libffmpegFLAC/decoder.c create mode 100644 apps/codecs/libffmpegFLAC/decoder.h create mode 100644 apps/codecs/libffmpegFLAC/golomb.h create mode 100644 apps/codecs/libffmpegFLAC/main.c diff --git a/apps/FILES b/apps/FILES index 07719a4501..816b8c60d9 100644 --- a/apps/FILES +++ b/apps/FILES @@ -34,11 +34,7 @@ codecs/SOURCES codecs/*.[ch] codecs/Makefile codecs/liba52/* -codecs/libFLAC/* -codecs/libFLAC/include/private/*.h -codecs/libFLAC/include/protected/*.h -codecs/libFLAC/include/FLAC/*.h -codecs/libFLAC/include/*.h +codecs/libffmpegFLAC/* codecs/libmad/* codecs/libwavpack/* codecs/Tremor/* diff --git a/apps/codecs/Makefile b/apps/codecs/Makefile index 8f869b350c..b29ed738b0 100644 --- a/apps/codecs/Makefile +++ b/apps/codecs/Makefile @@ -17,7 +17,7 @@ ifdef APPEXTRA endif ifdef SOFTWARECODECS - CODECLIBS = -lmad -la52 -lFLAC -lTremor -lwavpack -lmusepack -lalac + CODECLIBS = -lmad -la52 -lffmpegFLAC -lTremor -lwavpack -lmusepack -lalac endif # we "borrow" the plugin LDS file @@ -39,7 +39,7 @@ DIRS = . CODECDEPS = $(LINKCODEC) $(BUILDDIR)/libcodec.a -.PHONY: libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac +.PHONY: libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac OUTPUT = $(SOFTWARECODECS) @@ -55,7 +55,7 @@ ELFIT = $(SILENT)(file=`basename $@`; \ $(OBJDIR)/mpa.elf: $(OBJDIR)/mpa.o $(CODECDEPS) $(BUILDDIR)/libmad.a $(OBJDIR)/a52.elf: $(OBJDIR)/a52.o $(CODECDEPS) $(BUILDDIR)/liba52.a -$(OBJDIR)/flac.elf: $(OBJDIR)/flac.o $(CODECDEPS) $(BUILDDIR)/libFLAC.a +$(OBJDIR)/flac.elf: $(OBJDIR)/flac.o $(CODECDEPS) $(BUILDDIR)/libffmpegFLAC.a $(OBJDIR)/vorbis.elf: $(OBJDIR)/vorbis.o $(CODECDEPS) $(BUILDDIR)/libTremor.a $(OBJDIR)/mpc.elf: $(OBJDIR)/mpc.o $(CODECDEPS) $(BUILDDIR)/libmusepack.a $(OBJDIR)/wav.elf: $(OBJDIR)/wav.o $(CODECDEPS) @@ -128,10 +128,10 @@ liba52: @mkdir -p $(OBJDIR)/liba52 @$(MAKE) -C liba52 OBJDIR=$(OBJDIR)/liba52 OUTPUT=$(BUILDDIR)/liba52.a -libFLAC: - @echo "MAKE in libFLAC" - @mkdir -p $(OBJDIR)/libFLAC - @$(MAKE) -C libFLAC OBJDIR=$(OBJDIR)/libFLAC OUTPUT=$(BUILDDIR)/libFLAC.a +libffmpegFLAC: + @echo "MAKE in libffmpegFLAC" + @mkdir -p $(OBJDIR)/libffmpegFLAC + @$(MAKE) -C libffmpegFLAC OBJDIR=$(OBJDIR)/libffmpegFLAC OUTPUT=$(BUILDDIR)/libffmpegFLAC.a libTremor: @echo "MAKE in Tremor" @@ -160,10 +160,10 @@ libalac: clean: @echo "cleaning codecs" - $(SILENT)rm -fr $(OBJDIR)/libmad $(BUILDDIR)/libmad.a $(OBJDIR)/liba52 $(OBJDIR)/libFLAC $(OBJDIR)/Tremor $(OBJDIR)/libwavpack $(OBJDIR)/dumb $(BUILDDIR)/libdumb.a $(BUILDDIR)/libdumbd.a $(OBJDIR)/libmusepack $(BUILDDIR)/libmusepack.a $(OBJDIR)/libalac $(BUILDDIR)/libalac.a + $(SILENT)rm -fr $(OBJDIR)/libmad $(BUILDDIR)/libmad.a $(OBJDIR)/liba52 $(OBJDIR)/libffmpegFLAC $(BUILDDIR)/libffmpegFLAC.a $(OBJDIR)/Tremor $(OBJDIR)/libwavpack $(OBJDIR)/dumb $(BUILDDIR)/libdumb.a $(BUILDDIR)/libdumbd.a $(OBJDIR)/libmusepack $(BUILDDIR)/libmusepack.a $(OBJDIR)/libalac $(BUILDDIR)/libalac.a @$(MAKE) -C libmad clean OBJDIR=$(OBJDIR)/libmad @$(MAKE) -C liba52 clean OBJDIR=$(OBJDIR)/liba52 - @$(MAKE) -C libFLAC clean OBJDIR=$(OBJDIR)/libFLAC + @$(MAKE) -C libffmpegFLAC clean OBJDIR=$(OBJDIR)/libffmpegFLAC @$(MAKE) -C Tremor clean OBJDIR=$(OBJDIR)/Tremor @$(MAKE) -C libwavpack clean OBJDIR=$(OBJDIR)/libwavpack @$(MAKE) -C libmusepack clean OBJDIR=$(OBJDIR)/libmusepack diff --git a/apps/codecs/flac.c b/apps/codecs/flac.c index 45fc21466c..b9a3a8a249 100644 --- a/apps/codecs/flac.c +++ b/apps/codecs/flac.c @@ -7,7 +7,7 @@ * \/ \/ \/ \/ \/ * $Id$ * - * Copyright (C) 2002 Björn Stenberg + * 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. @@ -18,156 +18,7 @@ ****************************************************************************/ #include "codeclib.h" -#include -#include -#include - -#define FLAC_MAX_SUPPORTED_BLOCKSIZE 4608 -#define FLAC_MAX_SUPPORTED_CHANNELS 2 - -static uint32_t samplesdone; - -static FLAC__StreamMetadata *stream_info; -static FLAC__StreamMetadata *seek_table; -unsigned int metadata_length; - -/* 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) { - ci->yield(); - yieldcounter = 0; - } - } - } - - samplesdone+=samples; - ci->set_elapsed(samplesdone/(ci->id3->frequency/1000)); - - ci->yield(); - while (!ci->pcmbuf_insert(pcmbuf, data_size)) - ci->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)data; - - metadata_length += meta->length; - - if ( meta->type == FLAC__METADATA_TYPE_STREAMINFO ) { - stream_info = FLAC__metadata_object_clone( meta ); - if ( stream_info == NULL ) { - //return CODEC_ERROR; - } - } else if ( meta->type == FLAC__METADATA_TYPE_SEEKTABLE ) { - seek_table = FLAC__metadata_object_clone( meta ); - if ( seek_table == NULL ) { - //return CODEC_ERROR; - } - } -} - - -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; - } -} +#include #ifndef SIMULATOR extern char iramcopy[]; @@ -175,78 +26,101 @@ extern char iramstart[]; extern char iramend[]; #endif -FLAC__uint64 find_sample_number(struct codec_api *ci, size_t offset) +struct codec_api* rb; +struct codec_api* ci; + +/* The output buffers containing the decoded samples (channels 0 and 1) */ +int32_t decoded0[MAX_BLOCKSIZE] IBSS_ATTR; +int32_t decoded1[MAX_BLOCKSIZE] IBSS_ATTR; + +static bool flac_init(FLACContext* fc) { - FLAC__StreamMetadata_SeekPoint *points; - FLAC__uint64 prev_sample, next_sample; - size_t prev_offset, next_offset; - int percent; + unsigned char buf[255]; + bool found_streaminfo=false; + int endofmetadata=0; + int blocklength; - if (offset >= (ci->id3->filesize - metadata_length)) { - return stream_info->data.stream_info.total_samples; + if (ci->read_filebuf(buf, 4) < 4) + { + return false; } - prev_offset = 0; - prev_sample = 0; - next_offset = ci->id3->filesize - metadata_length; - next_sample = stream_info->data.stream_info.total_samples; - - if (seek_table) { - int left, right, middle; + if (ci->memcmp(buf,"fLaC",4) != 0) + { + return false; + } + fc->metadatalength = 4; - middle = 0; /* Silence compiler warnings */ - points = seek_table->data.seek_table.points; - left = 0; - right = seek_table->data.seek_table.num_points - 1; + while (!endofmetadata) { + if (ci->read_filebuf(buf, 4) < 4) + { + return false; + } - /* Do a binary search to find the matching seek point */ - while (left <= right) { - middle = (left + right) / 2; + endofmetadata=(buf[0]&0x80); + blocklength = (buf[1] << 16) | (buf[2] << 8) | buf[3]; + fc->metadatalength+=blocklength+4; - if ((FLAC__uint64)offset < points[middle].stream_offset) { - right = middle - 1; - } else if ((FLAC__uint64)offset > points[middle].stream_offset) { - left = middle + 1; - } else { - return points[middle].sample_number; + if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */ + { + /* FIXME: Don't trust the value of blocklength */ + if (ci->read_filebuf(buf, blocklength) < 0) + { + return false; } - } - - /* Didn't find a matching seek point, so get the sample numbers of the - * seek points to the left and right of offset to make our guess more - * accurate. Accuracy depends on how close these sample numbers are to - * each other. - */ - if ((unsigned)left >= seek_table->data.seek_table.num_points) { - prev_offset = points[middle].stream_offset; - prev_sample = points[middle].sample_number; - } else if (right < 0) { - next_offset = points[middle].stream_offset; - next_sample = points[middle].sample_number; + + fc->filesize = ci->filesize; + fc->min_blocksize = (buf[0] << 8) | buf[1]; + fc->max_blocksize = (buf[2] << 8) | buf[3]; + fc->min_framesize = (buf[4] << 16) | (buf[5] << 8) | buf[6]; + fc->max_framesize = (buf[7] << 16) | (buf[8] << 8) | buf[9]; + fc->samplerate = (buf[10] << 12) | (buf[11] << 4) + | ((buf[12] & 0xf0) >> 4); + fc->channels = ((buf[12]&0x0e)>>1) + 1; + fc->bps = (((buf[12]&0x01) << 4) | ((buf[13]&0xf0)>>4) ) + 1; + + /* totalsamples is a 36-bit field, but we assume <= 32 bits are + used */ + fc->totalsamples = (buf[14] << 24) | (buf[15] << 16) + | (buf[16] << 8) | buf[17]; + + /* Calculate track length (in ms) and estimate the bitrate + (in kbit/s) */ + fc->length = (fc->totalsamples / fc->samplerate) * 1000; + + found_streaminfo=true; + } else if ((buf[0] & 0x7f) == 3) { /* 3 is the SEEKTABLE block */ + ci->advance_buffer(blocklength); } else { - middle--; - prev_offset = points[middle].stream_offset; - prev_sample = points[middle].sample_number; - next_offset = points[middle+1].stream_offset; - next_sample = points[middle+1].sample_number; + /* Skip to next metadata block */ + ci->advance_buffer(blocklength); } } - /* Either there's no seek table or we didn't find our seek point, so now we - * have to guess. - */ - percent = ((offset - prev_offset) * 100) / (next_offset - prev_offset); - return (FLAC__uint64)(percent * (next_sample - prev_sample) / 100 + prev_sample); + if (found_streaminfo) { + fc->bitrate = ((fc->filesize-fc->metadatalength) * 8) / fc->length; + return true; + } else { + return false; + } } /* this is the codec entry point */ enum codec_status codec_start(struct codec_api* api) { - struct codec_api* ci = api; - FLAC__SeekableStreamDecoder* flacDecoder; - FLAC__uint64 offset; + size_t n; + static int8_t buf[MAX_FRAMESIZE]; + FLACContext fc; + uint32_t samplesdone; + uint32_t elapsedtime; + int bytesleft; + int consumed; - TEST_CODEC_API(ci); + /* Generic codec initialisation */ + TEST_CODEC_API(api); + + rb = api; + ci = (struct codec_api*)api; #ifndef SIMULATOR ci->memcpy(iramstart, iramcopy, iramend-iramstart); @@ -254,19 +128,17 @@ enum codec_status codec_start(struct codec_api* api) 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*1024)); + ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*128)); ci->configure(CODEC_DSP_ENABLE, (bool *)true); ci->configure(DSP_DITHER, (bool *)false); - ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED); - ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(16)); - -next_track: - metadata_length = 0; - seek_table = NULL; - stream_info = NULL; + ci->configure(DSP_SET_STEREO_MODE, (long *)STEREO_NONINTERLEAVED); + ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(28)); + + next_track: - if (codec_init(api)) { + if (!flac_init(&fc)) { + LOGF("FLAC: Error initialising codec\n"); return CODEC_ERROR; } @@ -274,87 +146,58 @@ next_track: ci->yield(); ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency)); - codec_set_replaygain(ci->id3); - - /* 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_all(flacDecoder); - - /* 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); - - if (ci->id3->offset && stream_info) { - FLAC__uint64 sample; - - sample = find_sample_number(ci, ci->id3->offset - metadata_length); - ci->advance_buffer(ci->id3->offset); - FLAC__seekable_stream_decoder_seek_absolute(flacDecoder, sample); - FLAC__seekable_stream_decoder_get_decode_position(flacDecoder, &offset); - ci->set_offset(offset); - samplesdone = (uint32_t)sample; - ci->set_elapsed(sample/(ci->id3->frequency/1000)); - } else { - 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) { + /* The main decoding loop */ + bytesleft=ci->read_filebuf(buf,sizeof(buf)); + while (bytesleft) { ci->yield(); if (ci->stop_codec || ci->reload_codec) { break; } + /* Deal with any pending seek requests */ if (ci->seek_time) { - int sample_loc; + /* We only support seeking to start of track at the moment */ + if (ci->seek_time==1) { + if (ci->seek_buffer(fc.metadatalength)) { + /* Refill the input buffer */ + bytesleft=ci->read_filebuf(buf,sizeof(buf)); + ci->set_elapsed(0); + } + } + ci->seek_time = 0; + } - 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; + if(flac_decode_frame(&fc,decoded0,decoded1,buf, + bytesleft,ci->yield) < 0) { + LOGF("FLAC: Decode error, aborting\n"); + return CODEC_ERROR; } + consumed=fc.gb.index/8; - FLAC__seekable_stream_decoder_process_single(flacDecoder); - FLAC__seekable_stream_decoder_get_decode_position(flacDecoder, &offset); - ci->set_offset(offset); - } + ci->yield(); + while(!ci->pcmbuf_insert_split((char*)decoded0,(char*)decoded1, + fc.blocksize*4)) { + ci->yield(); + } - /* Flush the libFLAC buffers */ - FLAC__seekable_stream_decoder_finish(flacDecoder); + /* Update the elapsed-time indicator */ + samplesdone=fc.samplenumber+fc.blocksize; + elapsedtime=(samplesdone*10)/(ci->id3->frequency/100); + ci->set_elapsed(elapsedtime); - if (ci->request_next_track()) { - if (stream_info) { - FLAC__metadata_object_delete(stream_info); - } - if (seek_table) { - FLAC__metadata_object_delete(seek_table); + memmove(buf,&buf[consumed],bytesleft-consumed); + bytesleft-=consumed; + + n=ci->read_filebuf(&buf[bytesleft],sizeof(buf)-bytesleft); + if (n > 0) { + bytesleft+=n; } - metadata_length = 0; - goto next_track; } + LOGF("FLAC: Decoded %d samples\n",samplesdone); + + if (ci->request_next_track()) + goto next_track; return CODEC_OK; } diff --git a/apps/codecs/libffmpegFLAC/COPYING b/apps/codecs/libffmpegFLAC/COPYING new file mode 100644 index 0000000000..223ede7de3 --- /dev/null +++ b/apps/codecs/libffmpegFLAC/COPYING @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/apps/codecs/libffmpegFLAC/Makefile b/apps/codecs/libffmpegFLAC/Makefile new file mode 100644 index 0000000000..240ce28a00 --- /dev/null +++ b/apps/codecs/libffmpegFLAC/Makefile @@ -0,0 +1,47 @@ +# __________ __ ___. +# Open \______ \ ____ ____ | | _\_ |__ _______ ___ +# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +# \/ \/ \/ \/ \/ +# $Id$ +# + +INCLUDES=-I$(APPSDIR) -I.. -I. -I$(FIRMDIR)/include -I$(FIRMDIR)/export \ + -I$(FIRMDIR)/common -I$(FIRMDIR)/drivers -I$(BUILDDIR) + +ifdef APPEXTRA +INCLUDES += -I$(APPSDIR)/$(APPEXTRA) +endif + +FLACOPTS = -O2 +CFLAGS = $(GCCOPTS) $(FLACOPTS) $(INCLUDES) $(TARGET) $(EXTRA_DEFINES) -DMEM=${MEMORYSIZE} + +# This sets up 'SRC' based on the files mentioned in SOURCES +include $(TOOLSDIR)/makesrc.inc + +SOURCES = $(SRC) +OBJS2 := $(SRC:%.c=$(OBJDIR)/%.o) +OBJS = $(patsubst %.S, $(OBJDIR)/%.o, $(OBJS2)) +DEPFILE = $(OBJDIR)/dep-libalac +DIRS = + +OUTPUT = $(BUILDDIR)/libalac.a + +all: $(OUTPUT) + +$(OUTPUT): $(OBJS) + @echo "AR $@" + @$(AR) ruv $@ $+ >/dev/null 2>&1 + +$(OBJDIR)/libalac/%.o: $(APPSDIR)/codecs/libalac/%.c + @echo "(libalac) CC $<" + @$(CC) -c $(CFLAGS) -I$(APPSDIR)/codecs/libalac/ $< -o $@ + +include $(TOOLSDIR)/make.inc + +clean: + @echo "cleaning libffmpegFLAC" + @rm -f $(OBJS) $(OUTPUT) $(DEPFILE) + +-include $(DEPFILE) diff --git a/apps/codecs/libffmpegFLAC/Makefile.test b/apps/codecs/libffmpegFLAC/Makefile.test new file mode 100644 index 0000000000..a6850e8246 --- /dev/null +++ b/apps/codecs/libffmpegFLAC/Makefile.test @@ -0,0 +1,21 @@ +# Simple Makefile for standalone testing of decoder + +OBJS=main.o decoder.o bitstream.o +CFLAGS=-O2 -Wall -DBUILD_STANDALONE +CC=gcc + +# Uncomment the next line to build on a big-endian target: + +#CFLAGS += -DBUILD_BIGENDIAN + +all: test + +main.o: main.c decoder.h +decoder.o: decoder.c bitstream.h golomb.h decoder.h +bitstream.o: bitstream.c + +test: $(OBJS) + gcc -o test $(OBJS) + +clean: + rm -f test $(OBJS) *~ diff --git a/apps/codecs/libffmpegFLAC/README b/apps/codecs/libffmpegFLAC/README new file mode 100644 index 0000000000..9bc0f6ed8a --- /dev/null +++ b/apps/codecs/libffmpegFLAC/README @@ -0,0 +1,19 @@ +FFmpeg README +------------- + +1) Documentation +---------------- + +* Read the documentation in the doc/ directory. + +2) Licensing +------------ + +* Read the file COPYING. ffmpeg and the associated libraries EXCEPT + liba52 and libpostproc are licensed under the Lesser GNU General + Public License. + +* liba52 and libpostproc are distributed under the GNU General Public + License and their compilation and use is optional in ffmpeg. + +Fabrice Bellard. \ No newline at end of file diff --git a/apps/codecs/libffmpegFLAC/README.rockbox b/apps/codecs/libffmpegFLAC/README.rockbox new file mode 100644 index 0000000000..850cdfcb7d --- /dev/null +++ b/apps/codecs/libffmpegFLAC/README.rockbox @@ -0,0 +1,46 @@ +Library: libffmpegFLAC +Imported: 2005-10-26 by Dave Chapman + +This directory contains a FLAC decoder based on the flac.c (and a small +number of other files) from the ffmpeg project. + +LICENSING INFORMATION + +ffmpeg is licensed under the Lesser GNU General Public License and the +file flac.c is Copyright 2003 Alex Beregszaszi. + +IMPORT DETAILS + +The decoder is based on ffmpeg-cvs from 26 October 2005. + +The file libavcodec/flac.c was renamed to decoder.c and all ffmpeg +related code was removed. It was also heavily modified to remove +debugging information and unused experiemental code. + +It was also changed to provide output as 32-bit integers (with the +data left-shifted to 28 bit accuracy). The original flac.c provided +16-bit output. + +In order to minimise memory usage, a small number of hard-coded limits +are present in decoder.h - mainly limiting the supported blocksize +(number of samples in a frame) to 4608. This is the default value +used by the reference FLAC encoder at all standard compression +settings. + + +TESTING + +A test program is contained in the file main.c and this should compile +in any Unix-like environment using the command "make -f Makefile.test". + +Running "test file.flac" will decode the audio data to a WAV file +called "test.wav" in the current directory. This has been tested with +16-bit and 24-bit mono and stereo FLAC files. + +This can be used to test the accuracy of the decoder by comparing the +md5sum of the WAV file produced by this decoder with a WAV file +produced by the official libFLAC decoder. + +This test program could be extended to perform an internal md5sum +calculation and comparing that against the md5sum stored in the FLAC +file's header. diff --git a/apps/codecs/libffmpegFLAC/SOURCES b/apps/codecs/libffmpegFLAC/SOURCES new file mode 100644 index 0000000000..6b23e7ca3e --- /dev/null +++ b/apps/codecs/libffmpegFLAC/SOURCES @@ -0,0 +1,2 @@ +bitstream.c +decoder.c diff --git a/apps/codecs/libffmpegFLAC/bitstream.c b/apps/codecs/libffmpegFLAC/bitstream.c new file mode 100644 index 0000000000..e53ec0fd46 --- /dev/null +++ b/apps/codecs/libffmpegFLAC/bitstream.c @@ -0,0 +1,62 @@ +/* + * Common bit i/o utils + * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2002-2004 Michael Niedermayer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * alternative bitstream reader & writer by Michael Niedermayer + */ + +/** + * @file bitstream.c + * bitstream api. + */ + +#include +#include "bitstream.h" + +/* bit input functions */ + +/** + * reads 0-32 bits. + */ +unsigned int get_bits_long(GetBitContext *s, int n){ + if(n<=17) return get_bits(s, n); + else{ + int ret= get_bits(s, 16) << (n-16); + return ret | get_bits(s, n-16); + } +} + +/** + * shows 0-32 bits. + */ +unsigned int show_bits_long(GetBitContext *s, int n){ + if(n<=17) return show_bits(s, n); + else{ + GetBitContext gb= *s; + int ret= get_bits_long(s, n); + *s= gb; + return ret; + } +} + +void align_get_bits(GetBitContext *s) +{ + int n= (-get_bits_count(s)) & 7; + if(n) skip_bits(s, n); +} + diff --git a/apps/codecs/libffmpegFLAC/bitstream.h b/apps/codecs/libffmpegFLAC/bitstream.h new file mode 100644 index 0000000000..62e98c07ea --- /dev/null +++ b/apps/codecs/libffmpegFLAC/bitstream.h @@ -0,0 +1,333 @@ +/** + * @file bitstream.h + * bitstream api header. + */ + +#ifndef BITSTREAM_H +#define BITSTREAM_H + +#include + +#ifndef BUILD_STANDALONE + #include + #include +#else + #include + #define IBSS_ATTR + #define ICONST_ATTR + +#endif + +/* Endian conversion routines for standalone compilation */ +#ifdef BUILD_STANDALONE + #ifdef BUILD_BIGENDIAN + #define betoh32(x) (x) + #define letoh32(x) swap32(x) + #else + #define letoh32(x) (x) + #define betoh32(x) swap32(x) + #endif + + /* Taken from rockbox/firmware/export/system.h */ + + static inline unsigned short swap16(unsigned short value) + /* + result[15..8] = value[ 7..0]; + result[ 7..0] = value[15..8]; + */ + { + return (value >> 8) | (value << 8); + } + + static inline unsigned long swap32(unsigned long value) + /* + result[31..24] = value[ 7.. 0]; + result[23..16] = value[15.. 8]; + result[15.. 8] = value[23..16]; + result[ 7.. 0] = value[31..24]; + */ + { + unsigned long hi = swap16(value >> 16); + unsigned long lo = swap16(value & 0xffff); + return (lo << 16) | hi; + } +#endif + +/* FLAC files are big-endian */ +#define ALT_BITSTREAM_READER_BE + +#define NEG_SSR32(a,s) (((int32_t)(a))>>(32-(s))) +#define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) + +/* bit input */ +/* buffer, buffer_end and size_in_bits must be present and used by every reader */ +typedef struct GetBitContext { + const uint8_t *buffer, *buffer_end; + int index; + int size_in_bits; +} GetBitContext; + +#define VLC_TYPE int16_t + +typedef struct VLC { + int bits; + VLC_TYPE (*table)[2]; ///< code, bits + int table_size, table_allocated; +} VLC; + +typedef struct RL_VLC_ELEM { + int16_t level; + int8_t len; + uint8_t run; +} RL_VLC_ELEM; + +#if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) +#define UNALIGNED_STORES_ARE_BAD +#endif + +/* used to avoid missaligned exceptions on some archs (alpha, ...) */ +#if defined(ARCH_X86) || defined(ARCH_X86_64) +# define unaligned32(a) (*(const uint32_t*)(a)) +#else +# ifdef __GNUC__ +static inline uint32_t unaligned32(const void *v) { + struct Unaligned { + uint32_t i; + } __attribute__((packed)); + + return ((const struct Unaligned *) v)->i; +} +# elif defined(__DECC) +static inline uint32_t unaligned32(const void *v) { + return *(const __unaligned uint32_t *) v; +} +# else +static inline uint32_t unaligned32(const void *v) { + return *(const uint32_t *) v; +} +# endif +#endif //!ARCH_X86 + + +/* Bitstream reader API docs: +name + abritary name which is used as prefix for the internal variables + +gb + getbitcontext + +OPEN_READER(name, gb) + loads gb into local variables + +CLOSE_READER(name, gb) + stores local vars in gb + +UPDATE_CACHE(name, gb) + refills the internal cache from the bitstream + after this call at least MIN_CACHE_BITS will be available, + +GET_CACHE(name, gb) + will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit) + +SHOW_UBITS(name, gb, num) + will return the next num bits + +SHOW_SBITS(name, gb, num) + will return the next num bits and do sign extension + +SKIP_BITS(name, gb, num) + will skip over the next num bits + note, this is equivalent to SKIP_CACHE; SKIP_COUNTER + +SKIP_CACHE(name, gb, num) + will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER) + +SKIP_COUNTER(name, gb, num) + will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS) + +LAST_SKIP_CACHE(name, gb, num) + will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing + +LAST_SKIP_BITS(name, gb, num) + is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER + +for examples see get_bits, show_bits, skip_bits, get_vlc +*/ + +static inline int unaligned32_be(const void *v) +{ +#ifdef CONFIG_ALIGN + const uint8_t *p=v; + return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]); +#else + return betoh32( unaligned32(v)); //original +#endif +} + +static inline int unaligned32_le(const void *v) +{ +#ifdef CONFIG_ALIGN + const uint8_t *p=v; + return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]); +#else + return letoh32( unaligned32(v)); //original +#endif +} + +# define MIN_CACHE_BITS 25 + +# define OPEN_READER(name, gb)\ + int name##_index= (gb)->index;\ + int name##_cache= 0;\ + +# define CLOSE_READER(name, gb)\ + (gb)->index= name##_index;\ + +# ifdef ALT_BITSTREAM_READER_LE +# define UPDATE_CACHE(name, gb)\ + name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\ + +# define SKIP_CACHE(name, gb, num)\ + name##_cache >>= (num); +# else +# define UPDATE_CACHE(name, gb)\ + name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\ + +# define SKIP_CACHE(name, gb, num)\ + name##_cache <<= (num); +# endif + +// FIXME name? +# define SKIP_COUNTER(name, gb, num)\ + name##_index += (num);\ + +# define SKIP_BITS(name, gb, num)\ + {\ + SKIP_CACHE(name, gb, num)\ + SKIP_COUNTER(name, gb, num)\ + }\ + +# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) +# define LAST_SKIP_CACHE(name, gb, num) ; + +# ifdef ALT_BITSTREAM_READER_LE +# define SHOW_UBITS(name, gb, num)\ + ((name##_cache) & (NEG_USR32(0xffffffff,num))) +# else +# define SHOW_UBITS(name, gb, num)\ + NEG_USR32(name##_cache, num) +# endif + +# define SHOW_SBITS(name, gb, num)\ + NEG_SSR32(name##_cache, num) + +# define GET_CACHE(name, gb)\ + ((uint32_t)name##_cache) + +static inline int get_bits_count(GetBitContext *s){ + return s->index; +} + +static inline int get_sbits(GetBitContext *s, int n){ + register int tmp; + OPEN_READER(re, s) + UPDATE_CACHE(re, s) + tmp= SHOW_SBITS(re, s, n); + LAST_SKIP_BITS(re, s, n) + CLOSE_READER(re, s) + return tmp; +} + +/** + * reads 0-17 bits. + * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't + */ +static inline unsigned int get_bits(GetBitContext *s, int n){ + register int tmp; + OPEN_READER(re, s) + UPDATE_CACHE(re, s) + tmp= SHOW_UBITS(re, s, n); + LAST_SKIP_BITS(re, s, n) + CLOSE_READER(re, s) + return tmp; +} + +unsigned int get_bits_long(GetBitContext *s, int n); + +/** + * shows 0-17 bits. + * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't + */ +static inline unsigned int show_bits(GetBitContext *s, int n){ + register int tmp; + OPEN_READER(re, s) + UPDATE_CACHE(re, s) + tmp= SHOW_UBITS(re, s, n); +// CLOSE_READER(re, s) + return tmp; +} + +unsigned int show_bits_long(GetBitContext *s, int n); + +static inline void skip_bits(GetBitContext *s, int n){ + //Note gcc seems to optimize this to s->index+=n for the ALT_READER :)) + OPEN_READER(re, s) + UPDATE_CACHE(re, s) + LAST_SKIP_BITS(re, s, n) + CLOSE_READER(re, s) +} + +static inline unsigned int get_bits1(GetBitContext *s){ + int index= s->index; + uint8_t result= s->buffer[ index>>3 ]; +#ifdef ALT_BITSTREAM_READER_LE + result>>= (index&0x07); + result&= 1; +#else + result<<= (index&0x07); + result>>= 8 - 1; +#endif + index++; + s->index= index; + + return result; +} + +static inline unsigned int show_bits1(GetBitContext *s){ + return show_bits(s, 1); +} + +static inline void skip_bits1(GetBitContext *s){ + skip_bits(s, 1); +} + +/** + * init GetBitContext. + * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits + * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end + * @param bit_size the size of the buffer in bits + */ +static inline void init_get_bits(GetBitContext *s, + const uint8_t *buffer, int bit_size) +{ + int buffer_size= (bit_size+7)>>3; + if(buffer_size < 0 || bit_size < 0) { + buffer_size = bit_size = 0; + buffer = 0; + } + + s->buffer= buffer; + s->size_in_bits= bit_size; + s->buffer_end= buffer + buffer_size; + s->index=0; + { + OPEN_READER(re, s) + UPDATE_CACHE(re, s) + UPDATE_CACHE(re, s) + CLOSE_READER(re, s) + } +} + +void align_get_bits(GetBitContext *s); + +#endif /* BITSTREAM_H */ diff --git a/apps/codecs/libffmpegFLAC/decoder.c b/apps/codecs/libffmpegFLAC/decoder.c new file mode 100644 index 0000000000..0033feb4f9 --- /dev/null +++ b/apps/codecs/libffmpegFLAC/decoder.c @@ -0,0 +1,584 @@ +/* + * FLAC (Free Lossless Audio Codec) decoder + * Copyright (c) 2003 Alex Beregszaszi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * @file flac.c + * FLAC (Free Lossless Audio Codec) decoder + * @author Alex Beregszaszi + * + * For more information on the FLAC format, visit: + * http://flac.sourceforge.net/ + * + * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed + * through, starting from the initial 'fLaC' signature; or by passing the + * 34-byte streaminfo structure through avctx->extradata[_size] followed + * by data starting with the 0xFFF8 marker. + */ + +#include +#include +#ifndef BUILD_STANDALONE +#include "../codec.h" +#endif + +#include "bitstream.h" +#include "golomb.h" + +#include "decoder.h" + +#define INT_MAX 2147483647 + +#define FFMAX(a,b) ((a) > (b) ? (a) : (b)) +#define FFMIN(a,b) ((a) > (b) ? (b) : (a)) + +/* Needed by golomb.h */ +const uint8_t ff_log2_tab[256] ICONST_ATTR = { + 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, + 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 +}; + +static const int sample_rate_table[] ICONST_ATTR = +{ 0, 0, 0, 0, + 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000, + 0, 0, 0, 0 }; + +static const int sample_size_table[] ICONST_ATTR = +{ 0, 8, 12, 0, 16, 20, 24, 0 }; + +static const int blocksize_table[] ICONST_ATTR = { + 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0, +256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7 +}; + +static const uint8_t table_crc8[256] ICONST_ATTR = { + 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, + 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d, + 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, + 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, + 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5, + 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd, + 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, + 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd, + 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2, + 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, + 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2, + 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a, + 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, + 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a, + 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42, + 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a, + 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c, + 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4, + 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, + 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4, + 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c, + 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, + 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c, + 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34, + 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, + 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63, + 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, + 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, + 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, + 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83, + 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, + 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3 +}; + +static int64_t get_utf8(GetBitContext *gb) +{ + uint64_t val; + int ones=0, bytes; + + while(get_bits1(gb)) + ones++; + + if (ones==0) bytes=0; + else if(ones==1) return -1; + else bytes= ones - 1; + + val= get_bits(gb, 7-ones); + while(bytes--){ + const int tmp = get_bits(gb, 8); + + if((tmp>>6) != 2) + return -1; + val<<=6; + val|= tmp&0x3F; + } + return val; +} + +static int get_crc8(const uint8_t *buf, int count){ + int crc=0; + int i; + + for(i=0; igb, 2); + if (method_type != 0){ + //fprintf(stderr,"illegal residual coding method %d\n", method_type); + return -1; + } + + rice_order = get_bits(&s->gb, 4); + + samples= s->blocksize >> rice_order; + + sample= + i= pred_order; + for (partition = 0; partition < (1 << rice_order); partition++) + { + tmp = get_bits(&s->gb, 4); + if (tmp == 15) + { + //fprintf(stderr,"fixed len partition\n"); + tmp = get_bits(&s->gb, 5); + for (; i < samples; i++, sample++) + decoded[sample] = get_sbits(&s->gb, tmp); + } + else + { + for (; i < samples; i++, sample++){ + decoded[sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0); + } + } + i= 0; + } + + return 0; +} + +static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order) +{ + int i; + + /* warm up samples */ + for (i = 0; i < pred_order; i++) + { + decoded[i] = get_sbits(&s->gb, s->curr_bps); + } + + if (decode_residuals(s, decoded, pred_order) < 0) + return -1; + + switch(pred_order) + { + case 0: + break; + case 1: + for (i = pred_order; i < s->blocksize; i++) + decoded[i] += decoded[i-1]; + break; + case 2: + for (i = pred_order; i < s->blocksize; i++) + decoded[i] += 2*decoded[i-1] + - decoded[i-2]; + break; + case 3: + for (i = pred_order; i < s->blocksize; i++) + decoded[i] += 3*decoded[i-1] + - 3*decoded[i-2] + + decoded[i-3]; + break; + case 4: + for (i = pred_order; i < s->blocksize; i++) + decoded[i] += 4*decoded[i-1] + - 6*decoded[i-2] + + 4*decoded[i-3] + - decoded[i-4]; + break; + default: + return -1; + } + + return 0; +} + +static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order) +{ + int sum, i, j; + int64_t wsum; + int coeff_prec, qlevel; + int coeffs[pred_order]; + + /* warm up samples */ + for (i = 0; i < pred_order; i++) + { + decoded[i] = get_sbits(&s->gb, s->curr_bps); + } + + coeff_prec = get_bits(&s->gb, 4) + 1; + if (coeff_prec == 16) + { + //fprintf(stderr,"invalid coeff precision\n"); + return -1; + } + qlevel = get_sbits(&s->gb, 5); + if (qlevel < 0) + { + //fprintf(stderr,"qlevel %d not supported, maybe buggy stream\n", qlevel); + return -1; + } + + for (i = 0; i < pred_order; i++) + { + coeffs[i] = get_sbits(&s->gb, coeff_prec); + } + + if (decode_residuals(s, decoded, pred_order) < 0) + return -1; + + if ((s->bps + coeff_prec + av_log2(pred_order)) <= 32) { + for (i = pred_order; i < s->blocksize; i++) + { + sum = 0; + for (j = 0; j < pred_order; j++) + sum += coeffs[j] * decoded[i-j-1]; + decoded[i] += sum >> qlevel; + } + } else { + for (i = pred_order; i < s->blocksize; i++) + { + wsum = 0; + for (j = 0; j < pred_order; j++) + wsum += (int64_t)coeffs[j] * (int64_t)decoded[i-j-1]; + decoded[i] += wsum >> qlevel; + } + } + + return 0; +} + +static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded) +{ + int type, wasted = 0; + int i, tmp; + + s->curr_bps = s->bps; + if(channel == 0){ + if(s->decorrelation == RIGHT_SIDE) + s->curr_bps++; + }else{ + if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE) + s->curr_bps++; + } + + if (get_bits1(&s->gb)) + { + //av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n"); + return -1; + } + type = get_bits(&s->gb, 6); +// wasted = get_bits1(&s->gb); + +// if (wasted) +// { +// while (!get_bits1(&s->gb)) +// wasted++; +// if (wasted) +// wasted++; +// s->curr_bps -= wasted; +// } +#if 0 + wasted= 16 - av_log2(show_bits(&s->gb, 17)); + skip_bits(&s->gb, wasted+1); + s->curr_bps -= wasted; +#else + if (get_bits1(&s->gb)) + { + wasted = 1; + while (!get_bits1(&s->gb)) + wasted++; + s->curr_bps -= wasted; + //fprintf(stderr,"%d wasted bits\n", wasted); + } +#endif +//FIXME use av_log2 for types + if (type == 0) + { + //fprintf(stderr,"coding type: constant\n"); + tmp = get_sbits(&s->gb, s->curr_bps); + for (i = 0; i < s->blocksize; i++) + decoded[i] = tmp; + } + else if (type == 1) + { + //fprintf(stderr,"coding type: verbatim\n"); + for (i = 0; i < s->blocksize; i++) + decoded[i] = get_sbits(&s->gb, s->curr_bps); + } + else if ((type >= 8) && (type <= 12)) + { + //fprintf(stderr,"coding type: fixed\n"); + if (decode_subframe_fixed(s, decoded, type & ~0x8) < 0) + return -1; + } + else if (type >= 32) + { + //fprintf(stderr,"coding type: lpc\n"); + if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1) < 0) + return -1; + } + else + { + //fprintf(stderr,"Unknown coding type: %d\n",type); + return -1; + } + + if (wasted) + { + int i; + for (i = 0; i < s->blocksize; i++) + decoded[i] <<= wasted; + } + + return 0; +} + +static int decode_frame(FLACContext *s, + int32_t* decoded0, + int32_t* decoded1, + void (*yield)(void)) +{ + int blocksize_code, sample_rate_code, sample_size_code, assignment, crc8; + int decorrelation, bps, blocksize, samplerate; + + blocksize_code = get_bits(&s->gb, 4); + + sample_rate_code = get_bits(&s->gb, 4); + + assignment = get_bits(&s->gb, 4); /* channel assignment */ + if (assignment < 8 && s->channels == assignment+1) + decorrelation = INDEPENDENT; + else if (assignment >=8 && assignment < 11 && s->channels == 2) + decorrelation = LEFT_SIDE + assignment - 8; + else + { + return -1; + } + + sample_size_code = get_bits(&s->gb, 3); + if(sample_size_code == 0) + bps= s->bps; + else if((sample_size_code != 3) && (sample_size_code != 7)) + bps = sample_size_table[sample_size_code]; + else + { + return -1; + } + + if (get_bits1(&s->gb)) + { + return -1; + } + + /* Get the samplenumber of the first sample in this block */ + s->samplenumber=get_utf8(&s->gb); + + /* samplenumber actually contains the frame number for streams + with a constant block size - so we multiply by blocksize to + get the actual sample number */ + if (s->min_blocksize == s->max_blocksize) { + s->samplenumber*=s->min_blocksize; + } + +#if 0 + if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/ + (s->min_blocksize != s->max_blocksize)){ + }else{ + } +#endif + + if (blocksize_code == 0) + blocksize = s->min_blocksize; + else if (blocksize_code == 6) + blocksize = get_bits(&s->gb, 8)+1; + else if (blocksize_code == 7) + blocksize = get_bits(&s->gb, 16)+1; + else + blocksize = blocksize_table[blocksize_code]; + + if(blocksize > s->max_blocksize){ + return -1; + } + + if (sample_rate_code == 0){ + samplerate= s->samplerate; + }else if ((sample_rate_code > 3) && (sample_rate_code < 12)) + samplerate = sample_rate_table[sample_rate_code]; + else if (sample_rate_code == 12) + samplerate = get_bits(&s->gb, 8) * 1000; + else if (sample_rate_code == 13) + samplerate = get_bits(&s->gb, 16); + else if (sample_rate_code == 14) + samplerate = get_bits(&s->gb, 16) * 10; + else{ + return -1; + } + + skip_bits(&s->gb, 8); + crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8); + if(crc8){ + return -1; + } + + s->blocksize = blocksize; + s->samplerate = samplerate; + s->bps = bps; + s->decorrelation= decorrelation; + + yield(); + /* subframes */ + if (decode_subframe(s, 0, decoded0) < 0) + return -1; + + yield(); + + if (s->channels==2) { + if (decode_subframe(s, 1, decoded1) < 0) + return -1; + } + + yield(); + align_get_bits(&s->gb); + + /* frame footer */ + skip_bits(&s->gb, 16); /* data crc */ + + return 0; +} + +int flac_decode_frame(FLACContext *s, + int32_t* decoded0, + int32_t* decoded1, + uint8_t *buf, int buf_size, + void (*yield)(void)) +{ + int tmp = 0, i, input_buf_size = 0; + int framesize; + int scale; + + init_get_bits(&s->gb, buf, buf_size*8); + + tmp = show_bits(&s->gb, 16); + if(tmp != 0xFFF8){ + //fprintf(stderr,"FRAME HEADER not here\n"); + while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8) + skip_bits(&s->gb, 8); + goto end; // we may not have enough bits left to decode a frame, so try next time + } + skip_bits(&s->gb, 16); + + if ((framesize=decode_frame(s,decoded0,decoded1,yield)) < 0){ + s->bitstream_size=0; + s->bitstream_index=0; + return -1; + } + + yield(); + + scale=FLAC_OUTPUT_DEPTH-s->bps; + switch(s->decorrelation) + { + case INDEPENDENT: + if (s->channels==1) {; + for (i = 0; i < s->blocksize; i++) + { + decoded0[i] = decoded0[i] << scale; + } + } else { + for (i = 0; i < s->blocksize; i++) + { + decoded0[i] = decoded0[i] << scale; + decoded1[i] = decoded1[i] << scale; + } + } + break; + case LEFT_SIDE: + //assert(s->channels == 2); + for (i = 0; i < s->blocksize; i++) + { + decoded1[i] = (decoded0[i] - decoded1[i]) << scale; + decoded0[i] = decoded0[i] << scale; + } + break; + case RIGHT_SIDE: + //assert(s->channels == 2); + for (i = 0; i < s->blocksize; i++) + { + decoded0[i] = (decoded0[i] + decoded1[i]) << scale; + decoded1[i] = decoded1[i] << scale; + } + break; + case MID_SIDE: + //assert(s->channels == 2); + for (i = 0; i < s->blocksize; i++) + { + int mid, side; + mid = decoded0[i]; + side = decoded1[i]; + +#if 1 //needs to be checked but IMHO it should be binary identical + mid -= side>>1; + decoded0[i] = (mid + side) << scale; + decoded1[i] = mid << scale; +#else + + mid <<= 1; + if (side & 1) + mid++; + decoded0[i] = ((mid + side) >> 1) << scale; + decoded1[i] = ((mid - side) >> 1) << scale; +#endif + } + break; + } + +end: + i= (get_bits_count(&s->gb)+7)/8;; + if(i > buf_size){ + s->bitstream_size=0; + s->bitstream_index=0; + return -1; + } + + if(s->bitstream_size){ + s->bitstream_index += i; + s->bitstream_size -= i; + return input_buf_size; + }else + return i; +} diff --git a/apps/codecs/libffmpegFLAC/decoder.h b/apps/codecs/libffmpegFLAC/decoder.h new file mode 100644 index 0000000000..5d503adc78 --- /dev/null +++ b/apps/codecs/libffmpegFLAC/decoder.h @@ -0,0 +1,46 @@ +#ifndef _FLAC_DECODER_H +#define _FLAC_DECODER_H + +#include "bitstream.h" + +#define MAX_CHANNELS 2 /* Maximum supported channels */ +#define MAX_BLOCKSIZE 4608 /* Maxsize in samples of one uncompressed frame */ +#define MAX_FRAMESIZE 32768 /* Maxsize in bytes of one compressed frame */ + +#define FLAC_OUTPUT_DEPTH 28 /* Provide samples left-shifted to 28-bits */ + +enum decorrelation_type { + INDEPENDENT, + LEFT_SIDE, + RIGHT_SIDE, + MID_SIDE, +}; + +typedef struct FLACContext { + GetBitContext gb; + + int min_blocksize, max_blocksize; + int min_framesize, max_framesize; + int samplerate, channels; + int blocksize/*, last_blocksize*/; + int bps, curr_bps; + unsigned long samplenumber; + unsigned long totalsamples; + enum decorrelation_type decorrelation; + + int filesize; + int length; + int bitrate; + int metadatalength; + + int bitstream_size; + int bitstream_index; +} FLACContext; + +int flac_decode_frame(FLACContext *s, + int32_t* decoded0, + int32_t* decoded1, + uint8_t *buf, int buf_size, + void (*yield)(void)); + +#endif diff --git a/apps/codecs/libffmpegFLAC/golomb.h b/apps/codecs/libffmpegFLAC/golomb.h new file mode 100644 index 0000000000..9dcbcb2d54 --- /dev/null +++ b/apps/codecs/libffmpegFLAC/golomb.h @@ -0,0 +1,106 @@ +/* + * exp golomb vlc stuff + * Copyright (c) 2003 Michael Niedermayer + * Copyright (c) 2004 Alex Beregszaszi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + + +/* From libavutil/common.h */ +extern const uint8_t ff_log2_tab[256]; +static inline int av_log2(unsigned int v) +{ + int n; + + n = 0; + if (v & 0xffff0000) { + v >>= 16; + n += 16; + } + if (v & 0xff00) { + v >>= 8; + n += 8; + } + n += ff_log2_tab[v]; + + return n; +} + +/** + * @file golomb.h + * @brief + * exp golomb vlc stuff + * @author Michael Niedermayer and Alex Beregszaszi + */ + + +/** + * read unsigned golomb rice code (jpegls). + */ +static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){ + unsigned int buf; + int log; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf=GET_CACHE(re, gb); + + log= av_log2(buf); + + if(log > 31-11){ + buf >>= log - k; + buf += (30-log)<>1) ^ -(v&1); +} diff --git a/apps/codecs/libffmpegFLAC/main.c b/apps/codecs/libffmpegFLAC/main.c new file mode 100644 index 0000000000..eea97f4f18 --- /dev/null +++ b/apps/codecs/libffmpegFLAC/main.c @@ -0,0 +1,292 @@ +/*************************************************************************** + * __________ __ ___. + * 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. + * + ****************************************************************************/ + +/* A test program for the Rockbox version of the ffmpeg FLAC decoder. + + Compile using Makefile.test - run it as "./test file.flac" to decode the + FLAC file to the file "test.wav" in the current directory + + This test program should support 16-bit and 24-bit mono and stereo files. + + The resulting "test.wav" should have the same md5sum as a WAV file created + by the official FLAC decoder (it produces the same 44-byte canonical WAV + header. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "decoder.h" + +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=Uncompressed) + 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 +}; + +int open_wav(char* filename) { + int fd; + + fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR); + if (fd >= 0) { + write(fd,wav_header,sizeof(wav_header)); + } + return(fd); +} + +void close_wav(int fd, FLACContext* fc) { + int x; + int filesize; + int bytespersample; + + bytespersample=fc->bps/8; + + filesize=fc->totalsamples*bytespersample*fc->channels+44; + + // 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; + + // Number of channels + wav_header[22]=fc->channels; + + // Samplerate + wav_header[24]=fc->samplerate&0xff; + wav_header[25]=(fc->samplerate&0xff00)>>8; + wav_header[26]=(fc->samplerate&0xff0000)>>16; + wav_header[27]=(fc->samplerate&0xff000000)>>24; + + // ByteRate + x=fc->samplerate*(fc->bps/8)*fc->channels; + wav_header[28]=(x&0xff); + wav_header[29]=(x&0xff00)>>8; + wav_header[30]=(x&0xff0000)>>16; + wav_header[31]=(x&0xff000000)>>24; + + // BlockAlign + wav_header[32]=(fc->bps/8)*fc->channels; + + // Bits per sample + wav_header[34]=fc->bps; + + // 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; + + lseek(fd,0,SEEK_SET); + write(fd,wav_header,sizeof(wav_header)); + close(fd); +} + +static void dump_headers(FLACContext *s) +{ + fprintf(stderr," Blocksize: %d .. %d\n", s->min_blocksize, + s->max_blocksize); + fprintf(stderr," Framesize: %d .. %d\n", s->min_framesize, + s->max_framesize); + fprintf(stderr," Samplerate: %d\n", s->samplerate); + fprintf(stderr," Channels: %d\n", s->channels); + fprintf(stderr," Bits per sample: %d\n", s->bps); + fprintf(stderr," Metadata length: %d\n", s->metadatalength); + fprintf(stderr," Total Samples: %lu\n",s->totalsamples); + fprintf(stderr," Duration: %d ms\n",s->length); + fprintf(stderr," Bitrate: %d kbps\n",s->bitrate); +} + +static bool flac_init(int fd, FLACContext* fc) +{ + unsigned char buf[255]; + struct stat statbuf; + bool found_streaminfo=false; + int endofmetadata=0; + int blocklength; + + if (lseek(fd, 0, SEEK_SET) < 0) + { + return false; + } + + if (read(fd, buf, 4) < 4) + { + return false; + } + + if (memcmp(buf,"fLaC",4) != 0) + { + return false; + } + fc->metadatalength = 4; + + while (!endofmetadata) { + if (read(fd, buf, 4) < 4) + { + return false; + } + + endofmetadata=(buf[0]&0x80); + blocklength = (buf[1] << 16) | (buf[2] << 8) | buf[3]; + fc->metadatalength+=blocklength+4; + + if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */ + { + /* FIXME: Don't trust the value of blocklength */ + if (read(fd, buf, blocklength) < 0) + { + return false; + } + + fstat(fd,&statbuf); + fc->filesize = statbuf.st_size; + fc->min_blocksize = (buf[0] << 8) | buf[1]; + fc->max_blocksize = (buf[2] << 8) | buf[3]; + fc->min_framesize = (buf[4] << 16) | (buf[5] << 8) | buf[6]; + fc->max_framesize = (buf[7] << 16) | (buf[8] << 8) | buf[9]; + fc->samplerate = (buf[10] << 12) | (buf[11] << 4) + | ((buf[12] & 0xf0) >> 4); + fc->channels = ((buf[12]&0x0e)>>1) + 1; + fc->bps = (((buf[12]&0x01) << 4) | ((buf[13]&0xf0)>>4) ) + 1; + + /* totalsamples is a 36-bit field, but we assume <= 32 bits are + used */ + fc->totalsamples = (buf[14] << 24) | (buf[15] << 16) + | (buf[16] << 8) | buf[17]; + + /* Calculate track length (in ms) and estimate the bitrate + (in kbit/s) */ + fc->length = (fc->totalsamples / fc->samplerate) * 1000; + + found_streaminfo=true; + } else if ((buf[0] & 0x7f) == 3) { /* 3 is the SEEKTABLE block */ + fprintf(stderr,"Seektable length = %d bytes\n",blocklength); + if (lseek(fd, blocklength, SEEK_CUR) < 0) { + return false; + } + } else { + /* Skip to next metadata block */ + if (lseek(fd, blocklength, SEEK_CUR) < 0) + { + return false; + } + } + } + + if (found_streaminfo) { + fc->bitrate = ((fc->filesize-fc->metadatalength) * 8) / fc->length; + return true; + } else { + return false; + } +} + +/* Dummy function needed to pass to flac_decode_frame() */ +void yield() { +} + +int main(int argc, char* argv[]) { + FLACContext fc; + int fd,fdout; + int n; + int i; + int bytesleft; + int consumed; + char buf[MAX_FRAMESIZE]; /* The input buffer */ + /* The output buffers containing the decoded samples (channels 0 and 1) */ + int32_t decoded0[MAX_BLOCKSIZE]; + int32_t decoded1[MAX_BLOCKSIZE]; + + /* For testing */ + int8_t wavbuf[MAX_CHANNELS*MAX_BLOCKSIZE*3]; + int8_t* p; + int scale; + + fd=open(argv[1],O_RDONLY); + + if (fd < 0) { + fprintf(stderr,"Can not parse %s\n",argv[1]); + return(1); + } + + /* Read the metadata and position the file pointer at the start of the + first audio frame */ + flac_init(fd,&fc); + + dump_headers(&fc); + + fdout=open_wav("test.wav"); + bytesleft=read(fd,buf,sizeof(buf)); + while (bytesleft) { + if(flac_decode_frame(&fc,decoded0,decoded1,buf,bytesleft,yield) < 0) { + fprintf(stderr,"DECODE ERROR, ABORTING\n"); + break; + } + consumed=fc.gb.index/8; + + scale=FLAC_OUTPUT_DEPTH-fc.bps; + p=wavbuf; + for (i=0;i>scale; + *(p++)=decoded0[i]&0xff; + *(p++)=(decoded0[i]&0xff00)>>8; + if (fc.bps==24) *(p++)=(decoded0[i]&0xff0000)>>16; + + if (fc.channels==2) { + /* Right sample */ + decoded1[i]=decoded1[i]>>scale; + *(p++)=decoded1[i]&0xff; + *(p++)=(decoded1[i]&0xff00)>>8; + if (fc.bps==24) *(p++)=(decoded1[i]&0xff0000)>>16; + } + } + write(fdout,wavbuf,fc.blocksize*fc.channels*(fc.bps/8)); + + memmove(buf,&buf[consumed],bytesleft-consumed); + bytesleft-=consumed; + + n=read(fd,&buf[bytesleft],sizeof(buf)-bytesleft); + if (n > 0) { + bytesleft+=n; + } + } + close_wav(fdout,&fc); + close(fd); + return(0); +} diff --git a/apps/plugins/Makefile b/apps/plugins/Makefile index 172b757acc..c7aa438b60 100644 --- a/apps/plugins/Makefile +++ b/apps/plugins/Makefile @@ -17,7 +17,7 @@ ifdef APPEXTRA endif ifdef SOFTWARECODECS - CODECLIBS = -lmad -la52 -lFLAC -lTremor -lwavpack -lmusepack + CODECLIBS = -lmad -la52 -lffmpegFLAC -lTremor -lwavpack -lmusepack endif LDS := plugin.lds diff --git a/tools/configure b/tools/configure index 9c6f2df319..4a0a4ee47a 100755 --- a/tools/configure +++ b/tools/configure @@ -523,7 +523,7 @@ appsdir='\$(ROOTDIR)/apps' archosrom="" flash="" plugins="yes" - codecs="libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac" + codecs="libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac" ;; 10) @@ -537,7 +537,7 @@ appsdir='\$(ROOTDIR)/apps' archosrom="" flash="" plugins="yes" - codecs="libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac" + codecs="libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac" ;; 11) @@ -551,7 +551,7 @@ appsdir='\$(ROOTDIR)/apps' archosrom="" flash="" plugins="yes" - codecs="libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac" + codecs="libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac" ;; 12) @@ -565,7 +565,7 @@ appsdir='\$(ROOTDIR)/apps' archosrom="" flash="" plugins="yes" - codecs="libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac" + codecs="libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac" ;; *) -- cgit v1.2.3