dvdnav: use ES_OUT_SET_VOUT_MOUSE_EVENT
[vlc.git] / modules / access / dvdnav.c
blob1c23352f8de90380e0204216b534642a81efb028
1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * NOTA BENE: this module requires the linking against a library which is
26 * known to require licensing under the GNU General Public License version 2
27 * (or later). Therefore, the result of compiling this module will normally
28 * be subject to the terms of that later license.
29 *****************************************************************************/
32 /*****************************************************************************
33 * Preamble
34 *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
40 #include <assert.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <unistd.h> /* close() */
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
48 #include <vlc_input.h>
49 #include <vlc_access.h>
50 #include <vlc_demux.h>
51 #include <vlc_charset.h>
52 #include <vlc_fs.h>
53 #include <vlc_mouse.h>
54 #include <vlc_dialog.h>
55 #include <vlc_iso_lang.h>
57 /* FIXME we should find a better way than including that */
58 #include "../../src/text/iso-639_def.h"
61 #include <dvdnav/dvdnav.h>
63 #include "../demux/mpeg/pes.h"
64 #include "../demux/mpeg/ps.h"
66 /*****************************************************************************
67 * Module descriptor
68 *****************************************************************************/
69 #define ANGLE_TEXT N_("DVD angle")
70 #define ANGLE_LONGTEXT N_( \
71 "Default DVD angle." )
73 #define MENU_TEXT N_("Start directly in menu")
74 #define MENU_LONGTEXT N_( \
75 "Start the DVD directly in the main menu. This "\
76 "will try to skip all the useless warning introductions." )
78 #define LANGUAGE_DEFAULT ("en")
80 static int AccessDemuxOpen ( vlc_object_t * );
81 static void Close( vlc_object_t * );
83 #if DVDREAD_VERSION >= 50300 && defined( HAVE_STREAM_CB_IN_DVDNAV_H )
84 #define HAVE_DVDNAV_DEMUX
85 static int DemuxOpen ( vlc_object_t * );
86 #endif
88 vlc_module_begin ()
89 set_shortname( N_("DVD with menus") )
90 set_description( N_("DVDnav Input") )
91 set_category( CAT_INPUT )
92 set_subcategory( SUBCAT_INPUT_ACCESS )
93 add_integer( "dvdnav-angle", 1, ANGLE_TEXT,
94 ANGLE_LONGTEXT, false )
95 add_bool( "dvdnav-menu", true,
96 MENU_TEXT, MENU_LONGTEXT, false )
97 set_capability( "access", 305 )
98 add_shortcut( "dvd", "dvdnav", "file" )
99 set_callbacks( AccessDemuxOpen, Close )
100 #ifdef HAVE_DVDNAV_DEMUX
101 add_submodule()
102 set_description( N_("DVDnav demuxer") )
103 set_category( CAT_INPUT )
104 set_subcategory( SUBCAT_INPUT_DEMUX )
105 set_capability( "demux", 5 )
106 set_callbacks( DemuxOpen, Close )
107 add_shortcut( "dvd", "iso" )
108 #endif
109 vlc_module_end ()
111 /* Shall we use libdvdnav's read ahead cache? */
112 #ifdef __OS2__
113 #define DVD_READ_CACHE 0
114 #else
115 #define DVD_READ_CACHE 1
116 #endif
118 #define BLOCK_FLAG_CELL_DISCONTINUITY (BLOCK_FLAG_PRIVATE_SHIFT << 1)
120 /*****************************************************************************
121 * Local prototypes
122 *****************************************************************************/
123 typedef struct
125 dvdnav_t *dvdnav;
127 /* */
128 bool b_reset_pcr;
129 bool b_readahead;
131 struct
133 bool b_created;
134 bool b_enabled;
135 vlc_mutex_t lock;
136 vlc_timer_t timer;
137 } still;
139 /* track */
140 ps_track_t tk[PS_TK_COUNT];
141 int i_mux_rate;
143 /* palette for menus */
144 uint32_t clut[16];
145 uint8_t palette[4][4];
146 bool b_spu_change;
148 /* Aspect ration */
149 struct {
150 unsigned i_num;
151 unsigned i_den;
152 } sar;
154 /* */
155 int i_title;
156 input_title_t **title;
157 int cur_title;
158 int cur_seekpoint;
159 unsigned updates;
161 /* length of program group chain */
162 vlc_tick_t i_pgc_length;
163 int i_vobu_index;
164 int i_vobu_flush;
166 vlc_mouse_t oldmouse;
167 } demux_sys_t;
169 static int Control( demux_t *, int, va_list );
170 static int Demux( demux_t * );
171 static int DemuxBlock( demux_t *, const uint8_t *, int );
172 static void DemuxForceStill( demux_t * );
174 static void DemuxTitles( demux_t * );
175 static void ESSubtitleUpdate( demux_t * );
176 static void ButtonUpdate( demux_t *, bool );
178 static void ESNew( demux_t *, int );
179 static int ProbeDVD( const char * );
181 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
183 static int ControlInternal( demux_t *, int, ... );
185 static void StillTimer( void * );
187 static void EventMouse( const vlc_mouse_t *mouse, void *p_data );
189 /*****************************************************************************
190 * CommonOpen:
191 *****************************************************************************/
192 static int CommonOpen( vlc_object_t *p_this,
193 dvdnav_t *p_dvdnav, bool b_readahead )
195 demux_t *p_demux = (demux_t*)p_this;
196 demux_sys_t *p_sys;
197 int i_angle;
198 char *psz_code;
200 assert( p_dvdnav );
202 /* Fill p_demux field */
203 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
204 p_sys->dvdnav = p_dvdnav;
205 p_sys->b_reset_pcr = false;
207 ps_track_init( p_sys->tk );
208 p_sys->sar.i_num = 0;
209 p_sys->sar.i_den = 0;
210 p_sys->i_mux_rate = 0;
211 p_sys->i_pgc_length = 0;
212 p_sys->b_spu_change = false;
213 p_sys->i_vobu_index = 0;
214 p_sys->i_vobu_flush = 0;
215 p_sys->b_readahead = b_readahead;
216 vlc_mouse_Init( &p_sys->oldmouse );
218 if( 1 )
220 // Hack for libdvdnav CVS.
221 // Without it dvdnav_get_number_of_titles() fails.
222 // Remove when fixed in libdvdnav CVS.
223 uint8_t buffer[DVD_VIDEO_LB_LEN];
224 int i_event, i_len;
226 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
227 == DVDNAV_STATUS_ERR )
229 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
232 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
235 /* Configure dvdnav */
236 if( dvdnav_set_readahead_flag( p_sys->dvdnav, p_sys->b_readahead ) !=
237 DVDNAV_STATUS_OK )
239 msg_Warn( p_demux, "cannot set read-a-head flag" );
242 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
243 DVDNAV_STATUS_OK )
245 msg_Warn( p_demux, "cannot set PGC positioning flag" );
248 /* Set menu language */
249 psz_code = DemuxGetLanguageCode( p_demux, "menu-language" );
250 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
251 DVDNAV_STATUS_OK )
253 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
254 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
255 /* We try to fall back to 'en' */
256 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
257 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
259 free( psz_code );
261 /* Set audio language */
262 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
263 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
264 DVDNAV_STATUS_OK )
266 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
267 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
268 /* We try to fall back to 'en' */
269 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
270 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
272 free( psz_code );
274 /* Set spu language */
275 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
276 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
277 DVDNAV_STATUS_OK )
279 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
280 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
281 /* We try to fall back to 'en' */
282 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
283 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
285 free( psz_code );
287 DemuxTitles( p_demux );
289 if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
291 msg_Dbg( p_demux, "trying to go to dvd menu" );
293 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
295 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
296 vlc_dialog_display_error( p_demux, _("Playback failure"), "%s",
297 _("VLC cannot set the DVD's title. It possibly "
298 "cannot decrypt the entire disc.") );
299 free( p_sys );
300 return VLC_EGENERIC;
303 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
304 DVDNAV_STATUS_OK )
306 /* Try going to menu root */
307 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
308 DVDNAV_STATUS_OK )
309 msg_Warn( p_demux, "cannot go to dvd menu" );
313 i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
314 if( i_angle <= 0 ) i_angle = 1;
316 /* FIXME hack hack hack hack FIXME */
317 /* Get p_input and create variable */
318 var_Create( p_demux->p_input, "x-start", VLC_VAR_INTEGER );
319 var_Create( p_demux->p_input, "y-start", VLC_VAR_INTEGER );
320 var_Create( p_demux->p_input, "x-end", VLC_VAR_INTEGER );
321 var_Create( p_demux->p_input, "y-end", VLC_VAR_INTEGER );
322 var_Create( p_demux->p_input, "color", VLC_VAR_ADDRESS );
323 var_Create( p_demux->p_input, "menu-palette", VLC_VAR_ADDRESS );
324 var_Create( p_demux->p_input, "highlight", VLC_VAR_BOOL );
326 p_sys->still.b_enabled = false;
327 vlc_mutex_init( &p_sys->still.lock );
328 if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
329 p_sys->still.b_created = true;
331 return VLC_SUCCESS;
334 /*****************************************************************************
335 * AccessDemuxOpen:
336 *****************************************************************************/
337 static int AccessDemuxOpen ( vlc_object_t *p_this )
339 demux_t *p_demux = (demux_t*)p_this;
340 dvdnav_t *p_dvdnav = NULL;
341 char *psz_file = NULL;
342 const char *psz_path = NULL;
343 int i_ret = VLC_EGENERIC;
344 bool forced = false;
346 if (p_demux->out == NULL)
347 return VLC_EGENERIC;
349 if( !strncasecmp(p_demux->psz_url, "dvd", 3) )
350 forced = true;
352 if( !p_demux->psz_filepath || !*p_demux->psz_filepath )
354 /* Only when selected */
355 if( !forced )
356 return VLC_EGENERIC;
358 psz_file = var_InheritString( p_this, "dvd" );
360 else
361 psz_file = strdup( p_demux->psz_filepath );
363 #if defined( _WIN32 ) || defined( __OS2__ )
364 if( psz_file != NULL )
366 /* Remove trailing backslash, otherwise dvdnav_open will fail */
367 size_t flen = strlen( psz_file );
368 if( flen > 0 && psz_file[flen - 1] == '\\' )
369 psz_file[flen - 1] = '\0';
371 else
372 psz_file = strdup("");
373 #endif
375 if( unlikely(psz_file == NULL) )
376 return VLC_EGENERIC;
378 /* Try some simple probing to avoid going through dvdnav_open too often */
379 if( !forced && ProbeDVD( psz_file ) != VLC_SUCCESS )
380 goto bailout;
382 /* Open dvdnav */
383 psz_path = ToLocale( psz_file );
384 if( dvdnav_open( &p_dvdnav, psz_path ) != DVDNAV_STATUS_OK )
386 msg_Warn( p_demux, "cannot open DVD (%s)", psz_file);
387 goto bailout;
390 i_ret = CommonOpen( p_this, p_dvdnav, !!DVD_READ_CACHE );
391 if( i_ret != VLC_SUCCESS )
392 dvdnav_close( p_dvdnav );
394 bailout:
395 free( psz_file );
396 if( psz_path )
397 LocaleFree( psz_path );
398 return i_ret;
401 #ifdef HAVE_DVDNAV_DEMUX
402 /*****************************************************************************
403 * StreamProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
404 *****************************************************************************/
405 static int StreamProbeDVD( stream_t *s )
407 /* first sector should be filled with zeros */
408 ssize_t i_peek;
409 const uint8_t *p_peek;
410 i_peek = vlc_stream_Peek( s, &p_peek, 2048 );
411 if( i_peek < 512 ) {
412 return VLC_EGENERIC;
414 while (i_peek > 0) {
415 if (p_peek[ --i_peek ]) {
416 return VLC_EGENERIC;
420 /* ISO 9660 volume descriptor */
421 char iso_dsc[6];
422 if( vlc_stream_Seek( s, 0x8000 + 1 ) != VLC_SUCCESS
423 || vlc_stream_Read( s, iso_dsc, sizeof (iso_dsc) ) < (int)sizeof (iso_dsc)
424 || memcmp( iso_dsc, "CD001\x01", 6 ) )
425 return VLC_EGENERIC;
427 /* Try to find the anchor (2 bytes at LBA 256) */
428 uint16_t anchor;
430 if( vlc_stream_Seek( s, 256 * DVD_VIDEO_LB_LEN ) == VLC_SUCCESS
431 && vlc_stream_Read( s, &anchor, 2 ) == 2
432 && GetWLE( &anchor ) == 2 )
433 return VLC_SUCCESS;
434 else
435 return VLC_EGENERIC;
438 /*****************************************************************************
439 * dvdnav stream callbacks
440 *****************************************************************************/
441 static int stream_cb_seek( void *s, uint64_t pos )
443 return vlc_stream_Seek( (stream_t *)s, pos );
446 static int stream_cb_read( void *s, void* buffer, int size )
448 return vlc_stream_Read( (stream_t *)s, buffer, size );
451 /*****************************************************************************
452 * DemuxOpen:
453 *****************************************************************************/
454 static int DemuxOpen ( vlc_object_t *p_this )
456 demux_t *p_demux = (demux_t*)p_this;
457 dvdnav_t *p_dvdnav = NULL;
458 bool forced = false, b_seekable = false;
460 if( p_demux->psz_name != NULL && !strncmp(p_demux->psz_name, "dvd", 3) )
461 forced = true;
463 /* StreamProbeDVD need FASTSEEK, but if dvd is forced, we don't probe thus
464 * don't need fastseek */
465 vlc_stream_Control( p_demux->s, forced ? STREAM_CAN_SEEK : STREAM_CAN_FASTSEEK,
466 &b_seekable );
467 if( !b_seekable )
468 return VLC_EGENERIC;
470 /* Try some simple probing to avoid going through dvdnav_open too often */
471 if( !forced && StreamProbeDVD( p_demux->s ) != VLC_SUCCESS )
472 return VLC_EGENERIC;
474 static dvdnav_stream_cb stream_cb =
476 .pf_seek = stream_cb_seek,
477 .pf_read = stream_cb_read,
478 .pf_readv = NULL,
481 /* Open dvdnav with stream callbacks */
482 if( dvdnav_open_stream( &p_dvdnav, p_demux->s,
483 &stream_cb ) != DVDNAV_STATUS_OK )
485 msg_Warn( p_demux, "cannot open DVD with open_stream" );
486 return VLC_EGENERIC;
489 int i_ret = CommonOpen( p_this, p_dvdnav, false );
490 if( i_ret != VLC_SUCCESS )
491 dvdnav_close( p_dvdnav );
492 return i_ret;
494 #endif
496 /*****************************************************************************
497 * Close:
498 *****************************************************************************/
499 static void Close( vlc_object_t *p_this )
501 demux_t *p_demux = (demux_t*)p_this;
502 demux_sys_t *p_sys = p_demux->p_sys;
504 /* Stop still image handler */
505 if( p_sys->still.b_created )
506 vlc_timer_destroy( p_sys->still.timer );
507 vlc_mutex_destroy( &p_sys->still.lock );
509 var_Destroy( p_demux->p_input, "highlight" );
510 var_Destroy( p_demux->p_input, "x-start" );
511 var_Destroy( p_demux->p_input, "x-end" );
512 var_Destroy( p_demux->p_input, "y-start" );
513 var_Destroy( p_demux->p_input, "y-end" );
514 var_Destroy( p_demux->p_input, "color" );
515 var_Destroy( p_demux->p_input, "menu-palette" );
517 for( int i = 0; i < PS_TK_COUNT; i++ )
519 ps_track_t *tk = &p_sys->tk[i];
520 if( tk->b_configured )
522 es_format_Clean( &tk->fmt );
523 if( tk->es ) es_out_Del( p_demux->out, tk->es );
527 /* Free the array of titles */
528 for( int i = 0; i < p_sys->i_title; i++ )
529 vlc_input_title_Delete( p_sys->title[i] );
530 TAB_CLEAN( p_sys->i_title, p_sys->title );
532 dvdnav_close( p_sys->dvdnav );
533 free( p_sys );
536 /*****************************************************************************
537 * Control:
538 *****************************************************************************/
539 static int Control( demux_t *p_demux, int i_query, va_list args )
541 demux_sys_t *p_sys = p_demux->p_sys;
542 input_title_t ***ppp_title;
543 int i;
545 switch( i_query )
547 case DEMUX_SET_POSITION:
548 case DEMUX_GET_POSITION:
549 case DEMUX_GET_TIME:
550 case DEMUX_GET_LENGTH:
552 uint32_t pos, len;
553 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
554 DVDNAV_STATUS_OK || len == 0 )
556 return VLC_EGENERIC;
559 switch( i_query )
561 case DEMUX_GET_POSITION:
562 *va_arg( args, double* ) = (double)pos / (double)len;
563 return VLC_SUCCESS;
565 case DEMUX_SET_POSITION:
566 pos = va_arg( args, double ) * len;
567 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
568 DVDNAV_STATUS_OK )
570 return VLC_SUCCESS;
572 break;
574 case DEMUX_GET_TIME:
575 if( p_sys->i_pgc_length > 0 )
577 *va_arg( args, int64_t * ) = p_sys->i_pgc_length*pos/len;
578 return VLC_SUCCESS;
580 break;
582 case DEMUX_GET_LENGTH:
583 if( p_sys->i_pgc_length > 0 )
585 *va_arg( args, int64_t * ) = (int64_t)p_sys->i_pgc_length;
586 return VLC_SUCCESS;
588 break;
590 return VLC_EGENERIC;
593 /* Special for access_demux */
594 case DEMUX_CAN_PAUSE:
595 case DEMUX_CAN_SEEK:
596 case DEMUX_CAN_CONTROL_PACE:
597 /* TODO */
598 *va_arg( args, bool * ) = true;
599 return VLC_SUCCESS;
601 case DEMUX_SET_PAUSE_STATE:
602 return VLC_SUCCESS;
604 case DEMUX_GET_TITLE_INFO:
605 ppp_title = va_arg( args, input_title_t*** );
606 *va_arg( args, int* ) = p_sys->i_title;
607 *va_arg( args, int* ) = 0; /* Title offset */
608 *va_arg( args, int* ) = 1; /* Chapter offset */
610 /* Duplicate title infos */
611 *ppp_title = vlc_alloc( p_sys->i_title, sizeof( input_title_t * ) );
612 for( i = 0; i < p_sys->i_title; i++ )
614 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
616 return VLC_SUCCESS;
618 case DEMUX_SET_TITLE:
619 i = va_arg( args, int );
620 if( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
621 != DVDNAV_STATUS_OK )
623 msg_Warn( p_demux, "cannot set title/chapter" );
624 return VLC_EGENERIC;
627 if( i != 0 )
629 dvdnav_still_skip( p_sys->dvdnav );
630 if( dvdnav_title_play( p_sys->dvdnav, i ) != DVDNAV_STATUS_OK )
632 msg_Warn( p_demux, "cannot set title/chapter" );
633 return VLC_EGENERIC;
637 p_sys->updates |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
638 p_sys->cur_title = i;
639 p_sys->cur_seekpoint = 0;
640 return VLC_SUCCESS;
642 case DEMUX_SET_SEEKPOINT:
643 i = va_arg( args, int );
644 if( p_sys->cur_title == 0 )
646 static const int argtab[] = {
647 DVD_MENU_Escape,
648 DVD_MENU_Root,
649 DVD_MENU_Title,
650 DVD_MENU_Part,
651 DVD_MENU_Subpicture,
652 DVD_MENU_Audio,
653 DVD_MENU_Angle
655 enum { numargs = sizeof(argtab)/sizeof(int) };
656 if( (unsigned)i >= numargs || DVDNAV_STATUS_OK !=
657 dvdnav_menu_call(p_sys->dvdnav,argtab[i]) )
658 return VLC_EGENERIC;
660 else if( dvdnav_part_play( p_sys->dvdnav, p_sys->cur_title,
661 i + 1 ) != DVDNAV_STATUS_OK )
663 msg_Warn( p_demux, "cannot set title/chapter" );
664 return VLC_EGENERIC;
666 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
667 p_sys->cur_seekpoint = i;
668 return VLC_SUCCESS;
670 case DEMUX_TEST_AND_CLEAR_FLAGS:
672 unsigned *restrict flags = va_arg(args, unsigned *);
673 *flags &= p_sys->updates;
674 p_sys->updates &= ~*flags;
675 break;
678 case DEMUX_GET_TITLE:
679 *va_arg( args, int * ) = p_sys->cur_title;
680 break;
682 case DEMUX_GET_SEEKPOINT:
683 *va_arg( args, int * ) = p_sys->cur_seekpoint;
684 break;
686 case DEMUX_GET_PTS_DELAY:
687 *va_arg( args, vlc_tick_t * ) =
688 VLC_TICK_FROM_MS( var_InheritInteger( p_demux, "disc-caching" ) );
689 return VLC_SUCCESS;
691 case DEMUX_GET_META:
693 const char *title_name = NULL;
695 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
696 if( (NULL != title_name) && ('\0' != title_name[0]) )
698 vlc_meta_t *p_meta = va_arg( args, vlc_meta_t* );
699 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
700 return VLC_SUCCESS;
702 return VLC_EGENERIC;
705 case DEMUX_NAV_ACTIVATE:
707 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
709 if( dvdnav_button_activate( p_sys->dvdnav, pci ) != DVDNAV_STATUS_OK )
710 return VLC_EGENERIC;
711 ButtonUpdate( p_demux, true );
712 break;
715 case DEMUX_NAV_UP:
717 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
719 if( dvdnav_upper_button_select( p_sys->dvdnav, pci ) != DVDNAV_STATUS_OK )
720 return VLC_EGENERIC;
721 break;
724 case DEMUX_NAV_DOWN:
726 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
728 if( dvdnav_lower_button_select( p_sys->dvdnav, pci ) != DVDNAV_STATUS_OK )
729 return VLC_EGENERIC;
730 break;
733 case DEMUX_NAV_LEFT:
735 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
737 if( dvdnav_left_button_select( p_sys->dvdnav, pci ) != DVDNAV_STATUS_OK )
738 return VLC_EGENERIC;
739 break;
742 case DEMUX_NAV_RIGHT:
744 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
746 if( dvdnav_right_button_select( p_sys->dvdnav, pci ) != DVDNAV_STATUS_OK )
747 return VLC_EGENERIC;
748 break;
751 case DEMUX_NAV_MENU:
753 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title )
754 != DVDNAV_STATUS_OK )
756 msg_Warn( p_demux, "cannot select Title menu" );
757 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
758 != DVDNAV_STATUS_OK )
760 msg_Warn( p_demux, "cannot select Root menu" );
761 return VLC_EGENERIC;
764 p_sys->updates |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
765 p_sys->cur_title = 0;
766 p_sys->cur_seekpoint = 2;
767 break;
770 /* TODO implement others */
771 default:
772 return VLC_EGENERIC;
775 return VLC_SUCCESS;
778 static int ControlInternal( demux_t *p_demux, int i_query, ... )
780 va_list args;
781 int i_result;
783 va_start( args, i_query );
784 i_result = Control( p_demux, i_query, args );
785 va_end( args );
787 return i_result;
789 /*****************************************************************************
790 * Demux:
791 *****************************************************************************/
792 static int Demux( demux_t *p_demux )
794 demux_sys_t *p_sys = p_demux->p_sys;
796 uint8_t buffer[DVD_VIDEO_LB_LEN];
797 uint8_t *packet = buffer;
798 int i_event;
799 int i_len;
800 dvdnav_status_t status;
802 if( p_sys->b_readahead )
803 status = dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event,
804 &i_len );
805 else
806 status = dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event,
807 &i_len );
808 if( status == DVDNAV_STATUS_ERR )
810 msg_Warn( p_demux, "cannot get next block (%s)",
811 dvdnav_err_to_string( p_sys->dvdnav ) );
812 if( p_sys->cur_title == 0 )
814 msg_Dbg( p_demux, "jumping to first title" );
815 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
817 return -1;
820 switch( i_event )
822 case DVDNAV_BLOCK_OK: /* mpeg block */
823 vlc_mutex_lock( &p_sys->still.lock );
824 vlc_timer_disarm( p_sys->still.timer );
825 p_sys->still.b_enabled = false;
826 vlc_mutex_unlock( &p_sys->still.lock );
827 if( p_sys->b_reset_pcr )
829 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
830 p_sys->b_reset_pcr = false;
832 DemuxBlock( p_demux, packet, i_len );
833 if( p_sys->i_vobu_index > 0 )
835 if( p_sys->i_vobu_flush == p_sys->i_vobu_index )
836 DemuxForceStill( p_demux );
837 p_sys->i_vobu_index++;
839 break;
841 case DVDNAV_NOP: /* Nothing */
842 msg_Dbg( p_demux, "DVDNAV_NOP" );
843 break;
845 case DVDNAV_STILL_FRAME:
847 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
848 bool b_still_init = false;
850 vlc_mutex_lock( &p_sys->still.lock );
851 if( !p_sys->still.b_enabled )
853 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
854 msg_Dbg( p_demux, " - length=0x%x", event->length );
855 p_sys->still.b_enabled = true;
857 if( event->length != 0xff && p_sys->still.b_created )
859 vlc_tick_t delay = vlc_tick_from_sec( event->length );
860 vlc_timer_schedule( p_sys->still.timer, false, delay, VLC_TIMER_FIRE_ONCE );
863 b_still_init = true;
865 vlc_mutex_unlock( &p_sys->still.lock );
867 if( b_still_init )
869 DemuxForceStill( p_demux );
870 p_sys->b_reset_pcr = true;
872 vlc_tick_sleep( VLC_TICK_FROM_MS(40) );
873 break;
876 case DVDNAV_SPU_CLUT_CHANGE:
878 int i;
880 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
881 /* Update color lookup table (16 *uint32_t in packet) */
882 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
884 /* HACK to get the SPU tracks registered in the right order */
885 for( i = 0; i < 0x1f; i++ )
887 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
888 ESNew( p_demux, 0xbd20 + i );
890 /* END HACK */
891 break;
894 case DVDNAV_SPU_STREAM_CHANGE:
896 dvdnav_spu_stream_change_event_t *event =
897 (dvdnav_spu_stream_change_event_t*)packet;
898 int i;
900 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
901 msg_Dbg( p_demux, " - physical_wide=%d",
902 event->physical_wide );
903 msg_Dbg( p_demux, " - physical_letterbox=%d",
904 event->physical_letterbox);
905 msg_Dbg( p_demux, " - physical_pan_scan=%d",
906 event->physical_pan_scan );
908 ESSubtitleUpdate( p_demux );
909 p_sys->b_spu_change = true;
911 /* HACK to get the SPU tracks registered in the right order */
912 for( i = 0; i < 0x1f; i++ )
914 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
915 ESNew( p_demux, 0xbd20 + i );
917 /* END HACK */
918 break;
921 case DVDNAV_AUDIO_STREAM_CHANGE:
923 dvdnav_audio_stream_change_event_t *event =
924 (dvdnav_audio_stream_change_event_t*)packet;
925 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
926 msg_Dbg( p_demux, " - physical=%d", event->physical );
927 /* TODO */
928 break;
931 case DVDNAV_VTS_CHANGE:
933 int32_t i_title = 0;
934 int32_t i_part = 0;
936 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
937 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
938 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
939 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
941 /* reset PCR */
942 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
944 for( int i = 0; i < PS_TK_COUNT; i++ )
946 ps_track_t *tk = &p_sys->tk[i];
947 if( tk->b_configured )
949 es_format_Clean( &tk->fmt );
950 if( tk->es ) es_out_Del( p_demux->out, tk->es );
952 tk->b_configured = false;
955 uint32_t i_width, i_height;
956 if( dvdnav_get_video_resolution( p_sys->dvdnav,
957 &i_width, &i_height ) )
958 i_width = i_height = 0;
959 switch( dvdnav_get_video_aspect( p_sys->dvdnav ) )
961 case 0:
962 p_sys->sar.i_num = 4 * i_height;
963 p_sys->sar.i_den = 3 * i_width;
964 break;
965 case 3:
966 p_sys->sar.i_num = 16 * i_height;
967 p_sys->sar.i_den = 9 * i_width;
968 break;
969 default:
970 p_sys->sar.i_num = 0;
971 p_sys->sar.i_den = 0;
972 break;
975 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
976 &i_part ) == DVDNAV_STATUS_OK )
978 if( i_title >= 0 && i_title < p_sys->i_title &&
979 p_sys->cur_title != i_title )
981 p_sys->updates |= INPUT_UPDATE_TITLE;
982 p_sys->cur_title = i_title;
985 break;
988 case DVDNAV_CELL_CHANGE:
990 int32_t i_title = 0;
991 int32_t i_part = 0;
993 dvdnav_cell_change_event_t *event =
994 (dvdnav_cell_change_event_t*)packet;
995 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
996 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
997 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
998 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
999 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
1000 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
1001 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
1002 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
1004 /* Store the length in time of the current PGC */
1005 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
1006 p_sys->i_vobu_index = 0;
1007 p_sys->i_vobu_flush = 0;
1009 for( int i=0; i<PS_TK_COUNT; i++ )
1010 p_sys->tk[i].i_next_block_flags |= BLOCK_FLAG_CELL_DISCONTINUITY;
1012 /* FIXME is it correct or there is better way to know chapter change */
1013 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
1014 &i_part ) == DVDNAV_STATUS_OK )
1016 if( i_title >= 0 && i_title < p_sys->i_title )
1018 p_sys->updates |= INPUT_UPDATE_TITLE;
1019 p_sys->cur_title = i_title;
1021 if( i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint )
1023 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
1024 p_sys->cur_seekpoint = i_part - 1;
1028 break;
1031 case DVDNAV_NAV_PACKET:
1033 p_sys->i_vobu_index = 1;
1034 p_sys->i_vobu_flush = 0;
1036 /* Look if we have need to force a flush (and when) */
1037 const pci_gi_t *p_pci_gi = &dvdnav_get_current_nav_pci( p_sys->dvdnav )->pci_gi;
1038 if( p_pci_gi->vobu_se_e_ptm != 0 && p_pci_gi->vobu_se_e_ptm < p_pci_gi->vobu_e_ptm )
1040 const dsi_gi_t *p_dsi_gi = &dvdnav_get_current_nav_dsi( p_sys->dvdnav )->dsi_gi;
1041 if( p_dsi_gi->vobu_3rdref_ea != 0 )
1042 p_sys->i_vobu_flush = p_dsi_gi->vobu_3rdref_ea;
1043 else if( p_dsi_gi->vobu_2ndref_ea != 0 )
1044 p_sys->i_vobu_flush = p_dsi_gi->vobu_2ndref_ea;
1045 else if( p_dsi_gi->vobu_1stref_ea != 0 )
1046 p_sys->i_vobu_flush = p_dsi_gi->vobu_1stref_ea;
1049 #ifdef DVDNAV_DEBUG
1050 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
1051 #endif
1052 /* A lot of thing to do here :
1053 * - handle packet
1054 * - fetch pts (for time display)
1055 * - ...
1057 DemuxBlock( p_demux, packet, i_len );
1058 if( p_sys->b_spu_change )
1060 ButtonUpdate( p_demux, false );
1061 p_sys->b_spu_change = false;
1063 break;
1066 case DVDNAV_STOP: /* EOF */
1067 msg_Dbg( p_demux, "DVDNAV_STOP" );
1069 if( p_sys->b_readahead )
1070 dvdnav_free_cache_block( p_sys->dvdnav, packet );
1071 return 0;
1073 case DVDNAV_HIGHLIGHT:
1075 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
1076 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
1077 msg_Dbg( p_demux, " - display=%d", event->display );
1078 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
1079 ButtonUpdate( p_demux, false );
1080 break;
1083 case DVDNAV_HOP_CHANNEL:
1084 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
1085 p_sys->i_vobu_index = 0;
1086 p_sys->i_vobu_flush = 0;
1087 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1088 break;
1090 case DVDNAV_WAIT:
1091 msg_Dbg( p_demux, "DVDNAV_WAIT" );
1093 bool b_empty;
1094 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
1095 if( !b_empty )
1097 vlc_tick_sleep( VLC_TICK_FROM_MS(40) );
1099 else
1101 dvdnav_wait_skip( p_sys->dvdnav );
1102 p_sys->b_reset_pcr = true;
1104 break;
1106 default:
1107 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
1108 break;
1111 if( p_sys->b_readahead )
1112 dvdnav_free_cache_block( p_sys->dvdnav, packet );
1114 return 1;
1117 /* Get a 2 char code
1118 * FIXME: partiallyy duplicated from src/input/es_out.c
1120 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
1122 const iso639_lang_t *pl;
1123 char *psz_lang;
1124 char *p;
1126 psz_lang = var_CreateGetString( p_demux, psz_var );
1127 if( !psz_lang )
1128 return strdup(LANGUAGE_DEFAULT);
1130 /* XXX: we will use only the first value
1131 * (and ignore other ones in case of a list) */
1132 if( ( p = strchr( psz_lang, ',' ) ) )
1133 *p = '\0';
1135 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
1137 if( *psz_lang == '\0' )
1138 continue;
1139 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1140 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1141 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1142 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1143 break;
1146 free( psz_lang );
1148 if( pl->psz_eng_name != NULL )
1149 return strdup( pl->psz_iso639_1 );
1151 return strdup(LANGUAGE_DEFAULT);
1154 static void DemuxTitles( demux_t *p_demux )
1156 demux_sys_t *p_sys = p_demux->p_sys;
1157 input_title_t *t;
1158 seekpoint_t *s;
1159 int32_t i_titles;
1161 /* Menu */
1162 t = vlc_input_title_New();
1163 t->i_flags = INPUT_TITLE_MENU | INPUT_TITLE_INTERACTIVE;
1164 t->psz_name = strdup( "DVD Menu" );
1166 s = vlc_seekpoint_New();
1167 s->psz_name = strdup( "Resume" );
1168 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1170 s = vlc_seekpoint_New();
1171 s->psz_name = strdup( "Root" );
1172 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1174 s = vlc_seekpoint_New();
1175 s->psz_name = strdup( "Title" );
1176 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1178 s = vlc_seekpoint_New();
1179 s->psz_name = strdup( "Chapter" );
1180 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1182 s = vlc_seekpoint_New();
1183 s->psz_name = strdup( "Subtitle" );
1184 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1186 s = vlc_seekpoint_New();
1187 s->psz_name = strdup( "Audio" );
1188 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1190 s = vlc_seekpoint_New();
1191 s->psz_name = strdup( "Angle" );
1192 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1194 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1196 /* Find out number of titles/chapters */
1197 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
1199 if( i_titles > 90 )
1200 msg_Err( p_demux, "This is probably an Arccos Protected DVD. This could take time..." );
1202 for( int i = 1; i <= i_titles; i++ )
1204 uint64_t i_title_length;
1205 uint64_t *p_chapters_time;
1207 int32_t i_chapters = dvdnav_describe_title_chapters( p_sys->dvdnav, i,
1208 &p_chapters_time,
1209 &i_title_length );
1210 if( i_chapters < 1 )
1212 i_title_length = 0;
1213 p_chapters_time = NULL;
1215 t = vlc_input_title_New();
1216 t->i_length = i_title_length * 1000 / 90;
1217 for( int j = 0; j < __MAX( i_chapters, 1 ); j++ )
1219 s = vlc_seekpoint_New();
1220 if( p_chapters_time )
1222 if ( j > 0 )
1223 s->i_time_offset = p_chapters_time[j - 1] * 1000 / 90;
1224 else
1225 s->i_time_offset = 0;
1227 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1229 free( p_chapters_time );
1230 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1234 /*****************************************************************************
1235 * Update functions:
1236 *****************************************************************************/
1237 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1239 demux_sys_t *p_sys = p_demux->p_sys;
1240 int32_t i_title, i_part;
1242 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1244 dvdnav_highlight_area_t hl;
1245 int32_t i_button;
1246 bool b_button_ok;
1248 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1249 != DVDNAV_STATUS_OK )
1251 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1252 return;
1255 b_button_ok = false;
1256 if( i_button > 0 && i_title == 0 )
1258 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1260 b_button_ok = DVDNAV_STATUS_OK ==
1261 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
1264 if( b_button_ok )
1266 for( unsigned i = 0; i < 4; i++ )
1268 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1269 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1271 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1272 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1273 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1274 p_sys->palette[i][3] = i_alpha;
1277 vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1278 var_SetInteger( p_demux->p_input, "x-start", hl.sx );
1279 var_SetInteger( p_demux->p_input, "x-end", hl.ex );
1280 var_SetInteger( p_demux->p_input, "y-start", hl.sy );
1281 var_SetInteger( p_demux->p_input, "y-end", hl.ey );
1283 var_SetAddress( p_demux->p_input, "menu-palette", p_sys->palette );
1284 var_SetBool( p_demux->p_input, "highlight", true );
1286 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1288 else
1290 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1291 i_button, i_title );
1293 /* Show all */
1294 vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1295 var_SetBool( p_demux->p_input, "highlight", false );
1297 vlc_global_unlock( VLC_HIGHLIGHT_MUTEX );
1300 static void ESSubtitleUpdate( demux_t *p_demux )
1302 demux_sys_t *p_sys = p_demux->p_sys;
1303 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1304 int32_t i_title, i_part;
1306 ButtonUpdate( p_demux, false );
1308 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1309 if( i_title > 0 ) return;
1311 /* dvdnav_get_active_spu_stream sets (in)visibility flag as 0xF0 */
1312 if( i_spu >= 0 && i_spu <= 0x1f )
1314 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(0xbd20 + i_spu)];
1316 ESNew( p_demux, 0xbd20 + i_spu );
1318 /* be sure to unselect it (reset) */
1319 if( tk->es )
1321 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1322 (bool)false );
1324 /* now select it */
1325 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1328 else
1330 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1332 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(0xbd20 + i_spu)];
1333 if( tk->es )
1335 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1336 (bool)false );
1342 /*****************************************************************************
1343 * DemuxBlock: demux a given block
1344 *****************************************************************************/
1345 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
1347 demux_sys_t *p_sys = p_demux->p_sys;
1349 while( len > 0 )
1351 int i_size = ps_pkt_size( p, len );
1352 if( i_size <= 0 || i_size > len )
1354 break;
1357 /* Create a block */
1358 block_t *p_pkt = block_Alloc( i_size );
1359 memcpy( p_pkt->p_buffer, p, i_size);
1361 /* Parse it and send it */
1362 switch( 0x100 | p[3] )
1364 case 0x1b9:
1365 case 0x1bb:
1366 case 0x1bc:
1367 #ifdef DVDNAV_DEBUG
1368 if( p[3] == 0xbc )
1370 msg_Warn( p_demux, "received a PSM packet" );
1372 else if( p[3] == 0xbb )
1374 msg_Warn( p_demux, "received a SYSTEM packet" );
1376 #endif
1377 block_Release( p_pkt );
1378 break;
1380 case 0x1ba:
1382 int64_t i_scr;
1383 int i_mux_rate;
1384 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1386 es_out_SetPCR( p_demux->out, i_scr + 1 );
1387 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1389 block_Release( p_pkt );
1390 break;
1392 default:
1394 int i_id = ps_pkt_id( p_pkt );
1395 if( i_id >= 0xc0 )
1397 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(i_id)];
1399 if( !tk->b_configured )
1401 ESNew( p_demux, i_id );
1404 if( tk->es &&
1405 !ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, tk->i_skip ) )
1407 int i_next_block_flags = tk->i_next_block_flags;
1408 tk->i_next_block_flags = 0;
1409 if( i_next_block_flags & BLOCK_FLAG_CELL_DISCONTINUITY )
1411 if( p_pkt->i_dts != VLC_TICK_INVALID )
1413 i_next_block_flags &= ~BLOCK_FLAG_CELL_DISCONTINUITY;
1414 i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1416 else tk->i_next_block_flags = BLOCK_FLAG_CELL_DISCONTINUITY;
1418 p_pkt->i_flags |= i_next_block_flags;
1419 es_out_Send( p_demux->out, tk->es, p_pkt );
1421 else
1423 tk->i_next_block_flags = 0;
1424 block_Release( p_pkt );
1427 else
1429 block_Release( p_pkt );
1431 break;
1435 p += i_size;
1436 len -= i_size;
1439 return VLC_SUCCESS;
1442 /*****************************************************************************
1443 * Force still images to be displayed by sending EOS and stopping buffering.
1444 *****************************************************************************/
1445 static void DemuxForceStill( demux_t *p_demux )
1447 static const uint8_t buffer[] = {
1448 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
1449 0x80, 0x00, 0x00,
1450 0x00, 0x00, 0x01, 0xB7,
1452 DemuxBlock( p_demux, buffer, sizeof(buffer) );
1454 bool b_empty;
1455 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
1458 /*****************************************************************************
1459 * ESNew: register a new elementary stream
1460 *****************************************************************************/
1461 static void ESNew( demux_t *p_demux, int i_id )
1463 demux_sys_t *p_sys = p_demux->p_sys;
1464 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(i_id)];
1465 bool b_select = false;
1467 if( tk->b_configured ) return;
1469 if( ps_track_fill( tk, 0, i_id, NULL, true ) )
1471 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1472 return;
1475 /* Add a new ES */
1476 if( tk->fmt.i_cat == VIDEO_ES )
1478 tk->fmt.video.i_sar_num = p_sys->sar.i_num;
1479 tk->fmt.video.i_sar_den = p_sys->sar.i_den;
1480 b_select = true;
1482 else if( tk->fmt.i_cat == AUDIO_ES )
1484 int i_audio = -1;
1485 /* find the audio number PLEASE find another way */
1486 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1488 i_audio = i_id&0x07;
1490 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1492 i_audio = i_id&0xf;
1494 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1496 i_audio = i_id&0x1f;
1498 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1500 i_audio = i_id&0x1f;
1502 if( i_audio >= 0 )
1504 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1505 if( i_lang != 0xffff )
1507 tk->fmt.psz_language = malloc( 3 );
1508 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1509 tk->fmt.psz_language[1] = (i_lang )&0xff;
1510 tk->fmt.psz_language[2] = 0;
1512 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1514 b_select = true;
1518 else if( tk->fmt.i_cat == SPU_ES )
1520 int32_t i_title, i_part;
1521 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1522 if( i_lang != 0xffff )
1524 tk->fmt.psz_language = malloc( 3 );
1525 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1526 tk->fmt.psz_language[1] = (i_lang )&0xff;
1527 tk->fmt.psz_language[2] = 0;
1530 /* Palette */
1531 tk->fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
1532 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1533 16 * sizeof( uint32_t ) );
1535 /* We select only when we are not in the menu */
1536 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1537 if( i_title > 0 &&
1538 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1540 b_select = true;
1544 tk->fmt.i_id = i_id;
1545 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1546 if( b_select && tk->es )
1548 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1550 if( tk->fmt.i_cat == VIDEO_ES )
1552 es_out_Control( p_demux->out, ES_OUT_VOUT_SET_MOUSE_EVENT, tk->es,
1553 EventMouse, p_demux );
1554 ButtonUpdate( p_demux, false );
1557 tk->b_configured = true;
1560 /*****************************************************************************
1561 * Still image end
1562 *****************************************************************************/
1563 static void StillTimer( void *p_data )
1565 demux_sys_t *p_sys = p_data;
1567 vlc_mutex_lock( &p_sys->still.lock );
1568 if( likely(p_sys->still.b_enabled) )
1570 p_sys->still.b_enabled = false;
1571 dvdnav_still_skip( p_sys->dvdnav );
1573 vlc_mutex_unlock( &p_sys->still.lock );
1576 static void EventMouse( const vlc_mouse_t *newmouse, void *p_data )
1578 demux_t *p_demux = p_data;
1579 demux_sys_t *p_sys = p_demux->p_sys;
1581 if( !newmouse )
1583 vlc_mouse_Init( &p_sys->oldmouse );
1584 return;
1587 /* FIXME? PCI usage thread safe? */
1588 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1590 if( vlc_mouse_HasMoved( &p_sys->oldmouse, newmouse ) )
1591 dvdnav_mouse_select( p_sys->dvdnav, pci, newmouse->i_x, newmouse->i_y );
1592 if( vlc_mouse_HasPressed( &p_sys->oldmouse, newmouse, MOUSE_BUTTON_LEFT ) )
1594 ButtonUpdate( p_demux, true );
1595 dvdnav_mouse_activate( p_sys->dvdnav, pci, newmouse->i_x, newmouse->i_y );
1597 p_sys->oldmouse = *newmouse;
1600 /*****************************************************************************
1601 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1602 *****************************************************************************/
1603 static int ProbeDVD( const char *psz_name )
1605 if( !*psz_name )
1606 /* Triggers libdvdcss autodetection */
1607 return VLC_SUCCESS;
1609 int fd = vlc_open( psz_name, O_RDONLY | O_NONBLOCK );
1610 if( fd == -1 )
1611 #ifdef HAVE_FDOPENDIR
1612 return VLC_EGENERIC;
1613 #else
1614 return (errno == ENOENT) ? VLC_EGENERIC : VLC_SUCCESS;
1615 #endif
1617 int ret = VLC_EGENERIC;
1618 struct stat stat_info;
1620 if( fstat( fd, &stat_info ) == -1 )
1621 goto bailout;
1622 if( !S_ISREG( stat_info.st_mode ) )
1624 if( S_ISDIR( stat_info.st_mode ) || S_ISBLK( stat_info.st_mode ) )
1625 ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1626 goto bailout;
1629 /* ISO 9660 volume descriptor */
1630 char iso_dsc[6];
1631 if( lseek( fd, 0x8000 + 1, SEEK_SET ) == -1
1632 || read( fd, iso_dsc, sizeof (iso_dsc) ) < (int)sizeof (iso_dsc)
1633 || memcmp( iso_dsc, "CD001\x01", 6 ) )
1634 goto bailout;
1636 /* Try to find the anchor (2 bytes at LBA 256) */
1637 uint16_t anchor;
1639 if( lseek( fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1640 && read( fd, &anchor, 2 ) == 2
1641 && GetWLE( &anchor ) == 2 )
1642 ret = VLC_SUCCESS; /* Found a potential anchor */
1643 bailout:
1644 vlc_close( fd );
1645 return ret;