1 /*****************************************************************************
2 * vcd.c : VCD input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 VideoLAN
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 *****************************************************************************/
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_description( _("VCD input") );
47 set_capability( "access2", 60 );
48 set_callbacks( Open, Close );
50 add_usage_hint( N_("[vcd:][device][@[title][,[chapter]]]") );
51 add_integer( "vcd-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
52 CACHING_LONGTEXT, VLC_TRUE );
53 add_shortcut( "vcd" );
54 add_shortcut( "svcd" );
57 /*****************************************************************************
59 *****************************************************************************/
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)
67 vcddev_t *vcddev; /* vcd device descriptor */
71 input_title_t *title[99]; /* No more that 99 track in a vcd ? */
74 int i_sector; /* Current Sector */
75 int *p_sectors; /* Track sectors */
78 static block_t *Block( access_t * );
79 static int Seek( access_t *, int64_t );
80 static int Control( access_t *, int, va_list );
81 static int EntryPoints( access_t * );
83 /*****************************************************************************
85 *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
88 access_t *p_access = (access_t *)p_this;
90 char *psz_dup = strdup( p_access->psz_path );
97 /* Command line: vcd://[dev_path][@title[,chapter]] */
98 if( ( psz = strchr( psz_dup, '@' ) ) )
102 i_title = strtol( psz, &psz, 0 );
104 i_chapter = strtol( psz+1, &psz, 0 );
107 if( *psz_dup == '\0' )
111 /* Only when selected */
112 if( strcmp( p_access->psz_access, "vcd" ) &&
113 strcmp( p_access->psz_access, "svcd" ) )
116 psz_dup = var_CreateGetString( p_access, "vcd" );
117 if( *psz_dup == '\0' )
125 if( !(vcddev = ioctl_Open( p_this, psz_dup )) )
127 msg_Warn( p_access, "could not open %s", psz_dup );
133 /* Set up p_access */
134 p_access->pf_read = NULL;
135 p_access->pf_block = Block;
136 p_access->pf_control = Control;
137 p_access->pf_seek = Seek;
138 p_access->info.i_update = 0;
139 p_access->info.i_size = 0;
140 p_access->info.i_pos = 0;
141 p_access->info.b_eof = VLC_FALSE;
142 p_access->info.i_title = 0;
143 p_access->info.i_seekpoint = 0;
144 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
145 memset( p_sys, 0, sizeof( access_sys_t ) );
146 p_sys->vcddev = vcddev;
148 /* We read the Table Of Content information */
149 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
150 p_sys->vcddev, &p_sys->p_sectors );
151 if( p_sys->i_titles < 0 )
153 msg_Err( p_access, "unable to count tracks" );
156 else if( p_sys->i_titles <= 1 )
158 msg_Err( p_access, "no movie tracks found" );
161 /* The first title isn't usable */
164 /* Build title table */
165 for( i = 0; i < p_sys->i_titles; i++ )
167 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
169 fprintf( stderr, "title[%d] start=%d\n", i, p_sys->p_sectors[1+i] );
170 fprintf( stderr, "title[%d] end=%d\n", i, p_sys->p_sectors[i+2] );
172 t->i_size = ( p_sys->p_sectors[i+2] - p_sys->p_sectors[i+1] ) *
173 (int64_t)VCD_DATA_SIZE;
176 /* Map entry points into chapters */
177 if( EntryPoints( p_access ) )
179 msg_Warn( p_access, "could not read entry points, will not use them" );
182 /* Starting title/chapter and sector */
183 if( i_title >= p_sys->i_titles )
185 if( i_chapter >= p_sys->title[i_title]->i_seekpoint )
188 p_sys->i_sector = p_sys->p_sectors[1+i_title];
192 ( p_sys->title[i_title]->seekpoint[i_chapter]->i_byte_offset /
195 p_access->info.i_title = i_title;
196 p_access->info.i_seekpoint = i_chapter;
197 p_access->info.i_size = p_sys->title[i_title]->i_size;
198 p_access->info.i_pos = ( p_sys->i_sector - p_sys->p_sectors[1+i_title] ) *
201 p_access->psz_demux = strdup( "ps" );
206 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
211 /*****************************************************************************
213 *****************************************************************************/
214 static void Close( vlc_object_t *p_this )
216 access_t *p_access = (access_t *)p_this;
217 access_sys_t *p_sys = p_access->p_sys;
219 ioctl_Close( p_this, p_sys->vcddev );
223 /*****************************************************************************
225 *****************************************************************************/
226 static int Control( access_t *p_access, int i_query, va_list args )
228 access_sys_t *p_sys = p_access->p_sys;
232 input_title_t ***ppp_title;
238 case ACCESS_CAN_SEEK:
239 case ACCESS_CAN_FASTSEEK:
240 case ACCESS_CAN_PAUSE:
241 case ACCESS_CAN_CONTROL_PACE:
242 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
248 pi_int = (int*)va_arg( args, int * );
249 *pi_int = VCD_DATA_ONCE;
252 case ACCESS_GET_PTS_DELAY:
253 pi_64 = (int64_t*)va_arg( args, int64_t * );
254 *pi_64 = var_GetInteger( p_access, "vcd-caching" ) * 1000;
258 case ACCESS_SET_PAUSE_STATE:
261 case ACCESS_GET_TITLE_INFO:
262 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
263 pi_int = (int*)va_arg( args, int* );
265 /* Duplicate title infos */
266 *pi_int = p_sys->i_titles;
267 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
268 for( i = 0; i < p_sys->i_titles; i++ )
270 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
274 case ACCESS_SET_TITLE:
275 i = (int)va_arg( args, int );
276 if( i != p_access->info.i_title )
279 p_access->info.i_update |=
280 INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
281 p_access->info.i_title = i;
282 p_access->info.i_seekpoint = 0;
283 p_access->info.i_size = p_sys->title[i]->i_size;
284 p_access->info.i_pos = 0;
286 /* Next sector to read */
287 p_sys->i_sector = p_sys->p_sectors[1+i];
291 case ACCESS_SET_SEEKPOINT:
293 input_title_t *t = p_sys->title[p_access->info.i_title];
294 i = (int)va_arg( args, int );
295 if( t->i_seekpoint > 0 )
297 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
298 p_access->info.i_seekpoint = i;
300 p_sys->i_sector = p_sys->p_sectors[1+p_access->info.i_title] +
301 t->seekpoint[i]->i_byte_offset / VCD_DATA_SIZE;
303 p_access->info.i_pos = (int64_t)(p_sys->i_sector -
304 p_sys->p_sectors[1+p_access->info.i_title]) *VCD_DATA_SIZE;
309 case ACCESS_SET_PRIVATE_ID_STATE:
313 msg_Warn( p_access, "unimplemented query in control" );
320 /*****************************************************************************
322 *****************************************************************************/
323 static block_t *Block( access_t *p_access )
325 access_sys_t *p_sys = p_access->p_sys;
326 int i_blocks = VCD_BLOCKS_ONCE;
330 /* Check end of file */
331 if( p_access->info.b_eof ) return NULL;
333 /* Check end of title */
334 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 2] )
336 if( p_access->info.i_title + 2 >= p_sys->i_titles )
338 p_access->info.b_eof = VLC_TRUE;
342 p_access->info.i_update |=
343 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT | INPUT_UPDATE_SIZE;
344 p_access->info.i_title++;
345 p_access->info.i_seekpoint = 0;
346 p_access->info.i_size =
347 p_sys->title[p_access->info.i_title]->i_size;
348 p_access->info.i_pos = 0;
351 /* Don't read after the end of a title */
352 if( p_sys->i_sector + i_blocks >=
353 p_sys->p_sectors[p_access->info.i_title + 2] )
355 i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] -
359 /* Do the actual reading */
360 if( !( p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE ) ) )
362 msg_Err( p_access, "cannot get a new block of size: %i",
363 i_blocks * VCD_DATA_SIZE );
367 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
368 p_sys->i_sector, p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
370 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
371 block_Release( p_block );
373 /* Try to skip one sector (in case of bad sectors) */
375 p_access->info.i_pos += VCD_DATA_SIZE;
379 /* Update seekpoints */
380 for( i_read = 0; i_read < i_blocks; i_read++ )
382 input_title_t *t = p_sys->title[p_access->info.i_title];
384 if( t->i_seekpoint > 0 &&
385 p_access->info.i_seekpoint + 1 < t->i_seekpoint &&
386 p_access->info.i_pos + i_read * VCD_DATA_SIZE >=
387 t->seekpoint[p_access->info.i_seekpoint+1]->i_byte_offset )
389 msg_Dbg( p_access, "seekpoint change" );
390 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
391 p_access->info.i_seekpoint++;
395 /* Update a few values */
396 p_sys->i_sector += i_blocks;
397 p_access->info.i_pos += p_block->i_buffer;
402 /*****************************************************************************
404 *****************************************************************************/
405 static int Seek( access_t *p_access, int64_t i_pos )
407 access_sys_t *p_sys = p_access->p_sys;
408 input_title_t *t = p_sys->title[p_access->info.i_title];
411 /* Next sector to read */
412 p_access->info.i_pos = i_pos;
413 p_sys->i_sector = i_pos / VCD_DATA_SIZE +
414 p_sys->p_sectors[p_access->info.i_title + 1];
416 /* Update current seekpoint */
417 for( i_seekpoint = 0; i_seekpoint < t->i_seekpoint; i_seekpoint++ )
419 if( i_seekpoint + 1 >= t->i_seekpoint ) break;
420 if( i_pos < t->seekpoint[i_seekpoint + 1]->i_byte_offset ) break;
423 if( i_seekpoint != p_access->info.i_seekpoint )
425 msg_Dbg( p_access, "seekpoint change" );
426 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
427 p_access->info.i_seekpoint = i_seekpoint;
433 /*****************************************************************************
434 * EntryPoints: Reads the information about the entry points on the disc.
435 *****************************************************************************/
436 static int EntryPoints( access_t *p_access )
438 access_sys_t *p_sys = p_access->p_sys;
439 uint8_t sector[VCD_DATA_SIZE];
441 entries_sect_t entries;
444 /* Read the entry point sector */
445 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
446 VCD_ENTRIES_SECTOR, sector, 1, VCD_TYPE ) < 0 )
448 msg_Err( p_access, "could not read entry points sector" );
451 memcpy( &entries, sector, CD_SECTOR_SIZE );
453 i_nb = GetWBE( &entries.i_entries_nb );
456 msg_Err( p_access, "invalid entry points number" );
460 if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) ) &&
461 strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ) )
463 msg_Err( p_access, "unrecognized entry points format" );
467 for( i = 0; i < i_nb; i++ )
469 const int i_title = BCD_TO_BIN(entries.entry[i].i_track) - 2;
471 (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
472 BCD_TO_BIN( entries.entry[i].msf.second ),
473 BCD_TO_BIN( entries.entry[i].msf.frame ) ));
476 if( i_title < 0 ) continue; /* Should not occur */
477 if( i_title >= p_sys->i_titles ) continue;
479 msg_Dbg( p_access, "Entry[%d] title=%d sector=%d\n",
480 i, i_title, i_sector );
482 s = vlc_seekpoint_New();
483 s->i_byte_offset = (i_sector - p_sys->p_sectors[i_title+1]) *
486 TAB_APPEND( p_sys->title[i_title]->i_seekpoint,
487 p_sys->title[i_title]->seekpoint, s );