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