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 #include "libavutil/crc.h"
00039 #include "avcodec.h"
00040 #include "internal.h"
00041 #include "get_bits.h"
00042 #include "bytestream.h"
00043 #include "golomb.h"
00044 #include "flac.h"
00045 #include "flacdata.h"
00046
00047 #undef NDEBUG
00048 #include <assert.h>
00049
00050 typedef struct FLACContext {
00051 FLACSTREAMINFO
00052
00053 AVCodecContext *avctx;
00054 GetBitContext gb;
00055
00056 int blocksize;
00057 int curr_bps;
00058 int sample_shift;
00059 int is32;
00060 int ch_mode;
00061 int got_streaminfo;
00062
00063 int32_t *decoded[FLAC_MAX_CHANNELS];
00064 } FLACContext;
00065
00066 static void allocate_buffers(FLACContext *s);
00067
00068 int ff_flac_is_extradata_valid(AVCodecContext *avctx,
00069 enum FLACExtradataFormat *format,
00070 uint8_t **streaminfo_start)
00071 {
00072 if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
00073 av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
00074 return 0;
00075 }
00076 if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
00077
00078 if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
00079 av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
00080 FLAC_STREAMINFO_SIZE-avctx->extradata_size);
00081 }
00082 *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
00083 *streaminfo_start = avctx->extradata;
00084 } else {
00085 if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
00086 av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
00087 return 0;
00088 }
00089 *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
00090 *streaminfo_start = &avctx->extradata[8];
00091 }
00092 return 1;
00093 }
00094
00095 static av_cold int flac_decode_init(AVCodecContext *avctx)
00096 {
00097 enum FLACExtradataFormat format;
00098 uint8_t *streaminfo;
00099 FLACContext *s = avctx->priv_data;
00100 s->avctx = avctx;
00101
00102 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00103
00104
00105
00106 if (!avctx->extradata)
00107 return 0;
00108
00109 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
00110 return -1;
00111
00112
00113 ff_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, streaminfo);
00114 if (s->bps > 16)
00115 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00116 else
00117 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00118 allocate_buffers(s);
00119 s->got_streaminfo = 1;
00120
00121 return 0;
00122 }
00123
00124 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
00125 {
00126 av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize);
00127 av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
00128 av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
00129 av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
00130 av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
00131 }
00132
00133 static void allocate_buffers(FLACContext *s)
00134 {
00135 int i;
00136
00137 assert(s->max_blocksize);
00138
00139 for (i = 0; i < s->channels; i++) {
00140 s->decoded[i] = av_realloc(s->decoded[i],
00141 sizeof(int32_t)*s->max_blocksize);
00142 }
00143 }
00144
00145 void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
00146 const uint8_t *buffer)
00147 {
00148 GetBitContext gb;
00149 init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
00150
00151 skip_bits(&gb, 16);
00152 s->max_blocksize = get_bits(&gb, 16);
00153 if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
00154 av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
00155 s->max_blocksize);
00156 s->max_blocksize = 16;
00157 }
00158
00159 skip_bits(&gb, 24);
00160 s->max_framesize = get_bits_long(&gb, 24);
00161
00162 s->samplerate = get_bits_long(&gb, 20);
00163 s->channels = get_bits(&gb, 3) + 1;
00164 s->bps = get_bits(&gb, 5) + 1;
00165
00166 avctx->channels = s->channels;
00167 avctx->sample_rate = s->samplerate;
00168 avctx->bits_per_raw_sample = s->bps;
00169
00170 s->samples = get_bits_long(&gb, 32) << 4;
00171 s->samples |= get_bits(&gb, 4);
00172
00173 skip_bits_long(&gb, 64);
00174 skip_bits_long(&gb, 64);
00175
00176 dump_headers(avctx, s);
00177 }
00178
00179 void ff_flac_parse_block_header(const uint8_t *block_header,
00180 int *last, int *type, int *size)
00181 {
00182 int tmp = bytestream_get_byte(&block_header);
00183 if (last)
00184 *last = tmp & 0x80;
00185 if (type)
00186 *type = tmp & 0x7F;
00187 if (size)
00188 *size = bytestream_get_be24(&block_header);
00189 }
00190
00198 static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
00199 {
00200 int metadata_type, metadata_size;
00201
00202 if (buf_size < FLAC_STREAMINFO_SIZE+8) {
00203
00204 return 0;
00205 }
00206 ff_flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
00207 if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
00208 metadata_size != FLAC_STREAMINFO_SIZE) {
00209 return AVERROR_INVALIDDATA;
00210 }
00211 ff_flac_parse_streaminfo(s->avctx, (FLACStreaminfo *)s, &buf[8]);
00212 allocate_buffers(s);
00213 s->got_streaminfo = 1;
00214
00215 return 0;
00216 }
00217
00224 static int get_metadata_size(const uint8_t *buf, int buf_size)
00225 {
00226 int metadata_last, metadata_size;
00227 const uint8_t *buf_end = buf + buf_size;
00228
00229 buf += 4;
00230 do {
00231 if (buf_end - buf < 4)
00232 return 0;
00233 ff_flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
00234 buf += 4;
00235 if (buf_end - buf < metadata_size) {
00236
00237 return 0;
00238 }
00239 buf += metadata_size;
00240 } while (!metadata_last);
00241
00242 return buf_size - (buf_end - buf);
00243 }
00244
00245 static int decode_residuals(FLACContext *s, int channel, int pred_order)
00246 {
00247 int i, tmp, partition, method_type, rice_order;
00248 int sample = 0, samples;
00249
00250 method_type = get_bits(&s->gb, 2);
00251 if (method_type > 1) {
00252 av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
00253 method_type);
00254 return -1;
00255 }
00256
00257 rice_order = get_bits(&s->gb, 4);
00258
00259 samples= s->blocksize >> rice_order;
00260 if (pred_order > samples) {
00261 av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
00262 pred_order, samples);
00263 return -1;
00264 }
00265
00266 sample=
00267 i= pred_order;
00268 for (partition = 0; partition < (1 << rice_order); partition++) {
00269 tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
00270 if (tmp == (method_type == 0 ? 15 : 31)) {
00271 tmp = get_bits(&s->gb, 5);
00272 for (; i < samples; i++, sample++)
00273 s->decoded[channel][sample] = get_sbits_long(&s->gb, tmp);
00274 } else {
00275 for (; i < samples; i++, sample++) {
00276 s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
00277 }
00278 }
00279 i= 0;
00280 }
00281
00282 return 0;
00283 }
00284
00285 static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
00286 {
00287 const int blocksize = s->blocksize;
00288 int32_t *decoded = s->decoded[channel];
00289 int av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d), i;
00290
00291
00292 for (i = 0; i < pred_order; i++) {
00293 decoded[i] = get_sbits_long(&s->gb, s->curr_bps);
00294 }
00295
00296 if (decode_residuals(s, channel, pred_order) < 0)
00297 return -1;
00298
00299 if (pred_order > 0)
00300 a = decoded[pred_order-1];
00301 if (pred_order > 1)
00302 b = a - decoded[pred_order-2];
00303 if (pred_order > 2)
00304 c = b - decoded[pred_order-2] + decoded[pred_order-3];
00305 if (pred_order > 3)
00306 d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
00307
00308 switch (pred_order) {
00309 case 0:
00310 break;
00311 case 1:
00312 for (i = pred_order; i < blocksize; i++)
00313 decoded[i] = a += decoded[i];
00314 break;
00315 case 2:
00316 for (i = pred_order; i < blocksize; i++)
00317 decoded[i] = a += b += decoded[i];
00318 break;
00319 case 3:
00320 for (i = pred_order; i < blocksize; i++)
00321 decoded[i] = a += b += c += decoded[i];
00322 break;
00323 case 4:
00324 for (i = pred_order; i < blocksize; i++)
00325 decoded[i] = a += b += c += d += decoded[i];
00326 break;
00327 default:
00328 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
00329 return -1;
00330 }
00331
00332 return 0;
00333 }
00334
00335 static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
00336 {
00337 int i, j;
00338 int coeff_prec, qlevel;
00339 int coeffs[32];
00340 int32_t *decoded = s->decoded[channel];
00341
00342
00343 for (i = 0; i < pred_order; i++) {
00344 decoded[i] = get_sbits_long(&s->gb, s->curr_bps);
00345 }
00346
00347 coeff_prec = get_bits(&s->gb, 4) + 1;
00348 if (coeff_prec == 16) {
00349 av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
00350 return -1;
00351 }
00352 qlevel = get_sbits(&s->gb, 5);
00353 if (qlevel < 0) {
00354 av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
00355 qlevel);
00356 return -1;
00357 }
00358
00359 for (i = 0; i < pred_order; i++) {
00360 coeffs[i] = get_sbits(&s->gb, coeff_prec);
00361 }
00362
00363 if (decode_residuals(s, channel, pred_order) < 0)
00364 return -1;
00365
00366 if (s->bps > 16) {
00367 int64_t sum;
00368 for (i = pred_order; i < s->blocksize; i++) {
00369 sum = 0;
00370 for (j = 0; j < pred_order; j++)
00371 sum += (int64_t)coeffs[j] * decoded[i-j-1];
00372 decoded[i] += sum >> qlevel;
00373 }
00374 } else {
00375 for (i = pred_order; i < s->blocksize-1; i += 2) {
00376 int c;
00377 int d = decoded[i-pred_order];
00378 int s0 = 0, s1 = 0;
00379 for (j = pred_order-1; j > 0; j--) {
00380 c = coeffs[j];
00381 s0 += c*d;
00382 d = decoded[i-j];
00383 s1 += c*d;
00384 }
00385 c = coeffs[0];
00386 s0 += c*d;
00387 d = decoded[i] += s0 >> qlevel;
00388 s1 += c*d;
00389 decoded[i+1] += s1 >> qlevel;
00390 }
00391 if (i < s->blocksize) {
00392 int sum = 0;
00393 for (j = 0; j < pred_order; j++)
00394 sum += coeffs[j] * decoded[i-j-1];
00395 decoded[i] += sum >> qlevel;
00396 }
00397 }
00398
00399 return 0;
00400 }
00401
00402 static inline int decode_subframe(FLACContext *s, int channel)
00403 {
00404 int type, wasted = 0;
00405 int i, tmp;
00406
00407 s->curr_bps = s->bps;
00408 if (channel == 0) {
00409 if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
00410 s->curr_bps++;
00411 } else {
00412 if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
00413 s->curr_bps++;
00414 }
00415
00416 if (get_bits1(&s->gb)) {
00417 av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
00418 return -1;
00419 }
00420 type = get_bits(&s->gb, 6);
00421
00422 if (get_bits1(&s->gb)) {
00423 int left = get_bits_left(&s->gb);
00424 wasted = 1;
00425 if ( left < 0 ||
00426 (left < s->curr_bps && !show_bits_long(&s->gb, left)) ||
00427 !show_bits_long(&s->gb, s->curr_bps)) {
00428 av_log(s->avctx, AV_LOG_ERROR,
00429 "Invalid number of wasted bits > available bits (%d) - left=%d\n",
00430 s->curr_bps, left);
00431 return AVERROR_INVALIDDATA;
00432 }
00433 while (!get_bits1(&s->gb))
00434 wasted++;
00435 s->curr_bps -= wasted;
00436 }
00437 if (s->curr_bps > 32) {
00438 av_log_missing_feature(s->avctx, "decorrelated bit depth > 32", 0);
00439 return -1;
00440 }
00441
00442
00443 if (type == 0) {
00444 tmp = get_sbits_long(&s->gb, s->curr_bps);
00445 for (i = 0; i < s->blocksize; i++)
00446 s->decoded[channel][i] = tmp;
00447 } else if (type == 1) {
00448 for (i = 0; i < s->blocksize; i++)
00449 s->decoded[channel][i] = get_sbits_long(&s->gb, s->curr_bps);
00450 } else if ((type >= 8) && (type <= 12)) {
00451 if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
00452 return -1;
00453 } else if (type >= 32) {
00454 if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
00455 return -1;
00456 } else {
00457 av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
00458 return -1;
00459 }
00460
00461 if (wasted) {
00462 int i;
00463 for (i = 0; i < s->blocksize; i++)
00464 s->decoded[channel][i] <<= wasted;
00465 }
00466
00467 return 0;
00468 }
00469
00470 static int decode_frame(FLACContext *s)
00471 {
00472 int i;
00473 GetBitContext *gb = &s->gb;
00474 FLACFrameInfo fi;
00475
00476 if (ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) {
00477 av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
00478 return -1;
00479 }
00480
00481 if (s->channels && fi.channels != s->channels) {
00482 av_log(s->avctx, AV_LOG_ERROR, "switching channel layout mid-stream "
00483 "is not supported\n");
00484 return -1;
00485 }
00486 s->channels = s->avctx->channels = fi.channels;
00487 s->ch_mode = fi.ch_mode;
00488
00489 if (!s->bps && !fi.bps) {
00490 av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
00491 return -1;
00492 }
00493 if (!fi.bps) {
00494 fi.bps = s->bps;
00495 } else if (s->bps && fi.bps != s->bps) {
00496 av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
00497 "supported\n");
00498 return -1;
00499 }
00500 s->bps = s->avctx->bits_per_raw_sample = fi.bps;
00501
00502 if (s->bps > 16) {
00503 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00504 s->sample_shift = 32 - s->bps;
00505 s->is32 = 1;
00506 } else {
00507 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00508 s->sample_shift = 16 - s->bps;
00509 s->is32 = 0;
00510 }
00511
00512 if (!s->max_blocksize)
00513 s->max_blocksize = FLAC_MAX_BLOCKSIZE;
00514 if (fi.blocksize > s->max_blocksize) {
00515 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
00516 s->max_blocksize);
00517 return -1;
00518 }
00519 s->blocksize = fi.blocksize;
00520
00521 if (!s->samplerate && !fi.samplerate) {
00522 av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
00523 " or frame header\n");
00524 return -1;
00525 }
00526 if (fi.samplerate == 0) {
00527 fi.samplerate = s->samplerate;
00528 } else if (s->samplerate && fi.samplerate != s->samplerate) {
00529 av_log(s->avctx, AV_LOG_WARNING, "sample rate changed from %d to %d\n",
00530 s->samplerate, fi.samplerate);
00531 }
00532 s->samplerate = s->avctx->sample_rate = fi.samplerate;
00533
00534 if (!s->got_streaminfo) {
00535 allocate_buffers(s);
00536 s->got_streaminfo = 1;
00537 dump_headers(s->avctx, (FLACStreaminfo *)s);
00538 }
00539
00540
00541
00542
00543 for (i = 0; i < s->channels; i++) {
00544 if (decode_subframe(s, i) < 0)
00545 return -1;
00546 }
00547
00548 align_get_bits(gb);
00549
00550
00551 skip_bits(gb, 16);
00552
00553 return 0;
00554 }
00555
00556 static int flac_decode_frame(AVCodecContext *avctx,
00557 void *data, int *data_size,
00558 AVPacket *avpkt)
00559 {
00560 const uint8_t *buf = avpkt->data;
00561 int buf_size = avpkt->size;
00562 FLACContext *s = avctx->priv_data;
00563 int i, j = 0, bytes_read = 0;
00564 int16_t *samples_16 = data;
00565 int32_t *samples_32 = data;
00566 int alloc_data_size= *data_size;
00567 int output_size;
00568
00569 *data_size=0;
00570
00571 if (s->max_framesize == 0) {
00572 s->max_framesize =
00573 ff_flac_get_max_frame_size(s->max_blocksize ? s->max_blocksize : FLAC_MAX_BLOCKSIZE,
00574 FLAC_MAX_CHANNELS, 32);
00575 }
00576
00577
00578
00579
00580 if (buf_size < FLAC_MIN_FRAME_SIZE)
00581 return buf_size;
00582
00583
00584 if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
00585 if (!s->got_streaminfo && parse_streaminfo(s, buf, buf_size)) {
00586 av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
00587 return -1;
00588 }
00589 return get_metadata_size(buf, buf_size);
00590 }
00591
00592
00593 init_get_bits(&s->gb, buf, buf_size*8);
00594 if (decode_frame(s) < 0) {
00595 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
00596 return -1;
00597 }
00598 bytes_read = (get_bits_count(&s->gb)+7)/8;
00599
00600
00601 output_size = s->blocksize * s->channels * (s->is32 ? 4 : 2);
00602 if (output_size > alloc_data_size) {
00603 av_log(s->avctx, AV_LOG_ERROR, "output data size is larger than "
00604 "allocated data size\n");
00605 return -1;
00606 }
00607 *data_size = output_size;
00608
00609 #define DECORRELATE(left, right)\
00610 assert(s->channels == 2);\
00611 for (i = 0; i < s->blocksize; i++) {\
00612 int a= s->decoded[0][i];\
00613 int b= s->decoded[1][i];\
00614 if (s->is32) {\
00615 *samples_32++ = (left) << s->sample_shift;\
00616 *samples_32++ = (right) << s->sample_shift;\
00617 } else {\
00618 *samples_16++ = (left) << s->sample_shift;\
00619 *samples_16++ = (right) << s->sample_shift;\
00620 }\
00621 }\
00622 break;
00623
00624 switch (s->ch_mode) {
00625 case FLAC_CHMODE_INDEPENDENT:
00626 for (j = 0; j < s->blocksize; j++) {
00627 for (i = 0; i < s->channels; i++) {
00628 if (s->is32)
00629 *samples_32++ = s->decoded[i][j] << s->sample_shift;
00630 else
00631 *samples_16++ = s->decoded[i][j] << s->sample_shift;
00632 }
00633 }
00634 break;
00635 case FLAC_CHMODE_LEFT_SIDE:
00636 DECORRELATE(a,a-b)
00637 case FLAC_CHMODE_RIGHT_SIDE:
00638 DECORRELATE(a+b,b)
00639 case FLAC_CHMODE_MID_SIDE:
00640 DECORRELATE( (a-=b>>1) + b, a)
00641 }
00642
00643 if (bytes_read > buf_size) {
00644 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
00645 return -1;
00646 }
00647 if (bytes_read < buf_size) {
00648 av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
00649 buf_size - bytes_read, buf_size);
00650 }
00651
00652 return bytes_read;
00653 }
00654
00655 static av_cold int flac_decode_close(AVCodecContext *avctx)
00656 {
00657 FLACContext *s = avctx->priv_data;
00658 int i;
00659
00660 for (i = 0; i < s->channels; i++) {
00661 av_freep(&s->decoded[i]);
00662 }
00663
00664 return 0;
00665 }
00666
00667 AVCodec ff_flac_decoder = {
00668 "flac",
00669 AVMEDIA_TYPE_AUDIO,
00670 CODEC_ID_FLAC,
00671 sizeof(FLACContext),
00672 flac_decode_init,
00673 NULL,
00674 flac_decode_close,
00675 flac_decode_frame,
00676 .long_name= NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
00677 };