• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 Libav.
00007  *
00008  * Libav 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  * Libav 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 Libav; 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 "get_bits.h"
00027 #include "libavutil/audioconvert.h"
00028 
00029 
00030 #define AC3_HEADER_SIZE 7
00031 
00032 
00033 static const uint8_t eac3_blocks[4] = {
00034     1, 2, 3, 6
00035 };
00036 
00041 static const uint8_t center_levels[4] = { 4, 5, 6, 5 };
00042 
00047 static const uint8_t surround_levels[4] = { 4, 6, 7, 6 };
00048 
00049 
00050 int avpriv_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
00051 {
00052     int frame_size_code;
00053 
00054     memset(hdr, 0, sizeof(*hdr));
00055 
00056     hdr->sync_word = get_bits(gbc, 16);
00057     if(hdr->sync_word != 0x0B77)
00058         return AAC_AC3_PARSE_ERROR_SYNC;
00059 
00060     /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
00061     hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
00062     if(hdr->bitstream_id > 16)
00063         return AAC_AC3_PARSE_ERROR_BSID;
00064 
00065     hdr->num_blocks = 6;
00066 
00067     /* set default mix levels */
00068     hdr->center_mix_level   = 5;  // -4.5dB
00069     hdr->surround_mix_level = 6;  // -6.0dB
00070 
00071     if(hdr->bitstream_id <= 10) {
00072         /* Normal AC-3 */
00073         hdr->crc1 = get_bits(gbc, 16);
00074         hdr->sr_code = get_bits(gbc, 2);
00075         if(hdr->sr_code == 3)
00076             return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
00077 
00078         frame_size_code = get_bits(gbc, 6);
00079         if(frame_size_code > 37)
00080             return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
00081 
00082         skip_bits(gbc, 5); // skip bsid, already got it
00083 
00084         hdr->bitstream_mode = get_bits(gbc, 3);
00085         hdr->channel_mode = get_bits(gbc, 3);
00086 
00087         if(hdr->channel_mode == AC3_CHMODE_STEREO) {
00088             skip_bits(gbc, 2); // skip dsurmod
00089         } else {
00090             if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
00091                 hdr->  center_mix_level =   center_levels[get_bits(gbc, 2)];
00092             if(hdr->channel_mode & 4)
00093                 hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)];
00094         }
00095         hdr->lfe_on = get_bits1(gbc);
00096 
00097         hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
00098         hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
00099         hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
00100         hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
00101         hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
00102         hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
00103         hdr->substreamid = 0;
00104     } else {
00105         /* Enhanced AC-3 */
00106         hdr->crc1 = 0;
00107         hdr->frame_type = get_bits(gbc, 2);
00108         if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
00109             return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
00110 
00111         hdr->substreamid = get_bits(gbc, 3);
00112 
00113         hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
00114         if(hdr->frame_size < AC3_HEADER_SIZE)
00115             return AAC_AC3_PARSE_ERROR_FRAME_SIZE;
00116 
00117         hdr->sr_code = get_bits(gbc, 2);
00118         if (hdr->sr_code == 3) {
00119             int sr_code2 = get_bits(gbc, 2);
00120             if(sr_code2 == 3)
00121                 return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;
00122             hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
00123             hdr->sr_shift = 1;
00124         } else {
00125             hdr->num_blocks = eac3_blocks[get_bits(gbc, 2)];
00126             hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
00127             hdr->sr_shift = 0;
00128         }
00129 
00130         hdr->channel_mode = get_bits(gbc, 3);
00131         hdr->lfe_on = get_bits1(gbc);
00132 
00133         hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
00134                         (hdr->num_blocks * 256.0));
00135         hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
00136     }
00137     hdr->channel_layout = avpriv_ac3_channel_layout_tab[hdr->channel_mode];
00138     if (hdr->lfe_on)
00139         hdr->channel_layout |= AV_CH_LOW_FREQUENCY;
00140 
00141     return 0;
00142 }
00143 
00144 static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
00145         int *need_next_header, int *new_frame_start)
00146 {
00147     int err;
00148     union {
00149         uint64_t u64;
00150         uint8_t  u8[8];
00151     } tmp = { av_be2ne64(state) };
00152     AC3HeaderInfo hdr;
00153     GetBitContext gbc;
00154 
00155     init_get_bits(&gbc, tmp.u8+8-AC3_HEADER_SIZE, 54);
00156     err = avpriv_ac3_parse_header(&gbc, &hdr);
00157 
00158     if(err < 0)
00159         return 0;
00160 
00161     hdr_info->sample_rate = hdr.sample_rate;
00162     hdr_info->bit_rate = hdr.bit_rate;
00163     hdr_info->channels = hdr.channels;
00164     hdr_info->channel_layout = hdr.channel_layout;
00165     hdr_info->samples = hdr.num_blocks * 256;
00166     hdr_info->service_type = hdr.bitstream_mode;
00167     if (hdr.bitstream_mode == 0x7 && hdr.channels > 1)
00168         hdr_info->service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
00169     if(hdr.bitstream_id>10)
00170         hdr_info->codec_id = CODEC_ID_EAC3;
00171     else if (hdr_info->codec_id == CODEC_ID_NONE)
00172         hdr_info->codec_id = CODEC_ID_AC3;
00173 
00174     *need_next_header = (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
00175     *new_frame_start  = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
00176     return hdr.frame_size;
00177 }
00178 
00179 static av_cold int ac3_parse_init(AVCodecParserContext *s1)
00180 {
00181     AACAC3ParseContext *s = s1->priv_data;
00182     s->header_size = AC3_HEADER_SIZE;
00183     s->sync = ac3_sync;
00184     return 0;
00185 }
00186 
00187 
00188 AVCodecParser ff_ac3_parser = {
00189     .codec_ids      = { CODEC_ID_AC3, CODEC_ID_EAC3 },
00190     .priv_data_size = sizeof(AACAC3ParseContext),
00191     .parser_init    = ac3_parse_init,
00192     .parser_parse   = ff_aac_ac3_parse,
00193     .parser_close   = ff_parse_close,
00194 };
Generated on Thu Jan 24 2013 17:08:49 for Libav by doxygen 1.7.1