]> git.sesse.net Git - vlc/blob - modules/access/vcd/vcd.c
11d134c443aced5828ec7ec57236183657d1e26c
[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 #include <vlc/vlc.h>
29 #include <vlc_input.h>
30 #include <vlc_access.h>
31
32 #include "cdrom.h"
33
34 /*****************************************************************************
35  * Module descriptior
36  *****************************************************************************/
37 static int  Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
39
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." )
44
45 vlc_module_begin();
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 );
52
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 );
56         change_safe();
57     add_shortcut( "vcd" );
58     add_shortcut( "svcd" );
59 vlc_module_end();
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64
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)
68
69 struct access_sys_t
70 {
71     vcddev_t    *vcddev;                            /* vcd device descriptor */
72
73     /* Title infos */
74     int           i_titles;
75     input_title_t *title[99];            /* No more that 99 track in a vcd ? */
76
77
78     int         i_sector;                                  /* Current Sector */
79     int         *p_sectors;                                 /* Track sectors */
80 };
81
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 * );
86
87 /*****************************************************************************
88  * VCDOpen: open vcd
89  *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
91 {
92     access_t     *p_access = (access_t *)p_this;
93     access_sys_t *p_sys;
94     char *psz_dup = strdup( p_access->psz_path );
95     char *psz;
96     int i_title = 0;
97     int i_chapter = 0;
98     int i;
99     vcddev_t *vcddev;
100
101     /* Command line: vcd://[dev_path][@title[,chapter]] */
102     if( ( psz = strchr( psz_dup, '@' ) ) )
103     {
104         *psz++ = '\0';
105
106         i_title = strtol( psz, &psz, 0 );
107         if( *psz )
108             i_chapter = strtol( psz+1, &psz, 0 );
109     }
110
111     if( *psz_dup == '\0' )
112     {
113         free( psz_dup );
114
115         /* Only when selected */
116         if( strcmp( p_access->psz_access, "vcd" ) &&
117             strcmp( p_access->psz_access, "svcd" ) )
118             return VLC_EGENERIC;
119
120         psz_dup = var_CreateGetString( p_access, "vcd" );
121         if( *psz_dup == '\0' )
122         {
123             free( psz_dup );
124             return VLC_EGENERIC;
125         }
126     }
127
128 #ifdef WIN32
129     if( psz_dup[0] && psz_dup[1] == ':' &&
130         psz_dup[2] == '\\' && psz_dup[3] == '\0' ) psz_dup[2] = '\0';
131 #endif
132
133     /* Open VCD */
134     if( !(vcddev = ioctl_Open( p_this, psz_dup )) )
135     {
136         free( psz_dup );
137         return VLC_EGENERIC;
138     }
139     free( psz_dup );
140
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;
155
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 )
160     {
161         msg_Err( p_access, "unable to count tracks" );
162         goto error;
163     }
164     else if( p_sys->i_titles <= 1 )
165     {
166         msg_Err( p_access, "no movie tracks found" );
167         goto error;
168     }
169     /* The first title isn't usable */
170     p_sys->i_titles--;
171
172     /* Build title table */
173     for( i = 0; i < p_sys->i_titles; i++ )
174     {
175         input_title_t *t = p_sys->title[i] = vlc_input_title_New();
176
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] );
179
180         t->i_size = ( p_sys->p_sectors[i+2] - p_sys->p_sectors[i+1] ) *
181                     (int64_t)VCD_DATA_SIZE;
182     }
183
184     /* Map entry points into chapters */
185     if( EntryPoints( p_access ) )
186     {
187         msg_Warn( p_access, "could not read entry points, will not use them" );
188     }
189
190     /* Starting title/chapter and sector */
191     if( i_title >= p_sys->i_titles )
192         i_title = 0;
193     if( i_chapter >= p_sys->title[i_title]->i_seekpoint )
194         i_chapter = 0;
195
196     p_sys->i_sector = p_sys->p_sectors[1+i_title];
197     if( i_chapter > 0 )
198     {
199         p_sys->i_sector +=
200             ( p_sys->title[i_title]->seekpoint[i_chapter]->i_byte_offset /
201               VCD_DATA_SIZE );
202     }
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] ) *
207         VCD_DATA_SIZE;
208
209     p_access->psz_demux = strdup( "ps" );
210
211     return VLC_SUCCESS;
212
213 error:
214     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
215     free( p_sys );
216     return VLC_EGENERIC;
217 }
218
219 /*****************************************************************************
220  * Close: closes vcd
221  *****************************************************************************/
222 static void Close( vlc_object_t *p_this )
223 {
224     access_t     *p_access = (access_t *)p_this;
225     access_sys_t *p_sys = p_access->p_sys;
226
227     ioctl_Close( p_this, p_sys->vcddev );
228     free( p_sys );
229 }
230
231 /*****************************************************************************
232  * Control:
233  *****************************************************************************/
234 static int Control( access_t *p_access, int i_query, va_list args )
235 {
236     access_sys_t *p_sys = p_access->p_sys;
237     vlc_bool_t   *pb_bool;
238     int          *pi_int;
239     int64_t      *pi_64;
240     input_title_t ***ppp_title;
241     int i;
242
243     switch( i_query )
244     {
245         /* */
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* );
251             *pb_bool = VLC_TRUE;
252             break;
253
254         /* */
255         case ACCESS_GET_MTU:
256             pi_int = (int*)va_arg( args, int * );
257             *pi_int = VCD_DATA_ONCE;
258             break;
259
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;
263             break;
264
265         /* */
266         case ACCESS_SET_PAUSE_STATE:
267             break;
268
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* );
272
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++ )
277             {
278                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
279             }
280             break;
281
282         case ACCESS_SET_TITLE:
283             i = (int)va_arg( args, int );
284             if( i != p_access->info.i_title )
285             {
286                 /* Update info */
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;
293
294                 /* Next sector to read */
295                 p_sys->i_sector = p_sys->p_sectors[1+i];
296             }
297             break;
298
299         case ACCESS_SET_SEEKPOINT:
300         {
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 )
304             {
305                 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
306                 p_access->info.i_seekpoint = i;
307
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;
310
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;
313             }
314             return VLC_SUCCESS;
315         }
316
317         case ACCESS_SET_PRIVATE_ID_STATE:
318         case ACCESS_GET_CONTENT_TYPE:
319             return VLC_EGENERIC;
320
321         default:
322             msg_Warn( p_access, "unimplemented query in control" );
323             return VLC_EGENERIC;
324
325     }
326     return VLC_SUCCESS;
327 }
328
329 /*****************************************************************************
330  * Block:
331  *****************************************************************************/
332 static block_t *Block( access_t *p_access )
333 {
334     access_sys_t *p_sys = p_access->p_sys;
335     int i_blocks = VCD_BLOCKS_ONCE;
336     block_t *p_block;
337     int i_read;
338
339     /* Check end of file */
340     if( p_access->info.b_eof ) return NULL;
341
342     /* Check end of title */
343     while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 2] )
344     {
345         if( p_access->info.i_title + 2 >= p_sys->i_titles )
346         {
347             p_access->info.b_eof = VLC_TRUE;
348             return NULL;
349         }
350
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;
358     }
359
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] )
363     {
364         i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] -
365                    p_sys->i_sector;
366     }
367
368     /* Do the actual reading */
369     if( !( p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE ) ) )
370     {
371         msg_Err( p_access, "cannot get a new block of size: %i",
372                  i_blocks * VCD_DATA_SIZE );
373         return NULL;
374     }
375
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 )
378     {
379         msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
380         block_Release( p_block );
381
382         /* Try to skip one sector (in case of bad sectors) */
383         p_sys->i_sector++;
384         p_access->info.i_pos += VCD_DATA_SIZE;
385         return NULL;
386     }
387
388     /* Update seekpoints */
389     for( i_read = 0; i_read < i_blocks; i_read++ )
390     {
391         input_title_t *t = p_sys->title[p_access->info.i_title];
392
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 )
397         {
398             msg_Dbg( p_access, "seekpoint change" );
399             p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
400             p_access->info.i_seekpoint++;
401         }
402     }
403
404     /* Update a few values */
405     p_sys->i_sector += i_blocks;
406     p_access->info.i_pos += p_block->i_buffer;
407
408     return p_block;
409 }
410
411 /*****************************************************************************
412  * Seek:
413  *****************************************************************************/
414 static int Seek( access_t *p_access, int64_t i_pos )
415 {
416     access_sys_t *p_sys = p_access->p_sys;
417     input_title_t *t = p_sys->title[p_access->info.i_title];
418     int i_seekpoint;
419
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];
424
425     /* Update current seekpoint */
426     for( i_seekpoint = 0; i_seekpoint < t->i_seekpoint; i_seekpoint++ )
427     {
428         if( i_seekpoint + 1 >= t->i_seekpoint ) break;
429         if( i_pos < t->seekpoint[i_seekpoint + 1]->i_byte_offset ) break;
430     }
431
432     if( i_seekpoint != p_access->info.i_seekpoint )
433     {
434         msg_Dbg( p_access, "seekpoint change" );
435         p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
436         p_access->info.i_seekpoint = i_seekpoint;
437     }
438
439     /* Reset eof */
440     p_access->info.b_eof = VLC_FALSE;
441
442     return VLC_SUCCESS;
443 }
444
445 /*****************************************************************************
446  * EntryPoints: Reads the information about the entry points on the disc.
447  *****************************************************************************/
448 static int EntryPoints( access_t *p_access )
449 {
450     access_sys_t *p_sys = p_access->p_sys;
451     uint8_t      sector[VCD_DATA_SIZE];
452
453     entries_sect_t entries;
454     int i_nb, i;
455
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 )
459     {
460         msg_Err( p_access, "could not read entry points sector" );
461         return VLC_EGENERIC;
462     }
463     memcpy( &entries, sector, CD_SECTOR_SIZE );
464
465     i_nb = GetWBE( &entries.i_entries_nb );
466     if( i_nb > 500 )
467     {
468         msg_Err( p_access, "invalid entry points number" );
469         return VLC_EGENERIC;
470     }
471
472     if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) ) &&
473         strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ) )
474     {
475         msg_Err( p_access, "unrecognized entry points format" );
476         return VLC_EGENERIC;
477     }
478
479     for( i = 0; i < i_nb; i++ )
480     {
481         const int i_title = BCD_TO_BIN(entries.entry[i].i_track) - 2;
482         const int i_sector =
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  ) ));
486         seekpoint_t *s;
487
488         if( i_title < 0 ) continue;   /* Should not occur */
489         if( i_title >= p_sys->i_titles ) continue;
490
491         msg_Dbg( p_access, "Entry[%d] title=%d sector=%d\n",
492                  i, i_title, i_sector );
493
494         s = vlc_seekpoint_New();
495         s->i_byte_offset = (i_sector - p_sys->p_sectors[i_title+1]) *
496             VCD_DATA_SIZE;
497
498         TAB_APPEND( p_sys->title[i_title]->i_seekpoint,
499                     p_sys->title[i_title]->seekpoint, s );
500     }
501
502     return VLC_SUCCESS;
503 }
504