1 /****************************************************************************
2 * input_vcd.c: VideoCD raw reading plugin.
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
6 * Author: Johan Bilien <jobi@via.ecp.fr>
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.
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.
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 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #include <vlc/input.h>
37 #include <sys/types.h>
43 # include <io.h> /* read() */
45 # include <sys/uio.h> /* struct iovec */
49 # include "input_iovec.h"
52 #include "input_vcd.h"
53 #include "cdrom_tools.h"
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)
59 /*****************************************************************************
61 *****************************************************************************/
62 /* called from outside */
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 * );
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 )
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;
88 * Data reading functions
91 /*****************************************************************************
93 *****************************************************************************/
94 static int VCDOpen( input_thread_t *p_input )
100 struct stat stat_info;
101 thread_vcd_data_t * p_vcd;
103 input_area_t * p_area;
109 /* parse the options passed in command line : */
110 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
117 while( *psz_parser && *psz_parser != '@' )
122 if( *psz_parser == '@' )
128 i_title = (int)strtol( psz_parser, &psz_next, 10 );
131 psz_parser = psz_next + 1;
132 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
135 i_title = i_title ? i_title : 1;
136 i_chapter = i_chapter ? i_chapter : 1;
141 if( !p_input->psz_access )
146 psz_source = config_GetPsz( p_input, "vcd" );
149 /* test the type of file given */
151 if( stat( psz_source, &stat_info ) == -1 )
153 msg_Err( p_input, "cannot stat() source `%s' (%s)",
154 psz_source, strerror(errno));
158 if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode))
160 msg_Warn( p_input, "vcd module discarded (not a valid drive)" );
165 p_vcd = malloc( sizeof(thread_vcd_data_t) );
169 msg_Err( p_input, "out of memory" );
173 p_input->p_access_data = (void *)p_vcd;
175 p_input->i_mtu = VCD_DATA_ONCE;
177 vlc_mutex_lock( &p_input->stream.stream_lock );
179 p_input->stream.b_pace_control = 1;
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;
185 vlc_mutex_unlock( &p_input->stream.stream_lock );
187 p_vcd->i_handle = open( psz_source, O_RDONLY | O_NONBLOCK );
189 if( p_vcd->i_handle == -1 )
191 msg_Err( p_input, "could not open %s\n", psz_source );
196 /* We read the Table Of Content information */
197 p_vcd->nb_tracks = ioctl_GetTrackCount( p_vcd->i_handle,
199 if( p_vcd->nb_tracks < 0 )
201 msg_Err( p_input, "unable to count tracks" );
202 close( p_vcd->i_handle );
206 else if( p_vcd->nb_tracks <= 1 )
208 msg_Err( p_input, "no movie tracks found" );
209 close( p_vcd->i_handle );
214 p_vcd->p_sectors = ioctl_GetSectors( p_vcd->i_handle,
216 if( p_vcd->p_sectors == NULL )
218 input_BuffersEnd( p_input, p_input->p_method_data );
219 close( p_vcd->i_handle );
224 /* Set stream and area data */
225 vlc_mutex_lock( &p_input->stream.stream_lock );
227 /* Initialize ES structures */
228 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
230 /* disc input method */
231 p_input->stream.i_method = INPUT_METHOD_VCD;
233 #define area p_input->stream.pp_areas
234 for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
236 input_AddArea( p_input );
238 /* Titles are Program Chains */
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;
246 /* Number of chapters */
247 area[i]->i_part_nb = 0; // will be the entry points
250 area[i]->i_plugin_data = p_vcd->p_sectors[i];
254 p_area = p_input->stream.pp_areas[i_title];
256 VCDSetArea( p_input, p_area );
258 vlc_mutex_unlock( &p_input->stream.stream_lock );
260 p_input->psz_demux = "ps";
265 /*****************************************************************************
266 * VCDClose: closes vcd
267 *****************************************************************************/
268 static void VCDClose( input_thread_t *p_input )
270 thread_vcd_data_t *p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
272 close( p_vcd->i_handle );
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
281 *****************************************************************************/
282 static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
285 thread_vcd_data_t * p_vcd;
289 byte_t p_last_sector[ VCD_DATA_SIZE ];
291 p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
295 /* Compute the number of blocks we have to read */
297 i_blocks = i_len / VCD_DATA_SIZE;
299 for ( i_index = 0 ; i_index < i_blocks ; i_index++ )
301 if ( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector,
302 p_buffer + i_index * VCD_DATA_SIZE ) < 0 )
304 msg_Err( p_input, "could not read sector %d", p_vcd->i_sector );
309 if ( p_vcd->i_sector == p_vcd->p_sectors[p_vcd->i_track + 1] )
311 input_area_t *p_area;
313 if ( p_vcd->i_track >= p_vcd->nb_tracks - 1 )
316 p_area = p_input->stream.pp_areas[
317 p_input->stream.p_selected_area->i_id + 1 ];
319 msg_Dbg( p_input, "new title" );
322 VCDSetArea( p_input, p_area );
325 i_read += VCD_DATA_SIZE;
328 if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
330 if ( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector,
331 p_last_sector ) < 0 )
333 msg_Err( p_input, "could not read sector %d", p_vcd->i_sector );
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;
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;
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)
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 )
366 thread_vcd_data_t * p_vcd;
368 p_vcd = (thread_vcd_data_t*)p_input->p_access_data;
370 /* we can't use the interface slider until initilization is complete */
371 p_input->stream.b_seekable = 0;
373 if( p_area != p_input->stream.p_selected_area )
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;
379 /* Change the default area */
380 p_input->stream.p_selected_area = p_area;
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];
388 /* warn interface that something has changed */
389 p_input->stream.b_seekable = 1;
390 p_input->stream.b_changed = 1;
396 /****************************************************************************
398 ****************************************************************************/
399 static void VCDSeek( input_thread_t * p_input, off_t i_off )
401 thread_vcd_data_t * p_vcd;
403 p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
405 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
406 + i_off / (off_t)VCD_DATA_SIZE;
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;