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 *****************************************************************************/
126 static void* EventThread( vlc_object_t * );
136 ps_track_t tk[PS_TK_COUNT];
139 /* for spu variables */
140 input_thread_t *p_input;
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 * );
178 /*****************************************************************************
180 *****************************************************************************/
181 static int Open( vlc_object_t *p_this )
183 demux_t *p_demux = (demux_t*)p_this;
191 if( !p_demux->psz_path || !*p_demux->psz_path )
193 /* Only when selected */
194 if( !p_this->b_force ) return VLC_EGENERIC;
196 psz_name = var_CreateGetString( p_this, "dvd" );
199 psz_name = strdup("");
203 psz_name = ToLocaleDup( p_demux->psz_path );
206 if( psz_name[0] && psz_name[1] == ':' &&
207 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
210 /* Try some simple probing to avoid going through dvdnav_open too often */
211 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
218 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
220 msg_Warn( p_demux, "cannot open dvdnav" );
226 /* Fill p_demux field */
227 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
228 p_sys->dvdnav = p_dvdnav;
229 p_sys->b_reset_pcr = false;
231 ps_track_init( p_sys->tk );
232 p_sys->i_aspect = -1;
233 p_sys->i_mux_rate = 0;
234 p_sys->i_pgc_length = 0;
235 p_sys->b_spu_change = false;
239 // Hack for libdvdnav CVS.
240 // Without it dvdnav_get_number_of_titles() fails.
241 // Remove when fixed in libdvdnav CVS.
242 uint8_t buffer[DVD_VIDEO_LB_LEN];
245 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
246 == DVDNAV_STATUS_ERR )
248 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
251 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
254 /* Configure dvdnav */
255 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
258 msg_Warn( p_demux, "cannot set read-a-head flag" );
261 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
264 msg_Warn( p_demux, "cannot set PGC positioning flag" );
268 * XXX A menu-language may be better than sub-language */
269 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
270 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
273 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
274 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
275 /* We try to fall back to 'en' */
276 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
277 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
281 /* Set audio language */
282 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
283 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
286 msg_Warn( p_demux, "can't set audio 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_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
294 /* Set spu language */
295 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
296 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
299 msg_Warn( p_demux, "can't set spu 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_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
307 DemuxTitles( p_demux );
309 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
310 var_Get( p_demux, "dvdnav-menu", &val );
313 msg_Dbg( p_demux, "trying to go to dvd menu" );
315 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
317 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
318 dialog_Fatal( p_demux, _("Playback failure"),
319 _("VLC cannot set the DVD's title. It possibly "
320 "cannot decrypt the entire disc.") );
321 dvdnav_close( p_sys->dvdnav );
326 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
329 /* Try going to menu root */
330 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
332 msg_Warn( p_demux, "cannot go to dvd menu" );
336 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
337 var_Get( p_demux, "dvdnav-angle", &val );
338 i_angle = val.i_int > 0 ? val.i_int : 1;
340 /* Update default_pts to a suitable value for dvdnav access */
341 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
343 /* FIXME hack hack hack hack FIXME */
344 /* Get p_input and create variable */
345 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
346 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
347 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
348 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
349 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
350 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
351 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
352 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
353 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
355 /* Now create our event thread catcher */
356 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
357 p_sys->p_ev->p_demux = p_demux;
358 vlc_mutex_init( &p_sys->p_ev->lock );
359 p_sys->p_ev->b_moved = false;
360 p_sys->p_ev->b_clicked = false;
361 p_sys->p_ev->i_key_action = 0;
362 p_sys->p_ev->b_still = false;
364 /* catch all key event */
365 var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_sys->p_ev );
367 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
368 VLC_THREAD_PRIORITY_LOW );
373 /*****************************************************************************
375 *****************************************************************************/
376 static void Close( vlc_object_t *p_this )
378 demux_t *p_demux = (demux_t*)p_this;
379 demux_sys_t *p_sys = p_demux->p_sys;
382 /* stop the event handler */
383 vlc_object_kill( p_sys->p_ev );
384 var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_sys->p_ev );
385 vlc_thread_join( p_sys->p_ev );
386 vlc_mutex_destroy( &p_sys->p_ev->lock );
387 vlc_object_release( p_sys->p_ev );
389 var_Destroy( p_sys->p_input, "highlight-mutex" );
390 var_Destroy( p_sys->p_input, "highlight" );
391 var_Destroy( p_sys->p_input, "x-start" );
392 var_Destroy( p_sys->p_input, "x-end" );
393 var_Destroy( p_sys->p_input, "y-start" );
394 var_Destroy( p_sys->p_input, "y-end" );
395 var_Destroy( p_sys->p_input, "color" );
396 var_Destroy( p_sys->p_input, "menu-palette" );
398 vlc_object_release( p_sys->p_input );
400 for( i = 0; i < PS_TK_COUNT; i++ )
402 ps_track_t *tk = &p_sys->tk[i];
405 es_format_Clean( &tk->fmt );
406 if( tk->es ) es_out_Del( p_demux->out, tk->es );
410 dvdnav_close( p_sys->dvdnav );
414 /*****************************************************************************
416 *****************************************************************************/
417 static int Control( demux_t *p_demux, int i_query, va_list args )
419 demux_sys_t *p_sys = p_demux->p_sys;
423 input_title_t ***ppp_title;
429 case DEMUX_SET_POSITION:
430 case DEMUX_GET_POSITION:
432 case DEMUX_GET_LENGTH:
435 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
436 DVDNAV_STATUS_OK || len == 0 )
441 if( i_query == DEMUX_GET_POSITION )
443 pf = (double*)va_arg( args, double* );
444 *pf = (double)pos / (double)len;
447 else if( i_query == DEMUX_SET_POSITION )
449 f = (double)va_arg( args, double );
451 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
457 else if( i_query == DEMUX_GET_TIME )
459 pi64 = (int64_t*)va_arg( args, int64_t * );
460 if( p_sys->i_pgc_length > 0 )
462 *pi64 = p_sys->i_pgc_length * pos / len;
466 else if( i_query == DEMUX_GET_LENGTH )
468 pi64 = (int64_t*)va_arg( args, int64_t * );
469 if( p_sys->i_pgc_length > 0 )
471 *pi64 = (int64_t)p_sys->i_pgc_length;
479 /* Special for access_demux */
480 case DEMUX_CAN_PAUSE:
482 case DEMUX_CAN_CONTROL_PACE:
484 pb = (bool*)va_arg( args, bool * );
488 case DEMUX_SET_PAUSE_STATE:
491 case DEMUX_GET_TITLE_INFO:
492 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
493 pi_int = (int*)va_arg( args, int* );
494 *((int*)va_arg( args, int* )) = 0; /* Title offset */
495 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
497 /* Duplicate title infos */
498 *pi_int = p_sys->i_title;
499 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
500 for( i = 0; i < p_sys->i_title; i++ )
502 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
506 case DEMUX_SET_TITLE:
507 i = (int)va_arg( args, int );
508 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
509 != DVDNAV_STATUS_OK ) ||
510 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
511 != DVDNAV_STATUS_OK ) )
513 msg_Warn( p_demux, "cannot set title/chapter" );
516 p_demux->info.i_update |=
517 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
518 p_demux->info.i_title = i;
519 p_demux->info.i_seekpoint = 0;
522 case DEMUX_SET_SEEKPOINT:
523 i = (int)va_arg( args, int );
524 if( p_demux->info.i_title == 0 )
531 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
534 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
537 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
540 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
543 i_ret = dvdnav_menu_call( p_sys->dvdnav,
544 DVD_MENU_Subpicture );
547 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
550 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
556 if( i_ret != DVDNAV_STATUS_OK )
559 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
560 i + 1 ) != DVDNAV_STATUS_OK )
562 msg_Warn( p_demux, "cannot set title/chapter" );
565 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
566 p_demux->info.i_seekpoint = i;
569 case DEMUX_GET_PTS_DELAY:
570 pi64 = (int64_t*)va_arg( args, int64_t * );
571 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
576 const char *title_name = NULL;
578 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
579 if( (NULL != title_name) && ('\0' != title_name[0]) )
581 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
582 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
588 /* TODO implement others */
594 static int ControlInternal( demux_t *p_demux, int i_query, ... )
599 va_start( args, i_query );
600 i_result = Control( p_demux, i_query, args );
605 /*****************************************************************************
607 *****************************************************************************/
608 static int Demux( demux_t *p_demux )
610 demux_sys_t *p_sys = p_demux->p_sys;
612 uint8_t buffer[DVD_VIDEO_LB_LEN];
613 uint8_t *packet = buffer;
618 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
619 == DVDNAV_STATUS_ERR )
621 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
622 == DVDNAV_STATUS_ERR )
625 msg_Warn( p_demux, "cannot get next block (%s)",
626 dvdnav_err_to_string( p_sys->dvdnav ) );
627 if( p_demux->info.i_title == 0 )
629 msg_Dbg( p_demux, "jumping to first title" );
630 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
637 case DVDNAV_BLOCK_OK: /* mpeg block */
638 p_sys->p_ev->b_still = false;
639 if( p_sys->b_reset_pcr )
641 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
642 p_sys->b_reset_pcr = false;
644 DemuxBlock( p_demux, packet, i_len );
647 case DVDNAV_NOP: /* Nothing */
648 msg_Dbg( p_demux, "DVDNAV_NOP" );
651 case DVDNAV_STILL_FRAME:
653 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
654 bool b_still_init = false;
656 vlc_mutex_lock( &p_sys->p_ev->lock );
657 if( !p_sys->p_ev->b_still )
659 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
660 msg_Dbg( p_demux, " - length=0x%x", event->length );
661 p_sys->p_ev->b_still = true;
663 if( event->length == 0xff )
664 p_sys->p_ev->i_still_end = 0;
666 p_sys->p_ev->i_still_end = mdate() +
667 event->length * INT64_C(1000000);
669 vlc_mutex_unlock( &p_sys->p_ev->lock );
673 /* We send a dummy mpeg2 end of sequence to force still frame display */
674 static const uint8_t buffer[] = {
675 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
677 0x00, 0x00, 0x01, 0xB7,
679 DemuxBlock( p_demux, buffer, sizeof(buffer) );
682 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
683 p_sys->b_reset_pcr = true;
689 case DVDNAV_SPU_CLUT_CHANGE:
693 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
694 /* Update color lookup table (16 *uint32_t in packet) */
695 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
697 /* HACK to get the SPU tracks registered in the right order */
698 for( i = 0; i < 0x1f; i++ )
700 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
701 ESNew( p_demux, 0xbd20 + i );
707 case DVDNAV_SPU_STREAM_CHANGE:
709 dvdnav_spu_stream_change_event_t *event =
710 (dvdnav_spu_stream_change_event_t*)packet;
713 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
714 msg_Dbg( p_demux, " - physical_wide=%d",
715 event->physical_wide );
716 msg_Dbg( p_demux, " - physical_letterbox=%d",
717 event->physical_letterbox);
718 msg_Dbg( p_demux, " - physical_pan_scan=%d",
719 event->physical_pan_scan );
721 ESSubtitleUpdate( p_demux );
722 p_sys->b_spu_change = true;
724 /* HACK to get the SPU tracks registered in the right order */
725 for( i = 0; i < 0x1f; i++ )
727 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
728 ESNew( p_demux, 0xbd20 + i );
734 case DVDNAV_AUDIO_STREAM_CHANGE:
736 dvdnav_audio_stream_change_event_t *event =
737 (dvdnav_audio_stream_change_event_t*)packet;
738 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
739 msg_Dbg( p_demux, " - physical=%d", event->physical );
744 case DVDNAV_VTS_CHANGE:
750 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
751 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
752 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
753 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
755 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
756 /* TODO check if we always have VTS and CELL */
757 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
760 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
762 for( i = 0; i < PS_TK_COUNT; i++ )
764 ps_track_t *tk = &p_sys->tk[i];
767 es_format_Clean( &tk->fmt );
768 if( tk->es ) es_out_Del( p_demux->out, tk->es );
773 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
774 &i_part ) == DVDNAV_STATUS_OK )
776 if( i_title >= 0 && i_title < p_sys->i_title &&
777 p_demux->info.i_title != i_title )
779 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
780 p_demux->info.i_title = i_title;
786 case DVDNAV_CELL_CHANGE:
791 dvdnav_cell_change_event_t *event =
792 (dvdnav_cell_change_event_t*)packet;
793 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
794 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
795 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
796 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
797 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
798 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
799 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
800 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
802 /* Store the lenght in time of the current PGC */
803 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
805 /* FIXME is it correct or there is better way to know chapter change */
806 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
807 &i_part ) == DVDNAV_STATUS_OK )
809 if( i_title >= 0 && i_title < p_sys->i_title &&
810 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
811 p_demux->info.i_seekpoint != i_part - 1 )
813 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
814 p_demux->info.i_seekpoint = i_part - 1;
820 case DVDNAV_NAV_PACKET:
823 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
825 /* A lot of thing to do here :
827 * - fetch pts (for time display)
830 DemuxBlock( p_demux, packet, i_len );
831 if( p_sys->b_spu_change )
833 ButtonUpdate( p_demux, false );
834 p_sys->b_spu_change = false;
839 case DVDNAV_STOP: /* EOF */
840 msg_Dbg( p_demux, "DVDNAV_STOP" );
843 dvdnav_free_cache_block( p_sys->dvdnav, packet );
847 case DVDNAV_HIGHLIGHT:
849 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
850 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
851 msg_Dbg( p_demux, " - display=%d", event->display );
852 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
853 ButtonUpdate( p_demux, false );
857 case DVDNAV_HOP_CHANNEL:
858 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
859 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
863 msg_Dbg( p_demux, "DVDNAV_WAIT" );
866 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
873 dvdnav_wait_skip( p_sys->dvdnav );
874 p_sys->b_reset_pcr = true;
879 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
884 dvdnav_free_cache_block( p_sys->dvdnav, packet );
891 * FIXME: partiallyy duplicated from src/input/es_out.c
893 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
895 const iso639_lang_t *pl;
899 psz_lang = var_CreateGetString( p_demux, psz_var );
901 return strdup(LANGUAGE_DEFAULT);
903 /* XXX: we will use only the first value
904 * (and ignore other ones in case of a list) */
905 if( ( p = strchr( psz_lang, ',' ) ) )
908 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
910 if( *psz_lang == '\0' )
912 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
913 !strcasecmp( pl->psz_native_name, psz_lang ) ||
914 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
915 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
916 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
922 if( pl->psz_iso639_1 != NULL )
923 return strdup( pl->psz_iso639_1 );
925 return strdup(LANGUAGE_DEFAULT);
928 static void DemuxTitles( demux_t *p_demux )
930 demux_sys_t *p_sys = p_demux->p_sys;
937 t = vlc_input_title_New();
939 t->psz_name = strdup( "DVD Menu" );
941 s = vlc_seekpoint_New();
942 s->psz_name = strdup( "Resume" );
943 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
945 s = vlc_seekpoint_New();
946 s->psz_name = strdup( "Root" );
947 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
949 s = vlc_seekpoint_New();
950 s->psz_name = strdup( "Title" );
951 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
953 s = vlc_seekpoint_New();
954 s->psz_name = strdup( "Chapter" );
955 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
957 s = vlc_seekpoint_New();
958 s->psz_name = strdup( "Subtitle" );
959 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
961 s = vlc_seekpoint_New();
962 s->psz_name = strdup( "Audio" );
963 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
965 s = vlc_seekpoint_New();
966 s->psz_name = strdup( "Angle" );
967 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
969 TAB_APPEND( p_sys->i_title, p_sys->title, t );
971 /* Find out number of titles/chapters */
972 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
973 for( i = 1; i <= i_titles; i++ )
975 int32_t i_chapters = 0;
978 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
980 t = vlc_input_title_New();
981 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
983 s = vlc_seekpoint_New();
984 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
987 TAB_APPEND( p_sys->i_title, p_sys->title, t );
991 /*****************************************************************************
993 *****************************************************************************/
994 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
996 demux_sys_t *p_sys = p_demux->p_sys;
998 int32_t i_title, i_part;
1000 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1002 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
1004 vlc_mutex_t *p_mutex = val.p_address;
1005 dvdnav_highlight_area_t hl;
1009 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1010 != DVDNAV_STATUS_OK )
1012 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1016 b_button_ok = false;
1017 if( i_button > 0 && i_title == 0 )
1019 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1021 b_button_ok = dvdnav_get_highlight_area( pci, i_button, b_mode, &hl ) == DVDNAV_STATUS_OK;
1026 for( i = 0; i < 4; i++ )
1028 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1029 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1031 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1032 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1033 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1034 p_sys->palette[i][3] = i_alpha;
1037 vlc_mutex_lock( p_mutex );
1038 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
1039 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
1040 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
1041 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
1043 val.p_address = (void *)p_sys->palette;
1044 var_Set( p_sys->p_input, "menu-palette", val );
1046 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
1047 vlc_mutex_unlock( p_mutex );
1049 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1053 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1054 i_button, i_title );
1057 vlc_mutex_lock( p_mutex );
1059 var_Set( p_sys->p_input, "highlight", val );
1060 vlc_mutex_unlock( p_mutex );
1065 static void ESSubtitleUpdate( demux_t *p_demux )
1067 demux_sys_t *p_sys = p_demux->p_sys;
1068 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1069 int32_t i_title, i_part;
1071 ButtonUpdate( p_demux, false );
1073 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1074 if( i_title > 0 ) return;
1076 if( i_spu >= 0 && i_spu <= 0x1f )
1078 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1080 ESNew( p_demux, 0xbd20 + i_spu );
1082 /* be sure to unselect it (reset) */
1083 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1087 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1091 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1093 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1096 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1103 /*****************************************************************************
1104 * DemuxBlock: demux a given block
1105 *****************************************************************************/
1106 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1108 demux_sys_t *p_sys = p_demux->p_sys;
1109 const uint8_t *p = pkt;
1111 while( p < &pkt[i_pkt] )
1113 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1120 /* Create a block */
1121 p_pkt = block_New( p_demux, i_size );
1122 memcpy( p_pkt->p_buffer, p, i_size);
1124 /* Parse it and send it */
1125 switch( 0x100 | p[3] )
1133 msg_Warn( p_demux, "received a PSM packet" );
1135 else if( p[3] == 0xbb )
1137 msg_Warn( p_demux, "received a SYSTEM packet" );
1140 block_Release( p_pkt );
1147 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1149 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1150 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1152 block_Release( p_pkt );
1157 int i_id = ps_pkt_id( p_pkt );
1160 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1164 ESNew( p_demux, i_id );
1166 if( tk->b_seen && tk->es &&
1167 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1169 es_out_Send( p_demux->out, tk->es, p_pkt );
1173 block_Release( p_pkt );
1178 block_Release( p_pkt );
1190 /*****************************************************************************
1191 * ESNew: register a new elementary stream
1192 *****************************************************************************/
1193 static void ESNew( demux_t *p_demux, int i_id )
1195 demux_sys_t *p_sys = p_demux->p_sys;
1196 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1197 bool b_select = false;
1199 if( tk->b_seen ) return;
1201 if( ps_track_fill( tk, 0, i_id ) )
1203 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1208 if( tk->fmt.i_cat == VIDEO_ES )
1210 switch( p_sys->i_aspect )
1212 case 1: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR; break;
1213 case 2: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3; break;
1214 case 3: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9; break;
1215 case 4: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 221 / 10; break;
1217 tk->fmt.video.i_aspect = 0;
1222 else if( tk->fmt.i_cat == AUDIO_ES )
1225 /* find the audio number PLEASE find another way */
1226 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1228 i_audio = i_id&0x07;
1230 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1234 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1236 i_audio = i_id&0x1f;
1238 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1240 i_audio = i_id&0x1f;
1244 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1245 if( i_lang != 0xffff )
1247 tk->fmt.psz_language = malloc( 3 );
1248 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1249 tk->fmt.psz_language[1] = (i_lang )&0xff;
1250 tk->fmt.psz_language[2] = 0;
1252 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1258 else if( tk->fmt.i_cat == SPU_ES )
1260 int32_t i_title, i_part;
1261 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1262 if( i_lang != 0xffff )
1264 tk->fmt.psz_language = malloc( 3 );
1265 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1266 tk->fmt.psz_language[1] = (i_lang )&0xff;
1267 tk->fmt.psz_language[2] = 0;
1271 tk->fmt.subs.spu.palette[0] = 0xBeef;
1272 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1273 16 * sizeof( uint32_t ) );
1275 /* We select only when we are not in the menu */
1276 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1278 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1284 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1287 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1291 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1294 /*****************************************************************************
1295 * Event handler code
1296 *****************************************************************************/
1297 static int EventMouse( vlc_object_t *, char const *,
1298 vlc_value_t, vlc_value_t, void * );
1300 static void* EventThread( vlc_object_t *p_this )
1302 event_thread_t *p_ev = (event_thread_t*)p_this;
1303 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1304 vlc_object_t *p_vout = NULL;
1305 int canc = vlc_savecancel ();
1308 while( vlc_object_alive( p_ev ) )
1311 if( p_ev->i_key_action != 0 )
1313 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1315 vlc_mutex_lock( &p_ev->lock );
1316 switch( p_ev->i_key_action )
1318 case ACTIONID_NAV_LEFT:
1319 dvdnav_left_button_select( p_sys->dvdnav, pci );
1321 case ACTIONID_NAV_RIGHT:
1322 dvdnav_right_button_select( p_sys->dvdnav, pci );
1324 case ACTIONID_NAV_UP:
1325 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1327 case ACTIONID_NAV_DOWN:
1328 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1330 case ACTIONID_NAV_ACTIVATE:
1331 ButtonUpdate( p_ev->p_demux, true );
1332 dvdnav_button_activate( p_sys->dvdnav, pci );
1337 p_ev->i_key_action = 0;
1338 vlc_mutex_unlock( &p_ev->lock );
1342 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1344 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1345 vlc_value_t valx, valy;
1347 vlc_mutex_lock( &p_ev->lock );
1348 var_Get( p_vout, "mouse-x", &valx );
1349 var_Get( p_vout, "mouse-y", &valy );
1353 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1356 if( p_ev->b_clicked )
1358 ButtonUpdate( p_ev->p_demux, true );
1359 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1363 p_ev->b_moved = false;
1364 p_ev->b_clicked = false;
1365 vlc_mutex_unlock( &p_ev->lock );
1367 if( p_vout && !vlc_object_alive (p_vout) )
1369 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1370 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1371 vlc_object_release( p_vout );
1374 if( p_vout == NULL )
1376 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1380 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1381 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1386 vlc_mutex_lock( &p_ev->lock );
1389 if( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() )
1391 p_ev->b_still = false;
1392 dvdnav_still_skip( p_sys->dvdnav );
1395 vlc_mutex_unlock( &p_ev->lock );
1401 /* Release callback */
1404 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1405 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1406 vlc_object_release( p_vout );
1408 vlc_restorecancel (canc);
1413 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1414 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1416 (void)p_this; (void)oldval; (void)newval; (void)p_data;
1417 event_thread_t *p_ev = p_data;
1418 vlc_mutex_lock( &p_ev->lock );
1419 if( psz_var[6] == 'c' )
1420 p_ev->b_clicked = true;
1421 else if( psz_var[6] == 'm' )
1422 p_ev->b_moved = true;
1423 vlc_mutex_unlock( &p_ev->lock );
1428 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1429 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1431 (void)p_this; (void)psz_var; (void)oldval;
1432 event_thread_t *p_ev = p_data;
1433 vlc_mutex_lock( &p_ev->lock );
1434 p_ev->i_key_action = newval.i_int;
1435 vlc_mutex_unlock( &p_ev->lock );
1440 /*****************************************************************************
1441 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1442 *****************************************************************************/
1443 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1446 #ifdef HAVE_SYS_STAT_H
1447 struct stat stat_info;
1448 uint8_t pi_anchor[2];
1453 /* Triggers libdvdcss autodetection */
1457 if( !strcmp( psz_name, "-" ) ) /* stdin -> file access */
1458 return VLC_EGENERIC;
1460 if( (i_fd = utf8_open( psz_name, O_RDONLY |O_NONBLOCK, 0666 )) == -1 )
1462 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1465 if( fstat( i_fd, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1469 if( S_ISFIFO( stat_info.st_mode ) )
1470 return VLC_EGENERIC;
1471 return VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1474 /* Try to find the anchor (2 bytes at LBA 256) */
1475 i_ret = VLC_EGENERIC;
1476 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1477 && read( i_fd, pi_anchor, 2 ) == 2
1478 && GetWLE( pi_anchor ) == 2 )
1479 i_ret = VLC_SUCCESS; /* Found a potential anchor */