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