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