]> git.sesse.net Git - vlc/blob - modules/codec/a52.h
Use var_InheritString for --decklink-video-connection.
[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
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.
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 General Public License for more details.
21  *
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  *****************************************************************************/
26
27 #ifndef _VLC_A52_H
28 #define _VLC_A52_H 1
29
30 #include <vlc_aout.h>
31 #include <vlc_bits.h>
32
33 typedef struct
34 {
35     unsigned int i_count;
36     unsigned int i_configuration;
37 } vlc_a52_acmod_t;
38
39 /**
40  * Minimum AC3 header size that vlc_a52_header_Parse needs.
41  */
42 #define VLC_A52_HEADER_SIZE (8)
43
44 /**
45  * AC3 header information.
46  */
47 typedef struct
48 {
49     bool b_eac3;
50
51     unsigned int i_channels;
52     unsigned int i_channels_conf;
53     unsigned int i_rate;
54     unsigned int i_bitrate;
55
56     unsigned int i_size;
57     unsigned int i_samples;
58
59 } vlc_a52_header_t;
60
61 /**
62  * It parse AC3 sync info.
63  *
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
66  * their SyncInfo...
67  */
68 static inline int vlc_a52_header_ParseAc3( vlc_a52_header_t *p_header,
69                                            const uint8_t *p_buf,
70                                            const vlc_a52_acmod_t *p_acmod )
71 {
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,
75                                 512, 576, 640 };
76     static const uint8_t pi_lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
77                                       0x04, 0x01, 0x04, 0x01 };
78
79     /* */
80     const unsigned i_rate_shift = pi_halfrate[p_buf[5] >> 3];
81
82     /* acmod, dsurmod and lfeon */
83     const unsigned i_acmod = p_buf[6] >> 5;
84     if( (p_buf[6] & 0xf8) == 0x50 )
85     {
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;
90     }
91     else
92     {
93         p_header->i_channels      = p_acmod[i_acmod].i_count;
94         p_header->i_channels_conf = p_acmod[i_acmod].i_configuration;
95     }
96
97     if( p_buf[6] & pi_lfeon[i_acmod] )
98     {
99         p_header->i_channels++;
100         p_header->i_channels_conf |= AOUT_CHAN_LFE;
101     }
102
103     const unsigned i_frmsizecod = p_buf[4] & 63;
104     if( i_frmsizecod >= 38 )
105         return VLC_EGENERIC;
106     const unsigned i_bitrate_base = pi_bitrate[i_frmsizecod >> 1];
107     p_header->i_bitrate = (i_bitrate_base * 1000) >> i_rate_shift;
108
109     switch( p_buf[4] & 0xc0 )
110     {
111     case 0:
112         p_header->i_rate = 48000 >> i_rate_shift;
113         p_header->i_size = 4 * i_bitrate_base;
114         break;
115     case 0x40:
116         p_header->i_rate = 44100 >> i_rate_shift;
117         p_header->i_size = 2 * (320 * i_bitrate_base / 147 + (i_frmsizecod & 1));
118         break;
119     case 0x80:
120         p_header->i_rate = 32000 >> i_rate_shift;
121         p_header->i_size = 6 * i_bitrate_base;
122         break;
123     default:
124         return VLC_EGENERIC;
125     }
126     p_header->i_samples = 6*256;
127
128     p_header->b_eac3 = false;
129     return VLC_SUCCESS;
130 }
131
132 /**
133  * It parse E-AC3 sync info
134  */
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 )
138 {
139     static const unsigned pi_samplerate[3] = { 48000, 44100, 32000 };
140     unsigned i_numblkscod;
141     bs_t s;
142
143
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 )
150         return VLC_EGENERIC;
151     p_header->i_size = 2 * ( i_frame_size + 1 );
152
153     const unsigned i_fscod = bs_read( &s, 2 );
154     if( i_fscod == 0x03 )
155     {
156         const unsigned i_fscod2 = bs_read( &s, 2 );
157         if( i_fscod2 == 0X03 )
158             return VLC_EGENERIC;
159         p_header->i_rate = pi_samplerate[i_fscod2] / 2;
160         i_numblkscod = 6;
161     }
162     else
163     {
164         static const int pi_blocks[4] = { 1, 2, 3, 6 };
165
166         p_header->i_rate = pi_samplerate[i_fscod];
167         i_numblkscod = pi_blocks[bs_read( &s, 2 )];
168     }
169
170     const unsigned i_acmod = bs_read( &s, 3 );
171     const unsigned i_lfeon = bs_read1( &s );
172
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;
177
178     p_header->b_eac3 = true;
179     return VLC_SUCCESS;
180 }
181
182 /**
183  * It will parse the header AC3 frame and fill vlc_a52_header_t* if
184  * it is valid or return VLC_EGENERIC.
185  *
186  * XXX It will only recognize big endian bitstream ie starting with 0x0b, 0x77
187  */
188 static inline int vlc_a52_header_Parse( vlc_a52_header_t *p_header,
189                                         const uint8_t *p_buffer, int i_buffer )
190 {
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 */
203     };
204
205     if( i_buffer < VLC_A52_HEADER_SIZE )
206         return VLC_EGENERIC;
207
208     /* Check synword */
209     if( p_buffer[0] != 0x0b || p_buffer[1] != 0x77 )
210         return VLC_EGENERIC;
211
212     /* Check bsid */
213     const int bsid = p_buffer[5] >> 3;
214     if( bsid > 16 )
215         return VLC_EGENERIC;
216
217     if( bsid <= 10 )
218     {
219         if( vlc_a52_header_ParseAc3( p_header, p_buffer, p_acmod ) )
220             return VLC_EGENERIC;
221     }
222     else
223     {
224         if( vlc_a52_header_ParseEac3( p_header, p_buffer, p_acmod ) )
225             return VLC_EGENERIC;
226     }
227     return VLC_SUCCESS;
228 }
229
230 #endif
231