1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004-2009 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_demux.h>
38 #include <vlc_charset.h>
40 #include <vlc_dialog.h>
45 #ifdef HAVE_SYS_TYPES_H
46 # include <sys/types.h>
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
56 #include <vlc_iso_lang.h>
58 /* FIXME we should find a better way than including that */
59 #include "../../src/text/iso-639_def.h"
62 #include <dvdnav/dvdnav.h>
64 #include "../demux/ps.h"
66 /*****************************************************************************
68 *****************************************************************************/
69 #define ANGLE_TEXT N_("DVD angle")
70 #define ANGLE_LONGTEXT N_( \
71 "Default DVD angle." )
73 #define CACHING_TEXT N_("Caching value in ms")
74 #define CACHING_LONGTEXT N_( \
75 "Caching value for DVDs. This "\
76 "value should be set in milliseconds." )
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 Open ( vlc_object_t * );
85 static void Close( vlc_object_t * );
88 set_shortname( N_("DVD with menus") )
89 set_description( N_("DVDnav Input") )
90 set_category( CAT_INPUT )
91 set_subcategory( SUBCAT_INPUT_ACCESS )
92 add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
93 ANGLE_LONGTEXT, false )
94 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
95 CACHING_TEXT, CACHING_LONGTEXT, true )
96 add_bool( "dvdnav-menu", true, NULL,
97 MENU_TEXT, MENU_LONGTEXT, false )
98 set_capability( "access_demux", 5 )
100 add_shortcut( "dvdnav" )
101 add_shortcut( "file" )
102 set_callbacks( Open, Close )
105 /* Shall we use libdvdnav's read ahead cache? */
106 #define DVD_READ_CACHE 1
108 /*****************************************************************************
110 *****************************************************************************/
127 ps_track_t tk[PS_TK_COUNT];
130 /* for spu variables */
131 input_thread_t *p_input;
134 vlc_object_t *p_vout;
136 /* palette for menus */
138 uint8_t palette[4][4];
145 input_title_t **title;
147 /* lenght of program group chain */
148 mtime_t i_pgc_length;
151 static int Control( demux_t *, int, va_list );
152 static int Demux( demux_t * );
153 static int DemuxBlock( demux_t *, const uint8_t *, int );
155 static void DemuxTitles( demux_t * );
156 static void ESSubtitleUpdate( demux_t * );
157 static void ButtonUpdate( demux_t *, bool );
159 static void ESNew( demux_t *, int );
160 static int ProbeDVD( demux_t *, char * );
162 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
164 static int ControlInternal( demux_t *, int, ... );
166 static void StillTimer( void * );
168 static int EventKey( vlc_object_t *, char const *,
169 vlc_value_t, vlc_value_t, void * );
170 static int EventMouse( vlc_object_t *, char const *,
171 vlc_value_t, vlc_value_t, void * );
172 static int EventIntf( vlc_object_t *, char const *,
173 vlc_value_t, vlc_value_t, void * );
175 /*****************************************************************************
177 *****************************************************************************/
178 static int Open( vlc_object_t *p_this )
180 demux_t *p_demux = (demux_t*)p_this;
188 if( !p_demux->psz_path || !*p_demux->psz_path )
190 /* Only when selected */
191 if( !p_this->b_force ) return VLC_EGENERIC;
193 psz_name = var_CreateGetString( p_this, "dvd" );
196 psz_name = strdup("");
200 psz_name = ToLocaleDup( p_demux->psz_path );
203 if( psz_name[0] && psz_name[1] == ':' &&
204 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
207 /* Try some simple probing to avoid going through dvdnav_open too often */
208 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
215 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
217 msg_Warn( p_demux, "cannot open dvdnav" );
223 /* Fill p_demux field */
224 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
225 p_sys->dvdnav = p_dvdnav;
226 p_sys->b_reset_pcr = false;
228 ps_track_init( p_sys->tk );
229 p_sys->i_aspect = -1;
230 p_sys->i_mux_rate = 0;
231 p_sys->i_pgc_length = 0;
232 p_sys->b_spu_change = false;
236 // Hack for libdvdnav CVS.
237 // Without it dvdnav_get_number_of_titles() fails.
238 // Remove when fixed in libdvdnav CVS.
239 uint8_t buffer[DVD_VIDEO_LB_LEN];
242 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
243 == DVDNAV_STATUS_ERR )
245 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
248 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
251 /* Configure dvdnav */
252 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
255 msg_Warn( p_demux, "cannot set read-a-head flag" );
258 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
261 msg_Warn( p_demux, "cannot set PGC positioning flag" );
265 * XXX A menu-language may be better than sub-language */
266 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
267 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
270 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
271 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
272 /* We try to fall back to 'en' */
273 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
274 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
278 /* Set audio language */
279 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
280 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
283 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
284 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
285 /* We try to fall back to 'en' */
286 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
287 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
291 /* Set spu language */
292 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
293 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
296 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
297 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
298 /* We try to fall back to 'en' */
299 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
300 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
304 DemuxTitles( p_demux );
306 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
307 var_Get( p_demux, "dvdnav-menu", &val );
310 msg_Dbg( p_demux, "trying to go to dvd menu" );
312 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
314 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
315 dialog_Fatal( p_demux, _("Playback failure"), "%s",
316 _("VLC cannot set the DVD's title. It possibly "
317 "cannot decrypt the entire disc.") );
318 dvdnav_close( p_sys->dvdnav );
323 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
326 /* Try going to menu root */
327 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
329 msg_Warn( p_demux, "cannot go to dvd menu" );
333 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
334 var_Get( p_demux, "dvdnav-angle", &val );
335 i_angle = val.i_int > 0 ? val.i_int : 1;
337 /* Update default_pts to a suitable value for dvdnav access */
338 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
340 /* FIXME hack hack hack hack FIXME */
341 /* Get p_input and create variable */
342 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
343 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
344 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
345 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
346 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
347 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
348 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
349 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
350 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
352 /* catch all key event */
353 var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
354 /* catch vout creation event */
355 var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
357 p_sys->still.b_enabled = false;
358 vlc_mutex_init( &p_sys->still.lock );
359 if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
360 p_sys->still.b_created = true;
365 /*****************************************************************************
367 *****************************************************************************/
368 static void Close( vlc_object_t *p_this )
370 demux_t *p_demux = (demux_t*)p_this;
371 demux_sys_t *p_sys = p_demux->p_sys;
374 /* Stop vout event handler */
375 var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
376 if( p_sys->p_vout != NULL )
377 { /* Should not happen, but better be safe than sorry. */
378 msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
379 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
380 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
383 /* Stop key event handler (FIXME: should really be per-vout too) */
384 var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
386 /* Stop still image handler */
387 if( p_sys->still.b_created )
388 vlc_timer_destroy( &p_sys->still.timer );
389 vlc_mutex_destroy( &p_sys->still.lock );
391 var_Destroy( p_sys->p_input, "highlight-mutex" );
392 var_Destroy( p_sys->p_input, "highlight" );
393 var_Destroy( p_sys->p_input, "x-start" );
394 var_Destroy( p_sys->p_input, "x-end" );
395 var_Destroy( p_sys->p_input, "y-start" );
396 var_Destroy( p_sys->p_input, "y-end" );
397 var_Destroy( p_sys->p_input, "color" );
398 var_Destroy( p_sys->p_input, "menu-palette" );
400 vlc_object_release( p_sys->p_input );
402 for( i = 0; i < PS_TK_COUNT; i++ )
404 ps_track_t *tk = &p_sys->tk[i];
407 es_format_Clean( &tk->fmt );
408 if( tk->es ) es_out_Del( p_demux->out, tk->es );
412 dvdnav_close( p_sys->dvdnav );
416 /*****************************************************************************
418 *****************************************************************************/
419 static int Control( demux_t *p_demux, int i_query, va_list args )
421 demux_sys_t *p_sys = p_demux->p_sys;
422 input_title_t ***ppp_title;
427 case DEMUX_SET_POSITION:
428 case DEMUX_GET_POSITION:
430 case DEMUX_GET_LENGTH:
433 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
434 DVDNAV_STATUS_OK || len == 0 )
441 case DEMUX_GET_POSITION:
442 *va_arg( args, double* ) = (double)pos / (double)len;
445 case DEMUX_SET_POSITION:
446 pos = va_arg( args, double ) * len;
447 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
455 if( p_sys->i_pgc_length > 0 )
457 *va_arg( args, int64_t * ) = p_sys->i_pgc_length*pos/len;
462 case DEMUX_GET_LENGTH:
463 if( p_sys->i_pgc_length > 0 )
465 *va_arg( args, int64_t * ) = (int64_t)p_sys->i_pgc_length;
473 /* Special for access_demux */
474 case DEMUX_CAN_PAUSE:
476 case DEMUX_CAN_CONTROL_PACE:
478 *va_arg( args, bool * ) = true;
481 case DEMUX_SET_PAUSE_STATE:
484 case DEMUX_GET_TITLE_INFO:
485 ppp_title = va_arg( args, input_title_t*** );
486 *va_arg( args, int* ) = p_sys->i_title;
487 *va_arg( args, int* ) = 0; /* Title offset */
488 *va_arg( args, int* ) = 1; /* Chapter offset */
490 /* Duplicate title infos */
491 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
492 for( i = 0; i < p_sys->i_title; i++ )
494 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
498 case DEMUX_SET_TITLE:
499 i = (int)va_arg( args, int );
500 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
501 != DVDNAV_STATUS_OK ) ||
502 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
503 != DVDNAV_STATUS_OK ) )
505 msg_Warn( p_demux, "cannot set title/chapter" );
508 p_demux->info.i_update |=
509 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
510 p_demux->info.i_title = i;
511 p_demux->info.i_seekpoint = 0;
514 case DEMUX_SET_SEEKPOINT:
515 i = va_arg( args, int );
516 if( p_demux->info.i_title == 0 )
518 static const int argtab[] = {
527 enum { numargs = sizeof(argtab)/sizeof(int) };
528 if( (unsigned)i >= numargs || DVDNAV_STATUS_OK !=
529 dvdnav_menu_call(p_sys->dvdnav,argtab[i]) )
532 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
533 i + 1 ) != DVDNAV_STATUS_OK )
535 msg_Warn( p_demux, "cannot set title/chapter" );
538 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
539 p_demux->info.i_seekpoint = i;
542 case DEMUX_GET_PTS_DELAY:
543 *va_arg( args, int64_t * )
544 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
549 const char *title_name = NULL;
551 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
552 if( (NULL != title_name) && ('\0' != title_name[0]) )
554 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
555 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
561 /* TODO implement others */
567 static int ControlInternal( demux_t *p_demux, int i_query, ... )
572 va_start( args, i_query );
573 i_result = Control( p_demux, i_query, args );
578 /*****************************************************************************
580 *****************************************************************************/
581 static int Demux( demux_t *p_demux )
583 demux_sys_t *p_sys = p_demux->p_sys;
585 uint8_t buffer[DVD_VIDEO_LB_LEN];
586 uint8_t *packet = buffer;
591 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
592 == DVDNAV_STATUS_ERR )
594 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
595 == DVDNAV_STATUS_ERR )
598 msg_Warn( p_demux, "cannot get next block (%s)",
599 dvdnav_err_to_string( p_sys->dvdnav ) );
600 if( p_demux->info.i_title == 0 )
602 msg_Dbg( p_demux, "jumping to first title" );
603 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
610 case DVDNAV_BLOCK_OK: /* mpeg block */
611 vlc_mutex_lock( &p_sys->still.lock );
612 p_sys->still.b_enabled = false;
613 vlc_mutex_unlock( &p_sys->still.lock );
614 if( p_sys->b_reset_pcr )
616 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
617 p_sys->b_reset_pcr = false;
619 DemuxBlock( p_demux, packet, i_len );
622 case DVDNAV_NOP: /* Nothing */
623 msg_Dbg( p_demux, "DVDNAV_NOP" );
626 case DVDNAV_STILL_FRAME:
628 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
629 bool b_still_init = false;
631 vlc_mutex_lock( &p_sys->still.lock );
632 if( !p_sys->still.b_enabled )
634 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
635 msg_Dbg( p_demux, " - length=0x%x", event->length );
636 p_sys->still.b_enabled = true;
638 if( event->length != 0xff && p_sys->still.b_created )
640 mtime_t delay = event->length * CLOCK_FREQ;
641 vlc_timer_schedule( &p_sys->still.timer, false, delay, 0 );
646 vlc_mutex_unlock( &p_sys->still.lock );
650 /* We send a dummy mpeg2 end of sequence to force still frame display */
651 static const uint8_t buffer[] = {
652 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
654 0x00, 0x00, 0x01, 0xB7,
656 DemuxBlock( p_demux, buffer, sizeof(buffer) );
659 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
660 p_sys->b_reset_pcr = true;
666 case DVDNAV_SPU_CLUT_CHANGE:
670 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
671 /* Update color lookup table (16 *uint32_t in packet) */
672 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
674 /* HACK to get the SPU tracks registered in the right order */
675 for( i = 0; i < 0x1f; i++ )
677 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
678 ESNew( p_demux, 0xbd20 + i );
684 case DVDNAV_SPU_STREAM_CHANGE:
686 dvdnav_spu_stream_change_event_t *event =
687 (dvdnav_spu_stream_change_event_t*)packet;
690 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
691 msg_Dbg( p_demux, " - physical_wide=%d",
692 event->physical_wide );
693 msg_Dbg( p_demux, " - physical_letterbox=%d",
694 event->physical_letterbox);
695 msg_Dbg( p_demux, " - physical_pan_scan=%d",
696 event->physical_pan_scan );
698 ESSubtitleUpdate( p_demux );
699 p_sys->b_spu_change = true;
701 /* HACK to get the SPU tracks registered in the right order */
702 for( i = 0; i < 0x1f; i++ )
704 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
705 ESNew( p_demux, 0xbd20 + i );
711 case DVDNAV_AUDIO_STREAM_CHANGE:
713 dvdnav_audio_stream_change_event_t *event =
714 (dvdnav_audio_stream_change_event_t*)packet;
715 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
716 msg_Dbg( p_demux, " - physical=%d", event->physical );
721 case DVDNAV_VTS_CHANGE:
727 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
728 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
729 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
730 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
732 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
733 /* TODO check if we always have VTS and CELL */
734 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
737 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
739 for( i = 0; i < PS_TK_COUNT; i++ )
741 ps_track_t *tk = &p_sys->tk[i];
744 es_format_Clean( &tk->fmt );
745 if( tk->es ) es_out_Del( p_demux->out, tk->es );
750 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
751 &i_part ) == DVDNAV_STATUS_OK )
753 if( i_title >= 0 && i_title < p_sys->i_title &&
754 p_demux->info.i_title != i_title )
756 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
757 p_demux->info.i_title = i_title;
763 case DVDNAV_CELL_CHANGE:
768 dvdnav_cell_change_event_t *event =
769 (dvdnav_cell_change_event_t*)packet;
770 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
771 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
772 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
773 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
774 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
775 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
776 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
777 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
779 /* Store the lenght in time of the current PGC */
780 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
782 /* FIXME is it correct or there is better way to know chapter change */
783 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
784 &i_part ) == DVDNAV_STATUS_OK )
786 if( i_title >= 0 && i_title < p_sys->i_title &&
787 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
788 p_demux->info.i_seekpoint != i_part - 1 )
790 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
791 p_demux->info.i_seekpoint = i_part - 1;
797 case DVDNAV_NAV_PACKET:
800 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
802 /* A lot of thing to do here :
804 * - fetch pts (for time display)
807 DemuxBlock( p_demux, packet, i_len );
808 if( p_sys->b_spu_change )
810 ButtonUpdate( p_demux, false );
811 p_sys->b_spu_change = false;
816 case DVDNAV_STOP: /* EOF */
817 msg_Dbg( p_demux, "DVDNAV_STOP" );
820 dvdnav_free_cache_block( p_sys->dvdnav, packet );
824 case DVDNAV_HIGHLIGHT:
826 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
827 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
828 msg_Dbg( p_demux, " - display=%d", event->display );
829 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
830 ButtonUpdate( p_demux, false );
834 case DVDNAV_HOP_CHANNEL:
835 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
836 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
840 msg_Dbg( p_demux, "DVDNAV_WAIT" );
843 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
850 dvdnav_wait_skip( p_sys->dvdnav );
851 p_sys->b_reset_pcr = true;
856 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
861 dvdnav_free_cache_block( p_sys->dvdnav, packet );
868 * FIXME: partiallyy duplicated from src/input/es_out.c
870 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
872 const iso639_lang_t *pl;
876 psz_lang = var_CreateGetString( p_demux, psz_var );
878 return strdup(LANGUAGE_DEFAULT);
880 /* XXX: we will use only the first value
881 * (and ignore other ones in case of a list) */
882 if( ( p = strchr( psz_lang, ',' ) ) )
885 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
887 if( *psz_lang == '\0' )
889 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
890 !strcasecmp( pl->psz_native_name, psz_lang ) ||
891 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
892 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
893 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
899 if( pl->psz_eng_name != NULL )
900 return strdup( pl->psz_iso639_1 );
902 return strdup(LANGUAGE_DEFAULT);
905 static void DemuxTitles( demux_t *p_demux )
907 demux_sys_t *p_sys = p_demux->p_sys;
914 t = vlc_input_title_New();
916 t->psz_name = strdup( "DVD Menu" );
918 s = vlc_seekpoint_New();
919 s->psz_name = strdup( "Resume" );
920 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
922 s = vlc_seekpoint_New();
923 s->psz_name = strdup( "Root" );
924 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
926 s = vlc_seekpoint_New();
927 s->psz_name = strdup( "Title" );
928 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
930 s = vlc_seekpoint_New();
931 s->psz_name = strdup( "Chapter" );
932 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
934 s = vlc_seekpoint_New();
935 s->psz_name = strdup( "Subtitle" );
936 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
938 s = vlc_seekpoint_New();
939 s->psz_name = strdup( "Audio" );
940 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
942 s = vlc_seekpoint_New();
943 s->psz_name = strdup( "Angle" );
944 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
946 TAB_APPEND( p_sys->i_title, p_sys->title, t );
948 /* Find out number of titles/chapters */
949 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
950 for( i = 1; i <= i_titles; i++ )
952 int32_t i_chapters = 0;
955 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
957 t = vlc_input_title_New();
958 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
960 s = vlc_seekpoint_New();
961 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
964 TAB_APPEND( p_sys->i_title, p_sys->title, t );
968 /*****************************************************************************
970 *****************************************************************************/
971 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
973 demux_sys_t *p_sys = p_demux->p_sys;
975 int32_t i_title, i_part;
977 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
979 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
981 vlc_mutex_t *p_mutex = val.p_address;
982 dvdnav_highlight_area_t hl;
986 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
987 != DVDNAV_STATUS_OK )
989 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
994 if( i_button > 0 && i_title == 0 )
996 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
998 b_button_ok = DVDNAV_STATUS_OK ==
999 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
1005 for( i = 0; i < 4; i++ )
1007 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1008 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1010 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1011 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1012 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1013 p_sys->palette[i][3] = i_alpha;
1016 vlc_mutex_lock( p_mutex );
1017 var_SetInteger( p_sys->p_input, "x-start", hl.sx );
1018 var_SetInteger( p_sys->p_input, "x-end", hl.ex );
1019 var_SetInteger( p_sys->p_input, "y-start", hl.sy );
1020 var_SetInteger( p_sys->p_input, "y-end", hl.ey );
1022 var_SetAddress( p_sys->p_input, "menu-palette", p_sys->palette );
1024 var_SetBool( p_sys->p_input, "highlight", true );
1025 vlc_mutex_unlock( p_mutex );
1027 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1031 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1032 i_button, i_title );
1035 vlc_mutex_lock( p_mutex );
1036 var_SetBool( p_sys->p_input, "highlight", false );
1037 vlc_mutex_unlock( p_mutex );
1042 static void ESSubtitleUpdate( demux_t *p_demux )
1044 demux_sys_t *p_sys = p_demux->p_sys;
1045 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1046 int32_t i_title, i_part;
1048 ButtonUpdate( p_demux, false );
1050 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1051 if( i_title > 0 ) return;
1053 if( i_spu >= 0 && i_spu <= 0x1f )
1055 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1057 ESNew( p_demux, 0xbd20 + i_spu );
1059 /* be sure to unselect it (reset) */
1060 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1064 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1068 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1070 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1073 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1080 /*****************************************************************************
1081 * DemuxBlock: demux a given block
1082 *****************************************************************************/
1083 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1085 demux_sys_t *p_sys = p_demux->p_sys;
1086 const uint8_t *p = pkt;
1088 while( p < &pkt[i_pkt] )
1090 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1097 /* Create a block */
1098 p_pkt = block_New( p_demux, i_size );
1099 memcpy( p_pkt->p_buffer, p, i_size);
1101 /* Parse it and send it */
1102 switch( 0x100 | p[3] )
1110 msg_Warn( p_demux, "received a PSM packet" );
1112 else if( p[3] == 0xbb )
1114 msg_Warn( p_demux, "received a SYSTEM packet" );
1117 block_Release( p_pkt );
1124 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1126 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1127 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1129 block_Release( p_pkt );
1134 int i_id = ps_pkt_id( p_pkt );
1137 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1141 ESNew( p_demux, i_id );
1143 if( tk->b_seen && tk->es &&
1144 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1146 es_out_Send( p_demux->out, tk->es, p_pkt );
1150 block_Release( p_pkt );
1155 block_Release( p_pkt );
1167 /*****************************************************************************
1168 * ESNew: register a new elementary stream
1169 *****************************************************************************/
1170 static void ESNew( demux_t *p_demux, int i_id )
1172 demux_sys_t *p_sys = p_demux->p_sys;
1173 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1174 bool b_select = false;
1176 if( tk->b_seen ) return;
1178 if( ps_track_fill( tk, 0, i_id ) )
1180 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1185 if( tk->fmt.i_cat == VIDEO_ES )
1187 switch( p_sys->i_aspect )
1189 case 1: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR; break;
1190 case 2: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3; break;
1191 case 3: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9; break;
1192 case 4: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 221 / 10; break;
1194 tk->fmt.video.i_aspect = 0;
1199 else if( tk->fmt.i_cat == AUDIO_ES )
1202 /* find the audio number PLEASE find another way */
1203 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1205 i_audio = i_id&0x07;
1207 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1211 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1213 i_audio = i_id&0x1f;
1215 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1217 i_audio = i_id&0x1f;
1221 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1222 if( i_lang != 0xffff )
1224 tk->fmt.psz_language = malloc( 3 );
1225 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1226 tk->fmt.psz_language[1] = (i_lang )&0xff;
1227 tk->fmt.psz_language[2] = 0;
1229 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1235 else if( tk->fmt.i_cat == SPU_ES )
1237 int32_t i_title, i_part;
1238 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1239 if( i_lang != 0xffff )
1241 tk->fmt.psz_language = malloc( 3 );
1242 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1243 tk->fmt.psz_language[1] = (i_lang )&0xff;
1244 tk->fmt.psz_language[2] = 0;
1248 tk->fmt.subs.spu.palette[0] = 0xBeef;
1249 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1250 16 * sizeof( uint32_t ) );
1252 /* We select only when we are not in the menu */
1253 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1255 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1261 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1264 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1268 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1271 /*****************************************************************************
1273 *****************************************************************************/
1274 static void StillTimer( void *p_data )
1276 demux_sys_t *p_sys = p_data;
1278 vlc_mutex_lock( &p_sys->still.lock );
1279 assert( p_sys->still.b_enabled );
1280 p_sys->still.b_enabled = false;
1281 dvdnav_still_skip( p_sys->dvdnav );
1282 vlc_mutex_unlock( &p_sys->still.lock );
1285 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1286 vlc_value_t oldval, vlc_value_t val, void *p_data )
1288 demux_t *p_demux = p_data;
1289 demux_sys_t *p_sys = p_demux->p_sys;
1291 /* FIXME? PCI usage thread safe? */
1292 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1293 int x = var_GetInteger( p_vout, "mouse-x" );
1294 int y = var_GetInteger( p_vout, "mouse-y" );
1296 if( psz_var[6] == 'm' ) /* mouse-moved */
1297 dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1300 assert( psz_var[6] == 'c' ); /* mouse-clicked */
1302 ButtonUpdate( p_demux, true );
1303 dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1306 (void)oldval; (void)val;
1310 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1311 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1313 demux_t *p_demux = p_data;
1314 demux_sys_t *p_sys = p_demux->p_sys;
1316 /* FIXME: thread-safe ? */
1317 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1319 switch( newval.i_int )
1321 case ACTIONID_NAV_LEFT:
1322 dvdnav_left_button_select( p_sys->dvdnav, pci );
1324 case ACTIONID_NAV_RIGHT:
1325 dvdnav_right_button_select( p_sys->dvdnav, pci );
1327 case ACTIONID_NAV_UP:
1328 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1330 case ACTIONID_NAV_DOWN:
1331 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1333 case ACTIONID_NAV_ACTIVATE:
1334 ButtonUpdate( p_demux, true );
1335 dvdnav_button_activate( p_sys->dvdnav, pci );
1339 (void)p_this; (void)psz_var; (void)oldval;
1343 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1344 vlc_value_t oldval, vlc_value_t val, void *p_data )
1346 demux_t *p_demux = p_data;
1347 demux_sys_t *p_sys = p_demux->p_sys;
1349 if (val.i_int == INPUT_EVENT_VOUT)
1351 vlc_object_t *p_vout;
1353 p_vout = p_sys->p_vout;
1354 if( p_vout != NULL )
1356 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1357 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1358 vlc_object_release( p_vout );
1361 p_vout = (vlc_object_t *)input_GetVout( (input_thread_t *)p_input );
1362 p_sys->p_vout = p_vout;
1363 if( p_vout != NULL )
1365 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1366 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1369 (void) psz_var; (void) oldval;
1373 /*****************************************************************************
1374 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1375 *****************************************************************************/
1376 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1379 #ifdef HAVE_SYS_STAT_H
1380 struct stat stat_info;
1381 uint8_t pi_anchor[2];
1386 /* Triggers libdvdcss autodetection */
1390 if( !strcmp( psz_name, "-" ) ) /* stdin -> file access */
1391 return VLC_EGENERIC;
1393 if( (i_fd = utf8_open( psz_name, O_RDONLY |O_NONBLOCK, 0666 )) == -1 )
1395 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1398 i_ret = VLC_EGENERIC;
1400 if( fstat( i_fd, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1402 if( !S_ISFIFO( stat_info.st_mode ) )
1403 i_ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1407 /* Try to find the anchor (2 bytes at LBA 256) */
1408 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1409 && read( i_fd, pi_anchor, 2 ) == 2
1410 && GetWLE( pi_anchor ) == 2 )
1411 i_ret = VLC_SUCCESS; /* Found a potential anchor */