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 *****************************************************************************/
36 unsigned int i_configuration;
40 * Minimum AC3 header size that vlc_a52_header_Parse needs.
42 #define VLC_A52_HEADER_SIZE (8)
45 * AC3 header information.
51 unsigned int i_channels;
52 unsigned int i_channels_conf;
54 unsigned int i_bitrate;
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->b_eac3 = false;
130 * It parse E-AC3 sync info
132 static inline int vlc_a52_header_ParseEac3( vlc_a52_header_t *p_header,
133 const uint8_t *p_buf,
134 const vlc_a52_acmod_t *p_acmod )
136 static const unsigned pi_samplerate[3] = { 48000, 44100, 32000 };
137 unsigned i_numblkscod;
141 bs_init( &s, (void*)p_buf, VLC_A52_HEADER_SIZE );
142 bs_skip( &s, 16 + /* start code */
143 2 + /* stream type */
144 3 ); /* substream id */
145 const unsigned i_frame_size = bs_read( &s, 11 );
146 if( i_frame_size < 2 )
148 p_header->i_size = 2 * ( i_frame_size + 1 );
150 const unsigned i_fscod = bs_read( &s, 2 );
151 if( i_fscod == 0x03 )
153 const unsigned i_fscod2 = bs_read( &s, 2 );
154 if( i_fscod2 == 0X03 )
156 p_header->i_rate = pi_samplerate[i_fscod2] / 2;
161 static const int pi_blocks[4] = { 1, 2, 3, 6 };
163 p_header->i_rate = pi_samplerate[i_fscod];
164 i_numblkscod = pi_blocks[bs_read( &s, 2 )];
167 const unsigned i_acmod = bs_read( &s, 3 );
168 const unsigned i_lfeon = bs_read1( &s );
170 p_header->i_channels = p_acmod[i_acmod].i_count + i_lfeon;
171 p_header->i_channels_conf = p_acmod[i_acmod].i_configuration | ( i_lfeon ? AOUT_CHAN_LFE : 0);
172 p_header->i_bitrate = 8 * p_header->i_size * (p_header->i_rate) / (i_numblkscod * 256);
174 p_header->b_eac3 = true;
179 * It will parse the header AC3 frame and fill vlc_a52_header_t* if
180 * it is valid or return VLC_EGENERIC.
182 * XXX It will only recognize big endian bitstream ie starting with 0x0b, 0x77
184 static inline int vlc_a52_header_Parse( vlc_a52_header_t *p_header,
185 const uint8_t *p_buffer, int i_buffer )
187 static const vlc_a52_acmod_t p_acmod[8] = {
188 { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DUALMONO }, /* Dual-channel 1+1 */
189 { 1, AOUT_CHAN_CENTER }, /* Mono 1/0 */
190 { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT }, /* Stereo 2/0 */
191 { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER }, /* 3F 3/0 */
192 { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER }, /* 2F1R 2/1 */
193 { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
194 AOUT_CHAN_REARCENTER }, /* 3F1R 3/1 */
195 { 5, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
196 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT }, /* 2F2R 2/2 */
197 { 6, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
198 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT }, /* 3F2R 3/2 */
201 if( i_buffer < VLC_A52_HEADER_SIZE )
205 if( p_buffer[0] != 0x0b || p_buffer[1] != 0x77 )
209 const int bsid = p_buffer[5] >> 3;
215 if( vlc_a52_header_ParseAc3( p_header, p_buffer, p_acmod ) )
220 if( vlc_a52_header_ParseEac3( p_header, p_buffer, p_acmod ) )