ffmpeg flac does actually support up to 192k sample rates, and my test file seems...
[kugel-rb.git] / apps / codecs / libffmpegFLAC / decoder.c
blob1fafec17fb9902f12358f7289c735d93de0409a6
1 /*
2 * FLAC (Free Lossless Audio Codec) decoder
3 * Copyright (c) 2003 Alex Beregszaszi
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.
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.
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
20 /**
21 * @file flac.c
22 * FLAC (Free Lossless Audio Codec) decoder
23 * @author Alex Beregszaszi
25 * For more information on the FLAC format, visit:
26 * http://flac.sourceforge.net/
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.
34 #include <inttypes.h>
35 #include <stdbool.h>
36 #ifndef BUILD_STANDALONE
37 #include "codeclib.h"
38 #endif
40 #include "bitstream.h"
41 #include "golomb.h"
43 #include "decoder.h"
45 #if defined(CPU_COLDFIRE)
46 #include "coldfire.h"
47 #elif defined(CPU_ARM)
48 #include "arm.h"
49 #endif
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 };
56 static const int sample_size_table[] ICONST_ATTR =
57 { 0, 8, 12, 0, 16, 20, 24, 0 };
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
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
99 static int64_t get_utf8(GetBitContext *gb) ICODE_ATTR_FLAC;
100 static int64_t get_utf8(GetBitContext *gb)
102 uint64_t val;
103 int ones=0, bytes;
105 while(get_bits1(gb))
106 ones++;
108 if (ones==0) bytes=0;
109 else if(ones==1) return -1;
110 else bytes= ones - 1;
112 val= get_bits(gb, 7-ones);
113 while(bytes--){
114 const int tmp = get_bits(gb, 8);
116 if((tmp>>6) != 2)
117 return -2;
118 val<<=6;
119 val|= tmp&0x3F;
121 return val;
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)
127 int crc=0;
128 int i;
130 for(i=0; i<count; i++){
131 crc = table_crc8[crc ^ buf[i]];
134 return crc;
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)
140 int i, tmp, partition, method_type, rice_order;
141 int sample = 0, samples;
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;
149 rice_order = get_bits(&s->gb, 4);
151 samples= s->blocksize >> rice_order;
153 sample=
154 i= pred_order;
155 for (partition = 0; partition < (1 << rice_order); partition++)
157 tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
158 if (tmp == (method_type == 0 ? 15 : 31))
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);
165 else
167 for (; i < samples; i++, sample++){
168 decoded[sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
171 i= 0;
174 return 0;
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)
180 const int blocksize = s->blocksize;
181 int a, b, c, d, i;
183 /* warm up samples */
184 for (i = 0; i < pred_order; i++)
186 decoded[i] = get_sbits(&s->gb, s->curr_bps);
189 if (decode_residuals(s, decoded, pred_order) < 0)
190 return -4;
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];
197 switch(pred_order)
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;
221 return 0;
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)
227 int sum, i, j;
228 int64_t wsum;
229 int coeff_prec, qlevel;
230 int coeffs[pred_order];
232 /* warm up samples */
233 for (i = 0; i < pred_order; i++)
235 decoded[i] = get_sbits(&s->gb, s->curr_bps);
238 coeff_prec = get_bits(&s->gb, 4) + 1;
239 if (coeff_prec == 16)
241 //fprintf(stderr,"invalid coeff precision\n");
242 return -6;
244 qlevel = get_sbits(&s->gb, 5);
245 if (qlevel < 0)
247 //fprintf(stderr,"qlevel %d not supported, maybe buggy stream\n", qlevel);
248 return -7;
251 for (i = 0; i < pred_order; i++)
253 coeffs[i] = get_sbits(&s->gb, coeff_prec);
256 if (decode_residuals(s, decoded, pred_order) < 0)
257 return -8;
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++)
271 sum = 0;
272 for (j = 0; j < pred_order; j++)
273 sum += coeffs[j] * decoded[i-j-1];
274 decoded[i] += sum >> qlevel;
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++)
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;
291 #endif
294 return 0;
297 static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded)
299 int type, wasted = 0;
300 int i, tmp;
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++;
311 if (get_bits1(&s->gb))
313 //fprintf(stderr,"invalid subframe padding\n");
314 return -9;
316 type = get_bits(&s->gb, 6);
317 // wasted = get_bits1(&s->gb);
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))
334 wasted = 1;
335 while (!get_bits1(&s->gb))
336 wasted++;
337 s->curr_bps -= wasted;
338 //fprintf(stderr,"%d wasted bits\n", wasted);
340 #endif
341 //FIXME use av_log2 for types
342 if (type == 0)
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;
349 else if (type == 1)
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);
355 else if ((type >= 8) && (type <= 12))
357 //fprintf(stderr,"coding type: fixed\n");
358 if (decode_subframe_fixed(s, decoded, type & ~0x8) < 0)
359 return -10;
361 else if (type >= 32)
363 //fprintf(stderr,"coding type: lpc\n");
364 if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1) < 0)
365 return -11;
367 else
369 //fprintf(stderr,"Unknown coding type: %d\n",type);
370 return -12;
373 if (wasted)
375 int i;
376 for (i = 0; i < s->blocksize; i++)
377 decoded[i] <<= wasted;
380 return 0;
383 static int decode_frame(FLACContext *s,
384 int32_t* decoded0,
385 int32_t* decoded1,
386 void (*yield)(void)) ICODE_ATTR_FLAC;
387 static int decode_frame(FLACContext *s,
388 int32_t* decoded0,
389 int32_t* decoded1,
390 void (*yield)(void))
392 int blocksize_code, sample_rate_code, sample_size_code, assignment, crc8;
393 int decorrelation, bps, blocksize, samplerate;
394 int res;
396 blocksize_code = get_bits(&s->gb, 4);
398 sample_rate_code = get_bits(&s->gb, 4);
400 assignment = get_bits(&s->gb, 4); /* channel assignment */
401 if (assignment < 8 && s->channels == assignment+1)
402 decorrelation = INDEPENDENT;
403 else if (assignment >=8 && assignment < 11 && s->channels == 2)
404 decorrelation = LEFT_SIDE + assignment - 8;
405 else
407 return -13;
410 sample_size_code = get_bits(&s->gb, 3);
411 if(sample_size_code == 0)
412 bps= s->bps;
413 else if((sample_size_code != 3) && (sample_size_code != 7))
414 bps = sample_size_table[sample_size_code];
415 else
417 return -14;
420 if (get_bits1(&s->gb))
422 return -15;
425 /* Get the samplenumber of the first sample in this block */
426 s->samplenumber=get_utf8(&s->gb);
428 /* samplenumber actually contains the frame number for streams
429 with a constant block size - so we multiply by blocksize to
430 get the actual sample number */
431 if (s->min_blocksize == s->max_blocksize) {
432 s->samplenumber*=s->min_blocksize;
435 #if 0
436 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
437 (s->min_blocksize != s->max_blocksize)){
438 }else{
440 #endif
442 if (blocksize_code == 0)
443 blocksize = s->min_blocksize;
444 else if (blocksize_code == 6)
445 blocksize = get_bits(&s->gb, 8)+1;
446 else if (blocksize_code == 7)
447 blocksize = get_bits(&s->gb, 16)+1;
448 else
449 blocksize = blocksize_table[blocksize_code];
451 if(blocksize > s->max_blocksize){
452 return -16;
455 if (sample_rate_code == 0){
456 samplerate= s->samplerate;
457 }else if ((sample_rate_code < 12))
458 samplerate = sample_rate_table[sample_rate_code];
459 else if (sample_rate_code == 12)
460 samplerate = get_bits(&s->gb, 8) * 1000;
461 else if (sample_rate_code == 13)
462 samplerate = get_bits(&s->gb, 16);
463 else if (sample_rate_code == 14)
464 samplerate = get_bits(&s->gb, 16) * 10;
465 else{
466 return -17;
469 skip_bits(&s->gb, 8);
470 crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8);
471 if(crc8){
472 return -18;
475 s->blocksize = blocksize;
476 s->samplerate = samplerate;
477 s->bps = bps;
478 s->decorrelation= decorrelation;
480 yield();
481 /* subframes */
482 if ((res=decode_subframe(s, 0, decoded0)) < 0)
483 return res-100;
485 yield();
487 if (s->channels==2) {
488 if ((res=decode_subframe(s, 1, decoded1)) < 0)
489 return res-200;
492 yield();
493 align_get_bits(&s->gb);
495 /* frame footer */
496 skip_bits(&s->gb, 16); /* data crc */
498 return 0;
501 int flac_decode_frame(FLACContext *s,
502 int32_t* decoded0,
503 int32_t* decoded1,
504 uint8_t *buf, int buf_size,
505 void (*yield)(void))
507 int tmp;
508 int i;
509 int framesize;
510 int scale;
512 init_get_bits(&s->gb, buf, buf_size*8);
514 tmp = get_bits(&s->gb, 16);
515 if ((tmp & 0xFFFE) != 0xFFF8){
516 return -41;
519 if ((framesize=decode_frame(s,decoded0,decoded1,yield)) < 0){
520 s->bitstream_size=0;
521 s->bitstream_index=0;
522 return framesize;
525 yield();
527 scale=FLAC_OUTPUT_DEPTH-s->bps;
528 switch(s->decorrelation)
530 case INDEPENDENT:
531 if (s->channels==1) {;
532 for (i = 0; i < s->blocksize; i++)
534 decoded0[i] = decoded0[i] << scale;
536 } else {
537 for (i = 0; i < s->blocksize; i++)
539 decoded0[i] = decoded0[i] << scale;
540 decoded1[i] = decoded1[i] << scale;
543 break;
544 case LEFT_SIDE:
545 //assert(s->channels == 2);
546 for (i = 0; i < s->blocksize; i++)
548 decoded1[i] = (decoded0[i] - decoded1[i]) << scale;
549 decoded0[i] = decoded0[i] << scale;
551 break;
552 case RIGHT_SIDE:
553 //assert(s->channels == 2);
554 for (i = 0; i < s->blocksize; i++)
556 decoded0[i] = (decoded0[i] + decoded1[i]) << scale;
557 decoded1[i] = decoded1[i] << scale;
559 break;
560 case MID_SIDE:
561 //assert(s->channels == 2);
562 for (i = 0; i < s->blocksize; i++)
564 int mid, side;
565 mid = decoded0[i];
566 side = decoded1[i];
568 #if 1 //needs to be checked but IMHO it should be binary identical
569 mid -= side>>1;
570 decoded0[i] = (mid + side) << scale;
571 decoded1[i] = mid << scale;
572 #else
574 mid <<= 1;
575 if (side & 1)
576 mid++;
577 decoded0[i] = ((mid + side) >> 1) << scale;
578 decoded1[i] = ((mid - side) >> 1) << scale;
579 #endif
581 break;
584 s->framesize = (get_bits_count(&s->gb)+7)>>3;
586 return 0;