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