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
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 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 *****************************************************************************/
129 ps_track_t tk[PS_TK_COUNT];
132 /* for spu variables */
133 input_thread_t *p_input;
136 vlc_object_t *p_vout;
138 /* palette for menus */
140 uint8_t palette[4][4];
147 input_title_t **title;
149 /* lenght of program group chain */
150 mtime_t i_pgc_length;
153 static int Control( demux_t *, int, va_list );
154 static int Demux( demux_t * );
155 static int DemuxBlock( demux_t *, const uint8_t *, int );
157 static void DemuxTitles( demux_t * );
158 static void ESSubtitleUpdate( demux_t * );
159 static void ButtonUpdate( demux_t *, bool );
161 static void ESNew( demux_t *, int );
162 static int ProbeDVD( demux_t *, char * );
164 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
166 static int ControlInternal( demux_t *, int, ... );
168 static void* StillThread( void * );
170 static int EventKey( vlc_object_t *, char const *,
171 vlc_value_t, vlc_value_t, void * );
172 static int EventMouse( vlc_object_t *, char const *,
173 vlc_value_t, vlc_value_t, void * );
174 static int EventIntf( vlc_object_t *, char const *,
175 vlc_value_t, vlc_value_t, void * );
177 /*****************************************************************************
179 *****************************************************************************/
180 static int Open( vlc_object_t *p_this )
182 demux_t *p_demux = (demux_t*)p_this;
190 if( !p_demux->psz_path || !*p_demux->psz_path )
192 /* Only when selected */
193 if( !p_this->b_force ) return VLC_EGENERIC;
195 psz_name = var_CreateGetString( p_this, "dvd" );
198 psz_name = strdup("");
202 psz_name = ToLocaleDup( p_demux->psz_path );
205 if( psz_name[0] && psz_name[1] == ':' &&
206 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
209 /* Try some simple probing to avoid going through dvdnav_open too often */
210 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
217 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
219 msg_Warn( p_demux, "cannot open dvdnav" );
225 /* Fill p_demux field */
226 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
227 p_sys->dvdnav = p_dvdnav;
228 p_sys->b_reset_pcr = false;
230 ps_track_init( p_sys->tk );
231 p_sys->i_aspect = -1;
232 p_sys->i_mux_rate = 0;
233 p_sys->i_pgc_length = 0;
234 p_sys->b_spu_change = false;
238 // Hack for libdvdnav CVS.
239 // Without it dvdnav_get_number_of_titles() fails.
240 // Remove when fixed in libdvdnav CVS.
241 uint8_t buffer[DVD_VIDEO_LB_LEN];
244 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
245 == DVDNAV_STATUS_ERR )
247 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
250 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
253 /* Configure dvdnav */
254 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
257 msg_Warn( p_demux, "cannot set read-a-head flag" );
260 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
263 msg_Warn( p_demux, "cannot set PGC positioning flag" );
267 * XXX A menu-language may be better than sub-language */
268 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
269 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
272 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
273 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
274 /* We try to fall back to 'en' */
275 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
276 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
280 /* Set audio language */
281 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
282 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
285 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
286 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
287 /* We try to fall back to 'en' */
288 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
289 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
293 /* Set spu language */
294 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
295 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
298 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
299 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
300 /* We try to fall back to 'en' */
301 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
302 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
306 DemuxTitles( p_demux );
308 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
309 var_Get( p_demux, "dvdnav-menu", &val );
312 msg_Dbg( p_demux, "trying to go to dvd menu" );
314 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
316 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
317 dialog_Fatal( p_demux, _("Playback failure"),
318 _("VLC cannot set the DVD's title. It possibly "
319 "cannot decrypt the entire disc.") );
320 dvdnav_close( p_sys->dvdnav );
325 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
328 /* Try going to menu root */
329 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
331 msg_Warn( p_demux, "cannot go to dvd menu" );
335 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
336 var_Get( p_demux, "dvdnav-angle", &val );
337 i_angle = val.i_int > 0 ? val.i_int : 1;
339 /* Update default_pts to a suitable value for dvdnav access */
340 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
342 /* FIXME hack hack hack hack FIXME */
343 /* Get p_input and create variable */
344 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
345 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
346 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
347 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
348 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
349 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
350 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
351 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
352 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
354 /* catch all key event */
355 var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
356 /* catch vout creation event */
357 var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
359 p_sys->still.b_enabled = false;
360 vlc_mutex_init( &p_sys->still.lock );
364 /*****************************************************************************
366 *****************************************************************************/
367 static void Close( vlc_object_t *p_this )
369 demux_t *p_demux = (demux_t*)p_this;
370 demux_sys_t *p_sys = p_demux->p_sys;
373 /* Stop vout event handler */
374 var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
375 if( p_sys->p_vout != NULL )
376 { /* Should not happen, but better be safe than sorry. */
377 msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
378 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
379 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
382 /* Stop key event handler (FIXME: should really be per-vout too) */
383 var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
385 /* Stop still image handler */
386 if( p_sys->still.b_created )
388 vlc_cancel( p_sys->still.thread );
389 vlc_join( p_sys->still.thread, NULL );
390 vlc_cond_destroy( &p_sys->still.wait );
392 vlc_mutex_destroy( &p_sys->still.lock );
394 var_Destroy( p_sys->p_input, "highlight-mutex" );
395 var_Destroy( p_sys->p_input, "highlight" );
396 var_Destroy( p_sys->p_input, "x-start" );
397 var_Destroy( p_sys->p_input, "x-end" );
398 var_Destroy( p_sys->p_input, "y-start" );
399 var_Destroy( p_sys->p_input, "y-end" );
400 var_Destroy( p_sys->p_input, "color" );
401 var_Destroy( p_sys->p_input, "menu-palette" );
403 vlc_object_release( p_sys->p_input );
405 for( i = 0; i < PS_TK_COUNT; i++ )
407 ps_track_t *tk = &p_sys->tk[i];
410 es_format_Clean( &tk->fmt );
411 if( tk->es ) es_out_Del( p_demux->out, tk->es );
415 dvdnav_close( p_sys->dvdnav );
419 /*****************************************************************************
421 *****************************************************************************/
422 static int Control( demux_t *p_demux, int i_query, va_list args )
424 demux_sys_t *p_sys = p_demux->p_sys;
428 input_title_t ***ppp_title;
434 case DEMUX_SET_POSITION:
435 case DEMUX_GET_POSITION:
437 case DEMUX_GET_LENGTH:
440 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
441 DVDNAV_STATUS_OK || len == 0 )
446 if( i_query == DEMUX_GET_POSITION )
448 pf = (double*)va_arg( args, double* );
449 *pf = (double)pos / (double)len;
452 else if( i_query == DEMUX_SET_POSITION )
454 f = (double)va_arg( args, double );
456 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
462 else if( i_query == DEMUX_GET_TIME )
464 pi64 = (int64_t*)va_arg( args, int64_t * );
465 if( p_sys->i_pgc_length > 0 )
467 *pi64 = p_sys->i_pgc_length * pos / len;
471 else if( i_query == DEMUX_GET_LENGTH )
473 pi64 = (int64_t*)va_arg( args, int64_t * );
474 if( p_sys->i_pgc_length > 0 )
476 *pi64 = (int64_t)p_sys->i_pgc_length;
484 /* Special for access_demux */
485 case DEMUX_CAN_PAUSE:
487 case DEMUX_CAN_CONTROL_PACE:
489 pb = (bool*)va_arg( args, bool * );
493 case DEMUX_SET_PAUSE_STATE:
496 case DEMUX_GET_TITLE_INFO:
497 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
498 pi_int = (int*)va_arg( args, int* );
499 *((int*)va_arg( args, int* )) = 0; /* Title offset */
500 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
502 /* Duplicate title infos */
503 *pi_int = p_sys->i_title;
504 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
505 for( i = 0; i < p_sys->i_title; i++ )
507 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
511 case DEMUX_SET_TITLE:
512 i = (int)va_arg( args, int );
513 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
514 != DVDNAV_STATUS_OK ) ||
515 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
516 != DVDNAV_STATUS_OK ) )
518 msg_Warn( p_demux, "cannot set title/chapter" );
521 p_demux->info.i_update |=
522 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
523 p_demux->info.i_title = i;
524 p_demux->info.i_seekpoint = 0;
527 case DEMUX_SET_SEEKPOINT:
528 i = (int)va_arg( args, int );
529 if( p_demux->info.i_title == 0 )
536 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
539 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
542 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
545 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
548 i_ret = dvdnav_menu_call( p_sys->dvdnav,
549 DVD_MENU_Subpicture );
552 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
555 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
561 if( i_ret != DVDNAV_STATUS_OK )
564 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
565 i + 1 ) != DVDNAV_STATUS_OK )
567 msg_Warn( p_demux, "cannot set title/chapter" );
570 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
571 p_demux->info.i_seekpoint = i;
574 case DEMUX_GET_PTS_DELAY:
575 pi64 = (int64_t*)va_arg( args, int64_t * );
576 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
581 const char *title_name = NULL;
583 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
584 if( (NULL != title_name) && ('\0' != title_name[0]) )
586 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
587 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
593 /* TODO implement others */
599 static int ControlInternal( demux_t *p_demux, int i_query, ... )
604 va_start( args, i_query );
605 i_result = Control( p_demux, i_query, args );
610 /*****************************************************************************
612 *****************************************************************************/
613 static int Demux( demux_t *p_demux )
615 demux_sys_t *p_sys = p_demux->p_sys;
617 uint8_t buffer[DVD_VIDEO_LB_LEN];
618 uint8_t *packet = buffer;
623 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
624 == DVDNAV_STATUS_ERR )
626 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
627 == DVDNAV_STATUS_ERR )
630 msg_Warn( p_demux, "cannot get next block (%s)",
631 dvdnav_err_to_string( p_sys->dvdnav ) );
632 if( p_demux->info.i_title == 0 )
634 msg_Dbg( p_demux, "jumping to first title" );
635 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
642 case DVDNAV_BLOCK_OK: /* mpeg block */
643 vlc_mutex_lock( &p_sys->still.lock );
644 p_sys->still.b_enabled = false;
645 vlc_mutex_unlock( &p_sys->still.lock );
646 if( p_sys->b_reset_pcr )
648 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
649 p_sys->b_reset_pcr = false;
651 DemuxBlock( p_demux, packet, i_len );
654 case DVDNAV_NOP: /* Nothing */
655 msg_Dbg( p_demux, "DVDNAV_NOP" );
658 case DVDNAV_STILL_FRAME:
660 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
661 bool b_still_init = false;
663 vlc_mutex_lock( &p_sys->still.lock );
664 if( !p_sys->still.b_enabled )
666 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
667 msg_Dbg( p_demux, " - length=0x%x", event->length );
668 /* FIXME: use vlc_timer_create() if it is ever invented */
669 p_sys->still.b_enabled = true;
671 if( !p_sys->still.b_created )
673 vlc_cond_init( &p_sys->still.wait );
674 p_sys->still.b_created =
675 !vlc_clone( &p_sys->still.thread, StillThread, p_sys,
676 VLC_THREAD_PRIORITY_LOW );
680 if( event->length == 0xff )
681 p_sys->still.i_end = 0;
683 p_sys->still.i_end = mdate() +
684 event->length * INT64_C(1000000);
685 vlc_cond_signal( &p_sys->still.wait );
687 vlc_mutex_unlock( &p_sys->still.lock );
691 /* We send a dummy mpeg2 end of sequence to force still frame display */
692 static const uint8_t buffer[] = {
693 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
695 0x00, 0x00, 0x01, 0xB7,
697 DemuxBlock( p_demux, buffer, sizeof(buffer) );
700 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
701 p_sys->b_reset_pcr = true;
707 case DVDNAV_SPU_CLUT_CHANGE:
711 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
712 /* Update color lookup table (16 *uint32_t in packet) */
713 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
715 /* HACK to get the SPU tracks registered in the right order */
716 for( i = 0; i < 0x1f; i++ )
718 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
719 ESNew( p_demux, 0xbd20 + i );
725 case DVDNAV_SPU_STREAM_CHANGE:
727 dvdnav_spu_stream_change_event_t *event =
728 (dvdnav_spu_stream_change_event_t*)packet;
731 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
732 msg_Dbg( p_demux, " - physical_wide=%d",
733 event->physical_wide );
734 msg_Dbg( p_demux, " - physical_letterbox=%d",
735 event->physical_letterbox);
736 msg_Dbg( p_demux, " - physical_pan_scan=%d",
737 event->physical_pan_scan );
739 ESSubtitleUpdate( p_demux );
740 p_sys->b_spu_change = true;
742 /* HACK to get the SPU tracks registered in the right order */
743 for( i = 0; i < 0x1f; i++ )
745 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
746 ESNew( p_demux, 0xbd20 + i );
752 case DVDNAV_AUDIO_STREAM_CHANGE:
754 dvdnav_audio_stream_change_event_t *event =
755 (dvdnav_audio_stream_change_event_t*)packet;
756 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
757 msg_Dbg( p_demux, " - physical=%d", event->physical );
762 case DVDNAV_VTS_CHANGE:
768 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
769 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
770 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
771 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
773 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
774 /* TODO check if we always have VTS and CELL */
775 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
778 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
780 for( i = 0; i < PS_TK_COUNT; i++ )
782 ps_track_t *tk = &p_sys->tk[i];
785 es_format_Clean( &tk->fmt );
786 if( tk->es ) es_out_Del( p_demux->out, tk->es );
791 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
792 &i_part ) == DVDNAV_STATUS_OK )
794 if( i_title >= 0 && i_title < p_sys->i_title &&
795 p_demux->info.i_title != i_title )
797 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
798 p_demux->info.i_title = i_title;
804 case DVDNAV_CELL_CHANGE:
809 dvdnav_cell_change_event_t *event =
810 (dvdnav_cell_change_event_t*)packet;
811 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
812 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
813 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
814 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
815 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
816 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
817 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
818 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
820 /* Store the lenght in time of the current PGC */
821 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
823 /* FIXME is it correct or there is better way to know chapter change */
824 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
825 &i_part ) == DVDNAV_STATUS_OK )
827 if( i_title >= 0 && i_title < p_sys->i_title &&
828 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
829 p_demux->info.i_seekpoint != i_part - 1 )
831 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
832 p_demux->info.i_seekpoint = i_part - 1;
838 case DVDNAV_NAV_PACKET:
841 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
843 /* A lot of thing to do here :
845 * - fetch pts (for time display)
848 DemuxBlock( p_demux, packet, i_len );
849 if( p_sys->b_spu_change )
851 ButtonUpdate( p_demux, false );
852 p_sys->b_spu_change = false;
857 case DVDNAV_STOP: /* EOF */
858 msg_Dbg( p_demux, "DVDNAV_STOP" );
861 dvdnav_free_cache_block( p_sys->dvdnav, packet );
865 case DVDNAV_HIGHLIGHT:
867 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
868 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
869 msg_Dbg( p_demux, " - display=%d", event->display );
870 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
871 ButtonUpdate( p_demux, false );
875 case DVDNAV_HOP_CHANNEL:
876 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
877 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
881 msg_Dbg( p_demux, "DVDNAV_WAIT" );
884 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
891 dvdnav_wait_skip( p_sys->dvdnav );
892 p_sys->b_reset_pcr = true;
897 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
902 dvdnav_free_cache_block( p_sys->dvdnav, packet );
909 * FIXME: partiallyy duplicated from src/input/es_out.c
911 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
913 const iso639_lang_t *pl;
917 psz_lang = var_CreateGetString( p_demux, psz_var );
919 return strdup(LANGUAGE_DEFAULT);
921 /* XXX: we will use only the first value
922 * (and ignore other ones in case of a list) */
923 if( ( p = strchr( psz_lang, ',' ) ) )
926 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
928 if( *psz_lang == '\0' )
930 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
931 !strcasecmp( pl->psz_native_name, psz_lang ) ||
932 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
933 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
934 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
940 if( pl->psz_iso639_1 != NULL )
941 return strdup( pl->psz_iso639_1 );
943 return strdup(LANGUAGE_DEFAULT);
946 static void DemuxTitles( demux_t *p_demux )
948 demux_sys_t *p_sys = p_demux->p_sys;
955 t = vlc_input_title_New();
957 t->psz_name = strdup( "DVD Menu" );
959 s = vlc_seekpoint_New();
960 s->psz_name = strdup( "Resume" );
961 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
963 s = vlc_seekpoint_New();
964 s->psz_name = strdup( "Root" );
965 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
967 s = vlc_seekpoint_New();
968 s->psz_name = strdup( "Title" );
969 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
971 s = vlc_seekpoint_New();
972 s->psz_name = strdup( "Chapter" );
973 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
975 s = vlc_seekpoint_New();
976 s->psz_name = strdup( "Subtitle" );
977 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
979 s = vlc_seekpoint_New();
980 s->psz_name = strdup( "Audio" );
981 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
983 s = vlc_seekpoint_New();
984 s->psz_name = strdup( "Angle" );
985 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
987 TAB_APPEND( p_sys->i_title, p_sys->title, t );
989 /* Find out number of titles/chapters */
990 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
991 for( i = 1; i <= i_titles; i++ )
993 int32_t i_chapters = 0;
996 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
998 t = vlc_input_title_New();
999 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1001 s = vlc_seekpoint_New();
1002 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1005 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1009 /*****************************************************************************
1011 *****************************************************************************/
1012 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1014 demux_sys_t *p_sys = p_demux->p_sys;
1016 int32_t i_title, i_part;
1018 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1020 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
1022 vlc_mutex_t *p_mutex = val.p_address;
1023 dvdnav_highlight_area_t hl;
1027 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1028 != DVDNAV_STATUS_OK )
1030 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1034 b_button_ok = false;
1035 if( i_button > 0 && i_title == 0 )
1037 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1039 b_button_ok = dvdnav_get_highlight_area( pci, i_button, b_mode, &hl ) == DVDNAV_STATUS_OK;
1044 for( i = 0; i < 4; i++ )
1046 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1047 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1049 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1050 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1051 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1052 p_sys->palette[i][3] = i_alpha;
1055 vlc_mutex_lock( p_mutex );
1056 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
1057 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
1058 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
1059 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
1061 val.p_address = (void *)p_sys->palette;
1062 var_Set( p_sys->p_input, "menu-palette", val );
1064 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
1065 vlc_mutex_unlock( p_mutex );
1067 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1071 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1072 i_button, i_title );
1075 vlc_mutex_lock( p_mutex );
1077 var_Set( p_sys->p_input, "highlight", val );
1078 vlc_mutex_unlock( p_mutex );
1083 static void ESSubtitleUpdate( demux_t *p_demux )
1085 demux_sys_t *p_sys = p_demux->p_sys;
1086 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1087 int32_t i_title, i_part;
1089 ButtonUpdate( p_demux, false );
1091 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1092 if( i_title > 0 ) return;
1094 if( i_spu >= 0 && i_spu <= 0x1f )
1096 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1098 ESNew( p_demux, 0xbd20 + i_spu );
1100 /* be sure to unselect it (reset) */
1101 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1105 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1109 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1111 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1114 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1121 /*****************************************************************************
1122 * DemuxBlock: demux a given block
1123 *****************************************************************************/
1124 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1126 demux_sys_t *p_sys = p_demux->p_sys;
1127 const uint8_t *p = pkt;
1129 while( p < &pkt[i_pkt] )
1131 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1138 /* Create a block */
1139 p_pkt = block_New( p_demux, i_size );
1140 memcpy( p_pkt->p_buffer, p, i_size);
1142 /* Parse it and send it */
1143 switch( 0x100 | p[3] )
1151 msg_Warn( p_demux, "received a PSM packet" );
1153 else if( p[3] == 0xbb )
1155 msg_Warn( p_demux, "received a SYSTEM packet" );
1158 block_Release( p_pkt );
1165 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1167 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1168 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1170 block_Release( p_pkt );
1175 int i_id = ps_pkt_id( p_pkt );
1178 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1182 ESNew( p_demux, i_id );
1184 if( tk->b_seen && tk->es &&
1185 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1187 es_out_Send( p_demux->out, tk->es, p_pkt );
1191 block_Release( p_pkt );
1196 block_Release( p_pkt );
1208 /*****************************************************************************
1209 * ESNew: register a new elementary stream
1210 *****************************************************************************/
1211 static void ESNew( demux_t *p_demux, int i_id )
1213 demux_sys_t *p_sys = p_demux->p_sys;
1214 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1215 bool b_select = false;
1217 if( tk->b_seen ) return;
1219 if( ps_track_fill( tk, 0, i_id ) )
1221 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1226 if( tk->fmt.i_cat == VIDEO_ES )
1228 switch( p_sys->i_aspect )
1230 case 1: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR; break;
1231 case 2: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3; break;
1232 case 3: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9; break;
1233 case 4: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 221 / 10; break;
1235 tk->fmt.video.i_aspect = 0;
1240 else if( tk->fmt.i_cat == AUDIO_ES )
1243 /* find the audio number PLEASE find another way */
1244 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1246 i_audio = i_id&0x07;
1248 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1252 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1254 i_audio = i_id&0x1f;
1256 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1258 i_audio = i_id&0x1f;
1262 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1263 if( i_lang != 0xffff )
1265 tk->fmt.psz_language = malloc( 3 );
1266 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1267 tk->fmt.psz_language[1] = (i_lang )&0xff;
1268 tk->fmt.psz_language[2] = 0;
1270 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1276 else if( tk->fmt.i_cat == SPU_ES )
1278 int32_t i_title, i_part;
1279 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1280 if( i_lang != 0xffff )
1282 tk->fmt.psz_language = malloc( 3 );
1283 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1284 tk->fmt.psz_language[1] = (i_lang )&0xff;
1285 tk->fmt.psz_language[2] = 0;
1289 tk->fmt.subs.spu.palette[0] = 0xBeef;
1290 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1291 16 * sizeof( uint32_t ) );
1293 /* We select only when we are not in the menu */
1294 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1296 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1302 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1305 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1309 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1312 /*****************************************************************************
1313 * Event handler code
1314 *****************************************************************************/
1315 static void* StillThread( void *p_data )
1317 demux_sys_t *p_sys = p_data;
1319 vlc_mutex_lock( &p_sys->still.lock );
1320 mutex_cleanup_push( &p_sys->still.lock );
1323 if( p_sys->still.b_enabled && p_sys->still.i_end )
1325 if( vlc_cond_timedwait( &p_sys->still.wait, &p_sys->still.lock,
1326 p_sys->still.i_end ) )
1327 { /* Still image time out */
1328 int canc = vlc_savecancel();
1329 p_sys->still.b_enabled = false;
1330 dvdnav_still_skip( p_sys->dvdnav );
1331 vlc_restorecancel( canc );
1335 vlc_cond_wait( &p_sys->still.wait, &p_sys->still.lock );
1342 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1343 vlc_value_t oldval, vlc_value_t val, void *p_data )
1345 demux_t *p_demux = p_data;
1346 demux_sys_t *p_sys = p_demux->p_sys;
1348 /* FIXME? PCI usage thread safe? */
1349 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1350 int x = var_GetInteger( p_vout, "mouse-x" );
1351 int y = var_GetInteger( p_vout, "mouse-y" );
1353 if( psz_var[6] == 'm' ) /* mouse-moved */
1354 dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1357 assert( psz_var[6] == 'c' ); /* mouse-clicked */
1359 ButtonUpdate( p_demux, true );
1360 dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1363 (void)oldval; (void)val;
1367 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1368 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1370 demux_t *p_demux = p_data;
1371 demux_sys_t *p_sys = p_demux->p_sys;
1373 /* FIXME: thread-safe ? */
1374 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1376 switch( newval.i_int )
1378 case ACTIONID_NAV_LEFT:
1379 dvdnav_left_button_select( p_sys->dvdnav, pci );
1381 case ACTIONID_NAV_RIGHT:
1382 dvdnav_right_button_select( p_sys->dvdnav, pci );
1384 case ACTIONID_NAV_UP:
1385 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1387 case ACTIONID_NAV_DOWN:
1388 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1390 case ACTIONID_NAV_ACTIVATE:
1391 ButtonUpdate( p_demux, true );
1392 dvdnav_button_activate( p_sys->dvdnav, pci );
1396 (void)p_this; (void)psz_var; (void)oldval;
1400 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1401 vlc_value_t oldval, vlc_value_t val, void *p_data )
1403 demux_t *p_demux = p_data;
1404 demux_sys_t *p_sys = p_demux->p_sys;
1406 if (val.i_int == INPUT_EVENT_VOUT)
1408 vlc_object_t *p_vout;
1410 p_vout = p_sys->p_vout;
1411 if( p_vout != NULL )
1413 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1414 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1415 vlc_object_release( p_vout );
1418 p_vout = (vlc_object_t *)input_GetVout( (input_thread_t *)p_input );
1419 p_sys->p_vout = p_vout;
1420 if( p_vout != NULL )
1422 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1423 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1426 (void) psz_var; (void) oldval;
1430 /*****************************************************************************
1431 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1432 *****************************************************************************/
1433 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1436 #ifdef HAVE_SYS_STAT_H
1437 struct stat stat_info;
1438 uint8_t pi_anchor[2];
1443 /* Triggers libdvdcss autodetection */
1447 if( !strcmp( psz_name, "-" ) ) /* stdin -> file access */
1448 return VLC_EGENERIC;
1450 if( (i_fd = utf8_open( psz_name, O_RDONLY |O_NONBLOCK, 0666 )) == -1 )
1452 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1455 if( fstat( i_fd, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1459 if( S_ISFIFO( stat_info.st_mode ) )
1460 return VLC_EGENERIC;
1461 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1464 /* Try to find the anchor (2 bytes at LBA 256) */
1465 i_ret = VLC_EGENERIC;
1466 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1467 && read( i_fd, pi_anchor, 2 ) == 2
1468 && GetWLE( pi_anchor ) == 2 )
1469 i_ret = VLC_SUCCESS; /* Found a potential anchor */