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