]> git.sesse.net Git - vlc/blob - modules/demux/dvdnav.c
* Massive spelling corrections.
[vlc] / modules / demux / dvdnav.c
1 /*****************************************************************************
2  * dvdnav.c: DVD module using the dvdnav library.
3  *****************************************************************************
4  * Copyright (C) 2004 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
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "vlc_keys.h"
33 #include "iso_lang.h"
34
35 #include <dvdnav/dvdnav.h>
36
37 #include "ps.h"
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 #define CACHING_TEXT N_("caching value in ms")
43 #define CACHING_LONGTEXT N_( \
44     "Allows you to modify the default caching value for DVDnav streams. This "\
45     "value should be set in millisecond units." )
46
47 static int  AccessOpen ( vlc_object_t * );
48 static void AccessClose( vlc_object_t * );
49
50 static int  DemuxOpen ( vlc_object_t * );
51 static void DemuxClose( vlc_object_t * );
52
53 vlc_module_begin();
54     set_description( _("DVDnav Input") );
55     add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
56         CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
57     set_capability( "access", 0 );
58     add_shortcut( "dvdnav" );
59     add_shortcut( "dvdnavsimple" );
60     set_callbacks( AccessOpen, AccessClose );
61
62     add_submodule();
63         set_description( _("DVDnav Input (demux)") );
64         set_capability( "demux2", 0 );
65         add_shortcut( "dvdnav" );
66         add_shortcut( "dvdnavsimple" );
67         set_callbacks( DemuxOpen, DemuxClose );
68 vlc_module_end();
69
70 /* TODO
71  *  - use dvdnav_get_next_cache_block/dvdnav_free_cache_block
72  *  - all
73  *  - once done the new input API remove the pseudo access
74  *  - ...
75  */
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 static ssize_t AccessRead( input_thread_t *, byte_t *, size_t ); /* dummy */
81 static char *ParseCL( vlc_object_t *, char *, vlc_bool_t, int *, int *, int *);
82
83 typedef struct
84 {
85     VLC_COMMON_MEMBERS
86
87     demux_t        *p_demux;
88     vlc_mutex_t     lock;
89
90     vlc_bool_t      b_moved;
91     vlc_bool_t      b_clicked;
92     vlc_bool_t      b_key;
93
94     vlc_bool_t      b_still;
95     int64_t         i_still_end;
96
97 } event_thread_t;
98
99 static int EventThread( vlc_object_t * );
100
101 struct demux_sys_t
102 {
103     dvdnav_t    *dvdnav;
104
105     /* track */
106     ps_track_t  tk[PS_TK_COUNT];
107
108     /* for spu variables */
109     input_thread_t *p_input;
110
111     /* event */
112     event_thread_t *p_ev;
113
114     /* FIXME */
115     uint8_t     alpha[4];
116     uint32_t    clut[16];
117
118     /* */
119     int i_aspect;
120
121     /* */
122     vlc_bool_t  b_es_out_ok;
123     vlc_bool_t  b_simple;
124 };
125
126 static int DemuxControl( demux_t *, int, va_list );
127 static int DemuxDemux  ( demux_t * );
128 static int DemuxBlock  ( demux_t *, uint8_t *pkt, int i_pkt );
129
130 enum
131 {
132     AR_SQUARE_PICTURE = 1,                          /* square pixels */
133     AR_3_4_PICTURE    = 2,                       /* 3:4 picture (TV) */
134     AR_16_9_PICTURE   = 3,             /* 16:9 picture (wide screen) */
135     AR_221_1_PICTURE  = 4,                 /* 2.21:1 picture (movie) */
136 };
137
138 static int MenusCallback( vlc_object_t *, char const *,
139                           vlc_value_t, vlc_value_t, void * );
140
141 static void ESSubtitleUpdate( demux_t * );
142 static void ButtonUpdate( demux_t * );
143
144 /*****************************************************************************
145  * Open: see if dvdnav can handle the input and if so force dvdnav demux
146  *****************************************************************************
147  * For now it has to be like that (ie open dvdnav twice) but will be fixed
148  * when a demux can work without an access module.
149  *****************************************************************************/
150 static int AccessOpen( vlc_object_t *p_this )
151 {
152     input_thread_t *p_input = (input_thread_t *)p_this;
153     dvdnav_t       *dvdnav;
154     int            i_title, i_chapter, i_angle;
155     char           *psz_name;
156     vlc_value_t    val;
157     vlc_bool_t     b_force;
158
159     /* We try only if we are forced */
160     if( p_input->psz_demux && *p_input->psz_demux &&
161         strncmp( p_input->psz_demux, "dvdnav", 6 ) )
162     {
163         msg_Warn( p_input, "dvdnav access discarded (demuxer forced)" );
164         return VLC_EGENERIC;
165     }
166
167     b_force = p_input->psz_access &&
168               !strncmp( p_input->psz_access, "dvdnav", 6 );
169
170     psz_name = ParseCL( VLC_OBJECT(p_input), p_input->psz_name, b_force,
171                         &i_title, &i_chapter, &i_angle );
172     if( !psz_name )
173     {
174         return VLC_EGENERIC;
175     }
176
177     /* Test dvdnav */
178     if( dvdnav_open( &dvdnav, psz_name ) != DVDNAV_STATUS_OK )
179     {
180         msg_Warn( p_input, "cannot open dvdnav" );
181         free( psz_name );
182         return VLC_EGENERIC;
183     }
184     dvdnav_close( dvdnav );
185     free( psz_name );
186
187     /* Fill p_input fields */
188     p_input->pf_read = AccessRead;
189     p_input->pf_set_program = input_SetProgram;
190     p_input->pf_set_area = NULL;
191     p_input->pf_seek = NULL;
192
193     vlc_mutex_lock( &p_input->stream.stream_lock );
194     p_input->stream.b_pace_control = VLC_TRUE;
195     p_input->stream.b_seekable = VLC_TRUE;
196     p_input->stream.p_selected_area->i_tell = 0;
197     p_input->stream.p_selected_area->i_size = 0;
198     p_input->stream.i_method = INPUT_METHOD_DVD;
199     vlc_mutex_unlock( &p_input->stream.stream_lock );
200     p_input->i_mtu = 0;
201
202     /* Force dvdnav demux */
203     if( p_input->psz_access && !strncmp(p_input->psz_access, "dvdnav", 6 ) )
204         p_input->psz_demux = strdup( p_input->psz_access );
205     else
206         p_input->psz_demux = strdup( "dvdnav" );
207
208     /* Update default_pts to a suitable value for udp access */
209     var_Create( p_input, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
210     var_Get( p_input, "dvdnav-caching", &val );
211     p_input->i_pts_delay = val.i_int * 1000;
212
213     return VLC_SUCCESS;
214 }
215
216 /*****************************************************************************
217  * Close: free unused data structures
218  *****************************************************************************/
219 static void AccessClose( vlc_object_t *p_this )
220 {
221 }
222
223 /*****************************************************************************
224  * Read: Should not be called (ie not used by the dvdnav demuxer)
225  *****************************************************************************/
226 static ssize_t AccessRead( input_thread_t *p_input, byte_t *p_buffer,
227                            size_t i_len )
228 {
229     memset( p_buffer, 0, i_len );
230     return i_len;
231 }
232
233 /*****************************************************************************
234  * ParseCL: parse command line
235  *****************************************************************************/
236 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
237                       int *i_title, int *i_chapter, int *i_angle )
238 {
239     char *psz_parser, *psz_source, *psz_next;
240
241     psz_source = strdup( psz_name );
242     if( psz_source == NULL ) return NULL;
243
244     *i_title = 0;
245     *i_chapter = 1;
246     *i_angle = 1;
247
248     /* Start with the end, because you could have :
249      * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
250      * (yes, this is kludgy). */
251     for( psz_parser = psz_source + strlen(psz_source) - 1;
252          psz_parser >= psz_source && *psz_parser != '@';
253          psz_parser-- );
254
255     if( psz_parser >= psz_source && *psz_parser == '@' )
256     {
257         /* Found options */
258         *psz_parser = '\0';
259         ++psz_parser;
260
261         *i_title = (int)strtol( psz_parser, &psz_next, 10 );
262         if( *psz_next )
263         {
264             psz_parser = psz_next + 1;
265             *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
266             if( *psz_next )
267             {
268                 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
269             }
270         }
271     }
272
273     *i_title   = *i_title >= 0 ? *i_title : 0;
274     *i_chapter = *i_chapter    ? *i_chapter : 1;
275     *i_angle   = *i_angle      ? *i_angle : 1;
276
277     if( !*psz_source )
278     {
279         free( psz_source );
280         if( !b_force )
281         {
282             return NULL;
283         }
284         psz_source = config_GetPsz( p_this, "dvd" );
285         if( !psz_source ) return NULL;
286     }
287
288     msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
289              psz_source, *i_title, *i_chapter, *i_angle );
290
291     return psz_source;
292 }
293
294 /*****************************************************************************
295  * DemuxOpen:
296  *****************************************************************************/
297 static int DemuxOpen( vlc_object_t *p_this )
298 {
299     demux_t     *p_demux = (demux_t*)p_this;
300     demux_sys_t *p_sys;
301     vlc_value_t val, text;
302     int         i_title, i_titles, i_chapter, i_chapters, i_angle, i;
303     char        *psz_name;
304
305     if( strncmp( p_demux->psz_access, "dvdnav", 6 ) ||
306         strncmp( p_demux->psz_demux,  "dvdnav", 6 ) )
307     {
308         msg_Warn( p_demux, "dvdnav module discarded" );
309         return VLC_EGENERIC;
310     }
311
312     psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
313                         &i_title, &i_chapter, &i_angle );
314     if( !psz_name )
315     {
316         return VLC_EGENERIC;
317     }
318
319     /* Init p_sys */
320     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
321     memset( p_sys, 0, sizeof( demux_sys_t ) );
322
323     p_sys->b_simple =
324         strcmp( p_demux->psz_access, "dvdnavsimple" ) ? VLC_FALSE : VLC_TRUE;
325     if( p_sys->b_simple && i_title < 1 )
326     {
327         /* Skip menu part */
328         i_title = 1;
329     }
330
331     ps_track_init( p_sys->tk );
332     p_sys->i_aspect = -1;
333     p_sys->b_es_out_ok = VLC_FALSE;
334
335     /* Open dvdnav */
336     if( dvdnav_open( &p_sys->dvdnav, psz_name ) != DVDNAV_STATUS_OK )
337     {
338         msg_Warn( p_demux, "cannot open dvdnav" );
339         free( psz_name );
340         return VLC_EGENERIC;
341     }
342     free( psz_name );
343
344     /* Configure dvdnav */
345     if( dvdnav_set_readahead_flag( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
346     {
347         msg_Warn( p_demux, "cannot set read-a-head flag" );
348     }
349
350     if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
351           DVDNAV_STATUS_OK )
352     {
353         msg_Warn( p_demux, "cannot set PGC positioning flag" );
354     }
355
356     if( dvdnav_menu_language_select ( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK||
357         dvdnav_audio_language_select( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK||
358         dvdnav_spu_language_select  ( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
359     {
360         msg_Warn( p_demux, "something failed while setting en language (%s)",
361                   dvdnav_err_to_string( p_sys->dvdnav ) );
362     }
363
364     /* Find out number of titles/chapters */
365     dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
366     for( i = 1; i <= i_titles; i++ )
367     {
368         i_chapters = 0;
369         dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
370     }
371
372     if( dvdnav_title_play( p_sys->dvdnav, i_title ) !=
373           DVDNAV_STATUS_OK )
374     {
375         msg_Warn( p_demux, "cannot set title/chapter" );
376     }
377     if( !p_sys->b_simple )
378     {
379         /* Get p_input and create variable */
380         p_sys->p_input =
381             vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_ANYWHERE );
382         var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
383         var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
384         var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
385         var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
386         var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
387         var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
388         var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
389         var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
390
391         /* Create a few object variables used for navigation in the interfaces */
392         var_Create( p_sys->p_input, "dvd_menus",
393                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
394         text.psz_string = _("DVD menus");
395         var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_SETTEXT, &text, NULL );
396         var_AddCallback( p_sys->p_input, "dvd_menus", MenusCallback, p_demux );
397         val.i_int = DVD_MENU_Escape; text.psz_string = _("Resume");
398         var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
399         val.i_int = DVD_MENU_Root; text.psz_string = _("Root");
400         var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
401         val.i_int = DVD_MENU_Title; text.psz_string = _("Title");
402         var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
403         val.i_int = DVD_MENU_Part; text.psz_string = _("Chapter");
404         var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
405         val.i_int = DVD_MENU_Subpicture; text.psz_string = _("Subtitle");
406         var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
407         val.i_int = DVD_MENU_Audio; text.psz_string = _("Audio");
408         var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
409         val.i_int = DVD_MENU_Angle; text.psz_string = _("Angle");
410         var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
411
412     /* Now create our event thread catcher */
413         p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
414         p_sys->p_ev->p_demux = p_demux;
415         vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
416                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
417     }
418
419     /* fill p_demux field */
420     p_demux->pf_control = DemuxControl;
421     p_demux->pf_demux = DemuxDemux;
422
423     return VLC_SUCCESS;
424 }
425
426 /*****************************************************************************
427  * DemuxClose:
428  *****************************************************************************/
429 static void DemuxClose( vlc_object_t *p_this )
430 {
431     demux_t     *p_demux = (demux_t*)p_this;
432     demux_sys_t *p_sys = p_demux->p_sys;
433
434     if( !p_sys->b_simple )
435     {
436         /* stop the event handler */
437         p_sys->p_ev->b_die = VLC_TRUE;
438         vlc_thread_join( p_sys->p_ev );
439         vlc_object_destroy( p_sys->p_ev );
440
441         var_Destroy( p_sys->p_input, "highlight-mutex" );
442         var_Destroy( p_sys->p_input, "highlight" );
443         var_Destroy( p_sys->p_input, "x-start" );
444         var_Destroy( p_sys->p_input, "x-end" );
445         var_Destroy( p_sys->p_input, "y-start" );
446         var_Destroy( p_sys->p_input, "y-end" );
447         var_Destroy( p_sys->p_input, "color" );
448         var_Destroy( p_sys->p_input, "contrast" );
449
450         vlc_object_release( p_sys->p_input );
451     }
452
453     dvdnav_close( p_sys->dvdnav );
454     free( p_sys );
455 }
456
457 /*****************************************************************************
458  * DemuxControl:
459  *****************************************************************************/
460 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
461 {
462     demux_sys_t *p_sys = p_demux->p_sys;
463     double f, *pf;
464
465     switch( i_query )
466     {
467         case DEMUX_GET_POSITION:
468         {
469             uint32_t pos, len;
470             pf = (double*) va_arg( args, double* );
471             if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) ==
472                   DVDNAV_STATUS_OK && len > 0 )
473             {
474                 *pf = (double)pos / (double)len;
475             }
476             else
477             {
478                 *pf = 0.0;
479             }
480             return VLC_SUCCESS;
481         }
482         case DEMUX_SET_POSITION:
483         {
484             uint32_t pos, len;
485             f = (double) va_arg( args, double );
486             if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) ==
487                   DVDNAV_STATUS_OK && len > 0 )
488             {
489                 pos = f * len;
490                 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
491                       DVDNAV_STATUS_OK )
492                 {
493                     return VLC_SUCCESS;
494                 }
495             }
496             return VLC_EGENERIC;
497         }
498
499         /* TODO implement others */
500         default:
501             return VLC_EGENERIC;
502     }
503 }
504
505 /*****************************************************************************
506  * DemuxDemux:
507  *****************************************************************************/
508 static int DemuxDemux( demux_t *p_demux )
509 {
510     demux_sys_t *p_sys = p_demux->p_sys;
511
512     uint8_t packet[DVD_VIDEO_LB_LEN];
513     int i_event;
514     int i_len;
515
516     if( !p_sys->b_es_out_ok )
517     {
518         if( !p_sys->b_simple )
519         {
520             /* We do ourself the selection/unselection
521              * Problem: bypass --audio-channel and --spu-channel
522              * Solution: call ourself dvdnav_??set_channel -> TODO
523              */
524             es_out_Control( p_demux->out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
525         }
526
527         p_sys->b_es_out_ok = VLC_TRUE;
528     }
529
530     if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len ) ==
531           DVDNAV_STATUS_ERR )
532     {
533         msg_Warn( p_demux, "cannot get next block (%s)",
534                   dvdnav_err_to_string( p_sys->dvdnav ) );
535         return -1;
536     }
537
538     switch( i_event )
539     {
540     case DVDNAV_BLOCK_OK:   /* mpeg block */
541         DemuxBlock( p_demux, packet, i_len );
542         break;
543
544     case DVDNAV_NOP:    /* Nothing */
545         msg_Dbg( p_demux, "DVDNAV_NOP" );
546         break;
547
548     case DVDNAV_STILL_FRAME:
549     {
550         dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
551         if( !p_sys->b_simple )
552         {
553             vlc_mutex_lock( &p_sys->p_ev->lock );
554             if( !p_sys->p_ev->b_still )
555             {
556                 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
557                 msg_Dbg( p_demux, "     - length=0x%x", event->length );
558                 p_sys->p_ev->b_still = VLC_TRUE;
559                 if( event->length == 0xff )
560                 {
561                     p_sys->p_ev->i_still_end = 0;
562                 }
563                 else
564                 {
565                     p_sys->p_ev->i_still_end = (int64_t)event->length *
566                         1000000 + mdate() + p_sys->p_input->i_pts_delay;
567                 }
568             }
569             vlc_mutex_unlock( &p_sys->p_ev->lock );
570             msleep( 40000 );
571         }
572         else
573         {
574             dvdnav_still_skip( p_sys->dvdnav );
575         }
576         break;
577     }
578     case DVDNAV_SPU_STREAM_CHANGE:
579     {
580         dvdnav_spu_stream_change_event_t *event =
581             (dvdnav_spu_stream_change_event_t*)packet;
582         msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
583         msg_Dbg( p_demux, "     - physical_wide=%d",
584                  event->physical_wide );
585         msg_Dbg( p_demux, "     - physical_letterbox=%d",
586                  event->physical_letterbox);
587         msg_Dbg( p_demux, "     - physical_pan_scan=%d",
588                  event->physical_pan_scan );
589
590         if( !p_sys->b_simple )
591         {
592             ESSubtitleUpdate( p_demux );
593         }
594         break;
595     }
596     case DVDNAV_AUDIO_STREAM_CHANGE:
597     {
598         dvdnav_audio_stream_change_event_t *event =
599             (dvdnav_audio_stream_change_event_t*)packet;
600         msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
601         msg_Dbg( p_demux, "     - physical=%d", event->physical );
602         /* TODO */
603         break;
604     }
605     case DVDNAV_VTS_CHANGE:
606     {
607         int i;
608         dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
609         msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
610         msg_Dbg( p_demux, "     - vtsN=%d", event->new_vtsN );
611         msg_Dbg( p_demux, "     - domain=%d", event->new_domain );
612
613         /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
614         /* TODO check if we alsways have VTS and CELL */
615         p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
616
617         /* reset PCR */
618         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
619
620         for( i = 0; i < PS_TK_COUNT; i++ )
621         {
622             ps_track_t *tk = &p_sys->tk[i];
623             if( tk->b_seen && tk->es )
624             {
625                 es_out_Del( p_demux->out, tk->es );
626             }
627             tk->b_seen = VLC_FALSE;
628         }
629
630         if( p_sys->b_simple )
631         {
632             int32_t i_title = 0;
633             int32_t i_part  = 0;
634             if( dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part ) == DVDNAV_STATUS_OK )
635             {
636                 if( i_title == 0 )
637                 {
638                     /* we have returned in menu, stop dvd */
639                     /* FIXME is it the right way ? */
640                     return 0;
641                 }
642             }
643         }
644         break;
645     }
646     case DVDNAV_CELL_CHANGE:
647     {
648         dvdnav_cell_change_event_t *event =
649             (dvdnav_cell_change_event_t*)packet;
650         msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
651         msg_Dbg( p_demux, "     - cellN=%d", event->cellN );
652         msg_Dbg( p_demux, "     - pgN=%d", event->pgN );
653         msg_Dbg( p_demux, "     - cell_length=%lld", event->cell_length );
654         msg_Dbg( p_demux, "     - pg_length=%lld", event->pg_length );
655         msg_Dbg( p_demux, "     - pgc_length=%lld", event->pgc_length );
656         msg_Dbg( p_demux, "     - cell_start=%lld", event->cell_start );
657         msg_Dbg( p_demux, "     - pg_start=%lld", event->pg_start );
658         break;
659     }
660     case DVDNAV_NAV_PACKET:
661     {
662         msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
663         /* A lot of thing to do here :
664          *  - handle packet
665          *  - fetch pts (for time display)
666          *  - ...
667          */
668         DemuxBlock( p_demux, packet, i_len );
669         break;
670     }
671     case DVDNAV_STOP:   /* EOF */
672         msg_Dbg( p_demux, "DVDNAV_STOP" );
673         return 0;
674
675     case DVDNAV_HIGHLIGHT:
676     {
677         dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
678         msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
679         msg_Dbg( p_demux, "     - display=%d", event->display );
680         msg_Dbg( p_demux, "     - buttonN=%d", event->buttonN );
681         if( !p_sys->b_simple )
682         {
683             ButtonUpdate( p_demux );
684         }
685         break;
686     }
687
688     case DVDNAV_SPU_CLUT_CHANGE:
689         msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
690         /* Update color lookup table (16 *uint32_t in packet) */
691         memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
692         break;
693
694     case DVDNAV_HOP_CHANNEL:
695         msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
696         /* We should try to flush all our internal buffer */
697         break;
698
699     case DVDNAV_WAIT:
700         msg_Dbg( p_demux, "DVDNAV_WAIT" );
701
702         dvdnav_wait_skip( p_sys->dvdnav );
703         break;
704
705     default:
706         msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
707         break;
708     }
709
710     return 1;
711 }
712 /*****************************************************************************
713  * Update functions:
714  *****************************************************************************/
715 static void ButtonUpdate( demux_t *p_demux )
716 {
717     demux_sys_t *p_sys = p_demux->p_sys;
718     vlc_value_t val;
719     int32_t i_title, i_part;
720
721     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
722
723     if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
724     {
725         vlc_mutex_t *p_mutex = val.p_address;
726         dvdnav_highlight_area_t hl;
727         int32_t i_button;
728
729         if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button ) != DVDNAV_STATUS_OK )
730         {
731             msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
732             return;
733         }
734
735         if( i_button > 0 && i_title ==  0 )
736         {
737             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
738
739             dvdnav_get_highlight_area( pci, i_button, 1, &hl );
740
741             /* I fear it is plain wrong */
742             //val.p_address = (void *)&hl.palette;
743             p_sys->alpha[0] = hl.palette&0x0f;
744             p_sys->alpha[1] = (hl.palette>>4)&0x0f;
745             p_sys->alpha[2] = (hl.palette>>8)&0x0f;
746             p_sys->alpha[3] = (hl.palette>>12)&0x0f;
747
748             vlc_mutex_lock( p_mutex );
749             val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
750             val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
751             val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
752             val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
753
754             val.p_address = (void *)p_sys->alpha;
755             var_Set( p_sys->p_input, "contrast", val );
756
757             val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
758             vlc_mutex_unlock( p_mutex );
759
760             msg_Dbg( p_demux, "buttonUpdate %d", i_button );
761         }
762         else
763         {
764             msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d", i_button, i_title );
765
766             /* Show all */
767             vlc_mutex_lock( p_mutex );
768             val.b_bool = VLC_FALSE; var_Set( p_sys->p_input, "highlight", val );
769             vlc_mutex_unlock( p_mutex );
770         }
771     }
772 }
773
774 static void ESNew( demux_t *p_demux, int i_id );
775
776 static void ESSubtitleUpdate( demux_t *p_demux )
777 {
778     demux_sys_t *p_sys = p_demux->p_sys;
779     int         i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
780     int32_t i_title, i_part;
781
782     ButtonUpdate( p_demux );
783
784     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
785     if( i_title > 0 )
786     {
787         return;
788     }
789
790     if( i_spu >= 0 && i_spu <= 0x1f )
791     {
792         ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
793
794         if( !tk->b_seen )
795         {
796             ESNew( p_demux, 0xbd20 + i_spu);
797         }
798         /* be sure to unselect it (reset) */
799         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
800                         (vlc_bool_t)VLC_FALSE );
801
802         /* now select it */
803         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
804     }
805     else
806     {
807         for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
808         {
809             ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
810             if( tk->b_seen )
811             {
812                 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
813                                 (vlc_bool_t)VLC_FALSE );
814             }
815         }
816     }
817 }
818
819
820 /*****************************************************************************
821  * DemuxBlock: demux a given block
822  *****************************************************************************/
823 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
824 {
825     demux_sys_t *p_sys = p_demux->p_sys;
826     uint8_t     *p = pkt;
827
828     while( p < &pkt[i_pkt] )
829     {
830         int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
831         block_t *p_pkt;
832         if( i_size <= 0 )
833         {
834             break;
835         }
836
837         /* Create a block */
838         p_pkt = block_New( p_demux, i_size );
839         memcpy( p_pkt->p_buffer, p, i_size);
840
841         /* Parse it and send it */
842         switch( 0x100 | p[3] )
843         {
844         case 0x1b9:
845         case 0x1bb:
846         case 0x1bc:
847             if( p[3] == 0xbc )
848             {
849                 msg_Warn( p_demux, "received a PSM packet" );
850             }
851             else if( p[3] == 0xbb )
852             {
853                 msg_Warn( p_demux, "received a SYSTEM packet" );
854             }
855             block_Release( p_pkt );
856             break;
857
858         case 0x1ba:
859         {
860             int64_t i_scr;
861             int i_mux_rate;
862             if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
863             {
864                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
865             }
866             block_Release( p_pkt );
867             break;
868         }
869         default:
870         {
871             int i_id = ps_pkt_id( p_pkt );
872             if( i_id >= 0xc0 )
873             {
874                 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
875
876                 if( !tk->b_seen )
877                 {
878                     ESNew( p_demux, i_id );
879                 }
880                 if( tk->b_seen && tk->es &&
881                     !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
882                 {
883                     es_out_Send( p_demux->out, tk->es, p_pkt );
884                 }
885                 else
886                 {
887                     block_Release( p_pkt );
888                 }
889             }
890             else
891             {
892                 block_Release( p_pkt );
893             }
894             break;
895         }
896         }
897
898         p += i_size;
899     }
900
901     return VLC_SUCCESS;
902 }
903
904 /*****************************************************************************
905  * ESNew: register a new elementary stream
906  *****************************************************************************/
907 static void ESNew( demux_t *p_demux, int i_id )
908 {
909     demux_sys_t *p_sys = p_demux->p_sys;
910     ps_track_t  *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
911     vlc_bool_t  b_select = VLC_FALSE;
912
913     if( tk->b_seen )
914     {
915         return;
916     }
917
918     if( ps_track_fill( tk, i_id ) )
919     {
920         msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
921         return;
922     }
923
924     /* Add a new ES */
925     if( tk->fmt.i_cat == VIDEO_ES )
926     {
927         if( p_sys->i_aspect >= 0 )
928         {
929             tk->fmt.video.i_aspect = p_sys->i_aspect;
930         }
931         b_select = VLC_TRUE;
932     }
933     else if( tk->fmt.i_cat == AUDIO_ES )
934     {
935         int i_audio = -1;
936         /* find the audio number PLEASE find another way */
937         if( (i_id&0xbdf8) == 0xbd88 )       /* dts */
938         {
939             i_audio = i_id&0x07;
940         }
941         else if( (i_id&0xbdf0) == 0xbd80 )  /* a52 */
942         {
943             i_audio = i_id&0xf;
944         }
945         else if( (i_id&0xbdf0) == 0xbda0 )  /* lpcm */
946         {
947             i_audio = i_id&0x1f;
948         }
949         else if( ( i_id&0xe0 ) == 0xc0 )    /* mpga */
950         {
951             i_audio = i_id&0x1f;
952         }
953         if( i_audio >= 0 )
954         {
955             int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
956             if( i_lang != 0xffff )
957             {
958                 tk->fmt.psz_language = malloc( 3 );
959                 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
960                 tk->fmt.psz_language[1] = (i_lang     )&0xff;
961                 tk->fmt.psz_language[2] = 0;
962             }
963             if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
964             {
965                 b_select = VLC_TRUE;
966             }
967         }
968     }
969     else if( tk->fmt.i_cat == SPU_ES )
970     {
971         int32_t i_title, i_part;
972         int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
973         if( i_lang != 0xffff )
974         {
975             tk->fmt.psz_language = malloc( 3 );
976             tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
977             tk->fmt.psz_language[1] = (i_lang     )&0xff;
978             tk->fmt.psz_language[2] = 0;
979         }
980
981         /* Palette */
982         tk->fmt.subs.spu.palette[0] = 0xBeef;
983         memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
984                 16 * sizeof( uint32_t ) );
985
986         /* We select only when we are not in the menu */
987         dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
988         if( i_title > 0 &&
989             dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
990         {
991             b_select = VLC_TRUE;
992         }
993     }
994
995     tk->es = es_out_Add( p_demux->out, &tk->fmt );
996     if( b_select )
997     {
998         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
999     }
1000     tk->b_seen = VLC_TRUE;
1001 }
1002
1003 /*****************************************************************************
1004  * Event handler code
1005  *****************************************************************************/
1006 static int  EventMouse( vlc_object_t *, char const *,
1007                         vlc_value_t, vlc_value_t, void * );
1008 static int  EventKey  ( vlc_object_t *, char const *,
1009                         vlc_value_t, vlc_value_t, void * );
1010
1011 static int EventThread( vlc_object_t *p_this )
1012 {
1013     event_thread_t *p_ev = (event_thread_t*)p_this;
1014     demux_sys_t    *p_sys = p_ev->p_demux->p_sys;
1015     vlc_object_t   *p_vout = NULL;
1016
1017     vlc_mutex_init( p_ev, &p_ev->lock );
1018     p_ev->b_moved   = VLC_FALSE;
1019     p_ev->b_clicked = VLC_FALSE;
1020     p_ev->b_key     = VLC_FALSE;
1021
1022     p_ev->b_still   = VLC_FALSE;
1023
1024     /* catch all key event */
1025     var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1026
1027     /* main loop */
1028     while( !p_ev->b_die )
1029     {
1030         vlc_bool_t b_activated = VLC_FALSE;
1031
1032         /* KEY part */
1033         if( p_ev->b_key )
1034         {
1035             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1036
1037             vlc_value_t valk;
1038             struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1039             int i, i_action = -1;
1040
1041             vlc_mutex_lock( &p_ev->lock );
1042             var_Get( p_ev->p_vlc, "key-pressed", &valk );
1043             for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1044             {
1045                 if( p_hotkeys[i].i_key == valk.i_int )
1046                 {
1047                     i_action = p_hotkeys[i].i_action;
1048                 }
1049             }
1050
1051             switch( i_action )
1052             {
1053             case ACTIONID_NAV_LEFT:
1054                 dvdnav_left_button_select( p_sys->dvdnav, pci );
1055                 break;
1056             case ACTIONID_NAV_RIGHT:
1057                 dvdnav_right_button_select( p_sys->dvdnav, pci );
1058                 break;
1059             case ACTIONID_NAV_UP:
1060                 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1061                 break;
1062             case ACTIONID_NAV_DOWN:
1063                 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1064                 break;
1065             case ACTIONID_NAV_ACTIVATE:
1066                 b_activated = VLC_TRUE;
1067                 dvdnav_button_activate( p_sys->dvdnav, pci );
1068                 break;
1069             default:
1070                 break;
1071             }
1072             p_ev->b_key = VLC_FALSE;
1073             vlc_mutex_unlock( &p_ev->lock );
1074         }
1075
1076         /* VOUT part */
1077         if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1078         {
1079             pci_t       *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1080             vlc_value_t valx, valy;
1081
1082             vlc_mutex_lock( &p_ev->lock );
1083             var_Get( p_vout, "mouse-x", &valx );
1084             var_Get( p_vout, "mouse-y", &valy );
1085
1086             if( p_ev->b_moved )
1087             {
1088                 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1089                                      valy.i_int );
1090             }
1091             if( p_ev->b_clicked )
1092             {
1093                 b_activated = VLC_TRUE;
1094                 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1095                                        valy.i_int );
1096             }
1097
1098             p_ev->b_moved = VLC_FALSE;
1099             p_ev->b_clicked = VLC_FALSE;
1100             vlc_mutex_unlock( &p_ev->lock );
1101         }
1102         if( p_vout && p_vout->b_die )
1103         {
1104             var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1105             var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1106             vlc_object_release( p_vout );
1107             p_vout = NULL;
1108         }
1109         if( p_vout == NULL )
1110         {
1111             p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1112                                       FIND_CHILD );
1113             if( p_vout)
1114             {
1115                 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1116                 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1117             }
1118         }
1119
1120         /* Still part */
1121         vlc_mutex_lock( &p_ev->lock );
1122         if( p_ev->b_still )
1123         {
1124             if( /* b_activated || // This breaks menus */
1125                 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1126             {
1127                 p_ev->b_still = VLC_FALSE;
1128                 dvdnav_still_skip( p_sys->dvdnav );
1129             }
1130         }
1131         vlc_mutex_unlock( &p_ev->lock );
1132
1133         /* Wait a bit */
1134         msleep( 10000 );
1135     }
1136
1137     /* Release callback */
1138     if( p_vout )
1139     {
1140         var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1141         var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1142         vlc_object_release( p_vout );
1143     }
1144     var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1145
1146     vlc_mutex_destroy( &p_ev->lock );
1147
1148     return VLC_SUCCESS;
1149 }
1150
1151 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1152                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1153 {
1154     event_thread_t *p_ev = p_data;
1155     vlc_mutex_lock( &p_ev->lock );
1156     if( psz_var[6] == 'c' )
1157         p_ev->b_clicked = VLC_TRUE;
1158     else if( psz_var[6] == 'm' )
1159         p_ev->b_moved = VLC_TRUE;
1160     vlc_mutex_unlock( &p_ev->lock );
1161
1162     return VLC_SUCCESS;
1163 }
1164
1165 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1166                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1167 {
1168     event_thread_t *p_ev = p_data;
1169     vlc_mutex_lock( &p_ev->lock );
1170     p_ev->b_key = VLC_TRUE;
1171     vlc_mutex_unlock( &p_ev->lock );
1172
1173     return VLC_SUCCESS;
1174 }
1175
1176 static int MenusCallback( vlc_object_t *p_this, char const *psz_name,
1177                           vlc_value_t oldval, vlc_value_t newval, void *p_arg )
1178 {
1179     demux_t *p_demux = (demux_t*)p_arg;
1180
1181     /* FIXME, not thread safe */
1182     dvdnav_menu_call( p_demux->p_sys->dvdnav, newval.i_int );
1183
1184     return VLC_SUCCESS;
1185 }