19
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
#include "libavutil/channel_layout.h"
23
#include "libavutil/crc.h"
24
#include "libavutil/log.h"
25
#include "bytestream.h"
30
static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
32
static const uint64_t flac_channel_layouts[8] = {
35
AV_CH_LAYOUT_SURROUND,
43
static int64_t get_utf8(GetBitContext *gb)
46
GET_UTF8(val, get_bits(gb, 8), return -1;)
50
int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
51
FLACFrameInfo *fi, int log_level_offset)
53
int bs_code, sr_code, bps_code;
56
if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
57
av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
58
return AVERROR_INVALIDDATA;
61
/* variable block size stream code */
62
fi->is_var_size = get_bits1(gb);
64
/* block size and sample rate codes */
65
bs_code = get_bits(gb, 4);
66
sr_code = get_bits(gb, 4);
68
/* channels and decorrelation */
69
fi->ch_mode = get_bits(gb, 4);
70
if (fi->ch_mode < FLAC_MAX_CHANNELS) {
71
fi->channels = fi->ch_mode + 1;
72
fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
73
} else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
75
fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
77
av_log(avctx, AV_LOG_ERROR + log_level_offset,
78
"invalid channel mode: %d\n", fi->ch_mode);
79
return AVERROR_INVALIDDATA;
83
bps_code = get_bits(gb, 3);
84
if (bps_code == 3 || bps_code == 7) {
85
av_log(avctx, AV_LOG_ERROR + log_level_offset,
86
"invalid sample size code (%d)\n",
88
return AVERROR_INVALIDDATA;
90
fi->bps = sample_size_table[bps_code];
94
av_log(avctx, AV_LOG_ERROR + log_level_offset,
95
"broken stream, invalid padding\n");
96
return AVERROR_INVALIDDATA;
99
/* sample or frame count */
100
fi->frame_or_sample_num = get_utf8(gb);
101
if (fi->frame_or_sample_num < 0) {
102
av_log(avctx, AV_LOG_ERROR + log_level_offset,
103
"sample/frame number invalid; utf8 fscked\n");
104
return AVERROR_INVALIDDATA;
109
av_log(avctx, AV_LOG_ERROR + log_level_offset,
110
"reserved blocksize code: 0\n");
111
return AVERROR_INVALIDDATA;
112
} else if (bs_code == 6) {
113
fi->blocksize = get_bits(gb, 8) + 1;
114
} else if (bs_code == 7) {
115
fi->blocksize = get_bits(gb, 16) + 1;
117
fi->blocksize = ff_flac_blocksize_table[bs_code];
122
fi->samplerate = ff_flac_sample_rate_table[sr_code];
123
} else if (sr_code == 12) {
124
fi->samplerate = get_bits(gb, 8) * 1000;
125
} else if (sr_code == 13) {
126
fi->samplerate = get_bits(gb, 16);
127
} else if (sr_code == 14) {
128
fi->samplerate = get_bits(gb, 16) * 10;
130
av_log(avctx, AV_LOG_ERROR + log_level_offset,
131
"illegal sample rate code %d\n",
133
return AVERROR_INVALIDDATA;
136
/* header CRC-8 check */
138
if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
139
get_bits_count(gb)/8)) {
140
av_log(avctx, AV_LOG_ERROR + log_level_offset,
141
"header crc mismatch\n");
142
return AVERROR_INVALIDDATA;
24
148
int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
169
int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
170
enum FLACExtradataFormat *format,
171
uint8_t **streaminfo_start)
173
if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
174
av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
177
if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
178
/* extradata contains STREAMINFO only */
179
if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
180
av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
181
FLAC_STREAMINFO_SIZE-avctx->extradata_size);
183
*format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
184
*streaminfo_start = avctx->extradata;
186
if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
187
av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
190
*format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
191
*streaminfo_start = &avctx->extradata[8];
196
void ff_flac_set_channel_layout(AVCodecContext *avctx)
198
if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
199
avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
201
avctx->channel_layout = 0;
204
void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
205
const uint8_t *buffer)
208
init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
210
skip_bits(&gb, 16); /* skip min blocksize */
211
s->max_blocksize = get_bits(&gb, 16);
212
if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
213
av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
215
s->max_blocksize = 16;
218
skip_bits(&gb, 24); /* skip min frame size */
219
s->max_framesize = get_bits_long(&gb, 24);
221
s->samplerate = get_bits_long(&gb, 20);
222
s->channels = get_bits(&gb, 3) + 1;
223
s->bps = get_bits(&gb, 5) + 1;
225
avctx->channels = s->channels;
226
avctx->sample_rate = s->samplerate;
227
avctx->bits_per_raw_sample = s->bps;
229
if (!avctx->channel_layout ||
230
av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels)
231
ff_flac_set_channel_layout(avctx);
233
s->samples = get_bits64(&gb, 36);
235
skip_bits_long(&gb, 64); /* md5 sum */
236
skip_bits_long(&gb, 64); /* md5 sum */