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