4f52c312 |
/*
* FLAC (Free Lossless Audio Codec) decoder
* Copyright (c) 2003 Alex Beregszaszi
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
4f52c312 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
4f52c312 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
4f52c312 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
4f52c312 |
*/
/** |
ba87f080 |
* @file |
4f52c312 |
* FLAC (Free Lossless Audio Codec) decoder
* @author Alex Beregszaszi |
ad4cd0c2 |
* @see http://flac.sourceforge.net/ |
9eda2f94 |
*
* This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed
* through, starting from the initial 'fLaC' signature; or by passing the
* 34-byte streaminfo structure through avctx->extradata[_size] followed
* by data starting with the 0xFFF8 marker. |
4f52c312 |
*/ |
115329f1 |
|
ac2570a8 |
#include <limits.h> |
115329f1 |
|
4d38b838 |
#include "libavutil/avassert.h" |
245976da |
#include "libavutil/crc.h" |
e609cfd6 |
#include "libavutil/opt.h" |
4f52c312 |
#include "avcodec.h" |
5b37e2fc |
#include "internal.h" |
9106a698 |
#include "get_bits.h" |
a8ec12bc |
#include "bytestream.h" |
4f52c312 |
#include "golomb.h" |
9d48410f |
#include "flac.h" |
d4df4e50 |
#include "flacdata.h" |
4a852834 |
#include "flacdsp.h" |
9300de04 |
#include "thread.h" |
4b12930f |
#include "unary.h"
|
4f52c312 |
typedef struct FLACContext { |
e609cfd6 |
AVClass *class; |
38f5a266 |
struct FLACStreaminfo flac_stream_info; |
9d48410f |
|
64cb3765 |
AVCodecContext *avctx; ///< parent AVCodecContext
GetBitContext gb; ///< GetBitContext initialized to start at the current frame |
4f52c312 |
|
64cb3765 |
int blocksize; ///< number of samples in the current frame |
7f3a6a05 |
int sample_shift; ///< shift required to make output samples 16-bit or 32-bit |
2a346725 |
int ch_mode; ///< channel decorrelation type in the current frame |
7d030358 |
int got_streaminfo; ///< indicates if the STREAMINFO has been read |
4f52c312 |
|
07d16e2e |
int32_t *decoded[FLAC_MAX_CHANNELS]; ///< decoded samples |
268f8ba1 |
uint8_t *decoded_buffer;
unsigned int decoded_buffer_size; |
e609cfd6 |
int buggy_lpc; ///< use workaround for old lavc encoded files |
4a852834 |
FLACDSPContext dsp; |
4f52c312 |
} FLACContext;
|
268f8ba1 |
static int allocate_buffers(FLACContext *s); |
59c6178a |
|
87466f81 |
static void flac_set_bps(FLACContext *s)
{ |
784514a4 |
enum AVSampleFormat req = s->avctx->request_sample_fmt; |
38f5a266 |
int need32 = s->flac_stream_info.bps > 16; |
784514a4 |
int want32 = av_get_bytes_per_sample(req) > 2;
int planar = av_sample_fmt_is_planar(req);
if (need32 || want32) {
if (planar)
s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
else
s->avctx->sample_fmt = AV_SAMPLE_FMT_S32; |
38f5a266 |
s->sample_shift = 32 - s->flac_stream_info.bps; |
87466f81 |
} else { |
784514a4 |
if (planar)
s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
else
s->avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
38f5a266 |
s->sample_shift = 16 - s->flac_stream_info.bps; |
87466f81 |
}
}
|
1bec121f |
static av_cold int flac_decode_init(AVCodecContext *avctx) |
4f52c312 |
{ |
59c6178a |
enum FLACExtradataFormat format;
uint8_t *streaminfo; |
268f8ba1 |
int ret; |
9eda2f94 |
FLACContext *s = avctx->priv_data;
s->avctx = avctx;
|
59c6178a |
/* for now, the raw FLAC header is allowed to be passed to the decoder as
frame data instead of extradata. */
if (!avctx->extradata)
return 0;
|
acc897e6 |
if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo)) |
45013b36 |
return AVERROR_INVALIDDATA; |
59c6178a |
|
26adc8d0 |
/* initialize based on the demuxer-supplied streamdata header */ |
6567c59c |
ret = ff_flac_parse_streaminfo(avctx, &s->flac_stream_info, streaminfo);
if (ret < 0)
return ret; |
268f8ba1 |
ret = allocate_buffers(s);
if (ret < 0)
return ret; |
87466f81 |
flac_set_bps(s); |
38f5a266 |
ff_flacdsp_init(&s->dsp, avctx->sample_fmt,
s->flac_stream_info.channels, s->flac_stream_info.bps); |
7d030358 |
s->got_streaminfo = 1; |
9eda2f94 |
|
4f52c312 |
return 0;
}
|
9482171b |
static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s) |
4f52c312 |
{ |
95db6659 |
av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize); |
4bc07e78 |
av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps); |
4f52c312 |
}
|
268f8ba1 |
static int allocate_buffers(FLACContext *s) |
1bec121f |
{ |
268f8ba1 |
int buf_size; |
6fac4826 |
int ret; |
4f52c312 |
|
38f5a266 |
av_assert0(s->flac_stream_info.max_blocksize); |
ac2570a8 |
|
38f5a266 |
buf_size = av_samples_get_buffer_size(NULL, s->flac_stream_info.channels,
s->flac_stream_info.max_blocksize, |
268f8ba1 |
AV_SAMPLE_FMT_S32P, 0);
if (buf_size < 0)
return buf_size;
av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
if (!s->decoded_buffer)
return AVERROR(ENOMEM);
|
6fac4826 |
ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL, |
38f5a266 |
s->decoded_buffer,
s->flac_stream_info.channels,
s->flac_stream_info.max_blocksize,
AV_SAMPLE_FMT_S32P, 0); |
6fac4826 |
return ret < 0 ? ret : 0; |
ac2570a8 |
}
|
17c90b9d |
/** |
a8ec12bc |
* Parse the STREAMINFO from an inline header.
* @param s the flac decoding context
* @param buf input buffer, starting with the "fLaC" marker
* @param buf_size buffer size |
55a72738 |
* @return non-zero if metadata is invalid |
17c90b9d |
*/ |
a8ec12bc |
static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size) |
17c90b9d |
{ |
268f8ba1 |
int metadata_type, metadata_size, ret; |
17c90b9d |
|
a8ec12bc |
if (buf_size < FLAC_STREAMINFO_SIZE+8) {
/* need more data */
return 0;
} |
5fdaf312 |
flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size); |
a8ec12bc |
if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
metadata_size != FLAC_STREAMINFO_SIZE) {
return AVERROR_INVALIDDATA;
} |
6567c59c |
ret = ff_flac_parse_streaminfo(s->avctx, &s->flac_stream_info, &buf[8]);
if (ret < 0)
return ret; |
268f8ba1 |
ret = allocate_buffers(s);
if (ret < 0)
return ret; |
4a852834 |
flac_set_bps(s); |
38f5a266 |
ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
s->flac_stream_info.channels, s->flac_stream_info.bps); |
a8ec12bc |
s->got_streaminfo = 1; |
17c90b9d |
|
a8ec12bc |
return 0;
} |
17c90b9d |
|
a8ec12bc |
/**
* Determine the size of an inline header.
* @param buf input buffer, starting with the "fLaC" marker
* @param buf_size buffer size
* @return number of bytes in the header, or 0 if more data is needed
*/
static int get_metadata_size(const uint8_t *buf, int buf_size)
{
int metadata_last, metadata_size;
const uint8_t *buf_end = buf + buf_size; |
e0168e3b |
|
a8ec12bc |
buf += 4;
do { |
4c5e7b27 |
if (buf_end - buf < 4) |
3f5a6853 |
return AVERROR_INVALIDDATA; |
5fdaf312 |
flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size); |
5b63d33d |
buf += 4; |
4c5e7b27 |
if (buf_end - buf < metadata_size) { |
a8ec12bc |
/* need more data in order to read the complete header */ |
3f5a6853 |
return AVERROR_INVALIDDATA; |
6a85fb34 |
} |
a8ec12bc |
buf += metadata_size; |
6a85fb34 |
} while (!metadata_last); |
17c90b9d |
|
a8ec12bc |
return buf_size - (buf_end - buf); |
4f52c312 |
}
|
44c56a16 |
static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order) |
4f52c312 |
{ |
ec703910 |
GetBitContext gb = s->gb; |
4f52c312 |
int i, tmp, partition, method_type, rice_order; |
ea98507d |
int rice_bits, rice_esc; |
44c56a16 |
int samples; |
4f52c312 |
|
ec703910 |
method_type = get_bits(&gb, 2);
rice_order = get_bits(&gb, 4); |
15f1cc09 |
samples = s->blocksize >> rice_order;
rice_bits = 4 + method_type;
rice_esc = (1 << rice_bits) - 1;
decoded += pred_order;
i = pred_order;
|
1bec121f |
if (method_type > 1) { |
9f3d3ecf |
av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
method_type); |
45013b36 |
return AVERROR_INVALIDDATA; |
ac2570a8 |
} |
115329f1 |
|
8ca9a68f |
if (samples << rice_order != s->blocksize) {
av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n",
rice_order, s->blocksize);
return AVERROR_INVALIDDATA;
}
|
5484dad7 |
if (pred_order > samples) { |
9f3d3ecf |
av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
pred_order, samples); |
45013b36 |
return AVERROR_INVALIDDATA; |
5484dad7 |
} |
4f52c312 |
|
1bec121f |
for (partition = 0; partition < (1 << rice_order); partition++) { |
ec703910 |
tmp = get_bits(&gb, rice_bits); |
ea98507d |
if (tmp == rice_esc) { |
ec703910 |
tmp = get_bits(&gb, 5); |
44c56a16 |
for (; i < samples; i++) |
ec703910 |
*decoded++ = get_sbits_long(&gb, tmp); |
1bec121f |
} else { |
0a65dae9 |
int real_limit = tmp ? (INT_MAX >> tmp) + 2 : INT_MAX; |
44c56a16 |
for (; i < samples; i++) { |
ec703910 |
int v = get_sr_golomb_flac(&gb, tmp, real_limit, 0); |
fd002035 |
if (v == 0x80000000){
av_log(s->avctx, AV_LOG_ERROR, "invalid residual\n");
return AVERROR_INVALIDDATA;
}
*decoded++ = v; |
ac2570a8 |
} |
4f52c312 |
} |
ac2570a8 |
i= 0; |
4f52c312 |
}
|
ec703910 |
s->gb = gb; |
0f5ad12b |
|
4f52c312 |
return 0; |
115329f1 |
} |
4f52c312 |
|
44c56a16 |
static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
int pred_order, int bps) |
4f52c312 |
{ |
08965b22 |
const int blocksize = s->blocksize; |
83a75bf6 |
unsigned av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d);
int i; |
45013b36 |
int ret; |
115329f1 |
|
4f52c312 |
/* warm up samples */ |
1bec121f |
for (i = 0; i < pred_order; i++) { |
0da301e1 |
decoded[i] = get_sbits_long(&s->gb, bps); |
4f52c312 |
} |
115329f1 |
|
45013b36 |
if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
return ret; |
4f52c312 |
|
1bec121f |
if (pred_order > 0) |
1f4fa6a4 |
a = decoded[pred_order-1]; |
1bec121f |
if (pred_order > 1) |
1f4fa6a4 |
b = a - decoded[pred_order-2]; |
1bec121f |
if (pred_order > 2) |
1f4fa6a4 |
c = b - decoded[pred_order-2] + decoded[pred_order-3]; |
1bec121f |
if (pred_order > 3) |
1f4fa6a4 |
d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4]; |
08965b22 |
|
1bec121f |
switch (pred_order) {
case 0:
break;
case 1:
for (i = pred_order; i < blocksize; i++)
decoded[i] = a += decoded[i];
break;
case 2:
for (i = pred_order; i < blocksize; i++)
decoded[i] = a += b += decoded[i];
break;
case 3:
for (i = pred_order; i < blocksize; i++)
decoded[i] = a += b += c += decoded[i];
break;
case 4:
for (i = pred_order; i < blocksize; i++)
decoded[i] = a += b += c += d += decoded[i];
break;
default:
av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); |
45013b36 |
return AVERROR_INVALIDDATA; |
4f52c312 |
} |
ac2570a8 |
|
4f52c312 |
return 0;
}
|
3e1028c6 |
static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32], |
b6fcb2bb |
int order, int qlevel, int len, int bps)
{
int i, j;
int ebps = 1 << (bps-1);
unsigned sigma = 0;
for (i = order; i < len; i++)
sigma |= decoded[i] + ebps;
if (sigma < 2*ebps)
return;
for (i = len - 1; i >= order; i--) {
int64_t p = 0;
for (j = 0; j < order; j++) |
3e1028c6 |
p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j]; |
b6fcb2bb |
decoded[i] -= p >> qlevel;
}
for (i = order; i < len; i++, decoded++) {
int32_t p = 0;
for (j = 0; j < order; j++)
p += coeffs[j] * (uint32_t)decoded[j];
decoded[j] += p >> qlevel;
}
}
|
44c56a16 |
static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order, |
0da301e1 |
int bps) |
4f52c312 |
{ |
45013b36 |
int i, ret; |
4f52c312 |
int coeff_prec, qlevel; |
8313e179 |
int coeffs[32]; |
115329f1 |
|
4f52c312 |
/* warm up samples */ |
1bec121f |
for (i = 0; i < pred_order; i++) { |
0da301e1 |
decoded[i] = get_sbits_long(&s->gb, bps); |
4f52c312 |
} |
115329f1 |
|
4f52c312 |
coeff_prec = get_bits(&s->gb, 4) + 1; |
1bec121f |
if (coeff_prec == 16) { |
5305f40b |
av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n"); |
45013b36 |
return AVERROR_INVALIDDATA; |
4f52c312 |
} |
ac2570a8 |
qlevel = get_sbits(&s->gb, 5); |
1bec121f |
if (qlevel < 0) { |
9f3d3ecf |
av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
qlevel); |
45013b36 |
return AVERROR_INVALIDDATA; |
9d656110 |
}
|
1bec121f |
for (i = 0; i < pred_order; i++) { |
bf1cf4d5 |
coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec); |
4f52c312 |
} |
115329f1 |
|
45013b36 |
if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
return ret; |
4f52c312 |
|
e609cfd6 |
if ( ( s->buggy_lpc && s->flac_stream_info.bps <= 16)
|| ( !s->buggy_lpc && bps <= 16
&& bps + coeff_prec + av_log2(pred_order) <= 32)) {
s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize);
} else {
s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize); |
b6fcb2bb |
if (s->flac_stream_info.bps <= 16)
lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps); |
e609cfd6 |
} |
115329f1 |
|
4f52c312 |
return 0;
}
static inline int decode_subframe(FLACContext *s, int channel)
{ |
44c56a16 |
int32_t *decoded = s->decoded[channel]; |
4f52c312 |
int type, wasted = 0; |
38f5a266 |
int bps = s->flac_stream_info.bps; |
45013b36 |
int i, tmp, ret; |
115329f1 |
|
1bec121f |
if (channel == 0) { |
2a346725 |
if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE) |
0da301e1 |
bps++; |
1bec121f |
} else { |
2a346725 |
if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE) |
0da301e1 |
bps++; |
ac2570a8 |
}
|
1bec121f |
if (get_bits1(&s->gb)) { |
1cef211d |
av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n"); |
45013b36 |
return AVERROR_INVALIDDATA; |
4f52c312 |
}
type = get_bits(&s->gb, 6); |
c5706efd |
|
1bec121f |
if (get_bits1(&s->gb)) { |
52e4018b |
int left = get_bits_left(&s->gb); |
5f305228 |
if ( left <= 0 || |
0da301e1 |
(left < bps && !show_bits_long(&s->gb, left)) ||
!show_bits_long(&s->gb, bps)) { |
52e4018b |
av_log(s->avctx, AV_LOG_ERROR,
"Invalid number of wasted bits > available bits (%d) - left=%d\n", |
0da301e1 |
bps, left); |
52e4018b |
return AVERROR_INVALIDDATA;
} |
4b12930f |
wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb)); |
0da301e1 |
bps -= wasted; |
4f52c312 |
} |
0da301e1 |
if (bps > 32) { |
63d744e2 |
avpriv_report_missing_feature(s->avctx, "Decorrelated bit depth > 32"); |
717addec |
return AVERROR_PATCHWELCOME; |
a3d2379b |
} |
c5706efd |
|
ac2570a8 |
//FIXME use av_log2 for types |
1bec121f |
if (type == 0) { |
0da301e1 |
tmp = get_sbits_long(&s->gb, bps); |
4f52c312 |
for (i = 0; i < s->blocksize; i++) |
44c56a16 |
decoded[i] = tmp; |
1bec121f |
} else if (type == 1) { |
4f52c312 |
for (i = 0; i < s->blocksize; i++) |
44c56a16 |
decoded[i] = get_sbits_long(&s->gb, bps); |
1bec121f |
} else if ((type >= 8) && (type <= 12)) { |
45013b36 |
if ((ret = decode_subframe_fixed(s, decoded, type & ~0x8, bps)) < 0)
return ret; |
1bec121f |
} else if (type >= 32) { |
45013b36 |
if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)
return ret; |
1bec121f |
} else { |
1cef211d |
av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n"); |
45013b36 |
return AVERROR_INVALIDDATA; |
4f52c312 |
} |
115329f1 |
|
1bec121f |
if (wasted) { |
4f52c312 |
int i;
for (i = 0; i < s->blocksize; i++) |
1f5630af |
decoded[i] = (unsigned)decoded[i] << wasted; |
4f52c312 |
}
return 0;
}
|
cd98a030 |
static int decode_frame(FLACContext *s)
{ |
90fcac0e |
int i, ret; |
cd98a030 |
GetBitContext *gb = &s->gb;
FLACFrameInfo fi;
|
0e78ef0f |
if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) { |
cd98a030 |
av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n"); |
0e78ef0f |
return ret; |
9d656110 |
} |
115329f1 |
|
38f5a266 |
if ( s->flac_stream_info.channels
&& fi.channels != s->flac_stream_info.channels
&& s->got_streaminfo) {
s->flac_stream_info.channels = s->avctx->channels = fi.channels; |
90fcac0e |
ff_flac_set_channel_layout(s->avctx);
ret = allocate_buffers(s);
if (ret < 0)
return ret; |
fbc4d9c9 |
} |
38f5a266 |
s->flac_stream_info.channels = s->avctx->channels = fi.channels; |
08797c59 |
if (!s->avctx->channel_layout) |
aef51507 |
ff_flac_set_channel_layout(s->avctx); |
cd98a030 |
s->ch_mode = fi.ch_mode; |
fbc4d9c9 |
|
38f5a266 |
if (!s->flac_stream_info.bps && !fi.bps) { |
2e0559b7 |
av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n"); |
45013b36 |
return AVERROR_INVALIDDATA; |
2e0559b7 |
}
if (!fi.bps) { |
38f5a266 |
fi.bps = s->flac_stream_info.bps;
} else if (s->flac_stream_info.bps && fi.bps != s->flac_stream_info.bps) { |
fbc4d9c9 |
av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
"supported\n"); |
0e78ef0f |
return AVERROR_INVALIDDATA; |
fbc4d9c9 |
} |
2e0559b7 |
|
38f5a266 |
if (!s->flac_stream_info.bps) {
s->flac_stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps; |
784514a4 |
flac_set_bps(s);
} |
fbc4d9c9 |
|
38f5a266 |
if (!s->flac_stream_info.max_blocksize)
s->flac_stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE;
if (fi.blocksize > s->flac_stream_info.max_blocksize) { |
cd98a030 |
av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize, |
38f5a266 |
s->flac_stream_info.max_blocksize); |
45013b36 |
return AVERROR_INVALIDDATA; |
fbc4d9c9 |
} |
cd98a030 |
s->blocksize = fi.blocksize; |
fbc4d9c9 |
|
38f5a266 |
if (!s->flac_stream_info.samplerate && !fi.samplerate) { |
2e0559b7 |
av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
" or frame header\n"); |
45013b36 |
return AVERROR_INVALIDDATA; |
2e0559b7 |
} |
99d86863 |
if (fi.samplerate == 0) |
38f5a266 |
fi.samplerate = s->flac_stream_info.samplerate;
s->flac_stream_info.samplerate = s->avctx->sample_rate = fi.samplerate; |
4f52c312 |
|
2e0559b7 |
if (!s->got_streaminfo) { |
90fcac0e |
ret = allocate_buffers(s); |
268f8ba1 |
if (ret < 0)
return ret; |
2e0559b7 |
s->got_streaminfo = 1; |
38f5a266 |
dump_headers(s->avctx, &s->flac_stream_info); |
2e0559b7 |
} |
38f5a266 |
ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
s->flac_stream_info.channels, s->flac_stream_info.bps); |
2e0559b7 |
|
38f5a266 |
// dump_headers(s->avctx, &s->flac_stream_info); |
4f52c312 |
/* subframes */ |
38f5a266 |
for (i = 0; i < s->flac_stream_info.channels; i++) { |
45013b36 |
if ((ret = decode_subframe(s, i)) < 0)
return ret; |
4f52c312 |
} |
115329f1 |
|
7e00bd84 |
align_get_bits(gb); |
4f52c312 |
/* frame footer */ |
7e00bd84 |
skip_bits(gb, 16); /* data crc */ |
4f52c312 |
return 0;
}
|
0eea2129 |
static int flac_decode_frame(AVCodecContext *avctx, void *data,
int *got_frame_ptr, AVPacket *avpkt) |
4f52c312 |
{ |
b8e9c99e |
AVFrame *frame = data; |
9300de04 |
ThreadFrame tframe = { .f = data }; |
7a00bbad |
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; |
4f52c312 |
FLACContext *s = avctx->priv_data; |
4a852834 |
int bytes_read = 0; |
0eea2129 |
int ret; |
ac66834c |
|
0eea2129 |
*got_frame_ptr = 0; |
4f52c312 |
|
38f5a266 |
if (s->flac_stream_info.max_framesize == 0) {
s->flac_stream_info.max_framesize =
ff_flac_get_max_frame_size(s->flac_stream_info.max_blocksize ? s->flac_stream_info.max_blocksize : FLAC_MAX_BLOCKSIZE, |
2e0559b7 |
FLAC_MAX_CHANNELS, 32); |
ac2570a8 |
} |
4f52c312 |
|
0e9b9a67 |
if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) { |
e9ad121b |
av_log(s->avctx, AV_LOG_DEBUG, "skipping flac header packet 1\n"); |
0e9b9a67 |
return buf_size;
}
if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) { |
e9ad121b |
av_log(s->avctx, AV_LOG_DEBUG, "skipping vorbis comment\n"); |
0e9b9a67 |
return buf_size;
}
|
5ef4fa87 |
/* check that there is at least the smallest decodable amount of data. |
8d1e885f |
this amount corresponds to the smallest valid FLAC frame possible.
FF F8 69 02 00 00 9A 00 00 34 46 */ |
a4151444 |
if (buf_size < FLAC_MIN_FRAME_SIZE) |
60a68493 |
return buf_size; |
5ef4fa87 |
|
55a72738 |
/* check for inline header */ |
a8ec12bc |
if (AV_RB32(buf) == MKBETAG('f','L','a','C')) { |
0e78ef0f |
if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) { |
55a72738 |
av_log(s->avctx, AV_LOG_ERROR, "invalid header\n"); |
0e78ef0f |
return ret; |
55a72738 |
} |
60a68493 |
return get_metadata_size(buf, buf_size); |
13de8a08 |
} |
fd6fd470 |
/* decode frame */ |
b4d4ef55 |
if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
return ret; |
45013b36 |
if ((ret = decode_frame(s)) < 0) { |
13de8a08 |
av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n"); |
45013b36 |
return ret; |
13de8a08 |
} |
1d5b35cc |
bytes_read = get_bits_count(&s->gb)/8;
|
3b56f665 |
if ((s->avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) && |
1d5b35cc |
av_crc(av_crc_get_table(AV_CRC_16_ANSI),
0, buf, bytes_read)) {
av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts);
if (s->avctx->err_recognition & AV_EF_EXPLODE)
return AVERROR_INVALIDDATA;
} |
ac2570a8 |
|
0eea2129 |
/* get output buffer */ |
b8e9c99e |
frame->nb_samples = s->blocksize; |
9300de04 |
if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0) |
0eea2129 |
return ret; |
4a852834 |
|
38f5a266 |
s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded,
s->flac_stream_info.channels, |
4a852834 |
s->blocksize, s->sample_shift); |
4f52c312 |
|
c5199729 |
if (bytes_read > buf_size) {
av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size); |
45013b36 |
return AVERROR_INVALIDDATA; |
ac2570a8 |
} |
60a68493 |
if (bytes_read < buf_size) {
av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
buf_size - bytes_read, buf_size);
} |
4f52c312 |
|
b8e9c99e |
*got_frame_ptr = 1; |
0eea2129 |
|
60a68493 |
return bytes_read; |
4f52c312 |
}
|
2cbaa078 |
#if HAVE_THREADS |
9300de04 |
static int init_thread_copy(AVCodecContext *avctx)
{
FLACContext *s = avctx->priv_data;
s->decoded_buffer = NULL;
s->decoded_buffer_size = 0; |
ff0bdf93 |
s->avctx = avctx; |
38f5a266 |
if (s->flac_stream_info.max_blocksize) |
a28f4fd1 |
return allocate_buffers(s);
return 0; |
9300de04 |
} |
2cbaa078 |
#endif |
9300de04 |
|
98a6fff9 |
static av_cold int flac_decode_close(AVCodecContext *avctx) |
4f52c312 |
{
FLACContext *s = avctx->priv_data; |
115329f1 |
|
268f8ba1 |
av_freep(&s->decoded_buffer); |
115329f1 |
|
4f52c312 |
return 0;
}
|
e609cfd6 |
static const AVOption options[] = { |
fb99ef0b |
{ "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 }, |
e609cfd6 |
{ NULL },
};
static const AVClass flac_decoder_class = {
"FLAC decoder",
av_default_item_name,
options,
LIBAVUTIL_VERSION_INT,
};
|
e7e2df27 |
AVCodec ff_flac_decoder = { |
ec6402b7 |
.name = "flac", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"), |
ec6402b7 |
.type = AVMEDIA_TYPE_AUDIO, |
36ef5369 |
.id = AV_CODEC_ID_FLAC, |
ec6402b7 |
.priv_data_size = sizeof(FLACContext),
.init = flac_decode_init,
.close = flac_decode_close,
.decode = flac_decode_frame, |
61d900fd |
.init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy), |
444e9874 |
.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, |
784514a4 |
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
AV_SAMPLE_FMT_S16P,
AV_SAMPLE_FMT_S32,
AV_SAMPLE_FMT_S32P, |
5864fe62 |
AV_SAMPLE_FMT_NONE }, |
e609cfd6 |
.priv_class = &flac_decoder_class, |
4f52c312 |
}; |