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 $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
32 /*****************************************************************************
34 *****************************************************************************/
35 static int Open ( vlc_object_t * );
36 static void Close ( vlc_object_t * );
39 set_description( _("A52 demuxer" ) );
40 set_capability( "demux", 100 );
41 set_callbacks( Open, Close );
42 add_shortcut( "a52" );
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Demux ( input_thread_t * );
58 es_descriptor_t *p_es;
61 static inline int HeaderCheck( const uint8_t * p )
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 ) )
73 static int HeaderInfo( const uint8_t * p,
78 /*****************************************************************************
79 * Open: initializes AAC demux structures
80 *****************************************************************************/
81 static int Open( vlc_object_t * p_this )
83 input_thread_t *p_input = (input_thread_t *)p_this;
85 int b_forced = VLC_FALSE;
92 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "a52", 3 ) )
96 if( p_input->psz_name )
98 int i_len = strlen( p_input->psz_name );
100 if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".a52" ) )
106 /* skip possible id3 header */
107 p_id3 = module_Need( p_input, "id3", NULL );
110 module_Unneed( p_input, p_id3 );
113 /* see if it could be 52 */
116 if( input_Peek( p_input, &p_peek, 6 ) < 6 )
118 msg_Err( p_input, "cannot peek" );
121 if( !HeaderCheck( p_peek ) )
123 msg_Warn( p_input, "A52 module discarded" );
128 p_input->pf_demux = Demux;
129 p_input->pf_demux_control = demux_vaControlDefault;
131 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
134 if( stream_Peek( p_input->s, &p_peek, 6 ) < 6 )
136 msg_Err( p_input, "cannot peek" );
140 if( HeaderCheck( p_peek ) )
142 int i_channels, i_sample_rate, i_frame_size;
143 input_info_category_t * p_category;
145 HeaderInfo( p_peek, &i_channels, &i_sample_rate, &i_frame_size );
148 "a52 channels=%d sample_rate=%d",
149 i_channels, i_sample_rate );
151 vlc_mutex_lock( &p_input->stream.stream_lock );
153 p_category = input_InfoCategory( p_input, _("A52") );
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 );
159 vlc_mutex_unlock( &p_input->stream.stream_lock );
162 vlc_mutex_lock( &p_input->stream.stream_lock );
163 if( input_InitStream( p_input, 0 ) == -1)
165 vlc_mutex_unlock( &p_input->stream.stream_lock );
166 msg_Err( p_input, "cannot init stream" );
169 if( input_AddProgram( p_input, 0, 0) == NULL )
171 vlc_mutex_unlock( &p_input->stream.stream_lock );
172 msg_Err( p_input, "cannot add program" );
175 p_input->stream.pp_programs[0]->b_is_ok = 0;
176 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
178 p_input->stream.i_mux_rate = 0 / 50;
180 p_sys->p_es = input_AddES( p_input,
181 p_input->stream.p_selected_program,
182 1 , AUDIO_ES, NULL, 0 );
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 );
188 p_input->stream.p_selected_program->b_is_ok = 1;
189 vlc_mutex_unlock( &p_input->stream.stream_lock );
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 )
206 demux_sys_t *p_sys = p_input->p_demux_data;
209 int i_channels, i_sample_rate, i_frame_size;
213 if( stream_Peek( p_input->s, &p_peek, 6 ) < 6 )
215 msg_Warn( p_input, "cannot peek" );
219 if( !HeaderCheck( p_peek ) )
221 /* we need to resynch */
222 vlc_bool_t b_ok = VLC_FALSE;
226 i_peek = stream_Peek( p_input->s, &p_peek, 8096 );
229 msg_Warn( p_input, "cannot peek" );
235 if( HeaderCheck( p_peek ) )
246 msg_Warn( p_input, "garbage=%d bytes", i_skip );
247 stream_Read( p_input->s, NULL, i_skip );
251 HeaderInfo( p_peek, &i_channels, &i_sample_rate, &i_frame_size );
253 input_ClockManageRef( p_input,
254 p_input->stream.p_selected_program,
255 p_sys->i_time * 9 / 100 );
257 if( ( p_pes = stream_PesPacket( p_input->s, i_frame_size ) ) == NULL )
259 msg_Warn( p_input, "cannot read data" );
264 p_pes->i_pts = input_ClockGetTS( p_input,
265 p_input->stream.p_selected_program,
266 p_sys->i_time * 9 / 100 );
268 if( !p_sys->p_es->p_decoder_fifo )
270 msg_Err( p_input, "no audio decoder" );
271 input_DeletePES( p_input->p_method_data, p_pes );
275 input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
276 p_sys->i_time += (mtime_t)1000000 *
278 (mtime_t)i_sample_rate;
282 /*****************************************************************************
283 * Close: frees unused data
284 *****************************************************************************/
285 static void Close( vlc_object_t * p_this )
287 input_thread_t *p_input = (input_thread_t*)p_this;
288 demux_sys_t *p_sys = p_input->p_demux_data;
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,
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,
309 static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
310 0x04, 0x01, 0x04, 0x01 };
316 if ((p[0] != 0x0b) || (p[1] != 0x77)) /* syncword */
319 if (p[5] >= 0x60) /* bsid >= 12 */
321 half = halfrate[p[5] >> 3];
323 /* acmod, dsurmod and lfeon */
325 if ( (p[6] & 0xf8) == 0x50 )
327 /* Dolby surround = stereo + Dolby */
332 static const int acmod_to_channels[8] =
334 2 /* dual mono */, 1 /* mono */, 2 /* stereo */,
335 3 /* 3F */, 3 /* 2f1R */,
336 4 /* 3F1R */, 4, /* 2F2R */
340 *pi_channels = acmod_to_channels[acmod];
343 if ( p[6] & lfeon[acmod] ) (*pi_channels)++; /* LFE */
345 frmsizecod = p[4] & 63;
346 if (frmsizecod >= 38)
348 bitrate = rate[frmsizecod >> 1];
350 switch (p[4] & 0xc0) {
352 *pi_sample_rate = 48000 >> half;
353 *pi_frame_size = 4 * bitrate;
356 *pi_sample_rate = 44100 >> half;
357 *pi_frame_size = 2 * (320 * bitrate / 147 + (frmsizecod & 1));
360 *pi_sample_rate = 32000 >> half;
361 *pi_frame_size =6 * bitrate;