1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * NOTA BENE: this module requires the linking against a library which is
25 * known to require licensing under the GNU General Public License version 2
26 * (or later). Therefore, the result of compiling this module will normally
27 * be subject to the terms of that later license.
28 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
43 #include <unistd.h> /* close() */
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_input.h>
48 #include <vlc_access.h>
49 #include <vlc_demux.h>
50 #include <vlc_charset.h>
52 #include <vlc_mouse.h>
53 #include <vlc_dialog.h>
54 #include <vlc_iso_lang.h>
56 /* FIXME we should find a better way than including that */
57 #include "../../src/text/iso-639_def.h"
60 #include <dvdnav/dvdnav.h>
61 /* Expose without patching headers */
62 dvdnav_status_t
dvdnav_jump_to_sector_by_time(dvdnav_t
*, uint64_t, int32_t);
64 #include "../demux/mpeg/pes.h"
65 #include "../demux/mpeg/ps.h"
66 #include "../demux/timestamps_filter.h"
68 #include "disc_helper.h"
70 /*****************************************************************************
72 *****************************************************************************/
73 #define ANGLE_TEXT N_("DVD angle")
74 #define ANGLE_LONGTEXT N_( \
75 "Default DVD angle." )
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 AccessDemuxOpen ( vlc_object_t
* );
85 static void Close( vlc_object_t
* );
87 static int DemuxOpen ( vlc_object_t
* );
90 set_shortname( N_("DVD with menus") )
91 set_description( N_("DVDnav Input") )
92 set_category( CAT_INPUT
)
93 set_subcategory( SUBCAT_INPUT_ACCESS
)
94 add_integer( "dvdnav-angle", 1, ANGLE_TEXT
,
95 ANGLE_LONGTEXT
, false )
96 add_bool( "dvdnav-menu", true,
97 MENU_TEXT
, MENU_LONGTEXT
, false )
98 set_capability( "access", 305 )
99 add_shortcut( "dvd", "dvdnav", "file" )
100 set_callbacks( AccessDemuxOpen
, Close
)
102 set_description( N_("DVDnav demuxer") )
103 set_category( CAT_INPUT
)
104 set_subcategory( SUBCAT_INPUT_DEMUX
)
105 set_capability( "demux", 5 )
106 set_callbacks( DemuxOpen
, Close
)
107 add_shortcut( "dvd", "iso" )
110 /* Shall we use libdvdnav's read ahead cache? */
112 #define DVD_READ_CACHE 0
114 #define DVD_READ_CACHE 1
117 #define BLOCK_FLAG_CELL_DISCONTINUITY (BLOCK_FLAG_PRIVATE_SHIFT << 1)
119 /*****************************************************************************
121 *****************************************************************************/
140 ps_track_t tk
[PS_TK_COUNT
];
143 vlc_mutex_t event_lock
;
146 /* palette for menus */
163 input_title_t
**title
;
168 /* length of program group chain */
169 vlc_tick_t i_pgc_length
;
173 vlc_mouse_t oldmouse
;
176 static int Control( demux_t
*, int, va_list );
177 static int Demux( demux_t
* );
178 static int DemuxBlock( demux_t
*, const uint8_t *, int );
179 static void DemuxForceStill( demux_t
* );
181 static void DemuxTitles( demux_t
* );
182 static void ESSubtitleUpdate( demux_t
* );
183 static void ButtonUpdate( demux_t
*, bool );
185 static void ESNew( demux_t
*, int );
186 static int ProbeDVD( const char * );
188 static char *DemuxGetLanguageCode( demux_t
*p_demux
, const char *psz_var
);
190 static int ControlInternal( demux_t
*, int, ... );
192 static void StillTimer( void * );
194 static void EventMouse( const vlc_mouse_t
*mouse
, void *p_data
);
196 #if DVDNAV_VERSION >= 60100
197 static void DvdNavLog( void *foo
, dvdnav_logger_level_t i
, const char *p
, va_list z
)
199 msg_GenericVa( (demux_t
*)foo
, i
, p
, z
);
203 /*****************************************************************************
205 *****************************************************************************/
208 DVDMenuID_t dvdnav_id
;
209 const char *psz_name
;
210 } menus_id_mapping
[] = {
211 { DVD_MENU_Escape
, "Resume" },
212 { DVD_MENU_Root
, "Root" },
213 { DVD_MENU_Title
, "Title" },
214 { DVD_MENU_Part
, "Chapter" },
215 { DVD_MENU_Subpicture
, "Subtitle" },
216 { DVD_MENU_Audio
, "Audio" },
217 { DVD_MENU_Angle
, "Angle" },
220 static int MenuIDToSeekpoint( DVDMenuID_t menuid
, int *seekpoint
)
222 for( size_t i
=0; i
<ARRAY_SIZE(menus_id_mapping
); i
++ )
224 if( menus_id_mapping
[i
].dvdnav_id
== menuid
)
233 static int SeekpointToMenuID( int seekpoint
, DVDMenuID_t
*id
)
235 if( (size_t) seekpoint
>= ARRAY_SIZE(menus_id_mapping
) )
237 *id
= menus_id_mapping
[seekpoint
].dvdnav_id
;
241 static int CallRootTitleMenu( dvdnav_t
*p_dvdnav
,
242 int *pi_title
, int *pi_seekpoint
)
244 const DVDMenuID_t menuids
[2] = { DVD_MENU_Title
, DVD_MENU_Root
};
245 for( int i
=0; i
<2; i
++ )
247 if( dvdnav_menu_call( p_dvdnav
, menuids
[i
] )
248 == DVDNAV_STATUS_OK
)
251 MenuIDToSeekpoint( menuids
[i
], pi_seekpoint
);
258 /*****************************************************************************
260 *****************************************************************************/
261 static int CommonOpen( vlc_object_t
*p_this
,
262 dvdnav_t
*p_dvdnav
, bool b_readahead
)
264 demux_t
*p_demux
= (demux_t
*)p_this
;
271 /* Fill p_demux field */
272 DEMUX_INIT_COMMON(); p_sys
= p_demux
->p_sys
;
273 p_sys
->dvdnav
= p_dvdnav
;
274 p_sys
->p_tf_out
= timestamps_filter_es_out_New( p_demux
->out
);
275 if( !p_sys
->p_tf_out
)
281 ps_track_init( p_sys
->tk
);
282 p_sys
->b_readahead
= b_readahead
;
283 vlc_mouse_Init( &p_sys
->oldmouse
);
285 /* Configure dvdnav */
286 if( dvdnav_set_readahead_flag( p_sys
->dvdnav
, p_sys
->b_readahead
) !=
289 msg_Warn( p_demux
, "cannot set read-a-head flag" );
292 if( dvdnav_set_PGC_positioning_flag( p_sys
->dvdnav
, 1 ) !=
295 msg_Warn( p_demux
, "cannot set PGC positioning flag" );
298 /* Set menu language */
299 psz_code
= DemuxGetLanguageCode( p_demux
, "menu-language" );
300 if( dvdnav_menu_language_select( p_sys
->dvdnav
, psz_code
) !=
303 msg_Warn( p_demux
, "can't set menu language to '%s' (%s)",
304 psz_code
, dvdnav_err_to_string( p_sys
->dvdnav
) );
305 /* We try to fall back to 'en' */
306 if( strcmp( psz_code
, LANGUAGE_DEFAULT
) )
307 dvdnav_menu_language_select( p_sys
->dvdnav
, (char*)LANGUAGE_DEFAULT
);
311 /* Set audio language */
312 psz_code
= DemuxGetLanguageCode( p_demux
, "audio-language" );
313 if( dvdnav_audio_language_select( p_sys
->dvdnav
, psz_code
) !=
316 msg_Warn( p_demux
, "can't set audio language to '%s' (%s)",
317 psz_code
, dvdnav_err_to_string( p_sys
->dvdnav
) );
318 /* We try to fall back to 'en' */
319 if( strcmp( psz_code
, LANGUAGE_DEFAULT
) )
320 dvdnav_audio_language_select( p_sys
->dvdnav
, (char*)LANGUAGE_DEFAULT
);
324 /* Set spu language */
325 psz_code
= DemuxGetLanguageCode( p_demux
, "sub-language" );
326 if( dvdnav_spu_language_select( p_sys
->dvdnav
, psz_code
) !=
329 msg_Warn( p_demux
, "can't set spu language to '%s' (%s)",
330 psz_code
, dvdnav_err_to_string( p_sys
->dvdnav
) );
331 /* We try to fall back to 'en' */
332 if( strcmp( psz_code
, LANGUAGE_DEFAULT
) )
333 dvdnav_spu_language_select(p_sys
->dvdnav
, (char*)LANGUAGE_DEFAULT
);
337 DemuxTitles( p_demux
);
339 if( var_CreateGetBool( p_demux
, "dvdnav-menu" ) )
341 msg_Dbg( p_demux
, "trying to go to dvd menu" );
343 if( dvdnav_title_play( p_sys
->dvdnav
, 1 ) != DVDNAV_STATUS_OK
)
345 msg_Err( p_demux
, "cannot set title (can't decrypt DVD?)" );
346 vlc_dialog_display_error( p_demux
, _("Playback failure"), "%s",
347 _("VLC cannot set the DVD's title. It possibly "
348 "cannot decrypt the entire disc.") );
349 timestamps_filter_es_out_Delete( p_sys
->p_tf_out
);
354 if( CallRootTitleMenu( p_sys
->dvdnav
, &p_sys
->cur_title
,
355 &p_sys
->cur_seekpoint
) )
356 msg_Warn( p_demux
, "cannot go to dvd menu" );
359 i_angle
= var_CreateGetInteger( p_demux
, "dvdnav-angle" );
360 if( i_angle
<= 0 ) i_angle
= 1;
362 p_sys
->still
.b_enabled
= false;
363 vlc_mutex_init( &p_sys
->still
.lock
);
364 vlc_mutex_init( &p_sys
->event_lock
);
365 if( !vlc_timer_create( &p_sys
->still
.timer
, StillTimer
, p_sys
) )
366 p_sys
->still
.b_created
= true;
371 /*****************************************************************************
373 *****************************************************************************/
374 static int AccessDemuxOpen ( vlc_object_t
*p_this
)
376 demux_t
*p_demux
= (demux_t
*)p_this
;
377 dvdnav_t
*p_dvdnav
= NULL
;
378 char *psz_file
= NULL
;
379 const char *psz_path
= NULL
;
380 int i_ret
= VLC_EGENERIC
;
383 if (p_demux
->out
== NULL
)
386 if( !strncasecmp(p_demux
->psz_url
, "dvd", 3) )
389 if( !p_demux
->psz_filepath
|| !*p_demux
->psz_filepath
)
391 /* Only when selected */
395 psz_file
= var_InheritString( p_this
, "dvd" );
398 psz_file
= strdup( p_demux
->psz_filepath
);
400 #if defined( _WIN32 ) || defined( __OS2__ )
401 if( psz_file
!= NULL
)
403 /* Remove trailing backslash, otherwise dvdnav_open will fail */
404 size_t flen
= strlen( psz_file
);
405 if( flen
> 0 && psz_file
[flen
- 1] == '\\' )
406 psz_file
[flen
- 1] = '\0';
409 psz_file
= strdup("");
412 if( unlikely(psz_file
== NULL
) )
415 /* Try some simple probing to avoid going through dvdnav_open too often */
416 if( !forced
&& ProbeDVD( psz_file
) != VLC_SUCCESS
)
419 if( forced
&& DiscProbeMacOSPermission( p_this
, psz_file
) != VLC_SUCCESS
)
423 psz_path
= ToLocale( psz_file
);
424 #if DVDNAV_VERSION >= 60100
425 dvdnav_logger_cb cbs
;
426 cbs
.pf_log
= DvdNavLog
;
427 if( dvdnav_open2( &p_dvdnav
, p_demux
, &cbs
, psz_path
) != DVDNAV_STATUS_OK
)
429 if( dvdnav_open( &p_dvdnav
, psz_path
) != DVDNAV_STATUS_OK
)
432 msg_Warn( p_demux
, "cannot open DVD (%s)", psz_file
);
436 i_ret
= CommonOpen( p_this
, p_dvdnav
, !!DVD_READ_CACHE
);
437 if( i_ret
!= VLC_SUCCESS
)
438 dvdnav_close( p_dvdnav
);
443 LocaleFree( psz_path
);
447 /*****************************************************************************
448 * StreamProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
449 *****************************************************************************/
450 static int StreamProbeDVD( stream_t
*s
)
452 /* first sector should be filled with zeros */
454 const uint8_t *p_peek
;
455 i_peek
= vlc_stream_Peek( s
, &p_peek
, 2048 );
460 if (p_peek
[ --i_peek
]) {
465 /* ISO 9660 volume descriptor */
467 if( vlc_stream_Seek( s
, 0x8000 + 1 ) != VLC_SUCCESS
468 || vlc_stream_Read( s
, iso_dsc
, sizeof (iso_dsc
) ) < (int)sizeof (iso_dsc
)
469 || memcmp( iso_dsc
, "CD001\x01", 6 ) )
472 /* Try to find the anchor (2 bytes at LBA 256) */
475 if( vlc_stream_Seek( s
, 256 * DVD_VIDEO_LB_LEN
) == VLC_SUCCESS
476 && vlc_stream_Read( s
, &anchor
, 2 ) == 2
477 && GetWLE( &anchor
) == 2 )
483 /*****************************************************************************
484 * dvdnav stream callbacks
485 *****************************************************************************/
486 static int stream_cb_seek( void *demux
, uint64_t pos
)
488 return vlc_stream_Seek( ((demux_t
*)demux
)->s
, pos
);
491 static int stream_cb_read( void *demux
, void* buffer
, int size
)
493 return vlc_stream_Read( ((demux_t
*)demux
)->s
, buffer
, size
);
496 /*****************************************************************************
498 *****************************************************************************/
499 static int DemuxOpen ( vlc_object_t
*p_this
)
501 demux_t
*p_demux
= (demux_t
*)p_this
;
502 dvdnav_t
*p_dvdnav
= NULL
;
503 bool forced
= false, b_seekable
= false;
505 if( p_demux
->psz_name
!= NULL
&& !strncmp(p_demux
->psz_name
, "dvd", 3) )
508 /* StreamProbeDVD need FASTSEEK, but if dvd is forced, we don't probe thus
509 * don't need fastseek */
510 vlc_stream_Control( p_demux
->s
, forced
? STREAM_CAN_SEEK
: STREAM_CAN_FASTSEEK
,
515 /* Try some simple probing to avoid going through dvdnav_open too often */
516 if( !forced
&& StreamProbeDVD( p_demux
->s
) != VLC_SUCCESS
)
519 static dvdnav_stream_cb stream_cb
=
521 .pf_seek
= stream_cb_seek
,
522 .pf_read
= stream_cb_read
,
526 /* Open dvdnav with stream callbacks */
527 #if DVDNAV_VERSION >= 60100
528 dvdnav_logger_cb cbs
;
529 cbs
.pf_log
= DvdNavLog
;
530 if( dvdnav_open_stream2( &p_dvdnav
, p_demux
,
531 &cbs
, &stream_cb
) != DVDNAV_STATUS_OK
)
533 if( dvdnav_open_stream( &p_dvdnav
, p_demux
,
534 &stream_cb
) != DVDNAV_STATUS_OK
)
537 msg_Warn( p_demux
, "cannot open DVD with open_stream" );
541 int i_ret
= CommonOpen( p_this
, p_dvdnav
, false );
542 if( i_ret
!= VLC_SUCCESS
)
543 dvdnav_close( p_dvdnav
);
547 /*****************************************************************************
549 *****************************************************************************/
550 static void Close( vlc_object_t
*p_this
)
552 demux_t
*p_demux
= (demux_t
*)p_this
;
553 demux_sys_t
*p_sys
= p_demux
->p_sys
;
555 /* Stop still image handler */
556 if( p_sys
->still
.b_created
)
557 vlc_timer_destroy( p_sys
->still
.timer
);
559 for( int i
= 0; i
< PS_TK_COUNT
; i
++ )
561 ps_track_t
*tk
= &p_sys
->tk
[i
];
562 if( tk
->b_configured
)
564 es_format_Clean( &tk
->fmt
);
565 if( tk
->es
) es_out_Del( p_sys
->p_tf_out
, tk
->es
);
569 /* Free the array of titles */
570 for( int i
= 0; i
< p_sys
->i_title
; i
++ )
571 vlc_input_title_Delete( p_sys
->title
[i
] );
572 TAB_CLEAN( p_sys
->i_title
, p_sys
->title
);
574 timestamps_filter_es_out_Delete( p_sys
->p_tf_out
);
576 dvdnav_close( p_sys
->dvdnav
);
581 /*****************************************************************************
582 * Reset variables on Random Access:
583 *****************************************************************************/
584 static void RandomAccessCleanup(demux_sys_t
*p_sys
)
586 p_sys
->highlight
.b_pending
= false;
589 /*****************************************************************************
591 *****************************************************************************/
592 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
594 demux_sys_t
*p_sys
= p_demux
->p_sys
;
595 input_title_t
***ppp_title
;
600 case DEMUX_SET_POSITION
:
601 case DEMUX_GET_POSITION
:
602 case DEMUX_GET_LENGTH
:
605 if( dvdnav_get_position( p_sys
->dvdnav
, &pos
, &len
) !=
606 DVDNAV_STATUS_OK
|| len
== 0 )
613 case DEMUX_GET_POSITION
:
614 *va_arg( args
, double* ) = (double)pos
/ (double)len
;
617 case DEMUX_SET_POSITION
:
618 pos
= va_arg( args
, double ) * len
;
619 if( dvdnav_sector_search( p_sys
->dvdnav
, pos
, SEEK_SET
) ==
624 RandomAccessCleanup( p_sys
);
627 case DEMUX_GET_LENGTH
:
628 if( p_sys
->i_pgc_length
> 0 )
630 *va_arg( args
, vlc_tick_t
* ) = p_sys
->i_pgc_length
;
639 if( p_sys
->i_pgc_length
> 0 )
641 *va_arg( args
, vlc_tick_t
* ) =
642 dvdnav_get_current_time( p_sys
->dvdnav
) * 100 / 9;
649 vlc_tick_t i_time
= va_arg( args
, vlc_tick_t
);
650 if( dvdnav_jump_to_sector_by_time( p_sys
->dvdnav
,
652 SEEK_SET
) == DVDNAV_STATUS_OK
)
654 msg_Err( p_demux
, "can't set time to %" PRId64
, i_time
);
658 /* Special for access_demux */
659 case DEMUX_CAN_PAUSE
:
661 case DEMUX_CAN_CONTROL_PACE
:
663 *va_arg( args
, bool * ) = true;
666 case DEMUX_SET_PAUSE_STATE
:
669 case DEMUX_GET_TITLE_INFO
:
670 ppp_title
= va_arg( args
, input_title_t
*** );
672 /* Duplicate title infos */
673 *ppp_title
= vlc_alloc( p_sys
->i_title
, sizeof( input_title_t
* ) );
676 for( i
= 0; i
< p_sys
->i_title
; i
++ )
678 (*ppp_title
)[i
] = vlc_input_title_Duplicate( p_sys
->title
[i
] );
682 free( (*ppp_title
)[--i
] );
687 *va_arg( args
, int* ) = p_sys
->i_title
;
688 *va_arg( args
, int* ) = 0; /* Title offset */
689 *va_arg( args
, int* ) = 1; /* Chapter offset */
692 case DEMUX_SET_TITLE
:
693 i
= va_arg( args
, int );
694 if( i
== 0 && dvdnav_menu_call( p_sys
->dvdnav
, DVD_MENU_Root
)
695 != DVDNAV_STATUS_OK
)
697 msg_Warn( p_demux
, "cannot set title/chapter" );
703 dvdnav_still_skip( p_sys
->dvdnav
);
704 if( dvdnav_title_play( p_sys
->dvdnav
, i
) != DVDNAV_STATUS_OK
)
706 msg_Warn( p_demux
, "cannot set title/chapter" );
711 p_sys
->updates
|= INPUT_UPDATE_TITLE
| INPUT_UPDATE_SEEKPOINT
;
712 p_sys
->cur_title
= i
;
714 p_sys
->cur_seekpoint
= 0;
716 MenuIDToSeekpoint( DVD_MENU_Root
, &p_sys
->cur_seekpoint
);
717 RandomAccessCleanup( p_sys
);
720 case DEMUX_SET_SEEKPOINT
:
721 i
= va_arg( args
, int );
722 if( p_sys
->cur_title
== 0 )
725 if( SeekpointToMenuID(i
, &menuid
) ||
726 dvdnav_menu_call(p_sys
->dvdnav
, menuid
) != DVDNAV_STATUS_OK
)
729 else if( dvdnav_part_play( p_sys
->dvdnav
, p_sys
->cur_title
,
730 i
+ 1 ) != DVDNAV_STATUS_OK
)
732 msg_Warn( p_demux
, "cannot set title/chapter" );
735 p_sys
->updates
|= INPUT_UPDATE_SEEKPOINT
;
736 p_sys
->cur_seekpoint
= i
;
737 RandomAccessCleanup( p_sys
);
740 case DEMUX_TEST_AND_CLEAR_FLAGS
:
742 unsigned *restrict flags
= va_arg(args
, unsigned *);
743 *flags
&= p_sys
->updates
;
744 p_sys
->updates
&= ~*flags
;
748 case DEMUX_GET_TITLE
:
749 *va_arg( args
, int * ) = p_sys
->cur_title
;
752 case DEMUX_GET_SEEKPOINT
:
753 *va_arg( args
, int * ) = p_sys
->cur_seekpoint
;
756 case DEMUX_GET_PTS_DELAY
:
757 *va_arg( args
, vlc_tick_t
* ) =
758 VLC_TICK_FROM_MS( var_InheritInteger( p_demux
, "disc-caching" ) );
763 const char *title_name
= NULL
;
765 dvdnav_get_title_string(p_sys
->dvdnav
, &title_name
);
766 if( (NULL
!= title_name
) && ('\0' != title_name
[0]) && IsUTF8(title_name
) )
768 vlc_meta_t
*p_meta
= va_arg( args
, vlc_meta_t
* );
769 vlc_meta_Set( p_meta
, vlc_meta_Title
, title_name
);
775 case DEMUX_NAV_ACTIVATE
:
777 pci_t
*pci
= dvdnav_get_current_nav_pci( p_sys
->dvdnav
);
779 if( dvdnav_button_activate( p_sys
->dvdnav
, pci
) != DVDNAV_STATUS_OK
)
781 vlc_mutex_lock( &p_sys
->event_lock
);
782 ButtonUpdate( p_demux
, true );
783 vlc_mutex_unlock( &p_sys
->event_lock
);
789 pci_t
*pci
= dvdnav_get_current_nav_pci( p_sys
->dvdnav
);
791 if( dvdnav_upper_button_select( p_sys
->dvdnav
, pci
) != DVDNAV_STATUS_OK
)
798 pci_t
*pci
= dvdnav_get_current_nav_pci( p_sys
->dvdnav
);
800 if( dvdnav_lower_button_select( p_sys
->dvdnav
, pci
) != DVDNAV_STATUS_OK
)
807 pci_t
*pci
= dvdnav_get_current_nav_pci( p_sys
->dvdnav
);
809 if( dvdnav_left_button_select( p_sys
->dvdnav
, pci
) != DVDNAV_STATUS_OK
)
814 case DEMUX_NAV_RIGHT
:
816 pci_t
*pci
= dvdnav_get_current_nav_pci( p_sys
->dvdnav
);
818 if( dvdnav_right_button_select( p_sys
->dvdnav
, pci
) != DVDNAV_STATUS_OK
)
825 if( CallRootTitleMenu( p_sys
->dvdnav
, &p_sys
->cur_title
,
826 &p_sys
->cur_seekpoint
) )
828 msg_Warn( p_demux
, "cannot select Title/Root menu" );
831 p_sys
->updates
|= INPUT_UPDATE_TITLE
| INPUT_UPDATE_SEEKPOINT
;
837 *va_arg( args
, int* ) = ITEM_TYPE_DISC
;
841 /* TODO implement others */
849 static int ControlInternal( demux_t
*p_demux
, int i_query
, ... )
854 va_start( args
, i_query
);
855 i_result
= Control( p_demux
, i_query
, args
);
860 /*****************************************************************************
862 *****************************************************************************/
863 static int Demux( demux_t
*p_demux
)
865 demux_sys_t
*p_sys
= p_demux
->p_sys
;
867 uint8_t buffer
[DVD_VIDEO_LB_LEN
];
868 uint8_t *packet
= buffer
;
871 dvdnav_status_t status
;
873 if( p_sys
->b_readahead
)
874 status
= dvdnav_get_next_cache_block( p_sys
->dvdnav
, &packet
, &i_event
,
877 status
= dvdnav_get_next_block( p_sys
->dvdnav
, packet
, &i_event
,
879 if( status
== DVDNAV_STATUS_ERR
)
881 msg_Warn( p_demux
, "cannot get next block (%s)",
882 dvdnav_err_to_string( p_sys
->dvdnav
) );
883 if( p_sys
->cur_title
== 0 )
885 msg_Dbg( p_demux
, "jumping to first title" );
886 return ControlInternal( p_demux
, DEMUX_SET_TITLE
, 1 ) == VLC_SUCCESS
?
887 VLC_DEMUXER_SUCCESS
: VLC_DEMUXER_EGENERIC
;
889 return VLC_DEMUXER_EGENERIC
;
892 vlc_mutex_lock( &p_sys
->event_lock
);
893 if(p_sys
->highlight
.b_pending
)
894 ButtonUpdate(p_demux
, p_sys
->highlight
.b_from_user
);
895 vlc_mutex_unlock( &p_sys
->event_lock
);
899 case DVDNAV_BLOCK_OK
: /* mpeg block */
900 vlc_mutex_lock( &p_sys
->still
.lock
);
901 vlc_timer_disarm( p_sys
->still
.timer
);
902 p_sys
->still
.b_enabled
= false;
903 vlc_mutex_unlock( &p_sys
->still
.lock
);
904 if( p_sys
->b_reset_pcr
)
906 es_out_Control( p_sys
->p_tf_out
, ES_OUT_RESET_PCR
);
907 p_sys
->b_reset_pcr
= false;
909 DemuxBlock( p_demux
, packet
, i_len
);
910 if( p_sys
->i_vobu_index
> 0 )
912 if( p_sys
->i_vobu_flush
== p_sys
->i_vobu_index
)
913 DemuxForceStill( p_demux
);
914 p_sys
->i_vobu_index
++;
918 case DVDNAV_NOP
: /* Nothing */
919 msg_Dbg( p_demux
, "DVDNAV_NOP" );
922 case DVDNAV_STILL_FRAME
:
924 dvdnav_still_event_t
*event
= (dvdnav_still_event_t
*)packet
;
925 bool b_still_init
= false;
927 vlc_mutex_lock( &p_sys
->still
.lock
);
928 if( !p_sys
->still
.b_enabled
)
930 msg_Dbg( p_demux
, "DVDNAV_STILL_FRAME" );
931 msg_Dbg( p_demux
, " - length=0x%x", event
->length
);
932 p_sys
->still
.b_enabled
= true;
934 if( event
->length
!= 0xff && p_sys
->still
.b_created
)
936 vlc_tick_t delay
= vlc_tick_from_sec( event
->length
);
937 vlc_timer_schedule( p_sys
->still
.timer
, false, delay
, VLC_TIMER_FIRE_ONCE
);
942 vlc_mutex_unlock( &p_sys
->still
.lock
);
946 DemuxForceStill( p_demux
);
947 p_sys
->b_reset_pcr
= true;
949 vlc_tick_sleep( VLC_TICK_FROM_MS(40) );
953 case DVDNAV_SPU_CLUT_CHANGE
:
957 msg_Dbg( p_demux
, "DVDNAV_SPU_CLUT_CHANGE" );
958 /* Update color lookup table (16 *uint32_t in packet) */
959 memcpy( p_sys
->clut
, packet
, 16 * sizeof( uint32_t ) );
961 /* HACK to get the SPU tracks registered in the right order */
962 for( i
= 0; i
< 0x1f; i
++ )
964 if( dvdnav_spu_stream_to_lang( p_sys
->dvdnav
, i
) != 0xffff )
965 ESNew( p_demux
, 0xbd20 + i
);
971 case DVDNAV_SPU_STREAM_CHANGE
:
973 dvdnav_spu_stream_change_event_t
*event
=
974 (dvdnav_spu_stream_change_event_t
*)packet
;
977 msg_Dbg( p_demux
, "DVDNAV_SPU_STREAM_CHANGE" );
978 msg_Dbg( p_demux
, " - physical_wide=%d",
979 event
->physical_wide
);
980 msg_Dbg( p_demux
, " - physical_letterbox=%d",
981 event
->physical_letterbox
);
982 msg_Dbg( p_demux
, " - physical_pan_scan=%d",
983 event
->physical_pan_scan
);
985 ESSubtitleUpdate( p_demux
);
986 p_sys
->b_spu_change
= true;
988 /* HACK to get the SPU tracks registered in the right order */
989 for( i
= 0; i
< 0x1f; i
++ )
991 if( dvdnav_spu_stream_to_lang( p_sys
->dvdnav
, i
) != 0xffff )
992 ESNew( p_demux
, 0xbd20 + i
);
998 case DVDNAV_AUDIO_STREAM_CHANGE
:
1000 dvdnav_audio_stream_change_event_t
*event
=
1001 (dvdnav_audio_stream_change_event_t
*)packet
;
1002 msg_Dbg( p_demux
, "DVDNAV_AUDIO_STREAM_CHANGE" );
1003 msg_Dbg( p_demux
, " - physical=%d", event
->physical
);
1008 case DVDNAV_VTS_CHANGE
:
1010 int32_t i_title
= 0;
1013 dvdnav_vts_change_event_t
*event
= (dvdnav_vts_change_event_t
*)packet
;
1014 msg_Dbg( p_demux
, "DVDNAV_VTS_CHANGE" );
1015 msg_Dbg( p_demux
, " - vtsN=%d", event
->new_vtsN
);
1016 msg_Dbg( p_demux
, " - domain=%d", event
->new_domain
);
1019 es_out_Control( p_sys
->p_tf_out
, ES_OUT_RESET_PCR
);
1021 for( int i
= 0; i
< PS_TK_COUNT
; i
++ )
1023 ps_track_t
*tk
= &p_sys
->tk
[i
];
1024 if( tk
->b_configured
)
1026 es_format_Clean( &tk
->fmt
);
1029 if( tk
->es
== p_sys
->spu_es
)
1031 vlc_mutex_lock( &p_sys
->event_lock
);
1032 p_sys
->spu_es
= NULL
;
1033 p_sys
->highlight
.b_pending
= false;
1034 vlc_mutex_unlock( &p_sys
->event_lock
);
1036 es_out_Del( p_sys
->p_tf_out
, tk
->es
);
1040 tk
->b_configured
= false;
1043 uint32_t i_width
, i_height
;
1044 if( dvdnav_get_video_resolution( p_sys
->dvdnav
,
1045 &i_width
, &i_height
) )
1046 i_width
= i_height
= 0;
1047 switch( dvdnav_get_video_aspect( p_sys
->dvdnav
) )
1050 p_sys
->sar
.i_num
= 4 * i_height
;
1051 p_sys
->sar
.i_den
= 3 * i_width
;
1054 p_sys
->sar
.i_num
= 16 * i_height
;
1055 p_sys
->sar
.i_den
= 9 * i_width
;
1058 p_sys
->sar
.i_num
= 0;
1059 p_sys
->sar
.i_den
= 0;
1063 if( dvdnav_current_title_info( p_sys
->dvdnav
, &i_title
,
1064 &i_part
) == DVDNAV_STATUS_OK
)
1066 if( i_title
>= 0 && i_title
< p_sys
->i_title
&&
1067 p_sys
->cur_title
!= i_title
)
1069 p_sys
->updates
|= INPUT_UPDATE_TITLE
;
1070 p_sys
->cur_title
= i_title
;
1076 case DVDNAV_CELL_CHANGE
:
1078 int32_t i_nav_title
= 0;
1079 int32_t i_nav_part
= 0;
1081 dvdnav_cell_change_event_t
*event
=
1082 (dvdnav_cell_change_event_t
*)packet
;
1083 msg_Dbg( p_demux
, "DVDNAV_CELL_CHANGE" );
1084 msg_Dbg( p_demux
, " - cellN=%d", event
->cellN
);
1085 msg_Dbg( p_demux
, " - pgN=%d", event
->pgN
);
1086 msg_Dbg( p_demux
, " - cell_length=%"PRId64
, event
->cell_length
);
1087 msg_Dbg( p_demux
, " - pg_length=%"PRId64
, event
->pg_length
);
1088 msg_Dbg( p_demux
, " - pgc_length=%"PRId64
, event
->pgc_length
);
1089 msg_Dbg( p_demux
, " - cell_start=%"PRId64
, event
->cell_start
);
1090 msg_Dbg( p_demux
, " - pg_start=%"PRId64
, event
->pg_start
);
1092 /* Store the length in time of the current PGC */
1093 p_sys
->i_pgc_length
= FROM_SCALE_NZ(event
->pgc_length
);
1094 p_sys
->i_vobu_index
= 0;
1095 p_sys
->i_vobu_flush
= 0;
1097 for( int i
=0; i
<PS_TK_COUNT
; i
++ )
1098 p_sys
->tk
[i
].i_next_block_flags
|= BLOCK_FLAG_CELL_DISCONTINUITY
;
1100 /* FIXME is it correct or there is better way to know chapter change */
1101 if( dvdnav_current_title_info( p_sys
->dvdnav
, &i_nav_title
,
1102 &i_nav_part
) == DVDNAV_STATUS_OK
)
1104 const int i_title
= p_sys
->cur_title
;
1105 const int i_seekpoint
= p_sys
->cur_seekpoint
;
1106 if( i_nav_title
> 0 && i_nav_title
< p_sys
->i_title
)
1108 p_sys
->cur_title
= i_nav_title
;
1109 if( i_nav_part
> 0 &&
1110 i_nav_part
<= p_sys
->title
[i_nav_title
]->i_seekpoint
)
1111 p_sys
->cur_seekpoint
= i_nav_part
- 1;
1112 else p_sys
->cur_seekpoint
= 0;
1114 else if( i_nav_title
== 0 ) /* in menus, i_part == menu id */
1116 if( MenuIDToSeekpoint( i_nav_part
, &p_sys
->cur_seekpoint
) )
1117 p_sys
->cur_seekpoint
= 0; /* non standard menu number, can't map back */
1119 p_sys
->cur_title
= 0;
1121 if( i_title
!= p_sys
->cur_title
)
1122 p_sys
->updates
|= INPUT_UPDATE_TITLE
;
1123 if( i_seekpoint
!= p_sys
->cur_seekpoint
)
1124 p_sys
->updates
|= INPUT_UPDATE_SEEKPOINT
;
1129 case DVDNAV_NAV_PACKET
:
1131 p_sys
->i_vobu_index
= 1;
1132 p_sys
->i_vobu_flush
= 0;
1134 /* Look if we have need to force a flush (and when) */
1135 const pci_t
*p_pci
= dvdnav_get_current_nav_pci( p_sys
->dvdnav
);
1136 if( unlikely(!p_pci
) )
1138 const pci_gi_t
*p_pci_gi
= &p_pci
->pci_gi
;
1139 if( p_pci_gi
->vobu_se_e_ptm
!= 0 && p_pci_gi
->vobu_se_e_ptm
< p_pci_gi
->vobu_e_ptm
)
1141 const dsi_t
*p_dsi
= dvdnav_get_current_nav_dsi( p_sys
->dvdnav
);
1142 if( unlikely(!p_dsi
) )
1144 const dsi_gi_t
*p_dsi_gi
= &p_dsi
->dsi_gi
;
1145 if( p_dsi_gi
->vobu_3rdref_ea
!= 0 )
1146 p_sys
->i_vobu_flush
= p_dsi_gi
->vobu_3rdref_ea
;
1147 else if( p_dsi_gi
->vobu_2ndref_ea
!= 0 )
1148 p_sys
->i_vobu_flush
= p_dsi_gi
->vobu_2ndref_ea
;
1149 else if( p_dsi_gi
->vobu_1stref_ea
!= 0 )
1150 p_sys
->i_vobu_flush
= p_dsi_gi
->vobu_1stref_ea
;
1154 msg_Dbg( p_demux
, "DVDNAV_NAV_PACKET" );
1156 /* A lot of thing to do here :
1158 * - fetch pts (for time display)
1161 DemuxBlock( p_demux
, packet
, i_len
);
1162 if( p_sys
->b_spu_change
)
1164 vlc_mutex_lock(&p_sys
->event_lock
);
1165 ButtonUpdate( p_demux
, false );
1166 vlc_mutex_unlock(&p_sys
->event_lock
);
1167 p_sys
->b_spu_change
= false;
1172 case DVDNAV_STOP
: /* EOF */
1173 msg_Dbg( p_demux
, "DVDNAV_STOP" );
1175 if( p_sys
->b_readahead
)
1176 dvdnav_free_cache_block( p_sys
->dvdnav
, packet
);
1177 return VLC_DEMUXER_EOF
;
1179 case DVDNAV_HIGHLIGHT
:
1181 dvdnav_highlight_event_t
*event
= (dvdnav_highlight_event_t
*)packet
;
1182 msg_Dbg( p_demux
, "DVDNAV_HIGHLIGHT" );
1183 msg_Dbg( p_demux
, " - display=%d", event
->display
);
1184 msg_Dbg( p_demux
, " - buttonN=%d", event
->buttonN
);
1185 vlc_mutex_lock(&p_sys
->event_lock
);
1186 ButtonUpdate( p_demux
, false );
1187 vlc_mutex_unlock(&p_sys
->event_lock
);
1191 case DVDNAV_HOP_CHANNEL
:
1192 msg_Dbg( p_demux
, "DVDNAV_HOP_CHANNEL" );
1193 p_sys
->i_vobu_index
= 0;
1194 p_sys
->i_vobu_flush
= 0;
1195 es_out_Control( p_sys
->p_tf_out
, ES_OUT_RESET_PCR
);
1199 msg_Dbg( p_demux
, "DVDNAV_WAIT" );
1202 es_out_Control( p_sys
->p_tf_out
, ES_OUT_GET_EMPTY
, &b_empty
);
1205 vlc_tick_sleep( VLC_TICK_FROM_MS(40) );
1209 dvdnav_wait_skip( p_sys
->dvdnav
);
1210 p_sys
->b_reset_pcr
= true;
1215 msg_Warn( p_demux
, "Unknown event (0x%x)", i_event
);
1219 if( p_sys
->b_readahead
)
1220 dvdnav_free_cache_block( p_sys
->dvdnav
, packet
);
1222 return VLC_DEMUXER_SUCCESS
;
1225 /* Get a 2 char code
1226 * FIXME: partiallyy duplicated from src/input/es_out.c
1228 static char *DemuxGetLanguageCode( demux_t
*p_demux
, const char *psz_var
)
1230 const iso639_lang_t
*pl
;
1234 psz_lang
= var_CreateGetString( p_demux
, psz_var
);
1236 return strdup(LANGUAGE_DEFAULT
);
1238 /* XXX: we will use only the first value
1239 * (and ignore other ones in case of a list) */
1240 if( ( p
= strchr( psz_lang
, ',' ) ) )
1243 for( pl
= p_languages
; pl
->psz_eng_name
!= NULL
; pl
++ )
1245 if( *psz_lang
== '\0' )
1247 if( !strcasecmp( pl
->psz_eng_name
, psz_lang
) ||
1248 !strcasecmp( pl
->psz_iso639_1
, psz_lang
) ||
1249 !strcasecmp( pl
->psz_iso639_2T
, psz_lang
) ||
1250 !strcasecmp( pl
->psz_iso639_2B
, psz_lang
) )
1256 if( pl
->psz_eng_name
!= NULL
)
1257 return strdup( pl
->psz_iso639_1
);
1259 return strdup(LANGUAGE_DEFAULT
);
1262 static void DemuxTitles( demux_t
*p_demux
)
1264 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1270 t
= vlc_input_title_New();
1271 t
->i_flags
= INPUT_TITLE_MENU
| INPUT_TITLE_INTERACTIVE
;
1272 t
->psz_name
= strdup( "DVD Menu" );
1274 for( size_t i
=0; i
<ARRAY_SIZE(menus_id_mapping
); i
++ )
1276 s
= vlc_seekpoint_New();
1279 s
->psz_name
= strdup( menus_id_mapping
[i
].psz_name
);
1280 TAB_APPEND( t
->i_seekpoint
, t
->seekpoint
, s
);
1283 TAB_APPEND( p_sys
->i_title
, p_sys
->title
, t
);
1285 /* Find out number of titles/chapters */
1286 dvdnav_get_number_of_titles( p_sys
->dvdnav
, &i_titles
);
1289 msg_Err( p_demux
, "This is probably an Arccos Protected DVD. This could take time..." );
1291 for( int i
= 1; i
<= i_titles
; i
++ )
1293 uint64_t i_title_length
;
1294 uint64_t *p_chapters_time
;
1296 int32_t i_chapters
= dvdnav_describe_title_chapters( p_sys
->dvdnav
, i
,
1299 if( i_chapters
< 1 )
1302 p_chapters_time
= NULL
;
1304 t
= vlc_input_title_New();
1305 t
->i_length
= FROM_SCALE_NZ(i_title_length
);
1306 for( int j
= 0; j
< __MAX( i_chapters
, 1 ); j
++ )
1308 s
= vlc_seekpoint_New();
1309 if( p_chapters_time
)
1312 s
->i_time_offset
= FROM_SCALE_NZ(p_chapters_time
[j
- 1]);
1314 s
->i_time_offset
= 0;
1316 TAB_APPEND( t
->i_seekpoint
, t
->seekpoint
, s
);
1318 free( p_chapters_time
);
1319 TAB_APPEND( p_sys
->i_title
, p_sys
->title
, t
);
1323 /*****************************************************************************
1325 *****************************************************************************/
1326 static void ButtonUpdate( demux_t
*p_demux
, bool b_mode
)
1328 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1329 int32_t i_title
, i_part
;
1332 p_sys
->highlight
.b_pending
= true;
1333 p_sys
->highlight
.b_from_user
= b_mode
;
1335 if( !p_sys
->spu_es
)
1338 if( dvdnav_current_title_info( p_sys
->dvdnav
, &i_title
, &i_part
)
1339 != DVDNAV_STATUS_OK
)
1342 dvdnav_highlight_area_t hl
;
1346 if( dvdnav_get_current_highlight( p_sys
->dvdnav
, &i_button
)
1347 != DVDNAV_STATUS_OK
)
1349 msg_Err( p_demux
, "dvdnav_get_current_highlight failed" );
1353 b_button_ok
= false;
1354 if( i_button
> 0 && i_title
== 0 )
1356 pci_t
*pci
= dvdnav_get_current_nav_pci( p_sys
->dvdnav
);
1358 b_button_ok
= DVDNAV_STATUS_OK
==
1359 dvdnav_get_highlight_area( pci
, i_button
, b_mode
, &hl
);
1364 vlc_spu_highlight_t spu_hl
= {
1374 for( unsigned i
= 0; i
< 4; i
++ )
1376 uint32_t i_yuv
= p_sys
->clut
[(hl
.palette
>>(16+i
*4))&0x0f];
1377 uint8_t i_alpha
= ( (hl
.palette
>>(i
*4))&0x0f ) * 0xff / 0xf;
1379 spu_hl
.palette
.palette
[i
][0] = (i_yuv
>> 16) & 0xff;
1380 spu_hl
.palette
.palette
[i
][1] = (i_yuv
>> 0) & 0xff;
1381 spu_hl
.palette
.palette
[i
][2] = (i_yuv
>> 8) & 0xff;
1382 spu_hl
.palette
.palette
[i
][3] = i_alpha
;
1385 i_ret
= es_out_Control( p_sys
->p_tf_out
, ES_OUT_SPU_SET_HIGHLIGHT
,
1386 p_sys
->spu_es
, &spu_hl
);
1390 i_ret
= es_out_Control( p_sys
->p_tf_out
, ES_OUT_SPU_SET_HIGHLIGHT
,
1391 p_sys
->spu_es
, NULL
);
1394 if( i_ret
== VLC_SUCCESS
)
1396 msg_Dbg( p_demux
, "menu highlight %s button=%d title=%d",
1397 b_button_ok
? "set" : "cleared",
1398 i_button
, i_title
);
1399 p_sys
->highlight
.b_pending
= false;
1403 static void ESSubtitleUpdate( demux_t
*p_demux
)
1405 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1406 int i_spu
= dvdnav_get_active_spu_stream( p_sys
->dvdnav
);
1407 int32_t i_title
, i_part
;
1409 vlc_mutex_lock(&p_sys
->event_lock
);
1410 ButtonUpdate( p_demux
, false );
1411 vlc_mutex_unlock(&p_sys
->event_lock
);
1413 if( dvdnav_current_title_info( p_sys
->dvdnav
, &i_title
, &i_part
)
1414 != DVDNAV_STATUS_OK
|| i_title
> 0 )
1417 /* dvdnav_get_active_spu_stream sets (in)visibility flag as 0xF0 */
1418 if( i_spu
>= 0 && i_spu
<= 0x1f )
1420 ps_track_t
*tk
= &p_sys
->tk
[ps_id_to_tk(0xbd20 + i_spu
)];
1422 ESNew( p_demux
, 0xbd20 + i_spu
);
1424 /* be sure to unselect it (reset) */
1427 es_out_Control( p_sys
->p_tf_out
, ES_OUT_SET_ES_STATE
, tk
->es
,
1431 es_out_Control( p_sys
->p_tf_out
, ES_OUT_SET_ES
, tk
->es
);
1433 if( tk
->fmt
.i_cat
== SPU_ES
)
1435 vlc_mutex_lock( &p_sys
->event_lock
);
1436 p_sys
->spu_es
= tk
->es
;
1437 ButtonUpdate( p_demux
, false );
1438 vlc_mutex_unlock( &p_sys
->event_lock
);
1444 for( i_spu
= 0; i_spu
<= 0x1F; i_spu
++ )
1446 ps_track_t
*tk
= &p_sys
->tk
[ps_id_to_tk(0xbd20 + i_spu
)];
1449 es_out_Control( p_sys
->p_tf_out
, ES_OUT_SET_ES_STATE
, tk
->es
,
1456 /*****************************************************************************
1457 * DemuxBlock: demux a given block
1458 *****************************************************************************/
1459 static int DemuxBlock( demux_t
*p_demux
, const uint8_t *p
, int len
)
1461 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1465 int i_size
= ps_pkt_size( p
, len
);
1466 if( i_size
<= 0 || i_size
> len
)
1471 /* Create a block */
1472 block_t
*p_pkt
= block_Alloc( i_size
);
1474 return VLC_EGENERIC
;
1475 memcpy( p_pkt
->p_buffer
, p
, i_size
);
1477 /* Parse it and send it */
1478 switch( 0x100 | p
[3] )
1486 msg_Warn( p_demux
, "received a PSM packet" );
1488 else if( p
[3] == 0xbb )
1490 msg_Warn( p_demux
, "received a SYSTEM packet" );
1493 block_Release( p_pkt
);
1500 if( !ps_pkt_parse_pack( p_pkt
->p_buffer
, p_pkt
->i_buffer
,
1501 &i_scr
, &i_mux_rate
) )
1503 es_out_SetPCR( p_sys
->p_tf_out
, i_scr
);
1504 if( i_mux_rate
> 0 ) p_sys
->i_mux_rate
= i_mux_rate
;
1506 block_Release( p_pkt
);
1511 int i_id
= ps_pkt_id( p_pkt
->p_buffer
, p_pkt
->i_buffer
);
1514 ps_track_t
*tk
= &p_sys
->tk
[ps_id_to_tk(i_id
)];
1516 if( !tk
->b_configured
)
1518 ESNew( p_demux
, i_id
);
1522 !ps_pkt_parse_pes( VLC_OBJECT(p_demux
), p_pkt
, tk
->i_skip
) )
1524 int i_next_block_flags
= tk
->i_next_block_flags
;
1525 tk
->i_next_block_flags
= 0;
1526 if( i_next_block_flags
& BLOCK_FLAG_CELL_DISCONTINUITY
)
1528 if( p_pkt
->i_dts
!= VLC_TICK_INVALID
)
1530 i_next_block_flags
&= ~BLOCK_FLAG_CELL_DISCONTINUITY
;
1531 i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
1533 else tk
->i_next_block_flags
= BLOCK_FLAG_CELL_DISCONTINUITY
;
1535 p_pkt
->i_flags
|= i_next_block_flags
;
1536 es_out_Send( p_sys
->p_tf_out
, tk
->es
, p_pkt
);
1540 tk
->i_next_block_flags
= 0;
1541 block_Release( p_pkt
);
1546 block_Release( p_pkt
);
1559 /*****************************************************************************
1560 * Force still images to be displayed by sending EOS and stopping buffering.
1561 *****************************************************************************/
1562 static void DemuxForceStill( demux_t
*p_demux
)
1564 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1566 static const uint8_t buffer
[] = {
1567 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
1569 0x00, 0x00, 0x01, 0xB7,
1571 DemuxBlock( p_demux
, buffer
, sizeof(buffer
) );
1574 es_out_Control( p_sys
->p_tf_out
, ES_OUT_GET_EMPTY
, &b_empty
);
1577 /*****************************************************************************
1578 * ESNew: register a new elementary stream
1579 *****************************************************************************/
1580 static void ESNew( demux_t
*p_demux
, int i_id
)
1582 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1583 ps_track_t
*tk
= &p_sys
->tk
[ps_id_to_tk(i_id
)];
1584 bool b_select
= false;
1585 int i_lang
= 0xffff;
1587 if( tk
->b_configured
) return;
1589 if( ps_track_fill( tk
, 0, i_id
, NULL
, 0, true ) )
1591 msg_Warn( p_demux
, "unknown codec for id=0x%x", i_id
);
1596 if( tk
->fmt
.i_cat
== VIDEO_ES
)
1598 tk
->fmt
.video
.i_sar_num
= p_sys
->sar
.i_num
;
1599 tk
->fmt
.video
.i_sar_den
= p_sys
->sar
.i_den
;
1602 else if( tk
->fmt
.i_cat
== AUDIO_ES
)
1605 /* find the audio number PLEASE find another way */
1606 if( (i_id
&0xbdf8) == 0xbd88 ) /* dts */
1608 i_audio
= i_id
&0x07;
1610 else if( (i_id
&0xbdf0) == 0xbd80 ) /* a52 */
1614 else if( (i_id
&0xbdf0) == 0xbda0 ) /* lpcm */
1616 i_audio
= i_id
&0x1f;
1618 else if( ( i_id
&0xe0 ) == 0xc0 ) /* mpga */
1620 i_audio
= i_id
&0x1f;
1624 i_lang
= dvdnav_audio_stream_to_lang( p_sys
->dvdnav
, i_audio
);
1625 if( dvdnav_get_active_audio_stream( p_sys
->dvdnav
) == i_audio
)
1631 else if( tk
->fmt
.i_cat
== SPU_ES
)
1633 int32_t i_title
, i_part
;
1634 i_lang
= dvdnav_spu_stream_to_lang( p_sys
->dvdnav
, i_id
&0x1f );
1637 tk
->fmt
.subs
.spu
.palette
[0] = SPU_PALETTE_DEFINED
;
1638 memcpy( &tk
->fmt
.subs
.spu
.palette
[1], p_sys
->clut
,
1639 16 * sizeof( uint32_t ) );
1641 /* We select only when we are not in the menu */
1642 if( dvdnav_current_title_info( p_sys
->dvdnav
, &i_title
, &i_part
) == DVDNAV_STATUS_OK
&&
1644 dvdnav_get_active_spu_stream( p_sys
->dvdnav
) == (i_id
&0x1f) )
1650 if( i_lang
!= 0xffff )
1652 tk
->fmt
.psz_language
= malloc( 3 );
1653 if( tk
->fmt
.psz_language
)
1655 tk
->fmt
.psz_language
[0] = (i_lang
>> 8)&0xff;
1656 tk
->fmt
.psz_language
[1] = (i_lang
)&0xff;
1657 tk
->fmt
.psz_language
[2] = 0;
1661 tk
->fmt
.i_id
= i_id
;
1662 tk
->es
= es_out_Add( p_sys
->p_tf_out
, &tk
->fmt
);
1663 if( b_select
&& tk
->es
)
1665 es_out_Control( p_sys
->p_tf_out
, ES_OUT_SET_ES
, tk
->es
);
1667 if( tk
->fmt
.i_cat
== VIDEO_ES
)
1669 es_out_Control( p_sys
->p_tf_out
, ES_OUT_VOUT_SET_MOUSE_EVENT
, tk
->es
,
1670 EventMouse
, p_demux
);
1671 vlc_mutex_lock( &p_sys
->event_lock
);
1672 ButtonUpdate( p_demux
, false );
1673 vlc_mutex_unlock( &p_sys
->event_lock
);
1676 tk
->b_configured
= true;
1679 /*****************************************************************************
1681 *****************************************************************************/
1682 static void StillTimer( void *p_data
)
1684 demux_sys_t
*p_sys
= p_data
;
1686 vlc_mutex_lock( &p_sys
->still
.lock
);
1687 if( likely(p_sys
->still
.b_enabled
) )
1689 p_sys
->still
.b_enabled
= false;
1690 dvdnav_still_skip( p_sys
->dvdnav
);
1692 vlc_mutex_unlock( &p_sys
->still
.lock
);
1695 static void EventMouse( const vlc_mouse_t
*newmouse
, void *p_data
)
1697 demux_t
*p_demux
= p_data
;
1698 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1702 vlc_mouse_Init( &p_sys
->oldmouse
);
1706 /* FIXME? PCI usage thread safe? */
1707 pci_t
*pci
= dvdnav_get_current_nav_pci( p_sys
->dvdnav
);
1709 if( vlc_mouse_HasMoved( &p_sys
->oldmouse
, newmouse
) )
1710 dvdnav_mouse_select( p_sys
->dvdnav
, pci
, newmouse
->i_x
, newmouse
->i_y
);
1711 if( vlc_mouse_HasPressed( &p_sys
->oldmouse
, newmouse
, MOUSE_BUTTON_LEFT
) )
1713 vlc_mutex_lock( &p_sys
->event_lock
);
1714 ButtonUpdate( p_demux
, true );
1715 vlc_mutex_unlock( &p_sys
->event_lock
);
1716 dvdnav_mouse_activate( p_sys
->dvdnav
, pci
, newmouse
->i_x
, newmouse
->i_y
);
1718 p_sys
->oldmouse
= *newmouse
;
1721 /*****************************************************************************
1722 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1723 *****************************************************************************/
1724 static int ProbeDVD( const char *psz_name
)
1727 /* Triggers libdvdcss autodetection */
1730 int fd
= vlc_open( psz_name
, O_RDONLY
| O_NONBLOCK
);
1732 #ifdef HAVE_FDOPENDIR
1733 return VLC_EGENERIC
;
1735 return (errno
== ENOENT
) ? VLC_EGENERIC
: VLC_SUCCESS
;
1738 int ret
= VLC_EGENERIC
;
1739 struct stat stat_info
;
1741 if( fstat( fd
, &stat_info
) == -1 )
1743 if( !S_ISREG( stat_info
.st_mode
) )
1745 if( S_ISDIR( stat_info
.st_mode
) || S_ISBLK( stat_info
.st_mode
) )
1746 ret
= VLC_SUCCESS
; /* Let dvdnav_open() do the probing */
1750 /* ISO 9660 volume descriptor */
1752 if( lseek( fd
, 0x8000 + 1, SEEK_SET
) == -1
1753 || read( fd
, iso_dsc
, sizeof (iso_dsc
) ) < (int)sizeof (iso_dsc
)
1754 || memcmp( iso_dsc
, "CD001\x01", 6 ) )
1757 /* Try to find the anchor (2 bytes at LBA 256) */
1760 if( lseek( fd
, 256 * DVD_VIDEO_LB_LEN
, SEEK_SET
) != -1
1761 && read( fd
, &anchor
, 2 ) == 2
1762 && GetWLE( &anchor
) == 2 )
1763 ret
= VLC_SUCCESS
; /* Found a potential anchor */