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