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