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