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