]> git.sesse.net Git - vlc/blob - modules/access/vcd/vcd.c
88226933e50675ceb12609fd24d48b78d1770f63
[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", 10 );
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         default:
310             msg_Err( p_access, "unimplemented query in control" );
311             return VLC_EGENERIC;
312
313     }
314     return VLC_SUCCESS;
315 }
316
317 /*****************************************************************************
318  * Block:
319  *****************************************************************************/
320 static block_t *Block( access_t *p_access )
321 {
322     access_sys_t *p_sys = p_access->p_sys;
323     int i_skip = p_access->info.i_pos % VCD_DATA_SIZE;
324     int i_blocks = VCD_BLOCKS_ONCE;
325     block_t *p_block;
326     int i_read;
327
328     /* Check end of file */
329     if( p_access->info.b_eof ) return NULL;
330
331     /* Check end of title */
332     while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 2] )
333     {
334         if( p_access->info.i_title + 2 >= p_sys->i_titles )
335         {
336             p_access->info.b_eof = VLC_TRUE;
337             return NULL;
338         }
339
340         p_access->info.i_update |=
341             INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT | INPUT_UPDATE_SIZE;
342         p_access->info.i_title++;
343         p_access->info.i_seekpoint = 0;
344         p_access->info.i_size =
345             p_sys->title[p_access->info.i_title]->i_size;
346         p_access->info.i_pos = 0;
347     }
348
349     /* Don't read after the end of a title */
350     if( p_sys->i_sector + i_blocks >=
351         p_sys->p_sectors[p_access->info.i_title + 2] )
352     {
353         i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] -
354                    p_sys->i_sector;
355     }
356
357     /* Do the actual reading */
358     if( !( p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE ) ) )
359     {
360         msg_Err( p_access, "cannot get a new block of size: %i",
361                  i_blocks * VCD_DATA_SIZE );
362         return NULL;
363     }
364
365     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
366             p_sys->i_sector, p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
367     {
368         msg_Err( p_access, "cannot read a sector" );
369         block_Release( p_block );
370         return NULL;
371     }
372
373     for( i_read = 0; i_read < i_blocks; i_read++ )
374     {
375         input_title_t *t = p_sys->title[p_access->info.i_title];
376
377         /* A good sector read */
378         p_sys->i_sector++;
379
380         if( t->i_seekpoint > 0 &&
381             p_access->info.i_seekpoint + 1 < t->i_seekpoint &&
382             p_access->info.i_pos - i_skip + i_read * VCD_DATA_SIZE >=
383             t->seekpoint[p_access->info.i_seekpoint+1]->i_byte_offset )
384         {
385             msg_Dbg( p_access, "seekpoint change" );
386             p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
387             p_access->info.i_seekpoint++;
388         }
389     }
390
391     /* Update a few values */
392     p_block->i_buffer = i_read * VCD_DATA_SIZE;
393     p_block->i_buffer -= i_skip;
394     p_block->p_buffer += i_skip;
395     p_access->info.i_pos += p_block->i_buffer;
396
397     return p_block;
398 }
399
400 /*****************************************************************************
401  * Seek:
402  *****************************************************************************/
403 static int Seek( access_t *p_access, int64_t i_pos )
404 {
405     access_sys_t *p_sys = p_access->p_sys;
406     input_title_t *t = p_sys->title[p_access->info.i_title];
407     int i_seekpoint;
408
409     /* Next sector to read */
410     p_sys->i_sector = i_pos / VCD_DATA_SIZE +
411         p_sys->p_sectors[p_access->info.i_title + 1];
412
413     /* Update current seekpoint */
414     for( i_seekpoint = 0; i_seekpoint < t->i_seekpoint; i_seekpoint++ )
415     {
416         if( i_seekpoint + 1 >= t->i_seekpoint ) break;
417         if( i_pos < t->seekpoint[i_seekpoint + 1]->i_byte_offset ) break;
418     }
419
420     if( i_seekpoint != p_access->info.i_seekpoint )
421     {
422         msg_Dbg( p_access, "seekpoint change" );
423         p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
424         p_access->info.i_seekpoint = i_seekpoint;
425     }
426
427     return VLC_SUCCESS;
428 }
429
430 /*****************************************************************************
431  * EntryPoints: Reads the information about the entry points on the disc.
432  *****************************************************************************/
433 static int EntryPoints( access_t *p_access )
434 {
435     access_sys_t *p_sys = p_access->p_sys;
436     uint8_t      sector[VCD_DATA_SIZE];
437
438     entries_sect_t entries;
439     int i_nb, i;
440
441     /* Read the entry point sector */
442     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
443         VCD_ENTRIES_SECTOR, sector, 1, VCD_TYPE ) < 0 )
444     {
445         msg_Err( p_access, "could not read entry points sector" );
446         return VLC_EGENERIC;
447     }
448     memcpy( &entries, sector, CD_SECTOR_SIZE );
449
450     i_nb = GetWBE( &entries.i_entries_nb );
451     if( i_nb > 500 )
452     {
453         msg_Err( p_access, "invalid entry points number" );
454         return VLC_EGENERIC;
455     }
456
457     if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) ) &&
458         strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ) )
459     {
460         msg_Err( p_access, "unrecognized entry points format" );
461         return VLC_EGENERIC;
462     }
463
464     for( i = 0; i < i_nb; i++ )
465     {
466         const int i_title = BCD_TO_BIN(entries.entry[i].i_track) - 2;
467         const int i_sector =
468             (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
469                           BCD_TO_BIN( entries.entry[i].msf.second ),
470                           BCD_TO_BIN( entries.entry[i].msf.frame  ) ));
471         seekpoint_t *s;
472
473         if( i_title < 0 ) continue;   /* Should not occur */
474         if( i_title >= p_sys->i_titles ) continue;
475
476         msg_Dbg( p_access, "Entry[%d] title=%d sector=%d\n",
477                  i, i_title, i_sector );
478
479         s = vlc_seekpoint_New();
480         s->i_byte_offset = (i_sector - p_sys->p_sectors[i_title+1]) *
481             VCD_DATA_SIZE;
482
483         TAB_APPEND( p_sys->title[i_title]->i_seekpoint,
484                     p_sys->title[i_title]->seekpoint, s );
485     }
486
487     return VLC_SUCCESS;
488 }