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>
42 #include <vlc_dialog.h>
47 #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 )
99 add_shortcut( "dvd", "dvdnav", "file" )
100 set_callbacks( Open, Close )
103 /* Shall we use libdvdnav's read ahead cache? */
104 #define DVD_READ_CACHE 1
106 /*****************************************************************************
108 *****************************************************************************/
125 ps_track_t tk[PS_TK_COUNT];
128 /* for spu variables */
129 input_thread_t *p_input;
132 vlc_object_t *p_vout;
134 /* palette for menus */
136 uint8_t palette[4][4];
147 input_title_t **title;
149 /* lenght of program group chain */
150 mtime_t i_pgc_length;
155 static int Control( demux_t *, int, va_list );
156 static int Demux( demux_t * );
157 static int DemuxBlock( demux_t *, const uint8_t *, int );
158 static void DemuxForceStill( demux_t * );
160 static void DemuxTitles( demux_t * );
161 static void ESSubtitleUpdate( demux_t * );
162 static void ButtonUpdate( demux_t *, bool );
164 static void ESNew( demux_t *, int );
165 static int ProbeDVD( demux_t *, char * );
167 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
169 static int ControlInternal( demux_t *, int, ... );
171 static void StillTimer( void * );
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;
192 if( !p_demux->psz_path || !*p_demux->psz_path )
194 /* Only when selected */
195 if( !p_demux->psz_access || !*p_demux->psz_access )
198 psz_name = var_CreateGetString( p_this, "dvd" );
201 psz_name = strdup("");
205 psz_name = ToLocaleDup( p_demux->psz_path );
208 /* Remove trailing backslash, otherwise dvdnav_open will fail */
209 if( *psz_name && *(psz_name + strlen(psz_name) - 1) == '\\' )
211 *(psz_name + strlen(psz_name) - 1) = '\0';
214 decode_URI( psz_name );
216 /* Try some simple probing to avoid going through dvdnav_open too often */
217 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
224 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
226 msg_Warn( p_demux, "cannot open dvdnav" );
232 /* Fill p_demux field */
233 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
234 p_sys->dvdnav = p_dvdnav;
235 p_sys->b_reset_pcr = false;
237 ps_track_init( p_sys->tk );
238 p_sys->sar.i_num = 0;
239 p_sys->sar.i_den = 0;
240 p_sys->i_mux_rate = 0;
241 p_sys->i_pgc_length = 0;
242 p_sys->b_spu_change = false;
243 p_sys->i_vobu_index = 0;
244 p_sys->i_vobu_flush = 0;
248 // Hack for libdvdnav CVS.
249 // Without it dvdnav_get_number_of_titles() fails.
250 // Remove when fixed in libdvdnav CVS.
251 uint8_t buffer[DVD_VIDEO_LB_LEN];
254 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
255 == DVDNAV_STATUS_ERR )
257 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
260 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
263 /* Configure dvdnav */
264 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
267 msg_Warn( p_demux, "cannot set read-a-head flag" );
270 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
273 msg_Warn( p_demux, "cannot set PGC positioning flag" );
277 * XXX A menu-language may be better than sub-language */
278 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
279 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
282 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
283 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
284 /* We try to fall back to 'en' */
285 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
286 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
290 /* Set audio language */
291 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
292 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
295 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
296 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
297 /* We try to fall back to 'en' */
298 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
299 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
303 /* Set spu language */
304 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
305 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
308 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
309 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
310 /* We try to fall back to 'en' */
311 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
312 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
316 DemuxTitles( p_demux );
318 if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
320 msg_Dbg( p_demux, "trying to go to dvd menu" );
322 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
324 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
325 dialog_Fatal( p_demux, _("Playback failure"), "%s",
326 _("VLC cannot set the DVD's title. It possibly "
327 "cannot decrypt the entire disc.") );
328 dvdnav_close( p_sys->dvdnav );
333 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
336 /* Try going to menu root */
337 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
339 msg_Warn( p_demux, "cannot go to dvd menu" );
343 i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
344 if( i_angle <= 0 ) i_angle = 1;
346 /* Update default_pts to a suitable value for dvdnav access */
347 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
349 /* FIXME hack hack hack hack FIXME */
350 /* Get p_input and create variable */
351 p_sys->p_input = demux_GetParentInput( p_demux );
352 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
353 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
354 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
355 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
356 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
357 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
358 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
359 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
361 /* catch all key event */
362 var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
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;
383 /* Stop vout event handler */
384 var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
385 if( p_sys->p_vout != NULL )
386 { /* Should not happen, but better be safe than sorry. */
387 msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
388 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
389 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
392 /* Stop key event handler (FIXME: should really be per-vout too) */
393 var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
395 /* Stop still image handler */
396 if( p_sys->still.b_created )
397 vlc_timer_destroy( p_sys->still.timer );
398 vlc_mutex_destroy( &p_sys->still.lock );
400 var_Destroy( p_sys->p_input, "highlight-mutex" );
401 var_Destroy( p_sys->p_input, "highlight" );
402 var_Destroy( p_sys->p_input, "x-start" );
403 var_Destroy( p_sys->p_input, "x-end" );
404 var_Destroy( p_sys->p_input, "y-start" );
405 var_Destroy( p_sys->p_input, "y-end" );
406 var_Destroy( p_sys->p_input, "color" );
407 var_Destroy( p_sys->p_input, "menu-palette" );
409 vlc_object_release( p_sys->p_input );
411 for( i = 0; i < PS_TK_COUNT; i++ )
413 ps_track_t *tk = &p_sys->tk[i];
416 es_format_Clean( &tk->fmt );
417 if( tk->es ) es_out_Del( p_demux->out, tk->es );
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( sizeof( input_title_t ** ) * p_sys->i_title );
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_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
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 /* TODO implement others */
576 static int ControlInternal( demux_t *p_demux, int i_query, ... )
581 va_start( args, i_query );
582 i_result = Control( p_demux, i_query, args );
587 /*****************************************************************************
589 *****************************************************************************/
590 static int Demux( demux_t *p_demux )
592 demux_sys_t *p_sys = p_demux->p_sys;
594 uint8_t buffer[DVD_VIDEO_LB_LEN];
595 uint8_t *packet = buffer;
600 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
601 == DVDNAV_STATUS_ERR )
603 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
604 == DVDNAV_STATUS_ERR )
607 msg_Warn( p_demux, "cannot get next block (%s)",
608 dvdnav_err_to_string( p_sys->dvdnav ) );
609 if( p_demux->info.i_title == 0 )
611 msg_Dbg( p_demux, "jumping to first title" );
612 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
619 case DVDNAV_BLOCK_OK: /* mpeg block */
620 vlc_mutex_lock( &p_sys->still.lock );
621 vlc_timer_schedule( p_sys->still.timer, false, 0, 0 );
622 p_sys->still.b_enabled = false;
623 vlc_mutex_unlock( &p_sys->still.lock );
624 if( p_sys->b_reset_pcr )
626 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
627 p_sys->b_reset_pcr = false;
629 DemuxBlock( p_demux, packet, i_len );
630 if( p_sys->i_vobu_index > 0 )
632 if( p_sys->i_vobu_flush == p_sys->i_vobu_index )
633 DemuxForceStill( p_demux );
634 p_sys->i_vobu_index++;
638 case DVDNAV_NOP: /* Nothing */
639 msg_Dbg( p_demux, "DVDNAV_NOP" );
642 case DVDNAV_STILL_FRAME:
644 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
645 bool b_still_init = false;
647 vlc_mutex_lock( &p_sys->still.lock );
648 if( !p_sys->still.b_enabled )
650 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
651 msg_Dbg( p_demux, " - length=0x%x", event->length );
652 p_sys->still.b_enabled = true;
654 if( event->length != 0xff && p_sys->still.b_created )
656 mtime_t delay = event->length * CLOCK_FREQ;
657 vlc_timer_schedule( p_sys->still.timer, false, delay, 0 );
662 vlc_mutex_unlock( &p_sys->still.lock );
666 DemuxForceStill( p_demux );
667 p_sys->b_reset_pcr = true;
673 case DVDNAV_SPU_CLUT_CHANGE:
677 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
678 /* Update color lookup table (16 *uint32_t in packet) */
679 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
681 /* HACK to get the SPU tracks registered in the right order */
682 for( i = 0; i < 0x1f; i++ )
684 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
685 ESNew( p_demux, 0xbd20 + i );
691 case DVDNAV_SPU_STREAM_CHANGE:
693 dvdnav_spu_stream_change_event_t *event =
694 (dvdnav_spu_stream_change_event_t*)packet;
697 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
698 msg_Dbg( p_demux, " - physical_wide=%d",
699 event->physical_wide );
700 msg_Dbg( p_demux, " - physical_letterbox=%d",
701 event->physical_letterbox);
702 msg_Dbg( p_demux, " - physical_pan_scan=%d",
703 event->physical_pan_scan );
705 ESSubtitleUpdate( p_demux );
706 p_sys->b_spu_change = true;
708 /* HACK to get the SPU tracks registered in the right order */
709 for( i = 0; i < 0x1f; i++ )
711 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
712 ESNew( p_demux, 0xbd20 + i );
718 case DVDNAV_AUDIO_STREAM_CHANGE:
720 dvdnav_audio_stream_change_event_t *event =
721 (dvdnav_audio_stream_change_event_t*)packet;
722 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
723 msg_Dbg( p_demux, " - physical=%d", event->physical );
728 case DVDNAV_VTS_CHANGE:
734 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
735 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
736 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
737 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
740 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
742 for( i = 0; i < PS_TK_COUNT; i++ )
744 ps_track_t *tk = &p_sys->tk[i];
747 es_format_Clean( &tk->fmt );
748 if( tk->es ) es_out_Del( p_demux->out, tk->es );
753 #if defined(HAVE_DVDNAV_GET_VIDEO_RESOLUTION)
754 uint32_t i_width, i_height;
755 if( dvdnav_get_video_resolution( p_sys->dvdnav,
756 &i_width, &i_height ) )
757 i_width = i_height = 0;
758 switch( dvdnav_get_video_aspect( p_sys->dvdnav ) )
761 p_sys->sar.i_num = 4 * i_height;
762 p_sys->sar.i_den = 3 * i_width;
765 p_sys->sar.i_num = 16 * i_height;
766 p_sys->sar.i_den = 9 * i_width;
769 p_sys->sar.i_num = 0;
770 p_sys->sar.i_den = 0;
775 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
776 &i_part ) == DVDNAV_STATUS_OK )
778 if( i_title >= 0 && i_title < p_sys->i_title &&
779 p_demux->info.i_title != i_title )
781 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
782 p_demux->info.i_title = i_title;
788 case DVDNAV_CELL_CHANGE:
793 dvdnav_cell_change_event_t *event =
794 (dvdnav_cell_change_event_t*)packet;
795 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
796 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
797 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
798 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
799 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
800 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
801 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
802 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
804 /* Store the lenght in time of the current PGC */
805 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
806 p_sys->i_vobu_index = 0;
807 p_sys->i_vobu_flush = 0;
809 /* FIXME is it correct or there is better way to know chapter change */
810 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
811 &i_part ) == DVDNAV_STATUS_OK )
813 if( i_title >= 0 && i_title < p_sys->i_title &&
814 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
815 p_demux->info.i_seekpoint != i_part - 1 )
817 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
818 p_demux->info.i_seekpoint = i_part - 1;
824 case DVDNAV_NAV_PACKET:
826 p_sys->i_vobu_index = 1;
827 p_sys->i_vobu_flush = 0;
829 /* Look if we have need to force a flush (and when) */
830 const pci_gi_t *p_pci_gi = &dvdnav_get_current_nav_pci( p_sys->dvdnav )->pci_gi;
831 if( p_pci_gi->vobu_se_e_ptm != 0 && p_pci_gi->vobu_se_e_ptm < p_pci_gi->vobu_e_ptm )
833 const dsi_gi_t *p_dsi_gi = &dvdnav_get_current_nav_dsi( p_sys->dvdnav )->dsi_gi;
834 if( p_dsi_gi->vobu_3rdref_ea != 0 )
835 p_sys->i_vobu_flush = p_dsi_gi->vobu_3rdref_ea;
836 else if( p_dsi_gi->vobu_2ndref_ea != 0 )
837 p_sys->i_vobu_flush = p_dsi_gi->vobu_2ndref_ea;
838 else if( p_dsi_gi->vobu_1stref_ea != 0 )
839 p_sys->i_vobu_flush = p_dsi_gi->vobu_1stref_ea;
843 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
845 /* A lot of thing to do here :
847 * - fetch pts (for time display)
850 DemuxBlock( p_demux, packet, i_len );
851 if( p_sys->b_spu_change )
853 ButtonUpdate( p_demux, false );
854 p_sys->b_spu_change = false;
859 case DVDNAV_STOP: /* EOF */
860 msg_Dbg( p_demux, "DVDNAV_STOP" );
863 dvdnav_free_cache_block( p_sys->dvdnav, packet );
867 case DVDNAV_HIGHLIGHT:
869 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
870 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
871 msg_Dbg( p_demux, " - display=%d", event->display );
872 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
873 ButtonUpdate( p_demux, false );
877 case DVDNAV_HOP_CHANNEL:
878 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
879 p_sys->i_vobu_index = 0;
880 p_sys->i_vobu_flush = 0;
881 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
885 msg_Dbg( p_demux, "DVDNAV_WAIT" );
888 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
895 dvdnav_wait_skip( p_sys->dvdnav );
896 p_sys->b_reset_pcr = true;
901 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
906 dvdnav_free_cache_block( p_sys->dvdnav, packet );
913 * FIXME: partiallyy duplicated from src/input/es_out.c
915 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
917 const iso639_lang_t *pl;
921 psz_lang = var_CreateGetString( p_demux, psz_var );
923 return strdup(LANGUAGE_DEFAULT);
925 /* XXX: we will use only the first value
926 * (and ignore other ones in case of a list) */
927 if( ( p = strchr( psz_lang, ',' ) ) )
930 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
932 if( *psz_lang == '\0' )
934 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
935 !strcasecmp( pl->psz_native_name, psz_lang ) ||
936 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
937 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
938 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
944 if( pl->psz_eng_name != NULL )
945 return strdup( pl->psz_iso639_1 );
947 return strdup(LANGUAGE_DEFAULT);
950 static void DemuxTitles( demux_t *p_demux )
952 demux_sys_t *p_sys = p_demux->p_sys;
959 t = vlc_input_title_New();
961 t->psz_name = strdup( "DVD Menu" );
963 s = vlc_seekpoint_New();
964 s->psz_name = strdup( "Resume" );
965 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
967 s = vlc_seekpoint_New();
968 s->psz_name = strdup( "Root" );
969 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
971 s = vlc_seekpoint_New();
972 s->psz_name = strdup( "Title" );
973 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
975 s = vlc_seekpoint_New();
976 s->psz_name = strdup( "Chapter" );
977 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
979 s = vlc_seekpoint_New();
980 s->psz_name = strdup( "Subtitle" );
981 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
983 s = vlc_seekpoint_New();
984 s->psz_name = strdup( "Audio" );
985 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
987 s = vlc_seekpoint_New();
988 s->psz_name = strdup( "Angle" );
989 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
991 TAB_APPEND( p_sys->i_title, p_sys->title, t );
993 /* Find out number of titles/chapters */
994 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
995 for( i = 1; i <= i_titles; i++ )
997 int32_t i_chapters = 0;
1000 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
1002 t = vlc_input_title_New();
1003 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1005 s = vlc_seekpoint_New();
1006 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1009 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1013 /*****************************************************************************
1015 *****************************************************************************/
1016 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1018 demux_sys_t *p_sys = p_demux->p_sys;
1020 int32_t i_title, i_part;
1022 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1024 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
1026 vlc_mutex_t *p_mutex = val.p_address;
1027 dvdnav_highlight_area_t hl;
1031 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1032 != DVDNAV_STATUS_OK )
1034 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1038 b_button_ok = false;
1039 if( i_button > 0 && i_title == 0 )
1041 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1043 b_button_ok = DVDNAV_STATUS_OK ==
1044 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
1050 for( i = 0; i < 4; i++ )
1052 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1053 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1055 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1056 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1057 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1058 p_sys->palette[i][3] = i_alpha;
1061 vlc_mutex_lock( p_mutex );
1062 var_SetInteger( p_sys->p_input, "x-start", hl.sx );
1063 var_SetInteger( p_sys->p_input, "x-end", hl.ex );
1064 var_SetInteger( p_sys->p_input, "y-start", hl.sy );
1065 var_SetInteger( p_sys->p_input, "y-end", hl.ey );
1067 var_SetAddress( p_sys->p_input, "menu-palette", p_sys->palette );
1069 var_SetBool( p_sys->p_input, "highlight", true );
1070 vlc_mutex_unlock( p_mutex );
1072 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1076 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1077 i_button, i_title );
1080 vlc_mutex_lock( p_mutex );
1081 var_SetBool( p_sys->p_input, "highlight", false );
1082 vlc_mutex_unlock( p_mutex );
1087 static void ESSubtitleUpdate( demux_t *p_demux )
1089 demux_sys_t *p_sys = p_demux->p_sys;
1090 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1091 int32_t i_title, i_part;
1093 ButtonUpdate( p_demux, false );
1095 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1096 if( i_title > 0 ) return;
1098 if( i_spu >= 0 && i_spu <= 0x1f )
1100 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1102 ESNew( p_demux, 0xbd20 + i_spu );
1104 /* be sure to unselect it (reset) */
1105 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1109 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1113 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1115 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1118 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1125 /*****************************************************************************
1126 * DemuxBlock: demux a given block
1127 *****************************************************************************/
1128 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1130 demux_sys_t *p_sys = p_demux->p_sys;
1131 const uint8_t *p = pkt;
1133 while( p < &pkt[i_pkt] )
1135 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1142 /* Create a block */
1143 p_pkt = block_New( p_demux, i_size );
1144 memcpy( p_pkt->p_buffer, p, i_size);
1146 /* Parse it and send it */
1147 switch( 0x100 | p[3] )
1155 msg_Warn( p_demux, "received a PSM packet" );
1157 else if( p[3] == 0xbb )
1159 msg_Warn( p_demux, "received a SYSTEM packet" );
1162 block_Release( p_pkt );
1169 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1171 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1172 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1174 block_Release( p_pkt );
1179 int i_id = ps_pkt_id( p_pkt );
1182 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1186 ESNew( p_demux, i_id );
1188 if( tk->b_seen && tk->es &&
1189 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1191 es_out_Send( p_demux->out, tk->es, p_pkt );
1195 block_Release( p_pkt );
1200 block_Release( p_pkt );
1212 /*****************************************************************************
1213 * Force still images to be displayed by sending EOS and stopping buffering.
1214 *****************************************************************************/
1215 static void DemuxForceStill( demux_t *p_demux )
1217 static const uint8_t buffer[] = {
1218 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
1220 0x00, 0x00, 0x01, 0xB7,
1222 DemuxBlock( p_demux, buffer, sizeof(buffer) );
1225 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
1228 /*****************************************************************************
1229 * ESNew: register a new elementary stream
1230 *****************************************************************************/
1231 static void ESNew( demux_t *p_demux, int i_id )
1233 demux_sys_t *p_sys = p_demux->p_sys;
1234 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1235 bool b_select = false;
1237 if( tk->b_seen ) return;
1239 if( ps_track_fill( tk, 0, i_id ) )
1241 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1246 if( tk->fmt.i_cat == VIDEO_ES )
1248 tk->fmt.video.i_sar_num = p_sys->sar.i_num;
1249 tk->fmt.video.i_sar_den = p_sys->sar.i_den;
1252 else if( tk->fmt.i_cat == AUDIO_ES )
1255 /* find the audio number PLEASE find another way */
1256 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1258 i_audio = i_id&0x07;
1260 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1264 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1266 i_audio = i_id&0x1f;
1268 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1270 i_audio = i_id&0x1f;
1274 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1275 if( i_lang != 0xffff )
1277 tk->fmt.psz_language = malloc( 3 );
1278 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1279 tk->fmt.psz_language[1] = (i_lang )&0xff;
1280 tk->fmt.psz_language[2] = 0;
1282 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1288 else if( tk->fmt.i_cat == SPU_ES )
1290 int32_t i_title, i_part;
1291 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1292 if( i_lang != 0xffff )
1294 tk->fmt.psz_language = malloc( 3 );
1295 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1296 tk->fmt.psz_language[1] = (i_lang )&0xff;
1297 tk->fmt.psz_language[2] = 0;
1301 tk->fmt.subs.spu.palette[0] = 0xBeef;
1302 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1303 16 * sizeof( uint32_t ) );
1305 /* We select only when we are not in the menu */
1306 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1308 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1314 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1317 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1321 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1324 /*****************************************************************************
1326 *****************************************************************************/
1327 static void StillTimer( void *p_data )
1329 demux_sys_t *p_sys = p_data;
1331 vlc_mutex_lock( &p_sys->still.lock );
1332 if( likely(p_sys->still.b_enabled) )
1334 p_sys->still.b_enabled = false;
1335 dvdnav_still_skip( p_sys->dvdnav );
1337 vlc_mutex_unlock( &p_sys->still.lock );
1340 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1341 vlc_value_t oldval, vlc_value_t val, void *p_data )
1343 demux_t *p_demux = p_data;
1344 demux_sys_t *p_sys = p_demux->p_sys;
1346 /* FIXME? PCI usage thread safe? */
1347 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1348 int x = val.coords.x;
1349 int y = val.coords.y;
1351 if( psz_var[6] == 'm' ) /* mouse-moved */
1352 dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1355 assert( psz_var[6] == 'c' ); /* mouse-clicked */
1357 ButtonUpdate( p_demux, true );
1358 dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1365 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1366 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1368 demux_t *p_demux = p_data;
1369 demux_sys_t *p_sys = p_demux->p_sys;
1371 /* FIXME: thread-safe ? */
1372 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1374 switch( newval.i_int )
1376 case ACTIONID_NAV_LEFT:
1377 dvdnav_left_button_select( p_sys->dvdnav, pci );
1379 case ACTIONID_NAV_RIGHT:
1380 dvdnav_right_button_select( p_sys->dvdnav, pci );
1382 case ACTIONID_NAV_UP:
1383 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1385 case ACTIONID_NAV_DOWN:
1386 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1388 case ACTIONID_NAV_ACTIVATE:
1389 ButtonUpdate( p_demux, true );
1390 dvdnav_button_activate( p_sys->dvdnav, pci );
1394 (void)p_this; (void)psz_var; (void)oldval;
1398 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1399 vlc_value_t oldval, vlc_value_t val, void *p_data )
1401 demux_t *p_demux = p_data;
1402 demux_sys_t *p_sys = p_demux->p_sys;
1404 if (val.i_int == INPUT_EVENT_VOUT)
1406 vlc_object_t *p_vout;
1408 p_vout = p_sys->p_vout;
1409 if( p_vout != NULL )
1411 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1412 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1413 vlc_object_release( p_vout );
1416 p_vout = (vlc_object_t *)input_GetVout( (input_thread_t *)p_input );
1417 p_sys->p_vout = p_vout;
1418 if( p_vout != NULL )
1420 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1421 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1424 (void) psz_var; (void) oldval;
1428 /*****************************************************************************
1429 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1430 *****************************************************************************/
1431 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1434 #ifdef HAVE_SYS_STAT_H
1435 struct stat stat_info;
1436 uint8_t pi_anchor[2];
1441 /* Triggers libdvdcss autodetection */
1445 if( (i_fd = vlc_open( psz_name, O_RDONLY |O_NONBLOCK )) == -1 )
1447 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1450 i_ret = VLC_EGENERIC;
1452 if( fstat( i_fd, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1454 if( !S_ISFIFO( stat_info.st_mode ) )
1455 i_ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1459 /* Try to find the anchor (2 bytes at LBA 256) */
1460 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1461 && read( i_fd, pi_anchor, 2 ) == 2
1462 && GetWLE( pi_anchor ) == 2 )
1463 i_ret = VLC_SUCCESS; /* Found a potential anchor */