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