1 /*****************************************************************************
2 * vcd.c : VCD input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: vcd.c,v 1.6 2002/10/04 18:07:21 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() */
52 /* how many blocks VCDRead will read in each loop */
53 #define VCD_BLOCKS_ONCE 20
54 #define VCD_DATA_ONCE (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)
56 /*****************************************************************************
58 *****************************************************************************/
59 static int VCDOpen ( vlc_object_t * );
60 static void VCDClose ( vlc_object_t * );
61 static int VCDRead ( input_thread_t *, byte_t *, size_t );
62 static void VCDSeek ( input_thread_t *, off_t );
63 static int VCDSetArea ( input_thread_t *, input_area_t * );
64 static int VCDSetProgram ( input_thread_t *, pgrm_descriptor_t * );
66 /*****************************************************************************
68 *****************************************************************************/
70 set_description( _("VCD input module") );
71 set_capability( "access", 80 );
72 set_callbacks( VCDOpen, VCDClose );
73 add_shortcut( "svcd" );
77 * Data reading functions
80 /*****************************************************************************
82 *****************************************************************************/
83 static int VCDOpen( vlc_object_t *p_this )
85 input_thread_t * p_input = (input_thread_t *)p_this;
90 struct stat stat_info;
91 thread_vcd_data_t * p_vcd;
93 input_area_t * p_area;
97 p_input->pf_read = VCDRead;
98 p_input->pf_seek = VCDSeek;
99 p_input->pf_set_area = VCDSetArea;
100 p_input->pf_set_program = VCDSetProgram;
102 /* parse the options passed in command line : */
103 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
110 while( *psz_parser && *psz_parser != '@' )
115 if( *psz_parser == '@' )
121 i_title = (int)strtol( psz_parser, &psz_next, 10 );
124 psz_parser = psz_next + 1;
125 i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
128 i_title = i_title ? i_title : 1;
129 i_chapter = i_chapter ? i_chapter : 1;
134 if( !p_input->psz_access )
139 psz_source = config_GetPsz( p_input, "vcd" );
142 /* test the type of file given */
144 if( stat( psz_source, &stat_info ) == -1 )
146 msg_Err( p_input, "cannot stat() source `%s' (%s)",
147 psz_source, strerror(errno));
151 if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode))
153 msg_Warn( p_input, "vcd module discarded (not a valid drive)" );
158 p_vcd = malloc( sizeof(thread_vcd_data_t) );
162 msg_Err( p_input, "out of memory" );
166 p_input->p_access_data = (void *)p_vcd;
168 p_input->i_mtu = VCD_DATA_ONCE;
170 vlc_mutex_lock( &p_input->stream.stream_lock );
172 p_input->stream.b_pace_control = 1;
174 p_input->stream.b_seekable = 1;
175 p_input->stream.p_selected_area->i_size = 0;
176 p_input->stream.p_selected_area->i_tell = 0;
178 vlc_mutex_unlock( &p_input->stream.stream_lock );
180 p_vcd->i_handle = open( psz_source, O_RDONLY | O_NONBLOCK );
182 if( p_vcd->i_handle == -1 )
184 msg_Err( p_input, "could not open %s", psz_source );
189 /* We read the Table Of Content information */
190 p_vcd->nb_tracks = ioctl_GetTrackCount( VLC_OBJECT( p_input),
191 p_vcd->i_handle, psz_source );
192 if( p_vcd->nb_tracks < 0 )
194 msg_Err( p_input, "unable to count tracks" );
195 close( p_vcd->i_handle );
199 else if( p_vcd->nb_tracks <= 1 )
201 msg_Err( p_input, "no movie tracks found" );
202 close( p_vcd->i_handle );
207 p_vcd->p_sectors = ioctl_GetSectors( VLC_OBJECT( p_input),
208 p_vcd->i_handle, psz_source );
209 if( p_vcd->p_sectors == NULL )
211 input_BuffersEnd( p_input, p_input->p_method_data );
212 close( p_vcd->i_handle );
217 /* Set stream and area data */
218 vlc_mutex_lock( &p_input->stream.stream_lock );
220 /* Initialize ES structures */
221 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
223 /* disc input method */
224 p_input->stream.i_method = INPUT_METHOD_VCD;
226 #define area p_input->stream.pp_areas
227 for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
229 input_AddArea( p_input );
231 /* Titles are Program Chains */
234 /* Absolute start offset and size */
235 area[i]->i_start = (off_t)p_vcd->p_sectors[i] * (off_t)VCD_DATA_SIZE;
236 area[i]->i_size = (off_t)(p_vcd->p_sectors[i+1] - p_vcd->p_sectors[i])
237 * (off_t)VCD_DATA_SIZE;
239 /* Number of chapters */
240 area[i]->i_part_nb = 0; /* will be the entry points */
243 area[i]->i_plugin_data = p_vcd->p_sectors[i];
247 p_area = p_input->stream.pp_areas[i_title];
249 VCDSetArea( p_input, p_area );
251 vlc_mutex_unlock( &p_input->stream.stream_lock );
253 p_input->psz_demux = "ps";
258 /*****************************************************************************
259 * VCDClose: closes vcd
260 *****************************************************************************/
261 static void VCDClose( vlc_object_t *p_this )
263 input_thread_t * p_input = (input_thread_t *)p_this;
264 thread_vcd_data_t *p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
266 close( p_vcd->i_handle );
270 /*****************************************************************************
271 * VCDRead: reads from the VCD into PES packets.
272 *****************************************************************************
273 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
275 *****************************************************************************/
276 static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
279 thread_vcd_data_t * p_vcd;
283 byte_t p_last_sector[ VCD_DATA_SIZE ];
285 p_vcd = (thread_vcd_data_t *)p_input->p_access_data;
289 /* Compute the number of blocks we have to read */
291 i_blocks = i_len / VCD_DATA_SIZE;
293 for ( i_index = 0 ; i_index < i_blocks ; i_index++ )
295 if ( ioctl_ReadSector( VLC_OBJECT(p_input), p_vcd->i_handle,
296 p_vcd->i_sector, p_buffer + i_index * VCD_DATA_SIZE ) < 0 )
298 msg_Err( p_input, "could not read sector %d", p_vcd->i_sector );
303 if ( p_vcd->i_sector == p_vcd->p_sectors[p_vcd->i_track + 1] )
305 input_area_t *p_area;
307 if ( p_vcd->i_track >= p_vcd->nb_tracks - 1 )
310 p_area = p_input->stream.pp_areas[
311 p_input->stream.p_selected_area->i_id + 1 ];
313 msg_Dbg( p_input, "new title" );
316 VCDSetArea( p_input, p_area );
319 i_read += VCD_DATA_SIZE;
322 if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
324 if ( ioctl_ReadSector( VLC_OBJECT(p_input), p_vcd->i_handle,
325 p_vcd->i_sector, p_last_sector ) < 0 )
327 msg_Err( p_input, "could not read sector %d", p_vcd->i_sector );
331 p_input->p_vlc->pf_memcpy( p_buffer + i_blocks * VCD_DATA_SIZE,
332 p_last_sector, i_len % VCD_DATA_SIZE );
333 i_read += i_len % VCD_DATA_SIZE;
336 p_input->stream.p_selected_area->i_tell =
337 (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
338 - p_input->stream.p_selected_area->i_start;
344 /*****************************************************************************
345 * VCDSetProgram: Does nothing since a VCD is mono_program
346 *****************************************************************************/
347 static int VCDSetProgram( input_thread_t * p_input,
348 pgrm_descriptor_t * p_program)
354 /*****************************************************************************
355 * VCDSetArea: initialize input data for title x, chapter y.
356 * It should be called for each user navigation request.
357 ****************************************************************************/
358 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
360 thread_vcd_data_t * p_vcd;
362 p_vcd = (thread_vcd_data_t*)p_input->p_access_data;
364 /* we can't use the interface slider until initilization is complete */
365 p_input->stream.b_seekable = 0;
367 if( p_area != p_input->stream.p_selected_area )
369 /* Reset the Chapter position of the current title */
370 p_input->stream.p_selected_area->i_part = 1;
371 p_input->stream.p_selected_area->i_tell = 0;
373 /* Change the default area */
374 p_input->stream.p_selected_area = p_area;
376 /* Change the current track */
377 /* The first track is not a valid one */
378 p_vcd->i_track = p_area->i_id;
379 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
382 /* warn interface that something has changed */
383 p_input->stream.b_seekable = 1;
384 p_input->stream.b_changed = 1;
390 /****************************************************************************
392 ****************************************************************************/
393 static void VCDSeek( input_thread_t * p_input, off_t i_off )
395 thread_vcd_data_t * p_vcd;
397 p_vcd = (thread_vcd_data_t *) p_input->p_access_data;
399 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
400 + i_off / (off_t)VCD_DATA_SIZE;
402 p_input->stream.p_selected_area->i_tell =
403 (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
404 - p_input->stream.p_selected_area->i_start;