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