00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/crc.h"
00023 #include "flac.h"
00024 #include "flacdata.h"
00025 #include "vorbis.h"
00026
00027 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
00028
00029 static int64_t get_utf8(GetBitContext *gb)
00030 {
00031 int64_t val;
00032 GET_UTF8(val, get_bits(gb, 8), return -1;)
00033 return val;
00034 }
00035
00036 int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
00037 FLACFrameInfo *fi, int log_level_offset)
00038 {
00039 int bs_code, sr_code, bps_code;
00040
00041
00042 if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
00043 av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
00044 return -1;
00045 }
00046
00047
00048 fi->is_var_size = get_bits1(gb);
00049
00050
00051 bs_code = get_bits(gb, 4);
00052 sr_code = get_bits(gb, 4);
00053
00054
00055 fi->ch_mode = get_bits(gb, 4);
00056 if (fi->ch_mode < FLAC_MAX_CHANNELS) {
00057 fi->channels = fi->ch_mode + 1;
00058 if (fi->ch_mode <= 5)
00059 avctx->channel_layout = ff_vorbis_channel_layouts[fi->ch_mode];
00060 fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
00061 } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) {
00062 fi->channels = 2;
00063 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
00064 } else {
00065 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00066 "invalid channel mode: %d\n", fi->ch_mode);
00067 return -1;
00068 }
00069
00070
00071 bps_code = get_bits(gb, 3);
00072 if (bps_code == 3 || bps_code == 7) {
00073 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00074 "invalid sample size code (%d)\n",
00075 bps_code);
00076 return -1;
00077 }
00078 fi->bps = sample_size_table[bps_code];
00079
00080
00081 if (get_bits1(gb)) {
00082 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00083 "broken stream, invalid padding\n");
00084 return -1;
00085 }
00086
00087
00088 fi->frame_or_sample_num = get_utf8(gb);
00089 if (fi->frame_or_sample_num < 0) {
00090 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00091 "sample/frame number invalid; utf8 fscked\n");
00092 return -1;
00093 }
00094
00095
00096 if (bs_code == 0) {
00097 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00098 "reserved blocksize code: 0\n");
00099 return -1;
00100 } else if (bs_code == 6) {
00101 fi->blocksize = get_bits(gb, 8) + 1;
00102 } else if (bs_code == 7) {
00103 fi->blocksize = get_bits(gb, 16) + 1;
00104 } else {
00105 fi->blocksize = ff_flac_blocksize_table[bs_code];
00106 }
00107
00108
00109 if (sr_code < 12) {
00110 fi->samplerate = ff_flac_sample_rate_table[sr_code];
00111 } else if (sr_code == 12) {
00112 fi->samplerate = get_bits(gb, 8) * 1000;
00113 } else if (sr_code == 13) {
00114 fi->samplerate = get_bits(gb, 16);
00115 } else if (sr_code == 14) {
00116 fi->samplerate = get_bits(gb, 16) * 10;
00117 } else {
00118 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00119 "illegal sample rate code %d\n",
00120 sr_code);
00121 return -1;
00122 }
00123
00124
00125 skip_bits(gb, 8);
00126 if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
00127 get_bits_count(gb)/8)) {
00128 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00129 "header crc mismatch\n");
00130 return -1;
00131 }
00132
00133 return 0;
00134 }
00135
00136 int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
00137 {
00138
00139
00140
00141
00142 int count;
00143
00144 count = 16;
00145 count += ch * ((7+bps+7)/8);
00146 if (ch == 2) {
00147
00148 count += (( 2*bps+1) * blocksize + 7) / 8;
00149 } else {
00150 count += ( ch*bps * blocksize + 7) / 8;
00151 }
00152 count += 2;
00153
00154 return count;
00155 }