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