00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdlib.h>
00028
00029 #include <vlc/vlc.h>
00030 #include <vlc/input.h>
00031
00032 #include "cdrom.h"
00033
00034
00035
00036
00037 static int Open ( vlc_object_t * );
00038 static void Close( vlc_object_t * );
00039
00040 #define CACHING_TEXT N_("Caching value in ms")
00041 #define CACHING_LONGTEXT N_( \
00042 "Allows you to modify the default caching value for cdda streams. This " \
00043 "value should be set in milliseconds units." )
00044
00045 vlc_module_begin();
00046 set_shortname( _("VCD"));
00047 set_description( _("VCD input") );
00048 set_capability( "access2", 60 );
00049 set_callbacks( Open, Close );
00050 set_category( CAT_INPUT );
00051 set_subcategory( SUBCAT_INPUT_ACCESS );
00052
00053 add_usage_hint( N_("[vcd:][device][@[title][,[chapter]]]") );
00054 add_integer( "vcd-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
00055 CACHING_LONGTEXT, VLC_TRUE );
00056 add_shortcut( "vcd" );
00057 add_shortcut( "svcd" );
00058 vlc_module_end();
00059
00060
00061
00062
00063
00064
00065 #define VCD_BLOCKS_ONCE 20
00066 #define VCD_DATA_ONCE (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)
00067
00068 struct access_sys_t
00069 {
00070 vcddev_t *vcddev;
00071
00072
00073 int i_titles;
00074 input_title_t *title[99];
00075
00076
00077 int i_sector;
00078 int *p_sectors;
00079 };
00080
00081 static block_t *Block( access_t * );
00082 static int Seek( access_t *, int64_t );
00083 static int Control( access_t *, int, va_list );
00084 static int EntryPoints( access_t * );
00085
00086
00087
00088
00089 static int Open( vlc_object_t *p_this )
00090 {
00091 access_t *p_access = (access_t *)p_this;
00092 access_sys_t *p_sys;
00093 char *psz_dup = strdup( p_access->psz_path );
00094 char *psz;
00095 int i_title = 0;
00096 int i_chapter = 0;
00097 int i;
00098 vcddev_t *vcddev;
00099
00100
00101 if( ( psz = strchr( psz_dup, '@' ) ) )
00102 {
00103 *psz++ = '\0';
00104
00105 i_title = strtol( psz, &psz, 0 );
00106 if( *psz )
00107 i_chapter = strtol( psz+1, &psz, 0 );
00108 }
00109
00110 if( *psz_dup == '\0' )
00111 {
00112 free( psz_dup );
00113
00114
00115 if( strcmp( p_access->psz_access, "vcd" ) &&
00116 strcmp( p_access->psz_access, "svcd" ) )
00117 return VLC_EGENERIC;
00118
00119 psz_dup = var_CreateGetString( p_access, "vcd" );
00120 if( *psz_dup == '\0' )
00121 {
00122 free( psz_dup );
00123 return VLC_EGENERIC;
00124 }
00125 }
00126
00127
00128 if( !(vcddev = ioctl_Open( p_this, psz_dup )) )
00129 {
00130 free( psz_dup );
00131 return VLC_EGENERIC;
00132 }
00133 free( psz_dup );
00134
00135
00136 p_access->pf_read = NULL;
00137 p_access->pf_block = Block;
00138 p_access->pf_control = Control;
00139 p_access->pf_seek = Seek;
00140 p_access->info.i_update = 0;
00141 p_access->info.i_size = 0;
00142 p_access->info.i_pos = 0;
00143 p_access->info.b_eof = VLC_FALSE;
00144 p_access->info.i_title = 0;
00145 p_access->info.i_seekpoint = 0;
00146 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
00147 memset( p_sys, 0, sizeof( access_sys_t ) );
00148 p_sys->vcddev = vcddev;
00149
00150
00151 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
00152 p_sys->vcddev, &p_sys->p_sectors );
00153 if( p_sys->i_titles < 0 )
00154 {
00155 msg_Err( p_access, "unable to count tracks" );
00156 goto error;
00157 }
00158 else if( p_sys->i_titles <= 1 )
00159 {
00160 msg_Err( p_access, "no movie tracks found" );
00161 goto error;
00162 }
00163
00164 p_sys->i_titles--;
00165
00166
00167 for( i = 0; i < p_sys->i_titles; i++ )
00168 {
00169 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
00170
00171 fprintf( stderr, "title[%d] start=%d\n", i, p_sys->p_sectors[1+i] );
00172 fprintf( stderr, "title[%d] end=%d\n", i, p_sys->p_sectors[i+2] );
00173
00174 t->i_size = ( p_sys->p_sectors[i+2] - p_sys->p_sectors[i+1] ) *
00175 (int64_t)VCD_DATA_SIZE;
00176 }
00177
00178
00179 if( EntryPoints( p_access ) )
00180 {
00181 msg_Warn( p_access, "could not read entry points, will not use them" );
00182 }
00183
00184
00185 if( i_title >= p_sys->i_titles )
00186 i_title = 0;
00187 if( i_chapter >= p_sys->title[i_title]->i_seekpoint )
00188 i_chapter = 0;
00189
00190 p_sys->i_sector = p_sys->p_sectors[1+i_title];
00191 if( i_chapter > 0 )
00192 {
00193 p_sys->i_sector +=
00194 ( p_sys->title[i_title]->seekpoint[i_chapter]->i_byte_offset /
00195 VCD_DATA_SIZE );
00196 }
00197 p_access->info.i_title = i_title;
00198 p_access->info.i_seekpoint = i_chapter;
00199 p_access->info.i_size = p_sys->title[i_title]->i_size;
00200 p_access->info.i_pos = ( p_sys->i_sector - p_sys->p_sectors[1+i_title] ) *
00201 VCD_DATA_SIZE;
00202
00203 p_access->psz_demux = strdup( "ps" );
00204
00205 return VLC_SUCCESS;
00206
00207 error:
00208 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
00209 free( p_sys );
00210 return VLC_EGENERIC;
00211 }
00212
00213
00214
00215
00216 static void Close( vlc_object_t *p_this )
00217 {
00218 access_t *p_access = (access_t *)p_this;
00219 access_sys_t *p_sys = p_access->p_sys;
00220
00221 ioctl_Close( p_this, p_sys->vcddev );
00222 free( p_sys );
00223 }
00224
00225
00226
00227
00228 static int Control( access_t *p_access, int i_query, va_list args )
00229 {
00230 access_sys_t *p_sys = p_access->p_sys;
00231 vlc_bool_t *pb_bool;
00232 int *pi_int;
00233 int64_t *pi_64;
00234 input_title_t ***ppp_title;
00235 int i;
00236
00237 switch( i_query )
00238 {
00239
00240 case ACCESS_CAN_SEEK:
00241 case ACCESS_CAN_FASTSEEK:
00242 case ACCESS_CAN_PAUSE:
00243 case ACCESS_CAN_CONTROL_PACE:
00244 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
00245 *pb_bool = VLC_TRUE;
00246 break;
00247
00248
00249 case ACCESS_GET_MTU:
00250 pi_int = (int*)va_arg( args, int * );
00251 *pi_int = VCD_DATA_ONCE;
00252 break;
00253
00254 case ACCESS_GET_PTS_DELAY:
00255 pi_64 = (int64_t*)va_arg( args, int64_t * );
00256 *pi_64 = var_GetInteger( p_access, "vcd-caching" ) * 1000;
00257 break;
00258
00259
00260 case ACCESS_SET_PAUSE_STATE:
00261 break;
00262
00263 case ACCESS_GET_TITLE_INFO:
00264 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
00265 pi_int = (int*)va_arg( args, int* );
00266
00267
00268 *pi_int = p_sys->i_titles;
00269 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
00270 for( i = 0; i < p_sys->i_titles; i++ )
00271 {
00272 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
00273 }
00274 break;
00275
00276 case ACCESS_SET_TITLE:
00277 i = (int)va_arg( args, int );
00278 if( i != p_access->info.i_title )
00279 {
00280
00281 p_access->info.i_update |=
00282 INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
00283 p_access->info.i_title = i;
00284 p_access->info.i_seekpoint = 0;
00285 p_access->info.i_size = p_sys->title[i]->i_size;
00286 p_access->info.i_pos = 0;
00287
00288
00289 p_sys->i_sector = p_sys->p_sectors[1+i];
00290 }
00291 break;
00292
00293 case ACCESS_SET_SEEKPOINT:
00294 {
00295 input_title_t *t = p_sys->title[p_access->info.i_title];
00296 i = (int)va_arg( args, int );
00297 if( t->i_seekpoint > 0 )
00298 {
00299 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
00300 p_access->info.i_seekpoint = i;
00301
00302 p_sys->i_sector = p_sys->p_sectors[1+p_access->info.i_title] +
00303 t->seekpoint[i]->i_byte_offset / VCD_DATA_SIZE;
00304
00305 p_access->info.i_pos = (int64_t)(p_sys->i_sector -
00306 p_sys->p_sectors[1+p_access->info.i_title]) *VCD_DATA_SIZE;
00307 }
00308 return VLC_SUCCESS;
00309 }
00310
00311 case ACCESS_SET_PRIVATE_ID_STATE:
00312 return VLC_EGENERIC;
00313
00314 default:
00315 msg_Warn( p_access, "unimplemented query in control" );
00316 return VLC_EGENERIC;
00317
00318 }
00319 return VLC_SUCCESS;
00320 }
00321
00322
00323
00324
00325 static block_t *Block( access_t *p_access )
00326 {
00327 access_sys_t *p_sys = p_access->p_sys;
00328 int i_blocks = VCD_BLOCKS_ONCE;
00329 block_t *p_block;
00330 int i_read;
00331
00332
00333 if( p_access->info.b_eof ) return NULL;
00334
00335
00336 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 2] )
00337 {
00338 if( p_access->info.i_title + 2 >= p_sys->i_titles )
00339 {
00340 p_access->info.b_eof = VLC_TRUE;
00341 return NULL;
00342 }
00343
00344 p_access->info.i_update |=
00345 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT | INPUT_UPDATE_SIZE;
00346 p_access->info.i_title++;
00347 p_access->info.i_seekpoint = 0;
00348 p_access->info.i_size =
00349 p_sys->title[p_access->info.i_title]->i_size;
00350 p_access->info.i_pos = 0;
00351 }
00352
00353
00354 if( p_sys->i_sector + i_blocks >=
00355 p_sys->p_sectors[p_access->info.i_title + 2] )
00356 {
00357 i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] -
00358 p_sys->i_sector;
00359 }
00360
00361
00362 if( !( p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE ) ) )
00363 {
00364 msg_Err( p_access, "cannot get a new block of size: %i",
00365 i_blocks * VCD_DATA_SIZE );
00366 return NULL;
00367 }
00368
00369 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
00370 p_sys->i_sector, p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
00371 {
00372 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
00373 block_Release( p_block );
00374
00375
00376 p_sys->i_sector++;
00377 p_access->info.i_pos += VCD_DATA_SIZE;
00378 return NULL;
00379 }
00380
00381
00382 for( i_read = 0; i_read < i_blocks; i_read++ )
00383 {
00384 input_title_t *t = p_sys->title[p_access->info.i_title];
00385
00386 if( t->i_seekpoint > 0 &&
00387 p_access->info.i_seekpoint + 1 < t->i_seekpoint &&
00388 p_access->info.i_pos + i_read * VCD_DATA_SIZE >=
00389 t->seekpoint[p_access->info.i_seekpoint+1]->i_byte_offset )
00390 {
00391 msg_Dbg( p_access, "seekpoint change" );
00392 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
00393 p_access->info.i_seekpoint++;
00394 }
00395 }
00396
00397
00398 p_sys->i_sector += i_blocks;
00399 p_access->info.i_pos += p_block->i_buffer;
00400
00401 return p_block;
00402 }
00403
00404
00405
00406
00407 static int Seek( access_t *p_access, int64_t i_pos )
00408 {
00409 access_sys_t *p_sys = p_access->p_sys;
00410 input_title_t *t = p_sys->title[p_access->info.i_title];
00411 int i_seekpoint;
00412
00413
00414 p_access->info.i_pos = i_pos;
00415 p_sys->i_sector = i_pos / VCD_DATA_SIZE +
00416 p_sys->p_sectors[p_access->info.i_title + 1];
00417
00418
00419 for( i_seekpoint = 0; i_seekpoint < t->i_seekpoint; i_seekpoint++ )
00420 {
00421 if( i_seekpoint + 1 >= t->i_seekpoint ) break;
00422 if( i_pos < t->seekpoint[i_seekpoint + 1]->i_byte_offset ) break;
00423 }
00424
00425 if( i_seekpoint != p_access->info.i_seekpoint )
00426 {
00427 msg_Dbg( p_access, "seekpoint change" );
00428 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
00429 p_access->info.i_seekpoint = i_seekpoint;
00430 }
00431
00432 return VLC_SUCCESS;
00433 }
00434
00435
00436
00437
00438 static int EntryPoints( access_t *p_access )
00439 {
00440 access_sys_t *p_sys = p_access->p_sys;
00441 uint8_t sector[VCD_DATA_SIZE];
00442
00443 entries_sect_t entries;
00444 int i_nb, i;
00445
00446
00447 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
00448 VCD_ENTRIES_SECTOR, sector, 1, VCD_TYPE ) < 0 )
00449 {
00450 msg_Err( p_access, "could not read entry points sector" );
00451 return VLC_EGENERIC;
00452 }
00453 memcpy( &entries, sector, CD_SECTOR_SIZE );
00454
00455 i_nb = GetWBE( &entries.i_entries_nb );
00456 if( i_nb > 500 )
00457 {
00458 msg_Err( p_access, "invalid entry points number" );
00459 return VLC_EGENERIC;
00460 }
00461
00462 if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) ) &&
00463 strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ) )
00464 {
00465 msg_Err( p_access, "unrecognized entry points format" );
00466 return VLC_EGENERIC;
00467 }
00468
00469 for( i = 0; i < i_nb; i++ )
00470 {
00471 const int i_title = BCD_TO_BIN(entries.entry[i].i_track) - 2;
00472 const int i_sector =
00473 (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
00474 BCD_TO_BIN( entries.entry[i].msf.second ),
00475 BCD_TO_BIN( entries.entry[i].msf.frame ) ));
00476 seekpoint_t *s;
00477
00478 if( i_title < 0 ) continue;
00479 if( i_title >= p_sys->i_titles ) continue;
00480
00481 msg_Dbg( p_access, "Entry[%d] title=%d sector=%d\n",
00482 i, i_title, i_sector );
00483
00484 s = vlc_seekpoint_New();
00485 s->i_byte_offset = (i_sector - p_sys->p_sectors[i_title+1]) *
00486 VCD_DATA_SIZE;
00487
00488 TAB_APPEND( p_sys->title[i_title]->i_seekpoint,
00489 p_sys->title[i_title]->seekpoint, s );
00490 }
00491
00492 return VLC_SUCCESS;
00493 }