lib: media_player: fix aout callbacks having no effects
[vlc.git] / modules / access / dvdnav.c
blob2b199f76c896d646ee23d518cde8b9b9035e75c6
1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * NOTA BENE: this module requires the linking against a library which is
25 * known to require licensing under the GNU General Public License version 2
26 * (or later). Therefore, the result of compiling this module will normally
27 * be subject to the terms of that later license.
28 *****************************************************************************/
31 /*****************************************************************************
32 * Preamble
33 *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
39 #include <assert.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <errno.h>
43 #include <unistd.h> /* close() */
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_input.h>
48 #include <vlc_access.h>
49 #include <vlc_demux.h>
50 #include <vlc_charset.h>
51 #include <vlc_fs.h>
52 #include <vlc_mouse.h>
53 #include <vlc_dialog.h>
54 #include <vlc_iso_lang.h>
56 /* FIXME we should find a better way than including that */
57 #include "../../src/text/iso-639_def.h"
60 #include <dvdnav/dvdnav.h>
61 /* Expose without patching headers */
62 dvdnav_status_t dvdnav_jump_to_sector_by_time(dvdnav_t *, uint64_t, int32_t);
64 #include "../demux/mpeg/pes.h"
65 #include "../demux/mpeg/ps.h"
66 #include "../demux/timestamps_filter.h"
68 #include "disc_helper.h"
70 /*****************************************************************************
71 * Module descriptor
72 *****************************************************************************/
73 #define ANGLE_TEXT N_("DVD angle")
74 #define ANGLE_LONGTEXT N_( \
75 "Default DVD angle." )
77 #define MENU_TEXT N_("Start directly in menu")
78 #define MENU_LONGTEXT N_( \
79 "Start the DVD directly in the main menu. This "\
80 "will try to skip all the useless warning introductions." )
82 #define LANGUAGE_DEFAULT ("en")
84 static int AccessDemuxOpen ( vlc_object_t * );
85 static void Close( vlc_object_t * );
87 static int DemuxOpen ( vlc_object_t * );
89 vlc_module_begin ()
90 set_shortname( N_("DVD with menus") )
91 set_description( N_("DVDnav Input") )
92 set_category( CAT_INPUT )
93 set_subcategory( SUBCAT_INPUT_ACCESS )
94 add_integer( "dvdnav-angle", 1, ANGLE_TEXT,
95 ANGLE_LONGTEXT, false )
96 add_bool( "dvdnav-menu", true,
97 MENU_TEXT, MENU_LONGTEXT, false )
98 set_capability( "access", 305 )
99 add_shortcut( "dvd", "dvdnav", "file" )
100 set_callbacks( AccessDemuxOpen, Close )
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 vlc_module_end ()
110 /* Shall we use libdvdnav's read ahead cache? */
111 #ifdef __OS2__
112 #define DVD_READ_CACHE 0
113 #else
114 #define DVD_READ_CACHE 1
115 #endif
117 #define BLOCK_FLAG_CELL_DISCONTINUITY (BLOCK_FLAG_PRIVATE_SHIFT << 1)
119 /*****************************************************************************
120 * Local prototypes
121 *****************************************************************************/
122 typedef struct
124 dvdnav_t *dvdnav;
125 es_out_t *p_tf_out;
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 vlc_mutex_t event_lock;
144 es_out_id_t *spu_es;
146 /* palette for menus */
147 uint32_t clut[16];
148 bool b_spu_change;
149 struct
151 bool b_pending;
152 bool b_from_user;
153 } highlight;
155 /* Aspect ration */
156 struct {
157 unsigned i_num;
158 unsigned i_den;
159 } sar;
161 /* */
162 int i_title;
163 input_title_t **title;
164 int cur_title;
165 int cur_seekpoint;
166 unsigned updates;
168 /* length of program group chain */
169 vlc_tick_t i_pgc_length;
170 int i_vobu_index;
171 int i_vobu_flush;
173 vlc_mouse_t oldmouse;
174 } demux_sys_t;
176 static int Control( demux_t *, int, va_list );
177 static int Demux( demux_t * );
178 static int DemuxBlock( demux_t *, const uint8_t *, int );
179 static void DemuxForceStill( demux_t * );
181 static void DemuxTitles( demux_t * );
182 static void ESSubtitleUpdate( demux_t * );
183 static void ButtonUpdate( demux_t *, bool );
185 static void ESNew( demux_t *, int );
186 static int ProbeDVD( const char * );
188 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
190 static int ControlInternal( demux_t *, int, ... );
192 static void StillTimer( void * );
194 static void EventMouse( const vlc_mouse_t *mouse, void *p_data );
196 #if DVDNAV_VERSION >= 60100
197 static void DvdNavLog( void *foo, dvdnav_logger_level_t i, const char *p, va_list z)
199 msg_GenericVa( (demux_t*)foo, i, p, z );
201 #endif
203 /*****************************************************************************
205 *****************************************************************************/
206 static const struct
208 DVDMenuID_t dvdnav_id;
209 const char *psz_name;
210 } menus_id_mapping[] = {
211 { DVD_MENU_Escape, "Resume" },
212 { DVD_MENU_Root, "Root" },
213 { DVD_MENU_Title, "Title" },
214 { DVD_MENU_Part, "Chapter" },
215 { DVD_MENU_Subpicture, "Subtitle" },
216 { DVD_MENU_Audio, "Audio" },
217 { DVD_MENU_Angle, "Angle" },
220 static int MenuIDToSeekpoint( DVDMenuID_t menuid, int *seekpoint )
222 for( size_t i=0; i<ARRAY_SIZE(menus_id_mapping); i++ )
224 if( menus_id_mapping[i].dvdnav_id == menuid )
226 *seekpoint = i;
227 return VLC_SUCCESS;
230 return VLC_EGENERIC;
233 static int SeekpointToMenuID( int seekpoint, DVDMenuID_t *id )
235 if( (size_t) seekpoint >= ARRAY_SIZE(menus_id_mapping) )
236 return VLC_EGENERIC;
237 *id = menus_id_mapping[seekpoint].dvdnav_id;
238 return VLC_SUCCESS;
241 static int CallRootTitleMenu( dvdnav_t *p_dvdnav,
242 int *pi_title, int *pi_seekpoint )
244 const DVDMenuID_t menuids[2] = { DVD_MENU_Title, DVD_MENU_Root };
245 for( int i=0; i<2; i++ )
247 if( dvdnav_menu_call( p_dvdnav, menuids[i] )
248 == DVDNAV_STATUS_OK )
250 *pi_title = 0;
251 MenuIDToSeekpoint( menuids[i], pi_seekpoint );
252 return VLC_SUCCESS;
255 return VLC_EGENERIC;
258 /*****************************************************************************
259 * CommonOpen:
260 *****************************************************************************/
261 static int CommonOpen( vlc_object_t *p_this,
262 dvdnav_t *p_dvdnav, bool b_readahead )
264 demux_t *p_demux = (demux_t*)p_this;
265 demux_sys_t *p_sys;
266 int i_angle;
267 char *psz_code;
269 assert( p_dvdnav );
271 /* Fill p_demux field */
272 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
273 p_sys->dvdnav = p_dvdnav;
274 p_sys->p_tf_out = timestamps_filter_es_out_New( p_demux->out );
275 if( !p_sys->p_tf_out )
277 free( p_sys );
278 return VLC_EGENERIC;
281 ps_track_init( p_sys->tk );
282 p_sys->b_readahead = b_readahead;
283 vlc_mouse_Init( &p_sys->oldmouse );
285 /* Configure dvdnav */
286 if( dvdnav_set_readahead_flag( p_sys->dvdnav, p_sys->b_readahead ) !=
287 DVDNAV_STATUS_OK )
289 msg_Warn( p_demux, "cannot set read-a-head flag" );
292 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
293 DVDNAV_STATUS_OK )
295 msg_Warn( p_demux, "cannot set PGC positioning flag" );
298 /* Set menu language */
299 psz_code = DemuxGetLanguageCode( p_demux, "menu-language" );
300 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
301 DVDNAV_STATUS_OK )
303 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
304 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
305 /* We try to fall back to 'en' */
306 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
307 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
309 free( psz_code );
311 /* Set audio language */
312 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
313 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
314 DVDNAV_STATUS_OK )
316 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
317 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
318 /* We try to fall back to 'en' */
319 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
320 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
322 free( psz_code );
324 /* Set spu language */
325 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
326 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
327 DVDNAV_STATUS_OK )
329 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
330 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
331 /* We try to fall back to 'en' */
332 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
333 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
335 free( psz_code );
337 DemuxTitles( p_demux );
339 if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
341 msg_Dbg( p_demux, "trying to go to dvd menu" );
343 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
345 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
346 vlc_dialog_display_error( p_demux, _("Playback failure"), "%s",
347 _("VLC cannot set the DVD's title. It possibly "
348 "cannot decrypt the entire disc.") );
349 timestamps_filter_es_out_Delete( p_sys->p_tf_out );
350 free( p_sys );
351 return VLC_EGENERIC;
354 if( CallRootTitleMenu( p_sys->dvdnav, &p_sys->cur_title,
355 &p_sys->cur_seekpoint ) )
356 msg_Warn( p_demux, "cannot go to dvd menu" );
359 i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
360 if( i_angle <= 0 ) i_angle = 1;
362 p_sys->still.b_enabled = false;
363 vlc_mutex_init( &p_sys->still.lock );
364 vlc_mutex_init( &p_sys->event_lock );
365 if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
366 p_sys->still.b_created = true;
368 return VLC_SUCCESS;
371 /*****************************************************************************
372 * AccessDemuxOpen:
373 *****************************************************************************/
374 static int AccessDemuxOpen ( vlc_object_t *p_this )
376 demux_t *p_demux = (demux_t*)p_this;
377 dvdnav_t *p_dvdnav = NULL;
378 char *psz_file = NULL;
379 const char *psz_path = NULL;
380 int i_ret = VLC_EGENERIC;
381 bool forced = false;
383 if (p_demux->out == NULL)
384 return VLC_EGENERIC;
386 if( !strncasecmp(p_demux->psz_url, "dvd", 3) )
387 forced = true;
389 if( !p_demux->psz_filepath || !*p_demux->psz_filepath )
391 /* Only when selected */
392 if( !forced )
393 return VLC_EGENERIC;
395 psz_file = var_InheritString( p_this, "dvd" );
397 else
398 psz_file = strdup( p_demux->psz_filepath );
400 #if defined( _WIN32 ) || defined( __OS2__ )
401 if( psz_file != NULL )
403 /* Remove trailing backslash, otherwise dvdnav_open will fail */
404 size_t flen = strlen( psz_file );
405 if( flen > 0 && psz_file[flen - 1] == '\\' )
406 psz_file[flen - 1] = '\0';
408 else
409 psz_file = strdup("");
410 #endif
412 if( unlikely(psz_file == NULL) )
413 return VLC_EGENERIC;
415 /* Try some simple probing to avoid going through dvdnav_open too often */
416 if( !forced && ProbeDVD( psz_file ) != VLC_SUCCESS )
417 goto bailout;
419 if( forced && DiscProbeMacOSPermission( p_this, psz_file ) != VLC_SUCCESS )
420 goto bailout;
422 /* Open dvdnav */
423 psz_path = ToLocale( psz_file );
424 #if DVDNAV_VERSION >= 60100
425 dvdnav_logger_cb cbs;
426 cbs.pf_log = DvdNavLog;
427 if( dvdnav_open2( &p_dvdnav, p_demux, &cbs, psz_path ) != DVDNAV_STATUS_OK )
428 #else
429 if( dvdnav_open( &p_dvdnav, psz_path ) != DVDNAV_STATUS_OK )
430 #endif
432 msg_Warn( p_demux, "cannot open DVD (%s)", psz_file);
433 goto bailout;
436 i_ret = CommonOpen( p_this, p_dvdnav, !!DVD_READ_CACHE );
437 if( i_ret != VLC_SUCCESS )
438 dvdnav_close( p_dvdnav );
440 bailout:
441 free( psz_file );
442 if( psz_path )
443 LocaleFree( psz_path );
444 return i_ret;
447 /*****************************************************************************
448 * StreamProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
449 *****************************************************************************/
450 static int StreamProbeDVD( stream_t *s )
452 /* first sector should be filled with zeros */
453 ssize_t i_peek;
454 const uint8_t *p_peek;
455 i_peek = vlc_stream_Peek( s, &p_peek, 2048 );
456 if( i_peek < 512 ) {
457 return VLC_EGENERIC;
459 while (i_peek > 0) {
460 if (p_peek[ --i_peek ]) {
461 return VLC_EGENERIC;
465 /* ISO 9660 volume descriptor */
466 char iso_dsc[6];
467 if( vlc_stream_Seek( s, 0x8000 + 1 ) != VLC_SUCCESS
468 || vlc_stream_Read( s, iso_dsc, sizeof (iso_dsc) ) < (int)sizeof (iso_dsc)
469 || memcmp( iso_dsc, "CD001\x01", 6 ) )
470 return VLC_EGENERIC;
472 /* Try to find the anchor (2 bytes at LBA 256) */
473 uint16_t anchor;
475 if( vlc_stream_Seek( s, 256 * DVD_VIDEO_LB_LEN ) == VLC_SUCCESS
476 && vlc_stream_Read( s, &anchor, 2 ) == 2
477 && GetWLE( &anchor ) == 2 )
478 return VLC_SUCCESS;
479 else
480 return VLC_EGENERIC;
483 /*****************************************************************************
484 * dvdnav stream callbacks
485 *****************************************************************************/
486 static int stream_cb_seek( void *demux, uint64_t pos )
488 return vlc_stream_Seek( ((demux_t *)demux)->s, pos );
491 static int stream_cb_read( void *demux, void* buffer, int size )
493 return vlc_stream_Read( ((demux_t *)demux)->s, buffer, size );
496 /*****************************************************************************
497 * DemuxOpen:
498 *****************************************************************************/
499 static int DemuxOpen ( vlc_object_t *p_this )
501 demux_t *p_demux = (demux_t*)p_this;
502 dvdnav_t *p_dvdnav = NULL;
503 bool forced = false, b_seekable = false;
505 if( p_demux->psz_name != NULL && !strncmp(p_demux->psz_name, "dvd", 3) )
506 forced = true;
508 /* StreamProbeDVD need FASTSEEK, but if dvd is forced, we don't probe thus
509 * don't need fastseek */
510 vlc_stream_Control( p_demux->s, forced ? STREAM_CAN_SEEK : STREAM_CAN_FASTSEEK,
511 &b_seekable );
512 if( !b_seekable )
513 return VLC_EGENERIC;
515 /* Try some simple probing to avoid going through dvdnav_open too often */
516 if( !forced && StreamProbeDVD( p_demux->s ) != VLC_SUCCESS )
517 return VLC_EGENERIC;
519 static dvdnav_stream_cb stream_cb =
521 .pf_seek = stream_cb_seek,
522 .pf_read = stream_cb_read,
523 .pf_readv = NULL,
526 /* Open dvdnav with stream callbacks */
527 #if DVDNAV_VERSION >= 60100
528 dvdnav_logger_cb cbs;
529 cbs.pf_log = DvdNavLog;
530 if( dvdnav_open_stream2( &p_dvdnav, p_demux,
531 &cbs, &stream_cb ) != DVDNAV_STATUS_OK )
532 #else
533 if( dvdnav_open_stream( &p_dvdnav, p_demux,
534 &stream_cb ) != DVDNAV_STATUS_OK )
535 #endif
537 msg_Warn( p_demux, "cannot open DVD with open_stream" );
538 return VLC_EGENERIC;
541 int i_ret = CommonOpen( p_this, p_dvdnav, false );
542 if( i_ret != VLC_SUCCESS )
543 dvdnav_close( p_dvdnav );
544 return i_ret;
547 /*****************************************************************************
548 * Close:
549 *****************************************************************************/
550 static void Close( vlc_object_t *p_this )
552 demux_t *p_demux = (demux_t*)p_this;
553 demux_sys_t *p_sys = p_demux->p_sys;
555 /* Stop still image handler */
556 if( p_sys->still.b_created )
557 vlc_timer_destroy( p_sys->still.timer );
559 for( int i = 0; i < PS_TK_COUNT; i++ )
561 ps_track_t *tk = &p_sys->tk[i];
562 if( tk->b_configured )
564 es_format_Clean( &tk->fmt );
565 if( tk->es ) es_out_Del( p_sys->p_tf_out, tk->es );
569 /* Free the array of titles */
570 for( int i = 0; i < p_sys->i_title; i++ )
571 vlc_input_title_Delete( p_sys->title[i] );
572 TAB_CLEAN( p_sys->i_title, p_sys->title );
574 timestamps_filter_es_out_Delete( p_sys->p_tf_out );
576 dvdnav_close( p_sys->dvdnav );
577 free( p_sys );
581 /*****************************************************************************
582 * Reset variables on Random Access:
583 *****************************************************************************/
584 static void RandomAccessCleanup(demux_sys_t *p_sys)
586 p_sys->highlight.b_pending = false;
589 /*****************************************************************************
590 * Control:
591 *****************************************************************************/
592 static int Control( demux_t *p_demux, int i_query, va_list args )
594 demux_sys_t *p_sys = p_demux->p_sys;
595 input_title_t ***ppp_title;
596 int i;
598 switch( i_query )
600 case DEMUX_SET_POSITION:
601 case DEMUX_GET_POSITION:
602 case DEMUX_GET_LENGTH:
604 uint32_t pos, len;
605 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
606 DVDNAV_STATUS_OK || len == 0 )
608 return VLC_EGENERIC;
611 switch( i_query )
613 case DEMUX_GET_POSITION:
614 *va_arg( args, double* ) = (double)pos / (double)len;
615 return VLC_SUCCESS;
617 case DEMUX_SET_POSITION:
618 pos = va_arg( args, double ) * len;
619 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
620 DVDNAV_STATUS_OK )
622 return VLC_SUCCESS;
624 RandomAccessCleanup( p_sys );
625 break;
627 case DEMUX_GET_LENGTH:
628 if( p_sys->i_pgc_length > 0 )
630 *va_arg( args, vlc_tick_t * ) = p_sys->i_pgc_length;
631 return VLC_SUCCESS;
633 break;
635 return VLC_EGENERIC;
638 case DEMUX_GET_TIME:
639 if( p_sys->i_pgc_length > 0 )
641 *va_arg( args, vlc_tick_t * ) =
642 dvdnav_get_current_time( p_sys->dvdnav ) * 100 / 9;
643 return VLC_SUCCESS;
645 break;
647 case DEMUX_SET_TIME:
649 vlc_tick_t i_time = va_arg( args, vlc_tick_t );
650 if( dvdnav_jump_to_sector_by_time( p_sys->dvdnav,
651 i_time * 9 / 100,
652 SEEK_SET ) == DVDNAV_STATUS_OK )
653 return VLC_SUCCESS;
654 msg_Err( p_demux, "can't set time to %" PRId64, i_time );
655 return VLC_EGENERIC;
658 /* Special for access_demux */
659 case DEMUX_CAN_PAUSE:
660 case DEMUX_CAN_SEEK:
661 case DEMUX_CAN_CONTROL_PACE:
662 /* TODO */
663 *va_arg( args, bool * ) = true;
664 return VLC_SUCCESS;
666 case DEMUX_SET_PAUSE_STATE:
667 return VLC_SUCCESS;
669 case DEMUX_GET_TITLE_INFO:
670 ppp_title = va_arg( args, input_title_t*** );
672 /* Duplicate title infos */
673 *ppp_title = vlc_alloc( p_sys->i_title, sizeof( input_title_t * ) );
674 if( !*ppp_title )
675 return VLC_EGENERIC;
676 for( i = 0; i < p_sys->i_title; i++ )
678 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
679 if(!(*ppp_title)[i])
681 while( i )
682 free( (*ppp_title)[--i] );
683 free( *ppp_title );
684 return VLC_EGENERIC;
687 *va_arg( args, int* ) = p_sys->i_title;
688 *va_arg( args, int* ) = 0; /* Title offset */
689 *va_arg( args, int* ) = 1; /* Chapter offset */
690 return VLC_SUCCESS;
692 case DEMUX_SET_TITLE:
693 i = va_arg( args, int );
694 if( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
695 != DVDNAV_STATUS_OK )
697 msg_Warn( p_demux, "cannot set title/chapter" );
698 return VLC_EGENERIC;
701 if( i != 0 )
703 dvdnav_still_skip( p_sys->dvdnav );
704 if( dvdnav_title_play( p_sys->dvdnav, i ) != DVDNAV_STATUS_OK )
706 msg_Warn( p_demux, "cannot set title/chapter" );
707 return VLC_EGENERIC;
711 p_sys->updates |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
712 p_sys->cur_title = i;
713 if( i != 0 )
714 p_sys->cur_seekpoint = 0;
715 else
716 MenuIDToSeekpoint( DVD_MENU_Root, &p_sys->cur_seekpoint );
717 RandomAccessCleanup( p_sys );
718 return VLC_SUCCESS;
720 case DEMUX_SET_SEEKPOINT:
721 i = va_arg( args, int );
722 if( p_sys->cur_title == 0 )
724 DVDMenuID_t menuid;
725 if( SeekpointToMenuID(i, &menuid) ||
726 dvdnav_menu_call(p_sys->dvdnav, menuid) != DVDNAV_STATUS_OK )
727 return VLC_EGENERIC;
729 else if( dvdnav_part_play( p_sys->dvdnav, p_sys->cur_title,
730 i + 1 ) != DVDNAV_STATUS_OK )
732 msg_Warn( p_demux, "cannot set title/chapter" );
733 return VLC_EGENERIC;
735 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
736 p_sys->cur_seekpoint = i;
737 RandomAccessCleanup( p_sys );
738 return VLC_SUCCESS;
740 case DEMUX_TEST_AND_CLEAR_FLAGS:
742 unsigned *restrict flags = va_arg(args, unsigned *);
743 *flags &= p_sys->updates;
744 p_sys->updates &= ~*flags;
745 break;
748 case DEMUX_GET_TITLE:
749 *va_arg( args, int * ) = p_sys->cur_title;
750 break;
752 case DEMUX_GET_SEEKPOINT:
753 *va_arg( args, int * ) = p_sys->cur_seekpoint;
754 break;
756 case DEMUX_GET_PTS_DELAY:
757 *va_arg( args, vlc_tick_t * ) =
758 VLC_TICK_FROM_MS( var_InheritInteger( p_demux, "disc-caching" ) );
759 return VLC_SUCCESS;
761 case DEMUX_GET_META:
763 const char *title_name = NULL;
765 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
766 if( (NULL != title_name) && ('\0' != title_name[0]) && IsUTF8(title_name) )
768 vlc_meta_t *p_meta = va_arg( args, vlc_meta_t* );
769 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
770 return VLC_SUCCESS;
772 return VLC_EGENERIC;
775 case DEMUX_NAV_ACTIVATE:
777 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
779 if( dvdnav_button_activate( p_sys->dvdnav, pci ) != DVDNAV_STATUS_OK )
780 return VLC_EGENERIC;
781 vlc_mutex_lock( &p_sys->event_lock );
782 ButtonUpdate( p_demux, true );
783 vlc_mutex_unlock( &p_sys->event_lock );
784 break;
787 case DEMUX_NAV_UP:
789 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
791 if( dvdnav_upper_button_select( p_sys->dvdnav, pci ) != DVDNAV_STATUS_OK )
792 return VLC_EGENERIC;
793 break;
796 case DEMUX_NAV_DOWN:
798 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
800 if( dvdnav_lower_button_select( p_sys->dvdnav, pci ) != DVDNAV_STATUS_OK )
801 return VLC_EGENERIC;
802 break;
805 case DEMUX_NAV_LEFT:
807 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
809 if( dvdnav_left_button_select( p_sys->dvdnav, pci ) != DVDNAV_STATUS_OK )
810 return VLC_EGENERIC;
811 break;
814 case DEMUX_NAV_RIGHT:
816 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
818 if( dvdnav_right_button_select( p_sys->dvdnav, pci ) != DVDNAV_STATUS_OK )
819 return VLC_EGENERIC;
820 break;
823 case DEMUX_NAV_MENU:
825 if( CallRootTitleMenu( p_sys->dvdnav, &p_sys->cur_title,
826 &p_sys->cur_seekpoint ) )
828 msg_Warn( p_demux, "cannot select Title/Root menu" );
829 return VLC_EGENERIC;
831 p_sys->updates |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
832 break;
835 /* TODO implement others */
836 default:
837 return VLC_EGENERIC;
840 return VLC_SUCCESS;
843 static int ControlInternal( demux_t *p_demux, int i_query, ... )
845 va_list args;
846 int i_result;
848 va_start( args, i_query );
849 i_result = Control( p_demux, i_query, args );
850 va_end( args );
852 return i_result;
854 /*****************************************************************************
855 * Demux:
856 *****************************************************************************/
857 static int Demux( demux_t *p_demux )
859 demux_sys_t *p_sys = p_demux->p_sys;
861 uint8_t buffer[DVD_VIDEO_LB_LEN];
862 uint8_t *packet = buffer;
863 int i_event;
864 int i_len;
865 dvdnav_status_t status;
867 if( p_sys->b_readahead )
868 status = dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event,
869 &i_len );
870 else
871 status = dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event,
872 &i_len );
873 if( status == DVDNAV_STATUS_ERR )
875 msg_Warn( p_demux, "cannot get next block (%s)",
876 dvdnav_err_to_string( p_sys->dvdnav ) );
877 if( p_sys->cur_title == 0 )
879 msg_Dbg( p_demux, "jumping to first title" );
880 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ?
881 VLC_DEMUXER_SUCCESS : VLC_DEMUXER_EGENERIC;
883 return VLC_DEMUXER_EGENERIC;
886 vlc_mutex_lock( &p_sys->event_lock );
887 if(p_sys->highlight.b_pending)
888 ButtonUpdate(p_demux, p_sys->highlight.b_from_user);
889 vlc_mutex_unlock( &p_sys->event_lock );
891 switch( i_event )
893 case DVDNAV_BLOCK_OK: /* mpeg block */
894 vlc_mutex_lock( &p_sys->still.lock );
895 vlc_timer_disarm( p_sys->still.timer );
896 p_sys->still.b_enabled = false;
897 vlc_mutex_unlock( &p_sys->still.lock );
898 if( p_sys->b_reset_pcr )
900 es_out_Control( p_sys->p_tf_out, ES_OUT_RESET_PCR );
901 p_sys->b_reset_pcr = false;
903 DemuxBlock( p_demux, packet, i_len );
904 if( p_sys->i_vobu_index > 0 )
906 if( p_sys->i_vobu_flush == p_sys->i_vobu_index )
907 DemuxForceStill( p_demux );
908 p_sys->i_vobu_index++;
910 break;
912 case DVDNAV_NOP: /* Nothing */
913 msg_Dbg( p_demux, "DVDNAV_NOP" );
914 break;
916 case DVDNAV_STILL_FRAME:
918 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
919 bool b_still_init = false;
921 vlc_mutex_lock( &p_sys->still.lock );
922 if( !p_sys->still.b_enabled )
924 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
925 msg_Dbg( p_demux, " - length=0x%x", event->length );
926 p_sys->still.b_enabled = true;
928 if( event->length != 0xff && p_sys->still.b_created )
930 vlc_tick_t delay = vlc_tick_from_sec( event->length );
931 vlc_timer_schedule( p_sys->still.timer, false, delay, VLC_TIMER_FIRE_ONCE );
934 b_still_init = true;
936 vlc_mutex_unlock( &p_sys->still.lock );
938 if( b_still_init )
940 DemuxForceStill( p_demux );
941 p_sys->b_reset_pcr = true;
943 vlc_tick_sleep( VLC_TICK_FROM_MS(40) );
944 break;
947 case DVDNAV_SPU_CLUT_CHANGE:
949 int i;
951 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
952 /* Update color lookup table (16 *uint32_t in packet) */
953 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
955 /* HACK to get the SPU tracks registered in the right order */
956 for( i = 0; i < 0x1f; i++ )
958 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
959 ESNew( p_demux, 0xbd20 + i );
961 /* END HACK */
962 break;
965 case DVDNAV_SPU_STREAM_CHANGE:
967 dvdnav_spu_stream_change_event_t *event =
968 (dvdnav_spu_stream_change_event_t*)packet;
969 int i;
971 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
972 msg_Dbg( p_demux, " - physical_wide=%d",
973 event->physical_wide );
974 msg_Dbg( p_demux, " - physical_letterbox=%d",
975 event->physical_letterbox);
976 msg_Dbg( p_demux, " - physical_pan_scan=%d",
977 event->physical_pan_scan );
979 ESSubtitleUpdate( p_demux );
980 p_sys->b_spu_change = true;
982 /* HACK to get the SPU tracks registered in the right order */
983 for( i = 0; i < 0x1f; i++ )
985 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
986 ESNew( p_demux, 0xbd20 + i );
988 /* END HACK */
989 break;
992 case DVDNAV_AUDIO_STREAM_CHANGE:
994 dvdnav_audio_stream_change_event_t *event =
995 (dvdnav_audio_stream_change_event_t*)packet;
996 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
997 msg_Dbg( p_demux, " - physical=%d", event->physical );
998 /* TODO */
999 break;
1002 case DVDNAV_VTS_CHANGE:
1004 int32_t i_title = 0;
1005 int32_t i_part = 0;
1007 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
1008 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
1009 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
1010 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
1012 /* reset PCR */
1013 es_out_Control( p_sys->p_tf_out, ES_OUT_RESET_PCR );
1015 for( int i = 0; i < PS_TK_COUNT; i++ )
1017 ps_track_t *tk = &p_sys->tk[i];
1018 if( tk->b_configured )
1020 es_format_Clean( &tk->fmt );
1021 if( tk->es )
1023 if( tk->es == p_sys->spu_es )
1025 vlc_mutex_lock( &p_sys->event_lock );
1026 p_sys->spu_es = NULL;
1027 p_sys->highlight.b_pending = false;
1028 vlc_mutex_unlock( &p_sys->event_lock );
1030 es_out_Del( p_sys->p_tf_out, tk->es );
1031 tk->es = NULL;
1034 tk->b_configured = false;
1037 uint32_t i_width, i_height;
1038 if( dvdnav_get_video_resolution( p_sys->dvdnav,
1039 &i_width, &i_height ) )
1040 i_width = i_height = 0;
1041 switch( dvdnav_get_video_aspect( p_sys->dvdnav ) )
1043 case 0:
1044 p_sys->sar.i_num = 4 * i_height;
1045 p_sys->sar.i_den = 3 * i_width;
1046 break;
1047 case 3:
1048 p_sys->sar.i_num = 16 * i_height;
1049 p_sys->sar.i_den = 9 * i_width;
1050 break;
1051 default:
1052 p_sys->sar.i_num = 0;
1053 p_sys->sar.i_den = 0;
1054 break;
1057 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
1058 &i_part ) == DVDNAV_STATUS_OK )
1060 if( i_title >= 0 && i_title < p_sys->i_title &&
1061 p_sys->cur_title != i_title )
1063 p_sys->updates |= INPUT_UPDATE_TITLE;
1064 p_sys->cur_title = i_title;
1067 break;
1070 case DVDNAV_CELL_CHANGE:
1072 int32_t i_nav_title = 0;
1073 int32_t i_nav_part = 0;
1075 dvdnav_cell_change_event_t *event =
1076 (dvdnav_cell_change_event_t*)packet;
1077 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
1078 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
1079 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
1080 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
1081 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
1082 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
1083 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
1084 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
1086 /* Store the length in time of the current PGC */
1087 p_sys->i_pgc_length = FROM_SCALE_NZ(event->pgc_length);
1088 p_sys->i_vobu_index = 0;
1089 p_sys->i_vobu_flush = 0;
1091 for( int i=0; i<PS_TK_COUNT; i++ )
1092 p_sys->tk[i].i_next_block_flags |= BLOCK_FLAG_CELL_DISCONTINUITY;
1094 /* FIXME is it correct or there is better way to know chapter change */
1095 if( dvdnav_current_title_info( p_sys->dvdnav, &i_nav_title,
1096 &i_nav_part ) == DVDNAV_STATUS_OK )
1098 const int i_title = p_sys->cur_title;
1099 const int i_seekpoint = p_sys->cur_seekpoint;
1100 if( i_nav_title > 0 && i_nav_title < p_sys->i_title )
1102 p_sys->cur_title = i_nav_title;
1103 if( i_nav_part > 0 &&
1104 i_nav_part <= p_sys->title[i_nav_title]->i_seekpoint )
1105 p_sys->cur_seekpoint = i_nav_part - 1;
1106 else p_sys->cur_seekpoint = 0;
1108 else if( i_nav_title == 0 ) /* in menus, i_part == menu id */
1110 if( MenuIDToSeekpoint( i_nav_part, &p_sys->cur_seekpoint ) )
1111 p_sys->cur_seekpoint = 0; /* non standard menu number, can't map back */
1112 else
1113 p_sys->cur_title = 0;
1115 if( i_title != p_sys->cur_title )
1116 p_sys->updates |= INPUT_UPDATE_TITLE;
1117 if( i_seekpoint != p_sys->cur_seekpoint )
1118 p_sys->updates |= INPUT_UPDATE_SEEKPOINT;
1120 break;
1123 case DVDNAV_NAV_PACKET:
1125 p_sys->i_vobu_index = 1;
1126 p_sys->i_vobu_flush = 0;
1128 /* Look if we have need to force a flush (and when) */
1129 const pci_t *p_pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1130 if( unlikely(!p_pci) )
1131 break;
1132 const pci_gi_t *p_pci_gi = &p_pci->pci_gi;
1133 if( p_pci_gi->vobu_se_e_ptm != 0 && p_pci_gi->vobu_se_e_ptm < p_pci_gi->vobu_e_ptm )
1135 const dsi_t *p_dsi = dvdnav_get_current_nav_dsi( p_sys->dvdnav );
1136 if( unlikely(!p_dsi) )
1137 break;
1138 const dsi_gi_t *p_dsi_gi = &p_dsi->dsi_gi;
1139 if( p_dsi_gi->vobu_3rdref_ea != 0 )
1140 p_sys->i_vobu_flush = p_dsi_gi->vobu_3rdref_ea;
1141 else if( p_dsi_gi->vobu_2ndref_ea != 0 )
1142 p_sys->i_vobu_flush = p_dsi_gi->vobu_2ndref_ea;
1143 else if( p_dsi_gi->vobu_1stref_ea != 0 )
1144 p_sys->i_vobu_flush = p_dsi_gi->vobu_1stref_ea;
1147 #ifdef DVDNAV_DEBUG
1148 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
1149 #endif
1150 /* A lot of thing to do here :
1151 * - handle packet
1152 * - fetch pts (for time display)
1153 * - ...
1155 DemuxBlock( p_demux, packet, i_len );
1156 if( p_sys->b_spu_change )
1158 vlc_mutex_lock(&p_sys->event_lock);
1159 ButtonUpdate( p_demux, false );
1160 vlc_mutex_unlock(&p_sys->event_lock);
1161 p_sys->b_spu_change = false;
1163 break;
1166 case DVDNAV_STOP: /* EOF */
1167 msg_Dbg( p_demux, "DVDNAV_STOP" );
1169 if( p_sys->b_readahead )
1170 dvdnav_free_cache_block( p_sys->dvdnav, packet );
1171 return VLC_DEMUXER_EOF;
1173 case DVDNAV_HIGHLIGHT:
1175 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
1176 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
1177 msg_Dbg( p_demux, " - display=%d", event->display );
1178 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
1179 vlc_mutex_lock(&p_sys->event_lock);
1180 ButtonUpdate( p_demux, false );
1181 vlc_mutex_unlock(&p_sys->event_lock);
1182 break;
1185 case DVDNAV_HOP_CHANNEL:
1186 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
1187 p_sys->i_vobu_index = 0;
1188 p_sys->i_vobu_flush = 0;
1189 es_out_Control( p_sys->p_tf_out, ES_OUT_RESET_PCR );
1190 break;
1192 case DVDNAV_WAIT:
1193 msg_Dbg( p_demux, "DVDNAV_WAIT" );
1195 bool b_empty;
1196 es_out_Control( p_sys->p_tf_out, ES_OUT_GET_EMPTY, &b_empty );
1197 if( !b_empty )
1199 vlc_tick_sleep( VLC_TICK_FROM_MS(40) );
1201 else
1203 dvdnav_wait_skip( p_sys->dvdnav );
1204 p_sys->b_reset_pcr = true;
1206 break;
1208 default:
1209 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
1210 break;
1213 if( p_sys->b_readahead )
1214 dvdnav_free_cache_block( p_sys->dvdnav, packet );
1216 return VLC_DEMUXER_SUCCESS;
1219 /* Get a 2 char code
1220 * FIXME: partiallyy duplicated from src/input/es_out.c
1222 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
1224 const iso639_lang_t *pl;
1225 char *psz_lang;
1226 char *p;
1228 psz_lang = var_CreateGetString( p_demux, psz_var );
1229 if( !psz_lang )
1230 return strdup(LANGUAGE_DEFAULT);
1232 /* XXX: we will use only the first value
1233 * (and ignore other ones in case of a list) */
1234 if( ( p = strchr( psz_lang, ',' ) ) )
1235 *p = '\0';
1237 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
1239 if( *psz_lang == '\0' )
1240 continue;
1241 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1242 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1243 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1244 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1245 break;
1248 free( psz_lang );
1250 if( pl->psz_eng_name != NULL )
1251 return strdup( pl->psz_iso639_1 );
1253 return strdup(LANGUAGE_DEFAULT);
1256 static void DemuxTitles( demux_t *p_demux )
1258 demux_sys_t *p_sys = p_demux->p_sys;
1259 input_title_t *t;
1260 seekpoint_t *s;
1261 int32_t i_titles;
1263 /* Menu */
1264 t = vlc_input_title_New();
1265 t->i_flags = INPUT_TITLE_MENU | INPUT_TITLE_INTERACTIVE;
1266 t->psz_name = strdup( "DVD Menu" );
1268 for( size_t i=0; i<ARRAY_SIZE(menus_id_mapping); i++ )
1270 s = vlc_seekpoint_New();
1271 if(!s)
1272 break;
1273 s->psz_name = strdup( menus_id_mapping[i].psz_name );
1274 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1277 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1279 /* Find out number of titles/chapters */
1280 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
1282 if( i_titles > 90 )
1283 msg_Err( p_demux, "This is probably an Arccos Protected DVD. This could take time..." );
1285 for( int i = 1; i <= i_titles; i++ )
1287 uint64_t i_title_length;
1288 uint64_t *p_chapters_time;
1290 int32_t i_chapters = dvdnav_describe_title_chapters( p_sys->dvdnav, i,
1291 &p_chapters_time,
1292 &i_title_length );
1293 if( i_chapters < 1 )
1295 i_title_length = 0;
1296 p_chapters_time = NULL;
1298 t = vlc_input_title_New();
1299 t->i_length = FROM_SCALE_NZ(i_title_length);
1300 for( int j = 0; j < __MAX( i_chapters, 1 ); j++ )
1302 s = vlc_seekpoint_New();
1303 if( p_chapters_time )
1305 if ( j > 0 )
1306 s->i_time_offset = FROM_SCALE_NZ(p_chapters_time[j - 1]);
1307 else
1308 s->i_time_offset = 0;
1310 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1312 free( p_chapters_time );
1313 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1317 /*****************************************************************************
1318 * Update functions:
1319 *****************************************************************************/
1320 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1322 demux_sys_t *p_sys = p_demux->p_sys;
1323 int32_t i_title, i_part;
1324 int i_ret;
1326 p_sys->highlight.b_pending = true;
1327 p_sys->highlight.b_from_user = b_mode;
1329 if( !p_sys->spu_es )
1330 return;
1332 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part )
1333 != DVDNAV_STATUS_OK )
1334 return;
1336 dvdnav_highlight_area_t hl;
1337 int32_t i_button;
1338 bool b_button_ok;
1340 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1341 != DVDNAV_STATUS_OK )
1343 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1344 return;
1347 b_button_ok = false;
1348 if( i_button > 0 && i_title == 0 )
1350 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1352 b_button_ok = DVDNAV_STATUS_OK ==
1353 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
1356 if( b_button_ok )
1358 vlc_spu_highlight_t spu_hl = {
1359 .x_start = hl.sx,
1360 .x_end = hl.ex,
1361 .y_start = hl.sy,
1362 .y_end = hl.ey,
1363 .palette = {
1364 .i_entries = 4,
1368 for( unsigned i = 0; i < 4; i++ )
1370 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1371 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1373 spu_hl.palette.palette[i][0] = (i_yuv >> 16) & 0xff;
1374 spu_hl.palette.palette[i][1] = (i_yuv >> 0) & 0xff;
1375 spu_hl.palette.palette[i][2] = (i_yuv >> 8) & 0xff;
1376 spu_hl.palette.palette[i][3] = i_alpha;
1379 i_ret = es_out_Control( p_sys->p_tf_out, ES_OUT_SPU_SET_HIGHLIGHT,
1380 p_sys->spu_es, &spu_hl );
1382 else
1384 i_ret = es_out_Control( p_sys->p_tf_out, ES_OUT_SPU_SET_HIGHLIGHT,
1385 p_sys->spu_es, NULL );
1388 if( i_ret == VLC_SUCCESS )
1390 msg_Dbg( p_demux, "menu highlight %s button=%d title=%d",
1391 b_button_ok ? "set" : "cleared",
1392 i_button, i_title );
1393 p_sys->highlight.b_pending = false;
1397 static void ESSubtitleUpdate( demux_t *p_demux )
1399 demux_sys_t *p_sys = p_demux->p_sys;
1400 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1401 int32_t i_title, i_part;
1403 vlc_mutex_lock(&p_sys->event_lock);
1404 ButtonUpdate( p_demux, false );
1405 vlc_mutex_unlock(&p_sys->event_lock);
1407 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part )
1408 != DVDNAV_STATUS_OK || i_title > 0 )
1409 return;
1411 /* dvdnav_get_active_spu_stream sets (in)visibility flag as 0xF0 */
1412 if( i_spu >= 0 && i_spu <= 0x1f )
1414 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(0xbd20 + i_spu)];
1416 ESNew( p_demux, 0xbd20 + i_spu );
1418 /* be sure to unselect it (reset) */
1419 if( tk->es )
1421 es_out_Control( p_sys->p_tf_out, ES_OUT_SET_ES_STATE, tk->es,
1422 (bool)false );
1424 /* now select it */
1425 es_out_Control( p_sys->p_tf_out, ES_OUT_SET_ES, tk->es );
1427 if( tk->fmt.i_cat == SPU_ES )
1429 vlc_mutex_lock( &p_sys->event_lock );
1430 p_sys->spu_es = tk->es;
1431 ButtonUpdate( p_demux, false );
1432 vlc_mutex_unlock( &p_sys->event_lock );
1436 else
1438 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1440 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(0xbd20 + i_spu)];
1441 if( tk->es )
1443 es_out_Control( p_sys->p_tf_out, ES_OUT_SET_ES_STATE, tk->es,
1444 (bool)false );
1450 /*****************************************************************************
1451 * DemuxBlock: demux a given block
1452 *****************************************************************************/
1453 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
1455 demux_sys_t *p_sys = p_demux->p_sys;
1457 while( len > 0 )
1459 int i_size = ps_pkt_size( p, len );
1460 if( i_size <= 0 || i_size > len )
1462 break;
1465 /* Create a block */
1466 block_t *p_pkt = block_Alloc( i_size );
1467 if( !p_pkt )
1468 return VLC_EGENERIC;
1469 memcpy( p_pkt->p_buffer, p, i_size);
1471 /* Parse it and send it */
1472 switch( 0x100 | p[3] )
1474 case 0x1b9:
1475 case 0x1bb:
1476 case 0x1bc:
1477 #ifdef DVDNAV_DEBUG
1478 if( p[3] == 0xbc )
1480 msg_Warn( p_demux, "received a PSM packet" );
1482 else if( p[3] == 0xbb )
1484 msg_Warn( p_demux, "received a SYSTEM packet" );
1486 #endif
1487 block_Release( p_pkt );
1488 break;
1490 case 0x1ba:
1492 vlc_tick_t i_scr;
1493 int i_mux_rate;
1494 if( !ps_pkt_parse_pack( p_pkt->p_buffer, p_pkt->i_buffer,
1495 &i_scr, &i_mux_rate ) )
1497 es_out_SetPCR( p_sys->p_tf_out, i_scr );
1498 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1500 block_Release( p_pkt );
1501 break;
1503 default:
1505 int i_id = ps_pkt_id( p_pkt->p_buffer, p_pkt->i_buffer );
1506 if( i_id >= 0xc0 )
1508 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(i_id)];
1510 if( !tk->b_configured )
1512 ESNew( p_demux, i_id );
1515 if( tk->es &&
1516 !ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, tk->i_skip ) )
1518 int i_next_block_flags = tk->i_next_block_flags;
1519 tk->i_next_block_flags = 0;
1520 if( i_next_block_flags & BLOCK_FLAG_CELL_DISCONTINUITY )
1522 if( p_pkt->i_dts != VLC_TICK_INVALID )
1524 i_next_block_flags &= ~BLOCK_FLAG_CELL_DISCONTINUITY;
1525 i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1527 else tk->i_next_block_flags = BLOCK_FLAG_CELL_DISCONTINUITY;
1529 p_pkt->i_flags |= i_next_block_flags;
1530 es_out_Send( p_sys->p_tf_out, tk->es, p_pkt );
1532 else
1534 tk->i_next_block_flags = 0;
1535 block_Release( p_pkt );
1538 else
1540 block_Release( p_pkt );
1542 break;
1546 p += i_size;
1547 len -= i_size;
1550 return VLC_SUCCESS;
1553 /*****************************************************************************
1554 * Force still images to be displayed by sending EOS and stopping buffering.
1555 *****************************************************************************/
1556 static void DemuxForceStill( demux_t *p_demux )
1558 demux_sys_t *p_sys = p_demux->p_sys;
1560 static const uint8_t buffer[] = {
1561 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
1562 0x80, 0x00, 0x00,
1563 0x00, 0x00, 0x01, 0xB7,
1565 DemuxBlock( p_demux, buffer, sizeof(buffer) );
1567 bool b_empty;
1568 es_out_Control( p_sys->p_tf_out, ES_OUT_GET_EMPTY, &b_empty );
1571 /*****************************************************************************
1572 * ESNew: register a new elementary stream
1573 *****************************************************************************/
1574 static void ESNew( demux_t *p_demux, int i_id )
1576 demux_sys_t *p_sys = p_demux->p_sys;
1577 ps_track_t *tk = &p_sys->tk[ps_id_to_tk(i_id)];
1578 bool b_select = false;
1579 int i_lang = 0xffff;
1581 if( tk->b_configured ) return;
1583 if( ps_track_fill( tk, 0, i_id, NULL, 0, true ) )
1585 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1586 return;
1589 /* Add a new ES */
1590 if( tk->fmt.i_cat == VIDEO_ES )
1592 tk->fmt.video.i_sar_num = p_sys->sar.i_num;
1593 tk->fmt.video.i_sar_den = p_sys->sar.i_den;
1594 b_select = true;
1596 else if( tk->fmt.i_cat == AUDIO_ES )
1598 int i_audio = -1;
1599 /* find the audio number PLEASE find another way */
1600 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1602 i_audio = i_id&0x07;
1604 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1606 i_audio = i_id&0xf;
1608 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1610 i_audio = i_id&0x1f;
1612 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1614 i_audio = i_id&0x1f;
1616 if( i_audio >= 0 )
1618 i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1619 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1621 b_select = true;
1625 else if( tk->fmt.i_cat == SPU_ES )
1627 int32_t i_title, i_part;
1628 i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1630 /* Palette */
1631 tk->fmt.subs.spu.palette[0] = SPU_PALETTE_DEFINED;
1632 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1633 16 * sizeof( uint32_t ) );
1635 /* We select only when we are not in the menu */
1636 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part ) == DVDNAV_STATUS_OK &&
1637 i_title > 0 &&
1638 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1640 b_select = true;
1644 if( i_lang != 0xffff )
1646 tk->fmt.psz_language = malloc( 3 );
1647 if( tk->fmt.psz_language )
1649 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1650 tk->fmt.psz_language[1] = (i_lang )&0xff;
1651 tk->fmt.psz_language[2] = 0;
1655 tk->fmt.i_id = i_id;
1656 tk->es = es_out_Add( p_sys->p_tf_out, &tk->fmt );
1657 if( b_select && tk->es )
1659 es_out_Control( p_sys->p_tf_out, ES_OUT_SET_ES, tk->es );
1661 if( tk->fmt.i_cat == VIDEO_ES )
1663 es_out_Control( p_sys->p_tf_out, ES_OUT_VOUT_SET_MOUSE_EVENT, tk->es,
1664 EventMouse, p_demux );
1665 vlc_mutex_lock( &p_sys->event_lock );
1666 ButtonUpdate( p_demux, false );
1667 vlc_mutex_unlock( &p_sys->event_lock );
1670 tk->b_configured = true;
1673 /*****************************************************************************
1674 * Still image end
1675 *****************************************************************************/
1676 static void StillTimer( void *p_data )
1678 demux_sys_t *p_sys = p_data;
1680 vlc_mutex_lock( &p_sys->still.lock );
1681 if( likely(p_sys->still.b_enabled) )
1683 p_sys->still.b_enabled = false;
1684 dvdnav_still_skip( p_sys->dvdnav );
1686 vlc_mutex_unlock( &p_sys->still.lock );
1689 static void EventMouse( const vlc_mouse_t *newmouse, void *p_data )
1691 demux_t *p_demux = p_data;
1692 demux_sys_t *p_sys = p_demux->p_sys;
1694 if( !newmouse )
1696 vlc_mouse_Init( &p_sys->oldmouse );
1697 return;
1700 /* FIXME? PCI usage thread safe? */
1701 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1703 if( vlc_mouse_HasMoved( &p_sys->oldmouse, newmouse ) )
1704 dvdnav_mouse_select( p_sys->dvdnav, pci, newmouse->i_x, newmouse->i_y );
1705 if( vlc_mouse_HasPressed( &p_sys->oldmouse, newmouse, MOUSE_BUTTON_LEFT ) )
1707 vlc_mutex_lock( &p_sys->event_lock );
1708 ButtonUpdate( p_demux, true );
1709 vlc_mutex_unlock( &p_sys->event_lock );
1710 dvdnav_mouse_activate( p_sys->dvdnav, pci, newmouse->i_x, newmouse->i_y );
1712 p_sys->oldmouse = *newmouse;
1715 /*****************************************************************************
1716 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1717 *****************************************************************************/
1718 static int ProbeDVD( const char *psz_name )
1720 if( !*psz_name )
1721 /* Triggers libdvdcss autodetection */
1722 return VLC_SUCCESS;
1724 int fd = vlc_open( psz_name, O_RDONLY | O_NONBLOCK );
1725 if( fd == -1 )
1726 #ifdef HAVE_FDOPENDIR
1727 return VLC_EGENERIC;
1728 #else
1729 return (errno == ENOENT) ? VLC_EGENERIC : VLC_SUCCESS;
1730 #endif
1732 int ret = VLC_EGENERIC;
1733 struct stat stat_info;
1735 if( fstat( fd, &stat_info ) == -1 )
1736 goto bailout;
1737 if( !S_ISREG( stat_info.st_mode ) )
1739 if( S_ISDIR( stat_info.st_mode ) || S_ISBLK( stat_info.st_mode ) )
1740 ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1741 goto bailout;
1744 /* ISO 9660 volume descriptor */
1745 char iso_dsc[6];
1746 if( lseek( fd, 0x8000 + 1, SEEK_SET ) == -1
1747 || read( fd, iso_dsc, sizeof (iso_dsc) ) < (int)sizeof (iso_dsc)
1748 || memcmp( iso_dsc, "CD001\x01", 6 ) )
1749 goto bailout;
1751 /* Try to find the anchor (2 bytes at LBA 256) */
1752 uint16_t anchor;
1754 if( lseek( fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1755 && read( fd, &anchor, 2 ) == 2
1756 && GetWLE( &anchor ) == 2 )
1757 ret = VLC_SUCCESS; /* Found a potential anchor */
1758 bailout:
1759 vlc_close( fd );
1760 return ret;