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
;
57 unsigned int i_samples
;
62 * It parse AC3 sync info.
64 * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
65 * since we don't want to oblige S/PDIF people to use liba52 just to get
68 static inline int vlc_a52_header_ParseAc3( vlc_a52_header_t
*p_header
,
70 const vlc_a52_acmod_t
*p_acmod
)
72 static const uint8_t pi_halfrate
[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
73 static const unsigned int pi_bitrate
[] = { 32, 40, 48, 56, 64, 80, 96, 112,
74 128, 160, 192, 224, 256, 320, 384, 448,
76 static const uint8_t pi_lfeon
[8] = { 0x10, 0x10, 0x04, 0x04,
77 0x04, 0x01, 0x04, 0x01 };
80 const unsigned i_rate_shift
= pi_halfrate
[p_buf
[5] >> 3];
82 /* acmod, dsurmod and lfeon */
83 const unsigned i_acmod
= p_buf
[6] >> 5;
84 if( (p_buf
[6] & 0xf8) == 0x50 )
86 /* Dolby surround = stereo + Dolby */
87 p_header
->i_channels
= 2;
88 p_header
->i_channels_conf
= AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
89 | AOUT_CHAN_DOLBYSTEREO
;
93 p_header
->i_channels
= p_acmod
[i_acmod
].i_count
;
94 p_header
->i_channels_conf
= p_acmod
[i_acmod
].i_configuration
;
97 if( p_buf
[6] & pi_lfeon
[i_acmod
] )
99 p_header
->i_channels
++;
100 p_header
->i_channels_conf
|= AOUT_CHAN_LFE
;
103 const unsigned i_frmsizecod
= p_buf
[4] & 63;
104 if( i_frmsizecod
>= 38 )
106 const unsigned i_bitrate_base
= pi_bitrate
[i_frmsizecod
>> 1];
107 p_header
->i_bitrate
= (i_bitrate_base
* 1000) >> i_rate_shift
;
109 switch( p_buf
[4] & 0xc0 )
112 p_header
->i_rate
= 48000 >> i_rate_shift
;
113 p_header
->i_size
= 4 * i_bitrate_base
;
116 p_header
->i_rate
= 44100 >> i_rate_shift
;
117 p_header
->i_size
= 2 * (320 * i_bitrate_base
/ 147 + (i_frmsizecod
& 1));
120 p_header
->i_rate
= 32000 >> i_rate_shift
;
121 p_header
->i_size
= 6 * i_bitrate_base
;
126 p_header
->i_samples
= 6*256;
128 p_header
->b_eac3
= false;
133 * It parse E-AC3 sync info
135 static inline int vlc_a52_header_ParseEac3( vlc_a52_header_t
*p_header
,
136 const uint8_t *p_buf
,
137 const vlc_a52_acmod_t
*p_acmod
)
139 static const unsigned pi_samplerate
[3] = { 48000, 44100, 32000 };
140 unsigned i_numblkscod
;
144 bs_init( &s
, (void*)p_buf
, VLC_A52_HEADER_SIZE
);
145 bs_skip( &s
, 16 + /* start code */
146 2 + /* stream type */
147 3 ); /* substream id */
148 const unsigned i_frame_size
= bs_read( &s
, 11 );
149 if( i_frame_size
< 2 )
151 p_header
->i_size
= 2 * ( i_frame_size
+ 1 );
153 const unsigned i_fscod
= bs_read( &s
, 2 );
154 if( i_fscod
== 0x03 )
156 const unsigned i_fscod2
= bs_read( &s
, 2 );
157 if( i_fscod2
== 0X03 )
159 p_header
->i_rate
= pi_samplerate
[i_fscod2
] / 2;
164 static const int pi_blocks
[4] = { 1, 2, 3, 6 };
166 p_header
->i_rate
= pi_samplerate
[i_fscod
];
167 i_numblkscod
= pi_blocks
[bs_read( &s
, 2 )];
170 const unsigned i_acmod
= bs_read( &s
, 3 );
171 const unsigned i_lfeon
= bs_read1( &s
);
173 p_header
->i_channels
= p_acmod
[i_acmod
].i_count
+ i_lfeon
;
174 p_header
->i_channels_conf
= p_acmod
[i_acmod
].i_configuration
| ( i_lfeon
? AOUT_CHAN_LFE
: 0);
175 p_header
->i_bitrate
= 8 * p_header
->i_size
* (p_header
->i_rate
) / (i_numblkscod
* 256);
176 p_header
->i_samples
= i_numblkscod
* 256;
178 p_header
->b_eac3
= true;
183 * It will parse the header AC3 frame and fill vlc_a52_header_t* if
184 * it is valid or return VLC_EGENERIC.
186 * XXX It will only recognize big endian bitstream ie starting with 0x0b, 0x77
188 static inline int vlc_a52_header_Parse( vlc_a52_header_t
*p_header
,
189 const uint8_t *p_buffer
, int i_buffer
)
191 static const vlc_a52_acmod_t p_acmod
[8] = {
192 { 2, AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_DUALMONO
}, /* Dual-channel 1+1 */
193 { 1, AOUT_CHAN_CENTER
}, /* Mono 1/0 */
194 { 2, AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
}, /* Stereo 2/0 */
195 { 3, AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
}, /* 3F 3/0 */
196 { 3, AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_REARCENTER
}, /* 2F1R 2/1 */
197 { 4, AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
|
198 AOUT_CHAN_REARCENTER
}, /* 3F1R 3/1 */
199 { 4, AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
|
200 AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
}, /* 2F2R 2/2 */
201 { 5, AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
|
202 AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
}, /* 3F2R 3/2 */
205 if( i_buffer
< VLC_A52_HEADER_SIZE
)
209 if( p_buffer
[0] != 0x0b || p_buffer
[1] != 0x77 )
213 const int bsid
= p_buffer
[5] >> 3;
219 if( vlc_a52_header_ParseAc3( p_header
, p_buffer
, p_acmod
) )
224 if( vlc_a52_header_ParseEac3( p_header
, p_buffer
, p_acmod
) )