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.28 2001/06/03 12:47:21 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Cyril Deguet <asmax@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #define MODULE_NAME ps
26 #include "modules_inner.h"
28 /*****************************************************************************
30 *****************************************************************************/
37 #ifdef STRNCASECMP_IN_STRINGS_H
41 #include <sys/types.h>
46 #elif defined( _MSC_VER ) && defined( _WIN32 )
62 #include "stream_control.h"
63 #include "input_ext-intf.h"
64 #include "input_ext-dec.h"
69 #include "mpeg_system.h"
74 #include "modules_export.h"
76 /*****************************************************************************
78 *****************************************************************************/
79 static int PSProbe ( probedata_t * );
80 static int PSRead ( struct input_thread_s *,
81 data_packet_t * p_packets[INPUT_READ_ONCE] );
82 static void PSInit ( struct input_thread_s * );
83 static void PSEnd ( struct input_thread_s * );
84 static void PSSeek ( struct input_thread_s *, off_t );
85 static struct pes_packet_s * NewPES ( void * );
86 static struct data_packet_s * NewPacket ( void *, size_t );
87 static void DeletePacket( void *, struct data_packet_s * );
88 static void DeletePES ( void *, struct pes_packet_s * );
90 /*****************************************************************************
91 * Functions exported as capabilities. They are declared as static so that
92 * we don't pollute the namespace too much.
93 *****************************************************************************/
94 void _M( input_getfunctions )( function_list_t * p_function_list )
96 #define input p_function_list->functions.input
97 p_function_list->pf_probe = PSProbe;
98 input.pf_init = PSInit;
99 input.pf_open = NULL; /* Set in PSInit */
100 input.pf_close = NULL;
101 input.pf_end = PSEnd;
102 input.pf_set_area = NULL;
103 input.pf_read = PSRead;
104 input.pf_demux = input_DemuxPS;
105 input.pf_new_packet = NewPacket;
106 input.pf_new_pes = NewPES;
107 input.pf_delete_packet = DeletePacket;
108 input.pf_delete_pes = DeletePES;
109 input.pf_rewind = NULL;
110 input.pf_seek = PSSeek;
115 * Data reading functions
118 /*****************************************************************************
119 * PSProbe: verifies that the stream is a PS stream
120 *****************************************************************************/
121 static int PSProbe( probedata_t *p_data )
123 input_thread_t * p_input = (input_thread_t *)p_data;
125 char * psz_name = p_input->p_source;
129 if( TestMethod( INPUT_METHOD_VAR, "ps" ) )
134 if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
136 /* If the user specified "file:" then it's probably a file */
141 i_handle = open( psz_name, 0 );
151 /*****************************************************************************
152 * PSInit: initializes PS structures
153 *****************************************************************************/
154 static void PSInit( input_thread_t * p_input )
156 thread_ps_data_t * p_method;
157 packet_cache_t * p_packet_cache;
159 p_method = (thread_ps_data_t *)malloc( sizeof(thread_ps_data_t) );
160 if( p_method == NULL )
162 intf_ErrMsg( "Out of memory" );
163 p_input->b_error = 1;
166 p_input->p_plugin_data = (void *)p_method;
168 /* creates the packet cache structure */
169 p_packet_cache = malloc( sizeof(packet_cache_t) );
170 if ( p_packet_cache == NULL )
172 intf_ErrMsg( "Out of memory" );
173 p_input->b_error = 1;
176 p_input->p_method_data = (void *)p_packet_cache;
179 p_input->pf_open = p_input->pf_file_open;
180 p_input->pf_close = p_input->pf_file_close;
182 /* Initialize packet cache mutex */
183 vlc_mutex_init( &p_packet_cache->lock );
185 /* allocates the data cache */
186 p_packet_cache->data.p_stack = malloc( DATA_CACHE_SIZE *
187 sizeof(data_packet_t*) );
188 if ( p_packet_cache->data.p_stack == NULL )
190 intf_ErrMsg( "Out of memory" );
191 p_input->b_error = 1;
194 p_packet_cache->data.l_index = 0;
196 /* allocates the PES cache */
197 p_packet_cache->pes.p_stack = malloc( PES_CACHE_SIZE *
198 sizeof(pes_packet_t*) );
199 if ( p_packet_cache->pes.p_stack == NULL )
201 intf_ErrMsg( "Out of memory" );
202 p_input->b_error = 1;
205 p_packet_cache->pes.l_index = 0;
207 /* allocates the small buffer cache */
208 p_packet_cache->smallbuffer.p_stack = malloc( SMALL_CACHE_SIZE *
209 sizeof(packet_buffer_t) );
210 if ( p_packet_cache->smallbuffer.p_stack == NULL )
212 intf_ErrMsg( "Out of memory" );
213 p_input->b_error = 1;
216 p_packet_cache->smallbuffer.l_index = 0;
218 /* allocates the large buffer cache */
219 p_packet_cache->largebuffer.p_stack = malloc( LARGE_CACHE_SIZE *
220 sizeof(packet_buffer_t) );
221 if ( p_packet_cache->largebuffer.p_stack == NULL )
223 intf_ErrMsg( "Out of memory" );
224 p_input->b_error = 1;
227 p_packet_cache->largebuffer.l_index = 0;
229 /* Re-open the socket as a buffered FILE stream */
230 p_method->stream = fdopen( p_input->i_handle, "r" );
232 if( p_method->stream == NULL )
234 intf_ErrMsg( "Cannot open file (%s)", strerror(errno) );
235 p_input->b_error = 1;
238 rewind( p_method->stream );
240 /* FIXME : detect if InitStream failed */
241 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
242 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
244 if( p_input->stream.b_seekable )
246 stream_ps_data_t * p_demux_data =
247 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
249 /* Pre-parse the stream to gather stream_descriptor_t. */
250 p_input->stream.pp_programs[0]->b_is_ok = 0;
251 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
253 while( !p_input->b_die && !p_input->b_error
254 && !p_demux_data->b_has_PSM )
257 data_packet_t * pp_packets[INPUT_READ_ONCE];
259 i_result = PSRead( p_input, pp_packets );
263 vlc_mutex_lock( &p_input->stream.stream_lock );
264 p_input->stream.pp_programs[0]->b_is_ok = 1;
265 vlc_mutex_unlock( &p_input->stream.stream_lock );
270 p_input->b_error = 1;
274 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
276 /* FIXME: use i_p_config_t */
277 input_ParsePS( p_input, pp_packets[i] );
278 DeletePacket( p_input->p_method_data, pp_packets[i] );
282 if( p_input->stream.p_selected_area->i_tell >
283 INPUT_PREPARSE_LENGTH )
288 rewind( p_method->stream );
289 vlc_mutex_lock( &p_input->stream.stream_lock );
291 p_input->stream.i_method = INPUT_METHOD_FILE;
292 p_input->stream.p_selected_area->i_tell = 0;
294 if( p_demux_data->b_has_PSM )
296 /* (The PSM decoder will care about spawning the decoders) */
297 p_input->stream.pp_programs[0]->b_is_ok = 1;
302 /* (We have to do it ourselves) */
305 /* FIXME: we should do multiple passes in case an audio type
308 i_es < p_input->stream.pp_programs[0]->i_es_number;
311 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
312 switch( p_es->i_type )
316 input_SelectES( p_input, p_es );
321 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
322 == (p_es->i_id & 0x1F) )
323 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
326 main_PutIntVariable( INPUT_AUDIO_VAR,
329 input_SelectES( p_input, p_es );
334 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
335 == ((p_es->i_id & 0xF00) >> 8) )
336 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
339 main_PutIntVariable( INPUT_AUDIO_VAR,
342 input_SelectES( p_input, p_es );
347 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
348 == ((p_es->i_id & 0x1F00) >> 8) )
350 input_SelectES( p_input, p_es );
355 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
356 == ((p_es->i_id & 0x1F00) >> 8) )
357 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
360 main_PutIntVariable( INPUT_AUDIO_VAR,
363 input_SelectES( p_input, p_es );
372 input_DumpStream( p_input );
374 vlc_mutex_unlock( &p_input->stream.stream_lock );
378 /* The programs will be added when we read them. */
379 vlc_mutex_lock( &p_input->stream.stream_lock );
380 p_input->stream.i_method = INPUT_METHOD_FILE;
381 p_input->stream.pp_programs[0]->b_is_ok = 0;
382 vlc_mutex_unlock( &p_input->stream.stream_lock );
386 /*****************************************************************************
387 * PSEnd: frees unused data
388 *****************************************************************************/
389 static void PSEnd( input_thread_t * p_input )
391 vlc_mutex_destroy( &((packet_cache_t *)p_input->p_plugin_data)->lock );
392 free( p_input->p_plugin_data );
395 /*****************************************************************************
396 * SafeRead: reads a chunk of stream and correctly detects errors
397 *****************************************************************************/
398 static __inline__ int SafeRead( input_thread_t * p_input, byte_t * p_buffer,
401 thread_ps_data_t * p_method;
404 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
405 while( fread( p_buffer, i_len, 1, p_method->stream ) != 1 )
407 if( feof( p_method->stream ) )
412 if( (i_error = ferror( p_method->stream )) )
414 intf_ErrMsg( "Read failed (%s)", strerror(i_error) );
418 vlc_mutex_lock( &p_input->stream.stream_lock );
419 p_input->stream.p_selected_area->i_tell += i_len;
420 vlc_mutex_unlock( &p_input->stream.stream_lock );
424 /*****************************************************************************
425 * PSRead: reads data packets
426 *****************************************************************************
427 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
429 *****************************************************************************/
430 static int PSRead( input_thread_t * p_input,
431 data_packet_t * pp_packets[INPUT_READ_ONCE] )
434 data_packet_t * p_data;
435 size_t i_packet_size;
436 int i_packet, i_error;
437 thread_ps_data_t * p_method;
439 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
441 memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
442 for( i_packet = 0; i_packet < INPUT_READ_ONCE; i_packet++ )
444 /* Read what we believe to be a packet header. */
445 if( (i_error = SafeRead( p_input, p_header, 6 )) )
450 if( (U32_AT(p_header) & 0xFFFFFF00) != 0x100L )
452 /* This is not the startcode of a packet. Read the stream
453 * until we find one. */
454 u32 i_startcode = U32_AT(p_header);
459 /* It is common for MPEG-1 streams to pad with zeros
460 * (although it is forbidden by the recommendation), so
461 * don't bother everybody in this case. */
462 intf_WarnMsg( 3, "Garbage at input (%.8x)", i_startcode );
465 while( (i_startcode & 0xFFFFFF00) != 0x100L )
468 if( (i_dummy = getc( p_method->stream )) != EOF )
470 i_startcode |= i_dummy;
478 *(u32 *)p_header = U32_AT(&i_startcode);
479 if( (i_error = SafeRead( p_input, p_header + 4, 2 )) )
485 if( U32_AT(p_header) != 0x1BA )
487 /* That's the case for all packets, except pack header. */
488 i_packet_size = U16_AT(&p_header[4]);
493 if( (p_header[4] & 0xC0) == 0x40 )
498 else if( (p_header[4] & 0xF0) == 0x20 )
505 intf_ErrMsg( "Unable to determine stream type" );
510 /* Fetch a packet of the appropriate size. */
511 p_data = NewPacket( p_input->p_method_data, i_packet_size + 6 );
514 intf_ErrMsg( "Out of memory" );
518 /* Copy the header we already read. */
519 memcpy( p_data->p_buffer, p_header, 6 );
521 /* Read the remaining of the packet. */
522 if( i_packet_size && (i_error =
523 SafeRead( p_input, p_data->p_buffer + 6, i_packet_size )) )
528 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
529 if( U32_AT(p_header) == 0x1BA )
531 if( i_packet_size == 8 && (p_data->p_buffer[13] & 0x7) != 0 )
533 /* MPEG-2 stuffing bytes */
535 if( (i_error = SafeRead( p_input, p_garbage,
536 p_data->p_buffer[13] & 0x7)) )
543 /* Give the packet to the other input stages. */
544 pp_packets[i_packet] = p_data;
550 /*****************************************************************************
551 * PSSeek: changes the stream position indicator
552 *****************************************************************************/
553 static void PSSeek( input_thread_t * p_input, off_t i_position )
555 thread_ps_data_t * p_method;
557 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
559 /* A little bourrin but should work for a while --Meuuh */
561 fseeko( p_method->stream, i_position, SEEK_SET );
563 fseek( p_method->stream, (long)i_position, SEEK_SET );
566 p_input->stream.p_selected_area->i_tell = i_position;
570 * Packet management utilities
574 /*****************************************************************************
575 * NewPacket: allocates a data packet
576 *****************************************************************************/
577 static struct data_packet_s * NewPacket( void * p_packet_cache,
580 packet_cache_t * p_cache;
581 data_packet_t * p_data;
584 p_cache = (packet_cache_t *)p_packet_cache;
587 if ( p_cache == NULL )
589 intf_ErrMsg( "PPacket cache not initialized" );
595 if( l_size > INPUT_MAX_PACKET_SIZE )
597 intf_ErrMsg( "Packet too big (%d)", l_size );
601 vlc_mutex_lock( &p_cache->lock );
603 /* Checks whether the data cache is empty */
604 if( p_cache->data.l_index == 0 )
606 /* Allocates a new packet */
607 p_data = malloc( sizeof(data_packet_t) );
610 intf_ErrMsg( "Out of memory" );
611 vlc_mutex_unlock( &p_cache->lock );
615 intf_DbgMsg( "PS input: data packet allocated" );
620 /* Takes the packet out from the cache */
621 if( (p_data = p_cache->data.p_stack[ -- p_cache->data.l_index ])
624 intf_ErrMsg( "NULL packet in the data cache" );
625 vlc_mutex_unlock( &p_cache->lock );
630 if( l_size < MAX_SMALL_SIZE )
634 /* Checks whether the buffer cache is empty */
635 if( p_cache->smallbuffer.l_index == 0 )
637 /* Allocates a new packet */
638 p_data->p_buffer = malloc( l_size );
639 if( p_data->p_buffer == NULL )
641 intf_DbgMsg( "Out of memory" );
643 vlc_mutex_unlock( &p_cache->lock );
647 intf_DbgMsg( "PS input: small buffer allocated" );
649 p_data->l_size = l_size;
653 /* Takes the packet out from the cache */
654 l_index = -- p_cache->smallbuffer.l_index;
655 if( (p_data->p_buffer = p_cache->smallbuffer.p_stack[l_index].p_data)
658 intf_ErrMsg( "NULL packet in the small buffer cache" );
660 vlc_mutex_unlock( &p_cache->lock );
663 /* Reallocates the packet if it is too small or too large */
664 if( p_cache->smallbuffer.p_stack[l_index].l_size < l_size ||
665 p_cache->smallbuffer.p_stack[l_index].l_size > 2*l_size )
667 p_data->p_buffer = realloc( p_data->p_buffer, l_size );
668 p_data->l_size = l_size;
672 p_data->l_size = p_cache->smallbuffer.p_stack[l_index].l_size;
680 /* Checks whether the buffer cache is empty */
681 if( p_cache->largebuffer.l_index == 0 )
683 /* Allocates a new packet */
684 p_data->p_buffer = malloc( l_size );
685 if ( p_data->p_buffer == NULL )
687 intf_ErrMsg( "Out of memory" );
689 vlc_mutex_unlock( &p_cache->lock );
693 intf_DbgMsg( "PS input: large buffer allocated" );
695 p_data->l_size = l_size;
699 /* Takes the packet out from the cache */
700 l_index = -- p_cache->largebuffer.l_index;
701 p_data->p_buffer = p_cache->largebuffer.p_stack[l_index].p_data;
702 if( p_data->p_buffer == NULL )
704 intf_ErrMsg( "NULL packet in the small buffer cache" );
706 vlc_mutex_unlock( &p_cache->lock );
709 /* Reallocates the packet if it is too small or too large */
710 if( p_cache->largebuffer.p_stack[l_index].l_size < l_size ||
711 p_cache->largebuffer.p_stack[l_index].l_size > 2*l_size )
713 p_data->p_buffer = realloc( p_data->p_buffer, l_size );
714 p_data->l_size = l_size;
718 p_data->l_size = p_cache->largebuffer.p_stack[l_index].l_size;
723 vlc_mutex_unlock( &p_cache->lock );
725 /* Initialize data */
726 p_data->p_next = NULL;
727 p_data->b_discard_payload = 0;
728 p_data->p_payload_start = p_data->p_buffer;
729 p_data->p_payload_end = p_data->p_buffer + l_size;
736 /*****************************************************************************
737 * NewPES: allocates a pes packet
738 *****************************************************************************/
739 static pes_packet_t * NewPES( void * p_packet_cache )
741 packet_cache_t * p_cache;
742 pes_packet_t * p_pes;
744 p_cache = (packet_cache_t *)p_packet_cache;
747 if ( p_cache == NULL )
749 intf_ErrMsg( "Packet cache not initialized" );
754 vlc_mutex_lock( &p_cache->lock );
756 /* Checks whether the PES cache is empty */
757 if( p_cache->pes.l_index == 0 )
759 /* Allocates a new packet */
760 p_pes = malloc( sizeof(pes_packet_t) );
763 intf_DbgMsg( "Out of memory" );
764 vlc_mutex_unlock( &p_cache->lock );
768 intf_DbgMsg( "PS input: PES packet allocated" );
773 /* Takes the packet out from the cache */
774 p_pes = p_cache->pes.p_stack[ -- p_cache->pes.l_index ];
777 intf_ErrMsg( "NULL packet in the data cache" );
778 vlc_mutex_unlock( &p_cache->lock );
783 vlc_mutex_unlock( &p_cache->lock );
785 p_pes->b_data_alignment = p_pes->b_discontinuity =
786 p_pes->i_pts = p_pes->i_dts = 0;
787 p_pes->i_pes_size = 0;
788 p_pes->p_first = NULL;
794 /*****************************************************************************
795 * DeletePacket: deletes a data packet
796 *****************************************************************************/
797 static void DeletePacket( void * p_packet_cache,
798 data_packet_t * p_data )
800 packet_cache_t * p_cache;
802 p_cache = (packet_cache_t *)p_packet_cache;
805 if ( p_cache == NULL )
807 intf_ErrMsg( "Packet cache not initialized" );
814 vlc_mutex_lock( &p_cache->lock );
816 /* Checks whether the data cache is full */
817 if ( p_cache->data.l_index < DATA_CACHE_SIZE )
819 /* Cache not full: store the packet in it */
820 p_cache->data.p_stack[ p_cache->data.l_index ++ ] = p_data;
821 /* Small buffer or large buffer? */
822 if ( p_data->l_size < MAX_SMALL_SIZE )
824 /* Checks whether the small buffer cache is full */
825 if ( p_cache->smallbuffer.l_index < SMALL_CACHE_SIZE )
827 p_cache->smallbuffer.p_stack[
828 p_cache->smallbuffer.l_index ].l_size = p_data->l_size;
829 p_cache->smallbuffer.p_stack[
830 p_cache->smallbuffer.l_index++ ].p_data = p_data->p_buffer;
834 ASSERT( p_data->p_buffer );
835 free( p_data->p_buffer );
837 intf_DbgMsg( "PS input: small buffer freed" );
843 /* Checks whether the large buffer cache is full */
844 if ( p_cache->largebuffer.l_index < LARGE_CACHE_SIZE )
846 p_cache->largebuffer.p_stack[
847 p_cache->largebuffer.l_index ].l_size = p_data->l_size;
848 p_cache->largebuffer.p_stack[
849 p_cache->largebuffer.l_index++ ].p_data = p_data->p_buffer;
853 ASSERT( p_data->p_buffer );
854 free( p_data->p_buffer );
856 intf_DbgMsg( "PS input: large buffer freed" );
863 /* Cache full: the packet must be freed */
864 free( p_data->p_buffer );
867 intf_DbgMsg( "PS input: data packet freed" );
871 vlc_mutex_unlock( &p_cache->lock );
874 /*****************************************************************************
875 * DeletePES: deletes a PES packet and associated data packets
876 *****************************************************************************/
877 static void DeletePES( void * p_packet_cache, pes_packet_t * p_pes )
879 packet_cache_t * p_cache;
880 data_packet_t * p_data;
881 data_packet_t * p_next;
883 p_cache = (packet_cache_t *)p_packet_cache;
886 if ( p_cache == NULL )
888 intf_ErrMsg( "Packet cache not initialized" );
895 p_data = p_pes->p_first;
897 while( p_data != NULL )
899 p_next = p_data->p_next;
900 DeletePacket( p_cache, p_data );
904 vlc_mutex_lock( &p_cache->lock );
906 /* Checks whether the PES cache is full */
907 if ( p_cache->pes.l_index < PES_CACHE_SIZE )
909 /* Cache not full: store the packet in it */
910 p_cache->pes.p_stack[ p_cache->pes.l_index ++ ] = p_pes;
914 /* Cache full: the packet must be freed */
917 intf_DbgMsg( "PS input: PES packet freed" );
921 vlc_mutex_unlock( &p_cache->lock );