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