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