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.25 2001/05/30 17:03:12 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>
55 #include "stream_control.h"
56 #include "input_ext-intf.h"
57 #include "input_ext-dec.h"
62 #include "mpeg_system.h"
68 /*****************************************************************************
70 *****************************************************************************/
71 static int PSProbe ( probedata_t * );
72 static int PSRead ( struct input_thread_s *,
73 data_packet_t * p_packets[INPUT_READ_ONCE] );
74 static void PSInit ( struct input_thread_s * );
75 static void PSEnd ( struct input_thread_s * );
76 static void PSSeek ( struct input_thread_s *, off_t );
77 static struct pes_packet_s * NewPES ( void * );
78 static struct data_packet_s * NewPacket ( void *, size_t );
79 static void DeletePacket( void *, struct data_packet_s * );
80 static void DeletePES ( void *, struct pes_packet_s * );
82 /*****************************************************************************
83 * Functions exported as capabilities. They are declared as static so that
84 * we don't pollute the namespace too much.
85 *****************************************************************************/
86 void _M( input_getfunctions )( function_list_t * p_function_list )
88 #define input p_function_list->functions.input
89 p_function_list->pf_probe = PSProbe;
90 input.pf_init = PSInit;
91 input.pf_open = NULL; /* Set in PSInit */
92 input.pf_close = NULL;
94 input.pf_set_area = NULL;
95 input.pf_read = PSRead;
96 input.pf_demux = input_DemuxPS;
97 input.pf_new_packet = NewPacket;
98 input.pf_new_pes = NewPES;
99 input.pf_delete_packet = DeletePacket;
100 input.pf_delete_pes = DeletePES;
101 input.pf_rewind = NULL;
102 input.pf_seek = PSSeek;
107 * Data reading functions
110 /*****************************************************************************
111 * PSProbe: verifies that the stream is a PS stream
112 *****************************************************************************/
113 static int PSProbe( probedata_t *p_data )
115 input_thread_t * p_input = (input_thread_t *)p_data;
117 char * psz_name = p_input->p_source;
121 if( TestMethod( INPUT_METHOD_VAR, "ps" ) )
126 if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
128 /* If the user specified "file:" then it's probably a file */
133 i_handle = open( psz_name, 0 );
143 /*****************************************************************************
144 * PSInit: initializes PS structures
145 *****************************************************************************/
146 static void PSInit( input_thread_t * p_input )
148 thread_ps_data_t * p_method;
149 packet_cache_t * p_packet_cache;
152 (thread_ps_data_t *)malloc( sizeof(thread_ps_data_t) )) == NULL )
154 intf_ErrMsg( "Out of memory" );
155 p_input->b_error = 1;
158 p_input->p_plugin_data = (void *)p_method;
160 /* creates the packet cache structure */
161 p_packet_cache = malloc( sizeof(packet_cache_t) );
162 if ( p_packet_cache == NULL )
164 intf_ErrMsg( "Out of memory" );
165 p_input->b_error = 1;
168 p_input->p_method_data = (void *)p_packet_cache;
171 p_input->pf_open = p_input->pf_file_open;
172 p_input->pf_close = p_input->pf_file_close;
174 /* Initialize packet cache mutex */
175 vlc_mutex_init( &p_packet_cache->lock );
177 /* allocates the data cache */
178 p_packet_cache->data.p_stack = malloc( DATA_CACHE_SIZE *
179 sizeof(data_packet_t*) );
180 if ( p_packet_cache->data.p_stack == NULL )
182 intf_ErrMsg( "Out of memory" );
183 p_input->b_error = 1;
186 p_packet_cache->data.l_index = 0;
188 /* allocates the PES cache */
189 p_packet_cache->pes.p_stack = malloc( PES_CACHE_SIZE *
190 sizeof(pes_packet_t*) );
191 if ( p_packet_cache->pes.p_stack == NULL )
193 intf_ErrMsg( "Out of memory" );
194 p_input->b_error = 1;
197 p_packet_cache->pes.l_index = 0;
199 /* allocates the small buffer cache */
200 p_packet_cache->small.p_stack = malloc( SMALL_CACHE_SIZE *
201 sizeof(packet_buffer_t) );
202 if ( p_packet_cache->small.p_stack == NULL )
204 intf_ErrMsg( "Out of memory" );
205 p_input->b_error = 1;
208 p_packet_cache->small.l_index = 0;
210 /* allocates the large buffer cache */
211 p_packet_cache->large.p_stack = malloc( LARGE_CACHE_SIZE *
212 sizeof(packet_buffer_t) );
213 if ( p_packet_cache->large.p_stack == NULL )
215 intf_ErrMsg( "Out of memory" );
216 p_input->b_error = 1;
219 p_packet_cache->large.l_index = 0;
221 /* Re-open the socket as a buffered FILE stream */
222 if( (p_method->stream = fdopen( p_input->i_handle, "r" )) == NULL )
224 intf_ErrMsg( "Cannot open file (%s)", strerror(errno) );
225 p_input->b_error = 1;
228 rewind( p_method->stream );
230 /* FIXME : detect if InitStream failed */
231 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
232 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
234 if( p_input->stream.b_seekable )
236 stream_ps_data_t * p_demux_data =
237 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
239 /* Pre-parse the stream to gather stream_descriptor_t. */
240 p_input->stream.pp_programs[0]->b_is_ok = 0;
241 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
243 while( !p_input->b_die && !p_input->b_error
244 && !p_demux_data->b_has_PSM )
247 data_packet_t * pp_packets[INPUT_READ_ONCE];
249 i_result = PSRead( p_input, pp_packets );
253 vlc_mutex_lock( &p_input->stream.stream_lock );
254 p_input->stream.pp_programs[0]->b_is_ok = 1;
255 vlc_mutex_unlock( &p_input->stream.stream_lock );
260 p_input->b_error = 1;
264 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
266 /* FIXME: use i_p_config_t */
267 input_ParsePS( p_input, pp_packets[i] );
268 DeletePacket( p_input->p_method_data, pp_packets[i] );
272 if( p_input->stream.p_selected_area->i_tell >
273 INPUT_PREPARSE_LENGTH )
278 rewind( p_method->stream );
279 vlc_mutex_lock( &p_input->stream.stream_lock );
281 p_input->stream.i_method = INPUT_METHOD_FILE;
282 p_input->stream.p_selected_area->i_tell = 0;
284 if( p_demux_data->b_has_PSM )
286 /* (The PSM decoder will care about spawning the decoders) */
287 p_input->stream.pp_programs[0]->b_is_ok = 1;
292 /* (We have to do it ourselves) */
295 /* FIXME: we should do multiple passes in case an audio type
298 i_es < p_input->stream.pp_programs[0]->i_es_number;
301 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
302 switch( p_es->i_type )
306 input_SelectES( p_input, p_es );
311 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
312 == (p_es->i_id & 0x1F) )
313 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
316 main_PutIntVariable( INPUT_AUDIO_VAR,
319 input_SelectES( p_input, p_es );
324 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
325 == ((p_es->i_id & 0xF00) >> 8) )
326 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
329 main_PutIntVariable( INPUT_AUDIO_VAR,
332 input_SelectES( p_input, p_es );
337 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
338 == ((p_es->i_id & 0x1F00) >> 8) )
340 input_SelectES( p_input, p_es );
345 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
346 == ((p_es->i_id & 0x1F00) >> 8) )
347 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
350 main_PutIntVariable( INPUT_AUDIO_VAR,
353 input_SelectES( p_input, p_es );
362 input_DumpStream( p_input );
364 vlc_mutex_unlock( &p_input->stream.stream_lock );
368 /* The programs will be added when we read them. */
369 vlc_mutex_lock( &p_input->stream.stream_lock );
370 p_input->stream.i_method = INPUT_METHOD_FILE;
371 p_input->stream.pp_programs[0]->b_is_ok = 0;
372 vlc_mutex_unlock( &p_input->stream.stream_lock );
376 /*****************************************************************************
377 * PSEnd: frees unused data
378 *****************************************************************************/
379 static void PSEnd( input_thread_t * p_input )
381 vlc_mutex_destroy( &((packet_cache_t *)p_input->p_plugin_data)->lock );
382 free( p_input->p_plugin_data );
385 /*****************************************************************************
386 * SafeRead: reads a chunk of stream and correctly detects errors
387 *****************************************************************************/
388 static __inline__ int SafeRead( input_thread_t * p_input, byte_t * p_buffer,
391 thread_ps_data_t * p_method;
394 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
395 while( fread( p_buffer, i_len, 1, p_method->stream ) != 1 )
397 if( feof( p_method->stream ) )
402 if( (i_error = ferror( p_method->stream )) )
404 intf_ErrMsg( "Read failed (%s)", strerror(i_error) );
408 vlc_mutex_lock( &p_input->stream.stream_lock );
409 p_input->stream.p_selected_area->i_tell += i_len;
410 vlc_mutex_unlock( &p_input->stream.stream_lock );
414 /*****************************************************************************
415 * PSRead: reads data packets
416 *****************************************************************************
417 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
419 *****************************************************************************/
420 static int PSRead( input_thread_t * p_input,
421 data_packet_t * pp_packets[INPUT_READ_ONCE] )
424 data_packet_t * p_data;
425 size_t i_packet_size;
426 int i_packet, i_error;
427 thread_ps_data_t * p_method;
429 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
431 memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
432 for( i_packet = 0; i_packet < INPUT_READ_ONCE; i_packet++ )
434 /* Read what we believe to be a packet header. */
435 if( (i_error = SafeRead( p_input, p_header, 6 )) )
440 if( (U32_AT(p_header) & 0xFFFFFF00) != 0x100L )
442 /* This is not the startcode of a packet. Read the stream
443 * until we find one. */
444 u32 i_startcode = U32_AT(p_header);
449 /* It is common for MPEG-1 streams to pad with zeros
450 * (although it is forbidden by the recommendation), so
451 * don't bother everybody in this case. */
452 intf_WarnMsg( 3, "Garbage at input (%.8x)", i_startcode );
455 while( (i_startcode & 0xFFFFFF00) != 0x100L )
458 if( (i_dummy = getc( p_method->stream )) != EOF )
460 i_startcode |= i_dummy;
468 *(u32 *)p_header = U32_AT(&i_startcode);
469 if( (i_error = SafeRead( p_input, p_header + 4, 2 )) )
475 if( U32_AT(p_header) != 0x1BA )
477 /* That's the case for all packets, except pack header. */
478 i_packet_size = U16_AT(&p_header[4]);
483 if( (p_header[4] & 0xC0) == 0x40 )
488 else if( (p_header[4] & 0xF0) == 0x20 )
495 intf_ErrMsg( "Unable to determine stream type" );
500 /* Fetch a packet of the appropriate size. */
501 if( (p_data = NewPacket( p_input->p_method_data, i_packet_size + 6 ))
504 intf_ErrMsg( "Out of memory" );
508 /* Copy the header we already read. */
509 memcpy( p_data->p_buffer, p_header, 6 );
511 /* Read the remaining of the packet. */
512 if( i_packet_size && (i_error =
513 SafeRead( p_input, p_data->p_buffer + 6, i_packet_size )) )
518 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
519 if( U32_AT(p_header) == 0x1BA )
521 if( i_packet_size == 8 && (p_data->p_buffer[13] & 0x7) != 0 )
523 /* MPEG-2 stuffing bytes */
525 if( (i_error = SafeRead( p_input, p_garbage,
526 p_data->p_buffer[13] & 0x7)) )
533 /* Give the packet to the other input stages. */
534 pp_packets[i_packet] = p_data;
540 /*****************************************************************************
541 * PSSeek: changes the stream position indicator
542 *****************************************************************************/
543 static void PSSeek( input_thread_t * p_input, off_t i_position )
545 thread_ps_data_t * p_method;
547 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
549 /* A little bourrin but should work for a while --Meuuh */
551 fseeko( p_method->stream, i_position, SEEK_SET );
553 fseek( p_method->stream, (long)i_position, SEEK_SET );
556 p_input->stream.p_selected_area->i_tell = i_position;
560 * Packet management utilities
564 /*****************************************************************************
565 * NewPacket: allocates a data packet
566 *****************************************************************************/
567 static struct data_packet_s * NewPacket( void * p_packet_cache,
570 packet_cache_t * p_cache;
571 data_packet_t * p_data;
574 p_cache = (packet_cache_t *)p_packet_cache;
577 if ( p_cache == NULL )
579 intf_ErrMsg( "PPacket cache not initialized" );
585 if( l_size > INPUT_MAX_PACKET_SIZE )
587 intf_ErrMsg( "Packet too big (%d)", l_size );
591 vlc_mutex_lock( &p_cache->lock );
593 /* Checks whether the data cache is empty */
594 if( p_cache->data.l_index == 0 )
596 /* Allocates a new packet */
597 if ( (p_data = malloc( sizeof(data_packet_t) )) == NULL )
599 intf_ErrMsg( "Out of memory" );
600 vlc_mutex_unlock( &p_cache->lock );
604 intf_DbgMsg( "PS input: data packet allocated" );
609 /* Takes the packet out from the cache */
610 if( (p_data = p_cache->data.p_stack[ -- p_cache->data.l_index ])
613 intf_ErrMsg( "NULL packet in the data cache" );
614 vlc_mutex_unlock( &p_cache->lock );
619 if( l_size < MAX_SMALL_SIZE )
623 /* Checks whether the buffer cache is empty */
624 if( p_cache->small.l_index == 0 )
626 /* Allocates a new packet */
627 if ( (p_data->p_buffer = malloc( l_size )) == NULL )
629 intf_DbgMsg( "Out of memory" );
631 vlc_mutex_unlock( &p_cache->lock );
635 intf_DbgMsg( "PS input: small buffer allocated" );
637 p_data->l_size = l_size;
641 /* Takes the packet out from the cache */
642 l_index = -- p_cache->small.l_index;
643 if( (p_data->p_buffer = p_cache->small.p_stack[l_index].p_data)
646 intf_ErrMsg( "NULL packet in the small buffer cache" );
648 vlc_mutex_unlock( &p_cache->lock );
651 /* Reallocates the packet if it is too small or too large */
652 if( p_cache->small.p_stack[l_index].l_size < l_size ||
653 p_cache->small.p_stack[l_index].l_size > 2*l_size )
655 p_data->p_buffer = realloc( p_data->p_buffer, l_size );
656 p_data->l_size = l_size;
660 p_data->l_size = p_cache->small.p_stack[l_index].l_size;
668 /* Checks whether the buffer cache is empty */
669 if( p_cache->large.l_index == 0 )
671 /* Allocates a new packet */
672 if ( (p_data->p_buffer = malloc( l_size )) == NULL )
674 intf_ErrMsg( "Out of memory" );
676 vlc_mutex_unlock( &p_cache->lock );
680 intf_DbgMsg( "PS input: large buffer allocated" );
682 p_data->l_size = l_size;
686 /* Takes the packet out from the cache */
687 l_index = -- p_cache->large.l_index;
688 if( (p_data->p_buffer = p_cache->large.p_stack[l_index].p_data)
691 intf_ErrMsg( "NULL packet in the small buffer cache" );
693 vlc_mutex_unlock( &p_cache->lock );
696 /* Reallocates the packet if it is too small or too large */
697 if( p_cache->large.p_stack[l_index].l_size < l_size ||
698 p_cache->large.p_stack[l_index].l_size > 2*l_size )
700 p_data->p_buffer = realloc( p_data->p_buffer, l_size );
701 p_data->l_size = l_size;
705 p_data->l_size = p_cache->large.p_stack[l_index].l_size;
710 vlc_mutex_unlock( &p_cache->lock );
712 /* Initialize data */
713 p_data->p_next = NULL;
714 p_data->b_discard_payload = 0;
715 p_data->p_payload_start = p_data->p_buffer;
716 p_data->p_payload_end = p_data->p_buffer + l_size;
723 /*****************************************************************************
724 * NewPES: allocates a pes packet
725 *****************************************************************************/
726 static pes_packet_t * NewPES( void * p_packet_cache )
728 packet_cache_t * p_cache;
729 pes_packet_t * p_pes;
731 p_cache = (packet_cache_t *)p_packet_cache;
734 if ( p_cache == NULL )
736 intf_ErrMsg( "Packet cache not initialized" );
741 vlc_mutex_lock( &p_cache->lock );
743 /* Checks whether the PES cache is empty */
744 if( p_cache->pes.l_index == 0 )
746 /* Allocates a new packet */
747 if ( (p_pes = malloc( sizeof(pes_packet_t) )) == NULL )
749 intf_DbgMsg( "Out of memory" );
750 vlc_mutex_unlock( &p_cache->lock );
754 intf_DbgMsg( "PS input: PES packet allocated" );
759 /* Takes the packet out from the cache */
760 if( (p_pes = p_cache->pes.p_stack[ -- p_cache->pes.l_index ])
763 intf_ErrMsg( "NULL packet in the data cache" );
764 vlc_mutex_unlock( &p_cache->lock );
769 vlc_mutex_unlock( &p_cache->lock );
771 p_pes->b_data_alignment = p_pes->b_discontinuity =
772 p_pes->i_pts = p_pes->i_dts = 0;
773 p_pes->i_pes_size = 0;
774 p_pes->p_first = NULL;
780 /*****************************************************************************
781 * DeletePacket: deletes a data packet
782 *****************************************************************************/
783 static void DeletePacket( void * p_packet_cache,
784 data_packet_t * p_data )
786 packet_cache_t * p_cache;
788 p_cache = (packet_cache_t *)p_packet_cache;
791 if ( p_cache == NULL )
793 intf_ErrMsg( "Packet cache not initialized" );
800 vlc_mutex_lock( &p_cache->lock );
802 /* Checks whether the data cache is full */
803 if ( p_cache->data.l_index < DATA_CACHE_SIZE )
805 /* Cache not full: store the packet in it */
806 p_cache->data.p_stack[ p_cache->data.l_index ++ ] = p_data;
807 /* Small buffer or large buffer? */
808 if ( p_data->l_size < MAX_SMALL_SIZE )
810 /* Checks whether the small buffer cache is full */
811 if ( p_cache->small.l_index < SMALL_CACHE_SIZE )
813 p_cache->small.p_stack[ p_cache->small.l_index ].l_size =
815 p_cache->small.p_stack[ p_cache->small.l_index ++ ].p_data =
820 ASSERT( p_data->p_buffer );
821 free( p_data->p_buffer );
823 intf_DbgMsg( "PS input: small buffer freed" );
829 /* Checks whether the large buffer cache is full */
830 if ( p_cache->large.l_index < LARGE_CACHE_SIZE )
832 p_cache->large.p_stack[ p_cache->large.l_index ].l_size =
834 p_cache->large.p_stack[ p_cache->large.l_index ++ ].p_data =
839 ASSERT( p_data->p_buffer );
840 free( p_data->p_buffer );
842 intf_DbgMsg( "PS input: large buffer freed" );
849 /* Cache full: the packet must be freed */
850 free( p_data->p_buffer );
853 intf_DbgMsg( "PS input: data packet freed" );
857 vlc_mutex_unlock( &p_cache->lock );
860 /*****************************************************************************
861 * DeletePES: deletes a PES packet and associated data packets
862 *****************************************************************************/
863 static void DeletePES( void * p_packet_cache, pes_packet_t * p_pes )
865 packet_cache_t * p_cache;
866 data_packet_t * p_data;
867 data_packet_t * p_next;
869 p_cache = (packet_cache_t *)p_packet_cache;
872 if ( p_cache == NULL )
874 intf_ErrMsg( "Packet cache not initialized" );
881 p_data = p_pes->p_first;
883 while( p_data != NULL )
885 p_next = p_data->p_next;
886 DeletePacket( p_cache, p_data );
890 vlc_mutex_lock( &p_cache->lock );
892 /* Checks whether the PES cache is full */
893 if ( p_cache->pes.l_index < PES_CACHE_SIZE )
895 /* Cache not full: store the packet in it */
896 p_cache->pes.p_stack[ p_cache->pes.l_index ++ ] = p_pes;
900 /* Cache full: the packet must be freed */
903 intf_DbgMsg( "PS input: PES packet freed" );
907 vlc_mutex_unlock( &p_cache->lock );