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