replace FIR with finite differences.
[libav.git] / libavcodec / flac.c
CommitLineData
4f52c312
MN
1/*
2 * FLAC (Free Lossless Audio Codec) decoder
3 * Copyright (c) 2003 Alex Beregszaszi
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
4f52c312
MN
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
4f52c312 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
4f52c312
MN
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4f52c312
MN
20 */
21
22/**
23 * @file flac.c
24 * FLAC (Free Lossless Audio Codec) decoder
25 * @author Alex Beregszaszi
9eda2f94
MM
26 *
27 * For more information on the FLAC format, visit:
28 * http://flac.sourceforge.net/
29 *
30 * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed
31 * through, starting from the initial 'fLaC' signature; or by passing the
32 * 34-byte streaminfo structure through avctx->extradata[_size] followed
33 * by data starting with the 0xFFF8 marker.
4f52c312 34 */
115329f1 35
ac2570a8 36#include <limits.h>
115329f1 37
75c998a2 38#define ALT_BITSTREAM_READER
4f52c312 39#include "avcodec.h"
caa336b4 40#include "bitstream.h"
4f52c312 41#include "golomb.h"
b865838e 42#include "crc.h"
4f52c312 43
ac2570a8
MN
44#undef NDEBUG
45#include <assert.h>
46
4f52c312
MN
47#define MAX_CHANNELS 8
48#define MAX_BLOCKSIZE 65535
9eda2f94 49#define FLAC_STREAMINFO_SIZE 34
4f52c312 50
9d656110 51enum decorrelation_type {
4f52c312
MN
52 INDEPENDENT,
53 LEFT_SIDE,
54 RIGHT_SIDE,
55 MID_SIDE,
56};
57
58typedef struct FLACContext {
59 AVCodecContext *avctx;
60 GetBitContext gb;
61
62 int min_blocksize, max_blocksize;
63 int min_framesize, max_framesize;
64 int samplerate, channels;
ac2570a8 65 int blocksize/*, last_blocksize*/;
4f52c312 66 int bps, curr_bps;
9d656110 67 enum decorrelation_type decorrelation;
4f52c312 68
ac2570a8
MN
69 int32_t *decoded[MAX_CHANNELS];
70 uint8_t *bitstream;
71 int bitstream_size;
72 int bitstream_index;
f038fe8b 73 unsigned int allocated_bitstream_size;
4f52c312
MN
74} FLACContext;
75
76#define METADATA_TYPE_STREAMINFO 0
77
78static int sample_rate_table[] =
79{ 0, 0, 0, 0,
80 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
115329f1 81 0, 0, 0, 0 };
4f52c312 82
115329f1 83static int sample_size_table[] =
4f52c312
MN
84{ 0, 8, 12, 0, 16, 20, 24, 0 };
85
0496a034 86static int blocksize_table[] = {
115329f1
DB
87 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
88256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
0496a034
MN
89};
90
9d82b0dd
MN
91static int64_t get_utf8(GetBitContext *gb){
92 int64_t val;
93 GET_UTF8(val, get_bits(gb, 8), return -1;)
ac2570a8 94 return val;
4f52c312
MN
95}
96
9eda2f94 97static void metadata_streaminfo(FLACContext *s);
17c90b9d
AJ
98static void allocate_buffers(FLACContext *s);
99static int metadata_parse(FLACContext *s);
9eda2f94 100
4f52c312
MN
101static int flac_decode_init(AVCodecContext * avctx)
102{
9eda2f94
MM
103 FLACContext *s = avctx->priv_data;
104 s->avctx = avctx;
105
17c90b9d
AJ
106 if (avctx->extradata_size > 4) {
107 /* initialize based on the demuxer-supplied streamdata header */
9eda2f94 108 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
17c90b9d
AJ
109 if (avctx->extradata_size == FLAC_STREAMINFO_SIZE) {
110 metadata_streaminfo(s);
111 allocate_buffers(s);
112 } else {
113 metadata_parse(s);
114 }
9eda2f94
MM
115 }
116
4f52c312
MN
117 return 0;
118}
119
120static void dump_headers(FLACContext *s)
121{
ac2570a8
MN
122 av_log(s->avctx, AV_LOG_DEBUG, " Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
123 av_log(s->avctx, AV_LOG_DEBUG, " Framesize: %d .. %d\n", s->min_framesize, s->max_framesize);
124 av_log(s->avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
125 av_log(s->avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
126 av_log(s->avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
4f52c312
MN
127}
128
ac2570a8 129static void allocate_buffers(FLACContext *s){
4f52c312
MN
130 int i;
131
ac2570a8
MN
132 assert(s->max_blocksize);
133
134 if(s->max_framesize == 0 && s->max_blocksize){
135 s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8; //FIXME header overhead
136 }
137
138 for (i = 0; i < s->channels; i++)
139 {
140 s->decoded[i] = av_realloc(s->decoded[i], sizeof(int32_t)*s->max_blocksize);
ac2570a8
MN
141 }
142
143 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
ac2570a8
MN
144}
145
146static void metadata_streaminfo(FLACContext *s)
147{
4f52c312
MN
148 /* mandatory streaminfo */
149 s->min_blocksize = get_bits(&s->gb, 16);
150 s->max_blocksize = get_bits(&s->gb, 16);
151
152 s->min_framesize = get_bits_long(&s->gb, 24);
153 s->max_framesize = get_bits_long(&s->gb, 24);
115329f1 154
4f52c312
MN
155 s->samplerate = get_bits_long(&s->gb, 20);
156 s->channels = get_bits(&s->gb, 3) + 1;
157 s->bps = get_bits(&s->gb, 5) + 1;
115329f1 158
4f52c312
MN
159 s->avctx->channels = s->channels;
160 s->avctx->sample_rate = s->samplerate;
161
162 skip_bits(&s->gb, 36); /* total num of samples */
115329f1 163
4f52c312
MN
164 skip_bits(&s->gb, 64); /* md5 sum */
165 skip_bits(&s->gb, 64); /* md5 sum */
115329f1 166
17c90b9d
AJ
167 dump_headers(s);
168}
169
170/**
171 * Parse a list of metadata blocks. This list of blocks must begin with
172 * the fLaC marker.
173 * @param s the flac decoding context containing the gb bit reader used to
174 * parse metadata
175 * @return 1 if some metadata was read, 0 if no fLaC marker was found
176 */
177static int metadata_parse(FLACContext *s)
178{
179 int i, metadata_last, metadata_type, metadata_size, streaminfo_updated=0;
180
181 if (show_bits_long(&s->gb, 32) == MKBETAG('f','L','a','C')) {
182 skip_bits(&s->gb, 32);
183
184 av_log(s->avctx, AV_LOG_DEBUG, "STREAM HEADER\n");
185 do {
5fc32c27 186 metadata_last = get_bits1(&s->gb);
17c90b9d
AJ
187 metadata_type = get_bits(&s->gb, 7);
188 metadata_size = get_bits_long(&s->gb, 24);
189
190 av_log(s->avctx, AV_LOG_DEBUG,
191 " metadata block: flag = %d, type = %d, size = %d\n",
192 metadata_last, metadata_type, metadata_size);
193 if (metadata_size) {
194 switch (metadata_type) {
195 case METADATA_TYPE_STREAMINFO:
196 metadata_streaminfo(s);
197 streaminfo_updated = 1;
198 break;
199
200 default:
201 for (i=0; i<metadata_size; i++)
202 skip_bits(&s->gb, 8);
203 }
204 }
205 } while (!metadata_last);
206
207 if (streaminfo_updated)
208 allocate_buffers(s);
209 return 1;
210 }
211 return 0;
4f52c312
MN
212}
213
214static int decode_residuals(FLACContext *s, int channel, int pred_order)
215{
216 int i, tmp, partition, method_type, rice_order;
217 int sample = 0, samples;
218
219 method_type = get_bits(&s->gb, 2);
ac2570a8
MN
220 if (method_type != 0){
221 av_log(s->avctx, AV_LOG_DEBUG, "illegal residual coding method %d\n", method_type);
4f52c312 222 return -1;
ac2570a8 223 }
115329f1 224
4f52c312
MN
225 rice_order = get_bits(&s->gb, 4);
226
ac2570a8 227 samples= s->blocksize >> rice_order;
5484dad7
RD
228 if (pred_order > samples) {
229 av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n", pred_order, samples);
230 return -1;
231 }
4f52c312 232
115329f1 233 sample=
ac2570a8 234 i= pred_order;
4f52c312
MN
235 for (partition = 0; partition < (1 << rice_order); partition++)
236 {
237 tmp = get_bits(&s->gb, 4);
ac2570a8 238 if (tmp == 15)
4f52c312 239 {
ac2570a8 240 av_log(s->avctx, AV_LOG_DEBUG, "fixed len partition\n");
4f52c312 241 tmp = get_bits(&s->gb, 5);
4f52c312 242 for (; i < samples; i++, sample++)
4fd12598 243 s->decoded[channel][sample] = get_sbits(&s->gb, tmp);
4f52c312
MN
244 }
245 else
246 {
ac2570a8
MN
247// av_log(s->avctx, AV_LOG_DEBUG, "rice coded partition k=%d\n", tmp);
248 for (; i < samples; i++, sample++){
4fd12598 249 s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
ac2570a8 250 }
4f52c312 251 }
ac2570a8 252 i= 0;
4f52c312
MN
253 }
254
ac2570a8 255// av_log(s->avctx, AV_LOG_DEBUG, "partitions: %d, samples: %d\n", 1 << rice_order, sample);
4f52c312
MN
256
257 return 0;
115329f1 258}
4f52c312
MN
259
260static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
261{
08965b22
LM
262 const int blocksize = s->blocksize;
263 int32_t *decoded = s->decoded[channel];
264 int a, b, c, d, i;
115329f1 265
9d656110 266// av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME FIXED\n");
115329f1 267
4f52c312 268 /* warm up samples */
9d656110 269// av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
115329f1 270
4f52c312
MN
271 for (i = 0; i < pred_order; i++)
272 {
08965b22 273 decoded[i] = get_sbits(&s->gb, s->curr_bps);
ac2570a8 274// av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
4f52c312 275 }
115329f1 276
4f52c312
MN
277 if (decode_residuals(s, channel, pred_order) < 0)
278 return -1;
279
08965b22
LM
280 a = decoded[pred_order-1];
281 b = a - decoded[pred_order-2];
282 c = b - decoded[pred_order-2] + decoded[pred_order-3];
283 d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
284
4f52c312
MN
285 switch(pred_order)
286 {
287 case 0:
4f52c312
MN
288 break;
289 case 1:
08965b22
LM
290 for (i = pred_order; i < blocksize; i++)
291 decoded[i] = a += decoded[i];
4f52c312
MN
292 break;
293 case 2:
08965b22
LM
294 for (i = pred_order; i < blocksize; i++)
295 decoded[i] = a += b += decoded[i];
4f52c312
MN
296 break;
297 case 3:
08965b22
LM
298 for (i = pred_order; i < blocksize; i++)
299 decoded[i] = a += b += c += decoded[i];
4f52c312
MN
300 break;
301 case 4:
08965b22
LM
302 for (i = pred_order; i < blocksize; i++)
303 decoded[i] = a += b += c += d += decoded[i];
4f52c312 304 break;
ac2570a8
MN
305 default:
306 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
307 return -1;
4f52c312 308 }
ac2570a8 309
4f52c312
MN
310 return 0;
311}
312
313static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
314{
0187178e 315 int i, j;
4f52c312
MN
316 int coeff_prec, qlevel;
317 int coeffs[pred_order];
115329f1 318
ac2570a8 319// av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME LPC\n");
115329f1 320
4f52c312 321 /* warm up samples */
ac2570a8 322// av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
115329f1 323
4f52c312
MN
324 for (i = 0; i < pred_order; i++)
325 {
ac2570a8
MN
326 s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
327// av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
4f52c312 328 }
115329f1 329
4f52c312
MN
330 coeff_prec = get_bits(&s->gb, 4) + 1;
331 if (coeff_prec == 16)
332 {
ac2570a8 333 av_log(s->avctx, AV_LOG_DEBUG, "invalid coeff precision\n");
4f52c312
MN
334 return -1;
335 }
9d656110 336// av_log(s->avctx, AV_LOG_DEBUG, " qlp coeff prec: %d\n", coeff_prec);
ac2570a8 337 qlevel = get_sbits(&s->gb, 5);
9d656110
MN
338// av_log(s->avctx, AV_LOG_DEBUG, " quant level: %d\n", qlevel);
339 if(qlevel < 0){
340 av_log(s->avctx, AV_LOG_DEBUG, "qlevel %d not supported, maybe buggy stream\n", qlevel);
341 return -1;
342 }
343
4f52c312
MN
344 for (i = 0; i < pred_order; i++)
345 {
ac2570a8
MN
346 coeffs[i] = get_sbits(&s->gb, coeff_prec);
347// av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, coeffs[i]);
4f52c312 348 }
115329f1 349
4f52c312
MN
350 if (decode_residuals(s, channel, pred_order) < 0)
351 return -1;
352
0187178e
LB
353 if (s->bps > 16) {
354 int64_t sum;
355 for (i = pred_order; i < s->blocksize; i++)
356 {
357 sum = 0;
358 for (j = 0; j < pred_order; j++)
359 sum += (int64_t)coeffs[j] * s->decoded[channel][i-j-1];
360 s->decoded[channel][i] += sum >> qlevel;
361 }
362 } else {
bd49d4fd 363 for (i = pred_order; i < s->blocksize-1; i += 2)
0187178e 364 {
bd49d4fd
LM
365 int c = coeffs[pred_order-1];
366 int s0 = c * s->decoded[channel][i-pred_order];
367 int s1 = 0;
368 for (j = pred_order-1; j > 0; j--)
369 {
370 int d = s->decoded[channel][i-j];
371 s1 += c*d;
372 c = coeffs[j-1];
373 s0 += c*d;
374 }
375 s0 = s->decoded[channel][i] += s0 >> qlevel;
376 s1 += c * s0;
377 s->decoded[channel][i+1] += s1 >> qlevel;
378 }
379 if (i < s->blocksize)
380 {
381 int sum = 0;
0187178e
LB
382 for (j = 0; j < pred_order; j++)
383 sum += coeffs[j] * s->decoded[channel][i-j-1];
384 s->decoded[channel][i] += sum >> qlevel;
385 }
4f52c312 386 }
115329f1 387
4f52c312
MN
388 return 0;
389}
390
391static inline int decode_subframe(FLACContext *s, int channel)
392{
393 int type, wasted = 0;
394 int i, tmp;
115329f1 395
4f52c312 396 s->curr_bps = s->bps;
ac2570a8 397 if(channel == 0){
9d656110 398 if(s->decorrelation == RIGHT_SIDE)
ac2570a8
MN
399 s->curr_bps++;
400 }else{
9d656110 401 if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
ac2570a8
MN
402 s->curr_bps++;
403 }
404
4f52c312
MN
405 if (get_bits1(&s->gb))
406 {
1cef211d 407 av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
4f52c312
MN
408 return -1;
409 }
410 type = get_bits(&s->gb, 6);
411// wasted = get_bits1(&s->gb);
115329f1 412
4f52c312
MN
413// if (wasted)
414// {
415// while (!get_bits1(&s->gb))
416// wasted++;
417// if (wasted)
418// wasted++;
419// s->curr_bps -= wasted;
420// }
ac2570a8
MN
421#if 0
422 wasted= 16 - av_log2(show_bits(&s->gb, 17));
423 skip_bits(&s->gb, wasted+1);
424 s->curr_bps -= wasted;
425#else
4f52c312
MN
426 if (get_bits1(&s->gb))
427 {
428 wasted = 1;
429 while (!get_bits1(&s->gb))
430 wasted++;
431 s->curr_bps -= wasted;
ac2570a8 432 av_log(s->avctx, AV_LOG_DEBUG, "%d wasted bits\n", wasted);
4f52c312 433 }
ac2570a8
MN
434#endif
435//FIXME use av_log2 for types
4f52c312
MN
436 if (type == 0)
437 {
ac2570a8
MN
438 av_log(s->avctx, AV_LOG_DEBUG, "coding type: constant\n");
439 tmp = get_sbits(&s->gb, s->curr_bps);
4f52c312
MN
440 for (i = 0; i < s->blocksize; i++)
441 s->decoded[channel][i] = tmp;
442 }
443 else if (type == 1)
444 {
ac2570a8 445 av_log(s->avctx, AV_LOG_DEBUG, "coding type: verbatim\n");
4f52c312 446 for (i = 0; i < s->blocksize; i++)
ac2570a8 447 s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
4f52c312
MN
448 }
449 else if ((type >= 8) && (type <= 12))
450 {
9d656110 451// av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
4f52c312
MN
452 if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
453 return -1;
454 }
455 else if (type >= 32)
456 {
ac2570a8 457// av_log(s->avctx, AV_LOG_DEBUG, "coding type: lpc\n");
4f52c312
MN
458 if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
459 return -1;
460 }
461 else
462 {
1cef211d 463 av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
4f52c312
MN
464 return -1;
465 }
115329f1 466
4f52c312
MN
467 if (wasted)
468 {
469 int i;
470 for (i = 0; i < s->blocksize; i++)
471 s->decoded[channel][i] <<= wasted;
472 }
473
474 return 0;
475}
476
ac66834c 477static int decode_frame(FLACContext *s, int alloc_data_size)
4f52c312 478{
9d656110 479 int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
0496a034 480 int decorrelation, bps, blocksize, samplerate;
115329f1 481
4f52c312 482 blocksize_code = get_bits(&s->gb, 4);
4f52c312
MN
483
484 sample_rate_code = get_bits(&s->gb, 4);
115329f1 485
4f52c312 486 assignment = get_bits(&s->gb, 4); /* channel assignment */
0496a034
MN
487 if (assignment < 8 && s->channels == assignment+1)
488 decorrelation = INDEPENDENT;
489 else if (assignment >=8 && assignment < 11 && s->channels == 2)
490 decorrelation = LEFT_SIDE + assignment - 8;
4f52c312
MN
491 else
492 {
1cef211d 493 av_log(s->avctx, AV_LOG_ERROR, "unsupported channel assignment %d (channels=%d)\n", assignment, s->channels);
4f52c312
MN
494 return -1;
495 }
115329f1 496
4f52c312 497 sample_size_code = get_bits(&s->gb, 3);
0496a034
MN
498 if(sample_size_code == 0)
499 bps= s->bps;
500 else if((sample_size_code != 3) && (sample_size_code != 7))
501 bps = sample_size_table[sample_size_code];
115329f1 502 else
4f52c312 503 {
1cef211d 504 av_log(s->avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n", sample_size_code);
4f52c312
MN
505 return -1;
506 }
507
508 if (get_bits1(&s->gb))
509 {
1cef211d 510 av_log(s->avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
0496a034 511 return -1;
4f52c312 512 }
115329f1 513
0496a034
MN
514 if(get_utf8(&s->gb) < 0){
515 av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
516 return -1;
517 }
115329f1 518#if 0
ac2570a8
MN
519 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
520 (s->min_blocksize != s->max_blocksize)){
ac2570a8 521 }else{
4f52c312 522 }
0496a034 523#endif
115329f1 524
ac2570a8 525 if (blocksize_code == 0)
0496a034 526 blocksize = s->min_blocksize;
ac2570a8 527 else if (blocksize_code == 6)
0496a034 528 blocksize = get_bits(&s->gb, 8)+1;
ac2570a8 529 else if (blocksize_code == 7)
0496a034 530 blocksize = get_bits(&s->gb, 16)+1;
115329f1 531 else
0496a034 532 blocksize = blocksize_table[blocksize_code];
4f52c312 533
0496a034
MN
534 if(blocksize > s->max_blocksize){
535 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize, s->max_blocksize);
ac2570a8
MN
536 return -1;
537 }
538
ac66834c
MN
539 if(blocksize * s->channels * sizeof(int16_t) > alloc_data_size)
540 return -1;
541
ac2570a8 542 if (sample_rate_code == 0){
0496a034 543 samplerate= s->samplerate;
ac2570a8 544 }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
0496a034 545 samplerate = sample_rate_table[sample_rate_code];
ac2570a8 546 else if (sample_rate_code == 12)
0496a034 547 samplerate = get_bits(&s->gb, 8) * 1000;
ac2570a8 548 else if (sample_rate_code == 13)
0496a034 549 samplerate = get_bits(&s->gb, 16);
ac2570a8 550 else if (sample_rate_code == 14)
0496a034 551 samplerate = get_bits(&s->gb, 16) * 10;
ac2570a8
MN
552 else{
553 av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
554 return -1;
4f52c312
MN
555 }
556
9d656110 557 skip_bits(&s->gb, 8);
b865838e 558 crc8= av_crc(av_crc07, 0, s->gb.buffer, get_bits_count(&s->gb)/8);
9d656110 559 if(crc8){
160d679c 560 av_log(s->avctx, AV_LOG_ERROR, "header crc mismatch crc=%2X\n", crc8);
9d656110
MN
561 return -1;
562 }
115329f1 563
0496a034
MN
564 s->blocksize = blocksize;
565 s->samplerate = samplerate;
566 s->bps = bps;
567 s->decorrelation= decorrelation;
4f52c312 568
ac2570a8 569// dump_headers(s);
4f52c312
MN
570
571 /* subframes */
572 for (i = 0; i < s->channels; i++)
573 {
ac2570a8 574// av_log(s->avctx, AV_LOG_DEBUG, "decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
4f52c312
MN
575 if (decode_subframe(s, i) < 0)
576 return -1;
577 }
115329f1 578
4f52c312
MN
579 align_get_bits(&s->gb);
580
581 /* frame footer */
582 skip_bits(&s->gb, 16); /* data crc */
583
584 return 0;
585}
586
587static int flac_decode_frame(AVCodecContext *avctx,
588 void *data, int *data_size,
589 uint8_t *buf, int buf_size)
590{
591 FLACContext *s = avctx->priv_data;
3f1899a8
MM
592 int tmp = 0, i, j = 0, input_buf_size = 0;
593 int16_t *samples = data;
ac66834c
MN
594 int alloc_data_size= *data_size;
595
596 *data_size=0;
4f52c312 597
ac2570a8 598 if(s->max_framesize == 0){
09f20d37 599 s->max_framesize= 65536; // should hopefully be enough for the first header
ac2570a8
MN
600 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
601 }
602
603 if(1 && s->max_framesize){//FIXME truncated
09f20d37 604 buf_size= FFMAX(FFMIN(buf_size, s->max_framesize - s->bitstream_size), 0);
ac2570a8
MN
605 input_buf_size= buf_size;
606
607 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
608// printf("memmove\n");
609 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
610 s->bitstream_index=0;
611 }
612 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
613 buf= &s->bitstream[s->bitstream_index];
614 buf_size += s->bitstream_size;
615 s->bitstream_size= buf_size;
115329f1 616
ac2570a8
MN
617 if(buf_size < s->max_framesize){
618// printf("wanna more data ...\n");
619 return input_buf_size;
620 }
621 }
4f52c312
MN
622
623 init_get_bits(&s->gb, buf, buf_size*8);
115329f1 624
17c90b9d 625 if (!metadata_parse(s))
4f52c312 626 {
9d656110 627 tmp = show_bits(&s->gb, 16);
ac2570a8
MN
628 if(tmp != 0xFFF8){
629 av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
630 while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8)
631 skip_bits(&s->gb, 8);
632 goto end; // we may not have enough bits left to decode a frame, so try next time
633 }
634 skip_bits(&s->gb, 16);
ac66834c 635 if (decode_frame(s, alloc_data_size) < 0){
9d656110
MN
636 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
637 s->bitstream_size=0;
638 s->bitstream_index=0;
4f52c312 639 return -1;
9d656110 640 }
4f52c312 641 }
ac2570a8 642
115329f1 643
4f52c312
MN
644#if 0
645 /* fix the channel order here */
646 if (s->order == MID_SIDE)
647 {
648 short *left = samples;
649 short *right = samples + s->blocksize;
650 for (i = 0; i < s->blocksize; i += 2)
651 {
652 uint32_t x = s->decoded[0][i];
653 uint32_t y = s->decoded[0][i+1];
654
655 right[i] = x - (y / 2);
656 left[i] = right[i] + y;
657 }
658 *data_size = 2 * s->blocksize;
659 }
660 else
661 {
662 for (i = 0; i < s->channels; i++)
663 {
664 switch(s->order)
665 {
666 case INDEPENDENT:
667 for (j = 0; j < s->blocksize; j++)
668 samples[(s->blocksize*i)+j] = s->decoded[i][j];
669 break;
670 case LEFT_SIDE:
671 case RIGHT_SIDE:
672 if (i == 0)
673 for (j = 0; j < s->blocksize; j++)
674 samples[(s->blocksize*i)+j] = s->decoded[0][j];
675 else
676 for (j = 0; j < s->blocksize; j++)
677 samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
678 break;
679// case MID_SIDE:
ac2570a8 680// av_log(s->avctx, AV_LOG_DEBUG, "mid-side unsupported\n");
4f52c312
MN
681 }
682 *data_size += s->blocksize;
683 }
684 }
685#else
c448303d
MN
686#define DECORRELATE(left, right)\
687 assert(s->channels == 2);\
688 for (i = 0; i < s->blocksize; i++)\
689 {\
690 int a= s->decoded[0][i];\
691 int b= s->decoded[1][i];\
71fd1225
BF
692 *samples++ = ((left) << (24 - s->bps)) >> 8;\
693 *samples++ = ((right) << (24 - s->bps)) >> 8;\
c448303d
MN
694 }\
695 break;
696
9d656110 697 switch(s->decorrelation)
4f52c312
MN
698 {
699 case INDEPENDENT:
ac2570a8 700 for (j = 0; j < s->blocksize; j++)
4f52c312 701 {
ac2570a8 702 for (i = 0; i < s->channels; i++)
71fd1225 703 *samples++ = (s->decoded[i][j] << (24 - s->bps)) >> 8;
4f52c312
MN
704 }
705 break;
706 case LEFT_SIDE:
c448303d 707 DECORRELATE(a,a-b)
4f52c312 708 case RIGHT_SIDE:
c448303d 709 DECORRELATE(a+b,b)
4f52c312 710 case MID_SIDE:
c448303d 711 DECORRELATE( (a-=b>>1) + b, a)
4f52c312
MN
712 }
713#endif
714
ac2570a8 715 *data_size = (int8_t *)samples - (int8_t *)data;
9d656110 716// av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
4f52c312 717
ac2570a8
MN
718// s->last_blocksize = s->blocksize;
719end:
720 i= (get_bits_count(&s->gb)+7)/8;;
721 if(i > buf_size){
722 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
9d656110
MN
723 s->bitstream_size=0;
724 s->bitstream_index=0;
ac2570a8
MN
725 return -1;
726 }
4f52c312 727
ac2570a8
MN
728 if(s->bitstream_size){
729 s->bitstream_index += i;
730 s->bitstream_size -= i;
731 return input_buf_size;
115329f1 732 }else
ac2570a8 733 return i;
4f52c312
MN
734}
735
736static int flac_decode_close(AVCodecContext *avctx)
737{
738 FLACContext *s = avctx->priv_data;
739 int i;
115329f1 740
4f52c312
MN
741 for (i = 0; i < s->channels; i++)
742 {
ac2570a8 743 av_freep(&s->decoded[i]);
4f52c312 744 }
ac2570a8 745 av_freep(&s->bitstream);
115329f1 746
4f52c312
MN
747 return 0;
748}
749
1e31d32c 750static void flac_flush(AVCodecContext *avctx){
1e31d32c
MN
751 FLACContext *s = avctx->priv_data;
752
753 s->bitstream_size=
754 s->bitstream_index= 0;
755}
756
4f52c312
MN
757AVCodec flac_decoder = {
758 "flac",
759 CODEC_TYPE_AUDIO,
760 CODEC_ID_FLAC,
761 sizeof(FLACContext),
762 flac_decode_init,
763 NULL,
764 flac_decode_close,
765 flac_decode_frame,
115329f1 766 .flush= flac_flush,
4f52c312 767};