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