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/avassert.h"
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 #include "flacdsp.h"
00047
00048 typedef struct FLACContext {
00049 FLACSTREAMINFO
00050
00051 AVCodecContext *avctx;
00052 AVFrame frame;
00053 GetBitContext gb;
00054
00055 int blocksize;
00056 int sample_shift;
00057 int ch_mode;
00058 int got_streaminfo;
00059
00060 int32_t *decoded[FLAC_MAX_CHANNELS];
00061
00062 FLACDSPContext dsp;
00063 } FLACContext;
00064
00065 static const int64_t flac_channel_layouts[6] = {
00066 AV_CH_LAYOUT_MONO,
00067 AV_CH_LAYOUT_STEREO,
00068 AV_CH_LAYOUT_SURROUND,
00069 AV_CH_LAYOUT_QUAD,
00070 AV_CH_LAYOUT_5POINT0,
00071 AV_CH_LAYOUT_5POINT1
00072 };
00073
00074 static void allocate_buffers(FLACContext *s);
00075
00076 static void flac_set_bps(FLACContext *s)
00077 {
00078 enum AVSampleFormat req = s->avctx->request_sample_fmt;
00079 int need32 = s->bps > 16;
00080 int want32 = av_get_bytes_per_sample(req) > 2;
00081 int planar = av_sample_fmt_is_planar(req);
00082
00083 if (need32 || want32) {
00084 if (planar)
00085 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
00086 else
00087 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00088 s->sample_shift = 32 - s->bps;
00089 } else {
00090 if (planar)
00091 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
00092 else
00093 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00094 s->sample_shift = 16 - s->bps;
00095 }
00096 }
00097
00098 static av_cold int flac_decode_init(AVCodecContext *avctx)
00099 {
00100 enum FLACExtradataFormat format;
00101 uint8_t *streaminfo;
00102 FLACContext *s = avctx->priv_data;
00103 s->avctx = avctx;
00104
00105
00106
00107 if (!avctx->extradata)
00108 return 0;
00109
00110 if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo))
00111 return -1;
00112
00113
00114 avpriv_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, streaminfo);
00115 allocate_buffers(s);
00116 flac_set_bps(s);
00117 ff_flacdsp_init(&s->dsp, avctx->sample_fmt, s->bps);
00118 s->got_streaminfo = 1;
00119
00120 avcodec_get_frame_defaults(&s->frame);
00121 avctx->coded_frame = &s->frame;
00122
00123 if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
00124 avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
00125
00126 return 0;
00127 }
00128
00129 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
00130 {
00131 av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize);
00132 av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
00133 av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
00134 av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
00135 av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
00136 }
00137
00138 static void allocate_buffers(FLACContext *s)
00139 {
00140 int i;
00141
00142 av_assert0(s->max_blocksize);
00143
00144 for (i = 0; i < s->channels; i++) {
00145 s->decoded[i] = av_malloc(sizeof(int32_t)*s->max_blocksize);
00146 }
00147 }
00148
00156 static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
00157 {
00158 int metadata_type, metadata_size;
00159
00160 if (buf_size < FLAC_STREAMINFO_SIZE+8) {
00161
00162 return 0;
00163 }
00164 avpriv_flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
00165 if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
00166 metadata_size != FLAC_STREAMINFO_SIZE) {
00167 return AVERROR_INVALIDDATA;
00168 }
00169 avpriv_flac_parse_streaminfo(s->avctx, (FLACStreaminfo *)s, &buf[8]);
00170 allocate_buffers(s);
00171 flac_set_bps(s);
00172 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps);
00173 s->got_streaminfo = 1;
00174
00175 return 0;
00176 }
00177
00184 static int get_metadata_size(const uint8_t *buf, int buf_size)
00185 {
00186 int metadata_last, metadata_size;
00187 const uint8_t *buf_end = buf + buf_size;
00188
00189 buf += 4;
00190 do {
00191 if (buf_end - buf < 4)
00192 return 0;
00193 avpriv_flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
00194 buf += 4;
00195 if (buf_end - buf < metadata_size) {
00196
00197 return 0;
00198 }
00199 buf += metadata_size;
00200 } while (!metadata_last);
00201
00202 return buf_size - (buf_end - buf);
00203 }
00204
00205 static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
00206 {
00207 int i, tmp, partition, method_type, rice_order;
00208 int rice_bits, rice_esc;
00209 int samples;
00210
00211 method_type = get_bits(&s->gb, 2);
00212 if (method_type > 1) {
00213 av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
00214 method_type);
00215 return -1;
00216 }
00217
00218 rice_order = get_bits(&s->gb, 4);
00219
00220 samples= s->blocksize >> rice_order;
00221 if (pred_order > samples) {
00222 av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
00223 pred_order, samples);
00224 return -1;
00225 }
00226
00227 rice_bits = 4 + method_type;
00228 rice_esc = (1 << rice_bits) - 1;
00229
00230 decoded += pred_order;
00231 i= pred_order;
00232 for (partition = 0; partition < (1 << rice_order); partition++) {
00233 tmp = get_bits(&s->gb, rice_bits);
00234 if (tmp == rice_esc) {
00235 tmp = get_bits(&s->gb, 5);
00236 for (; i < samples; i++)
00237 *decoded++ = get_sbits_long(&s->gb, tmp);
00238 } else {
00239 for (; i < samples; i++) {
00240 *decoded++ = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
00241 }
00242 }
00243 i= 0;
00244 }
00245
00246 return 0;
00247 }
00248
00249 static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
00250 int pred_order, int bps)
00251 {
00252 const int blocksize = s->blocksize;
00253 int a, b, c, d, i;
00254
00255
00256 for (i = 0; i < pred_order; i++) {
00257 decoded[i] = get_sbits_long(&s->gb, bps);
00258 }
00259
00260 if (decode_residuals(s, decoded, pred_order) < 0)
00261 return -1;
00262
00263 if (pred_order > 0)
00264 a = decoded[pred_order-1];
00265 if (pred_order > 1)
00266 b = a - decoded[pred_order-2];
00267 if (pred_order > 2)
00268 c = b - decoded[pred_order-2] + decoded[pred_order-3];
00269 if (pred_order > 3)
00270 d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
00271
00272 switch (pred_order) {
00273 case 0:
00274 break;
00275 case 1:
00276 for (i = pred_order; i < blocksize; i++)
00277 decoded[i] = a += decoded[i];
00278 break;
00279 case 2:
00280 for (i = pred_order; i < blocksize; i++)
00281 decoded[i] = a += b += decoded[i];
00282 break;
00283 case 3:
00284 for (i = pred_order; i < blocksize; i++)
00285 decoded[i] = a += b += c += decoded[i];
00286 break;
00287 case 4:
00288 for (i = pred_order; i < blocksize; i++)
00289 decoded[i] = a += b += c += d += decoded[i];
00290 break;
00291 default:
00292 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
00293 return -1;
00294 }
00295
00296 return 0;
00297 }
00298
00299 static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
00300 int bps)
00301 {
00302 int i;
00303 int coeff_prec, qlevel;
00304 int coeffs[32];
00305
00306
00307 for (i = 0; i < pred_order; i++) {
00308 decoded[i] = get_sbits_long(&s->gb, bps);
00309 }
00310
00311 coeff_prec = get_bits(&s->gb, 4) + 1;
00312 if (coeff_prec == 16) {
00313 av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
00314 return -1;
00315 }
00316 qlevel = get_sbits(&s->gb, 5);
00317 if (qlevel < 0) {
00318 av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
00319 qlevel);
00320 return -1;
00321 }
00322
00323 for (i = 0; i < pred_order; i++) {
00324 coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
00325 }
00326
00327 if (decode_residuals(s, decoded, pred_order) < 0)
00328 return -1;
00329
00330 s->dsp.lpc(decoded, coeffs, pred_order, qlevel, s->blocksize);
00331
00332 return 0;
00333 }
00334
00335 static inline int decode_subframe(FLACContext *s, int channel)
00336 {
00337 int32_t *decoded = s->decoded[channel];
00338 int type, wasted = 0;
00339 int bps = s->bps;
00340 int i, tmp;
00341
00342 if (channel == 0) {
00343 if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
00344 bps++;
00345 } else {
00346 if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
00347 bps++;
00348 }
00349
00350 if (get_bits1(&s->gb)) {
00351 av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
00352 return -1;
00353 }
00354 type = get_bits(&s->gb, 6);
00355
00356 if (get_bits1(&s->gb)) {
00357 int left = get_bits_left(&s->gb);
00358 wasted = 1;
00359 if ( left < 0 ||
00360 (left < bps && !show_bits_long(&s->gb, left)) ||
00361 !show_bits_long(&s->gb, bps)) {
00362 av_log(s->avctx, AV_LOG_ERROR,
00363 "Invalid number of wasted bits > available bits (%d) - left=%d\n",
00364 bps, left);
00365 return AVERROR_INVALIDDATA;
00366 }
00367 while (!get_bits1(&s->gb))
00368 wasted++;
00369 bps -= wasted;
00370 }
00371 if (bps > 32) {
00372 av_log_missing_feature(s->avctx, "decorrelated bit depth > 32", 0);
00373 return -1;
00374 }
00375
00376
00377 if (type == 0) {
00378 tmp = get_sbits_long(&s->gb, bps);
00379 for (i = 0; i < s->blocksize; i++)
00380 decoded[i] = tmp;
00381 } else if (type == 1) {
00382 for (i = 0; i < s->blocksize; i++)
00383 decoded[i] = get_sbits_long(&s->gb, bps);
00384 } else if ((type >= 8) && (type <= 12)) {
00385 if (decode_subframe_fixed(s, decoded, type & ~0x8, bps) < 0)
00386 return -1;
00387 } else if (type >= 32) {
00388 if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps) < 0)
00389 return -1;
00390 } else {
00391 av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
00392 return -1;
00393 }
00394
00395 if (wasted) {
00396 int i;
00397 for (i = 0; i < s->blocksize; i++)
00398 decoded[i] <<= wasted;
00399 }
00400
00401 return 0;
00402 }
00403
00404 static int decode_frame(FLACContext *s)
00405 {
00406 int i;
00407 GetBitContext *gb = &s->gb;
00408 FLACFrameInfo fi;
00409
00410 if (ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) {
00411 av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
00412 return -1;
00413 }
00414
00415 if (s->channels && fi.channels != s->channels) {
00416 av_log(s->avctx, AV_LOG_ERROR, "switching channel layout mid-stream "
00417 "is not supported\n");
00418 return -1;
00419 }
00420 s->channels = s->avctx->channels = fi.channels;
00421 s->ch_mode = fi.ch_mode;
00422
00423 if (!s->bps && !fi.bps) {
00424 av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
00425 return -1;
00426 }
00427 if (!fi.bps) {
00428 fi.bps = s->bps;
00429 } else if (s->bps && fi.bps != s->bps) {
00430 av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
00431 "supported\n");
00432 return -1;
00433 }
00434
00435 if (!s->bps) {
00436 s->bps = s->avctx->bits_per_raw_sample = fi.bps;
00437 flac_set_bps(s);
00438 }
00439
00440 if (!s->max_blocksize)
00441 s->max_blocksize = FLAC_MAX_BLOCKSIZE;
00442 if (fi.blocksize > s->max_blocksize) {
00443 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
00444 s->max_blocksize);
00445 return -1;
00446 }
00447 s->blocksize = fi.blocksize;
00448
00449 if (!s->samplerate && !fi.samplerate) {
00450 av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
00451 " or frame header\n");
00452 return -1;
00453 }
00454 if (fi.samplerate == 0) {
00455 fi.samplerate = s->samplerate;
00456 } else if (s->samplerate && fi.samplerate != s->samplerate) {
00457 av_log(s->avctx, AV_LOG_WARNING, "sample rate changed from %d to %d\n",
00458 s->samplerate, fi.samplerate);
00459 }
00460 s->samplerate = s->avctx->sample_rate = fi.samplerate;
00461
00462 if (!s->got_streaminfo) {
00463 allocate_buffers(s);
00464 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps);
00465 s->got_streaminfo = 1;
00466 dump_headers(s->avctx, (FLACStreaminfo *)s);
00467 }
00468
00469
00470
00471
00472 for (i = 0; i < s->channels; i++) {
00473 if (decode_subframe(s, i) < 0)
00474 return -1;
00475 }
00476
00477 align_get_bits(gb);
00478
00479
00480 skip_bits(gb, 16);
00481
00482 return 0;
00483 }
00484
00485 static int flac_decode_frame(AVCodecContext *avctx, void *data,
00486 int *got_frame_ptr, AVPacket *avpkt)
00487 {
00488 const uint8_t *buf = avpkt->data;
00489 int buf_size = avpkt->size;
00490 FLACContext *s = avctx->priv_data;
00491 int bytes_read = 0;
00492 int ret;
00493
00494 *got_frame_ptr = 0;
00495
00496 if (s->max_framesize == 0) {
00497 s->max_framesize =
00498 ff_flac_get_max_frame_size(s->max_blocksize ? s->max_blocksize : FLAC_MAX_BLOCKSIZE,
00499 FLAC_MAX_CHANNELS, 32);
00500 }
00501
00502
00503
00504
00505 if (buf_size < FLAC_MIN_FRAME_SIZE)
00506 return buf_size;
00507
00508
00509 if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
00510 if (!s->got_streaminfo && parse_streaminfo(s, buf, buf_size)) {
00511 av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
00512 return -1;
00513 }
00514 return get_metadata_size(buf, buf_size);
00515 }
00516
00517
00518 init_get_bits(&s->gb, buf, buf_size*8);
00519 if (decode_frame(s) < 0) {
00520 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
00521 return -1;
00522 }
00523 bytes_read = (get_bits_count(&s->gb)+7)/8;
00524
00525
00526 s->frame.nb_samples = s->blocksize;
00527 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00528 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00529 return ret;
00530 }
00531
00532 s->dsp.decorrelate[s->ch_mode](s->frame.data, s->decoded, s->channels,
00533 s->blocksize, s->sample_shift);
00534
00535 if (bytes_read > buf_size) {
00536 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
00537 return -1;
00538 }
00539 if (bytes_read < buf_size) {
00540 av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
00541 buf_size - bytes_read, buf_size);
00542 }
00543
00544 *got_frame_ptr = 1;
00545 *(AVFrame *)data = s->frame;
00546
00547 return bytes_read;
00548 }
00549
00550 static av_cold int flac_decode_close(AVCodecContext *avctx)
00551 {
00552 FLACContext *s = avctx->priv_data;
00553 int i;
00554
00555 for (i = 0; i < s->channels; i++) {
00556 av_freep(&s->decoded[i]);
00557 }
00558
00559 return 0;
00560 }
00561
00562 AVCodec ff_flac_decoder = {
00563 .name = "flac",
00564 .type = AVMEDIA_TYPE_AUDIO,
00565 .id = AV_CODEC_ID_FLAC,
00566 .priv_data_size = sizeof(FLACContext),
00567 .init = flac_decode_init,
00568 .close = flac_decode_close,
00569 .decode = flac_decode_frame,
00570 .capabilities = CODEC_CAP_DR1,
00571 .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
00572 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
00573 AV_SAMPLE_FMT_S16P,
00574 AV_SAMPLE_FMT_S32,
00575 AV_SAMPLE_FMT_S32P,
00576 -1 },
00577 };