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