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