1 /*****************************************************************************
2 * a52.c : raw A/52 stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: a52.c,v 1.2 2004/02/13 22:10:00 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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 *****************************************************************************/
28 #include <vlc/input.h>
29 #include <vlc_codec.h>
35 #define A52_PACKET_SIZE 16384
36 #define A52_MAX_HEADER_SIZE 10
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close ( vlc_object_t * );
43 static int Demux ( input_thread_t * );
45 static int Control( input_thread_t *, int, va_list );
53 decoder_t *p_packetizer;
56 vlc_bool_t b_big_endian;
59 /*****************************************************************************
61 *****************************************************************************/
63 set_description( _("Raw A/52 demuxer") );
64 set_capability( "demux", 155 );
65 set_callbacks( Open, Close );
66 add_shortcut( "a52" );
69 /*****************************************************************************
70 * CheckSync: Check if buffer starts with an A52 sync code
71 *****************************************************************************/
72 int CheckSync( uint8_t *p_peek, vlc_bool_t *p_big_endian )
74 /* Little endian version of the bitstream */
75 if( p_peek[0] == 0x77 && p_peek[1] == 0x0b &&
76 p_peek[4] < 0x60 /* bsid < 12 */ )
80 /* Big endian version of the bitstream */
81 else if( p_peek[0] == 0x0b && p_peek[1] == 0x77 &&
82 p_peek[5] < 0x60 /* bsid < 12 */ )
90 /*****************************************************************************
91 * Open: initializes ES structures
92 *****************************************************************************/
93 static int Open( vlc_object_t * p_this )
95 input_thread_t *p_input = (input_thread_t *)p_this;
99 vlc_bool_t b_big_endian;
101 p_input->pf_demux = Demux;
102 p_input->pf_demux_control = Control;
103 p_input->pf_rewind = NULL;
105 /* Check if we are dealing with a WAV file */
106 if( input_Peek( p_input, &p_peek, 12 ) == 12 &&
107 !strncmp( p_peek, "RIFF", 4 ) && !strncmp( &p_peek[8], "WAVE", 4 ) )
111 /* Skip the wave header */
113 while( input_Peek( p_input, &p_peek, i_peek ) == i_peek &&
114 strncmp( p_peek + i_peek - 8, "data", 4 ) )
116 i_peek += GetDWLE( p_peek + i_peek - 4 ) + 8;
119 /* TODO: should check wave format and sample_rate */
121 /* Some A52 wav files don't begin with a sync code so we do a more
122 * extensive search */
123 i_size = input_Peek( p_input, &p_peek, i_peek + A52_PACKET_SIZE * 2);
124 i_size -= A52_MAX_HEADER_SIZE;
126 while( i_peek < i_size )
128 if( CheckSync( p_peek + i_peek, &b_big_endian ) != VLC_SUCCESS )
129 /* The data is stored in 16 bits words */
136 /* Have a peep at the show. */
137 if( input_Peek( p_input, &p_peek, i_peek + A52_MAX_HEADER_SIZE * 2 ) <
138 i_peek + A52_MAX_HEADER_SIZE * 2 )
140 /* Stream too short */
141 msg_Warn( p_input, "cannot peek()" );
145 if( CheckSync( p_peek + i_peek, &b_big_endian ) != VLC_SUCCESS )
147 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "a52", 3 ) )
150 msg_Err( p_input, "this doesn't look like a A52 audio stream, "
151 "continuing anyway" );
159 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
160 p_sys->b_start = VLC_TRUE;
161 p_sys->i_mux_rate = 0;
162 p_sys->b_big_endian = b_big_endian;
165 * Load the A52 packetizer
167 p_sys->p_packetizer = vlc_object_create( p_input, VLC_OBJECT_DECODER );
168 p_sys->p_packetizer->pf_decode_audio = 0;
169 p_sys->p_packetizer->pf_decode_video = 0;
170 p_sys->p_packetizer->pf_decode_sub = 0;
171 p_sys->p_packetizer->pf_packetize = 0;
173 /* Initialization of decoder structure */
174 es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
175 VLC_FOURCC( 'a', '5', '2', ' ' ) );
177 p_sys->p_packetizer->p_module =
178 module_Need( p_sys->p_packetizer, "packetizer", NULL );
179 if( !p_sys->p_packetizer->p_module )
181 msg_Err( p_input, "cannot find A52 packetizer" );
185 /* Create one program */
186 vlc_mutex_lock( &p_input->stream.stream_lock );
187 if( input_InitStream( p_input, 0 ) == -1 )
189 vlc_mutex_unlock( &p_input->stream.stream_lock );
190 msg_Err( p_input, "cannot init stream" );
193 p_input->stream.i_mux_rate = 0;
194 vlc_mutex_unlock( &p_input->stream.stream_lock );
197 es_out_Add( p_input->p_es_out, &p_sys->p_packetizer->fmt_in );
202 /*****************************************************************************
203 * Close: frees unused data
204 *****************************************************************************/
205 static void Close( vlc_object_t * p_this )
207 input_thread_t *p_input = (input_thread_t*)p_this;
208 demux_sys_t *p_sys = p_input->p_demux_data;
211 module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
213 /* Delete the decoder */
214 vlc_object_destroy( p_sys->p_packetizer );
219 /*****************************************************************************
220 * Demux: reads and demuxes data packets
221 *****************************************************************************
222 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
223 *****************************************************************************/
224 static int Demux( input_thread_t * p_input )
226 demux_sys_t *p_sys = p_input->p_demux_data;
227 block_t *p_block_in, *p_block_out;
230 int64_t i_pos = stream_Tell( p_input->s );
231 if( i_pos % 2 ) stream_Read( p_input->s, NULL, 1 );
233 if( !( p_block_in = stream_Block( p_input->s, A52_PACKET_SIZE ) ) )
238 if( !p_sys->b_big_endian && p_block_in->i_buffer )
240 /* Convert to big endian */
243 swab(p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer);
248 p_tmp = p_block_in->p_buffer;
249 for( i = p_block_in->i_buffer / 2 ; i-- ; )
260 p_block_in->i_pts = p_block_in->i_dts = 1;
262 p_block_in->i_pts = p_block_in->i_dts = 0;
264 while( (p_block_out = p_sys->p_packetizer->pf_packetize(
265 p_sys->p_packetizer, &p_block_in )) )
267 p_sys->b_start = VLC_FALSE;
271 block_t *p_next = p_block_out->p_next;
273 /* We assume a constant bitrate */
274 if( p_block_out->i_length )
276 p_block_out->i_buffer * I64C(1000000) / p_block_out->i_length;
277 p_input->stream.i_mux_rate = p_sys->i_mux_rate / 50;
279 input_ClockManageRef( p_input,
280 p_input->stream.p_selected_program,
281 p_block_out->i_pts * 9 / 100 );
283 p_block_in->b_discontinuity = 0;
284 p_block_out->i_dts = p_block_out->i_pts =
285 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
286 p_block_out->i_pts * 9 / 100 );
288 es_out_Send( p_input->p_es_out, p_sys->p_es, p_block_out );
290 p_block_out = p_next;
297 /*****************************************************************************
299 *****************************************************************************/
300 static int Control( input_thread_t *p_input, int i_query, va_list args )
302 demux_sys_t *p_sys = (demux_sys_t *)p_input->p_demux_data;
308 pi64 = (int64_t*)va_arg( args, int64_t * );
309 if( p_sys->i_mux_rate > 0 )
311 *pi64 = I64C(1000000) * stream_Tell( p_input->s ) /
318 case DEMUX_GET_LENGTH:
319 pi64 = (int64_t*)va_arg( args, int64_t * );
320 if( p_sys->i_mux_rate > 0 )
322 *pi64 = I64C(1000000) * stream_Size( p_input->s ) /
330 return demux_vaControlDefault( p_input, i_query, args );