]> git.sesse.net Git - vlc/blob - modules/access/vcd/vcd.c
update module LIST file.
[vlc] / modules / access / vcd / vcd.c
1 /*****************************************************************************
2  * vcd.c : VCD input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
5  * $Id$
6  *
7  * Author: Johan Bilien <jobi@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_input.h>
34 #include <vlc_access.h>
35
36 #include "cdrom.h"
37
38 /*****************************************************************************
39  * Module descriptior
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 #define CACHING_TEXT N_("Caching value in ms")
45 #define CACHING_LONGTEXT N_( \
46     "Caching value for VCDs. This " \
47     "value should be set in milliseconds." )
48
49 vlc_module_begin();
50     set_shortname( _("VCD"));
51     set_description( _("VCD input") );
52     set_capability( "access2", 60 );
53     set_callbacks( Open, Close );
54     set_category( CAT_INPUT );
55     set_subcategory( SUBCAT_INPUT_ACCESS );
56
57     add_usage_hint( N_("[vcd:][device][@[title][,[chapter]]]") );
58     add_integer( "vcd-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
59                  CACHING_LONGTEXT, VLC_TRUE );
60     add_shortcut( "vcd" );
61     add_shortcut( "svcd" );
62 vlc_module_end();
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67
68 /* how many blocks VCDRead will read in each loop */
69 #define VCD_BLOCKS_ONCE 20
70 #define VCD_DATA_ONCE   (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)
71
72 struct access_sys_t
73 {
74     vcddev_t    *vcddev;                            /* vcd device descriptor */
75
76     /* Title infos */
77     int           i_titles;
78     input_title_t *title[99];            /* No more that 99 track in a vcd ? */
79
80
81     int         i_sector;                                  /* Current Sector */
82     int         *p_sectors;                                 /* Track sectors */
83 };
84
85 static block_t *Block( access_t * );
86 static int      Seek( access_t *, int64_t );
87 static int      Control( access_t *, int, va_list );
88 static int      EntryPoints( access_t * );
89
90 /*****************************************************************************
91  * VCDOpen: open vcd
92  *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
94 {
95     access_t     *p_access = (access_t *)p_this;
96     access_sys_t *p_sys;
97     char *psz_dup = strdup( p_access->psz_path );
98     char *psz;
99     int i_title = 0;
100     int i_chapter = 0;
101     int i;
102     vcddev_t *vcddev;
103
104     /* Command line: vcd://[dev_path][@title[,chapter]] */
105     if( ( psz = strchr( psz_dup, '@' ) ) )
106     {
107         *psz++ = '\0';
108
109         i_title = strtol( psz, &psz, 0 );
110         if( *psz )
111             i_chapter = strtol( psz+1, &psz, 0 );
112     }
113
114     if( *psz_dup == '\0' )
115     {
116         free( psz_dup );
117
118         /* Only when selected */
119         if( strcmp( p_access->psz_access, "vcd" ) &&
120             strcmp( p_access->psz_access, "svcd" ) )
121             return VLC_EGENERIC;
122
123         psz_dup = var_CreateGetString( p_access, "vcd" );
124         if( *psz_dup == '\0' )
125         {
126             free( psz_dup );
127             return VLC_EGENERIC;
128         }
129     }
130
131 #ifdef WIN32
132     if( psz_dup[0] && psz_dup[1] == ':' &&
133         psz_dup[2] == '\\' && psz_dup[3] == '\0' ) psz_dup[2] = '\0';
134 #endif
135
136     /* Open VCD */
137     if( !(vcddev = ioctl_Open( p_this, psz_dup )) )
138     {
139         free( psz_dup );
140         return VLC_EGENERIC;
141     }
142     free( psz_dup );
143
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 = VLC_FALSE;
153     p_access->info.i_title = 0;
154     p_access->info.i_seekpoint = 0;
155     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
156     memset( p_sys, 0, sizeof( access_sys_t ) );
157     p_sys->vcddev = vcddev;
158
159     /* We read the Table Of Content information */
160     p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
161                                           p_sys->vcddev, &p_sys->p_sectors );
162     if( p_sys->i_titles < 0 )
163     {
164         msg_Err( p_access, "unable to count tracks" );
165         goto error;
166     }
167     else if( p_sys->i_titles <= 1 )
168     {
169         msg_Err( p_access, "no movie tracks found" );
170         goto error;
171     }
172     /* The first title isn't usable */
173     p_sys->i_titles--;
174
175     /* Build title table */
176     for( i = 0; i < p_sys->i_titles; i++ )
177     {
178         input_title_t *t = p_sys->title[i] = vlc_input_title_New();
179
180         msg_Dbg( p_access, "title[%d] start=%d\n", i, p_sys->p_sectors[1+i] );
181         msg_Dbg( p_access, "title[%d] end=%d\n", i, p_sys->p_sectors[i+2] );
182
183         t->i_size = ( p_sys->p_sectors[i+2] - p_sys->p_sectors[i+1] ) *
184                     (int64_t)VCD_DATA_SIZE;
185     }
186
187     /* Map entry points into chapters */
188     if( EntryPoints( p_access ) )
189     {
190         msg_Warn( p_access, "could not read entry points, will not use them" );
191     }
192
193     /* Starting title/chapter and sector */
194     if( i_title >= p_sys->i_titles )
195         i_title = 0;
196     if( i_chapter >= p_sys->title[i_title]->i_seekpoint )
197         i_chapter = 0;
198
199     p_sys->i_sector = p_sys->p_sectors[1+i_title];
200     if( i_chapter > 0 )
201     {
202         p_sys->i_sector +=
203             ( p_sys->title[i_title]->seekpoint[i_chapter]->i_byte_offset /
204               VCD_DATA_SIZE );
205     }
206     p_access->info.i_title = i_title;
207     p_access->info.i_seekpoint = i_chapter;
208     p_access->info.i_size = p_sys->title[i_title]->i_size;
209     p_access->info.i_pos = ( p_sys->i_sector - p_sys->p_sectors[1+i_title] ) *
210         VCD_DATA_SIZE;
211
212     p_access->psz_demux = strdup( "ps" );
213
214     return VLC_SUCCESS;
215
216 error:
217     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
218     free( p_sys );
219     return VLC_EGENERIC;
220 }
221
222 /*****************************************************************************
223  * Close: closes vcd
224  *****************************************************************************/
225 static void Close( vlc_object_t *p_this )
226 {
227     access_t     *p_access = (access_t *)p_this;
228     access_sys_t *p_sys = p_access->p_sys;
229
230     ioctl_Close( p_this, p_sys->vcddev );
231     free( p_sys );
232 }
233
234 /*****************************************************************************
235  * Control:
236  *****************************************************************************/
237 static int Control( access_t *p_access, int i_query, va_list args )
238 {
239     access_sys_t *p_sys = p_access->p_sys;
240     vlc_bool_t   *pb_bool;
241     int          *pi_int;
242     int64_t      *pi_64;
243     input_title_t ***ppp_title;
244     int i;
245
246     switch( i_query )
247     {
248         /* */
249         case ACCESS_CAN_SEEK:
250         case ACCESS_CAN_FASTSEEK:
251         case ACCESS_CAN_PAUSE:
252         case ACCESS_CAN_CONTROL_PACE:
253             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
254             *pb_bool = VLC_TRUE;
255             break;
256
257         /* */
258         case ACCESS_GET_MTU:
259             pi_int = (int*)va_arg( args, int * );
260             *pi_int = VCD_DATA_ONCE;
261             break;
262
263         case ACCESS_GET_PTS_DELAY:
264             pi_64 = (int64_t*)va_arg( args, int64_t * );
265             *pi_64 = var_GetInteger( p_access, "vcd-caching" ) * 1000;
266             break;
267
268         /* */
269         case ACCESS_SET_PAUSE_STATE:
270             break;
271
272         case ACCESS_GET_TITLE_INFO:
273             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
274             pi_int    = (int*)va_arg( args, int* );
275
276             /* Duplicate title infos */
277             *pi_int = p_sys->i_titles;
278             *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
279             for( i = 0; i < p_sys->i_titles; i++ )
280             {
281                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
282             }
283             break;
284
285         case ACCESS_SET_TITLE:
286             i = (int)va_arg( args, int );
287             if( i != p_access->info.i_title )
288             {
289                 /* Update info */
290                 p_access->info.i_update |=
291                   INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
292                 p_access->info.i_title = i;
293                 p_access->info.i_seekpoint = 0;
294                 p_access->info.i_size = p_sys->title[i]->i_size;
295                 p_access->info.i_pos  = 0;
296
297                 /* Next sector to read */
298                 p_sys->i_sector = p_sys->p_sectors[1+i];
299             }
300             break;
301
302         case ACCESS_SET_SEEKPOINT:
303         {
304             input_title_t *t = p_sys->title[p_access->info.i_title];
305             i = (int)va_arg( args, int );
306             if( t->i_seekpoint > 0 )
307             {
308                 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
309                 p_access->info.i_seekpoint = i;
310
311                 p_sys->i_sector = p_sys->p_sectors[1+p_access->info.i_title] +
312                     t->seekpoint[i]->i_byte_offset / VCD_DATA_SIZE;
313
314                 p_access->info.i_pos = (int64_t)(p_sys->i_sector -
315                     p_sys->p_sectors[1+p_access->info.i_title]) *VCD_DATA_SIZE;
316             }
317             return VLC_SUCCESS;
318         }
319
320         case ACCESS_SET_PRIVATE_ID_STATE:
321         case ACCESS_GET_CONTENT_TYPE:
322             return VLC_EGENERIC;
323
324         default:
325             msg_Warn( p_access, "unimplemented query in control" );
326             return VLC_EGENERIC;
327
328     }
329     return VLC_SUCCESS;
330 }
331
332 /*****************************************************************************
333  * Block:
334  *****************************************************************************/
335 static block_t *Block( access_t *p_access )
336 {
337     access_sys_t *p_sys = p_access->p_sys;
338     int i_blocks = VCD_BLOCKS_ONCE;
339     block_t *p_block;
340     int i_read;
341
342     /* Check end of file */
343     if( p_access->info.b_eof ) return NULL;
344
345     /* Check end of title */
346     while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 2] )
347     {
348         if( p_access->info.i_title + 2 >= p_sys->i_titles )
349         {
350             p_access->info.b_eof = VLC_TRUE;
351             return NULL;
352         }
353
354         p_access->info.i_update |=
355             INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT | INPUT_UPDATE_SIZE;
356         p_access->info.i_title++;
357         p_access->info.i_seekpoint = 0;
358         p_access->info.i_size =
359             p_sys->title[p_access->info.i_title]->i_size;
360         p_access->info.i_pos = 0;
361     }
362
363     /* Don't read after the end of a title */
364     if( p_sys->i_sector + i_blocks >=
365         p_sys->p_sectors[p_access->info.i_title + 2] )
366     {
367         i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] -
368                    p_sys->i_sector;
369     }
370
371     /* Do the actual reading */
372     if( !( p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE ) ) )
373     {
374         msg_Err( p_access, "cannot get a new block of size: %i",
375                  i_blocks * VCD_DATA_SIZE );
376         return NULL;
377     }
378
379     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
380             p_sys->i_sector, p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
381     {
382         msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
383         block_Release( p_block );
384
385         /* Try to skip one sector (in case of bad sectors) */
386         p_sys->i_sector++;
387         p_access->info.i_pos += VCD_DATA_SIZE;
388         return NULL;
389     }
390
391     /* Update seekpoints */
392     for( i_read = 0; i_read < i_blocks; i_read++ )
393     {
394         input_title_t *t = p_sys->title[p_access->info.i_title];
395
396         if( t->i_seekpoint > 0 &&
397             p_access->info.i_seekpoint + 1 < t->i_seekpoint &&
398             p_access->info.i_pos + i_read * VCD_DATA_SIZE >=
399             t->seekpoint[p_access->info.i_seekpoint+1]->i_byte_offset )
400         {
401             msg_Dbg( p_access, "seekpoint change" );
402             p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
403             p_access->info.i_seekpoint++;
404         }
405     }
406
407     /* Update a few values */
408     p_sys->i_sector += i_blocks;
409     p_access->info.i_pos += p_block->i_buffer;
410
411     return p_block;
412 }
413
414 /*****************************************************************************
415  * Seek:
416  *****************************************************************************/
417 static int Seek( access_t *p_access, int64_t i_pos )
418 {
419     access_sys_t *p_sys = p_access->p_sys;
420     input_title_t *t = p_sys->title[p_access->info.i_title];
421     int i_seekpoint;
422
423     /* Next sector to read */
424     p_access->info.i_pos = i_pos;
425     p_sys->i_sector = i_pos / VCD_DATA_SIZE +
426         p_sys->p_sectors[p_access->info.i_title + 1];
427
428     /* Update current seekpoint */
429     for( i_seekpoint = 0; i_seekpoint < t->i_seekpoint; i_seekpoint++ )
430     {
431         if( i_seekpoint + 1 >= t->i_seekpoint ) break;
432         if( i_pos < t->seekpoint[i_seekpoint + 1]->i_byte_offset ) break;
433     }
434
435     if( i_seekpoint != p_access->info.i_seekpoint )
436     {
437         msg_Dbg( p_access, "seekpoint change" );
438         p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
439         p_access->info.i_seekpoint = i_seekpoint;
440     }
441
442     /* Reset eof */
443     p_access->info.b_eof = VLC_FALSE;
444
445     return VLC_SUCCESS;
446 }
447
448 /*****************************************************************************
449  * EntryPoints: Reads the information about the entry points on the disc.
450  *****************************************************************************/
451 static int EntryPoints( access_t *p_access )
452 {
453     access_sys_t *p_sys = p_access->p_sys;
454     uint8_t      sector[VCD_DATA_SIZE];
455
456     entries_sect_t entries;
457     int i_nb, i;
458
459     /* Read the entry point sector */
460     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
461         VCD_ENTRIES_SECTOR, sector, 1, VCD_TYPE ) < 0 )
462     {
463         msg_Err( p_access, "could not read entry points sector" );
464         return VLC_EGENERIC;
465     }
466     memcpy( &entries, sector, CD_SECTOR_SIZE );
467
468     i_nb = GetWBE( &entries.i_entries_nb );
469     if( i_nb > 500 )
470     {
471         msg_Err( p_access, "invalid entry points number" );
472         return VLC_EGENERIC;
473     }
474
475     if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) ) &&
476         strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ) )
477     {
478         msg_Err( p_access, "unrecognized entry points format" );
479         return VLC_EGENERIC;
480     }
481
482     for( i = 0; i < i_nb; i++ )
483     {
484         const int i_title = BCD_TO_BIN(entries.entry[i].i_track) - 2;
485         const int i_sector =
486             (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
487                           BCD_TO_BIN( entries.entry[i].msf.second ),
488                           BCD_TO_BIN( entries.entry[i].msf.frame  ) ));
489         seekpoint_t *s;
490
491         if( i_title < 0 ) continue;   /* Should not occur */
492         if( i_title >= p_sys->i_titles ) continue;
493
494         msg_Dbg( p_access, "Entry[%d] title=%d sector=%d\n",
495                  i, i_title, i_sector );
496
497         s = vlc_seekpoint_New();
498         s->i_byte_offset = (i_sector - p_sys->p_sectors[i_title+1]) *
499             VCD_DATA_SIZE;
500
501         TAB_APPEND( p_sys->title[i_title]->i_seekpoint,
502                     p_sys->title[i_title]->seekpoint, s );
503     }
504
505     return VLC_SUCCESS;
506 }
507