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