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