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