]> git.sesse.net Git - vlc/blob - modules/access/vcd/vcd.c
57e1cbf534f479344e134126ae312cffa30296be
[vlc] / modules / access / vcd / vcd.c
1 /*****************************************************************************
2  * vcd.c : VCD input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: vcd.c,v 1.22 2003/05/22 12:00:57 gbazin Exp $
6  *
7  * Author: Johan Bilien <jobi@via.ecp.fr>
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 <stdio.h>
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "../../demux/mpeg/system.h"
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>
37 #endif
38
39 #include <string.h>
40
41 #include "cdrom.h"
42
43 /* how many blocks VCDRead will read in each loop */
44 #define VCD_BLOCKS_ONCE 20
45 #define VCD_DATA_ONCE   (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)
46
47 /*****************************************************************************
48  * thread_vcd_data_t: VCD information
49  *****************************************************************************/
50 typedef struct thread_vcd_data_s
51 {
52     vcddev_t    *vcddev;                            /* vcd device descriptor */
53     int         i_nb_tracks;                        /* Nb of tracks (titles) */
54     int         i_track;                                    /* Current track */
55     int         i_sector;                                  /* Current Sector */
56     int *       p_sectors;                                  /* Track sectors */
57     int         i_entries_nb;                      /* Number of entry points */
58     int *       p_entries;                                   /* Entry points */
59     vlc_bool_t  b_valid_ep;                       /* Valid entry points flag */
60     vlc_bool_t  b_end_of_track;           /* If the end of track was reached */
61
62 } thread_vcd_data_t;
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static int  VCDOpen         ( vlc_object_t * );
68 static void VCDClose        ( vlc_object_t * );
69 static int  VCDRead         ( input_thread_t *, byte_t *, size_t );
70 static void VCDSeek         ( input_thread_t *, off_t );
71 static int  VCDSetArea      ( input_thread_t *, input_area_t * );
72 static int  VCDSetProgram   ( input_thread_t *, pgrm_descriptor_t * );
73 static int  VCDEntryPoints  ( input_thread_t * );
74
75 /*****************************************************************************
76  * Module descriptior
77  *****************************************************************************/
78 vlc_module_begin();
79     set_description( _("VCD input") );
80     set_capability( "access", 80 );
81     set_callbacks( VCDOpen, VCDClose );
82     add_shortcut( "svcd" );
83 vlc_module_end();
84
85 /*
86  * Data reading functions
87  */
88
89 /*****************************************************************************
90  * VCDOpen: open vcd
91  *****************************************************************************/
92 static int VCDOpen( vlc_object_t *p_this )
93 {
94     input_thread_t *        p_input = (input_thread_t *)p_this;
95     char *                  psz_orig;
96     char *                  psz_parser;
97     char *                  psz_source;
98     char *                  psz_next;
99     thread_vcd_data_t *     p_vcd;
100     int                     i;
101     input_area_t *          p_area;
102     int                     i_title = 1;
103     int                     i_chapter = 1;
104     vcddev_t                *vcddev;
105
106     /* parse the options passed in command line : */
107     psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
108
109     if( !psz_orig )
110     {
111         return( -1 );
112     }
113
114     while( *psz_parser && *psz_parser != '@' )
115     {
116         psz_parser++;
117     }
118
119     if( *psz_parser == '@' )
120     {
121         /* Found options */
122         *psz_parser = '\0';
123         ++psz_parser;
124
125         i_title = (int)strtol( psz_parser, &psz_next, 10 );
126         if( *psz_next )
127         {
128             psz_parser = psz_next + 1;
129             i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
130         }
131
132         i_title = i_title ? i_title : 1;
133         i_chapter = i_chapter ? i_chapter : 1;
134     }
135
136     if( !*psz_source )
137     {
138         if( !p_input->psz_access )
139         {
140             free( psz_orig );
141             return -1;
142         }
143         psz_source = config_GetPsz( p_input, "vcd" );
144         if( !psz_source ) return -1;
145     }
146
147     /* Open VCD */
148     if( !(vcddev = ioctl_Open( p_this, psz_source )) )
149     {
150         msg_Warn( p_input, "could not open %s", psz_source );
151         free( psz_source );
152         return -1;
153     }
154
155     p_vcd = malloc( sizeof(thread_vcd_data_t) );
156     if( p_vcd == NULL )
157     {
158         msg_Err( p_input, "out of memory" );
159         free( psz_source );
160         return -1;
161     }
162     free( psz_source );
163
164     p_vcd->vcddev = vcddev;
165     p_input->p_access_data = (void *)p_vcd;
166
167     p_input->i_mtu = VCD_DATA_ONCE;
168
169     vlc_mutex_lock( &p_input->stream.stream_lock );
170     p_input->stream.b_pace_control = 1;
171     p_input->stream.b_seekable = 1;
172     p_input->stream.p_selected_area->i_size = 0;
173     p_input->stream.p_selected_area->i_tell = 0;
174     vlc_mutex_unlock( &p_input->stream.stream_lock );
175
176     /* We read the Table Of Content information */
177     p_vcd->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
178                                            p_vcd->vcddev, &p_vcd->p_sectors );
179     if( p_vcd->i_nb_tracks < 0 )
180         msg_Err( p_input, "unable to count tracks" );
181     else if( p_vcd->i_nb_tracks <= 1 )
182         msg_Err( p_input, "no movie tracks found" );
183     if( p_vcd->i_nb_tracks <= 1)
184     {
185         ioctl_Close( p_this, p_vcd->vcddev );
186         free( p_vcd );
187         return -1;
188     }
189
190     /* Allocate the entry points table */
191     p_vcd->p_entries = malloc( p_vcd->i_nb_tracks * sizeof( int ) );
192
193     if( p_vcd->p_entries == NULL )
194     {
195         msg_Err( p_input, "not enough memory" );
196         ioctl_Close( p_this, p_vcd->vcddev );
197         free( p_vcd );
198     }
199
200     /* Set stream and area data */
201     vlc_mutex_lock( &p_input->stream.stream_lock );
202
203     /* Initialize ES structures */
204     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
205
206     /* disc input method */
207     p_input->stream.i_method = INPUT_METHOD_VCD;
208
209     p_input->stream.i_area_nb = 1;
210
211 #define area p_input->stream.pp_areas
212     for( i = 1 ; i <= p_vcd->i_nb_tracks - 1 ; i++ )
213     {
214         /* Titles are Program Chains */
215         input_AddArea( p_input, i, 1 );
216
217         /* Absolute start offset and size */
218         area[i]->i_start = (off_t)p_vcd->p_sectors[i] * (off_t)VCD_DATA_SIZE;
219         area[i]->i_size = (off_t)(p_vcd->p_sectors[i+1] - p_vcd->p_sectors[i])
220                            * (off_t)VCD_DATA_SIZE;
221
222         /* Default Chapter */
223         area[i]->i_part = 1;
224
225         /* i_plugin_data is used to store which entry point is the first
226          * of the track (area) */
227         area[i]->i_plugin_data = 0;
228     }
229 #undef area
230
231     p_area = p_input->stream.pp_areas[i_title];
232
233     p_vcd->b_valid_ep = 1;
234     if( VCDEntryPoints( p_input ) < 0 )
235     {
236         msg_Warn( p_input, "could not read entry points, will not use them" );
237         p_vcd->b_valid_ep = 0;
238     }
239
240     VCDSetArea( p_input, p_area );
241
242     vlc_mutex_unlock( &p_input->stream.stream_lock );
243
244     if( !p_input->psz_demux || !*p_input->psz_demux )
245     {
246         p_input->psz_demux = "ps";
247     }
248
249     p_input->pf_read = VCDRead;
250     p_input->pf_seek = VCDSeek;
251     p_input->pf_set_area = VCDSetArea;
252     p_input->pf_set_program = VCDSetProgram;
253
254     return 0;
255 }
256
257 /*****************************************************************************
258  * VCDClose: closes vcd
259  *****************************************************************************/
260 static void VCDClose( vlc_object_t *p_this )
261 {
262     input_thread_t *   p_input = (input_thread_t *)p_this;
263     thread_vcd_data_t *p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
264
265     ioctl_Close( p_this, p_vcd->vcddev );
266     free( p_vcd );
267 }
268
269 /*****************************************************************************
270  * VCDRead: reads from the VCD into PES packets.
271  *****************************************************************************
272  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
273  * bytes.
274  *****************************************************************************/
275 static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
276                      size_t i_len )
277 {
278     thread_vcd_data_t *     p_vcd;
279     int                     i_blocks;
280     int                     i_index;
281     int                     i_read;
282     byte_t                  p_last_sector[ VCD_DATA_SIZE ];
283
284     p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
285
286     i_read = 0;
287
288     /* Compute the number of blocks we have to read */
289
290     i_blocks = i_len / VCD_DATA_SIZE;
291
292     for ( i_index = 0 ; i_index < i_blocks ; i_index++ )
293     {
294         if ( ioctl_ReadSectors( VLC_OBJECT(p_input), p_vcd->vcddev,
295              p_vcd->i_sector, p_buffer + i_index * VCD_DATA_SIZE, 1,
296              VCD_TYPE ) < 0 )
297         {
298             msg_Err( p_input, "could not read sector %d", p_vcd->i_sector );
299             return -1;
300         }
301
302         p_vcd->i_sector ++;
303         if ( p_vcd->i_sector == p_vcd->p_sectors[p_vcd->i_track + 1] )
304         {
305             input_area_t *p_area;
306
307             if ( p_vcd->i_track >= p_vcd->i_nb_tracks - 1 )
308                 return 0; /* EOF */
309
310             vlc_mutex_lock( &p_input->stream.stream_lock );
311             p_area = p_input->stream.pp_areas[
312                     p_input->stream.p_selected_area->i_id + 1 ];
313
314             msg_Dbg( p_input, "new title" );
315
316             p_area->i_part = 1;
317             VCDSetArea( p_input, p_area );
318             vlc_mutex_unlock( &p_input->stream.stream_lock );
319         }
320
321         /* Update chapter */
322         else if( p_vcd->b_valid_ep &&
323                 /* FIXME kludge so that read does not update chapter
324                  * when a manual chapter change was requested and not
325                  * yet accomplished */
326                 !p_input->stream.p_new_area )
327         {
328             int i_entry;
329
330             vlc_mutex_lock( &p_input->stream.stream_lock );
331             i_entry = p_input->stream.p_selected_area->i_plugin_data
332                 /* 1st entry point of the track (area)*/
333                         + p_input->stream.p_selected_area->i_part - 1;
334             if( i_entry + 1 < p_vcd->i_entries_nb &&
335                     p_vcd->i_sector >= p_vcd->p_entries[i_entry + 1] )
336             {
337                 msg_Dbg( p_input, "new chapter" );
338                 p_input->stream.p_selected_area->i_part ++;
339             }
340             vlc_mutex_unlock( &p_input->stream.stream_lock );
341         }
342
343         i_read += VCD_DATA_SIZE;
344     }
345
346     if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
347     {
348         if ( ioctl_ReadSectors( VLC_OBJECT(p_input), p_vcd->vcddev,
349              p_vcd->i_sector, p_last_sector, 1, VCD_TYPE ) < 0 )
350         {
351             msg_Err( p_input, "could not read sector %d", p_vcd->i_sector );
352             return -1;
353         }
354
355         p_input->p_vlc->pf_memcpy( p_buffer + i_blocks * VCD_DATA_SIZE,
356                                    p_last_sector, i_len % VCD_DATA_SIZE );
357         i_read += i_len % VCD_DATA_SIZE;
358     }
359
360     return i_read;
361 }
362
363 /*****************************************************************************
364  * VCDSetProgram: Does nothing since a VCD is mono_program
365  *****************************************************************************/
366 static int VCDSetProgram( input_thread_t * p_input,
367                           pgrm_descriptor_t * p_program)
368 {
369     return 0;
370 }
371
372 /*****************************************************************************
373  * VCDSetArea: initialize input data for title x, chapter y.
374  * It should be called for each user navigation request.
375  ****************************************************************************/
376 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
377 {
378     thread_vcd_data_t *     p_vcd;
379     vlc_value_t val;
380
381     p_vcd = (thread_vcd_data_t*)p_input->p_access_data;
382
383     /* we can't use the interface slider until initilization is complete */
384     p_input->stream.b_seekable = 0;
385
386     if( p_area != p_input->stream.p_selected_area )
387     {
388         unsigned int i;
389
390         /* Reset the Chapter position of the current title */
391         p_input->stream.p_selected_area->i_part = 1;
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->i_track = p_area->i_id;
400         p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
401
402         /* Update the navigation variables without triggering a callback */
403         val.i_int = p_area->i_id;
404         var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
405         var_Change( p_input, "chapter", VLC_VAR_CLEARCHOICES, NULL, NULL );
406         for( i = 1; i <= p_area->i_part_nb; i++ )
407         {
408             val.i_int = i;
409             var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val, NULL );
410         }
411     }
412
413     if( p_vcd->b_valid_ep )
414     {
415         int i_entry = p_area->i_plugin_data /* 1st entry point of
416                                                the track (area)*/
417                             + p_area->i_part - 1;
418         p_vcd->i_sector = p_vcd->p_entries[i_entry];
419     }
420     else
421         p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
422
423     p_input->stream.p_selected_area->i_tell =
424         (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
425          - p_input->stream.p_selected_area->i_start;
426
427     /* warn interface that something has changed */
428     p_input->stream.b_seekable = 1;
429     p_input->stream.b_changed = 1;
430
431     /* Update the navigation variables without triggering a callback */
432     val.i_int = p_area->i_part;
433     var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
434
435     return 0;
436 }
437
438 /****************************************************************************
439  * VCDSeek
440  ****************************************************************************/
441 static void VCDSeek( input_thread_t * p_input, off_t i_off )
442 {
443     thread_vcd_data_t * p_vcd;
444     unsigned int i_index;
445
446     p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
447
448     p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
449                        + i_off / (off_t)VCD_DATA_SIZE;
450
451     vlc_mutex_lock( &p_input->stream.stream_lock );
452 #define p_area p_input->stream.p_selected_area
453     /* Find chapter */
454     if( p_vcd->b_valid_ep )
455     {
456         for( i_index = 0 ; i_index < p_area->i_part_nb - 1 ; i_index ++ )
457         {
458             if( p_vcd->i_sector < p_vcd->p_entries[p_area->i_plugin_data
459                 + i_index + 1] )
460             {
461                 p_area->i_part = i_index;
462                 break;
463             }
464         }
465     }
466 #undef p_area
467
468     p_input->stream.p_selected_area->i_tell =
469         (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
470          - p_input->stream.p_selected_area->i_start;
471     vlc_mutex_unlock( &p_input->stream.stream_lock );
472 }
473
474 /*****************************************************************************
475  * VCDEntryPoints: Reads the information about the entry points on the disc.
476  *****************************************************************************/
477 static int VCDEntryPoints( input_thread_t * p_input )
478 {
479     thread_vcd_data_t *               p_vcd;
480     byte_t *                          p_sector;
481     entries_sect_t                    entries;
482     uint16_t                          i_nb;
483     int                               i, i_entry_index = 0;
484     int                               i_previous_track = -1;
485
486     p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
487
488     p_sector = malloc( VCD_DATA_SIZE * sizeof( byte_t ) );
489     if( p_sector == NULL )
490     {
491         msg_Err( p_input, "not enough memory for entry points treatment" );
492         return -1;
493     }
494
495     if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_vcd->vcddev,
496         VCD_ENTRIES_SECTOR, p_sector, 1, VCD_TYPE ) < 0 )
497     {
498         msg_Err( p_input, "could not read entry points sector" );
499         free( p_sector );
500         return( -1 );
501     }
502
503     memcpy( &entries, p_sector, CD_SECTOR_SIZE );
504     free( p_sector );
505
506     if( (i_nb = U16_AT( &entries.i_entries_nb )) > 500 )
507     {
508         msg_Err( p_input, "invalid entry points number" );
509         return( -1 );
510     }
511
512     p_vcd->p_entries = malloc( sizeof( int ) * i_nb );
513     if( p_vcd->p_entries == NULL )
514     {
515         msg_Err( p_input, "not enough memory for entry points treatment" );
516         return -1;
517     }
518
519     if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) )
520      && strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ))
521     {
522         msg_Err( p_input, "unrecognized entry points format" );
523         free( p_vcd->p_entries );
524         return -1;
525     }
526
527     p_vcd->i_entries_nb = 0;
528
529 #define i_track BCD_TO_BIN(entries.entry[i].i_track)
530     for( i = 0 ; i < i_nb ; i++ )
531     {
532         if( i_track <= p_input->stream.i_area_nb )
533         {
534             p_vcd->p_entries[i_entry_index] =
535                 (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
536                               BCD_TO_BIN( entries.entry[i].msf.second ),
537                               BCD_TO_BIN( entries.entry[i].msf.frame  ) ));
538             p_input->stream.pp_areas[i_track-1]->i_part_nb ++;
539             /* if this entry belongs to a new track */
540             if( i_track != i_previous_track )
541             {
542                 /* i_plugin_data is used to store the first entry of the area*/
543                 p_input->stream.pp_areas[i_track-1]->i_plugin_data =
544                                                             i_entry_index;
545                 i_previous_track = i_track;
546             }
547             i_entry_index ++;
548             p_vcd->i_entries_nb ++;
549         }
550         else
551             msg_Warn( p_input, "wrong track number found in entry points" );
552     }
553 #undef i_track
554     return 0;
555 }