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