Libav 0.7.1
|
00001 /* 00002 * FLAC common code 00003 * Copyright (c) 2009 Justin Ruggles 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "libavutil/crc.h" 00023 #include "flac.h" 00024 #include "flacdata.h" 00025 00026 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 }; 00027 00028 static int64_t get_utf8(GetBitContext *gb) 00029 { 00030 int64_t val; 00031 GET_UTF8(val, get_bits(gb, 8), return -1;) 00032 return val; 00033 } 00034 00035 int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, 00036 FLACFrameInfo *fi, int log_level_offset) 00037 { 00038 int bs_code, sr_code, bps_code; 00039 00040 /* frame sync code */ 00041 if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) { 00042 av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n"); 00043 return -1; 00044 } 00045 00046 /* variable block size stream code */ 00047 fi->is_var_size = get_bits1(gb); 00048 00049 /* block size and sample rate codes */ 00050 bs_code = get_bits(gb, 4); 00051 sr_code = get_bits(gb, 4); 00052 00053 /* channels and decorrelation */ 00054 fi->ch_mode = get_bits(gb, 4); 00055 if (fi->ch_mode < FLAC_MAX_CHANNELS) { 00056 fi->channels = fi->ch_mode + 1; 00057 fi->ch_mode = FLAC_CHMODE_INDEPENDENT; 00058 } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) { 00059 fi->channels = 2; 00060 } else { 00061 av_log(avctx, AV_LOG_ERROR + log_level_offset, 00062 "invalid channel mode: %d\n", fi->ch_mode); 00063 return -1; 00064 } 00065 00066 /* bits per sample */ 00067 bps_code = get_bits(gb, 3); 00068 if (bps_code == 3 || bps_code == 7) { 00069 av_log(avctx, AV_LOG_ERROR + log_level_offset, 00070 "invalid sample size code (%d)\n", 00071 bps_code); 00072 return -1; 00073 } 00074 fi->bps = sample_size_table[bps_code]; 00075 00076 /* reserved bit */ 00077 if (get_bits1(gb)) { 00078 av_log(avctx, AV_LOG_ERROR + log_level_offset, 00079 "broken stream, invalid padding\n"); 00080 return -1; 00081 } 00082 00083 /* sample or frame count */ 00084 fi->frame_or_sample_num = get_utf8(gb); 00085 if (fi->frame_or_sample_num < 0) { 00086 av_log(avctx, AV_LOG_ERROR + log_level_offset, 00087 "sample/frame number invalid; utf8 fscked\n"); 00088 return -1; 00089 } 00090 00091 /* blocksize */ 00092 if (bs_code == 0) { 00093 av_log(avctx, AV_LOG_ERROR + log_level_offset, 00094 "reserved blocksize code: 0\n"); 00095 return -1; 00096 } else if (bs_code == 6) { 00097 fi->blocksize = get_bits(gb, 8) + 1; 00098 } else if (bs_code == 7) { 00099 fi->blocksize = get_bits(gb, 16) + 1; 00100 } else { 00101 fi->blocksize = ff_flac_blocksize_table[bs_code]; 00102 } 00103 00104 /* sample rate */ 00105 if (sr_code < 12) { 00106 fi->samplerate = ff_flac_sample_rate_table[sr_code]; 00107 } else if (sr_code == 12) { 00108 fi->samplerate = get_bits(gb, 8) * 1000; 00109 } else if (sr_code == 13) { 00110 fi->samplerate = get_bits(gb, 16); 00111 } else if (sr_code == 14) { 00112 fi->samplerate = get_bits(gb, 16) * 10; 00113 } else { 00114 av_log(avctx, AV_LOG_ERROR + log_level_offset, 00115 "illegal sample rate code %d\n", 00116 sr_code); 00117 return -1; 00118 } 00119 00120 /* header CRC-8 check */ 00121 skip_bits(gb, 8); 00122 if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer, 00123 get_bits_count(gb)/8)) { 00124 av_log(avctx, AV_LOG_ERROR + log_level_offset, 00125 "header crc mismatch\n"); 00126 return -1; 00127 } 00128 00129 return 0; 00130 } 00131 00132 int ff_flac_get_max_frame_size(int blocksize, int ch, int bps) 00133 { 00134 /* Technically, there is no limit to FLAC frame size, but an encoder 00135 should not write a frame that is larger than if verbatim encoding mode 00136 were to be used. */ 00137 00138 int count; 00139 00140 count = 16; /* frame header */ 00141 count += ch * ((7+bps+7)/8); /* subframe headers */ 00142 if (ch == 2) { 00143 /* for stereo, need to account for using decorrelation */ 00144 count += (( 2*bps+1) * blocksize + 7) / 8; 00145 } else { 00146 count += ( ch*bps * blocksize + 7) / 8; 00147 } 00148 count += 2; /* frame footer */ 00149 00150 return count; 00151 }