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