a52.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * a52.h
00003  *****************************************************************************
00004  * Copyright (C) 2001-2009 Laurent Aimar
00005  * $Id: 204be334fd9b8dfbe22ac1b191f65676d80efe8d $
00006  *
00007  * Authors: Stéphane Borel <stef@via.ecp.fr>
00008  *          Christophe Massiot <massiot@via.ecp.fr>
00009  *          Gildas Bazin <gbazin@videolan.org>
00010  *          Laurent Aimar <fenrir@via.ecp.fr>
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00025  *****************************************************************************/
00026 
00027 #ifndef _VLC_A52_H
00028 #define _VLC_A52_H 1
00029 
00030 #include <vlc_aout.h>
00031 #include <vlc_bits.h>
00032 
00033 typedef struct
00034 {
00035     unsigned int i_count;
00036     unsigned int i_configuration;
00037 } vlc_a52_acmod_t;
00038 
00039 /**
00040  * Minimum AC3 header size that vlc_a52_header_Parse needs.
00041  */
00042 #define VLC_A52_HEADER_SIZE (8)
00043 
00044 /**
00045  * AC3 header information.
00046  */
00047 typedef struct
00048 {
00049     bool b_eac3;
00050 
00051     unsigned int i_channels;
00052     unsigned int i_channels_conf;
00053     unsigned int i_rate;
00054     unsigned int i_bitrate;
00055 
00056     unsigned int i_size;
00057     unsigned int i_samples;
00058 
00059 } vlc_a52_header_t;
00060 
00061 /**
00062  * It parse AC3 sync info.
00063  *
00064  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
00065  * since we don't want to oblige S/PDIF people to use liba52 just to get
00066  * their SyncInfo...
00067  */
00068 static inline int vlc_a52_header_ParseAc3( vlc_a52_header_t *p_header,
00069                                            const uint8_t *p_buf,
00070                                            const vlc_a52_acmod_t *p_acmod )
00071 {
00072     static const uint8_t pi_halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
00073     static const unsigned int pi_bitrate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
00074                                 128, 160, 192, 224, 256, 320, 384, 448,
00075                                 512, 576, 640 };
00076     static const uint8_t pi_lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
00077                                       0x04, 0x01, 0x04, 0x01 };
00078 
00079     /* */
00080     const unsigned i_rate_shift = pi_halfrate[p_buf[5] >> 3];
00081 
00082     /* acmod, dsurmod and lfeon */
00083     const unsigned i_acmod = p_buf[6] >> 5;
00084     if( (p_buf[6] & 0xf8) == 0x50 )
00085     {
00086         /* Dolby surround = stereo + Dolby */
00087         p_header->i_channels = 2;
00088         p_header->i_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
00089                             | AOUT_CHAN_DOLBYSTEREO;
00090     }
00091     else
00092     {
00093         p_header->i_channels      = p_acmod[i_acmod].i_count;
00094         p_header->i_channels_conf = p_acmod[i_acmod].i_configuration;
00095     }
00096 
00097     if( p_buf[6] & pi_lfeon[i_acmod] )
00098     {
00099         p_header->i_channels++;
00100         p_header->i_channels_conf |= AOUT_CHAN_LFE;
00101     }
00102 
00103     const unsigned i_frmsizecod = p_buf[4] & 63;
00104     if( i_frmsizecod >= 38 )
00105         return VLC_EGENERIC;
00106     const unsigned i_bitrate_base = pi_bitrate[i_frmsizecod >> 1];
00107     p_header->i_bitrate = (i_bitrate_base * 1000) >> i_rate_shift;
00108 
00109     switch( p_buf[4] & 0xc0 )
00110     {
00111     case 0:
00112         p_header->i_rate = 48000 >> i_rate_shift;
00113         p_header->i_size = 4 * i_bitrate_base;
00114         break;
00115     case 0x40:
00116         p_header->i_rate = 44100 >> i_rate_shift;
00117         p_header->i_size = 2 * (320 * i_bitrate_base / 147 + (i_frmsizecod & 1));
00118         break;
00119     case 0x80:
00120         p_header->i_rate = 32000 >> i_rate_shift;
00121         p_header->i_size = 6 * i_bitrate_base;
00122         break;
00123     default:
00124         return VLC_EGENERIC;
00125     }
00126     p_header->i_samples = 6*256;
00127 
00128     p_header->b_eac3 = false;
00129     return VLC_SUCCESS;
00130 }
00131 
00132 /**
00133  * It parse E-AC3 sync info
00134  */
00135 static inline int vlc_a52_header_ParseEac3( vlc_a52_header_t *p_header,
00136                                             const uint8_t *p_buf,
00137                                             const vlc_a52_acmod_t *p_acmod )
00138 {
00139     static const unsigned pi_samplerate[3] = { 48000, 44100, 32000 };
00140     unsigned i_numblkscod;
00141     bs_t s;
00142 
00143 
00144     bs_init( &s, (void*)p_buf, VLC_A52_HEADER_SIZE );
00145     bs_skip( &s, 16 +   /* start code */
00146                  2 +    /* stream type */
00147                  3 );   /* substream id */
00148     const unsigned i_frame_size = bs_read( &s, 11 );
00149     if( i_frame_size < 2 )
00150         return VLC_EGENERIC;
00151     p_header->i_size = 2 * ( i_frame_size + 1 );
00152 
00153     const unsigned i_fscod = bs_read( &s, 2 );
00154     if( i_fscod == 0x03 )
00155     {
00156         const unsigned i_fscod2 = bs_read( &s, 2 );
00157         if( i_fscod2 == 0X03 )
00158             return VLC_EGENERIC;
00159         p_header->i_rate = pi_samplerate[i_fscod2] / 2;
00160         i_numblkscod = 6;
00161     }
00162     else
00163     {
00164         static const int pi_blocks[4] = { 1, 2, 3, 6 };
00165 
00166         p_header->i_rate = pi_samplerate[i_fscod];
00167         i_numblkscod = pi_blocks[bs_read( &s, 2 )];
00168     }
00169 
00170     const unsigned i_acmod = bs_read( &s, 3 );
00171     const unsigned i_lfeon = bs_read1( &s );
00172 
00173     p_header->i_channels      = p_acmod[i_acmod].i_count + i_lfeon;
00174     p_header->i_channels_conf = p_acmod[i_acmod].i_configuration | ( i_lfeon ? AOUT_CHAN_LFE : 0);
00175     p_header->i_bitrate = 8 * p_header->i_size * (p_header->i_rate) / (i_numblkscod * 256);
00176     p_header->i_samples = i_numblkscod * 256;
00177 
00178     p_header->b_eac3 = true;
00179     return VLC_SUCCESS;
00180 }
00181 
00182 /**
00183  * It will parse the header AC3 frame and fill vlc_a52_header_t* if
00184  * it is valid or return VLC_EGENERIC.
00185  *
00186  * XXX It will only recognize big endian bitstream ie starting with 0x0b, 0x77
00187  */
00188 static inline int vlc_a52_header_Parse( vlc_a52_header_t *p_header,
00189                                         const uint8_t *p_buffer, int i_buffer )
00190 {
00191     static const vlc_a52_acmod_t p_acmod[8] = {
00192         { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DUALMONO },   /* Dual-channel 1+1 */
00193         { 1, AOUT_CHAN_CENTER },                                        /* Mono 1/0 */
00194         { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT },                        /* Stereo 2/0 */
00195         { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER },     /* 3F 3/0 */
00196         { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER }, /* 2F1R 2/1 */
00197         { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
00198              AOUT_CHAN_REARCENTER },                                    /* 3F1R 3/1 */
00199         { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
00200              AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT },                /* 2F2R 2/2 */
00201         { 5, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
00202              AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT },                /* 3F2R 3/2 */
00203     };
00204 
00205     if( i_buffer < VLC_A52_HEADER_SIZE )
00206         return VLC_EGENERIC;
00207 
00208     /* Check synword */
00209     if( p_buffer[0] != 0x0b || p_buffer[1] != 0x77 )
00210         return VLC_EGENERIC;
00211 
00212     /* Check bsid */
00213     const int bsid = p_buffer[5] >> 3;
00214     if( bsid > 16 )
00215         return VLC_EGENERIC;
00216 
00217     if( bsid <= 10 )
00218     {
00219         if( vlc_a52_header_ParseAc3( p_header, p_buffer, p_acmod ) )
00220             return VLC_EGENERIC;
00221     }
00222     else
00223     {
00224         if( vlc_a52_header_ParseEac3( p_header, p_buffer, p_acmod ) )
00225             return VLC_EGENERIC;
00226     }
00227     return VLC_SUCCESS;
00228 }
00229 
00230 #endif
00231 

Generated on Tue May 25 08:04:53 2010 for VLC by  doxygen 1.5.6