00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00036 #include <limits.h>
00037
00038 #define ALT_BITSTREAM_READER
00039 #include "libavutil/crc.h"
00040 #include "avcodec.h"
00041 #include "bitstream.h"
00042 #include "golomb.h"
00043 #include "flac.h"
00044
00045 #undef NDEBUG
00046 #include <assert.h>
00047
00048 #define MAX_CHANNELS 8
00049 #define MAX_BLOCKSIZE 65535
00050
00051 enum decorrelation_type {
00052 INDEPENDENT,
00053 LEFT_SIDE,
00054 RIGHT_SIDE,
00055 MID_SIDE,
00056 };
00057
00058 typedef struct FLACContext {
00059 FLACSTREAMINFO
00060
00061 AVCodecContext *avctx;
00062 GetBitContext gb;
00063
00064 int blocksize;
00065 int curr_bps;
00066 int sample_shift;
00067 int is32;
00068 enum decorrelation_type decorrelation;
00069
00070 int32_t *decoded[MAX_CHANNELS];
00071 uint8_t *bitstream;
00072 unsigned int bitstream_size;
00073 unsigned int bitstream_index;
00074 unsigned int allocated_bitstream_size;
00075 } FLACContext;
00076
00077 static const int sample_rate_table[] =
00078 { 0,
00079 88200, 176400, 192000,
00080 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
00081 0, 0, 0, 0 };
00082
00083 static const int sample_size_table[] =
00084 { 0, 8, 12, 0, 16, 20, 24, 0 };
00085
00086 static const int blocksize_table[] = {
00087 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
00088 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
00089 };
00090
00091 static int64_t get_utf8(GetBitContext *gb)
00092 {
00093 int64_t val;
00094 GET_UTF8(val, get_bits(gb, 8), return -1;)
00095 return val;
00096 }
00097
00098 static void allocate_buffers(FLACContext *s);
00099
00100 int ff_flac_is_extradata_valid(AVCodecContext *avctx,
00101 enum FLACExtradataFormat *format,
00102 uint8_t **streaminfo_start)
00103 {
00104 if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
00105 av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
00106 return 0;
00107 }
00108 if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
00109
00110 if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
00111 av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
00112 FLAC_STREAMINFO_SIZE-avctx->extradata_size);
00113 }
00114 *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
00115 *streaminfo_start = avctx->extradata;
00116 } else {
00117 if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
00118 av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
00119 return 0;
00120 }
00121 *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
00122 *streaminfo_start = &avctx->extradata[8];
00123 }
00124 return 1;
00125 }
00126
00127 static av_cold int flac_decode_init(AVCodecContext *avctx)
00128 {
00129 enum FLACExtradataFormat format;
00130 uint8_t *streaminfo;
00131 FLACContext *s = avctx->priv_data;
00132 s->avctx = avctx;
00133
00134 avctx->sample_fmt = SAMPLE_FMT_S16;
00135
00136
00137
00138 if (!avctx->extradata)
00139 return 0;
00140
00141 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
00142 return -1;
00143
00144
00145 ff_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, streaminfo);
00146 allocate_buffers(s);
00147
00148 return 0;
00149 }
00150
00151 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
00152 {
00153 av_log(avctx, AV_LOG_DEBUG, " Blocksize: %d .. %d\n", s->min_blocksize,
00154 s->max_blocksize);
00155 av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
00156 av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
00157 av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
00158 av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
00159 }
00160
00161 static void allocate_buffers(FLACContext *s)
00162 {
00163 int i;
00164
00165 assert(s->max_blocksize);
00166
00167 if (s->max_framesize == 0 && s->max_blocksize) {
00168
00169 s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8;
00170 }
00171
00172 for (i = 0; i < s->channels; i++) {
00173 s->decoded[i] = av_realloc(s->decoded[i],
00174 sizeof(int32_t)*s->max_blocksize);
00175 }
00176
00177 if (s->allocated_bitstream_size < s->max_framesize)
00178 s->bitstream= av_fast_realloc(s->bitstream,
00179 &s->allocated_bitstream_size,
00180 s->max_framesize);
00181 }
00182
00183 void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
00184 const uint8_t *buffer)
00185 {
00186 GetBitContext gb;
00187 init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
00188
00189
00190 s->min_blocksize = get_bits(&gb, 16);
00191 s->max_blocksize = get_bits(&gb, 16);
00192
00193 skip_bits(&gb, 24);
00194 s->max_framesize = get_bits_long(&gb, 24);
00195
00196 s->samplerate = get_bits_long(&gb, 20);
00197 s->channels = get_bits(&gb, 3) + 1;
00198 s->bps = get_bits(&gb, 5) + 1;
00199
00200 avctx->channels = s->channels;
00201 avctx->sample_rate = s->samplerate;
00202 avctx->bits_per_raw_sample = s->bps;
00203 if (s->bps > 16)
00204 avctx->sample_fmt = SAMPLE_FMT_S32;
00205 else
00206 avctx->sample_fmt = SAMPLE_FMT_S16;
00207
00208 s->samples = get_bits_long(&gb, 32) << 4;
00209 s->samples |= get_bits_long(&gb, 4);
00210
00211 skip_bits(&gb, 64);
00212 skip_bits(&gb, 64);
00213
00214 dump_headers(avctx, s);
00215 }
00216
00224 static int metadata_parse(FLACContext *s)
00225 {
00226 int i, metadata_last, metadata_type, metadata_size, streaminfo_updated=0;
00227 int initial_pos= get_bits_count(&s->gb);
00228
00229 if (show_bits_long(&s->gb, 32) == MKBETAG('f','L','a','C')) {
00230 skip_bits(&s->gb, 32);
00231
00232 do {
00233 metadata_last = get_bits1(&s->gb);
00234 metadata_type = get_bits(&s->gb, 7);
00235 metadata_size = get_bits_long(&s->gb, 24);
00236
00237 if (get_bits_count(&s->gb) + 8*metadata_size > s->gb.size_in_bits) {
00238 skip_bits_long(&s->gb, initial_pos - get_bits_count(&s->gb));
00239 break;
00240 }
00241
00242 if (metadata_size) {
00243 switch (metadata_type) {
00244 case FLAC_METADATA_TYPE_STREAMINFO:
00245 ff_flac_parse_streaminfo(s->avctx, (FLACStreaminfo *)s,
00246 s->gb.buffer+get_bits_count(&s->gb)/8);
00247 streaminfo_updated = 1;
00248
00249 default:
00250 for (i = 0; i < metadata_size; i++)
00251 skip_bits(&s->gb, 8);
00252 }
00253 }
00254 } while (!metadata_last);
00255
00256 if (streaminfo_updated)
00257 allocate_buffers(s);
00258 return 1;
00259 }
00260 return 0;
00261 }
00262
00263 static int decode_residuals(FLACContext *s, int channel, int pred_order)
00264 {
00265 int i, tmp, partition, method_type, rice_order;
00266 int sample = 0, samples;
00267
00268 method_type = get_bits(&s->gb, 2);
00269 if (method_type > 1) {
00270 av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
00271 method_type);
00272 return -1;
00273 }
00274
00275 rice_order = get_bits(&s->gb, 4);
00276
00277 samples= s->blocksize >> rice_order;
00278 if (pred_order > samples) {
00279 av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
00280 pred_order, samples);
00281 return -1;
00282 }
00283
00284 sample=
00285 i= pred_order;
00286 for (partition = 0; partition < (1 << rice_order); partition++) {
00287 tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
00288 if (tmp == (method_type == 0 ? 15 : 31)) {
00289 tmp = get_bits(&s->gb, 5);
00290 for (; i < samples; i++, sample++)
00291 s->decoded[channel][sample] = get_sbits(&s->gb, tmp);
00292 } else {
00293 for (; i < samples; i++, sample++) {
00294 s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
00295 }
00296 }
00297 i= 0;
00298 }
00299
00300 return 0;
00301 }
00302
00303 static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
00304 {
00305 const int blocksize = s->blocksize;
00306 int32_t *decoded = s->decoded[channel];
00307 int av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d), i;
00308
00309
00310 for (i = 0; i < pred_order; i++) {
00311 decoded[i] = get_sbits(&s->gb, s->curr_bps);
00312 }
00313
00314 if (decode_residuals(s, channel, pred_order) < 0)
00315 return -1;
00316
00317 if (pred_order > 0)
00318 a = decoded[pred_order-1];
00319 if (pred_order > 1)
00320 b = a - decoded[pred_order-2];
00321 if (pred_order > 2)
00322 c = b - decoded[pred_order-2] + decoded[pred_order-3];
00323 if (pred_order > 3)
00324 d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
00325
00326 switch (pred_order) {
00327 case 0:
00328 break;
00329 case 1:
00330 for (i = pred_order; i < blocksize; i++)
00331 decoded[i] = a += decoded[i];
00332 break;
00333 case 2:
00334 for (i = pred_order; i < blocksize; i++)
00335 decoded[i] = a += b += decoded[i];
00336 break;
00337 case 3:
00338 for (i = pred_order; i < blocksize; i++)
00339 decoded[i] = a += b += c += decoded[i];
00340 break;
00341 case 4:
00342 for (i = pred_order; i < blocksize; i++)
00343 decoded[i] = a += b += c += d += decoded[i];
00344 break;
00345 default:
00346 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
00347 return -1;
00348 }
00349
00350 return 0;
00351 }
00352
00353 static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
00354 {
00355 int i, j;
00356 int coeff_prec, qlevel;
00357 int coeffs[pred_order];
00358 int32_t *decoded = s->decoded[channel];
00359
00360
00361 for (i = 0; i < pred_order; i++) {
00362 decoded[i] = get_sbits(&s->gb, s->curr_bps);
00363 }
00364
00365 coeff_prec = get_bits(&s->gb, 4) + 1;
00366 if (coeff_prec == 16) {
00367 av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
00368 return -1;
00369 }
00370 qlevel = get_sbits(&s->gb, 5);
00371 if (qlevel < 0) {
00372 av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
00373 qlevel);
00374 return -1;
00375 }
00376
00377 for (i = 0; i < pred_order; i++) {
00378 coeffs[i] = get_sbits(&s->gb, coeff_prec);
00379 }
00380
00381 if (decode_residuals(s, channel, pred_order) < 0)
00382 return -1;
00383
00384 if (s->bps > 16) {
00385 int64_t sum;
00386 for (i = pred_order; i < s->blocksize; i++) {
00387 sum = 0;
00388 for (j = 0; j < pred_order; j++)
00389 sum += (int64_t)coeffs[j] * decoded[i-j-1];
00390 decoded[i] += sum >> qlevel;
00391 }
00392 } else {
00393 for (i = pred_order; i < s->blocksize-1; i += 2) {
00394 int c;
00395 int d = decoded[i-pred_order];
00396 int s0 = 0, s1 = 0;
00397 for (j = pred_order-1; j > 0; j--) {
00398 c = coeffs[j];
00399 s0 += c*d;
00400 d = decoded[i-j];
00401 s1 += c*d;
00402 }
00403 c = coeffs[0];
00404 s0 += c*d;
00405 d = decoded[i] += s0 >> qlevel;
00406 s1 += c*d;
00407 decoded[i+1] += s1 >> qlevel;
00408 }
00409 if (i < s->blocksize) {
00410 int sum = 0;
00411 for (j = 0; j < pred_order; j++)
00412 sum += coeffs[j] * decoded[i-j-1];
00413 decoded[i] += sum >> qlevel;
00414 }
00415 }
00416
00417 return 0;
00418 }
00419
00420 static inline int decode_subframe(FLACContext *s, int channel)
00421 {
00422 int type, wasted = 0;
00423 int i, tmp;
00424
00425 s->curr_bps = s->bps;
00426 if (channel == 0) {
00427 if (s->decorrelation == RIGHT_SIDE)
00428 s->curr_bps++;
00429 } else {
00430 if (s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
00431 s->curr_bps++;
00432 }
00433
00434 if (get_bits1(&s->gb)) {
00435 av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
00436 return -1;
00437 }
00438 type = get_bits(&s->gb, 6);
00439
00440 if (get_bits1(&s->gb)) {
00441 wasted = 1;
00442 while (!get_bits1(&s->gb))
00443 wasted++;
00444 s->curr_bps -= wasted;
00445 }
00446
00447
00448 if (type == 0) {
00449 tmp = get_sbits(&s->gb, s->curr_bps);
00450 for (i = 0; i < s->blocksize; i++)
00451 s->decoded[channel][i] = tmp;
00452 } else if (type == 1) {
00453 for (i = 0; i < s->blocksize; i++)
00454 s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
00455 } else if ((type >= 8) && (type <= 12)) {
00456 if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
00457 return -1;
00458 } else if (type >= 32) {
00459 if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
00460 return -1;
00461 } else {
00462 av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
00463 return -1;
00464 }
00465
00466 if (wasted) {
00467 int i;
00468 for (i = 0; i < s->blocksize; i++)
00469 s->decoded[channel][i] <<= wasted;
00470 }
00471
00472 return 0;
00473 }
00474
00475 static int decode_frame(FLACContext *s, int alloc_data_size)
00476 {
00477 int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
00478 int decorrelation, bps, blocksize, samplerate;
00479
00480 blocksize_code = get_bits(&s->gb, 4);
00481
00482 sample_rate_code = get_bits(&s->gb, 4);
00483
00484 assignment = get_bits(&s->gb, 4);
00485 if (assignment < 8 && s->channels == assignment+1)
00486 decorrelation = INDEPENDENT;
00487 else if (assignment >=8 && assignment < 11 && s->channels == 2)
00488 decorrelation = LEFT_SIDE + assignment - 8;
00489 else {
00490 av_log(s->avctx, AV_LOG_ERROR, "unsupported channel assignment %d (channels=%d)\n",
00491 assignment, s->channels);
00492 return -1;
00493 }
00494
00495 sample_size_code = get_bits(&s->gb, 3);
00496 if (sample_size_code == 0)
00497 bps= s->bps;
00498 else if ((sample_size_code != 3) && (sample_size_code != 7))
00499 bps = sample_size_table[sample_size_code];
00500 else {
00501 av_log(s->avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n",
00502 sample_size_code);
00503 return -1;
00504 }
00505 if (bps > 16) {
00506 s->avctx->sample_fmt = SAMPLE_FMT_S32;
00507 s->sample_shift = 32 - bps;
00508 s->is32 = 1;
00509 } else {
00510 s->avctx->sample_fmt = SAMPLE_FMT_S16;
00511 s->sample_shift = 16 - bps;
00512 s->is32 = 0;
00513 }
00514 s->bps = s->avctx->bits_per_raw_sample = bps;
00515
00516 if (get_bits1(&s->gb)) {
00517 av_log(s->avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
00518 return -1;
00519 }
00520
00521 if (get_utf8(&s->gb) < 0) {
00522 av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
00523 return -1;
00524 }
00525
00526 if (blocksize_code == 0)
00527 blocksize = s->min_blocksize;
00528 else if (blocksize_code == 6)
00529 blocksize = get_bits(&s->gb, 8)+1;
00530 else if (blocksize_code == 7)
00531 blocksize = get_bits(&s->gb, 16)+1;
00532 else
00533 blocksize = blocksize_table[blocksize_code];
00534
00535 if (blocksize > s->max_blocksize) {
00536 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize,
00537 s->max_blocksize);
00538 return -1;
00539 }
00540
00541 if (blocksize * s->channels * sizeof(int16_t) > alloc_data_size)
00542 return -1;
00543
00544 if (sample_rate_code == 0)
00545 samplerate= s->samplerate;
00546 else if (sample_rate_code < 12)
00547 samplerate = sample_rate_table[sample_rate_code];
00548 else if (sample_rate_code == 12)
00549 samplerate = get_bits(&s->gb, 8) * 1000;
00550 else if (sample_rate_code == 13)
00551 samplerate = get_bits(&s->gb, 16);
00552 else if (sample_rate_code == 14)
00553 samplerate = get_bits(&s->gb, 16) * 10;
00554 else {
00555 av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n",
00556 sample_rate_code);
00557 return -1;
00558 }
00559
00560 skip_bits(&s->gb, 8);
00561 crc8 = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0,
00562 s->gb.buffer, get_bits_count(&s->gb)/8);
00563 if (crc8) {
00564 av_log(s->avctx, AV_LOG_ERROR, "header crc mismatch crc=%2X\n", crc8);
00565 return -1;
00566 }
00567
00568 s->blocksize = blocksize;
00569 s->samplerate = samplerate;
00570 s->bps = bps;
00571 s->decorrelation= decorrelation;
00572
00573
00574
00575
00576 for (i = 0; i < s->channels; i++) {
00577 if (decode_subframe(s, i) < 0)
00578 return -1;
00579 }
00580
00581 align_get_bits(&s->gb);
00582
00583
00584 skip_bits(&s->gb, 16);
00585
00586 return 0;
00587 }
00588
00589 static int flac_decode_frame(AVCodecContext *avctx,
00590 void *data, int *data_size,
00591 const uint8_t *buf, int buf_size)
00592 {
00593 FLACContext *s = avctx->priv_data;
00594 int tmp = 0, i, j = 0, input_buf_size = 0;
00595 int16_t *samples_16 = data;
00596 int32_t *samples_32 = data;
00597 int alloc_data_size= *data_size;
00598
00599 *data_size=0;
00600
00601 if (s->max_framesize == 0) {
00602 s->max_framesize= FFMAX(4, buf_size);
00603 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
00604 }
00605
00606 if (1 && s->max_framesize) {
00607 if (s->bitstream_size < 4 || AV_RL32(s->bitstream) != MKTAG('f','L','a','C'))
00608 buf_size= FFMIN(buf_size, s->max_framesize - FFMIN(s->bitstream_size, s->max_framesize));
00609 input_buf_size= buf_size;
00610
00611 if (s->bitstream_size + buf_size < buf_size || s->bitstream_index + s->bitstream_size + buf_size < s->bitstream_index)
00612 return -1;
00613
00614 if (s->allocated_bitstream_size < s->bitstream_size + buf_size)
00615 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->bitstream_size + buf_size);
00616
00617 if (s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size) {
00618 memmove(s->bitstream, &s->bitstream[s->bitstream_index],
00619 s->bitstream_size);
00620 s->bitstream_index=0;
00621 }
00622 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size],
00623 buf, buf_size);
00624 buf= &s->bitstream[s->bitstream_index];
00625 buf_size += s->bitstream_size;
00626 s->bitstream_size= buf_size;
00627
00628 if (buf_size < s->max_framesize && input_buf_size) {
00629 return input_buf_size;
00630 }
00631 }
00632
00633 init_get_bits(&s->gb, buf, buf_size*8);
00634
00635 if (metadata_parse(s))
00636 goto end;
00637
00638 tmp = show_bits(&s->gb, 16);
00639 if ((tmp & 0xFFFE) != 0xFFF8) {
00640 av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
00641 while (get_bits_count(&s->gb)/8+2 < buf_size && (show_bits(&s->gb, 16) & 0xFFFE) != 0xFFF8)
00642 skip_bits(&s->gb, 8);
00643 goto end;
00644 }
00645 skip_bits(&s->gb, 16);
00646 if (decode_frame(s, alloc_data_size) < 0) {
00647 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
00648 s->bitstream_size=0;
00649 s->bitstream_index=0;
00650 return -1;
00651 }
00652
00653 #define DECORRELATE(left, right)\
00654 assert(s->channels == 2);\
00655 for (i = 0; i < s->blocksize; i++) {\
00656 int a= s->decoded[0][i];\
00657 int b= s->decoded[1][i];\
00658 if (s->is32) {\
00659 *samples_32++ = (left) << s->sample_shift;\
00660 *samples_32++ = (right) << s->sample_shift;\
00661 } else {\
00662 *samples_16++ = (left) << s->sample_shift;\
00663 *samples_16++ = (right) << s->sample_shift;\
00664 }\
00665 }\
00666 break;
00667
00668 switch (s->decorrelation) {
00669 case INDEPENDENT:
00670 for (j = 0; j < s->blocksize; j++) {
00671 for (i = 0; i < s->channels; i++) {
00672 if (s->is32)
00673 *samples_32++ = s->decoded[i][j] << s->sample_shift;
00674 else
00675 *samples_16++ = s->decoded[i][j] << s->sample_shift;
00676 }
00677 }
00678 break;
00679 case LEFT_SIDE:
00680 DECORRELATE(a,a-b)
00681 case RIGHT_SIDE:
00682 DECORRELATE(a+b,b)
00683 case MID_SIDE:
00684 DECORRELATE( (a-=b>>1) + b, a)
00685 }
00686
00687 *data_size = s->blocksize * s->channels * (s->is32 ? 4 : 2);
00688
00689 end:
00690 i= (get_bits_count(&s->gb)+7)/8;
00691 if (i > buf_size) {
00692 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
00693 s->bitstream_size=0;
00694 s->bitstream_index=0;
00695 return -1;
00696 }
00697
00698 if (s->bitstream_size) {
00699 s->bitstream_index += i;
00700 s->bitstream_size -= i;
00701 return input_buf_size;
00702 } else
00703 return i;
00704 }
00705
00706 static av_cold int flac_decode_close(AVCodecContext *avctx)
00707 {
00708 FLACContext *s = avctx->priv_data;
00709 int i;
00710
00711 for (i = 0; i < s->channels; i++) {
00712 av_freep(&s->decoded[i]);
00713 }
00714 av_freep(&s->bitstream);
00715
00716 return 0;
00717 }
00718
00719 static void flac_flush(AVCodecContext *avctx)
00720 {
00721 FLACContext *s = avctx->priv_data;
00722
00723 s->bitstream_size=
00724 s->bitstream_index= 0;
00725 }
00726
00727 AVCodec flac_decoder = {
00728 "flac",
00729 CODEC_TYPE_AUDIO,
00730 CODEC_ID_FLAC,
00731 sizeof(FLACContext),
00732 flac_decode_init,
00733 NULL,
00734 flac_decode_close,
00735 flac_decode_frame,
00736 CODEC_CAP_DELAY,
00737 .flush= flac_flush,
00738 .long_name= NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
00739 };