]> git.sesse.net Git - vlc/blob - plugins/vcd/input_vcd.c
* Fixed a segfault at EOF in input_es.c and input_ts.c (when i_read == 0).
[vlc] / plugins / vcd / input_vcd.c
1 /****************************************************************************
2  * input_vcd.c: VideoCD raw reading plugin.
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  *
6  * Author: Johan Bilien <jobi@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 #define MODULE_NAME vcd
24 #include "modules_inner.h"
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "defs.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #ifdef HAVE_UNISTD_H
35 #   include <unistd.h>
36 #endif
37
38 #include <fcntl.h>
39 #include <sys/types.h>
40 #include <string.h>
41 #include <errno.h>
42
43 #ifdef STRNCASECMP_IN_STRINGS_H
44 #   include <strings.h>
45 #endif
46
47 #if defined( WIN32 )
48 #   include <io.h>                                                 /* read() */
49 #else
50 #   include <sys/uio.h>                                      /* struct iovec */
51 #endif
52
53 #include "common.h"
54 #include "intf_msg.h"
55 #include "threads.h"
56 #include "mtime.h"
57 #include "tests.h"
58
59 #if defined( WIN32 )
60 #   include "input_iovec.h"
61 #endif
62
63 #include "stream_control.h"
64 #include "input_ext-intf.h"
65 #include "input_ext-dec.h"
66 #include "input_ext-plugins.h"
67
68 #include "debug.h"
69
70 #include "modules.h"
71 #include "modules_export.h"
72
73 #include "input_vcd.h"
74 #include "linux_cdrom_tools.h"
75
76 /* how many blocks VCDRead will read in each loop */
77 #define VCD_BLOCKS_ONCE 4
78 #define VCD_DATA_ONCE   (2 * VCD_BLOCKS_ONCE)
79 #define BUFFER_SIZE VCD_DATA_SIZE
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 /* called from outside */
85 static int  VCDProbe        ( probedata_t *p_data );
86 static void VCDInit         ( struct input_thread_s * );
87 static int  VCDRead         ( struct input_thread_s *, data_packet_t ** );
88 static int  VCDSetArea      ( struct input_thread_s *, struct input_area_s * );
89 static int  VCDSetProgram   ( struct input_thread_s *, pgrm_descriptor_t * );
90 static void VCDOpen         ( struct input_thread_s *);
91 static void VCDClose        ( struct input_thread_s *);
92 static void VCDEnd          ( struct input_thread_s *);
93 static void VCDSeek         ( struct input_thread_s *, off_t );
94 static int  VCDRewind       ( struct input_thread_s * );
95
96 /*****************************************************************************
97  * Declare a buffer manager
98  *****************************************************************************/
99 #define FLAGS           BUFFERS_NOFLAGS
100 #define NB_LIFO         2
101 DECLARE_BUFFERS_EMBEDDED( FLAGS, NB_LIFO );
102 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
103 DECLARE_BUFFERS_END( FLAGS, NB_LIFO );
104 DECLARE_BUFFERS_NEWPACKET( FLAGS, NB_LIFO );
105 DECLARE_BUFFERS_DELETEPACKET( FLAGS, NB_LIFO, 1000 );
106 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
107 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 1000 );
108
109
110 /*****************************************************************************
111  * Functions exported as capabilities. They are declared as static so that
112  * we don't pollute the namespace too much.
113  *****************************************************************************/
114 void _M( input_getfunctions )( function_list_t * p_function_list )
115 {
116 #define input p_function_list->functions.input
117     p_function_list->pf_probe = VCDProbe;
118     input.pf_init             = VCDInit;
119     input.pf_open             = VCDOpen;
120     input.pf_close            = VCDClose;
121     input.pf_end              = VCDEnd;
122     input.pf_init_bit_stream  = InitBitstream;
123     input.pf_read             = VCDRead;
124     input.pf_set_area         = VCDSetArea;
125     input.pf_set_program      = VCDSetProgram;
126     input.pf_demux            = input_DemuxPS;
127     input.pf_new_packet       = input_NewPacket;
128     input.pf_new_pes          = input_NewPES;
129     input.pf_delete_packet    = input_DeletePacket;
130     input.pf_delete_pes       = input_DeletePES;
131     input.pf_rewind           = VCDRewind;
132     input.pf_seek             = VCDSeek;
133 #undef input
134 }
135
136 /*
137  * Data reading functions
138  */
139
140 /*****************************************************************************
141  * VCDProbe: verifies that the stream is a PS stream
142  *****************************************************************************/
143 static int VCDProbe( probedata_t *p_data )
144 {
145     input_thread_t * p_input = (input_thread_t *)p_data;
146
147     char * psz_name = p_input->p_source;
148     int i_score = 5;
149
150     if( TestMethod( INPUT_METHOD_VAR, "vcd" ) )
151     {
152         return( 999 );
153     }
154
155     if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "vcd:", 4 ) )
156     {
157         /* If the user specified "vcd:" then it's probably a VCD */
158         i_score = 100;
159         psz_name += 4;
160     }
161
162     return( i_score );
163 }
164
165 /*****************************************************************************
166  * VCDOpen: open vcd
167  *****************************************************************************/
168 static void VCDOpen( struct input_thread_s *p_input )
169 {
170     vlc_mutex_lock( &p_input->stream.stream_lock );
171
172     /* If we are here we can control the pace... */
173     p_input->stream.b_pace_control = 1;
174
175     p_input->stream.b_seekable = 1;
176     p_input->stream.p_selected_area->i_size = 0;
177     p_input->stream.p_selected_area->i_tell = 0;
178
179     vlc_mutex_unlock( &p_input->stream.stream_lock );
180
181     /* XXX: put this shit in an access plugin */
182     if( strlen( p_input->p_source ) > 4
183          && !strncasecmp( p_input->p_source, "vcd:", 4 ) )
184     {
185         p_input->i_handle
186             = open( p_input->p_source + 4, O_RDONLY | O_NONBLOCK );
187     }
188     else
189     {
190         p_input->i_handle
191             = open( p_input->p_source + 4, O_RDONLY | O_NONBLOCK );
192     }
193
194     if( p_input->i_handle == -1 )
195     {
196         p_input->b_error = 1;
197     }
198 }
199
200 /*****************************************************************************
201  * VCDClose: close vcd
202  *****************************************************************************/
203 static void VCDClose( struct input_thread_s *p_input )
204 {
205     close( p_input->i_handle );
206 }
207
208 /*****************************************************************************
209  * VCDInit: initializes VCD structures
210  *****************************************************************************/
211 static void VCDInit( input_thread_t * p_input )
212 {
213     thread_vcd_data_t *  p_vcd;
214     int                  i_title;
215     int                  i_chapter;
216     int                  i;
217     input_area_t *       p_area;
218     es_descriptor_t *    p_es;
219
220     p_vcd = malloc( sizeof(thread_vcd_data_t) );
221
222     if( p_vcd == NULL )
223     {
224         intf_ErrMsg( "vcd error: out of memory" );
225         p_input->b_error = 1;
226         return;
227     }
228
229     p_input->p_plugin_data = (void *)p_vcd;
230
231     if( (p_input->p_method_data = input_BuffersInit()) == NULL )
232     {
233         p_input->b_error = 1;
234         return;
235     }
236
237     p_vcd->i_handle = p_input->i_handle;
238     p_vcd->b_end_of_track = 0;
239
240     /* we read the Table Of Content information */
241     if ( VCDReadToc( p_vcd ) == -1 )
242     {
243         intf_ErrMsg( "vcd error: could not read TOC" );
244         input_BuffersEnd( p_input->p_method_data );
245         p_input->b_error = 1;
246         return;
247     }
248
249     /* Set stream and area data */
250     vlc_mutex_lock( &p_input->stream.stream_lock );
251
252     /* Initialize ES structures */
253     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
254
255     /* disc input method */
256     p_input->stream.i_method = INPUT_METHOD_VCD;
257
258
259 #define area p_input->stream.pp_areas
260     for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
261     {
262         input_AddArea( p_input );
263
264         /* Titles are Program Chains */
265         area[i]->i_id = i;
266
267         /* Absolute start offset and size */
268         area[i]->i_start = p_vcd->p_sectors[i];
269         area[i]->i_size = p_vcd->p_sectors[i+1] - p_vcd->p_sectors[i];
270
271         /* Number of chapters */
272         area[i]->i_part_nb = 0;   // will be the entry points
273         area[i]->i_part = 1;
274
275         /* Number of angles */
276         area[i]->i_angle_nb = 1; // no angle support in VCDs
277         area[i]->i_angle = 1;
278
279         area[i]->i_plugin_data = p_vcd->p_sectors[i];
280     }
281 #undef area
282
283     /* Get requested title - if none try the first title */
284     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
285     if( i_title <= 0 )
286     {
287         i_title = 1;
288     }
289
290     // p_vcd->i_track = i_title-1;
291
292     /* Get requested chapter - if none defaults to first one */
293     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
294     if( i_chapter <= 0 )
295     {
296         i_chapter = 1;
297     }
298
299     p_input->stream.pp_areas[i_title]->i_part = i_chapter;
300
301     p_area = p_input->stream.pp_areas[i_title];
302
303     VCDSetArea( p_input, p_area );
304
305     /* Set program information. */
306
307     input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
308     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
309
310     /* No PSM to read in disc mode, we already have all information */
311     p_input->stream.p_selected_program->b_is_ok = 1;
312
313     p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
314     p_es->i_stream_id = 0xe0;
315     p_es->i_type = MPEG1_VIDEO_ES;
316     p_es->i_cat = VIDEO_ES;
317
318     if( p_main->b_video )
319     {
320         input_SelectES( p_input, p_es );
321     }
322
323     p_es = input_AddES( p_input,
324                 p_input->stream.p_selected_program, 0xc0, 0 );
325     p_es->i_stream_id = 0xc0;
326     p_es->i_type = MPEG1_AUDIO_ES;
327     p_es->b_audio = 1;
328     p_es->i_cat = AUDIO_ES;
329
330     if( p_main->b_audio )
331     {
332         input_SelectES( p_input, p_es );
333     }
334
335     vlc_mutex_unlock( &p_input->stream.stream_lock );
336 }
337
338 /*****************************************************************************
339  * VCDEnd: frees unused data
340  *****************************************************************************/
341 static void VCDEnd( input_thread_t * p_input )
342 {
343     thread_vcd_data_t *     p_vcd;
344
345     p_vcd = (thread_vcd_data_t*)p_input->p_plugin_data;
346
347     free( p_vcd );
348
349     input_BuffersEnd( p_input->p_method_data );
350 }
351
352 /*****************************************************************************
353  * VCDSetProgram: Does nothing since a VCD is mono_program
354  *****************************************************************************/
355 static int VCDSetProgram( input_thread_t * p_input,
356                           pgrm_descriptor_t * p_program)
357 {
358     return 0;
359 }
360
361
362 /*****************************************************************************
363  * VCDSetArea: initialize input data for title x, chapter y.
364  * It should be called for each user navigation request.
365  ****************************************************************************/
366 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
367 {
368     thread_vcd_data_t *     p_vcd;
369
370     p_vcd = (thread_vcd_data_t*)p_input->p_plugin_data;
371
372     /* we can't use the interface slider until initilization is complete */
373     p_input->stream.b_seekable = 0;
374
375     if( p_area != p_input->stream.p_selected_area )
376     {
377         /* Reset the Chapter position of the current title */
378         p_input->stream.p_selected_area->i_part = 1;
379         p_input->stream.p_selected_area->i_tell = 0;
380
381         /* Change the default area */
382         p_input->stream.p_selected_area = p_area;
383
384         /* Change the current track */
385         /* The first track is not a valid one  */
386         p_vcd->i_track = p_area->i_id;
387         p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
388     }
389
390     /* warn interface that something has changed */
391     p_input->stream.b_seekable = 1;
392     p_input->stream.b_changed = 1;
393
394     return 0;
395 }
396
397 /*****************************************************************************
398  * VCDRead: reads from the VCD into PES packets.
399  *****************************************************************************
400  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
401  * packets.
402  *****************************************************************************/
403 static int VCDRead( input_thread_t * p_input, data_packet_t ** pp_data )
404 {
405     thread_vcd_data_t *     p_vcd;
406     data_packet_t *         p_data;
407     int                     i_packet_size;
408     int                     i_index;
409     int                     i_packet;
410     u32                     i_header;
411     byte_t                  p_buffer[ VCD_DATA_SIZE ];
412     /* boolean_t               b_eoc; No chapters yet */
413
414     p_vcd = (thread_vcd_data_t *)p_input->p_plugin_data;
415
416     i_packet = 0;
417     *pp_data = NULL;
418
419     while( i_packet < VCD_DATA_ONCE && !p_vcd->b_end_of_track )
420     {
421         if ( VCDReadSector( p_vcd, p_buffer ) == -1 )
422         {
423             return -1;
424         }
425
426         i_index = 0;
427
428         while( i_index < BUFFER_SIZE - 6
429                 && !p_vcd->b_end_of_track
430                 && i_packet < VCD_DATA_ONCE )
431         {
432             i_header = U32_AT(p_buffer + i_index);
433
434             /* This is not the startcode of a packet. Read the stream
435              * until we find one. */
436             if( !i_header )
437             {
438                 /* It is common for MPEG-1 streams to pad with zeros
439                  * (although it is forbidden by the recommendation), so
440                  * don't bother everybody in this case. */
441                 intf_WarnMsg( 12, "vcd warning: garbage at input" );
442                 break;
443             }
444
445             while( (i_header & 0xFFFFFF00) != 0x100L
446                      && (++i_index < BUFFER_SIZE - 4) )
447             {
448                 i_header = U32_AT(p_buffer + i_index);
449             }
450
451             if( (i_header & 0xFFFFFF00) != 0x100L )
452             {
453                 intf_WarnMsg( 3, "vcd warning: no packet at sector %d\n",
454                                  p_vcd->i_sector - 1 );
455                 break; /* go to the next sector */
456             }
457 #ifdef DEBUG
458             intf_DbgMsg( "packet start code : %X\n", i_header );
459 #endif
460
461             switch( i_header )
462             {
463                 /* 0x1b9 == SYSTEM_END_CODE, it is only 4 bytes long. */
464                 case 0x1b9:
465                     i_packet_size = -2;
466                     break;
467
468                 /* Pack header */
469                 case 0x1ba:
470                     if( ( *( p_buffer + ( i_index + 4 ) ) & 0xC0) == 0x40 )
471                     {
472                         /* MPEG-2 */
473                         i_packet_size = 8;
474                     }
475                     else if( (*(p_buffer + ( i_index + 4 ) ) & 0xF0) == 0x20 )
476                     {
477                         /* MPEG-1 */
478                         i_packet_size = 6;
479                     }
480                     else
481                     {
482                         intf_ErrMsg( "Unable to determine stream type" );
483                         return( -1 );
484                     }
485                     break;
486
487                 /* The packet is at least 6 bytes long. */
488                 default:
489                     /* That's the case for all packets, except pack header. */
490                     i_packet_size = U16_AT((p_buffer + ( i_index + 4 )));
491                     break;
492             }
493
494             intf_DbgMsg("i_index : %d\n", i_index);
495             intf_DbgMsg("i_packet_size : %d\n", i_packet_size);
496
497             if ( i_index + i_packet_size > BUFFER_SIZE )
498             {
499                 intf_ErrMsg( "vcd error: packet too long (%i)",
500                              i_index + i_packet_size );
501                 continue;
502             }
503
504             /* Fetch a packet of the appropriate size. */
505             p_data = p_input->pf_new_packet( p_input->p_method_data,
506                                              i_packet_size + 6 );
507
508             if( p_data == NULL )
509             {
510                 intf_ErrMsg( "vcd error: out of memory" );
511                 return( -1 );
512             }
513
514             if( U32_AT(p_buffer) != 0x1B9 )
515             {
516                 p_main->fast_memcpy( p_data->p_demux_start, p_buffer + i_index,
517                                      6 + i_packet_size );
518                 i_index += ( 6 + i_packet_size );
519             }
520             else
521             {
522                 /* Copy the small header. */
523                 memcpy( p_data->p_demux_start, p_buffer + i_index, 4 );
524                 i_index += 4;
525             }
526
527             /* Give the packet to the other input stages. */
528             *pp_data = p_data;
529             pp_data = &p_data->p_next;
530
531             i_packet++;
532         }
533     }
534
535     vlc_mutex_lock( &p_input->stream.stream_lock );
536
537     p_input->stream.p_selected_area->i_tell =
538         p_vcd->i_sector - p_input->stream.p_selected_area->i_start;
539
540     /* no chapter for the moment*/
541     /*if( b_eoc )
542     {
543         * We modify i_part only at end of chapter not to erase
544          * some modification from the interface *
545         p_input->stream.p_selected_area->i_part = p_vcd->i_chapter;
546     }*/
547
548     if( p_vcd->b_end_of_track )
549     {
550         input_area_t *p_area;
551
552         /* EOF ? */
553         if( p_vcd->i_track >= p_vcd->nb_tracks - 1 )
554         {
555             vlc_mutex_unlock( &p_input->stream.stream_lock );
556             return 0;
557         }
558
559         intf_WarnMsg( 4, "vcd info: new title" );
560
561         p_vcd->b_end_of_track = 0;
562
563         p_area = p_input->stream.pp_areas[
564                                  p_input->stream.p_selected_area->i_id + 1 ];
565
566         p_area->i_part = 1;
567         VCDSetArea( p_input, p_area );
568     }
569
570     vlc_mutex_unlock( &p_input->stream.stream_lock );
571
572     return( i_packet + 1 );
573 }
574
575 /*****************************************************************************
576  * VCDRewind : reads a stream backward
577  *****************************************************************************/
578 static int VCDRewind( input_thread_t * p_input )
579 {
580     return( -1 );
581 }
582
583 /****************************************************************************
584  * VCDSeek
585  ****************************************************************************/
586 static void VCDSeek( input_thread_t * p_input, off_t i_off )
587 {
588     thread_vcd_data_t *               p_vcd;
589
590     p_vcd = (thread_vcd_data_t *) p_input->p_plugin_data;
591
592     p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track] + i_off;
593
594     p_input->stream.p_selected_area->i_tell = p_vcd->i_sector
595         - p_input->stream.p_selected_area->i_start;
596 }
597