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