flac decoder by (Alex Beregszaszi <alex at fsn dot hu>)
[libav.git] / libavcodec / flac.c
CommitLineData
4f52c312
MN
1/*
2 * FLAC (Free Lossless Audio Codec) decoder
3 * Copyright (c) 2003 Alex Beregszaszi
4 *
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.
9 *
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.
14 *
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
18 */
19
20/**
21 * @file flac.c
22 * FLAC (Free Lossless Audio Codec) decoder
23 * @author Alex Beregszaszi
24 */
25
26#include "avcodec.h"
27#include "golomb.h"
28
29#define MAX_CHANNELS 8
30#define MAX_BLOCKSIZE 65535
31
32enum channel_order {
33 INDEPENDENT,
34 LEFT_SIDE,
35 RIGHT_SIDE,
36 MID_SIDE,
37};
38
39typedef struct FLACContext {
40 AVCodecContext *avctx;
41 GetBitContext gb;
42
43 int min_blocksize, max_blocksize;
44 int min_framesize, max_framesize;
45 int samplerate, channels;
46 int blocksize, last_blocksize;
47 int bps, curr_bps;
48 enum channel_order order;
49
50 uint8_t *residual[MAX_CHANNELS];
51 uint32_t *decoded[MAX_CHANNELS];
52} FLACContext;
53
54#define METADATA_TYPE_STREAMINFO 0
55
56static int sample_rate_table[] =
57{ 0, 0, 0, 0,
58 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
59 0, 0, 0, 0 };
60
61static int sample_size_table[] =
62{ 0, 8, 12, 0, 16, 20, 24, 0 };
63
64static uint64_t get_uvlc(GetBitContext *gb, int is64)
65{
66 uint64_t val = 0;
67 int i = 0;
68
69 while(i++ < 5+is64)
70 {
71 const int tmp = get_bits(gb, 8);
72
73 if (tmp & 0x80)
74 val = (val << 7) + tmp - 0x80;
75 else
76 return (val << 7) + tmp;
77 }
78 return -1;
79}
80
81static int flac_decode_init(AVCodecContext * avctx)
82{
83 return 0;
84}
85
86static void dump_headers(FLACContext *s)
87{
88 printf(" Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
89 printf(" Framesize: %d .. %d\n", s->min_framesize, s->max_framesize);
90 printf(" Samplerate: %d\n", s->samplerate);
91 printf(" Channels: %d\n", s->channels);
92 printf(" Bits: %d\n", s->bps);
93}
94
95static void metadata_streaminfo(FLACContext *s)
96{
97 int i;
98
99 /* mandatory streaminfo */
100 s->min_blocksize = get_bits(&s->gb, 16);
101 s->max_blocksize = get_bits(&s->gb, 16);
102
103 s->min_framesize = get_bits_long(&s->gb, 24);
104 s->max_framesize = get_bits_long(&s->gb, 24);
105
106 s->samplerate = get_bits_long(&s->gb, 20);
107 s->channels = get_bits(&s->gb, 3) + 1;
108 s->bps = get_bits(&s->gb, 5) + 1;
109
110 s->avctx->channels = s->channels;
111 s->avctx->sample_rate = s->samplerate;
112
113 skip_bits(&s->gb, 36); /* total num of samples */
114
115 skip_bits(&s->gb, 64); /* md5 sum */
116 skip_bits(&s->gb, 64); /* md5 sum */
117
118 for (i = 0; i < s->channels; i++)
119 {
120 s->decoded[i] = av_realloc(s->decoded[i], sizeof(uint32_t)*s->max_blocksize);
121 s->residual[i] = av_realloc(s->residual[i], sizeof(uint8_t)*s->max_blocksize);
122 }
123}
124
125static int decode_residuals(FLACContext *s, int channel, int pred_order)
126{
127 int i, tmp, partition, method_type, rice_order;
128 int sample = 0, samples;
129
130 method_type = get_bits(&s->gb, 2);
131 if (method_type != 0)
132 return -1;
133
134 rice_order = get_bits(&s->gb, 4);
135
136 samples = (rice_order > 0) ?
137 (s->blocksize >> rice_order) : (s->blocksize - pred_order);
138
139 for (partition = 0; partition < (1 << rice_order); partition++)
140 {
141 tmp = get_bits(&s->gb, 4);
142 if (tmp == 0)
143 {
144 i = (!rice_order || partition) ? 0 : pred_order;
145 for (; i < samples; i++, sample++)
146 s->residual[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, 0, 0);
147 printf("zero k\n");
148 }
149 else if (tmp == 15)
150 {
151 printf("fixed len partition\n");
152 tmp = get_bits(&s->gb, 5);
153 i = (!rice_order || partition) ? 0 : pred_order;
154 for (; i < samples; i++, sample++)
155 s->residual[channel][sample] = get_bits(&s->gb, tmp);
156 }
157 else
158 {
159// printf("rice coded partition\n");
160#if 1
161 i = (!rice_order || partition) ? 0 : pred_order;
162 for (; i < samples; i++, sample++)
163 s->residual[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, 0, 0);
164#else
165 i = ((!rice_order || partition) ? samples : samples - pred_order) + sample;
166 for (; sample < i; sample++)
167 s->residual[channel][sample] = get_ur_golomb(&s->gb, tmp, 0, 0);
168// s->residual[channel][sample] = get_se_golomb(&s->gb);
169#endif
170 }
171 }
172
173 printf("partitions: %d, samples: %d\n", 1 << rice_order, sample);
174
175 return 0;
176}
177
178static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
179{
180 int i;
181
182 printf(" SUBFRAME FIXED\n");
183
184 /* warm up samples */
185 printf(" warm up samples: %d\n", pred_order);
186
187 for (i = 0; i < pred_order; i++)
188 {
189 s->decoded[channel][i] = get_bits(&s->gb, s->curr_bps);
190 printf(" %d: %d\n", i, s->decoded[channel][i]);
191 }
192
193 if (decode_residuals(s, channel, pred_order) < 0)
194 return -1;
195
196 switch(pred_order)
197 {
198 case 0:
199 for (i = pred_order; i < s->blocksize; i++)
200 s->decoded[channel][i] = s->residual[channel][i];
201 break;
202 case 1:
203 for (i = pred_order; i < s->blocksize; i++)
204 s->decoded[channel][i] = s->residual[channel][i] +
205 s->decoded[channel][i-1];
206 break;
207 case 2:
208 for (i = pred_order; i < s->blocksize; i++)
209 s->decoded[channel][i] = s->residual[channel][i] +
210 (s->decoded[channel][i-1] << 1) -
211 s->decoded[channel][i-2];
212 break;
213 case 3:
214 for (i = pred_order; i < s->blocksize; i++)
215 s->decoded[channel][i] = s->residual[channel][i] +
216 (((s->decoded[channel][i-1] -
217 s->decoded[channel][i-2]) << 1) +
218 (s->decoded[channel][i-1] -
219 s->decoded[channel][i-2])) +
220 s->decoded[channel][i-3];
221 break;
222 case 4:
223 for (i = pred_order; i < s->blocksize; i++)
224 s->decoded[channel][i] = s->residual[channel][i] +
225 ((s->decoded[channel][i-1] +
226 s->decoded[channel][i-3]) << 2) -
227 ((s->decoded[channel][i-2] << 2) +
228 (s->decoded[channel][i-2] << 1)) -
229 s->decoded[channel][i-4];
230 break;
231 }
232
233 return 0;
234}
235
236static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
237{
238 int sum, i, j;
239 int coeff_prec, qlevel;
240 int coeffs[pred_order];
241
242 printf(" SUBFRAME LPC\n");
243
244 /* warm up samples */
245 printf(" warm up samples: %d\n", pred_order);
246
247 for (i = 0; i < pred_order; i++)
248 {
249 s->decoded[channel][i] = get_bits(&s->gb, s->curr_bps);
250 printf(" %d: %d\n", i, s->decoded[channel][i]);
251 }
252
253 coeff_prec = get_bits(&s->gb, 4) + 1;
254 if (coeff_prec == 16)
255 {
256 printf("invalid coeff precision\n");
257 return -1;
258 }
259 printf(" qlp coeff prec: %d\n", coeff_prec);
260 qlevel = get_bits(&s->gb, 5);
261 printf(" quant level: %d\n", qlevel);
262
263 for (i = 0; i < pred_order; i++)
264 {
265 coeffs[i] = get_bits(&s->gb, coeff_prec);
266 printf(" %d: %d\n", i, coeffs[i]);
267 }
268
269 if (decode_residuals(s, channel, pred_order) < 0)
270 return -1;
271
272 for (i = pred_order; i < s->blocksize; i++)
273 {
274 sum = 0;
275 for (j = 0; j < pred_order; j++)
276 sum += coeffs[j] * s->decoded[channel][i-j-1];
277 s->decoded[channel][i] = s->residual[channel][i] + (sum >> qlevel);
278 }
279
280 return 0;
281}
282
283static inline int decode_subframe(FLACContext *s, int channel)
284{
285 int type, wasted = 0;
286 int i, tmp;
287
288 s->curr_bps = s->bps;
289
290 if (get_bits1(&s->gb))
291 {
292 printf("invalid subframe padding\n");
293 return -1;
294 }
295 type = get_bits(&s->gb, 6);
296// wasted = get_bits1(&s->gb);
297
298// if (wasted)
299// {
300// while (!get_bits1(&s->gb))
301// wasted++;
302// if (wasted)
303// wasted++;
304// s->curr_bps -= wasted;
305// }
306
307 if (get_bits1(&s->gb))
308 {
309 wasted = 1;
310 while (!get_bits1(&s->gb))
311 wasted++;
312 s->curr_bps -= wasted;
313 }
314
315 if (type == 0)
316 {
317 printf("coding type: constant\n");
318 tmp = get_bits(&s->gb, s->curr_bps);
319 for (i = 0; i < s->blocksize; i++)
320 s->decoded[channel][i] = tmp;
321 }
322 else if (type == 1)
323 {
324 printf("coding type: verbatim\n");
325 for (i = 0; i < s->blocksize; i++)
326 s->decoded[channel][i] = get_bits(&s->gb, s->curr_bps);
327 }
328 else if ((type >= 8) && (type <= 12))
329 {
330 printf("coding type: fixed\n");
331 if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
332 return -1;
333 }
334 else if (type >= 32)
335 {
336 printf("coding type: lpc\n");
337 if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
338 return -1;
339 }
340 else
341 {
342 printf("invalid coding type\n");
343 return -1;
344 }
345
346 if (wasted)
347 {
348 int i;
349 for (i = 0; i < s->blocksize; i++)
350 s->decoded[channel][i] <<= wasted;
351 }
352
353 return 0;
354}
355
356static int decode_frame(FLACContext *s)
357{
358 int blocksize_code, sample_rate_code, sample_size_code, assignment, i;
359
360 blocksize_code = get_bits(&s->gb, 4);
361 if (blocksize_code == 0)
362 s->blocksize = s->min_blocksize;
363 else if (blocksize_code == 1)
364 s->blocksize = 192;
365 else if (blocksize_code <= 5)
366 s->blocksize = 576 << (blocksize_code - 2);
367 else if (blocksize_code >= 8)
368 s->blocksize = 256 << (blocksize_code - 8);
369
370 sample_rate_code = get_bits(&s->gb, 4);
371 if ((sample_rate_code > 3) && (sample_rate_code < 12))
372 s->samplerate = sample_rate_table[sample_rate_code];
373
374 assignment = get_bits(&s->gb, 4); /* channel assignment */
375 if (assignment < 8)
376 {
377 s->order = INDEPENDENT;
378 if (s->channels != assignment+1)
379 printf("channel number and number of assigned channels differ!\n");
380 printf("channels: %d\n", assignment+1);
381 }
382 else if (assignment == 8)
383 {
384 s->order = LEFT_SIDE;
385 printf("left/side\n");
386 }
387 else if (assignment == 9)
388 {
389 s->order = RIGHT_SIDE;
390 printf("right/side\n");
391 }
392 else if (assignment == 10)
393 {
394 s->order = MID_SIDE;
395 printf("mid/side\n");
396 }
397 else
398 {
399 printf("unsupported channel assignment\n");
400 return -1;
401 }
402
403 if ((assignment >= 8) && (s->channels != 2))
404 {
405 return -1;
406 }
407
408 sample_size_code = get_bits(&s->gb, 3);
409 if (s->bps != 0)
410 s->bps = sample_size_table[sample_size_code];
411
412 if ((sample_size_code == 3) || (sample_size_code == 7))
413 {
414 printf("invalid sample size code (%d)\n", sample_size_code);
415 return -1;
416 }
417
418 if (get_bits1(&s->gb))
419 {
420 printf("broken stream, invalid padding\n");
421// return -1;
422 }
423
424 if (((blocksize_code == 6) || (blocksize_code == 7)) &&
425 (s->min_blocksize != s->max_blocksize))
426 {
427 get_uvlc(&s->gb, 1);
428 }
429 else
430 get_uvlc(&s->gb, 0);
431
432 if (blocksize_code == 6)
433 s->blocksize = get_bits(&s->gb, 8)+1;
434 if (blocksize_code == 7)
435 s->blocksize = get_bits(&s->gb, 16)+1;
436
437 if ((sample_rate_code > 11) && (sample_rate_code < 15))
438 {
439 switch(sample_rate_code)
440 {
441 case 12:
442 s->samplerate = get_bits(&s->gb, 8) * 1000;
443 break;
444 case 13:
445 s->samplerate = get_bits(&s->gb, 16);
446 break;
447 case 14:
448 s->samplerate = get_bits(&s->gb, 16) * 10;
449 break;
450 }
451 }
452
453 skip_bits(&s->gb, 8); /* header crc */
454
455 dump_headers(s);
456
457 /* subframes */
458 for (i = 0; i < s->channels; i++)
459 {
460 if (s->blocksize != s->last_blocksize)
461 {
462 s->decoded[i] = av_realloc(s->decoded[i], sizeof(uint32_t)*s->blocksize);
463 s->residual[i] = av_realloc(s->residual[i], sizeof(uint8_t)*s->blocksize);
464 }
465 printf("decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
466 if (decode_subframe(s, i) < 0)
467 return -1;
468 }
469
470 align_get_bits(&s->gb);
471
472 /* frame footer */
473 skip_bits(&s->gb, 16); /* data crc */
474
475 return 0;
476}
477
478static int flac_decode_frame(AVCodecContext *avctx,
479 void *data, int *data_size,
480 uint8_t *buf, int buf_size)
481{
482 FLACContext *s = avctx->priv_data;
483 int metadata_flag, metadata_type, metadata_size;
484 int tmp = 0, i, j = 0;
485 int16_t *samples = data, *left, *right;
486
487 *data_size = 0;
488
489 s->avctx = avctx;
490
491 init_get_bits(&s->gb, buf, buf_size*8);
492
493 /* fLaC signature (be) */
494 if (get_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("fLaC")))
495 {
496 printf("STREAM HEADER\n");
497 do {
498 metadata_flag = get_bits(&s->gb, 1);
499 metadata_type = get_bits(&s->gb, 7);
500 metadata_size = get_bits_long(&s->gb, 24);
501
502 printf(" metadata block: flag = %d, type = %d, size = %d\n",
503 metadata_flag, metadata_type,
504 metadata_size);
505
506 switch(metadata_type)
507 {
508 case METADATA_TYPE_STREAMINFO:
509 metadata_streaminfo(s);
510 dump_headers(s);
511 break;
512 default:
513 while ((metadata_size -= 8) > 0)
514 skip_bits(&s->gb, 8);
515 }
516 } while(metadata_flag != 1);
517 }
518 else
519 {
520 init_get_bits(&s->gb, buf, buf_size*8);
521 tmp = get_bits(&s->gb, 16);
522 if (tmp == 0xfff8)
523 printf("FRAME HEADER\n");
524
525 if (decode_frame(s) < 0)
526 return -1;
527 }
528
529#if 0
530 /* fix the channel order here */
531 if (s->order == MID_SIDE)
532 {
533 short *left = samples;
534 short *right = samples + s->blocksize;
535 for (i = 0; i < s->blocksize; i += 2)
536 {
537 uint32_t x = s->decoded[0][i];
538 uint32_t y = s->decoded[0][i+1];
539
540 right[i] = x - (y / 2);
541 left[i] = right[i] + y;
542 }
543 *data_size = 2 * s->blocksize;
544 }
545 else
546 {
547 for (i = 0; i < s->channels; i++)
548 {
549 switch(s->order)
550 {
551 case INDEPENDENT:
552 for (j = 0; j < s->blocksize; j++)
553 samples[(s->blocksize*i)+j] = s->decoded[i][j];
554 break;
555 case LEFT_SIDE:
556 case RIGHT_SIDE:
557 if (i == 0)
558 for (j = 0; j < s->blocksize; j++)
559 samples[(s->blocksize*i)+j] = s->decoded[0][j];
560 else
561 for (j = 0; j < s->blocksize; j++)
562 samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
563 break;
564// case MID_SIDE:
565// printf("mid-side unsupported\n");
566 }
567 *data_size += s->blocksize;
568 }
569 }
570#else
571 switch(s->order)
572 {
573 case INDEPENDENT:
574 for (i = 0; i < s->channels; i++)
575 {
576 for (j = 0; j < s->blocksize; j++)
577 *(samples++) = s->decoded[i][j];
578 *data_size += s->blocksize;
579 }
580 break;
581 case LEFT_SIDE:
582 assert(s->channels == 2);
583 for (i = 0; i < s->blocksize; i++)
584 {
585 *(samples++) = s->decoded[0][i];
586 *(samples++) = s->decoded[0][i] - s->decoded[1][i];
587 }
588 *data_size = 2*s->blocksize;
589 break;
590 case RIGHT_SIDE:
591 assert(s->channels == 2);
592 for (i = 0; i < s->blocksize; i++)
593 {
594 *(samples++) = s->decoded[0][i] + s->decoded[1][i];
595 *(samples++) = s->decoded[1][i];
596 }
597 *data_size = 2*s->blocksize;
598 break;
599 case MID_SIDE:
600 assert(s->channels == 2);
601 for (i = 0; i < s->blocksize; i++)
602 {
603 int16_t mid, side;
604 mid = s->decoded[0][i];
605 side = s->decoded[1][i];
606
607 mid <<= 1;
608 if (side & 1)
609 mid++;
610 *(samples++) = (mid + side) >> 1;
611 *(samples++) = (mid - side) >> 1;
612 }
613 *data_size = 2*s->blocksize;
614 break;
615 }
616#endif
617
618// *data_size = (int8_t *)samples - (int8_t *)data;
619 printf("data size: %d\n", *data_size);
620
621 s->last_blocksize = s->blocksize;
622
623 return (get_bits_count(&s->gb)+7)/8;
624}
625
626static int flac_decode_close(AVCodecContext *avctx)
627{
628 FLACContext *s = avctx->priv_data;
629 int i;
630
631 for (i = 0; i < s->channels; i++)
632 {
633 if (s->decoded[i])
634 av_free(s->decoded[i]);
635 if (s->residual[i])
636 av_free(s->residual[i]);
637 }
638
639 return 0;
640}
641
642AVCodec flac_decoder = {
643 "flac",
644 CODEC_TYPE_AUDIO,
645 CODEC_ID_FLAC,
646 sizeof(FLACContext),
647 flac_decode_init,
648 NULL,
649 flac_decode_close,
650 flac_decode_frame,
651};