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"
58 #include "input_vcd.h"
59 #include "cdrom_tools.h"
61 /* how many blocks VCDRead will read in each loop */
62 #define VCD_BLOCKS_ONCE 20
63 #define VCD_DATA_ONCE (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)
65 /*****************************************************************************
67 *****************************************************************************/
68 /* called from outside */
69 static int VCDInit ( struct input_thread_s * );
70 static void VCDEnd ( struct input_thread_s * );
71 static int VCDDemux ( struct input_thread_s * );
72 static int VCDRewind ( struct input_thread_s * );
74 static int VCDOpen ( struct input_thread_s *);
75 static void VCDClose ( struct input_thread_s *);
76 static int VCDRead ( struct input_thread_s *, byte_t *, size_t );
77 static void VCDSeek ( struct input_thread_s *, off_t );
78 static int VCDSetArea ( struct input_thread_s *, struct input_area_s * );
79 static int VCDSetProgram ( struct input_thread_s *, pgrm_descriptor_t * );
81 /*****************************************************************************
82 * Functions exported as capabilities. They are declared as static so that
83 * we don't pollute the namespace too much.
84 *****************************************************************************/
85 void _M( access_getfunctions )( function_list_t * p_function_list )
87 #define access p_function_list->functions.access
88 access.pf_open = VCDOpen;
89 access.pf_close = VCDClose;
90 access.pf_read = VCDRead;
91 access.pf_set_area = VCDSetArea;
92 access.pf_set_program = VCDSetProgram;
93 access.pf_seek = VCDSeek;
98 void _M( demux_getfunctions )( function_list_t * p_function_list )
100 #define demux p_function_list->functions.demux
101 demux.pf_init = VCDInit;
102 demux.pf_end = VCDEnd;
103 demux.pf_demux = VCDDemux;
104 demux.pf_rewind = VCDRewind;
109 * Data reading functions
112 /*****************************************************************************
114 *****************************************************************************/
115 static int VCDOpen( struct input_thread_s *p_input )
121 struct stat stat_info;
122 thread_vcd_data_t * p_vcd;
124 input_area_t * p_area;
130 /* parse the options passed in command line : */
131 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
138 while( *psz_parser && *psz_parser != '@' )
143 if( *psz_parser == '@' )
149 i_title = (int)strtol( psz_parser, &psz_next, 10 );
152 psz_parser = psz_next + 1;
153 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
156 i_title = i_title ? i_title : 1;
157 i_chapter = i_chapter ? i_chapter : 1;
162 if( !p_input->psz_access )
167 psz_source = config_GetPszVariable( "vcd_device" );
170 /* test the type of file given */
172 if( stat( psz_source, &stat_info ) == -1 )
174 intf_ErrMsg( "input: vcd: cannot stat() source `%s' (%s)",
175 psz_source, strerror(errno));
179 if( !S_ISBLK(stat_info.st_mode) )
181 intf_WarnMsg( 3, "input : VCD plugin discarded"
182 " (not a valid drive)" );
187 p_vcd = malloc( sizeof(thread_vcd_data_t) );
191 intf_ErrMsg( "vcd error: out of memory" );
195 p_input->p_access_data = (void *)p_vcd;
197 p_input->i_mtu = VCD_DATA_ONCE;
199 vlc_mutex_lock( &p_input->stream.stream_lock );
201 p_input->stream.b_pace_control = 1;
203 p_input->stream.b_seekable = 1;
204 p_input->stream.p_selected_area->i_size = 0;
205 p_input->stream.p_selected_area->i_tell = 0;
207 vlc_mutex_unlock( &p_input->stream.stream_lock );
209 p_vcd->i_handle = open( psz_source, O_RDONLY | O_NONBLOCK );
211 if( p_vcd->i_handle == -1 )
213 intf_ErrMsg( "input: vcd: Could not open %s\n", psz_source );
218 /* We read the Table Of Content information */
219 p_vcd->nb_tracks = ioctl_GetTrackCount( p_vcd->i_handle,
221 if( p_vcd->nb_tracks < 0 )
223 intf_ErrMsg( "input: vcd: was unable to count tracks" );
224 close( p_vcd->i_handle );
228 else if( p_vcd->nb_tracks <= 1 )
230 intf_ErrMsg( "input: vcd: no movie tracks found" );
231 close( p_vcd->i_handle );
236 p_vcd->p_sectors = ioctl_GetSectors( p_vcd->i_handle,
238 if( p_vcd->p_sectors == NULL )
240 input_BuffersEnd( p_input->p_method_data );
241 close( p_vcd->i_handle );
246 /* Set stream and area data */
247 vlc_mutex_lock( &p_input->stream.stream_lock );
249 /* Initialize ES structures */
250 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
252 /* disc input method */
253 p_input->stream.i_method = INPUT_METHOD_VCD;
255 #define area p_input->stream.pp_areas
256 for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
258 input_AddArea( p_input );
260 /* Titles are Program Chains */
263 /* Absolute start offset and size */
264 area[i]->i_start = (off_t)p_vcd->p_sectors[i] * (off_t)VCD_DATA_SIZE;
265 area[i]->i_size = (off_t)(p_vcd->p_sectors[i+1] - p_vcd->p_sectors[i])
266 * (off_t)VCD_DATA_SIZE;
268 /* Number of chapters */
269 area[i]->i_part_nb = 0; // will be the entry points
272 area[i]->i_plugin_data = p_vcd->p_sectors[i];
276 p_area = p_input->stream.pp_areas[i_title];
278 VCDSetArea( p_input, p_area );
280 vlc_mutex_unlock( &p_input->stream.stream_lock );
282 p_input->psz_demux = "vcd";
287 /*****************************************************************************
288 * VCDClose: closes vcd
289 *****************************************************************************/
290 static void VCDClose( struct input_thread_s *p_input )
292 thread_vcd_data_t *p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
294 input_BuffersEnd( p_input->p_method_data );
295 close( p_vcd->i_handle );
299 /*****************************************************************************
300 * VCDRead: reads from the VCD into PES packets.
301 *****************************************************************************
302 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
304 *****************************************************************************/
305 static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
308 thread_vcd_data_t * p_vcd;
312 byte_t p_last_sector[ VCD_DATA_SIZE ];
314 p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
318 /* Compute the number of blocks we have to read */
320 i_blocks = i_len / VCD_DATA_SIZE;
322 for ( i_index = 0 ; i_index < i_blocks ; i_index++ )
324 if ( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector,
325 p_buffer + i_index * VCD_DATA_SIZE ) < 0 )
327 intf_ErrMsg( "input: vcd: could not read sector %d\n",
333 if ( p_vcd->i_sector == p_vcd->p_sectors[p_vcd->i_track + 1] )
335 input_area_t *p_area;
337 if ( p_vcd->i_track >= p_vcd->nb_tracks - 1 )
340 p_area = p_input->stream.pp_areas[
341 p_input->stream.p_selected_area->i_id + 1 ];
343 intf_WarnMsg( 4, "input: vcd info: new title" );
346 VCDSetArea( p_input, p_area );
349 i_read += VCD_DATA_SIZE;
352 if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
354 if ( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector,
355 p_last_sector ) < 0 )
357 intf_ErrMsg( "input: vcd: could not read sector %d\n",
362 FAST_MEMCPY( p_buffer + i_blocks * VCD_DATA_SIZE,
363 p_last_sector, i_len % VCD_DATA_SIZE );
364 i_read += i_len % VCD_DATA_SIZE;
367 p_input->stream.p_selected_area->i_tell =
368 (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
369 - p_input->stream.p_selected_area->i_start;
375 /*****************************************************************************
376 * VCDSetProgram: Does nothing since a VCD is mono_program
377 *****************************************************************************/
378 static int VCDSetProgram( input_thread_t * p_input,
379 pgrm_descriptor_t * p_program)
385 /*****************************************************************************
386 * VCDSetArea: initialize input data for title x, chapter y.
387 * It should be called for each user navigation request.
388 ****************************************************************************/
389 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
391 thread_vcd_data_t * p_vcd;
393 p_vcd = (thread_vcd_data_t*)p_input->p_access_data;
395 /* we can't use the interface slider until initilization is complete */
396 p_input->stream.b_seekable = 0;
398 if( p_area != p_input->stream.p_selected_area )
400 /* Reset the Chapter position of the current title */
401 p_input->stream.p_selected_area->i_part = 1;
402 p_input->stream.p_selected_area->i_tell = 0;
404 /* Change the default area */
405 p_input->stream.p_selected_area = p_area;
407 /* Change the current track */
408 /* The first track is not a valid one */
409 p_vcd->i_track = p_area->i_id;
410 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
413 /* warn interface that something has changed */
414 p_input->stream.b_seekable = 1;
415 p_input->stream.b_changed = 1;
421 /*****************************************************************************
422 * VCDRewind : reads a stream backward
423 *****************************************************************************/
424 static int VCDRewind( input_thread_t * p_input )
429 /****************************************************************************
431 ****************************************************************************/
432 static void VCDSeek( input_thread_t * p_input, off_t i_off )
434 thread_vcd_data_t * p_vcd;
436 p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
438 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
439 + i_off / (off_t)VCD_DATA_SIZE;
441 p_input->stream.p_selected_area->i_tell =
442 (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
443 - p_input->stream.p_selected_area->i_start;
451 /*****************************************************************************
452 * VCDInit: initializes VCD structures
453 *****************************************************************************/
454 static int VCDInit( input_thread_t * p_input )
456 es_descriptor_t * p_es;
458 vlc_mutex_lock( &p_input->stream.stream_lock );
460 /* Set program information. */
461 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
462 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
464 /* No PSM to read in disc mode, we already have all the information */
465 p_input->stream.p_selected_program->b_is_ok = 1;
467 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
468 p_es->i_stream_id = 0xe0;
469 p_es->i_type = MPEG1_VIDEO_ES;
470 p_es->i_cat = VIDEO_ES;
472 if( p_main->b_video )
474 input_SelectES( p_input, p_es );
477 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xc0, 0 );
478 p_es->i_stream_id = 0xc0;
479 p_es->i_type = MPEG1_AUDIO_ES;
481 p_es->i_cat = AUDIO_ES;
483 if( p_main->b_audio )
485 input_SelectES( p_input, p_es );
488 vlc_mutex_unlock( &p_input->stream.stream_lock );
493 /*****************************************************************************
494 * VCDEnd: frees unused data
495 *****************************************************************************/
496 static void VCDEnd( input_thread_t * p_input )
502 /*****************************************************************************
503 * VCDDemux: reads and demuxes data packets
504 *****************************************************************************
505 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
507 *****************************************************************************/
508 static int VCDDemux( input_thread_t * p_input )
512 for( i = 0; i < VCD_BLOCKS_ONCE; i++ )
514 data_packet_t * p_data;
517 i_result = input_ReadPS( p_input, &p_data );
524 input_DemuxPS( p_input, p_data );