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>
40 #ifdef STRNCASECMP_IN_STRINGS_H
45 # include <io.h> /* read() */
47 # include <sys/uio.h> /* struct iovec */
51 # include "input_iovec.h"
54 #include "stream_control.h"
55 #include "input_ext-intf.h"
56 #include "input_ext-dec.h"
57 #include "input_ext-plugins.h"
61 #include "input_vcd.h"
62 #include "cdrom_tools.h"
64 /* how many blocks VCDRead will read in each loop */
65 #define VCD_BLOCKS_ONCE 20
66 #define VCD_DATA_ONCE (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)
68 /*****************************************************************************
70 *****************************************************************************/
71 /* called from outside */
72 static int VCDInit ( struct input_thread_s * );
73 static void VCDEnd ( struct input_thread_s * );
74 static int PSDemux ( struct input_thread_s * );
75 static int VCDRewind ( struct input_thread_s * );
77 static int VCDOpen ( struct input_thread_s *);
78 static void VCDClose ( struct input_thread_s *);
79 static int VCDRead ( struct input_thread_s *, byte_t *, size_t );
80 static void VCDSeek ( struct input_thread_s *, off_t );
81 static int VCDSetArea ( struct input_thread_s *, struct input_area_s * );
82 static int VCDSetProgram ( struct input_thread_s *, pgrm_descriptor_t * );
84 static ssize_t PSRead ( struct input_thread_s *, data_packet_t ** );
85 /*****************************************************************************
86 * Functions exported as capabilities. They are declared as static so that
87 * we don't pollute the namespace too much.
88 *****************************************************************************/
89 void _M( access_getfunctions )( function_list_t * p_function_list )
91 #define access p_function_list->functions.access
92 access.pf_open = VCDOpen;
93 access.pf_close = VCDClose;
94 access.pf_read = VCDRead;
95 access.pf_set_area = VCDSetArea;
96 access.pf_set_program = VCDSetProgram;
97 access.pf_seek = VCDSeek;
102 void _M( demux_getfunctions )( function_list_t * p_function_list )
104 #define demux p_function_list->functions.demux
105 demux.pf_init = VCDInit;
106 demux.pf_end = VCDEnd;
107 demux.pf_demux = PSDemux;
108 demux.pf_rewind = VCDRewind;
113 * Data reading functions
116 /*****************************************************************************
118 *****************************************************************************/
119 static int VCDOpen( struct input_thread_s *p_input )
125 thread_vcd_data_t * p_vcd;
127 input_area_t * p_area;
131 p_vcd = malloc( sizeof(thread_vcd_data_t) );
135 intf_ErrMsg( "vcd error: out of memory" );
139 p_input->i_mtu = VCD_DATA_ONCE;
140 p_input->p_access_data = (void *)p_vcd;
142 /* parse the options passed in command line : */
143 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
150 while( *psz_parser && *psz_parser != '@' )
155 if( *psz_parser == '@' )
161 i_title = (int)strtol( psz_parser, &psz_next, 10 );
164 psz_parser = psz_next + 1;
165 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
168 i_title = i_title ? i_title : 1;
169 i_chapter = i_chapter ? i_chapter : 1;
174 if( !p_input->psz_access )
179 psz_source = config_GetPszVariable( INPUT_VCD_DEVICE_VAR );
182 vlc_mutex_lock( &p_input->stream.stream_lock );
184 /* If we are here we can control the pace... */
185 p_input->stream.b_pace_control = 1;
187 p_input->stream.b_seekable = 1;
188 p_input->stream.p_selected_area->i_size = 0;
189 p_input->stream.p_selected_area->i_tell = 0;
191 vlc_mutex_unlock( &p_input->stream.stream_lock );
193 p_vcd->i_handle = open( psz_source, O_RDONLY | O_NONBLOCK );
195 if( p_vcd->i_handle == -1 )
197 intf_ErrMsg( "input: vcd: Could not open %s\n", psz_source );
202 /* We read the Table Of Content information */
203 p_vcd->nb_tracks = ioctl_GetTrackCount( p_vcd->i_handle,
205 if( p_vcd->nb_tracks < 0 )
207 intf_ErrMsg( "input: vcd: was unable to count tracks" );
211 else if( p_vcd->nb_tracks <= 1 )
213 intf_ErrMsg( "input: vcd: no movie tracks found" );
218 p_vcd->p_sectors = ioctl_GetSectors( p_vcd->i_handle,
220 if ( p_vcd->p_sectors == NULL )
222 input_BuffersEnd( p_input->p_method_data );
227 /* Set stream and area data */
228 vlc_mutex_lock( &p_input->stream.stream_lock );
230 /* Initialize ES structures */
231 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
233 /* disc input method */
234 p_input->stream.i_method = INPUT_METHOD_VCD;
236 #define area p_input->stream.pp_areas
237 for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
239 input_AddArea( p_input );
241 /* Titles are Program Chains */
244 /* Absolute start offset and size */
245 area[i]->i_start = (off_t)p_vcd->p_sectors[i] * (off_t)VCD_DATA_SIZE;
246 area[i]->i_size = (off_t)(p_vcd->p_sectors[i+1] - p_vcd->p_sectors[i])
247 * (off_t)VCD_DATA_SIZE;
249 /* Number of chapters */
250 area[i]->i_part_nb = 0; // will be the entry points
253 area[i]->i_plugin_data = p_vcd->p_sectors[i];
257 p_area = p_input->stream.pp_areas[i_title];
259 VCDSetArea( p_input, p_area );
261 vlc_mutex_unlock( &p_input->stream.stream_lock );
263 p_input->psz_demux = "vcd";
270 /*****************************************************************************
271 * VCDClose: closes vcd
272 *****************************************************************************/
273 static void VCDClose( struct input_thread_s *p_input )
275 thread_vcd_data_t * p_vcd
276 = (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 /* FIXME we should go to next track */
320 i_read += VCD_DATA_SIZE;
323 if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
325 if ( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector,
326 p_last_sector ) < 0 )
328 intf_ErrMsg( "input: vcd: could not read sector %d\n",
333 FAST_MEMCPY( p_buffer + i_blocks * VCD_DATA_SIZE,
334 p_last_sector, i_len % VCD_DATA_SIZE );
335 i_read += i_len % VCD_DATA_SIZE;
338 p_input->stream.p_selected_area->i_tell =
339 (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
340 - p_input->stream.p_selected_area->i_start;
346 /*****************************************************************************
347 * VCDSetProgram: Does nothing since a VCD is mono_program
348 *****************************************************************************/
349 static int VCDSetProgram( input_thread_t * p_input,
350 pgrm_descriptor_t * p_program)
356 /*****************************************************************************
357 * VCDSetArea: initialize input data for title x, chapter y.
358 * It should be called for each user navigation request.
359 ****************************************************************************/
360 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
362 thread_vcd_data_t * p_vcd;
364 p_vcd = (thread_vcd_data_t*)p_input->p_access_data;
366 /* we can't use the interface slider until initilization is complete */
367 p_input->stream.b_seekable = 0;
369 if( p_area != p_input->stream.p_selected_area )
371 /* Reset the Chapter position of the current title */
372 p_input->stream.p_selected_area->i_part = 1;
373 p_input->stream.p_selected_area->i_tell = 0;
375 /* Change the default area */
376 p_input->stream.p_selected_area = p_area;
378 /* Change the current track */
379 /* The first track is not a valid one */
380 p_vcd->i_track = p_area->i_id;
381 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
384 /* warn interface that something has changed */
385 p_input->stream.b_seekable = 1;
386 p_input->stream.b_changed = 1;
392 /*****************************************************************************
393 * VCDRewind : reads a stream backward
394 *****************************************************************************/
395 static int VCDRewind( input_thread_t * p_input )
400 /****************************************************************************
402 ****************************************************************************/
403 static void VCDSeek( input_thread_t * p_input, off_t i_off )
405 thread_vcd_data_t * p_vcd;
407 p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
409 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
410 + i_off / (off_t)VCD_DATA_SIZE;
412 p_input->stream.p_selected_area->i_tell =
413 (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
414 - p_input->stream.p_selected_area->i_start;
422 /*****************************************************************************
423 * VCDInit: initializes VCD structures
424 *****************************************************************************/
425 static int VCDInit( input_thread_t * p_input )
427 es_descriptor_t * p_es;
429 vlc_mutex_lock( &p_input->stream.stream_lock );
431 /* Set program information. */
432 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
433 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
435 /* No PSM to read in disc mode, we already have all the information */
436 p_input->stream.p_selected_program->b_is_ok = 1;
438 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
439 p_es->i_stream_id = 0xe0;
440 p_es->i_type = MPEG1_VIDEO_ES;
441 p_es->i_cat = VIDEO_ES;
443 if( p_main->b_video )
445 input_SelectES( p_input, p_es );
448 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xc0, 0 );
449 p_es->i_stream_id = 0xc0;
450 p_es->i_type = MPEG1_AUDIO_ES;
452 p_es->i_cat = AUDIO_ES;
454 if( p_main->b_audio )
456 input_SelectES( p_input, p_es );
459 vlc_mutex_unlock( &p_input->stream.stream_lock );
464 /*****************************************************************************
465 * VCDEnd: frees unused data
466 *****************************************************************************/
467 static void VCDEnd( input_thread_t * p_input )
469 thread_vcd_data_t * p_vcd;
471 input_BuffersEnd( p_input->p_method_data );
473 p_vcd = (thread_vcd_data_t*)p_input->p_access_data;
479 /*****************************************************************************
480 * PSDemux: reads and demuxes data packets
481 *****************************************************************************
482 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
484 *****************************************************************************/
485 static int PSDemux( input_thread_t * p_input )
489 for( i = 0; i < VCD_BLOCKS_ONCE; i++ )
491 data_packet_t * p_data;
494 i_result = input_ReadPS( p_input, &p_data );
501 input_DemuxPS( p_input, p_data );