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.16 2002/07/23 00:39:17 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() */
32 #include <vlc/input.h>
34 #include <sys/types.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 #define PS_READ_ONCE 50
41 /*****************************************************************************
43 *****************************************************************************/
44 static void input_getfunctions( function_list_t * p_function_list );
45 static int PSDemux ( input_thread_t * );
46 static int PSInit ( input_thread_t * );
47 static void PSEnd ( input_thread_t * );
49 /*****************************************************************************
50 * Build configuration tree.
51 *****************************************************************************/
56 SET_DESCRIPTION( _("ISO 13818-1 MPEG Program Stream input") )
57 ADD_CAPABILITY( DEMUX, 100 )
62 input_getfunctions( &p_module->p_functions->demux );
65 MODULE_DEACTIVATE_START
66 MODULE_DEACTIVATE_STOP
68 /*****************************************************************************
69 * Functions exported as capabilities. They are declared as static so that
70 * we don't pollute the namespace too much.
71 *****************************************************************************/
72 static void input_getfunctions( function_list_t * p_function_list )
74 #define input p_function_list->functions.demux
75 input.pf_init = PSInit;
77 input.pf_demux = PSDemux;
78 input.pf_rewind = NULL;
82 /*****************************************************************************
83 * PSInit: initializes PS structures
84 *****************************************************************************/
85 static int PSInit( input_thread_t * p_input )
89 /* Initialize access plug-in structures. */
90 if( p_input->i_mtu == 0 )
93 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
96 /* Have a peep at the show. */
97 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
99 /* Stream shorter than 4 bytes... */
100 msg_Err( p_input, "cannot peek()" );
104 if( *p_peek || *(p_peek + 1) || *(p_peek + 2) != 1 )
106 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "ps", 3 ) )
109 msg_Err( p_input, "this does not look like an MPEG PS stream, continuing" );
113 msg_Warn( p_input, "this does not look like an MPEG PS stream, "
114 "but continuing anyway" );
117 else if( *(p_peek + 3) <= 0xb9 )
119 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "ps", 3 ) )
122 msg_Err( p_input, "this seems to be an elementary stream (ES module?), but continuing" );
126 msg_Warn( p_input, "this seems to be an elementary stream (ES module?), but continuing" );
130 if( input_InitStream( p_input, sizeof( stream_ps_data_t ) ) == -1 )
134 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
136 p_input->stream.p_selected_program =
137 p_input->stream.pp_programs[0] ;
139 if( p_input->stream.b_seekable )
141 stream_ps_data_t * p_demux_data =
142 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
144 /* Pre-parse the stream to gather stream_descriptor_t. */
145 p_input->stream.pp_programs[0]->b_is_ok = 0;
146 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
148 while( !p_input->b_die && !p_input->b_error
149 && !p_demux_data->b_has_PSM )
152 data_packet_t * p_data;
154 i_result = input_ReadPS( p_input, &p_data );
159 vlc_mutex_lock( &p_input->stream.stream_lock );
160 p_input->stream.pp_programs[0]->b_is_ok = 1;
161 vlc_mutex_unlock( &p_input->stream.stream_lock );
164 else if( i_result == -1 )
166 p_input->b_error = 1;
170 input_ParsePS( p_input, p_data );
171 input_DeletePacket( p_input->p_method_data, p_data );
174 if( p_input->stream.p_selected_area->i_tell >
175 INPUT_PREPARSE_LENGTH )
180 input_AccessReinit( p_input );
181 p_input->pf_seek( p_input, (off_t)0 );
182 vlc_mutex_lock( &p_input->stream.stream_lock );
184 if( p_demux_data->b_has_PSM )
186 /* (The PSM decoder will care about spawning the decoders) */
187 p_input->stream.pp_programs[0]->b_is_ok = 1;
192 /* (We have to do it ourselves) */
195 /* FIXME: we should do multiple passes in case an audio type
198 i_es < p_input->stream.pp_programs[0]->i_es_number;
201 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
202 switch( p_es->i_fourcc )
204 case VLC_FOURCC('m','p','g','v'):
205 input_SelectES( p_input, p_es );
208 case VLC_FOURCC('m','p','g','a'):
209 if( config_GetInt( p_input, "audio-channel" )
210 == (p_es->i_id & 0x1F) ||
211 ( config_GetInt( p_input, "audio-channel" ) < 0
212 && !(p_es->i_id & 0x1F) ) )
213 switch( config_GetInt( p_input, "audio-type" ) )
217 input_SelectES( p_input, p_es );
221 case VLC_FOURCC('a','5','2',' '):
222 if( config_GetInt( p_input, "audio-channel" )
223 == ((p_es->i_id & 0xF00) >> 8) ||
224 ( config_GetInt( p_input, "audio-channel" ) < 0
225 && !((p_es->i_id & 0xF00) >> 8) ) )
226 switch( config_GetInt( p_input, "audio-type" ) )
230 input_SelectES( p_input, p_es );
234 case VLC_FOURCC('s','p','u',' '):
235 if( config_GetInt( p_input, "spu-channel" )
236 == ((p_es->i_id & 0x1F00) >> 8) )
238 input_SelectES( p_input, p_es );
242 case VLC_FOURCC('l','p','c','m'):
243 if( config_GetInt( p_input, "audio-channel" )
244 == ((p_es->i_id & 0x1F00) >> 8) ||
245 ( config_GetInt( p_input, "audio-channel" ) < 0
246 && !((p_es->i_id & 0x1F00) >> 8) ) )
247 switch( config_GetInt( p_input, "audio-type" ) )
251 input_SelectES( p_input, p_es );
259 input_DumpStream( p_input );
260 vlc_mutex_unlock( &p_input->stream.stream_lock );
264 /* The programs will be added when we read them. */
265 vlc_mutex_lock( &p_input->stream.stream_lock );
266 p_input->stream.pp_programs[0]->b_is_ok = 0;
267 vlc_mutex_unlock( &p_input->stream.stream_lock );
273 /*****************************************************************************
274 * PSEnd: frees unused data
275 *****************************************************************************/
276 static void PSEnd( input_thread_t * p_input )
280 /*****************************************************************************
281 * PSDemux: reads and demuxes data packets
282 *****************************************************************************
283 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
285 *****************************************************************************/
286 static int PSDemux( input_thread_t * p_input )
290 for( i = 0; i < PS_READ_ONCE; i++ )
292 data_packet_t * p_data;
294 i_result = input_ReadPS( p_input, &p_data );
301 input_DemuxPS( p_input, p_data );