diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/libffmpegFLAC/decoder.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/libffmpegFLAC/decoder.c')
-rw-r--r-- | lib/rbcodec/codecs/libffmpegFLAC/decoder.c | 627 |
1 files changed, 627 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libffmpegFLAC/decoder.c b/lib/rbcodec/codecs/libffmpegFLAC/decoder.c new file mode 100644 index 0000000000..2e92c4b90d --- /dev/null +++ b/lib/rbcodec/codecs/libffmpegFLAC/decoder.c | |||
@@ -0,0 +1,627 @@ | |||
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 "codeclib.h" | ||
38 | #endif | ||
39 | |||
40 | #include "bitstream.h" | ||
41 | #include "golomb.h" | ||
42 | |||
43 | #include "decoder.h" | ||
44 | |||
45 | #if defined(CPU_COLDFIRE) | ||
46 | #include "coldfire.h" | ||
47 | #elif defined(CPU_ARM) | ||
48 | #include "arm.h" | ||
49 | #endif | ||
50 | |||
51 | static const int sample_rate_table[] ICONST_ATTR = | ||
52 | { 0, 88200, 176400, 192000, | ||
53 | 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000, | ||
54 | 0, 0, 0, 0 }; | ||
55 | |||
56 | static const int sample_size_table[] ICONST_ATTR = | ||
57 | { 0, 8, 12, 0, 16, 20, 24, 0 }; | ||
58 | |||
59 | static const int blocksize_table[] ICONST_ATTR = { | ||
60 | 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0, | ||
61 | 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7 | ||
62 | }; | ||
63 | |||
64 | static const uint8_t table_crc8[256] ICONST_ATTR = { | ||
65 | 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, | ||
66 | 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d, | ||
67 | 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, | ||
68 | 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, | ||
69 | 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5, | ||
70 | 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd, | ||
71 | 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, | ||
72 | 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd, | ||
73 | 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2, | ||
74 | 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, | ||
75 | 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2, | ||
76 | 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a, | ||
77 | 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, | ||
78 | 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a, | ||
79 | 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42, | ||
80 | 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a, | ||
81 | 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c, | ||
82 | 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4, | ||
83 | 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, | ||
84 | 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4, | ||
85 | 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c, | ||
86 | 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, | ||
87 | 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c, | ||
88 | 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34, | ||
89 | 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, | ||
90 | 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63, | ||
91 | 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, | ||
92 | 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, | ||
93 | 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, | ||
94 | 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83, | ||
95 | 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, | ||
96 | 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3 | ||
97 | }; | ||
98 | |||
99 | static int64_t get_utf8(GetBitContext *gb) ICODE_ATTR_FLAC; | ||
100 | static int64_t get_utf8(GetBitContext *gb) | ||
101 | { | ||
102 | uint64_t val; | ||
103 | int ones=0, bytes; | ||
104 | |||
105 | while(get_bits1(gb)) | ||
106 | ones++; | ||
107 | |||
108 | if (ones==0) bytes=0; | ||
109 | else if(ones==1) return -1; | ||
110 | else bytes= ones - 1; | ||
111 | |||
112 | val= get_bits(gb, 7-ones); | ||
113 | while(bytes--){ | ||
114 | const int tmp = get_bits(gb, 8); | ||
115 | |||
116 | if((tmp>>6) != 2) | ||
117 | return -2; | ||
118 | val<<=6; | ||
119 | val|= tmp&0x3F; | ||
120 | } | ||
121 | return val; | ||
122 | } | ||
123 | |||
124 | static int get_crc8(const uint8_t *buf, int count) ICODE_ATTR_FLAC; | ||
125 | static int get_crc8(const uint8_t *buf, int count) | ||
126 | { | ||
127 | int crc=0; | ||
128 | int i; | ||
129 | |||
130 | for(i=0; i<count; i++){ | ||
131 | crc = table_crc8[crc ^ buf[i]]; | ||
132 | } | ||
133 | |||
134 | return crc; | ||
135 | } | ||
136 | |||
137 | static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC; | ||
138 | static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order) | ||
139 | { | ||
140 | int i, tmp, partition, method_type, rice_order; | ||
141 | int sample = 0, samples; | ||
142 | |||
143 | method_type = get_bits(&s->gb, 2); | ||
144 | if (method_type > 1){ | ||
145 | //fprintf(stderr,"illegal residual coding method %d\n", method_type); | ||
146 | return -3; | ||
147 | } | ||
148 | |||
149 | rice_order = get_bits(&s->gb, 4); | ||
150 | |||
151 | samples= s->blocksize >> rice_order; | ||
152 | |||
153 | sample= | ||
154 | i= pred_order; | ||
155 | for (partition = 0; partition < (1 << rice_order); partition++) | ||
156 | { | ||
157 | tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5); | ||
158 | if (tmp == (method_type == 0 ? 15 : 31)) | ||
159 | { | ||
160 | //fprintf(stderr,"fixed len partition\n"); | ||
161 | tmp = get_bits(&s->gb, 5); | ||
162 | for (; i < samples; i++, sample++) | ||
163 | decoded[sample] = get_sbits(&s->gb, tmp); | ||
164 | } | ||
165 | else | ||
166 | { | ||
167 | for (; i < samples; i++, sample++){ | ||
168 | decoded[sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0); | ||
169 | } | ||
170 | } | ||
171 | i= 0; | ||
172 | } | ||
173 | |||
174 | return 0; | ||
175 | } | ||
176 | |||
177 | static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC; | ||
178 | static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order) | ||
179 | { | ||
180 | const int blocksize = s->blocksize; | ||
181 | int a, b, c, d, i; | ||
182 | |||
183 | /* warm up samples */ | ||
184 | for (i = 0; i < pred_order; i++) | ||
185 | { | ||
186 | decoded[i] = get_sbits(&s->gb, s->curr_bps); | ||
187 | } | ||
188 | |||
189 | if (decode_residuals(s, decoded, pred_order) < 0) | ||
190 | return -4; | ||
191 | |||
192 | a = decoded[pred_order-1]; | ||
193 | b = a - decoded[pred_order-2]; | ||
194 | c = b - decoded[pred_order-2] + decoded[pred_order-3]; | ||
195 | d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4]; | ||
196 | |||
197 | switch(pred_order) | ||
198 | { | ||
199 | case 0: | ||
200 | break; | ||
201 | case 1: | ||
202 | for (i = pred_order; i < blocksize; i++) | ||
203 | decoded[i] = a += decoded[i]; | ||
204 | break; | ||
205 | case 2: | ||
206 | for (i = pred_order; i < blocksize; i++) | ||
207 | decoded[i] = a += b += decoded[i]; | ||
208 | break; | ||
209 | case 3: | ||
210 | for (i = pred_order; i < blocksize; i++) | ||
211 | decoded[i] = a += b += c += decoded[i]; | ||
212 | break; | ||
213 | case 4: | ||
214 | for (i = pred_order; i < blocksize; i++) | ||
215 | decoded[i] = a += b += c += d += decoded[i]; | ||
216 | break; | ||
217 | default: | ||
218 | return -5; | ||
219 | } | ||
220 | |||
221 | return 0; | ||
222 | } | ||
223 | |||
224 | static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC; | ||
225 | static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order) | ||
226 | { | ||
227 | int sum, i, j; | ||
228 | int64_t wsum; | ||
229 | int coeff_prec, qlevel; | ||
230 | int coeffs[pred_order]; | ||
231 | |||
232 | /* warm up samples */ | ||
233 | for (i = 0; i < pred_order; i++) | ||
234 | { | ||
235 | decoded[i] = get_sbits(&s->gb, s->curr_bps); | ||
236 | } | ||
237 | |||
238 | coeff_prec = get_bits(&s->gb, 4) + 1; | ||
239 | if (coeff_prec == 16) | ||
240 | { | ||
241 | //fprintf(stderr,"invalid coeff precision\n"); | ||
242 | return -6; | ||
243 | } | ||
244 | qlevel = get_sbits(&s->gb, 5); | ||
245 | if (qlevel < 0) | ||
246 | { | ||
247 | //fprintf(stderr,"qlevel %d not supported, maybe buggy stream\n", qlevel); | ||
248 | return -7; | ||
249 | } | ||
250 | |||
251 | for (i = 0; i < pred_order; i++) | ||
252 | { | ||
253 | coeffs[i] = get_sbits(&s->gb, coeff_prec); | ||
254 | } | ||
255 | |||
256 | if (decode_residuals(s, decoded, pred_order) < 0) | ||
257 | return -8; | ||
258 | |||
259 | if ((s->bps + coeff_prec + av_log2(pred_order)) <= 32) { | ||
260 | #if defined(CPU_COLDFIRE) | ||
261 | (void)sum; | ||
262 | lpc_decode_emac(s->blocksize - pred_order, qlevel, pred_order, | ||
263 | decoded + pred_order, coeffs); | ||
264 | #elif defined(CPU_ARM) | ||
265 | (void)sum; | ||
266 | lpc_decode_arm(s->blocksize - pred_order, qlevel, pred_order, | ||
267 | decoded + pred_order, coeffs); | ||
268 | #else | ||
269 | for (i = pred_order; i < s->blocksize; i++) | ||
270 | { | ||
271 | sum = 0; | ||
272 | for (j = 0; j < pred_order; j++) | ||
273 | sum += coeffs[j] * decoded[i-j-1]; | ||
274 | decoded[i] += sum >> qlevel; | ||
275 | } | ||
276 | #endif | ||
277 | } else { | ||
278 | #if defined(CPU_COLDFIRE) | ||
279 | (void)wsum; | ||
280 | (void)j; | ||
281 | lpc_decode_emac_wide(s->blocksize - pred_order, qlevel, pred_order, | ||
282 | decoded + pred_order, coeffs); | ||
283 | #else | ||
284 | for (i = pred_order; i < s->blocksize; i++) | ||
285 | { | ||
286 | wsum = 0; | ||
287 | for (j = 0; j < pred_order; j++) | ||
288 | wsum += (int64_t)coeffs[j] * (int64_t)decoded[i-j-1]; | ||
289 | decoded[i] += wsum >> qlevel; | ||
290 | } | ||
291 | #endif | ||
292 | } | ||
293 | |||
294 | return 0; | ||
295 | } | ||
296 | |||
297 | static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded) | ||
298 | { | ||
299 | int type, wasted = 0; | ||
300 | int i, tmp; | ||
301 | |||
302 | s->curr_bps = s->bps; | ||
303 | if(channel == 0){ | ||
304 | if(s->decorrelation == RIGHT_SIDE) | ||
305 | s->curr_bps++; | ||
306 | }else{ | ||
307 | if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE) | ||
308 | s->curr_bps++; | ||
309 | } | ||
310 | |||
311 | if (get_bits1(&s->gb)) | ||
312 | { | ||
313 | //fprintf(stderr,"invalid subframe padding\n"); | ||
314 | return -9; | ||
315 | } | ||
316 | type = get_bits(&s->gb, 6); | ||
317 | // wasted = get_bits1(&s->gb); | ||
318 | |||
319 | // if (wasted) | ||
320 | // { | ||
321 | // while (!get_bits1(&s->gb)) | ||
322 | // wasted++; | ||
323 | // if (wasted) | ||
324 | // wasted++; | ||
325 | // s->curr_bps -= wasted; | ||
326 | // } | ||
327 | #if 0 | ||
328 | wasted= 16 - av_log2(show_bits(&s->gb, 17)); | ||
329 | skip_bits(&s->gb, wasted+1); | ||
330 | s->curr_bps -= wasted; | ||
331 | #else | ||
332 | if (get_bits1(&s->gb)) | ||
333 | { | ||
334 | wasted = 1; | ||
335 | while (!get_bits1(&s->gb)) | ||
336 | wasted++; | ||
337 | s->curr_bps -= wasted; | ||
338 | //fprintf(stderr,"%d wasted bits\n", wasted); | ||
339 | } | ||
340 | #endif | ||
341 | //FIXME use av_log2 for types | ||
342 | if (type == 0) | ||
343 | { | ||
344 | //fprintf(stderr,"coding type: constant\n"); | ||
345 | tmp = get_sbits(&s->gb, s->curr_bps); | ||
346 | for (i = 0; i < s->blocksize; i++) | ||
347 | decoded[i] = tmp; | ||
348 | } | ||
349 | else if (type == 1) | ||
350 | { | ||
351 | //fprintf(stderr,"coding type: verbatim\n"); | ||
352 | for (i = 0; i < s->blocksize; i++) | ||
353 | decoded[i] = get_sbits(&s->gb, s->curr_bps); | ||
354 | } | ||
355 | else if ((type >= 8) && (type <= 12)) | ||
356 | { | ||
357 | //fprintf(stderr,"coding type: fixed\n"); | ||
358 | if (decode_subframe_fixed(s, decoded, type & ~0x8) < 0) | ||
359 | return -10; | ||
360 | } | ||
361 | else if (type >= 32) | ||
362 | { | ||
363 | //fprintf(stderr,"coding type: lpc\n"); | ||
364 | if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1) < 0) | ||
365 | return -11; | ||
366 | } | ||
367 | else | ||
368 | { | ||
369 | //fprintf(stderr,"Unknown coding type: %d\n",type); | ||
370 | return -12; | ||
371 | } | ||
372 | |||
373 | if (wasted) | ||
374 | { | ||
375 | int i; | ||
376 | for (i = 0; i < s->blocksize; i++) | ||
377 | decoded[i] <<= wasted; | ||
378 | } | ||
379 | |||
380 | return 0; | ||
381 | } | ||
382 | |||
383 | static int decode_frame(FLACContext *s, | ||
384 | void (*yield)(void)) ICODE_ATTR_FLAC; | ||
385 | static int decode_frame(FLACContext *s, | ||
386 | void (*yield)(void)) | ||
387 | { | ||
388 | int blocksize_code, sample_rate_code, sample_size_code, assignment, crc8; | ||
389 | int decorrelation, bps, blocksize, samplerate; | ||
390 | int res, ch; | ||
391 | |||
392 | blocksize_code = get_bits(&s->gb, 4); | ||
393 | |||
394 | sample_rate_code = get_bits(&s->gb, 4); | ||
395 | |||
396 | assignment = get_bits(&s->gb, 4); /* channel assignment */ | ||
397 | if (assignment < 8 && s->channels == assignment+1) | ||
398 | decorrelation = INDEPENDENT; | ||
399 | else if (assignment >=8 && assignment < 11 && s->channels == 2) | ||
400 | decorrelation = LEFT_SIDE + assignment - 8; | ||
401 | else | ||
402 | { | ||
403 | return -13; | ||
404 | } | ||
405 | |||
406 | sample_size_code = get_bits(&s->gb, 3); | ||
407 | if(sample_size_code == 0) | ||
408 | bps= s->bps; | ||
409 | else if((sample_size_code != 3) && (sample_size_code != 7)) | ||
410 | bps = sample_size_table[sample_size_code]; | ||
411 | else | ||
412 | { | ||
413 | return -14; | ||
414 | } | ||
415 | |||
416 | if (get_bits1(&s->gb)) | ||
417 | { | ||
418 | return -15; | ||
419 | } | ||
420 | |||
421 | /* Get the samplenumber of the first sample in this block */ | ||
422 | s->samplenumber=get_utf8(&s->gb); | ||
423 | |||
424 | /* samplenumber actually contains the frame number for streams | ||
425 | with a constant block size - so we multiply by blocksize to | ||
426 | get the actual sample number */ | ||
427 | if (s->min_blocksize == s->max_blocksize) { | ||
428 | s->samplenumber*=s->min_blocksize; | ||
429 | } | ||
430 | |||
431 | #if 0 | ||
432 | if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/ | ||
433 | (s->min_blocksize != s->max_blocksize)){ | ||
434 | }else{ | ||
435 | } | ||
436 | #endif | ||
437 | |||
438 | if (blocksize_code == 0) | ||
439 | blocksize = s->min_blocksize; | ||
440 | else if (blocksize_code == 6) | ||
441 | blocksize = get_bits(&s->gb, 8)+1; | ||
442 | else if (blocksize_code == 7) | ||
443 | blocksize = get_bits(&s->gb, 16)+1; | ||
444 | else | ||
445 | blocksize = blocksize_table[blocksize_code]; | ||
446 | |||
447 | if(blocksize > s->max_blocksize){ | ||
448 | return -16; | ||
449 | } | ||
450 | |||
451 | if (sample_rate_code == 0){ | ||
452 | samplerate= s->samplerate; | ||
453 | }else if ((sample_rate_code < 12)) | ||
454 | samplerate = sample_rate_table[sample_rate_code]; | ||
455 | else if (sample_rate_code == 12) | ||
456 | samplerate = get_bits(&s->gb, 8) * 1000; | ||
457 | else if (sample_rate_code == 13) | ||
458 | samplerate = get_bits(&s->gb, 16); | ||
459 | else if (sample_rate_code == 14) | ||
460 | samplerate = get_bits(&s->gb, 16) * 10; | ||
461 | else{ | ||
462 | return -17; | ||
463 | } | ||
464 | |||
465 | skip_bits(&s->gb, 8); | ||
466 | crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8); | ||
467 | if(crc8){ | ||
468 | return -18; | ||
469 | } | ||
470 | |||
471 | s->blocksize = blocksize; | ||
472 | s->samplerate = samplerate; | ||
473 | s->bps = bps; | ||
474 | s->decorrelation= decorrelation; | ||
475 | |||
476 | for (ch=0; ch<s->channels; ++ch) { | ||
477 | yield(); | ||
478 | if ((res=decode_subframe(s, ch, s->decoded[ch])) < 0) | ||
479 | return res-100; | ||
480 | } | ||
481 | |||
482 | yield(); | ||
483 | align_get_bits(&s->gb); | ||
484 | |||
485 | /* frame footer */ | ||
486 | skip_bits(&s->gb, 16); /* data crc */ | ||
487 | |||
488 | return 0; | ||
489 | } | ||
490 | |||
491 | static int flac_downmix(FLACContext *s) ICODE_ATTR_FLAC; | ||
492 | static int flac_downmix(FLACContext *s) | ||
493 | { | ||
494 | int32_t *FL, *FR, *FC, *SB, *RL, *RR; | ||
495 | int32_t *outL = s->decoded[0]; | ||
496 | int32_t *outR = s->decoded[1]; | ||
497 | int i, scale=FLAC_OUTPUT_DEPTH-s->bps; | ||
498 | |||
499 | switch(s->channels) | ||
500 | { | ||
501 | case 3: /* 3.0 channel order: FL FR FC */ | ||
502 | FL = s->decoded[0]; | ||
503 | FR = s->decoded[1]; | ||
504 | FC = s->decoded[2]; | ||
505 | /* LF = 0.66 LF + 0.33 FC | ||
506 | LR = 0.66 LR + 0.33 FC */ | ||
507 | for (i=0; i<s->blocksize; ++i) { | ||
508 | int32_t a = *(FL)*2 + *(FC); | ||
509 | int32_t b = *(FR)*2 + *(FC); | ||
510 | *outL++ = ((a + (a<<2))>>4) << scale; /* 1/3 ~= 5>>4 */ | ||
511 | *outR++ = ((b + (b<<2))>>4) << scale; /* 1/3 ~= 5>>4 */ | ||
512 | FL++; FR++; FC++; | ||
513 | } | ||
514 | break; | ||
515 | case 4: /* 4.0 channel order: FL FR RL RR */ | ||
516 | FL = s->decoded[0]; | ||
517 | FR = s->decoded[1]; | ||
518 | RL = s->decoded[2]; | ||
519 | RR = s->decoded[3]; | ||
520 | /* LF = 0.50 LF + 0.50 RL + 0.00 RR | ||
521 | LR = 0.50 LR + 0.00 RL + 0.50 RR */ | ||
522 | for (i=0; i<s->blocksize; ++i) { | ||
523 | int32_t a = *(FL) + *(RL); | ||
524 | int32_t b = *(FR) + *(RR); | ||
525 | *outL++ = (a>>1) << scale; | ||
526 | *outR++ = (b>>1) << scale; | ||
527 | FL++; FR++; RL++; RR++; | ||
528 | } | ||
529 | break; | ||
530 | case 5: /* 5.0 channel order: FL FR FC RL RR */ | ||
531 | FL = s->decoded[0]; | ||
532 | FR = s->decoded[1]; | ||
533 | FC = s->decoded[2]; | ||
534 | RL = s->decoded[3]; | ||
535 | RR = s->decoded[4]; | ||
536 | /* LF = 0.40 LF + 0.20 FC + 0.40 RL + 0.00 RR | ||
537 | LR = 0.40 LR + 0.20 FC + 0.00 RL + 0.40 RR */ | ||
538 | for (i=0; i<s->blocksize; ++i) { | ||
539 | int32_t a = *(FL)*2 + *(FC) + *(RL)*2; | ||
540 | int32_t b = *(FR)*2 + *(FC) + *(RR)*2; | ||
541 | *outL++ = ((a + (a<<1))>>4) << scale; /* 3>>4 ~= 1/5 */ | ||
542 | *outR++ = ((b + (b<<1))>>4) << scale; /* 3>>4 ~= 1/5 */ | ||
543 | FL++; FR++; FC++; RL++; RR++; | ||
544 | } | ||
545 | break; | ||
546 | case 6: /* 5.1 channel order: FL FR FC SUB RL RR */ | ||
547 | FL = s->decoded[0]; | ||
548 | FR = s->decoded[1]; | ||
549 | FC = s->decoded[2]; | ||
550 | SB = s->decoded[3]; | ||
551 | RL = s->decoded[4]; | ||
552 | RR = s->decoded[5]; | ||
553 | /* LF = 0.33 LF + 0.16 SUB + 0.16 FC + 0.33 RL + 0.00 RR | ||
554 | LR = 0.33 LR + 0.16 SUB + 0.16 FC + 0.00 RL + 0.33 RR */ | ||
555 | for (i=0; i<s->blocksize; ++i) { | ||
556 | int32_t a = *(FL)*2 + *(SB) + *(FC) + *(RL)*2; | ||
557 | int32_t b = *(FR)*2 + *(SB) + *(FC) + *(RR)*2; | ||
558 | *outL++ = ((a + (a<<2))>>5) << scale; /* 5>>5 ~= 1/6 */ | ||
559 | *outR++ = ((b + (b<<2))>>5) << scale; /* 5>>5 ~= 1/6 */ | ||
560 | FL++; FR++; SB++; FC++; RL++; RR++; | ||
561 | } | ||
562 | break; | ||
563 | default: /* 1.0 and 2.0 do not need downmix, other formats unknown. */ | ||
564 | return -501; | ||
565 | break; | ||
566 | } | ||
567 | return 0; | ||
568 | } | ||
569 | |||
570 | int flac_decode_frame(FLACContext *s, | ||
571 | uint8_t *buf, int buf_size, | ||
572 | void (*yield)(void)) | ||
573 | { | ||
574 | int tmp; | ||
575 | int i; | ||
576 | int framesize; | ||
577 | int scale; | ||
578 | |||
579 | init_get_bits(&s->gb, buf, buf_size*8); | ||
580 | |||
581 | tmp = get_bits(&s->gb, 16); | ||
582 | if ((tmp & 0xFFFE) != 0xFFF8){ | ||
583 | return -41; | ||
584 | } | ||
585 | |||
586 | if ((framesize=decode_frame(s,yield)) < 0){ | ||
587 | s->bitstream_size=0; | ||
588 | s->bitstream_index=0; | ||
589 | return framesize; | ||
590 | } | ||
591 | |||
592 | yield(); | ||
593 | |||
594 | #define DECORRELATE(left, right)\ | ||
595 | for (i = 0; i < s->blocksize; i++) {\ | ||
596 | int32_t a = s->decoded[0][i];\ | ||
597 | int32_t b = s->decoded[1][i];\ | ||
598 | s->decoded[0][i] = (left) << scale;\ | ||
599 | s->decoded[1][i] = (right) << scale;\ | ||
600 | }\ | ||
601 | |||
602 | scale=FLAC_OUTPUT_DEPTH-s->bps; | ||
603 | switch(s->decorrelation) | ||
604 | { | ||
605 | case INDEPENDENT: | ||
606 | if (s->channels <= 2) { | ||
607 | DECORRELATE(a, b) /* Always decorrelate exactly the two supported channels. */ | ||
608 | } else { | ||
609 | if ((tmp=flac_downmix(s)) != 0) | ||
610 | return tmp; | ||
611 | } | ||
612 | break; | ||
613 | case LEFT_SIDE: | ||
614 | DECORRELATE(a, a-b) | ||
615 | break; | ||
616 | case RIGHT_SIDE: | ||
617 | DECORRELATE(a+b, b) | ||
618 | break; | ||
619 | case MID_SIDE: | ||
620 | DECORRELATE( (a-=b>>1) + b, a) | ||
621 | break; | ||
622 | } | ||
623 | |||
624 | s->framesize = (get_bits_count(&s->gb)+7)>>3; | ||
625 | |||
626 | return 0; | ||
627 | } | ||