]> git.sesse.net Git - vlc/blob - modules/access/dvdnav.c
ad1240a5c72cf242d374a919cecd2632d1ba9137
[vlc] / modules / access / dvdnav.c
1 /*****************************************************************************
2  * dvdnav.c: DVD module using the dvdnav library.
3  *****************************************************************************
4  * Copyright (C) 2004-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * NOTA BENE: this module requires the linking against a library which is
26  * known to require licensing under the GNU General Public License version 2
27  * (or later). Therefore, the result of compiling this module will normally
28  * be subject to the terms of that later license.
29  *****************************************************************************/
30
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <assert.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <unistd.h>     /* close() */
45
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
48 #include <vlc_input.h>
49 #include <vlc_access.h>
50 #include <vlc_demux.h>
51 #include <vlc_charset.h>
52 #include <vlc_fs.h>
53 #include <vlc_vout.h>
54 #include <vlc_dialog.h>
55 #include <vlc_iso_lang.h>
56
57 /* FIXME we should find a better way than including that */
58 #include "../../src/text/iso-639_def.h"
59
60
61 #include <dvdnav/dvdnav.h>
62
63 #include "../demux/ps.h"
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 #define ANGLE_TEXT N_("DVD angle")
69 #define ANGLE_LONGTEXT N_( \
70      "Default DVD angle." )
71
72 #define MENU_TEXT N_("Start directly in menu")
73 #define MENU_LONGTEXT N_( \
74     "Start the DVD directly in the main menu. This "\
75     "will try to skip all the useless warning introductions." )
76
77 #define LANGUAGE_DEFAULT ("en")
78
79 static int  Open ( vlc_object_t * );
80 static void Close( vlc_object_t * );
81
82 vlc_module_begin ()
83     set_shortname( N_("DVD with menus") )
84     set_description( N_("DVDnav Input") )
85     set_category( CAT_INPUT )
86     set_subcategory( SUBCAT_INPUT_ACCESS )
87     add_integer( "dvdnav-angle", 1, ANGLE_TEXT,
88         ANGLE_LONGTEXT, false )
89     add_bool( "dvdnav-menu", true,
90         MENU_TEXT, MENU_LONGTEXT, false )
91     set_capability( "access_demux", 5 )
92     add_shortcut( "dvd", "dvdnav", "file" )
93     set_callbacks( Open, Close )
94 vlc_module_end ()
95
96 /* Shall we use libdvdnav's read ahead cache? */
97 #ifdef __OS2__
98 #define DVD_READ_CACHE 0
99 #else
100 #define DVD_READ_CACHE 1
101 #endif
102
103 /*****************************************************************************
104  * Local prototypes
105  *****************************************************************************/
106 struct demux_sys_t
107 {
108     dvdnav_t    *dvdnav;
109
110     /* */
111     bool        b_reset_pcr;
112
113     struct
114     {
115         bool         b_created;
116         bool         b_enabled;
117         vlc_mutex_t  lock;
118         vlc_timer_t  timer;
119     } still;
120
121     /* track */
122     ps_track_t  tk[PS_TK_COUNT];
123     int         i_mux_rate;
124
125     /* for spu variables */
126     input_thread_t *p_input;
127
128     /* event */
129     vout_thread_t *p_vout;
130
131     /* palette for menus */
132     uint32_t clut[16];
133     uint8_t  palette[4][4];
134     bool b_spu_change;
135
136     /* Aspect ration */
137     struct {
138         unsigned i_num;
139         unsigned i_den;
140     } sar;
141
142     /* */
143     int           i_title;
144     input_title_t **title;
145
146     /* lenght of program group chain */
147     mtime_t     i_pgc_length;
148     int         i_vobu_index;
149     int         i_vobu_flush;
150 };
151
152 static int Control( demux_t *, int, va_list );
153 static int Demux( demux_t * );
154 static int DemuxBlock( demux_t *, const uint8_t *, int );
155 static void DemuxForceStill( demux_t * );
156
157 static void DemuxTitles( demux_t * );
158 static void ESSubtitleUpdate( demux_t * );
159 static void ButtonUpdate( demux_t *, bool );
160
161 static void ESNew( demux_t *, int );
162 static int ProbeDVD( const char * );
163
164 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
165
166 static int ControlInternal( demux_t *, int, ... );
167
168 static void StillTimer( void * );
169
170 static int EventMouse( vlc_object_t *, char const *,
171                        vlc_value_t, vlc_value_t, void * );
172 static int EventIntf( vlc_object_t *, char const *,
173                       vlc_value_t, vlc_value_t, void * );
174
175 /*****************************************************************************
176  * DemuxOpen:
177  *****************************************************************************/
178 static int Open( vlc_object_t *p_this )
179 {
180     demux_t     *p_demux = (demux_t*)p_this;
181     demux_sys_t *p_sys;
182     dvdnav_t    *p_dvdnav;
183     int         i_angle;
184     char        *psz_file;
185     char        *psz_code;
186     bool forced = false;
187
188     if( p_demux->psz_access != NULL
189      && !strncmp(p_demux->psz_access, "dvd", 3) )
190         forced = true;
191
192     if( !p_demux->psz_file || !*p_demux->psz_file )
193     {
194         /* Only when selected */
195         if( !forced )
196             return VLC_EGENERIC;
197
198         psz_file = var_InheritString( p_this, "dvd" );
199     }
200     else
201         psz_file = strdup( p_demux->psz_file );
202
203 #if defined( _WIN32 ) || defined( __OS2__ )
204     if( psz_file != NULL )
205     {
206         /* Remove trailing backslash, otherwise dvdnav_open will fail */
207         size_t flen = strlen( psz_file );
208         if( flen > 0 && psz_file[flen - 1] == '\\' )
209             psz_file[flen - 1] = '\0';
210     }
211     else
212         psz_file = strdup("");
213 #endif
214     if( unlikely(psz_file == NULL) )
215         return VLC_EGENERIC;
216
217     /* Try some simple probing to avoid going through dvdnav_open too often */
218     if( !forced && ProbeDVD( psz_file ) != VLC_SUCCESS )
219     {
220         free( psz_file );
221         return VLC_EGENERIC;
222     }
223
224     /* Open dvdnav */
225     const char *psz_path = ToLocale( psz_file );
226     if( dvdnav_open( &p_dvdnav, psz_path ) != DVDNAV_STATUS_OK )
227         p_dvdnav = NULL;
228     LocaleFree( psz_path );
229     if( p_dvdnav == NULL )
230     {
231         msg_Warn( p_demux, "cannot open DVD (%s)", psz_file);
232         free( psz_file );
233         return VLC_EGENERIC;
234     }
235     free( psz_file );
236
237     /* Fill p_demux field */
238     DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
239     p_sys->dvdnav = p_dvdnav;
240     p_sys->b_reset_pcr = false;
241
242     ps_track_init( p_sys->tk );
243     p_sys->sar.i_num = 0;
244     p_sys->sar.i_den = 0;
245     p_sys->i_mux_rate = 0;
246     p_sys->i_pgc_length = 0;
247     p_sys->b_spu_change = false;
248     p_sys->i_vobu_index = 0;
249     p_sys->i_vobu_flush = 0;
250
251     if( 1 )
252     {
253         // Hack for libdvdnav CVS.
254         // Without it dvdnav_get_number_of_titles() fails.
255         // Remove when fixed in libdvdnav CVS.
256         uint8_t buffer[DVD_VIDEO_LB_LEN];
257         int i_event, i_len;
258
259         if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
260               == DVDNAV_STATUS_ERR )
261         {
262             msg_Warn( p_demux, "dvdnav_get_next_block failed" );
263         }
264
265         dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
266     }
267
268     /* Configure dvdnav */
269     if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
270           DVDNAV_STATUS_OK )
271     {
272         msg_Warn( p_demux, "cannot set read-a-head flag" );
273     }
274
275     if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
276           DVDNAV_STATUS_OK )
277     {
278         msg_Warn( p_demux, "cannot set PGC positioning flag" );
279     }
280
281     /* Set menu language */
282     psz_code = DemuxGetLanguageCode( p_demux, "menu-language" );
283     if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
284         DVDNAV_STATUS_OK )
285     {
286         msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
287                   psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
288         /* We try to fall back to 'en' */
289         if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
290             dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
291     }
292     free( psz_code );
293
294     /* Set audio language */
295     psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
296     if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
297         DVDNAV_STATUS_OK )
298     {
299         msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
300                   psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
301         /* We try to fall back to 'en' */
302         if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
303             dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
304     }
305     free( psz_code );
306
307     /* Set spu language */
308     psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
309     if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
310         DVDNAV_STATUS_OK )
311     {
312         msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
313                   psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
314         /* We try to fall back to 'en' */
315         if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
316             dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
317     }
318     free( psz_code );
319
320     DemuxTitles( p_demux );
321
322     if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
323     {
324         msg_Dbg( p_demux, "trying to go to dvd menu" );
325
326         if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
327         {
328             msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
329             dialog_Fatal( p_demux, _("Playback failure"), "%s",
330                             _("VLC cannot set the DVD's title. It possibly "
331                               "cannot decrypt the entire disc.") );
332             dvdnav_close( p_sys->dvdnav );
333             free( p_sys );
334             return VLC_EGENERIC;
335         }
336
337         if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
338             DVDNAV_STATUS_OK )
339         {
340             /* Try going to menu root */
341             if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
342                 DVDNAV_STATUS_OK )
343                     msg_Warn( p_demux, "cannot go to dvd menu" );
344         }
345     }
346
347     i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
348     if( i_angle <= 0 ) i_angle = 1;
349
350     /* FIXME hack hack hack hack FIXME */
351     /* Get p_input and create variable */
352     p_sys->p_input = demux_GetParentInput( p_demux );
353     var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
354     var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
355     var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
356     var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
357     var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
358     var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
359     var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
360
361     /* catch vout creation event */
362     var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
363
364     p_sys->still.b_enabled = false;
365     vlc_mutex_init( &p_sys->still.lock );
366     if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
367         p_sys->still.b_created = true;
368
369     return VLC_SUCCESS;
370 }
371
372 /*****************************************************************************
373  * Close:
374  *****************************************************************************/
375 static void Close( vlc_object_t *p_this )
376 {
377     demux_t     *p_demux = (demux_t*)p_this;
378     demux_sys_t *p_sys = p_demux->p_sys;
379
380     /* Stop vout event handler */
381     var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
382     if( p_sys->p_vout != NULL )
383     {   /* Should not happen, but better be safe than sorry. */
384         msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
385         var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
386         var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
387     }
388
389     /* Stop still image handler */
390     if( p_sys->still.b_created )
391         vlc_timer_destroy( p_sys->still.timer );
392     vlc_mutex_destroy( &p_sys->still.lock );
393
394     var_Destroy( p_sys->p_input, "highlight" );
395     var_Destroy( p_sys->p_input, "x-start" );
396     var_Destroy( p_sys->p_input, "x-end" );
397     var_Destroy( p_sys->p_input, "y-start" );
398     var_Destroy( p_sys->p_input, "y-end" );
399     var_Destroy( p_sys->p_input, "color" );
400     var_Destroy( p_sys->p_input, "menu-palette" );
401
402     vlc_object_release( p_sys->p_input );
403
404     for( int i = 0; i < PS_TK_COUNT; i++ )
405     {
406         ps_track_t *tk = &p_sys->tk[i];
407         if( tk->b_seen )
408         {
409             es_format_Clean( &tk->fmt );
410             if( tk->es ) es_out_Del( p_demux->out, tk->es );
411         }
412     }
413
414     /* Free the array of titles */
415     for( int i = 0; i < p_sys->i_title; i++ )
416         vlc_input_title_Delete( p_sys->title[i] );
417     TAB_CLEAN( p_sys->i_title, p_sys->title );
418
419     dvdnav_close( p_sys->dvdnav );
420     free( p_sys );
421 }
422
423 /*****************************************************************************
424  * Control:
425  *****************************************************************************/
426 static int Control( demux_t *p_demux, int i_query, va_list args )
427 {
428     demux_sys_t *p_sys = p_demux->p_sys;
429     input_title_t ***ppp_title;
430     int i;
431
432     switch( i_query )
433     {
434         case DEMUX_SET_POSITION:
435         case DEMUX_GET_POSITION:
436         case DEMUX_GET_TIME:
437         case DEMUX_GET_LENGTH:
438         {
439             uint32_t pos, len;
440             if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
441                   DVDNAV_STATUS_OK || len == 0 )
442             {
443                 return VLC_EGENERIC;
444             }
445
446             switch( i_query )
447             {
448             case DEMUX_GET_POSITION:
449                 *va_arg( args, double* ) = (double)pos / (double)len;
450                 return VLC_SUCCESS;
451
452             case DEMUX_SET_POSITION:
453                 pos = va_arg( args, double ) * len;
454                 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
455                       DVDNAV_STATUS_OK )
456                 {
457                     return VLC_SUCCESS;
458                 }
459                 break;
460
461             case DEMUX_GET_TIME:
462                 if( p_sys->i_pgc_length > 0 )
463                 {
464                     *va_arg( args, int64_t * ) = p_sys->i_pgc_length*pos/len;
465                     return VLC_SUCCESS;
466                 }
467                 break;
468
469             case DEMUX_GET_LENGTH:
470                 if( p_sys->i_pgc_length > 0 )
471                 {
472                     *va_arg( args, int64_t * ) = (int64_t)p_sys->i_pgc_length;
473                     return VLC_SUCCESS;
474                 }
475                 break;
476             }
477             return VLC_EGENERIC;
478         }
479
480         /* Special for access_demux */
481         case DEMUX_CAN_PAUSE:
482         case DEMUX_CAN_SEEK:
483         case DEMUX_CAN_CONTROL_PACE:
484             /* TODO */
485             *va_arg( args, bool * ) = true;
486             return VLC_SUCCESS;
487
488         case DEMUX_SET_PAUSE_STATE:
489             return VLC_SUCCESS;
490
491         case DEMUX_GET_TITLE_INFO:
492             ppp_title = va_arg( args, input_title_t*** );
493             *va_arg( args, int* ) = p_sys->i_title;
494             *va_arg( args, int* ) = 0; /* Title offset */
495             *va_arg( args, int* ) = 1; /* Chapter offset */
496
497             /* Duplicate title infos */
498             *ppp_title = malloc( p_sys->i_title * sizeof( input_title_t * ) );
499             for( i = 0; i < p_sys->i_title; i++ )
500             {
501                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
502             }
503             return VLC_SUCCESS;
504
505         case DEMUX_SET_TITLE:
506             i = (int)va_arg( args, int );
507             if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
508                   != DVDNAV_STATUS_OK ) ||
509                 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
510                   != DVDNAV_STATUS_OK ) )
511             {
512                 msg_Warn( p_demux, "cannot set title/chapter" );
513                 return VLC_EGENERIC;
514             }
515             p_demux->info.i_update |=
516                 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
517             p_demux->info.i_title = i;
518             p_demux->info.i_seekpoint = 0;
519             return VLC_SUCCESS;
520
521         case DEMUX_SET_SEEKPOINT:
522             i = va_arg( args, int );
523             if( p_demux->info.i_title == 0 )
524             {
525                 static const int argtab[] = {
526                     DVD_MENU_Escape,
527                     DVD_MENU_Root,
528                     DVD_MENU_Title,
529                     DVD_MENU_Part,
530                     DVD_MENU_Subpicture,
531                     DVD_MENU_Audio,
532                     DVD_MENU_Angle
533                 };
534                 enum { numargs = sizeof(argtab)/sizeof(int) };
535                 if( (unsigned)i >= numargs || DVDNAV_STATUS_OK !=
536                            dvdnav_menu_call(p_sys->dvdnav,argtab[i]) )
537                     return VLC_EGENERIC;
538             }
539             else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
540                                        i + 1 ) != DVDNAV_STATUS_OK )
541             {
542                 msg_Warn( p_demux, "cannot set title/chapter" );
543                 return VLC_EGENERIC;
544             }
545             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
546             p_demux->info.i_seekpoint = i;
547             return VLC_SUCCESS;
548
549         case DEMUX_GET_PTS_DELAY:
550             *va_arg( args, int64_t * ) =
551                 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
552             return VLC_SUCCESS;
553
554         case DEMUX_GET_META:
555         {
556             const char *title_name = NULL;
557
558             dvdnav_get_title_string(p_sys->dvdnav, &title_name);
559             if( (NULL != title_name) && ('\0' != title_name[0]) )
560             {
561                 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
562                 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
563                 return VLC_SUCCESS;
564             }
565             return VLC_EGENERIC;
566         }
567
568         case DEMUX_NAV_ACTIVATE:
569         {
570             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
571
572             ButtonUpdate( p_demux, true );
573             dvdnav_button_activate( p_sys->dvdnav, pci );
574             break;
575         }
576
577         case DEMUX_NAV_UP:
578         {
579             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
580
581             dvdnav_upper_button_select( p_sys->dvdnav, pci );
582             break;
583         }
584
585         case DEMUX_NAV_DOWN:
586         {
587             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
588
589             dvdnav_lower_button_select( p_sys->dvdnav, pci );
590             break;
591         }
592
593         case DEMUX_NAV_LEFT:
594         {
595             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
596
597             dvdnav_left_button_select( p_sys->dvdnav, pci );
598             break;
599         }
600
601         case DEMUX_NAV_RIGHT:
602         {
603             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
604
605             dvdnav_right_button_select( p_sys->dvdnav, pci );
606             break;
607         }
608
609         /* TODO implement others */
610         default:
611             return VLC_EGENERIC;
612     }
613
614     return VLC_SUCCESS;
615 }
616
617 static int ControlInternal( demux_t *p_demux, int i_query, ... )
618 {
619     va_list args;
620     int     i_result;
621
622     va_start( args, i_query );
623     i_result = Control( p_demux, i_query, args );
624     va_end( args );
625
626     return i_result;
627 }
628 /*****************************************************************************
629  * Demux:
630  *****************************************************************************/
631 static int Demux( demux_t *p_demux )
632 {
633     demux_sys_t *p_sys = p_demux->p_sys;
634
635     uint8_t buffer[DVD_VIDEO_LB_LEN];
636     uint8_t *packet = buffer;
637     int i_event;
638     int i_len;
639
640 #if DVD_READ_CACHE
641     if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
642         == DVDNAV_STATUS_ERR )
643 #else
644     if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
645         == DVDNAV_STATUS_ERR )
646 #endif
647     {
648         msg_Warn( p_demux, "cannot get next block (%s)",
649                   dvdnav_err_to_string( p_sys->dvdnav ) );
650         if( p_demux->info.i_title == 0 )
651         {
652             msg_Dbg( p_demux, "jumping to first title" );
653             return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
654         }
655         return -1;
656     }
657
658     switch( i_event )
659     {
660     case DVDNAV_BLOCK_OK:   /* mpeg block */
661         vlc_mutex_lock( &p_sys->still.lock );
662         vlc_timer_schedule( p_sys->still.timer, false, 0, 0 );
663         p_sys->still.b_enabled = false;
664         vlc_mutex_unlock( &p_sys->still.lock );
665         if( p_sys->b_reset_pcr )
666         {
667             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
668             p_sys->b_reset_pcr = false;
669         }
670         DemuxBlock( p_demux, packet, i_len );
671         if( p_sys->i_vobu_index > 0 )
672         {
673             if( p_sys->i_vobu_flush == p_sys->i_vobu_index )
674                 DemuxForceStill( p_demux );
675             p_sys->i_vobu_index++;
676         }
677         break;
678
679     case DVDNAV_NOP:    /* Nothing */
680         msg_Dbg( p_demux, "DVDNAV_NOP" );
681         break;
682
683     case DVDNAV_STILL_FRAME:
684     {
685         dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
686         bool b_still_init = false;
687
688         vlc_mutex_lock( &p_sys->still.lock );
689         if( !p_sys->still.b_enabled )
690         {
691             msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
692             msg_Dbg( p_demux, "     - length=0x%x", event->length );
693             p_sys->still.b_enabled = true;
694
695             if( event->length != 0xff && p_sys->still.b_created )
696             {
697                 mtime_t delay = event->length * CLOCK_FREQ;
698                 vlc_timer_schedule( p_sys->still.timer, false, delay, 0 );
699             }
700
701             b_still_init = true;
702         }
703         vlc_mutex_unlock( &p_sys->still.lock );
704
705         if( b_still_init )
706         {
707             DemuxForceStill( p_demux );
708             p_sys->b_reset_pcr = true;
709         }
710         msleep( 40000 );
711         break;
712     }
713
714     case DVDNAV_SPU_CLUT_CHANGE:
715     {
716         int i;
717
718         msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
719         /* Update color lookup table (16 *uint32_t in packet) */
720         memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
721
722         /* HACK to get the SPU tracks registered in the right order */
723         for( i = 0; i < 0x1f; i++ )
724         {
725             if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
726                 ESNew( p_demux, 0xbd20 + i );
727         }
728         /* END HACK */
729         break;
730     }
731
732     case DVDNAV_SPU_STREAM_CHANGE:
733     {
734         dvdnav_spu_stream_change_event_t *event =
735             (dvdnav_spu_stream_change_event_t*)packet;
736         int i;
737
738         msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
739         msg_Dbg( p_demux, "     - physical_wide=%d",
740                  event->physical_wide );
741         msg_Dbg( p_demux, "     - physical_letterbox=%d",
742                  event->physical_letterbox);
743         msg_Dbg( p_demux, "     - physical_pan_scan=%d",
744                  event->physical_pan_scan );
745
746         ESSubtitleUpdate( p_demux );
747         p_sys->b_spu_change = true;
748
749         /* HACK to get the SPU tracks registered in the right order */
750         for( i = 0; i < 0x1f; i++ )
751         {
752             if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
753                 ESNew( p_demux, 0xbd20 + i );
754         }
755         /* END HACK */
756         break;
757     }
758
759     case DVDNAV_AUDIO_STREAM_CHANGE:
760     {
761         dvdnav_audio_stream_change_event_t *event =
762             (dvdnav_audio_stream_change_event_t*)packet;
763         msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
764         msg_Dbg( p_demux, "     - physical=%d", event->physical );
765         /* TODO */
766         break;
767     }
768
769     case DVDNAV_VTS_CHANGE:
770     {
771         int32_t i_title = 0;
772         int32_t i_part  = 0;
773         int i;
774
775         dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
776         msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
777         msg_Dbg( p_demux, "     - vtsN=%d", event->new_vtsN );
778         msg_Dbg( p_demux, "     - domain=%d", event->new_domain );
779
780         /* reset PCR */
781         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
782
783         for( i = 0; i < PS_TK_COUNT; i++ )
784         {
785             ps_track_t *tk = &p_sys->tk[i];
786             if( tk->b_seen )
787             {
788                 es_format_Clean( &tk->fmt );
789                 if( tk->es ) es_out_Del( p_demux->out, tk->es );
790             }
791             tk->b_seen = false;
792         }
793
794         uint32_t i_width, i_height;
795         if( dvdnav_get_video_resolution( p_sys->dvdnav,
796                                          &i_width, &i_height ) )
797             i_width = i_height = 0;
798         switch( dvdnav_get_video_aspect( p_sys->dvdnav ) )
799         {
800         case 0:
801             p_sys->sar.i_num = 4 * i_height;
802             p_sys->sar.i_den = 3 * i_width;
803             break;
804         case 3:
805             p_sys->sar.i_num = 16 * i_height;
806             p_sys->sar.i_den =  9 * i_width;
807             break;
808         default:
809             p_sys->sar.i_num = 0;
810             p_sys->sar.i_den = 0;
811             break;
812         }
813
814         if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
815                                        &i_part ) == DVDNAV_STATUS_OK )
816         {
817             if( i_title >= 0 && i_title < p_sys->i_title &&
818                 p_demux->info.i_title != i_title )
819             {
820                 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
821                 p_demux->info.i_title = i_title;
822             }
823         }
824         break;
825     }
826
827     case DVDNAV_CELL_CHANGE:
828     {
829         int32_t i_title = 0;
830         int32_t i_part  = 0;
831
832         dvdnav_cell_change_event_t *event =
833             (dvdnav_cell_change_event_t*)packet;
834         msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
835         msg_Dbg( p_demux, "     - cellN=%d", event->cellN );
836         msg_Dbg( p_demux, "     - pgN=%d", event->pgN );
837         msg_Dbg( p_demux, "     - cell_length=%"PRId64, event->cell_length );
838         msg_Dbg( p_demux, "     - pg_length=%"PRId64, event->pg_length );
839         msg_Dbg( p_demux, "     - pgc_length=%"PRId64, event->pgc_length );
840         msg_Dbg( p_demux, "     - cell_start=%"PRId64, event->cell_start );
841         msg_Dbg( p_demux, "     - pg_start=%"PRId64, event->pg_start );
842
843         /* Store the lenght in time of the current PGC */
844         p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
845         p_sys->i_vobu_index = 0;
846         p_sys->i_vobu_flush = 0;
847
848         /* FIXME is it correct or there is better way to know chapter change */
849         if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
850                                        &i_part ) == DVDNAV_STATUS_OK )
851         {
852             if( i_title >= 0 && i_title < p_sys->i_title )
853             {
854                 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
855                 p_demux->info.i_title = i_title;
856
857                 if( i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint )
858                 {
859                     p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
860                     p_demux->info.i_seekpoint = i_part - 1;
861                 }
862             }
863         }
864         break;
865     }
866
867     case DVDNAV_NAV_PACKET:
868     {
869         p_sys->i_vobu_index = 1;
870         p_sys->i_vobu_flush = 0;
871
872         /* Look if we have need to force a flush (and when) */
873         const pci_gi_t *p_pci_gi = &dvdnav_get_current_nav_pci( p_sys->dvdnav )->pci_gi;
874         if( p_pci_gi->vobu_se_e_ptm != 0 && p_pci_gi->vobu_se_e_ptm < p_pci_gi->vobu_e_ptm )
875         {
876             const dsi_gi_t *p_dsi_gi = &dvdnav_get_current_nav_dsi( p_sys->dvdnav )->dsi_gi;
877             if( p_dsi_gi->vobu_3rdref_ea != 0 )
878                 p_sys->i_vobu_flush = p_dsi_gi->vobu_3rdref_ea;
879             else if( p_dsi_gi->vobu_2ndref_ea != 0 )
880                 p_sys->i_vobu_flush = p_dsi_gi->vobu_2ndref_ea;
881             else if( p_dsi_gi->vobu_1stref_ea != 0 )
882                 p_sys->i_vobu_flush = p_dsi_gi->vobu_1stref_ea;
883         }
884
885 #ifdef DVDNAV_DEBUG
886         msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
887 #endif
888         /* A lot of thing to do here :
889          *  - handle packet
890          *  - fetch pts (for time display)
891          *  - ...
892          */
893         DemuxBlock( p_demux, packet, i_len );
894         if( p_sys->b_spu_change )
895         {
896             ButtonUpdate( p_demux, false );
897             p_sys->b_spu_change = false;
898         }
899         break;
900     }
901
902     case DVDNAV_STOP:   /* EOF */
903         msg_Dbg( p_demux, "DVDNAV_STOP" );
904
905 #if DVD_READ_CACHE
906         dvdnav_free_cache_block( p_sys->dvdnav, packet );
907 #endif
908         return 0;
909
910     case DVDNAV_HIGHLIGHT:
911     {
912         dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
913         msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
914         msg_Dbg( p_demux, "     - display=%d", event->display );
915         msg_Dbg( p_demux, "     - buttonN=%d", event->buttonN );
916         ButtonUpdate( p_demux, false );
917         break;
918     }
919
920     case DVDNAV_HOP_CHANNEL:
921         msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
922         p_sys->i_vobu_index = 0;
923         p_sys->i_vobu_flush = 0;
924         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
925         break;
926
927     case DVDNAV_WAIT:
928         msg_Dbg( p_demux, "DVDNAV_WAIT" );
929
930         bool b_empty;
931         es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
932         if( !b_empty )
933         {
934             msleep( 40*1000 );
935         }
936         else
937         {
938             dvdnav_wait_skip( p_sys->dvdnav );
939             p_sys->b_reset_pcr = true;
940         }
941         break;
942
943     default:
944         msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
945         break;
946     }
947
948 #if DVD_READ_CACHE
949     dvdnav_free_cache_block( p_sys->dvdnav, packet );
950 #endif
951
952     return 1;
953 }
954
955 /* Get a 2 char code
956  * FIXME: partiallyy duplicated from src/input/es_out.c
957  */
958 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
959 {
960     const iso639_lang_t *pl;
961     char *psz_lang;
962     char *p;
963
964     psz_lang = var_CreateGetString( p_demux, psz_var );
965     if( !psz_lang )
966         return strdup(LANGUAGE_DEFAULT);
967
968     /* XXX: we will use only the first value
969      * (and ignore other ones in case of a list) */
970     if( ( p = strchr( psz_lang, ',' ) ) )
971         *p = '\0';
972
973     for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
974     {
975         if( *psz_lang == '\0' )
976             continue;
977         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
978             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
979             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
980             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
981             break;
982     }
983
984     free( psz_lang );
985
986     if( pl->psz_eng_name != NULL )
987         return strdup( pl->psz_iso639_1 );
988
989     return strdup(LANGUAGE_DEFAULT);
990 }
991
992 static void DemuxTitles( demux_t *p_demux )
993 {
994     demux_sys_t *p_sys = p_demux->p_sys;
995     input_title_t *t;
996     seekpoint_t *s;
997     int32_t i_titles;
998     int i;
999
1000     /* Menu */
1001     t = vlc_input_title_New();
1002     t->b_menu = true;
1003     t->psz_name = strdup( "DVD Menu" );
1004
1005     s = vlc_seekpoint_New();
1006     s->psz_name = strdup( "Resume" );
1007     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1008
1009     s = vlc_seekpoint_New();
1010     s->psz_name = strdup( "Root" );
1011     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1012
1013     s = vlc_seekpoint_New();
1014     s->psz_name = strdup( "Title" );
1015     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1016
1017     s = vlc_seekpoint_New();
1018     s->psz_name = strdup( "Chapter" );
1019     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1020
1021     s = vlc_seekpoint_New();
1022     s->psz_name = strdup( "Subtitle" );
1023     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1024
1025     s = vlc_seekpoint_New();
1026     s->psz_name = strdup( "Audio" );
1027     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1028
1029     s = vlc_seekpoint_New();
1030     s->psz_name = strdup( "Angle" );
1031     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1032
1033     TAB_APPEND( p_sys->i_title, p_sys->title, t );
1034
1035     /* Find out number of titles/chapters */
1036     dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
1037
1038     if( i_titles > 90 )
1039         msg_Err( p_demux, "This is probably an Arccos Protected DVD. This could take time..." );
1040
1041     for( i = 1; i <= i_titles; i++ )
1042     {
1043         int32_t i_chapters;
1044         uint64_t i_title_length;
1045         uint64_t *p_chapters_time;
1046
1047         i_chapters = dvdnav_describe_title_chapters( p_sys->dvdnav, i,
1048                                                      &p_chapters_time,
1049                                                      &i_title_length );
1050         if( i_chapters < 1 )
1051         {
1052             i_title_length = 0;
1053             p_chapters_time = NULL;
1054         }
1055         t = vlc_input_title_New();
1056         t->i_length = i_title_length * 1000 / 90;
1057         for( int j = 0; j < __MAX( i_chapters, 1 ); j++ )
1058         {
1059             s = vlc_seekpoint_New();
1060             if( p_chapters_time )
1061                 s->i_time_offset = p_chapters_time[j] * 1000 / 90;
1062             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1063         }
1064         free( p_chapters_time );
1065         TAB_APPEND( p_sys->i_title, p_sys->title, t );
1066     }
1067 }
1068
1069 /*****************************************************************************
1070  * Update functions:
1071  *****************************************************************************/
1072 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1073 {
1074     demux_sys_t *p_sys = p_demux->p_sys;
1075     int32_t i_title, i_part;
1076
1077     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1078
1079     dvdnav_highlight_area_t hl;
1080     int32_t i_button;
1081     bool    b_button_ok;
1082
1083     if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1084         != DVDNAV_STATUS_OK )
1085     {
1086         msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1087         return;
1088     }
1089
1090     b_button_ok = false;
1091     if( i_button > 0 && i_title ==  0 )
1092     {
1093         pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1094
1095         b_button_ok = DVDNAV_STATUS_OK ==
1096                   dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
1097     }
1098
1099     if( b_button_ok )
1100     {
1101         for( unsigned i = 0; i < 4; i++ )
1102         {
1103             uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1104             uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1105
1106             p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1107             p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1108             p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1109             p_sys->palette[i][3] = i_alpha;
1110         }
1111
1112         vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1113         var_SetInteger( p_sys->p_input, "x-start", hl.sx );
1114         var_SetInteger( p_sys->p_input, "x-end",  hl.ex );
1115         var_SetInteger( p_sys->p_input, "y-start", hl.sy );
1116         var_SetInteger( p_sys->p_input, "y-end", hl.ey );
1117
1118         var_SetAddress( p_sys->p_input, "menu-palette", p_sys->palette );
1119         var_SetBool( p_sys->p_input, "highlight", true );
1120
1121         msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1122     }
1123     else
1124     {
1125         msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1126                  i_button, i_title );
1127
1128         /* Show all */
1129         vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1130         var_SetBool( p_sys->p_input, "highlight", false );
1131     }
1132     vlc_global_unlock( VLC_HIGHLIGHT_MUTEX );
1133 }
1134
1135 static void ESSubtitleUpdate( demux_t *p_demux )
1136 {
1137     demux_sys_t *p_sys = p_demux->p_sys;
1138     int         i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1139     int32_t i_title, i_part;
1140
1141     ButtonUpdate( p_demux, false );
1142
1143     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1144     if( i_title > 0 ) return;
1145
1146     if( i_spu >= 0 && i_spu <= 0x1f )
1147     {
1148         ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1149
1150         ESNew( p_demux, 0xbd20 + i_spu );
1151
1152         /* be sure to unselect it (reset) */
1153         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1154                         (bool)false );
1155
1156         /* now select it */
1157         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1158     }
1159     else
1160     {
1161         for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1162         {
1163             ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1164             if( tk->b_seen )
1165             {
1166                 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1167                                 (bool)false );
1168             }
1169         }
1170     }
1171 }
1172
1173 /*****************************************************************************
1174  * DemuxBlock: demux a given block
1175  *****************************************************************************/
1176 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
1177 {
1178     demux_sys_t *p_sys = p_demux->p_sys;
1179
1180     while( len > 0 )
1181     {
1182         int i_size = ps_pkt_size( p, len );
1183         if( i_size <= 0 || i_size > len )
1184         {
1185             break;
1186         }
1187
1188         /* Create a block */
1189         block_t *p_pkt = block_Alloc( i_size );
1190         memcpy( p_pkt->p_buffer, p, i_size);
1191
1192         /* Parse it and send it */
1193         switch( 0x100 | p[3] )
1194         {
1195         case 0x1b9:
1196         case 0x1bb:
1197         case 0x1bc:
1198 #ifdef DVDNAV_DEBUG
1199             if( p[3] == 0xbc )
1200             {
1201                 msg_Warn( p_demux, "received a PSM packet" );
1202             }
1203             else if( p[3] == 0xbb )
1204             {
1205                 msg_Warn( p_demux, "received a SYSTEM packet" );
1206             }
1207 #endif
1208             block_Release( p_pkt );
1209             break;
1210
1211         case 0x1ba:
1212         {
1213             int64_t i_scr;
1214             int i_mux_rate;
1215             if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1216             {
1217                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1218                 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1219             }
1220             block_Release( p_pkt );
1221             break;
1222         }
1223         default:
1224         {
1225             int i_id = ps_pkt_id( p_pkt );
1226             if( i_id >= 0xc0 )
1227             {
1228                 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1229
1230                 if( !tk->b_seen )
1231                 {
1232                     ESNew( p_demux, i_id );
1233                 }
1234                 if( tk->b_seen && tk->es &&
1235                     !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1236                 {
1237                     es_out_Send( p_demux->out, tk->es, p_pkt );
1238                 }
1239                 else
1240                 {
1241                     block_Release( p_pkt );
1242                 }
1243             }
1244             else
1245             {
1246                 block_Release( p_pkt );
1247             }
1248             break;
1249         }
1250         }
1251
1252         p += i_size;
1253         len -= i_size;
1254     }
1255
1256     return VLC_SUCCESS;
1257 }
1258
1259 /*****************************************************************************
1260  * Force still images to be displayed by sending EOS and stopping buffering.
1261  *****************************************************************************/
1262 static void DemuxForceStill( demux_t *p_demux )
1263 {
1264     static const uint8_t buffer[] = {
1265         0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
1266         0x80, 0x00, 0x00,
1267         0x00, 0x00, 0x01, 0xB7,
1268     };
1269     DemuxBlock( p_demux, buffer, sizeof(buffer) );
1270
1271     bool b_empty;
1272     es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
1273 }
1274
1275 /*****************************************************************************
1276  * ESNew: register a new elementary stream
1277  *****************************************************************************/
1278 static void ESNew( demux_t *p_demux, int i_id )
1279 {
1280     demux_sys_t *p_sys = p_demux->p_sys;
1281     ps_track_t  *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1282     bool  b_select = false;
1283
1284     if( tk->b_seen ) return;
1285
1286     if( ps_track_fill( tk, 0, i_id, NULL ) )
1287     {
1288         msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1289         return;
1290     }
1291
1292     /* Add a new ES */
1293     if( tk->fmt.i_cat == VIDEO_ES )
1294     {
1295         tk->fmt.video.i_sar_num = p_sys->sar.i_num;
1296         tk->fmt.video.i_sar_den = p_sys->sar.i_den;
1297         b_select = true;
1298     }
1299     else if( tk->fmt.i_cat == AUDIO_ES )
1300     {
1301         int i_audio = -1;
1302         /* find the audio number PLEASE find another way */
1303         if( (i_id&0xbdf8) == 0xbd88 )       /* dts */
1304         {
1305             i_audio = i_id&0x07;
1306         }
1307         else if( (i_id&0xbdf0) == 0xbd80 )  /* a52 */
1308         {
1309             i_audio = i_id&0xf;
1310         }
1311         else if( (i_id&0xbdf0) == 0xbda0 )  /* lpcm */
1312         {
1313             i_audio = i_id&0x1f;
1314         }
1315         else if( ( i_id&0xe0 ) == 0xc0 )    /* mpga */
1316         {
1317             i_audio = i_id&0x1f;
1318         }
1319         if( i_audio >= 0 )
1320         {
1321             int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1322             if( i_lang != 0xffff )
1323             {
1324                 tk->fmt.psz_language = malloc( 3 );
1325                 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1326                 tk->fmt.psz_language[1] = (i_lang     )&0xff;
1327                 tk->fmt.psz_language[2] = 0;
1328             }
1329             if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1330             {
1331                 b_select = true;
1332             }
1333         }
1334     }
1335     else if( tk->fmt.i_cat == SPU_ES )
1336     {
1337         int32_t i_title, i_part;
1338         int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1339         if( i_lang != 0xffff )
1340         {
1341             tk->fmt.psz_language = malloc( 3 );
1342             tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1343             tk->fmt.psz_language[1] = (i_lang     )&0xff;
1344             tk->fmt.psz_language[2] = 0;
1345         }
1346
1347         /* Palette */
1348         tk->fmt.subs.spu.palette[0] = 0xBeef;
1349         memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1350                 16 * sizeof( uint32_t ) );
1351
1352         /* We select only when we are not in the menu */
1353         dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1354         if( i_title > 0 &&
1355             dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1356         {
1357             b_select = true;
1358         }
1359     }
1360
1361     tk->es = es_out_Add( p_demux->out, &tk->fmt );
1362     if( b_select )
1363     {
1364         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1365     }
1366     tk->b_seen = true;
1367
1368     if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1369 }
1370
1371 /*****************************************************************************
1372  * Still image end
1373  *****************************************************************************/
1374 static void StillTimer( void *p_data )
1375 {
1376     demux_sys_t    *p_sys = p_data;
1377
1378     vlc_mutex_lock( &p_sys->still.lock );
1379     if( likely(p_sys->still.b_enabled) )
1380     {
1381         p_sys->still.b_enabled = false;
1382         dvdnav_still_skip( p_sys->dvdnav );
1383     }
1384     vlc_mutex_unlock( &p_sys->still.lock );
1385 }
1386
1387 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1388                        vlc_value_t oldval, vlc_value_t val, void *p_data )
1389 {
1390     demux_t *p_demux = p_data;
1391     demux_sys_t *p_sys = p_demux->p_sys;
1392
1393     /* FIXME? PCI usage thread safe? */
1394     pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1395     int x = val.coords.x;
1396     int y = val.coords.y;
1397
1398     if( psz_var[6] == 'm' ) /* mouse-moved */
1399         dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1400     else
1401     {
1402         assert( psz_var[6] == 'c' ); /* mouse-clicked */
1403
1404         ButtonUpdate( p_demux, true );
1405         dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1406     }
1407     (void)p_vout;
1408     (void)oldval;
1409     return VLC_SUCCESS;
1410 }
1411
1412 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1413                       vlc_value_t oldval, vlc_value_t val, void *p_data )
1414 {
1415     demux_t *p_demux = p_data;
1416     demux_sys_t *p_sys = p_demux->p_sys;
1417
1418     if (val.i_int == INPUT_EVENT_VOUT)
1419     {
1420         if( p_sys->p_vout != NULL )
1421         {
1422             var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
1423             var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
1424             vlc_object_release( p_sys->p_vout );
1425         }
1426
1427         p_sys->p_vout = input_GetVout( (input_thread_t *)p_input );
1428         if( p_sys->p_vout != NULL )
1429         {
1430             var_AddCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
1431             var_AddCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
1432         }
1433     }
1434     (void) psz_var; (void) oldval;
1435     return VLC_SUCCESS;
1436 }
1437
1438 /*****************************************************************************
1439  * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1440  *****************************************************************************/
1441 static int ProbeDVD( const char *psz_name )
1442 {
1443     if( !*psz_name )
1444         /* Triggers libdvdcss autodetection */
1445         return VLC_SUCCESS;
1446
1447     int fd = vlc_open( psz_name, O_RDONLY | O_NONBLOCK );
1448     if( fd == -1 )
1449 #ifdef HAVE_FDOPENDIR
1450         return VLC_EGENERIC;
1451 #else
1452         return (errno == ENOENT) ? VLC_EGENERIC : VLC_SUCCESS;
1453 #endif
1454
1455     int ret = VLC_EGENERIC;
1456     struct stat stat_info;
1457
1458     if( fstat( fd, &stat_info ) == -1 )
1459          goto bailout;
1460     if( !S_ISREG( stat_info.st_mode ) )
1461     {
1462         if( S_ISDIR( stat_info.st_mode ) || S_ISBLK( stat_info.st_mode ) )
1463             ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1464         goto bailout;
1465     }
1466
1467     /* ISO 9660 volume descriptor */
1468     char iso_dsc[6];
1469     if( lseek( fd, 0x8000 + 1, SEEK_SET ) == -1
1470      || read( fd, iso_dsc, sizeof (iso_dsc) ) < (int)sizeof (iso_dsc)
1471      || memcmp( iso_dsc, "CD001\x01", 6 ) )
1472         goto bailout;
1473
1474     /* Try to find the anchor (2 bytes at LBA 256) */
1475     uint16_t anchor;
1476
1477     if( lseek( fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1478      && read( fd, &anchor, 2 ) == 2
1479      && GetWLE( &anchor ) == 2 )
1480         ret = VLC_SUCCESS; /* Found a potential anchor */
1481 bailout:
1482     close( fd );
1483     return ret;
1484 }