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 "libavutil/log.h"
00024 #include "bytestream.h"
00025 #include "get_bits.h"
00026 #include "flac.h"
00027 #include "flacdata.h"
00028
00029 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
00030
00031 static int64_t get_utf8(GetBitContext *gb)
00032 {
00033 int64_t val;
00034 GET_UTF8(val, get_bits(gb, 8), return -1;)
00035 return val;
00036 }
00037
00038 int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
00039 FLACFrameInfo *fi, int log_level_offset)
00040 {
00041 int bs_code, sr_code, bps_code;
00042
00043
00044 if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
00045 av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
00046 return -1;
00047 }
00048
00049
00050 fi->is_var_size = get_bits1(gb);
00051
00052
00053 bs_code = get_bits(gb, 4);
00054 sr_code = get_bits(gb, 4);
00055
00056
00057 fi->ch_mode = get_bits(gb, 4);
00058 if (fi->ch_mode < FLAC_MAX_CHANNELS) {
00059 fi->channels = fi->ch_mode + 1;
00060 fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
00061 } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
00062 fi->channels = 2;
00063 fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
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 }
00156
00157 int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
00158 enum FLACExtradataFormat *format,
00159 uint8_t **streaminfo_start)
00160 {
00161 if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
00162 av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
00163 return 0;
00164 }
00165 if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
00166
00167 if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
00168 av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
00169 FLAC_STREAMINFO_SIZE-avctx->extradata_size);
00170 }
00171 *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
00172 *streaminfo_start = avctx->extradata;
00173 } else {
00174 if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
00175 av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
00176 return 0;
00177 }
00178 *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
00179 *streaminfo_start = &avctx->extradata[8];
00180 }
00181 return 1;
00182 }
00183
00184 void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
00185 const uint8_t *buffer)
00186 {
00187 GetBitContext gb;
00188 init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
00189
00190 skip_bits(&gb, 16);
00191 s->max_blocksize = get_bits(&gb, 16);
00192 if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
00193 av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
00194 s->max_blocksize);
00195 s->max_blocksize = 16;
00196 }
00197
00198 skip_bits(&gb, 24);
00199 s->max_framesize = get_bits_long(&gb, 24);
00200
00201 s->samplerate = get_bits_long(&gb, 20);
00202 s->channels = get_bits(&gb, 3) + 1;
00203 s->bps = get_bits(&gb, 5) + 1;
00204
00205 avctx->channels = s->channels;
00206 avctx->sample_rate = s->samplerate;
00207 avctx->bits_per_raw_sample = s->bps;
00208
00209 s->samples = get_bits_long(&gb, 32) << 4;
00210 s->samples |= get_bits(&gb, 4);
00211
00212 skip_bits_long(&gb, 64);
00213 skip_bits_long(&gb, 64);
00214 }
00215
00216 void avpriv_flac_parse_block_header(const uint8_t *block_header,
00217 int *last, int *type, int *size)
00218 {
00219 int tmp = bytestream_get_byte(&block_header);
00220 if (last)
00221 *last = tmp & 0x80;
00222 if (type)
00223 *type = tmp & 0x7F;
00224 if (size)
00225 *size = bytestream_get_be24(&block_header);
00226 }