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