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