summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Chapman <dave@dchapman.com>2005-10-26 12:35:58 +0000
committerDave Chapman <dave@dchapman.com>2005-10-26 12:35:58 +0000
commit273d2e81f72c7721447ab9c539877f6712faaecc (patch)
tree79c66b0b699ca65ac2cc2369cd5bbed764c35a4b
parent48be8e6a8b45fa16380af97829ba944430a78f17 (diff)
downloadrockbox-273d2e81f72c7721447ab9c539877f6712faaecc.tar.gz
rockbox-273d2e81f72c7721447ab9c539877f6712faaecc.zip
New FLAC decoder from the ffmpeg project
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7656 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/FILES6
-rw-r--r--apps/codecs/Makefile18
-rw-r--r--apps/codecs/flac.c405
-rw-r--r--apps/codecs/libffmpegFLAC/COPYING504
-rw-r--r--apps/codecs/libffmpegFLAC/Makefile47
-rw-r--r--apps/codecs/libffmpegFLAC/Makefile.test21
-rw-r--r--apps/codecs/libffmpegFLAC/README19
-rw-r--r--apps/codecs/libffmpegFLAC/README.rockbox46
-rw-r--r--apps/codecs/libffmpegFLAC/SOURCES2
-rw-r--r--apps/codecs/libffmpegFLAC/bitstream.c62
-rw-r--r--apps/codecs/libffmpegFLAC/bitstream.h333
-rw-r--r--apps/codecs/libffmpegFLAC/decoder.c584
-rw-r--r--apps/codecs/libffmpegFLAC/decoder.h46
-rw-r--r--apps/codecs/libffmpegFLAC/golomb.h106
-rw-r--r--apps/codecs/libffmpegFLAC/main.c292
-rw-r--r--apps/plugins/Makefile2
-rwxr-xr-xtools/configure8
17 files changed, 2201 insertions, 300 deletions
diff --git a/apps/FILES b/apps/FILES
index 07719a4501..816b8c60d9 100644
--- a/apps/FILES
+++ b/apps/FILES
@@ -34,11 +34,7 @@ codecs/SOURCES
34codecs/*.[ch] 34codecs/*.[ch]
35codecs/Makefile 35codecs/Makefile
36codecs/liba52/* 36codecs/liba52/*
37codecs/libFLAC/* 37codecs/libffmpegFLAC/*
38codecs/libFLAC/include/private/*.h
39codecs/libFLAC/include/protected/*.h
40codecs/libFLAC/include/FLAC/*.h
41codecs/libFLAC/include/*.h
42codecs/libmad/* 38codecs/libmad/*
43codecs/libwavpack/* 39codecs/libwavpack/*
44codecs/Tremor/* 40codecs/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
17endif 17endif
18 18
19ifdef SOFTWARECODECS 19ifdef SOFTWARECODECS
20 CODECLIBS = -lmad -la52 -lFLAC -lTremor -lwavpack -lmusepack -lalac 20 CODECLIBS = -lmad -la52 -lffmpegFLAC -lTremor -lwavpack -lmusepack -lalac
21endif 21endif
22 22
23# we "borrow" the plugin LDS file 23# we "borrow" the plugin LDS file
@@ -39,7 +39,7 @@ DIRS = .
39 39
40CODECDEPS = $(LINKCODEC) $(BUILDDIR)/libcodec.a 40CODECDEPS = $(LINKCODEC) $(BUILDDIR)/libcodec.a
41 41
42.PHONY: libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac 42.PHONY: libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac
43 43
44OUTPUT = $(SOFTWARECODECS) 44OUTPUT = $(SOFTWARECODECS)
45 45
@@ -55,7 +55,7 @@ ELFIT = $(SILENT)(file=`basename $@`; \
55 55
56$(OBJDIR)/mpa.elf: $(OBJDIR)/mpa.o $(CODECDEPS) $(BUILDDIR)/libmad.a 56$(OBJDIR)/mpa.elf: $(OBJDIR)/mpa.o $(CODECDEPS) $(BUILDDIR)/libmad.a
57$(OBJDIR)/a52.elf: $(OBJDIR)/a52.o $(CODECDEPS) $(BUILDDIR)/liba52.a 57$(OBJDIR)/a52.elf: $(OBJDIR)/a52.o $(CODECDEPS) $(BUILDDIR)/liba52.a
58$(OBJDIR)/flac.elf: $(OBJDIR)/flac.o $(CODECDEPS) $(BUILDDIR)/libFLAC.a 58$(OBJDIR)/flac.elf: $(OBJDIR)/flac.o $(CODECDEPS) $(BUILDDIR)/libffmpegFLAC.a
59$(OBJDIR)/vorbis.elf: $(OBJDIR)/vorbis.o $(CODECDEPS) $(BUILDDIR)/libTremor.a 59$(OBJDIR)/vorbis.elf: $(OBJDIR)/vorbis.o $(CODECDEPS) $(BUILDDIR)/libTremor.a
60$(OBJDIR)/mpc.elf: $(OBJDIR)/mpc.o $(CODECDEPS) $(BUILDDIR)/libmusepack.a 60$(OBJDIR)/mpc.elf: $(OBJDIR)/mpc.o $(CODECDEPS) $(BUILDDIR)/libmusepack.a
61$(OBJDIR)/wav.elf: $(OBJDIR)/wav.o $(CODECDEPS) 61$(OBJDIR)/wav.elf: $(OBJDIR)/wav.o $(CODECDEPS)
@@ -128,10 +128,10 @@ liba52:
128 @mkdir -p $(OBJDIR)/liba52 128 @mkdir -p $(OBJDIR)/liba52
129 @$(MAKE) -C liba52 OBJDIR=$(OBJDIR)/liba52 OUTPUT=$(BUILDDIR)/liba52.a 129 @$(MAKE) -C liba52 OBJDIR=$(OBJDIR)/liba52 OUTPUT=$(BUILDDIR)/liba52.a
130 130
131libFLAC: 131libffmpegFLAC:
132 @echo "MAKE in libFLAC" 132 @echo "MAKE in libffmpegFLAC"
133 @mkdir -p $(OBJDIR)/libFLAC 133 @mkdir -p $(OBJDIR)/libffmpegFLAC
134 @$(MAKE) -C libFLAC OBJDIR=$(OBJDIR)/libFLAC OUTPUT=$(BUILDDIR)/libFLAC.a 134 @$(MAKE) -C libffmpegFLAC OBJDIR=$(OBJDIR)/libffmpegFLAC OUTPUT=$(BUILDDIR)/libffmpegFLAC.a
135 135
136libTremor: 136libTremor:
137 @echo "MAKE in Tremor" 137 @echo "MAKE in Tremor"
@@ -160,10 +160,10 @@ libalac:
160 160
161clean: 161clean:
162 @echo "cleaning codecs" 162 @echo "cleaning codecs"
163 $(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 163 $(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
164 @$(MAKE) -C libmad clean OBJDIR=$(OBJDIR)/libmad 164 @$(MAKE) -C libmad clean OBJDIR=$(OBJDIR)/libmad
165 @$(MAKE) -C liba52 clean OBJDIR=$(OBJDIR)/liba52 165 @$(MAKE) -C liba52 clean OBJDIR=$(OBJDIR)/liba52
166 @$(MAKE) -C libFLAC clean OBJDIR=$(OBJDIR)/libFLAC 166 @$(MAKE) -C libffmpegFLAC clean OBJDIR=$(OBJDIR)/libffmpegFLAC
167 @$(MAKE) -C Tremor clean OBJDIR=$(OBJDIR)/Tremor 167 @$(MAKE) -C Tremor clean OBJDIR=$(OBJDIR)/Tremor
168 @$(MAKE) -C libwavpack clean OBJDIR=$(OBJDIR)/libwavpack 168 @$(MAKE) -C libwavpack clean OBJDIR=$(OBJDIR)/libwavpack
169 @$(MAKE) -C libmusepack clean OBJDIR=$(OBJDIR)/libmusepack 169 @$(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 @@
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2002 Björn Stenberg 10 * Copyright (C) 2005 Dave Chapman
11 * 11 *
12 * All files in this archive are subject to the GNU General Public License. 12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement. 13 * See the file COPYING in the source tree root for full license agreement.
@@ -18,156 +18,7 @@
18 ****************************************************************************/ 18 ****************************************************************************/
19 19
20#include "codeclib.h" 20#include "codeclib.h"
21#include <codecs/libFLAC/include/FLAC/seekable_stream_decoder.h> 21#include <codecs/libffmpegFLAC/decoder.h>
22#include <codecs/libFLAC/include/FLAC/format.h>
23#include <codecs/libFLAC/include/FLAC/metadata.h>
24
25#define FLAC_MAX_SUPPORTED_BLOCKSIZE 4608
26#define FLAC_MAX_SUPPORTED_CHANNELS 2
27
28static uint32_t samplesdone;
29
30static FLAC__StreamMetadata *stream_info;
31static FLAC__StreamMetadata *seek_table;
32unsigned int metadata_length;
33
34/* Called when the FLAC decoder needs some FLAC data to decode */
35FLAC__SeekableStreamDecoderReadStatus flac_read_handler(const FLAC__SeekableStreamDecoder *dec,
36 FLAC__byte buffer[], unsigned *bytes, void *data)
37{
38 struct codec_api* ci = (struct codec_api*)data;
39 (void)dec;
40
41 *bytes=(unsigned)(ci->read_filebuf(buffer,*bytes));
42
43 if (*bytes==0) {
44 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
45 } else {
46 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
47 }
48}
49
50static unsigned char pcmbuf[FLAC_MAX_SUPPORTED_BLOCKSIZE*FLAC_MAX_SUPPORTED_CHANNELS*2] IDATA_ATTR;
51
52/* Called when the FLAC decoder has some decoded PCM data to write */
53FLAC__StreamDecoderWriteStatus flac_write_handler(const FLAC__SeekableStreamDecoder *dec,
54 const FLAC__Frame *frame,
55 const FLAC__int32 * const buf[],
56 void *data)
57{
58 struct codec_api* ci = (struct codec_api*)data;
59 (void)dec;
60 unsigned int c_samp, c_chan, d_samp;
61 uint32_t data_size = frame->header.blocksize * frame->header.channels * 2; /* Assume 16-bit words */
62 uint32_t samples = frame->header.blocksize;
63 int yieldcounter = 0;
64
65
66 if (samples*frame->header.channels > (FLAC_MAX_SUPPORTED_BLOCKSIZE*FLAC_MAX_SUPPORTED_CHANNELS)) {
67 // ERROR!!!
68 // DEBUGF("ERROR: samples*frame->header.channels=%d\n",samples*frame->header.channels);
69 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
70 }
71
72 (void)dec;
73 for (c_samp = d_samp = 0; c_samp < samples; c_samp++) {
74 for(c_chan = 0; c_chan < frame->header.channels; c_chan++, d_samp++) {
75 pcmbuf[d_samp*2] = (buf[c_chan][c_samp]&0xff00)>>8;
76 pcmbuf[(d_samp*2)+1] = buf[c_chan][c_samp]&0xff;
77 if (yieldcounter++ == 100) {
78 ci->yield();
79 yieldcounter = 0;
80 }
81 }
82 }
83
84 samplesdone+=samples;
85 ci->set_elapsed(samplesdone/(ci->id3->frequency/1000));
86
87 ci->yield();
88 while (!ci->pcmbuf_insert(pcmbuf, data_size))
89 ci->yield();
90
91 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
92}
93
94void flac_metadata_handler(const FLAC__SeekableStreamDecoder *dec,
95 const FLAC__StreamMetadata *meta, void *data)
96{
97 /* Ignore metadata for now... */
98 (void)dec;
99 (void)data;
100
101 metadata_length += meta->length;
102
103 if ( meta->type == FLAC__METADATA_TYPE_STREAMINFO ) {
104 stream_info = FLAC__metadata_object_clone( meta );
105 if ( stream_info == NULL ) {
106 //return CODEC_ERROR;
107 }
108 } else if ( meta->type == FLAC__METADATA_TYPE_SEEKTABLE ) {
109 seek_table = FLAC__metadata_object_clone( meta );
110 if ( seek_table == NULL ) {
111 //return CODEC_ERROR;
112 }
113 }
114}
115
116
117void flac_error_handler(const FLAC__SeekableStreamDecoder *dec,
118 FLAC__StreamDecoderErrorStatus status, void *data)
119{
120 (void)dec;
121 (void)status;
122 (void)data;
123}
124
125FLAC__SeekableStreamDecoderSeekStatus flac_seek_handler(const FLAC__SeekableStreamDecoder *decoder,
126 FLAC__uint64 absolute_byte_offset,
127 void *client_data)
128{
129 (void)decoder;
130 struct codec_api* ci = (struct codec_api*)client_data;
131
132 if (ci->seek_buffer(absolute_byte_offset)) {
133 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
134 } else {
135 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
136 }
137}
138
139FLAC__SeekableStreamDecoderTellStatus flac_tell_handler(const FLAC__SeekableStreamDecoder *decoder,
140 FLAC__uint64 *absolute_byte_offset, void *client_data)
141{
142 struct codec_api* ci = (struct codec_api*)client_data;
143
144 (void)decoder;
145 *absolute_byte_offset = ci->curpos;
146 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
147}
148
149FLAC__SeekableStreamDecoderLengthStatus flac_length_handler(const FLAC__SeekableStreamDecoder *decoder,
150 FLAC__uint64 *stream_length, void *client_data)
151{
152 struct codec_api* ci = (struct codec_api*)client_data;
153
154 (void)decoder;
155 *stream_length = ci->filesize;
156 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
157}
158
159FLAC__bool flac_eof_handler(const FLAC__SeekableStreamDecoder *decoder,
160 void *client_data)
161{
162 struct codec_api* ci = (struct codec_api*)client_data;
163
164 (void)decoder;
165 if (ci->curpos >= ci->filesize) {
166 return true;
167 } else {
168 return false;
169 }
170}
171 22
172#ifndef SIMULATOR 23#ifndef SIMULATOR
173extern char iramcopy[]; 24extern char iramcopy[];
@@ -175,78 +26,101 @@ extern char iramstart[];
175extern char iramend[]; 26extern char iramend[];
176#endif 27#endif
177 28
178FLAC__uint64 find_sample_number(struct codec_api *ci, size_t offset) 29struct codec_api* rb;
30struct codec_api* ci;
31
32/* The output buffers containing the decoded samples (channels 0 and 1) */
33int32_t decoded0[MAX_BLOCKSIZE] IBSS_ATTR;
34int32_t decoded1[MAX_BLOCKSIZE] IBSS_ATTR;
35
36static bool flac_init(FLACContext* fc)
179{ 37{
180 FLAC__StreamMetadata_SeekPoint *points; 38 unsigned char buf[255];
181 FLAC__uint64 prev_sample, next_sample; 39 bool found_streaminfo=false;
182 size_t prev_offset, next_offset; 40 int endofmetadata=0;
183 int percent; 41 int blocklength;
184 42
185 if (offset >= (ci->id3->filesize - metadata_length)) { 43 if (ci->read_filebuf(buf, 4) < 4)
186 return stream_info->data.stream_info.total_samples; 44 {
45 return false;
187 } 46 }
188 47
189 prev_offset = 0; 48 if (ci->memcmp(buf,"fLaC",4) != 0)
190 prev_sample = 0; 49 {
191 next_offset = ci->id3->filesize - metadata_length; 50 return false;
192 next_sample = stream_info->data.stream_info.total_samples; 51 }
193 52 fc->metadatalength = 4;
194 if (seek_table) {
195 int left, right, middle;
196 53
197 middle = 0; /* Silence compiler warnings */ 54 while (!endofmetadata) {
198 points = seek_table->data.seek_table.points; 55 if (ci->read_filebuf(buf, 4) < 4)
199 left = 0; 56 {
200 right = seek_table->data.seek_table.num_points - 1; 57 return false;
58 }
201 59
202 /* Do a binary search to find the matching seek point */ 60 endofmetadata=(buf[0]&0x80);
203 while (left <= right) { 61 blocklength = (buf[1] << 16) | (buf[2] << 8) | buf[3];
204 middle = (left + right) / 2; 62 fc->metadatalength+=blocklength+4;
205 63
206 if ((FLAC__uint64)offset < points[middle].stream_offset) { 64 if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */
207 right = middle - 1; 65 {
208 } else if ((FLAC__uint64)offset > points[middle].stream_offset) { 66 /* FIXME: Don't trust the value of blocklength */
209 left = middle + 1; 67 if (ci->read_filebuf(buf, blocklength) < 0)
210 } else { 68 {
211 return points[middle].sample_number; 69 return false;
212 } 70 }
213 } 71
214 72 fc->filesize = ci->filesize;
215 /* Didn't find a matching seek point, so get the sample numbers of the 73 fc->min_blocksize = (buf[0] << 8) | buf[1];
216 * seek points to the left and right of offset to make our guess more 74 fc->max_blocksize = (buf[2] << 8) | buf[3];
217 * accurate. Accuracy depends on how close these sample numbers are to 75 fc->min_framesize = (buf[4] << 16) | (buf[5] << 8) | buf[6];
218 * each other. 76 fc->max_framesize = (buf[7] << 16) | (buf[8] << 8) | buf[9];
219 */ 77 fc->samplerate = (buf[10] << 12) | (buf[11] << 4)
220 if ((unsigned)left >= seek_table->data.seek_table.num_points) { 78 | ((buf[12] & 0xf0) >> 4);
221 prev_offset = points[middle].stream_offset; 79 fc->channels = ((buf[12]&0x0e)>>1) + 1;
222 prev_sample = points[middle].sample_number; 80 fc->bps = (((buf[12]&0x01) << 4) | ((buf[13]&0xf0)>>4) ) + 1;
223 } else if (right < 0) { 81
224 next_offset = points[middle].stream_offset; 82 /* totalsamples is a 36-bit field, but we assume <= 32 bits are
225 next_sample = points[middle].sample_number; 83 used */
84 fc->totalsamples = (buf[14] << 24) | (buf[15] << 16)
85 | (buf[16] << 8) | buf[17];
86
87 /* Calculate track length (in ms) and estimate the bitrate
88 (in kbit/s) */
89 fc->length = (fc->totalsamples / fc->samplerate) * 1000;
90
91 found_streaminfo=true;
92 } else if ((buf[0] & 0x7f) == 3) { /* 3 is the SEEKTABLE block */
93 ci->advance_buffer(blocklength);
226 } else { 94 } else {
227 middle--; 95 /* Skip to next metadata block */
228 prev_offset = points[middle].stream_offset; 96 ci->advance_buffer(blocklength);
229 prev_sample = points[middle].sample_number;
230 next_offset = points[middle+1].stream_offset;
231 next_sample = points[middle+1].sample_number;
232 } 97 }
233 } 98 }
234 99
235 /* Either there's no seek table or we didn't find our seek point, so now we 100 if (found_streaminfo) {
236 * have to guess. 101 fc->bitrate = ((fc->filesize-fc->metadatalength) * 8) / fc->length;
237 */ 102 return true;
238 percent = ((offset - prev_offset) * 100) / (next_offset - prev_offset); 103 } else {
239 return (FLAC__uint64)(percent * (next_sample - prev_sample) / 100 + prev_sample); 104 return false;
105 }
240} 106}
241 107
242/* this is the codec entry point */ 108/* this is the codec entry point */
243enum codec_status codec_start(struct codec_api* api) 109enum codec_status codec_start(struct codec_api* api)
244{ 110{
245 struct codec_api* ci = api; 111 size_t n;
246 FLAC__SeekableStreamDecoder* flacDecoder; 112 static int8_t buf[MAX_FRAMESIZE];
247 FLAC__uint64 offset; 113 FLACContext fc;
114 uint32_t samplesdone;
115 uint32_t elapsedtime;
116 int bytesleft;
117 int consumed;
248 118
249 TEST_CODEC_API(ci); 119 /* Generic codec initialisation */
120 TEST_CODEC_API(api);
121
122 rb = api;
123 ci = (struct codec_api*)api;
250 124
251#ifndef SIMULATOR 125#ifndef SIMULATOR
252 ci->memcpy(iramstart, iramcopy, iramend-iramstart); 126 ci->memcpy(iramstart, iramcopy, iramend-iramstart);
@@ -254,19 +128,17 @@ enum codec_status codec_start(struct codec_api* api)
254 128
255 ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*10)); 129 ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*10));
256 ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512)); 130 ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512));
257 ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*1024)); 131 ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*128));
258 132
259 ci->configure(CODEC_DSP_ENABLE, (bool *)true); 133 ci->configure(CODEC_DSP_ENABLE, (bool *)true);
260 ci->configure(DSP_DITHER, (bool *)false); 134 ci->configure(DSP_DITHER, (bool *)false);
261 ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED); 135 ci->configure(DSP_SET_STEREO_MODE, (long *)STEREO_NONINTERLEAVED);
262 ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(16)); 136 ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(28));
263 137
264next_track: 138 next_track:
265 metadata_length = 0;
266 seek_table = NULL;
267 stream_info = NULL;
268 139
269 if (codec_init(api)) { 140 if (!flac_init(&fc)) {
141 LOGF("FLAC: Error initialising codec\n");
270 return CODEC_ERROR; 142 return CODEC_ERROR;
271 } 143 }
272 144
@@ -274,87 +146,58 @@ next_track:
274 ci->yield(); 146 ci->yield();
275 147
276 ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency)); 148 ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency));
277 codec_set_replaygain(ci->id3);
278
279 /* Create a decoder instance */
280 flacDecoder = FLAC__seekable_stream_decoder_new();
281 149
282 /* Set up the decoder and the callback functions - this must be done before init */ 150 /* The main decoding loop */
283 151 bytesleft=ci->read_filebuf(buf,sizeof(buf));
284 /* The following are required for stream_decoder and higher */ 152 while (bytesleft) {
285 FLAC__seekable_stream_decoder_set_client_data(flacDecoder,ci);
286 FLAC__seekable_stream_decoder_set_write_callback(flacDecoder, flac_write_handler);
287 FLAC__seekable_stream_decoder_set_read_callback(flacDecoder, flac_read_handler);
288 FLAC__seekable_stream_decoder_set_metadata_callback(flacDecoder, flac_metadata_handler);
289 FLAC__seekable_stream_decoder_set_error_callback(flacDecoder, flac_error_handler);
290 FLAC__seekable_stream_decoder_set_metadata_respond_all(flacDecoder);
291
292 /* The following are only for the seekable_stream_decoder */
293 FLAC__seekable_stream_decoder_set_seek_callback(flacDecoder, flac_seek_handler);
294 FLAC__seekable_stream_decoder_set_tell_callback(flacDecoder, flac_tell_handler);
295 FLAC__seekable_stream_decoder_set_length_callback(flacDecoder, flac_length_handler);
296 FLAC__seekable_stream_decoder_set_eof_callback(flacDecoder, flac_eof_handler);
297
298
299 /* QUESTION: What do we do when the init fails? */
300 if (FLAC__seekable_stream_decoder_init(flacDecoder)) {
301 return CODEC_ERROR;
302 }
303
304 /* The first thing to do is to parse the metadata */
305 FLAC__seekable_stream_decoder_process_until_end_of_metadata(flacDecoder);
306
307 if (ci->id3->offset && stream_info) {
308 FLAC__uint64 sample;
309
310 sample = find_sample_number(ci, ci->id3->offset - metadata_length);
311 ci->advance_buffer(ci->id3->offset);
312 FLAC__seekable_stream_decoder_seek_absolute(flacDecoder, sample);
313 FLAC__seekable_stream_decoder_get_decode_position(flacDecoder, &offset);
314 ci->set_offset(offset);
315 samplesdone = (uint32_t)sample;
316 ci->set_elapsed(sample/(ci->id3->frequency/1000));
317 } else {
318 samplesdone = 0;
319 ci->set_elapsed(0);
320 }
321
322 /* The main decoder loop */
323 while (FLAC__seekable_stream_decoder_get_state(flacDecoder) != FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM) {
324 ci->yield(); 153 ci->yield();
325 if (ci->stop_codec || ci->reload_codec) { 154 if (ci->stop_codec || ci->reload_codec) {
326 break; 155 break;
327 } 156 }
328 157
158 /* Deal with any pending seek requests */
329 if (ci->seek_time) { 159 if (ci->seek_time) {
330 int sample_loc; 160 /* We only support seeking to start of track at the moment */
161 if (ci->seek_time==1) {
162 if (ci->seek_buffer(fc.metadatalength)) {
163 /* Refill the input buffer */
164 bytesleft=ci->read_filebuf(buf,sizeof(buf));
165 ci->set_elapsed(0);
166 }
167 }
168 ci->seek_time = 0;
169 }
331 170
332 sample_loc = ci->seek_time/1000 * ci->id3->frequency; 171 if(flac_decode_frame(&fc,decoded0,decoded1,buf,
333 if (FLAC__seekable_stream_decoder_seek_absolute(flacDecoder, sample_loc)) { 172 bytesleft,ci->yield) < 0) {
334 samplesdone = sample_loc; 173 LOGF("FLAC: Decode error, aborting\n");
335 ci->set_elapsed(samplesdone/(ci->id3->frequency/1000)); 174 return CODEC_ERROR;
336 }
337 ci->seek_time = 0;
338 } 175 }
176 consumed=fc.gb.index/8;
339 177
340 FLAC__seekable_stream_decoder_process_single(flacDecoder); 178 ci->yield();
341 FLAC__seekable_stream_decoder_get_decode_position(flacDecoder, &offset); 179 while(!ci->pcmbuf_insert_split((char*)decoded0,(char*)decoded1,
342 ci->set_offset(offset); 180 fc.blocksize*4)) {
343 } 181 ci->yield();
182 }
344 183
345 /* Flush the libFLAC buffers */ 184 /* Update the elapsed-time indicator */
346 FLAC__seekable_stream_decoder_finish(flacDecoder); 185 samplesdone=fc.samplenumber+fc.blocksize;
186 elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
187 ci->set_elapsed(elapsedtime);
347 188
348 if (ci->request_next_track()) { 189 memmove(buf,&buf[consumed],bytesleft-consumed);
349 if (stream_info) { 190 bytesleft-=consumed;
350 FLAC__metadata_object_delete(stream_info); 191
351 } 192 n=ci->read_filebuf(&buf[bytesleft],sizeof(buf)-bytesleft);
352 if (seek_table) { 193 if (n > 0) {
353 FLAC__metadata_object_delete(seek_table); 194 bytesleft+=n;
354 } 195 }
355 metadata_length = 0;
356 goto next_track;
357 } 196 }
197 LOGF("FLAC: Decoded %d samples\n",samplesdone);
198
199 if (ci->request_next_track())
200 goto next_track;
358 201
359 return CODEC_OK; 202 return CODEC_OK;
360} 203}
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 @@
1 GNU LESSER GENERAL PUBLIC LICENSE
2 Version 2.1, February 1999
3
4 Copyright (C) 1991, 1999 Free Software Foundation, Inc.
5 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6 Everyone is permitted to copy and distribute verbatim copies
7 of this license document, but changing it is not allowed.
8
9[This is the first released version of the Lesser GPL. It also counts
10 as the successor of the GNU Library Public License, version 2, hence
11 the version number 2.1.]
12
13 Preamble
14
15 The licenses for most software are designed to take away your
16freedom to share and change it. By contrast, the GNU General Public
17Licenses are intended to guarantee your freedom to share and change
18free software--to make sure the software is free for all its users.
19
20 This license, the Lesser General Public License, applies to some
21specially designated software packages--typically libraries--of the
22Free Software Foundation and other authors who decide to use it. You
23can use it too, but we suggest you first think carefully about whether
24this license or the ordinary General Public License is the better
25strategy to use in any particular case, based on the explanations below.
26
27 When we speak of free software, we are referring to freedom of use,
28not price. Our General Public Licenses are designed to make sure that
29you have the freedom to distribute copies of free software (and charge
30for this service if you wish); that you receive source code or can get
31it if you want it; that you can change the software and use pieces of
32it in new free programs; and that you are informed that you can do
33these things.
34
35 To protect your rights, we need to make restrictions that forbid
36distributors to deny you these rights or to ask you to surrender these
37rights. These restrictions translate to certain responsibilities for
38you if you distribute copies of the library or if you modify it.
39
40 For example, if you distribute copies of the library, whether gratis
41or for a fee, you must give the recipients all the rights that we gave
42you. You must make sure that they, too, receive or can get the source
43code. If you link other code with the library, you must provide
44complete object files to the recipients, so that they can relink them
45with the library after making changes to the library and recompiling
46it. And you must show them these terms so they know their rights.
47
48 We protect your rights with a two-step method: (1) we copyright the
49library, and (2) we offer you this license, which gives you legal
50permission to copy, distribute and/or modify the library.
51
52 To protect each distributor, we want to make it very clear that
53there is no warranty for the free library. Also, if the library is
54modified by someone else and passed on, the recipients should know
55that what they have is not the original version, so that the original
56author's reputation will not be affected by problems that might be
57introduced by others.
58
59 Finally, software patents pose a constant threat to the existence of
60any free program. We wish to make sure that a company cannot
61effectively restrict the users of a free program by obtaining a
62restrictive license from a patent holder. Therefore, we insist that
63any patent license obtained for a version of the library must be
64consistent with the full freedom of use specified in this license.
65
66 Most GNU software, including some libraries, is covered by the
67ordinary GNU General Public License. This license, the GNU Lesser
68General Public License, applies to certain designated libraries, and
69is quite different from the ordinary General Public License. We use
70this license for certain libraries in order to permit linking those
71libraries into non-free programs.
72
73 When a program is linked with a library, whether statically or using
74a shared library, the combination of the two is legally speaking a
75combined work, a derivative of the original library. The ordinary
76General Public License therefore permits such linking only if the
77entire combination fits its criteria of freedom. The Lesser General
78Public License permits more lax criteria for linking other code with
79the library.
80
81 We call this license the "Lesser" General Public License because it
82does Less to protect the user's freedom than the ordinary General
83Public License. It also provides other free software developers Less
84of an advantage over competing non-free programs. These disadvantages
85are the reason we use the ordinary General Public License for many
86libraries. However, the Lesser license provides advantages in certain
87special circumstances.
88
89 For example, on rare occasions, there may be a special need to
90encourage the widest possible use of a certain library, so that it becomes
91a de-facto standard. To achieve this, non-free programs must be
92allowed to use the library. A more frequent case is that a free
93library does the same job as widely used non-free libraries. In this
94case, there is little to gain by limiting the free library to free
95software only, so we use the Lesser General Public License.
96
97 In other cases, permission to use a particular library in non-free
98programs enables a greater number of people to use a large body of
99free software. For example, permission to use the GNU C Library in
100non-free programs enables many more people to use the whole GNU
101operating system, as well as its variant, the GNU/Linux operating
102system.
103
104 Although the Lesser General Public License is Less protective of the
105users' freedom, it does ensure that the user of a program that is
106linked with the Library has the freedom and the wherewithal to run
107that program using a modified version of the Library.
108
109 The precise terms and conditions for copying, distribution and
110modification follow. Pay close attention to the difference between a
111"work based on the library" and a "work that uses the library". The
112former contains code derived from the library, whereas the latter must
113be combined with the library in order to run.
114
115 GNU LESSER GENERAL PUBLIC LICENSE
116 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
117
118 0. This License Agreement applies to any software library or other
119program which contains a notice placed by the copyright holder or
120other authorized party saying it may be distributed under the terms of
121this Lesser General Public License (also called "this License").
122Each licensee is addressed as "you".
123
124 A "library" means a collection of software functions and/or data
125prepared so as to be conveniently linked with application programs
126(which use some of those functions and data) to form executables.
127
128 The "Library", below, refers to any such software library or work
129which has been distributed under these terms. A "work based on the
130Library" means either the Library or any derivative work under
131copyright law: that is to say, a work containing the Library or a
132portion of it, either verbatim or with modifications and/or translated
133straightforwardly into another language. (Hereinafter, translation is
134included without limitation in the term "modification".)
135
136 "Source code" for a work means the preferred form of the work for
137making modifications to it. For a library, complete source code means
138all the source code for all modules it contains, plus any associated
139interface definition files, plus the scripts used to control compilation
140and installation of the library.
141
142 Activities other than copying, distribution and modification are not
143covered by this License; they are outside its scope. The act of
144running a program using the Library is not restricted, and output from
145such a program is covered only if its contents constitute a work based
146on the Library (independent of the use of the Library in a tool for
147writing it). Whether that is true depends on what the Library does
148and what the program that uses the Library does.
149
150 1. You may copy and distribute verbatim copies of the Library's
151complete source code as you receive it, in any medium, provided that
152you conspicuously and appropriately publish on each copy an
153appropriate copyright notice and disclaimer of warranty; keep intact
154all the notices that refer to this License and to the absence of any
155warranty; and distribute a copy of this License along with the
156Library.
157
158 You may charge a fee for the physical act of transferring a copy,
159and you may at your option offer warranty protection in exchange for a
160fee.
161
162 2. You may modify your copy or copies of the Library or any portion
163of it, thus forming a work based on the Library, and copy and
164distribute such modifications or work under the terms of Section 1
165above, provided that you also meet all of these conditions:
166
167 a) The modified work must itself be a software library.
168
169 b) You must cause the files modified to carry prominent notices
170 stating that you changed the files and the date of any change.
171
172 c) You must cause the whole of the work to be licensed at no
173 charge to all third parties under the terms of this License.
174
175 d) If a facility in the modified Library refers to a function or a
176 table of data to be supplied by an application program that uses
177 the facility, other than as an argument passed when the facility
178 is invoked, then you must make a good faith effort to ensure that,
179 in the event an application does not supply such function or
180 table, the facility still operates, and performs whatever part of
181 its purpose remains meaningful.
182
183 (For example, a function in a library to compute square roots has
184 a purpose that is entirely well-defined independent of the
185 application. Therefore, Subsection 2d requires that any
186 application-supplied function or table used by this function must
187 be optional: if the application does not supply it, the square
188 root function must still compute square roots.)
189
190These requirements apply to the modified work as a whole. If
191identifiable sections of that work are not derived from the Library,
192and can be reasonably considered independent and separate works in
193themselves, then this License, and its terms, do not apply to those
194sections when you distribute them as separate works. But when you
195distribute the same sections as part of a whole which is a work based
196on the Library, the distribution of the whole must be on the terms of
197this License, whose permissions for other licensees extend to the
198entire whole, and thus to each and every part regardless of who wrote
199it.
200
201Thus, it is not the intent of this section to claim rights or contest
202your rights to work written entirely by you; rather, the intent is to
203exercise the right to control the distribution of derivative or
204collective works based on the Library.
205
206In addition, mere aggregation of another work not based on the Library
207with the Library (or with a work based on the Library) on a volume of
208a storage or distribution medium does not bring the other work under
209the scope of this License.
210
211 3. You may opt to apply the terms of the ordinary GNU General Public
212License instead of this License to a given copy of the Library. To do
213this, you must alter all the notices that refer to this License, so
214that they refer to the ordinary GNU General Public License, version 2,
215instead of to this License. (If a newer version than version 2 of the
216ordinary GNU General Public License has appeared, then you can specify
217that version instead if you wish.) Do not make any other change in
218these notices.
219
220 Once this change is made in a given copy, it is irreversible for
221that copy, so the ordinary GNU General Public License applies to all
222subsequent copies and derivative works made from that copy.
223
224 This option is useful when you wish to copy part of the code of
225the Library into a program that is not a library.
226
227 4. You may copy and distribute the Library (or a portion or
228derivative of it, under Section 2) in object code or executable form
229under the terms of Sections 1 and 2 above provided that you accompany
230it with the complete corresponding machine-readable source code, which
231must be distributed under the terms of Sections 1 and 2 above on a
232medium customarily used for software interchange.
233
234 If distribution of object code is made by offering access to copy
235from a designated place, then offering equivalent access to copy the
236source code from the same place satisfies the requirement to
237distribute the source code, even though third parties are not
238compelled to copy the source along with the object code.
239
240 5. A program that contains no derivative of any portion of the
241Library, but is designed to work with the Library by being compiled or
242linked with it, is called a "work that uses the Library". Such a
243work, in isolation, is not a derivative work of the Library, and
244therefore falls outside the scope of this License.
245
246 However, linking a "work that uses the Library" with the Library
247creates an executable that is a derivative of the Library (because it
248contains portions of the Library), rather than a "work that uses the
249library". The executable is therefore covered by this License.
250Section 6 states terms for distribution of such executables.
251
252 When a "work that uses the Library" uses material from a header file
253that is part of the Library, the object code for the work may be a
254derivative work of the Library even though the source code is not.
255Whether this is true is especially significant if the work can be
256linked without the Library, or if the work is itself a library. The
257threshold for this to be true is not precisely defined by law.
258
259 If such an object file uses only numerical parameters, data
260structure layouts and accessors, and small macros and small inline
261functions (ten lines or less in length), then the use of the object
262file is unrestricted, regardless of whether it is legally a derivative
263work. (Executables containing this object code plus portions of the
264Library will still fall under Section 6.)
265
266 Otherwise, if the work is a derivative of the Library, you may
267distribute the object code for the work under the terms of Section 6.
268Any executables containing that work also fall under Section 6,
269whether or not they are linked directly with the Library itself.
270
271 6. As an exception to the Sections above, you may also combine or
272link a "work that uses the Library" with the Library to produce a
273work containing portions of the Library, and distribute that work
274under terms of your choice, provided that the terms permit
275modification of the work for the customer's own use and reverse
276engineering for debugging such modifications.
277
278 You must give prominent notice with each copy of the work that the
279Library is used in it and that the Library and its use are covered by
280this License. You must supply a copy of this License. If the work
281during execution displays copyright notices, you must include the
282copyright notice for the Library among them, as well as a reference
283directing the user to the copy of this License. Also, you must do one
284of these things:
285
286 a) Accompany the work with the complete corresponding
287 machine-readable source code for the Library including whatever
288 changes were used in the work (which must be distributed under
289 Sections 1 and 2 above); and, if the work is an executable linked
290 with the Library, with the complete machine-readable "work that
291 uses the Library", as object code and/or source code, so that the
292 user can modify the Library and then relink to produce a modified
293 executable containing the modified Library. (It is understood
294 that the user who changes the contents of definitions files in the
295 Library will not necessarily be able to recompile the application
296 to use the modified definitions.)
297
298 b) Use a suitable shared library mechanism for linking with the
299 Library. A suitable mechanism is one that (1) uses at run time a
300 copy of the library already present on the user's computer system,
301 rather than copying library functions into the executable, and (2)
302 will operate properly with a modified version of the library, if
303 the user installs one, as long as the modified version is
304 interface-compatible with the version that the work was made with.
305
306 c) Accompany the work with a written offer, valid for at
307 least three years, to give the same user the materials
308 specified in Subsection 6a, above, for a charge no more
309 than the cost of performing this distribution.
310
311 d) If distribution of the work is made by offering access to copy
312 from a designated place, offer equivalent access to copy the above
313 specified materials from the same place.
314
315 e) Verify that the user has already received a copy of these
316 materials or that you have already sent this user a copy.
317
318 For an executable, the required form of the "work that uses the
319Library" must include any data and utility programs needed for
320reproducing the executable from it. However, as a special exception,
321the materials to be distributed need not include anything that is
322normally distributed (in either source or binary form) with the major
323components (compiler, kernel, and so on) of the operating system on
324which the executable runs, unless that component itself accompanies
325the executable.
326
327 It may happen that this requirement contradicts the license
328restrictions of other proprietary libraries that do not normally
329accompany the operating system. Such a contradiction means you cannot
330use both them and the Library together in an executable that you
331distribute.
332
333 7. You may place library facilities that are a work based on the
334Library side-by-side in a single library together with other library
335facilities not covered by this License, and distribute such a combined
336library, provided that the separate distribution of the work based on
337the Library and of the other library facilities is otherwise
338permitted, and provided that you do these two things:
339
340 a) Accompany the combined library with a copy of the same work
341 based on the Library, uncombined with any other library
342 facilities. This must be distributed under the terms of the
343 Sections above.
344
345 b) Give prominent notice with the combined library of the fact
346 that part of it is a work based on the Library, and explaining
347 where to find the accompanying uncombined form of the same work.
348
349 8. You may not copy, modify, sublicense, link with, or distribute
350the Library except as expressly provided under this License. Any
351attempt otherwise to copy, modify, sublicense, link with, or
352distribute the Library is void, and will automatically terminate your
353rights under this License. However, parties who have received copies,
354or rights, from you under this License will not have their licenses
355terminated so long as such parties remain in full compliance.
356
357 9. You are not required to accept this License, since you have not
358signed it. However, nothing else grants you permission to modify or
359distribute the Library or its derivative works. These actions are
360prohibited by law if you do not accept this License. Therefore, by
361modifying or distributing the Library (or any work based on the
362Library), you indicate your acceptance of this License to do so, and
363all its terms and conditions for copying, distributing or modifying
364the Library or works based on it.
365
366 10. Each time you redistribute the Library (or any work based on the
367Library), the recipient automatically receives a license from the
368original licensor to copy, distribute, link with or modify the Library
369subject to these terms and conditions. You may not impose any further
370restrictions on the recipients' exercise of the rights granted herein.
371You are not responsible for enforcing compliance by third parties with
372this License.
373
374 11. If, as a consequence of a court judgment or allegation of patent
375infringement or for any other reason (not limited to patent issues),
376conditions are imposed on you (whether by court order, agreement or
377otherwise) that contradict the conditions of this License, they do not
378excuse you from the conditions of this License. If you cannot
379distribute so as to satisfy simultaneously your obligations under this
380License and any other pertinent obligations, then as a consequence you
381may not distribute the Library at all. For example, if a patent
382license would not permit royalty-free redistribution of the Library by
383all those who receive copies directly or indirectly through you, then
384the only way you could satisfy both it and this License would be to
385refrain entirely from distribution of the Library.
386
387If any portion of this section is held invalid or unenforceable under any
388particular circumstance, the balance of the section is intended to apply,
389and the section as a whole is intended to apply in other circumstances.
390
391It is not the purpose of this section to induce you to infringe any
392patents or other property right claims or to contest validity of any
393such claims; this section has the sole purpose of protecting the
394integrity of the free software distribution system which is
395implemented by public license practices. Many people have made
396generous contributions to the wide range of software distributed
397through that system in reliance on consistent application of that
398system; it is up to the author/donor to decide if he or she is willing
399to distribute software through any other system and a licensee cannot
400impose that choice.
401
402This section is intended to make thoroughly clear what is believed to
403be a consequence of the rest of this License.
404
405 12. If the distribution and/or use of the Library is restricted in
406certain countries either by patents or by copyrighted interfaces, the
407original copyright holder who places the Library under this License may add
408an explicit geographical distribution limitation excluding those countries,
409so that distribution is permitted only in or among countries not thus
410excluded. In such case, this License incorporates the limitation as if
411written in the body of this License.
412
413 13. The Free Software Foundation may publish revised and/or new
414versions of the Lesser General Public License from time to time.
415Such new versions will be similar in spirit to the present version,
416but may differ in detail to address new problems or concerns.
417
418Each version is given a distinguishing version number. If the Library
419specifies a version number of this License which applies to it and
420"any later version", you have the option of following the terms and
421conditions either of that version or of any later version published by
422the Free Software Foundation. If the Library does not specify a
423license version number, you may choose any version ever published by
424the Free Software Foundation.
425
426 14. If you wish to incorporate parts of the Library into other free
427programs whose distribution conditions are incompatible with these,
428write to the author to ask for permission. For software which is
429copyrighted by the Free Software Foundation, write to the Free
430Software Foundation; we sometimes make exceptions for this. Our
431decision will be guided by the two goals of preserving the free status
432of all derivatives of our free software and of promoting the sharing
433and reuse of software generally.
434
435 NO WARRANTY
436
437 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
438WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
439EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
440OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
441KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
442IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
443PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
444LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
445THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
446
447 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
448WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
449AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
450FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
451CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
452LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
453RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
454FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
455SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
456DAMAGES.
457
458 END OF TERMS AND CONDITIONS
459
460 How to Apply These Terms to Your New Libraries
461
462 If you develop a new library, and you want it to be of the greatest
463possible use to the public, we recommend making it free software that
464everyone can redistribute and change. You can do so by permitting
465redistribution under these terms (or, alternatively, under the terms of the
466ordinary General Public License).
467
468 To apply these terms, attach the following notices to the library. It is
469safest to attach them to the start of each source file to most effectively
470convey the exclusion of warranty; and each file should have at least the
471"copyright" line and a pointer to where the full notice is found.
472
473 <one line to give the library's name and a brief idea of what it does.>
474 Copyright (C) <year> <name of author>
475
476 This library is free software; you can redistribute it and/or
477 modify it under the terms of the GNU Lesser General Public
478 License as published by the Free Software Foundation; either
479 version 2 of the License, or (at your option) any later version.
480
481 This library is distributed in the hope that it will be useful,
482 but WITHOUT ANY WARRANTY; without even the implied warranty of
483 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
484 Lesser General Public License for more details.
485
486 You should have received a copy of the GNU Lesser General Public
487 License along with this library; if not, write to the Free Software
488 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
489
490Also add information on how to contact you by electronic and paper mail.
491
492You should also get your employer (if you work as a programmer) or your
493school, if any, to sign a "copyright disclaimer" for the library, if
494necessary. Here is a sample; alter the names:
495
496 Yoyodyne, Inc., hereby disclaims all copyright interest in the
497 library `Frob' (a library for tweaking knobs) written by James Random Hacker.
498
499 <signature of Ty Coon>, 1 April 1990
500 Ty Coon, President of Vice
501
502That's all there is to it!
503
504
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 @@
1# __________ __ ___.
2# Open \______ \ ____ ____ | | _\_ |__ _______ ___
3# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
4# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
5# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
6# \/ \/ \/ \/ \/
7# $Id$
8#
9
10INCLUDES=-I$(APPSDIR) -I.. -I. -I$(FIRMDIR)/include -I$(FIRMDIR)/export \
11 -I$(FIRMDIR)/common -I$(FIRMDIR)/drivers -I$(BUILDDIR)
12
13ifdef APPEXTRA
14INCLUDES += -I$(APPSDIR)/$(APPEXTRA)
15endif
16
17FLACOPTS = -O2
18CFLAGS = $(GCCOPTS) $(FLACOPTS) $(INCLUDES) $(TARGET) $(EXTRA_DEFINES) -DMEM=${MEMORYSIZE}
19
20# This sets up 'SRC' based on the files mentioned in SOURCES
21include $(TOOLSDIR)/makesrc.inc
22
23SOURCES = $(SRC)
24OBJS2 := $(SRC:%.c=$(OBJDIR)/%.o)
25OBJS = $(patsubst %.S, $(OBJDIR)/%.o, $(OBJS2))
26DEPFILE = $(OBJDIR)/dep-libalac
27DIRS =
28
29OUTPUT = $(BUILDDIR)/libalac.a
30
31all: $(OUTPUT)
32
33$(OUTPUT): $(OBJS)
34 @echo "AR $@"
35 @$(AR) ruv $@ $+ >/dev/null 2>&1
36
37$(OBJDIR)/libalac/%.o: $(APPSDIR)/codecs/libalac/%.c
38 @echo "(libalac) CC $<"
39 @$(CC) -c $(CFLAGS) -I$(APPSDIR)/codecs/libalac/ $< -o $@
40
41include $(TOOLSDIR)/make.inc
42
43clean:
44 @echo "cleaning libffmpegFLAC"
45 @rm -f $(OBJS) $(OUTPUT) $(DEPFILE)
46
47-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 @@
1# Simple Makefile for standalone testing of decoder
2
3OBJS=main.o decoder.o bitstream.o
4CFLAGS=-O2 -Wall -DBUILD_STANDALONE
5CC=gcc
6
7# Uncomment the next line to build on a big-endian target:
8
9#CFLAGS += -DBUILD_BIGENDIAN
10
11all: test
12
13main.o: main.c decoder.h
14decoder.o: decoder.c bitstream.h golomb.h decoder.h
15bitstream.o: bitstream.c
16
17test: $(OBJS)
18 gcc -o test $(OBJS)
19
20clean:
21 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 @@
1FFmpeg README
2-------------
3
41) Documentation
5----------------
6
7* Read the documentation in the doc/ directory.
8
92) Licensing
10------------
11
12* Read the file COPYING. ffmpeg and the associated libraries EXCEPT
13 liba52 and libpostproc are licensed under the Lesser GNU General
14 Public License.
15
16* liba52 and libpostproc are distributed under the GNU General Public
17 License and their compilation and use is optional in ffmpeg.
18
19Fabrice 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 @@
1Library: libffmpegFLAC
2Imported: 2005-10-26 by Dave Chapman
3
4This directory contains a FLAC decoder based on the flac.c (and a small
5number of other files) from the ffmpeg project.
6
7LICENSING INFORMATION
8
9ffmpeg is licensed under the Lesser GNU General Public License and the
10file flac.c is Copyright 2003 Alex Beregszaszi.
11
12IMPORT DETAILS
13
14The decoder is based on ffmpeg-cvs from 26 October 2005.
15
16The file libavcodec/flac.c was renamed to decoder.c and all ffmpeg
17related code was removed. It was also heavily modified to remove
18debugging information and unused experiemental code.
19
20It was also changed to provide output as 32-bit integers (with the
21data left-shifted to 28 bit accuracy). The original flac.c provided
2216-bit output.
23
24In order to minimise memory usage, a small number of hard-coded limits
25are present in decoder.h - mainly limiting the supported blocksize
26(number of samples in a frame) to 4608. This is the default value
27used by the reference FLAC encoder at all standard compression
28settings.
29
30
31TESTING
32
33A test program is contained in the file main.c and this should compile
34in any Unix-like environment using the command "make -f Makefile.test".
35
36Running "test file.flac" will decode the audio data to a WAV file
37called "test.wav" in the current directory. This has been tested with
3816-bit and 24-bit mono and stereo FLAC files.
39
40This can be used to test the accuracy of the decoder by comparing the
41md5sum of the WAV file produced by this decoder with a WAV file
42produced by the official libFLAC decoder.
43
44This test program could be extended to perform an internal md5sum
45calculation and comparing that against the md5sum stored in the FLAC
46file'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 @@
1bitstream.c
2decoder.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 @@
1/*
2 * Common bit i/o utils
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
21 */
22
23/**
24 * @file bitstream.c
25 * bitstream api.
26 */
27
28#include <stdio.h>
29#include "bitstream.h"
30
31/* bit input functions */
32
33/**
34 * reads 0-32 bits.
35 */
36unsigned int get_bits_long(GetBitContext *s, int n){
37 if(n<=17) return get_bits(s, n);
38 else{
39 int ret= get_bits(s, 16) << (n-16);
40 return ret | get_bits(s, n-16);
41 }
42}
43
44/**
45 * shows 0-32 bits.
46 */
47unsigned int show_bits_long(GetBitContext *s, int n){
48 if(n<=17) return show_bits(s, n);
49 else{
50 GetBitContext gb= *s;
51 int ret= get_bits_long(s, n);
52 *s= gb;
53 return ret;
54 }
55}
56
57void align_get_bits(GetBitContext *s)
58{
59 int n= (-get_bits_count(s)) & 7;
60 if(n) skip_bits(s, n);
61}
62
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 @@
1/**
2 * @file bitstream.h
3 * bitstream api header.
4 */
5
6#ifndef BITSTREAM_H
7#define BITSTREAM_H
8
9#include <inttypes.h>
10
11#ifndef BUILD_STANDALONE
12 #include <config.h>
13 #include <system.h>
14#else
15 #include <stdio.h>
16 #define IBSS_ATTR
17 #define ICONST_ATTR
18
19#endif
20
21/* Endian conversion routines for standalone compilation */
22#ifdef BUILD_STANDALONE
23 #ifdef BUILD_BIGENDIAN
24 #define betoh32(x) (x)
25 #define letoh32(x) swap32(x)
26 #else
27 #define letoh32(x) (x)
28 #define betoh32(x) swap32(x)
29 #endif
30
31 /* Taken from rockbox/firmware/export/system.h */
32
33 static inline unsigned short swap16(unsigned short value)
34 /*
35 result[15..8] = value[ 7..0];
36 result[ 7..0] = value[15..8];
37 */
38 {
39 return (value >> 8) | (value << 8);
40 }
41
42 static inline unsigned long swap32(unsigned long value)
43 /*
44 result[31..24] = value[ 7.. 0];
45 result[23..16] = value[15.. 8];
46 result[15.. 8] = value[23..16];
47 result[ 7.. 0] = value[31..24];
48 */
49 {
50 unsigned long hi = swap16(value >> 16);
51 unsigned long lo = swap16(value & 0xffff);
52 return (lo << 16) | hi;
53 }
54#endif
55
56/* FLAC files are big-endian */
57#define ALT_BITSTREAM_READER_BE
58
59#define NEG_SSR32(a,s) (((int32_t)(a))>>(32-(s)))
60#define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
61
62/* bit input */
63/* buffer, buffer_end and size_in_bits must be present and used by every reader */
64typedef struct GetBitContext {
65 const uint8_t *buffer, *buffer_end;
66 int index;
67 int size_in_bits;
68} GetBitContext;
69
70#define VLC_TYPE int16_t
71
72typedef struct VLC {
73 int bits;
74 VLC_TYPE (*table)[2]; ///< code, bits
75 int table_size, table_allocated;
76} VLC;
77
78typedef struct RL_VLC_ELEM {
79 int16_t level;
80 int8_t len;
81 uint8_t run;
82} RL_VLC_ELEM;
83
84#if defined(ARCH_SPARC) || defined(ARCH_ARMV4L)
85#define UNALIGNED_STORES_ARE_BAD
86#endif
87
88/* used to avoid missaligned exceptions on some archs (alpha, ...) */
89#if defined(ARCH_X86) || defined(ARCH_X86_64)
90# define unaligned32(a) (*(const uint32_t*)(a))
91#else
92# ifdef __GNUC__
93static inline uint32_t unaligned32(const void *v) {
94 struct Unaligned {
95 uint32_t i;
96 } __attribute__((packed));
97
98 return ((const struct Unaligned *) v)->i;
99}
100# elif defined(__DECC)
101static inline uint32_t unaligned32(const void *v) {
102 return *(const __unaligned uint32_t *) v;
103}
104# else
105static inline uint32_t unaligned32(const void *v) {
106 return *(const uint32_t *) v;
107}
108# endif
109#endif //!ARCH_X86
110
111
112/* Bitstream reader API docs:
113name
114 abritary name which is used as prefix for the internal variables
115
116gb
117 getbitcontext
118
119OPEN_READER(name, gb)
120 loads gb into local variables
121
122CLOSE_READER(name, gb)
123 stores local vars in gb
124
125UPDATE_CACHE(name, gb)
126 refills the internal cache from the bitstream
127 after this call at least MIN_CACHE_BITS will be available,
128
129GET_CACHE(name, gb)
130 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
131
132SHOW_UBITS(name, gb, num)
133 will return the next num bits
134
135SHOW_SBITS(name, gb, num)
136 will return the next num bits and do sign extension
137
138SKIP_BITS(name, gb, num)
139 will skip over the next num bits
140 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
141
142SKIP_CACHE(name, gb, num)
143 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
144
145SKIP_COUNTER(name, gb, num)
146 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
147
148LAST_SKIP_CACHE(name, gb, num)
149 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
150
151LAST_SKIP_BITS(name, gb, num)
152 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
153
154for examples see get_bits, show_bits, skip_bits, get_vlc
155*/
156
157static inline int unaligned32_be(const void *v)
158{
159#ifdef CONFIG_ALIGN
160 const uint8_t *p=v;
161 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
162#else
163 return betoh32( unaligned32(v)); //original
164#endif
165}
166
167static inline int unaligned32_le(const void *v)
168{
169#ifdef CONFIG_ALIGN
170 const uint8_t *p=v;
171 return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
172#else
173 return letoh32( unaligned32(v)); //original
174#endif
175}
176
177# define MIN_CACHE_BITS 25
178
179# define OPEN_READER(name, gb)\
180 int name##_index= (gb)->index;\
181 int name##_cache= 0;\
182
183# define CLOSE_READER(name, gb)\
184 (gb)->index= name##_index;\
185
186# ifdef ALT_BITSTREAM_READER_LE
187# define UPDATE_CACHE(name, gb)\
188 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
189
190# define SKIP_CACHE(name, gb, num)\
191 name##_cache >>= (num);
192# else
193# define UPDATE_CACHE(name, gb)\
194 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
195
196# define SKIP_CACHE(name, gb, num)\
197 name##_cache <<= (num);
198# endif
199
200// FIXME name?
201# define SKIP_COUNTER(name, gb, num)\
202 name##_index += (num);\
203
204# define SKIP_BITS(name, gb, num)\
205 {\
206 SKIP_CACHE(name, gb, num)\
207 SKIP_COUNTER(name, gb, num)\
208 }\
209
210# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
211# define LAST_SKIP_CACHE(name, gb, num) ;
212
213# ifdef ALT_BITSTREAM_READER_LE
214# define SHOW_UBITS(name, gb, num)\
215 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
216# else
217# define SHOW_UBITS(name, gb, num)\
218 NEG_USR32(name##_cache, num)
219# endif
220
221# define SHOW_SBITS(name, gb, num)\
222 NEG_SSR32(name##_cache, num)
223
224# define GET_CACHE(name, gb)\
225 ((uint32_t)name##_cache)
226
227static inline int get_bits_count(GetBitContext *s){
228 return s->index;
229}
230
231static inline int get_sbits(GetBitContext *s, int n){
232 register int tmp;
233 OPEN_READER(re, s)
234 UPDATE_CACHE(re, s)
235 tmp= SHOW_SBITS(re, s, n);
236 LAST_SKIP_BITS(re, s, n)
237 CLOSE_READER(re, s)
238 return tmp;
239}
240
241/**
242 * reads 0-17 bits.
243 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
244 */
245static inline unsigned int get_bits(GetBitContext *s, int n){
246 register int tmp;
247 OPEN_READER(re, s)
248 UPDATE_CACHE(re, s)
249 tmp= SHOW_UBITS(re, s, n);
250 LAST_SKIP_BITS(re, s, n)
251 CLOSE_READER(re, s)
252 return tmp;
253}
254
255unsigned int get_bits_long(GetBitContext *s, int n);
256
257/**
258 * shows 0-17 bits.
259 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
260 */
261static inline unsigned int show_bits(GetBitContext *s, int n){
262 register int tmp;
263 OPEN_READER(re, s)
264 UPDATE_CACHE(re, s)
265 tmp= SHOW_UBITS(re, s, n);
266// CLOSE_READER(re, s)
267 return tmp;
268}
269
270unsigned int show_bits_long(GetBitContext *s, int n);
271
272static inline void skip_bits(GetBitContext *s, int n){
273 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
274 OPEN_READER(re, s)
275 UPDATE_CACHE(re, s)
276 LAST_SKIP_BITS(re, s, n)
277 CLOSE_READER(re, s)
278}
279
280static inline unsigned int get_bits1(GetBitContext *s){
281 int index= s->index;
282 uint8_t result= s->buffer[ index>>3 ];
283#ifdef ALT_BITSTREAM_READER_LE
284 result>>= (index&0x07);
285 result&= 1;
286#else
287 result<<= (index&0x07);
288 result>>= 8 - 1;
289#endif
290 index++;
291 s->index= index;
292
293 return result;
294}
295
296static inline unsigned int show_bits1(GetBitContext *s){
297 return show_bits(s, 1);
298}
299
300static inline void skip_bits1(GetBitContext *s){
301 skip_bits(s, 1);
302}
303
304/**
305 * init GetBitContext.
306 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
307 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
308 * @param bit_size the size of the buffer in bits
309 */
310static inline void init_get_bits(GetBitContext *s,
311 const uint8_t *buffer, int bit_size)
312{
313 int buffer_size= (bit_size+7)>>3;
314 if(buffer_size < 0 || bit_size < 0) {
315 buffer_size = bit_size = 0;
316 buffer = 0;
317 }
318
319 s->buffer= buffer;
320 s->size_in_bits= bit_size;
321 s->buffer_end= buffer + buffer_size;
322 s->index=0;
323 {
324 OPEN_READER(re, s)
325 UPDATE_CACHE(re, s)
326 UPDATE_CACHE(re, s)
327 CLOSE_READER(re, s)
328 }
329}
330
331void align_get_bits(GetBitContext *s);
332
333#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 @@
1/*
2 * FLAC (Free Lossless Audio Codec) decoder
3 * Copyright (c) 2003 Alex Beregszaszi
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/**
21 * @file flac.c
22 * FLAC (Free Lossless Audio Codec) decoder
23 * @author Alex Beregszaszi
24 *
25 * For more information on the FLAC format, visit:
26 * http://flac.sourceforge.net/
27 *
28 * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed
29 * through, starting from the initial 'fLaC' signature; or by passing the
30 * 34-byte streaminfo structure through avctx->extradata[_size] followed
31 * by data starting with the 0xFFF8 marker.
32 */
33
34#include <inttypes.h>
35#include <stdbool.h>
36#ifndef BUILD_STANDALONE
37#include "../codec.h"
38#endif
39
40#include "bitstream.h"
41#include "golomb.h"
42
43#include "decoder.h"
44
45#define INT_MAX 2147483647
46
47#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
48#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
49
50/* Needed by golomb.h */
51const uint8_t ff_log2_tab[256] ICONST_ATTR = {
52 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,
53 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,
54 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,
55 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,
56 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,
57 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,
58 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,
59 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
60};
61
62static const int sample_rate_table[] ICONST_ATTR =
63{ 0, 0, 0, 0,
64 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
65 0, 0, 0, 0 };
66
67static const int sample_size_table[] ICONST_ATTR =
68{ 0, 8, 12, 0, 16, 20, 24, 0 };
69
70static const int blocksize_table[] ICONST_ATTR = {
71 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
72256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
73};
74
75static const uint8_t table_crc8[256] ICONST_ATTR = {
76 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
77 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d,
78 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
79 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d,
80 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5,
81 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
82 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85,
83 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd,
84 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
85 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea,
86 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2,
87 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
88 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32,
89 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a,
90 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
91 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a,
92 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c,
93 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
94 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec,
95 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4,
96 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
97 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44,
98 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c,
99 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
100 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b,
101 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63,
102 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
103 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13,
104 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb,
105 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
106 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb,
107 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3
108};
109
110static int64_t get_utf8(GetBitContext *gb)
111{
112 uint64_t val;
113 int ones=0, bytes;
114
115 while(get_bits1(gb))
116 ones++;
117
118 if (ones==0) bytes=0;
119 else if(ones==1) return -1;
120 else bytes= ones - 1;
121
122 val= get_bits(gb, 7-ones);
123 while(bytes--){
124 const int tmp = get_bits(gb, 8);
125
126 if((tmp>>6) != 2)
127 return -1;
128 val<<=6;
129 val|= tmp&0x3F;
130 }
131 return val;
132}
133
134static int get_crc8(const uint8_t *buf, int count){
135 int crc=0;
136 int i;
137
138 for(i=0; i<count; i++){
139 crc = table_crc8[crc ^ buf[i]];
140 }
141
142 return crc;
143}
144
145static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order)
146{
147 int i, tmp, partition, method_type, rice_order;
148 int sample = 0, samples;
149
150 method_type = get_bits(&s->gb, 2);
151 if (method_type != 0){
152 //fprintf(stderr,"illegal residual coding method %d\n", method_type);
153 return -1;
154 }
155
156 rice_order = get_bits(&s->gb, 4);
157
158 samples= s->blocksize >> rice_order;
159
160 sample=
161 i= pred_order;
162 for (partition = 0; partition < (1 << rice_order); partition++)
163 {
164 tmp = get_bits(&s->gb, 4);
165 if (tmp == 15)
166 {
167 //fprintf(stderr,"fixed len partition\n");
168 tmp = get_bits(&s->gb, 5);
169 for (; i < samples; i++, sample++)
170 decoded[sample] = get_sbits(&s->gb, tmp);
171 }
172 else
173 {
174 for (; i < samples; i++, sample++){
175 decoded[sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
176 }
177 }
178 i= 0;
179 }
180
181 return 0;
182}
183
184static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order)
185{
186 int i;
187
188 /* warm up samples */
189 for (i = 0; i < pred_order; i++)
190 {
191 decoded[i] = get_sbits(&s->gb, s->curr_bps);
192 }
193
194 if (decode_residuals(s, decoded, pred_order) < 0)
195 return -1;
196
197 switch(pred_order)
198 {
199 case 0:
200 break;
201 case 1:
202 for (i = pred_order; i < s->blocksize; i++)
203 decoded[i] += decoded[i-1];
204 break;
205 case 2:
206 for (i = pred_order; i < s->blocksize; i++)
207 decoded[i] += 2*decoded[i-1]
208 - decoded[i-2];
209 break;
210 case 3:
211 for (i = pred_order; i < s->blocksize; i++)
212 decoded[i] += 3*decoded[i-1]
213 - 3*decoded[i-2]
214 + decoded[i-3];
215 break;
216 case 4:
217 for (i = pred_order; i < s->blocksize; i++)
218 decoded[i] += 4*decoded[i-1]
219 - 6*decoded[i-2]
220 + 4*decoded[i-3]
221 - decoded[i-4];
222 break;
223 default:
224 return -1;
225 }
226
227 return 0;
228}
229
230static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order)
231{
232 int sum, i, j;
233 int64_t wsum;
234 int coeff_prec, qlevel;
235 int coeffs[pred_order];
236
237 /* warm up samples */
238 for (i = 0; i < pred_order; i++)
239 {
240 decoded[i] = get_sbits(&s->gb, s->curr_bps);
241 }
242
243 coeff_prec = get_bits(&s->gb, 4) + 1;
244 if (coeff_prec == 16)
245 {
246 //fprintf(stderr,"invalid coeff precision\n");
247 return -1;
248 }
249 qlevel = get_sbits(&s->gb, 5);
250 if (qlevel < 0)
251 {
252 //fprintf(stderr,"qlevel %d not supported, maybe buggy stream\n", qlevel);
253 return -1;
254 }
255
256 for (i = 0; i < pred_order; i++)
257 {
258 coeffs[i] = get_sbits(&s->gb, coeff_prec);
259 }
260
261 if (decode_residuals(s, decoded, pred_order) < 0)
262 return -1;
263
264 if ((s->bps + coeff_prec + av_log2(pred_order)) <= 32) {
265 for (i = pred_order; i < s->blocksize; i++)
266 {
267 sum = 0;
268 for (j = 0; j < pred_order; j++)
269 sum += coeffs[j] * decoded[i-j-1];
270 decoded[i] += sum >> qlevel;
271 }
272 } else {
273 for (i = pred_order; i < s->blocksize; i++)
274 {
275 wsum = 0;
276 for (j = 0; j < pred_order; j++)
277 wsum += (int64_t)coeffs[j] * (int64_t)decoded[i-j-1];
278 decoded[i] += wsum >> qlevel;
279 }
280 }
281
282 return 0;
283}
284
285static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded)
286{
287 int type, wasted = 0;
288 int i, tmp;
289
290 s->curr_bps = s->bps;
291 if(channel == 0){
292 if(s->decorrelation == RIGHT_SIDE)
293 s->curr_bps++;
294 }else{
295 if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
296 s->curr_bps++;
297 }
298
299 if (get_bits1(&s->gb))
300 {
301 //av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
302 return -1;
303 }
304 type = get_bits(&s->gb, 6);
305// wasted = get_bits1(&s->gb);
306
307// if (wasted)
308// {
309// while (!get_bits1(&s->gb))
310// wasted++;
311// if (wasted)
312// wasted++;
313// s->curr_bps -= wasted;
314// }
315#if 0
316 wasted= 16 - av_log2(show_bits(&s->gb, 17));
317 skip_bits(&s->gb, wasted+1);
318 s->curr_bps -= wasted;
319#else
320 if (get_bits1(&s->gb))
321 {
322 wasted = 1;
323 while (!get_bits1(&s->gb))
324 wasted++;
325 s->curr_bps -= wasted;
326 //fprintf(stderr,"%d wasted bits\n", wasted);
327 }
328#endif
329//FIXME use av_log2 for types
330 if (type == 0)
331 {
332 //fprintf(stderr,"coding type: constant\n");
333 tmp = get_sbits(&s->gb, s->curr_bps);
334 for (i = 0; i < s->blocksize; i++)
335 decoded[i] = tmp;
336 }
337 else if (type == 1)
338 {
339 //fprintf(stderr,"coding type: verbatim\n");
340 for (i = 0; i < s->blocksize; i++)
341 decoded[i] = get_sbits(&s->gb, s->curr_bps);
342 }
343 else if ((type >= 8) && (type <= 12))
344 {
345 //fprintf(stderr,"coding type: fixed\n");
346 if (decode_subframe_fixed(s, decoded, type & ~0x8) < 0)
347 return -1;
348 }
349 else if (type >= 32)
350 {
351 //fprintf(stderr,"coding type: lpc\n");
352 if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1) < 0)
353 return -1;
354 }
355 else
356 {
357 //fprintf(stderr,"Unknown coding type: %d\n",type);
358 return -1;
359 }
360
361 if (wasted)
362 {
363 int i;
364 for (i = 0; i < s->blocksize; i++)
365 decoded[i] <<= wasted;
366 }
367
368 return 0;
369}
370
371static int decode_frame(FLACContext *s,
372 int32_t* decoded0,
373 int32_t* decoded1,
374 void (*yield)(void))
375{
376 int blocksize_code, sample_rate_code, sample_size_code, assignment, crc8;
377 int decorrelation, bps, blocksize, samplerate;
378
379 blocksize_code = get_bits(&s->gb, 4);
380
381 sample_rate_code = get_bits(&s->gb, 4);
382
383 assignment = get_bits(&s->gb, 4); /* channel assignment */
384 if (assignment < 8 && s->channels == assignment+1)
385 decorrelation = INDEPENDENT;
386 else if (assignment >=8 && assignment < 11 && s->channels == 2)
387 decorrelation = LEFT_SIDE + assignment - 8;
388 else
389 {
390 return -1;
391 }
392
393 sample_size_code = get_bits(&s->gb, 3);
394 if(sample_size_code == 0)
395 bps= s->bps;
396 else if((sample_size_code != 3) && (sample_size_code != 7))
397 bps = sample_size_table[sample_size_code];
398 else
399 {
400 return -1;
401 }
402
403 if (get_bits1(&s->gb))
404 {
405 return -1;
406 }
407
408 /* Get the samplenumber of the first sample in this block */
409 s->samplenumber=get_utf8(&s->gb);
410
411 /* samplenumber actually contains the frame number for streams
412 with a constant block size - so we multiply by blocksize to
413 get the actual sample number */
414 if (s->min_blocksize == s->max_blocksize) {
415 s->samplenumber*=s->min_blocksize;
416 }
417
418#if 0
419 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
420 (s->min_blocksize != s->max_blocksize)){
421 }else{
422 }
423#endif
424
425 if (blocksize_code == 0)
426 blocksize = s->min_blocksize;
427 else if (blocksize_code == 6)
428 blocksize = get_bits(&s->gb, 8)+1;
429 else if (blocksize_code == 7)
430 blocksize = get_bits(&s->gb, 16)+1;
431 else
432 blocksize = blocksize_table[blocksize_code];
433
434 if(blocksize > s->max_blocksize){
435 return -1;
436 }
437
438 if (sample_rate_code == 0){
439 samplerate= s->samplerate;
440 }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
441 samplerate = sample_rate_table[sample_rate_code];
442 else if (sample_rate_code == 12)
443 samplerate = get_bits(&s->gb, 8) * 1000;
444 else if (sample_rate_code == 13)
445 samplerate = get_bits(&s->gb, 16);
446 else if (sample_rate_code == 14)
447 samplerate = get_bits(&s->gb, 16) * 10;
448 else{
449 return -1;
450 }
451
452 skip_bits(&s->gb, 8);
453 crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8);
454 if(crc8){
455 return -1;
456 }
457
458 s->blocksize = blocksize;
459 s->samplerate = samplerate;
460 s->bps = bps;
461 s->decorrelation= decorrelation;
462
463 yield();
464 /* subframes */
465 if (decode_subframe(s, 0, decoded0) < 0)
466 return -1;
467
468 yield();
469
470 if (s->channels==2) {
471 if (decode_subframe(s, 1, decoded1) < 0)
472 return -1;
473 }
474
475 yield();
476 align_get_bits(&s->gb);
477
478 /* frame footer */
479 skip_bits(&s->gb, 16); /* data crc */
480
481 return 0;
482}
483
484int flac_decode_frame(FLACContext *s,
485 int32_t* decoded0,
486 int32_t* decoded1,
487 uint8_t *buf, int buf_size,
488 void (*yield)(void))
489{
490 int tmp = 0, i, input_buf_size = 0;
491 int framesize;
492 int scale;
493
494 init_get_bits(&s->gb, buf, buf_size*8);
495
496 tmp = show_bits(&s->gb, 16);
497 if(tmp != 0xFFF8){
498 //fprintf(stderr,"FRAME HEADER not here\n");
499 while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8)
500 skip_bits(&s->gb, 8);
501 goto end; // we may not have enough bits left to decode a frame, so try next time
502 }
503 skip_bits(&s->gb, 16);
504
505 if ((framesize=decode_frame(s,decoded0,decoded1,yield)) < 0){
506 s->bitstream_size=0;
507 s->bitstream_index=0;
508 return -1;
509 }
510
511 yield();
512
513 scale=FLAC_OUTPUT_DEPTH-s->bps;
514 switch(s->decorrelation)
515 {
516 case INDEPENDENT:
517 if (s->channels==1) {;
518 for (i = 0; i < s->blocksize; i++)
519 {
520 decoded0[i] = decoded0[i] << scale;
521 }
522 } else {
523 for (i = 0; i < s->blocksize; i++)
524 {
525 decoded0[i] = decoded0[i] << scale;
526 decoded1[i] = decoded1[i] << scale;
527 }
528 }
529 break;
530 case LEFT_SIDE:
531 //assert(s->channels == 2);
532 for (i = 0; i < s->blocksize; i++)
533 {
534 decoded1[i] = (decoded0[i] - decoded1[i]) << scale;
535 decoded0[i] = decoded0[i] << scale;
536 }
537 break;
538 case RIGHT_SIDE:
539 //assert(s->channels == 2);
540 for (i = 0; i < s->blocksize; i++)
541 {
542 decoded0[i] = (decoded0[i] + decoded1[i]) << scale;
543 decoded1[i] = decoded1[i] << scale;
544 }
545 break;
546 case MID_SIDE:
547 //assert(s->channels == 2);
548 for (i = 0; i < s->blocksize; i++)
549 {
550 int mid, side;
551 mid = decoded0[i];
552 side = decoded1[i];
553
554#if 1 //needs to be checked but IMHO it should be binary identical
555 mid -= side>>1;
556 decoded0[i] = (mid + side) << scale;
557 decoded1[i] = mid << scale;
558#else
559
560 mid <<= 1;
561 if (side & 1)
562 mid++;
563 decoded0[i] = ((mid + side) >> 1) << scale;
564 decoded1[i] = ((mid - side) >> 1) << scale;
565#endif
566 }
567 break;
568 }
569
570end:
571 i= (get_bits_count(&s->gb)+7)/8;;
572 if(i > buf_size){
573 s->bitstream_size=0;
574 s->bitstream_index=0;
575 return -1;
576 }
577
578 if(s->bitstream_size){
579 s->bitstream_index += i;
580 s->bitstream_size -= i;
581 return input_buf_size;
582 }else
583 return i;
584}
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 @@
1#ifndef _FLAC_DECODER_H
2#define _FLAC_DECODER_H
3
4#include "bitstream.h"
5
6#define MAX_CHANNELS 2 /* Maximum supported channels */
7#define MAX_BLOCKSIZE 4608 /* Maxsize in samples of one uncompressed frame */
8#define MAX_FRAMESIZE 32768 /* Maxsize in bytes of one compressed frame */
9
10#define FLAC_OUTPUT_DEPTH 28 /* Provide samples left-shifted to 28-bits */
11
12enum decorrelation_type {
13 INDEPENDENT,
14 LEFT_SIDE,
15 RIGHT_SIDE,
16 MID_SIDE,
17};
18
19typedef struct FLACContext {
20 GetBitContext gb;
21
22 int min_blocksize, max_blocksize;
23 int min_framesize, max_framesize;
24 int samplerate, channels;
25 int blocksize/*, last_blocksize*/;
26 int bps, curr_bps;
27 unsigned long samplenumber;
28 unsigned long totalsamples;
29 enum decorrelation_type decorrelation;
30
31 int filesize;
32 int length;
33 int bitrate;
34 int metadatalength;
35
36 int bitstream_size;
37 int bitstream_index;
38} FLACContext;
39
40int flac_decode_frame(FLACContext *s,
41 int32_t* decoded0,
42 int32_t* decoded1,
43 uint8_t *buf, int buf_size,
44 void (*yield)(void));
45
46#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 @@
1/*
2 * exp golomb vlc stuff
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Alex Beregszaszi
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22
23/* From libavutil/common.h */
24extern const uint8_t ff_log2_tab[256];
25static inline int av_log2(unsigned int v)
26{
27 int n;
28
29 n = 0;
30 if (v & 0xffff0000) {
31 v >>= 16;
32 n += 16;
33 }
34 if (v & 0xff00) {
35 v >>= 8;
36 n += 8;
37 }
38 n += ff_log2_tab[v];
39
40 return n;
41}
42
43/**
44 * @file golomb.h
45 * @brief
46 * exp golomb vlc stuff
47 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
48 */
49
50
51/**
52 * read unsigned golomb rice code (jpegls).
53 */
54static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
55 unsigned int buf;
56 int log;
57
58 OPEN_READER(re, gb);
59 UPDATE_CACHE(re, gb);
60 buf=GET_CACHE(re, gb);
61
62 log= av_log2(buf);
63
64 if(log > 31-11){
65 buf >>= log - k;
66 buf += (30-log)<<k;
67 LAST_SKIP_BITS(re, gb, 32 + k - log);
68 CLOSE_READER(re, gb);
69
70 return buf;
71 }else{
72 int i;
73 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
74 LAST_SKIP_BITS(re, gb, 1);
75 UPDATE_CACHE(re, gb);
76 }
77 SKIP_BITS(re, gb, 1);
78
79 if(i < limit - 1){
80 if(k){
81 buf = SHOW_UBITS(re, gb, k);
82 LAST_SKIP_BITS(re, gb, k);
83 }else{
84 buf=0;
85 }
86
87 CLOSE_READER(re, gb);
88 return buf + (i<<k);
89 }else if(i == limit - 1){
90 buf = SHOW_UBITS(re, gb, esc_len);
91 LAST_SKIP_BITS(re, gb, esc_len);
92 CLOSE_READER(re, gb);
93
94 return buf + 1;
95 }else
96 return -1;
97 }
98}
99
100/**
101 * read signed golomb rice code (flac).
102 */
103static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
104 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
105 return (v>>1) ^ -(v&1);
106}
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 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Dave Chapman
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20/* A test program for the Rockbox version of the ffmpeg FLAC decoder.
21
22 Compile using Makefile.test - run it as "./test file.flac" to decode the
23 FLAC file to the file "test.wav" in the current directory
24
25 This test program should support 16-bit and 24-bit mono and stereo files.
26
27 The resulting "test.wav" should have the same md5sum as a WAV file created
28 by the official FLAC decoder (it produces the same 44-byte canonical WAV
29 header.
30*/
31
32#include <stdio.h>
33#include <string.h>
34#include <inttypes.h>
35#include <stdbool.h>
36#include <fcntl.h>
37#include <unistd.h>
38#include <sys/types.h>
39#include <sys/stat.h>
40
41#include "decoder.h"
42
43static unsigned char wav_header[44]={
44 'R','I','F','F',// 0 - ChunkID
45 0,0,0,0, // 4 - ChunkSize (filesize-8)
46 'W','A','V','E',// 8 - Format
47 'f','m','t',' ',// 12 - SubChunkID
48 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
49 1,0, // 20 - AudioFormat (1=Uncompressed)
50 2,0, // 22 - NumChannels
51 0,0,0,0, // 24 - SampleRate in Hz
52 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
53 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
54 16,0, // 34 - BitsPerSample
55 'd','a','t','a',// 36 - Subchunk2ID
56 0,0,0,0 // 40 - Subchunk2Size
57};
58
59int open_wav(char* filename) {
60 int fd;
61
62 fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR);
63 if (fd >= 0) {
64 write(fd,wav_header,sizeof(wav_header));
65 }
66 return(fd);
67}
68
69void close_wav(int fd, FLACContext* fc) {
70 int x;
71 int filesize;
72 int bytespersample;
73
74 bytespersample=fc->bps/8;
75
76 filesize=fc->totalsamples*bytespersample*fc->channels+44;
77
78 // ChunkSize
79 x=filesize-8;
80 wav_header[4]=(x&0xff);
81 wav_header[5]=(x&0xff00)>>8;
82 wav_header[6]=(x&0xff0000)>>16;
83 wav_header[7]=(x&0xff000000)>>24;
84
85 // Number of channels
86 wav_header[22]=fc->channels;
87
88 // Samplerate
89 wav_header[24]=fc->samplerate&0xff;
90 wav_header[25]=(fc->samplerate&0xff00)>>8;
91 wav_header[26]=(fc->samplerate&0xff0000)>>16;
92 wav_header[27]=(fc->samplerate&0xff000000)>>24;
93
94 // ByteRate
95 x=fc->samplerate*(fc->bps/8)*fc->channels;
96 wav_header[28]=(x&0xff);
97 wav_header[29]=(x&0xff00)>>8;
98 wav_header[30]=(x&0xff0000)>>16;
99 wav_header[31]=(x&0xff000000)>>24;
100
101 // BlockAlign
102 wav_header[32]=(fc->bps/8)*fc->channels;
103
104 // Bits per sample
105 wav_header[34]=fc->bps;
106
107 // Subchunk2Size
108 x=filesize-44;
109 wav_header[40]=(x&0xff);
110 wav_header[41]=(x&0xff00)>>8;
111 wav_header[42]=(x&0xff0000)>>16;
112 wav_header[43]=(x&0xff000000)>>24;
113
114 lseek(fd,0,SEEK_SET);
115 write(fd,wav_header,sizeof(wav_header));
116 close(fd);
117}
118
119static void dump_headers(FLACContext *s)
120{
121 fprintf(stderr," Blocksize: %d .. %d\n", s->min_blocksize,
122 s->max_blocksize);
123 fprintf(stderr," Framesize: %d .. %d\n", s->min_framesize,
124 s->max_framesize);
125 fprintf(stderr," Samplerate: %d\n", s->samplerate);
126 fprintf(stderr," Channels: %d\n", s->channels);
127 fprintf(stderr," Bits per sample: %d\n", s->bps);
128 fprintf(stderr," Metadata length: %d\n", s->metadatalength);
129 fprintf(stderr," Total Samples: %lu\n",s->totalsamples);
130 fprintf(stderr," Duration: %d ms\n",s->length);
131 fprintf(stderr," Bitrate: %d kbps\n",s->bitrate);
132}
133
134static bool flac_init(int fd, FLACContext* fc)
135{
136 unsigned char buf[255];
137 struct stat statbuf;
138 bool found_streaminfo=false;
139 int endofmetadata=0;
140 int blocklength;
141
142 if (lseek(fd, 0, SEEK_SET) < 0)
143 {
144 return false;
145 }
146
147 if (read(fd, buf, 4) < 4)
148 {
149 return false;
150 }
151
152 if (memcmp(buf,"fLaC",4) != 0)
153 {
154 return false;
155 }
156 fc->metadatalength = 4;
157
158 while (!endofmetadata) {
159 if (read(fd, buf, 4) < 4)
160 {
161 return false;
162 }
163
164 endofmetadata=(buf[0]&0x80);
165 blocklength = (buf[1] << 16) | (buf[2] << 8) | buf[3];
166 fc->metadatalength+=blocklength+4;
167
168 if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */
169 {
170 /* FIXME: Don't trust the value of blocklength */
171 if (read(fd, buf, blocklength) < 0)
172 {
173 return false;
174 }
175
176 fstat(fd,&statbuf);
177 fc->filesize = statbuf.st_size;
178 fc->min_blocksize = (buf[0] << 8) | buf[1];
179 fc->max_blocksize = (buf[2] << 8) | buf[3];
180 fc->min_framesize = (buf[4] << 16) | (buf[5] << 8) | buf[6];
181 fc->max_framesize = (buf[7] << 16) | (buf[8] << 8) | buf[9];
182 fc->samplerate = (buf[10] << 12) | (buf[11] << 4)
183 | ((buf[12] & 0xf0) >> 4);
184 fc->channels = ((buf[12]&0x0e)>>1) + 1;
185 fc->bps = (((buf[12]&0x01) << 4) | ((buf[13]&0xf0)>>4) ) + 1;
186
187 /* totalsamples is a 36-bit field, but we assume <= 32 bits are
188 used */
189 fc->totalsamples = (buf[14] << 24) | (buf[15] << 16)
190 | (buf[16] << 8) | buf[17];
191
192 /* Calculate track length (in ms) and estimate the bitrate
193 (in kbit/s) */
194 fc->length = (fc->totalsamples / fc->samplerate) * 1000;
195
196 found_streaminfo=true;
197 } else if ((buf[0] & 0x7f) == 3) { /* 3 is the SEEKTABLE block */
198 fprintf(stderr,"Seektable length = %d bytes\n",blocklength);
199 if (lseek(fd, blocklength, SEEK_CUR) < 0) {
200 return false;
201 }
202 } else {
203 /* Skip to next metadata block */
204 if (lseek(fd, blocklength, SEEK_CUR) < 0)
205 {
206 return false;
207 }
208 }
209 }
210
211 if (found_streaminfo) {
212 fc->bitrate = ((fc->filesize-fc->metadatalength) * 8) / fc->length;
213 return true;
214 } else {
215 return false;
216 }
217}
218
219/* Dummy function needed to pass to flac_decode_frame() */
220void yield() {
221}
222
223int main(int argc, char* argv[]) {
224 FLACContext fc;
225 int fd,fdout;
226 int n;
227 int i;
228 int bytesleft;
229 int consumed;
230 char buf[MAX_FRAMESIZE]; /* The input buffer */
231 /* The output buffers containing the decoded samples (channels 0 and 1) */
232 int32_t decoded0[MAX_BLOCKSIZE];
233 int32_t decoded1[MAX_BLOCKSIZE];
234
235 /* For testing */
236 int8_t wavbuf[MAX_CHANNELS*MAX_BLOCKSIZE*3];
237 int8_t* p;
238 int scale;
239
240 fd=open(argv[1],O_RDONLY);
241
242 if (fd < 0) {
243 fprintf(stderr,"Can not parse %s\n",argv[1]);
244 return(1);
245 }
246
247 /* Read the metadata and position the file pointer at the start of the
248 first audio frame */
249 flac_init(fd,&fc);
250
251 dump_headers(&fc);
252
253 fdout=open_wav("test.wav");
254 bytesleft=read(fd,buf,sizeof(buf));
255 while (bytesleft) {
256 if(flac_decode_frame(&fc,decoded0,decoded1,buf,bytesleft,yield) < 0) {
257 fprintf(stderr,"DECODE ERROR, ABORTING\n");
258 break;
259 }
260 consumed=fc.gb.index/8;
261
262 scale=FLAC_OUTPUT_DEPTH-fc.bps;
263 p=wavbuf;
264 for (i=0;i<fc.blocksize;i++) {
265 /* Left sample */
266 decoded0[i]=decoded0[i]>>scale;
267 *(p++)=decoded0[i]&0xff;
268 *(p++)=(decoded0[i]&0xff00)>>8;
269 if (fc.bps==24) *(p++)=(decoded0[i]&0xff0000)>>16;
270
271 if (fc.channels==2) {
272 /* Right sample */
273 decoded1[i]=decoded1[i]>>scale;
274 *(p++)=decoded1[i]&0xff;
275 *(p++)=(decoded1[i]&0xff00)>>8;
276 if (fc.bps==24) *(p++)=(decoded1[i]&0xff0000)>>16;
277 }
278 }
279 write(fdout,wavbuf,fc.blocksize*fc.channels*(fc.bps/8));
280
281 memmove(buf,&buf[consumed],bytesleft-consumed);
282 bytesleft-=consumed;
283
284 n=read(fd,&buf[bytesleft],sizeof(buf)-bytesleft);
285 if (n > 0) {
286 bytesleft+=n;
287 }
288 }
289 close_wav(fdout,&fc);
290 close(fd);
291 return(0);
292}
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
17endif 17endif
18 18
19ifdef SOFTWARECODECS 19ifdef SOFTWARECODECS
20 CODECLIBS = -lmad -la52 -lFLAC -lTremor -lwavpack -lmusepack 20 CODECLIBS = -lmad -la52 -lffmpegFLAC -lTremor -lwavpack -lmusepack
21endif 21endif
22 22
23LDS := plugin.lds 23LDS := 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'
523 archosrom="" 523 archosrom=""
524 flash="" 524 flash=""
525 plugins="yes" 525 plugins="yes"
526 codecs="libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac" 526 codecs="libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac"
527 ;; 527 ;;
528 528
529 10) 529 10)
@@ -537,7 +537,7 @@ appsdir='\$(ROOTDIR)/apps'
537 archosrom="" 537 archosrom=""
538 flash="" 538 flash=""
539 plugins="yes" 539 plugins="yes"
540 codecs="libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac" 540 codecs="libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac"
541 ;; 541 ;;
542 542
543 11) 543 11)
@@ -551,7 +551,7 @@ appsdir='\$(ROOTDIR)/apps'
551 archosrom="" 551 archosrom=""
552 flash="" 552 flash=""
553 plugins="yes" 553 plugins="yes"
554 codecs="libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac" 554 codecs="libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac"
555 ;; 555 ;;
556 556
557 12) 557 12)
@@ -565,7 +565,7 @@ appsdir='\$(ROOTDIR)/apps'
565 archosrom="" 565 archosrom=""
566 flash="" 566 flash=""
567 plugins="yes" 567 plugins="yes"
568 codecs="libmad liba52 libFLAC libTremor libwavpack dumb libmusepack libalac" 568 codecs="libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac"
569 ;; 569 ;;
570 570
571 *) 571 *)