FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / apps / codecs / libffmpegFLAC / decoder.c
blobed175548f2573e0509b3533de456d536c3165b69
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 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
52 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
54 static const int sample_rate_table[] ICONST_ATTR =
55 { 0, 0, 0, 0,
56 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
57 0, 0, 0, 0 };
59 static const int sample_size_table[] ICONST_ATTR =
60 { 0, 8, 12, 0, 16, 20, 24, 0 };
62 static const int blocksize_table[] ICONST_ATTR = {
63 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
64 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
67 static const uint8_t table_crc8[256] ICONST_ATTR = {
68 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
69 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d,
70 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
71 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d,
72 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5,
73 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
74 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85,
75 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd,
76 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
77 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea,
78 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2,
79 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
80 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32,
81 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a,
82 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
83 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a,
84 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c,
85 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
86 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec,
87 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4,
88 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
89 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44,
90 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c,
91 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
92 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b,
93 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63,
94 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
95 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13,
96 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb,
97 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
98 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb,
99 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3
102 static int64_t get_utf8(GetBitContext *gb) ICODE_ATTR_FLAC;
103 static int64_t get_utf8(GetBitContext *gb)
105 uint64_t val;
106 int ones=0, bytes;
108 while(get_bits1(gb))
109 ones++;
111 if (ones==0) bytes=0;
112 else if(ones==1) return -1;
113 else bytes= ones - 1;
115 val= get_bits(gb, 7-ones);
116 while(bytes--){
117 const int tmp = get_bits(gb, 8);
119 if((tmp>>6) != 2)
120 return -2;
121 val<<=6;
122 val|= tmp&0x3F;
124 return val;
127 static int get_crc8(const uint8_t *buf, int count) ICODE_ATTR_FLAC;
128 static int get_crc8(const uint8_t *buf, int count)
130 int crc=0;
131 int i;
133 for(i=0; i<count; i++){
134 crc = table_crc8[crc ^ buf[i]];
137 return crc;
140 static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC;
141 static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order)
143 int i, tmp, partition, method_type, rice_order;
144 int sample = 0, samples;
146 method_type = get_bits(&s->gb, 2);
147 if (method_type > 1){
148 //fprintf(stderr,"illegal residual coding method %d\n", method_type);
149 return -3;
152 rice_order = get_bits(&s->gb, 4);
154 samples= s->blocksize >> rice_order;
156 sample=
157 i= pred_order;
158 for (partition = 0; partition < (1 << rice_order); partition++)
160 tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
161 if (tmp == (method_type == 0 ? 15 : 31))
163 //fprintf(stderr,"fixed len partition\n");
164 tmp = get_bits(&s->gb, 5);
165 for (; i < samples; i++, sample++)
166 decoded[sample] = get_sbits(&s->gb, tmp);
168 else
170 for (; i < samples; i++, sample++){
171 decoded[sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
174 i= 0;
177 return 0;
180 static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC;
181 static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order)
183 const int blocksize = s->blocksize;
184 int a, b, c, d, i;
186 /* warm up samples */
187 for (i = 0; i < pred_order; i++)
189 decoded[i] = get_sbits(&s->gb, s->curr_bps);
192 if (decode_residuals(s, decoded, pred_order) < 0)
193 return -4;
195 a = decoded[pred_order-1];
196 b = a - decoded[pred_order-2];
197 c = b - decoded[pred_order-2] + decoded[pred_order-3];
198 d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
200 switch(pred_order)
202 case 0:
203 break;
204 case 1:
205 for (i = pred_order; i < blocksize; i++)
206 decoded[i] = a += decoded[i];
207 break;
208 case 2:
209 for (i = pred_order; i < blocksize; i++)
210 decoded[i] = a += b += decoded[i];
211 break;
212 case 3:
213 for (i = pred_order; i < blocksize; i++)
214 decoded[i] = a += b += c += decoded[i];
215 break;
216 case 4:
217 for (i = pred_order; i < blocksize; i++)
218 decoded[i] = a += b += c += d += decoded[i];
219 break;
220 default:
221 return -5;
224 return 0;
227 static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC;
228 static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order)
230 int sum, i, j;
231 int64_t wsum;
232 int coeff_prec, qlevel;
233 int coeffs[pred_order];
235 /* warm up samples */
236 for (i = 0; i < pred_order; i++)
238 decoded[i] = get_sbits(&s->gb, s->curr_bps);
241 coeff_prec = get_bits(&s->gb, 4) + 1;
242 if (coeff_prec == 16)
244 //fprintf(stderr,"invalid coeff precision\n");
245 return -6;
247 qlevel = get_sbits(&s->gb, 5);
248 if (qlevel < 0)
250 //fprintf(stderr,"qlevel %d not supported, maybe buggy stream\n", qlevel);
251 return -7;
254 for (i = 0; i < pred_order; i++)
256 coeffs[i] = get_sbits(&s->gb, coeff_prec);
259 if (decode_residuals(s, decoded, pred_order) < 0)
260 return -8;
262 if ((s->bps + coeff_prec + av_log2(pred_order)) <= 32) {
263 #if defined(CPU_COLDFIRE)
264 (void)sum;
265 lpc_decode_emac(s->blocksize - pred_order, qlevel, pred_order,
266 decoded + pred_order, coeffs);
267 #elif defined(CPU_ARM)
268 (void)sum;
269 lpc_decode_arm(s->blocksize - pred_order, qlevel, pred_order,
270 decoded + pred_order, coeffs);
271 #else
272 for (i = pred_order; i < s->blocksize; i++)
274 sum = 0;
275 for (j = 0; j < pred_order; j++)
276 sum += coeffs[j] * decoded[i-j-1];
277 decoded[i] += sum >> qlevel;
279 #endif
280 } else {
281 #if defined(CPU_COLDFIRE)
282 (void)wsum;
283 (void)j;
284 lpc_decode_emac_wide(s->blocksize - pred_order, qlevel, pred_order,
285 decoded + pred_order, coeffs);
286 #else
287 for (i = pred_order; i < s->blocksize; i++)
289 wsum = 0;
290 for (j = 0; j < pred_order; j++)
291 wsum += (int64_t)coeffs[j] * (int64_t)decoded[i-j-1];
292 decoded[i] += wsum >> qlevel;
294 #endif
297 return 0;
300 static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded)
302 int type, wasted = 0;
303 int i, tmp;
305 s->curr_bps = s->bps;
306 if(channel == 0){
307 if(s->decorrelation == RIGHT_SIDE)
308 s->curr_bps++;
309 }else{
310 if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
311 s->curr_bps++;
314 if (get_bits1(&s->gb))
316 //fprintf(stderr,"invalid subframe padding\n");
317 return -9;
319 type = get_bits(&s->gb, 6);
320 // wasted = get_bits1(&s->gb);
322 // if (wasted)
323 // {
324 // while (!get_bits1(&s->gb))
325 // wasted++;
326 // if (wasted)
327 // wasted++;
328 // s->curr_bps -= wasted;
329 // }
330 #if 0
331 wasted= 16 - av_log2(show_bits(&s->gb, 17));
332 skip_bits(&s->gb, wasted+1);
333 s->curr_bps -= wasted;
334 #else
335 if (get_bits1(&s->gb))
337 wasted = 1;
338 while (!get_bits1(&s->gb))
339 wasted++;
340 s->curr_bps -= wasted;
341 //fprintf(stderr,"%d wasted bits\n", wasted);
343 #endif
344 //FIXME use av_log2 for types
345 if (type == 0)
347 //fprintf(stderr,"coding type: constant\n");
348 tmp = get_sbits(&s->gb, s->curr_bps);
349 for (i = 0; i < s->blocksize; i++)
350 decoded[i] = tmp;
352 else if (type == 1)
354 //fprintf(stderr,"coding type: verbatim\n");
355 for (i = 0; i < s->blocksize; i++)
356 decoded[i] = get_sbits(&s->gb, s->curr_bps);
358 else if ((type >= 8) && (type <= 12))
360 //fprintf(stderr,"coding type: fixed\n");
361 if (decode_subframe_fixed(s, decoded, type & ~0x8) < 0)
362 return -10;
364 else if (type >= 32)
366 //fprintf(stderr,"coding type: lpc\n");
367 if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1) < 0)
368 return -11;
370 else
372 //fprintf(stderr,"Unknown coding type: %d\n",type);
373 return -12;
376 if (wasted)
378 int i;
379 for (i = 0; i < s->blocksize; i++)
380 decoded[i] <<= wasted;
383 return 0;
386 static int decode_frame(FLACContext *s,
387 int32_t* decoded0,
388 int32_t* decoded1,
389 void (*yield)(void)) ICODE_ATTR_FLAC;
390 static int decode_frame(FLACContext *s,
391 int32_t* decoded0,
392 int32_t* decoded1,
393 void (*yield)(void))
395 int blocksize_code, sample_rate_code, sample_size_code, assignment, crc8;
396 int decorrelation, bps, blocksize, samplerate;
397 int res;
399 blocksize_code = get_bits(&s->gb, 4);
401 sample_rate_code = get_bits(&s->gb, 4);
403 assignment = get_bits(&s->gb, 4); /* channel assignment */
404 if (assignment < 8 && s->channels == assignment+1)
405 decorrelation = INDEPENDENT;
406 else if (assignment >=8 && assignment < 11 && s->channels == 2)
407 decorrelation = LEFT_SIDE + assignment - 8;
408 else
410 return -13;
413 sample_size_code = get_bits(&s->gb, 3);
414 if(sample_size_code == 0)
415 bps= s->bps;
416 else if((sample_size_code != 3) && (sample_size_code != 7))
417 bps = sample_size_table[sample_size_code];
418 else
420 return -14;
423 if (get_bits1(&s->gb))
425 return -15;
428 /* Get the samplenumber of the first sample in this block */
429 s->samplenumber=get_utf8(&s->gb);
431 /* samplenumber actually contains the frame number for streams
432 with a constant block size - so we multiply by blocksize to
433 get the actual sample number */
434 if (s->min_blocksize == s->max_blocksize) {
435 s->samplenumber*=s->min_blocksize;
438 #if 0
439 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
440 (s->min_blocksize != s->max_blocksize)){
441 }else{
443 #endif
445 if (blocksize_code == 0)
446 blocksize = s->min_blocksize;
447 else if (blocksize_code == 6)
448 blocksize = get_bits(&s->gb, 8)+1;
449 else if (blocksize_code == 7)
450 blocksize = get_bits(&s->gb, 16)+1;
451 else
452 blocksize = blocksize_table[blocksize_code];
454 if(blocksize > s->max_blocksize){
455 return -16;
458 if (sample_rate_code == 0){
459 samplerate= s->samplerate;
460 }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
461 samplerate = sample_rate_table[sample_rate_code];
462 else if (sample_rate_code == 12)
463 samplerate = get_bits(&s->gb, 8) * 1000;
464 else if (sample_rate_code == 13)
465 samplerate = get_bits(&s->gb, 16);
466 else if (sample_rate_code == 14)
467 samplerate = get_bits(&s->gb, 16) * 10;
468 else{
469 return -17;
472 skip_bits(&s->gb, 8);
473 crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8);
474 if(crc8){
475 return -18;
478 s->blocksize = blocksize;
479 s->samplerate = samplerate;
480 s->bps = bps;
481 s->decorrelation= decorrelation;
483 yield();
484 /* subframes */
485 if ((res=decode_subframe(s, 0, decoded0)) < 0)
486 return res-100;
488 yield();
490 if (s->channels==2) {
491 if ((res=decode_subframe(s, 1, decoded1)) < 0)
492 return res-200;
495 yield();
496 align_get_bits(&s->gb);
498 /* frame footer */
499 skip_bits(&s->gb, 16); /* data crc */
501 return 0;
504 int flac_decode_frame(FLACContext *s,
505 int32_t* decoded0,
506 int32_t* decoded1,
507 uint8_t *buf, int buf_size,
508 void (*yield)(void))
510 int tmp;
511 int i;
512 int framesize;
513 int scale;
515 init_get_bits(&s->gb, buf, buf_size*8);
517 tmp = get_bits(&s->gb, 16);
518 if ((tmp & 0xFFFE) != 0xFFF8){
519 return -41;
522 if ((framesize=decode_frame(s,decoded0,decoded1,yield)) < 0){
523 s->bitstream_size=0;
524 s->bitstream_index=0;
525 return framesize;
528 yield();
530 scale=FLAC_OUTPUT_DEPTH-s->bps;
531 switch(s->decorrelation)
533 case INDEPENDENT:
534 if (s->channels==1) {;
535 for (i = 0; i < s->blocksize; i++)
537 decoded0[i] = decoded0[i] << scale;
539 } else {
540 for (i = 0; i < s->blocksize; i++)
542 decoded0[i] = decoded0[i] << scale;
543 decoded1[i] = decoded1[i] << scale;
546 break;
547 case LEFT_SIDE:
548 //assert(s->channels == 2);
549 for (i = 0; i < s->blocksize; i++)
551 decoded1[i] = (decoded0[i] - decoded1[i]) << scale;
552 decoded0[i] = decoded0[i] << scale;
554 break;
555 case RIGHT_SIDE:
556 //assert(s->channels == 2);
557 for (i = 0; i < s->blocksize; i++)
559 decoded0[i] = (decoded0[i] + decoded1[i]) << scale;
560 decoded1[i] = decoded1[i] << scale;
562 break;
563 case MID_SIDE:
564 //assert(s->channels == 2);
565 for (i = 0; i < s->blocksize; i++)
567 int mid, side;
568 mid = decoded0[i];
569 side = decoded1[i];
571 #if 1 //needs to be checked but IMHO it should be binary identical
572 mid -= side>>1;
573 decoded0[i] = (mid + side) << scale;
574 decoded1[i] = mid << scale;
575 #else
577 mid <<= 1;
578 if (side & 1)
579 mid++;
580 decoded0[i] = ((mid + side) >> 1) << scale;
581 decoded1[i] = ((mid - side) >> 1) << scale;
582 #endif
584 break;
587 s->framesize = (get_bits_count(&s->gb)+7)>>3;
589 return 0;