1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2016 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>
11 * Thomas Guillem <thomas@gllm.fr>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
34 * Minimum AC3 header size that vlc_a52_header_Parse needs.
36 #define VLC_A52_MIN_HEADER_SIZE (8)
37 #define VLC_A52_EAC3_BSI_SIZE ((532 + 7)/8)
38 #define VLC_A52_EAC3_HEADER_SIZE (VLC_A52_EAC3_BSI_SIZE + 2)
41 * AC3 header information.
43 struct vlc_a52_bitstream_info
55 EAC3_STRMTYP_INDEPENDENT
= 0,
56 EAC3_STRMTYP_DEPENDENT
= 1,
57 EAC3_STRMTYP_AC3_CONVERT
= 2,
58 EAC3_STRMTYP_RESERVED
,
63 uint8_t i_substreamid
;
76 unsigned int i_channels
;
77 unsigned int i_channels_conf
;
78 unsigned int i_chan_mode
;
80 unsigned int i_bitrate
;
83 unsigned int i_samples
;
85 struct vlc_a52_bitstream_info bs
;
87 uint8_t i_blocks_per_sync_frame
;
91 * It parse AC3 sync info.
95 static inline int vlc_a52_ParseAc3BitstreamInfo( struct vlc_a52_bitstream_info
*bs
,
96 const uint8_t *p_buf
, size_t i_buf
)
99 bs_init( &s
, p_buf
, i_buf
);
102 bs
->i_fscod
= bs_read( &s
, 2 );
103 if( bs
->i_fscod
== 3 )
105 bs
->i_frmsizcod
= bs_read( &s
, 6 );
106 if( bs
->i_frmsizcod
>= 38 )
108 bs
->i_bsid
= bs_read( &s
, 5 );
109 bs
->i_bsmod
= bs_read( &s
, 3 );
110 bs
->i_acmod
= bs_read( &s
, 3 );
111 if( ( bs
->i_acmod
& 0x1 ) && ( bs
->i_acmod
!= 0x1 ) )
113 /* if 3 front channels */
114 bs_skip( &s
, 2 ); /* i_cmixlev */
116 if( bs
->i_acmod
& 0x4 )
118 /* if a surround channel exists */
119 bs_skip( &s
, 2 ); /* i_surmixlev */
122 bs
->ac3
.i_dsurmod
= bs
->i_acmod
== 0x2 ? bs_read( &s
, 2 ) : 0;
123 bs
->i_lfeon
= bs_read( &s
, 1 );
128 static inline int vlc_a52_header_ParseAc3( vlc_a52_header_t
*p_header
,
129 const uint8_t *p_buf
,
130 const uint32_t *p_acmod
,
131 const unsigned *pi_fscod_samplerates
)
133 if( vlc_a52_ParseAc3BitstreamInfo( &p_header
->bs
,
134 &p_buf
[4], /* start code + CRC */
135 VLC_A52_MIN_HEADER_SIZE
- 4 ) != VLC_SUCCESS
)
138 /* cf. Table 5.18 Frame Size Code Table */
139 static const uint16_t ppi_frmsizcod_fscod_sizes
[][3] = {
140 /* 32K, 44.1K, 48K */
173 { 1536, 1114, 1024 },
174 { 1536, 1115, 1024 },
175 { 1728, 1253, 1152 },
176 { 1728, 1254, 1152 },
177 { 1920, 1393, 1280 },
180 static const uint16_t pi_frmsizcod_bitrates
[] = {
188 const struct vlc_a52_bitstream_info
*bs
= &p_header
->bs
;
190 p_header
->i_channels_conf
= p_acmod
[bs
->i_acmod
];
191 p_header
->i_chan_mode
= 0;
192 if( bs
->ac3
.i_dsurmod
== 2 )
193 p_header
->i_chan_mode
|= AOUT_CHANMODE_DOLBYSTEREO
;
194 if( bs
->i_acmod
== 0 )
195 p_header
->i_chan_mode
|= AOUT_CHANMODE_DUALMONO
;
198 p_header
->i_channels_conf
|= AOUT_CHAN_LFE
;
200 p_header
->i_channels
= vlc_popcount(p_header
->i_channels_conf
);
202 const unsigned i_rate_shift
= VLC_CLIP(bs
->i_bsid
, 8, 11) - 8;
203 p_header
->i_bitrate
= (pi_frmsizcod_bitrates
[bs
->i_frmsizcod
>> 1] * 1000)
205 p_header
->i_rate
= pi_fscod_samplerates
[bs
->i_fscod
] >> i_rate_shift
;
207 p_header
->i_size
= ppi_frmsizcod_fscod_sizes
[bs
->i_frmsizcod
]
208 [2 - bs
->i_fscod
] * 2;
209 p_header
->i_blocks_per_sync_frame
= 6;
210 p_header
->i_samples
= p_header
->i_blocks_per_sync_frame
* 256;
212 p_header
->b_eac3
= false;
217 * It parse E-AC3 sync info
219 static inline int vlc_a52_ParseEac3BitstreamInfo( struct vlc_a52_bitstream_info
*bs
,
220 const uint8_t *p_buf
, size_t i_buf
)
223 bs_init( &s
, p_buf
, i_buf
);
224 bs
->eac3
.strmtyp
= bs_read( &s
, 2 ); /* Stream Type */
225 bs
->eac3
.i_substreamid
= bs_read( &s
, 3 );/* Substream Identification */
227 bs
->eac3
.i_frmsiz
= bs_read( &s
, 11 );
228 if( bs
->eac3
.i_frmsiz
< 2 )
231 bs
->i_fscod
= bs_read( &s
, 2 );
232 if( bs
->i_fscod
== 0x03 )
234 bs
->eac3
.i_fscod2
= bs_read( &s
, 2 );
235 if( bs
->eac3
.i_fscod2
== 0x03 )
237 bs
->eac3
.i_numblkscod
= 0x03;
239 else bs
->eac3
.i_numblkscod
= bs_read( &s
, 2 );
241 bs
->i_acmod
= bs_read( &s
, 3 );
242 bs
->i_lfeon
= bs_read1( &s
);
247 static inline int vlc_a52_header_ParseEac3( vlc_a52_header_t
*p_header
,
248 const uint8_t *p_buf
,
249 const uint32_t *p_acmod
,
250 const unsigned *pi_fscod_samplerates
)
252 if( vlc_a52_ParseEac3BitstreamInfo( &p_header
->bs
,
253 &p_buf
[2], /* start code */
254 VLC_A52_MIN_HEADER_SIZE
- 2 ) != VLC_SUCCESS
)
257 const struct vlc_a52_bitstream_info
*bs
= &p_header
->bs
;
259 p_header
->i_size
= 2 * (bs
->eac3
.i_frmsiz
+ 1 );
261 if( bs
->i_fscod
== 0x03 )
263 p_header
->i_rate
= pi_fscod_samplerates
[bs
->eac3
.i_fscod2
] / 2;
264 p_header
->i_blocks_per_sync_frame
= 6;
268 static const int pi_numblkscod
[4] = { 1, 2, 3, 6 };
269 p_header
->i_rate
= pi_fscod_samplerates
[bs
->i_fscod
];
270 p_header
->i_blocks_per_sync_frame
= pi_numblkscod
[bs
->eac3
.i_numblkscod
];
273 p_header
->i_channels_conf
= p_acmod
[bs
->i_acmod
];
274 p_header
->i_chan_mode
= 0;
275 if( bs
->i_acmod
== 0 )
276 p_header
->i_chan_mode
|= AOUT_CHANMODE_DUALMONO
;
278 p_header
->i_channels_conf
|= AOUT_CHAN_LFE
;
279 p_header
->i_channels
= vlc_popcount( p_header
->i_channels_conf
);
280 p_header
->i_bitrate
= 8 * p_header
->i_size
* p_header
->i_rate
281 / (p_header
->i_blocks_per_sync_frame
* 256);
282 p_header
->i_samples
= p_header
->i_blocks_per_sync_frame
* 256;
284 p_header
->b_eac3
= true;
289 * It will parse the header AC3 frame and fill vlc_a52_header_t* if
290 * it is valid or return VLC_EGENERIC.
292 * XXX It will only recognize big endian bitstream ie starting with 0x0b, 0x77
294 static inline int vlc_a52_header_Parse( vlc_a52_header_t
*p_header
,
295 const uint8_t *p_buffer
, int i_buffer
)
297 static const uint32_t p_acmod
[8] = {
302 AOUT_CHANS_FRONT
| AOUT_CHAN_REARCENTER
, /* 2F1R */
303 AOUT_CHANS_FRONT
| AOUT_CHANS_CENTER
, /* 3F1R */
307 static const unsigned pi_fscod_samplerates
[] = {
311 if( i_buffer
< VLC_A52_MIN_HEADER_SIZE
)
315 if( p_buffer
[0] != 0x0b || p_buffer
[1] != 0x77 )
319 const int bsid
= p_buffer
[5] >> 3;
321 /* cf. Annex E 2.3.1.6 of AC3 spec */
323 return vlc_a52_header_ParseAc3( p_header
, p_buffer
,
324 p_acmod
, pi_fscod_samplerates
);
325 else if( bsid
<= 16 )
326 return vlc_a52_header_ParseEac3( p_header
, p_buffer
,
327 p_acmod
, pi_fscod_samplerates
);