1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004-2009 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_demux.h>
38 #include <vlc_charset.h>
43 #include <vlc_dialog.h>
48 #include <sys/types.h>
49 #ifdef HAVE_SYS_STAT_H
50 # include <sys/stat.h>
58 #include <vlc_iso_lang.h>
60 /* FIXME we should find a better way than including that */
61 #include "../../src/text/iso-639_def.h"
64 #include <dvdnav/dvdnav.h>
66 #include "../demux/ps.h"
68 /*****************************************************************************
70 *****************************************************************************/
71 #define ANGLE_TEXT N_("DVD angle")
72 #define ANGLE_LONGTEXT N_( \
73 "Default DVD angle." )
75 #define CACHING_TEXT N_("Caching value in ms")
76 #define CACHING_LONGTEXT N_( \
77 "Caching value for DVDs. This "\
78 "value should be set in milliseconds." )
79 #define MENU_TEXT N_("Start directly in menu")
80 #define MENU_LONGTEXT N_( \
81 "Start the DVD directly in the main menu. This "\
82 "will try to skip all the useless warning introductions." )
84 #define LANGUAGE_DEFAULT ("en")
86 static int Open ( vlc_object_t * );
87 static void Close( vlc_object_t * );
90 set_shortname( N_("DVD with menus") )
91 set_description( N_("DVDnav Input") )
92 set_category( CAT_INPUT )
93 set_subcategory( SUBCAT_INPUT_ACCESS )
94 add_integer( "dvdnav-angle", 1, ANGLE_TEXT,
95 ANGLE_LONGTEXT, false )
96 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000,
97 CACHING_TEXT, CACHING_LONGTEXT, true )
98 add_bool( "dvdnav-menu", true,
99 MENU_TEXT, MENU_LONGTEXT, false )
100 set_capability( "access_demux", 5 )
101 add_shortcut( "dvd", "dvdnav", "file" )
102 set_callbacks( Open, Close )
105 /* Shall we use libdvdnav's read ahead cache? */
106 #define DVD_READ_CACHE 1
108 /*****************************************************************************
110 *****************************************************************************/
127 ps_track_t tk[PS_TK_COUNT];
130 /* for spu variables */
131 input_thread_t *p_input;
134 vout_thread_t *p_vout;
136 /* palette for menus */
138 uint8_t palette[4][4];
149 input_title_t **title;
151 /* lenght of program group chain */
152 mtime_t i_pgc_length;
157 static int Control( demux_t *, int, va_list );
158 static int Demux( demux_t * );
159 static int DemuxBlock( demux_t *, const uint8_t *, int );
160 static void DemuxForceStill( demux_t * );
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( const char * );
169 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
171 static int ControlInternal( demux_t *, int, ... );
173 static void StillTimer( void * );
175 static int EventKey( vlc_object_t *, char const *,
176 vlc_value_t, vlc_value_t, void * );
177 static int EventMouse( vlc_object_t *, char const *,
178 vlc_value_t, vlc_value_t, void * );
179 static int EventIntf( vlc_object_t *, char const *,
180 vlc_value_t, vlc_value_t, void * );
182 /*****************************************************************************
184 *****************************************************************************/
185 static int Open( vlc_object_t *p_this )
187 demux_t *p_demux = (demux_t*)p_this;
195 if( p_demux->psz_access != NULL
196 && !strncmp(p_demux->psz_access, "dvd", 3) )
199 if( !p_demux->psz_file || !*p_demux->psz_file )
201 /* Only when selected */
205 psz_file = var_InheritString( p_this, "dvd" );
208 psz_file = strdup( p_demux->psz_file );
210 #if defined( WIN32 ) || defined( __OS2__ )
211 if( psz_file != NULL )
213 /* Remove trailing backslash, otherwise dvdnav_open will fail */
214 size_t flen = strlen( psz_file );
215 if( flen > 0 && psz_file[flen - 1] == '\\' )
216 psz_file[flen - 1] = '\0';
219 psz_file = strdup("");
221 if( unlikely(psz_file == NULL) )
224 /* Try some simple probing to avoid going through dvdnav_open too often */
225 if( !forced && ProbeDVD( psz_file ) != VLC_SUCCESS )
232 const char *psz_path = ToLocale( psz_file );
233 if( dvdnav_open( &p_dvdnav, psz_path ) != DVDNAV_STATUS_OK )
235 LocaleFree( psz_path );
236 if( p_dvdnav == NULL )
238 msg_Warn( p_demux, "cannot open DVD (%s)", psz_file);
244 /* Fill p_demux field */
245 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
246 p_sys->dvdnav = p_dvdnav;
247 p_sys->b_reset_pcr = false;
249 ps_track_init( p_sys->tk );
250 p_sys->sar.i_num = 0;
251 p_sys->sar.i_den = 0;
252 p_sys->i_mux_rate = 0;
253 p_sys->i_pgc_length = 0;
254 p_sys->b_spu_change = false;
255 p_sys->i_vobu_index = 0;
256 p_sys->i_vobu_flush = 0;
260 // Hack for libdvdnav CVS.
261 // Without it dvdnav_get_number_of_titles() fails.
262 // Remove when fixed in libdvdnav CVS.
263 uint8_t buffer[DVD_VIDEO_LB_LEN];
266 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
267 == DVDNAV_STATUS_ERR )
269 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
272 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
275 /* Configure dvdnav */
276 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
279 msg_Warn( p_demux, "cannot set read-a-head flag" );
282 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
285 msg_Warn( p_demux, "cannot set PGC positioning flag" );
289 * XXX A menu-language may be better than sub-language */
290 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
291 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
294 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
295 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
296 /* We try to fall back to 'en' */
297 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
298 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
302 /* Set audio language */
303 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
304 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
307 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
308 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
309 /* We try to fall back to 'en' */
310 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
311 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
315 /* Set spu language */
316 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
317 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
320 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
321 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
322 /* We try to fall back to 'en' */
323 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
324 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
328 DemuxTitles( p_demux );
330 if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
332 msg_Dbg( p_demux, "trying to go to dvd menu" );
334 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
336 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
337 dialog_Fatal( p_demux, _("Playback failure"), "%s",
338 _("VLC cannot set the DVD's title. It possibly "
339 "cannot decrypt the entire disc.") );
340 dvdnav_close( p_sys->dvdnav );
345 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
348 /* Try going to menu root */
349 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
351 msg_Warn( p_demux, "cannot go to dvd menu" );
355 i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
356 if( i_angle <= 0 ) i_angle = 1;
358 /* Update default_pts to a suitable value for dvdnav access */
359 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
361 /* FIXME hack hack hack hack FIXME */
362 /* Get p_input and create variable */
363 p_sys->p_input = demux_GetParentInput( p_demux );
364 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
365 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
366 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
367 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
368 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
369 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
370 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
372 /* catch all key event */
373 var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
374 /* catch vout creation event */
375 var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
377 p_sys->still.b_enabled = false;
378 vlc_mutex_init( &p_sys->still.lock );
379 if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
380 p_sys->still.b_created = true;
385 /*****************************************************************************
387 *****************************************************************************/
388 static void Close( vlc_object_t *p_this )
390 demux_t *p_demux = (demux_t*)p_this;
391 demux_sys_t *p_sys = p_demux->p_sys;
393 /* Stop vout event handler */
394 var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
395 if( p_sys->p_vout != NULL )
396 { /* Should not happen, but better be safe than sorry. */
397 msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
398 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
399 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
402 /* Stop key event handler (FIXME: should really be per-vout too) */
403 var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
405 /* Stop still image handler */
406 if( p_sys->still.b_created )
407 vlc_timer_destroy( p_sys->still.timer );
408 vlc_mutex_destroy( &p_sys->still.lock );
410 var_Destroy( p_sys->p_input, "highlight" );
411 var_Destroy( p_sys->p_input, "x-start" );
412 var_Destroy( p_sys->p_input, "x-end" );
413 var_Destroy( p_sys->p_input, "y-start" );
414 var_Destroy( p_sys->p_input, "y-end" );
415 var_Destroy( p_sys->p_input, "color" );
416 var_Destroy( p_sys->p_input, "menu-palette" );
418 vlc_object_release( p_sys->p_input );
420 for( int i = 0; i < PS_TK_COUNT; i++ )
422 ps_track_t *tk = &p_sys->tk[i];
425 es_format_Clean( &tk->fmt );
426 if( tk->es ) es_out_Del( p_demux->out, tk->es );
430 /* Free the array of titles */
431 for( int i = 0; i < p_sys->i_title; i++ )
432 vlc_input_title_Delete( p_sys->title[i] );
433 TAB_CLEAN( p_sys->i_title, p_sys->title );
435 dvdnav_close( p_sys->dvdnav );
439 /*****************************************************************************
441 *****************************************************************************/
442 static int Control( demux_t *p_demux, int i_query, va_list args )
444 demux_sys_t *p_sys = p_demux->p_sys;
445 input_title_t ***ppp_title;
450 case DEMUX_SET_POSITION:
451 case DEMUX_GET_POSITION:
453 case DEMUX_GET_LENGTH:
456 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
457 DVDNAV_STATUS_OK || len == 0 )
464 case DEMUX_GET_POSITION:
465 *va_arg( args, double* ) = (double)pos / (double)len;
468 case DEMUX_SET_POSITION:
469 pos = va_arg( args, double ) * len;
470 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
478 if( p_sys->i_pgc_length > 0 )
480 *va_arg( args, int64_t * ) = p_sys->i_pgc_length*pos/len;
485 case DEMUX_GET_LENGTH:
486 if( p_sys->i_pgc_length > 0 )
488 *va_arg( args, int64_t * ) = (int64_t)p_sys->i_pgc_length;
496 /* Special for access_demux */
497 case DEMUX_CAN_PAUSE:
499 case DEMUX_CAN_CONTROL_PACE:
501 *va_arg( args, bool * ) = true;
504 case DEMUX_SET_PAUSE_STATE:
507 case DEMUX_GET_TITLE_INFO:
508 ppp_title = va_arg( args, input_title_t*** );
509 *va_arg( args, int* ) = p_sys->i_title;
510 *va_arg( args, int* ) = 0; /* Title offset */
511 *va_arg( args, int* ) = 1; /* Chapter offset */
513 /* Duplicate title infos */
514 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
515 for( i = 0; i < p_sys->i_title; i++ )
517 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
521 case DEMUX_SET_TITLE:
522 i = (int)va_arg( args, int );
523 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
524 != DVDNAV_STATUS_OK ) ||
525 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
526 != DVDNAV_STATUS_OK ) )
528 msg_Warn( p_demux, "cannot set title/chapter" );
531 p_demux->info.i_update |=
532 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
533 p_demux->info.i_title = i;
534 p_demux->info.i_seekpoint = 0;
537 case DEMUX_SET_SEEKPOINT:
538 i = va_arg( args, int );
539 if( p_demux->info.i_title == 0 )
541 static const int argtab[] = {
550 enum { numargs = sizeof(argtab)/sizeof(int) };
551 if( (unsigned)i >= numargs || DVDNAV_STATUS_OK !=
552 dvdnav_menu_call(p_sys->dvdnav,argtab[i]) )
555 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
556 i + 1 ) != DVDNAV_STATUS_OK )
558 msg_Warn( p_demux, "cannot set title/chapter" );
561 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
562 p_demux->info.i_seekpoint = i;
565 case DEMUX_GET_PTS_DELAY:
566 *va_arg( args, int64_t * )
567 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
572 const char *title_name = NULL;
574 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
575 if( (NULL != title_name) && ('\0' != title_name[0]) )
577 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
578 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
584 /* TODO implement others */
590 static int ControlInternal( demux_t *p_demux, int i_query, ... )
595 va_start( args, i_query );
596 i_result = Control( p_demux, i_query, args );
601 /*****************************************************************************
603 *****************************************************************************/
604 static int Demux( demux_t *p_demux )
606 demux_sys_t *p_sys = p_demux->p_sys;
608 uint8_t buffer[DVD_VIDEO_LB_LEN];
609 uint8_t *packet = buffer;
614 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
615 == DVDNAV_STATUS_ERR )
617 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
618 == DVDNAV_STATUS_ERR )
621 msg_Warn( p_demux, "cannot get next block (%s)",
622 dvdnav_err_to_string( p_sys->dvdnav ) );
623 if( p_demux->info.i_title == 0 )
625 msg_Dbg( p_demux, "jumping to first title" );
626 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
633 case DVDNAV_BLOCK_OK: /* mpeg block */
634 vlc_mutex_lock( &p_sys->still.lock );
635 vlc_timer_schedule( p_sys->still.timer, false, 0, 0 );
636 p_sys->still.b_enabled = false;
637 vlc_mutex_unlock( &p_sys->still.lock );
638 if( p_sys->b_reset_pcr )
640 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
641 p_sys->b_reset_pcr = false;
643 DemuxBlock( p_demux, packet, i_len );
644 if( p_sys->i_vobu_index > 0 )
646 if( p_sys->i_vobu_flush == p_sys->i_vobu_index )
647 DemuxForceStill( p_demux );
648 p_sys->i_vobu_index++;
652 case DVDNAV_NOP: /* Nothing */
653 msg_Dbg( p_demux, "DVDNAV_NOP" );
656 case DVDNAV_STILL_FRAME:
658 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
659 bool b_still_init = false;
661 vlc_mutex_lock( &p_sys->still.lock );
662 if( !p_sys->still.b_enabled )
664 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
665 msg_Dbg( p_demux, " - length=0x%x", event->length );
666 p_sys->still.b_enabled = true;
668 if( event->length != 0xff && p_sys->still.b_created )
670 mtime_t delay = event->length * CLOCK_FREQ;
671 vlc_timer_schedule( p_sys->still.timer, false, delay, 0 );
676 vlc_mutex_unlock( &p_sys->still.lock );
680 DemuxForceStill( p_demux );
681 p_sys->b_reset_pcr = true;
687 case DVDNAV_SPU_CLUT_CHANGE:
691 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
692 /* Update color lookup table (16 *uint32_t in packet) */
693 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
695 /* HACK to get the SPU tracks registered in the right order */
696 for( i = 0; i < 0x1f; i++ )
698 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
699 ESNew( p_demux, 0xbd20 + i );
705 case DVDNAV_SPU_STREAM_CHANGE:
707 dvdnav_spu_stream_change_event_t *event =
708 (dvdnav_spu_stream_change_event_t*)packet;
711 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
712 msg_Dbg( p_demux, " - physical_wide=%d",
713 event->physical_wide );
714 msg_Dbg( p_demux, " - physical_letterbox=%d",
715 event->physical_letterbox);
716 msg_Dbg( p_demux, " - physical_pan_scan=%d",
717 event->physical_pan_scan );
719 ESSubtitleUpdate( p_demux );
720 p_sys->b_spu_change = true;
722 /* HACK to get the SPU tracks registered in the right order */
723 for( i = 0; i < 0x1f; i++ )
725 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
726 ESNew( p_demux, 0xbd20 + i );
732 case DVDNAV_AUDIO_STREAM_CHANGE:
734 dvdnav_audio_stream_change_event_t *event =
735 (dvdnav_audio_stream_change_event_t*)packet;
736 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
737 msg_Dbg( p_demux, " - physical=%d", event->physical );
742 case DVDNAV_VTS_CHANGE:
748 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
749 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
750 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
751 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
754 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
756 for( i = 0; i < PS_TK_COUNT; i++ )
758 ps_track_t *tk = &p_sys->tk[i];
761 es_format_Clean( &tk->fmt );
762 if( tk->es ) es_out_Del( p_demux->out, tk->es );
767 #if defined(HAVE_DVDNAV_GET_VIDEO_RESOLUTION)
768 uint32_t i_width, i_height;
769 if( dvdnav_get_video_resolution( p_sys->dvdnav,
770 &i_width, &i_height ) )
771 i_width = i_height = 0;
772 switch( dvdnav_get_video_aspect( p_sys->dvdnav ) )
775 p_sys->sar.i_num = 4 * i_height;
776 p_sys->sar.i_den = 3 * i_width;
779 p_sys->sar.i_num = 16 * i_height;
780 p_sys->sar.i_den = 9 * i_width;
783 p_sys->sar.i_num = 0;
784 p_sys->sar.i_den = 0;
789 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
790 &i_part ) == DVDNAV_STATUS_OK )
792 if( i_title >= 0 && i_title < p_sys->i_title &&
793 p_demux->info.i_title != i_title )
795 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
796 p_demux->info.i_title = i_title;
802 case DVDNAV_CELL_CHANGE:
807 dvdnav_cell_change_event_t *event =
808 (dvdnav_cell_change_event_t*)packet;
809 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
810 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
811 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
812 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
813 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
814 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
815 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
816 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
818 /* Store the lenght in time of the current PGC */
819 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
820 p_sys->i_vobu_index = 0;
821 p_sys->i_vobu_flush = 0;
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:
840 p_sys->i_vobu_index = 1;
841 p_sys->i_vobu_flush = 0;
843 /* Look if we have need to force a flush (and when) */
844 const pci_gi_t *p_pci_gi = &dvdnav_get_current_nav_pci( p_sys->dvdnav )->pci_gi;
845 if( p_pci_gi->vobu_se_e_ptm != 0 && p_pci_gi->vobu_se_e_ptm < p_pci_gi->vobu_e_ptm )
847 const dsi_gi_t *p_dsi_gi = &dvdnav_get_current_nav_dsi( p_sys->dvdnav )->dsi_gi;
848 if( p_dsi_gi->vobu_3rdref_ea != 0 )
849 p_sys->i_vobu_flush = p_dsi_gi->vobu_3rdref_ea;
850 else if( p_dsi_gi->vobu_2ndref_ea != 0 )
851 p_sys->i_vobu_flush = p_dsi_gi->vobu_2ndref_ea;
852 else if( p_dsi_gi->vobu_1stref_ea != 0 )
853 p_sys->i_vobu_flush = p_dsi_gi->vobu_1stref_ea;
857 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
859 /* A lot of thing to do here :
861 * - fetch pts (for time display)
864 DemuxBlock( p_demux, packet, i_len );
865 if( p_sys->b_spu_change )
867 ButtonUpdate( p_demux, false );
868 p_sys->b_spu_change = false;
873 case DVDNAV_STOP: /* EOF */
874 msg_Dbg( p_demux, "DVDNAV_STOP" );
877 dvdnav_free_cache_block( p_sys->dvdnav, packet );
881 case DVDNAV_HIGHLIGHT:
883 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
884 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
885 msg_Dbg( p_demux, " - display=%d", event->display );
886 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
887 ButtonUpdate( p_demux, false );
891 case DVDNAV_HOP_CHANNEL:
892 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
893 p_sys->i_vobu_index = 0;
894 p_sys->i_vobu_flush = 0;
895 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
899 msg_Dbg( p_demux, "DVDNAV_WAIT" );
902 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
909 dvdnav_wait_skip( p_sys->dvdnav );
910 p_sys->b_reset_pcr = true;
915 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
920 dvdnav_free_cache_block( p_sys->dvdnav, packet );
927 * FIXME: partiallyy duplicated from src/input/es_out.c
929 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
931 const iso639_lang_t *pl;
935 psz_lang = var_CreateGetString( p_demux, psz_var );
937 return strdup(LANGUAGE_DEFAULT);
939 /* XXX: we will use only the first value
940 * (and ignore other ones in case of a list) */
941 if( ( p = strchr( psz_lang, ',' ) ) )
944 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
946 if( *psz_lang == '\0' )
948 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
949 !strcasecmp( pl->psz_native_name, psz_lang ) ||
950 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
951 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
952 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
958 if( pl->psz_eng_name != NULL )
959 return strdup( pl->psz_iso639_1 );
961 return strdup(LANGUAGE_DEFAULT);
964 static void DemuxTitles( demux_t *p_demux )
966 demux_sys_t *p_sys = p_demux->p_sys;
973 t = vlc_input_title_New();
975 t->psz_name = strdup( "DVD Menu" );
977 s = vlc_seekpoint_New();
978 s->psz_name = strdup( "Resume" );
979 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
981 s = vlc_seekpoint_New();
982 s->psz_name = strdup( "Root" );
983 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
985 s = vlc_seekpoint_New();
986 s->psz_name = strdup( "Title" );
987 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
989 s = vlc_seekpoint_New();
990 s->psz_name = strdup( "Chapter" );
991 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
993 s = vlc_seekpoint_New();
994 s->psz_name = strdup( "Subtitle" );
995 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
997 s = vlc_seekpoint_New();
998 s->psz_name = strdup( "Audio" );
999 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1001 s = vlc_seekpoint_New();
1002 s->psz_name = strdup( "Angle" );
1003 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1005 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1007 /* Find out number of titles/chapters */
1008 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
1009 for( i = 1; i <= i_titles; i++ )
1012 uint64_t i_title_length;
1014 #if defined(HAVE_DVDNAV_DESCRIBE_TITLE_CHAPTERS)
1015 uint64_t *p_chapters_time;
1016 i_chapters = dvdnav_describe_title_chapters( p_sys->dvdnav, i,
1019 if( i_chapters > 0 )
1020 free( p_chapters_time );
1024 if( dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters ) != DVDNAV_STATUS_OK )
1028 t = vlc_input_title_New();
1029 t->i_length = i_title_length * 1000 / 90;
1030 for( int j = 0; j < __MAX( i_chapters, 1 ); j++ )
1032 s = vlc_seekpoint_New();
1033 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1036 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1040 /*****************************************************************************
1042 *****************************************************************************/
1043 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1045 demux_sys_t *p_sys = p_demux->p_sys;
1046 int32_t i_title, i_part;
1048 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1050 dvdnav_highlight_area_t hl;
1054 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1055 != DVDNAV_STATUS_OK )
1057 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1061 b_button_ok = false;
1062 if( i_button > 0 && i_title == 0 )
1064 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1066 b_button_ok = DVDNAV_STATUS_OK ==
1067 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
1072 for( unsigned i = 0; i < 4; i++ )
1074 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1075 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1077 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1078 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1079 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1080 p_sys->palette[i][3] = i_alpha;
1083 vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1084 var_SetInteger( p_sys->p_input, "x-start", hl.sx );
1085 var_SetInteger( p_sys->p_input, "x-end", hl.ex );
1086 var_SetInteger( p_sys->p_input, "y-start", hl.sy );
1087 var_SetInteger( p_sys->p_input, "y-end", hl.ey );
1089 var_SetAddress( p_sys->p_input, "menu-palette", p_sys->palette );
1090 var_SetBool( p_sys->p_input, "highlight", true );
1092 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1096 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1097 i_button, i_title );
1100 vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1101 var_SetBool( p_sys->p_input, "highlight", false );
1103 vlc_global_unlock( VLC_HIGHLIGHT_MUTEX );
1106 static void ESSubtitleUpdate( demux_t *p_demux )
1108 demux_sys_t *p_sys = p_demux->p_sys;
1109 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1110 int32_t i_title, i_part;
1112 ButtonUpdate( p_demux, false );
1114 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1115 if( i_title > 0 ) return;
1117 if( i_spu >= 0 && i_spu <= 0x1f )
1119 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1121 ESNew( p_demux, 0xbd20 + i_spu );
1123 /* be sure to unselect it (reset) */
1124 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1128 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1132 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1134 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1137 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1144 /*****************************************************************************
1145 * DemuxBlock: demux a given block
1146 *****************************************************************************/
1147 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
1149 demux_sys_t *p_sys = p_demux->p_sys;
1153 int i_size = ps_pkt_size( p, len );
1154 if( i_size <= 0 || i_size > len )
1159 /* Create a block */
1160 block_t *p_pkt = block_New( p_demux, i_size );
1161 memcpy( p_pkt->p_buffer, p, i_size);
1163 /* Parse it and send it */
1164 switch( 0x100 | p[3] )
1172 msg_Warn( p_demux, "received a PSM packet" );
1174 else if( p[3] == 0xbb )
1176 msg_Warn( p_demux, "received a SYSTEM packet" );
1179 block_Release( p_pkt );
1186 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1188 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1189 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1191 block_Release( p_pkt );
1196 int i_id = ps_pkt_id( p_pkt );
1199 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1203 ESNew( p_demux, i_id );
1205 if( tk->b_seen && tk->es &&
1206 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1208 es_out_Send( p_demux->out, tk->es, p_pkt );
1212 block_Release( p_pkt );
1217 block_Release( p_pkt );
1230 /*****************************************************************************
1231 * Force still images to be displayed by sending EOS and stopping buffering.
1232 *****************************************************************************/
1233 static void DemuxForceStill( demux_t *p_demux )
1235 static const uint8_t buffer[] = {
1236 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
1238 0x00, 0x00, 0x01, 0xB7,
1240 DemuxBlock( p_demux, buffer, sizeof(buffer) );
1243 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
1246 /*****************************************************************************
1247 * ESNew: register a new elementary stream
1248 *****************************************************************************/
1249 static void ESNew( demux_t *p_demux, int i_id )
1251 demux_sys_t *p_sys = p_demux->p_sys;
1252 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1253 bool b_select = false;
1255 if( tk->b_seen ) return;
1257 if( ps_track_fill( tk, 0, i_id ) )
1259 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1264 if( tk->fmt.i_cat == VIDEO_ES )
1266 tk->fmt.video.i_sar_num = p_sys->sar.i_num;
1267 tk->fmt.video.i_sar_den = p_sys->sar.i_den;
1270 else if( tk->fmt.i_cat == AUDIO_ES )
1273 /* find the audio number PLEASE find another way */
1274 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1276 i_audio = i_id&0x07;
1278 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1282 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1284 i_audio = i_id&0x1f;
1286 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1288 i_audio = i_id&0x1f;
1292 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1293 if( i_lang != 0xffff )
1295 tk->fmt.psz_language = malloc( 3 );
1296 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1297 tk->fmt.psz_language[1] = (i_lang )&0xff;
1298 tk->fmt.psz_language[2] = 0;
1300 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1306 else if( tk->fmt.i_cat == SPU_ES )
1308 int32_t i_title, i_part;
1309 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1310 if( i_lang != 0xffff )
1312 tk->fmt.psz_language = malloc( 3 );
1313 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1314 tk->fmt.psz_language[1] = (i_lang )&0xff;
1315 tk->fmt.psz_language[2] = 0;
1319 tk->fmt.subs.spu.palette[0] = 0xBeef;
1320 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1321 16 * sizeof( uint32_t ) );
1323 /* We select only when we are not in the menu */
1324 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1326 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1332 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1335 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1339 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1342 /*****************************************************************************
1344 *****************************************************************************/
1345 static void StillTimer( void *p_data )
1347 demux_sys_t *p_sys = p_data;
1349 vlc_mutex_lock( &p_sys->still.lock );
1350 if( likely(p_sys->still.b_enabled) )
1352 p_sys->still.b_enabled = false;
1353 dvdnav_still_skip( p_sys->dvdnav );
1355 vlc_mutex_unlock( &p_sys->still.lock );
1358 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1359 vlc_value_t oldval, vlc_value_t val, void *p_data )
1361 demux_t *p_demux = p_data;
1362 demux_sys_t *p_sys = p_demux->p_sys;
1364 /* FIXME? PCI usage thread safe? */
1365 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1366 int x = val.coords.x;
1367 int y = val.coords.y;
1369 if( psz_var[6] == 'm' ) /* mouse-moved */
1370 dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1373 assert( psz_var[6] == 'c' ); /* mouse-clicked */
1375 ButtonUpdate( p_demux, true );
1376 dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1383 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1384 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1386 demux_t *p_demux = p_data;
1387 demux_sys_t *p_sys = p_demux->p_sys;
1389 /* FIXME: thread-safe ? */
1390 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1392 switch( newval.i_int )
1394 case ACTIONID_NAV_LEFT:
1395 dvdnav_left_button_select( p_sys->dvdnav, pci );
1397 case ACTIONID_NAV_RIGHT:
1398 dvdnav_right_button_select( p_sys->dvdnav, pci );
1400 case ACTIONID_NAV_UP:
1401 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1403 case ACTIONID_NAV_DOWN:
1404 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1406 case ACTIONID_NAV_ACTIVATE:
1407 ButtonUpdate( p_demux, true );
1408 dvdnav_button_activate( p_sys->dvdnav, pci );
1412 (void)p_this; (void)psz_var; (void)oldval;
1416 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1417 vlc_value_t oldval, vlc_value_t val, void *p_data )
1419 demux_t *p_demux = p_data;
1420 demux_sys_t *p_sys = p_demux->p_sys;
1422 if (val.i_int == INPUT_EVENT_VOUT)
1424 if( p_sys->p_vout != NULL )
1426 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
1427 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
1428 vlc_object_release( p_sys->p_vout );
1431 p_sys->p_vout = input_GetVout( (input_thread_t *)p_input );
1432 if( p_sys->p_vout != NULL )
1434 var_AddCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
1435 var_AddCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
1438 (void) psz_var; (void) oldval;
1442 /*****************************************************************************
1443 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1444 *****************************************************************************/
1445 static int ProbeDVD( const char *psz_name )
1448 /* Triggers libdvdcss autodetection */
1451 int fd = vlc_open( psz_name, O_RDONLY | O_NONBLOCK );
1453 #ifdef HAVE_FDOPENDIR
1454 return VLC_EGENERIC;
1456 return (errno == ENOENT) ? VLC_EGENERIC : VLC_SUCCESS;
1459 int ret = VLC_EGENERIC;
1461 #ifdef HAVE_SYS_STAT_H
1462 struct stat stat_info;
1464 if( fstat( fd, &stat_info ) == -1 )
1467 if( !S_ISREG( stat_info.st_mode ) )
1469 if( S_ISDIR( stat_info.st_mode ) || S_ISBLK( stat_info.st_mode ) )
1470 ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1474 /* Match extension as the anchor exhibits too many false positives */
1475 const char *ext = strrchr( psz_name, '.' );
1479 if( strcasecmp( ext, "iso" ) && strcasecmp( ext, "img" ) )
1482 /* Try to find the anchor (2 bytes at LBA 256) */
1485 if( lseek( fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1486 && read( fd, &anchor, 2 ) == 2
1487 && GetWLE( &anchor ) == 2 )
1488 ret = VLC_SUCCESS; /* Found a potential anchor */