]> git.sesse.net Git - vlc/blob - plugins/vcd/input_vcd.c
* plugins/directx/vout_directx.h: removed unused variables.
[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 <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #ifdef HAVE_UNISTD_H
33 #   include <unistd.h>
34 #endif
35
36 #include <fcntl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <string.h>
40 #include <errno.h>
41
42 #if defined( WIN32 )
43 #   include <io.h>                                                 /* read() */
44 #else
45 #   include <sys/uio.h>                                      /* struct iovec */
46 #endif
47
48 #if defined( WIN32 )
49 #   include "input_iovec.h"
50 #endif
51
52 #include "input_vcd.h"
53 #include "cdrom_tools.h"
54
55 /* how many blocks VCDRead will read in each loop */
56 #define VCD_BLOCKS_ONCE 20
57 #define VCD_DATA_ONCE   (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 /* called from outside */
63
64 static int  VCDOpen         ( input_thread_t *);
65 static void VCDClose        ( input_thread_t *);
66 static int  VCDRead         ( input_thread_t *, byte_t *, size_t );
67 static void VCDSeek         ( input_thread_t *, off_t );
68 static int  VCDSetArea      ( input_thread_t *, input_area_t * );
69 static int  VCDSetProgram   ( input_thread_t *, pgrm_descriptor_t * );
70
71 /*****************************************************************************
72  * Functions exported as capabilities. They are declared as static so that
73  * we don't pollute the namespace too much.
74  *****************************************************************************/
75 void _M( access_getfunctions )( function_list_t * p_function_list )
76 {
77 #define access p_function_list->functions.access
78     access.pf_open             = VCDOpen;
79     access.pf_close            = VCDClose;
80     access.pf_read             = VCDRead;
81     access.pf_set_area         = VCDSetArea;
82     access.pf_set_program      = VCDSetProgram;
83     access.pf_seek             = VCDSeek;
84 #undef access
85 }
86
87 /*
88  * Data reading functions
89  */
90
91 /*****************************************************************************
92  * VCDOpen: open vcd
93  *****************************************************************************/
94 static int VCDOpen( input_thread_t *p_input )
95 {
96     char *                  psz_orig;
97     char *                  psz_parser;
98     char *                  psz_source;
99     char *                  psz_next;
100     struct stat             stat_info;
101     thread_vcd_data_t *     p_vcd;
102     int                     i;
103     input_area_t *          p_area;
104     int                     i_title = 1;
105     int                     i_chapter = 1;
106
107     
108
109     /* parse the options passed in command line : */
110     psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
111     
112     if( !psz_orig )
113     {
114         return( -1 );
115     }
116  
117     while( *psz_parser && *psz_parser != '@' )
118     {
119         psz_parser++;
120     }
121
122     if( *psz_parser == '@' )
123     {
124         /* Found options */
125         *psz_parser = '\0';
126         ++psz_parser;
127
128         i_title = (int)strtol( psz_parser, &psz_next, 10 );
129         if( *psz_next )
130         {
131             psz_parser = psz_next + 1;
132             i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
133         }
134
135         i_title = i_title ? i_title : 1;
136         i_chapter = i_chapter ? i_chapter : 1;
137     }
138
139     if( !*psz_source )
140     {
141         if( !p_input->psz_access )
142         {
143             free( psz_orig );
144             return -1;
145         }
146         psz_source = config_GetPsz( p_input, "vcd" );
147     }
148
149     /* test the type of file given */
150     
151     if( stat( psz_source, &stat_info ) == -1 )
152     {
153         msg_Err( p_input, "cannot stat() source `%s' (%s)",
154                           psz_source, strerror(errno));
155         return( -1 );
156     }
157     
158     if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode))
159     {
160         msg_Warn( p_input, "vcd module discarded (not a valid drive)" );
161         return -1;
162     }
163     
164     
165     p_vcd = malloc( sizeof(thread_vcd_data_t) );
166
167     if( p_vcd == NULL )
168     {
169         msg_Err( p_input, "out of memory" );
170         return -1;
171     }
172     
173     p_input->p_access_data = (void *)p_vcd;
174     
175     p_input->i_mtu = VCD_DATA_ONCE;
176    
177     vlc_mutex_lock( &p_input->stream.stream_lock );
178
179     p_input->stream.b_pace_control = 1;
180
181     p_input->stream.b_seekable = 1;
182     p_input->stream.p_selected_area->i_size = 0;
183     p_input->stream.p_selected_area->i_tell = 0;
184
185     vlc_mutex_unlock( &p_input->stream.stream_lock );
186
187     p_vcd->i_handle = open( psz_source, O_RDONLY | O_NONBLOCK );
188
189     if( p_vcd->i_handle == -1 )
190     {
191         msg_Err( p_input, "could not open %s\n", psz_source );
192         free (p_vcd);
193         return -1;
194     }
195
196     /* We read the Table Of Content information */
197     p_vcd->nb_tracks = ioctl_GetTrackCount( p_vcd->i_handle,
198                                             psz_source );
199     if( p_vcd->nb_tracks < 0 )
200     {
201         msg_Err( p_input, "unable to count tracks" );
202         close( p_vcd->i_handle );
203         free( p_vcd );
204         return -1;
205     }
206     else if( p_vcd->nb_tracks <= 1 )
207     {
208         msg_Err( p_input, "no movie tracks found" );
209         close( p_vcd->i_handle );
210         free( p_vcd );
211         return -1;
212     }
213
214     p_vcd->p_sectors = ioctl_GetSectors( p_vcd->i_handle,
215                                          psz_source );
216     if( p_vcd->p_sectors == NULL )
217     {
218         input_BuffersEnd( p_input, p_input->p_method_data );
219         close( p_vcd->i_handle );
220         free( p_vcd );
221         return -1;
222     }
223
224     /* Set stream and area data */
225     vlc_mutex_lock( &p_input->stream.stream_lock );
226
227     /* Initialize ES structures */
228     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
229
230     /* disc input method */
231     p_input->stream.i_method = INPUT_METHOD_VCD;
232
233 #define area p_input->stream.pp_areas
234     for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
235     {
236         input_AddArea( p_input );
237
238         /* Titles are Program Chains */
239         area[i]->i_id = i;
240
241         /* Absolute start offset and size */
242         area[i]->i_start = (off_t)p_vcd->p_sectors[i] * (off_t)VCD_DATA_SIZE;
243         area[i]->i_size = (off_t)(p_vcd->p_sectors[i+1] - p_vcd->p_sectors[i])
244                            * (off_t)VCD_DATA_SIZE;
245
246         /* Number of chapters */
247         area[i]->i_part_nb = 0;   // will be the entry points
248         area[i]->i_part = 1;
249
250         area[i]->i_plugin_data = p_vcd->p_sectors[i];
251     }
252 #undef area
253
254     p_area = p_input->stream.pp_areas[i_title];
255
256     VCDSetArea( p_input, p_area );
257
258     vlc_mutex_unlock( &p_input->stream.stream_lock );
259
260     p_input->psz_demux = "ps";
261
262     return 0;
263 }
264
265 /*****************************************************************************
266  * VCDClose: closes vcd
267  *****************************************************************************/
268 static void VCDClose( input_thread_t *p_input )
269 {
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 }