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