]> git.sesse.net Git - vlc/blob - plugins/mpeg/input_ps.c
*Added udf fix by Billy Biggs
[vlc] / plugins / mpeg / input_ps.c
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.14 2001/04/13 05:36:12 stef Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Cyril Deguet <asmax@via.ecp.fr>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 #define MODULE_NAME ps
26 #include "modules_inner.h"
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include "defs.h"
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41
42 #include "config.h"
43 #include "common.h"
44 #include "threads.h"
45 #include "mtime.h"
46 #include "tests.h"
47
48 #include "intf_msg.h"
49
50 #include "main.h"
51
52 #include "stream_control.h"
53 #include "input_ext-intf.h"
54 #include "input_ext-dec.h"
55
56 #include "input.h"
57
58 #include "input_ps.h"
59 #include "mpeg_system.h"
60
61 #include "debug.h"
62
63 #include "modules.h"
64
65 /*****************************************************************************
66  * Local prototypes
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 * );
78
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 )
84 {
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;
90     input.pf_end              = PSEnd;
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;
100 #undef input
101 }
102
103 /*
104  * Data reading functions
105  */
106
107 /*****************************************************************************
108  * PSProbe: verifies that the stream is a PS stream
109  *****************************************************************************/
110 static int PSProbe( probedata_t *p_data )
111 {
112     input_thread_t * p_input = (input_thread_t *)p_data;
113
114     char * psz_name = p_input->p_source;
115     int i_handle;
116     int i_score = 10;
117
118     if( TestMethod( INPUT_METHOD_VAR, "ps" ) )
119     {
120         return( 999 );
121     }
122
123     if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
124     {
125         /* If the user specified "file:" then it's probably a file */
126         i_score = 100;
127         psz_name += 5;
128     }
129
130     i_handle = open( psz_name, 0 );
131     if( i_handle == -1 )
132     {
133         return( 0 );
134     }
135     close( i_handle );
136
137     return( i_score );
138 }
139
140 /*****************************************************************************
141  * PSInit: initializes PS structures
142  *****************************************************************************/
143 static void PSInit( input_thread_t * p_input )
144 {
145     thread_ps_data_t *  p_method;
146
147     if( (p_method =
148          (thread_ps_data_t *)malloc( sizeof(thread_ps_data_t) )) == NULL )
149     {
150         intf_ErrMsg( "Out of memory" );
151         p_input->b_error = 1;
152         return;
153     }
154
155     p_input->p_plugin_data = (void *)p_method;
156     p_input->p_method_data = NULL;
157
158     /* Re-open the socket as a buffered FILE stream */
159     if( (p_method->stream = fdopen( p_input->i_handle, "r" )) == NULL )
160     {
161         intf_ErrMsg( "Cannot open file (%s)", strerror(errno) );
162         p_input->b_error = 1;
163         return;
164     }
165     rewind( p_method->stream );
166
167     /* FIXME : detect if InitStream failed */
168     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
169     input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
170
171     if( p_input->stream.b_seekable )
172     {
173         stream_ps_data_t * p_demux_data =
174              (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
175
176         /* Pre-parse the stream to gather stream_descriptor_t. */
177         p_input->stream.pp_programs[0]->b_is_ok = 0;
178         p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
179
180         while( !p_input->b_die && !p_input->b_error
181                 && !p_demux_data->b_has_PSM )
182         {
183             int                 i_result, i;
184             data_packet_t *     pp_packets[INPUT_READ_ONCE];
185
186             i_result = PSRead( p_input, pp_packets );
187             if( i_result == 1 )
188             {
189                 /* EOF */
190                 vlc_mutex_lock( &p_input->stream.stream_lock );
191                 p_input->stream.pp_programs[0]->b_is_ok = 1;
192                 vlc_mutex_unlock( &p_input->stream.stream_lock );
193                 break;
194             }
195             if( i_result == -1 )
196             {
197                 p_input->b_error = 1;
198                 break;
199             }
200
201             for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
202             {
203                 /* FIXME: use i_p_config_t */
204                 input_ParsePS( p_input, pp_packets[i] );
205                 DeletePacket( p_input->p_method_data, pp_packets[i] );
206             }
207
208             /* File too big. */
209             if( p_input->stream.p_selected_area->i_tell >
210                                                     INPUT_PREPARSE_LENGTH )
211             {
212                 break;
213             }
214         }
215         rewind( p_method->stream );
216         vlc_mutex_lock( &p_input->stream.stream_lock );
217
218         /* file input method */
219         p_input->stream.i_method = INPUT_METHOD_FILE;
220
221         p_input->stream.p_selected_area->i_tell = 0;
222         if( p_demux_data->b_has_PSM )
223         {
224             /* (The PSM decoder will care about spawning the decoders) */
225             p_input->stream.pp_programs[0]->b_is_ok = 1;
226         }
227 #ifdef AUTO_SPAWN
228         else
229         {
230             /* (We have to do it ourselves) */
231             int                 i_es;
232
233             /* FIXME: we should do multiple passes in case an audio type
234              * is not present */
235             for( i_es = 0;
236                  i_es < p_input->stream.pp_programs[0]->i_es_number;
237                  i_es++ )
238             {
239 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
240                 switch( p_es->i_type )
241                 {
242                     case MPEG1_VIDEO_ES:
243                     case MPEG2_VIDEO_ES:
244                         input_SelectES( p_input, p_es );
245                         break;
246
247                     case MPEG1_AUDIO_ES:
248                     case MPEG2_AUDIO_ES:
249                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
250                                 == (p_es->i_id & 0x1F) )
251                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
252                         {
253                         case 0:
254                             main_PutIntVariable( INPUT_AUDIO_VAR,
255                                                  REQUESTED_MPEG );
256                         case REQUESTED_MPEG:
257                             input_SelectES( p_input, p_es );
258                         }
259                         break;
260
261                     case AC3_AUDIO_ES:
262                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
263                                 == ((p_es->i_id & 0xF00) >> 8) )
264                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
265                         {
266                         case 0:
267                             main_PutIntVariable( INPUT_AUDIO_VAR,
268                                                  REQUESTED_AC3 );
269                         case REQUESTED_AC3:
270                             input_SelectES( p_input, p_es );
271                         }
272                         break;
273
274                     case DVD_SPU_ES:
275                         if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
276                                 == ((p_es->i_id & 0x1F00) >> 8) )
277                         {
278                             input_SelectES( p_input, p_es );
279                         }
280                         break;
281
282                     case LPCM_AUDIO_ES:
283                         /* FIXME ! */
284                         break;
285                 }
286             }
287                     
288         }
289 #endif
290 #ifdef STATS
291         input_DumpStream( p_input );
292 #endif
293         vlc_mutex_unlock( &p_input->stream.stream_lock );
294     }
295     else
296     {
297         /* The programs will be added when we read them. */
298         vlc_mutex_lock( &p_input->stream.stream_lock );
299
300         /* file input method */
301         p_input->stream.i_method = INPUT_METHOD_FILE;
302
303         p_input->stream.pp_programs[0]->b_is_ok = 0;
304         vlc_mutex_unlock( &p_input->stream.stream_lock );
305     }
306 }
307
308 /*****************************************************************************
309  * PSEnd: frees unused data
310  *****************************************************************************/
311 static void PSEnd( input_thread_t * p_input )
312 {
313     free( p_input->p_plugin_data );
314 }
315
316 /*****************************************************************************
317  * SafeRead: reads a chunk of stream and correctly detects errors
318  *****************************************************************************/
319 static __inline__ int SafeRead( input_thread_t * p_input, byte_t * p_buffer,
320                                 size_t i_len )
321 {
322     thread_ps_data_t *  p_method;
323     int                 i_error;
324
325     p_method = (thread_ps_data_t *)p_input->p_plugin_data;
326     while( fread( p_buffer, i_len, 1, p_method->stream ) != 1 )
327     {
328         if( feof( p_method->stream ) )
329         {
330             return( 1 );
331         }
332
333         if( (i_error = ferror( p_method->stream )) )
334         {
335             intf_ErrMsg( "Read failed (%s)", strerror(i_error) );
336             return( -1 );
337         }
338     }
339     vlc_mutex_lock( &p_input->stream.stream_lock );
340     p_input->stream.p_selected_area->i_tell += i_len;
341     vlc_mutex_unlock( &p_input->stream.stream_lock );
342     return( 0 );
343 }
344
345 /*****************************************************************************
346  * PSRead: reads data packets
347  *****************************************************************************
348  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
349  * EOF.
350  *****************************************************************************/
351 static int PSRead( input_thread_t * p_input,
352                    data_packet_t * pp_packets[INPUT_READ_ONCE] )
353 {
354     byte_t              p_header[6];
355     data_packet_t *     p_data;
356     size_t              i_packet_size;
357     int                 i_packet, i_error;
358     thread_ps_data_t *  p_method;
359
360     p_method = (thread_ps_data_t *)p_input->p_plugin_data;
361
362     memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
363     for( i_packet = 0; i_packet < INPUT_READ_ONCE; i_packet++ )
364     {
365         /* Read what we believe to be a packet header. */
366         if( (i_error = SafeRead( p_input, p_header, 6 )) )
367         {
368             return( i_error );
369         }
370
371         if( (U32_AT(p_header) & 0xFFFFFF00) != 0x100L )
372         {
373             /* This is not the startcode of a packet. Read the stream
374              * until we find one. */
375             u32         i_startcode = U32_AT(p_header);
376             int         i_dummy;
377
378             if( i_startcode )
379             {
380                 /* It is common for MPEG-1 streams to pad with zeros
381                  * (although it is forbidden by the recommendation), so
382                  * don't bother everybody in this case. */
383                 intf_WarnMsg( 1, "Garbage at input (%.8x)", i_startcode );
384             }
385
386             while( (i_startcode & 0xFFFFFF00) != 0x100L )
387             {
388                 i_startcode <<= 8;
389                 if( (i_dummy = getc( p_method->stream )) != EOF )
390                 {
391                     i_startcode |= i_dummy;
392                 }
393                 else
394                 {
395                     return( 1 );
396                 }
397             }
398             /* Packet found. */
399             *(u32 *)p_header = U32_AT(&i_startcode);
400             if( (i_error = SafeRead( p_input, p_header + 4, 2 )) )
401             {
402                 return( i_error );
403             }
404         }
405
406         if( U32_AT(p_header) != 0x1BA )
407         {
408             /* That's the case for all packets, except pack header. */
409             i_packet_size = U16_AT(&p_header[4]);
410         }
411         else
412         {
413             /* Pack header. */
414             if( (p_header[4] & 0xC0) == 0x40 )
415             {
416                 /* MPEG-2 */
417                 i_packet_size = 8;
418             }
419             else if( (p_header[4] & 0xF0) == 0x20 )
420             {
421                 /* MPEG-1 */
422                 i_packet_size = 6;
423             }
424             else
425             {
426                 intf_ErrMsg( "Unable to determine stream type" );
427                 return( -1 );
428             }
429         }
430
431         /* Fetch a packet of the appropriate size. */
432         if( (p_data = NewPacket( p_input, i_packet_size + 6 )) == NULL )
433         {
434             intf_ErrMsg( "Out of memory" );
435             return( -1 );
436         }
437
438         /* Copy the header we already read. */
439         memcpy( p_data->p_buffer, p_header, 6 );
440
441         /* Read the remaining of the packet. */
442         if( i_packet_size && (i_error =
443                 SafeRead( p_input, p_data->p_buffer + 6, i_packet_size )) )
444         {
445             return( i_error );
446         }
447
448         /* In MPEG-2 pack headers we still have to read stuffing bytes. */
449         if( U32_AT(p_header) == 0x1BA )
450         {
451             if( i_packet_size == 8 && (p_data->p_buffer[13] & 0x7) != 0 )
452             {
453                 /* MPEG-2 stuffing bytes */
454                 byte_t      p_garbage[8];
455                 if( (i_error = SafeRead( p_input, p_garbage,
456                                          p_data->p_buffer[13] & 0x7)) )
457                 {
458                     return( i_error );
459                 }
460             }
461         }
462
463         /* Give the packet to the other input stages. */
464         pp_packets[i_packet] = p_data;
465     }
466
467     return( 0 );
468 }
469
470 /*****************************************************************************
471  * PSSeek: changes the stream position indicator
472  *****************************************************************************/
473 static void PSSeek( input_thread_t * p_input, off_t i_position )
474 {
475     thread_ps_data_t *  p_method;
476
477     p_method = (thread_ps_data_t *)p_input->p_plugin_data;
478
479     /* A little bourrin but should work for a while --Meuuh */
480     fseeko( p_method->stream, i_position, SEEK_SET );
481
482     p_input->stream.p_selected_area->i_tell = i_position;
483 }
484
485 /*
486  * Packet management utilities
487  */
488
489
490 /*****************************************************************************
491  * NewPacket: allocates a data packet
492  *****************************************************************************/
493 static struct data_packet_s * NewPacket( void * p_garbage,
494                                          size_t i_size )
495 {
496     data_packet_t * p_data;
497
498     /* Safety check */
499     if( i_size > INPUT_MAX_PACKET_SIZE )
500     {
501         intf_ErrMsg( "Packet too big (%d)", i_size );
502         return NULL;
503     }
504
505     if( (p_data = (data_packet_t *)malloc( sizeof(data_packet_t) )) == NULL )
506     {
507         intf_DbgMsg( "Out of memory" );
508         return NULL;
509     }
510
511     if( (p_data->p_buffer = (byte_t *)malloc( i_size )) == NULL )
512     {
513         intf_DbgMsg( "Out of memory" );
514         free( p_data );
515         return NULL;
516     }
517
518     /* Initialize data */
519     p_data->p_next = NULL;
520     p_data->b_discard_payload = 0;
521
522     p_data->p_payload_start = p_data->p_buffer;
523     p_data->p_payload_end = p_data->p_buffer + i_size;
524
525     return( p_data );
526 }
527
528 /*****************************************************************************
529  * NewPES: allocates a pes packet
530  *****************************************************************************/
531 static pes_packet_t * NewPES( void * p_garbage )
532 {
533     pes_packet_t * p_pes;
534
535     if( (p_pes = (pes_packet_t *)malloc( sizeof(pes_packet_t) )) == NULL )
536     {
537         intf_DbgMsg( "Out of memory" );
538         return NULL;
539     }
540
541     p_pes->b_data_alignment = p_pes->b_discontinuity =
542         p_pes->i_pts = p_pes->i_dts = 0;
543     p_pes->i_pes_size = 0;
544     p_pes->p_first = NULL;
545
546     return( p_pes );
547 }
548
549 /*****************************************************************************
550  * DeletePacket: deletes a data packet
551  *****************************************************************************/
552 static void DeletePacket( void * p_garbage,
553                           data_packet_t * p_data )
554 {
555     ASSERT(p_data);
556     ASSERT(p_data->p_buffer);
557     free( p_data->p_buffer );
558     free( p_data );
559 }
560
561 /*****************************************************************************
562  * DeletePES: deletes a PES packet and associated data packets
563  *****************************************************************************/
564 static void DeletePES( void * p_garbage, pes_packet_t * p_pes )
565 {
566     data_packet_t *     p_data;
567     data_packet_t *     p_next;
568
569     p_data = p_pes->p_first;
570
571     while( p_data != NULL )
572     {
573         p_next = p_data->p_next;
574         free( p_data->p_buffer );
575         free( p_data );
576         p_data = p_next;
577     }
578
579     free( p_pes );
580 }
581