]> git.sesse.net Git - vlc/blob - modules/access/vcd/vcd.c
* ./configure.ac.in: removed now unnecessary --force-exe-suffix flag.
[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.6 2002/10/04 18:07:21 sam 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 <fcntl.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <string.h>
43 #include <errno.h>
44
45 #if defined( WIN32 )
46 #   include <io.h>                                                 /* read() */
47 #endif
48
49 #include "vcd.h"
50 #include "cdrom.h"
51
52 /* how many blocks VCDRead will read in each loop */
53 #define VCD_BLOCKS_ONCE 20
54 #define VCD_DATA_ONCE   (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static int  VCDOpen         ( vlc_object_t * );
60 static void VCDClose        ( vlc_object_t * );
61 static int  VCDRead         ( input_thread_t *, byte_t *, size_t );
62 static void VCDSeek         ( input_thread_t *, off_t );
63 static int  VCDSetArea      ( input_thread_t *, input_area_t * );
64 static int  VCDSetProgram   ( input_thread_t *, pgrm_descriptor_t * );
65
66 /*****************************************************************************
67  * Module descriptior
68  *****************************************************************************/
69 vlc_module_begin();
70     set_description( _("VCD input module") );
71     set_capability( "access", 80 );
72     set_callbacks( VCDOpen, VCDClose );
73     add_shortcut( "svcd" );
74 vlc_module_end();
75
76 /*
77  * Data reading functions
78  */
79
80 /*****************************************************************************
81  * VCDOpen: open vcd
82  *****************************************************************************/
83 static int VCDOpen( vlc_object_t *p_this )
84 {
85     input_thread_t *        p_input = (input_thread_t *)p_this;
86     char *                  psz_orig;
87     char *                  psz_parser;
88     char *                  psz_source;
89     char *                  psz_next;
90     struct stat             stat_info;
91     thread_vcd_data_t *     p_vcd;
92     int                     i;
93     input_area_t *          p_area;
94     int                     i_title = 1;
95     int                     i_chapter = 1;
96
97     p_input->pf_read = VCDRead;
98     p_input->pf_seek = VCDSeek;
99     p_input->pf_set_area = VCDSetArea;
100     p_input->pf_set_program = VCDSetProgram;
101
102     /* parse the options passed in command line : */
103     psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
104     
105     if( !psz_orig )
106     {
107         return( -1 );
108     }
109  
110     while( *psz_parser && *psz_parser != '@' )
111     {
112         psz_parser++;
113     }
114
115     if( *psz_parser == '@' )
116     {
117         /* Found options */
118         *psz_parser = '\0';
119         ++psz_parser;
120
121         i_title = (int)strtol( psz_parser, &psz_next, 10 );
122         if( *psz_next )
123         {
124             psz_parser = psz_next + 1;
125             i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
126         }
127
128         i_title = i_title ? i_title : 1;
129         i_chapter = i_chapter ? i_chapter : 1;
130     }
131
132     if( !*psz_source )
133     {
134         if( !p_input->psz_access )
135         {
136             free( psz_orig );
137             return -1;
138         }
139         psz_source = config_GetPsz( p_input, "vcd" );
140     }
141
142     /* test the type of file given */
143     
144     if( stat( psz_source, &stat_info ) == -1 )
145     {
146         msg_Err( p_input, "cannot stat() source `%s' (%s)",
147                           psz_source, strerror(errno));
148         return( -1 );
149     }
150     
151     if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode))
152     {
153         msg_Warn( p_input, "vcd module discarded (not a valid drive)" );
154         return -1;
155     }
156     
157     
158     p_vcd = malloc( sizeof(thread_vcd_data_t) );
159
160     if( p_vcd == NULL )
161     {
162         msg_Err( p_input, "out of memory" );
163         return -1;
164     }
165     
166     p_input->p_access_data = (void *)p_vcd;
167     
168     p_input->i_mtu = VCD_DATA_ONCE;
169    
170     vlc_mutex_lock( &p_input->stream.stream_lock );
171
172     p_input->stream.b_pace_control = 1;
173
174     p_input->stream.b_seekable = 1;
175     p_input->stream.p_selected_area->i_size = 0;
176     p_input->stream.p_selected_area->i_tell = 0;
177
178     vlc_mutex_unlock( &p_input->stream.stream_lock );
179
180     p_vcd->i_handle = open( psz_source, O_RDONLY | O_NONBLOCK );
181
182     if( p_vcd->i_handle == -1 )
183     {
184         msg_Err( p_input, "could not open %s", psz_source );
185         free (p_vcd);
186         return -1;
187     }
188
189     /* We read the Table Of Content information */
190     p_vcd->nb_tracks = ioctl_GetTrackCount( VLC_OBJECT( p_input),
191                                             p_vcd->i_handle, psz_source );
192     if( p_vcd->nb_tracks < 0 )
193     {
194         msg_Err( p_input, "unable to count tracks" );
195         close( p_vcd->i_handle );
196         free( p_vcd );
197         return -1;
198     }
199     else if( p_vcd->nb_tracks <= 1 )
200     {
201         msg_Err( p_input, "no movie tracks found" );
202         close( p_vcd->i_handle );
203         free( p_vcd );
204         return -1;
205     }
206
207     p_vcd->p_sectors = ioctl_GetSectors( VLC_OBJECT( p_input),
208                                          p_vcd->i_handle, psz_source );
209     if( p_vcd->p_sectors == NULL )
210     {
211         input_BuffersEnd( p_input, p_input->p_method_data );
212         close( p_vcd->i_handle );
213         free( p_vcd );
214         return -1;
215     }
216
217     /* Set stream and area data */
218     vlc_mutex_lock( &p_input->stream.stream_lock );
219
220     /* Initialize ES structures */
221     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
222
223     /* disc input method */
224     p_input->stream.i_method = INPUT_METHOD_VCD;
225
226 #define area p_input->stream.pp_areas
227     for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
228     {
229         input_AddArea( p_input );
230
231         /* Titles are Program Chains */
232         area[i]->i_id = i;
233
234         /* Absolute start offset and size */
235         area[i]->i_start = (off_t)p_vcd->p_sectors[i] * (off_t)VCD_DATA_SIZE;
236         area[i]->i_size = (off_t)(p_vcd->p_sectors[i+1] - p_vcd->p_sectors[i])
237                            * (off_t)VCD_DATA_SIZE;
238
239         /* Number of chapters */
240         area[i]->i_part_nb = 0;   /* will be the entry points */
241         area[i]->i_part = 1;
242
243         area[i]->i_plugin_data = p_vcd->p_sectors[i];
244     }
245 #undef area
246
247     p_area = p_input->stream.pp_areas[i_title];
248
249     VCDSetArea( p_input, p_area );
250
251     vlc_mutex_unlock( &p_input->stream.stream_lock );
252
253     p_input->psz_demux = "ps";
254
255     return 0;
256 }
257
258 /*****************************************************************************
259  * VCDClose: closes vcd
260  *****************************************************************************/
261 static void VCDClose( vlc_object_t *p_this )
262 {
263     input_thread_t *   p_input = (input_thread_t *)p_this;
264     thread_vcd_data_t *p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
265
266     close( p_vcd->i_handle );
267     free( p_vcd );
268 }
269
270 /*****************************************************************************
271  * VCDRead: reads from the VCD into PES packets.
272  *****************************************************************************
273  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
274  * bytes.
275  *****************************************************************************/
276 static int VCDRead( input_thread_t * p_input, byte_t * p_buffer, 
277                      size_t i_len )
278 {
279     thread_vcd_data_t *     p_vcd;
280     int                     i_blocks;
281     int                     i_index;
282     int                     i_read;
283     byte_t                  p_last_sector[ VCD_DATA_SIZE ];
284
285     p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
286
287     i_read = 0;
288
289     /* Compute the number of blocks we have to read */
290
291     i_blocks = i_len / VCD_DATA_SIZE;
292
293     for ( i_index = 0 ; i_index < i_blocks ; i_index++ ) 
294     {
295         if ( ioctl_ReadSector( VLC_OBJECT(p_input), p_vcd->i_handle,
296                     p_vcd->i_sector, p_buffer + i_index * VCD_DATA_SIZE ) < 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->nb_tracks - 1 )
308                 return 0; /* EOF */
309             
310             p_area = p_input->stream.pp_areas[
311                     p_input->stream.p_selected_area->i_id + 1 ];
312             
313             msg_Dbg( p_input, "new title" );
314             
315             p_area->i_part = 1;
316             VCDSetArea( p_input, p_area );
317     
318         }
319         i_read += VCD_DATA_SIZE;
320     }
321     
322     if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
323     { 
324         if ( ioctl_ReadSector( VLC_OBJECT(p_input), p_vcd->i_handle,
325                                p_vcd->i_sector, p_last_sector ) < 0 )
326         {
327             msg_Err( p_input, "could not read sector %d", p_vcd->i_sector );
328             return -1;
329         }
330         
331         p_input->p_vlc->pf_memcpy( p_buffer + i_blocks * VCD_DATA_SIZE,
332                                    p_last_sector, i_len % VCD_DATA_SIZE );
333         i_read += i_len % VCD_DATA_SIZE;
334     }
335     
336     p_input->stream.p_selected_area->i_tell = 
337         (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
338          - p_input->stream.p_selected_area->i_start;
339
340     return i_read;
341 }
342
343
344 /*****************************************************************************
345  * VCDSetProgram: Does nothing since a VCD is mono_program
346  *****************************************************************************/
347 static int VCDSetProgram( input_thread_t * p_input,
348                           pgrm_descriptor_t * p_program)
349 {
350     return 0;
351 }
352
353
354 /*****************************************************************************
355  * VCDSetArea: initialize input data for title x, chapter y.
356  * It should be called for each user navigation request.
357  ****************************************************************************/
358 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
359 {
360     thread_vcd_data_t *     p_vcd;
361
362     p_vcd = (thread_vcd_data_t*)p_input->p_access_data;
363
364     /* we can't use the interface slider until initilization is complete */
365     p_input->stream.b_seekable = 0;
366
367     if( p_area != p_input->stream.p_selected_area )
368     {
369         /* Reset the Chapter position of the current title */
370         p_input->stream.p_selected_area->i_part = 1;
371         p_input->stream.p_selected_area->i_tell = 0;
372
373         /* Change the default area */
374         p_input->stream.p_selected_area = p_area;
375
376         /* Change the current track */
377         /* The first track is not a valid one  */
378         p_vcd->i_track = p_area->i_id;
379         p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
380     }
381
382     /* warn interface that something has changed */
383     p_input->stream.b_seekable = 1;
384     p_input->stream.b_changed = 1;
385
386     return 0;
387 }
388
389
390 /****************************************************************************
391  * VCDSeek
392  ****************************************************************************/
393 static void VCDSeek( input_thread_t * p_input, off_t i_off )
394 {
395     thread_vcd_data_t *               p_vcd;
396
397     p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
398
399     p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
400                        + i_off / (off_t)VCD_DATA_SIZE;
401
402     p_input->stream.p_selected_area->i_tell = 
403         (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
404          - p_input->stream.p_selected_area->i_start;
405 }