00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/channel_layout.h"
00023 #include "libavutil/crc.h"
00024 #include "libavutil/log.h"
00025 #include "bytestream.h"
00026 #include "get_bits.h"
00027 #include "flac.h"
00028 #include "flacdata.h"
00029
00030 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
00031
00032 static const int64_t flac_channel_layouts[6] = {
00033 AV_CH_LAYOUT_MONO,
00034 AV_CH_LAYOUT_STEREO,
00035 AV_CH_LAYOUT_SURROUND,
00036 AV_CH_LAYOUT_QUAD,
00037 AV_CH_LAYOUT_5POINT0,
00038 AV_CH_LAYOUT_5POINT1
00039 };
00040
00041 static int64_t get_utf8(GetBitContext *gb)
00042 {
00043 int64_t val;
00044 GET_UTF8(val, get_bits(gb, 8), return -1;)
00045 return val;
00046 }
00047
00048 int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
00049 FLACFrameInfo *fi, int log_level_offset)
00050 {
00051 int bs_code, sr_code, bps_code;
00052
00053
00054 if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
00055 av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
00056 return -1;
00057 }
00058
00059
00060 fi->is_var_size = get_bits1(gb);
00061
00062
00063 bs_code = get_bits(gb, 4);
00064 sr_code = get_bits(gb, 4);
00065
00066
00067 fi->ch_mode = get_bits(gb, 4);
00068 if (fi->ch_mode < FLAC_MAX_CHANNELS) {
00069 fi->channels = fi->ch_mode + 1;
00070 fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
00071 } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
00072 fi->channels = 2;
00073 fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
00074 } else {
00075 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00076 "invalid channel mode: %d\n", fi->ch_mode);
00077 return -1;
00078 }
00079
00080
00081 bps_code = get_bits(gb, 3);
00082 if (bps_code == 3 || bps_code == 7) {
00083 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00084 "invalid sample size code (%d)\n",
00085 bps_code);
00086 return -1;
00087 }
00088 fi->bps = sample_size_table[bps_code];
00089
00090
00091 if (get_bits1(gb)) {
00092 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00093 "broken stream, invalid padding\n");
00094 return -1;
00095 }
00096
00097
00098 fi->frame_or_sample_num = get_utf8(gb);
00099 if (fi->frame_or_sample_num < 0) {
00100 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00101 "sample/frame number invalid; utf8 fscked\n");
00102 return -1;
00103 }
00104
00105
00106 if (bs_code == 0) {
00107 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00108 "reserved blocksize code: 0\n");
00109 return -1;
00110 } else if (bs_code == 6) {
00111 fi->blocksize = get_bits(gb, 8) + 1;
00112 } else if (bs_code == 7) {
00113 fi->blocksize = get_bits(gb, 16) + 1;
00114 } else {
00115 fi->blocksize = ff_flac_blocksize_table[bs_code];
00116 }
00117
00118
00119 if (sr_code < 12) {
00120 fi->samplerate = ff_flac_sample_rate_table[sr_code];
00121 } else if (sr_code == 12) {
00122 fi->samplerate = get_bits(gb, 8) * 1000;
00123 } else if (sr_code == 13) {
00124 fi->samplerate = get_bits(gb, 16);
00125 } else if (sr_code == 14) {
00126 fi->samplerate = get_bits(gb, 16) * 10;
00127 } else {
00128 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00129 "illegal sample rate code %d\n",
00130 sr_code);
00131 return -1;
00132 }
00133
00134
00135 skip_bits(gb, 8);
00136 if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
00137 get_bits_count(gb)/8)) {
00138 av_log(avctx, AV_LOG_ERROR + log_level_offset,
00139 "header crc mismatch\n");
00140 return -1;
00141 }
00142
00143 return 0;
00144 }
00145
00146 int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
00147 {
00148
00149
00150
00151
00152 int count;
00153
00154 count = 16;
00155 count += ch * ((7+bps+7)/8);
00156 if (ch == 2) {
00157
00158 count += (( 2*bps+1) * blocksize + 7) / 8;
00159 } else {
00160 count += ( ch*bps * blocksize + 7) / 8;
00161 }
00162 count += 2;
00163
00164 return count;
00165 }
00166
00167 int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
00168 enum FLACExtradataFormat *format,
00169 uint8_t **streaminfo_start)
00170 {
00171 if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
00172 av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
00173 return 0;
00174 }
00175 if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
00176
00177 if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
00178 av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
00179 FLAC_STREAMINFO_SIZE-avctx->extradata_size);
00180 }
00181 *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
00182 *streaminfo_start = avctx->extradata;
00183 } else {
00184 if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
00185 av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
00186 return 0;
00187 }
00188 *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
00189 *streaminfo_start = &avctx->extradata[8];
00190 }
00191 return 1;
00192 }
00193
00194 void ff_flac_set_channel_layout(AVCodecContext *avctx)
00195 {
00196 if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
00197 avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
00198 else
00199 avctx->channel_layout = 0;
00200 }
00201
00202 void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
00203 const uint8_t *buffer)
00204 {
00205 GetBitContext gb;
00206 init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
00207
00208 skip_bits(&gb, 16);
00209 s->max_blocksize = get_bits(&gb, 16);
00210 if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
00211 av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
00212 s->max_blocksize);
00213 s->max_blocksize = 16;
00214 }
00215
00216 skip_bits(&gb, 24);
00217 s->max_framesize = get_bits_long(&gb, 24);
00218
00219 s->samplerate = get_bits_long(&gb, 20);
00220 s->channels = get_bits(&gb, 3) + 1;
00221 s->bps = get_bits(&gb, 5) + 1;
00222
00223 avctx->channels = s->channels;
00224 avctx->sample_rate = s->samplerate;
00225 avctx->bits_per_raw_sample = s->bps;
00226 ff_flac_set_channel_layout(avctx);
00227
00228 s->samples = get_bits64(&gb, 36);
00229
00230 skip_bits_long(&gb, 64);
00231 skip_bits_long(&gb, 64);
00232 }
00233
00234 void avpriv_flac_parse_block_header(const uint8_t *block_header,
00235 int *last, int *type, int *size)
00236 {
00237 int tmp = bytestream_get_byte(&block_header);
00238 if (last)
00239 *last = tmp & 0x80;
00240 if (type)
00241 *type = tmp & 0x7F;
00242 if (size)
00243 *size = bytestream_get_be24(&block_header);
00244 }