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