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