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