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
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
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
00047 fi->is_var_size = get_bits1(gb);
00048
00049
00050 bs_code = get_bits(gb, 4);
00051 sr_code = get_bits(gb, 4);
00052
00053
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
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
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
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
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
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
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
00135
00136
00137
00138 int count;
00139
00140 count = 16;
00141 count += ch * ((7+bps+7)/8);
00142 if (ch == 2) {
00143
00144 count += (( 2*bps+1) * blocksize + 7) / 8;
00145 } else {
00146 count += ( ch*bps * blocksize + 7) / 8;
00147 }
00148 count += 2;
00149
00150 return count;
00151 }