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