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 *****************************************************************************/
29 #include <videolan/vlc.h>
36 #include <sys/types.h>
42 # include <io.h> /* read() */
44 # include <sys/uio.h> /* struct iovec */
48 # include "input_iovec.h"
51 #include "stream_control.h"
52 #include "input_ext-intf.h"
53 #include "input_ext-dec.h"
54 #include "input_ext-plugins.h"
56 #include "input_vcd.h"
57 #include "cdrom_tools.h"
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)
63 /*****************************************************************************
65 *****************************************************************************/
66 /* called from outside */
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 * );
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 )
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;
92 * Data reading functions
95 /*****************************************************************************
97 *****************************************************************************/
98 static int VCDOpen( struct input_thread_s *p_input )
104 struct stat stat_info;
105 thread_vcd_data_t * p_vcd;
107 input_area_t * p_area;
113 /* parse the options passed in command line : */
114 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
121 while( *psz_parser && *psz_parser != '@' )
126 if( *psz_parser == '@' )
132 i_title = (int)strtol( psz_parser, &psz_next, 10 );
135 psz_parser = psz_next + 1;
136 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
139 i_title = i_title ? i_title : 1;
140 i_chapter = i_chapter ? i_chapter : 1;
145 if( !p_input->psz_access )
150 psz_source = config_GetPszVariable( "vcd" );
153 /* test the type of file given */
155 if( stat( psz_source, &stat_info ) == -1 )
157 intf_ErrMsg( "input: vcd: cannot stat() source `%s' (%s)",
158 psz_source, strerror(errno));
162 if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode))
164 intf_WarnMsg( 3, "input : VCD plugin discarded"
165 " (not a valid drive)" );
170 p_vcd = malloc( sizeof(thread_vcd_data_t) );
174 intf_ErrMsg( "vcd error: out of memory" );
178 p_input->p_access_data = (void *)p_vcd;
180 p_input->i_mtu = VCD_DATA_ONCE;
182 vlc_mutex_lock( &p_input->stream.stream_lock );
184 p_input->stream.b_pace_control = 1;
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;
190 vlc_mutex_unlock( &p_input->stream.stream_lock );
192 p_vcd->i_handle = open( psz_source, O_RDONLY | O_NONBLOCK );
194 if( p_vcd->i_handle == -1 )
196 intf_ErrMsg( "input: vcd: Could not open %s\n", psz_source );
201 /* We read the Table Of Content information */
202 p_vcd->nb_tracks = ioctl_GetTrackCount( p_vcd->i_handle,
204 if( p_vcd->nb_tracks < 0 )
206 intf_ErrMsg( "input: vcd: was unable to count tracks" );
207 close( p_vcd->i_handle );
211 else if( p_vcd->nb_tracks <= 1 )
213 intf_ErrMsg( "input: vcd: no movie tracks found" );
214 close( p_vcd->i_handle );
219 p_vcd->p_sectors = ioctl_GetSectors( p_vcd->i_handle,
221 if( p_vcd->p_sectors == NULL )
223 input_BuffersEnd( p_input->p_method_data );
224 close( p_vcd->i_handle );
229 /* Set stream and area data */
230 vlc_mutex_lock( &p_input->stream.stream_lock );
232 /* Initialize ES structures */
233 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
235 /* disc input method */
236 p_input->stream.i_method = INPUT_METHOD_VCD;
238 #define area p_input->stream.pp_areas
239 for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
241 input_AddArea( p_input );
243 /* Titles are Program Chains */
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;
251 /* Number of chapters */
252 area[i]->i_part_nb = 0; // will be the entry points
255 area[i]->i_plugin_data = p_vcd->p_sectors[i];
259 p_area = p_input->stream.pp_areas[i_title];
261 VCDSetArea( p_input, p_area );
263 vlc_mutex_unlock( &p_input->stream.stream_lock );
265 p_input->psz_demux = "ps";
270 /*****************************************************************************
271 * VCDClose: closes vcd
272 *****************************************************************************/
273 static void VCDClose( struct input_thread_s *p_input )
275 thread_vcd_data_t *p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
277 close( p_vcd->i_handle );
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
286 *****************************************************************************/
287 static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
290 thread_vcd_data_t * p_vcd;
294 byte_t p_last_sector[ VCD_DATA_SIZE ];
296 p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
300 /* Compute the number of blocks we have to read */
302 i_blocks = i_len / VCD_DATA_SIZE;
304 for ( i_index = 0 ; i_index < i_blocks ; i_index++ )
306 if ( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector,
307 p_buffer + i_index * VCD_DATA_SIZE ) < 0 )
309 intf_ErrMsg( "input: vcd: could not read sector %d\n",
315 if ( p_vcd->i_sector == p_vcd->p_sectors[p_vcd->i_track + 1] )
317 input_area_t *p_area;
319 if ( p_vcd->i_track >= p_vcd->nb_tracks - 1 )
322 p_area = p_input->stream.pp_areas[
323 p_input->stream.p_selected_area->i_id + 1 ];
325 intf_WarnMsg( 4, "input: vcd info: new title" );
328 VCDSetArea( p_input, p_area );
331 i_read += VCD_DATA_SIZE;
334 if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
336 if ( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector,
337 p_last_sector ) < 0 )
339 intf_ErrMsg( "input: vcd: could not read sector %d\n",
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;
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;
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)
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 )
373 thread_vcd_data_t * p_vcd;
375 p_vcd = (thread_vcd_data_t*)p_input->p_access_data;
377 /* we can't use the interface slider until initilization is complete */
378 p_input->stream.b_seekable = 0;
380 if( p_area != p_input->stream.p_selected_area )
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;
386 /* Change the default area */
387 p_input->stream.p_selected_area = p_area;
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];
395 /* warn interface that something has changed */
396 p_input->stream.b_seekable = 1;
397 p_input->stream.b_changed = 1;
403 /****************************************************************************
405 ****************************************************************************/
406 static void VCDSeek( input_thread_t * p_input, off_t i_off )
408 thread_vcd_data_t * p_vcd;
410 p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
412 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
413 + i_off / (off_t)VCD_DATA_SIZE;
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;