1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004 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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_input.h>
35 #include <vlc_access.h>
36 #include <vlc_demux.h>
37 #include <vlc_charset.h>
39 #include <vlc_dialog.h>
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
55 #include "vlc_iso_lang.h"
57 /* FIXME we should find a better way than including that */
58 #include "../../src/text/iso-639_def.h"
61 #include <dvdnav/dvdnav.h>
63 #include "../demux/ps.h"
65 /*****************************************************************************
67 *****************************************************************************/
68 #define ANGLE_TEXT N_("DVD angle")
69 #define ANGLE_LONGTEXT N_( \
70 "Default DVD angle." )
72 #define CACHING_TEXT N_("Caching value in ms")
73 #define CACHING_LONGTEXT N_( \
74 "Caching value for DVDs. This "\
75 "value should be set in milliseconds." )
76 #define MENU_TEXT N_("Start directly in menu")
77 #define MENU_LONGTEXT N_( \
78 "Start the DVD directly in the main menu. This "\
79 "will try to skip all the useless warning introductions." )
81 #define LANGUAGE_DEFAULT ("en")
83 static int Open ( vlc_object_t * );
84 static void Close( vlc_object_t * );
87 set_shortname( N_("DVD with menus") )
88 set_description( N_("DVDnav Input") )
89 set_category( CAT_INPUT )
90 set_subcategory( SUBCAT_INPUT_ACCESS )
91 add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
92 ANGLE_LONGTEXT, false )
93 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
94 CACHING_TEXT, CACHING_LONGTEXT, true )
95 add_bool( "dvdnav-menu", true, NULL,
96 MENU_TEXT, MENU_LONGTEXT, false )
97 set_capability( "access_demux", 5 )
99 add_shortcut( "dvdnav" )
100 add_shortcut( "file" )
101 set_callbacks( Open, Close )
104 /* Shall we use libdvdnav's read ahead cache? */
105 #define DVD_READ_CACHE 1
107 /*****************************************************************************
109 *****************************************************************************/
126 static void* EventThread( vlc_object_t * );
136 ps_track_t tk[PS_TK_COUNT];
139 /* for spu variables */
140 input_thread_t *p_input;
143 event_thread_t *p_ev;
145 /* palette for menus */
147 uint8_t palette[4][4];
154 input_title_t **title;
156 /* lenght of program group chain */
157 mtime_t i_pgc_length;
160 static int Control( demux_t *, int, va_list );
161 static int Demux( demux_t * );
162 static int DemuxBlock( demux_t *, const uint8_t *, int );
164 static void DemuxTitles( demux_t * );
165 static void ESSubtitleUpdate( demux_t * );
166 static void ButtonUpdate( demux_t *, bool );
168 static void ESNew( demux_t *, int );
169 static int ProbeDVD( demux_t *, char * );
171 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
173 static int ControlInternal( demux_t *, int, ... );
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"),
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 /* Now create our event thread catcher */
353 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
354 p_sys->p_ev->p_demux = p_demux;
355 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
356 VLC_THREAD_PRIORITY_LOW );
361 /*****************************************************************************
363 *****************************************************************************/
364 static void Close( vlc_object_t *p_this )
366 demux_t *p_demux = (demux_t*)p_this;
367 demux_sys_t *p_sys = p_demux->p_sys;
370 /* stop the event handler */
371 vlc_object_kill( p_sys->p_ev );
372 vlc_thread_join( p_sys->p_ev );
373 vlc_object_release( p_sys->p_ev );
375 var_Destroy( p_sys->p_input, "highlight-mutex" );
376 var_Destroy( p_sys->p_input, "highlight" );
377 var_Destroy( p_sys->p_input, "x-start" );
378 var_Destroy( p_sys->p_input, "x-end" );
379 var_Destroy( p_sys->p_input, "y-start" );
380 var_Destroy( p_sys->p_input, "y-end" );
381 var_Destroy( p_sys->p_input, "color" );
382 var_Destroy( p_sys->p_input, "menu-palette" );
384 vlc_object_release( p_sys->p_input );
386 for( i = 0; i < PS_TK_COUNT; i++ )
388 ps_track_t *tk = &p_sys->tk[i];
391 es_format_Clean( &tk->fmt );
392 if( tk->es ) es_out_Del( p_demux->out, tk->es );
396 dvdnav_close( p_sys->dvdnav );
400 /*****************************************************************************
402 *****************************************************************************/
403 static int Control( demux_t *p_demux, int i_query, va_list args )
405 demux_sys_t *p_sys = p_demux->p_sys;
409 input_title_t ***ppp_title;
415 case DEMUX_SET_POSITION:
416 case DEMUX_GET_POSITION:
418 case DEMUX_GET_LENGTH:
421 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
422 DVDNAV_STATUS_OK || len == 0 )
427 if( i_query == DEMUX_GET_POSITION )
429 pf = (double*)va_arg( args, double* );
430 *pf = (double)pos / (double)len;
433 else if( i_query == DEMUX_SET_POSITION )
435 f = (double)va_arg( args, double );
437 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
443 else if( i_query == DEMUX_GET_TIME )
445 pi64 = (int64_t*)va_arg( args, int64_t * );
446 if( p_sys->i_pgc_length > 0 )
448 *pi64 = p_sys->i_pgc_length * pos / len;
452 else if( i_query == DEMUX_GET_LENGTH )
454 pi64 = (int64_t*)va_arg( args, int64_t * );
455 if( p_sys->i_pgc_length > 0 )
457 *pi64 = (int64_t)p_sys->i_pgc_length;
465 /* Special for access_demux */
466 case DEMUX_CAN_PAUSE:
468 case DEMUX_CAN_CONTROL_PACE:
470 pb = (bool*)va_arg( args, bool * );
474 case DEMUX_SET_PAUSE_STATE:
477 case DEMUX_GET_TITLE_INFO:
478 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
479 pi_int = (int*)va_arg( args, int* );
480 *((int*)va_arg( args, int* )) = 0; /* Title offset */
481 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
483 /* Duplicate title infos */
484 *pi_int = p_sys->i_title;
485 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
486 for( i = 0; i < p_sys->i_title; i++ )
488 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
492 case DEMUX_SET_TITLE:
493 i = (int)va_arg( args, int );
494 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
495 != DVDNAV_STATUS_OK ) ||
496 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
497 != DVDNAV_STATUS_OK ) )
499 msg_Warn( p_demux, "cannot set title/chapter" );
502 p_demux->info.i_update |=
503 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
504 p_demux->info.i_title = i;
505 p_demux->info.i_seekpoint = 0;
508 case DEMUX_SET_SEEKPOINT:
509 i = (int)va_arg( args, int );
510 if( p_demux->info.i_title == 0 )
517 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
520 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
523 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
526 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
529 i_ret = dvdnav_menu_call( p_sys->dvdnav,
530 DVD_MENU_Subpicture );
533 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
536 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
542 if( i_ret != DVDNAV_STATUS_OK )
545 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
546 i + 1 ) != DVDNAV_STATUS_OK )
548 msg_Warn( p_demux, "cannot set title/chapter" );
551 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
552 p_demux->info.i_seekpoint = i;
555 case DEMUX_GET_PTS_DELAY:
556 pi64 = (int64_t*)va_arg( args, int64_t * );
557 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
562 const char *title_name = NULL;
564 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
565 if( (NULL != title_name) && ('\0' != title_name[0]) )
567 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
568 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
574 /* TODO implement others */
580 static int ControlInternal( demux_t *p_demux, int i_query, ... )
585 va_start( args, i_query );
586 i_result = Control( p_demux, i_query, args );
591 /*****************************************************************************
593 *****************************************************************************/
594 static int Demux( demux_t *p_demux )
596 demux_sys_t *p_sys = p_demux->p_sys;
598 uint8_t buffer[DVD_VIDEO_LB_LEN];
599 uint8_t *packet = buffer;
604 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
605 == DVDNAV_STATUS_ERR )
607 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
608 == DVDNAV_STATUS_ERR )
611 msg_Warn( p_demux, "cannot get next block (%s)",
612 dvdnav_err_to_string( p_sys->dvdnav ) );
613 if( p_demux->info.i_title == 0 )
615 msg_Dbg( p_demux, "jumping to first title" );
616 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
623 case DVDNAV_BLOCK_OK: /* mpeg block */
624 p_sys->p_ev->b_still = false;
625 if( p_sys->b_reset_pcr )
627 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
628 p_sys->b_reset_pcr = false;
630 DemuxBlock( p_demux, packet, i_len );
633 case DVDNAV_NOP: /* Nothing */
634 msg_Dbg( p_demux, "DVDNAV_NOP" );
637 case DVDNAV_STILL_FRAME:
639 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
640 bool b_still_init = false;
642 vlc_mutex_lock( &p_sys->p_ev->lock );
643 if( !p_sys->p_ev->b_still )
645 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
646 msg_Dbg( p_demux, " - length=0x%x", event->length );
647 p_sys->p_ev->b_still = true;
649 if( event->length == 0xff )
650 p_sys->p_ev->i_still_end = 0;
652 p_sys->p_ev->i_still_end = mdate() +
653 event->length * INT64_C(1000000);
655 vlc_mutex_unlock( &p_sys->p_ev->lock );
659 /* We send a dummy mpeg2 end of sequence to force still frame display */
660 static const uint8_t buffer[] = {
661 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
663 0x00, 0x00, 0x01, 0xB7,
665 DemuxBlock( p_demux, buffer, sizeof(buffer) );
668 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
669 p_sys->b_reset_pcr = true;
675 case DVDNAV_SPU_CLUT_CHANGE:
679 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
680 /* Update color lookup table (16 *uint32_t in packet) */
681 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
683 /* HACK to get the SPU tracks registered in the right order */
684 for( i = 0; i < 0x1f; i++ )
686 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
687 ESNew( p_demux, 0xbd20 + i );
693 case DVDNAV_SPU_STREAM_CHANGE:
695 dvdnav_spu_stream_change_event_t *event =
696 (dvdnav_spu_stream_change_event_t*)packet;
699 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
700 msg_Dbg( p_demux, " - physical_wide=%d",
701 event->physical_wide );
702 msg_Dbg( p_demux, " - physical_letterbox=%d",
703 event->physical_letterbox);
704 msg_Dbg( p_demux, " - physical_pan_scan=%d",
705 event->physical_pan_scan );
707 ESSubtitleUpdate( p_demux );
708 p_sys->b_spu_change = true;
710 /* HACK to get the SPU tracks registered in the right order */
711 for( i = 0; i < 0x1f; i++ )
713 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
714 ESNew( p_demux, 0xbd20 + i );
720 case DVDNAV_AUDIO_STREAM_CHANGE:
722 dvdnav_audio_stream_change_event_t *event =
723 (dvdnav_audio_stream_change_event_t*)packet;
724 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
725 msg_Dbg( p_demux, " - physical=%d", event->physical );
730 case DVDNAV_VTS_CHANGE:
736 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
737 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
738 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
739 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
741 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
742 /* TODO check if we always have VTS and CELL */
743 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
746 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
748 for( i = 0; i < PS_TK_COUNT; i++ )
750 ps_track_t *tk = &p_sys->tk[i];
753 es_format_Clean( &tk->fmt );
754 if( tk->es ) es_out_Del( p_demux->out, tk->es );
759 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
760 &i_part ) == DVDNAV_STATUS_OK )
762 if( i_title >= 0 && i_title < p_sys->i_title &&
763 p_demux->info.i_title != i_title )
765 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
766 p_demux->info.i_title = i_title;
772 case DVDNAV_CELL_CHANGE:
777 dvdnav_cell_change_event_t *event =
778 (dvdnav_cell_change_event_t*)packet;
779 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
780 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
781 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
782 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
783 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
784 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
785 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
786 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
788 /* Store the lenght in time of the current PGC */
789 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
791 /* FIXME is it correct or there is better way to know chapter change */
792 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
793 &i_part ) == DVDNAV_STATUS_OK )
795 if( i_title >= 0 && i_title < p_sys->i_title &&
796 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
797 p_demux->info.i_seekpoint != i_part - 1 )
799 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
800 p_demux->info.i_seekpoint = i_part - 1;
806 case DVDNAV_NAV_PACKET:
809 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
811 /* A lot of thing to do here :
813 * - fetch pts (for time display)
816 DemuxBlock( p_demux, packet, i_len );
817 if( p_sys->b_spu_change )
819 ButtonUpdate( p_demux, false );
820 p_sys->b_spu_change = false;
825 case DVDNAV_STOP: /* EOF */
826 msg_Dbg( p_demux, "DVDNAV_STOP" );
829 dvdnav_free_cache_block( p_sys->dvdnav, packet );
833 case DVDNAV_HIGHLIGHT:
835 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
836 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
837 msg_Dbg( p_demux, " - display=%d", event->display );
838 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
839 ButtonUpdate( p_demux, false );
843 case DVDNAV_HOP_CHANNEL:
844 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
845 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
849 msg_Dbg( p_demux, "DVDNAV_WAIT" );
852 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
859 dvdnav_wait_skip( p_sys->dvdnav );
860 p_sys->b_reset_pcr = true;
865 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
870 dvdnav_free_cache_block( p_sys->dvdnav, packet );
877 * FIXME: partiallyy duplicated from src/input/es_out.c
879 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
881 const iso639_lang_t *pl;
885 psz_lang = var_CreateGetString( p_demux, psz_var );
887 return strdup(LANGUAGE_DEFAULT);
889 /* XXX: we will use only the first value
890 * (and ignore other ones in case of a list) */
891 if( ( p = strchr( psz_lang, ',' ) ) )
894 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
896 if( *psz_lang == '\0' )
898 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
899 !strcasecmp( pl->psz_native_name, psz_lang ) ||
900 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
901 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
902 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
908 if( pl->psz_iso639_1 != NULL )
909 return strdup( pl->psz_iso639_1 );
911 return strdup(LANGUAGE_DEFAULT);
914 static void DemuxTitles( demux_t *p_demux )
916 demux_sys_t *p_sys = p_demux->p_sys;
923 t = vlc_input_title_New();
925 t->psz_name = strdup( "DVD Menu" );
927 s = vlc_seekpoint_New();
928 s->psz_name = strdup( "Resume" );
929 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
931 s = vlc_seekpoint_New();
932 s->psz_name = strdup( "Root" );
933 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
935 s = vlc_seekpoint_New();
936 s->psz_name = strdup( "Title" );
937 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
939 s = vlc_seekpoint_New();
940 s->psz_name = strdup( "Chapter" );
941 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
943 s = vlc_seekpoint_New();
944 s->psz_name = strdup( "Subtitle" );
945 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
947 s = vlc_seekpoint_New();
948 s->psz_name = strdup( "Audio" );
949 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
951 s = vlc_seekpoint_New();
952 s->psz_name = strdup( "Angle" );
953 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
955 TAB_APPEND( p_sys->i_title, p_sys->title, t );
957 /* Find out number of titles/chapters */
958 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
959 for( i = 1; i <= i_titles; i++ )
961 int32_t i_chapters = 0;
964 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
966 t = vlc_input_title_New();
967 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
969 s = vlc_seekpoint_New();
970 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
973 TAB_APPEND( p_sys->i_title, p_sys->title, t );
977 /*****************************************************************************
979 *****************************************************************************/
980 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
982 demux_sys_t *p_sys = p_demux->p_sys;
984 int32_t i_title, i_part;
986 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
988 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
990 vlc_mutex_t *p_mutex = val.p_address;
991 dvdnav_highlight_area_t hl;
995 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
996 != DVDNAV_STATUS_OK )
998 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1002 b_button_ok = false;
1003 if( i_button > 0 && i_title == 0 )
1005 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1007 b_button_ok = dvdnav_get_highlight_area( pci, i_button, b_mode, &hl ) == DVDNAV_STATUS_OK;
1012 for( i = 0; i < 4; i++ )
1014 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1015 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1017 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1018 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1019 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1020 p_sys->palette[i][3] = i_alpha;
1023 vlc_mutex_lock( p_mutex );
1024 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
1025 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
1026 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
1027 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
1029 val.p_address = (void *)p_sys->palette;
1030 var_Set( p_sys->p_input, "menu-palette", val );
1032 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
1033 vlc_mutex_unlock( p_mutex );
1035 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1039 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1040 i_button, i_title );
1043 vlc_mutex_lock( p_mutex );
1045 var_Set( p_sys->p_input, "highlight", val );
1046 vlc_mutex_unlock( p_mutex );
1051 static void ESSubtitleUpdate( demux_t *p_demux )
1053 demux_sys_t *p_sys = p_demux->p_sys;
1054 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1055 int32_t i_title, i_part;
1057 ButtonUpdate( p_demux, false );
1059 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1060 if( i_title > 0 ) return;
1062 if( i_spu >= 0 && i_spu <= 0x1f )
1064 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1066 ESNew( p_demux, 0xbd20 + i_spu );
1068 /* be sure to unselect it (reset) */
1069 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1073 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1077 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1079 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1082 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1089 /*****************************************************************************
1090 * DemuxBlock: demux a given block
1091 *****************************************************************************/
1092 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1094 demux_sys_t *p_sys = p_demux->p_sys;
1095 const uint8_t *p = pkt;
1097 while( p < &pkt[i_pkt] )
1099 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1106 /* Create a block */
1107 p_pkt = block_New( p_demux, i_size );
1108 memcpy( p_pkt->p_buffer, p, i_size);
1110 /* Parse it and send it */
1111 switch( 0x100 | p[3] )
1119 msg_Warn( p_demux, "received a PSM packet" );
1121 else if( p[3] == 0xbb )
1123 msg_Warn( p_demux, "received a SYSTEM packet" );
1126 block_Release( p_pkt );
1133 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1135 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1136 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1138 block_Release( p_pkt );
1143 int i_id = ps_pkt_id( p_pkt );
1146 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1150 ESNew( p_demux, i_id );
1152 if( tk->b_seen && tk->es &&
1153 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1155 es_out_Send( p_demux->out, tk->es, p_pkt );
1159 block_Release( p_pkt );
1164 block_Release( p_pkt );
1176 /*****************************************************************************
1177 * ESNew: register a new elementary stream
1178 *****************************************************************************/
1179 static void ESNew( demux_t *p_demux, int i_id )
1181 demux_sys_t *p_sys = p_demux->p_sys;
1182 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1183 bool b_select = false;
1185 if( tk->b_seen ) return;
1187 if( ps_track_fill( tk, 0, i_id ) )
1189 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1194 if( tk->fmt.i_cat == VIDEO_ES )
1196 switch( p_sys->i_aspect )
1198 case 1: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR; break;
1199 case 2: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3; break;
1200 case 3: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9; break;
1201 case 4: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 221 / 10; break;
1203 tk->fmt.video.i_aspect = 0;
1208 else if( tk->fmt.i_cat == AUDIO_ES )
1211 /* find the audio number PLEASE find another way */
1212 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1214 i_audio = i_id&0x07;
1216 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1220 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1222 i_audio = i_id&0x1f;
1224 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1226 i_audio = i_id&0x1f;
1230 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1231 if( i_lang != 0xffff )
1233 tk->fmt.psz_language = malloc( 3 );
1234 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1235 tk->fmt.psz_language[1] = (i_lang )&0xff;
1236 tk->fmt.psz_language[2] = 0;
1238 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1244 else if( tk->fmt.i_cat == SPU_ES )
1246 int32_t i_title, i_part;
1247 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1248 if( i_lang != 0xffff )
1250 tk->fmt.psz_language = malloc( 3 );
1251 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1252 tk->fmt.psz_language[1] = (i_lang )&0xff;
1253 tk->fmt.psz_language[2] = 0;
1257 tk->fmt.subs.spu.palette[0] = 0xBeef;
1258 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1259 16 * sizeof( uint32_t ) );
1261 /* We select only when we are not in the menu */
1262 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1264 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1270 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1273 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1277 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1280 /*****************************************************************************
1281 * Event handler code
1282 *****************************************************************************/
1283 static int EventMouse( vlc_object_t *, char const *,
1284 vlc_value_t, vlc_value_t, void * );
1285 static int EventKey ( vlc_object_t *, char const *,
1286 vlc_value_t, vlc_value_t, void * );
1288 static void* EventThread( vlc_object_t *p_this )
1290 event_thread_t *p_ev = (event_thread_t*)p_this;
1291 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1292 vlc_object_t *p_vout = NULL;
1294 vlc_mutex_init( &p_ev->lock );
1295 p_ev->b_moved = false;
1296 p_ev->b_clicked = false;
1297 p_ev->i_key_action = 0;
1298 p_ev->b_still = false;
1300 int canc = vlc_savecancel ();
1302 /* catch all key event */
1303 var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
1306 while( vlc_object_alive( p_ev ) )
1308 bool b_activated = false;
1311 if( p_ev->i_key_action != 0 )
1313 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1315 vlc_mutex_lock( &p_ev->lock );
1316 switch( p_ev->i_key_action )
1318 case ACTIONID_NAV_LEFT:
1319 dvdnav_left_button_select( p_sys->dvdnav, pci );
1321 case ACTIONID_NAV_RIGHT:
1322 dvdnav_right_button_select( p_sys->dvdnav, pci );
1324 case ACTIONID_NAV_UP:
1325 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1327 case ACTIONID_NAV_DOWN:
1328 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1330 case ACTIONID_NAV_ACTIVATE:
1332 ButtonUpdate( p_ev->p_demux, true );
1333 dvdnav_button_activate( p_sys->dvdnav, pci );
1338 p_ev->i_key_action = 0;
1339 vlc_mutex_unlock( &p_ev->lock );
1343 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1345 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1346 vlc_value_t valx, valy;
1348 vlc_mutex_lock( &p_ev->lock );
1349 var_Get( p_vout, "mouse-x", &valx );
1350 var_Get( p_vout, "mouse-y", &valy );
1354 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1357 if( p_ev->b_clicked )
1360 ButtonUpdate( p_ev->p_demux, true );
1361 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1365 p_ev->b_moved = false;
1366 p_ev->b_clicked = false;
1367 vlc_mutex_unlock( &p_ev->lock );
1369 if( p_vout && !vlc_object_alive (p_vout) )
1371 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1372 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1373 vlc_object_release( p_vout );
1376 if( p_vout == NULL )
1378 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1382 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1383 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1388 vlc_mutex_lock( &p_ev->lock );
1391 if( /* b_activated || // This breaks menus */
1392 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1394 p_ev->b_still = false;
1395 dvdnav_still_skip( p_sys->dvdnav );
1398 vlc_mutex_unlock( &p_ev->lock );
1404 /* Release callback */
1407 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1408 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1409 vlc_object_release( p_vout );
1411 var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
1412 vlc_restorecancel (canc);
1413 vlc_mutex_destroy( &p_ev->lock );
1418 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1419 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1421 (void)p_this; (void)oldval; (void)newval; (void)p_data;
1422 event_thread_t *p_ev = p_data;
1423 vlc_mutex_lock( &p_ev->lock );
1424 if( psz_var[6] == 'c' )
1425 p_ev->b_clicked = true;
1426 else if( psz_var[6] == 'm' )
1427 p_ev->b_moved = true;
1428 vlc_mutex_unlock( &p_ev->lock );
1433 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1434 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1436 (void)p_this; (void)psz_var; (void)oldval;
1437 event_thread_t *p_ev = p_data;
1438 vlc_mutex_lock( &p_ev->lock );
1439 p_ev->i_key_action = newval.i_int;
1440 vlc_mutex_unlock( &p_ev->lock );
1445 /*****************************************************************************
1446 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1447 *****************************************************************************/
1448 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1451 #ifdef HAVE_SYS_STAT_H
1452 struct stat stat_info;
1453 uint8_t pi_anchor[2];
1458 /* Triggers libdvdcss autodetection */
1462 if( !strcmp( psz_name, "-" ) ) /* stdin -> file access */
1463 return VLC_EGENERIC;
1465 if( (i_fd = utf8_open( psz_name, O_RDONLY |O_NONBLOCK, 0666 )) == -1 )
1467 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1470 if( fstat( i_fd, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1474 if( S_ISFIFO( stat_info.st_mode ) )
1475 return VLC_EGENERIC;
1476 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1479 /* Try to find the anchor (2 bytes at LBA 256) */
1480 i_ret = VLC_EGENERIC;
1481 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1482 && read( i_fd, pi_anchor, 2 ) == 2
1483 && GetWLE( pi_anchor ) == 2 )
1484 i_ret = VLC_SUCCESS; /* Found a potential anchor */