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