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