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 *****************************************************************************/
44 #include <unistd.h> /* close() */
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
48 #include <vlc_input.h>
49 #include <vlc_access.h>
50 #include <vlc_demux.h>
51 #include <vlc_charset.h>
54 #include <vlc_dialog.h>
55 #include <vlc_iso_lang.h>
57 /* FIXME we should find a better way than including that */
58 #include "../../src/text/iso-639_def.h"
61 #include <dvdnav/dvdnav.h>
63 #include "../demux/ps.h"
65 /*****************************************************************************
67 *****************************************************************************/
68 #define ANGLE_TEXT N_("DVD angle")
69 #define ANGLE_LONGTEXT N_( \
70 "Default DVD angle." )
72 #define MENU_TEXT N_("Start directly in menu")
73 #define MENU_LONGTEXT N_( \
74 "Start the DVD directly in the main menu. This "\
75 "will try to skip all the useless warning introductions." )
77 #define LANGUAGE_DEFAULT ("en")
79 static int Open ( vlc_object_t * );
80 static void Close( vlc_object_t * );
83 set_shortname( N_("DVD with menus") )
84 set_description( N_("DVDnav Input") )
85 set_category( CAT_INPUT )
86 set_subcategory( SUBCAT_INPUT_ACCESS )
87 add_integer( "dvdnav-angle", 1, ANGLE_TEXT,
88 ANGLE_LONGTEXT, false )
89 add_bool( "dvdnav-menu", true,
90 MENU_TEXT, MENU_LONGTEXT, false )
91 set_capability( "access_demux", 5 )
92 add_shortcut( "dvd", "dvdnav", "file" )
93 set_callbacks( Open, Close )
96 /* Shall we use libdvdnav's read ahead cache? */
98 #define DVD_READ_CACHE 0
100 #define DVD_READ_CACHE 1
103 /*****************************************************************************
105 *****************************************************************************/
122 ps_track_t tk[PS_TK_COUNT];
125 /* for spu variables */
126 input_thread_t *p_input;
129 vout_thread_t *p_vout;
131 /* palette for menus */
133 uint8_t palette[4][4];
144 input_title_t **title;
146 /* lenght of program group chain */
147 mtime_t i_pgc_length;
152 static int Control( demux_t *, int, va_list );
153 static int Demux( demux_t * );
154 static int DemuxBlock( demux_t *, const uint8_t *, int );
155 static void DemuxForceStill( demux_t * );
157 static void DemuxTitles( demux_t * );
158 static void ESSubtitleUpdate( demux_t * );
159 static void ButtonUpdate( demux_t *, bool );
161 static void ESNew( demux_t *, int );
162 static int ProbeDVD( const char * );
164 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
166 static int ControlInternal( demux_t *, int, ... );
168 static void StillTimer( void * );
170 static int EventMouse( vlc_object_t *, char const *,
171 vlc_value_t, vlc_value_t, void * );
172 static int EventIntf( vlc_object_t *, char const *,
173 vlc_value_t, vlc_value_t, void * );
175 /*****************************************************************************
177 *****************************************************************************/
178 static int Open( vlc_object_t *p_this )
180 demux_t *p_demux = (demux_t*)p_this;
188 if( p_demux->psz_access != NULL
189 && !strncmp(p_demux->psz_access, "dvd", 3) )
192 if( !p_demux->psz_file || !*p_demux->psz_file )
194 /* Only when selected */
198 psz_file = var_InheritString( p_this, "dvd" );
201 psz_file = strdup( p_demux->psz_file );
203 #if defined( _WIN32 ) || defined( __OS2__ )
204 if( psz_file != NULL )
206 /* Remove trailing backslash, otherwise dvdnav_open will fail */
207 size_t flen = strlen( psz_file );
208 if( flen > 0 && psz_file[flen - 1] == '\\' )
209 psz_file[flen - 1] = '\0';
212 psz_file = strdup("");
214 if( unlikely(psz_file == NULL) )
217 /* Try some simple probing to avoid going through dvdnav_open too often */
218 if( !forced && ProbeDVD( psz_file ) != VLC_SUCCESS )
225 const char *psz_path = ToLocale( psz_file );
226 if( dvdnav_open( &p_dvdnav, psz_path ) != DVDNAV_STATUS_OK )
228 LocaleFree( psz_path );
229 if( p_dvdnav == NULL )
231 msg_Warn( p_demux, "cannot open DVD (%s)", psz_file);
237 /* Fill p_demux field */
238 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
239 p_sys->dvdnav = p_dvdnav;
240 p_sys->b_reset_pcr = false;
242 ps_track_init( p_sys->tk );
243 p_sys->sar.i_num = 0;
244 p_sys->sar.i_den = 0;
245 p_sys->i_mux_rate = 0;
246 p_sys->i_pgc_length = 0;
247 p_sys->b_spu_change = false;
248 p_sys->i_vobu_index = 0;
249 p_sys->i_vobu_flush = 0;
253 // Hack for libdvdnav CVS.
254 // Without it dvdnav_get_number_of_titles() fails.
255 // Remove when fixed in libdvdnav CVS.
256 uint8_t buffer[DVD_VIDEO_LB_LEN];
259 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
260 == DVDNAV_STATUS_ERR )
262 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
265 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
268 /* Configure dvdnav */
269 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
272 msg_Warn( p_demux, "cannot set read-a-head flag" );
275 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
278 msg_Warn( p_demux, "cannot set PGC positioning flag" );
281 /* Set menu language */
282 psz_code = DemuxGetLanguageCode( p_demux, "menu-language" );
283 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
286 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
287 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
288 /* We try to fall back to 'en' */
289 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
290 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
294 /* Set audio language */
295 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
296 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
299 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
300 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
301 /* We try to fall back to 'en' */
302 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
303 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
307 /* Set spu language */
308 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
309 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
312 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
313 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
314 /* We try to fall back to 'en' */
315 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
316 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
320 DemuxTitles( p_demux );
322 if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
324 msg_Dbg( p_demux, "trying to go to dvd menu" );
326 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
328 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
329 dialog_Fatal( p_demux, _("Playback failure"), "%s",
330 _("VLC cannot set the DVD's title. It possibly "
331 "cannot decrypt the entire disc.") );
332 dvdnav_close( p_sys->dvdnav );
337 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
340 /* Try going to menu root */
341 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
343 msg_Warn( p_demux, "cannot go to dvd menu" );
347 i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
348 if( i_angle <= 0 ) i_angle = 1;
350 /* FIXME hack hack hack hack FIXME */
351 /* Get p_input and create variable */
352 p_sys->p_input = demux_GetParentInput( p_demux );
353 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
354 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
355 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
356 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
357 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
358 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
359 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
361 /* catch vout creation event */
362 var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
364 p_sys->still.b_enabled = false;
365 vlc_mutex_init( &p_sys->still.lock );
366 if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
367 p_sys->still.b_created = true;
372 /*****************************************************************************
374 *****************************************************************************/
375 static void Close( vlc_object_t *p_this )
377 demux_t *p_demux = (demux_t*)p_this;
378 demux_sys_t *p_sys = p_demux->p_sys;
380 /* Stop vout event handler */
381 var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
382 if( p_sys->p_vout != NULL )
383 { /* Should not happen, but better be safe than sorry. */
384 msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
385 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
386 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
389 /* Stop still image handler */
390 if( p_sys->still.b_created )
391 vlc_timer_destroy( p_sys->still.timer );
392 vlc_mutex_destroy( &p_sys->still.lock );
394 var_Destroy( p_sys->p_input, "highlight" );
395 var_Destroy( p_sys->p_input, "x-start" );
396 var_Destroy( p_sys->p_input, "x-end" );
397 var_Destroy( p_sys->p_input, "y-start" );
398 var_Destroy( p_sys->p_input, "y-end" );
399 var_Destroy( p_sys->p_input, "color" );
400 var_Destroy( p_sys->p_input, "menu-palette" );
402 vlc_object_release( p_sys->p_input );
404 for( int i = 0; i < PS_TK_COUNT; i++ )
406 ps_track_t *tk = &p_sys->tk[i];
409 es_format_Clean( &tk->fmt );
410 if( tk->es ) es_out_Del( p_demux->out, tk->es );
414 /* Free the array of titles */
415 for( int i = 0; i < p_sys->i_title; i++ )
416 vlc_input_title_Delete( p_sys->title[i] );
417 TAB_CLEAN( p_sys->i_title, p_sys->title );
419 dvdnav_close( p_sys->dvdnav );
423 /*****************************************************************************
425 *****************************************************************************/
426 static int Control( demux_t *p_demux, int i_query, va_list args )
428 demux_sys_t *p_sys = p_demux->p_sys;
429 input_title_t ***ppp_title;
434 case DEMUX_SET_POSITION:
435 case DEMUX_GET_POSITION:
437 case DEMUX_GET_LENGTH:
440 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
441 DVDNAV_STATUS_OK || len == 0 )
448 case DEMUX_GET_POSITION:
449 *va_arg( args, double* ) = (double)pos / (double)len;
452 case DEMUX_SET_POSITION:
453 pos = va_arg( args, double ) * len;
454 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
462 if( p_sys->i_pgc_length > 0 )
464 *va_arg( args, int64_t * ) = p_sys->i_pgc_length*pos/len;
469 case DEMUX_GET_LENGTH:
470 if( p_sys->i_pgc_length > 0 )
472 *va_arg( args, int64_t * ) = (int64_t)p_sys->i_pgc_length;
480 /* Special for access_demux */
481 case DEMUX_CAN_PAUSE:
483 case DEMUX_CAN_CONTROL_PACE:
485 *va_arg( args, bool * ) = true;
488 case DEMUX_SET_PAUSE_STATE:
491 case DEMUX_GET_TITLE_INFO:
492 ppp_title = va_arg( args, input_title_t*** );
493 *va_arg( args, int* ) = p_sys->i_title;
494 *va_arg( args, int* ) = 0; /* Title offset */
495 *va_arg( args, int* ) = 1; /* Chapter offset */
497 /* Duplicate title infos */
498 *ppp_title = malloc( p_sys->i_title * sizeof( input_title_t * ) );
499 for( i = 0; i < p_sys->i_title; i++ )
501 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
505 case DEMUX_SET_TITLE:
506 i = (int)va_arg( args, int );
507 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
508 != DVDNAV_STATUS_OK ) ||
509 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
510 != DVDNAV_STATUS_OK ) )
512 msg_Warn( p_demux, "cannot set title/chapter" );
515 p_demux->info.i_update |=
516 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
517 p_demux->info.i_title = i;
518 p_demux->info.i_seekpoint = 0;
521 case DEMUX_SET_SEEKPOINT:
522 i = va_arg( args, int );
523 if( p_demux->info.i_title == 0 )
525 static const int argtab[] = {
534 enum { numargs = sizeof(argtab)/sizeof(int) };
535 if( (unsigned)i >= numargs || DVDNAV_STATUS_OK !=
536 dvdnav_menu_call(p_sys->dvdnav,argtab[i]) )
539 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
540 i + 1 ) != DVDNAV_STATUS_OK )
542 msg_Warn( p_demux, "cannot set title/chapter" );
545 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
546 p_demux->info.i_seekpoint = i;
549 case DEMUX_GET_PTS_DELAY:
550 *va_arg( args, int64_t * ) =
551 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
556 const char *title_name = NULL;
558 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
559 if( (NULL != title_name) && ('\0' != title_name[0]) )
561 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
562 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
568 case DEMUX_NAV_ACTIVATE:
570 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
572 ButtonUpdate( p_demux, true );
573 dvdnav_button_activate( p_sys->dvdnav, pci );
579 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
581 dvdnav_upper_button_select( p_sys->dvdnav, pci );
587 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
589 dvdnav_lower_button_select( p_sys->dvdnav, pci );
595 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
597 dvdnav_left_button_select( p_sys->dvdnav, pci );
601 case DEMUX_NAV_RIGHT:
603 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
605 dvdnav_right_button_select( p_sys->dvdnav, pci );
609 /* TODO implement others */
617 static int ControlInternal( demux_t *p_demux, int i_query, ... )
622 va_start( args, i_query );
623 i_result = Control( p_demux, i_query, args );
628 /*****************************************************************************
630 *****************************************************************************/
631 static int Demux( demux_t *p_demux )
633 demux_sys_t *p_sys = p_demux->p_sys;
635 uint8_t buffer[DVD_VIDEO_LB_LEN];
636 uint8_t *packet = buffer;
641 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
642 == DVDNAV_STATUS_ERR )
644 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
645 == DVDNAV_STATUS_ERR )
648 msg_Warn( p_demux, "cannot get next block (%s)",
649 dvdnav_err_to_string( p_sys->dvdnav ) );
650 if( p_demux->info.i_title == 0 )
652 msg_Dbg( p_demux, "jumping to first title" );
653 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
660 case DVDNAV_BLOCK_OK: /* mpeg block */
661 vlc_mutex_lock( &p_sys->still.lock );
662 vlc_timer_schedule( p_sys->still.timer, false, 0, 0 );
663 p_sys->still.b_enabled = false;
664 vlc_mutex_unlock( &p_sys->still.lock );
665 if( p_sys->b_reset_pcr )
667 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
668 p_sys->b_reset_pcr = false;
670 DemuxBlock( p_demux, packet, i_len );
671 if( p_sys->i_vobu_index > 0 )
673 if( p_sys->i_vobu_flush == p_sys->i_vobu_index )
674 DemuxForceStill( p_demux );
675 p_sys->i_vobu_index++;
679 case DVDNAV_NOP: /* Nothing */
680 msg_Dbg( p_demux, "DVDNAV_NOP" );
683 case DVDNAV_STILL_FRAME:
685 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
686 bool b_still_init = false;
688 vlc_mutex_lock( &p_sys->still.lock );
689 if( !p_sys->still.b_enabled )
691 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
692 msg_Dbg( p_demux, " - length=0x%x", event->length );
693 p_sys->still.b_enabled = true;
695 if( event->length != 0xff && p_sys->still.b_created )
697 mtime_t delay = event->length * CLOCK_FREQ;
698 vlc_timer_schedule( p_sys->still.timer, false, delay, 0 );
703 vlc_mutex_unlock( &p_sys->still.lock );
707 DemuxForceStill( p_demux );
708 p_sys->b_reset_pcr = true;
714 case DVDNAV_SPU_CLUT_CHANGE:
718 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
719 /* Update color lookup table (16 *uint32_t in packet) */
720 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
722 /* HACK to get the SPU tracks registered in the right order */
723 for( i = 0; i < 0x1f; i++ )
725 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
726 ESNew( p_demux, 0xbd20 + i );
732 case DVDNAV_SPU_STREAM_CHANGE:
734 dvdnav_spu_stream_change_event_t *event =
735 (dvdnav_spu_stream_change_event_t*)packet;
738 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
739 msg_Dbg( p_demux, " - physical_wide=%d",
740 event->physical_wide );
741 msg_Dbg( p_demux, " - physical_letterbox=%d",
742 event->physical_letterbox);
743 msg_Dbg( p_demux, " - physical_pan_scan=%d",
744 event->physical_pan_scan );
746 ESSubtitleUpdate( p_demux );
747 p_sys->b_spu_change = true;
749 /* HACK to get the SPU tracks registered in the right order */
750 for( i = 0; i < 0x1f; i++ )
752 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
753 ESNew( p_demux, 0xbd20 + i );
759 case DVDNAV_AUDIO_STREAM_CHANGE:
761 dvdnav_audio_stream_change_event_t *event =
762 (dvdnav_audio_stream_change_event_t*)packet;
763 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
764 msg_Dbg( p_demux, " - physical=%d", event->physical );
769 case DVDNAV_VTS_CHANGE:
775 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
776 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
777 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
778 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
781 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
783 for( i = 0; i < PS_TK_COUNT; i++ )
785 ps_track_t *tk = &p_sys->tk[i];
788 es_format_Clean( &tk->fmt );
789 if( tk->es ) es_out_Del( p_demux->out, tk->es );
794 uint32_t i_width, i_height;
795 if( dvdnav_get_video_resolution( p_sys->dvdnav,
796 &i_width, &i_height ) )
797 i_width = i_height = 0;
798 switch( dvdnav_get_video_aspect( p_sys->dvdnav ) )
801 p_sys->sar.i_num = 4 * i_height;
802 p_sys->sar.i_den = 3 * i_width;
805 p_sys->sar.i_num = 16 * i_height;
806 p_sys->sar.i_den = 9 * i_width;
809 p_sys->sar.i_num = 0;
810 p_sys->sar.i_den = 0;
814 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
815 &i_part ) == DVDNAV_STATUS_OK )
817 if( i_title >= 0 && i_title < p_sys->i_title &&
818 p_demux->info.i_title != i_title )
820 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
821 p_demux->info.i_title = i_title;
827 case DVDNAV_CELL_CHANGE:
832 dvdnav_cell_change_event_t *event =
833 (dvdnav_cell_change_event_t*)packet;
834 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
835 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
836 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
837 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
838 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
839 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
840 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
841 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
843 /* Store the lenght in time of the current PGC */
844 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
845 p_sys->i_vobu_index = 0;
846 p_sys->i_vobu_flush = 0;
848 /* FIXME is it correct or there is better way to know chapter change */
849 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
850 &i_part ) == DVDNAV_STATUS_OK )
852 if( i_title >= 0 && i_title < p_sys->i_title )
854 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
855 p_demux->info.i_title = i_title;
857 if( i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint )
859 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
860 p_demux->info.i_seekpoint = i_part - 1;
867 case DVDNAV_NAV_PACKET:
869 p_sys->i_vobu_index = 1;
870 p_sys->i_vobu_flush = 0;
872 /* Look if we have need to force a flush (and when) */
873 const pci_gi_t *p_pci_gi = &dvdnav_get_current_nav_pci( p_sys->dvdnav )->pci_gi;
874 if( p_pci_gi->vobu_se_e_ptm != 0 && p_pci_gi->vobu_se_e_ptm < p_pci_gi->vobu_e_ptm )
876 const dsi_gi_t *p_dsi_gi = &dvdnav_get_current_nav_dsi( p_sys->dvdnav )->dsi_gi;
877 if( p_dsi_gi->vobu_3rdref_ea != 0 )
878 p_sys->i_vobu_flush = p_dsi_gi->vobu_3rdref_ea;
879 else if( p_dsi_gi->vobu_2ndref_ea != 0 )
880 p_sys->i_vobu_flush = p_dsi_gi->vobu_2ndref_ea;
881 else if( p_dsi_gi->vobu_1stref_ea != 0 )
882 p_sys->i_vobu_flush = p_dsi_gi->vobu_1stref_ea;
886 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
888 /* A lot of thing to do here :
890 * - fetch pts (for time display)
893 DemuxBlock( p_demux, packet, i_len );
894 if( p_sys->b_spu_change )
896 ButtonUpdate( p_demux, false );
897 p_sys->b_spu_change = false;
902 case DVDNAV_STOP: /* EOF */
903 msg_Dbg( p_demux, "DVDNAV_STOP" );
906 dvdnav_free_cache_block( p_sys->dvdnav, packet );
910 case DVDNAV_HIGHLIGHT:
912 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
913 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
914 msg_Dbg( p_demux, " - display=%d", event->display );
915 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
916 ButtonUpdate( p_demux, false );
920 case DVDNAV_HOP_CHANNEL:
921 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
922 p_sys->i_vobu_index = 0;
923 p_sys->i_vobu_flush = 0;
924 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
928 msg_Dbg( p_demux, "DVDNAV_WAIT" );
931 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
938 dvdnav_wait_skip( p_sys->dvdnav );
939 p_sys->b_reset_pcr = true;
944 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
949 dvdnav_free_cache_block( p_sys->dvdnav, packet );
956 * FIXME: partiallyy duplicated from src/input/es_out.c
958 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
960 const iso639_lang_t *pl;
964 psz_lang = var_CreateGetString( p_demux, psz_var );
966 return strdup(LANGUAGE_DEFAULT);
968 /* XXX: we will use only the first value
969 * (and ignore other ones in case of a list) */
970 if( ( p = strchr( psz_lang, ',' ) ) )
973 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
975 if( *psz_lang == '\0' )
977 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
978 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
979 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
980 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
986 if( pl->psz_eng_name != NULL )
987 return strdup( pl->psz_iso639_1 );
989 return strdup(LANGUAGE_DEFAULT);
992 static void DemuxTitles( demux_t *p_demux )
994 demux_sys_t *p_sys = p_demux->p_sys;
1001 t = vlc_input_title_New();
1003 t->psz_name = strdup( "DVD Menu" );
1005 s = vlc_seekpoint_New();
1006 s->psz_name = strdup( "Resume" );
1007 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1009 s = vlc_seekpoint_New();
1010 s->psz_name = strdup( "Root" );
1011 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1013 s = vlc_seekpoint_New();
1014 s->psz_name = strdup( "Title" );
1015 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1017 s = vlc_seekpoint_New();
1018 s->psz_name = strdup( "Chapter" );
1019 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1021 s = vlc_seekpoint_New();
1022 s->psz_name = strdup( "Subtitle" );
1023 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1025 s = vlc_seekpoint_New();
1026 s->psz_name = strdup( "Audio" );
1027 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1029 s = vlc_seekpoint_New();
1030 s->psz_name = strdup( "Angle" );
1031 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1033 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1035 /* Find out number of titles/chapters */
1036 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
1039 msg_Err( p_demux, "This is probably an Arccos Protected DVD. This could take time..." );
1041 for( i = 1; i <= i_titles; i++ )
1044 uint64_t i_title_length;
1045 uint64_t *p_chapters_time;
1047 i_chapters = dvdnav_describe_title_chapters( p_sys->dvdnav, i,
1050 if( i_chapters < 1 )
1053 p_chapters_time = NULL;
1055 t = vlc_input_title_New();
1056 t->i_length = i_title_length * 1000 / 90;
1057 for( int j = 0; j < __MAX( i_chapters, 1 ); j++ )
1059 s = vlc_seekpoint_New();
1060 if( p_chapters_time )
1061 s->i_time_offset = p_chapters_time[j] * 1000 / 90;
1062 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1064 free( p_chapters_time );
1065 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1069 /*****************************************************************************
1071 *****************************************************************************/
1072 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1074 demux_sys_t *p_sys = p_demux->p_sys;
1075 int32_t i_title, i_part;
1077 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1079 dvdnav_highlight_area_t hl;
1083 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1084 != DVDNAV_STATUS_OK )
1086 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1090 b_button_ok = false;
1091 if( i_button > 0 && i_title == 0 )
1093 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1095 b_button_ok = DVDNAV_STATUS_OK ==
1096 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
1101 for( unsigned i = 0; i < 4; i++ )
1103 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1104 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1106 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1107 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1108 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1109 p_sys->palette[i][3] = i_alpha;
1112 vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1113 var_SetInteger( p_sys->p_input, "x-start", hl.sx );
1114 var_SetInteger( p_sys->p_input, "x-end", hl.ex );
1115 var_SetInteger( p_sys->p_input, "y-start", hl.sy );
1116 var_SetInteger( p_sys->p_input, "y-end", hl.ey );
1118 var_SetAddress( p_sys->p_input, "menu-palette", p_sys->palette );
1119 var_SetBool( p_sys->p_input, "highlight", true );
1121 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1125 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1126 i_button, i_title );
1129 vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1130 var_SetBool( p_sys->p_input, "highlight", false );
1132 vlc_global_unlock( VLC_HIGHLIGHT_MUTEX );
1135 static void ESSubtitleUpdate( demux_t *p_demux )
1137 demux_sys_t *p_sys = p_demux->p_sys;
1138 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1139 int32_t i_title, i_part;
1141 ButtonUpdate( p_demux, false );
1143 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1144 if( i_title > 0 ) return;
1146 if( i_spu >= 0 && i_spu <= 0x1f )
1148 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1150 ESNew( p_demux, 0xbd20 + i_spu );
1152 /* be sure to unselect it (reset) */
1153 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1157 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1161 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1163 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1166 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1173 /*****************************************************************************
1174 * DemuxBlock: demux a given block
1175 *****************************************************************************/
1176 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
1178 demux_sys_t *p_sys = p_demux->p_sys;
1182 int i_size = ps_pkt_size( p, len );
1183 if( i_size <= 0 || i_size > len )
1188 /* Create a block */
1189 block_t *p_pkt = block_Alloc( i_size );
1190 memcpy( p_pkt->p_buffer, p, i_size);
1192 /* Parse it and send it */
1193 switch( 0x100 | p[3] )
1201 msg_Warn( p_demux, "received a PSM packet" );
1203 else if( p[3] == 0xbb )
1205 msg_Warn( p_demux, "received a SYSTEM packet" );
1208 block_Release( p_pkt );
1215 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1217 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1218 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1220 block_Release( p_pkt );
1225 int i_id = ps_pkt_id( p_pkt );
1228 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1232 ESNew( p_demux, i_id );
1234 if( tk->b_seen && tk->es &&
1235 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1237 es_out_Send( p_demux->out, tk->es, p_pkt );
1241 block_Release( p_pkt );
1246 block_Release( p_pkt );
1259 /*****************************************************************************
1260 * Force still images to be displayed by sending EOS and stopping buffering.
1261 *****************************************************************************/
1262 static void DemuxForceStill( demux_t *p_demux )
1264 static const uint8_t buffer[] = {
1265 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
1267 0x00, 0x00, 0x01, 0xB7,
1269 DemuxBlock( p_demux, buffer, sizeof(buffer) );
1272 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
1275 /*****************************************************************************
1276 * ESNew: register a new elementary stream
1277 *****************************************************************************/
1278 static void ESNew( demux_t *p_demux, int i_id )
1280 demux_sys_t *p_sys = p_demux->p_sys;
1281 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1282 bool b_select = false;
1284 if( tk->b_seen ) return;
1286 if( ps_track_fill( tk, 0, i_id, NULL ) )
1288 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1293 if( tk->fmt.i_cat == VIDEO_ES )
1295 tk->fmt.video.i_sar_num = p_sys->sar.i_num;
1296 tk->fmt.video.i_sar_den = p_sys->sar.i_den;
1299 else if( tk->fmt.i_cat == AUDIO_ES )
1302 /* find the audio number PLEASE find another way */
1303 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1305 i_audio = i_id&0x07;
1307 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1311 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1313 i_audio = i_id&0x1f;
1315 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1317 i_audio = i_id&0x1f;
1321 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1322 if( i_lang != 0xffff )
1324 tk->fmt.psz_language = malloc( 3 );
1325 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1326 tk->fmt.psz_language[1] = (i_lang )&0xff;
1327 tk->fmt.psz_language[2] = 0;
1329 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1335 else if( tk->fmt.i_cat == SPU_ES )
1337 int32_t i_title, i_part;
1338 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1339 if( i_lang != 0xffff )
1341 tk->fmt.psz_language = malloc( 3 );
1342 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1343 tk->fmt.psz_language[1] = (i_lang )&0xff;
1344 tk->fmt.psz_language[2] = 0;
1348 tk->fmt.subs.spu.palette[0] = 0xBeef;
1349 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1350 16 * sizeof( uint32_t ) );
1352 /* We select only when we are not in the menu */
1353 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1355 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1361 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1364 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1368 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1371 /*****************************************************************************
1373 *****************************************************************************/
1374 static void StillTimer( void *p_data )
1376 demux_sys_t *p_sys = p_data;
1378 vlc_mutex_lock( &p_sys->still.lock );
1379 if( likely(p_sys->still.b_enabled) )
1381 p_sys->still.b_enabled = false;
1382 dvdnav_still_skip( p_sys->dvdnav );
1384 vlc_mutex_unlock( &p_sys->still.lock );
1387 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1388 vlc_value_t oldval, vlc_value_t val, void *p_data )
1390 demux_t *p_demux = p_data;
1391 demux_sys_t *p_sys = p_demux->p_sys;
1393 /* FIXME? PCI usage thread safe? */
1394 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1395 int x = val.coords.x;
1396 int y = val.coords.y;
1398 if( psz_var[6] == 'm' ) /* mouse-moved */
1399 dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1402 assert( psz_var[6] == 'c' ); /* mouse-clicked */
1404 ButtonUpdate( p_demux, true );
1405 dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1412 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1413 vlc_value_t oldval, vlc_value_t val, void *p_data )
1415 demux_t *p_demux = p_data;
1416 demux_sys_t *p_sys = p_demux->p_sys;
1418 if (val.i_int == INPUT_EVENT_VOUT)
1420 if( p_sys->p_vout != NULL )
1422 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
1423 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
1424 vlc_object_release( p_sys->p_vout );
1427 p_sys->p_vout = input_GetVout( (input_thread_t *)p_input );
1428 if( p_sys->p_vout != NULL )
1430 var_AddCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
1431 var_AddCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
1434 (void) psz_var; (void) oldval;
1438 /*****************************************************************************
1439 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1440 *****************************************************************************/
1441 static int ProbeDVD( const char *psz_name )
1444 /* Triggers libdvdcss autodetection */
1447 int fd = vlc_open( psz_name, O_RDONLY | O_NONBLOCK );
1449 #ifdef HAVE_FDOPENDIR
1450 return VLC_EGENERIC;
1452 return (errno == ENOENT) ? VLC_EGENERIC : VLC_SUCCESS;
1455 int ret = VLC_EGENERIC;
1456 struct stat stat_info;
1458 if( fstat( fd, &stat_info ) == -1 )
1460 if( !S_ISREG( stat_info.st_mode ) )
1462 if( S_ISDIR( stat_info.st_mode ) || S_ISBLK( stat_info.st_mode ) )
1463 ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1467 /* ISO 9660 volume descriptor */
1469 if( lseek( fd, 0x8000 + 1, SEEK_SET ) == -1
1470 || read( fd, iso_dsc, sizeof (iso_dsc) ) < (int)sizeof (iso_dsc)
1471 || memcmp( iso_dsc, "CD001\x01", 6 ) )
1474 /* Try to find the anchor (2 bytes at LBA 256) */
1477 if( lseek( fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1478 && read( fd, &anchor, 2 ) == 2
1479 && GetWLE( &anchor ) == 2 )
1480 ret = VLC_SUCCESS; /* Found a potential anchor */