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 along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_input.h>
35 #include <vlc_access.h>
36 #include <vlc_charset.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 #define CACHING_TEXT N_("Caching value in ms")
47 #define CACHING_LONGTEXT N_( \
48 "Caching value for VCDs. This " \
49 "value should be set in milliseconds." )
52 set_shortname( N_("VCD"))
53 set_description( N_("VCD input") )
54 set_capability( "access", 60 )
55 set_callbacks( Open, Close )
56 set_category( CAT_INPUT )
57 set_subcategory( SUBCAT_INPUT_ACCESS )
59 add_usage_hint( N_("[vcd:][device][@[title][,[chapter]]]") )
60 add_integer( "vcd-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
61 CACHING_LONGTEXT, true )
63 add_shortcut( "svcd" )
66 /*****************************************************************************
68 *****************************************************************************/
70 /* how many blocks VCDRead will read in each loop */
71 #define VCD_BLOCKS_ONCE 20
72 #define VCD_DATA_ONCE (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)
76 vcddev_t *vcddev; /* vcd device descriptor */
80 input_title_t *title[99]; /* No more that 99 track in a vcd ? */
83 int i_sector; /* Current Sector */
84 int *p_sectors; /* Track sectors */
87 static block_t *Block( access_t * );
88 static int Seek( access_t *, uint64_t );
89 static int Control( access_t *, int, va_list );
90 static int EntryPoints( access_t * );
92 /*****************************************************************************
94 *****************************************************************************/
95 static int Open( vlc_object_t *p_this )
97 access_t *p_access = (access_t *)p_this;
99 char *psz_dup = ToLocaleDup( p_access->psz_path );
106 /* Command line: vcd://[dev_path][@title[,chapter]] */
107 if( ( psz = strchr( psz_dup, '@' ) ) )
111 i_title = strtol( psz, &psz, 0 );
113 i_chapter = strtol( psz+1, &psz, 0 );
116 if( *psz_dup == '\0' )
120 /* Only when selected */
121 if( strcmp( p_access->psz_access, "vcd" ) &&
122 strcmp( p_access->psz_access, "svcd" ) )
125 psz_dup = var_CreateGetString( p_access, "vcd" );
126 if( *psz_dup == '\0' )
134 if( psz_dup[0] && psz_dup[1] == ':' &&
135 psz_dup[2] == '\\' && psz_dup[3] == '\0' ) psz_dup[2] = '\0';
139 vcddev = ioctl_Open( p_this, psz_dup );
144 /* Set up p_access */
145 p_access->pf_read = NULL;
146 p_access->pf_block = Block;
147 p_access->pf_control = Control;
148 p_access->pf_seek = Seek;
149 p_access->info.i_update = 0;
150 p_access->info.i_size = 0;
151 p_access->info.i_pos = 0;
152 p_access->info.b_eof = false;
153 p_access->info.i_title = 0;
154 p_access->info.i_seekpoint = 0;
155 p_access->p_sys = p_sys = calloc( 1, sizeof( access_sys_t ) );
158 p_sys->vcddev = vcddev;
160 /* We read the Table Of Content information */
161 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
162 p_sys->vcddev, &p_sys->p_sectors );
163 if( p_sys->i_titles < 0 )
165 msg_Err( p_access, "unable to count tracks" );
168 else if( p_sys->i_titles <= 1 )
170 msg_Err( p_access, "no movie tracks found" );
173 /* The first title isn't usable */
176 /* Build title table */
177 for( i = 0; i < p_sys->i_titles; i++ )
179 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
181 msg_Dbg( p_access, "title[%d] start=%d", i, p_sys->p_sectors[1+i] );
182 msg_Dbg( p_access, "title[%d] end=%d", i, p_sys->p_sectors[i+2] );
184 t->i_size = ( p_sys->p_sectors[i+2] - p_sys->p_sectors[i+1] ) *
185 (int64_t)VCD_DATA_SIZE;
188 /* Map entry points into chapters */
189 if( EntryPoints( p_access ) )
191 msg_Warn( p_access, "could not read entry points, will not use them" );
194 /* Starting title/chapter and sector */
195 if( i_title >= p_sys->i_titles )
197 if( i_chapter >= p_sys->title[i_title]->i_seekpoint )
200 p_sys->i_sector = p_sys->p_sectors[1+i_title];
204 ( p_sys->title[i_title]->seekpoint[i_chapter]->i_byte_offset /
207 p_access->info.i_title = i_title;
208 p_access->info.i_seekpoint = i_chapter;
209 p_access->info.i_size = p_sys->title[i_title]->i_size;
210 p_access->info.i_pos = ( p_sys->i_sector - p_sys->p_sectors[1+i_title] ) *
213 free( p_access->psz_demux );
214 p_access->psz_demux = strdup( "ps" );
219 ioctl_Close( VLC_OBJECT(p_access), vcddev );
224 /*****************************************************************************
226 *****************************************************************************/
227 static void Close( vlc_object_t *p_this )
229 access_t *p_access = (access_t *)p_this;
230 access_sys_t *p_sys = p_access->p_sys;
232 ioctl_Close( p_this, p_sys->vcddev );
236 /*****************************************************************************
238 *****************************************************************************/
239 static int Control( access_t *p_access, int i_query, va_list args )
241 access_sys_t *p_sys = p_access->p_sys;
242 input_title_t ***ppp_title;
248 case ACCESS_CAN_SEEK:
249 case ACCESS_CAN_FASTSEEK:
250 case ACCESS_CAN_PAUSE:
251 case ACCESS_CAN_CONTROL_PACE:
252 *va_arg( args, bool* ) = true;
256 case ACCESS_GET_PTS_DELAY:
257 *va_arg( args, int64_t * )
258 = (int64_t)var_GetInteger(p_access,"vcd-caching") * 1000;
262 case ACCESS_SET_PAUSE_STATE:
265 case ACCESS_GET_TITLE_INFO:
266 ppp_title = va_arg( args, input_title_t*** );
267 *va_arg( args, int* ) = p_sys->i_titles;
269 /* Duplicate title infos */
270 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
271 for( i = 0; i < p_sys->i_titles; i++ )
273 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
277 case ACCESS_SET_TITLE:
278 i = va_arg( args, int );
279 if( i != p_access->info.i_title )
282 p_access->info.i_update |=
283 INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
284 p_access->info.i_title = i;
285 p_access->info.i_seekpoint = 0;
286 p_access->info.i_size = p_sys->title[i]->i_size;
287 p_access->info.i_pos = 0;
289 /* Next sector to read */
290 p_sys->i_sector = p_sys->p_sectors[1+i];
294 case ACCESS_SET_SEEKPOINT:
296 input_title_t *t = p_sys->title[p_access->info.i_title];
297 i = va_arg( args, int );
298 if( t->i_seekpoint > 0 )
300 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
301 p_access->info.i_seekpoint = i;
303 p_sys->i_sector = p_sys->p_sectors[1+p_access->info.i_title] +
304 t->seekpoint[i]->i_byte_offset / VCD_DATA_SIZE;
306 p_access->info.i_pos = (uint64_t)(p_sys->i_sector -
307 p_sys->p_sectors[1+p_access->info.i_title]) *VCD_DATA_SIZE;
312 case ACCESS_SET_PRIVATE_ID_STATE:
313 case ACCESS_GET_CONTENT_TYPE:
317 msg_Warn( p_access, "unimplemented query in control" );
324 /*****************************************************************************
326 *****************************************************************************/
327 static block_t *Block( access_t *p_access )
329 access_sys_t *p_sys = p_access->p_sys;
330 int i_blocks = VCD_BLOCKS_ONCE;
334 /* Check end of file */
335 if( p_access->info.b_eof ) return NULL;
337 /* Check end of title */
338 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 2] )
340 if( p_access->info.i_title + 2 >= p_sys->i_titles )
342 p_access->info.b_eof = true;
346 p_access->info.i_update |=
347 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT | INPUT_UPDATE_SIZE;
348 p_access->info.i_title++;
349 p_access->info.i_seekpoint = 0;
350 p_access->info.i_size =
351 p_sys->title[p_access->info.i_title]->i_size;
352 p_access->info.i_pos = 0;
355 /* Don't read after the end of a title */
356 if( p_sys->i_sector + i_blocks >=
357 p_sys->p_sectors[p_access->info.i_title + 2] )
359 i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] -
363 /* Do the actual reading */
364 if( !( p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE ) ) )
366 msg_Err( p_access, "cannot get a new block of size: %i",
367 i_blocks * VCD_DATA_SIZE );
371 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
372 p_sys->i_sector, p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
374 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
375 block_Release( p_block );
377 /* Try to skip one sector (in case of bad sectors) */
379 p_access->info.i_pos += VCD_DATA_SIZE;
383 /* Update seekpoints */
384 for( i_read = 0; i_read < i_blocks; i_read++ )
386 input_title_t *t = p_sys->title[p_access->info.i_title];
388 if( t->i_seekpoint > 0 &&
389 p_access->info.i_seekpoint + 1 < t->i_seekpoint &&
390 p_access->info.i_pos + i_read * VCD_DATA_SIZE >=
391 t->seekpoint[p_access->info.i_seekpoint+1]->i_byte_offset )
393 msg_Dbg( p_access, "seekpoint change" );
394 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
395 p_access->info.i_seekpoint++;
399 /* Update a few values */
400 p_sys->i_sector += i_blocks;
401 p_access->info.i_pos += p_block->i_buffer;
406 /*****************************************************************************
408 *****************************************************************************/
409 static int Seek( access_t *p_access, uint64_t i_pos )
411 access_sys_t *p_sys = p_access->p_sys;
412 input_title_t *t = p_sys->title[p_access->info.i_title];
415 /* Next sector to read */
416 p_access->info.i_pos = i_pos;
417 p_sys->i_sector = i_pos / VCD_DATA_SIZE +
418 p_sys->p_sectors[p_access->info.i_title + 1];
420 /* Update current seekpoint */
421 for( i_seekpoint = 0; i_seekpoint < t->i_seekpoint; i_seekpoint++ )
423 if( i_seekpoint + 1 >= t->i_seekpoint ) break;
424 if( i_pos < t->seekpoint[i_seekpoint + 1]->i_byte_offset ) break;
427 if( i_seekpoint != p_access->info.i_seekpoint )
429 msg_Dbg( p_access, "seekpoint change" );
430 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
431 p_access->info.i_seekpoint = i_seekpoint;
435 p_access->info.b_eof = false;
440 /*****************************************************************************
441 * EntryPoints: Reads the information about the entry points on the disc.
442 *****************************************************************************/
443 static int EntryPoints( access_t *p_access )
445 access_sys_t *p_sys = p_access->p_sys;
446 uint8_t sector[VCD_DATA_SIZE];
448 entries_sect_t entries;
451 /* Read the entry point sector */
452 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
453 VCD_ENTRIES_SECTOR, sector, 1, VCD_TYPE ) < 0 )
455 msg_Err( p_access, "could not read entry points sector" );
458 memcpy( &entries, sector, CD_SECTOR_SIZE );
460 i_nb = GetWBE( &entries.i_entries_nb );
463 msg_Err( p_access, "invalid entry points number" );
467 if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) ) &&
468 strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ) )
470 msg_Err( p_access, "unrecognized entry points format" );
474 for( i = 0; i < i_nb; i++ )
476 const int i_title = BCD_TO_BIN(entries.entry[i].i_track) - 2;
478 (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
479 BCD_TO_BIN( entries.entry[i].msf.second ),
480 BCD_TO_BIN( entries.entry[i].msf.frame ) ));
483 if( i_title < 0 ) continue; /* Should not occur */
484 if( i_title >= p_sys->i_titles ) continue;
486 msg_Dbg( p_access, "Entry[%d] title=%d sector=%d",
487 i, i_title, i_sector );
489 s = vlc_seekpoint_New();
490 s->i_byte_offset = (i_sector - p_sys->p_sectors[i_title+1]) *
493 TAB_APPEND( p_sys->title[i_title]->i_seekpoint,
494 p_sys->title[i_title]->seekpoint, s );