2 * FLAC (Free Lossless Audio Codec) decoder
3 * Copyright (c) 2003 Alex Beregszaszi
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * FLAC (Free Lossless Audio Codec) decoder
25 * @author Alex Beregszaszi
26 * @see http://flac.sourceforge.net/
28 * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed
29 * through, starting from the initial 'fLaC' signature; or by passing the
30 * 34-byte streaminfo structure through avctx->extradata[_size] followed
31 * by data starting with the 0xFFF8 marker.
36 #include "libavutil/avassert.h"
37 #include "libavutil/crc.h"
38 #include "libavutil/opt.h"
42 #include "bytestream.h"
51 typedef struct FLACContext {
53 struct FLACStreaminfo flac_stream_info;
55 AVCodecContext *avctx; ///< parent AVCodecContext
56 GetBitContext gb; ///< GetBitContext initialized to start at the current frame
58 int blocksize; ///< number of samples in the current frame
59 int sample_shift; ///< shift required to make output samples 16-bit or 32-bit
60 int ch_mode; ///< channel decorrelation type in the current frame
61 int got_streaminfo; ///< indicates if the STREAMINFO has been read
63 int32_t *decoded[FLAC_MAX_CHANNELS]; ///< decoded samples
64 uint8_t *decoded_buffer;
65 unsigned int decoded_buffer_size;
66 int buggy_lpc; ///< use workaround for old lavc encoded files
71 static int allocate_buffers(FLACContext *s);
73 static void flac_set_bps(FLACContext *s)
75 enum AVSampleFormat req = s->avctx->request_sample_fmt;
76 int need32 = s->flac_stream_info.bps > 16;
77 int want32 = av_get_bytes_per_sample(req) > 2;
78 int planar = av_sample_fmt_is_planar(req);
80 if (need32 || want32) {
82 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
84 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
85 s->sample_shift = 32 - s->flac_stream_info.bps;
88 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
90 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
91 s->sample_shift = 16 - s->flac_stream_info.bps;
95 static av_cold int flac_decode_init(AVCodecContext *avctx)
97 enum FLACExtradataFormat format;
100 FLACContext *s = avctx->priv_data;
103 /* for now, the raw FLAC header is allowed to be passed to the decoder as
104 frame data instead of extradata. */
105 if (!avctx->extradata)
108 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
109 return AVERROR_INVALIDDATA;
111 /* initialize based on the demuxer-supplied streamdata header */
112 ret = ff_flac_parse_streaminfo(avctx, &s->flac_stream_info, streaminfo);
115 ret = allocate_buffers(s);
119 ff_flacdsp_init(&s->dsp, avctx->sample_fmt,
120 s->flac_stream_info.channels, s->flac_stream_info.bps);
121 s->got_streaminfo = 1;
126 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
128 av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize);
129 av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
130 av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
131 av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
132 av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
135 static int allocate_buffers(FLACContext *s)
140 av_assert0(s->flac_stream_info.max_blocksize);
142 buf_size = av_samples_get_buffer_size(NULL, s->flac_stream_info.channels,
143 s->flac_stream_info.max_blocksize,
144 AV_SAMPLE_FMT_S32P, 0);
148 av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
149 if (!s->decoded_buffer)
150 return AVERROR(ENOMEM);
152 ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
154 s->flac_stream_info.channels,
155 s->flac_stream_info.max_blocksize,
156 AV_SAMPLE_FMT_S32P, 0);
157 return ret < 0 ? ret : 0;
161 * Parse the STREAMINFO from an inline header.
162 * @param s the flac decoding context
163 * @param buf input buffer, starting with the "fLaC" marker
164 * @param buf_size buffer size
165 * @return non-zero if metadata is invalid
167 static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
169 int metadata_type, metadata_size, ret;
171 if (buf_size < FLAC_STREAMINFO_SIZE+8) {
175 flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
176 if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
177 metadata_size != FLAC_STREAMINFO_SIZE) {
178 return AVERROR_INVALIDDATA;
180 ret = ff_flac_parse_streaminfo(s->avctx, &s->flac_stream_info, &buf[8]);
183 ret = allocate_buffers(s);
187 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
188 s->flac_stream_info.channels, s->flac_stream_info.bps);
189 s->got_streaminfo = 1;
195 * Determine the size of an inline header.
196 * @param buf input buffer, starting with the "fLaC" marker
197 * @param buf_size buffer size
198 * @return number of bytes in the header, or 0 if more data is needed
200 static int get_metadata_size(const uint8_t *buf, int buf_size)
202 int metadata_last, metadata_size;
203 const uint8_t *buf_end = buf + buf_size;
207 if (buf_end - buf < 4)
209 flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
211 if (buf_end - buf < metadata_size) {
212 /* need more data in order to read the complete header */
215 buf += metadata_size;
216 } while (!metadata_last);
218 return buf_size - (buf_end - buf);
221 static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
223 int i, tmp, partition, method_type, rice_order;
224 int rice_bits, rice_esc;
227 method_type = get_bits(&s->gb, 2);
228 if (method_type > 1) {
229 av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
231 return AVERROR_INVALIDDATA;
234 rice_order = get_bits(&s->gb, 4);
236 samples= s->blocksize >> rice_order;
237 if (samples << rice_order != s->blocksize) {
238 av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n",
239 rice_order, s->blocksize);
240 return AVERROR_INVALIDDATA;
243 if (pred_order > samples) {
244 av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
245 pred_order, samples);
246 return AVERROR_INVALIDDATA;
249 rice_bits = 4 + method_type;
250 rice_esc = (1 << rice_bits) - 1;
252 decoded += pred_order;
254 for (partition = 0; partition < (1 << rice_order); partition++) {
255 tmp = get_bits(&s->gb, rice_bits);
256 if (tmp == rice_esc) {
257 tmp = get_bits(&s->gb, 5);
258 for (; i < samples; i++)
259 *decoded++ = get_sbits_long(&s->gb, tmp);
261 int real_limit = tmp ? (INT_MAX >> tmp) + 2 : INT_MAX;
262 for (; i < samples; i++) {
263 int v = get_sr_golomb_flac(&s->gb, tmp, real_limit, 0);
264 if (v == 0x80000000){
265 av_log(s->avctx, AV_LOG_ERROR, "invalid residual\n");
266 return AVERROR_INVALIDDATA;
278 static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
279 int pred_order, int bps)
281 const int blocksize = s->blocksize;
282 unsigned av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d);
286 /* warm up samples */
287 for (i = 0; i < pred_order; i++) {
288 decoded[i] = get_sbits_long(&s->gb, bps);
291 if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
295 a = decoded[pred_order-1];
297 b = a - decoded[pred_order-2];
299 c = b - decoded[pred_order-2] + decoded[pred_order-3];
301 d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
303 switch (pred_order) {
307 for (i = pred_order; i < blocksize; i++)
308 decoded[i] = a += decoded[i];
311 for (i = pred_order; i < blocksize; i++)
312 decoded[i] = a += b += decoded[i];
315 for (i = pred_order; i < blocksize; i++)
316 decoded[i] = a += b += c += decoded[i];
319 for (i = pred_order; i < blocksize; i++)
320 decoded[i] = a += b += c += d += decoded[i];
323 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
324 return AVERROR_INVALIDDATA;
330 static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32],
331 int order, int qlevel, int len, int bps)
334 int ebps = 1 << (bps-1);
337 for (i = order; i < len; i++)
338 sigma |= decoded[i] + ebps;
343 for (i = len - 1; i >= order; i--) {
345 for (j = 0; j < order; j++)
346 p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j];
347 decoded[i] -= p >> qlevel;
349 for (i = order; i < len; i++, decoded++) {
351 for (j = 0; j < order; j++)
352 p += coeffs[j] * (uint32_t)decoded[j];
353 decoded[j] += p >> qlevel;
357 static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
361 int coeff_prec, qlevel;
364 /* warm up samples */
365 for (i = 0; i < pred_order; i++) {
366 decoded[i] = get_sbits_long(&s->gb, bps);
369 coeff_prec = get_bits(&s->gb, 4) + 1;
370 if (coeff_prec == 16) {
371 av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
372 return AVERROR_INVALIDDATA;
374 qlevel = get_sbits(&s->gb, 5);
376 av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
378 return AVERROR_INVALIDDATA;
381 for (i = 0; i < pred_order; i++) {
382 coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
385 if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
388 if ( ( s->buggy_lpc && s->flac_stream_info.bps <= 16)
389 || ( !s->buggy_lpc && bps <= 16
390 && bps + coeff_prec + av_log2(pred_order) <= 32)) {
391 s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize);
393 s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize);
394 if (s->flac_stream_info.bps <= 16)
395 lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps);
401 static inline int decode_subframe(FLACContext *s, int channel)
403 int32_t *decoded = s->decoded[channel];
404 int type, wasted = 0;
405 int bps = s->flac_stream_info.bps;
409 if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
412 if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
416 if (get_bits1(&s->gb)) {
417 av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
418 return AVERROR_INVALIDDATA;
420 type = get_bits(&s->gb, 6);
422 if (get_bits1(&s->gb)) {
423 int left = get_bits_left(&s->gb);
425 (left < bps && !show_bits_long(&s->gb, left)) ||
426 !show_bits_long(&s->gb, bps)) {
427 av_log(s->avctx, AV_LOG_ERROR,
428 "Invalid number of wasted bits > available bits (%d) - left=%d\n",
430 return AVERROR_INVALIDDATA;
432 wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb));
436 avpriv_report_missing_feature(s->avctx, "Decorrelated bit depth > 32");
437 return AVERROR_PATCHWELCOME;
440 //FIXME use av_log2 for types
442 tmp = get_sbits_long(&s->gb, bps);
443 for (i = 0; i < s->blocksize; i++)
445 } else if (type == 1) {
446 for (i = 0; i < s->blocksize; i++)
447 decoded[i] = get_sbits_long(&s->gb, bps);
448 } else if ((type >= 8) && (type <= 12)) {
449 if ((ret = decode_subframe_fixed(s, decoded, type & ~0x8, bps)) < 0)
451 } else if (type >= 32) {
452 if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)
455 av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
456 return AVERROR_INVALIDDATA;
461 for (i = 0; i < s->blocksize; i++)
462 decoded[i] = (unsigned)decoded[i] << wasted;
468 static int decode_frame(FLACContext *s)
471 GetBitContext *gb = &s->gb;
474 if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) {
475 av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
479 if ( s->flac_stream_info.channels
480 && fi.channels != s->flac_stream_info.channels
481 && s->got_streaminfo) {
482 s->flac_stream_info.channels = s->avctx->channels = fi.channels;
483 ff_flac_set_channel_layout(s->avctx);
484 ret = allocate_buffers(s);
488 s->flac_stream_info.channels = s->avctx->channels = fi.channels;
489 if (!s->avctx->channel_layout)
490 ff_flac_set_channel_layout(s->avctx);
491 s->ch_mode = fi.ch_mode;
493 if (!s->flac_stream_info.bps && !fi.bps) {
494 av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
495 return AVERROR_INVALIDDATA;
498 fi.bps = s->flac_stream_info.bps;
499 } else if (s->flac_stream_info.bps && fi.bps != s->flac_stream_info.bps) {
500 av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
502 return AVERROR_INVALIDDATA;
505 if (!s->flac_stream_info.bps) {
506 s->flac_stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps;
510 if (!s->flac_stream_info.max_blocksize)
511 s->flac_stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE;
512 if (fi.blocksize > s->flac_stream_info.max_blocksize) {
513 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
514 s->flac_stream_info.max_blocksize);
515 return AVERROR_INVALIDDATA;
517 s->blocksize = fi.blocksize;
519 if (!s->flac_stream_info.samplerate && !fi.samplerate) {
520 av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
521 " or frame header\n");
522 return AVERROR_INVALIDDATA;
524 if (fi.samplerate == 0)
525 fi.samplerate = s->flac_stream_info.samplerate;
526 s->flac_stream_info.samplerate = s->avctx->sample_rate = fi.samplerate;
528 if (!s->got_streaminfo) {
529 ret = allocate_buffers(s);
532 s->got_streaminfo = 1;
533 dump_headers(s->avctx, &s->flac_stream_info);
535 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
536 s->flac_stream_info.channels, s->flac_stream_info.bps);
538 // dump_headers(s->avctx, &s->flac_stream_info);
541 for (i = 0; i < s->flac_stream_info.channels; i++) {
542 if ((ret = decode_subframe(s, i)) < 0)
549 skip_bits(gb, 16); /* data crc */
554 static int flac_decode_frame(AVCodecContext *avctx, void *data,
555 int *got_frame_ptr, AVPacket *avpkt)
557 AVFrame *frame = data;
558 ThreadFrame tframe = { .f = data };
559 const uint8_t *buf = avpkt->data;
560 int buf_size = avpkt->size;
561 FLACContext *s = avctx->priv_data;
567 if (s->flac_stream_info.max_framesize == 0) {
568 s->flac_stream_info.max_framesize =
569 ff_flac_get_max_frame_size(s->flac_stream_info.max_blocksize ? s->flac_stream_info.max_blocksize : FLAC_MAX_BLOCKSIZE,
570 FLAC_MAX_CHANNELS, 32);
573 if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) {
574 av_log(s->avctx, AV_LOG_DEBUG, "skipping flac header packet 1\n");
578 if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
579 av_log(s->avctx, AV_LOG_DEBUG, "skipping vorbis comment\n");
583 /* check that there is at least the smallest decodable amount of data.
584 this amount corresponds to the smallest valid FLAC frame possible.
585 FF F8 69 02 00 00 9A 00 00 34 46 */
586 if (buf_size < FLAC_MIN_FRAME_SIZE)
589 /* check for inline header */
590 if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
591 if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) {
592 av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
595 return get_metadata_size(buf, buf_size);
599 if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
601 if ((ret = decode_frame(s)) < 0) {
602 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
605 bytes_read = get_bits_count(&s->gb)/8;
607 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) &&
608 av_crc(av_crc_get_table(AV_CRC_16_ANSI),
609 0, buf, bytes_read)) {
610 av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts);
611 if (s->avctx->err_recognition & AV_EF_EXPLODE)
612 return AVERROR_INVALIDDATA;
615 /* get output buffer */
616 frame->nb_samples = s->blocksize;
617 if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
620 s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded,
621 s->flac_stream_info.channels,
622 s->blocksize, s->sample_shift);
624 if (bytes_read > buf_size) {
625 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
626 return AVERROR_INVALIDDATA;
628 if (bytes_read < buf_size) {
629 av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
630 buf_size - bytes_read, buf_size);
639 static int init_thread_copy(AVCodecContext *avctx)
641 FLACContext *s = avctx->priv_data;
642 s->decoded_buffer = NULL;
643 s->decoded_buffer_size = 0;
645 if (s->flac_stream_info.max_blocksize)
646 return allocate_buffers(s);
651 static av_cold int flac_decode_close(AVCodecContext *avctx)
653 FLACContext *s = avctx->priv_data;
655 av_freep(&s->decoded_buffer);
660 static const AVOption options[] = {
661 { "use_buggy_lpc", "emulate old buggy lavc behavior", offsetof(FLACContext, buggy_lpc), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
665 static const AVClass flac_decoder_class = {
667 av_default_item_name,
669 LIBAVUTIL_VERSION_INT,
672 AVCodec ff_flac_decoder = {
674 .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
675 .type = AVMEDIA_TYPE_AUDIO,
676 .id = AV_CODEC_ID_FLAC,
677 .priv_data_size = sizeof(FLACContext),
678 .init = flac_decode_init,
679 .close = flac_decode_close,
680 .decode = flac_decode_frame,
681 .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
682 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
683 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
687 AV_SAMPLE_FMT_NONE },
688 .priv_class = &flac_decoder_class,