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 *****************************************************************************/
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 *****************************************************************************/
123 static void* EventThread( vlc_object_t * );
133 ps_track_t tk[PS_TK_COUNT];
136 /* for spu variables */
137 input_thread_t *p_input;
140 vlc_object_t *p_vout;
141 event_thread_t *p_ev;
143 /* palette for menus */
145 uint8_t palette[4][4];
152 input_title_t **title;
154 /* lenght of program group chain */
155 mtime_t i_pgc_length;
158 static int Control( demux_t *, int, va_list );
159 static int Demux( demux_t * );
160 static int DemuxBlock( demux_t *, const uint8_t *, int );
162 static void DemuxTitles( demux_t * );
163 static void ESSubtitleUpdate( demux_t * );
164 static void ButtonUpdate( demux_t *, bool );
166 static void ESNew( demux_t *, int );
167 static int ProbeDVD( demux_t *, char * );
169 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
171 static int ControlInternal( demux_t *, int, ... );
173 static int EventKey( vlc_object_t *, char const *,
174 vlc_value_t, vlc_value_t, void * );
175 static int EventMouse( vlc_object_t *, char const *,
176 vlc_value_t, vlc_value_t, void * );
177 static int EventIntf( vlc_object_t *, char const *,
178 vlc_value_t, vlc_value_t, void * );
180 /*****************************************************************************
182 *****************************************************************************/
183 static int Open( vlc_object_t *p_this )
185 demux_t *p_demux = (demux_t*)p_this;
193 if( !p_demux->psz_path || !*p_demux->psz_path )
195 /* Only when selected */
196 if( !p_this->b_force ) return VLC_EGENERIC;
198 psz_name = var_CreateGetString( p_this, "dvd" );
201 psz_name = strdup("");
205 psz_name = ToLocaleDup( p_demux->psz_path );
208 if( psz_name[0] && psz_name[1] == ':' &&
209 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
212 /* Try some simple probing to avoid going through dvdnav_open too often */
213 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
220 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
222 msg_Warn( p_demux, "cannot open dvdnav" );
228 /* Fill p_demux field */
229 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
230 p_sys->dvdnav = p_dvdnav;
231 p_sys->b_reset_pcr = false;
233 ps_track_init( p_sys->tk );
234 p_sys->i_aspect = -1;
235 p_sys->i_mux_rate = 0;
236 p_sys->i_pgc_length = 0;
237 p_sys->b_spu_change = false;
241 // Hack for libdvdnav CVS.
242 // Without it dvdnav_get_number_of_titles() fails.
243 // Remove when fixed in libdvdnav CVS.
244 uint8_t buffer[DVD_VIDEO_LB_LEN];
247 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
248 == DVDNAV_STATUS_ERR )
250 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
253 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
256 /* Configure dvdnav */
257 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
260 msg_Warn( p_demux, "cannot set read-a-head flag" );
263 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
266 msg_Warn( p_demux, "cannot set PGC positioning flag" );
270 * XXX A menu-language may be better than sub-language */
271 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
272 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
275 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
276 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
277 /* We try to fall back to 'en' */
278 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
279 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
283 /* Set audio language */
284 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
285 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
288 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
289 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
290 /* We try to fall back to 'en' */
291 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
292 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
296 /* Set spu language */
297 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
298 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
301 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
302 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
303 /* We try to fall back to 'en' */
304 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
305 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
309 DemuxTitles( p_demux );
311 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
312 var_Get( p_demux, "dvdnav-menu", &val );
315 msg_Dbg( p_demux, "trying to go to dvd menu" );
317 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
319 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
320 dialog_Fatal( p_demux, _("Playback failure"),
321 _("VLC cannot set the DVD's title. It possibly "
322 "cannot decrypt the entire disc.") );
323 dvdnav_close( p_sys->dvdnav );
328 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
331 /* Try going to menu root */
332 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
334 msg_Warn( p_demux, "cannot go to dvd menu" );
338 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
339 var_Get( p_demux, "dvdnav-angle", &val );
340 i_angle = val.i_int > 0 ? val.i_int : 1;
342 /* Update default_pts to a suitable value for dvdnav access */
343 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
345 /* FIXME hack hack hack hack FIXME */
346 /* Get p_input and create variable */
347 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
348 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
349 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
350 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
351 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
352 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
353 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
354 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
355 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
357 /* Now create our event thread catcher */
358 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
359 p_sys->p_ev->p_demux = p_demux;
360 vlc_mutex_init( &p_sys->p_ev->lock );
361 p_sys->p_ev->b_still = false;
363 /* catch all key event */
364 var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
365 /* catch vout creation event */
366 var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
368 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
369 VLC_THREAD_PRIORITY_LOW );
374 /*****************************************************************************
376 *****************************************************************************/
377 static void Close( vlc_object_t *p_this )
379 demux_t *p_demux = (demux_t*)p_this;
380 demux_sys_t *p_sys = p_demux->p_sys;
383 var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
384 if( p_sys->p_vout != NULL )
385 { /* Should not happen, but better be safe than sorry. */
386 msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
387 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
388 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
391 /* stop the event handler */
392 vlc_object_kill( p_sys->p_ev );
393 var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
394 vlc_thread_join( p_sys->p_ev );
395 vlc_mutex_destroy( &p_sys->p_ev->lock );
396 vlc_object_release( p_sys->p_ev );
398 var_Destroy( p_sys->p_input, "highlight-mutex" );
399 var_Destroy( p_sys->p_input, "highlight" );
400 var_Destroy( p_sys->p_input, "x-start" );
401 var_Destroy( p_sys->p_input, "x-end" );
402 var_Destroy( p_sys->p_input, "y-start" );
403 var_Destroy( p_sys->p_input, "y-end" );
404 var_Destroy( p_sys->p_input, "color" );
405 var_Destroy( p_sys->p_input, "menu-palette" );
407 vlc_object_release( p_sys->p_input );
409 for( i = 0; i < PS_TK_COUNT; i++ )
411 ps_track_t *tk = &p_sys->tk[i];
414 es_format_Clean( &tk->fmt );
415 if( tk->es ) es_out_Del( p_demux->out, tk->es );
419 dvdnav_close( p_sys->dvdnav );
423 /*****************************************************************************
425 *****************************************************************************/
426 static int Control( demux_t *p_demux, int i_query, va_list args )
428 demux_sys_t *p_sys = p_demux->p_sys;
432 input_title_t ***ppp_title;
438 case DEMUX_SET_POSITION:
439 case DEMUX_GET_POSITION:
441 case DEMUX_GET_LENGTH:
444 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
445 DVDNAV_STATUS_OK || len == 0 )
450 if( i_query == DEMUX_GET_POSITION )
452 pf = (double*)va_arg( args, double* );
453 *pf = (double)pos / (double)len;
456 else if( i_query == DEMUX_SET_POSITION )
458 f = (double)va_arg( args, double );
460 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
466 else if( i_query == DEMUX_GET_TIME )
468 pi64 = (int64_t*)va_arg( args, int64_t * );
469 if( p_sys->i_pgc_length > 0 )
471 *pi64 = p_sys->i_pgc_length * pos / len;
475 else if( i_query == DEMUX_GET_LENGTH )
477 pi64 = (int64_t*)va_arg( args, int64_t * );
478 if( p_sys->i_pgc_length > 0 )
480 *pi64 = (int64_t)p_sys->i_pgc_length;
488 /* Special for access_demux */
489 case DEMUX_CAN_PAUSE:
491 case DEMUX_CAN_CONTROL_PACE:
493 pb = (bool*)va_arg( args, bool * );
497 case DEMUX_SET_PAUSE_STATE:
500 case DEMUX_GET_TITLE_INFO:
501 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
502 pi_int = (int*)va_arg( args, int* );
503 *((int*)va_arg( args, int* )) = 0; /* Title offset */
504 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
506 /* Duplicate title infos */
507 *pi_int = p_sys->i_title;
508 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
509 for( i = 0; i < p_sys->i_title; i++ )
511 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
515 case DEMUX_SET_TITLE:
516 i = (int)va_arg( args, int );
517 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
518 != DVDNAV_STATUS_OK ) ||
519 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
520 != DVDNAV_STATUS_OK ) )
522 msg_Warn( p_demux, "cannot set title/chapter" );
525 p_demux->info.i_update |=
526 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
527 p_demux->info.i_title = i;
528 p_demux->info.i_seekpoint = 0;
531 case DEMUX_SET_SEEKPOINT:
532 i = (int)va_arg( args, int );
533 if( p_demux->info.i_title == 0 )
540 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
543 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
546 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
549 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
552 i_ret = dvdnav_menu_call( p_sys->dvdnav,
553 DVD_MENU_Subpicture );
556 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
559 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
565 if( i_ret != DVDNAV_STATUS_OK )
568 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
569 i + 1 ) != DVDNAV_STATUS_OK )
571 msg_Warn( p_demux, "cannot set title/chapter" );
574 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
575 p_demux->info.i_seekpoint = i;
578 case DEMUX_GET_PTS_DELAY:
579 pi64 = (int64_t*)va_arg( args, int64_t * );
580 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
585 const char *title_name = NULL;
587 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
588 if( (NULL != title_name) && ('\0' != title_name[0]) )
590 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
591 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
597 /* TODO implement others */
603 static int ControlInternal( demux_t *p_demux, int i_query, ... )
608 va_start( args, i_query );
609 i_result = Control( p_demux, i_query, args );
614 /*****************************************************************************
616 *****************************************************************************/
617 static int Demux( demux_t *p_demux )
619 demux_sys_t *p_sys = p_demux->p_sys;
621 uint8_t buffer[DVD_VIDEO_LB_LEN];
622 uint8_t *packet = buffer;
627 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
628 == DVDNAV_STATUS_ERR )
630 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
631 == DVDNAV_STATUS_ERR )
634 msg_Warn( p_demux, "cannot get next block (%s)",
635 dvdnav_err_to_string( p_sys->dvdnav ) );
636 if( p_demux->info.i_title == 0 )
638 msg_Dbg( p_demux, "jumping to first title" );
639 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
646 case DVDNAV_BLOCK_OK: /* mpeg block */
647 p_sys->p_ev->b_still = false;
648 if( p_sys->b_reset_pcr )
650 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
651 p_sys->b_reset_pcr = false;
653 DemuxBlock( p_demux, packet, i_len );
656 case DVDNAV_NOP: /* Nothing */
657 msg_Dbg( p_demux, "DVDNAV_NOP" );
660 case DVDNAV_STILL_FRAME:
662 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
663 bool b_still_init = false;
665 vlc_mutex_lock( &p_sys->p_ev->lock );
666 if( !p_sys->p_ev->b_still )
668 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
669 msg_Dbg( p_demux, " - length=0x%x", event->length );
670 p_sys->p_ev->b_still = true;
672 if( event->length == 0xff )
673 p_sys->p_ev->i_still_end = 0;
675 p_sys->p_ev->i_still_end = mdate() +
676 event->length * INT64_C(1000000);
678 vlc_mutex_unlock( &p_sys->p_ev->lock );
682 /* We send a dummy mpeg2 end of sequence to force still frame display */
683 static const uint8_t buffer[] = {
684 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
686 0x00, 0x00, 0x01, 0xB7,
688 DemuxBlock( p_demux, buffer, sizeof(buffer) );
691 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
692 p_sys->b_reset_pcr = true;
698 case DVDNAV_SPU_CLUT_CHANGE:
702 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
703 /* Update color lookup table (16 *uint32_t in packet) */
704 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
706 /* HACK to get the SPU tracks registered in the right order */
707 for( i = 0; i < 0x1f; i++ )
709 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
710 ESNew( p_demux, 0xbd20 + i );
716 case DVDNAV_SPU_STREAM_CHANGE:
718 dvdnav_spu_stream_change_event_t *event =
719 (dvdnav_spu_stream_change_event_t*)packet;
722 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
723 msg_Dbg( p_demux, " - physical_wide=%d",
724 event->physical_wide );
725 msg_Dbg( p_demux, " - physical_letterbox=%d",
726 event->physical_letterbox);
727 msg_Dbg( p_demux, " - physical_pan_scan=%d",
728 event->physical_pan_scan );
730 ESSubtitleUpdate( p_demux );
731 p_sys->b_spu_change = true;
733 /* HACK to get the SPU tracks registered in the right order */
734 for( i = 0; i < 0x1f; i++ )
736 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
737 ESNew( p_demux, 0xbd20 + i );
743 case DVDNAV_AUDIO_STREAM_CHANGE:
745 dvdnav_audio_stream_change_event_t *event =
746 (dvdnav_audio_stream_change_event_t*)packet;
747 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
748 msg_Dbg( p_demux, " - physical=%d", event->physical );
753 case DVDNAV_VTS_CHANGE:
759 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
760 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
761 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
762 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
764 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
765 /* TODO check if we always have VTS and CELL */
766 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
769 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
771 for( i = 0; i < PS_TK_COUNT; i++ )
773 ps_track_t *tk = &p_sys->tk[i];
776 es_format_Clean( &tk->fmt );
777 if( tk->es ) es_out_Del( p_demux->out, tk->es );
782 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
783 &i_part ) == DVDNAV_STATUS_OK )
785 if( i_title >= 0 && i_title < p_sys->i_title &&
786 p_demux->info.i_title != i_title )
788 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
789 p_demux->info.i_title = i_title;
795 case DVDNAV_CELL_CHANGE:
800 dvdnav_cell_change_event_t *event =
801 (dvdnav_cell_change_event_t*)packet;
802 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
803 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
804 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
805 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
806 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
807 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
808 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
809 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
811 /* Store the lenght in time of the current PGC */
812 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
814 /* FIXME is it correct or there is better way to know chapter change */
815 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
816 &i_part ) == DVDNAV_STATUS_OK )
818 if( i_title >= 0 && i_title < p_sys->i_title &&
819 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
820 p_demux->info.i_seekpoint != i_part - 1 )
822 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
823 p_demux->info.i_seekpoint = i_part - 1;
829 case DVDNAV_NAV_PACKET:
832 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
834 /* A lot of thing to do here :
836 * - fetch pts (for time display)
839 DemuxBlock( p_demux, packet, i_len );
840 if( p_sys->b_spu_change )
842 ButtonUpdate( p_demux, false );
843 p_sys->b_spu_change = false;
848 case DVDNAV_STOP: /* EOF */
849 msg_Dbg( p_demux, "DVDNAV_STOP" );
852 dvdnav_free_cache_block( p_sys->dvdnav, packet );
856 case DVDNAV_HIGHLIGHT:
858 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
859 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
860 msg_Dbg( p_demux, " - display=%d", event->display );
861 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
862 ButtonUpdate( p_demux, false );
866 case DVDNAV_HOP_CHANNEL:
867 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
868 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
872 msg_Dbg( p_demux, "DVDNAV_WAIT" );
875 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
882 dvdnav_wait_skip( p_sys->dvdnav );
883 p_sys->b_reset_pcr = true;
888 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
893 dvdnav_free_cache_block( p_sys->dvdnav, packet );
900 * FIXME: partiallyy duplicated from src/input/es_out.c
902 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
904 const iso639_lang_t *pl;
908 psz_lang = var_CreateGetString( p_demux, psz_var );
910 return strdup(LANGUAGE_DEFAULT);
912 /* XXX: we will use only the first value
913 * (and ignore other ones in case of a list) */
914 if( ( p = strchr( psz_lang, ',' ) ) )
917 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
919 if( *psz_lang == '\0' )
921 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
922 !strcasecmp( pl->psz_native_name, psz_lang ) ||
923 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
924 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
925 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
931 if( pl->psz_iso639_1 != NULL )
932 return strdup( pl->psz_iso639_1 );
934 return strdup(LANGUAGE_DEFAULT);
937 static void DemuxTitles( demux_t *p_demux )
939 demux_sys_t *p_sys = p_demux->p_sys;
946 t = vlc_input_title_New();
948 t->psz_name = strdup( "DVD Menu" );
950 s = vlc_seekpoint_New();
951 s->psz_name = strdup( "Resume" );
952 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
954 s = vlc_seekpoint_New();
955 s->psz_name = strdup( "Root" );
956 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
958 s = vlc_seekpoint_New();
959 s->psz_name = strdup( "Title" );
960 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
962 s = vlc_seekpoint_New();
963 s->psz_name = strdup( "Chapter" );
964 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
966 s = vlc_seekpoint_New();
967 s->psz_name = strdup( "Subtitle" );
968 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
970 s = vlc_seekpoint_New();
971 s->psz_name = strdup( "Audio" );
972 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
974 s = vlc_seekpoint_New();
975 s->psz_name = strdup( "Angle" );
976 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
978 TAB_APPEND( p_sys->i_title, p_sys->title, t );
980 /* Find out number of titles/chapters */
981 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
982 for( i = 1; i <= i_titles; i++ )
984 int32_t i_chapters = 0;
987 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
989 t = vlc_input_title_New();
990 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
992 s = vlc_seekpoint_New();
993 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
996 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1000 /*****************************************************************************
1002 *****************************************************************************/
1003 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1005 demux_sys_t *p_sys = p_demux->p_sys;
1007 int32_t i_title, i_part;
1009 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1011 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
1013 vlc_mutex_t *p_mutex = val.p_address;
1014 dvdnav_highlight_area_t hl;
1018 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1019 != DVDNAV_STATUS_OK )
1021 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1025 b_button_ok = false;
1026 if( i_button > 0 && i_title == 0 )
1028 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1030 b_button_ok = dvdnav_get_highlight_area( pci, i_button, b_mode, &hl ) == DVDNAV_STATUS_OK;
1035 for( i = 0; i < 4; i++ )
1037 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1038 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1040 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1041 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1042 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1043 p_sys->palette[i][3] = i_alpha;
1046 vlc_mutex_lock( p_mutex );
1047 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
1048 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
1049 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
1050 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
1052 val.p_address = (void *)p_sys->palette;
1053 var_Set( p_sys->p_input, "menu-palette", val );
1055 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
1056 vlc_mutex_unlock( p_mutex );
1058 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1062 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1063 i_button, i_title );
1066 vlc_mutex_lock( p_mutex );
1068 var_Set( p_sys->p_input, "highlight", val );
1069 vlc_mutex_unlock( p_mutex );
1074 static void ESSubtitleUpdate( demux_t *p_demux )
1076 demux_sys_t *p_sys = p_demux->p_sys;
1077 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1078 int32_t i_title, i_part;
1080 ButtonUpdate( p_demux, false );
1082 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1083 if( i_title > 0 ) return;
1085 if( i_spu >= 0 && i_spu <= 0x1f )
1087 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1089 ESNew( p_demux, 0xbd20 + i_spu );
1091 /* be sure to unselect it (reset) */
1092 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1096 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1100 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1102 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1105 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1112 /*****************************************************************************
1113 * DemuxBlock: demux a given block
1114 *****************************************************************************/
1115 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1117 demux_sys_t *p_sys = p_demux->p_sys;
1118 const uint8_t *p = pkt;
1120 while( p < &pkt[i_pkt] )
1122 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1129 /* Create a block */
1130 p_pkt = block_New( p_demux, i_size );
1131 memcpy( p_pkt->p_buffer, p, i_size);
1133 /* Parse it and send it */
1134 switch( 0x100 | p[3] )
1142 msg_Warn( p_demux, "received a PSM packet" );
1144 else if( p[3] == 0xbb )
1146 msg_Warn( p_demux, "received a SYSTEM packet" );
1149 block_Release( p_pkt );
1156 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1158 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1159 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1161 block_Release( p_pkt );
1166 int i_id = ps_pkt_id( p_pkt );
1169 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1173 ESNew( p_demux, i_id );
1175 if( tk->b_seen && tk->es &&
1176 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1178 es_out_Send( p_demux->out, tk->es, p_pkt );
1182 block_Release( p_pkt );
1187 block_Release( p_pkt );
1199 /*****************************************************************************
1200 * ESNew: register a new elementary stream
1201 *****************************************************************************/
1202 static void ESNew( demux_t *p_demux, int i_id )
1204 demux_sys_t *p_sys = p_demux->p_sys;
1205 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1206 bool b_select = false;
1208 if( tk->b_seen ) return;
1210 if( ps_track_fill( tk, 0, i_id ) )
1212 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1217 if( tk->fmt.i_cat == VIDEO_ES )
1219 switch( p_sys->i_aspect )
1221 case 1: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR; break;
1222 case 2: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3; break;
1223 case 3: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9; break;
1224 case 4: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 221 / 10; break;
1226 tk->fmt.video.i_aspect = 0;
1231 else if( tk->fmt.i_cat == AUDIO_ES )
1234 /* find the audio number PLEASE find another way */
1235 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1237 i_audio = i_id&0x07;
1239 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1243 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1245 i_audio = i_id&0x1f;
1247 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1249 i_audio = i_id&0x1f;
1253 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1254 if( i_lang != 0xffff )
1256 tk->fmt.psz_language = malloc( 3 );
1257 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1258 tk->fmt.psz_language[1] = (i_lang )&0xff;
1259 tk->fmt.psz_language[2] = 0;
1261 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1267 else if( tk->fmt.i_cat == SPU_ES )
1269 int32_t i_title, i_part;
1270 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1271 if( i_lang != 0xffff )
1273 tk->fmt.psz_language = malloc( 3 );
1274 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1275 tk->fmt.psz_language[1] = (i_lang )&0xff;
1276 tk->fmt.psz_language[2] = 0;
1280 tk->fmt.subs.spu.palette[0] = 0xBeef;
1281 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1282 16 * sizeof( uint32_t ) );
1284 /* We select only when we are not in the menu */
1285 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1287 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1293 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1296 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1300 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1303 /*****************************************************************************
1304 * Event handler code
1305 *****************************************************************************/
1306 static void* EventThread( vlc_object_t *p_this )
1308 event_thread_t *p_ev = (event_thread_t*)p_this;
1309 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1310 int canc = vlc_savecancel ();
1313 while( vlc_object_alive( p_ev ) )
1315 vlc_mutex_lock( &p_ev->lock );
1321 if( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() )
1323 p_ev->b_still = false;
1324 dvdnav_still_skip( p_sys->dvdnav );
1327 vlc_mutex_unlock( &p_ev->lock );
1332 vlc_restorecancel (canc);
1337 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1338 vlc_value_t oldval, vlc_value_t val, void *p_data )
1340 demux_t *p_demux = p_data;
1341 demux_sys_t *p_sys = p_demux->p_sys;
1343 /* FIXME? PCI usage thread safe? */
1344 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1345 int x = var_GetInteger( p_vout, "mouse-x" );
1346 int y = var_GetInteger( p_vout, "mouse-y" );
1348 if( psz_var[6] == 'm' ) /* mouse-moved */
1349 dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1352 assert( psz_var[6] == 'c' ); /* mouse-clicked */
1354 ButtonUpdate( p_demux, true );
1355 dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1358 (void)oldval; (void)val;
1362 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1363 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1365 demux_t *p_demux = p_data;
1366 demux_sys_t *p_sys = p_demux->p_sys;
1368 /* FIXME: thread-safe ? */
1369 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1371 switch( newval.i_int )
1373 case ACTIONID_NAV_LEFT:
1374 dvdnav_left_button_select( p_sys->dvdnav, pci );
1376 case ACTIONID_NAV_RIGHT:
1377 dvdnav_right_button_select( p_sys->dvdnav, pci );
1379 case ACTIONID_NAV_UP:
1380 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1382 case ACTIONID_NAV_DOWN:
1383 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1385 case ACTIONID_NAV_ACTIVATE:
1386 ButtonUpdate( p_demux, true );
1387 dvdnav_button_activate( p_sys->dvdnav, pci );
1391 (void)p_this; (void)psz_var; (void)oldval;
1395 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1396 vlc_value_t oldval, vlc_value_t val, void *p_data )
1398 demux_t *p_demux = p_data;
1399 demux_sys_t *p_sys = p_demux->p_sys;
1401 if (val.i_int == INPUT_EVENT_VOUT)
1403 vlc_object_t *p_vout;
1405 p_vout = p_sys->p_vout;
1406 if( p_vout != NULL )
1408 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1409 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1410 vlc_object_release( p_vout );
1413 p_vout = (vlc_object_t *)input_GetVout( (input_thread_t *)p_input );
1414 p_sys->p_vout = p_vout;
1415 if( p_vout != NULL )
1417 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1418 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1421 (void) psz_var; (void) oldval;
1425 /*****************************************************************************
1426 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1427 *****************************************************************************/
1428 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1431 #ifdef HAVE_SYS_STAT_H
1432 struct stat stat_info;
1433 uint8_t pi_anchor[2];
1438 /* Triggers libdvdcss autodetection */
1442 if( !strcmp( psz_name, "-" ) ) /* stdin -> file access */
1443 return VLC_EGENERIC;
1445 if( (i_fd = utf8_open( psz_name, O_RDONLY |O_NONBLOCK, 0666 )) == -1 )
1447 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1450 if( fstat( i_fd, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1454 if( S_ISFIFO( stat_info.st_mode ) )
1455 return VLC_EGENERIC;
1456 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1459 /* Try to find the anchor (2 bytes at LBA 256) */
1460 i_ret = VLC_EGENERIC;
1461 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1462 && read( i_fd, pi_anchor, 2 ) == 2
1463 && GetWLE( pi_anchor ) == 2 )
1464 i_ret = VLC_SUCCESS; /* Found a potential anchor */