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 ff_flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
00232 buf += 4;
00233 if (buf + metadata_size > buf_end) {
00234
00235 return 0;
00236 }
00237 buf += metadata_size;
00238 } while (!metadata_last);
00239
00240 return buf_size - (buf_end - buf);
00241 }
00242
00243 static int decode_residuals(FLACContext *s, int channel, int pred_order)
00244 {
00245 int i, tmp, partition, method_type, rice_order;
00246 int sample = 0, samples;
00247
00248 method_type = get_bits(&s->gb, 2);
00249 if (method_type > 1) {
00250 av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
00251 method_type);
00252 return -1;
00253 }
00254
00255 rice_order = get_bits(&s->gb, 4);
00256
00257 samples= s->blocksize >> rice_order;
00258 if (pred_order > samples) {
00259 av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
00260 pred_order, samples);
00261 return -1;
00262 }
00263
00264 sample=
00265 i= pred_order;
00266 for (partition = 0; partition < (1 << rice_order); partition++) {
00267 tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
00268 if (tmp == (method_type == 0 ? 15 : 31)) {
00269 tmp = get_bits(&s->gb, 5);
00270 for (; i < samples; i++, sample++)
00271 s->decoded[channel][sample] = get_sbits_long(&s->gb, tmp);
00272 } else {
00273 for (; i < samples; i++, sample++) {
00274 s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
00275 }
00276 }
00277 i= 0;
00278 }
00279
00280 return 0;
00281 }
00282
00283 static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
00284 {
00285 const int blocksize = s->blocksize;
00286 int32_t *decoded = s->decoded[channel];
00287 int av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d), i;
00288
00289
00290 for (i = 0; i < pred_order; i++) {
00291 decoded[i] = get_sbits_long(&s->gb, s->curr_bps);
00292 }
00293
00294 if (decode_residuals(s, channel, pred_order) < 0)
00295 return -1;
00296
00297 if (pred_order > 0)
00298 a = decoded[pred_order-1];
00299 if (pred_order > 1)
00300 b = a - decoded[pred_order-2];
00301 if (pred_order > 2)
00302 c = b - decoded[pred_order-2] + decoded[pred_order-3];
00303 if (pred_order > 3)
00304 d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
00305
00306 switch (pred_order) {
00307 case 0:
00308 break;
00309 case 1:
00310 for (i = pred_order; i < blocksize; i++)
00311 decoded[i] = a += decoded[i];
00312 break;
00313 case 2:
00314 for (i = pred_order; i < blocksize; i++)
00315 decoded[i] = a += b += decoded[i];
00316 break;
00317 case 3:
00318 for (i = pred_order; i < blocksize; i++)
00319 decoded[i] = a += b += c += decoded[i];
00320 break;
00321 case 4:
00322 for (i = pred_order; i < blocksize; i++)
00323 decoded[i] = a += b += c += d += decoded[i];
00324 break;
00325 default:
00326 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
00327 return -1;
00328 }
00329
00330 return 0;
00331 }
00332
00333 static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
00334 {
00335 int i, j;
00336 int coeff_prec, qlevel;
00337 int coeffs[32];
00338 int32_t *decoded = s->decoded[channel];
00339
00340
00341 for (i = 0; i < pred_order; i++) {
00342 decoded[i] = get_sbits_long(&s->gb, s->curr_bps);
00343 }
00344
00345 coeff_prec = get_bits(&s->gb, 4) + 1;
00346 if (coeff_prec == 16) {
00347 av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
00348 return -1;
00349 }
00350 qlevel = get_sbits(&s->gb, 5);
00351 if (qlevel < 0) {
00352 av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
00353 qlevel);
00354 return -1;
00355 }
00356
00357 for (i = 0; i < pred_order; i++) {
00358 coeffs[i] = get_sbits(&s->gb, coeff_prec);
00359 }
00360
00361 if (decode_residuals(s, channel, pred_order) < 0)
00362 return -1;
00363
00364 if (s->bps > 16) {
00365 int64_t sum;
00366 for (i = pred_order; i < s->blocksize; i++) {
00367 sum = 0;
00368 for (j = 0; j < pred_order; j++)
00369 sum += (int64_t)coeffs[j] * decoded[i-j-1];
00370 decoded[i] += sum >> qlevel;
00371 }
00372 } else {
00373 for (i = pred_order; i < s->blocksize-1; i += 2) {
00374 int c;
00375 int d = decoded[i-pred_order];
00376 int s0 = 0, s1 = 0;
00377 for (j = pred_order-1; j > 0; j--) {
00378 c = coeffs[j];
00379 s0 += c*d;
00380 d = decoded[i-j];
00381 s1 += c*d;
00382 }
00383 c = coeffs[0];
00384 s0 += c*d;
00385 d = decoded[i] += s0 >> qlevel;
00386 s1 += c*d;
00387 decoded[i+1] += s1 >> qlevel;
00388 }
00389 if (i < s->blocksize) {
00390 int sum = 0;
00391 for (j = 0; j < pred_order; j++)
00392 sum += coeffs[j] * decoded[i-j-1];
00393 decoded[i] += sum >> qlevel;
00394 }
00395 }
00396
00397 return 0;
00398 }
00399
00400 static inline int decode_subframe(FLACContext *s, int channel)
00401 {
00402 int type, wasted = 0;
00403 int i, tmp;
00404
00405 s->curr_bps = s->bps;
00406 if (channel == 0) {
00407 if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
00408 s->curr_bps++;
00409 } else {
00410 if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
00411 s->curr_bps++;
00412 }
00413
00414 if (get_bits1(&s->gb)) {
00415 av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
00416 return -1;
00417 }
00418 type = get_bits(&s->gb, 6);
00419
00420 if (get_bits1(&s->gb)) {
00421 wasted = 1;
00422 while (!get_bits1(&s->gb))
00423 wasted++;
00424 s->curr_bps -= wasted;
00425 }
00426 if (s->curr_bps > 32) {
00427 av_log_missing_feature(s->avctx, "decorrelated bit depth > 32", 0);
00428 return -1;
00429 }
00430
00431
00432 if (type == 0) {
00433 tmp = get_sbits_long(&s->gb, s->curr_bps);
00434 for (i = 0; i < s->blocksize; i++)
00435 s->decoded[channel][i] = tmp;
00436 } else if (type == 1) {
00437 for (i = 0; i < s->blocksize; i++)
00438 s->decoded[channel][i] = get_sbits_long(&s->gb, s->curr_bps);
00439 } else if ((type >= 8) && (type <= 12)) {
00440 if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
00441 return -1;
00442 } else if (type >= 32) {
00443 if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
00444 return -1;
00445 } else {
00446 av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
00447 return -1;
00448 }
00449
00450 if (wasted) {
00451 int i;
00452 for (i = 0; i < s->blocksize; i++)
00453 s->decoded[channel][i] <<= wasted;
00454 }
00455
00456 return 0;
00457 }
00458
00459 static int decode_frame(FLACContext *s)
00460 {
00461 int i;
00462 GetBitContext *gb = &s->gb;
00463 FLACFrameInfo fi;
00464
00465 if (ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) {
00466 av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
00467 return -1;
00468 }
00469
00470 if (s->channels && fi.channels != s->channels) {
00471 av_log(s->avctx, AV_LOG_ERROR, "switching channel layout mid-stream "
00472 "is not supported\n");
00473 return -1;
00474 }
00475 s->channels = s->avctx->channels = fi.channels;
00476 s->ch_mode = fi.ch_mode;
00477
00478 if (!s->bps && !fi.bps) {
00479 av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
00480 return -1;
00481 }
00482 if (!fi.bps) {
00483 fi.bps = s->bps;
00484 } else if (s->bps && fi.bps != s->bps) {
00485 av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
00486 "supported\n");
00487 return -1;
00488 }
00489 s->bps = s->avctx->bits_per_raw_sample = fi.bps;
00490
00491 if (s->bps > 16) {
00492 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00493 s->sample_shift = 32 - s->bps;
00494 s->is32 = 1;
00495 } else {
00496 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00497 s->sample_shift = 16 - s->bps;
00498 s->is32 = 0;
00499 }
00500
00501 if (!s->max_blocksize)
00502 s->max_blocksize = FLAC_MAX_BLOCKSIZE;
00503 if (fi.blocksize > s->max_blocksize) {
00504 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
00505 s->max_blocksize);
00506 return -1;
00507 }
00508 s->blocksize = fi.blocksize;
00509
00510 if (!s->samplerate && !fi.samplerate) {
00511 av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
00512 " or frame header\n");
00513 return -1;
00514 }
00515 if (fi.samplerate == 0) {
00516 fi.samplerate = s->samplerate;
00517 } else if (s->samplerate && fi.samplerate != s->samplerate) {
00518 av_log(s->avctx, AV_LOG_WARNING, "sample rate changed from %d to %d\n",
00519 s->samplerate, fi.samplerate);
00520 }
00521 s->samplerate = s->avctx->sample_rate = fi.samplerate;
00522
00523 if (!s->got_streaminfo) {
00524 allocate_buffers(s);
00525 s->got_streaminfo = 1;
00526 dump_headers(s->avctx, (FLACStreaminfo *)s);
00527 }
00528
00529
00530
00531
00532 for (i = 0; i < s->channels; i++) {
00533 if (decode_subframe(s, i) < 0)
00534 return -1;
00535 }
00536
00537 align_get_bits(gb);
00538
00539
00540 skip_bits(gb, 16);
00541
00542 return 0;
00543 }
00544
00545 static int flac_decode_frame(AVCodecContext *avctx,
00546 void *data, int *data_size,
00547 AVPacket *avpkt)
00548 {
00549 const uint8_t *buf = avpkt->data;
00550 int buf_size = avpkt->size;
00551 FLACContext *s = avctx->priv_data;
00552 int i, j = 0, bytes_read = 0;
00553 int16_t *samples_16 = data;
00554 int32_t *samples_32 = data;
00555 int alloc_data_size= *data_size;
00556 int output_size;
00557
00558 *data_size=0;
00559
00560 if (s->max_framesize == 0) {
00561 s->max_framesize =
00562 ff_flac_get_max_frame_size(s->max_blocksize ? s->max_blocksize : FLAC_MAX_BLOCKSIZE,
00563 FLAC_MAX_CHANNELS, 32);
00564 }
00565
00566
00567
00568
00569 if (buf_size < FLAC_MIN_FRAME_SIZE)
00570 return buf_size;
00571
00572
00573 if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
00574 if (!s->got_streaminfo && parse_streaminfo(s, buf, buf_size)) {
00575 av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
00576 return -1;
00577 }
00578 return get_metadata_size(buf, buf_size);
00579 }
00580
00581
00582 init_get_bits(&s->gb, buf, buf_size*8);
00583 if (decode_frame(s) < 0) {
00584 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
00585 return -1;
00586 }
00587 bytes_read = (get_bits_count(&s->gb)+7)/8;
00588
00589
00590 output_size = s->blocksize * s->channels * (s->is32 ? 4 : 2);
00591 if (output_size > alloc_data_size) {
00592 av_log(s->avctx, AV_LOG_ERROR, "output data size is larger than "
00593 "allocated data size\n");
00594 return -1;
00595 }
00596 *data_size = output_size;
00597
00598 #define DECORRELATE(left, right)\
00599 assert(s->channels == 2);\
00600 for (i = 0; i < s->blocksize; i++) {\
00601 int a= s->decoded[0][i];\
00602 int b= s->decoded[1][i];\
00603 if (s->is32) {\
00604 *samples_32++ = (left) << s->sample_shift;\
00605 *samples_32++ = (right) << s->sample_shift;\
00606 } else {\
00607 *samples_16++ = (left) << s->sample_shift;\
00608 *samples_16++ = (right) << s->sample_shift;\
00609 }\
00610 }\
00611 break;
00612
00613 switch (s->ch_mode) {
00614 case FLAC_CHMODE_INDEPENDENT:
00615 for (j = 0; j < s->blocksize; j++) {
00616 for (i = 0; i < s->channels; i++) {
00617 if (s->is32)
00618 *samples_32++ = s->decoded[i][j] << s->sample_shift;
00619 else
00620 *samples_16++ = s->decoded[i][j] << s->sample_shift;
00621 }
00622 }
00623 break;
00624 case FLAC_CHMODE_LEFT_SIDE:
00625 DECORRELATE(a,a-b)
00626 case FLAC_CHMODE_RIGHT_SIDE:
00627 DECORRELATE(a+b,b)
00628 case FLAC_CHMODE_MID_SIDE:
00629 DECORRELATE( (a-=b>>1) + b, a)
00630 }
00631
00632 if (bytes_read > buf_size) {
00633 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
00634 return -1;
00635 }
00636 if (bytes_read < buf_size) {
00637 av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
00638 buf_size - bytes_read, buf_size);
00639 }
00640
00641 return bytes_read;
00642 }
00643
00644 static av_cold int flac_decode_close(AVCodecContext *avctx)
00645 {
00646 FLACContext *s = avctx->priv_data;
00647 int i;
00648
00649 for (i = 0; i < s->channels; i++) {
00650 av_freep(&s->decoded[i]);
00651 }
00652
00653 return 0;
00654 }
00655
00656 AVCodec ff_flac_decoder = {
00657 "flac",
00658 AVMEDIA_TYPE_AUDIO,
00659 CODEC_ID_FLAC,
00660 sizeof(FLACContext),
00661 flac_decode_init,
00662 NULL,
00663 flac_decode_close,
00664 flac_decode_frame,
00665 .long_name= NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
00666 };