]> git.sesse.net Git - vlc/blob - plugins/vcd/input_vcd.c
2a0a789671b68fb2e592079cb253ba0145e7744c
[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 "../mpeg_system/input_ps.h"
74 #include "input_vcd.h"
75 #include "linux_cdrom_tools.h"
76
77 /* how many blocks VCDRead will read in each loop */
78 #define VCD_BLOCKS_ONCE 64
79 #define VCD_DATA_ONCE  (2 * VCD_BLOCKS_ONCE)
80 #define BUFFER_SIZE VCD_DATA_SIZE
81
82
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 /* called from outside */
88 static int  VCDProbe        ( probedata_t *p_data );
89 static void VCDInit         ( struct input_thread_s * );
90 static int  VCDRead         ( struct input_thread_s *, data_packet_t ** );
91 static int  VCDSetArea      ( struct input_thread_s *, struct input_area_s * ); 
92 static int  VCDSetProgram   ( struct input_thread_s *, pgrm_descriptor_t * ); 
93 static void VCDOpen         ( struct input_thread_s *);
94 static void VCDClose         ( struct input_thread_s *);
95 static void VCDEnd          ( struct input_thread_s *);
96 static void VCDSeek         ( struct input_thread_s *, off_t );
97 static int  VCDRewind       ( struct input_thread_s * );
98
99 /*****************************************************************************
100  * Declare a buffer manager
101  *****************************************************************************/
102 #define FLAGS           BUFFERS_NOFLAGS
103 #define NB_LIFO         2
104 DECLARE_BUFFERS_EMBEDDED( FLAGS, NB_LIFO );
105 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
106 DECLARE_BUFFERS_END( FLAGS, NB_LIFO );
107 DECLARE_BUFFERS_NEWPACKET( FLAGS, NB_LIFO );
108 DECLARE_BUFFERS_DELETEPACKET( FLAGS, NB_LIFO, 150 );
109 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
110 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 150 );
111
112
113 /*****************************************************************************
114  * Functions exported as capabilities. They are declared as static so that
115  * we don't pollute the namespace too much.
116  *****************************************************************************/
117 void _M( input_getfunctions )( function_list_t * p_function_list )
118 {
119 #define input p_function_list->functions.input
120     p_function_list->pf_probe = VCDProbe;
121     input.pf_init             = VCDInit;
122     input.pf_open             = VCDOpen;
123     input.pf_close            = VCDClose;
124     input.pf_end              = VCDEnd;
125     input.pf_init_bit_stream  = InitBitstream;
126     input.pf_read             = VCDRead;
127     input.pf_set_area         = VCDSetArea;
128     input.pf_set_program      = VCDSetProgram;
129     input.pf_demux            = input_DemuxPS;
130     input.pf_new_packet       = input_NewPacket;
131     input.pf_new_pes          = input_NewPES;
132     input.pf_delete_packet    = input_DeletePacket;
133     input.pf_delete_pes       = input_DeletePES;
134     input.pf_rewind           = VCDRewind;
135     input.pf_seek             = VCDSeek;
136 #undef input
137 }
138
139 /*
140  * Data reading functions
141  */
142
143 /*****************************************************************************
144  * VCDProbe: verifies that the stream is a PS stream
145  *****************************************************************************/
146 static int VCDProbe( probedata_t *p_data )
147 {
148     
149     input_thread_t * p_input = (input_thread_t *)p_data;
150
151     char * psz_name = p_input->p_source;
152     int i_score = 5;
153
154     if( TestMethod( INPUT_METHOD_VAR, "vcd" ) )
155     {
156         return( 999 );
157     }
158
159     if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "vcd:", 4 ) )
160     {
161         /* If the user specified "vcd:" then it's probably a VCD */
162         i_score = 100;
163         psz_name += 4;
164     }
165     return( i_score );
166 }
167
168 /*****************************************************************************
169  * VCDOpen: open vcd
170  *****************************************************************************/
171 static void VCDOpen( struct input_thread_s *p_input )
172 {
173     int vcdhandle;
174
175     vlc_mutex_lock( &p_input->stream.stream_lock );
176
177     /* If we are here we can control the pace... */
178     p_input->stream.b_pace_control = 1;
179
180     p_input->stream.b_seekable = 1;
181     p_input->stream.p_selected_area->i_size = 0;
182
183     p_input->stream.p_selected_area->i_tell = 0;
184
185     vlc_mutex_unlock( &p_input->stream.stream_lock );
186     
187     
188     /* XXX: put this shit in an access plugin */
189     if( strlen( p_input->p_source ) > 4
190          && !strncasecmp( p_input->p_source, "vcd:", 4 ) )
191     {
192         vcdhandle = open( p_input->p_source + 4, O_RDONLY | O_NONBLOCK );
193     }
194     else
195     {
196         vcdhandle = open( p_input->p_source + 4, O_RDONLY | O_NONBLOCK );
197     }
198
199     if( vcdhandle == -1 )
200     {
201         p_input->b_error = 1;
202         return;
203     }
204
205     p_input->i_handle = (int) vcdhandle;
206 }
207
208 /*****************************************************************************
209  * VCDClose: close vcd
210  *****************************************************************************/
211 static void VCDClose( struct input_thread_s *p_input )
212 {
213     close( p_input->i_handle );
214 }
215
216 /*****************************************************************************
217  * VCDInit: initializes VCD structures
218  *****************************************************************************/
219 static void VCDInit( input_thread_t * p_input )
220 {
221     thread_vcd_data_t *  p_vcd;
222     int                  i_title;
223     int                  i_chapter;
224     int                  i;
225     input_area_t *       p_area;
226     es_descriptor_t *    p_es;
227     packet_cache_t *     p_packet_cache;
228     
229     p_vcd = malloc( sizeof(thread_vcd_data_t) );
230         
231     if( p_vcd == NULL )
232     {
233         intf_ErrMsg( "vcd error: out of memory" );
234         p_input->b_error = 1;
235         return;
236     }
237
238     p_input->p_plugin_data = (void *)p_vcd;
239
240     if( (p_input->p_method_data = input_BuffersInit()) == NULL )
241     {
242         p_input->b_error = 1;
243         return;
244     }
245
246     p_vcd->vcdhandle = p_input->i_handle;
247     p_vcd->b_end_of_track = 0;
248
249     /* we read the Table Of Content information */
250     if ( read_toc(p_vcd) == -1 )
251     {
252         intf_ErrMsg("An error occured when reading vcd's TOC");
253     }
254     
255     p_input->i_read_once = VCD_DATA_ONCE;
256     
257     /* Set stream and area data */
258     vlc_mutex_lock( &p_input->stream.stream_lock );
259
260     /* Initialize ES structures */
261     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
262
263     /* disc input method */
264     p_input->stream.i_method = INPUT_METHOD_VCD;
265
266
267 #define area p_input->stream.pp_areas
268     for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
269     {
270         input_AddArea( p_input );
271
272         /* Titles are Program Chains */
273         area[i]->i_id = i;
274
275         /* Absolute start offset and size */
276         area[i]->i_start = p_vcd->tracks_sector[i];
277         area[i]->i_size = p_vcd->tracks_sector[i+1] - p_vcd->tracks_sector[i];
278
279         /* Number of chapters */
280         area[i]->i_part_nb = 0;   // will be the entry points
281         area[i]->i_part = 1;
282
283         /* Number of angles */
284         area[i]->i_angle_nb = 1; // no angle support in VCDs
285         area[i]->i_angle = 1;
286
287         area[i]->i_plugin_data = p_vcd->tracks_sector[i];
288     }   
289 #undef area
290
291     /* Get requested title - if none try the first title */
292     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
293     if( i_title <= 0)
294     {
295         i_title = 1;
296     }
297     
298     // p_vcd->current_track = i_title-1 ;
299     
300     /* Get requested chapter - if none defaults to first one */
301     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
302     if( i_chapter <= 0 )
303     {
304         i_chapter = 1;
305     }
306
307     
308     p_input->stream.pp_areas[i_title]->i_part = i_chapter;
309
310     p_area = p_input->stream.pp_areas[i_title];
311
312     VCDSetArea( p_input, p_area );
313
314     /* Set program information. */
315
316     input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
317     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
318
319     /* No PSM to read in disc mode, we already have all information */
320     p_input->stream.p_selected_program->b_is_ok = 1;
321
322     p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
323     p_es->i_stream_id = 0xe0;
324     p_es->i_type = MPEG1_VIDEO_ES;
325     p_es->i_cat = VIDEO_ES;
326     
327     if( p_main->b_video )
328     {
329         input_SelectES( p_input, p_es );
330     }
331     
332     p_es = input_AddES( p_input,
333                 p_input->stream.p_selected_program, 0xc0, 0 );
334     p_es->i_stream_id = 0xc0;
335     p_es->i_type = MPEG1_AUDIO_ES;
336     p_es->b_audio = 1;
337     p_es->i_cat = AUDIO_ES;
338      
339     if( p_main->b_audio )
340     {
341         input_SelectES( p_input, p_es );
342     }
343     
344     vlc_mutex_unlock( &p_input->stream.stream_lock );
345
346     
347     return;
348 }
349
350
351 /*****************************************************************************
352  * VCDEnd: frees unused data
353  *****************************************************************************/
354 static void VCDEnd( input_thread_t * p_input )
355 {
356     thread_vcd_data_t *     p_vcd;
357
358     p_vcd = (thread_vcd_data_t*)p_input->p_plugin_data;
359
360     free( p_vcd );
361
362     input_BuffersEnd( p_input->p_method_data );
363 }
364
365 /*****************************************************************************
366  * VCDSetProgram: Does nothing since a VCD is mono_program
367  *****************************************************************************/
368 static int VCDSetProgram( input_thread_t * p_input, 
369             pgrm_descriptor_t * p_program)
370 {
371     return 0;
372 }
373
374
375 /*****************************************************************************
376  * VCDSetArea: initialize input data for title x, chapter y.
377  * It should be called for each user navigation request.
378  ****************************************************************************/
379 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
380 {
381     thread_vcd_data_t *     p_vcd;
382     
383     p_vcd = (thread_vcd_data_t*)p_input->p_plugin_data;
384     
385     /* we can't use the interface slider until initilization is complete */
386     p_input->stream.b_seekable = 0;
387     
388     if ( p_area != p_input->stream.p_selected_area ) 
389     {
390         /* Reset the Chapter position of the old title */
391         p_input->stream.p_selected_area->i_part = 0;
392         p_input->stream.p_selected_area->i_tell = 0;
393         
394         /* Change the default area */
395         p_input->stream.p_selected_area = p_area;
396
397         /* Change the current track */
398         /* The first track is not a valid one  */
399         p_vcd->current_track = p_area->i_id ;
400         p_vcd->current_sector = p_vcd->tracks_sector[p_vcd->current_track] ;
401     }
402     /* warn interface that something has changed */
403     p_input->stream.b_seekable = 1;
404     p_input->stream.b_changed = 1;
405     return 0 ;
406     
407 }
408
409
410
411 /*****************************************************************************
412  * VCDRead: reads from the VCD into PES packets.
413  *****************************************************************************
414  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
415  * EOF.
416  *****************************************************************************/
417 static int VCDRead( input_thread_t * p_input,
418                     data_packet_t ** pp_packets )
419 {
420     thread_vcd_data_t *     p_vcd;
421     data_packet_t *         p_data;
422     int                     i_packet_size;
423     int                     i_index;
424     int                     i_packet;   
425     boolean_t               b_eof;
426     byte_t *                p_buffer;
427     boolean_t                  b_no_packet;
428     /* boolean_t               b_eoc; No chapters yet */
429     
430     p_vcd = (thread_vcd_data_t *)p_input->p_plugin_data;
431    
432
433     p_buffer = malloc ( VCD_DATA_SIZE );
434
435     if ( p_buffer == NULL )
436     {
437         intf_ErrMsg("Could not malloc the read buffer");
438         return -1;
439     }
440
441     
442     i_packet = 0;
443     b_no_packet = 0;
444
445     while( i_packet < VCD_DATA_ONCE ) 
446     {
447         i_index = 0;
448         
449         if ( VCD_sector_read( p_vcd, p_buffer ) == -1 )
450         {
451               return -1;
452         }
453         
454         while (i_index < BUFFER_SIZE - 6) 
455         {
456             
457             if( (U32_AT(p_buffer + i_index) & 0xFFFFFF00) != 0x100L )
458             {
459                 /* This is not the startcode of a packet. Read the stream
460                  * until we find one. */
461     
462                 if( !U32_AT( p_buffer + i_index ) )
463                 {
464                     /* It is common for MPEG-1 streams to pad with zeros
465                      * (although it is forbidden by the recommendation), so
466                      * don't bother everybody in this case. */
467                     intf_WarnMsg( 3, "Garbage at input" );
468                 }
469     
470                 while( ( (U32_AT(p_buffer + i_index) & 0xFFFFFF00) != 0x100L )
471                        && ( i_index < BUFFER_SIZE - 4 ) )
472                 {
473                     i_index ++;
474                 }
475     
476                 if ( i_index == BUFFER_SIZE - 4 )
477                 {
478                     b_no_packet = 1;
479                 }
480                 /* Packet found. */
481             }
482             
483             if (b_no_packet)
484             {
485                 b_no_packet = 0;
486                 intf_WarnMsg(3, "No packet found on sector %d\n", 
487                             p_vcd->current_sector -1 );
488                 break; /* go to the next sector */
489             }
490             
491 #ifdef DEBUG
492             intf_DbgMsg("packet start code : %X\n", 
493                         U32_AT(p_buffer + i_index));
494 #endif
495             /* 0x1B9 == SYSTEM_END_CODE, it is only 4 bytes long. */
496             if( U32_AT(p_buffer + i_index) != 0x1B9 )
497             {
498                 /* The packet is at least 6 bytes long. */
499     
500                 if( U32_AT(p_buffer + i_index) != 0x1BA )
501                 {
502                     /* That's the case for all packets, except pack header. */
503                     i_packet_size = U16_AT((p_buffer + ( i_index + 4 )));
504                 }
505                 else
506                 {
507                     /* Pack header. */
508                     if( ( *( p_buffer + ( i_index + 4 ) ) & 0xC0) == 0x40 )
509                     {
510                         /* MPEG-2 */
511                         i_packet_size = 8;
512                     }
513                     else if( (*(p_buffer + ( i_index + 4 ) ) & 0xF0) == 0x20 )
514                     {
515                         /* MPEG-1 */
516                         i_packet_size = 6;
517                     }
518                     else
519                     {
520                         intf_ErrMsg( "Unable to determine stream type" );
521                         return( -1 );
522                     }
523                 }
524             }
525             else
526             {
527                 /* System End Code */
528                 i_packet_size = -2;
529             }
530 #ifdef DEBUG
531             intf_DbgMsg("i_index : %d\n", i_index);
532             intf_DbgMsg("i_packet_size : %d\n", i_packet_size);
533 #endif
534             if ( i_index + i_packet_size > BUFFER_SIZE )
535             {
536                 intf_ErrMsg( "Too long packet");
537                 continue;
538             }
539             
540             /* Fetch a packet of the appropriate size. */
541             
542             p_data = p_input->pf_new_packet( p_input->p_method_data,
543                                              i_packet_size + 6 );
544             
545             if( p_data == NULL )
546             {
547                 intf_ErrMsg( "Out of memory" );
548                 return( -1 );
549             }
550     
551             if( U32_AT(p_buffer) != 0x1B9 )
552             {
553                 p_main->fast_memcpy( p_data->p_demux_start, p_buffer + i_index,
554                                      6 + i_packet_size );
555                 i_index += ( 6 + i_packet_size );
556     
557             }
558             else
559             {
560                 /* Copy the small header. */
561                 memcpy( p_data->p_demux_start, p_buffer + i_index, 4 );
562                 i_index += 4;
563             }
564     
565             /* Give the packet to the other input stages. */
566             pp_packets[i_packet] = p_data;
567             i_packet ++;
568         }
569         
570         if ( p_vcd->b_end_of_track )
571             break;
572     }
573
574
575     
576     vlc_mutex_lock( &p_input->stream.stream_lock );
577
578     p_input->stream.p_selected_area->i_tell =
579         p_vcd->current_sector -
580         p_input->stream.p_selected_area->i_start ;
581     
582     /* no chapter for the moment*/
583     /*if( b_eoc )
584     {
585         * We modify i_part only at end of chapter not to erase
586          * some modification from the interface *
587         p_input->stream.p_selected_area->i_part = p_vcd->i_chapter;
588     }*/
589
590     
591     b_eof = p_vcd->b_end_of_track; 
592         /*FIXME&& ( ( p_vcd->current_track ) >= p_vcd->nb_tracks - 1);*/
593
594     if( b_eof )
595     {
596         vlc_mutex_unlock( &p_input->stream.stream_lock );
597         return 1;
598     }
599
600     if( p_vcd->b_end_of_track )
601     {
602         intf_WarnMsg( 4, "vcd info: new title" );
603         p_vcd->b_end_of_track = 0;
604         VCDSetArea( p_input, p_input->stream.pp_areas[
605                      p_input->stream.p_selected_area->i_id + 1] );
606         vlc_mutex_unlock( &p_input->stream.stream_lock );
607         return 0;
608     }
609     
610     vlc_mutex_unlock( &p_input->stream.stream_lock );
611
612
613     return 0;
614 }
615
616 /*****************************************************************************
617  * VCDRewind : reads a stream backward
618  *****************************************************************************/
619 static int VCDRewind( input_thread_t * p_input )
620 {
621     return( -1 );
622 }
623
624
625 /****************************************************************************
626  * VCDSeek
627  ****************************************************************************/
628 static void VCDSeek( input_thread_t * p_input, off_t i_off )
629 {
630     thread_vcd_data_t *               p_vcd;
631
632     p_vcd = (thread_vcd_data_t *) p_input->p_plugin_data;
633
634     p_vcd->current_sector = p_vcd->tracks_sector[p_vcd->current_track]
635                                 + i_off;
636
637     p_input->stream.p_selected_area->i_tell = p_vcd->current_sector
638         - p_input->stream.p_selected_area->i_start;
639     
640     return ;
641 }
642