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