ffmpeg / libavcodec / flacdec.c @ 625daac4
History  View  Annotate  Download (22.8 KB)
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 021101301 USA

20 
*/

21  
22 
/**

23 
* @file

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 
* 34byte streaminfo structure through avctx>extradata[_size] followed

33 
* by data starting with the 0xFFF8 marker.

34 
*/

35  
36 
#include <limits.h> 
37  
38 
#include "libavutil/crc.h" 
39 
#include "avcodec.h" 
40 
#include "internal.h" 
41 
#include "get_bits.h" 
42 
#include "bytestream.h" 
43 
#include "golomb.h" 
44 
#include "flac.h" 
45 
#include "flacdata.h" 
46  
47 
#undef NDEBUG

48 
#include <assert.h> 
49  
50 
typedef struct FLACContext { 
51 
FLACSTREAMINFO 
52  
53 
AVCodecContext *avctx; ///< parent AVCodecContext

54 
GetBitContext gb; ///< GetBitContext initialized to start at the current frame

55  
56 
int blocksize; ///< number of samples in the current frame 
57 
int curr_bps; ///< bps for current subframe, adjusted for channel correlation and wasted bits 
58 
int sample_shift; ///< shift required to make output samples 16bit or 32bit 
59 
int is32; ///< flag to indicate if output should be 32bit instead of 16bit 
60 
int ch_mode; ///< channel decorrelation type in the current frame 
61 
int got_streaminfo; ///< indicates if the STREAMINFO has been read 
62  
63 
int32_t *decoded[FLAC_MAX_CHANNELS]; ///< decoded samples

64 
uint8_t *bitstream; 
65 
unsigned int bitstream_size; 
66 
unsigned int bitstream_index; 
67 
unsigned int allocated_bitstream_size; 
68 
} FLACContext; 
69  
70 
static void allocate_buffers(FLACContext *s); 
71  
72 
int ff_flac_is_extradata_valid(AVCodecContext *avctx,

73 
enum FLACExtradataFormat *format,

74 
uint8_t **streaminfo_start) 
75 
{ 
76 
if (!avctx>extradata  avctx>extradata_size < FLAC_STREAMINFO_SIZE) {

77 
av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");

78 
return 0; 
79 
} 
80 
if (AV_RL32(avctx>extradata) != MKTAG('f','L','a','C')) { 
81 
/* extradata contains STREAMINFO only */

82 
if (avctx>extradata_size != FLAC_STREAMINFO_SIZE) {

83 
av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",

84 
FLAC_STREAMINFO_SIZEavctx>extradata_size); 
85 
} 
86 
*format = FLAC_EXTRADATA_FORMAT_STREAMINFO; 
87 
*streaminfo_start = avctx>extradata; 
88 
} else {

89 
if (avctx>extradata_size < 8+FLAC_STREAMINFO_SIZE) { 
90 
av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");

91 
return 0; 
92 
} 
93 
*format = FLAC_EXTRADATA_FORMAT_FULL_HEADER; 
94 
*streaminfo_start = &avctx>extradata[8];

95 
} 
96 
return 1; 
97 
} 
98  
99 
static av_cold int flac_decode_init(AVCodecContext *avctx) 
100 
{ 
101 
enum FLACExtradataFormat format;

102 
uint8_t *streaminfo; 
103 
FLACContext *s = avctx>priv_data; 
104 
s>avctx = avctx; 
105  
106 
avctx>sample_fmt = AV_SAMPLE_FMT_S16; 
107  
108 
/* for now, the raw FLAC header is allowed to be passed to the decoder as

109 
frame data instead of extradata. */

110 
if (!avctx>extradata)

111 
return 0; 
112  
113 
if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))

114 
return 1; 
115  
116 
/* initialize based on the demuxersupplied streamdata header */

117 
ff_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, streaminfo); 
118 
if (s>bps > 16) 
119 
avctx>sample_fmt = AV_SAMPLE_FMT_S32; 
120 
else

121 
avctx>sample_fmt = AV_SAMPLE_FMT_S16; 
122 
allocate_buffers(s); 
123 
s>got_streaminfo = 1;

124  
125 
return 0; 
126 
} 
127  
128 
static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s) 
129 
{ 
130 
av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s>max_blocksize);

131 
av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s>max_framesize);

132 
av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s>samplerate);

133 
av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s>channels);

134 
av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s>bps);

135 
} 
136  
137 
static void allocate_buffers(FLACContext *s) 
138 
{ 
139 
int i;

140  
141 
assert(s>max_blocksize); 
142  
143 
if (s>max_framesize == 0 && s>max_blocksize) { 
144 
s>max_framesize = ff_flac_get_max_frame_size(s>max_blocksize, 
145 
s>channels, s>bps); 
146 
} 
147  
148 
for (i = 0; i < s>channels; i++) { 
149 
s>decoded[i] = av_realloc(s>decoded[i], 
150 
sizeof(int32_t)*s>max_blocksize);

151 
} 
152  
153 
if (s>allocated_bitstream_size < s>max_framesize)

154 
s>bitstream= av_fast_realloc(s>bitstream, 
155 
&s>allocated_bitstream_size, 
156 
s>max_framesize); 
157 
} 
158  
159 
void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s, 
160 
const uint8_t *buffer)

161 
{ 
162 
GetBitContext gb; 
163 
init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);

164  
165 
skip_bits(&gb, 16); /* skip min blocksize */ 
166 
s>max_blocksize = get_bits(&gb, 16);

167 
if (s>max_blocksize < FLAC_MIN_BLOCKSIZE) {

168 
av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",

169 
s>max_blocksize); 
170 
s>max_blocksize = 16;

171 
} 
172  
173 
skip_bits(&gb, 24); /* skip min frame size */ 
174 
s>max_framesize = get_bits_long(&gb, 24);

175  
176 
s>samplerate = get_bits_long(&gb, 20);

177 
s>channels = get_bits(&gb, 3) + 1; 
178 
s>bps = get_bits(&gb, 5) + 1; 
179  
180 
avctx>channels = s>channels; 
181 
avctx>sample_rate = s>samplerate; 
182 
avctx>bits_per_raw_sample = s>bps; 
183  
184 
s>samples = get_bits_long(&gb, 32) << 4; 
185 
s>samples = get_bits(&gb, 4);

186  
187 
skip_bits_long(&gb, 64); /* md5 sum */ 
188 
skip_bits_long(&gb, 64); /* md5 sum */ 
189  
190 
dump_headers(avctx, s); 
191 
} 
192  
193 
void ff_flac_parse_block_header(const uint8_t *block_header, 
194 
int *last, int *type, int *size) 
195 
{ 
196 
int tmp = bytestream_get_byte(&block_header);

197 
if (last)

198 
*last = tmp & 0x80;

199 
if (type)

200 
*type = tmp & 0x7F;

201 
if (size)

202 
*size = bytestream_get_be24(&block_header); 
203 
} 
204  
205 
/**

206 
* Parse the STREAMINFO from an inline header.

207 
* @param s the flac decoding context

208 
* @param buf input buffer, starting with the "fLaC" marker

209 
* @param buf_size buffer size

210 
* @return nonzero if metadata is invalid

211 
*/

212 
static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size) 
213 
{ 
214 
int metadata_type, metadata_size;

215  
216 
if (buf_size < FLAC_STREAMINFO_SIZE+8) { 
217 
/* need more data */

218 
return 0; 
219 
} 
220 
ff_flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size); 
221 
if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO 

222 
metadata_size != FLAC_STREAMINFO_SIZE) { 
223 
return AVERROR_INVALIDDATA;

224 
} 
225 
ff_flac_parse_streaminfo(s>avctx, (FLACStreaminfo *)s, &buf[8]);

226 
allocate_buffers(s); 
227 
s>got_streaminfo = 1;

228  
229 
return 0; 
230 
} 
231  
232 
/**

233 
* Determine the size of an inline header.

234 
* @param buf input buffer, starting with the "fLaC" marker

235 
* @param buf_size buffer size

236 
* @return number of bytes in the header, or 0 if more data is needed

237 
*/

238 
static int get_metadata_size(const uint8_t *buf, int buf_size) 
239 
{ 
240 
int metadata_last, metadata_size;

241 
const uint8_t *buf_end = buf + buf_size;

242  
243 
buf += 4;

244 
do {

245 
ff_flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);

246 
buf += 4;

247 
if (buf + metadata_size > buf_end) {

248 
/* need more data in order to read the complete header */

249 
return 0; 
250 
} 
251 
buf += metadata_size; 
252 
} while (!metadata_last);

253  
254 
return buf_size  (buf_end  buf);

255 
} 
256  
257 
static int decode_residuals(FLACContext *s, int channel, int pred_order) 
258 
{ 
259 
int i, tmp, partition, method_type, rice_order;

260 
int sample = 0, samples; 
261  
262 
method_type = get_bits(&s>gb, 2);

263 
if (method_type > 1) { 
264 
av_log(s>avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",

265 
method_type); 
266 
return 1; 
267 
} 
268  
269 
rice_order = get_bits(&s>gb, 4);

270  
271 
samples= s>blocksize >> rice_order; 
272 
if (pred_order > samples) {

273 
av_log(s>avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",

274 
pred_order, samples); 
275 
return 1; 
276 
} 
277  
278 
sample= 
279 
i= pred_order; 
280 
for (partition = 0; partition < (1 << rice_order); partition++) { 
281 
tmp = get_bits(&s>gb, method_type == 0 ? 4 : 5); 
282 
if (tmp == (method_type == 0 ? 15 : 31)) { 
283 
tmp = get_bits(&s>gb, 5);

284 
for (; i < samples; i++, sample++)

285 
s>decoded[channel][sample] = get_sbits_long(&s>gb, tmp); 
286 
} else {

287 
for (; i < samples; i++, sample++) {

288 
s>decoded[channel][sample] = get_sr_golomb_flac(&s>gb, tmp, INT_MAX, 0);

289 
} 
290 
} 
291 
i= 0;

292 
} 
293  
294 
return 0; 
295 
} 
296  
297 
static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order) 
298 
{ 
299 
const int blocksize = s>blocksize; 
300 
int32_t *decoded = s>decoded[channel]; 
301 
int av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d), i;

302  
303 
/* warm up samples */

304 
for (i = 0; i < pred_order; i++) { 
305 
decoded[i] = get_sbits_long(&s>gb, s>curr_bps); 
306 
} 
307  
308 
if (decode_residuals(s, channel, pred_order) < 0) 
309 
return 1; 
310  
311 
if (pred_order > 0) 
312 
a = decoded[pred_order1];

313 
if (pred_order > 1) 
314 
b = a  decoded[pred_order2];

315 
if (pred_order > 2) 
316 
c = b  decoded[pred_order2] + decoded[pred_order3]; 
317 
if (pred_order > 3) 
318 
d = c  decoded[pred_order2] + 2*decoded[pred_order3]  decoded[pred_order4]; 
319  
320 
switch (pred_order) {

321 
case 0: 
322 
break;

323 
case 1: 
324 
for (i = pred_order; i < blocksize; i++)

325 
decoded[i] = a += decoded[i]; 
326 
break;

327 
case 2: 
328 
for (i = pred_order; i < blocksize; i++)

329 
decoded[i] = a += b += decoded[i]; 
330 
break;

331 
case 3: 
332 
for (i = pred_order; i < blocksize; i++)

333 
decoded[i] = a += b += c += decoded[i]; 
334 
break;

335 
case 4: 
336 
for (i = pred_order; i < blocksize; i++)

337 
decoded[i] = a += b += c += d += decoded[i]; 
338 
break;

339 
default:

340 
av_log(s>avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);

341 
return 1; 
342 
} 
343  
344 
return 0; 
345 
} 
346  
347 
static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order) 
348 
{ 
349 
int i, j;

350 
int coeff_prec, qlevel;

351 
int coeffs[32]; 
352 
int32_t *decoded = s>decoded[channel]; 
353  
354 
/* warm up samples */

355 
for (i = 0; i < pred_order; i++) { 
356 
decoded[i] = get_sbits_long(&s>gb, s>curr_bps); 
357 
} 
358  
359 
coeff_prec = get_bits(&s>gb, 4) + 1; 
360 
if (coeff_prec == 16) { 
361 
av_log(s>avctx, AV_LOG_ERROR, "invalid coeff precision\n");

362 
return 1; 
363 
} 
364 
qlevel = get_sbits(&s>gb, 5);

365 
if (qlevel < 0) { 
366 
av_log(s>avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",

367 
qlevel); 
368 
return 1; 
369 
} 
370  
371 
for (i = 0; i < pred_order; i++) { 
372 
coeffs[i] = get_sbits(&s>gb, coeff_prec); 
373 
} 
374  
375 
if (decode_residuals(s, channel, pred_order) < 0) 
376 
return 1; 
377  
378 
if (s>bps > 16) { 
379 
int64_t sum; 
380 
for (i = pred_order; i < s>blocksize; i++) {

381 
sum = 0;

382 
for (j = 0; j < pred_order; j++) 
383 
sum += (int64_t)coeffs[j] * decoded[ij1];

384 
decoded[i] += sum >> qlevel; 
385 
} 
386 
} else {

387 
for (i = pred_order; i < s>blocksize1; i += 2) { 
388 
int c;

389 
int d = decoded[ipred_order];

390 
int s0 = 0, s1 = 0; 
391 
for (j = pred_order1; j > 0; j) { 
392 
c = coeffs[j]; 
393 
s0 += c*d; 
394 
d = decoded[ij]; 
395 
s1 += c*d; 
396 
} 
397 
c = coeffs[0];

398 
s0 += c*d; 
399 
d = decoded[i] += s0 >> qlevel; 
400 
s1 += c*d; 
401 
decoded[i+1] += s1 >> qlevel;

402 
} 
403 
if (i < s>blocksize) {

404 
int sum = 0; 
405 
for (j = 0; j < pred_order; j++) 
406 
sum += coeffs[j] * decoded[ij1];

407 
decoded[i] += sum >> qlevel; 
408 
} 
409 
} 
410  
411 
return 0; 
412 
} 
413  
414 
static inline int decode_subframe(FLACContext *s, int channel) 
415 
{ 
416 
int type, wasted = 0; 
417 
int i, tmp;

418  
419 
s>curr_bps = s>bps; 
420 
if (channel == 0) { 
421 
if (s>ch_mode == FLAC_CHMODE_RIGHT_SIDE)

422 
s>curr_bps++; 
423 
} else {

424 
if (s>ch_mode == FLAC_CHMODE_LEFT_SIDE  s>ch_mode == FLAC_CHMODE_MID_SIDE)

425 
s>curr_bps++; 
426 
} 
427  
428 
if (get_bits1(&s>gb)) {

429 
av_log(s>avctx, AV_LOG_ERROR, "invalid subframe padding\n");

430 
return 1; 
431 
} 
432 
type = get_bits(&s>gb, 6);

433  
434 
if (get_bits1(&s>gb)) {

435 
wasted = 1;

436 
while (!get_bits1(&s>gb))

437 
wasted++; 
438 
s>curr_bps = wasted; 
439 
} 
440 
if (s>curr_bps > 32) { 
441 
av_log_missing_feature(s>avctx, "decorrelated bit depth > 32", 0); 
442 
return 1; 
443 
} 
444  
445 
//FIXME use av_log2 for types

446 
if (type == 0) { 
447 
tmp = get_sbits_long(&s>gb, s>curr_bps); 
448 
for (i = 0; i < s>blocksize; i++) 
449 
s>decoded[channel][i] = tmp; 
450 
} else if (type == 1) { 
451 
for (i = 0; i < s>blocksize; i++) 
452 
s>decoded[channel][i] = get_sbits_long(&s>gb, s>curr_bps); 
453 
} else if ((type >= 8) && (type <= 12)) { 
454 
if (decode_subframe_fixed(s, channel, type & ~0x8) < 0) 
455 
return 1; 
456 
} else if (type >= 32) { 
457 
if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0) 
458 
return 1; 
459 
} else {

460 
av_log(s>avctx, AV_LOG_ERROR, "invalid coding type\n");

461 
return 1; 
462 
} 
463  
464 
if (wasted) {

465 
int i;

466 
for (i = 0; i < s>blocksize; i++) 
467 
s>decoded[channel][i] <<= wasted; 
468 
} 
469  
470 
return 0; 
471 
} 
472  
473 
static int decode_frame(FLACContext *s) 
474 
{ 
475 
int i;

476 
GetBitContext *gb = &s>gb; 
477 
FLACFrameInfo fi; 
478  
479 
if (ff_flac_decode_frame_header(s>avctx, gb, &fi)) {

480 
av_log(s>avctx, AV_LOG_ERROR, "invalid frame header\n");

481 
return 1; 
482 
} 
483  
484 
if (s>channels && fi.channels != s>channels) {

485 
av_log(s>avctx, AV_LOG_ERROR, "switching channel layout midstream "

486 
"is not supported\n");

487 
return 1; 
488 
} 
489 
s>channels = s>avctx>channels = fi.channels; 
490 
s>ch_mode = fi.ch_mode; 
491  
492 
if (!s>bps && !fi.bps) {

493 
av_log(s>avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");

494 
return 1; 
495 
} 
496 
if (!fi.bps) {

497 
fi.bps = s>bps; 
498 
} else if (s>bps && fi.bps != s>bps) { 
499 
av_log(s>avctx, AV_LOG_ERROR, "switching bps midstream is not "

500 
"supported\n");

501 
return 1; 
502 
} 
503 
s>bps = s>avctx>bits_per_raw_sample = fi.bps; 
504  
505 
if (s>bps > 16) { 
506 
s>avctx>sample_fmt = AV_SAMPLE_FMT_S32; 
507 
s>sample_shift = 32  s>bps;

508 
s>is32 = 1;

509 
} else {

510 
s>avctx>sample_fmt = AV_SAMPLE_FMT_S16; 
511 
s>sample_shift = 16  s>bps;

512 
s>is32 = 0;

513 
} 
514  
515 
if (!s>max_blocksize)

516 
s>max_blocksize = FLAC_MAX_BLOCKSIZE; 
517 
if (fi.blocksize > s>max_blocksize) {

518 
av_log(s>avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,

519 
s>max_blocksize); 
520 
return 1; 
521 
} 
522 
s>blocksize = fi.blocksize; 
523  
524 
if (!s>samplerate && !fi.samplerate) {

525 
av_log(s>avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"

526 
" or frame header\n");

527 
return 1; 
528 
} 
529 
if (fi.samplerate == 0) { 
530 
fi.samplerate = s>samplerate; 
531 
} else if (s>samplerate && fi.samplerate != s>samplerate) { 
532 
av_log(s>avctx, AV_LOG_WARNING, "sample rate changed from %d to %d\n",

533 
s>samplerate, fi.samplerate); 
534 
} 
535 
s>samplerate = s>avctx>sample_rate = fi.samplerate; 
536  
537 
if (!s>got_streaminfo) {

538 
allocate_buffers(s); 
539 
s>got_streaminfo = 1;

540 
dump_headers(s>avctx, (FLACStreaminfo *)s); 
541 
} 
542  
543 
// dump_headers(s>avctx, (FLACStreaminfo *)s);

544  
545 
/* subframes */

546 
for (i = 0; i < s>channels; i++) { 
547 
if (decode_subframe(s, i) < 0) 
548 
return 1; 
549 
} 
550  
551 
align_get_bits(gb); 
552  
553 
/* frame footer */

554 
skip_bits(gb, 16); /* data crc */ 
555  
556 
return 0; 
557 
} 
558  
559 
static int flac_decode_frame(AVCodecContext *avctx, 
560 
void *data, int *data_size, 
561 
AVPacket *avpkt) 
562 
{ 
563 
const uint8_t *buf = avpkt>data;

564 
int buf_size = avpkt>size;

565 
FLACContext *s = avctx>priv_data; 
566 
int i, j = 0, input_buf_size = 0, bytes_read = 0; 
567 
int16_t *samples_16 = data; 
568 
int32_t *samples_32 = data; 
569 
int alloc_data_size= *data_size;

570 
int output_size;

571  
572 
*data_size=0;

573  
574 
if (s>max_framesize == 0) { 
575 
s>max_framesize = 
576 
ff_flac_get_max_frame_size(s>max_blocksize ? s>max_blocksize : FLAC_MAX_BLOCKSIZE, 
577 
FLAC_MAX_CHANNELS, 32);

578 
s>bitstream= av_fast_realloc(s>bitstream, &s>allocated_bitstream_size, s>max_framesize); 
579 
} 
580  
581 
if (1 && s>max_framesize) { //FIXME truncated 
582 
if (s>bitstream_size < 4  AV_RL32(s>bitstream) != MKTAG('f','L','a','C')) 
583 
buf_size= FFMIN(buf_size, s>max_framesize  FFMIN(s>bitstream_size, s>max_framesize)); 
584 
input_buf_size= buf_size; 
585  
586 
if (s>bitstream_size + buf_size < buf_size  s>bitstream_index + s>bitstream_size + buf_size < s>bitstream_index)

587 
return 1; 
588  
589 
if (s>allocated_bitstream_size < s>bitstream_size + buf_size)

590 
s>bitstream= av_fast_realloc(s>bitstream, &s>allocated_bitstream_size, s>bitstream_size + buf_size); 
591  
592 
if (s>bitstream_index + s>bitstream_size + buf_size > s>allocated_bitstream_size) {

593 
memmove(s>bitstream, &s>bitstream[s>bitstream_index], 
594 
s>bitstream_size); 
595 
s>bitstream_index=0;

596 
} 
597 
memcpy(&s>bitstream[s>bitstream_index + s>bitstream_size], 
598 
buf, buf_size); 
599 
buf= &s>bitstream[s>bitstream_index]; 
600 
buf_size += s>bitstream_size; 
601 
s>bitstream_size= buf_size; 
602  
603 
if (buf_size < s>max_framesize && input_buf_size) {

604 
return input_buf_size;

605 
} 
606 
} 
607  
608 
/* check that there is at least the smallest decodable amount of data.

609 
this amount corresponds to the smallest valid FLAC frame possible.

610 
FF F8 69 02 00 00 9A 00 00 34 46 */

611 
if (buf_size < 11) 
612 
goto end;

613  
614 
/* check for inline header */

615 
if (AV_RB32(buf) == MKBETAG('f','L','a','C')) { 
616 
if (!s>got_streaminfo && parse_streaminfo(s, buf, buf_size)) {

617 
av_log(s>avctx, AV_LOG_ERROR, "invalid header\n");

618 
return 1; 
619 
} 
620 
bytes_read = get_metadata_size(buf, buf_size); 
621 
goto end;

622 
} 
623  
624 
/* check for frame sync code and resync stream if necessary */

625 
if ((AV_RB16(buf) & 0xFFFE) != 0xFFF8) { 
626 
const uint8_t *buf_end = buf + buf_size;

627 
av_log(s>avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");

628 
while (buf+2 < buf_end && (AV_RB16(buf) & 0xFFFE) != 0xFFF8) 
629 
buf++; 
630 
bytes_read = buf_size  (buf_end  buf); 
631 
goto end; // we may not have enough bits left to decode a frame, so try next time 
632 
} 
633  
634 
/* decode frame */

635 
init_get_bits(&s>gb, buf, buf_size*8);

636 
if (decode_frame(s) < 0) { 
637 
av_log(s>avctx, AV_LOG_ERROR, "decode_frame() failed\n");

638 
s>bitstream_size=0;

639 
s>bitstream_index=0;

640 
return 1; 
641 
} 
642 
bytes_read = (get_bits_count(&s>gb)+7)/8; 
643  
644 
/* check if allocated data size is large enough for output */

645 
output_size = s>blocksize * s>channels * (s>is32 ? 4 : 2); 
646 
if (output_size > alloc_data_size) {

647 
av_log(s>avctx, AV_LOG_ERROR, "output data size is larger than "

648 
"allocated data size\n");

649 
goto end;

650 
} 
651 
*data_size = output_size; 
652  
653 
#define DECORRELATE(left, right)\

654 
assert(s>channels == 2);\

655 
for (i = 0; i < s>blocksize; i++) {\ 
656 
int a= s>decoded[0][i];\ 
657 
int b= s>decoded[1][i];\ 
658 
if (s>is32) {\

659 
*samples_32++ = (left) << s>sample_shift;\ 
660 
*samples_32++ = (right) << s>sample_shift;\ 
661 
} else {\

662 
*samples_16++ = (left) << s>sample_shift;\ 
663 
*samples_16++ = (right) << s>sample_shift;\ 
664 
}\ 
665 
}\ 
666 
break;

667  
668 
switch (s>ch_mode) {

669 
case FLAC_CHMODE_INDEPENDENT:

670 
for (j = 0; j < s>blocksize; j++) { 
671 
for (i = 0; i < s>channels; i++) { 
672 
if (s>is32)

673 
*samples_32++ = s>decoded[i][j] << s>sample_shift; 
674 
else

675 
*samples_16++ = s>decoded[i][j] << s>sample_shift; 
676 
} 
677 
} 
678 
break;

679 
case FLAC_CHMODE_LEFT_SIDE:

680 
DECORRELATE(a,ab) 
681 
case FLAC_CHMODE_RIGHT_SIDE:

682 
DECORRELATE(a+b,b) 
683 
case FLAC_CHMODE_MID_SIDE:

684 
DECORRELATE( (a=b>>1) + b, a)

685 
} 
686  
687 
end:

688 
if (bytes_read > buf_size) {

689 
av_log(s>avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read  buf_size);

690 
s>bitstream_size=0;

691 
s>bitstream_index=0;

692 
return 1; 
693 
} 
694  
695 
if (s>bitstream_size) {

696 
s>bitstream_index += bytes_read; 
697 
s>bitstream_size = bytes_read; 
698 
return input_buf_size;

699 
} else

700 
return bytes_read;

701 
} 
702  
703 
static av_cold int flac_decode_close(AVCodecContext *avctx) 
704 
{ 
705 
FLACContext *s = avctx>priv_data; 
706 
int i;

707  
708 
for (i = 0; i < s>channels; i++) { 
709 
av_freep(&s>decoded[i]); 
710 
} 
711 
av_freep(&s>bitstream); 
712  
713 
return 0; 
714 
} 
715  
716 
static void flac_flush(AVCodecContext *avctx) 
717 
{ 
718 
FLACContext *s = avctx>priv_data; 
719  
720 
s>bitstream_size= 
721 
s>bitstream_index= 0;

722 
} 
723  
724 
AVCodec flac_decoder = { 
725 
"flac",

726 
AVMEDIA_TYPE_AUDIO, 
727 
CODEC_ID_FLAC, 
728 
sizeof(FLACContext),

729 
flac_decode_init, 
730 
NULL,

731 
flac_decode_close, 
732 
flac_decode_frame, 
733 
CODEC_CAP_DELAY  CODEC_CAP_SUBFRAMES, /* FIXME: add a FLAC parser so that

734 
we will not need to use either

735 
of these capabilities */

736 
.flush= flac_flush, 
737 
.long_name= NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),

738 
}; 