flac: remove ARM assembly
[kugel-rb.git] / apps / codecs / libffmpegFLAC / decoder.c
blobe5c4b426d5678253a0c6844ca7b091c8f5e22188
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 #endif
49 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
50 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
52 static const int sample_rate_table[] ICONST_ATTR =
53 { 0, 0, 0, 0,
54 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
55 0, 0, 0, 0 };
57 static const int sample_size_table[] ICONST_ATTR =
58 { 0, 8, 12, 0, 16, 20, 24, 0 };
60 static const int blocksize_table[] ICONST_ATTR = {
61 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
62 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
65 static const uint8_t table_crc8[256] ICONST_ATTR = {
66 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
67 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d,
68 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
69 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d,
70 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5,
71 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
72 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85,
73 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd,
74 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
75 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea,
76 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2,
77 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
78 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32,
79 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a,
80 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
81 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a,
82 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c,
83 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
84 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec,
85 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4,
86 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
87 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44,
88 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c,
89 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
90 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b,
91 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63,
92 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
93 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13,
94 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb,
95 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
96 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb,
97 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3
100 static int64_t get_utf8(GetBitContext *gb) ICODE_ATTR_FLAC;
101 static int64_t get_utf8(GetBitContext *gb)
103 uint64_t val;
104 int ones=0, bytes;
106 while(get_bits1(gb))
107 ones++;
109 if (ones==0) bytes=0;
110 else if(ones==1) return -1;
111 else bytes= ones - 1;
113 val= get_bits(gb, 7-ones);
114 while(bytes--){
115 const int tmp = get_bits(gb, 8);
117 if((tmp>>6) != 2)
118 return -2;
119 val<<=6;
120 val|= tmp&0x3F;
122 return val;
125 static int get_crc8(const uint8_t *buf, int count) ICODE_ATTR_FLAC;
126 static int get_crc8(const uint8_t *buf, int count)
128 int crc=0;
129 int i;
131 for(i=0; i<count; i++){
132 crc = table_crc8[crc ^ buf[i]];
135 return crc;
138 static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC;
139 static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order)
141 int i, tmp, partition, method_type, rice_order;
142 int sample = 0, samples;
144 method_type = get_bits(&s->gb, 2);
145 if (method_type > 1){
146 //fprintf(stderr,"illegal residual coding method %d\n", method_type);
147 return -3;
150 rice_order = get_bits(&s->gb, 4);
152 samples= s->blocksize >> rice_order;
154 sample=
155 i= pred_order;
156 for (partition = 0; partition < (1 << rice_order); partition++)
158 tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
159 if (tmp == (method_type == 0 ? 15 : 31))
161 //fprintf(stderr,"fixed len partition\n");
162 tmp = get_bits(&s->gb, 5);
163 for (; i < samples; i++, sample++)
164 decoded[sample] = get_sbits(&s->gb, tmp);
166 else
168 for (; i < samples; i++, sample++){
169 decoded[sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
172 i= 0;
175 return 0;
178 static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC;
179 static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order)
181 const int blocksize = s->blocksize;
182 int a, b, c, d, i;
184 /* warm up samples */
185 for (i = 0; i < pred_order; i++)
187 decoded[i] = get_sbits(&s->gb, s->curr_bps);
190 if (decode_residuals(s, decoded, pred_order) < 0)
191 return -4;
193 a = decoded[pred_order-1];
194 b = a - decoded[pred_order-2];
195 c = b - decoded[pred_order-2] + decoded[pred_order-3];
196 d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
198 switch(pred_order)
200 case 0:
201 break;
202 case 1:
203 for (i = pred_order; i < blocksize; i++)
204 decoded[i] = a += decoded[i];
205 break;
206 case 2:
207 for (i = pred_order; i < blocksize; i++)
208 decoded[i] = a += b += decoded[i];
209 break;
210 case 3:
211 for (i = pred_order; i < blocksize; i++)
212 decoded[i] = a += b += c += decoded[i];
213 break;
214 case 4:
215 for (i = pred_order; i < blocksize; i++)
216 decoded[i] = a += b += c += d += decoded[i];
217 break;
218 default:
219 return -5;
222 return 0;
225 static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC;
226 static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order)
228 int sum, i, j;
229 int64_t wsum;
230 int coeff_prec, qlevel;
231 int coeffs[pred_order];
233 /* warm up samples */
234 for (i = 0; i < pred_order; i++)
236 decoded[i] = get_sbits(&s->gb, s->curr_bps);
239 coeff_prec = get_bits(&s->gb, 4) + 1;
240 if (coeff_prec == 16)
242 //fprintf(stderr,"invalid coeff precision\n");
243 return -6;
245 qlevel = get_sbits(&s->gb, 5);
246 if (qlevel < 0)
248 //fprintf(stderr,"qlevel %d not supported, maybe buggy stream\n", qlevel);
249 return -7;
252 for (i = 0; i < pred_order; i++)
254 coeffs[i] = get_sbits(&s->gb, coeff_prec);
257 if (decode_residuals(s, decoded, pred_order) < 0)
258 return -8;
260 if ((s->bps + coeff_prec + av_log2(pred_order)) <= 32) {
261 #if defined(CPU_COLDFIRE)
262 (void)sum;
263 lpc_decode_emac(s->blocksize - pred_order, qlevel, pred_order,
264 decoded + pred_order, coeffs);
265 #else
266 for (i = pred_order; i < s->blocksize; i++)
268 sum = 0;
269 for (j = 0; j < pred_order; j++)
270 sum += coeffs[j] * decoded[i-j-1];
271 decoded[i] += sum >> qlevel;
273 #endif
274 } else {
275 #if defined(CPU_COLDFIRE)
276 (void)wsum;
277 (void)j;
278 lpc_decode_emac_wide(s->blocksize - pred_order, qlevel, pred_order,
279 decoded + pred_order, coeffs);
280 #else
281 for (i = pred_order; i < s->blocksize; i++)
283 wsum = 0;
284 for (j = 0; j < pred_order; j++)
285 wsum += (int64_t)coeffs[j] * (int64_t)decoded[i-j-1];
286 decoded[i] += wsum >> qlevel;
288 #endif
291 return 0;
294 static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded)
296 int type, wasted = 0;
297 int i, tmp;
299 s->curr_bps = s->bps;
300 if(channel == 0){
301 if(s->decorrelation == RIGHT_SIDE)
302 s->curr_bps++;
303 }else{
304 if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
305 s->curr_bps++;
308 if (get_bits1(&s->gb))
310 //fprintf(stderr,"invalid subframe padding\n");
311 return -9;
313 type = get_bits(&s->gb, 6);
314 // wasted = get_bits1(&s->gb);
316 // if (wasted)
317 // {
318 // while (!get_bits1(&s->gb))
319 // wasted++;
320 // if (wasted)
321 // wasted++;
322 // s->curr_bps -= wasted;
323 // }
324 #if 0
325 wasted= 16 - av_log2(show_bits(&s->gb, 17));
326 skip_bits(&s->gb, wasted+1);
327 s->curr_bps -= wasted;
328 #else
329 if (get_bits1(&s->gb))
331 wasted = 1;
332 while (!get_bits1(&s->gb))
333 wasted++;
334 s->curr_bps -= wasted;
335 //fprintf(stderr,"%d wasted bits\n", wasted);
337 #endif
338 //FIXME use av_log2 for types
339 if (type == 0)
341 //fprintf(stderr,"coding type: constant\n");
342 tmp = get_sbits(&s->gb, s->curr_bps);
343 for (i = 0; i < s->blocksize; i++)
344 decoded[i] = tmp;
346 else if (type == 1)
348 //fprintf(stderr,"coding type: verbatim\n");
349 for (i = 0; i < s->blocksize; i++)
350 decoded[i] = get_sbits(&s->gb, s->curr_bps);
352 else if ((type >= 8) && (type <= 12))
354 //fprintf(stderr,"coding type: fixed\n");
355 if (decode_subframe_fixed(s, decoded, type & ~0x8) < 0)
356 return -10;
358 else if (type >= 32)
360 //fprintf(stderr,"coding type: lpc\n");
361 if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1) < 0)
362 return -11;
364 else
366 //fprintf(stderr,"Unknown coding type: %d\n",type);
367 return -12;
370 if (wasted)
372 int i;
373 for (i = 0; i < s->blocksize; i++)
374 decoded[i] <<= wasted;
377 return 0;
380 static int decode_frame(FLACContext *s,
381 int32_t* decoded0,
382 int32_t* decoded1,
383 void (*yield)(void)) ICODE_ATTR_FLAC;
384 static int decode_frame(FLACContext *s,
385 int32_t* decoded0,
386 int32_t* decoded1,
387 void (*yield)(void))
389 int blocksize_code, sample_rate_code, sample_size_code, assignment, crc8;
390 int decorrelation, bps, blocksize, samplerate;
391 int res;
393 blocksize_code = get_bits(&s->gb, 4);
395 sample_rate_code = get_bits(&s->gb, 4);
397 assignment = get_bits(&s->gb, 4); /* channel assignment */
398 if (assignment < 8 && s->channels == assignment+1)
399 decorrelation = INDEPENDENT;
400 else if (assignment >=8 && assignment < 11 && s->channels == 2)
401 decorrelation = LEFT_SIDE + assignment - 8;
402 else
404 return -13;
407 sample_size_code = get_bits(&s->gb, 3);
408 if(sample_size_code == 0)
409 bps= s->bps;
410 else if((sample_size_code != 3) && (sample_size_code != 7))
411 bps = sample_size_table[sample_size_code];
412 else
414 return -14;
417 if (get_bits1(&s->gb))
419 return -15;
422 /* Get the samplenumber of the first sample in this block */
423 s->samplenumber=get_utf8(&s->gb);
425 /* samplenumber actually contains the frame number for streams
426 with a constant block size - so we multiply by blocksize to
427 get the actual sample number */
428 if (s->min_blocksize == s->max_blocksize) {
429 s->samplenumber*=s->min_blocksize;
432 #if 0
433 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
434 (s->min_blocksize != s->max_blocksize)){
435 }else{
437 #endif
439 if (blocksize_code == 0)
440 blocksize = s->min_blocksize;
441 else if (blocksize_code == 6)
442 blocksize = get_bits(&s->gb, 8)+1;
443 else if (blocksize_code == 7)
444 blocksize = get_bits(&s->gb, 16)+1;
445 else
446 blocksize = blocksize_table[blocksize_code];
448 if(blocksize > s->max_blocksize){
449 return -16;
452 if (sample_rate_code == 0){
453 samplerate= s->samplerate;
454 }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
455 samplerate = sample_rate_table[sample_rate_code];
456 else if (sample_rate_code == 12)
457 samplerate = get_bits(&s->gb, 8) * 1000;
458 else if (sample_rate_code == 13)
459 samplerate = get_bits(&s->gb, 16);
460 else if (sample_rate_code == 14)
461 samplerate = get_bits(&s->gb, 16) * 10;
462 else{
463 return -17;
466 skip_bits(&s->gb, 8);
467 crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8);
468 if(crc8){
469 return -18;
472 s->blocksize = blocksize;
473 s->samplerate = samplerate;
474 s->bps = bps;
475 s->decorrelation= decorrelation;
477 yield();
478 /* subframes */
479 if ((res=decode_subframe(s, 0, decoded0)) < 0)
480 return res-100;
482 yield();
484 if (s->channels==2) {
485 if ((res=decode_subframe(s, 1, decoded1)) < 0)
486 return res-200;
489 yield();
490 align_get_bits(&s->gb);
492 /* frame footer */
493 skip_bits(&s->gb, 16); /* data crc */
495 return 0;
498 int flac_decode_frame(FLACContext *s,
499 int32_t* decoded0,
500 int32_t* decoded1,
501 uint8_t *buf, int buf_size,
502 void (*yield)(void))
504 int tmp;
505 int i;
506 int framesize;
507 int scale;
509 init_get_bits(&s->gb, buf, buf_size*8);
511 tmp = get_bits(&s->gb, 16);
512 if ((tmp & 0xFFFE) != 0xFFF8){
513 return -41;
516 if ((framesize=decode_frame(s,decoded0,decoded1,yield)) < 0){
517 s->bitstream_size=0;
518 s->bitstream_index=0;
519 return framesize;
522 yield();
524 scale=FLAC_OUTPUT_DEPTH-s->bps;
525 switch(s->decorrelation)
527 case INDEPENDENT:
528 if (s->channels==1) {;
529 for (i = 0; i < s->blocksize; i++)
531 decoded0[i] = decoded0[i] << scale;
533 } else {
534 for (i = 0; i < s->blocksize; i++)
536 decoded0[i] = decoded0[i] << scale;
537 decoded1[i] = decoded1[i] << scale;
540 break;
541 case LEFT_SIDE:
542 //assert(s->channels == 2);
543 for (i = 0; i < s->blocksize; i++)
545 decoded1[i] = (decoded0[i] - decoded1[i]) << scale;
546 decoded0[i] = decoded0[i] << scale;
548 break;
549 case RIGHT_SIDE:
550 //assert(s->channels == 2);
551 for (i = 0; i < s->blocksize; i++)
553 decoded0[i] = (decoded0[i] + decoded1[i]) << scale;
554 decoded1[i] = decoded1[i] << scale;
556 break;
557 case MID_SIDE:
558 //assert(s->channels == 2);
559 for (i = 0; i < s->blocksize; i++)
561 int mid, side;
562 mid = decoded0[i];
563 side = decoded1[i];
565 #if 1 //needs to be checked but IMHO it should be binary identical
566 mid -= side>>1;
567 decoded0[i] = (mid + side) << scale;
568 decoded1[i] = mid << scale;
569 #else
571 mid <<= 1;
572 if (side & 1)
573 mid++;
574 decoded0[i] = ((mid + side) >> 1) << scale;
575 decoded1[i] = ((mid - side) >> 1) << scale;
576 #endif
578 break;
581 s->framesize = (get_bits_count(&s->gb)+7)>>3;
583 return 0;