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" );
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) && !S_ISCHR(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 close( p_vcd->i_handle );
298 /*****************************************************************************
299 * VCDRead: reads from the VCD into PES packets.
300 *****************************************************************************
301 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
303 *****************************************************************************/
304 static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
307 thread_vcd_data_t * p_vcd;
311 byte_t p_last_sector[ VCD_DATA_SIZE ];
313 p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
317 /* Compute the number of blocks we have to read */
319 i_blocks = i_len / VCD_DATA_SIZE;
321 for ( i_index = 0 ; i_index < i_blocks ; i_index++ )
323 if ( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector,
324 p_buffer + i_index * VCD_DATA_SIZE ) < 0 )
326 intf_ErrMsg( "input: vcd: could not read sector %d\n",
332 if ( p_vcd->i_sector == p_vcd->p_sectors[p_vcd->i_track + 1] )
334 input_area_t *p_area;
336 if ( p_vcd->i_track >= p_vcd->nb_tracks - 1 )
339 p_area = p_input->stream.pp_areas[
340 p_input->stream.p_selected_area->i_id + 1 ];
342 intf_WarnMsg( 4, "input: vcd info: new title" );
345 VCDSetArea( p_input, p_area );
348 i_read += VCD_DATA_SIZE;
351 if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
353 if ( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector,
354 p_last_sector ) < 0 )
356 intf_ErrMsg( "input: vcd: could not read sector %d\n",
361 FAST_MEMCPY( p_buffer + i_blocks * VCD_DATA_SIZE,
362 p_last_sector, i_len % VCD_DATA_SIZE );
363 i_read += i_len % VCD_DATA_SIZE;
366 p_input->stream.p_selected_area->i_tell =
367 (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
368 - p_input->stream.p_selected_area->i_start;
374 /*****************************************************************************
375 * VCDSetProgram: Does nothing since a VCD is mono_program
376 *****************************************************************************/
377 static int VCDSetProgram( input_thread_t * p_input,
378 pgrm_descriptor_t * p_program)
384 /*****************************************************************************
385 * VCDSetArea: initialize input data for title x, chapter y.
386 * It should be called for each user navigation request.
387 ****************************************************************************/
388 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
390 thread_vcd_data_t * p_vcd;
392 p_vcd = (thread_vcd_data_t*)p_input->p_access_data;
394 /* we can't use the interface slider until initilization is complete */
395 p_input->stream.b_seekable = 0;
397 if( p_area != p_input->stream.p_selected_area )
399 /* Reset the Chapter position of the current title */
400 p_input->stream.p_selected_area->i_part = 1;
401 p_input->stream.p_selected_area->i_tell = 0;
403 /* Change the default area */
404 p_input->stream.p_selected_area = p_area;
406 /* Change the current track */
407 /* The first track is not a valid one */
408 p_vcd->i_track = p_area->i_id;
409 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
412 /* warn interface that something has changed */
413 p_input->stream.b_seekable = 1;
414 p_input->stream.b_changed = 1;
420 /*****************************************************************************
421 * VCDRewind : reads a stream backward
422 *****************************************************************************/
423 static int VCDRewind( input_thread_t * p_input )
428 /****************************************************************************
430 ****************************************************************************/
431 static void VCDSeek( input_thread_t * p_input, off_t i_off )
433 thread_vcd_data_t * p_vcd;
435 p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
437 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
438 + i_off / (off_t)VCD_DATA_SIZE;
440 p_input->stream.p_selected_area->i_tell =
441 (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
442 - p_input->stream.p_selected_area->i_start;
450 /*****************************************************************************
451 * VCDInit: initializes VCD structures
452 *****************************************************************************/
453 static int VCDInit( input_thread_t * p_input )
455 es_descriptor_t * p_es;
457 if( p_input->stream.i_method != INPUT_METHOD_VCD )
462 vlc_mutex_lock( &p_input->stream.stream_lock );
464 /* Set program information. */
465 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
466 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
468 /* No PSM to read in disc mode, we already have all the information */
469 p_input->stream.p_selected_program->b_is_ok = 1;
471 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
472 p_es->i_stream_id = 0xe0;
473 p_es->i_type = MPEG1_VIDEO_ES;
474 p_es->i_cat = VIDEO_ES;
476 if( p_main->b_video )
478 input_SelectES( p_input, p_es );
481 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xc0, 0 );
482 p_es->i_stream_id = 0xc0;
483 p_es->i_type = MPEG1_AUDIO_ES;
485 p_es->i_cat = AUDIO_ES;
487 if( p_main->b_audio )
489 input_SelectES( p_input, p_es );
492 vlc_mutex_unlock( &p_input->stream.stream_lock );
497 /*****************************************************************************
498 * VCDEnd: frees unused data
499 *****************************************************************************/
500 static void VCDEnd( input_thread_t * p_input )
506 /*****************************************************************************
507 * VCDDemux: reads and demuxes data packets
508 *****************************************************************************
509 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
511 *****************************************************************************/
512 static int VCDDemux( input_thread_t * p_input )
516 for( i = 0; i < VCD_BLOCKS_ONCE; i++ )
518 data_packet_t * p_data;
521 i_result = input_ReadPS( p_input, &p_data );
528 input_DemuxPS( p_input, p_data );