]> git.sesse.net Git - vlc/blob - modules/access/vcd/vcd.c
* vcd/vcd.c: ported to access2.
[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
82 static int      EntryPoints( access_t * );
83
84 /*****************************************************************************
85  * VCDOpen: open vcd
86  *****************************************************************************/
87 static int Open( vlc_object_t *p_this )
88 {
89     access_t     *p_access = (access_t *)p_this;
90     access_sys_t *p_sys;
91     char *psz_dup = strdup( p_access->psz_path );
92     char *psz;
93     int i_title = 0;
94     int i_chapter = 0;
95     int i;
96     vcddev_t *vcddev;
97
98     /* Command line: vcd://[dev_path][@title[,chapter]] */
99     if( ( psz = strchr( psz_dup, '@' ) ) )
100     {
101         *psz++ = '\0';
102
103         i_title = strtol( psz, &psz, 0 );
104         if( *psz )
105             i_chapter = strtol( psz+1, &psz, 0 );
106     }
107
108     if( *psz_dup == '\0' )
109     {
110         free( psz_dup );
111
112         /* Only when selected */
113         if( *p_access->psz_access == '\0' )
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         int64_t i_off = p_sys->title[i_title]->seekpoint[i_chapter]->i_byte_offset;
192         p_sys->i_sector += i_off / VCD_DATA_SIZE;
193     }
194     p_access->info.i_title = i_title;
195     p_access->info.i_seekpoint = i_chapter;
196     p_access->info.i_size = p_sys->title[i_title]->i_size;
197     p_access->info.i_pos = ( p_sys->i_sector - p_sys->p_sectors[1+i_title] ) * VCD_DATA_SIZE;
198
199     p_access->psz_demux = strdup( "ps2" );
200
201     return VLC_SUCCESS;
202
203 error:
204     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
205     free( p_sys );
206     return VLC_EGENERIC;
207 }
208
209 /*****************************************************************************
210  * Close: closes vcd
211  *****************************************************************************/
212 static void Close( vlc_object_t *p_this )
213 {
214     access_t     *p_access = (access_t *)p_this;
215     access_sys_t *p_sys = p_access->p_sys;
216
217     ioctl_Close( p_this, p_sys->vcddev );
218     free( p_sys );
219 }
220
221 /*****************************************************************************
222  * Control:
223  *****************************************************************************/
224 static int Control( access_t *p_access, int i_query, va_list args )
225 {
226     access_sys_t *p_sys = p_access->p_sys;
227     vlc_bool_t   *pb_bool;
228     int          *pi_int;
229     int64_t      *pi_64;
230     input_title_t ***ppp_title;
231     int i;
232
233     switch( i_query )
234     {
235         /* */
236         case ACCESS_CAN_SEEK:
237         case ACCESS_CAN_FASTSEEK:
238         case ACCESS_CAN_PAUSE:
239         case ACCESS_CAN_CONTROL_PACE:
240             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
241             *pb_bool = VLC_TRUE;
242             break;
243
244         /* */
245         case ACCESS_GET_MTU:
246             pi_int = (int*)va_arg( args, int * );
247             *pi_int = VCD_DATA_ONCE;
248             break;
249
250         case ACCESS_GET_PTS_DELAY:
251             pi_64 = (int64_t*)va_arg( args, int64_t * );
252             *pi_64 = (int64_t)var_GetInteger( p_access, "vcd-caching" ) * I64C(1000);
253             break;
254
255         /* */
256         case ACCESS_SET_PAUSE_STATE:
257             break;
258
259         case ACCESS_GET_TITLE_INFO:
260             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
261             pi_int    = (int*)va_arg( args, int* );
262
263             /* Duplicate title infos */
264             *pi_int = p_sys->i_titles;
265             *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_titles );
266             for( i = 0; i < p_sys->i_titles; i++ )
267             {
268                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
269             }
270             break;
271
272         case ACCESS_SET_TITLE:
273             i = (int)va_arg( args, int );
274             if( i != p_access->info.i_title )
275             {
276                 /* Update info */
277                 p_access->info.i_update |= INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
278                 p_access->info.i_title = i;
279                 p_access->info.i_seekpoint = 0;
280                 p_access->info.i_size = p_sys->title[i]->i_size;
281                 p_access->info.i_pos  = 0;
282
283                 /* Next sector to read */
284                 p_sys->i_sector = p_sys->p_sectors[1+i];
285             }
286             break;
287
288         case ACCESS_SET_SEEKPOINT:
289         {
290             input_title_t *t = p_sys->title[p_access->info.i_title];
291             i = (int)va_arg( args, int );
292             if( t->i_seekpoint > 0 )
293             {
294                 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
295                 p_access->info.i_seekpoint = i;
296
297                 p_sys->i_sector = p_sys->p_sectors[1+p_access->info.i_title] +
298                                   t->seekpoint[i]->i_byte_offset / VCD_DATA_SIZE;
299
300                 p_access->info.i_pos = (int64_t)(p_sys->i_sector - p_sys->p_sectors[1+p_access->info.i_title] ) *
301                                        (int64_t)VCD_DATA_SIZE;
302             }
303             return VLC_SUCCESS;
304         }
305
306         default:
307             msg_Err( p_access, "unimplemented query in control" );
308             return VLC_EGENERIC;
309
310     }
311     return VLC_SUCCESS;
312 }
313
314 /*****************************************************************************
315  * Block:
316  *****************************************************************************/
317 static block_t *Block( access_t *p_access )
318 {
319     access_sys_t *p_sys = p_access->p_sys;
320     int i_skip = p_access->info.i_pos % VCD_DATA_SIZE;
321     block_t      *p_block;
322     int          i_blocks;
323     int          i_read;
324     int          i;
325
326     if( p_access->info.b_eof )
327         return NULL;
328
329     /* Read raw data */
330     i_blocks = VCD_BLOCKS_ONCE;
331
332     /* Don't read after a title */
333     if( p_sys->i_sector + i_blocks >= p_sys->p_sectors[p_access->info.i_title + 2] )
334     {
335         i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] - p_sys->i_sector;
336         if( i_blocks <= 0 ) i_blocks = 1;   /* Should never occur */
337     }
338
339     p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE );
340
341     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev, p_sys->i_sector,
342                            p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
343     {
344         msg_Err( p_access, "cannot read a sector" );
345         block_Release( p_block );
346
347         p_block = NULL;
348         i_blocks = 1;   /* Next sector */
349     }
350
351
352     i_read = 0;
353     for( i = 0; i < i_blocks; i++ )
354     {
355         input_title_t *t = p_sys->title[p_access->info.i_title];
356
357         /* A good sector read */
358         i_read++;
359         p_sys->i_sector++;
360
361         /* Check end of title */
362         if( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 2] )
363         {
364             if( p_access->info.i_title + 2 >= p_sys->i_titles )
365             {
366                 p_access->info.b_eof = VLC_TRUE;
367                 break;
368             }
369             p_access->info.i_update |= INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
370             p_access->info.i_title++;
371             p_access->info.i_seekpoint = 0;
372             p_access->info.i_size = p_sys->title[p_access->info.i_title]->i_size;
373             p_access->info.i_pos = 0;
374         }
375         else if( t->i_seekpoint > 0 &&
376                  p_access->info.i_seekpoint + 1 < t->i_seekpoint &&
377                  p_access->info.i_pos - i_skip + i_read * VCD_DATA_SIZE >= t->seekpoint[p_access->info.i_seekpoint+1]->i_byte_offset )
378         {
379             fprintf( stderr, "seekpoint change\n" );
380             p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
381             p_access->info.i_seekpoint++;
382         }
383         /* TODO */
384     }
385
386     if( i_read <= 0 )
387     {
388         block_Release( p_block );
389         return NULL;
390     }
391
392     if( p_block )
393     {
394
395         /* Really read data */
396         p_block->i_buffer = i_read * VCD_DATA_SIZE;
397         /* */
398         p_block->i_buffer -= i_skip;
399         p_block->p_buffer += i_skip;
400
401         p_access->info.i_pos += p_block->i_buffer;
402     }
403
404     return p_block;
405 }
406
407 /*****************************************************************************
408  * Seek:
409  *****************************************************************************/
410 static int Seek( access_t *p_access, int64_t i_pos )
411 {
412     return VLC_EGENERIC;
413 }
414
415 /*****************************************************************************
416  * EntryPoints:
417  *****************************************************************************/
418 static int EntryPoints( access_t *p_access )
419 {
420     access_sys_t *p_sys = p_access->p_sys;
421     uint8_t      sector[VCD_DATA_SIZE];
422
423     entries_sect_t entries;
424     int i_nb;
425     int i;
426
427
428     /* Read the entry point sector */
429     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
430         VCD_ENTRIES_SECTOR, sector, 1, VCD_TYPE ) < 0 )
431     {
432         msg_Err( p_access, "could not read entry points sector" );
433         return VLC_EGENERIC;
434     }
435     memcpy( &entries, sector, CD_SECTOR_SIZE );
436
437     i_nb = GetWBE( &entries.i_entries_nb );
438     if( i_nb > 500 )
439     {
440         msg_Err( p_access, "invalid entry points number" );
441         return VLC_EGENERIC;
442     }
443
444     if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) ) &&
445         strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ) )
446     {
447         msg_Err( p_access, "unrecognized entry points format" );
448         return VLC_EGENERIC;
449     }
450
451     for( i = 0; i < i_nb; i++ )
452     {
453         const int i_title = BCD_TO_BIN(entries.entry[i].i_track) - 2;
454         const int i_sector =
455             (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
456                           BCD_TO_BIN( entries.entry[i].msf.second ),
457                           BCD_TO_BIN( entries.entry[i].msf.frame  ) ));
458         seekpoint_t *s;
459
460         if( i_title < 0 )
461             continue;   /* Should not occur */
462         if( i_title >= p_sys->i_titles )
463             continue;
464
465         fprintf( stderr, "Entry[%d] title=%d sector=%d\n",
466                  i, i_title, i_sector );
467
468         s = vlc_seekpoint_New();
469         s->i_byte_offset = (i_sector - p_sys->p_sectors[i_title+1]) * VCD_DATA_SIZE;
470
471         TAB_APPEND( p_sys->title[i_title]->i_seekpoint, p_sys->title[i_title]->seekpoint, s );
472     }
473
474 #if 0
475 #define i_track BCD_TO_BIN(entries.entry[i].i_track)
476     /* Reset the i_part_nb for each track */
477     for( i = 0 ; i < i_nb ; i++ )
478     {
479         if( i_track <= p_input->stream.i_area_nb )
480         {
481             p_input->stream.pp_areas[i_track-1]->i_part_nb = 0;
482         }
483     }
484
485     for( i = 0 ; i < i_nb ; i++ )
486     {
487         if( i_track <= p_input->stream.i_area_nb )
488         {
489             p_sys->p_entries[i_entry_index] =
490                 (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
491                               BCD_TO_BIN( entries.entry[i].msf.second ),
492                               BCD_TO_BIN( entries.entry[i].msf.frame  ) ));
493             p_input->stream.pp_areas[i_track-1]->i_part_nb ++;
494             /* if this entry belongs to a new track */
495             if( i_track != i_previous_track )
496             {
497                 /* i_plugin_data is used to store the first entry of the area*/
498                 p_input->stream.pp_areas[i_track-1]->i_plugin_data =
499                                                             i_entry_index;
500                 i_previous_track = i_track;
501             }
502             msg_Dbg( p_input, "entry point %i begins at LBA: %i",
503                      i_entry_index, p_sys->p_entries[i_entry_index] );
504
505             i_entry_index ++;
506             p_sys->i_entries_nb ++;
507         }
508         else
509             msg_Warn( p_input, "wrong track number found in entry points" );
510     }
511 #undef i_track
512     return 0;
513 #endif
514     return VLC_EGENERIC;
515 }
516
517 #if 0
518 /*****************************************************************************
519  * VCDRead: reads from the VCD into PES packets.
520  *****************************************************************************
521  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
522  * bytes.
523  *****************************************************************************/
524 static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
525                      size_t i_len )
526 {
527     access_sys_t *p_sys;
528     int                     i_blocks;
529     int                     i_index;
530     int                     i_read;
531     byte_t                  p_last_sector[ VCD_DATA_SIZE ];
532
533     p_sys = p_input->p_access_data;
534
535     i_read = 0;
536
537     /* Compute the number of blocks we have to read */
538
539     i_blocks = i_len / VCD_DATA_SIZE;
540
541     for ( i_index = 0 ; i_index < i_blocks ; i_index++ )
542     {
543         if ( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev,
544              p_sys->i_sector, p_buffer + i_index * VCD_DATA_SIZE, 1,
545              VCD_TYPE ) < 0 )
546         {
547             msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
548             return -1;
549         }
550
551         p_sys->i_sector ++;
552         if ( p_sys->i_sector == p_sys->p_sectors[p_sys->i_track + 1] )
553         {
554             input_area_t *p_area;
555
556             if ( p_sys->i_track >= p_sys->i_nb_tracks - 1 )
557                 return 0; /* EOF */
558
559             vlc_mutex_lock( &p_input->stream.stream_lock );
560             p_area = p_input->stream.pp_areas[
561                     p_input->stream.p_selected_area->i_id + 1 ];
562
563             msg_Dbg( p_input, "new title" );
564
565             p_area->i_part = 1;
566             VCDSetArea( p_input, p_area );
567             vlc_mutex_unlock( &p_input->stream.stream_lock );
568         }
569
570         /* Update chapter */
571         else if( p_sys->b_valid_ep &&
572                 /* FIXME kludge so that read does not update chapter
573                  * when a manual chapter change was requested and not
574                  * yet accomplished */
575                 !p_input->stream.p_new_area )
576         {
577             int i_entry;
578
579             vlc_mutex_lock( &p_input->stream.stream_lock );
580             i_entry = p_input->stream.p_selected_area->i_plugin_data
581                 /* 1st entry point of the track (area)*/
582                         + p_input->stream.p_selected_area->i_part - 1;
583             if( i_entry + 1 < p_sys->i_entries_nb &&
584                     p_sys->i_sector >= p_sys->p_entries[i_entry + 1] )
585             {
586                 vlc_value_t val;
587
588                 msg_Dbg( p_input, "new chapter" );
589                 p_input->stream.p_selected_area->i_part ++;
590
591                 /* Update the navigation variables without triggering
592                  * a callback */
593                 val.i_int = p_input->stream.p_selected_area->i_part;
594                 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
595             }
596             vlc_mutex_unlock( &p_input->stream.stream_lock );
597         }
598
599         i_read += VCD_DATA_SIZE;
600     }
601
602     if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
603     {
604         if ( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev,
605              p_sys->i_sector, p_last_sector, 1, VCD_TYPE ) < 0 )
606         {
607             msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
608             return -1;
609         }
610
611         p_input->p_vlc->pf_memcpy( p_buffer + i_blocks * VCD_DATA_SIZE,
612                                    p_last_sector, i_len % VCD_DATA_SIZE );
613         i_read += i_len % VCD_DATA_SIZE;
614     }
615
616     return i_read;
617 }
618
619 /*****************************************************************************
620  * VCDSetProgram: Does nothing since a VCD is mono_program
621  *****************************************************************************/
622 static int VCDSetProgram( input_thread_t * p_input,
623                           pgrm_descriptor_t * p_program)
624 {
625     return 0;
626 }
627
628 /*****************************************************************************
629  * VCDSetArea: initialize input data for title x, chapter y.
630  * It should be called for each user navigation request.
631  ****************************************************************************/
632 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
633 {
634     access_sys_t *p_sys = p_input->p_access_data;
635     vlc_value_t val;
636
637     /* we can't use the interface slider until initilization is complete */
638     p_input->stream.b_seekable = 0;
639
640     if( p_area != p_input->stream.p_selected_area )
641     {
642         unsigned int i;
643
644         /* Reset the Chapter position of the current title */
645         p_input->stream.p_selected_area->i_part = 1;
646         p_input->stream.p_selected_area->i_tell = 0;
647
648         /* Change the default area */
649         p_input->stream.p_selected_area = p_area;
650
651         /* Change the current track */
652         /* The first track is not a valid one  */
653         p_sys->i_track = p_area->i_id;
654         p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
655
656         /* Update the navigation variables without triggering a callback */
657         val.i_int = p_area->i_id;
658         var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
659         var_Change( p_input, "chapter", VLC_VAR_CLEARCHOICES, NULL, NULL );
660         for( i = 1; i <= p_area->i_part_nb; i++ )
661         {
662             val.i_int = i;
663             var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val, NULL );
664         }
665     }
666
667     if( p_sys->b_valid_ep )
668     {
669         int i_entry = p_area->i_plugin_data /* 1st entry point of
670                                                the track (area)*/
671                             + p_area->i_part - 1;
672         p_sys->i_sector = p_sys->p_entries[i_entry];
673     }
674     else
675         p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
676
677     p_input->stream.p_selected_area->i_tell =
678         (off_t)p_sys->i_sector * (off_t)VCD_DATA_SIZE
679          - p_input->stream.p_selected_area->i_start;
680
681     /* warn interface that something has changed */
682     p_input->stream.b_seekable = 1;
683     p_input->stream.b_changed = 1;
684
685     /* Update the navigation variables without triggering a callback */
686     val.i_int = p_area->i_part;
687     var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
688
689     return 0;
690 }
691
692 /****************************************************************************
693  * VCDSeek
694  ****************************************************************************/
695 static void VCDSeek( input_thread_t * p_input, off_t i_off )
696 {
697     access_sys_t * p_sys = p_input->p_access_data;
698     unsigned int i_index;
699
700     p_sys->i_sector = p_sys->p_sectors[p_sys->i_track]
701                        + i_off / (off_t)VCD_DATA_SIZE;
702
703     vlc_mutex_lock( &p_input->stream.stream_lock );
704 #define p_area p_input->stream.p_selected_area
705     /* Find chapter */
706     if( p_sys->b_valid_ep )
707     {
708         for( i_index = 2 ; i_index <= p_area->i_part_nb; i_index ++ )
709         {
710             if( p_sys->i_sector < p_sys->p_entries[p_area->i_plugin_data
711                 + i_index - 1] )
712             {
713                 vlc_value_t val;
714
715                 p_area->i_part = i_index - 1;
716
717                 /* Update the navigation variables without triggering
718                  * a callback */
719                 val.i_int = p_area->i_part;
720                 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
721                 break;
722             }
723         }
724     }
725 #undef p_area
726
727     p_input->stream.p_selected_area->i_tell =
728         (off_t)p_sys->i_sector * (off_t)VCD_DATA_SIZE
729          - p_input->stream.p_selected_area->i_start;
730     vlc_mutex_unlock( &p_input->stream.stream_lock );
731 }
732
733 /*****************************************************************************
734  * VCDEntryPoints: Reads the information about the entry points on the disc.
735  *****************************************************************************/
736 static int VCDEntryPoints( input_thread_t * p_input )
737 {
738     access_sys_t * p_sys = p_input->p_access_data;
739     byte_t *                          p_sector;
740     entries_sect_t                    entries;
741     uint16_t                          i_nb;
742     int                               i, i_entry_index = 0;
743     int                               i_previous_track = -1;
744
745     p_sector = malloc( VCD_DATA_SIZE * sizeof( byte_t ) );
746     if( p_sector == NULL )
747     {
748         msg_Err( p_input, "not enough memory for entry points treatment" );
749         return -1;
750     }
751
752     if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev,
753         VCD_ENTRIES_SECTOR, p_sector, 1, VCD_TYPE ) < 0 )
754     {
755         msg_Err( p_input, "could not read entry points sector" );
756         free( p_sector );
757         return( -1 );
758     }
759
760     memcpy( &entries, p_sector, CD_SECTOR_SIZE );
761     free( p_sector );
762
763     if( (i_nb = U16_AT( &entries.i_entries_nb )) > 500 )
764     {
765         msg_Err( p_input, "invalid entry points number" );
766         return( -1 );
767     }
768
769     p_sys->p_entries = malloc( sizeof( int ) * i_nb );
770     if( p_sys->p_entries == NULL )
771     {
772         msg_Err( p_input, "not enough memory for entry points treatment" );
773         return -1;
774     }
775
776     if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) )
777      && strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ))
778     {
779         msg_Err( p_input, "unrecognized entry points format" );
780         free( p_sys->p_entries );
781         return -1;
782     }
783
784     p_sys->i_entries_nb = 0;
785
786 #define i_track BCD_TO_BIN(entries.entry[i].i_track)
787     /* Reset the i_part_nb for each track */
788     for( i = 0 ; i < i_nb ; i++ )
789     {
790         if( i_track <= p_input->stream.i_area_nb )
791         {
792             p_input->stream.pp_areas[i_track-1]->i_part_nb = 0;
793         }
794     }
795
796     for( i = 0 ; i < i_nb ; i++ )
797     {
798         if( i_track <= p_input->stream.i_area_nb )
799         {
800             p_sys->p_entries[i_entry_index] =
801                 (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
802                               BCD_TO_BIN( entries.entry[i].msf.second ),
803                               BCD_TO_BIN( entries.entry[i].msf.frame  ) ));
804             p_input->stream.pp_areas[i_track-1]->i_part_nb ++;
805             /* if this entry belongs to a new track */
806             if( i_track != i_previous_track )
807             {
808                 /* i_plugin_data is used to store the first entry of the area*/
809                 p_input->stream.pp_areas[i_track-1]->i_plugin_data =
810                                                             i_entry_index;
811                 i_previous_track = i_track;
812             }
813             msg_Dbg( p_input, "entry point %i begins at LBA: %i",
814                      i_entry_index, p_sys->p_entries[i_entry_index] );
815
816             i_entry_index ++;
817             p_sys->i_entries_nb ++;
818         }
819         else
820             msg_Warn( p_input, "wrong track number found in entry points" );
821     }
822 #undef i_track
823     return 0;
824 }
825 #endif