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 $
7 * Author: Johan Bilien <jobi@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc/input.h>
33 #include "../../demux/mpeg/system.h"
40 #include <sys/types.h>
46 # include <io.h> /* read() */
48 # include <sys/uio.h> /* struct iovec */
52 # include "input_iovec.h"
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)
62 /*****************************************************************************
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 * );
72 /*****************************************************************************
74 *****************************************************************************/
76 set_description( _("VCD input module") );
77 set_capability( "access", 80 );
78 set_callbacks( VCDOpen, VCDClose );
79 add_shortcut( "svcd" );
83 * Data reading functions
86 /*****************************************************************************
88 *****************************************************************************/
89 static int VCDOpen( vlc_object_t *p_this )
91 input_thread_t * p_input = (input_thread_t *)p_this;
96 struct stat stat_info;
97 thread_vcd_data_t * p_vcd;
99 input_area_t * p_area;
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;
108 /* parse the options passed in command line : */
109 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
116 while( *psz_parser && *psz_parser != '@' )
121 if( *psz_parser == '@' )
127 i_title = (int)strtol( psz_parser, &psz_next, 10 );
130 psz_parser = psz_next + 1;
131 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
134 i_title = i_title ? i_title : 1;
135 i_chapter = i_chapter ? i_chapter : 1;
140 if( !p_input->psz_access )
145 psz_source = config_GetPsz( p_input, "vcd" );
148 /* test the type of file given */
150 if( stat( psz_source, &stat_info ) == -1 )
152 msg_Err( p_input, "cannot stat() source `%s' (%s)",
153 psz_source, strerror(errno));
157 if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode))
159 msg_Warn( p_input, "vcd module discarded (not a valid drive)" );
164 p_vcd = malloc( sizeof(thread_vcd_data_t) );
168 msg_Err( p_input, "out of memory" );
172 p_input->p_access_data = (void *)p_vcd;
174 p_input->i_mtu = VCD_DATA_ONCE;
176 vlc_mutex_lock( &p_input->stream.stream_lock );
178 p_input->stream.b_pace_control = 1;
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;
184 vlc_mutex_unlock( &p_input->stream.stream_lock );
186 p_vcd->i_handle = open( psz_source, O_RDONLY | O_NONBLOCK );
188 if( p_vcd->i_handle == -1 )
190 msg_Err( p_input, "could not open %s\n", psz_source );
195 /* We read the Table Of Content information */
196 p_vcd->nb_tracks = ioctl_GetTrackCount( p_vcd->i_handle,
198 if( p_vcd->nb_tracks < 0 )
200 msg_Err( p_input, "unable to count tracks" );
201 close( p_vcd->i_handle );
205 else if( p_vcd->nb_tracks <= 1 )
207 msg_Err( p_input, "no movie tracks found" );
208 close( p_vcd->i_handle );
213 p_vcd->p_sectors = ioctl_GetSectors( p_vcd->i_handle,
215 if( p_vcd->p_sectors == NULL )
217 input_BuffersEnd( p_input, p_input->p_method_data );
218 close( p_vcd->i_handle );
223 /* Set stream and area data */
224 vlc_mutex_lock( &p_input->stream.stream_lock );
226 /* Initialize ES structures */
227 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
229 /* disc input method */
230 p_input->stream.i_method = INPUT_METHOD_VCD;
232 #define area p_input->stream.pp_areas
233 for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
235 input_AddArea( p_input );
237 /* Titles are Program Chains */
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;
245 /* Number of chapters */
246 area[i]->i_part_nb = 0; /* will be the entry points */
249 area[i]->i_plugin_data = p_vcd->p_sectors[i];
253 p_area = p_input->stream.pp_areas[i_title];
255 VCDSetArea( p_input, p_area );
257 vlc_mutex_unlock( &p_input->stream.stream_lock );
259 p_input->psz_demux = "ps";
264 /*****************************************************************************
265 * VCDClose: closes vcd
266 *****************************************************************************/
267 static void VCDClose( vlc_object_t *p_this )
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;
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;