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