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