]> git.sesse.net Git - vlc/blob - modules/codec/a52.h
Fixed acmod table for ac3/eac3.
[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
58 } vlc_a52_header_t;
59
60 /**
61  * It parse AC3 sync info.
62  *
63  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
64  * since we don't want to oblige S/PDIF people to use liba52 just to get
65  * their SyncInfo...
66  */
67 static inline int vlc_a52_header_ParseAc3( vlc_a52_header_t *p_header,
68                                            const uint8_t *p_buf,
69                                            const vlc_a52_acmod_t *p_acmod )
70 {
71     static const uint8_t pi_halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
72     static const unsigned int pi_bitrate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
73                                 128, 160, 192, 224, 256, 320, 384, 448,
74                                 512, 576, 640 };
75     static const uint8_t pi_lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
76                                       0x04, 0x01, 0x04, 0x01 };
77
78     /* */
79     const unsigned i_rate_shift = pi_halfrate[p_buf[5] >> 3];
80
81     /* acmod, dsurmod and lfeon */
82     const unsigned i_acmod = p_buf[6] >> 5;
83     if( (p_buf[6] & 0xf8) == 0x50 )
84     {
85         /* Dolby surround = stereo + Dolby */
86         p_header->i_channels = 2;
87         p_header->i_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
88                             | AOUT_CHAN_DOLBYSTEREO;
89     }
90     else
91     {
92         p_header->i_channels      = p_acmod[i_acmod].i_count;
93         p_header->i_channels_conf = p_acmod[i_acmod].i_configuration;
94     }
95
96     if( p_buf[6] & pi_lfeon[i_acmod] )
97     {
98         p_header->i_channels++;
99         p_header->i_channels_conf |= AOUT_CHAN_LFE;
100     }
101
102     const unsigned i_frmsizecod = p_buf[4] & 63;
103     if( i_frmsizecod >= 38 )
104         return VLC_EGENERIC;
105     const unsigned i_bitrate_base = pi_bitrate[i_frmsizecod >> 1];
106     p_header->i_bitrate = (i_bitrate_base * 1000) >> i_rate_shift;
107
108     switch( p_buf[4] & 0xc0 )
109     {
110     case 0:
111         p_header->i_rate = 48000 >> i_rate_shift;
112         p_header->i_size = 4 * i_bitrate_base;
113         break;
114     case 0x40:
115         p_header->i_rate = 44100 >> i_rate_shift;
116         p_header->i_size = 2 * (320 * i_bitrate_base / 147 + (i_frmsizecod & 1));
117         break;
118     case 0x80:
119         p_header->i_rate = 32000 >> i_rate_shift;
120         p_header->i_size = 6 * i_bitrate_base;
121         break;
122     default:
123         return VLC_EGENERIC;
124     }
125     p_header->b_eac3 = false;
126     return VLC_SUCCESS;
127 }
128
129 /**
130  * It parse E-AC3 sync info
131  */
132 static inline int vlc_a52_header_ParseEac3( vlc_a52_header_t *p_header,
133                                             const uint8_t *p_buf,
134                                             const vlc_a52_acmod_t *p_acmod )
135 {
136     static const unsigned pi_samplerate[3] = { 48000, 44100, 32000 };
137     unsigned i_numblkscod;
138     bs_t s;
139
140
141     bs_init( &s, (void*)p_buf, VLC_A52_HEADER_SIZE );
142     bs_skip( &s, 16 +   /* start code */
143                  2 +    /* stream type */
144                  3 );   /* substream id */
145     const unsigned i_frame_size = bs_read( &s, 11 );
146     if( i_frame_size < 2 )
147         return VLC_EGENERIC;
148     p_header->i_size = 2 * ( i_frame_size + 1 );
149
150     const unsigned i_fscod = bs_read( &s, 2 );
151     if( i_fscod == 0x03 )
152     {
153         const unsigned i_fscod2 = bs_read( &s, 2 );
154         if( i_fscod2 == 0X03 )
155             return VLC_EGENERIC;
156         p_header->i_rate = pi_samplerate[i_fscod2] / 2;
157         i_numblkscod = 6;
158     }
159     else
160     {
161         static const int pi_blocks[4] = { 1, 2, 3, 6 };
162
163         p_header->i_rate = pi_samplerate[i_fscod];
164         i_numblkscod = pi_blocks[bs_read( &s, 2 )];
165     }
166
167     const unsigned i_acmod = bs_read( &s, 3 );
168     const unsigned i_lfeon = bs_read1( &s );
169
170     p_header->i_channels      = p_acmod[i_acmod].i_count + i_lfeon;
171     p_header->i_channels_conf = p_acmod[i_acmod].i_configuration | ( i_lfeon ? AOUT_CHAN_LFE : 0);
172     p_header->i_bitrate = 8 * p_header->i_size * (p_header->i_rate) / (i_numblkscod * 256);
173
174     p_header->b_eac3 = true;
175     return VLC_SUCCESS;
176 }
177
178 /**
179  * It will parse the header AC3 frame and fill vlc_a52_header_t* if
180  * it is valid or return VLC_EGENERIC.
181  *
182  * XXX It will only recognize big endian bitstream ie starting with 0x0b, 0x77
183  */
184 static inline int vlc_a52_header_Parse( vlc_a52_header_t *p_header,
185                                         const uint8_t *p_buffer, int i_buffer )
186 {
187     static const vlc_a52_acmod_t p_acmod[8] = {
188         { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DUALMONO },   /* Dual-channel 1+1 */
189         { 1, AOUT_CHAN_CENTER },                                        /* Mono 1/0 */
190         { 2, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT },                        /* Stereo 2/0 */
191         { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER },     /* 3F 3/0 */
192         { 3, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER }, /* 2F1R 2/1 */
193         { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
194              AOUT_CHAN_REARCENTER },                                    /* 3F1R 3/1 */
195         { 4, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
196              AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT },                /* 2F2R 2/2 */
197         { 5, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
198              AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT },                /* 3F2R 3/2 */
199     };
200
201     if( i_buffer < VLC_A52_HEADER_SIZE )
202         return VLC_EGENERIC;
203
204     /* Check synword */
205     if( p_buffer[0] != 0x0b || p_buffer[1] != 0x77 )
206         return VLC_EGENERIC;
207
208     /* Check bsid */
209     const int bsid = p_buffer[5] >> 3;
210     if( bsid > 16 )
211         return VLC_EGENERIC;
212
213     if( bsid <= 10 )
214     {
215         if( vlc_a52_header_ParseAc3( p_header, p_buffer, p_acmod ) )
216             return VLC_EGENERIC;
217     }
218     else
219     {
220         if( vlc_a52_header_ParseEac3( p_header, p_buffer, p_acmod ) )
221             return VLC_EGENERIC;
222     }
223     return VLC_SUCCESS;
224 }
225
226 #endif
227