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