• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/ac3_parser.c

Go to the documentation of this file.
00001 /*
00002  * AC-3 parser
00003  * Copyright (c) 2003 Fabrice Bellard
00004  * Copyright (c) 2003 Michael Niedermayer
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "parser.h"
00024 #include "ac3_parser.h"
00025 #include "aac_ac3_parser.h"
00026 #include "bitstream.h"
00027 
00028 
00029 #define AC3_HEADER_SIZE 7
00030 
00031 
00032 static const uint8_t eac3_blocks[4] = {
00033     1, 2, 3, 6
00034 };
00035 
00036 
00037 int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
00038 {
00039     int frame_size_code;
00040 
00041     memset(hdr, 0, sizeof(*hdr));
00042 
00043     hdr->sync_word = get_bits(gbc, 16);
00044     if(hdr->sync_word != 0x0B77)
00045         return AAC_AC3_PARSE_ERROR_SYNC;
00046 
00047     /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
00048     hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
00049     if(hdr->bitstream_id > 16)
00050         return AAC_AC3_PARSE_ERROR_BSID;
00051 
00052     hdr->num_blocks = 6;
00053 
00054     /* set default mix levels */
00055     hdr->center_mix_level   = 1;  // -4.5dB
00056     hdr->surround_mix_level = 1;  // -6.0dB
00057 
00058     if(hdr->bitstream_id <= 10) {
00059         /* Normal AC-3 */
00060         hdr->crc1 = get_bits(gbc, 16);
00061         hdr->sr_code = get_bits(gbc, 2);
00062         if(hdr->sr_code == 3)
00063             return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
00064 
00065         frame_size_code = get_bits(gbc, 6);
00066         if(frame_size_code > 37)
00067             return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
00068 
00069         skip_bits(gbc, 5); // skip bsid, already got it
00070 
00071         skip_bits(gbc, 3); // skip bitstream mode
00072         hdr->channel_mode = get_bits(gbc, 3);
00073 
00074         if(hdr->channel_mode == AC3_CHMODE_STEREO) {
00075             skip_bits(gbc, 2); // skip dsurmod
00076         } else {
00077             if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
00078                 hdr->center_mix_level = get_bits(gbc, 2);
00079             if(hdr->channel_mode & 4)
00080                 hdr->surround_mix_level = get_bits(gbc, 2);
00081         }
00082         hdr->lfe_on = get_bits1(gbc);
00083 
00084         hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
00085         hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
00086         hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
00087         hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
00088         hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
00089         hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
00090         hdr->substreamid = 0;
00091     } else {
00092         /* Enhanced AC-3 */
00093         hdr->crc1 = 0;
00094         hdr->frame_type = get_bits(gbc, 2);
00095         if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
00096             return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
00097 
00098         hdr->substreamid = get_bits(gbc, 3);
00099 
00100         hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
00101         if(hdr->frame_size < AC3_HEADER_SIZE)
00102             return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
00103 
00104         hdr->sr_code = get_bits(gbc, 2);
00105         if (hdr->sr_code == 3) {
00106             int sr_code2 = get_bits(gbc, 2);
00107             if(sr_code2 == 3)
00108                 return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
00109             hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
00110             hdr->sr_shift = 1;
00111         } else {
00112             hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];
00113             hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
00114             hdr->sr_shift = 0;
00115         }
00116 
00117         hdr->channel_mode = get_bits(gbc, 3);
00118         hdr->lfe_on = get_bits1(gbc);
00119 
00120         hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
00121                         (hdr->num_blocks * 256.0));
00122         hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
00123     }
00124 
00125     return 0;
00126 }
00127 
00128 int ff_ac3_parse_header_full(GetBitContext *gbc, AC3HeaderInfo *hdr){
00129     int ret, i;
00130     ret = ff_ac3_parse_header(gbc, hdr);
00131     if(!ret){
00132         if(hdr->bitstream_id>10){
00133             /* Enhanced AC-3 */
00134             skip_bits(gbc, 5); // skip bitstream id
00135 
00136             /* skip dialog normalization and compression gain */
00137             for (i = 0; i < (hdr->channel_mode ? 1 : 2); i++) {
00138                 skip_bits(gbc, 5); // skip dialog normalization
00139                 if (get_bits1(gbc)) {
00140                     skip_bits(gbc, 8); //skip Compression gain word
00141                 }
00142             }
00143             /* dependent stream channel map */
00144             if (hdr->frame_type == EAC3_FRAME_TYPE_DEPENDENT && get_bits1(gbc)) {
00145                     hdr->channel_map = get_bits(gbc, 16); //custom channel map
00146                     return 0;
00147             }
00148         }
00149         //default channel map based on acmod and lfeon
00150         hdr->channel_map = ff_eac3_default_chmap[hdr->channel_mode];
00151         if(hdr->lfe_on)
00152             hdr->channel_map |= AC3_CHMAP_LFE;
00153     }
00154     return ret;
00155 }
00156 
00157 static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
00158         int *need_next_header, int *new_frame_start)
00159 {
00160     int err;
00161     union {
00162         uint64_t u64;
00163         uint8_t  u8[8];
00164     } tmp = { be2me_64(state) };
00165     AC3HeaderInfo hdr;
00166     GetBitContext gbc;
00167 
00168     init_get_bits(&gbc, tmp.u8+8-AC3_HEADER_SIZE, 54);
00169     err = ff_ac3_parse_header(&gbc, &hdr);
00170 
00171     if(err < 0)
00172         return 0;
00173 
00174     hdr_info->sample_rate = hdr.sample_rate;
00175     hdr_info->bit_rate = hdr.bit_rate;
00176     hdr_info->channels = hdr.channels;
00177     hdr_info->samples = hdr.num_blocks * 256;
00178     if(hdr.bitstream_id>10)
00179         hdr_info->codec_id = CODEC_ID_EAC3;
00180     else
00181         hdr_info->codec_id = CODEC_ID_AC3;
00182 
00183     *need_next_header = (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
00184     *new_frame_start  = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
00185     return hdr.frame_size;
00186 }
00187 
00188 static av_cold int ac3_parse_init(AVCodecParserContext *s1)
00189 {
00190     AACAC3ParseContext *s = s1->priv_data;
00191     s->header_size = AC3_HEADER_SIZE;
00192     s->sync = ac3_sync;
00193     return 0;
00194 }
00195 
00196 
00197 AVCodecParser ac3_parser = {
00198     { CODEC_ID_AC3, CODEC_ID_EAC3 },
00199     sizeof(AACAC3ParseContext),
00200     ac3_parse_init,
00201     ff_aac_ac3_parse,
00202     ff_parse_close,
00203 };

Generated on Sat Feb 16 2013 09:23:11 for ffmpeg by  doxygen 1.7.1