2009-03-21 01:54:31 +00:00
|
|
|
/*
|
|
|
|
* FLAC common code
|
|
|
|
* Copyright (c) 2009 Justin Ruggles
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2010-12-07 13:30:18 +00:00
|
|
|
#include "libavutil/crc.h"
|
2009-03-21 01:54:31 +00:00
|
|
|
#include "flac.h"
|
2010-12-07 13:30:18 +00:00
|
|
|
#include "flacdata.h"
|
2011-04-23 11:37:28 +00:00
|
|
|
#include "vorbis.h"
|
2010-12-07 13:30:18 +00:00
|
|
|
|
|
|
|
static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
|
|
|
|
|
|
|
|
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 14:00:38 +00:00
|
|
|
FLACFrameInfo *fi, int log_level_offset)
|
2010-12-07 13:30:18 +00:00
|
|
|
{
|
|
|
|
int bs_code, sr_code, bps_code;
|
|
|
|
|
|
|
|
/* frame sync code */
|
2010-12-07 13:42:52 +00:00
|
|
|
if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
|
2010-12-07 14:00:38 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
|
2010-12-07 13:42:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* variable block size stream code */
|
2010-12-07 13:50:13 +00:00
|
|
|
fi->is_var_size = get_bits1(gb);
|
2010-12-07 13:30:18 +00: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;
|
2011-04-23 11:37:28 +00:00
|
|
|
if (fi->ch_mode <= 5)
|
|
|
|
avctx->channel_layout = ff_vorbis_channel_layouts[fi->ch_mode];
|
2010-12-07 13:30:18 +00:00
|
|
|
fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
|
|
|
|
} else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) {
|
|
|
|
fi->channels = 2;
|
2011-04-29 08:42:43 +00:00
|
|
|
avctx->channel_layout = AV_CH_LAYOUT_STEREO;
|
2010-12-07 13:30:18 +00:00
|
|
|
} else {
|
2010-12-07 14:02:42 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"invalid channel mode: %d\n", fi->ch_mode);
|
2010-12-07 13:30:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bits per sample */
|
|
|
|
bps_code = get_bits(gb, 3);
|
|
|
|
if (bps_code == 3 || bps_code == 7) {
|
2010-12-07 14:02:42 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"invalid sample size code (%d)\n",
|
2010-12-07 13:30:18 +00:00
|
|
|
bps_code);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fi->bps = sample_size_table[bps_code];
|
|
|
|
|
|
|
|
/* reserved bit */
|
|
|
|
if (get_bits1(gb)) {
|
2010-12-07 14:02:42 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"broken stream, invalid padding\n");
|
2010-12-07 13:30:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sample or frame count */
|
2010-12-07 13:50:13 +00:00
|
|
|
fi->frame_or_sample_num = get_utf8(gb);
|
|
|
|
if (fi->frame_or_sample_num < 0) {
|
2010-12-07 14:02:42 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"sample/frame number invalid; utf8 fscked\n");
|
2010-12-07 13:30:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* blocksize */
|
|
|
|
if (bs_code == 0) {
|
2010-12-07 14:02:42 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"reserved blocksize code: 0\n");
|
2010-12-07 13:30:18 +00:00
|
|
|
return -1;
|
|
|
|
} 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 14:02:42 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"illegal sample rate code %d\n",
|
2010-12-07 13:30:18 +00:00
|
|
|
sr_code);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 14:02:42 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR + log_level_offset,
|
|
|
|
"header crc mismatch\n");
|
2010-12-07 13:30:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-21 01:54:31 +00: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;
|
|
|
|
}
|