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