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 *****************************************************************************/
33 * Minimum AC3 header size that vlc_a52_header_Parse needs.
35 #define VLC_A52_HEADER_SIZE (8)
38 * AC3 header information.
44 unsigned int i_channels;
45 unsigned int i_channels_conf;
47 unsigned int i_bitrate;
50 unsigned int i_samples;
55 * It parse AC3 sync info.
57 * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
58 * since we don't want to oblige S/PDIF people to use liba52 just to get
61 static inline int vlc_a52_header_ParseAc3( vlc_a52_header_t *p_header,
63 const uint32_t *p_acmod )
65 static const uint8_t pi_halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
66 static const unsigned int pi_bitrate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
67 128, 160, 192, 224, 256, 320, 384, 448,
69 static const uint8_t pi_lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
70 0x04, 0x01, 0x04, 0x01 };
73 const unsigned i_rate_shift = pi_halfrate[p_buf[5] >> 3];
75 /* acmod, dsurmod and lfeon */
76 const unsigned i_acmod = p_buf[6] >> 5;
77 if( (p_buf[6] & 0xf8) == 0x50 )
78 /* Dolby surround = stereo + Dolby */
79 p_header->i_channels_conf = AOUT_CHANS_STEREO | AOUT_CHAN_DOLBYSTEREO;
81 p_header->i_channels_conf = p_acmod[i_acmod];
82 if( p_buf[6] & pi_lfeon[i_acmod] )
83 p_header->i_channels_conf |= AOUT_CHAN_LFE;
85 p_header->i_channels = popcount(p_header->i_channels_conf
86 & AOUT_CHAN_PHYSMASK);
88 const unsigned i_frmsizecod = p_buf[4] & 63;
89 if( i_frmsizecod >= 38 )
91 const unsigned i_bitrate_base = pi_bitrate[i_frmsizecod >> 1];
92 p_header->i_bitrate = (i_bitrate_base * 1000) >> i_rate_shift;
94 switch( p_buf[4] & 0xc0 )
97 p_header->i_rate = 48000 >> i_rate_shift;
98 p_header->i_size = 4 * i_bitrate_base;
101 p_header->i_rate = 44100 >> i_rate_shift;
102 p_header->i_size = 2 * (320 * i_bitrate_base / 147 + (i_frmsizecod & 1));
105 p_header->i_rate = 32000 >> i_rate_shift;
106 p_header->i_size = 6 * i_bitrate_base;
111 p_header->i_samples = 6*256;
113 p_header->b_eac3 = false;
118 * It parse E-AC3 sync info
120 static inline int vlc_a52_header_ParseEac3( vlc_a52_header_t *p_header,
121 const uint8_t *p_buf,
122 const uint32_t *p_acmod )
124 static const unsigned pi_samplerate[3] = { 48000, 44100, 32000 };
125 unsigned i_numblkscod;
129 bs_init( &s, (void*)p_buf, VLC_A52_HEADER_SIZE );
130 bs_skip( &s, 16 + /* start code */
131 2 + /* stream type */
132 3 ); /* substream id */
133 const unsigned i_frame_size = bs_read( &s, 11 );
134 if( i_frame_size < 2 )
136 p_header->i_size = 2 * ( i_frame_size + 1 );
138 const unsigned i_fscod = bs_read( &s, 2 );
139 if( i_fscod == 0x03 )
141 const unsigned i_fscod2 = bs_read( &s, 2 );
142 if( i_fscod2 == 0X03 )
144 p_header->i_rate = pi_samplerate[i_fscod2] / 2;
149 static const int pi_blocks[4] = { 1, 2, 3, 6 };
151 p_header->i_rate = pi_samplerate[i_fscod];
152 i_numblkscod = pi_blocks[bs_read( &s, 2 )];
155 const unsigned i_acmod = bs_read( &s, 3 );
156 const unsigned i_lfeon = bs_read1( &s );
158 p_header->i_channels_conf = p_acmod[i_acmod];
160 p_header->i_channels_conf |= AOUT_CHAN_LFE;
161 p_header->i_channels = popcount(p_header->i_channels_conf
162 & AOUT_CHAN_PHYSMASK);
163 p_header->i_bitrate = 8 * p_header->i_size * (p_header->i_rate)
164 / (i_numblkscod * 256);
165 p_header->i_samples = i_numblkscod * 256;
167 p_header->b_eac3 = true;
172 * It will parse the header AC3 frame and fill vlc_a52_header_t* if
173 * it is valid or return VLC_EGENERIC.
175 * XXX It will only recognize big endian bitstream ie starting with 0x0b, 0x77
177 static inline int vlc_a52_header_Parse( vlc_a52_header_t *p_header,
178 const uint8_t *p_buffer, int i_buffer )
180 static const uint32_t p_acmod[8] = {
181 AOUT_CHANS_2_0 | AOUT_CHAN_DUALMONO,
185 AOUT_CHANS_FRONT | AOUT_CHAN_REARCENTER, /* 2F1R */
186 AOUT_CHANS_FRONT | AOUT_CHANS_CENTER, /* 3F1R */
191 if( i_buffer < VLC_A52_HEADER_SIZE )
195 if( p_buffer[0] != 0x0b || p_buffer[1] != 0x77 )
199 const int bsid = p_buffer[5] >> 3;
205 if( vlc_a52_header_ParseAc3( p_header, p_buffer, p_acmod ) )
210 if( vlc_a52_header_ParseEac3( p_header, p_buffer, p_acmod ) )