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_file || !*p_demux->psz_file )
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_file );
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';
215 /* Try some simple probing to avoid going through dvdnav_open too often */
216 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
223 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
225 msg_Warn( p_demux, "cannot open dvdnav" );
231 /* Fill p_demux field */
232 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
233 p_sys->dvdnav = p_dvdnav;
234 p_sys->b_reset_pcr = false;
236 ps_track_init( p_sys->tk );
237 p_sys->sar.i_num = 0;
238 p_sys->sar.i_den = 0;
239 p_sys->i_mux_rate = 0;
240 p_sys->i_pgc_length = 0;
241 p_sys->b_spu_change = false;
242 p_sys->i_vobu_index = 0;
243 p_sys->i_vobu_flush = 0;
247 // Hack for libdvdnav CVS.
248 // Without it dvdnav_get_number_of_titles() fails.
249 // Remove when fixed in libdvdnav CVS.
250 uint8_t buffer[DVD_VIDEO_LB_LEN];
253 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
254 == DVDNAV_STATUS_ERR )
256 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
259 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
262 /* Configure dvdnav */
263 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
266 msg_Warn( p_demux, "cannot set read-a-head flag" );
269 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
272 msg_Warn( p_demux, "cannot set PGC positioning flag" );
276 * XXX A menu-language may be better than sub-language */
277 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
278 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
281 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
282 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
283 /* We try to fall back to 'en' */
284 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
285 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
289 /* Set audio language */
290 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
291 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
294 msg_Warn( p_demux, "can't set audio 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_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
302 /* Set spu language */
303 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
304 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
307 msg_Warn( p_demux, "can't set spu 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_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
315 DemuxTitles( p_demux );
317 if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
319 msg_Dbg( p_demux, "trying to go to dvd menu" );
321 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
323 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
324 dialog_Fatal( p_demux, _("Playback failure"), "%s",
325 _("VLC cannot set the DVD's title. It possibly "
326 "cannot decrypt the entire disc.") );
327 dvdnav_close( p_sys->dvdnav );
332 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
335 /* Try going to menu root */
336 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
338 msg_Warn( p_demux, "cannot go to dvd menu" );
342 i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
343 if( i_angle <= 0 ) i_angle = 1;
345 /* Update default_pts to a suitable value for dvdnav access */
346 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
348 /* FIXME hack hack hack hack FIXME */
349 /* Get p_input and create variable */
350 p_sys->p_input = demux_GetParentInput( p_demux );
351 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
352 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
353 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
354 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
355 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
356 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
357 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
358 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
360 /* catch all key event */
361 var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
362 /* catch vout creation event */
363 var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
365 p_sys->still.b_enabled = false;
366 vlc_mutex_init( &p_sys->still.lock );
367 if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
368 p_sys->still.b_created = true;
373 /*****************************************************************************
375 *****************************************************************************/
376 static void Close( vlc_object_t *p_this )
378 demux_t *p_demux = (demux_t*)p_this;
379 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 key event handler (FIXME: should really be per-vout too) */
392 var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
394 /* Stop still image handler */
395 if( p_sys->still.b_created )
396 vlc_timer_destroy( p_sys->still.timer );
397 vlc_mutex_destroy( &p_sys->still.lock );
399 var_Destroy( p_sys->p_input, "highlight-mutex" );
400 var_Destroy( p_sys->p_input, "highlight" );
401 var_Destroy( p_sys->p_input, "x-start" );
402 var_Destroy( p_sys->p_input, "x-end" );
403 var_Destroy( p_sys->p_input, "y-start" );
404 var_Destroy( p_sys->p_input, "y-end" );
405 var_Destroy( p_sys->p_input, "color" );
406 var_Destroy( p_sys->p_input, "menu-palette" );
408 vlc_object_release( p_sys->p_input );
410 for( i = 0; i < PS_TK_COUNT; i++ )
412 ps_track_t *tk = &p_sys->tk[i];
415 es_format_Clean( &tk->fmt );
416 if( tk->es ) es_out_Del( p_demux->out, tk->es );
420 dvdnav_close( p_sys->dvdnav );
424 /*****************************************************************************
426 *****************************************************************************/
427 static int Control( demux_t *p_demux, int i_query, va_list args )
429 demux_sys_t *p_sys = p_demux->p_sys;
430 input_title_t ***ppp_title;
435 case DEMUX_SET_POSITION:
436 case DEMUX_GET_POSITION:
438 case DEMUX_GET_LENGTH:
441 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
442 DVDNAV_STATUS_OK || len == 0 )
449 case DEMUX_GET_POSITION:
450 *va_arg( args, double* ) = (double)pos / (double)len;
453 case DEMUX_SET_POSITION:
454 pos = va_arg( args, double ) * len;
455 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
463 if( p_sys->i_pgc_length > 0 )
465 *va_arg( args, int64_t * ) = p_sys->i_pgc_length*pos/len;
470 case DEMUX_GET_LENGTH:
471 if( p_sys->i_pgc_length > 0 )
473 *va_arg( args, int64_t * ) = (int64_t)p_sys->i_pgc_length;
481 /* Special for access_demux */
482 case DEMUX_CAN_PAUSE:
484 case DEMUX_CAN_CONTROL_PACE:
486 *va_arg( args, bool * ) = true;
489 case DEMUX_SET_PAUSE_STATE:
492 case DEMUX_GET_TITLE_INFO:
493 ppp_title = va_arg( args, input_title_t*** );
494 *va_arg( args, int* ) = p_sys->i_title;
495 *va_arg( args, int* ) = 0; /* Title offset */
496 *va_arg( args, int* ) = 1; /* Chapter offset */
498 /* Duplicate title infos */
499 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
500 for( i = 0; i < p_sys->i_title; i++ )
502 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
506 case DEMUX_SET_TITLE:
507 i = (int)va_arg( args, int );
508 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
509 != DVDNAV_STATUS_OK ) ||
510 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
511 != DVDNAV_STATUS_OK ) )
513 msg_Warn( p_demux, "cannot set title/chapter" );
516 p_demux->info.i_update |=
517 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
518 p_demux->info.i_title = i;
519 p_demux->info.i_seekpoint = 0;
522 case DEMUX_SET_SEEKPOINT:
523 i = va_arg( args, int );
524 if( p_demux->info.i_title == 0 )
526 static const int argtab[] = {
535 enum { numargs = sizeof(argtab)/sizeof(int) };
536 if( (unsigned)i >= numargs || DVDNAV_STATUS_OK !=
537 dvdnav_menu_call(p_sys->dvdnav,argtab[i]) )
540 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
541 i + 1 ) != DVDNAV_STATUS_OK )
543 msg_Warn( p_demux, "cannot set title/chapter" );
546 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
547 p_demux->info.i_seekpoint = i;
550 case DEMUX_GET_PTS_DELAY:
551 *va_arg( args, int64_t * )
552 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
557 const char *title_name = NULL;
559 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
560 if( (NULL != title_name) && ('\0' != title_name[0]) )
562 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
563 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
569 /* TODO implement others */
575 static int ControlInternal( demux_t *p_demux, int i_query, ... )
580 va_start( args, i_query );
581 i_result = Control( p_demux, i_query, args );
586 /*****************************************************************************
588 *****************************************************************************/
589 static int Demux( demux_t *p_demux )
591 demux_sys_t *p_sys = p_demux->p_sys;
593 uint8_t buffer[DVD_VIDEO_LB_LEN];
594 uint8_t *packet = buffer;
599 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
600 == DVDNAV_STATUS_ERR )
602 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
603 == DVDNAV_STATUS_ERR )
606 msg_Warn( p_demux, "cannot get next block (%s)",
607 dvdnav_err_to_string( p_sys->dvdnav ) );
608 if( p_demux->info.i_title == 0 )
610 msg_Dbg( p_demux, "jumping to first title" );
611 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
618 case DVDNAV_BLOCK_OK: /* mpeg block */
619 vlc_mutex_lock( &p_sys->still.lock );
620 vlc_timer_schedule( p_sys->still.timer, false, 0, 0 );
621 p_sys->still.b_enabled = false;
622 vlc_mutex_unlock( &p_sys->still.lock );
623 if( p_sys->b_reset_pcr )
625 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
626 p_sys->b_reset_pcr = false;
628 DemuxBlock( p_demux, packet, i_len );
629 if( p_sys->i_vobu_index > 0 )
631 if( p_sys->i_vobu_flush == p_sys->i_vobu_index )
632 DemuxForceStill( p_demux );
633 p_sys->i_vobu_index++;
637 case DVDNAV_NOP: /* Nothing */
638 msg_Dbg( p_demux, "DVDNAV_NOP" );
641 case DVDNAV_STILL_FRAME:
643 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
644 bool b_still_init = false;
646 vlc_mutex_lock( &p_sys->still.lock );
647 if( !p_sys->still.b_enabled )
649 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
650 msg_Dbg( p_demux, " - length=0x%x", event->length );
651 p_sys->still.b_enabled = true;
653 if( event->length != 0xff && p_sys->still.b_created )
655 mtime_t delay = event->length * CLOCK_FREQ;
656 vlc_timer_schedule( p_sys->still.timer, false, delay, 0 );
661 vlc_mutex_unlock( &p_sys->still.lock );
665 DemuxForceStill( p_demux );
666 p_sys->b_reset_pcr = true;
672 case DVDNAV_SPU_CLUT_CHANGE:
676 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
677 /* Update color lookup table (16 *uint32_t in packet) */
678 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
680 /* HACK to get the SPU tracks registered in the right order */
681 for( i = 0; i < 0x1f; i++ )
683 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
684 ESNew( p_demux, 0xbd20 + i );
690 case DVDNAV_SPU_STREAM_CHANGE:
692 dvdnav_spu_stream_change_event_t *event =
693 (dvdnav_spu_stream_change_event_t*)packet;
696 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
697 msg_Dbg( p_demux, " - physical_wide=%d",
698 event->physical_wide );
699 msg_Dbg( p_demux, " - physical_letterbox=%d",
700 event->physical_letterbox);
701 msg_Dbg( p_demux, " - physical_pan_scan=%d",
702 event->physical_pan_scan );
704 ESSubtitleUpdate( p_demux );
705 p_sys->b_spu_change = true;
707 /* HACK to get the SPU tracks registered in the right order */
708 for( i = 0; i < 0x1f; i++ )
710 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
711 ESNew( p_demux, 0xbd20 + i );
717 case DVDNAV_AUDIO_STREAM_CHANGE:
719 dvdnav_audio_stream_change_event_t *event =
720 (dvdnav_audio_stream_change_event_t*)packet;
721 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
722 msg_Dbg( p_demux, " - physical=%d", event->physical );
727 case DVDNAV_VTS_CHANGE:
733 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
734 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
735 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
736 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
739 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
741 for( i = 0; i < PS_TK_COUNT; i++ )
743 ps_track_t *tk = &p_sys->tk[i];
746 es_format_Clean( &tk->fmt );
747 if( tk->es ) es_out_Del( p_demux->out, tk->es );
752 #if defined(HAVE_DVDNAV_GET_VIDEO_RESOLUTION)
753 uint32_t i_width, i_height;
754 if( dvdnav_get_video_resolution( p_sys->dvdnav,
755 &i_width, &i_height ) )
756 i_width = i_height = 0;
757 switch( dvdnav_get_video_aspect( p_sys->dvdnav ) )
760 p_sys->sar.i_num = 4 * i_height;
761 p_sys->sar.i_den = 3 * i_width;
764 p_sys->sar.i_num = 16 * i_height;
765 p_sys->sar.i_den = 9 * i_width;
768 p_sys->sar.i_num = 0;
769 p_sys->sar.i_den = 0;
774 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
775 &i_part ) == DVDNAV_STATUS_OK )
777 if( i_title >= 0 && i_title < p_sys->i_title &&
778 p_demux->info.i_title != i_title )
780 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
781 p_demux->info.i_title = i_title;
787 case DVDNAV_CELL_CHANGE:
792 dvdnav_cell_change_event_t *event =
793 (dvdnav_cell_change_event_t*)packet;
794 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
795 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
796 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
797 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
798 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
799 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
800 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
801 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
803 /* Store the lenght in time of the current PGC */
804 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
805 p_sys->i_vobu_index = 0;
806 p_sys->i_vobu_flush = 0;
808 /* FIXME is it correct or there is better way to know chapter change */
809 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
810 &i_part ) == DVDNAV_STATUS_OK )
812 if( i_title >= 0 && i_title < p_sys->i_title &&
813 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
814 p_demux->info.i_seekpoint != i_part - 1 )
816 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
817 p_demux->info.i_seekpoint = i_part - 1;
823 case DVDNAV_NAV_PACKET:
825 p_sys->i_vobu_index = 1;
826 p_sys->i_vobu_flush = 0;
828 /* Look if we have need to force a flush (and when) */
829 const pci_gi_t *p_pci_gi = &dvdnav_get_current_nav_pci( p_sys->dvdnav )->pci_gi;
830 if( p_pci_gi->vobu_se_e_ptm != 0 && p_pci_gi->vobu_se_e_ptm < p_pci_gi->vobu_e_ptm )
832 const dsi_gi_t *p_dsi_gi = &dvdnav_get_current_nav_dsi( p_sys->dvdnav )->dsi_gi;
833 if( p_dsi_gi->vobu_3rdref_ea != 0 )
834 p_sys->i_vobu_flush = p_dsi_gi->vobu_3rdref_ea;
835 else if( p_dsi_gi->vobu_2ndref_ea != 0 )
836 p_sys->i_vobu_flush = p_dsi_gi->vobu_2ndref_ea;
837 else if( p_dsi_gi->vobu_1stref_ea != 0 )
838 p_sys->i_vobu_flush = p_dsi_gi->vobu_1stref_ea;
842 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
844 /* A lot of thing to do here :
846 * - fetch pts (for time display)
849 DemuxBlock( p_demux, packet, i_len );
850 if( p_sys->b_spu_change )
852 ButtonUpdate( p_demux, false );
853 p_sys->b_spu_change = false;
858 case DVDNAV_STOP: /* EOF */
859 msg_Dbg( p_demux, "DVDNAV_STOP" );
862 dvdnav_free_cache_block( p_sys->dvdnav, packet );
866 case DVDNAV_HIGHLIGHT:
868 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
869 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
870 msg_Dbg( p_demux, " - display=%d", event->display );
871 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
872 ButtonUpdate( p_demux, false );
876 case DVDNAV_HOP_CHANNEL:
877 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
878 p_sys->i_vobu_index = 0;
879 p_sys->i_vobu_flush = 0;
880 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
884 msg_Dbg( p_demux, "DVDNAV_WAIT" );
887 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
894 dvdnav_wait_skip( p_sys->dvdnav );
895 p_sys->b_reset_pcr = true;
900 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
905 dvdnav_free_cache_block( p_sys->dvdnav, packet );
912 * FIXME: partiallyy duplicated from src/input/es_out.c
914 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
916 const iso639_lang_t *pl;
920 psz_lang = var_CreateGetString( p_demux, psz_var );
922 return strdup(LANGUAGE_DEFAULT);
924 /* XXX: we will use only the first value
925 * (and ignore other ones in case of a list) */
926 if( ( p = strchr( psz_lang, ',' ) ) )
929 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
931 if( *psz_lang == '\0' )
933 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
934 !strcasecmp( pl->psz_native_name, psz_lang ) ||
935 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
936 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
937 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
943 if( pl->psz_eng_name != NULL )
944 return strdup( pl->psz_iso639_1 );
946 return strdup(LANGUAGE_DEFAULT);
949 static void DemuxTitles( demux_t *p_demux )
951 demux_sys_t *p_sys = p_demux->p_sys;
958 t = vlc_input_title_New();
960 t->psz_name = strdup( "DVD Menu" );
962 s = vlc_seekpoint_New();
963 s->psz_name = strdup( "Resume" );
964 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
966 s = vlc_seekpoint_New();
967 s->psz_name = strdup( "Root" );
968 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
970 s = vlc_seekpoint_New();
971 s->psz_name = strdup( "Title" );
972 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
974 s = vlc_seekpoint_New();
975 s->psz_name = strdup( "Chapter" );
976 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
978 s = vlc_seekpoint_New();
979 s->psz_name = strdup( "Subtitle" );
980 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
982 s = vlc_seekpoint_New();
983 s->psz_name = strdup( "Audio" );
984 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
986 s = vlc_seekpoint_New();
987 s->psz_name = strdup( "Angle" );
988 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
990 TAB_APPEND( p_sys->i_title, p_sys->title, t );
992 /* Find out number of titles/chapters */
993 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
994 for( i = 1; i <= i_titles; i++ )
996 int32_t i_chapters = 0;
999 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
1001 t = vlc_input_title_New();
1002 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1004 s = vlc_seekpoint_New();
1005 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1008 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1012 /*****************************************************************************
1014 *****************************************************************************/
1015 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1017 demux_sys_t *p_sys = p_demux->p_sys;
1019 int32_t i_title, i_part;
1021 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1023 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
1025 vlc_mutex_t *p_mutex = val.p_address;
1026 dvdnav_highlight_area_t hl;
1030 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1031 != DVDNAV_STATUS_OK )
1033 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1037 b_button_ok = false;
1038 if( i_button > 0 && i_title == 0 )
1040 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1042 b_button_ok = DVDNAV_STATUS_OK ==
1043 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
1049 for( i = 0; i < 4; i++ )
1051 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1052 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1054 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1055 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1056 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1057 p_sys->palette[i][3] = i_alpha;
1060 vlc_mutex_lock( p_mutex );
1061 var_SetInteger( p_sys->p_input, "x-start", hl.sx );
1062 var_SetInteger( p_sys->p_input, "x-end", hl.ex );
1063 var_SetInteger( p_sys->p_input, "y-start", hl.sy );
1064 var_SetInteger( p_sys->p_input, "y-end", hl.ey );
1066 var_SetAddress( p_sys->p_input, "menu-palette", p_sys->palette );
1068 var_SetBool( p_sys->p_input, "highlight", true );
1069 vlc_mutex_unlock( p_mutex );
1071 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1075 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1076 i_button, i_title );
1079 vlc_mutex_lock( p_mutex );
1080 var_SetBool( p_sys->p_input, "highlight", false );
1081 vlc_mutex_unlock( p_mutex );
1086 static void ESSubtitleUpdate( demux_t *p_demux )
1088 demux_sys_t *p_sys = p_demux->p_sys;
1089 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1090 int32_t i_title, i_part;
1092 ButtonUpdate( p_demux, false );
1094 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1095 if( i_title > 0 ) return;
1097 if( i_spu >= 0 && i_spu <= 0x1f )
1099 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1101 ESNew( p_demux, 0xbd20 + i_spu );
1103 /* be sure to unselect it (reset) */
1104 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1108 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1112 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1114 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1117 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1124 /*****************************************************************************
1125 * DemuxBlock: demux a given block
1126 *****************************************************************************/
1127 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1129 demux_sys_t *p_sys = p_demux->p_sys;
1130 const uint8_t *p = pkt;
1132 while( p < &pkt[i_pkt] )
1134 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1141 /* Create a block */
1142 p_pkt = block_New( p_demux, i_size );
1143 memcpy( p_pkt->p_buffer, p, i_size);
1145 /* Parse it and send it */
1146 switch( 0x100 | p[3] )
1154 msg_Warn( p_demux, "received a PSM packet" );
1156 else if( p[3] == 0xbb )
1158 msg_Warn( p_demux, "received a SYSTEM packet" );
1161 block_Release( p_pkt );
1168 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1170 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1171 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1173 block_Release( p_pkt );
1178 int i_id = ps_pkt_id( p_pkt );
1181 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1185 ESNew( p_demux, i_id );
1187 if( tk->b_seen && tk->es &&
1188 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1190 es_out_Send( p_demux->out, tk->es, p_pkt );
1194 block_Release( p_pkt );
1199 block_Release( p_pkt );
1211 /*****************************************************************************
1212 * Force still images to be displayed by sending EOS and stopping buffering.
1213 *****************************************************************************/
1214 static void DemuxForceStill( demux_t *p_demux )
1216 static const uint8_t buffer[] = {
1217 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
1219 0x00, 0x00, 0x01, 0xB7,
1221 DemuxBlock( p_demux, buffer, sizeof(buffer) );
1224 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
1227 /*****************************************************************************
1228 * ESNew: register a new elementary stream
1229 *****************************************************************************/
1230 static void ESNew( demux_t *p_demux, int i_id )
1232 demux_sys_t *p_sys = p_demux->p_sys;
1233 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1234 bool b_select = false;
1236 if( tk->b_seen ) return;
1238 if( ps_track_fill( tk, 0, i_id ) )
1240 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1245 if( tk->fmt.i_cat == VIDEO_ES )
1247 tk->fmt.video.i_sar_num = p_sys->sar.i_num;
1248 tk->fmt.video.i_sar_den = p_sys->sar.i_den;
1251 else if( tk->fmt.i_cat == AUDIO_ES )
1254 /* find the audio number PLEASE find another way */
1255 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1257 i_audio = i_id&0x07;
1259 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1263 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1265 i_audio = i_id&0x1f;
1267 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1269 i_audio = i_id&0x1f;
1273 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1274 if( i_lang != 0xffff )
1276 tk->fmt.psz_language = malloc( 3 );
1277 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1278 tk->fmt.psz_language[1] = (i_lang )&0xff;
1279 tk->fmt.psz_language[2] = 0;
1281 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1287 else if( tk->fmt.i_cat == SPU_ES )
1289 int32_t i_title, i_part;
1290 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1291 if( i_lang != 0xffff )
1293 tk->fmt.psz_language = malloc( 3 );
1294 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1295 tk->fmt.psz_language[1] = (i_lang )&0xff;
1296 tk->fmt.psz_language[2] = 0;
1300 tk->fmt.subs.spu.palette[0] = 0xBeef;
1301 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1302 16 * sizeof( uint32_t ) );
1304 /* We select only when we are not in the menu */
1305 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1307 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1313 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1316 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1320 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1323 /*****************************************************************************
1325 *****************************************************************************/
1326 static void StillTimer( void *p_data )
1328 demux_sys_t *p_sys = p_data;
1330 vlc_mutex_lock( &p_sys->still.lock );
1331 if( likely(p_sys->still.b_enabled) )
1333 p_sys->still.b_enabled = false;
1334 dvdnav_still_skip( p_sys->dvdnav );
1336 vlc_mutex_unlock( &p_sys->still.lock );
1339 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1340 vlc_value_t oldval, vlc_value_t val, void *p_data )
1342 demux_t *p_demux = p_data;
1343 demux_sys_t *p_sys = p_demux->p_sys;
1345 /* FIXME? PCI usage thread safe? */
1346 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1347 int x = val.coords.x;
1348 int y = val.coords.y;
1350 if( psz_var[6] == 'm' ) /* mouse-moved */
1351 dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1354 assert( psz_var[6] == 'c' ); /* mouse-clicked */
1356 ButtonUpdate( p_demux, true );
1357 dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1364 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1365 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1367 demux_t *p_demux = p_data;
1368 demux_sys_t *p_sys = p_demux->p_sys;
1370 /* FIXME: thread-safe ? */
1371 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1373 switch( newval.i_int )
1375 case ACTIONID_NAV_LEFT:
1376 dvdnav_left_button_select( p_sys->dvdnav, pci );
1378 case ACTIONID_NAV_RIGHT:
1379 dvdnav_right_button_select( p_sys->dvdnav, pci );
1381 case ACTIONID_NAV_UP:
1382 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1384 case ACTIONID_NAV_DOWN:
1385 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1387 case ACTIONID_NAV_ACTIVATE:
1388 ButtonUpdate( p_demux, true );
1389 dvdnav_button_activate( p_sys->dvdnav, pci );
1393 (void)p_this; (void)psz_var; (void)oldval;
1397 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1398 vlc_value_t oldval, vlc_value_t val, void *p_data )
1400 demux_t *p_demux = p_data;
1401 demux_sys_t *p_sys = p_demux->p_sys;
1403 if (val.i_int == INPUT_EVENT_VOUT)
1405 vlc_object_t *p_vout;
1407 p_vout = p_sys->p_vout;
1408 if( p_vout != NULL )
1410 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1411 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1412 vlc_object_release( p_vout );
1415 p_vout = (vlc_object_t *)input_GetVout( (input_thread_t *)p_input );
1416 p_sys->p_vout = p_vout;
1417 if( p_vout != NULL )
1419 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_demux );
1420 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
1423 (void) psz_var; (void) oldval;
1427 /*****************************************************************************
1428 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1429 *****************************************************************************/
1430 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1433 #ifdef HAVE_SYS_STAT_H
1434 struct stat stat_info;
1435 uint8_t pi_anchor[2];
1440 /* Triggers libdvdcss autodetection */
1444 if( (i_fd = vlc_open( psz_name, O_RDONLY |O_NONBLOCK )) == -1 )
1446 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1449 i_ret = VLC_EGENERIC;
1451 if( fstat( i_fd, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1453 if( !S_ISFIFO( stat_info.st_mode ) )
1454 i_ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1458 /* Try to find the anchor (2 bytes at LBA 256) */
1459 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1460 && read( i_fd, pi_anchor, 2 ) == 2
1461 && GetWLE( pi_anchor ) == 2 )
1462 i_ret = VLC_SUCCESS; /* Found a potential anchor */