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
34 #define MAX_CHANNELS 8
35 #define MAX_BLOCKSIZE 65535
44 typedef struct FLACContext {
45 AVCodecContext *avctx;
48 int min_blocksize, max_blocksize;
49 int min_framesize, max_framesize;
50 int samplerate, channels;
51 int blocksize/*, last_blocksize*/;
53 enum channel_order order;
55 int32_t *residual[MAX_CHANNELS];
56 int32_t *decoded[MAX_CHANNELS];
60 int allocated_bitstream_size;
63 #define METADATA_TYPE_STREAMINFO 0
65 static int sample_rate_table[] =
67 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
70 static int sample_size_table[] =
71 { 0, 8, 12, 0, 16, 20, 24, 0 };
73 static int64_t get_utf8(GetBitContext *gb)
82 else if(ones==1) return -1;
85 val= get_bits(gb, 7-ones);
87 const int tmp = get_bits(gb, 8);
97 static int flac_decode_init(AVCodecContext * avctx)
102 static void dump_headers(FLACContext *s)
104 av_log(s->avctx, AV_LOG_DEBUG, " Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
105 av_log(s->avctx, AV_LOG_DEBUG, " Framesize: %d .. %d\n", s->min_framesize, s->max_framesize);
106 av_log(s->avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
107 av_log(s->avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
108 av_log(s->avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
111 static void allocate_buffers(FLACContext *s){
114 assert(s->max_blocksize);
116 if(s->max_framesize == 0 && s->max_blocksize){
117 s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8; //FIXME header overhead
120 for (i = 0; i < s->channels; i++)
122 s->decoded[i] = av_realloc(s->decoded[i], sizeof(int32_t)*s->max_blocksize);
123 s->residual[i] = av_realloc(s->residual[i], sizeof(int32_t)*s->max_blocksize);
126 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
127 // s->bitstream= av_realloc(s->bitstream, s->max_framesize);
130 static void metadata_streaminfo(FLACContext *s)
132 /* mandatory streaminfo */
133 s->min_blocksize = get_bits(&s->gb, 16);
134 s->max_blocksize = get_bits(&s->gb, 16);
136 s->min_framesize = get_bits_long(&s->gb, 24);
137 s->max_framesize = get_bits_long(&s->gb, 24);
139 s->samplerate = get_bits_long(&s->gb, 20);
140 s->channels = get_bits(&s->gb, 3) + 1;
141 s->bps = get_bits(&s->gb, 5) + 1;
143 s->avctx->channels = s->channels;
144 s->avctx->sample_rate = s->samplerate;
146 skip_bits(&s->gb, 36); /* total num of samples */
148 skip_bits(&s->gb, 64); /* md5 sum */
149 skip_bits(&s->gb, 64); /* md5 sum */
154 static int decode_residuals(FLACContext *s, int channel, int pred_order)
156 int i, tmp, partition, method_type, rice_order;
157 int sample = 0, samples;
159 method_type = get_bits(&s->gb, 2);
160 if (method_type != 0){
161 av_log(s->avctx, AV_LOG_DEBUG, "illegal residual coding method %d\n", method_type);
165 rice_order = get_bits(&s->gb, 4);
167 samples= s->blocksize >> rice_order;
171 for (partition = 0; partition < (1 << rice_order); partition++)
173 tmp = get_bits(&s->gb, 4);
176 av_log(s->avctx, AV_LOG_DEBUG, "fixed len partition\n");
177 tmp = get_bits(&s->gb, 5);
178 for (; i < samples; i++, sample++)
179 s->residual[channel][sample] = get_sbits(&s->gb, tmp);
183 // av_log(s->avctx, AV_LOG_DEBUG, "rice coded partition k=%d\n", tmp);
184 for (; i < samples; i++, sample++){
185 s->residual[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
186 if(get_bits_count(&s->gb) > s->gb.size_in_bits){
187 av_log(s->avctx, AV_LOG_ERROR, "fucking FLAC\n");
195 // av_log(s->avctx, AV_LOG_DEBUG, "partitions: %d, samples: %d\n", 1 << rice_order, sample);
200 static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
204 av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME FIXED\n");
206 /* warm up samples */
207 av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
209 for (i = 0; i < pred_order; i++)
211 s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
212 // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
215 if (decode_residuals(s, channel, pred_order) < 0)
221 for (i = pred_order; i < s->blocksize; i++)
222 s->decoded[channel][i] = s->residual[channel][i];
225 for (i = pred_order; i < s->blocksize; i++)
226 s->decoded[channel][i] = s->residual[channel][i] +
227 s->decoded[channel][i-1];
230 for (i = pred_order; i < s->blocksize; i++)
231 s->decoded[channel][i] = s->residual[channel][i] +
232 (s->decoded[channel][i-1] << 1) -
233 s->decoded[channel][i-2];
236 for (i = pred_order; i < s->blocksize; i++)
237 s->decoded[channel][i] = s->residual[channel][i] +
238 3*(s->decoded[channel][i-1] - s->decoded[channel][i-2])
239 + s->decoded[channel][i-3];
242 for (i = pred_order; i < s->blocksize; i++)
243 s->decoded[channel][i] = s->residual[channel][i] +
244 ((s->decoded[channel][i-1] +
245 s->decoded[channel][i-3]) << 2) -
246 ((s->decoded[channel][i-2] << 2) +
247 (s->decoded[channel][i-2] << 1)) -
248 s->decoded[channel][i-4];
251 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
258 static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
261 int coeff_prec, qlevel;
262 int coeffs[pred_order];
264 // av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME LPC\n");
266 /* warm up samples */
267 // av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
269 for (i = 0; i < pred_order; i++)
271 s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
272 // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
275 coeff_prec = get_bits(&s->gb, 4) + 1;
276 if (coeff_prec == 16)
278 av_log(s->avctx, AV_LOG_DEBUG, "invalid coeff precision\n");
281 av_log(s->avctx, AV_LOG_DEBUG, " qlp coeff prec: %d\n", coeff_prec);
282 qlevel = get_sbits(&s->gb, 5);
283 av_log(s->avctx, AV_LOG_DEBUG, " quant level: %d\n", qlevel);
284 assert(qlevel >= 0); //FIXME
285 for (i = 0; i < pred_order; i++)
287 coeffs[i] = get_sbits(&s->gb, coeff_prec);
288 // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, coeffs[i]);
291 if (decode_residuals(s, channel, pred_order) < 0)
294 for (i = pred_order; i < s->blocksize; i++)
297 for (j = 0; j < pred_order; j++)
298 sum += coeffs[j] * s->decoded[channel][i-j-1];
299 s->decoded[channel][i] = s->residual[channel][i] + (sum >> qlevel);
305 static inline int decode_subframe(FLACContext *s, int channel)
307 int type, wasted = 0;
310 s->curr_bps = s->bps;
312 if(s->order == RIGHT_SIDE)
315 if(s->order == LEFT_SIDE || s->order == MID_SIDE)
319 if (get_bits1(&s->gb))
321 av_log(s->avctx, AV_LOG_DEBUG, "invalid subframe padding\n");
324 type = get_bits(&s->gb, 6);
325 // wasted = get_bits1(&s->gb);
329 // while (!get_bits1(&s->gb))
333 // s->curr_bps -= wasted;
336 wasted= 16 - av_log2(show_bits(&s->gb, 17));
337 skip_bits(&s->gb, wasted+1);
338 s->curr_bps -= wasted;
340 if (get_bits1(&s->gb))
343 while (!get_bits1(&s->gb))
345 s->curr_bps -= wasted;
346 av_log(s->avctx, AV_LOG_DEBUG, "%d wasted bits\n", wasted);
349 //FIXME use av_log2 for types
352 av_log(s->avctx, AV_LOG_DEBUG, "coding type: constant\n");
353 tmp = get_sbits(&s->gb, s->curr_bps);
354 for (i = 0; i < s->blocksize; i++)
355 s->decoded[channel][i] = tmp;
359 av_log(s->avctx, AV_LOG_DEBUG, "coding type: verbatim\n");
360 for (i = 0; i < s->blocksize; i++)
361 s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
363 else if ((type >= 8) && (type <= 12))
365 av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
366 if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
371 // av_log(s->avctx, AV_LOG_DEBUG, "coding type: lpc\n");
372 if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
377 av_log(s->avctx, AV_LOG_DEBUG, "invalid coding type\n");
384 for (i = 0; i < s->blocksize; i++)
385 s->decoded[channel][i] <<= wasted;
391 static int decode_frame(FLACContext *s)
393 int blocksize_code, sample_rate_code, sample_size_code, assignment, i;
395 blocksize_code = get_bits(&s->gb, 4);
397 sample_rate_code = get_bits(&s->gb, 4);
399 assignment = get_bits(&s->gb, 4); /* channel assignment */
402 s->order = INDEPENDENT;
403 if (s->channels != assignment+1)
404 av_log(s->avctx, AV_LOG_DEBUG, "channel number and number of assigned channels differ!\n");
405 av_log(s->avctx, AV_LOG_DEBUG, "channels: %d\n", assignment+1);
407 else if (assignment == 8)
409 s->order = LEFT_SIDE;
410 av_log(s->avctx, AV_LOG_DEBUG, "left/side\n");
412 else if (assignment == 9)
414 s->order = RIGHT_SIDE;
415 av_log(s->avctx, AV_LOG_DEBUG, "right/side\n");
417 else if (assignment == 10)
420 av_log(s->avctx, AV_LOG_DEBUG, "mid/side\n");
424 av_log(s->avctx, AV_LOG_DEBUG, "unsupported channel assignment\n");
428 if ((assignment >= 8) && (s->channels != 2))
433 sample_size_code = get_bits(&s->gb, 3);
434 if (sample_size_code != 0)
435 s->bps = sample_size_table[sample_size_code];
437 if ((sample_size_code == 3) || (sample_size_code == 7))
439 av_log(s->avctx, AV_LOG_DEBUG, "invalid sample size code (%d)\n", sample_size_code);
443 if (get_bits1(&s->gb))
445 av_log(s->avctx, AV_LOG_DEBUG, "broken stream, invalid padding\n");
449 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
450 (s->min_blocksize != s->max_blocksize)){
451 if(get_utf8(&s->gb) < 0){
452 av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
456 if(get_utf8(&s->gb) < 0){
457 av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
462 if (blocksize_code == 0)
463 s->blocksize = s->min_blocksize;
464 else if (blocksize_code == 1)
466 else if (blocksize_code <= 5)
467 s->blocksize = 576 << (blocksize_code - 2);
468 else if (blocksize_code == 6)
469 s->blocksize = get_bits(&s->gb, 8)+1;
470 else if (blocksize_code == 7)
471 s->blocksize = get_bits(&s->gb, 16)+1;
472 else if (blocksize_code >= 8)
473 s->blocksize = 256 << (blocksize_code - 8);
475 if(s->blocksize > s->max_blocksize){
476 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", s->blocksize, s->max_blocksize);
480 if (sample_rate_code == 0){
482 }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
483 s->samplerate = sample_rate_table[sample_rate_code];
484 else if (sample_rate_code == 12)
485 s->samplerate = get_bits(&s->gb, 8) * 1000;
486 else if (sample_rate_code == 13)
487 s->samplerate = get_bits(&s->gb, 16);
488 else if (sample_rate_code == 14)
489 s->samplerate = get_bits(&s->gb, 16) * 10;
491 av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
495 skip_bits(&s->gb, 8); /* header crc */
500 for (i = 0; i < s->channels; i++)
502 /* if (s->blocksize != s->last_blocksize)
504 s->decoded[i] = av_realloc(s->decoded[i], sizeof(uint32_t)*s->blocksize);
505 s->residual[i] = av_realloc(s->residual[i], sizeof(uint8_t)*s->blocksize);
507 // av_log(s->avctx, AV_LOG_DEBUG, "decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
508 if (decode_subframe(s, i) < 0)
512 align_get_bits(&s->gb);
515 skip_bits(&s->gb, 16); /* data crc */
520 static int flac_decode_frame(AVCodecContext *avctx,
521 void *data, int *data_size,
522 uint8_t *buf, int buf_size)
524 FLACContext *s = avctx->priv_data;
525 int metadata_last, metadata_type, metadata_size;
526 int tmp = 0, i, j = 0, input_buf_size;
527 int16_t *samples = data, *left, *right;
533 if(s->max_framesize == 0){
534 s->max_framesize= 8192; // should hopefully be enough for the first header
535 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
538 if(1 && s->max_framesize){//FIXME truncated
539 buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
540 input_buf_size= buf_size;
542 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
543 // printf("memmove\n");
544 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
545 s->bitstream_index=0;
547 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
548 buf= &s->bitstream[s->bitstream_index];
549 buf_size += s->bitstream_size;
550 s->bitstream_size= buf_size;
552 if(buf_size < s->max_framesize){
553 // printf("wanna more data ...\n");
554 return input_buf_size;
558 init_get_bits(&s->gb, buf, buf_size*8);
560 /* fLaC signature (be) */
561 if (show_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("fLaC")))
563 skip_bits(&s->gb, 32);
565 av_log(s->avctx, AV_LOG_DEBUG, "STREAM HEADER\n");
567 metadata_last = get_bits(&s->gb, 1);
568 metadata_type = get_bits(&s->gb, 7);
569 metadata_size = get_bits_long(&s->gb, 24);
571 av_log(s->avctx, AV_LOG_DEBUG, " metadata block: flag = %d, type = %d, size = %d\n",
572 metadata_last, metadata_type,
575 switch(metadata_type)
577 case METADATA_TYPE_STREAMINFO:
578 if(metadata_size == 0)
579 av_log(s->avctx, AV_LOG_DEBUG, "size= 0 WTF!?\n");
581 metadata_streaminfo(s);
585 for(i=0; i<metadata_size; i++)
586 skip_bits(&s->gb, 8);
589 } while(!metadata_last);
594 tmp = show_bits(&s->gb, 16);
596 av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
597 while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8)
598 skip_bits(&s->gb, 8);
599 goto end; // we may not have enough bits left to decode a frame, so try next time
601 skip_bits(&s->gb, 16);
602 if (decode_frame(s) < 0)
608 /* fix the channel order here */
609 if (s->order == MID_SIDE)
611 short *left = samples;
612 short *right = samples + s->blocksize;
613 for (i = 0; i < s->blocksize; i += 2)
615 uint32_t x = s->decoded[0][i];
616 uint32_t y = s->decoded[0][i+1];
618 right[i] = x - (y / 2);
619 left[i] = right[i] + y;
621 *data_size = 2 * s->blocksize;
625 for (i = 0; i < s->channels; i++)
630 for (j = 0; j < s->blocksize; j++)
631 samples[(s->blocksize*i)+j] = s->decoded[i][j];
636 for (j = 0; j < s->blocksize; j++)
637 samples[(s->blocksize*i)+j] = s->decoded[0][j];
639 for (j = 0; j < s->blocksize; j++)
640 samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
643 // av_log(s->avctx, AV_LOG_DEBUG, "mid-side unsupported\n");
645 *data_size += s->blocksize;
652 for (j = 0; j < s->blocksize; j++)
654 for (i = 0; i < s->channels; i++)
655 *(samples++) = s->decoded[i][j];
659 assert(s->channels == 2);
660 for (i = 0; i < s->blocksize; i++)
662 *(samples++) = s->decoded[0][i];
663 *(samples++) = s->decoded[0][i] - s->decoded[1][i];
667 assert(s->channels == 2);
668 for (i = 0; i < s->blocksize; i++)
670 *(samples++) = s->decoded[0][i] + s->decoded[1][i];
671 *(samples++) = s->decoded[1][i];
675 assert(s->channels == 2);
676 for (i = 0; i < s->blocksize; i++)
679 mid = s->decoded[0][i];
680 side = s->decoded[1][i];
685 *(samples++) = (mid + side) >> 1;
686 *(samples++) = (mid - side) >> 1;
692 *data_size = (int8_t *)samples - (int8_t *)data;
693 av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
695 // s->last_blocksize = s->blocksize;
697 i= (get_bits_count(&s->gb)+7)/8;;
699 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
703 if(s->bitstream_size){
704 s->bitstream_index += i;
705 s->bitstream_size -= i;
706 return input_buf_size;
711 static int flac_decode_close(AVCodecContext *avctx)
713 FLACContext *s = avctx->priv_data;
716 for (i = 0; i < s->channels; i++)
718 av_freep(&s->decoded[i]);
719 av_freep(&s->residual[i]);
721 av_freep(&s->bitstream);
726 AVCodec flac_decoder = {