2009-03-21 03:54:31 +02:00
|
|
|
/*
|
|
|
|
* FLAC common code
|
|
|
|
* Copyright (c) 2009 Justin Ruggles
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2009-03-21 03:54:31 +02:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2009-03-21 03:54:31 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2009-03-21 03:54:31 +02:00
|
|
|
* 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
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2009-03-21 03:54:31 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2012-11-10 18:00:00 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2010-12-07 15:30:18 +02:00
|
|
|
#include "libavutil/crc.h"
|
2012-07-10 19:32:41 +03:00
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "bytestream.h"
|
|
|
|
#include "get_bits.h"
|
2009-03-21 03:54:31 +02:00
|
|
|
#include "flac.h"
|
2010-12-07 15:30:18 +02:00
|
|
|
#include "flacdata.h"
|
|
|
|
|
|
|
|
static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
|
|
|
|
|
2013-01-22 23:53:54 +03:00
|
|
|
static const uint64_t flac_channel_layouts[8] = {
|
2012-10-22 00:02:28 +03:00
|
|
|
AV_CH_LAYOUT_MONO,
|
|
|
|
AV_CH_LAYOUT_STEREO,
|
|
|
|
AV_CH_LAYOUT_SURROUND,
|
|
|
|
AV_CH_LAYOUT_QUAD,
|
|
|
|
AV_CH_LAYOUT_5POINT0,
|
2013-01-22 23:53:54 +03:00
|
|
|
AV_CH_LAYOUT_5POINT1,
|
|
|
|
AV_CH_LAYOUT_6POINT1,
|
|
|
|
AV_CH_LAYOUT_7POINT1
|
2012-10-22 00:02:28 +03:00
|
|
|
};
|
|
|
|
|
2010-12-07 15:30:18 +02:00
|
|
|
static int64_t get_utf8(GetBitContext *gb)
|
|
|
|
{
|
|
|
|
int64_t val;
|
|
|
|
GET_UTF8(val, get_bits(gb, 8), return -1;)
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
|
2010-12-07 16:00:38 +02:00
|
|
|
FLACFrameInfo *fi, int log_level_offset)
|
2010-12-07 15:30:18 +02:00
|
|
|
{
|
|
|
|
int bs_code, sr_code, bps_code;
|
|
|
|
|
|
|
|
/* frame sync code */
|
2010-12-07 15:42:52 +02:00
|
|
|
if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
|
2010-12-07 16:00:38 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
|
2013-07-10 17:34:14 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-12-07 15:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* variable block size stream code */
|
2010-12-07 15:50:13 +02:00
|
|
|
fi->is_var_size = get_bits1(gb);
|
2010-12-07 15:30:18 +02:00
|
|
|
|
|
|
|
/* block size and sample rate codes */
|
|
|
|
bs_code = get_bits(gb, 4);
|
|
|
|
sr_code = get_bits(gb, 4);
|
|
|
|
|
|
|
|
/* channels and decorrelation */
|
|
|
|
fi->ch_mode = get_bits(gb, 4);
|
|
|
|
if (fi->ch_mode < FLAC_MAX_CHANNELS) {
|
|
|
|
fi->channels = fi->ch_mode + 1;
|
|
|
|
fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
|
2012-06-17 13:45:10 +03:00
|
|
|
} else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
|
2010-12-07 15:30:18 +02:00
|
|
|
fi->channels = 2;
|
2012-06-17 13:45:10 +03:00
|
|
|
fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
|
2010-12-07 15:30:18 +02:00
|
|
|
} else {
|
2010-12-07 16:02:42 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"invalid channel mode: %d\n", fi->ch_mode);
|
2013-07-10 17:34:14 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-12-07 15:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* bits per sample */
|
|
|
|
bps_code = get_bits(gb, 3);
|
|
|
|
if (bps_code == 3 || bps_code == 7) {
|
2010-12-07 16:02:42 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"invalid sample size code (%d)\n",
|
2010-12-07 15:30:18 +02:00
|
|
|
bps_code);
|
2013-07-10 17:34:14 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-12-07 15:30:18 +02:00
|
|
|
}
|
|
|
|
fi->bps = sample_size_table[bps_code];
|
|
|
|
|
|
|
|
/* reserved bit */
|
|
|
|
if (get_bits1(gb)) {
|
2010-12-07 16:02:42 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"broken stream, invalid padding\n");
|
2013-07-10 17:34:14 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-12-07 15:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sample or frame count */
|
2010-12-07 15:50:13 +02:00
|
|
|
fi->frame_or_sample_num = get_utf8(gb);
|
|
|
|
if (fi->frame_or_sample_num < 0) {
|
2010-12-07 16:02:42 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"sample/frame number invalid; utf8 fscked\n");
|
2013-07-10 17:34:14 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-12-07 15:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* blocksize */
|
|
|
|
if (bs_code == 0) {
|
2010-12-07 16:02:42 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"reserved blocksize code: 0\n");
|
2013-07-10 17:34:14 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-12-07 15:30:18 +02:00
|
|
|
} else if (bs_code == 6) {
|
|
|
|
fi->blocksize = get_bits(gb, 8) + 1;
|
|
|
|
} else if (bs_code == 7) {
|
|
|
|
fi->blocksize = get_bits(gb, 16) + 1;
|
|
|
|
} else {
|
|
|
|
fi->blocksize = ff_flac_blocksize_table[bs_code];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sample rate */
|
|
|
|
if (sr_code < 12) {
|
|
|
|
fi->samplerate = ff_flac_sample_rate_table[sr_code];
|
|
|
|
} else if (sr_code == 12) {
|
|
|
|
fi->samplerate = get_bits(gb, 8) * 1000;
|
|
|
|
} else if (sr_code == 13) {
|
|
|
|
fi->samplerate = get_bits(gb, 16);
|
|
|
|
} else if (sr_code == 14) {
|
|
|
|
fi->samplerate = get_bits(gb, 16) * 10;
|
|
|
|
} else {
|
2010-12-07 16:02:42 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"illegal sample rate code %d\n",
|
2010-12-07 15:30:18 +02:00
|
|
|
sr_code);
|
2013-07-10 17:34:14 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-12-07 15:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* header CRC-8 check */
|
|
|
|
skip_bits(gb, 8);
|
|
|
|
if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
|
|
|
|
get_bits_count(gb)/8)) {
|
2010-12-07 16:02:42 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"header crc mismatch\n");
|
2013-07-10 17:34:14 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-12-07 15:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-21 03:54:31 +02:00
|
|
|
|
|
|
|
int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
|
|
|
|
{
|
|
|
|
/* Technically, there is no limit to FLAC frame size, but an encoder
|
|
|
|
should not write a frame that is larger than if verbatim encoding mode
|
|
|
|
were to be used. */
|
|
|
|
|
|
|
|
int count;
|
|
|
|
|
|
|
|
count = 16; /* frame header */
|
|
|
|
count += ch * ((7+bps+7)/8); /* subframe headers */
|
|
|
|
if (ch == 2) {
|
|
|
|
/* for stereo, need to account for using decorrelation */
|
|
|
|
count += (( 2*bps+1) * blocksize + 7) / 8;
|
|
|
|
} else {
|
|
|
|
count += ( ch*bps * blocksize + 7) / 8;
|
|
|
|
}
|
|
|
|
count += 2; /* frame footer */
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2012-07-10 19:32:41 +03:00
|
|
|
|
|
|
|
int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
|
|
|
|
enum FLACExtradataFormat *format,
|
|
|
|
uint8_t **streaminfo_start)
|
|
|
|
{
|
|
|
|
if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
|
|
|
|
/* extradata contains STREAMINFO only */
|
|
|
|
if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
|
|
|
|
FLAC_STREAMINFO_SIZE-avctx->extradata_size);
|
|
|
|
}
|
|
|
|
*format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
|
|
|
|
*streaminfo_start = avctx->extradata;
|
|
|
|
} else {
|
|
|
|
if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
|
|
|
|
*streaminfo_start = &avctx->extradata[8];
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-22 00:02:28 +03:00
|
|
|
void ff_flac_set_channel_layout(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
|
|
|
|
avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
|
|
|
|
else
|
|
|
|
avctx->channel_layout = 0;
|
|
|
|
}
|
|
|
|
|
2012-07-10 19:32:41 +03:00
|
|
|
void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
|
|
|
|
const uint8_t *buffer)
|
|
|
|
{
|
|
|
|
GetBitContext gb;
|
|
|
|
init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
|
|
|
|
|
|
|
|
skip_bits(&gb, 16); /* skip min blocksize */
|
|
|
|
s->max_blocksize = get_bits(&gb, 16);
|
|
|
|
if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
|
|
|
|
s->max_blocksize);
|
|
|
|
s->max_blocksize = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
skip_bits(&gb, 24); /* skip min frame size */
|
|
|
|
s->max_framesize = get_bits_long(&gb, 24);
|
|
|
|
|
|
|
|
s->samplerate = get_bits_long(&gb, 20);
|
|
|
|
s->channels = get_bits(&gb, 3) + 1;
|
|
|
|
s->bps = get_bits(&gb, 5) + 1;
|
|
|
|
|
|
|
|
avctx->channels = s->channels;
|
|
|
|
avctx->sample_rate = s->samplerate;
|
|
|
|
avctx->bits_per_raw_sample = s->bps;
|
2014-05-25 12:12:33 +03:00
|
|
|
|
|
|
|
if (!avctx->channel_layout ||
|
|
|
|
av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels)
|
|
|
|
ff_flac_set_channel_layout(avctx);
|
2012-07-10 19:32:41 +03:00
|
|
|
|
|
|
|
s->samples = get_bits_long(&gb, 32) << 4;
|
|
|
|
s->samples |= get_bits(&gb, 4);
|
|
|
|
|
|
|
|
skip_bits_long(&gb, 64); /* md5 sum */
|
|
|
|
skip_bits_long(&gb, 64); /* md5 sum */
|
|
|
|
}
|
|
|
|
|
2014-05-26 10:43:50 +03:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 56
|
2012-07-10 19:32:41 +03:00
|
|
|
void avpriv_flac_parse_block_header(const uint8_t *block_header,
|
|
|
|
int *last, int *type, int *size)
|
|
|
|
{
|
2014-05-26 10:43:50 +03:00
|
|
|
flac_parse_block_header(block_header, last, type, size);
|
2012-07-10 19:32:41 +03:00
|
|
|
}
|
2014-05-26 10:43:50 +03:00
|
|
|
#endif
|