1 /*****************************************************************************
2 * vcd.c : VCD input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42 "Allows you to modify the default caching value for cdda streams. This " \
43 "value should be set in milliseconds units." )
46 set_shortname( _("VCD"));
47 set_description( _("VCD input") );
48 set_capability( "access2", 60 );
49 set_callbacks( Open, Close );
50 set_category( CAT_INPUT );
51 set_subcategory( SUBCAT_INPUT_ACCESS );
53 add_usage_hint( N_("[vcd:][device][@[title][,[chapter]]]") );
54 add_integer( "vcd-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
55 CACHING_LONGTEXT, VLC_TRUE );
56 add_shortcut( "vcd" );
57 add_shortcut( "svcd" );
60 /*****************************************************************************
62 *****************************************************************************/
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)
70 vcddev_t *vcddev; /* vcd device descriptor */
74 input_title_t *title[99]; /* No more that 99 track in a vcd ? */
77 int i_sector; /* Current Sector */
78 int *p_sectors; /* Track sectors */
81 static block_t *Block( access_t * );
82 static int Seek( access_t *, int64_t );
83 static int Control( access_t *, int, va_list );
84 static int EntryPoints( access_t * );
86 /*****************************************************************************
88 *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
91 access_t *p_access = (access_t *)p_this;
93 char *psz_dup = strdup( p_access->psz_path );
100 /* Command line: vcd://[dev_path][@title[,chapter]] */
101 if( ( psz = strchr( psz_dup, '@' ) ) )
105 i_title = strtol( psz, &psz, 0 );
107 i_chapter = strtol( psz+1, &psz, 0 );
110 if( *psz_dup == '\0' )
114 /* Only when selected */
115 if( strcmp( p_access->psz_access, "vcd" ) &&
116 strcmp( p_access->psz_access, "svcd" ) )
119 psz_dup = var_CreateGetString( p_access, "vcd" );
120 if( *psz_dup == '\0' )
128 if( !(vcddev = ioctl_Open( p_this, psz_dup )) )
135 /* Set up p_access */
136 p_access->pf_read = NULL;
137 p_access->pf_block = Block;
138 p_access->pf_control = Control;
139 p_access->pf_seek = Seek;
140 p_access->info.i_update = 0;
141 p_access->info.i_size = 0;
142 p_access->info.i_pos = 0;
143 p_access->info.b_eof = VLC_FALSE;
144 p_access->info.i_title = 0;
145 p_access->info.i_seekpoint = 0;
146 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
147 memset( p_sys, 0, sizeof( access_sys_t ) );
148 p_sys->vcddev = vcddev;
150 /* We read the Table Of Content information */
151 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
152 p_sys->vcddev, &p_sys->p_sectors );
153 if( p_sys->i_titles < 0 )
155 msg_Err( p_access, "unable to count tracks" );
158 else if( p_sys->i_titles <= 1 )
160 msg_Err( p_access, "no movie tracks found" );
163 /* The first title isn't usable */
166 /* Build title table */
167 for( i = 0; i < p_sys->i_titles; i++ )
169 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
171 msg_Dbg( p_access, "title[%d] start=%d\n", i, p_sys->p_sectors[1+i] );
172 msg_Dbg( p_access, "title[%d] end=%d\n", i, p_sys->p_sectors[i+2] );
174 t->i_size = ( p_sys->p_sectors[i+2] - p_sys->p_sectors[i+1] ) *
175 (int64_t)VCD_DATA_SIZE;
178 /* Map entry points into chapters */
179 if( EntryPoints( p_access ) )
181 msg_Warn( p_access, "could not read entry points, will not use them" );
184 /* Starting title/chapter and sector */
185 if( i_title >= p_sys->i_titles )
187 if( i_chapter >= p_sys->title[i_title]->i_seekpoint )
190 p_sys->i_sector = p_sys->p_sectors[1+i_title];
194 ( p_sys->title[i_title]->seekpoint[i_chapter]->i_byte_offset /
197 p_access->info.i_title = i_title;
198 p_access->info.i_seekpoint = i_chapter;
199 p_access->info.i_size = p_sys->title[i_title]->i_size;
200 p_access->info.i_pos = ( p_sys->i_sector - p_sys->p_sectors[1+i_title] ) *
203 p_access->psz_demux = strdup( "ps" );
208 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
213 /*****************************************************************************
215 *****************************************************************************/
216 static void Close( vlc_object_t *p_this )
218 access_t *p_access = (access_t *)p_this;
219 access_sys_t *p_sys = p_access->p_sys;
221 ioctl_Close( p_this, p_sys->vcddev );
225 /*****************************************************************************
227 *****************************************************************************/
228 static int Control( access_t *p_access, int i_query, va_list args )
230 access_sys_t *p_sys = p_access->p_sys;
234 input_title_t ***ppp_title;
240 case ACCESS_CAN_SEEK:
241 case ACCESS_CAN_FASTSEEK:
242 case ACCESS_CAN_PAUSE:
243 case ACCESS_CAN_CONTROL_PACE:
244 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
250 pi_int = (int*)va_arg( args, int * );
251 *pi_int = VCD_DATA_ONCE;
254 case ACCESS_GET_PTS_DELAY:
255 pi_64 = (int64_t*)va_arg( args, int64_t * );
256 *pi_64 = var_GetInteger( p_access, "vcd-caching" ) * 1000;
260 case ACCESS_SET_PAUSE_STATE:
263 case ACCESS_GET_TITLE_INFO:
264 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
265 pi_int = (int*)va_arg( args, int* );
267 /* Duplicate title infos */
268 *pi_int = p_sys->i_titles;
269 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
270 for( i = 0; i < p_sys->i_titles; i++ )
272 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
276 case ACCESS_SET_TITLE:
277 i = (int)va_arg( args, int );
278 if( i != p_access->info.i_title )
281 p_access->info.i_update |=
282 INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
283 p_access->info.i_title = i;
284 p_access->info.i_seekpoint = 0;
285 p_access->info.i_size = p_sys->title[i]->i_size;
286 p_access->info.i_pos = 0;
288 /* Next sector to read */
289 p_sys->i_sector = p_sys->p_sectors[1+i];
293 case ACCESS_SET_SEEKPOINT:
295 input_title_t *t = p_sys->title[p_access->info.i_title];
296 i = (int)va_arg( args, int );
297 if( t->i_seekpoint > 0 )
299 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
300 p_access->info.i_seekpoint = i;
302 p_sys->i_sector = p_sys->p_sectors[1+p_access->info.i_title] +
303 t->seekpoint[i]->i_byte_offset / VCD_DATA_SIZE;
305 p_access->info.i_pos = (int64_t)(p_sys->i_sector -
306 p_sys->p_sectors[1+p_access->info.i_title]) *VCD_DATA_SIZE;
311 case ACCESS_SET_PRIVATE_ID_STATE:
315 msg_Warn( p_access, "unimplemented query in control" );
322 /*****************************************************************************
324 *****************************************************************************/
325 static block_t *Block( access_t *p_access )
327 access_sys_t *p_sys = p_access->p_sys;
328 int i_blocks = VCD_BLOCKS_ONCE;
332 /* Check end of file */
333 if( p_access->info.b_eof ) return NULL;
335 /* Check end of title */
336 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 2] )
338 if( p_access->info.i_title + 2 >= p_sys->i_titles )
340 p_access->info.b_eof = VLC_TRUE;
344 p_access->info.i_update |=
345 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT | INPUT_UPDATE_SIZE;
346 p_access->info.i_title++;
347 p_access->info.i_seekpoint = 0;
348 p_access->info.i_size =
349 p_sys->title[p_access->info.i_title]->i_size;
350 p_access->info.i_pos = 0;
353 /* Don't read after the end of a title */
354 if( p_sys->i_sector + i_blocks >=
355 p_sys->p_sectors[p_access->info.i_title + 2] )
357 i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] -
361 /* Do the actual reading */
362 if( !( p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE ) ) )
364 msg_Err( p_access, "cannot get a new block of size: %i",
365 i_blocks * VCD_DATA_SIZE );
369 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
370 p_sys->i_sector, p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
372 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
373 block_Release( p_block );
375 /* Try to skip one sector (in case of bad sectors) */
377 p_access->info.i_pos += VCD_DATA_SIZE;
381 /* Update seekpoints */
382 for( i_read = 0; i_read < i_blocks; i_read++ )
384 input_title_t *t = p_sys->title[p_access->info.i_title];
386 if( t->i_seekpoint > 0 &&
387 p_access->info.i_seekpoint + 1 < t->i_seekpoint &&
388 p_access->info.i_pos + i_read * VCD_DATA_SIZE >=
389 t->seekpoint[p_access->info.i_seekpoint+1]->i_byte_offset )
391 msg_Dbg( p_access, "seekpoint change" );
392 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
393 p_access->info.i_seekpoint++;
397 /* Update a few values */
398 p_sys->i_sector += i_blocks;
399 p_access->info.i_pos += p_block->i_buffer;
404 /*****************************************************************************
406 *****************************************************************************/
407 static int Seek( access_t *p_access, int64_t i_pos )
409 access_sys_t *p_sys = p_access->p_sys;
410 input_title_t *t = p_sys->title[p_access->info.i_title];
413 /* Next sector to read */
414 p_access->info.i_pos = i_pos;
415 p_sys->i_sector = i_pos / VCD_DATA_SIZE +
416 p_sys->p_sectors[p_access->info.i_title + 1];
418 /* Update current seekpoint */
419 for( i_seekpoint = 0; i_seekpoint < t->i_seekpoint; i_seekpoint++ )
421 if( i_seekpoint + 1 >= t->i_seekpoint ) break;
422 if( i_pos < t->seekpoint[i_seekpoint + 1]->i_byte_offset ) break;
425 if( i_seekpoint != p_access->info.i_seekpoint )
427 msg_Dbg( p_access, "seekpoint change" );
428 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
429 p_access->info.i_seekpoint = i_seekpoint;
435 /*****************************************************************************
436 * EntryPoints: Reads the information about the entry points on the disc.
437 *****************************************************************************/
438 static int EntryPoints( access_t *p_access )
440 access_sys_t *p_sys = p_access->p_sys;
441 uint8_t sector[VCD_DATA_SIZE];
443 entries_sect_t entries;
446 /* Read the entry point sector */
447 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
448 VCD_ENTRIES_SECTOR, sector, 1, VCD_TYPE ) < 0 )
450 msg_Err( p_access, "could not read entry points sector" );
453 memcpy( &entries, sector, CD_SECTOR_SIZE );
455 i_nb = GetWBE( &entries.i_entries_nb );
458 msg_Err( p_access, "invalid entry points number" );
462 if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) ) &&
463 strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ) )
465 msg_Err( p_access, "unrecognized entry points format" );
469 for( i = 0; i < i_nb; i++ )
471 const int i_title = BCD_TO_BIN(entries.entry[i].i_track) - 2;
473 (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
474 BCD_TO_BIN( entries.entry[i].msf.second ),
475 BCD_TO_BIN( entries.entry[i].msf.frame ) ));
478 if( i_title < 0 ) continue; /* Should not occur */
479 if( i_title >= p_sys->i_titles ) continue;
481 msg_Dbg( p_access, "Entry[%d] title=%d sector=%d\n",
482 i, i_title, i_sector );
484 s = vlc_seekpoint_New();
485 s->i_byte_offset = (i_sector - p_sys->p_sectors[i_title+1]) *
488 TAB_APPEND( p_sys->title[i_title]->i_seekpoint,
489 p_sys->title[i_title]->seekpoint, s );