From 625daac4bddda7858d16869c90ba7df95e7702c6 Mon Sep 17 00:00:00 2001 From: Michael Chinen Date: Tue, 7 Dec 2010 13:30:18 +0000 Subject: [PATCH] Move decode_frame_header() from flacdec.c to flac.c/h to share with the forthcoming FLAC parser. Patch by Michael Chinen [mchinen at gmail] Originally committed as revision 25909 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/flac.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/flac.h | 10 +++++ libavcodec/flacdec.c | 102 +-------------------------------------------------- 3 files changed, 105 insertions(+), 101 deletions(-) diff --git a/libavcodec/flac.c b/libavcodec/flac.c index a649e08..f6b65ce 100644 --- a/libavcodec/flac.c +++ b/libavcodec/flac.c @@ -19,7 +19,101 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/crc.h" #include "flac.h" +#include "flacdata.h" + +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, + FLACFrameInfo *fi) +{ + int bs_code, sr_code, bps_code; + + /* frame sync code */ + skip_bits(gb, 16); + + /* 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; + } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) { + fi->channels = 2; + } else { + av_log(avctx, AV_LOG_ERROR, "invalid channel mode: %d\n", fi->ch_mode); + return -1; + } + + /* bits per sample */ + bps_code = get_bits(gb, 3); + if (bps_code == 3 || bps_code == 7) { + av_log(avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n", + bps_code); + return -1; + } + fi->bps = sample_size_table[bps_code]; + + /* reserved bit */ + if (get_bits1(gb)) { + av_log(avctx, AV_LOG_ERROR, "broken stream, invalid padding\n"); + return -1; + } + + /* sample or frame count */ + if (get_utf8(gb) < 0) { + av_log(avctx, AV_LOG_ERROR, "utf8 fscked\n"); + return -1; + } + + /* blocksize */ + if (bs_code == 0) { + av_log(avctx, AV_LOG_ERROR, "reserved blocksize code: 0\n"); + 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 { + av_log(avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", + 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)) { + av_log(avctx, AV_LOG_ERROR, "header crc mismatch\n"); + return -1; + } + + return 0; +} int ff_flac_get_max_frame_size(int blocksize, int ch, int bps) { diff --git a/libavcodec/flac.h b/libavcodec/flac.h index 1b11463..fe38463 100644 --- a/libavcodec/flac.h +++ b/libavcodec/flac.h @@ -28,6 +28,7 @@ #define AVCODEC_FLAC_H #include "avcodec.h" +#include "get_bits.h" #define FLAC_STREAMINFO_SIZE 34 #define FLAC_MAX_CHANNELS 8 @@ -120,4 +121,13 @@ void ff_flac_parse_block_header(const uint8_t *block_header, */ int ff_flac_get_max_frame_size(int blocksize, int ch, int bps); +/** + * Validate and decode a frame header. + * @param avctx AVCodecContext to use as av_log() context + * @param gb GetBitContext from which to read frame header + * @param[out] fi frame information + * @return non-zero on error, 0 if ok + */ +int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, + FLACFrameInfo *fi); #endif /* AVCODEC_FLAC_H */ diff --git a/libavcodec/flacdec.c b/libavcodec/flacdec.c index 8488a9d..f620694 100644 --- a/libavcodec/flacdec.c +++ b/libavcodec/flacdec.c @@ -67,16 +67,6 @@ typedef struct FLACContext { unsigned int allocated_bitstream_size; } FLACContext; -static const int 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; -} - static void allocate_buffers(FLACContext *s); int ff_flac_is_extradata_valid(AVCodecContext *avctx, @@ -480,103 +470,13 @@ static inline int decode_subframe(FLACContext *s, int channel) return 0; } -/** - * Validate and decode a frame header. - * @param avctx AVCodecContext to use as av_log() context - * @param gb GetBitContext from which to read frame header - * @param[out] fi frame information - * @return non-zero on error, 0 if ok - */ -static int decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, - FLACFrameInfo *fi) -{ - int bs_code, sr_code, bps_code; - - /* frame sync code */ - skip_bits(gb, 16); - - /* 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; - } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) { - fi->channels = 2; - } else { - av_log(avctx, AV_LOG_ERROR, "invalid channel mode: %d\n", fi->ch_mode); - return -1; - } - - /* bits per sample */ - bps_code = get_bits(gb, 3); - if (bps_code == 3 || bps_code == 7) { - av_log(avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n", - bps_code); - return -1; - } - fi->bps = sample_size_table[bps_code]; - - /* reserved bit */ - if (get_bits1(gb)) { - av_log(avctx, AV_LOG_ERROR, "broken stream, invalid padding\n"); - return -1; - } - - /* sample or frame count */ - if (get_utf8(gb) < 0) { - av_log(avctx, AV_LOG_ERROR, "utf8 fscked\n"); - return -1; - } - - /* blocksize */ - if (bs_code == 0) { - av_log(avctx, AV_LOG_ERROR, "reserved blocksize code: 0\n"); - 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 { - av_log(avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", - 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)) { - av_log(avctx, AV_LOG_ERROR, "header crc mismatch\n"); - return -1; - } - - return 0; -} - static int decode_frame(FLACContext *s) { int i; GetBitContext *gb = &s->gb; FLACFrameInfo fi; - if (decode_frame_header(s->avctx, gb, &fi)) { + if (ff_flac_decode_frame_header(s->avctx, gb, &fi)) { av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n"); return -1; } -- 2.7.4