]> git.sesse.net Git - vlc/blob - modules/access/dvdread.c
* Some more const warning fixes
[vlc] / modules / access / dvdread.c
1 /*****************************************************************************
2  * dvdread.c : DvdRead input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Stéphane Borel <stef@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>                                              /* strdup() */
31
32 #include <vlc/vlc.h>
33 #include <vlc_input.h>
34 #include <vlc_access.h>
35
36 #include <vlc_interface.h>
37
38 #include "iso_lang.h"
39
40 #include "../demux/ps.h"
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #endif
45
46 #include <fcntl.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <string.h>
50
51 #include <dvdread/dvd_reader.h>
52 #include <dvdread/ifo_types.h>
53 #include <dvdread/ifo_read.h>
54 #include <dvdread/nav_read.h>
55 #include <dvdread/nav_print.h>
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60 #define ANGLE_TEXT N_("DVD angle")
61 #define ANGLE_LONGTEXT N_( \
62     "Default DVD angle." )
63
64 #define CACHING_TEXT N_("Caching value in ms")
65 #define CACHING_LONGTEXT N_( \
66     "Caching value for DVDs. " \
67     "This value should be set in milliseconds." )
68
69 #define CSSMETHOD_TEXT N_("Method used by libdvdcss for decryption")
70 #define CSSMETHOD_LONGTEXT N_( \
71     "Set the method used by libdvdcss for key decryption.\n" \
72     "title: decrypted title key is guessed from the encrypted sectors of " \
73            "the stream. Thus it should work with a file as well as the " \
74            "DVD device. But it sometimes takes much time to decrypt a title " \
75            "key and may even fail. With this method, the key is only checked "\
76            "at the beginning of each title, so it won't work if the key " \
77            "changes in the middle of a title.\n" \
78     "disc: the disc key is first cracked, then all title keys can be " \
79            "decrypted instantly, which allows us to check them often.\n" \
80     "key: the same as \"disc\" if you don't have a file with player keys " \
81            "at compilation time. If you do, the decryption of the disc key " \
82            "will be faster with this method. It is the one that was used by " \
83            "libcss.\n" \
84     "The default method is: key.")
85
86 static const char *psz_css_list[] = { "title", "disc", "key" };
87 static const char *psz_css_list_text[] = { N_("title"), N_("Disc"), N_("Key") };
88
89 static int  Open ( vlc_object_t * );
90 static void Close( vlc_object_t * );
91
92 vlc_module_begin();
93     set_shortname( _("DVD without menus") );
94     set_description( _("DVDRead Input (DVD without menu support)") );
95     set_category( CAT_INPUT );
96     set_subcategory( SUBCAT_INPUT_ACCESS );
97     add_integer( "dvdread-angle", 1, NULL, ANGLE_TEXT,
98         ANGLE_LONGTEXT, VLC_FALSE );
99     add_integer( "dvdread-caching", DEFAULT_PTS_DELAY / 1000, NULL,
100         CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
101     add_string( "dvdread-css-method", NULL, NULL, CSSMETHOD_TEXT,
102                 CSSMETHOD_LONGTEXT, VLC_TRUE );
103         change_string_list( psz_css_list, psz_css_list_text, 0 );
104     set_capability( "access_demux", 0 );
105     add_shortcut( "dvd" );
106     add_shortcut( "dvdread" );
107     add_shortcut( "dvdsimple" );
108     set_callbacks( Open, Close );
109 vlc_module_end();
110
111 /* how many blocks DVDRead will read in each loop */
112 #define DVD_BLOCK_READ_ONCE 4
113
114 /*****************************************************************************
115  * Local prototypes
116  *****************************************************************************/
117
118 struct demux_sys_t
119 {
120     /* DVDRead state */
121     dvd_reader_t *p_dvdread;
122     dvd_file_t   *p_title;
123
124     ifo_handle_t *p_vmg_file;
125     ifo_handle_t *p_vts_file;
126
127     int i_title;
128     int i_chapter, i_chapters;
129     int i_angle, i_angles;
130
131     tt_srpt_t    *p_tt_srpt;
132     pgc_t        *p_cur_pgc;
133     dsi_t        dsi_pack;
134     int          i_ttn;
135
136     int i_pack_len;
137     int i_cur_block;
138     int i_next_vobu;
139
140     int i_mux_rate;
141
142     /* Current title start/end blocks */
143     int i_title_start_block;
144     int i_title_end_block;
145     int i_title_blocks;
146     int i_title_offset;
147     mtime_t i_title_cur_time;
148
149     int i_title_start_cell;
150     int i_title_end_cell;
151     int i_cur_cell;
152     int i_next_cell;
153     mtime_t i_cell_cur_time;
154     mtime_t i_cell_duration;
155
156     /* Track */
157     ps_track_t    tk[PS_TK_COUNT];
158
159     int           i_titles;
160     input_title_t **titles;
161
162     /* Video */
163     int i_aspect;
164
165     /* SPU */
166     uint32_t clut[16];
167 };
168
169 static int Control   ( demux_t *, int, va_list );
170 static int Demux     ( demux_t * );
171 static int DemuxBlock( demux_t *, uint8_t *, int );
172
173 static void DemuxTitles( demux_t *, int * );
174 static void ESNew( demux_t *, int, int );
175
176 static int  DvdReadSetArea  ( demux_t *, int, int, int );
177 static void DvdReadSeek     ( demux_t *, int );
178 static void DvdReadHandleDSI( demux_t *, uint8_t * );
179 static void DvdReadFindCell ( demux_t * );
180
181 /*****************************************************************************
182  * Open:
183  *****************************************************************************/
184 static int Open( vlc_object_t *p_this )
185 {
186     demux_t      *p_demux = (demux_t*)p_this;
187     demux_sys_t  *p_sys;
188     char         *psz_name;
189     char         *psz_dvdcss_env;
190     dvd_reader_t *p_dvdread;
191     ifo_handle_t *p_vmg_file;
192     vlc_value_t  val;
193
194     if( !p_demux->psz_path || !*p_demux->psz_path )
195     {
196         /* Only when selected */
197         if( !p_this->b_force ) return VLC_EGENERIC;
198
199         psz_name = var_CreateGetString( p_this, "dvd" );
200         if( !psz_name )
201         {
202             psz_name = strdup("");
203         }
204     }
205     else
206         psz_name = strdup( p_demux->psz_path );
207
208 #ifdef WIN32
209     if( psz_name[0] && psz_name[1] == ':' &&
210         psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
211 #endif
212
213     /* Override environment variable DVDCSS_METHOD with config option
214      * (FIXME: this creates a small memory leak) */
215     psz_dvdcss_env = config_GetPsz( p_demux, "dvdread-css-method" );
216     if( psz_dvdcss_env && *psz_dvdcss_env )
217     {
218         char *psz_env;
219
220         psz_env = malloc( strlen("DVDCSS_METHOD=") +
221                           strlen( psz_dvdcss_env ) + 1 );
222         if( !psz_env )
223         {
224             free( psz_dvdcss_env );
225             return VLC_ENOMEM;
226         }
227
228         sprintf( psz_env, "%s%s", "DVDCSS_METHOD=", psz_dvdcss_env );
229
230         putenv( psz_env );
231     }
232     if( psz_dvdcss_env ) free( psz_dvdcss_env );
233
234     /* Open dvdread */
235     if( !(p_dvdread = DVDOpen( psz_name )) )
236     {
237         msg_Err( p_demux, "DVDRead cannot open source: %s", psz_name );
238         intf_UserFatal( p_demux, VLC_FALSE, _("Playback failure"), 
239                         _("DVDRead could not open disk \"%s\"."), psz_name );
240         free( psz_name );
241         return VLC_EGENERIC;
242     }
243     free( psz_name );
244
245     /* Ifo allocation & initialisation */
246     if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
247     {
248         msg_Warn( p_demux, "cannot open VMG info" );
249         return VLC_EGENERIC;
250     }
251     msg_Dbg( p_demux, "VMG opened" );
252
253     /* Fill p_demux field */
254     STANDARD_DEMUX_INIT; p_sys = p_demux->p_sys;
255
256     ps_track_init( p_sys->tk );
257     p_sys->i_aspect = -1;
258     p_sys->i_title_cur_time = (mtime_t) 0;
259     p_sys->i_cell_cur_time = (mtime_t) 0;
260     p_sys->i_cell_duration = (mtime_t) 0;
261
262     p_sys->p_dvdread = p_dvdread;
263     p_sys->p_vmg_file = p_vmg_file;
264     p_sys->p_title = NULL;
265     p_sys->p_vts_file = NULL;
266
267     p_sys->i_title = p_sys->i_chapter = -1;
268     p_sys->i_mux_rate = 0;
269
270     var_Create( p_demux, "dvdread-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
271     var_Get( p_demux, "dvdread-angle", &val );
272     p_sys->i_angle = val.i_int > 0 ? val.i_int : 1;
273
274     DemuxTitles( p_demux, &p_sys->i_angle );
275     if( DvdReadSetArea( p_demux, 0, 0, p_sys->i_angle ) != VLC_SUCCESS )
276     {
277         Close( p_this );
278         msg_Err( p_demux, "DvdReadSetArea(0,0,%i) failed (can't decrypt DVD?)",
279                  p_sys->i_angle );
280         return VLC_EGENERIC;
281     }
282
283     /* Update default_pts to a suitable value for dvdread access */
284     var_Create( p_demux, "dvdread-caching",
285                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
286
287     return VLC_SUCCESS;
288 }
289
290 /*****************************************************************************
291  * Close:
292  *****************************************************************************/
293 static void Close( vlc_object_t *p_this )
294 {
295     demux_t     *p_demux = (demux_t*)p_this;
296     demux_sys_t *p_sys = p_demux->p_sys;
297     int i;
298
299     for( i = 0; i < PS_TK_COUNT; i++ )
300     {
301         ps_track_t *tk = &p_sys->tk[i];
302         if( tk->b_seen )
303         {
304             es_format_Clean( &tk->fmt );
305             if( tk->es ) es_out_Del( p_demux->out, tk->es );
306         }
307     }
308
309     /* Close libdvdread */
310     if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
311     if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
312     if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
313     DVDClose( p_sys->p_dvdread );
314
315     free( p_sys );
316 }
317
318 static int64_t dvdtime_to_time( dvd_time_t *dtime, uint8_t still_time )
319 {
320 /* Macro to convert Binary Coded Decimal to Decimal */
321 #define BCD2D(__x__) (((__x__ & 0xf0) >> 4) * 10 + (__x__ & 0x0f))
322
323     double f_fps, f_ms;
324     int64_t i_micro_second = 0;
325
326     if (still_time == 0 || still_time == 0xFF)
327     {
328         i_micro_second += (int64_t)(BCD2D(dtime->hour)) * 60 * 60 * 1000000;
329         i_micro_second += (int64_t)(BCD2D(dtime->minute)) * 60 * 1000000;
330         i_micro_second += (int64_t)(BCD2D(dtime->second)) * 1000000;
331
332         switch((dtime->frame_u & 0xc0) >> 6) 
333         {
334         case 1:
335             f_fps = 25.0;
336             break;
337         case 3:
338             f_fps = 29.97;
339             break;
340         default:
341             f_fps = 2500.0;
342             break;
343         }
344         f_ms = BCD2D(dtime->frame_u&0x3f) * 1000.0 / f_fps;
345         i_micro_second += (int64_t)(f_ms * 1000.0);
346     }
347     else
348     {
349         i_micro_second = still_time;
350         i_micro_second = (int64_t)((double)i_micro_second * 1000000.0);
351     }
352
353     return i_micro_second;
354 }
355
356 /*****************************************************************************
357  * Control:
358  *****************************************************************************/
359 static int Control( demux_t *p_demux, int i_query, va_list args )
360 {
361     demux_sys_t *p_sys = p_demux->p_sys;
362     double f, *pf;
363     vlc_bool_t *pb;
364     int64_t *pi64;
365     input_title_t ***ppp_title;
366     int *pi_int;
367     int i;
368
369     switch( i_query )
370     {
371         case DEMUX_GET_POSITION:
372         {
373             pf = (double*) va_arg( args, double* );
374
375             if( p_sys->i_title_blocks > 0 )
376                 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
377             else
378                 *pf = 0.0;
379
380             return VLC_SUCCESS;
381         }
382         case DEMUX_SET_POSITION:
383         {
384             f = (double)va_arg( args, double );
385
386             DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
387
388             return VLC_SUCCESS;
389         }
390         case DEMUX_GET_TIME:
391             pi64 = (int64_t*)va_arg( args, int64_t * );
392             if( p_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles )
393             {
394                 *pi64 = (int64_t) dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 ) / 
395                         p_sys->i_title_blocks * p_sys->i_title_offset;
396                 return VLC_SUCCESS;
397             }
398             *pi64 = 0;
399             return VLC_EGENERIC;
400
401         case DEMUX_GET_LENGTH:
402             pi64 = (int64_t*)va_arg( args, int64_t * );
403             if( p_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles )
404             {
405                 *pi64 = (int64_t)dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 );
406                 return VLC_SUCCESS;
407             }
408             *pi64 = 0;
409             return VLC_EGENERIC;
410
411         /* Special for access_demux */
412         case DEMUX_CAN_PAUSE:
413         case DEMUX_CAN_CONTROL_PACE:
414             /* TODO */
415             pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
416             *pb = VLC_TRUE;
417             return VLC_SUCCESS;
418
419         case DEMUX_SET_PAUSE_STATE:
420             return VLC_SUCCESS;
421
422         case DEMUX_GET_TITLE_INFO:
423             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
424             pi_int    = (int*)va_arg( args, int* );
425             *((int*)va_arg( args, int* )) = 1; /* Title offset */
426             *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
427
428             /* Duplicate title infos */
429             *pi_int = p_sys->i_titles;
430             *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
431             for( i = 0; i < p_sys->i_titles; i++ )
432             {
433                 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
434             }
435             return VLC_SUCCESS;
436
437         case DEMUX_SET_TITLE:
438             i = (int)va_arg( args, int );
439             if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
440             {
441                 msg_Warn( p_demux, "cannot set title/chapter" );
442                 return VLC_EGENERIC;
443             }
444             p_demux->info.i_update |=
445                 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
446             p_demux->info.i_title = i;
447             p_demux->info.i_seekpoint = 0;
448             return VLC_SUCCESS;
449
450         case DEMUX_SET_SEEKPOINT:
451             i = (int)va_arg( args, int );
452             if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
453             {
454                 msg_Warn( p_demux, "cannot set title/chapter" );
455                 return VLC_EGENERIC;
456             }
457             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
458             p_demux->info.i_seekpoint = i;
459             return VLC_SUCCESS;
460
461         case DEMUX_GET_PTS_DELAY:
462             pi64 = (int64_t*)va_arg( args, int64_t * );
463             *pi64 = (int64_t)var_GetInteger( p_demux, "dvdread-caching" )*1000;
464             return VLC_SUCCESS;
465
466         /* TODO implement others */
467         default:
468             return VLC_EGENERIC;
469     }
470 }
471
472 /*****************************************************************************
473  * Demux:
474  *****************************************************************************/
475 static int Demux( demux_t *p_demux )
476 {
477     demux_sys_t *p_sys = p_demux->p_sys;
478
479     uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
480     int i_blocks_once, i_read;
481     int i;
482
483     /*
484      * Playback by cell in this pgc, starting at the cell for our chapter.
485      */
486
487     /*
488      * Check end of pack, and select the following one
489      */
490     if( !p_sys->i_pack_len )
491     {
492         /* Read NAV packet */
493         if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
494                            1, p_buffer ) != 1 )
495         {
496             msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
497             intf_UserWarn( p_demux, _("Playback failure"), 
498                             _("DVDRead could not read block %d."),
499                            p_sys->i_next_vobu );
500             return -1;
501         }
502
503         /* Basic check to be sure we don't have a empty title
504          * go to next title if so */
505         //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
506
507         /* Parse the contained dsi packet */
508         DvdReadHandleDSI( p_demux, p_buffer );
509
510         /* End of title */
511         if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
512         {
513             if( p_sys->i_title + 1 >= p_sys->i_titles )
514             {
515                 return 0; /* EOF */
516             }
517
518             DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
519         }
520
521         if( p_sys->i_pack_len >= 1024 )
522         {
523             msg_Err( p_demux, "i_pack_len >= 1024 (%i). "
524                      "This shouldn't happen!", p_sys->i_pack_len );
525             return 0; /* EOF */
526         }
527
528         /* FIXME: Ugly kludge: we send the pack block to the input for it
529          * sometimes has a zero scr and restart the sync */
530         p_sys->i_cur_block++;
531         p_sys->i_title_offset++;
532
533         DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
534     }
535
536     if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
537     {
538         if( p_sys->i_title + 1 >= p_sys->i_titles )
539         {
540             return 0; /* EOF */
541         }
542
543         DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
544     }
545
546     /*
547      * Read actual data
548      */
549     i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
550     p_sys->i_pack_len -= i_blocks_once;
551
552     /* Reads from DVD */
553     i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
554                             i_blocks_once, p_buffer );
555     if( i_read != i_blocks_once )
556     {
557         msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
558                  i_read, i_blocks_once, p_sys->i_cur_block );
559         intf_UserFatal( p_demux, VLC_FALSE, _("Playback failure"), 
560                         _("DVDRead could not read %d/%d blocks at 0x%02x."),
561                         i_read, i_blocks_once, p_sys->i_cur_block );
562         return -1;
563     }
564
565     p_sys->i_cur_block += i_read;
566     p_sys->i_title_offset += i_read;
567
568 #if 0
569     msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
570              i_read, p_sys->i_pack_len, p_sys->i_cur_block );
571 #endif
572
573     for( i = 0; i < i_read; i++ )
574     {
575         DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
576                     DVD_VIDEO_LB_LEN );
577     }
578
579 #undef p_pgc
580
581     return 1;
582 }
583
584 /*****************************************************************************
585  * DemuxBlock: demux a given block
586  *****************************************************************************/
587 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
588 {
589     demux_sys_t *p_sys = p_demux->p_sys;
590     uint8_t     *p = pkt;
591
592     while( p < &pkt[i_pkt] )
593     {
594         int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
595         block_t *p_pkt;
596         if( i_size <= 0 )
597         {
598             break;
599         }
600
601         /* Create a block */
602         p_pkt = block_New( p_demux, i_size );
603         memcpy( p_pkt->p_buffer, p, i_size);
604
605         /* Parse it and send it */
606         switch( 0x100 | p[3] )
607         {
608         case 0x1b9:
609         case 0x1bb:
610         case 0x1bc:
611
612 #ifdef DVDREAD_DEBUG
613             if( p[3] == 0xbc )
614             {
615                 msg_Warn( p_demux, "received a PSM packet" );
616             }
617             else if( p[3] == 0xbb )
618             {
619                 msg_Warn( p_demux, "received a SYSTEM packet" );
620             }
621 #endif
622             block_Release( p_pkt );
623             break;
624
625         case 0x1ba:
626         {
627             int64_t i_scr;
628             int i_mux_rate;
629             if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
630             {
631                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
632                 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
633             }
634             block_Release( p_pkt );
635             break;
636         }
637         default:
638         {
639             int i_id = ps_pkt_id( p_pkt );
640             if( i_id >= 0xc0 )
641             {
642                 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
643
644                 if( !tk->b_seen )
645                 {
646                     ESNew( p_demux, i_id, 0 );
647                 }
648                 if( tk->b_seen && tk->es &&
649                     !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
650                 {
651                     es_out_Send( p_demux->out, tk->es, p_pkt );
652                 }
653                 else
654                 {
655                     block_Release( p_pkt );
656                 }
657             }
658             else
659             {
660                 block_Release( p_pkt );
661             }
662             break;
663         }
664         }
665
666         p += i_size;
667     }
668
669     return VLC_SUCCESS;
670 }
671
672 /*****************************************************************************
673  * ESNew: register a new elementary stream
674  *****************************************************************************/
675 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
676 {
677     demux_sys_t *p_sys = p_demux->p_sys;
678     ps_track_t  *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
679     char psz_language[3];
680
681     if( tk->b_seen ) return;
682
683     if( ps_track_fill( tk, 0, i_id ) )
684     {
685         msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
686         return;
687     }
688
689     psz_language[0] = psz_language[1] = psz_language[2] = 0;
690     if( i_lang && i_lang != 0xffff )
691     {
692         psz_language[0] = (i_lang >> 8)&0xff;
693         psz_language[1] = (i_lang     )&0xff;
694     }
695
696     /* Add a new ES */
697     if( tk->fmt.i_cat == VIDEO_ES )
698     {
699         if( p_sys->i_aspect >= 0 )
700         {
701             tk->fmt.video.i_aspect = p_sys->i_aspect;
702         }
703     }
704     else if( tk->fmt.i_cat == AUDIO_ES )
705     {
706         int i_audio = -1;
707         /* find the audio number PLEASE find another way */
708         if( (i_id&0xbdf8) == 0xbd88 )       /* dts */
709         {
710             i_audio = i_id&0x07;
711         }
712         else if( (i_id&0xbdf0) == 0xbd80 )  /* a52 */
713         {
714             i_audio = i_id&0xf;
715         }
716         else if( (i_id&0xbdf0) == 0xbda0 )  /* lpcm */
717         {
718             i_audio = i_id&0x1f;
719         }
720         else if( ( i_id&0xe0 ) == 0xc0 )    /* mpga */
721         {
722             i_audio = i_id&0x1f;
723         }
724
725         if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
726     }
727     else if( tk->fmt.i_cat == SPU_ES )
728     {
729         /* Palette */
730         tk->fmt.subs.spu.palette[0] = 0xBeef;
731         memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
732                 16 * sizeof( uint32_t ) );
733
734         if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
735     }
736
737     tk->es = es_out_Add( p_demux->out, &tk->fmt );
738     tk->b_seen = VLC_TRUE;
739 }
740
741 /*****************************************************************************
742  * DvdReadSetArea: initialize input data for title x, chapter y.
743  * It should be called for each user navigation request.
744  *****************************************************************************
745  * Take care that i_title and i_chapter start from 0.
746  *****************************************************************************/
747 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
748                            int i_angle )
749 {
750     demux_sys_t *p_sys = p_demux->p_sys;
751     int pgc_id = 0, pgn = 0;
752     int i;
753
754 #define p_pgc p_sys->p_cur_pgc
755 #define p_vmg p_sys->p_vmg_file
756 #define p_vts p_sys->p_vts_file
757
758     if( i_title >= 0 && i_title < p_sys->i_titles &&
759         i_title != p_sys->i_title )
760     {
761         int i_start_cell, i_end_cell;
762
763         if( p_sys->p_title != NULL ) DVDCloseFile( p_sys->p_title );
764         if( p_vts != NULL ) ifoClose( p_vts );
765         p_sys->i_title = i_title;
766
767         /*
768          *  We have to load all title information
769          */
770         msg_Dbg( p_demux, "open VTS %d, for title %d",
771                  p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
772
773         /* Ifo vts */
774         if( !( p_vts = ifoOpen( p_sys->p_dvdread,
775                p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
776         {
777             msg_Err( p_demux, "fatal error in vts ifo" );
778             return VLC_EGENERIC;
779         }
780
781         /* Title position inside the selected vts */
782         p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
783
784         /* Find title start/end */
785         pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
786         pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
787         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
788
789         p_sys->i_title_start_cell =
790             i_start_cell = p_pgc->program_map[pgn - 1] - 1;
791         p_sys->i_title_start_block =
792             p_pgc->cell_playback[i_start_cell].first_sector;
793
794         p_sys->i_title_end_cell =
795             i_end_cell = p_pgc->nr_of_cells - 1;
796         p_sys->i_title_end_block =
797             p_pgc->cell_playback[i_end_cell].last_sector;
798
799         p_sys->i_title_offset = 0;
800
801         p_sys->i_title_blocks = 0;
802         for( i = i_start_cell; i <= i_end_cell; i++ )
803         {
804             p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
805                 p_pgc->cell_playback[i].first_sector + 1;
806         }
807
808         msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
809                  "start %d end %d blocks: %d",
810                  i_title + 1, p_sys->i_ttn, pgc_id, pgn,
811                  p_sys->i_title_start_block, p_sys->i_title_end_block,
812                  p_sys->i_title_blocks );
813
814         /*
815          * Set properties for current chapter
816          */
817         p_sys->i_chapter = 0;
818         p_sys->i_chapters =
819             p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
820
821         pgc_id = p_vts->vts_ptt_srpt->title[
822                     p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
823         pgn = p_vts->vts_ptt_srpt->title[
824                     p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
825
826         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
827         p_sys->i_pack_len = 0;
828         p_sys->i_next_cell =
829             p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
830         DvdReadFindCell( p_demux );
831
832         p_sys->i_next_vobu = p_sys->i_cur_block =
833             p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
834
835         /*
836          * Angle management
837          */
838         p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
839         if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
840
841         /*
842          * We've got enough info, time to open the title set data.
843          */
844         if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
845             p_vmg->tt_srpt->title[i_title].title_set_nr,
846             DVD_READ_TITLE_VOBS ) ) )
847         {
848             msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
849                      p_vmg->tt_srpt->title[i_title].title_set_nr );
850             return VLC_EGENERIC;
851         }
852
853         //IfoPrintTitle( p_demux );
854
855         /*
856          * Destroy obsolete ES by reinitializing program 0
857          * and find all ES in title with ifo data
858          */
859         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
860
861         for( i = 0; i < PS_TK_COUNT; i++ )
862         {
863             ps_track_t *tk = &p_sys->tk[i];
864             if( tk->b_seen )
865             {
866                 es_format_Clean( &tk->fmt );
867                 if( tk->es ) es_out_Del( p_demux->out, tk->es );
868             }
869             tk->b_seen = VLC_FALSE;
870         }
871
872         if( p_demux->info.i_title != i_title )
873         {
874             p_demux->info.i_update |=
875                 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
876             p_demux->info.i_title = i_title;
877             p_demux->info.i_seekpoint = 0;
878         }
879
880         /* TODO: re-add angles */
881
882
883         ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
884         p_sys->i_aspect = p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio;
885
886 #define audio_control \
887     p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
888
889         /* Audio ES, in the order they appear in the .ifo */
890         for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
891         {
892             int i_position = 0;
893             uint16_t i_id;
894
895             //IfoPrintAudio( p_demux, i );
896
897             /* Audio channel is active if first byte is 0x80 */
898             if( audio_control & 0x8000 )
899             {
900                 i_position = ( audio_control & 0x7F00 ) >> 8;
901
902                 msg_Dbg( p_demux, "audio position  %d", i_position );
903                 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
904                 {
905                 case 0x00: /* A52 */
906                     i_id = (0x80 + i_position) | 0xbd00;
907                     break;
908                 case 0x02:
909                 case 0x03: /* MPEG audio */
910                     i_id = 0xc000 + i_position;
911                     break;
912                 case 0x04: /* LPCM */
913                     i_id = (0xa0 + i_position) | 0xbd00;
914                     break;
915                 case 0x06: /* DTS */
916                     i_id = (0x88 + i_position) | 0xbd00;
917                     break;
918                 default:
919                     i_id = 0;
920                     msg_Err( p_demux, "unknown audio type %.2x",
921                         p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
922                 }
923
924                 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
925                        vts_audio_attr[i - 1].lang_code );
926             }
927         }
928 #undef audio_control
929
930 #define spu_palette \
931     p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
932
933         memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
934
935 #define spu_control \
936     p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
937
938         /* Sub Picture ES */
939         for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
940         {
941             int i_position = 0;
942             uint16_t i_id;
943
944             //IfoPrintSpu( p_sys, i );
945             msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
946
947             if( spu_control & 0x80000000 )
948             {
949                 /*  there are several streams for one spu */
950                 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
951                 {
952                     /* 16:9 */
953                     switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
954                     {
955                     case 1: /* letterbox */
956                         i_position = spu_control & 0xff;
957                         break;
958                     case 2: /* pan&scan */
959                         i_position = ( spu_control >> 8 ) & 0xff;
960                         break;
961                     default: /* widescreen */
962                         i_position = ( spu_control >> 16 ) & 0xff;
963                         break;
964                     }
965                 }
966                 else
967                 {
968                     /* 4:3 */
969                     i_position = ( spu_control >> 24 ) & 0x7F;
970                 }
971
972                 i_id = (0x20 + i_position) | 0xbd00;
973
974                 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
975                        vts_subp_attr[i - 1].lang_code );
976             }
977         }
978 #undef spu_control
979
980     }
981     else if( i_title != -1 && i_title != p_sys->i_title )
982
983     {
984         return VLC_EGENERIC; /* Couldn't set title */
985     }
986
987     /*
988      * Chapter selection
989      */
990
991     if( i_chapter >= 0 && i_chapter < p_sys->i_chapters )
992     {
993         pgc_id = p_vts->vts_ptt_srpt->title[
994                      p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
995         pgn = p_vts->vts_ptt_srpt->title[
996                   p_sys->i_ttn - 1].ptt[i_chapter].pgn;
997
998         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
999
1000         p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
1001         p_sys->i_chapter = i_chapter;
1002         DvdReadFindCell( p_demux );
1003
1004         p_sys->i_title_offset = 0;
1005         for( i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
1006         {
1007             p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
1008                 p_pgc->cell_playback[i].first_sector + 1;
1009         }
1010
1011         p_sys->i_pack_len = 0;
1012         p_sys->i_next_vobu = p_sys->i_cur_block =
1013             p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1014
1015         if( p_demux->info.i_seekpoint != i_chapter )
1016         {
1017             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1018             p_demux->info.i_seekpoint = i_chapter;
1019         }
1020     }
1021     else if( i_chapter != -1 )
1022
1023     {
1024         return VLC_EGENERIC; /* Couldn't set chapter */
1025     }
1026
1027 #undef p_pgc
1028 #undef p_vts
1029 #undef p_vmg
1030
1031     return VLC_SUCCESS;
1032 }
1033
1034 /*****************************************************************************
1035  * DvdReadSeek : Goes to a given position on the stream.
1036  *****************************************************************************
1037  * This one is used by the input and translate chronological position from
1038  * input to logical position on the device.
1039  *****************************************************************************/
1040 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
1041 {
1042     demux_sys_t *p_sys = p_demux->p_sys;
1043     int i_chapter = 0;
1044     int i_cell = 0;
1045     int i_vobu = 0;
1046     int i_sub_cell = 0;
1047     int i_block;
1048
1049 #define p_pgc p_sys->p_cur_pgc
1050 #define p_vts p_sys->p_vts_file
1051
1052     /* Find cell */
1053     i_block = i_block_offset;
1054     for( i_cell = p_sys->i_title_start_cell;
1055          i_cell <= p_sys->i_title_end_cell; i_cell++ )
1056     {
1057         if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
1058             (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
1059
1060         i_block -= (p_pgc->cell_playback[i_cell].last_sector -
1061             p_pgc->cell_playback[i_cell].first_sector + 1);
1062     }
1063     if( i_cell > p_sys->i_title_end_cell )
1064     {
1065         msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
1066         return;
1067     }
1068     i_block += p_pgc->cell_playback[i_cell].first_sector;
1069     p_sys->i_title_offset = i_block_offset;
1070
1071     /* Find chapter */
1072     for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
1073     {
1074         int pgc_id, pgn, i_tmp;
1075
1076         pgc_id = p_vts->vts_ptt_srpt->title[
1077                     p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1078         pgn = p_vts->vts_ptt_srpt->title[
1079                     p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1080
1081         i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
1082
1083         if( i_tmp > i_cell ) break;
1084     }
1085
1086     if( i_chapter < p_sys->i_chapters &&
1087         p_demux->info.i_seekpoint != i_chapter )
1088     {
1089         p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1090         p_demux->info.i_seekpoint = i_chapter;
1091     }
1092
1093     /* Find vobu */
1094     while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1095     {
1096         i_vobu++;
1097     }
1098
1099     /* Find sub_cell */
1100     while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1101            p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1102     {
1103         i_sub_cell++;
1104     }
1105
1106 #if 1
1107     msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1108              "cell_sector %d vobu_sector %d sub_cell_sector %d",
1109              i_cell, i_sub_cell, i_chapter, i_vobu,
1110              p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1111              p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1112              p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1113 #endif
1114
1115     p_sys->i_cur_block = i_block;
1116     p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1117     p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1118     p_sys->i_cur_cell = i_cell;
1119     p_sys->i_chapter = i_chapter;
1120     DvdReadFindCell( p_demux );
1121
1122 #undef p_vts
1123 #undef p_pgc
1124
1125     return;
1126 }
1127
1128 /*****************************************************************************
1129  * DvdReadHandleDSI
1130  *****************************************************************************/
1131 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1132 {
1133     demux_sys_t *p_sys = p_demux->p_sys;
1134
1135     navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1136
1137     /*
1138      * Determine where we go next.  These values are the ones we mostly
1139      * care about.
1140      */
1141     p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1142     p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1143
1144     /*
1145      * Store the timecodes so we can get the current time
1146      */
1147     p_sys->i_title_cur_time = (mtime_t) (p_sys->dsi_pack.dsi_gi.nv_pck_scr / 90 * 1000);
1148     p_sys->i_cell_cur_time = (mtime_t) dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 );
1149
1150     /*
1151      * If we're not at the end of this cell, we can determine the next
1152      * VOBU to display using the VOBU_SRI information section of the
1153      * DSI.  Using this value correctly follows the current angle,
1154      * avoiding the doubled scenes in The Matrix, and makes our life
1155      * really happy.
1156      */
1157
1158     p_sys->i_next_vobu = p_sys->i_cur_block +
1159         ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1160
1161     if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL
1162         && p_sys->i_angle > 1 )
1163     {
1164         switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1165         {
1166         case 0x4:
1167             /* Interleaved unit with no angle */
1168             if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1169             {
1170                 p_sys->i_next_vobu = p_sys->i_cur_block +
1171                     p_sys->dsi_pack.sml_pbi.ilvu_sa;
1172                 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1173             }
1174             else
1175             {
1176                 p_sys->i_next_vobu = p_sys->i_cur_block +
1177                     p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1178             }
1179             break;
1180         case 0x5:
1181             /* vobu is end of ilvu */
1182             if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1183             {
1184                 p_sys->i_next_vobu = p_sys->i_cur_block +
1185                     p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1186                 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1187
1188                 break;
1189             }
1190         case 0x6:
1191             /* vobu is beginning of ilvu */
1192         case 0x9:
1193             /* next scr is 0 */
1194         case 0xa:
1195             /* entering interleaved section */
1196         case 0x8:
1197             /* non interleaved cells in interleaved section */
1198         default:
1199             p_sys->i_next_vobu = p_sys->i_cur_block +
1200                 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1201             break;
1202         }
1203     }
1204     else if( p_sys->dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
1205     {
1206         p_sys->i_cur_cell = p_sys->i_next_cell;
1207
1208         /* End of title */
1209         if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells ) return;
1210
1211         DvdReadFindCell( p_demux );
1212
1213         p_sys->i_next_vobu =
1214             p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1215
1216         p_sys->i_cell_duration = (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 );
1217     }
1218
1219
1220 #if 0
1221     msg_Dbg( p_demux, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d c_time %lld",
1222              p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1223              p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1224              p_sys->dsi_pack.dsi_gi.vobu_ea,
1225              p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1226              p_sys->dsi_pack.dsi_gi.vobu_c_idn,
1227              dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 ) );
1228
1229     msg_Dbg( p_demux, "cell duration: %lld", 
1230              (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 ) );
1231
1232     msg_Dbg( p_demux, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1233              p_sys->dsi_pack.sml_pbi.category,
1234              p_sys->dsi_pack.sml_pbi.ilvu_ea,
1235              p_sys->dsi_pack.sml_pbi.ilvu_sa,
1236              p_sys->dsi_pack.sml_pbi.size );
1237
1238     msg_Dbg( p_demux, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1239              p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1240              p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1241              p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1242 #endif
1243 }
1244
1245 /*****************************************************************************
1246  * DvdReadFindCell
1247  *****************************************************************************/
1248 static void DvdReadFindCell( demux_t *p_demux )
1249 {
1250     demux_sys_t *p_sys = p_demux->p_sys;
1251
1252     pgc_t *p_pgc;
1253     int   pgc_id, pgn;
1254     int   i = 0;
1255
1256 #define cell p_sys->p_cur_pgc->cell_playback
1257
1258     if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1259     {
1260         p_sys->i_cur_cell += p_sys->i_angle - 1;
1261
1262         while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1263         {
1264             i++;
1265         }
1266         p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1267     }
1268     else
1269     {
1270         p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1271     }
1272
1273 #undef cell
1274
1275     if( p_sys->i_chapter + 1 >= p_sys->i_chapters ) return;
1276
1277     pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1278                 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgcn;
1279     pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1280               p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgn;
1281     p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1282
1283     if( p_sys->i_cur_cell >= p_pgc->program_map[pgn - 1] - 1 )
1284     {
1285         p_sys->i_chapter++;
1286
1287         if( p_sys->i_chapter < p_sys->i_chapters &&
1288             p_demux->info.i_seekpoint != p_sys->i_chapter )
1289         {
1290             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1291             p_demux->info.i_seekpoint = p_sys->i_chapter;
1292         }
1293     }
1294 }
1295
1296 /*****************************************************************************
1297  * DemuxTitles: get the titles/chapters structure
1298  *****************************************************************************/
1299 static void DemuxTitles( demux_t *p_demux, int *pi_angle )
1300 {
1301     demux_sys_t *p_sys = p_demux->p_sys;
1302     input_title_t *t;
1303     seekpoint_t *s;
1304     int32_t i_titles;
1305     int i;
1306
1307     /* Find out number of titles/chapters */
1308 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1309
1310     i_titles = tt_srpt->nr_of_srpts;
1311     msg_Dbg( p_demux, "number of titles: %d", i_titles );
1312
1313     for( i = 0; i < i_titles; i++ )
1314     {
1315         int32_t i_chapters = 0;
1316         int j;
1317
1318         i_chapters = tt_srpt->title[i].nr_of_ptts;
1319         msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1320
1321         t = vlc_input_title_New();
1322
1323         for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1324         {
1325             s = vlc_seekpoint_New();
1326             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1327         }
1328
1329         TAB_APPEND( p_sys->i_titles, p_sys->titles, t );
1330     }
1331
1332 #undef tt_srpt
1333 }