3 * Copyright (c) 2009 Justin Ruggles
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/crc.h"
27 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
29 static int64_t get_utf8(GetBitContext *gb)
32 GET_UTF8(val, get_bits(gb, 8), return -1;)
36 int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
37 FLACFrameInfo *fi, int log_level_offset)
39 int bs_code, sr_code, bps_code;
42 if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
43 av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
47 /* variable block size stream code */
48 fi->is_var_size = get_bits1(gb);
50 /* block size and sample rate codes */
51 bs_code = get_bits(gb, 4);
52 sr_code = get_bits(gb, 4);
54 /* channels and decorrelation */
55 fi->ch_mode = get_bits(gb, 4);
56 if (fi->ch_mode < FLAC_MAX_CHANNELS) {
57 fi->channels = fi->ch_mode + 1;
59 avctx->channel_layout = ff_vorbis_channel_layouts[fi->ch_mode];
60 fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
61 } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) {
63 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
65 av_log(avctx, AV_LOG_ERROR + log_level_offset,
66 "invalid channel mode: %d\n", fi->ch_mode);
71 bps_code = get_bits(gb, 3);
72 if (bps_code == 3 || bps_code == 7) {
73 av_log(avctx, AV_LOG_ERROR + log_level_offset,
74 "invalid sample size code (%d)\n",
78 fi->bps = sample_size_table[bps_code];
82 av_log(avctx, AV_LOG_ERROR + log_level_offset,
83 "broken stream, invalid padding\n");
87 /* sample or frame count */
88 fi->frame_or_sample_num = get_utf8(gb);
89 if (fi->frame_or_sample_num < 0) {
90 av_log(avctx, AV_LOG_ERROR + log_level_offset,
91 "sample/frame number invalid; utf8 fscked\n");
97 av_log(avctx, AV_LOG_ERROR + log_level_offset,
98 "reserved blocksize code: 0\n");
100 } else if (bs_code == 6) {
101 fi->blocksize = get_bits(gb, 8) + 1;
102 } else if (bs_code == 7) {
103 fi->blocksize = get_bits(gb, 16) + 1;
105 fi->blocksize = ff_flac_blocksize_table[bs_code];
110 fi->samplerate = ff_flac_sample_rate_table[sr_code];
111 } else if (sr_code == 12) {
112 fi->samplerate = get_bits(gb, 8) * 1000;
113 } else if (sr_code == 13) {
114 fi->samplerate = get_bits(gb, 16);
115 } else if (sr_code == 14) {
116 fi->samplerate = get_bits(gb, 16) * 10;
118 av_log(avctx, AV_LOG_ERROR + log_level_offset,
119 "illegal sample rate code %d\n",
124 /* header CRC-8 check */
126 if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
127 get_bits_count(gb)/8)) {
128 av_log(avctx, AV_LOG_ERROR + log_level_offset,
129 "header crc mismatch\n");
136 int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
138 /* Technically, there is no limit to FLAC frame size, but an encoder
139 should not write a frame that is larger than if verbatim encoding mode
144 count = 16; /* frame header */
145 count += ch * ((7+bps+7)/8); /* subframe headers */
147 /* for stereo, need to account for using decorrelation */
148 count += (( 2*bps+1) * blocksize + 7) / 8;
150 count += ( ch*bps * blocksize + 7) / 8;
152 count += 2; /* frame footer */