1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * NOTA BENE: this module requires the linking against a library which is
26 * known to require licensing under the GNU General Public License version 2
27 * (or later). Therefore, the result of compiling this module will normally
28 * be subject to the terms of that later license.
29 *****************************************************************************/
32 /*****************************************************************************
34 *****************************************************************************/
42 #include <sys/types.h>
47 #include <vlc_common.h>
48 #include <vlc_plugin.h>
49 #include <vlc_input.h>
50 #include <vlc_access.h>
51 #include <vlc_demux.h>
52 #include <vlc_charset.h>
56 #include <vlc_dialog.h>
57 #include <vlc_iso_lang.h>
59 /* FIXME we should find a better way than including that */
60 #include "../../src/text/iso-639_def.h"
63 #include <dvdnav/dvdnav.h>
65 #include "../demux/ps.h"
67 /*****************************************************************************
69 *****************************************************************************/
70 #define ANGLE_TEXT N_("DVD angle")
71 #define ANGLE_LONGTEXT N_( \
72 "Default DVD angle." )
74 #define MENU_TEXT N_("Start directly in menu")
75 #define MENU_LONGTEXT N_( \
76 "Start the DVD directly in the main menu. This "\
77 "will try to skip all the useless warning introductions." )
79 #define LANGUAGE_DEFAULT ("en")
81 static int Open ( vlc_object_t * );
82 static void Close( vlc_object_t * );
85 set_shortname( N_("DVD with menus") )
86 set_description( N_("DVDnav Input") )
87 set_category( CAT_INPUT )
88 set_subcategory( SUBCAT_INPUT_ACCESS )
89 add_integer( "dvdnav-angle", 1, ANGLE_TEXT,
90 ANGLE_LONGTEXT, false )
91 add_bool( "dvdnav-menu", true,
92 MENU_TEXT, MENU_LONGTEXT, false )
93 set_capability( "access_demux", 5 )
94 add_shortcut( "dvd", "dvdnav", "file" )
95 set_callbacks( Open, Close )
98 /* Shall we use libdvdnav's read ahead cache? */
100 #define DVD_READ_CACHE 0
102 #define DVD_READ_CACHE 1
105 /*****************************************************************************
107 *****************************************************************************/
124 ps_track_t tk[PS_TK_COUNT];
127 /* for spu variables */
128 input_thread_t *p_input;
131 vout_thread_t *p_vout;
133 /* palette for menus */
135 uint8_t palette[4][4];
146 input_title_t **title;
148 /* lenght of program group chain */
149 mtime_t i_pgc_length;
154 static int Control( demux_t *, int, va_list );
155 static int Demux( demux_t * );
156 static int DemuxBlock( demux_t *, const uint8_t *, int );
157 static void DemuxForceStill( demux_t * );
159 static void DemuxTitles( demux_t * );
160 static void ESSubtitleUpdate( demux_t * );
161 static void ButtonUpdate( demux_t *, bool );
163 static void ESNew( demux_t *, int );
164 static int ProbeDVD( const char * );
166 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
168 static int ControlInternal( demux_t *, int, ... );
170 static void StillTimer( void * );
172 static int EventMouse( vlc_object_t *, char const *,
173 vlc_value_t, vlc_value_t, void * );
174 static int EventIntf( vlc_object_t *, char const *,
175 vlc_value_t, vlc_value_t, void * );
177 /*****************************************************************************
179 *****************************************************************************/
180 static int Open( vlc_object_t *p_this )
182 demux_t *p_demux = (demux_t*)p_this;
190 if( p_demux->psz_access != NULL
191 && !strncmp(p_demux->psz_access, "dvd", 3) )
194 if( !p_demux->psz_file || !*p_demux->psz_file )
196 /* Only when selected */
200 psz_file = var_InheritString( p_this, "dvd" );
203 psz_file = strdup( p_demux->psz_file );
205 #if defined( _WIN32 ) || defined( __OS2__ )
206 if( psz_file != NULL )
208 /* Remove trailing backslash, otherwise dvdnav_open will fail */
209 size_t flen = strlen( psz_file );
210 if( flen > 0 && psz_file[flen - 1] == '\\' )
211 psz_file[flen - 1] = '\0';
214 psz_file = strdup("");
216 if( unlikely(psz_file == NULL) )
219 /* Try some simple probing to avoid going through dvdnav_open too often */
220 if( !forced && ProbeDVD( psz_file ) != VLC_SUCCESS )
227 const char *psz_path = ToLocale( psz_file );
228 if( dvdnav_open( &p_dvdnav, psz_path ) != DVDNAV_STATUS_OK )
230 LocaleFree( psz_path );
231 if( p_dvdnav == NULL )
233 msg_Warn( p_demux, "cannot open DVD (%s)", psz_file);
239 /* Fill p_demux field */
240 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
241 p_sys->dvdnav = p_dvdnav;
242 p_sys->b_reset_pcr = false;
244 ps_track_init( p_sys->tk );
245 p_sys->sar.i_num = 0;
246 p_sys->sar.i_den = 0;
247 p_sys->i_mux_rate = 0;
248 p_sys->i_pgc_length = 0;
249 p_sys->b_spu_change = false;
250 p_sys->i_vobu_index = 0;
251 p_sys->i_vobu_flush = 0;
255 // Hack for libdvdnav CVS.
256 // Without it dvdnav_get_number_of_titles() fails.
257 // Remove when fixed in libdvdnav CVS.
258 uint8_t buffer[DVD_VIDEO_LB_LEN];
261 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
262 == DVDNAV_STATUS_ERR )
264 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
267 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
270 /* Configure dvdnav */
271 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
274 msg_Warn( p_demux, "cannot set read-a-head flag" );
277 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
280 msg_Warn( p_demux, "cannot set PGC positioning flag" );
283 /* Set menu language */
284 psz_code = DemuxGetLanguageCode( p_demux, "menu-language" );
285 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
288 msg_Warn( p_demux, "can't set menu 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_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
296 /* Set audio language */
297 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
298 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
301 msg_Warn( p_demux, "can't set audio 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_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
309 /* Set spu language */
310 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
311 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
314 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
315 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
316 /* We try to fall back to 'en' */
317 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
318 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
322 DemuxTitles( p_demux );
324 if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
326 msg_Dbg( p_demux, "trying to go to dvd menu" );
328 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
330 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
331 dialog_Fatal( p_demux, _("Playback failure"), "%s",
332 _("VLC cannot set the DVD's title. It possibly "
333 "cannot decrypt the entire disc.") );
334 dvdnav_close( p_sys->dvdnav );
339 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
342 /* Try going to menu root */
343 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
345 msg_Warn( p_demux, "cannot go to dvd menu" );
349 i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
350 if( i_angle <= 0 ) i_angle = 1;
352 /* FIXME hack hack hack hack FIXME */
353 /* Get p_input and create variable */
354 p_sys->p_input = demux_GetParentInput( p_demux );
355 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
356 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
357 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
358 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
359 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
360 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
361 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
363 /* catch vout creation event */
364 var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
366 p_sys->still.b_enabled = false;
367 vlc_mutex_init( &p_sys->still.lock );
368 if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
369 p_sys->still.b_created = true;
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;
382 /* Stop vout event handler */
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 still image handler */
392 if( p_sys->still.b_created )
393 vlc_timer_destroy( p_sys->still.timer );
394 vlc_mutex_destroy( &p_sys->still.lock );
396 var_Destroy( p_sys->p_input, "highlight" );
397 var_Destroy( p_sys->p_input, "x-start" );
398 var_Destroy( p_sys->p_input, "x-end" );
399 var_Destroy( p_sys->p_input, "y-start" );
400 var_Destroy( p_sys->p_input, "y-end" );
401 var_Destroy( p_sys->p_input, "color" );
402 var_Destroy( p_sys->p_input, "menu-palette" );
404 vlc_object_release( p_sys->p_input );
406 for( int i = 0; i < PS_TK_COUNT; i++ )
408 ps_track_t *tk = &p_sys->tk[i];
411 es_format_Clean( &tk->fmt );
412 if( tk->es ) es_out_Del( p_demux->out, tk->es );
416 /* Free the array of titles */
417 for( int i = 0; i < p_sys->i_title; i++ )
418 vlc_input_title_Delete( p_sys->title[i] );
419 TAB_CLEAN( p_sys->i_title, p_sys->title );
421 dvdnav_close( p_sys->dvdnav );
425 /*****************************************************************************
427 *****************************************************************************/
428 static int Control( demux_t *p_demux, int i_query, va_list args )
430 demux_sys_t *p_sys = p_demux->p_sys;
431 input_title_t ***ppp_title;
436 case DEMUX_SET_POSITION:
437 case DEMUX_GET_POSITION:
439 case DEMUX_GET_LENGTH:
442 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
443 DVDNAV_STATUS_OK || len == 0 )
450 case DEMUX_GET_POSITION:
451 *va_arg( args, double* ) = (double)pos / (double)len;
454 case DEMUX_SET_POSITION:
455 pos = va_arg( args, double ) * len;
456 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
464 if( p_sys->i_pgc_length > 0 )
466 *va_arg( args, int64_t * ) = p_sys->i_pgc_length*pos/len;
471 case DEMUX_GET_LENGTH:
472 if( p_sys->i_pgc_length > 0 )
474 *va_arg( args, int64_t * ) = (int64_t)p_sys->i_pgc_length;
482 /* Special for access_demux */
483 case DEMUX_CAN_PAUSE:
485 case DEMUX_CAN_CONTROL_PACE:
487 *va_arg( args, bool * ) = true;
490 case DEMUX_SET_PAUSE_STATE:
493 case DEMUX_GET_TITLE_INFO:
494 ppp_title = va_arg( args, input_title_t*** );
495 *va_arg( args, int* ) = p_sys->i_title;
496 *va_arg( args, int* ) = 0; /* Title offset */
497 *va_arg( args, int* ) = 1; /* Chapter offset */
499 /* Duplicate title infos */
500 *ppp_title = malloc( p_sys->i_title * sizeof( input_title_t * ) );
501 for( i = 0; i < p_sys->i_title; i++ )
503 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
507 case DEMUX_SET_TITLE:
508 i = (int)va_arg( args, int );
509 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
510 != DVDNAV_STATUS_OK ) ||
511 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
512 != DVDNAV_STATUS_OK ) )
514 msg_Warn( p_demux, "cannot set title/chapter" );
517 p_demux->info.i_update |=
518 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
519 p_demux->info.i_title = i;
520 p_demux->info.i_seekpoint = 0;
523 case DEMUX_SET_SEEKPOINT:
524 i = va_arg( args, int );
525 if( p_demux->info.i_title == 0 )
527 static const int argtab[] = {
536 enum { numargs = sizeof(argtab)/sizeof(int) };
537 if( (unsigned)i >= numargs || DVDNAV_STATUS_OK !=
538 dvdnav_menu_call(p_sys->dvdnav,argtab[i]) )
541 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
542 i + 1 ) != DVDNAV_STATUS_OK )
544 msg_Warn( p_demux, "cannot set title/chapter" );
547 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
548 p_demux->info.i_seekpoint = i;
551 case DEMUX_GET_PTS_DELAY:
552 *va_arg( args, int64_t * ) =
553 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
558 const char *title_name = NULL;
560 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
561 if( (NULL != title_name) && ('\0' != title_name[0]) )
563 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
564 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
570 case DEMUX_NAV_ACTIVATE:
572 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
574 ButtonUpdate( p_demux, true );
575 dvdnav_button_activate( p_sys->dvdnav, pci );
581 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
583 dvdnav_upper_button_select( p_sys->dvdnav, pci );
589 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
591 dvdnav_lower_button_select( p_sys->dvdnav, pci );
597 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
599 dvdnav_left_button_select( p_sys->dvdnav, pci );
603 case DEMUX_NAV_RIGHT:
605 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
607 dvdnav_right_button_select( p_sys->dvdnav, pci );
611 /* TODO implement others */
619 static int ControlInternal( demux_t *p_demux, int i_query, ... )
624 va_start( args, i_query );
625 i_result = Control( p_demux, i_query, args );
630 /*****************************************************************************
632 *****************************************************************************/
633 static int Demux( demux_t *p_demux )
635 demux_sys_t *p_sys = p_demux->p_sys;
637 uint8_t buffer[DVD_VIDEO_LB_LEN];
638 uint8_t *packet = buffer;
643 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
644 == DVDNAV_STATUS_ERR )
646 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
647 == DVDNAV_STATUS_ERR )
650 msg_Warn( p_demux, "cannot get next block (%s)",
651 dvdnav_err_to_string( p_sys->dvdnav ) );
652 if( p_demux->info.i_title == 0 )
654 msg_Dbg( p_demux, "jumping to first title" );
655 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
662 case DVDNAV_BLOCK_OK: /* mpeg block */
663 vlc_mutex_lock( &p_sys->still.lock );
664 vlc_timer_schedule( p_sys->still.timer, false, 0, 0 );
665 p_sys->still.b_enabled = false;
666 vlc_mutex_unlock( &p_sys->still.lock );
667 if( p_sys->b_reset_pcr )
669 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
670 p_sys->b_reset_pcr = false;
672 DemuxBlock( p_demux, packet, i_len );
673 if( p_sys->i_vobu_index > 0 )
675 if( p_sys->i_vobu_flush == p_sys->i_vobu_index )
676 DemuxForceStill( p_demux );
677 p_sys->i_vobu_index++;
681 case DVDNAV_NOP: /* Nothing */
682 msg_Dbg( p_demux, "DVDNAV_NOP" );
685 case DVDNAV_STILL_FRAME:
687 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
688 bool b_still_init = false;
690 vlc_mutex_lock( &p_sys->still.lock );
691 if( !p_sys->still.b_enabled )
693 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
694 msg_Dbg( p_demux, " - length=0x%x", event->length );
695 p_sys->still.b_enabled = true;
697 if( event->length != 0xff && p_sys->still.b_created )
699 mtime_t delay = event->length * CLOCK_FREQ;
700 vlc_timer_schedule( p_sys->still.timer, false, delay, 0 );
705 vlc_mutex_unlock( &p_sys->still.lock );
709 DemuxForceStill( p_demux );
710 p_sys->b_reset_pcr = true;
716 case DVDNAV_SPU_CLUT_CHANGE:
720 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
721 /* Update color lookup table (16 *uint32_t in packet) */
722 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
724 /* HACK to get the SPU tracks registered in the right order */
725 for( i = 0; i < 0x1f; i++ )
727 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
728 ESNew( p_demux, 0xbd20 + i );
734 case DVDNAV_SPU_STREAM_CHANGE:
736 dvdnav_spu_stream_change_event_t *event =
737 (dvdnav_spu_stream_change_event_t*)packet;
740 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
741 msg_Dbg( p_demux, " - physical_wide=%d",
742 event->physical_wide );
743 msg_Dbg( p_demux, " - physical_letterbox=%d",
744 event->physical_letterbox);
745 msg_Dbg( p_demux, " - physical_pan_scan=%d",
746 event->physical_pan_scan );
748 ESSubtitleUpdate( p_demux );
749 p_sys->b_spu_change = true;
751 /* HACK to get the SPU tracks registered in the right order */
752 for( i = 0; i < 0x1f; i++ )
754 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
755 ESNew( p_demux, 0xbd20 + i );
761 case DVDNAV_AUDIO_STREAM_CHANGE:
763 dvdnav_audio_stream_change_event_t *event =
764 (dvdnav_audio_stream_change_event_t*)packet;
765 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
766 msg_Dbg( p_demux, " - physical=%d", event->physical );
771 case DVDNAV_VTS_CHANGE:
777 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
778 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
779 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
780 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
783 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
785 for( i = 0; i < PS_TK_COUNT; i++ )
787 ps_track_t *tk = &p_sys->tk[i];
790 es_format_Clean( &tk->fmt );
791 if( tk->es ) es_out_Del( p_demux->out, tk->es );
796 #if defined(HAVE_DVDNAV_GET_VIDEO_RESOLUTION)
797 uint32_t i_width, i_height;
798 if( dvdnav_get_video_resolution( p_sys->dvdnav,
799 &i_width, &i_height ) )
800 i_width = i_height = 0;
801 switch( dvdnav_get_video_aspect( p_sys->dvdnav ) )
804 p_sys->sar.i_num = 4 * i_height;
805 p_sys->sar.i_den = 3 * i_width;
808 p_sys->sar.i_num = 16 * i_height;
809 p_sys->sar.i_den = 9 * i_width;
812 p_sys->sar.i_num = 0;
813 p_sys->sar.i_den = 0;
818 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
819 &i_part ) == DVDNAV_STATUS_OK )
821 if( i_title >= 0 && i_title < p_sys->i_title &&
822 p_demux->info.i_title != i_title )
824 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
825 p_demux->info.i_title = i_title;
831 case DVDNAV_CELL_CHANGE:
836 dvdnav_cell_change_event_t *event =
837 (dvdnav_cell_change_event_t*)packet;
838 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
839 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
840 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
841 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
842 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
843 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
844 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
845 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
847 /* Store the lenght in time of the current PGC */
848 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
849 p_sys->i_vobu_index = 0;
850 p_sys->i_vobu_flush = 0;
852 /* FIXME is it correct or there is better way to know chapter change */
853 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
854 &i_part ) == DVDNAV_STATUS_OK )
856 if( i_title >= 0 && i_title < p_sys->i_title )
858 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
859 p_demux->info.i_title = i_title;
861 if( i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
862 p_demux->info.i_seekpoint != i_part - 1 )
864 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
865 p_demux->info.i_seekpoint = i_part - 1;
872 case DVDNAV_NAV_PACKET:
874 p_sys->i_vobu_index = 1;
875 p_sys->i_vobu_flush = 0;
877 /* Look if we have need to force a flush (and when) */
878 const pci_gi_t *p_pci_gi = &dvdnav_get_current_nav_pci( p_sys->dvdnav )->pci_gi;
879 if( p_pci_gi->vobu_se_e_ptm != 0 && p_pci_gi->vobu_se_e_ptm < p_pci_gi->vobu_e_ptm )
881 const dsi_gi_t *p_dsi_gi = &dvdnav_get_current_nav_dsi( p_sys->dvdnav )->dsi_gi;
882 if( p_dsi_gi->vobu_3rdref_ea != 0 )
883 p_sys->i_vobu_flush = p_dsi_gi->vobu_3rdref_ea;
884 else if( p_dsi_gi->vobu_2ndref_ea != 0 )
885 p_sys->i_vobu_flush = p_dsi_gi->vobu_2ndref_ea;
886 else if( p_dsi_gi->vobu_1stref_ea != 0 )
887 p_sys->i_vobu_flush = p_dsi_gi->vobu_1stref_ea;
891 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
893 /* A lot of thing to do here :
895 * - fetch pts (for time display)
898 DemuxBlock( p_demux, packet, i_len );
899 if( p_sys->b_spu_change )
901 ButtonUpdate( p_demux, false );
902 p_sys->b_spu_change = false;
907 case DVDNAV_STOP: /* EOF */
908 msg_Dbg( p_demux, "DVDNAV_STOP" );
911 dvdnav_free_cache_block( p_sys->dvdnav, packet );
915 case DVDNAV_HIGHLIGHT:
917 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
918 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
919 msg_Dbg( p_demux, " - display=%d", event->display );
920 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
921 ButtonUpdate( p_demux, false );
925 case DVDNAV_HOP_CHANNEL:
926 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
927 p_sys->i_vobu_index = 0;
928 p_sys->i_vobu_flush = 0;
929 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
933 msg_Dbg( p_demux, "DVDNAV_WAIT" );
936 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
943 dvdnav_wait_skip( p_sys->dvdnav );
944 p_sys->b_reset_pcr = true;
949 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
954 dvdnav_free_cache_block( p_sys->dvdnav, packet );
961 * FIXME: partiallyy duplicated from src/input/es_out.c
963 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
965 const iso639_lang_t *pl;
969 psz_lang = var_CreateGetString( p_demux, psz_var );
971 return strdup(LANGUAGE_DEFAULT);
973 /* XXX: we will use only the first value
974 * (and ignore other ones in case of a list) */
975 if( ( p = strchr( psz_lang, ',' ) ) )
978 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
980 if( *psz_lang == '\0' )
982 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
983 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
984 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
985 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
991 if( pl->psz_eng_name != NULL )
992 return strdup( pl->psz_iso639_1 );
994 return strdup(LANGUAGE_DEFAULT);
997 static void DemuxTitles( demux_t *p_demux )
999 demux_sys_t *p_sys = p_demux->p_sys;
1006 t = vlc_input_title_New();
1008 t->psz_name = strdup( "DVD Menu" );
1010 s = vlc_seekpoint_New();
1011 s->psz_name = strdup( "Resume" );
1012 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1014 s = vlc_seekpoint_New();
1015 s->psz_name = strdup( "Root" );
1016 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1018 s = vlc_seekpoint_New();
1019 s->psz_name = strdup( "Title" );
1020 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1022 s = vlc_seekpoint_New();
1023 s->psz_name = strdup( "Chapter" );
1024 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1026 s = vlc_seekpoint_New();
1027 s->psz_name = strdup( "Subtitle" );
1028 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1030 s = vlc_seekpoint_New();
1031 s->psz_name = strdup( "Audio" );
1032 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1034 s = vlc_seekpoint_New();
1035 s->psz_name = strdup( "Angle" );
1036 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1038 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1040 /* Find out number of titles/chapters */
1041 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
1042 for( i = 1; i <= i_titles; i++ )
1045 uint64_t i_title_length;
1046 uint64_t *p_chapters_time;
1048 #if defined(HAVE_DVDNAV_DESCRIBE_TITLE_CHAPTERS)
1049 i_chapters = dvdnav_describe_title_chapters( p_sys->dvdnav, i,
1052 if( i_chapters < 1 )
1055 p_chapters_time = NULL;
1058 if( dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters ) != DVDNAV_STATUS_OK )
1061 p_chapters_time = NULL;
1063 t = vlc_input_title_New();
1064 t->i_length = i_title_length * 1000 / 90;
1065 for( int j = 0; j < __MAX( i_chapters, 1 ); j++ )
1067 s = vlc_seekpoint_New();
1068 if( p_chapters_time )
1069 s->i_time_offset = p_chapters_time[j] * 1000 / 90;
1070 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1072 free( p_chapters_time );
1073 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1077 /*****************************************************************************
1079 *****************************************************************************/
1080 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1082 demux_sys_t *p_sys = p_demux->p_sys;
1083 int32_t i_title, i_part;
1085 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1087 dvdnav_highlight_area_t hl;
1091 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1092 != DVDNAV_STATUS_OK )
1094 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1098 b_button_ok = false;
1099 if( i_button > 0 && i_title == 0 )
1101 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1103 b_button_ok = DVDNAV_STATUS_OK ==
1104 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
1109 for( unsigned i = 0; i < 4; i++ )
1111 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1112 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1114 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1115 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1116 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1117 p_sys->palette[i][3] = i_alpha;
1120 vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1121 var_SetInteger( p_sys->p_input, "x-start", hl.sx );
1122 var_SetInteger( p_sys->p_input, "x-end", hl.ex );
1123 var_SetInteger( p_sys->p_input, "y-start", hl.sy );
1124 var_SetInteger( p_sys->p_input, "y-end", hl.ey );
1126 var_SetAddress( p_sys->p_input, "menu-palette", p_sys->palette );
1127 var_SetBool( p_sys->p_input, "highlight", true );
1129 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1133 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1134 i_button, i_title );
1137 vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1138 var_SetBool( p_sys->p_input, "highlight", false );
1140 vlc_global_unlock( VLC_HIGHLIGHT_MUTEX );
1143 static void ESSubtitleUpdate( demux_t *p_demux )
1145 demux_sys_t *p_sys = p_demux->p_sys;
1146 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1147 int32_t i_title, i_part;
1149 ButtonUpdate( p_demux, false );
1151 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1152 if( i_title > 0 ) return;
1154 if( i_spu >= 0 && i_spu <= 0x1f )
1156 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1158 ESNew( p_demux, 0xbd20 + i_spu );
1160 /* be sure to unselect it (reset) */
1161 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1165 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1169 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1171 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1174 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1181 /*****************************************************************************
1182 * DemuxBlock: demux a given block
1183 *****************************************************************************/
1184 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
1186 demux_sys_t *p_sys = p_demux->p_sys;
1190 int i_size = ps_pkt_size( p, len );
1191 if( i_size <= 0 || i_size > len )
1196 /* Create a block */
1197 block_t *p_pkt = block_Alloc( i_size );
1198 memcpy( p_pkt->p_buffer, p, i_size);
1200 /* Parse it and send it */
1201 switch( 0x100 | p[3] )
1209 msg_Warn( p_demux, "received a PSM packet" );
1211 else if( p[3] == 0xbb )
1213 msg_Warn( p_demux, "received a SYSTEM packet" );
1216 block_Release( p_pkt );
1223 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1225 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1226 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1228 block_Release( p_pkt );
1233 int i_id = ps_pkt_id( p_pkt );
1236 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1240 ESNew( p_demux, i_id );
1242 if( tk->b_seen && tk->es &&
1243 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1245 es_out_Send( p_demux->out, tk->es, p_pkt );
1249 block_Release( p_pkt );
1254 block_Release( p_pkt );
1267 /*****************************************************************************
1268 * Force still images to be displayed by sending EOS and stopping buffering.
1269 *****************************************************************************/
1270 static void DemuxForceStill( demux_t *p_demux )
1272 static const uint8_t buffer[] = {
1273 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
1275 0x00, 0x00, 0x01, 0xB7,
1277 DemuxBlock( p_demux, buffer, sizeof(buffer) );
1280 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
1283 /*****************************************************************************
1284 * ESNew: register a new elementary stream
1285 *****************************************************************************/
1286 static void ESNew( demux_t *p_demux, int i_id )
1288 demux_sys_t *p_sys = p_demux->p_sys;
1289 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1290 bool b_select = false;
1292 if( tk->b_seen ) return;
1294 if( ps_track_fill( tk, 0, i_id ) )
1296 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1301 if( tk->fmt.i_cat == VIDEO_ES )
1303 tk->fmt.video.i_sar_num = p_sys->sar.i_num;
1304 tk->fmt.video.i_sar_den = p_sys->sar.i_den;
1307 else if( tk->fmt.i_cat == AUDIO_ES )
1310 /* find the audio number PLEASE find another way */
1311 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1313 i_audio = i_id&0x07;
1315 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1319 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1321 i_audio = i_id&0x1f;
1323 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1325 i_audio = i_id&0x1f;
1329 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1330 if( i_lang != 0xffff )
1332 tk->fmt.psz_language = malloc( 3 );
1333 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1334 tk->fmt.psz_language[1] = (i_lang )&0xff;
1335 tk->fmt.psz_language[2] = 0;
1337 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1343 else if( tk->fmt.i_cat == SPU_ES )
1345 int32_t i_title, i_part;
1346 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1347 if( i_lang != 0xffff )
1349 tk->fmt.psz_language = malloc( 3 );
1350 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1351 tk->fmt.psz_language[1] = (i_lang )&0xff;
1352 tk->fmt.psz_language[2] = 0;
1356 tk->fmt.subs.spu.palette[0] = 0xBeef;
1357 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1358 16 * sizeof( uint32_t ) );
1360 /* We select only when we are not in the menu */
1361 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1363 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1369 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1372 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1376 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1379 /*****************************************************************************
1381 *****************************************************************************/
1382 static void StillTimer( void *p_data )
1384 demux_sys_t *p_sys = p_data;
1386 vlc_mutex_lock( &p_sys->still.lock );
1387 if( likely(p_sys->still.b_enabled) )
1389 p_sys->still.b_enabled = false;
1390 dvdnav_still_skip( p_sys->dvdnav );
1392 vlc_mutex_unlock( &p_sys->still.lock );
1395 static int EventMouse( vlc_object_t *p_vout, 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 /* FIXME? PCI usage thread safe? */
1402 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1403 int x = val.coords.x;
1404 int y = val.coords.y;
1406 if( psz_var[6] == 'm' ) /* mouse-moved */
1407 dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1410 assert( psz_var[6] == 'c' ); /* mouse-clicked */
1412 ButtonUpdate( p_demux, true );
1413 dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1420 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1421 vlc_value_t oldval, vlc_value_t val, void *p_data )
1423 demux_t *p_demux = p_data;
1424 demux_sys_t *p_sys = p_demux->p_sys;
1426 if (val.i_int == INPUT_EVENT_VOUT)
1428 if( p_sys->p_vout != NULL )
1430 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
1431 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
1432 vlc_object_release( p_sys->p_vout );
1435 p_sys->p_vout = input_GetVout( (input_thread_t *)p_input );
1436 if( p_sys->p_vout != NULL )
1438 var_AddCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
1439 var_AddCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
1442 (void) psz_var; (void) oldval;
1446 /*****************************************************************************
1447 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1448 *****************************************************************************/
1449 static int ProbeDVD( const char *psz_name )
1452 /* Triggers libdvdcss autodetection */
1455 int fd = vlc_open( psz_name, O_RDONLY | O_NONBLOCK );
1457 #ifdef HAVE_FDOPENDIR
1458 return VLC_EGENERIC;
1460 return (errno == ENOENT) ? VLC_EGENERIC : VLC_SUCCESS;
1463 int ret = VLC_EGENERIC;
1464 struct stat stat_info;
1466 if( fstat( fd, &stat_info ) == -1 )
1468 if( !S_ISREG( stat_info.st_mode ) )
1470 if( S_ISDIR( stat_info.st_mode ) || S_ISBLK( stat_info.st_mode ) )
1471 ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1475 /* ISO 9660 volume descriptor */
1477 if( lseek( fd, 0x8000 + 1, SEEK_SET ) == -1
1478 || read( fd, iso_dsc, sizeof (iso_dsc) ) < sizeof (iso_dsc)
1479 || memcmp( iso_dsc, "CD001\x01", 6 ) )
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 */