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