]> git.sesse.net Git - vlc/blob - plugins/vcd/input_vcd.c
* ./plugins/beos/*: BeOS fixes from Rudolf Cornelissen.
[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 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include <videolan/vlc.h>
30
31 #ifdef HAVE_UNISTD_H
32 #   include <unistd.h>
33 #endif
34
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #ifdef STRNCASECMP_IN_STRINGS_H
41 #   include <strings.h>
42 #endif
43
44 #if defined( WIN32 )
45 #   include <io.h>                                                 /* read() */
46 #else
47 #   include <sys/uio.h>                                      /* struct iovec */
48 #endif
49
50 #if defined( WIN32 )
51 #   include "input_iovec.h"
52 #endif
53
54 #include "stream_control.h"
55 #include "input_ext-intf.h"
56 #include "input_ext-dec.h"
57 #include "input_ext-plugins.h"
58
59 #include "debug.h"
60
61 #include "input_vcd.h"
62 #include "linux_cdrom_tools.h"
63
64 /* how many blocks VCDRead will read in each loop */
65 #define VCD_BLOCKS_ONCE 4
66 #define VCD_DATA_ONCE   (2 * VCD_BLOCKS_ONCE)
67 #define BUFFER_SIZE VCD_DATA_SIZE
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 /* called from outside */
73 static int  VCDProbe        ( probedata_t *p_data );
74 static void VCDInit         ( struct input_thread_s * );
75 static int  VCDRead         ( struct input_thread_s *, data_packet_t ** );
76 static int  VCDSetArea      ( struct input_thread_s *, struct input_area_s * );
77 static int  VCDSetProgram   ( struct input_thread_s *, pgrm_descriptor_t * );
78 static void VCDOpen         ( struct input_thread_s *);
79 static void VCDClose        ( struct input_thread_s *);
80 static void VCDEnd          ( struct input_thread_s *);
81 static void VCDSeek         ( struct input_thread_s *, off_t );
82 static int  VCDRewind       ( struct input_thread_s * );
83
84 /*****************************************************************************
85  * Declare a buffer manager
86  *****************************************************************************/
87 #define FLAGS           BUFFERS_NOFLAGS
88 #define NB_LIFO         2
89 DECLARE_BUFFERS_EMBEDDED( FLAGS, NB_LIFO );
90 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
91 DECLARE_BUFFERS_END( FLAGS, NB_LIFO );
92 DECLARE_BUFFERS_NEWPACKET( FLAGS, NB_LIFO );
93 DECLARE_BUFFERS_DELETEPACKET( FLAGS, NB_LIFO, 1000 );
94 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
95 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 1000 );
96
97
98 /*****************************************************************************
99  * Functions exported as capabilities. They are declared as static so that
100  * we don't pollute the namespace too much.
101  *****************************************************************************/
102 void _M( input_getfunctions )( function_list_t * p_function_list )
103 {
104 #define input p_function_list->functions.input
105     p_function_list->pf_probe = VCDProbe;
106     input.pf_init             = VCDInit;
107     input.pf_open             = VCDOpen;
108     input.pf_close            = VCDClose;
109     input.pf_end              = VCDEnd;
110     input.pf_init_bit_stream  = InitBitstream;
111     input.pf_read             = VCDRead;
112     input.pf_set_area         = VCDSetArea;
113     input.pf_set_program      = VCDSetProgram;
114     input.pf_demux            = input_DemuxPS;
115     input.pf_new_packet       = input_NewPacket;
116     input.pf_new_pes          = input_NewPES;
117     input.pf_delete_packet    = input_DeletePacket;
118     input.pf_delete_pes       = input_DeletePES;
119     input.pf_rewind           = VCDRewind;
120     input.pf_seek             = VCDSeek;
121 #undef input
122 }
123
124 /*
125  * Data reading functions
126  */
127
128 /*****************************************************************************
129  * VCDProbe: verifies that the stream is a PS stream
130  *****************************************************************************/
131 static int VCDProbe( probedata_t *p_data )
132 {
133     input_thread_t * p_input = (input_thread_t *)p_data;
134
135     char * psz_name = p_input->p_source;
136     int i_score = 0;
137
138     if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "vcd:", 4 ) )
139     {
140         /* If the user specified "vcd:" then it's probably a VCD */
141         i_score = 100;
142         psz_name += 4;
143     }
144
145     return( i_score );
146 }
147
148 /*****************************************************************************
149  * VCDOpen: open vcd
150  *****************************************************************************/
151 static void VCDOpen( struct input_thread_s *p_input )
152 {
153     vlc_mutex_lock( &p_input->stream.stream_lock );
154
155     /* If we are here we can control the pace... */
156     p_input->stream.b_pace_control = 1;
157
158     p_input->stream.b_seekable = 1;
159     p_input->stream.p_selected_area->i_size = 0;
160     p_input->stream.p_selected_area->i_tell = 0;
161
162     vlc_mutex_unlock( &p_input->stream.stream_lock );
163
164     /* XXX: put this shit in an access plugin */
165     if( strlen( p_input->p_source ) > 4
166          && !strncasecmp( p_input->p_source, "vcd:", 4 ) )
167     {
168         p_input->i_handle
169             = open( p_input->p_source + 4, O_RDONLY | O_NONBLOCK );
170     }
171     else
172     {
173         p_input->i_handle
174             = open( p_input->p_source, O_RDONLY | O_NONBLOCK );
175     }
176
177     if( p_input->i_handle == -1 )
178     {
179         p_input->b_error = 1;
180     }
181 }
182
183 /*****************************************************************************
184  * VCDClose: close vcd
185  *****************************************************************************/
186 static void VCDClose( struct input_thread_s *p_input )
187 {
188     close( p_input->i_handle );
189 }
190
191 /*****************************************************************************
192  * VCDInit: initializes VCD structures
193  *****************************************************************************/
194 static void VCDInit( input_thread_t * p_input )
195 {
196     thread_vcd_data_t *  p_vcd;
197     int                  i_title;
198     int                  i_chapter;
199     int                  i;
200     input_area_t *       p_area;
201     es_descriptor_t *    p_es;
202
203     p_vcd = malloc( sizeof(thread_vcd_data_t) );
204
205     if( p_vcd == NULL )
206     {
207         intf_ErrMsg( "vcd error: out of memory" );
208         p_input->b_error = 1;
209         return;
210     }
211
212     p_input->p_plugin_data = (void *)p_vcd;
213
214     if( (p_input->p_method_data = input_BuffersInit()) == NULL )
215     {
216         free( p_vcd );
217         p_input->b_error = 1;
218         return;
219     }
220
221     p_vcd->i_handle = p_input->i_handle;
222
223     /* We read the Table Of Content information */
224     p_vcd->nb_tracks = ioctl_GetTrackCount( p_input->i_handle );
225     if( p_vcd->nb_tracks < 0 )
226     {
227         input_BuffersEnd( p_input->p_method_data );
228         free( p_vcd );
229         p_input->b_error = 1;
230         return;
231     }
232     else if( p_vcd->nb_tracks <= 1 )
233     {
234         intf_ErrMsg( "input error: no movie tracks found" );
235         input_BuffersEnd( p_input->p_method_data );
236         free( p_vcd );
237         p_input->b_error = 1;
238         return;
239     }
240
241     p_vcd->p_sectors = ioctl_GetSectors( p_input->i_handle );
242     if ( p_vcd->p_sectors == NULL )
243     {
244         input_BuffersEnd( p_input->p_method_data );
245         free( p_vcd );
246         p_input->b_error = 1;
247         return;
248     }
249
250     /* Set stream and area data */
251     vlc_mutex_lock( &p_input->stream.stream_lock );
252
253     /* Initialize ES structures */
254     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
255
256     /* disc input method */
257     p_input->stream.i_method = INPUT_METHOD_VCD;
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 = (off_t)p_vcd->p_sectors[i] * (off_t)VCD_DATA_SIZE;
269         area[i]->i_size = (off_t)(p_vcd->p_sectors[i+1] - p_vcd->p_sectors[i])
270                            * (off_t)VCD_DATA_SIZE;
271
272         /* Number of chapters */
273         area[i]->i_part_nb = 0;   // will be the entry points
274         area[i]->i_part = 1;
275
276         /* Number of angles */
277         area[i]->i_angle_nb = 1; // no angle support in VCDs
278         area[i]->i_angle = 1;
279
280         area[i]->i_plugin_data = p_vcd->p_sectors[i];
281     }
282 #undef area
283
284     /* Get requested title - if none try the first title */
285     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
286     if( i_title <= 0 )
287     {
288         i_title = 1;
289     }
290
291     // p_vcd->i_track = i_title-1;
292
293     /* Get requested chapter - if none defaults to first one */
294     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
295     if( i_chapter <= 0 )
296     {
297         i_chapter = 1;
298     }
299
300     p_input->stream.pp_areas[i_title]->i_part = i_chapter;
301
302     p_area = p_input->stream.pp_areas[i_title];
303
304     VCDSetArea( p_input, p_area );
305
306     /* Set program information. */
307
308     input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
309     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
310
311     /* No PSM to read in disc mode, we already have all the information */
312     p_input->stream.p_selected_program->b_is_ok = 1;
313
314     p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
315     p_es->i_stream_id = 0xe0;
316     p_es->i_type = MPEG1_VIDEO_ES;
317     p_es->i_cat = VIDEO_ES;
318
319     if( p_main->b_video )
320     {
321         input_SelectES( p_input, p_es );
322     }
323
324     p_es = input_AddES( p_input, 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     input_BuffersEnd( p_input->p_method_data );
346
347     p_vcd = (thread_vcd_data_t*)p_input->p_plugin_data;
348
349     free( p_vcd );
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_eot = 0; /* end of track */
413     /* boolean_t               b_eoc; No chapters yet */
414
415     p_vcd = (thread_vcd_data_t *)p_input->p_plugin_data;
416
417     i_packet = 0;
418     *pp_data = NULL;
419
420     while( i_packet < VCD_DATA_ONCE )
421     {
422         if( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector, p_buffer ) )
423         {
424             /* Read error, but assume we reached the end of the track */
425             b_eot = 1;
426             break;
427         }
428
429         p_vcd->i_sector++;
430
431         if( p_vcd->i_sector >= p_vcd->p_sectors[p_vcd->i_track + 1] )
432         {
433             b_eot = 1;
434             break;
435         }
436
437         i_index = 0;
438
439         while( i_index < BUFFER_SIZE-6 && i_packet < VCD_DATA_ONCE )
440         {
441             i_header = U32_AT(p_buffer + i_index);
442
443             /* It is common for MPEG-1 streams to pad with zeros
444              * (although it is forbidden by the recommendation), so
445              * don't bother everybody in this case. */
446             while( !i_header && (++i_index < BUFFER_SIZE - 4) )
447             {
448                 i_header = U32_AT(p_buffer + i_index);
449             }
450
451             if( !i_header )
452             {
453                 intf_WarnMsg( 12, "vcd warning: zero-padded packet" );
454                 break;
455             }
456
457             /* Read the stream until we find a startcode. */
458             while( (i_header & 0xFFFFFF00) != 0x100L
459                      && (++i_index < BUFFER_SIZE - 4) )
460             {
461                 i_header = U32_AT(p_buffer + i_index);
462             }
463
464             if( (i_header & 0xFFFFFF00) != 0x100L )
465             {
466                 intf_WarnMsg( 3, "vcd warning: no packet at sector %d",
467                                  p_vcd->i_sector - 1 );
468                 break; /* go to the next sector */
469             }
470
471             intf_DbgMsg( "packet start code : %X", i_header );
472
473             switch( i_header )
474             {
475                 /* 0x1b9 == SYSTEM_END_CODE, it is only 4 bytes long. */
476                 case 0x1b9:
477                     i_packet_size = -2;
478                     break;
479
480                 /* Pack header */
481                 case 0x1ba:
482                     if( ( *( p_buffer + ( i_index + 4 ) ) & 0xC0) == 0x40 )
483                     {
484                         /* MPEG-2 */
485                         i_packet_size = 8;
486                     }
487                     else if( (*(p_buffer + ( i_index + 4 ) ) & 0xF0) == 0x20 )
488                     {
489                         /* MPEG-1 */
490                         i_packet_size = 6;
491                     }
492                     else
493                     {
494                         intf_ErrMsg( "vcd error: unable to determine "
495                                      "stream type" );
496                         return( -1 );
497                     }
498                     break;
499
500                 /* The packet is at least 6 bytes long. */
501                 default:
502                     /* That's the case for all packets, except pack header. */
503                     i_packet_size = U16_AT((p_buffer + ( i_index + 4 )));
504                     break;
505             }
506
507             intf_DbgMsg( "i_index : %d", i_index );
508             intf_DbgMsg( "i_packet_size : %d", i_packet_size );
509
510             if ( i_index + i_packet_size > BUFFER_SIZE )
511             {
512                 intf_ErrMsg( "vcd error: packet too long (%i)",
513                              i_index + i_packet_size );
514                 continue;
515             }
516
517             /* Fetch a packet of the appropriate size. */
518             p_data = p_input->pf_new_packet( p_input->p_method_data,
519                                              i_packet_size + 6 );
520
521             if( p_data == NULL )
522             {
523                 intf_ErrMsg( "vcd error: out of memory" );
524                 return( -1 );
525             }
526
527             if( U32_AT(p_buffer) != 0x1B9 )
528             {
529                 FAST_MEMCPY( p_data->p_demux_start, p_buffer + i_index,
530                              6 + i_packet_size );
531                 i_index += ( 6 + i_packet_size );
532             }
533             else
534             {
535                 /* Copy the small header. */
536                 memcpy( p_data->p_demux_start, p_buffer + i_index, 4 );
537                 i_index += 4;
538             }
539
540             /* Give the packet to the other input stages. */
541             *pp_data = p_data;
542             pp_data = &p_data->p_next;
543
544             i_packet++;
545         }
546     }
547
548     vlc_mutex_lock( &p_input->stream.stream_lock );
549
550     p_input->stream.p_selected_area->i_tell =
551         (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
552           - p_input->stream.p_selected_area->i_start;
553
554     /* no chapter for the moment*/
555 #if 0
556     if( b_eoc )
557     {
558         /* We modify i_part only at end of chapter not to erase
559          * some modification from the interface */
560         p_input->stream.p_selected_area->i_part = p_vcd->i_chapter;
561     }
562 #endif
563
564     if( b_eot )
565     {
566         input_area_t *p_area;
567
568         /* EOF ? */
569         if( p_vcd->i_track >= p_vcd->nb_tracks - 1 )
570         {
571             vlc_mutex_unlock( &p_input->stream.stream_lock );
572             return 0;
573         }
574
575         intf_WarnMsg( 4, "vcd info: new title" );
576
577         p_area = p_input->stream.pp_areas[
578                                  p_input->stream.p_selected_area->i_id + 1 ];
579
580         p_area->i_part = 1;
581         VCDSetArea( p_input, p_area );
582     }
583
584     vlc_mutex_unlock( &p_input->stream.stream_lock );
585
586     return( i_packet + 1 );
587 }
588
589 /*****************************************************************************
590  * VCDRewind : reads a stream backward
591  *****************************************************************************/
592 static int VCDRewind( input_thread_t * p_input )
593 {
594     return( -1 );
595 }
596
597 /****************************************************************************
598  * VCDSeek
599  ****************************************************************************/
600 static void VCDSeek( input_thread_t * p_input, off_t i_off )
601 {
602     thread_vcd_data_t *               p_vcd;
603
604     p_vcd = (thread_vcd_data_t *) p_input->p_plugin_data;
605
606     p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
607                        + i_off / (off_t)VCD_DATA_SIZE;
608
609     p_input->stream.p_selected_area->i_tell = 
610         (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
611          - p_input->stream.p_selected_area->i_start;
612 }
613