1 /*****************************************************************************
2 * input_ps.c: PS demux and packet management
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
5 * $Id: input_ps.c,v 1.7 2001/02/20 02:53:13 stef 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 *****************************************************************************/
33 #include <sys/types.h>
48 #include "stream_control.h"
49 #include "input_ext-intf.h"
50 #include "input_ext-dec.h"
55 #include "mpeg_system.h"
61 /*****************************************************************************
63 *****************************************************************************/
64 static int PSProbe ( probedata_t * );
65 static int PSRead ( struct input_thread_s *,
66 data_packet_t * p_packets[INPUT_READ_ONCE] );
67 static void PSInit ( struct input_thread_s * );
68 static void PSEnd ( struct input_thread_s * );
69 static void PSSeek ( struct input_thread_s *, off_t );
70 static struct pes_packet_s * NewPES ( void * );
71 static struct data_packet_s * NewPacket ( void *, size_t );
72 static void DeletePacket( void *, struct data_packet_s * );
73 static void DeletePES ( void *, struct pes_packet_s * );
75 /*****************************************************************************
76 * Functions exported as capabilities. They are declared as static so that
77 * we don't pollute the namespace too much.
78 *****************************************************************************/
79 void input_getfunctions( function_list_t * p_function_list )
81 #define input p_function_list->functions.input
82 p_function_list->pf_probe = PSProbe;
83 input.pf_init = PSInit;
84 input.pf_open = input_FileOpen;
85 input.pf_close = input_FileClose;
87 input.pf_set_area = NULL;
88 input.pf_read = PSRead;
89 input.pf_demux = input_DemuxPS;
90 input.pf_new_packet = NewPacket;
91 input.pf_new_pes = NewPES;
92 input.pf_delete_packet = DeletePacket;
93 input.pf_delete_pes = DeletePES;
94 input.pf_rewind = NULL;
95 input.pf_seek = PSSeek;
100 * Data reading functions
103 /*****************************************************************************
104 * PSProbe: verifies that the stream is a PS stream
105 *****************************************************************************/
106 static int PSProbe( probedata_t *p_data )
108 input_thread_t * p_input = (input_thread_t *)p_data;
110 char * psz_name = p_input->p_source;
114 if( TestMethod( INPUT_METHOD_VAR, "ps" ) )
119 if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
121 /* If the user specified "file:" then it's probably a file */
126 i_handle = open( psz_name, 0 );
136 /*****************************************************************************
137 * PSInit: initializes PS structures
138 *****************************************************************************/
139 static void PSInit( input_thread_t * p_input )
141 thread_ps_data_t * p_method;
144 (thread_ps_data_t *)malloc( sizeof(thread_ps_data_t) )) == NULL )
146 intf_ErrMsg( "Out of memory" );
147 p_input->b_error = 1;
151 p_input->p_plugin_data = (void *)p_method;
152 p_input->p_method_data = NULL;
154 /* Re-open the socket as a buffered FILE stream */
155 if( (p_method->stream = fdopen( p_input->i_handle, "r" )) == NULL )
157 intf_ErrMsg( "Cannot open file (%s)", strerror(errno) );
158 p_input->b_error = 1;
161 rewind( p_method->stream );
163 /* FIXME : detect if InitStream failed */
164 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
165 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
167 if( p_input->stream.b_seekable )
169 stream_ps_data_t * p_demux_data =
170 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
172 /* Pre-parse the stream to gather stream_descriptor_t. */
173 p_input->stream.pp_programs[0]->b_is_ok = 0;
174 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
176 while( !p_input->b_die && !p_input->b_error
177 && !p_demux_data->b_has_PSM )
180 data_packet_t * pp_packets[INPUT_READ_ONCE];
182 i_result = PSRead( p_input, pp_packets );
186 vlc_mutex_lock( &p_input->stream.stream_lock );
187 p_input->stream.pp_programs[0]->b_is_ok = 1;
188 vlc_mutex_unlock( &p_input->stream.stream_lock );
193 p_input->b_error = 1;
197 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
199 /* FIXME: use i_p_config_t */
200 input_ParsePS( p_input, pp_packets[i] );
201 DeletePacket( p_input->p_method_data, pp_packets[i] );
205 if( p_input->stream.pp_areas[0]->i_tell > INPUT_PREPARSE_LENGTH )
210 rewind( p_method->stream );
211 vlc_mutex_lock( &p_input->stream.stream_lock );
212 p_input->stream.pp_areas[0]->i_tell = 0;
213 if( p_demux_data->b_has_PSM )
215 /* (The PSM decoder will care about spawning the decoders) */
216 p_input->stream.pp_programs[0]->b_is_ok = 1;
221 /* (We have to do it ourselves) */
224 /* FIXME: we should do multiple passes in case an audio type
227 i_es < p_input->stream.pp_programs[0]->i_es_number;
230 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
231 switch( p_es->i_type )
235 input_SelectES( p_input, p_es );
240 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
241 == (p_es->i_id & 0x1F) )
242 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
245 main_PutIntVariable( INPUT_AUDIO_VAR,
248 input_SelectES( p_input, p_es );
253 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
254 == ((p_es->i_id & 0xF00) >> 8) )
255 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
258 main_PutIntVariable( INPUT_AUDIO_VAR,
261 input_SelectES( p_input, p_es );
266 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
267 == ((p_es->i_id & 0x1F00) >> 8) )
269 input_SelectES( p_input, p_es );
282 input_DumpStream( p_input );
284 vlc_mutex_unlock( &p_input->stream.stream_lock );
288 /* The programs will be added when we read them. */
289 vlc_mutex_lock( &p_input->stream.stream_lock );
290 p_input->stream.pp_programs[0]->b_is_ok = 0;
291 vlc_mutex_unlock( &p_input->stream.stream_lock );
295 /*****************************************************************************
296 * PSEnd: frees unused data
297 *****************************************************************************/
298 static void PSEnd( input_thread_t * p_input )
300 free( p_input->stream.p_demux_data );
301 free( p_input->p_plugin_data );
304 /*****************************************************************************
305 * SafeRead: reads a chunk of stream and correctly detects errors
306 *****************************************************************************/
307 static __inline__ int SafeRead( input_thread_t * p_input, byte_t * p_buffer,
310 thread_ps_data_t * p_method;
313 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
314 while( fread( p_buffer, i_len, 1, p_method->stream ) != 1 )
316 if( feof( p_method->stream ) )
321 if( (i_error = ferror( p_method->stream )) )
323 intf_ErrMsg( "Read failed (%s)", strerror(i_error) );
327 vlc_mutex_lock( &p_input->stream.stream_lock );
328 p_input->stream.pp_areas[0]->i_tell += i_len;
329 vlc_mutex_unlock( &p_input->stream.stream_lock );
333 /*****************************************************************************
334 * PSRead: reads data packets
335 *****************************************************************************
336 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
338 *****************************************************************************/
339 static int PSRead( input_thread_t * p_input,
340 data_packet_t * pp_packets[INPUT_READ_ONCE] )
343 data_packet_t * p_data;
344 size_t i_packet_size;
345 int i_packet, i_error;
346 thread_ps_data_t * p_method;
348 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
350 memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
351 for( i_packet = 0; i_packet < INPUT_READ_ONCE; i_packet++ )
353 /* Read what we believe to be a packet header. */
354 if( (i_error = SafeRead( p_input, p_header, 6 )) )
359 if( (U32_AT(p_header) & 0xFFFFFF00) != 0x100L )
361 /* This is not the startcode of a packet. Read the stream
362 * until we find one. */
363 u32 i_startcode = U32_AT(p_header);
368 /* It is common for MPEG-1 streams to pad with zeros
369 * (although it is forbidden by the recommendation), so
370 * don't bother everybody in this case. */
371 intf_WarnMsg( 1, "Garbage at input (%x)", i_startcode );
374 while( (i_startcode & 0xFFFFFF00) != 0x100L )
377 if( (i_dummy = getc( p_method->stream )) != EOF )
379 i_startcode |= i_dummy;
387 *(u32 *)p_header = U32_AT(&i_startcode);
388 if( (i_error = SafeRead( p_input, p_header + 4, 2 )) )
394 if( U32_AT(p_header) != 0x1BA )
396 /* That's the case for all packets, except pack header. */
397 i_packet_size = U16_AT(&p_header[4]);
402 if( (p_header[4] & 0xC0) == 0x40 )
407 else if( (p_header[4] & 0xF0) == 0x20 )
414 intf_ErrMsg( "Unable to determine stream type" );
419 /* Fetch a packet of the appropriate size. */
420 if( (p_data = NewPacket( p_input, i_packet_size + 6 )) == NULL )
422 intf_ErrMsg( "Out of memory" );
426 /* Copy the header we already read. */
427 memcpy( p_data->p_buffer, p_header, 6 );
429 /* Read the remaining of the packet. */
430 if( i_packet_size && (i_error =
431 SafeRead( p_input, p_data->p_buffer + 6, i_packet_size )) )
436 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
437 if( U32_AT(p_header) == 0x1BA )
439 if( i_packet_size == 8 && (p_data->p_buffer[13] & 0x7) != 0 )
441 /* MPEG-2 stuffing bytes */
443 if( (i_error = SafeRead( p_input, p_garbage,
444 p_data->p_buffer[13] & 0x7)) )
451 /* Give the packet to the other input stages. */
452 pp_packets[i_packet] = p_data;
458 /*****************************************************************************
459 * PSSeek: changes the stream position indicator
460 *****************************************************************************/
461 static void PSSeek( input_thread_t * p_input, off_t i_position )
463 thread_ps_data_t * p_method;
465 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
467 /* A little bourrin but should work for a while --Meuuh */
468 fseeko( p_method->stream, i_position, SEEK_SET );
470 p_input->stream.pp_areas[0]->i_tell = i_position;
474 * Packet management utilities
477 /*****************************************************************************
478 * NewPacket: allocates a data packet
479 *****************************************************************************/
480 static struct data_packet_s * NewPacket( void * p_garbage,
483 data_packet_t * p_data;
486 if( i_size > INPUT_MAX_PACKET_SIZE )
488 intf_ErrMsg( "Packet too big (%d)", i_size );
492 if( (p_data = (data_packet_t *)malloc( sizeof(data_packet_t) )) == NULL )
494 intf_DbgMsg( "Out of memory" );
498 if( (p_data->p_buffer = (byte_t *)malloc( i_size )) == NULL )
500 intf_DbgMsg( "Out of memory" );
505 /* Initialize data */
506 p_data->p_next = NULL;
507 p_data->b_discard_payload = 0;
509 p_data->p_payload_start = p_data->p_buffer;
510 p_data->p_payload_end = p_data->p_buffer + i_size;
515 /*****************************************************************************
516 * NewPES: allocates a pes packet
517 *****************************************************************************/
518 static pes_packet_t * NewPES( void * p_garbage )
520 pes_packet_t * p_pes;
522 if( (p_pes = (pes_packet_t *)malloc( sizeof(pes_packet_t) )) == NULL )
524 intf_DbgMsg( "Out of memory" );
528 p_pes->b_data_alignment = p_pes->b_discontinuity =
529 p_pes->i_pts = p_pes->i_dts = 0;
530 p_pes->i_pes_size = 0;
531 p_pes->p_first = NULL;
536 /*****************************************************************************
537 * DeletePacket: deletes a data packet
538 *****************************************************************************/
539 static void DeletePacket( void * p_garbage,
540 data_packet_t * p_data )
543 ASSERT(p_data->p_buffer);
544 free( p_data->p_buffer );
548 /*****************************************************************************
549 * DeletePES: deletes a PES packet and associated data packets
550 *****************************************************************************/
551 static void DeletePES( void * p_garbage, pes_packet_t * p_pes )
553 data_packet_t * p_data;
554 data_packet_t * p_next;
556 p_data = p_pes->p_first;
558 while( p_data != NULL )
560 p_next = p_data->p_next;
561 free( p_data->p_buffer );