1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004 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
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_input.h>
35 #include <vlc_access.h>
36 #include <vlc_demux.h>
37 #include <vlc_charset.h>
39 #include <vlc_dialog.h>
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.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 CACHING_TEXT N_("Caching value in ms")
73 #define CACHING_LONGTEXT N_( \
74 "Caching value for DVDs. This "\
75 "value should be set in milliseconds." )
76 #define MENU_TEXT N_("Start directly in menu")
77 #define MENU_LONGTEXT N_( \
78 "Start the DVD directly in the main menu. This "\
79 "will try to skip all the useless warning introductions." )
81 #define LANGUAGE_DEFAULT ("en")
83 static int Open ( vlc_object_t * );
84 static void Close( vlc_object_t * );
87 set_shortname( N_("DVD with menus") )
88 set_description( N_("DVDnav Input") )
89 set_category( CAT_INPUT )
90 set_subcategory( SUBCAT_INPUT_ACCESS )
91 add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
92 ANGLE_LONGTEXT, false )
93 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
94 CACHING_TEXT, CACHING_LONGTEXT, true )
95 add_bool( "dvdnav-menu", true, NULL,
96 MENU_TEXT, MENU_LONGTEXT, false )
97 set_capability( "access_demux", 5 )
99 add_shortcut( "dvdnav" )
100 add_shortcut( "file" )
101 set_callbacks( Open, Close )
104 /* Shall we use libdvdnav's read ahead cache? */
105 #define DVD_READ_CACHE 1
107 /*****************************************************************************
109 *****************************************************************************/
125 static void* EventThread( vlc_object_t * );
135 ps_track_t tk[PS_TK_COUNT];
138 /* for spu variables */
139 input_thread_t *p_input;
142 vlc_object_t *p_vout;
143 event_thread_t *p_ev;
145 /* palette for menus */
147 uint8_t palette[4][4];
154 input_title_t **title;
156 /* lenght of program group chain */
157 mtime_t i_pgc_length;
160 static int Control( demux_t *, int, va_list );
161 static int Demux( demux_t * );
162 static int DemuxBlock( demux_t *, const uint8_t *, int );
164 static void DemuxTitles( demux_t * );
165 static void ESSubtitleUpdate( demux_t * );
166 static void ButtonUpdate( demux_t *, bool );
168 static void ESNew( demux_t *, int );
169 static int ProbeDVD( demux_t *, char * );
171 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
173 static int ControlInternal( demux_t *, int, ... );
175 static int EventKey( vlc_object_t *, char const *,
176 vlc_value_t, vlc_value_t, void * );
177 static int EventMouse( vlc_object_t *, char const *,
178 vlc_value_t, vlc_value_t, void * );
179 static int EventIntf( vlc_object_t *, char const *,
180 vlc_value_t, vlc_value_t, void * );
182 /*****************************************************************************
184 *****************************************************************************/
185 static int Open( vlc_object_t *p_this )
187 demux_t *p_demux = (demux_t*)p_this;
195 if( !p_demux->psz_path || !*p_demux->psz_path )
197 /* Only when selected */
198 if( !p_this->b_force ) return VLC_EGENERIC;
200 psz_name = var_CreateGetString( p_this, "dvd" );
203 psz_name = strdup("");
207 psz_name = ToLocaleDup( p_demux->psz_path );
210 if( psz_name[0] && psz_name[1] == ':' &&
211 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
214 /* Try some simple probing to avoid going through dvdnav_open too often */
215 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
222 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
224 msg_Warn( p_demux, "cannot open dvdnav" );
230 /* Fill p_demux field */
231 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
232 p_sys->dvdnav = p_dvdnav;
233 p_sys->b_reset_pcr = false;
235 ps_track_init( p_sys->tk );
236 p_sys->i_aspect = -1;
237 p_sys->i_mux_rate = 0;
238 p_sys->i_pgc_length = 0;
239 p_sys->b_spu_change = false;
243 // Hack for libdvdnav CVS.
244 // Without it dvdnav_get_number_of_titles() fails.
245 // Remove when fixed in libdvdnav CVS.
246 uint8_t buffer[DVD_VIDEO_LB_LEN];
249 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
250 == DVDNAV_STATUS_ERR )
252 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
255 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
258 /* Configure dvdnav */
259 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
262 msg_Warn( p_demux, "cannot set read-a-head flag" );
265 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
268 msg_Warn( p_demux, "cannot set PGC positioning flag" );
272 * XXX A menu-language may be better than sub-language */
273 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
274 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
277 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
278 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
279 /* We try to fall back to 'en' */
280 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
281 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
285 /* Set audio language */
286 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
287 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
290 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
291 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
292 /* We try to fall back to 'en' */
293 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
294 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
298 /* Set spu language */
299 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
300 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
303 msg_Warn( p_demux, "can't set spu 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_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
311 DemuxTitles( p_demux );
313 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
314 var_Get( p_demux, "dvdnav-menu", &val );
317 msg_Dbg( p_demux, "trying to go to dvd menu" );
319 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
321 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
322 dialog_Fatal( p_demux, _("Playback failure"),
323 _("VLC cannot set the DVD's title. It possibly "
324 "cannot decrypt the entire disc.") );
325 dvdnav_close( p_sys->dvdnav );
330 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
333 /* Try going to menu root */
334 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
336 msg_Warn( p_demux, "cannot go to dvd menu" );
340 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
341 var_Get( p_demux, "dvdnav-angle", &val );
342 i_angle = val.i_int > 0 ? val.i_int : 1;
344 /* Update default_pts to a suitable value for dvdnav access */
345 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
347 /* FIXME hack hack hack hack FIXME */
348 /* Get p_input and create variable */
349 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
350 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
351 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
352 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
353 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
354 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
355 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
356 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
357 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
359 /* Now create our event thread catcher */
360 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
361 p_sys->p_ev->p_demux = p_demux;
362 vlc_mutex_init( &p_sys->p_ev->lock );
363 p_sys->p_ev->b_moved = false;
364 p_sys->p_ev->b_clicked = false;
365 p_sys->p_ev->b_still = false;
367 /* catch all key event */
368 var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
369 /* catch vout creation event */
370 var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_sys );
372 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
373 VLC_THREAD_PRIORITY_LOW );
378 /*****************************************************************************
380 *****************************************************************************/
381 static void Close( vlc_object_t *p_this )
383 demux_t *p_demux = (demux_t*)p_this;
384 demux_sys_t *p_sys = p_demux->p_sys;
387 var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_sys );
388 if( p_sys->p_vout != NULL )
389 { /* Should not happen, but better be safe than sorry. */
390 msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
391 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse,
393 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse,
397 /* stop the event handler */
398 vlc_object_kill( p_sys->p_ev );
399 var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
400 vlc_thread_join( p_sys->p_ev );
401 vlc_mutex_destroy( &p_sys->p_ev->lock );
402 vlc_object_release( p_sys->p_ev );
404 var_Destroy( p_sys->p_input, "highlight-mutex" );
405 var_Destroy( p_sys->p_input, "highlight" );
406 var_Destroy( p_sys->p_input, "x-start" );
407 var_Destroy( p_sys->p_input, "x-end" );
408 var_Destroy( p_sys->p_input, "y-start" );
409 var_Destroy( p_sys->p_input, "y-end" );
410 var_Destroy( p_sys->p_input, "color" );
411 var_Destroy( p_sys->p_input, "menu-palette" );
413 vlc_object_release( p_sys->p_input );
415 for( i = 0; i < PS_TK_COUNT; i++ )
417 ps_track_t *tk = &p_sys->tk[i];
420 es_format_Clean( &tk->fmt );
421 if( tk->es ) es_out_Del( p_demux->out, tk->es );
425 dvdnav_close( p_sys->dvdnav );
429 /*****************************************************************************
431 *****************************************************************************/
432 static int Control( demux_t *p_demux, int i_query, va_list args )
434 demux_sys_t *p_sys = p_demux->p_sys;
438 input_title_t ***ppp_title;
444 case DEMUX_SET_POSITION:
445 case DEMUX_GET_POSITION:
447 case DEMUX_GET_LENGTH:
450 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
451 DVDNAV_STATUS_OK || len == 0 )
456 if( i_query == DEMUX_GET_POSITION )
458 pf = (double*)va_arg( args, double* );
459 *pf = (double)pos / (double)len;
462 else if( i_query == DEMUX_SET_POSITION )
464 f = (double)va_arg( args, double );
466 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
472 else if( i_query == DEMUX_GET_TIME )
474 pi64 = (int64_t*)va_arg( args, int64_t * );
475 if( p_sys->i_pgc_length > 0 )
477 *pi64 = p_sys->i_pgc_length * pos / len;
481 else if( i_query == DEMUX_GET_LENGTH )
483 pi64 = (int64_t*)va_arg( args, int64_t * );
484 if( p_sys->i_pgc_length > 0 )
486 *pi64 = (int64_t)p_sys->i_pgc_length;
494 /* Special for access_demux */
495 case DEMUX_CAN_PAUSE:
497 case DEMUX_CAN_CONTROL_PACE:
499 pb = (bool*)va_arg( args, bool * );
503 case DEMUX_SET_PAUSE_STATE:
506 case DEMUX_GET_TITLE_INFO:
507 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
508 pi_int = (int*)va_arg( args, int* );
509 *((int*)va_arg( args, int* )) = 0; /* Title offset */
510 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
512 /* Duplicate title infos */
513 *pi_int = p_sys->i_title;
514 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
515 for( i = 0; i < p_sys->i_title; i++ )
517 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
521 case DEMUX_SET_TITLE:
522 i = (int)va_arg( args, int );
523 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
524 != DVDNAV_STATUS_OK ) ||
525 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
526 != DVDNAV_STATUS_OK ) )
528 msg_Warn( p_demux, "cannot set title/chapter" );
531 p_demux->info.i_update |=
532 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
533 p_demux->info.i_title = i;
534 p_demux->info.i_seekpoint = 0;
537 case DEMUX_SET_SEEKPOINT:
538 i = (int)va_arg( args, int );
539 if( p_demux->info.i_title == 0 )
546 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
549 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
552 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
555 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
558 i_ret = dvdnav_menu_call( p_sys->dvdnav,
559 DVD_MENU_Subpicture );
562 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
565 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
571 if( i_ret != DVDNAV_STATUS_OK )
574 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
575 i + 1 ) != DVDNAV_STATUS_OK )
577 msg_Warn( p_demux, "cannot set title/chapter" );
580 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
581 p_demux->info.i_seekpoint = i;
584 case DEMUX_GET_PTS_DELAY:
585 pi64 = (int64_t*)va_arg( args, int64_t * );
586 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
591 const char *title_name = NULL;
593 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
594 if( (NULL != title_name) && ('\0' != title_name[0]) )
596 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
597 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
603 /* TODO implement others */
609 static int ControlInternal( demux_t *p_demux, int i_query, ... )
614 va_start( args, i_query );
615 i_result = Control( p_demux, i_query, args );
620 /*****************************************************************************
622 *****************************************************************************/
623 static int Demux( demux_t *p_demux )
625 demux_sys_t *p_sys = p_demux->p_sys;
627 uint8_t buffer[DVD_VIDEO_LB_LEN];
628 uint8_t *packet = buffer;
633 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
634 == DVDNAV_STATUS_ERR )
636 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
637 == DVDNAV_STATUS_ERR )
640 msg_Warn( p_demux, "cannot get next block (%s)",
641 dvdnav_err_to_string( p_sys->dvdnav ) );
642 if( p_demux->info.i_title == 0 )
644 msg_Dbg( p_demux, "jumping to first title" );
645 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
652 case DVDNAV_BLOCK_OK: /* mpeg block */
653 p_sys->p_ev->b_still = false;
654 if( p_sys->b_reset_pcr )
656 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
657 p_sys->b_reset_pcr = false;
659 DemuxBlock( p_demux, packet, i_len );
662 case DVDNAV_NOP: /* Nothing */
663 msg_Dbg( p_demux, "DVDNAV_NOP" );
666 case DVDNAV_STILL_FRAME:
668 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
669 bool b_still_init = false;
671 vlc_mutex_lock( &p_sys->p_ev->lock );
672 if( !p_sys->p_ev->b_still )
674 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
675 msg_Dbg( p_demux, " - length=0x%x", event->length );
676 p_sys->p_ev->b_still = true;
678 if( event->length == 0xff )
679 p_sys->p_ev->i_still_end = 0;
681 p_sys->p_ev->i_still_end = mdate() +
682 event->length * INT64_C(1000000);
684 vlc_mutex_unlock( &p_sys->p_ev->lock );
688 /* We send a dummy mpeg2 end of sequence to force still frame display */
689 static const uint8_t buffer[] = {
690 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
692 0x00, 0x00, 0x01, 0xB7,
694 DemuxBlock( p_demux, buffer, sizeof(buffer) );
697 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
698 p_sys->b_reset_pcr = true;
704 case DVDNAV_SPU_CLUT_CHANGE:
708 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
709 /* Update color lookup table (16 *uint32_t in packet) */
710 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
712 /* HACK to get the SPU tracks registered in the right order */
713 for( i = 0; i < 0x1f; i++ )
715 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
716 ESNew( p_demux, 0xbd20 + i );
722 case DVDNAV_SPU_STREAM_CHANGE:
724 dvdnav_spu_stream_change_event_t *event =
725 (dvdnav_spu_stream_change_event_t*)packet;
728 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
729 msg_Dbg( p_demux, " - physical_wide=%d",
730 event->physical_wide );
731 msg_Dbg( p_demux, " - physical_letterbox=%d",
732 event->physical_letterbox);
733 msg_Dbg( p_demux, " - physical_pan_scan=%d",
734 event->physical_pan_scan );
736 ESSubtitleUpdate( p_demux );
737 p_sys->b_spu_change = true;
739 /* HACK to get the SPU tracks registered in the right order */
740 for( i = 0; i < 0x1f; i++ )
742 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
743 ESNew( p_demux, 0xbd20 + i );
749 case DVDNAV_AUDIO_STREAM_CHANGE:
751 dvdnav_audio_stream_change_event_t *event =
752 (dvdnav_audio_stream_change_event_t*)packet;
753 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
754 msg_Dbg( p_demux, " - physical=%d", event->physical );
759 case DVDNAV_VTS_CHANGE:
765 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
766 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
767 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
768 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
770 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
771 /* TODO check if we always have VTS and CELL */
772 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
775 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
777 for( i = 0; i < PS_TK_COUNT; i++ )
779 ps_track_t *tk = &p_sys->tk[i];
782 es_format_Clean( &tk->fmt );
783 if( tk->es ) es_out_Del( p_demux->out, tk->es );
788 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
789 &i_part ) == DVDNAV_STATUS_OK )
791 if( i_title >= 0 && i_title < p_sys->i_title &&
792 p_demux->info.i_title != i_title )
794 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
795 p_demux->info.i_title = i_title;
801 case DVDNAV_CELL_CHANGE:
806 dvdnav_cell_change_event_t *event =
807 (dvdnav_cell_change_event_t*)packet;
808 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
809 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
810 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
811 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
812 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
813 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
814 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
815 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
817 /* Store the lenght in time of the current PGC */
818 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
820 /* FIXME is it correct or there is better way to know chapter change */
821 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
822 &i_part ) == DVDNAV_STATUS_OK )
824 if( i_title >= 0 && i_title < p_sys->i_title &&
825 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
826 p_demux->info.i_seekpoint != i_part - 1 )
828 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
829 p_demux->info.i_seekpoint = i_part - 1;
835 case DVDNAV_NAV_PACKET:
838 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
840 /* A lot of thing to do here :
842 * - fetch pts (for time display)
845 DemuxBlock( p_demux, packet, i_len );
846 if( p_sys->b_spu_change )
848 ButtonUpdate( p_demux, false );
849 p_sys->b_spu_change = false;
854 case DVDNAV_STOP: /* EOF */
855 msg_Dbg( p_demux, "DVDNAV_STOP" );
858 dvdnav_free_cache_block( p_sys->dvdnav, packet );
862 case DVDNAV_HIGHLIGHT:
864 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
865 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
866 msg_Dbg( p_demux, " - display=%d", event->display );
867 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
868 ButtonUpdate( p_demux, false );
872 case DVDNAV_HOP_CHANNEL:
873 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
874 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
878 msg_Dbg( p_demux, "DVDNAV_WAIT" );
881 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
888 dvdnav_wait_skip( p_sys->dvdnav );
889 p_sys->b_reset_pcr = true;
894 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
899 dvdnav_free_cache_block( p_sys->dvdnav, packet );
906 * FIXME: partiallyy duplicated from src/input/es_out.c
908 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
910 const iso639_lang_t *pl;
914 psz_lang = var_CreateGetString( p_demux, psz_var );
916 return strdup(LANGUAGE_DEFAULT);
918 /* XXX: we will use only the first value
919 * (and ignore other ones in case of a list) */
920 if( ( p = strchr( psz_lang, ',' ) ) )
923 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
925 if( *psz_lang == '\0' )
927 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
928 !strcasecmp( pl->psz_native_name, psz_lang ) ||
929 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
930 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
931 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
937 if( pl->psz_iso639_1 != NULL )
938 return strdup( pl->psz_iso639_1 );
940 return strdup(LANGUAGE_DEFAULT);
943 static void DemuxTitles( demux_t *p_demux )
945 demux_sys_t *p_sys = p_demux->p_sys;
952 t = vlc_input_title_New();
954 t->psz_name = strdup( "DVD Menu" );
956 s = vlc_seekpoint_New();
957 s->psz_name = strdup( "Resume" );
958 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
960 s = vlc_seekpoint_New();
961 s->psz_name = strdup( "Root" );
962 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
964 s = vlc_seekpoint_New();
965 s->psz_name = strdup( "Title" );
966 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
968 s = vlc_seekpoint_New();
969 s->psz_name = strdup( "Chapter" );
970 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
972 s = vlc_seekpoint_New();
973 s->psz_name = strdup( "Subtitle" );
974 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
976 s = vlc_seekpoint_New();
977 s->psz_name = strdup( "Audio" );
978 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
980 s = vlc_seekpoint_New();
981 s->psz_name = strdup( "Angle" );
982 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
984 TAB_APPEND( p_sys->i_title, p_sys->title, t );
986 /* Find out number of titles/chapters */
987 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
988 for( i = 1; i <= i_titles; i++ )
990 int32_t i_chapters = 0;
993 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
995 t = vlc_input_title_New();
996 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
998 s = vlc_seekpoint_New();
999 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1002 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1006 /*****************************************************************************
1008 *****************************************************************************/
1009 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1011 demux_sys_t *p_sys = p_demux->p_sys;
1013 int32_t i_title, i_part;
1015 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1017 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
1019 vlc_mutex_t *p_mutex = val.p_address;
1020 dvdnav_highlight_area_t hl;
1024 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1025 != DVDNAV_STATUS_OK )
1027 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1031 b_button_ok = false;
1032 if( i_button > 0 && i_title == 0 )
1034 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1036 b_button_ok = dvdnav_get_highlight_area( pci, i_button, b_mode, &hl ) == DVDNAV_STATUS_OK;
1041 for( i = 0; i < 4; i++ )
1043 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1044 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1046 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1047 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1048 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1049 p_sys->palette[i][3] = i_alpha;
1052 vlc_mutex_lock( p_mutex );
1053 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
1054 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
1055 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
1056 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
1058 val.p_address = (void *)p_sys->palette;
1059 var_Set( p_sys->p_input, "menu-palette", val );
1061 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
1062 vlc_mutex_unlock( p_mutex );
1064 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1068 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1069 i_button, i_title );
1072 vlc_mutex_lock( p_mutex );
1074 var_Set( p_sys->p_input, "highlight", val );
1075 vlc_mutex_unlock( p_mutex );
1080 static void ESSubtitleUpdate( demux_t *p_demux )
1082 demux_sys_t *p_sys = p_demux->p_sys;
1083 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1084 int32_t i_title, i_part;
1086 ButtonUpdate( p_demux, false );
1088 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1089 if( i_title > 0 ) return;
1091 if( i_spu >= 0 && i_spu <= 0x1f )
1093 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1095 ESNew( p_demux, 0xbd20 + i_spu );
1097 /* be sure to unselect it (reset) */
1098 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1102 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1106 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1108 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1111 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1118 /*****************************************************************************
1119 * DemuxBlock: demux a given block
1120 *****************************************************************************/
1121 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1123 demux_sys_t *p_sys = p_demux->p_sys;
1124 const uint8_t *p = pkt;
1126 while( p < &pkt[i_pkt] )
1128 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1135 /* Create a block */
1136 p_pkt = block_New( p_demux, i_size );
1137 memcpy( p_pkt->p_buffer, p, i_size);
1139 /* Parse it and send it */
1140 switch( 0x100 | p[3] )
1148 msg_Warn( p_demux, "received a PSM packet" );
1150 else if( p[3] == 0xbb )
1152 msg_Warn( p_demux, "received a SYSTEM packet" );
1155 block_Release( p_pkt );
1162 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1164 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1165 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1167 block_Release( p_pkt );
1172 int i_id = ps_pkt_id( p_pkt );
1175 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1179 ESNew( p_demux, i_id );
1181 if( tk->b_seen && tk->es &&
1182 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1184 es_out_Send( p_demux->out, tk->es, p_pkt );
1188 block_Release( p_pkt );
1193 block_Release( p_pkt );
1205 /*****************************************************************************
1206 * ESNew: register a new elementary stream
1207 *****************************************************************************/
1208 static void ESNew( demux_t *p_demux, int i_id )
1210 demux_sys_t *p_sys = p_demux->p_sys;
1211 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1212 bool b_select = false;
1214 if( tk->b_seen ) return;
1216 if( ps_track_fill( tk, 0, i_id ) )
1218 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1223 if( tk->fmt.i_cat == VIDEO_ES )
1225 switch( p_sys->i_aspect )
1227 case 1: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR; break;
1228 case 2: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3; break;
1229 case 3: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9; break;
1230 case 4: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 221 / 10; break;
1232 tk->fmt.video.i_aspect = 0;
1237 else if( tk->fmt.i_cat == AUDIO_ES )
1240 /* find the audio number PLEASE find another way */
1241 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1243 i_audio = i_id&0x07;
1245 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1249 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1251 i_audio = i_id&0x1f;
1253 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1255 i_audio = i_id&0x1f;
1259 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1260 if( i_lang != 0xffff )
1262 tk->fmt.psz_language = malloc( 3 );
1263 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1264 tk->fmt.psz_language[1] = (i_lang )&0xff;
1265 tk->fmt.psz_language[2] = 0;
1267 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1273 else if( tk->fmt.i_cat == SPU_ES )
1275 int32_t i_title, i_part;
1276 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1277 if( i_lang != 0xffff )
1279 tk->fmt.psz_language = malloc( 3 );
1280 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1281 tk->fmt.psz_language[1] = (i_lang )&0xff;
1282 tk->fmt.psz_language[2] = 0;
1286 tk->fmt.subs.spu.palette[0] = 0xBeef;
1287 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1288 16 * sizeof( uint32_t ) );
1290 /* We select only when we are not in the menu */
1291 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1293 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1299 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1302 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1306 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1309 /*****************************************************************************
1310 * Event handler code
1311 *****************************************************************************/
1312 static void* EventThread( vlc_object_t *p_this )
1314 event_thread_t *p_ev = (event_thread_t*)p_this;
1315 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1316 int canc = vlc_savecancel ();
1319 while( vlc_object_alive( p_ev ) )
1321 vlc_mutex_lock( &p_ev->lock );
1323 if( p_sys->p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1325 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1326 vlc_value_t valx, valy;
1328 var_Get( p_sys->p_vout, "mouse-x", &valx );
1329 var_Get( p_sys->p_vout, "mouse-y", &valy );
1333 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1336 if( p_ev->b_clicked )
1338 ButtonUpdate( p_ev->p_demux, true );
1339 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1343 p_ev->b_moved = false;
1344 p_ev->b_clicked = false;
1350 if( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() )
1352 p_ev->b_still = false;
1353 dvdnav_still_skip( p_sys->dvdnav );
1356 vlc_mutex_unlock( &p_ev->lock );
1361 vlc_restorecancel (canc);
1366 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1367 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1369 (void)p_this; (void)oldval; (void)newval; (void)p_data;
1370 event_thread_t *p_ev = p_data;
1371 vlc_mutex_lock( &p_ev->lock );
1372 if( psz_var[6] == 'c' )
1373 p_ev->b_clicked = true;
1374 else if( psz_var[6] == 'm' )
1375 p_ev->b_moved = true;
1376 vlc_mutex_unlock( &p_ev->lock );
1381 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1382 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1384 demux_t *p_demux = p_data;
1385 demux_sys_t *p_sys = p_demux->p_sys;
1387 /* FIXME: thread-safe ? */
1388 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1390 vlc_mutex_lock( &p_sys->p_ev->lock );
1391 switch( newval.i_int )
1393 case ACTIONID_NAV_LEFT:
1394 dvdnav_left_button_select( p_sys->dvdnav, pci );
1396 case ACTIONID_NAV_RIGHT:
1397 dvdnav_right_button_select( p_sys->dvdnav, pci );
1399 case ACTIONID_NAV_UP:
1400 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1402 case ACTIONID_NAV_DOWN:
1403 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1405 case ACTIONID_NAV_ACTIVATE:
1406 ButtonUpdate( p_demux, true );
1407 dvdnav_button_activate( p_sys->dvdnav, pci );
1410 vlc_mutex_unlock( &p_sys->p_ev->lock );
1412 (void)p_this; (void)psz_var; (void)oldval;
1416 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1417 vlc_value_t oldval, vlc_value_t val, void *p_data )
1419 demux_sys_t *p_sys = p_data;
1421 if (val.i_int == INPUT_EVENT_VOUT)
1423 vlc_object_t *p_vout;
1425 vlc_mutex_lock( &p_sys->p_ev->lock );
1426 p_vout = p_sys->p_vout;
1427 if( p_vout != NULL )
1429 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_sys->p_ev );
1430 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_sys->p_ev );
1431 vlc_object_release( p_vout );
1434 p_vout = (vlc_object_t *)input_GetVout( (input_thread_t *)p_input );
1435 p_sys->p_vout = p_vout;
1436 if( p_vout != NULL )
1438 var_AddCallback( p_vout, "mouse-moved", EventMouse,
1440 var_AddCallback( p_vout, "mouse-clicked", EventMouse,
1443 vlc_mutex_unlock( &p_sys->p_ev->lock );
1445 (void) psz_var; (void) oldval;
1449 /*****************************************************************************
1450 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1451 *****************************************************************************/
1452 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1455 #ifdef HAVE_SYS_STAT_H
1456 struct stat stat_info;
1457 uint8_t pi_anchor[2];
1462 /* Triggers libdvdcss autodetection */
1466 if( !strcmp( psz_name, "-" ) ) /* stdin -> file access */
1467 return VLC_EGENERIC;
1469 if( (i_fd = utf8_open( psz_name, O_RDONLY |O_NONBLOCK, 0666 )) == -1 )
1471 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1474 if( fstat( i_fd, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1478 if( S_ISFIFO( stat_info.st_mode ) )
1479 return VLC_EGENERIC;
1480 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1483 /* Try to find the anchor (2 bytes at LBA 256) */
1484 i_ret = VLC_EGENERIC;
1485 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1486 && read( i_fd, pi_anchor, 2 ) == 2
1487 && GetWLE( pi_anchor ) == 2 )
1488 i_ret = VLC_SUCCESS; /* Found a potential anchor */