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