]> git.sesse.net Git - vlc/blob - modules/demux/a52sys.c
* Stringreview !!!
[vlc] / modules / demux / a52sys.c
1 /*****************************************************************************
2  * a52.c : Raw A/52 Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id: a52sys.c,v 1.12 2004/01/25 20:05:28 hartman Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 static int  Open    ( vlc_object_t * );
36 static void Close  ( vlc_object_t * );
37
38 vlc_module_begin();
39     set_description( _("A/52 demuxer" ) );
40     set_capability( "demux", 100 );
41     set_callbacks( Open, Close );
42     add_shortcut( "a52" );
43 vlc_module_end();
44
45 /* TODO:
46  *
47  */
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int  Demux       ( input_thread_t * );
53
54 struct demux_sys_t
55 {
56     mtime_t         i_time;
57
58     es_out_id_t     *p_es;
59 };
60
61 static inline int HeaderCheck( const uint8_t * p )
62 {
63     if( (p[0] != 0x0b) || (p[1] != 0x77) || /* syncword */
64         (p[5] >= 0x60) || /* bsid >= 12 */
65         (p[4] & 63) >= 38  || /* frmsizecod */
66         ( (p[4] & 0xc0) != 0 && (p[4] & 0xc0) != 0x40 && (p[4] & 0xc0) != 0x80 ) )
67     {
68         return VLC_FALSE;
69     }
70     return VLC_TRUE;
71 }
72
73 static int HeaderInfo( const uint8_t * p,
74                        int *pi_channels,
75                        int *pi_sample_rate,
76                        int *pi_frame_size );
77
78 /*****************************************************************************
79  * Open: initializes A52 demux structures
80  *****************************************************************************/
81 static int Open( vlc_object_t * p_this )
82 {
83     input_thread_t *p_input = (input_thread_t *)p_this;
84     demux_sys_t    *p_sys;
85     int            b_forced = VLC_FALSE;
86
87     uint8_t        *p_peek;
88
89     module_t       *p_id3;
90
91     es_format_t    fmt;
92
93
94     if( p_input->psz_demux && !strncmp( p_input->psz_demux, "a52", 3 ) )
95     {
96         b_forced = VLC_TRUE;
97     }
98     if( p_input->psz_name )
99     {
100         int  i_len = strlen( p_input->psz_name );
101
102         if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".a52" ) )
103         {
104             b_forced = VLC_TRUE;
105         }
106     }
107
108     /* skip possible id3 header */
109     p_id3 = module_Need( p_input, "id3", NULL );
110     if ( p_id3 )
111     {
112         module_Unneed( p_input, p_id3 );
113     }
114
115     /* see if it could be 52 */
116     if( !b_forced )
117     {
118         if( input_Peek( p_input, &p_peek, 6 ) < 6 )
119         {
120             msg_Err( p_input, "cannot peek" );
121             return VLC_EGENERIC;
122         }
123         if( !HeaderCheck( p_peek ) )
124         {
125             msg_Warn( p_input, "A52 module discarded" );
126             return VLC_EGENERIC;
127         }
128     }
129
130     p_input->pf_demux = Demux;
131     p_input->pf_demux_control = demux_vaControlDefault;
132
133     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
134     p_sys->i_time = 0;
135
136     if( stream_Peek( p_input->s, &p_peek, 6 ) < 6 )
137     {
138         msg_Err( p_input, "cannot peek" );
139         goto error;
140     }
141
142     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
143     if( HeaderCheck( p_peek ) )
144     {
145         int i_frame_size;
146
147         HeaderInfo( p_peek, &fmt.audio.i_channels, &fmt.audio.i_rate, &i_frame_size );
148
149         msg_Dbg( p_input, "a52 channels=%d sample_rate=%d",
150                  fmt.audio.i_channels, fmt.audio.i_rate );
151     }
152
153     vlc_mutex_lock( &p_input->stream.stream_lock );
154     if( input_InitStream( p_input, 0 ) == -1)
155     {
156         vlc_mutex_unlock( &p_input->stream.stream_lock );
157         msg_Err( p_input, "cannot init stream" );
158         goto error;
159     }
160     p_input->stream.i_mux_rate = 0 / 50;
161     vlc_mutex_unlock( &p_input->stream.stream_lock );
162
163     p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
164     return VLC_SUCCESS;
165
166 error:
167     free( p_sys );
168     return VLC_EGENERIC;
169 }
170
171
172 /*****************************************************************************
173  * Demux: reads and demuxes data packets
174  *****************************************************************************
175  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
176  *****************************************************************************/
177 static int Demux( input_thread_t * p_input )
178 {
179     demux_sys_t  *p_sys = p_input->p_demux_data;
180     block_t *p_block;
181
182     int i_channels, i_sample_rate, i_frame_size;
183
184     uint8_t      *p_peek;
185
186     if( stream_Peek( p_input->s, &p_peek, 6 ) < 6 )
187     {
188         msg_Warn( p_input, "cannot peek" );
189         return 0;
190     }
191
192     if( !HeaderCheck( p_peek ) )
193     {
194         /* we need to resynch */
195         vlc_bool_t  b_ok = VLC_FALSE;
196         int         i_skip = 0;
197         int         i_peek;
198
199         i_peek = stream_Peek( p_input->s, &p_peek, 8096 );
200         if( i_peek < 8 )
201         {
202             msg_Warn( p_input, "cannot peek" );
203             return 0;
204         }
205
206         while( i_peek >= 8 )
207         {
208             if( HeaderCheck( p_peek ) )
209             {
210                 b_ok = VLC_TRUE;
211                 break;
212             }
213
214             p_peek++;
215             i_peek--;
216             i_skip++;
217         }
218
219         msg_Warn( p_input, "garbage=%d bytes", i_skip );
220         stream_Read( p_input->s, NULL, i_skip );
221         return 1;
222     }
223
224     HeaderInfo( p_peek, &i_channels, &i_sample_rate, &i_frame_size );
225
226     input_ClockManageRef( p_input,
227                           p_input->stream.p_selected_program,
228                           p_sys->i_time * 9 / 100 );
229
230     if( ( p_block = stream_Block( p_input->s, i_frame_size ) ) == NULL )
231     {
232         msg_Warn( p_input, "cannot read data" );
233         return 0;
234     }
235
236     p_block->i_dts =
237     p_block->i_pts = input_ClockGetTS( p_input,
238                                      p_input->stream.p_selected_program,
239                                      p_sys->i_time * 9 / 100 );
240
241     es_out_Send( p_input->p_es_out, p_sys->p_es, p_block );
242
243     p_sys->i_time += (mtime_t)1000000 *
244                      (mtime_t)1536 /
245                      (mtime_t)i_sample_rate;
246     return( 1 );
247 }
248
249 /*****************************************************************************
250  * Close: frees unused data
251  *****************************************************************************/
252 static void Close( vlc_object_t * p_this )
253 {
254     input_thread_t *p_input = (input_thread_t*)p_this;
255     demux_sys_t    *p_sys = p_input->p_demux_data;
256
257     free( p_sys );
258 }
259
260
261
262 /*****************************************************************************
263  * SyncInfo: parse A/52 sync info
264  *****************************************************************************
265  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
266  *****************************************************************************/
267 static int HeaderInfo( const uint8_t * p,
268                         int *pi_channels,
269                         int *pi_sample_rate,
270                         int *pi_frame_size )
271 {
272     static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
273     static const int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
274                                 128, 160, 192, 224, 256, 320, 384, 448,
275                                 512, 576, 640 };
276     static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
277                                       0x04, 0x01, 0x04, 0x01 };
278     int frmsizecod;
279     int bitrate;
280     int half;
281     int acmod;
282
283     if ((p[0] != 0x0b) || (p[1] != 0x77))        /* syncword */
284         return VLC_FALSE;
285
286     if (p[5] >= 0x60)                /* bsid >= 12 */
287         return VLC_FALSE;
288     half = halfrate[p[5] >> 3];
289
290     /* acmod, dsurmod and lfeon */
291     acmod = p[6] >> 5;
292     if ( (p[6] & 0xf8) == 0x50 )
293     {
294         /* Dolby surround = stereo + Dolby */
295         *pi_channels = 2;
296     }
297     else
298     {
299         static const int acmod_to_channels[8] =
300         {
301             2 /* dual mono */, 1 /* mono */, 2 /* stereo */,
302             3 /* 3F */, 3 /* 2f1R */,
303             4 /* 3F1R */, 4, /* 2F2R */
304             5 /* 3F2R */
305         };
306
307         *pi_channels = acmod_to_channels[acmod];
308     }
309
310     if ( p[6] & lfeon[acmod] ) (*pi_channels)++;    /* LFE */
311
312     frmsizecod = p[4] & 63;
313     if (frmsizecod >= 38)
314         return VLC_FALSE;
315     bitrate = rate[frmsizecod >> 1];
316
317     switch (p[4] & 0xc0) {
318     case 0:
319         *pi_sample_rate = 48000 >> half;
320         *pi_frame_size = 4 * bitrate;
321         return VLC_TRUE;
322     case 0x40:
323         *pi_sample_rate = 44100 >> half;
324         *pi_frame_size = 2 * (320 * bitrate / 147 + (frmsizecod & 1));
325         return VLC_TRUE;
326     case 0x80:
327         *pi_sample_rate = 32000 >> half;
328         *pi_frame_size =6 * bitrate;
329         return VLC_TRUE;
330     default:
331         return VLC_FALSE;
332     }
333 }
334