1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2009 Laurent Aimar
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Laurent Aimar <fenrir@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
35 unsigned int i_configuration;
39 * Minimum AC3 header size that vlc_a52_header_Parse needs.
41 #define VLC_A52_HEADER_SIZE (8)
44 * AC3 header information.
50 unsigned int i_channels;
51 unsigned int i_channels_conf;
53 unsigned int i_bitrate;
56 unsigned int i_samples;
61 * It parse AC3 sync info.
63 * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
64 * since we don't want to oblige S/PDIF people to use liba52 just to get
67 static inline int vlc_a52_header_ParseAc3( vlc_a52_header_t *p_header,
69 const vlc_a52_acmod_t *p_acmod )
71 static const uint8_t pi_halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
72 static const unsigned int pi_bitrate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
73 128, 160, 192, 224, 256, 320, 384, 448,
75 static const uint8_t pi_lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
76 0x04, 0x01, 0x04, 0x01 };
79 const unsigned i_rate_shift = pi_halfrate[p_buf[5] >> 3];
81 /* acmod, dsurmod and lfeon */
82 const unsigned i_acmod = p_buf[6] >> 5;
83 if( (p_buf[6] & 0xf8) == 0x50 )
85 /* Dolby surround = stereo + Dolby */
86 p_header->i_channels = 2;
87 p_header->i_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
88 | AOUT_CHAN_DOLBYSTEREO;
92 p_header->i_channels = p_acmod[i_acmod].i_count;
93 p_header->i_channels_conf = p_acmod[i_acmod].i_configuration;
96 if( p_buf[6] & pi_lfeon[i_acmod] )
98 p_header->i_channels++;
99 p_header->i_channels_conf |= AOUT_CHAN_LFE;
102 const unsigned i_frmsizecod = p_buf[4] & 63;
103 if( i_frmsizecod >= 38 )
105 const unsigned i_bitrate_base = pi_bitrate[i_frmsizecod >> 1];
106 p_header->i_bitrate = (i_bitrate_base * 1000) >> i_rate_shift;
108 switch( p_buf[4] & 0xc0 )
111 p_header->i_rate = 48000 >> i_rate_shift;
112 p_header->i_size = 4 * i_bitrate_base;
115 p_header->i_rate = 44100 >> i_rate_shift;
116 p_header->i_size = 2 * (320 * i_bitrate_base / 147 + (i_frmsizecod & 1));
119 p_header->i_rate = 32000 >> i_rate_shift;
120 p_header->i_size = 6 * i_bitrate_base;
125 p_header->i_samples = 6*256;
127 p_header->b_eac3 = false;
132 * It parse E-AC3 sync info
134 static inline int vlc_a52_header_ParseEac3( vlc_a52_header_t *p_header,
135 const uint8_t *p_buf,
136 const vlc_a52_acmod_t *p_acmod )
138 static const unsigned pi_samplerate[3] = { 48000, 44100, 32000 };
139 unsigned i_numblkscod;
143 bs_init( &s, (void*)p_buf, VLC_A52_HEADER_SIZE );
144 bs_skip( &s, 16 + /* start code */
145 2 + /* stream type */
146 3 ); /* substream id */
147 const unsigned i_frame_size = bs_read( &s, 11 );
148 if( i_frame_size < 2 )
150 p_header->i_size = 2 * ( i_frame_size + 1 );
152 const unsigned i_fscod = bs_read( &s, 2 );
153 if( i_fscod == 0x03 )
155 const unsigned i_fscod2 = bs_read( &s, 2 );
156 if( i_fscod2 == 0X03 )
158 p_header->i_rate = pi_samplerate[i_fscod2] / 2;
163 static const int pi_blocks[4] = { 1, 2, 3, 6 };
165 p_header->i_rate = pi_samplerate[i_fscod];
166 i_numblkscod = pi_blocks[bs_read( &s, 2 )];
169 const unsigned i_acmod = bs_read( &s, 3 );
170 const unsigned i_lfeon = bs_read1( &s );
172 p_header->i_channels = p_acmod[i_acmod].i_count + i_lfeon;
173 p_header->i_channels_conf = p_acmod[i_acmod].i_configuration | ( i_lfeon ? AOUT_CHAN_LFE : 0);
174 p_header->i_bitrate = 8 * p_header->i_size * (p_header->i_rate) / (i_numblkscod * 256);
175 p_header->i_samples = i_numblkscod * 256;
177 p_header->b_eac3 = true;
182 * It will parse the header AC3 frame and fill vlc_a52_header_t* if
183 * it is valid or return VLC_EGENERIC.
185 * XXX It will only recognize big endian bitstream ie starting with 0x0b, 0x77
187 static inline int vlc_a52_header_Parse( vlc_a52_header_t *p_header,
188 const uint8_t *p_buffer, int i_buffer )
190 static const vlc_a52_acmod_t p_acmod[8] = {
191 { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DUALMONO }, /* Dual-channel 1+1 */
192 { 1, AOUT_CHAN_CENTER }, /* Mono 1/0 */
193 { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT }, /* Stereo 2/0 */
194 { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER }, /* 3F 3/0 */
195 { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER }, /* 2F1R 2/1 */
196 { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
197 AOUT_CHAN_REARCENTER }, /* 3F1R 3/1 */
198 { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
199 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT }, /* 2F2R 2/2 */
200 { 5, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
201 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT }, /* 3F2R 3/2 */
204 if( i_buffer < VLC_A52_HEADER_SIZE )
208 if( p_buffer[0] != 0x0b || p_buffer[1] != 0x77 )
212 const int bsid = p_buffer[5] >> 3;
218 if( vlc_a52_header_ParseAc3( p_header, p_buffer, p_acmod ) )
223 if( vlc_a52_header_ParseEac3( p_header, p_buffer, p_acmod ) )