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