packetizer: a52: add EAC3 header defines
[vlc.git] / modules / packetizer / a52.h
blobe29b56b1e2d91afd61247950252023f69b57b3b1
1 /*****************************************************************************
2 * a52.h
3 *****************************************************************************
4 * Copyright (C) 2001-2016 Laurent Aimar
5 * $Id$
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 *****************************************************************************/
28 #ifndef VLC_A52_H_
29 #define VLC_A52_H_
31 #include <vlc_bits.h>
33 /**
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)
40 /**
41 * AC3 header information.
43 struct vlc_a52_bitstream_info
45 uint8_t i_fscod;
46 uint8_t i_frmsizcod;
47 uint8_t i_bsid;
48 uint8_t i_bsmod;
49 uint8_t i_acmod;
50 uint8_t i_lfeon;
51 union
53 struct {
54 enum {
55 EAC3_STRMTYP_INDEPENDENT = 0,
56 EAC3_STRMTYP_DEPENDENT = 1,
57 EAC3_STRMTYP_AC3_CONVERT = 2,
58 EAC3_STRMTYP_RESERVED,
59 } strmtyp;
60 uint16_t i_frmsiz;
61 uint8_t i_fscod2;
62 uint8_t i_numblkscod;
63 uint8_t i_substreamid;
64 } eac3;
65 struct
67 uint8_t i_dsurmod;
68 } ac3;
72 typedef struct
74 bool b_eac3;
76 unsigned int i_channels;
77 unsigned int i_channels_conf;
78 unsigned int i_chan_mode;
79 unsigned int i_rate;
80 unsigned int i_bitrate;
82 unsigned int i_size;
83 unsigned int i_samples;
85 struct vlc_a52_bitstream_info bs;
87 uint8_t i_blocks_per_sync_frame;
88 } vlc_a52_header_t;
90 /**
91 * It parse AC3 sync info.
93 * cf. AC3 spec
95 static inline int vlc_a52_ParseAc3BitstreamInfo( struct vlc_a52_bitstream_info *bs,
96 const uint8_t *p_buf, size_t i_buf )
98 bs_t s;
99 bs_init( &s, p_buf, i_buf );
101 /* cf. 5.3.2 */
102 bs->i_fscod = bs_read( &s, 2 );
103 if( bs->i_fscod == 3 )
104 return VLC_EGENERIC;
105 bs->i_frmsizcod = bs_read( &s, 6 );
106 if( bs->i_frmsizcod >= 38 )
107 return VLC_EGENERIC;
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 */
121 /* if in 2/0 mode */
122 bs->ac3.i_dsurmod = bs->i_acmod == 0x2 ? bs_read( &s, 2 ) : 0;
123 bs->i_lfeon = bs_read( &s, 1 );
125 return VLC_SUCCESS;
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 )
136 return VLC_EGENERIC;
138 /* cf. Table 5.18 Frame Size Code Table */
139 static const uint16_t ppi_frmsizcod_fscod_sizes[][3] = {
140 /* 32K, 44.1K, 48K */
141 { 96, 69, 64 },
142 { 96, 70, 64 },
143 { 120, 87, 80 },
144 { 120, 88, 80 },
145 { 144, 104, 96 },
146 { 144, 105, 96 },
147 { 168, 121, 112 },
148 { 168, 122, 112 },
149 { 192, 139, 128 },
150 { 192, 140, 128 },
151 { 240, 174, 160 },
152 { 240, 175, 160 },
153 { 288, 208, 192 },
154 { 288, 209, 192 },
155 { 336, 243, 224 },
156 { 336, 244, 224 },
157 { 384, 278, 256 },
158 { 384, 279, 256 },
159 { 480, 348, 320 },
160 { 480, 349, 320 },
161 { 576, 417, 384 },
162 { 576, 418, 384 },
163 { 672, 487, 448 },
164 { 672, 488, 448 },
165 { 768, 557, 512 },
166 { 768, 558, 512 },
167 { 960, 696, 640 },
168 { 960, 697, 640 },
169 { 1152, 835, 768 },
170 { 1152, 836, 768 },
171 { 1344, 975, 896 },
172 { 1344, 976, 896 },
173 { 1536, 1114, 1024 },
174 { 1536, 1115, 1024 },
175 { 1728, 1253, 1152 },
176 { 1728, 1254, 1152 },
177 { 1920, 1393, 1280 },
178 { 1920, 1394, 1280 }
180 static const uint16_t pi_frmsizcod_bitrates[] = {
181 32, 40, 48, 56,
182 64, 80, 96, 112,
183 128, 160, 192, 224,
184 256, 320, 384, 448,
185 512, 576, 640
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;
197 if( bs->i_lfeon )
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)
204 >> i_rate_shift;
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;
213 return VLC_SUCCESS;
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 )
222 bs_t s;
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 )
229 return VLC_EGENERIC;
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 )
236 return VLC_EGENERIC;
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 );
244 return VLC_SUCCESS;
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 )
255 return VLC_EGENERIC;
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;
266 else
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;
277 if( bs->i_lfeon )
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;
285 return VLC_SUCCESS;
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] = {
298 AOUT_CHANS_2_0,
299 AOUT_CHAN_CENTER,
300 AOUT_CHANS_2_0,
301 AOUT_CHANS_3_0,
302 AOUT_CHANS_FRONT | AOUT_CHAN_REARCENTER, /* 2F1R */
303 AOUT_CHANS_FRONT | AOUT_CHANS_CENTER, /* 3F1R */
304 AOUT_CHANS_4_0,
305 AOUT_CHANS_5_0,
307 static const unsigned pi_fscod_samplerates[] = {
308 48000, 44100, 32000
311 if( i_buffer < VLC_A52_MIN_HEADER_SIZE )
312 return VLC_EGENERIC;
314 /* Check synword */
315 if( p_buffer[0] != 0x0b || p_buffer[1] != 0x77 )
316 return VLC_EGENERIC;
318 /* Check bsid */
319 const int bsid = p_buffer[5] >> 3;
321 /* cf. Annex E 2.3.1.6 of AC3 spec */
322 if( bsid <= 10 )
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 );
328 else
329 return VLC_EGENERIC;
332 #endif