]> git.sesse.net Git - vlc/blob - modules/demux/a52sys.c
* useless.
[vlc] / modules / demux / a52sys.c
1 /*****************************************************************************
2  * a52.c : Raw a52 Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: a52sys.c,v 1.6 2003/09/12 16:26:40 fenrir 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( _("A52 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_descriptor_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 AAC 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
92     if( p_input->psz_demux && !strncmp( p_input->psz_demux, "a52", 3 ) )
93     {
94         b_forced = VLC_TRUE;
95     }
96     if( p_input->psz_name )
97     {
98         int  i_len = strlen( p_input->psz_name );
99
100         if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".a52" ) )
101         {
102             b_forced = VLC_TRUE;
103         }
104     }
105
106     /* skip possible id3 header */
107     p_id3 = module_Need( p_input, "id3", NULL );
108     if ( p_id3 )
109     {
110         module_Unneed( p_input, p_id3 );
111     }
112
113     /* see if it could be 52 */
114     if( !b_forced )
115     {
116         if( input_Peek( p_input, &p_peek, 6 ) < 6 )
117         {
118             msg_Err( p_input, "cannot peek" );
119             return VLC_EGENERIC;
120         }
121         if( !HeaderCheck( p_peek ) )
122         {
123             msg_Warn( p_input, "A52 module discarded" );
124             return VLC_EGENERIC;
125         }
126     }
127
128     p_input->pf_demux = Demux;
129     p_input->pf_demux_control = demux_vaControlDefault;
130
131     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
132     p_sys->i_time = 0;
133
134     if( stream_Peek( p_input->s, &p_peek, 6 ) < 6 )
135     {
136         msg_Err( p_input, "cannot peek" );
137         goto error;
138     }
139
140     if( HeaderCheck( p_peek ) )
141     {
142         int i_channels, i_sample_rate, i_frame_size;
143         input_info_category_t * p_category;
144
145         HeaderInfo( p_peek, &i_channels, &i_sample_rate, &i_frame_size );
146
147         msg_Dbg( p_input,
148                  "a52 channels=%d sample_rate=%d",
149                  i_channels, i_sample_rate );
150
151         vlc_mutex_lock( &p_input->stream.stream_lock );
152
153         p_category = input_InfoCategory( p_input, _("A52") );
154
155         input_AddInfo( p_category, _("Input Type"), "A52" );
156         input_AddInfo( p_category, _("Channels"), "%d", i_channels );
157         input_AddInfo( p_category, _("Sample Rate"), "%dHz", i_sample_rate );
158
159         vlc_mutex_unlock( &p_input->stream.stream_lock );
160     }
161
162     vlc_mutex_lock( &p_input->stream.stream_lock );
163     if( input_InitStream( p_input, 0 ) == -1)
164     {
165         vlc_mutex_unlock( &p_input->stream.stream_lock );
166         msg_Err( p_input, "cannot init stream" );
167         goto error;
168     }
169     if( input_AddProgram( p_input, 0, 0) == NULL )
170     {
171         vlc_mutex_unlock( &p_input->stream.stream_lock );
172         msg_Err( p_input, "cannot add program" );
173         goto error;
174     }
175     p_input->stream.pp_programs[0]->b_is_ok = 0;
176     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
177
178     p_input->stream.i_mux_rate = 0 / 50;
179
180     p_sys->p_es = input_AddES( p_input,
181                                p_input->stream.p_selected_program,
182                                1 , AUDIO_ES, NULL, 0 );
183
184     p_sys->p_es->i_stream_id = 1;
185     p_sys->p_es->i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
186     input_SelectES( p_input, p_sys->p_es );
187
188     p_input->stream.p_selected_program->b_is_ok = 1;
189     vlc_mutex_unlock( &p_input->stream.stream_lock );
190
191     return VLC_SUCCESS;
192
193 error:
194     free( p_sys );
195     return VLC_EGENERIC;
196 }
197
198
199 /*****************************************************************************
200  * Demux: reads and demuxes data packets
201  *****************************************************************************
202  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
203  *****************************************************************************/
204 static int Demux( input_thread_t * p_input )
205 {
206     demux_sys_t  *p_sys = p_input->p_demux_data;
207     pes_packet_t *p_pes;
208
209     int i_channels, i_sample_rate, i_frame_size;
210
211     uint8_t      *p_peek;
212
213     if( stream_Peek( p_input->s, &p_peek, 6 ) < 6 )
214     {
215         msg_Warn( p_input, "cannot peek" );
216         return 0;
217     }
218
219     if( !HeaderCheck( p_peek ) )
220     {
221         /* we need to resynch */
222         vlc_bool_t  b_ok = VLC_FALSE;
223         int         i_skip = 0;
224         int         i_peek;
225
226         i_peek = stream_Peek( p_input->s, &p_peek, 8096 );
227         if( i_peek < 8 )
228         {
229             msg_Warn( p_input, "cannot peek" );
230             return 0;
231         }
232
233         while( i_peek >= 8 )
234         {
235             if( HeaderCheck( p_peek ) )
236             {
237                 b_ok = VLC_TRUE;
238                 break;
239             }
240
241             p_peek++;
242             i_peek--;
243             i_skip++;
244         }
245
246         msg_Warn( p_input, "garbage=%d bytes", i_skip );
247         stream_Read( p_input->s, NULL, i_skip );
248         return 1;
249     }
250
251     HeaderInfo( p_peek, &i_channels, &i_sample_rate, &i_frame_size );
252
253     input_ClockManageRef( p_input,
254                           p_input->stream.p_selected_program,
255                           p_sys->i_time * 9 / 100 );
256
257     if( ( p_pes = stream_PesPacket( p_input->s, i_frame_size ) ) == NULL )
258     {
259         msg_Warn( p_input, "cannot read data" );
260         return 0;
261     }
262
263     p_pes->i_dts =
264     p_pes->i_pts = input_ClockGetTS( p_input,
265                                      p_input->stream.p_selected_program,
266                                      p_sys->i_time * 9 / 100 );
267
268     if( !p_sys->p_es->p_decoder_fifo )
269     {
270         msg_Err( p_input, "no audio decoder" );
271         input_DeletePES( p_input->p_method_data, p_pes );
272         return( -1 );
273     }
274
275     input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
276     p_sys->i_time += (mtime_t)1000000 *
277                      (mtime_t)1536 /
278                      (mtime_t)i_sample_rate;
279     return( 1 );
280 }
281
282 /*****************************************************************************
283  * Close: frees unused data
284  *****************************************************************************/
285 static void Close( vlc_object_t * p_this )
286 {
287     input_thread_t *p_input = (input_thread_t*)p_this;
288     demux_sys_t    *p_sys = p_input->p_demux_data;
289
290     free( p_sys );
291 }
292
293
294
295 /*****************************************************************************
296  * SyncInfo: parse A/52 sync info
297  *****************************************************************************
298  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
299  *****************************************************************************/
300 static int HeaderInfo( const uint8_t * p,
301                         int *pi_channels,
302                         int *pi_sample_rate,
303                         int *pi_frame_size )
304 {
305     static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
306     static const int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
307                                 128, 160, 192, 224, 256, 320, 384, 448,
308                                 512, 576, 640 };
309     static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
310                                       0x04, 0x01, 0x04, 0x01 };
311     int frmsizecod;
312     int bitrate;
313     int half;
314     int acmod;
315
316     if ((p[0] != 0x0b) || (p[1] != 0x77))        /* syncword */
317         return VLC_FALSE;
318
319     if (p[5] >= 0x60)                /* bsid >= 12 */
320         return VLC_FALSE;
321     half = halfrate[p[5] >> 3];
322
323     /* acmod, dsurmod and lfeon */
324     acmod = p[6] >> 5;
325     if ( (p[6] & 0xf8) == 0x50 )
326     {
327         /* Dolby surround = stereo + Dolby */
328         *pi_channels = 2;
329     }
330     else
331     {
332         static const int acmod_to_channels[8] =
333         {
334             2 /* dual mono */, 1 /* mono */, 2 /* stereo */,
335             3 /* 3F */, 3 /* 2f1R */,
336             4 /* 3F1R */, 4, /* 2F2R */
337             5 /* 3F2R */
338         };
339
340         *pi_channels = acmod_to_channels[acmod];
341     }
342
343     if ( p[6] & lfeon[acmod] ) (*pi_channels)++;    /* LFE */
344
345     frmsizecod = p[4] & 63;
346     if (frmsizecod >= 38)
347         return VLC_FALSE;
348     bitrate = rate[frmsizecod >> 1];
349
350     switch (p[4] & 0xc0) {
351     case 0:
352         *pi_sample_rate = 48000 >> half;
353         *pi_frame_size = 4 * bitrate;
354         return VLC_TRUE;
355     case 0x40:
356         *pi_sample_rate = 44100 >> half;
357         *pi_frame_size = 2 * (320 * bitrate / 147 + (frmsizecod & 1));
358         return VLC_TRUE;
359     case 0x80:
360         *pi_sample_rate = 32000 >> half;
361         *pi_frame_size =6 * bitrate;
362         return VLC_TRUE;
363     default:
364         return VLC_FALSE;
365     }
366 }
367