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