summaryrefslogtreecommitdiff
path: root/apps/codecs/libffmpegFLAC/decoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libffmpegFLAC/decoder.c')
-rw-r--r--apps/codecs/libffmpegFLAC/decoder.c584
1 files changed, 584 insertions, 0 deletions
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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 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}