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