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 $
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( _("A/52 demuxer" ) );
40 set_capability( "demux", 100 );
41 set_callbacks( Open, Close );
42 add_shortcut( "a52" );
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Demux ( input_thread_t * );
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 A52 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;
94 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "a52", 3 ) )
98 if( p_input->psz_name )
100 int i_len = strlen( p_input->psz_name );
102 if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".a52" ) )
108 /* skip possible id3 header */
109 p_id3 = module_Need( p_input, "id3", NULL );
112 module_Unneed( p_input, p_id3 );
115 /* see if it could be 52 */
118 if( input_Peek( p_input, &p_peek, 6 ) < 6 )
120 msg_Err( p_input, "cannot peek" );
123 if( !HeaderCheck( p_peek ) )
125 msg_Warn( p_input, "A52 module discarded" );
130 p_input->pf_demux = Demux;
131 p_input->pf_demux_control = demux_vaControlDefault;
133 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
136 if( stream_Peek( p_input->s, &p_peek, 6 ) < 6 )
138 msg_Err( p_input, "cannot peek" );
142 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
143 if( HeaderCheck( p_peek ) )
147 HeaderInfo( p_peek, &fmt.audio.i_channels, &fmt.audio.i_rate, &i_frame_size );
149 msg_Dbg( p_input, "a52 channels=%d sample_rate=%d",
150 fmt.audio.i_channels, fmt.audio.i_rate );
153 vlc_mutex_lock( &p_input->stream.stream_lock );
154 if( input_InitStream( p_input, 0 ) == -1)
156 vlc_mutex_unlock( &p_input->stream.stream_lock );
157 msg_Err( p_input, "cannot init stream" );
160 p_input->stream.i_mux_rate = 0 / 50;
161 vlc_mutex_unlock( &p_input->stream.stream_lock );
163 p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
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 )
179 demux_sys_t *p_sys = p_input->p_demux_data;
182 int i_channels, i_sample_rate, i_frame_size;
186 if( stream_Peek( p_input->s, &p_peek, 6 ) < 6 )
188 msg_Warn( p_input, "cannot peek" );
192 if( !HeaderCheck( p_peek ) )
194 /* we need to resynch */
195 vlc_bool_t b_ok = VLC_FALSE;
199 i_peek = stream_Peek( p_input->s, &p_peek, 8096 );
202 msg_Warn( p_input, "cannot peek" );
208 if( HeaderCheck( p_peek ) )
219 msg_Warn( p_input, "garbage=%d bytes", i_skip );
220 stream_Read( p_input->s, NULL, i_skip );
224 HeaderInfo( p_peek, &i_channels, &i_sample_rate, &i_frame_size );
226 input_ClockManageRef( p_input,
227 p_input->stream.p_selected_program,
228 p_sys->i_time * 9 / 100 );
230 if( ( p_block = stream_Block( p_input->s, i_frame_size ) ) == NULL )
232 msg_Warn( p_input, "cannot read data" );
237 p_block->i_pts = input_ClockGetTS( p_input,
238 p_input->stream.p_selected_program,
239 p_sys->i_time * 9 / 100 );
241 es_out_Send( p_input->p_es_out, p_sys->p_es, p_block );
243 p_sys->i_time += (mtime_t)1000000 *
245 (mtime_t)i_sample_rate;
249 /*****************************************************************************
250 * Close: frees unused data
251 *****************************************************************************/
252 static void Close( vlc_object_t * p_this )
254 input_thread_t *p_input = (input_thread_t*)p_this;
255 demux_sys_t *p_sys = p_input->p_demux_data;
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,
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,
276 static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
277 0x04, 0x01, 0x04, 0x01 };
283 if ((p[0] != 0x0b) || (p[1] != 0x77)) /* syncword */
286 if (p[5] >= 0x60) /* bsid >= 12 */
288 half = halfrate[p[5] >> 3];
290 /* acmod, dsurmod and lfeon */
292 if ( (p[6] & 0xf8) == 0x50 )
294 /* Dolby surround = stereo + Dolby */
299 static const int acmod_to_channels[8] =
301 2 /* dual mono */, 1 /* mono */, 2 /* stereo */,
302 3 /* 3F */, 3 /* 2f1R */,
303 4 /* 3F1R */, 4, /* 2F2R */
307 *pi_channels = acmod_to_channels[acmod];
310 if ( p[6] & lfeon[acmod] ) (*pi_channels)++; /* LFE */
312 frmsizecod = p[4] & 63;
313 if (frmsizecod >= 38)
315 bitrate = rate[frmsizecod >> 1];
317 switch (p[4] & 0xc0) {
319 *pi_sample_rate = 48000 >> half;
320 *pi_frame_size = 4 * bitrate;
323 *pi_sample_rate = 44100 >> half;
324 *pi_frame_size = 2 * (320 * bitrate / 147 + (frmsizecod & 1));
327 *pi_sample_rate = 32000 >> half;
328 *pi_frame_size =6 * bitrate;