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.18 2001/04/27 19:29:11 massiot 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 #include <sys/types.h>
52 #include "stream_control.h"
53 #include "input_ext-intf.h"
54 #include "input_ext-dec.h"
59 #include "mpeg_system.h"
65 /*****************************************************************************
67 *****************************************************************************/
68 static int PSProbe ( probedata_t * );
69 static int PSRead ( struct input_thread_s *,
70 data_packet_t * p_packets[INPUT_READ_ONCE] );
71 static void PSInit ( struct input_thread_s * );
72 static void PSEnd ( struct input_thread_s * );
73 static void PSSeek ( struct input_thread_s *, off_t );
74 static struct pes_packet_s * NewPES ( void * );
75 static struct data_packet_s * NewPacket ( void *, size_t );
76 static void DeletePacket( void *, struct data_packet_s * );
77 static void DeletePES ( void *, struct pes_packet_s * );
79 /*****************************************************************************
80 * Functions exported as capabilities. They are declared as static so that
81 * we don't pollute the namespace too much.
82 *****************************************************************************/
83 void _M( input_getfunctions )( function_list_t * p_function_list )
85 #define input p_function_list->functions.input
86 p_function_list->pf_probe = PSProbe;
87 input.pf_init = PSInit;
88 input.pf_open = input_FileOpen;
89 input.pf_close = input_FileClose;
91 input.pf_set_area = NULL;
92 input.pf_read = PSRead;
93 input.pf_demux = input_DemuxPS;
94 input.pf_new_packet = NewPacket;
95 input.pf_new_pes = NewPES;
96 input.pf_delete_packet = DeletePacket;
97 input.pf_delete_pes = DeletePES;
98 input.pf_rewind = NULL;
99 input.pf_seek = PSSeek;
104 * Data reading functions
107 /*****************************************************************************
108 * PSProbe: verifies that the stream is a PS stream
109 *****************************************************************************/
110 static int PSProbe( probedata_t *p_data )
112 input_thread_t * p_input = (input_thread_t *)p_data;
114 char * psz_name = p_input->p_source;
118 if( TestMethod( INPUT_METHOD_VAR, "ps" ) )
123 if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
125 /* If the user specified "file:" then it's probably a file */
130 i_handle = open( psz_name, 0 );
140 /*****************************************************************************
141 * PSInit: initializes PS structures
142 *****************************************************************************/
143 static void PSInit( input_thread_t * p_input )
145 thread_ps_data_t * p_method;
146 packet_cache_t * p_packet_cache;
149 (thread_ps_data_t *)malloc( sizeof(thread_ps_data_t) )) == NULL )
151 intf_ErrMsg( "Out of memory" );
152 p_input->b_error = 1;
155 p_input->p_plugin_data = (void *)p_method;
157 /* creates the packet cache structure */
158 p_packet_cache = malloc( sizeof(packet_cache_t) );
159 if ( p_packet_cache == NULL )
161 intf_ErrMsg( "Out of memory" );
162 p_input->b_error = 1;
165 p_input->p_method_data = (void *)p_packet_cache;
167 /* allocates the data cache */
168 p_packet_cache->data.p_stack = malloc( DATA_CACHE_SIZE *
169 sizeof(data_packet_t*) );
170 if ( p_packet_cache->data.p_stack == NULL )
172 intf_ErrMsg( "Out of memory" );
173 p_input->b_error = 1;
176 p_packet_cache->data.l_index = 0;
178 /* allocates the PES cache */
179 p_packet_cache->pes.p_stack = malloc( PES_CACHE_SIZE *
180 sizeof(pes_packet_t*) );
181 if ( p_packet_cache->pes.p_stack == NULL )
183 intf_ErrMsg( "Out of memory" );
184 p_input->b_error = 1;
187 p_packet_cache->pes.l_index = 0;
189 /* allocates the small buffer cache */
190 p_packet_cache->small.p_stack = malloc( SMALL_CACHE_SIZE *
191 sizeof(packet_buffer_t) );
192 if ( p_packet_cache->small.p_stack == NULL )
194 intf_ErrMsg( "Out of memory" );
195 p_input->b_error = 1;
198 p_packet_cache->small.l_index = 0;
200 /* allocates the large buffer cache */
201 p_packet_cache->large.p_stack = malloc( LARGE_CACHE_SIZE *
202 sizeof(packet_buffer_t) );
203 if ( p_packet_cache->large.p_stack == NULL )
205 intf_ErrMsg( "Out of memory" );
206 p_input->b_error = 1;
209 p_packet_cache->large.l_index = 0;
211 /* Re-open the socket as a buffered FILE stream */
212 if( (p_method->stream = fdopen( p_input->i_handle, "r" )) == NULL )
214 intf_ErrMsg( "Cannot open file (%s)", strerror(errno) );
215 p_input->b_error = 1;
218 rewind( p_method->stream );
220 /* FIXME : detect if InitStream failed */
221 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
222 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
224 if( p_input->stream.b_seekable )
226 stream_ps_data_t * p_demux_data =
227 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
229 /* Pre-parse the stream to gather stream_descriptor_t. */
230 p_input->stream.pp_programs[0]->b_is_ok = 0;
231 p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
233 while( !p_input->b_die && !p_input->b_error
234 && !p_demux_data->b_has_PSM )
237 data_packet_t * pp_packets[INPUT_READ_ONCE];
239 i_result = PSRead( p_input, pp_packets );
243 vlc_mutex_lock( &p_input->stream.stream_lock );
244 p_input->stream.pp_programs[0]->b_is_ok = 1;
245 vlc_mutex_unlock( &p_input->stream.stream_lock );
250 p_input->b_error = 1;
254 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
256 /* FIXME: use i_p_config_t */
257 input_ParsePS( p_input, pp_packets[i] );
258 DeletePacket( p_input->p_method_data, pp_packets[i] );
262 if( p_input->stream.p_selected_area->i_tell >
263 INPUT_PREPARSE_LENGTH )
268 rewind( p_method->stream );
269 vlc_mutex_lock( &p_input->stream.stream_lock );
271 p_input->stream.i_method = INPUT_METHOD_FILE;
272 p_input->stream.p_selected_area->i_tell = 0;
274 if( p_demux_data->b_has_PSM )
276 /* (The PSM decoder will care about spawning the decoders) */
277 p_input->stream.pp_programs[0]->b_is_ok = 1;
282 /* (We have to do it ourselves) */
285 /* FIXME: we should do multiple passes in case an audio type
288 i_es < p_input->stream.pp_programs[0]->i_es_number;
291 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
292 switch( p_es->i_type )
296 input_SelectES( p_input, p_es );
301 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
302 == (p_es->i_id & 0x1F) )
303 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
306 main_PutIntVariable( INPUT_AUDIO_VAR,
309 input_SelectES( p_input, p_es );
314 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
315 == ((p_es->i_id & 0xF00) >> 8) )
316 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
319 main_PutIntVariable( INPUT_AUDIO_VAR,
322 input_SelectES( p_input, p_es );
327 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
328 == ((p_es->i_id & 0x1F00) >> 8) )
330 input_SelectES( p_input, p_es );
343 input_DumpStream( p_input );
345 vlc_mutex_unlock( &p_input->stream.stream_lock );
349 /* The programs will be added when we read them. */
350 vlc_mutex_lock( &p_input->stream.stream_lock );
351 p_input->stream.i_method = INPUT_METHOD_FILE;
352 p_input->stream.pp_programs[0]->b_is_ok = 0;
353 vlc_mutex_unlock( &p_input->stream.stream_lock );
357 /*****************************************************************************
358 * PSEnd: frees unused data
359 *****************************************************************************/
360 static void PSEnd( input_thread_t * p_input )
362 free( p_input->p_plugin_data );
365 /*****************************************************************************
366 * SafeRead: reads a chunk of stream and correctly detects errors
367 *****************************************************************************/
368 static __inline__ int SafeRead( input_thread_t * p_input, byte_t * p_buffer,
371 thread_ps_data_t * p_method;
374 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
375 while( fread( p_buffer, i_len, 1, p_method->stream ) != 1 )
377 if( feof( p_method->stream ) )
382 if( (i_error = ferror( p_method->stream )) )
384 intf_ErrMsg( "Read failed (%s)", strerror(i_error) );
388 vlc_mutex_lock( &p_input->stream.stream_lock );
389 p_input->stream.p_selected_area->i_tell += i_len;
390 vlc_mutex_unlock( &p_input->stream.stream_lock );
394 /*****************************************************************************
395 * PSRead: reads data packets
396 *****************************************************************************
397 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
399 *****************************************************************************/
400 static int PSRead( input_thread_t * p_input,
401 data_packet_t * pp_packets[INPUT_READ_ONCE] )
404 data_packet_t * p_data;
405 size_t i_packet_size;
406 int i_packet, i_error;
407 thread_ps_data_t * p_method;
409 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
411 memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
412 for( i_packet = 0; i_packet < INPUT_READ_ONCE; i_packet++ )
414 /* Read what we believe to be a packet header. */
415 if( (i_error = SafeRead( p_input, p_header, 6 )) )
420 if( (U32_AT(p_header) & 0xFFFFFF00) != 0x100L )
422 /* This is not the startcode of a packet. Read the stream
423 * until we find one. */
424 u32 i_startcode = U32_AT(p_header);
429 /* It is common for MPEG-1 streams to pad with zeros
430 * (although it is forbidden by the recommendation), so
431 * don't bother everybody in this case. */
432 intf_WarnMsg( 1, "Garbage at input (%.8x)", i_startcode );
435 while( (i_startcode & 0xFFFFFF00) != 0x100L )
438 if( (i_dummy = getc( p_method->stream )) != EOF )
440 i_startcode |= i_dummy;
448 *(u32 *)p_header = U32_AT(&i_startcode);
449 if( (i_error = SafeRead( p_input, p_header + 4, 2 )) )
455 if( U32_AT(p_header) != 0x1BA )
457 /* That's the case for all packets, except pack header. */
458 i_packet_size = U16_AT(&p_header[4]);
463 if( (p_header[4] & 0xC0) == 0x40 )
468 else if( (p_header[4] & 0xF0) == 0x20 )
475 intf_ErrMsg( "Unable to determine stream type" );
480 /* Fetch a packet of the appropriate size. */
481 if( (p_data = NewPacket( p_input->p_method_data, i_packet_size + 6 ))
484 intf_ErrMsg( "Out of memory" );
488 /* Copy the header we already read. */
489 memcpy( p_data->p_buffer, p_header, 6 );
491 /* Read the remaining of the packet. */
492 if( i_packet_size && (i_error =
493 SafeRead( p_input, p_data->p_buffer + 6, i_packet_size )) )
498 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
499 if( U32_AT(p_header) == 0x1BA )
501 if( i_packet_size == 8 && (p_data->p_buffer[13] & 0x7) != 0 )
503 /* MPEG-2 stuffing bytes */
505 if( (i_error = SafeRead( p_input, p_garbage,
506 p_data->p_buffer[13] & 0x7)) )
513 /* Give the packet to the other input stages. */
514 pp_packets[i_packet] = p_data;
520 /*****************************************************************************
521 * PSSeek: changes the stream position indicator
522 *****************************************************************************/
523 static void PSSeek( input_thread_t * p_input, off_t i_position )
525 thread_ps_data_t * p_method;
527 p_method = (thread_ps_data_t *)p_input->p_plugin_data;
529 /* A little bourrin but should work for a while --Meuuh */
530 fseeko( p_method->stream, i_position, SEEK_SET );
532 p_input->stream.p_selected_area->i_tell = i_position;
536 * Packet management utilities
540 /*****************************************************************************
541 * NewPacket: allocates a data packet
542 *****************************************************************************/
543 static struct data_packet_s * NewPacket( void * p_packet_cache,
546 packet_cache_t * p_cache;
547 data_packet_t * p_data;
551 if ( (p_cache = (packet_cache_t *)p_packet_cache) == NULL )
553 intf_ErrMsg( "PPacket cache not initialized" );
559 if( l_size > INPUT_MAX_PACKET_SIZE )
561 intf_ErrMsg( "Packet too big (%d)", l_size );
565 /* Checks whether the data cache is empty */
566 if( p_cache->data.l_index == 0 )
568 /* Allocates a new packet */
569 if ( (p_data = malloc( sizeof(data_packet_t) )) == NULL )
571 intf_ErrMsg( "Out of memory" );
575 intf_DbgMsg( "PS input: data packet allocated" );
580 /* Takes the packet out from the cache */
581 if( (p_data = p_cache->data.p_stack[ -- p_cache->data.l_index ])
584 intf_ErrMsg( "NULL packet in the data cache" );
589 if( l_size < MAX_SMALL_SIZE )
593 /* Checks whether the buffer cache is empty */
594 if( p_cache->small.l_index == 0 )
596 /* Allocates a new packet */
597 if ( (p_data->p_buffer = malloc( l_size )) == NULL )
599 intf_DbgMsg( "Out of memory" );
604 intf_DbgMsg( "PS input: small buffer allocated" );
606 p_data->l_size = l_size;
610 /* Takes the packet out from the cache */
611 l_index = -- p_cache->small.l_index;
612 if( (p_data->p_buffer = p_cache->small.p_stack[l_index].p_data)
615 intf_ErrMsg( "NULL packet in the small buffer cache" );
619 /* Reallocates the packet if it is too small or too large */
620 if( p_cache->small.p_stack[l_index].l_size < l_size ||
621 p_cache->small.p_stack[l_index].l_size > 2*l_size )
623 p_data->p_buffer = realloc( p_data->p_buffer, l_size );
624 p_data->l_size = l_size;
628 p_data->l_size = p_cache->small.p_stack[l_index].l_size;
636 /* Checks whether the buffer cache is empty */
637 if( p_cache->large.l_index == 0 )
639 /* Allocates a new packet */
640 if ( (p_data->p_buffer = malloc( l_size )) == NULL )
642 intf_ErrMsg( "Out of memory" );
647 intf_DbgMsg( "PS input: large buffer allocated" );
649 p_data->l_size = l_size;
653 /* Takes the packet out from the cache */
654 l_index = -- p_cache->large.l_index;
655 if( (p_data->p_buffer = p_cache->large.p_stack[l_index].p_data)
658 intf_ErrMsg( "NULL packet in the small buffer cache" );
662 /* Reallocates the packet if it is too small or too large */
663 if( p_cache->large.p_stack[l_index].l_size < l_size ||
664 p_cache->large.p_stack[l_index].l_size > 2*l_size )
666 p_data->p_buffer = realloc( p_data->p_buffer, l_size );
667 p_data->l_size = l_size;
671 p_data->l_size = p_cache->large.p_stack[l_index].l_size;
676 /* Initialize data */
677 p_data->p_next = NULL;
678 p_data->b_discard_payload = 0;
679 p_data->p_payload_start = p_data->p_buffer;
680 p_data->p_payload_end = p_data->p_buffer + l_size;
687 /*****************************************************************************
688 * NewPES: allocates a pes packet
689 *****************************************************************************/
690 static pes_packet_t * NewPES( void * p_packet_cache )
692 packet_cache_t * p_cache;
693 pes_packet_t * p_pes;
696 if ( (p_cache = (packet_cache_t *)p_packet_cache) == NULL )
698 intf_ErrMsg( "Packet cache not initialized" );
703 /* Checks whether the PES cache is empty */
704 if( p_cache->pes.l_index == 0 )
706 /* Allocates a new packet */
707 if ( (p_pes = malloc( sizeof(pes_packet_t) )) == NULL )
709 intf_DbgMsg( "Out of memory" );
713 intf_DbgMsg( "PS input: PES packet allocated" );
718 /* Takes the packet out from the cache */
719 if( (p_pes = p_cache->pes.p_stack[ -- p_cache->pes.l_index ])
722 intf_ErrMsg( "NULL packet in the data cache" );
727 p_pes->b_data_alignment = p_pes->b_discontinuity =
728 p_pes->i_pts = p_pes->i_dts = 0;
729 p_pes->i_pes_size = 0;
730 p_pes->p_first = NULL;
736 /*****************************************************************************
737 * DeletePacket: deletes a data packet
738 *****************************************************************************/
739 static void DeletePacket( void * p_packet_cache,
740 data_packet_t * p_data )
742 packet_cache_t * p_cache;
745 if ( (p_cache = (packet_cache_t *)p_packet_cache) == NULL )
747 intf_ErrMsg( "Packet cache not initialized" );
754 /* Checks whether the data cache is full */
755 if ( p_cache->data.l_index < DATA_CACHE_SIZE )
757 /* Cache not full: store the packet in it */
758 p_cache->data.p_stack[ p_cache->data.l_index ++ ] = p_data;
759 /* Small buffer or large buffer? */
760 if ( p_data->l_size < MAX_SMALL_SIZE )
762 /* Checks whether the small buffer cache is full */
763 if ( p_cache->small.l_index < SMALL_CACHE_SIZE )
765 p_cache->small.p_stack[ p_cache->small.l_index ].l_size =
767 p_cache->small.p_stack[ p_cache->small.l_index ++ ].p_data =
772 ASSERT( p_data->p_buffer );
773 free( p_data->p_buffer );
775 intf_DbgMsg( "PS input: small buffer freed" );
781 /* Checks whether the large buffer cache is full */
782 if ( p_cache->large.l_index < LARGE_CACHE_SIZE )
784 p_cache->large.p_stack[ p_cache->large.l_index ].l_size =
786 p_cache->large.p_stack[ p_cache->large.l_index ++ ].p_data =
791 ASSERT( p_data->p_buffer );
792 free( p_data->p_buffer );
794 intf_DbgMsg( "PS input: large buffer freed" );
801 /* Cache full: the packet must be freed */
802 free( p_data->p_buffer );
805 intf_DbgMsg( "PS input: data packet freed" );
811 /*****************************************************************************
812 * DeletePES: deletes a PES packet and associated data packets
813 *****************************************************************************/
814 static void DeletePES( void * p_packet_cache, pes_packet_t * p_pes )
816 packet_cache_t * p_cache;
817 data_packet_t * p_data;
818 data_packet_t * p_next;
821 if ( (p_cache = (packet_cache_t *)p_packet_cache) == NULL )
823 intf_ErrMsg( "Packet cache not initialized" );
830 p_data = p_pes->p_first;
832 while( p_data != NULL )
834 p_next = p_data->p_next;
835 DeletePacket( p_cache, p_data );
839 /* Checks whether the PES cache is full */
840 if ( p_cache->pes.l_index < PES_CACHE_SIZE )
842 /* Cache not full: store the packet in it */
843 p_cache->pes.p_stack[ p_cache->pes.l_index ++ ] = p_pes;
847 /* Cache full: the packet must be freed */
850 intf_DbgMsg( "PS input: PES packet freed" );