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.26 2001/05/31 01:37:08 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 *****************************************************************************/
35 #ifdef STRNCASECMP_IN_STRINGS_H
40 #include <sys/types.h>
45 #elif defined( _MSC_VER ) && defined( _WIN32 )
61 #include "stream_control.h"
62 #include "input_ext-intf.h"
63 #include "input_ext-dec.h"
68 #include "mpeg_system.h"
74 /*****************************************************************************
76 *****************************************************************************/
77 static int PSProbe ( probedata_t * );
78 static int PSRead ( struct input_thread_s *,
79 data_packet_t * p_packets[INPUT_READ_ONCE] );
80 static void PSInit ( struct input_thread_s * );
81 static void PSEnd ( struct input_thread_s * );
82 static void PSSeek ( struct input_thread_s *, off_t );
83 static struct pes_packet_s * NewPES ( void * );
84 static struct data_packet_s * NewPacket ( void *, size_t );
85 static void DeletePacket( void *, struct data_packet_s * );
86 static void DeletePES ( void *, struct pes_packet_s * );
88 /*****************************************************************************
89 * Functions exported as capabilities. They are declared as static so that
90 * we don't pollute the namespace too much.
91 *****************************************************************************/
92 void _M( input_getfunctions )( function_list_t * p_function_list )
94 #define input p_function_list->functions.input
95 p_function_list->pf_probe = PSProbe;
96 input.pf_init = PSInit;
97 input.pf_open = NULL; /* Set in PSInit */
98 input.pf_close = NULL;
100 input.pf_set_area = NULL;
101 input.pf_read = PSRead;
102 input.pf_demux = input_DemuxPS;
103 input.pf_new_packet = NewPacket;
104 input.pf_new_pes = NewPES;
105 input.pf_delete_packet = DeletePacket;
106 input.pf_delete_pes = DeletePES;
107 input.pf_rewind = NULL;
108 input.pf_seek = PSSeek;
113 * Data reading functions
116 /*****************************************************************************
117 * PSProbe: verifies that the stream is a PS stream
118 *****************************************************************************/
119 static int PSProbe( probedata_t *p_data )
121 input_thread_t * p_input = (input_thread_t *)p_data;
123 char * psz_name = p_input->p_source;
127 if( TestMethod( INPUT_METHOD_VAR, "ps" ) )
132 if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
134 /* If the user specified "file:" then it's probably a file */
139 i_handle = open( psz_name, 0 );
149 /*****************************************************************************
150 * PSInit: initializes PS structures
151 *****************************************************************************/
152 static void PSInit( input_thread_t * p_input )
154 thread_ps_data_t * p_method;
155 packet_cache_t * p_packet_cache;
158 (thread_ps_data_t *)malloc( sizeof(thread_ps_data_t) )) == NULL )
160 intf_ErrMsg( "Out of memory" );
161 p_input->b_error = 1;
164 p_input->p_plugin_data = (void *)p_method;
166 /* creates the packet cache structure */
167 p_packet_cache = malloc( sizeof(packet_cache_t) );
168 if ( p_packet_cache == NULL )
170 intf_ErrMsg( "Out of memory" );
171 p_input->b_error = 1;
174 p_input->p_method_data = (void *)p_packet_cache;
177 p_input->pf_open = p_input->pf_file_open;
178 p_input->pf_close = p_input->pf_file_close;
180 /* Initialize packet cache mutex */
181 vlc_mutex_init( &p_packet_cache->lock );
183 /* allocates the data cache */
184 p_packet_cache->data.p_stack = malloc( DATA_CACHE_SIZE *
185 sizeof(data_packet_t*) );
186 if ( p_packet_cache->data.p_stack == NULL )
188 intf_ErrMsg( "Out of memory" );
189 p_input->b_error = 1;
192 p_packet_cache->data.l_index = 0;
194 /* allocates the PES cache */
195 p_packet_cache->pes.p_stack = malloc( PES_CACHE_SIZE *
196 sizeof(pes_packet_t*) );
197 if ( p_packet_cache->pes.p_stack == NULL )
199 intf_ErrMsg( "Out of memory" );
200 p_input->b_error = 1;
203 p_packet_cache->pes.l_index = 0;
205 /* allocates the small buffer cache */
206 p_packet_cache->small.p_stack = malloc( SMALL_CACHE_SIZE *
207 sizeof(packet_buffer_t) );
208 if ( p_packet_cache->small.p_stack == NULL )
210 intf_ErrMsg( "Out of memory" );
211 p_input->b_error = 1;
214 p_packet_cache->small.l_index = 0;
216 /* allocates the large buffer cache */
217 p_packet_cache->large.p_stack = malloc( LARGE_CACHE_SIZE *
218 sizeof(packet_buffer_t) );
219 if ( p_packet_cache->large.p_stack == NULL )
221 intf_ErrMsg( "Out of memory" );
222 p_input->b_error = 1;
225 p_packet_cache->large.l_index = 0;
227 /* Re-open the socket as a buffered FILE stream */
228 if( (p_method->stream = fdopen( p_input->i_handle, "r" )) == NULL )
230 intf_ErrMsg( "Cannot open file (%s)", strerror(errno) );
231 p_input->b_error = 1;
234 rewind( p_method->stream );
236 /* FIXME : detect if InitStream failed */
237 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
238 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
240 if( p_input->stream.b_seekable )
242 stream_ps_data_t * p_demux_data =
243 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
245 /* Pre-parse the stream to gather stream_descriptor_t. */
246 p_input->stream.pp_programs[0]->b_is_ok = 0;
247 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
249 while( !p_input->b_die && !p_input->b_error
250 && !p_demux_data->b_has_PSM )
253 data_packet_t * pp_packets[INPUT_READ_ONCE];
255 i_result = PSRead( p_input, pp_packets );
259 vlc_mutex_lock( &p_input->stream.stream_lock );
260 p_input->stream.pp_programs[0]->b_is_ok = 1;
261 vlc_mutex_unlock( &p_input->stream.stream_lock );
266 p_input->b_error = 1;
270 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
272 /* FIXME: use i_p_config_t */
273 input_ParsePS( p_input, pp_packets[i] );
274 DeletePacket( p_input->p_method_data, pp_packets[i] );
278 if( p_input->stream.p_selected_area->i_tell >
279 INPUT_PREPARSE_LENGTH )
284 rewind( p_method->stream );
285 vlc_mutex_lock( &p_input->stream.stream_lock );
287 p_input->stream.i_method = INPUT_METHOD_FILE;
288 p_input->stream.p_selected_area->i_tell = 0;
290 if( p_demux_data->b_has_PSM )
292 /* (The PSM decoder will care about spawning the decoders) */
293 p_input->stream.pp_programs[0]->b_is_ok = 1;
298 /* (We have to do it ourselves) */
301 /* FIXME: we should do multiple passes in case an audio type
304 i_es < p_input->stream.pp_programs[0]->i_es_number;
307 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
308 switch( p_es->i_type )
312 input_SelectES( p_input, p_es );
317 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
318 == (p_es->i_id & 0x1F) )
319 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
322 main_PutIntVariable( INPUT_AUDIO_VAR,
325 input_SelectES( p_input, p_es );
330 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
331 == ((p_es->i_id & 0xF00) >> 8) )
332 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
335 main_PutIntVariable( INPUT_AUDIO_VAR,
338 input_SelectES( p_input, p_es );
343 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
344 == ((p_es->i_id & 0x1F00) >> 8) )
346 input_SelectES( p_input, p_es );
351 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
352 == ((p_es->i_id & 0x1F00) >> 8) )
353 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
356 main_PutIntVariable( INPUT_AUDIO_VAR,
359 input_SelectES( p_input, p_es );
368 input_DumpStream( p_input );
370 vlc_mutex_unlock( &p_input->stream.stream_lock );
374 /* The programs will be added when we read them. */
375 vlc_mutex_lock( &p_input->stream.stream_lock );
376 p_input->stream.i_method = INPUT_METHOD_FILE;
377 p_input->stream.pp_programs[0]->b_is_ok = 0;
378 vlc_mutex_unlock( &p_input->stream.stream_lock );
382 /*****************************************************************************
383 * PSEnd: frees unused data
384 *****************************************************************************/
385 static void PSEnd( input_thread_t * p_input )
387 vlc_mutex_destroy( &((packet_cache_t *)p_input->p_plugin_data)->lock );
388 free( p_input->p_plugin_data );
391 /*****************************************************************************
392 * SafeRead: reads a chunk of stream and correctly detects errors
393 *****************************************************************************/
394 static __inline__ int SafeRead( input_thread_t * p_input, byte_t * p_buffer,
397 thread_ps_data_t * p_method;
400 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
401 while( fread( p_buffer, i_len, 1, p_method->stream ) != 1 )
403 if( feof( p_method->stream ) )
408 if( (i_error = ferror( p_method->stream )) )
410 intf_ErrMsg( "Read failed (%s)", strerror(i_error) );
414 vlc_mutex_lock( &p_input->stream.stream_lock );
415 p_input->stream.p_selected_area->i_tell += i_len;
416 vlc_mutex_unlock( &p_input->stream.stream_lock );
420 /*****************************************************************************
421 * PSRead: reads data packets
422 *****************************************************************************
423 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
425 *****************************************************************************/
426 static int PSRead( input_thread_t * p_input,
427 data_packet_t * pp_packets[INPUT_READ_ONCE] )
430 data_packet_t * p_data;
431 size_t i_packet_size;
432 int i_packet, i_error;
433 thread_ps_data_t * p_method;
435 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
437 memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
438 for( i_packet = 0; i_packet < INPUT_READ_ONCE; i_packet++ )
440 /* Read what we believe to be a packet header. */
441 if( (i_error = SafeRead( p_input, p_header, 6 )) )
446 if( (U32_AT(p_header) & 0xFFFFFF00) != 0x100L )
448 /* This is not the startcode of a packet. Read the stream
449 * until we find one. */
450 u32 i_startcode = U32_AT(p_header);
455 /* It is common for MPEG-1 streams to pad with zeros
456 * (although it is forbidden by the recommendation), so
457 * don't bother everybody in this case. */
458 intf_WarnMsg( 3, "Garbage at input (%.8x)", i_startcode );
461 while( (i_startcode & 0xFFFFFF00) != 0x100L )
464 if( (i_dummy = getc( p_method->stream )) != EOF )
466 i_startcode |= i_dummy;
474 *(u32 *)p_header = U32_AT(&i_startcode);
475 if( (i_error = SafeRead( p_input, p_header + 4, 2 )) )
481 if( U32_AT(p_header) != 0x1BA )
483 /* That's the case for all packets, except pack header. */
484 i_packet_size = U16_AT(&p_header[4]);
489 if( (p_header[4] & 0xC0) == 0x40 )
494 else if( (p_header[4] & 0xF0) == 0x20 )
501 intf_ErrMsg( "Unable to determine stream type" );
506 /* Fetch a packet of the appropriate size. */
507 if( (p_data = NewPacket( p_input->p_method_data, i_packet_size + 6 ))
510 intf_ErrMsg( "Out of memory" );
514 /* Copy the header we already read. */
515 memcpy( p_data->p_buffer, p_header, 6 );
517 /* Read the remaining of the packet. */
518 if( i_packet_size && (i_error =
519 SafeRead( p_input, p_data->p_buffer + 6, i_packet_size )) )
524 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
525 if( U32_AT(p_header) == 0x1BA )
527 if( i_packet_size == 8 && (p_data->p_buffer[13] & 0x7) != 0 )
529 /* MPEG-2 stuffing bytes */
531 if( (i_error = SafeRead( p_input, p_garbage,
532 p_data->p_buffer[13] & 0x7)) )
539 /* Give the packet to the other input stages. */
540 pp_packets[i_packet] = p_data;
546 /*****************************************************************************
547 * PSSeek: changes the stream position indicator
548 *****************************************************************************/
549 static void PSSeek( input_thread_t * p_input, off_t i_position )
551 thread_ps_data_t * p_method;
553 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
555 /* A little bourrin but should work for a while --Meuuh */
557 fseeko( p_method->stream, i_position, SEEK_SET );
559 fseek( p_method->stream, (long)i_position, SEEK_SET );
562 p_input->stream.p_selected_area->i_tell = i_position;
566 * Packet management utilities
570 /*****************************************************************************
571 * NewPacket: allocates a data packet
572 *****************************************************************************/
573 static struct data_packet_s * NewPacket( void * p_packet_cache,
576 packet_cache_t * p_cache;
577 data_packet_t * p_data;
580 p_cache = (packet_cache_t *)p_packet_cache;
583 if ( p_cache == NULL )
585 intf_ErrMsg( "PPacket cache not initialized" );
591 if( l_size > INPUT_MAX_PACKET_SIZE )
593 intf_ErrMsg( "Packet too big (%d)", l_size );
597 vlc_mutex_lock( &p_cache->lock );
599 /* Checks whether the data cache is empty */
600 if( p_cache->data.l_index == 0 )
602 /* Allocates a new packet */
603 if ( (p_data = malloc( sizeof(data_packet_t) )) == NULL )
605 intf_ErrMsg( "Out of memory" );
606 vlc_mutex_unlock( &p_cache->lock );
610 intf_DbgMsg( "PS input: data packet allocated" );
615 /* Takes the packet out from the cache */
616 if( (p_data = p_cache->data.p_stack[ -- p_cache->data.l_index ])
619 intf_ErrMsg( "NULL packet in the data cache" );
620 vlc_mutex_unlock( &p_cache->lock );
625 if( l_size < MAX_SMALL_SIZE )
629 /* Checks whether the buffer cache is empty */
630 if( p_cache->small.l_index == 0 )
632 /* Allocates a new packet */
633 if ( (p_data->p_buffer = malloc( l_size )) == NULL )
635 intf_DbgMsg( "Out of memory" );
637 vlc_mutex_unlock( &p_cache->lock );
641 intf_DbgMsg( "PS input: small buffer allocated" );
643 p_data->l_size = l_size;
647 /* Takes the packet out from the cache */
648 l_index = -- p_cache->small.l_index;
649 if( (p_data->p_buffer = p_cache->small.p_stack[l_index].p_data)
652 intf_ErrMsg( "NULL packet in the small buffer cache" );
654 vlc_mutex_unlock( &p_cache->lock );
657 /* Reallocates the packet if it is too small or too large */
658 if( p_cache->small.p_stack[l_index].l_size < l_size ||
659 p_cache->small.p_stack[l_index].l_size > 2*l_size )
661 p_data->p_buffer = realloc( p_data->p_buffer, l_size );
662 p_data->l_size = l_size;
666 p_data->l_size = p_cache->small.p_stack[l_index].l_size;
674 /* Checks whether the buffer cache is empty */
675 if( p_cache->large.l_index == 0 )
677 /* Allocates a new packet */
678 if ( (p_data->p_buffer = malloc( l_size )) == NULL )
680 intf_ErrMsg( "Out of memory" );
682 vlc_mutex_unlock( &p_cache->lock );
686 intf_DbgMsg( "PS input: large buffer allocated" );
688 p_data->l_size = l_size;
692 /* Takes the packet out from the cache */
693 l_index = -- p_cache->large.l_index;
694 if( (p_data->p_buffer = p_cache->large.p_stack[l_index].p_data)
697 intf_ErrMsg( "NULL packet in the small buffer cache" );
699 vlc_mutex_unlock( &p_cache->lock );
702 /* Reallocates the packet if it is too small or too large */
703 if( p_cache->large.p_stack[l_index].l_size < l_size ||
704 p_cache->large.p_stack[l_index].l_size > 2*l_size )
706 p_data->p_buffer = realloc( p_data->p_buffer, l_size );
707 p_data->l_size = l_size;
711 p_data->l_size = p_cache->large.p_stack[l_index].l_size;
716 vlc_mutex_unlock( &p_cache->lock );
718 /* Initialize data */
719 p_data->p_next = NULL;
720 p_data->b_discard_payload = 0;
721 p_data->p_payload_start = p_data->p_buffer;
722 p_data->p_payload_end = p_data->p_buffer + l_size;
729 /*****************************************************************************
730 * NewPES: allocates a pes packet
731 *****************************************************************************/
732 static pes_packet_t * NewPES( void * p_packet_cache )
734 packet_cache_t * p_cache;
735 pes_packet_t * p_pes;
737 p_cache = (packet_cache_t *)p_packet_cache;
740 if ( p_cache == NULL )
742 intf_ErrMsg( "Packet cache not initialized" );
747 vlc_mutex_lock( &p_cache->lock );
749 /* Checks whether the PES cache is empty */
750 if( p_cache->pes.l_index == 0 )
752 /* Allocates a new packet */
753 if ( (p_pes = malloc( sizeof(pes_packet_t) )) == NULL )
755 intf_DbgMsg( "Out of memory" );
756 vlc_mutex_unlock( &p_cache->lock );
760 intf_DbgMsg( "PS input: PES packet allocated" );
765 /* Takes the packet out from the cache */
766 if( (p_pes = p_cache->pes.p_stack[ -- p_cache->pes.l_index ])
769 intf_ErrMsg( "NULL packet in the data cache" );
770 vlc_mutex_unlock( &p_cache->lock );
775 vlc_mutex_unlock( &p_cache->lock );
777 p_pes->b_data_alignment = p_pes->b_discontinuity =
778 p_pes->i_pts = p_pes->i_dts = 0;
779 p_pes->i_pes_size = 0;
780 p_pes->p_first = NULL;
786 /*****************************************************************************
787 * DeletePacket: deletes a data packet
788 *****************************************************************************/
789 static void DeletePacket( void * p_packet_cache,
790 data_packet_t * p_data )
792 packet_cache_t * p_cache;
794 p_cache = (packet_cache_t *)p_packet_cache;
797 if ( p_cache == NULL )
799 intf_ErrMsg( "Packet cache not initialized" );
806 vlc_mutex_lock( &p_cache->lock );
808 /* Checks whether the data cache is full */
809 if ( p_cache->data.l_index < DATA_CACHE_SIZE )
811 /* Cache not full: store the packet in it */
812 p_cache->data.p_stack[ p_cache->data.l_index ++ ] = p_data;
813 /* Small buffer or large buffer? */
814 if ( p_data->l_size < MAX_SMALL_SIZE )
816 /* Checks whether the small buffer cache is full */
817 if ( p_cache->small.l_index < SMALL_CACHE_SIZE )
819 p_cache->small.p_stack[ p_cache->small.l_index ].l_size =
821 p_cache->small.p_stack[ p_cache->small.l_index ++ ].p_data =
826 ASSERT( p_data->p_buffer );
827 free( p_data->p_buffer );
829 intf_DbgMsg( "PS input: small buffer freed" );
835 /* Checks whether the large buffer cache is full */
836 if ( p_cache->large.l_index < LARGE_CACHE_SIZE )
838 p_cache->large.p_stack[ p_cache->large.l_index ].l_size =
840 p_cache->large.p_stack[ p_cache->large.l_index ++ ].p_data =
845 ASSERT( p_data->p_buffer );
846 free( p_data->p_buffer );
848 intf_DbgMsg( "PS input: large buffer freed" );
855 /* Cache full: the packet must be freed */
856 free( p_data->p_buffer );
859 intf_DbgMsg( "PS input: data packet freed" );
863 vlc_mutex_unlock( &p_cache->lock );
866 /*****************************************************************************
867 * DeletePES: deletes a PES packet and associated data packets
868 *****************************************************************************/
869 static void DeletePES( void * p_packet_cache, pes_packet_t * p_pes )
871 packet_cache_t * p_cache;
872 data_packet_t * p_data;
873 data_packet_t * p_next;
875 p_cache = (packet_cache_t *)p_packet_cache;
878 if ( p_cache == NULL )
880 intf_ErrMsg( "Packet cache not initialized" );
887 p_data = p_pes->p_first;
889 while( p_data != NULL )
891 p_next = p_data->p_next;
892 DeletePacket( p_cache, p_data );
896 vlc_mutex_lock( &p_cache->lock );
898 /* Checks whether the PES cache is full */
899 if ( p_cache->pes.l_index < PES_CACHE_SIZE )
901 /* Cache not full: store the packet in it */
902 p_cache->pes.p_stack[ p_cache->pes.l_index ++ ] = p_pes;
906 /* Cache full: the packet must be freed */
909 intf_DbgMsg( "PS input: PES packet freed" );
913 vlc_mutex_unlock( &p_cache->lock );