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