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 wasted = 1;
00426 while (!get_bits1(&s->gb))
00427 wasted++;
00428 s->curr_bps -= wasted;
00429 }
00430 if (s->curr_bps > 32) {
00431 av_log_missing_feature(s->avctx, "decorrelated bit depth > 32", 0);
00432 return -1;
00433 }
00434
00435
00436 if (type == 0) {
00437 tmp = get_sbits_long(&s->gb, s->curr_bps);
00438 for (i = 0; i < s->blocksize; i++)
00439 s->decoded[channel][i] = tmp;
00440 } else if (type == 1) {
00441 for (i = 0; i < s->blocksize; i++)
00442 s->decoded[channel][i] = get_sbits_long(&s->gb, s->curr_bps);
00443 } else if ((type >= 8) && (type <= 12)) {
00444 if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
00445 return -1;
00446 } else if (type >= 32) {
00447 if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
00448 return -1;
00449 } else {
00450 av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
00451 return -1;
00452 }
00453
00454 if (wasted) {
00455 int i;
00456 for (i = 0; i < s->blocksize; i++)
00457 s->decoded[channel][i] <<= wasted;
00458 }
00459
00460 return 0;
00461 }
00462
00463 static int decode_frame(FLACContext *s)
00464 {
00465 int i;
00466 GetBitContext *gb = &s->gb;
00467 FLACFrameInfo fi;
00468
00469 if (ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) {
00470 av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
00471 return -1;
00472 }
00473
00474 if (s->channels && fi.channels != s->channels) {
00475 av_log(s->avctx, AV_LOG_ERROR, "switching channel layout mid-stream "
00476 "is not supported\n");
00477 return -1;
00478 }
00479 s->channels = s->avctx->channels = fi.channels;
00480 s->ch_mode = fi.ch_mode;
00481
00482 if (!s->bps && !fi.bps) {
00483 av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
00484 return -1;
00485 }
00486 if (!fi.bps) {
00487 fi.bps = s->bps;
00488 } else if (s->bps && fi.bps != s->bps) {
00489 av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
00490 "supported\n");
00491 return -1;
00492 }
00493 s->bps = s->avctx->bits_per_raw_sample = fi.bps;
00494
00495 if (s->bps > 16) {
00496 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00497 s->sample_shift = 32 - s->bps;
00498 s->is32 = 1;
00499 } else {
00500 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00501 s->sample_shift = 16 - s->bps;
00502 s->is32 = 0;
00503 }
00504
00505 if (!s->max_blocksize)
00506 s->max_blocksize = FLAC_MAX_BLOCKSIZE;
00507 if (fi.blocksize > s->max_blocksize) {
00508 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
00509 s->max_blocksize);
00510 return -1;
00511 }
00512 s->blocksize = fi.blocksize;
00513
00514 if (!s->samplerate && !fi.samplerate) {
00515 av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
00516 " or frame header\n");
00517 return -1;
00518 }
00519 if (fi.samplerate == 0) {
00520 fi.samplerate = s->samplerate;
00521 } else if (s->samplerate && fi.samplerate != s->samplerate) {
00522 av_log(s->avctx, AV_LOG_WARNING, "sample rate changed from %d to %d\n",
00523 s->samplerate, fi.samplerate);
00524 }
00525 s->samplerate = s->avctx->sample_rate = fi.samplerate;
00526
00527 if (!s->got_streaminfo) {
00528 allocate_buffers(s);
00529 s->got_streaminfo = 1;
00530 dump_headers(s->avctx, (FLACStreaminfo *)s);
00531 }
00532
00533
00534
00535
00536 for (i = 0; i < s->channels; i++) {
00537 if (decode_subframe(s, i) < 0)
00538 return -1;
00539 }
00540
00541 align_get_bits(gb);
00542
00543
00544 skip_bits(gb, 16);
00545
00546 return 0;
00547 }
00548
00549 static int flac_decode_frame(AVCodecContext *avctx, void *data,
00550 int *got_frame_ptr, AVPacket *avpkt)
00551 {
00552 const uint8_t *buf = avpkt->data;
00553 int buf_size = avpkt->size;
00554 FLACContext *s = avctx->priv_data;
00555 int i, j = 0, bytes_read = 0;
00556 int16_t *samples_16;
00557 int32_t *samples_32;
00558 int ret;
00559
00560 *got_frame_ptr = 0;
00561
00562 if (s->max_framesize == 0) {
00563 s->max_framesize =
00564 ff_flac_get_max_frame_size(s->max_blocksize ? s->max_blocksize : FLAC_MAX_BLOCKSIZE,
00565 FLAC_MAX_CHANNELS, 32);
00566 }
00567
00568
00569
00570
00571 if (buf_size < FLAC_MIN_FRAME_SIZE)
00572 return buf_size;
00573
00574
00575 if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
00576 if (!s->got_streaminfo && parse_streaminfo(s, buf, buf_size)) {
00577 av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
00578 return -1;
00579 }
00580 return get_metadata_size(buf, buf_size);
00581 }
00582
00583
00584 init_get_bits(&s->gb, buf, buf_size*8);
00585 if (decode_frame(s) < 0) {
00586 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
00587 return -1;
00588 }
00589 bytes_read = (get_bits_count(&s->gb)+7)/8;
00590
00591
00592 s->frame.nb_samples = s->blocksize;
00593 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00594 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00595 return ret;
00596 }
00597 samples_16 = (int16_t *)s->frame.data[0];
00598 samples_32 = (int32_t *)s->frame.data[0];
00599
00600 #define DECORRELATE(left, right)\
00601 assert(s->channels == 2);\
00602 for (i = 0; i < s->blocksize; i++) {\
00603 int a= s->decoded[0][i];\
00604 int b= s->decoded[1][i];\
00605 if (s->is32) {\
00606 *samples_32++ = (left) << s->sample_shift;\
00607 *samples_32++ = (right) << s->sample_shift;\
00608 } else {\
00609 *samples_16++ = (left) << s->sample_shift;\
00610 *samples_16++ = (right) << s->sample_shift;\
00611 }\
00612 }\
00613 break;
00614
00615 switch (s->ch_mode) {
00616 case FLAC_CHMODE_INDEPENDENT:
00617 for (j = 0; j < s->blocksize; j++) {
00618 for (i = 0; i < s->channels; i++) {
00619 if (s->is32)
00620 *samples_32++ = s->decoded[i][j] << s->sample_shift;
00621 else
00622 *samples_16++ = s->decoded[i][j] << s->sample_shift;
00623 }
00624 }
00625 break;
00626 case FLAC_CHMODE_LEFT_SIDE:
00627 DECORRELATE(a,a-b)
00628 case FLAC_CHMODE_RIGHT_SIDE:
00629 DECORRELATE(a+b,b)
00630 case FLAC_CHMODE_MID_SIDE:
00631 DECORRELATE( (a-=b>>1) + b, a)
00632 }
00633
00634 if (bytes_read > buf_size) {
00635 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
00636 return -1;
00637 }
00638 if (bytes_read < buf_size) {
00639 av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
00640 buf_size - bytes_read, buf_size);
00641 }
00642
00643 *got_frame_ptr = 1;
00644 *(AVFrame *)data = s->frame;
00645
00646 return bytes_read;
00647 }
00648
00649 static av_cold int flac_decode_close(AVCodecContext *avctx)
00650 {
00651 FLACContext *s = avctx->priv_data;
00652 int i;
00653
00654 for (i = 0; i < s->channels; i++) {
00655 av_freep(&s->decoded[i]);
00656 }
00657
00658 return 0;
00659 }
00660
00661 AVCodec ff_flac_decoder = {
00662 .name = "flac",
00663 .type = AVMEDIA_TYPE_AUDIO,
00664 .id = CODEC_ID_FLAC,
00665 .priv_data_size = sizeof(FLACContext),
00666 .init = flac_decode_init,
00667 .close = flac_decode_close,
00668 .decode = flac_decode_frame,
00669 .capabilities = CODEC_CAP_DR1,
00670 .long_name= NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
00671 };