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