]> git.sesse.net Git - vlc/blob - modules/codec/a52.h
upnp: change item b_net and i_type
[vlc] / modules / codec / a52.h
1 /*****************************************************************************
2  * a52.h
3  *****************************************************************************
4  * Copyright (C) 2001-2009 Laurent Aimar
5  * $Id$
6  *
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  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 of the License, or
15  * (at your option) any later version.
16  *
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 Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifndef _VLC_A52_H
28 #define _VLC_A52_H 1
29
30 #include <vlc_bits.h>
31
32 /**
33  * Minimum AC3 header size that vlc_a52_header_Parse needs.
34  */
35 #define VLC_A52_HEADER_SIZE (8)
36
37 /**
38  * AC3 header information.
39  */
40 typedef struct
41 {
42     bool b_eac3;
43
44     unsigned int i_channels;
45     unsigned int i_channels_conf;
46     unsigned int i_rate;
47     unsigned int i_bitrate;
48
49     unsigned int i_size;
50     unsigned int i_samples;
51
52 } vlc_a52_header_t;
53
54 /**
55  * It parse AC3 sync info.
56  *
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
59  * their SyncInfo...
60  */
61 static inline int vlc_a52_header_ParseAc3( vlc_a52_header_t *p_header,
62                                            const uint8_t *p_buf,
63                                            const uint32_t *p_acmod )
64 {
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,
68                                 512, 576, 640 };
69     static const uint8_t pi_lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
70                                       0x04, 0x01, 0x04, 0x01 };
71
72     /* */
73     const unsigned i_rate_shift = pi_halfrate[p_buf[5] >> 3];
74
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;
80     else
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;
84
85     p_header->i_channels = popcount(p_header->i_channels_conf
86                                                          & AOUT_CHAN_PHYSMASK);
87
88     const unsigned i_frmsizecod = p_buf[4] & 63;
89     if( i_frmsizecod >= 38 )
90         return VLC_EGENERIC;
91     const unsigned i_bitrate_base = pi_bitrate[i_frmsizecod >> 1];
92     p_header->i_bitrate = (i_bitrate_base * 1000) >> i_rate_shift;
93
94     switch( p_buf[4] & 0xc0 )
95     {
96     case 0:
97         p_header->i_rate = 48000 >> i_rate_shift;
98         p_header->i_size = 4 * i_bitrate_base;
99         break;
100     case 0x40:
101         p_header->i_rate = 44100 >> i_rate_shift;
102         p_header->i_size = 2 * (320 * i_bitrate_base / 147 + (i_frmsizecod & 1));
103         break;
104     case 0x80:
105         p_header->i_rate = 32000 >> i_rate_shift;
106         p_header->i_size = 6 * i_bitrate_base;
107         break;
108     default:
109         return VLC_EGENERIC;
110     }
111     p_header->i_samples = 6*256;
112
113     p_header->b_eac3 = false;
114     return VLC_SUCCESS;
115 }
116
117 /**
118  * It parse E-AC3 sync info
119  */
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 )
123 {
124     static const unsigned pi_samplerate[3] = { 48000, 44100, 32000 };
125     unsigned i_numblkscod;
126     bs_t s;
127
128
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 )
135         return VLC_EGENERIC;
136     p_header->i_size = 2 * ( i_frame_size + 1 );
137
138     const unsigned i_fscod = bs_read( &s, 2 );
139     if( i_fscod == 0x03 )
140     {
141         const unsigned i_fscod2 = bs_read( &s, 2 );
142         if( i_fscod2 == 0X03 )
143             return VLC_EGENERIC;
144         p_header->i_rate = pi_samplerate[i_fscod2] / 2;
145         i_numblkscod = 6;
146     }
147     else
148     {
149         static const int pi_blocks[4] = { 1, 2, 3, 6 };
150
151         p_header->i_rate = pi_samplerate[i_fscod];
152         i_numblkscod = pi_blocks[bs_read( &s, 2 )];
153     }
154
155     const unsigned i_acmod = bs_read( &s, 3 );
156     const unsigned i_lfeon = bs_read1( &s );
157
158     p_header->i_channels_conf = p_acmod[i_acmod];
159     if( i_lfeon )
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;
166
167     p_header->b_eac3 = true;
168     return VLC_SUCCESS;
169 }
170
171 /**
172  * It will parse the header AC3 frame and fill vlc_a52_header_t* if
173  * it is valid or return VLC_EGENERIC.
174  *
175  * XXX It will only recognize big endian bitstream ie starting with 0x0b, 0x77
176  */
177 static inline int vlc_a52_header_Parse( vlc_a52_header_t *p_header,
178                                         const uint8_t *p_buffer, int i_buffer )
179 {
180     static const uint32_t p_acmod[8] = {
181         AOUT_CHANS_2_0 | AOUT_CHAN_DUALMONO,
182         AOUT_CHAN_CENTER,
183         AOUT_CHANS_2_0,
184         AOUT_CHANS_3_0,
185         AOUT_CHANS_FRONT | AOUT_CHAN_REARCENTER, /* 2F1R */
186         AOUT_CHANS_FRONT | AOUT_CHANS_CENTER,    /* 3F1R */
187         AOUT_CHANS_4_0,
188         AOUT_CHANS_5_0,
189     };
190
191     if( i_buffer < VLC_A52_HEADER_SIZE )
192         return VLC_EGENERIC;
193
194     /* Check synword */
195     if( p_buffer[0] != 0x0b || p_buffer[1] != 0x77 )
196         return VLC_EGENERIC;
197
198     /* Check bsid */
199     const int bsid = p_buffer[5] >> 3;
200     if( bsid > 16 )
201         return VLC_EGENERIC;
202
203     if( bsid <= 10 )
204     {
205         if( vlc_a52_header_ParseAc3( p_header, p_buffer, p_acmod ) )
206             return VLC_EGENERIC;
207     }
208     else
209     {
210         if( vlc_a52_header_ParseEac3( p_header, p_buffer, p_acmod ) )
211             return VLC_EGENERIC;
212     }
213     return VLC_SUCCESS;
214 }
215
216 #endif