libavcodec/flac.c
Go to the documentation of this file.
00001 /*
00002  * FLAC common code
00003  * Copyright (c) 2009 Justin Ruggles
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #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     /* frame sync code */
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     /* variable block size stream code */
00048     fi->is_var_size = get_bits1(gb);
00049 
00050     /* block size and sample rate codes */
00051     bs_code = get_bits(gb, 4);
00052     sr_code = get_bits(gb, 4);
00053 
00054     /* channels and decorrelation */
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     /* bits per sample */
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     /* reserved bit */
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     /* sample or frame count */
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     /* blocksize */
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     /* sample rate */
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     /* header CRC-8 check */
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     /* Technically, there is no limit to FLAC frame size, but an encoder
00139        should not write a frame that is larger than if verbatim encoding mode
00140        were to be used. */
00141 
00142     int count;
00143 
00144     count = 16;                  /* frame header */
00145     count += ch * ((7+bps+7)/8); /* subframe headers */
00146     if (ch == 2) {
00147         /* for stereo, need to account for using decorrelation */
00148         count += (( 2*bps+1) * blocksize + 7) / 8;
00149     } else {
00150         count += ( ch*bps    * blocksize + 7) / 8;
00151     }
00152     count += 2; /* frame footer */
00153 
00154     return count;
00155 }