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
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.
36 #ifndef BUILD_STANDALONE
40 #include "bitstream.h"
45 #if defined(CPU_COLDFIRE)
47 #elif defined(CPU_ARM)
51 static const int sample_rate_table
[] ICONST_ATTR
=
52 { 0, 88200, 176400, 192000,
53 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
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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 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
)
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
);
114 const int tmp
= get_bits(gb
, 8);
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
)
130 for(i
=0; i
<count
; i
++){
131 crc
= table_crc8
[crc
^ buf
[i
]];
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);
149 rice_order
= get_bits(&s
->gb
, 4);
151 samples
= s
->blocksize
>> rice_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
);
167 for (; i
< samples
; i
++, sample
++){
168 decoded
[sample
] = get_sr_golomb_flac(&s
->gb
, tmp
, INT_MAX
, 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
;
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)
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];
202 for (i
= pred_order
; i
< blocksize
; i
++)
203 decoded
[i
] = a
+= decoded
[i
];
206 for (i
= pred_order
; i
< blocksize
; i
++)
207 decoded
[i
] = a
+= b
+= decoded
[i
];
210 for (i
= pred_order
; i
< blocksize
; i
++)
211 decoded
[i
] = a
+= b
+= c
+= decoded
[i
];
214 for (i
= pred_order
; i
< blocksize
; i
++)
215 decoded
[i
] = a
+= b
+= c
+= d
+= decoded
[i
];
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
)
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");
244 qlevel
= get_sbits(&s
->gb
, 5);
247 //fprintf(stderr,"qlevel %d not supported, maybe buggy stream\n", qlevel);
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)
259 if ((s
->bps
+ coeff_prec
+ av_log2(pred_order
)) <= 32) {
260 #if defined(CPU_COLDFIRE)
262 lpc_decode_emac(s
->blocksize
- pred_order
, qlevel
, pred_order
,
263 decoded
+ pred_order
, coeffs
);
264 #elif defined(CPU_ARM)
266 lpc_decode_arm(s
->blocksize
- pred_order
, qlevel
, pred_order
,
267 decoded
+ pred_order
, coeffs
);
269 for (i
= pred_order
; i
< s
->blocksize
; i
++)
272 for (j
= 0; j
< pred_order
; j
++)
273 sum
+= coeffs
[j
] * decoded
[i
-j
-1];
274 decoded
[i
] += sum
>> qlevel
;
278 #if defined(CPU_COLDFIRE)
281 lpc_decode_emac_wide(s
->blocksize
- pred_order
, qlevel
, pred_order
,
282 decoded
+ pred_order
, coeffs
);
284 for (i
= pred_order
; i
< s
->blocksize
; i
++)
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
;
297 static inline int decode_subframe(FLACContext
*s
, int channel
, int32_t* decoded
)
299 int type
, wasted
= 0;
302 s
->curr_bps
= s
->bps
;
304 if(s
->decorrelation
== RIGHT_SIDE
)
307 if(s
->decorrelation
== LEFT_SIDE
|| s
->decorrelation
== MID_SIDE
)
311 if (get_bits1(&s
->gb
))
313 //fprintf(stderr,"invalid subframe padding\n");
316 type
= get_bits(&s
->gb
, 6);
317 // wasted = get_bits1(&s->gb);
321 // while (!get_bits1(&s->gb))
325 // s->curr_bps -= wasted;
328 wasted
= 16 - av_log2(show_bits(&s
->gb
, 17));
329 skip_bits(&s
->gb
, wasted
+1);
330 s
->curr_bps
-= wasted
;
332 if (get_bits1(&s
->gb
))
335 while (!get_bits1(&s
->gb
))
337 s
->curr_bps
-= wasted
;
338 //fprintf(stderr,"%d wasted bits\n", wasted);
341 //FIXME use av_log2 for types
344 //fprintf(stderr,"coding type: constant\n");
345 tmp
= get_sbits(&s
->gb
, s
->curr_bps
);
346 for (i
= 0; i
< s
->blocksize
; i
++)
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)
363 //fprintf(stderr,"coding type: lpc\n");
364 if (decode_subframe_lpc(s
, decoded
, (type
& ~0x20)+1) < 0)
369 //fprintf(stderr,"Unknown coding type: %d\n",type);
376 for (i
= 0; i
< s
->blocksize
; i
++)
377 decoded
[i
] <<= wasted
;
383 static int decode_frame(FLACContext
*s
,
386 void (*yield
)(void)) ICODE_ATTR_FLAC
;
387 static int decode_frame(FLACContext
*s
,
392 int blocksize_code
, sample_rate_code
, sample_size_code
, assignment
, crc8
;
393 int decorrelation
, bps
, blocksize
, samplerate
;
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;
410 sample_size_code
= get_bits(&s
->gb
, 3);
411 if(sample_size_code
== 0)
413 else if((sample_size_code
!= 3) && (sample_size_code
!= 7))
414 bps
= sample_size_table
[sample_size_code
];
420 if (get_bits1(&s
->gb
))
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
;
436 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
437 (s
->min_blocksize
!= s
->max_blocksize
)){
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;
449 blocksize
= blocksize_table
[blocksize_code
];
451 if(blocksize
> s
->max_blocksize
){
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;
469 skip_bits(&s
->gb
, 8);
470 crc8
= get_crc8(s
->gb
.buffer
, get_bits_count(&s
->gb
)/8);
475 s
->blocksize
= blocksize
;
476 s
->samplerate
= samplerate
;
478 s
->decorrelation
= decorrelation
;
482 if ((res
=decode_subframe(s
, 0, decoded0
)) < 0)
487 if (s
->channels
==2) {
488 if ((res
=decode_subframe(s
, 1, decoded1
)) < 0)
493 align_get_bits(&s
->gb
);
496 skip_bits(&s
->gb
, 16); /* data crc */
501 int flac_decode_frame(FLACContext
*s
,
504 uint8_t *buf
, int buf_size
,
512 init_get_bits(&s
->gb
, buf
, buf_size
*8);
514 tmp
= get_bits(&s
->gb
, 16);
515 if ((tmp
& 0xFFFE) != 0xFFF8){
519 if ((framesize
=decode_frame(s
,decoded0
,decoded1
,yield
)) < 0){
521 s
->bitstream_index
=0;
527 scale
=FLAC_OUTPUT_DEPTH
-s
->bps
;
528 switch(s
->decorrelation
)
531 if (s
->channels
==1) {;
532 for (i
= 0; i
< s
->blocksize
; i
++)
534 decoded0
[i
] = decoded0
[i
] << scale
;
537 for (i
= 0; i
< s
->blocksize
; i
++)
539 decoded0
[i
] = decoded0
[i
] << scale
;
540 decoded1
[i
] = decoded1
[i
] << scale
;
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
;
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
;
561 //assert(s->channels == 2);
562 for (i
= 0; i
< s
->blocksize
; i
++)
568 #if 1 //needs to be checked but IMHO it should be binary identical
570 decoded0
[i
] = (mid
+ side
) << scale
;
571 decoded1
[i
] = mid
<< scale
;
577 decoded0
[i
] = ((mid
+ side
) >> 1) << scale
;
578 decoded1
[i
] = ((mid
- side
) >> 1) << scale
;
584 s
->framesize
= (get_bits_count(&s
->gb
)+7)>>3;