1 /*****************************************************************************
2 * ps.c : Program Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: ps.c,v 1.7 2002/12/06 16:34:07 sam Exp $
7 * Authors: Christophe Massiot <massiot@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() */
28 #include <string.h> /* strdup() */
31 #include <vlc/input.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 #define PS_READ_ONCE 50
40 /*****************************************************************************
42 *****************************************************************************/
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Activate ( vlc_object_t * );
53 static void Deactivate ( vlc_object_t * );
54 static int Demux ( input_thread_t * );
56 /*****************************************************************************
58 *****************************************************************************/
60 set_description( _("ISO 13818-1 MPEG Program Stream input") );
61 set_capability( "demux", 1 );
62 set_callbacks( Activate, Deactivate );
66 /*****************************************************************************
67 * Activate: initialize PS structures
68 *****************************************************************************/
69 static int Activate( vlc_object_t * p_this )
71 input_thread_t * p_input = (input_thread_t *)p_this;
72 demux_sys_t * p_demux;
75 /* Set the demux function */
76 p_input->pf_demux = Demux;
78 /* Initialize access plug-in structures. */
79 if( p_input->i_mtu == 0 )
82 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
85 /* Have a peep at the show. */
86 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
88 /* Stream shorter than 4 bytes... */
89 msg_Err( p_input, "cannot peek()" );
93 if( *p_peek || *(p_peek + 1) || *(p_peek + 2) != 1 )
95 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "ps", 3 ) )
98 msg_Err( p_input, "this does not look like an MPEG PS stream, continuing" );
102 msg_Warn( p_input, "this does not look like an MPEG PS stream, "
103 "but continuing anyway" );
106 else if( *(p_peek + 3) <= 0xb9 )
108 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "ps", 3 ) )
111 msg_Err( p_input, "this seems to be an elementary stream (ES module?), but continuing" );
115 msg_Warn( p_input, "this seems to be an elementary stream (ES module?), but continuing" );
119 p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t ) );
120 if( p_demux == NULL )
125 p_input->p_private = (void*)&p_demux->mpeg;
126 p_demux->p_module = module_Need( p_input, "mpeg-system", NULL );
127 if( p_demux->p_module == NULL )
129 free( p_input->p_demux_data );
133 if( input_InitStream( p_input, sizeof( stream_ps_data_t ) ) == -1 )
135 module_Unneed( p_input, p_demux->p_module );
136 free( p_input->p_demux_data );
139 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
141 p_input->stream.p_selected_program =
142 p_input->stream.pp_programs[0] ;
144 if( p_input->stream.b_seekable )
146 stream_ps_data_t * p_demux_data =
147 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
149 /* Pre-parse the stream to gather stream_descriptor_t. */
150 p_input->stream.pp_programs[0]->b_is_ok = 0;
151 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
153 while( !p_input->b_die && !p_input->b_error
154 && !p_demux_data->b_has_PSM )
157 data_packet_t * p_data;
159 i_result = p_demux->mpeg.pf_read_ps( p_input, &p_data );
164 vlc_mutex_lock( &p_input->stream.stream_lock );
165 p_input->stream.pp_programs[0]->b_is_ok = 1;
166 vlc_mutex_unlock( &p_input->stream.stream_lock );
169 else if( i_result == -1 )
171 p_input->b_error = 1;
175 p_demux->mpeg.pf_parse_ps( p_input, p_data );
176 input_DeletePacket( p_input->p_method_data, p_data );
179 if( p_input->stream.p_selected_area->i_tell >
180 INPUT_PREPARSE_LENGTH )
185 input_AccessReinit( p_input );
186 p_input->pf_seek( p_input, (off_t)0 );
187 vlc_mutex_lock( &p_input->stream.stream_lock );
189 if( p_demux_data->b_has_PSM )
191 /* (The PSM decoder will care about spawning the decoders) */
192 p_input->stream.pp_programs[0]->b_is_ok = 1;
197 /* (We have to do it ourselves) */
200 /* FIXME: we should do multiple passes in case an audio type
203 i_es < p_input->stream.pp_programs[0]->i_es_number;
206 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
207 switch( p_es->i_fourcc )
209 case VLC_FOURCC('m','p','g','v'):
210 input_SelectES( p_input, p_es );
213 case VLC_FOURCC('m','p','g','a'):
214 if( config_GetInt( p_input, "audio-channel" )
215 == (p_es->i_id & 0x1F) ||
216 ( config_GetInt( p_input, "audio-channel" ) < 0
217 && !(p_es->i_id & 0x1F) ) )
218 switch( config_GetInt( p_input, "audio-type" ) )
222 input_SelectES( p_input, p_es );
226 case VLC_FOURCC('a','5','2',' '):
227 case VLC_FOURCC('a','5','2','b'):
228 if( config_GetInt( p_input, "audio-channel" )
229 == ((p_es->i_id & 0xF00) >> 8) ||
230 ( config_GetInt( p_input, "audio-channel" ) < 0
231 && !((p_es->i_id & 0xF00) >> 8) ) )
232 switch( config_GetInt( p_input, "audio-type" ) )
236 input_SelectES( p_input, p_es );
240 case VLC_FOURCC('s','p','u',' '):
241 case VLC_FOURCC('s','p','u','b'):
242 if( config_GetInt( p_input, "spu-channel" )
243 == ((p_es->i_id & 0x1F00) >> 8) )
245 input_SelectES( p_input, p_es );
249 case VLC_FOURCC('l','p','c','m'):
250 case VLC_FOURCC('l','p','c','b'):
251 if( config_GetInt( p_input, "audio-channel" )
252 == ((p_es->i_id & 0x1F00) >> 8) ||
253 ( config_GetInt( p_input, "audio-channel" ) < 0
254 && !((p_es->i_id & 0x1F00) >> 8) ) )
255 switch( config_GetInt( p_input, "audio-type" ) )
259 input_SelectES( p_input, p_es );
267 input_DumpStream( p_input );
268 vlc_mutex_unlock( &p_input->stream.stream_lock );
272 /* The programs will be added when we read them. */
273 vlc_mutex_lock( &p_input->stream.stream_lock );
274 p_input->stream.pp_programs[0]->b_is_ok = 0;
275 vlc_mutex_unlock( &p_input->stream.stream_lock );
281 /*****************************************************************************
282 * Deactivate: deinitialize PS structures
283 *****************************************************************************/
284 static void Deactivate( vlc_object_t * p_this )
286 input_thread_t * p_input = (input_thread_t *)p_this;
288 module_Unneed( p_input, p_input->p_demux_data->p_module );
289 free( p_input->p_demux_data );
292 /*****************************************************************************
293 * Demux: reads and demuxes data packets
294 *****************************************************************************
295 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
297 *****************************************************************************/
298 static int Demux( input_thread_t * p_input )
302 for( i = 0; i < PS_READ_ONCE; i++ )
304 data_packet_t * p_data;
306 i_result = p_input->p_demux_data->mpeg.pf_read_ps( p_input, &p_data );
313 p_input->p_demux_data->mpeg.pf_demux_ps( p_input, p_data );