1 /*****************************************************************************
2 * mpeg_ps.c : Program Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: mpeg_ps.c,v 1.11 2002/04/15 23:04:08 massiot 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 <videolan/vlc.h>
33 #include <sys/types.h>
35 #include "stream_control.h"
36 #include "input_ext-intf.h"
37 #include "input_ext-dec.h"
38 #include "input_ext-plugins.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 #define PS_READ_ONCE 50
45 /*****************************************************************************
47 *****************************************************************************/
48 static void input_getfunctions( function_list_t * p_function_list );
49 static int PSDemux ( struct input_thread_s * );
50 static int PSInit ( struct input_thread_s * );
51 static void PSEnd ( struct input_thread_s * );
53 /*****************************************************************************
54 * Build configuration tree.
55 *****************************************************************************/
60 SET_DESCRIPTION( "ISO 13818-1 MPEG Program Stream input" )
61 ADD_CAPABILITY( DEMUX, 100 )
66 input_getfunctions( &p_module->p_functions->demux );
69 MODULE_DEACTIVATE_START
70 MODULE_DEACTIVATE_STOP
72 /*****************************************************************************
73 * Functions exported as capabilities. They are declared as static so that
74 * we don't pollute the namespace too much.
75 *****************************************************************************/
76 static void input_getfunctions( function_list_t * p_function_list )
78 #define input p_function_list->functions.demux
79 input.pf_init = PSInit;
81 input.pf_demux = PSDemux;
82 input.pf_rewind = NULL;
86 /*****************************************************************************
87 * PSInit: initializes PS structures
88 *****************************************************************************/
89 static int PSInit( input_thread_t * p_input )
93 /* Initialize access plug-in structures. */
94 if( p_input->i_mtu == 0 )
97 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
100 /* Have a peep at the show. */
101 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
103 /* Stream shorter than 4 bytes... */
104 intf_ErrMsg( "input error: cannot peek() (mpeg_ps)" );
108 if( *p_peek || *(p_peek + 1) || *(p_peek + 2) != 1 )
110 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "ps", 3 ) )
113 intf_ErrMsg( "input error: this doesn't look like an MPEG PS stream, continuing" );
117 intf_WarnMsg( 2, "input error: this doesn't look like an MPEG PS stream, but continuing anyway" );
120 else if( *(p_peek + 3) <= 0xb9 )
122 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "ps", 3 ) )
125 intf_ErrMsg( "input error: this seems to be an elementary stream (ES plug-in ?),");
126 intf_ErrMsg( "but continuing" );
130 intf_WarnMsg( 2, "input error: this seems to be an elementary stream (ES plug-in ?),");
131 intf_WarnMsg( 2, "but continuing" );
135 if( input_InitStream( p_input, sizeof( stream_ps_data_t ) ) == -1 )
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 = input_ReadPS( 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 input_ParsePS( 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_type )
211 input_SelectES( p_input, p_es );
216 if( config_GetIntVariable( "input_channel" )
217 == (p_es->i_id & 0x1F) ||
218 ( config_GetIntVariable( "input_channel" ) < 0
219 && !(p_es->i_id & 0x1F) ) )
220 switch( config_GetIntVariable( "input_audio" ) )
224 input_SelectES( p_input, p_es );
229 if( config_GetIntVariable( "input_channel" )
230 == ((p_es->i_id & 0xF00) >> 8) ||
231 ( config_GetIntVariable( "input_channel" ) < 0
232 && !((p_es->i_id & 0xF00) >> 8) ) )
233 switch( config_GetIntVariable( "input_audio" ) )
237 input_SelectES( p_input, p_es );
242 if( config_GetIntVariable( "input_subtitle" )
243 == ((p_es->i_id & 0x1F00) >> 8) )
245 input_SelectES( p_input, p_es );
250 if( config_GetIntVariable( "input_channel" )
251 == ((p_es->i_id & 0x1F00) >> 8) ||
252 ( config_GetIntVariable( "input_channel" ) < 0
253 && !((p_es->i_id & 0x1F00) >> 8) ) )
254 switch( config_GetIntVariable( "input_audio" ) )
258 input_SelectES( p_input, p_es );
266 if( p_main->b_stats )
268 input_DumpStream( p_input );
270 vlc_mutex_unlock( &p_input->stream.stream_lock );
274 /* The programs will be added when we read them. */
275 vlc_mutex_lock( &p_input->stream.stream_lock );
276 p_input->stream.i_method = INPUT_METHOD_FILE;
277 p_input->stream.pp_programs[0]->b_is_ok = 0;
278 vlc_mutex_unlock( &p_input->stream.stream_lock );
284 /*****************************************************************************
285 * PSEnd: frees unused data
286 *****************************************************************************/
287 static void PSEnd( input_thread_t * p_input )
291 /*****************************************************************************
292 * PSDemux: reads and demuxes data packets
293 *****************************************************************************
294 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
296 *****************************************************************************/
297 static int PSDemux( input_thread_t * p_input )
301 for( i = 0; i < PS_READ_ONCE; i++ )
303 data_packet_t * p_data;
306 i_result = input_ReadPS( p_input, &p_data );
313 input_DemuxPS( p_input, p_data );