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