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 *****************************************************************************/
29 #include <vlc_input.h>
30 #include <vlc_access.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 "Caching value for VCDs. This " \
43 "value should be set in milliseconds." )
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 );
57 add_shortcut( "vcd" );
58 add_shortcut( "svcd" );
61 /*****************************************************************************
63 *****************************************************************************/
65 /* how many blocks VCDRead will read in each loop */
66 #define VCD_BLOCKS_ONCE 20
67 #define VCD_DATA_ONCE (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)
71 vcddev_t *vcddev; /* vcd device descriptor */
75 input_title_t *title[99]; /* No more that 99 track in a vcd ? */
78 int i_sector; /* Current Sector */
79 int *p_sectors; /* Track sectors */
82 static block_t *Block( access_t * );
83 static int Seek( access_t *, int64_t );
84 static int Control( access_t *, int, va_list );
85 static int EntryPoints( access_t * );
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
92 access_t *p_access = (access_t *)p_this;
94 char *psz_dup = strdup( p_access->psz_path );
101 /* Command line: vcd://[dev_path][@title[,chapter]] */
102 if( ( psz = strchr( psz_dup, '@' ) ) )
106 i_title = strtol( psz, &psz, 0 );
108 i_chapter = strtol( psz+1, &psz, 0 );
111 if( *psz_dup == '\0' )
115 /* Only when selected */
116 if( strcmp( p_access->psz_access, "vcd" ) &&
117 strcmp( p_access->psz_access, "svcd" ) )
120 psz_dup = var_CreateGetString( p_access, "vcd" );
121 if( *psz_dup == '\0' )
129 if( psz_dup[0] && psz_dup[1] == ':' &&
130 psz_dup[2] == '\\' && psz_dup[3] == '\0' ) psz_dup[2] = '\0';
134 if( !(vcddev = ioctl_Open( p_this, psz_dup )) )
141 /* Set up p_access */
142 p_access->pf_read = NULL;
143 p_access->pf_block = Block;
144 p_access->pf_control = Control;
145 p_access->pf_seek = Seek;
146 p_access->info.i_update = 0;
147 p_access->info.i_size = 0;
148 p_access->info.i_pos = 0;
149 p_access->info.b_eof = VLC_FALSE;
150 p_access->info.i_title = 0;
151 p_access->info.i_seekpoint = 0;
152 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
153 memset( p_sys, 0, sizeof( access_sys_t ) );
154 p_sys->vcddev = vcddev;
156 /* We read the Table Of Content information */
157 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
158 p_sys->vcddev, &p_sys->p_sectors );
159 if( p_sys->i_titles < 0 )
161 msg_Err( p_access, "unable to count tracks" );
164 else if( p_sys->i_titles <= 1 )
166 msg_Err( p_access, "no movie tracks found" );
169 /* The first title isn't usable */
172 /* Build title table */
173 for( i = 0; i < p_sys->i_titles; i++ )
175 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
177 msg_Dbg( p_access, "title[%d] start=%d\n", i, p_sys->p_sectors[1+i] );
178 msg_Dbg( p_access, "title[%d] end=%d\n", i, p_sys->p_sectors[i+2] );
180 t->i_size = ( p_sys->p_sectors[i+2] - p_sys->p_sectors[i+1] ) *
181 (int64_t)VCD_DATA_SIZE;
184 /* Map entry points into chapters */
185 if( EntryPoints( p_access ) )
187 msg_Warn( p_access, "could not read entry points, will not use them" );
190 /* Starting title/chapter and sector */
191 if( i_title >= p_sys->i_titles )
193 if( i_chapter >= p_sys->title[i_title]->i_seekpoint )
196 p_sys->i_sector = p_sys->p_sectors[1+i_title];
200 ( p_sys->title[i_title]->seekpoint[i_chapter]->i_byte_offset /
203 p_access->info.i_title = i_title;
204 p_access->info.i_seekpoint = i_chapter;
205 p_access->info.i_size = p_sys->title[i_title]->i_size;
206 p_access->info.i_pos = ( p_sys->i_sector - p_sys->p_sectors[1+i_title] ) *
209 p_access->psz_demux = strdup( "ps" );
214 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
219 /*****************************************************************************
221 *****************************************************************************/
222 static void Close( vlc_object_t *p_this )
224 access_t *p_access = (access_t *)p_this;
225 access_sys_t *p_sys = p_access->p_sys;
227 ioctl_Close( p_this, p_sys->vcddev );
231 /*****************************************************************************
233 *****************************************************************************/
234 static int Control( access_t *p_access, int i_query, va_list args )
236 access_sys_t *p_sys = p_access->p_sys;
240 input_title_t ***ppp_title;
246 case ACCESS_CAN_SEEK:
247 case ACCESS_CAN_FASTSEEK:
248 case ACCESS_CAN_PAUSE:
249 case ACCESS_CAN_CONTROL_PACE:
250 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
256 pi_int = (int*)va_arg( args, int * );
257 *pi_int = VCD_DATA_ONCE;
260 case ACCESS_GET_PTS_DELAY:
261 pi_64 = (int64_t*)va_arg( args, int64_t * );
262 *pi_64 = var_GetInteger( p_access, "vcd-caching" ) * 1000;
266 case ACCESS_SET_PAUSE_STATE:
269 case ACCESS_GET_TITLE_INFO:
270 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
271 pi_int = (int*)va_arg( args, int* );
273 /* Duplicate title infos */
274 *pi_int = p_sys->i_titles;
275 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
276 for( i = 0; i < p_sys->i_titles; i++ )
278 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
282 case ACCESS_SET_TITLE:
283 i = (int)va_arg( args, int );
284 if( i != p_access->info.i_title )
287 p_access->info.i_update |=
288 INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
289 p_access->info.i_title = i;
290 p_access->info.i_seekpoint = 0;
291 p_access->info.i_size = p_sys->title[i]->i_size;
292 p_access->info.i_pos = 0;
294 /* Next sector to read */
295 p_sys->i_sector = p_sys->p_sectors[1+i];
299 case ACCESS_SET_SEEKPOINT:
301 input_title_t *t = p_sys->title[p_access->info.i_title];
302 i = (int)va_arg( args, int );
303 if( t->i_seekpoint > 0 )
305 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
306 p_access->info.i_seekpoint = i;
308 p_sys->i_sector = p_sys->p_sectors[1+p_access->info.i_title] +
309 t->seekpoint[i]->i_byte_offset / VCD_DATA_SIZE;
311 p_access->info.i_pos = (int64_t)(p_sys->i_sector -
312 p_sys->p_sectors[1+p_access->info.i_title]) *VCD_DATA_SIZE;
317 case ACCESS_SET_PRIVATE_ID_STATE:
318 case ACCESS_GET_CONTENT_TYPE:
322 msg_Warn( p_access, "unimplemented query in control" );
329 /*****************************************************************************
331 *****************************************************************************/
332 static block_t *Block( access_t *p_access )
334 access_sys_t *p_sys = p_access->p_sys;
335 int i_blocks = VCD_BLOCKS_ONCE;
339 /* Check end of file */
340 if( p_access->info.b_eof ) return NULL;
342 /* Check end of title */
343 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 2] )
345 if( p_access->info.i_title + 2 >= p_sys->i_titles )
347 p_access->info.b_eof = VLC_TRUE;
351 p_access->info.i_update |=
352 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT | INPUT_UPDATE_SIZE;
353 p_access->info.i_title++;
354 p_access->info.i_seekpoint = 0;
355 p_access->info.i_size =
356 p_sys->title[p_access->info.i_title]->i_size;
357 p_access->info.i_pos = 0;
360 /* Don't read after the end of a title */
361 if( p_sys->i_sector + i_blocks >=
362 p_sys->p_sectors[p_access->info.i_title + 2] )
364 i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] -
368 /* Do the actual reading */
369 if( !( p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE ) ) )
371 msg_Err( p_access, "cannot get a new block of size: %i",
372 i_blocks * VCD_DATA_SIZE );
376 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
377 p_sys->i_sector, p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
379 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
380 block_Release( p_block );
382 /* Try to skip one sector (in case of bad sectors) */
384 p_access->info.i_pos += VCD_DATA_SIZE;
388 /* Update seekpoints */
389 for( i_read = 0; i_read < i_blocks; i_read++ )
391 input_title_t *t = p_sys->title[p_access->info.i_title];
393 if( t->i_seekpoint > 0 &&
394 p_access->info.i_seekpoint + 1 < t->i_seekpoint &&
395 p_access->info.i_pos + i_read * VCD_DATA_SIZE >=
396 t->seekpoint[p_access->info.i_seekpoint+1]->i_byte_offset )
398 msg_Dbg( p_access, "seekpoint change" );
399 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
400 p_access->info.i_seekpoint++;
404 /* Update a few values */
405 p_sys->i_sector += i_blocks;
406 p_access->info.i_pos += p_block->i_buffer;
411 /*****************************************************************************
413 *****************************************************************************/
414 static int Seek( access_t *p_access, int64_t i_pos )
416 access_sys_t *p_sys = p_access->p_sys;
417 input_title_t *t = p_sys->title[p_access->info.i_title];
420 /* Next sector to read */
421 p_access->info.i_pos = i_pos;
422 p_sys->i_sector = i_pos / VCD_DATA_SIZE +
423 p_sys->p_sectors[p_access->info.i_title + 1];
425 /* Update current seekpoint */
426 for( i_seekpoint = 0; i_seekpoint < t->i_seekpoint; i_seekpoint++ )
428 if( i_seekpoint + 1 >= t->i_seekpoint ) break;
429 if( i_pos < t->seekpoint[i_seekpoint + 1]->i_byte_offset ) break;
432 if( i_seekpoint != p_access->info.i_seekpoint )
434 msg_Dbg( p_access, "seekpoint change" );
435 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
436 p_access->info.i_seekpoint = i_seekpoint;
440 p_access->info.b_eof = VLC_FALSE;
445 /*****************************************************************************
446 * EntryPoints: Reads the information about the entry points on the disc.
447 *****************************************************************************/
448 static int EntryPoints( access_t *p_access )
450 access_sys_t *p_sys = p_access->p_sys;
451 uint8_t sector[VCD_DATA_SIZE];
453 entries_sect_t entries;
456 /* Read the entry point sector */
457 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
458 VCD_ENTRIES_SECTOR, sector, 1, VCD_TYPE ) < 0 )
460 msg_Err( p_access, "could not read entry points sector" );
463 memcpy( &entries, sector, CD_SECTOR_SIZE );
465 i_nb = GetWBE( &entries.i_entries_nb );
468 msg_Err( p_access, "invalid entry points number" );
472 if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) ) &&
473 strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ) )
475 msg_Err( p_access, "unrecognized entry points format" );
479 for( i = 0; i < i_nb; i++ )
481 const int i_title = BCD_TO_BIN(entries.entry[i].i_track) - 2;
483 (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
484 BCD_TO_BIN( entries.entry[i].msf.second ),
485 BCD_TO_BIN( entries.entry[i].msf.frame ) ));
488 if( i_title < 0 ) continue; /* Should not occur */
489 if( i_title >= p_sys->i_titles ) continue;
491 msg_Dbg( p_access, "Entry[%d] title=%d sector=%d\n",
492 i, i_title, i_sector );
494 s = vlc_seekpoint_New();
495 s->i_byte_offset = (i_sector - p_sys->p_sectors[i_title+1]) *
498 TAB_APPEND( p_sys->title[i_title]->i_seekpoint,
499 p_sys->title[i_title]->seekpoint, s );