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 #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
=
56 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
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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 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
)
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
);
117 const int tmp
= get_bits(gb
, 8);
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
)
133 for(i
=0; i
<count
; i
++){
134 crc
= table_crc8
[crc
^ buf
[i
]];
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);
152 rice_order
= get_bits(&s
->gb
, 4);
154 samples
= s
->blocksize
>> rice_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
);
170 for (; i
< samples
; i
++, sample
++){
171 decoded
[sample
] = get_sr_golomb_flac(&s
->gb
, tmp
, INT_MAX
, 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
;
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)
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];
205 for (i
= pred_order
; i
< blocksize
; i
++)
206 decoded
[i
] = a
+= decoded
[i
];
209 for (i
= pred_order
; i
< blocksize
; i
++)
210 decoded
[i
] = a
+= b
+= decoded
[i
];
213 for (i
= pred_order
; i
< blocksize
; i
++)
214 decoded
[i
] = a
+= b
+= c
+= decoded
[i
];
217 for (i
= pred_order
; i
< blocksize
; i
++)
218 decoded
[i
] = a
+= b
+= c
+= d
+= decoded
[i
];
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
)
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");
247 qlevel
= get_sbits(&s
->gb
, 5);
250 //fprintf(stderr,"qlevel %d not supported, maybe buggy stream\n", qlevel);
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)
262 if ((s
->bps
+ coeff_prec
+ av_log2(pred_order
)) <= 32) {
263 #if defined(CPU_COLDFIRE)
265 lpc_decode_emac(s
->blocksize
- pred_order
, qlevel
, pred_order
,
266 decoded
+ pred_order
, coeffs
);
267 #elif defined(CPU_ARM)
269 lpc_decode_arm(s
->blocksize
- pred_order
, qlevel
, pred_order
,
270 decoded
+ pred_order
, coeffs
);
272 for (i
= pred_order
; i
< s
->blocksize
; i
++)
275 for (j
= 0; j
< pred_order
; j
++)
276 sum
+= coeffs
[j
] * decoded
[i
-j
-1];
277 decoded
[i
] += sum
>> qlevel
;
281 #if defined(CPU_COLDFIRE)
284 lpc_decode_emac_wide(s
->blocksize
- pred_order
, qlevel
, pred_order
,
285 decoded
+ pred_order
, coeffs
);
287 for (i
= pred_order
; i
< s
->blocksize
; i
++)
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
;
300 static inline int decode_subframe(FLACContext
*s
, int channel
, int32_t* decoded
)
302 int type
, wasted
= 0;
305 s
->curr_bps
= s
->bps
;
307 if(s
->decorrelation
== RIGHT_SIDE
)
310 if(s
->decorrelation
== LEFT_SIDE
|| s
->decorrelation
== MID_SIDE
)
314 if (get_bits1(&s
->gb
))
316 //fprintf(stderr,"invalid subframe padding\n");
319 type
= get_bits(&s
->gb
, 6);
320 // wasted = get_bits1(&s->gb);
324 // while (!get_bits1(&s->gb))
328 // s->curr_bps -= wasted;
331 wasted
= 16 - av_log2(show_bits(&s
->gb
, 17));
332 skip_bits(&s
->gb
, wasted
+1);
333 s
->curr_bps
-= wasted
;
335 if (get_bits1(&s
->gb
))
338 while (!get_bits1(&s
->gb
))
340 s
->curr_bps
-= wasted
;
341 //fprintf(stderr,"%d wasted bits\n", wasted);
344 //FIXME use av_log2 for types
347 //fprintf(stderr,"coding type: constant\n");
348 tmp
= get_sbits(&s
->gb
, s
->curr_bps
);
349 for (i
= 0; i
< s
->blocksize
; i
++)
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)
366 //fprintf(stderr,"coding type: lpc\n");
367 if (decode_subframe_lpc(s
, decoded
, (type
& ~0x20)+1) < 0)
372 //fprintf(stderr,"Unknown coding type: %d\n",type);
379 for (i
= 0; i
< s
->blocksize
; i
++)
380 decoded
[i
] <<= wasted
;
386 static int decode_frame(FLACContext
*s
,
389 void (*yield
)(void)) ICODE_ATTR_FLAC
;
390 static int decode_frame(FLACContext
*s
,
395 int blocksize_code
, sample_rate_code
, sample_size_code
, assignment
, crc8
;
396 int decorrelation
, bps
, blocksize
, samplerate
;
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;
413 sample_size_code
= get_bits(&s
->gb
, 3);
414 if(sample_size_code
== 0)
416 else if((sample_size_code
!= 3) && (sample_size_code
!= 7))
417 bps
= sample_size_table
[sample_size_code
];
423 if (get_bits1(&s
->gb
))
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
;
439 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
440 (s
->min_blocksize
!= s
->max_blocksize
)){
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;
452 blocksize
= blocksize_table
[blocksize_code
];
454 if(blocksize
> s
->max_blocksize
){
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;
472 skip_bits(&s
->gb
, 8);
473 crc8
= get_crc8(s
->gb
.buffer
, get_bits_count(&s
->gb
)/8);
478 s
->blocksize
= blocksize
;
479 s
->samplerate
= samplerate
;
481 s
->decorrelation
= decorrelation
;
485 if ((res
=decode_subframe(s
, 0, decoded0
)) < 0)
490 if (s
->channels
==2) {
491 if ((res
=decode_subframe(s
, 1, decoded1
)) < 0)
496 align_get_bits(&s
->gb
);
499 skip_bits(&s
->gb
, 16); /* data crc */
504 int flac_decode_frame(FLACContext
*s
,
507 uint8_t *buf
, int buf_size
,
515 init_get_bits(&s
->gb
, buf
, buf_size
*8);
517 tmp
= get_bits(&s
->gb
, 16);
518 if ((tmp
& 0xFFFE) != 0xFFF8){
522 if ((framesize
=decode_frame(s
,decoded0
,decoded1
,yield
)) < 0){
524 s
->bitstream_index
=0;
530 scale
=FLAC_OUTPUT_DEPTH
-s
->bps
;
531 switch(s
->decorrelation
)
534 if (s
->channels
==1) {;
535 for (i
= 0; i
< s
->blocksize
; i
++)
537 decoded0
[i
] = decoded0
[i
] << scale
;
540 for (i
= 0; i
< s
->blocksize
; i
++)
542 decoded0
[i
] = decoded0
[i
] << scale
;
543 decoded1
[i
] = decoded1
[i
] << scale
;
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
;
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
;
564 //assert(s->channels == 2);
565 for (i
= 0; i
< s
->blocksize
; i
++)
571 #if 1 //needs to be checked but IMHO it should be binary identical
573 decoded0
[i
] = (mid
+ side
) << scale
;
574 decoded1
[i
] = mid
<< scale
;
580 decoded0
[i
] = ((mid
+ side
) >> 1) << scale
;
581 decoded1
[i
] = ((mid
- side
) >> 1) << scale
;
587 s
->framesize
= (get_bits_count(&s
->gb
)+7)>>3;