00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00041
00042 #define VLC_A52_HEADER_SIZE (8)
00043
00044
00045
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
00063
00064
00065
00066
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
00083 const unsigned i_acmod = p_buf[6] >> 5;
00084 if( (p_buf[6] & 0xf8) == 0x50 )
00085 {
00086
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
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 +
00146 2 +
00147 3 );
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
00184
00185
00186
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 },
00193 { 1, AOUT_CHAN_CENTER },
00194 { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT },
00195 { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER },
00196 { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER },
00197 { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
00198 AOUT_CHAN_REARCENTER },
00199 { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
00200 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT },
00201 { 5, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
00202 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT },
00203 };
00204
00205 if( i_buffer < VLC_A52_HEADER_SIZE )
00206 return VLC_EGENERIC;
00207
00208
00209 if( p_buffer[0] != 0x0b || p_buffer[1] != 0x77 )
00210 return VLC_EGENERIC;
00211
00212
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