1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
48 /* FIXME we should find a better way than including that */
49 #include "../../src/misc/iso-639_def.h"
52 #include <dvdnav/dvdnav.h>
54 #include "../demux/ps.h"
56 /*****************************************************************************
58 *****************************************************************************/
59 #define ANGLE_TEXT N_("DVD angle")
60 #define ANGLE_LONGTEXT N_( \
61 "Allows you to select the default DVD angle." )
63 #define CACHING_TEXT N_("Caching value in ms")
64 #define CACHING_LONGTEXT N_( \
65 "Allows you to modify the default caching value for DVDnav streams. This "\
66 "value should be set in millisecond units." )
67 #define MENU_TEXT N_("Start directly in menu")
68 #define MENU_LONGTEXT N_( \
69 "Allows you to start the DVD directly in the main menu. This "\
70 "will try to skip all the useless warnings introductions." )
72 #define LANGUAGE_DEFAULT ("en")
74 static int Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
78 set_shortname( _("DVD with menus") );
79 set_description( _("DVDnav Input") );
80 set_category( CAT_INPUT );
81 set_subcategory( SUBCAT_INPUT_ACCESS );
82 add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
83 ANGLE_LONGTEXT, VLC_FALSE );
84 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
85 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
86 add_bool( "dvdnav-menu", VLC_TRUE, NULL,
87 MENU_TEXT, MENU_LONGTEXT, VLC_FALSE );
88 set_capability( "access_demux", 5 );
89 add_shortcut( "dvd" );
90 add_shortcut( "dvdnav" );
91 set_callbacks( Open, Close );
94 /* Shall we use libdvdnav's read ahead cache? */
95 #define DVD_READ_CACHE 1
97 /*****************************************************************************
99 *****************************************************************************/
108 vlc_bool_t b_clicked;
116 static int EventThread( vlc_object_t * );
123 ps_track_t tk[PS_TK_COUNT];
126 /* for spu variables */
127 input_thread_t *p_input;
130 event_thread_t *p_ev;
140 input_title_t **title;
143 static int Control( demux_t *, int, va_list );
144 static int Demux( demux_t * );
145 static int DemuxBlock( demux_t *, uint8_t *, int );
147 static void DemuxTitles( demux_t * );
148 static void ESSubtitleUpdate( demux_t * );
149 static void ButtonUpdate( demux_t * );
151 static void ESNew( demux_t *, int );
152 static int ProbeDVD( demux_t *, char * );
154 static char *DemuxGetLanguageCode( demux_t *p_demux, char *psz_var );
156 /*****************************************************************************
158 *****************************************************************************/
159 static int Open( vlc_object_t *p_this )
161 demux_t *p_demux = (demux_t*)p_this;
169 if( !p_demux->psz_path || !*p_demux->psz_path )
171 /* Only when selected */
172 if( !p_this->b_force ) return VLC_EGENERIC;
174 psz_name = var_CreateGetString( p_this, "dvd" );
175 if( !psz_name || !*psz_name )
177 if( psz_name ) free( psz_name );
181 else psz_name = strdup( p_demux->psz_path );
184 if( psz_name[0] && psz_name[1] == ':' &&
185 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
188 /* Try some simple probing to avoid going through dvdnav_open too often */
189 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
196 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
198 msg_Warn( p_demux, "cannot open dvdnav" );
204 /* Fill p_demux field */
205 p_demux->pf_demux = Demux;
206 p_demux->pf_control = Control;
207 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
208 memset( p_sys, 0, sizeof( demux_sys_t ) );
209 p_sys->dvdnav = p_dvdnav;
211 ps_track_init( p_sys->tk );
212 p_sys->i_aspect = -1;
213 p_sys->i_mux_rate = 0;
217 // Hack for libdvdnav CVS.
218 // Without it dvdnav_get_number_of_titles() fails.
219 // Remove when fixed in libdvdnav CVS.
220 uint8_t buffer[DVD_VIDEO_LB_LEN];
223 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
224 == DVDNAV_STATUS_ERR )
226 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
229 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
232 /* Configure dvdnav */
233 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
236 msg_Warn( p_demux, "cannot set read-a-head flag" );
239 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
242 msg_Warn( p_demux, "cannot set PGC positioning flag" );
245 /* Set menu language ("en")
246 * XXX: maybe it would be better to set it like audio/spu or to create a --menu-language option */
247 if( dvdnav_menu_language_select( p_sys->dvdnav,LANGUAGE_DEFAULT ) != DVDNAV_STATUS_OK )
249 msg_Warn( p_demux, "something failed while setting menu '%s' language (%s)",
251 dvdnav_err_to_string( p_sys->dvdnav ) );
254 /* Set audio language */
255 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
256 if( dvdnav_audio_language_select(p_sys->dvdnav, psz_code ) != DVDNAV_STATUS_OK )
258 msg_Warn( p_demux, "something failed while setting audio '%s' language (%s)",
260 dvdnav_err_to_string( p_sys->dvdnav ) );
261 /* We try to fall back to 'en' */
262 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
263 dvdnav_audio_language_select(p_sys->dvdnav, LANGUAGE_DEFAULT );
267 /* Set spu language */
268 psz_code = DemuxGetLanguageCode( p_demux, "spu-language" );
269 if( dvdnav_spu_language_select( p_sys->dvdnav,psz_code ) != DVDNAV_STATUS_OK )
271 msg_Warn( p_demux, "something failed while setting spu '%s' language (%s)",
273 dvdnav_err_to_string( p_sys->dvdnav ) );
274 /* We try to fall back to 'en' */
275 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
276 dvdnav_spu_language_select(p_sys->dvdnav, LANGUAGE_DEFAULT );
280 DemuxTitles( p_demux );
282 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
283 var_Get( p_demux, "dvdnav-menu", &val );
286 msg_Dbg( p_demux, "trying to go to dvd menu" );
288 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
290 msg_Warn( p_demux, "cannot set title" );
293 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
296 msg_Warn( p_demux, "cannot go to dvd menu" );
300 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
301 var_Get( p_demux, "dvdnav-angle", &val );
302 i_angle = val.i_int > 0 ? val.i_int : 1;
304 /* Update default_pts to a suitable value for dvdnav access */
305 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
307 /* FIXME hack hack hack hack FIXME */
308 /* Get p_input and create variable */
309 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
310 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
311 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
312 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
313 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
314 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
315 var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
316 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
317 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
319 /* Now create our event thread catcher */
320 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
321 p_sys->p_ev->p_demux = p_demux;
322 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
323 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
328 /*****************************************************************************
330 *****************************************************************************/
331 static void Close( vlc_object_t *p_this )
333 demux_t *p_demux = (demux_t*)p_this;
334 demux_sys_t *p_sys = p_demux->p_sys;
337 /* stop the event handler */
338 p_sys->p_ev->b_die = VLC_TRUE;
339 vlc_thread_join( p_sys->p_ev );
340 vlc_object_destroy( p_sys->p_ev );
342 var_Destroy( p_sys->p_input, "highlight-mutex" );
343 var_Destroy( p_sys->p_input, "highlight" );
344 var_Destroy( p_sys->p_input, "x-start" );
345 var_Destroy( p_sys->p_input, "x-end" );
346 var_Destroy( p_sys->p_input, "y-start" );
347 var_Destroy( p_sys->p_input, "y-end" );
348 var_Destroy( p_sys->p_input, "color" );
349 var_Destroy( p_sys->p_input, "contrast" );
351 vlc_object_release( p_sys->p_input );
353 for( i = 0; i < PS_TK_COUNT; i++ )
355 ps_track_t *tk = &p_sys->tk[i];
358 es_format_Clean( &tk->fmt );
359 if( tk->es ) es_out_Del( p_demux->out, tk->es );
363 dvdnav_close( p_sys->dvdnav );
367 /*****************************************************************************
369 *****************************************************************************/
370 static int Control( demux_t *p_demux, int i_query, va_list args )
372 demux_sys_t *p_sys = p_demux->p_sys;
376 input_title_t ***ppp_title;
382 case DEMUX_SET_POSITION:
383 case DEMUX_GET_POSITION:
385 case DEMUX_GET_LENGTH:
388 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
389 DVDNAV_STATUS_OK || len == 0 )
394 if( i_query == DEMUX_GET_POSITION )
396 pf = (double*)va_arg( args, double* );
397 *pf = (double)pos / (double)len;
400 else if( i_query == DEMUX_SET_POSITION )
402 f = (double)va_arg( args, double );
404 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
410 else if( i_query == DEMUX_GET_TIME )
412 pi64 = (int64_t*)va_arg( args, int64_t * );
413 if( p_sys->i_mux_rate > 0 )
415 *pi64 = (int64_t)1000000 * 2048 * pos / 50 /
420 else if( i_query == DEMUX_GET_LENGTH )
422 pi64 = (int64_t*)va_arg( args, int64_t * );
423 if( p_sys->i_mux_rate > 0 )
425 *pi64 = (int64_t)1000000 * len * 2048 / 50 /
434 /* Special for access_demux */
435 case DEMUX_CAN_PAUSE:
436 case DEMUX_CAN_CONTROL_PACE:
438 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
442 case DEMUX_SET_PAUSE_STATE:
445 case DEMUX_GET_TITLE_INFO:
446 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
447 pi_int = (int*)va_arg( args, int* );
448 *((int*)va_arg( args, int* )) = 0; /* Title offset */
449 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
451 /* Duplicate title infos */
452 *pi_int = p_sys->i_title;
453 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
454 for( i = 0; i < p_sys->i_title; i++ )
456 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
460 case DEMUX_SET_TITLE:
461 i = (int)va_arg( args, int );
462 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
463 != DVDNAV_STATUS_OK ) ||
464 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
465 != DVDNAV_STATUS_OK ) )
467 msg_Warn( p_demux, "cannot set title/chapter" );
470 p_demux->info.i_update |=
471 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
472 p_demux->info.i_title = i;
473 p_demux->info.i_seekpoint = 0;
476 case DEMUX_SET_SEEKPOINT:
477 i = (int)va_arg( args, int );
478 if( p_demux->info.i_title == 0 )
485 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
488 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
491 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
494 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
497 i_ret = dvdnav_menu_call( p_sys->dvdnav,
498 DVD_MENU_Subpicture );
501 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
504 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
510 if( i_ret != DVDNAV_STATUS_OK )
513 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
514 i + 1 ) != DVDNAV_STATUS_OK )
516 msg_Warn( p_demux, "cannot set title/chapter" );
519 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
520 p_demux->info.i_seekpoint = i;
523 case DEMUX_GET_PTS_DELAY:
524 pi64 = (int64_t*)va_arg( args, int64_t * );
525 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
528 /* TODO implement others */
534 /*****************************************************************************
536 *****************************************************************************/
537 static int Demux( demux_t *p_demux )
539 demux_sys_t *p_sys = p_demux->p_sys;
541 uint8_t buffer[DVD_VIDEO_LB_LEN];
542 uint8_t *packet = buffer;
547 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
548 == DVDNAV_STATUS_ERR )
550 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
551 == DVDNAV_STATUS_ERR )
554 msg_Warn( p_demux, "cannot get next block (%s)",
555 dvdnav_err_to_string( p_sys->dvdnav ) );
561 case DVDNAV_BLOCK_OK: /* mpeg block */
562 DemuxBlock( p_demux, packet, i_len );
565 case DVDNAV_NOP: /* Nothing */
566 msg_Dbg( p_demux, "DVDNAV_NOP" );
569 case DVDNAV_STILL_FRAME:
571 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
572 vlc_mutex_lock( &p_sys->p_ev->lock );
573 if( !p_sys->p_ev->b_still )
575 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
576 msg_Dbg( p_demux, " - length=0x%x", event->length );
577 p_sys->p_ev->b_still = VLC_TRUE;
578 if( event->length == 0xff )
580 p_sys->p_ev->i_still_end = 0;
584 p_sys->p_ev->i_still_end = (int64_t)event->length *
585 1000000 + mdate() + p_sys->p_input->i_pts_delay;
588 vlc_mutex_unlock( &p_sys->p_ev->lock );
593 case DVDNAV_SPU_CLUT_CHANGE:
597 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
598 /* Update color lookup table (16 *uint32_t in packet) */
599 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
601 /* HACK to get the SPU tracks registered in the right order */
602 for( i = 0; i < 0x1f; i++ )
604 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
605 ESNew( p_demux, 0xbd20 + i );
611 case DVDNAV_SPU_STREAM_CHANGE:
613 dvdnav_spu_stream_change_event_t *event =
614 (dvdnav_spu_stream_change_event_t*)packet;
617 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
618 msg_Dbg( p_demux, " - physical_wide=%d",
619 event->physical_wide );
620 msg_Dbg( p_demux, " - physical_letterbox=%d",
621 event->physical_letterbox);
622 msg_Dbg( p_demux, " - physical_pan_scan=%d",
623 event->physical_pan_scan );
625 ESSubtitleUpdate( p_demux );
627 /* HACK to get the SPU tracks registered in the right order */
628 for( i = 0; i < 0x1f; i++ )
630 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
631 ESNew( p_demux, 0xbd20 + i );
637 case DVDNAV_AUDIO_STREAM_CHANGE:
639 dvdnav_audio_stream_change_event_t *event =
640 (dvdnav_audio_stream_change_event_t*)packet;
641 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
642 msg_Dbg( p_demux, " - physical=%d", event->physical );
647 case DVDNAV_VTS_CHANGE:
653 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
654 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
655 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
656 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
658 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
659 /* TODO check if we always have VTS and CELL */
660 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
663 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
665 for( i = 0; i < PS_TK_COUNT; i++ )
667 ps_track_t *tk = &p_sys->tk[i];
670 es_format_Clean( &tk->fmt );
671 if( tk->es ) es_out_Del( p_demux->out, tk->es );
673 tk->b_seen = VLC_FALSE;
676 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
677 &i_part ) == DVDNAV_STATUS_OK )
679 if( i_title >= 0 && i_title < p_sys->i_title &&
680 p_demux->info.i_title != i_title )
682 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
683 p_demux->info.i_title = i_title;
689 case DVDNAV_CELL_CHANGE:
694 dvdnav_cell_change_event_t *event =
695 (dvdnav_cell_change_event_t*)packet;
696 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
697 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
698 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
699 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
700 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
701 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
702 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
703 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
705 /* FIXME is it correct or there is better way to know chapter change */
706 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
707 &i_part ) == DVDNAV_STATUS_OK )
709 if( i_title >= 0 && i_title < p_sys->i_title &&
710 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
711 p_demux->info.i_seekpoint != i_part - 1 )
713 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
714 p_demux->info.i_seekpoint = i_part - 1;
720 case DVDNAV_NAV_PACKET:
723 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
725 /* A lot of thing to do here :
727 * - fetch pts (for time display)
730 DemuxBlock( p_demux, packet, i_len );
734 case DVDNAV_STOP: /* EOF */
735 msg_Dbg( p_demux, "DVDNAV_STOP" );
738 case DVDNAV_HIGHLIGHT:
740 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
741 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
742 msg_Dbg( p_demux, " - display=%d", event->display );
743 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
744 ButtonUpdate( p_demux );
748 case DVDNAV_HOP_CHANNEL:
749 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
750 /* We should try to flush all our internal buffer */
754 msg_Dbg( p_demux, "DVDNAV_WAIT" );
756 dvdnav_wait_skip( p_sys->dvdnav );
760 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
765 dvdnav_free_cache_block( p_sys->dvdnav, packet );
772 * FIXME: partiallyy duplicated from src/input/es_out.c
774 static char *DemuxGetLanguageCode( demux_t *p_demux, char *psz_var )
776 const iso639_lang_t *pl;
780 psz_lang = var_CreateGetString( p_demux, psz_var );
781 /* XXX: we will use only the first value (and ignore other ones in case of a list) */
782 if( ( p = strchr( psz_lang, "," ) ) )
785 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
787 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
788 !strcasecmp( pl->psz_native_name, psz_lang ) ||
789 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
790 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
791 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
797 if( pl->psz_iso639_1 != NULL )
798 return strdup( pl->psz_iso639_1 );
800 return strdup(LANGUAGE_DEFAULT);
803 static void DemuxTitles( demux_t *p_demux )
805 demux_sys_t *p_sys = p_demux->p_sys;
812 t = vlc_input_title_New();
813 t->b_menu = VLC_TRUE;
814 t->psz_name = strdup( "DVD Menu" );
816 s = vlc_seekpoint_New();
817 s->psz_name = strdup( "Resume" );
818 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
820 s = vlc_seekpoint_New();
821 s->psz_name = strdup( "Root" );
822 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
824 s = vlc_seekpoint_New();
825 s->psz_name = strdup( "Title" );
826 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
828 s = vlc_seekpoint_New();
829 s->psz_name = strdup( "Chapter" );
830 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
832 s = vlc_seekpoint_New();
833 s->psz_name = strdup( "Subtitle" );
834 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
836 s = vlc_seekpoint_New();
837 s->psz_name = strdup( "Audio" );
838 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
840 s = vlc_seekpoint_New();
841 s->psz_name = strdup( "Angle" );
842 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
844 TAB_APPEND( p_sys->i_title, p_sys->title, t );
846 /* Find out number of titles/chapters */
847 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
848 for( i = 1; i <= i_titles; i++ )
850 int32_t i_chapters = 0;
853 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
855 t = vlc_input_title_New();
856 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
858 s = vlc_seekpoint_New();
859 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
862 TAB_APPEND( p_sys->i_title, p_sys->title, t );
866 /*****************************************************************************
868 *****************************************************************************/
869 static void ButtonUpdate( demux_t *p_demux )
871 demux_sys_t *p_sys = p_demux->p_sys;
873 int32_t i_title, i_part;
875 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
877 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
879 vlc_mutex_t *p_mutex = val.p_address;
880 dvdnav_highlight_area_t hl;
883 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
884 != DVDNAV_STATUS_OK )
886 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
890 if( i_button > 0 && i_title == 0 )
892 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
894 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
896 /* I fear it is plain wrong */
897 p_sys->alpha[0] = hl.palette&0x0f;
898 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
899 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
900 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
902 vlc_mutex_lock( p_mutex );
903 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
904 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
905 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
906 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
908 val.p_address = (void *)p_sys->alpha;
909 var_Set( p_sys->p_input, "contrast", val );
911 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
912 vlc_mutex_unlock( p_mutex );
914 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
918 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
922 vlc_mutex_lock( p_mutex );
923 val.b_bool = VLC_FALSE;
924 var_Set( p_sys->p_input, "highlight", val );
925 vlc_mutex_unlock( p_mutex );
930 static void ESSubtitleUpdate( demux_t *p_demux )
932 demux_sys_t *p_sys = p_demux->p_sys;
933 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
934 int32_t i_title, i_part;
936 ButtonUpdate( p_demux );
938 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
939 if( i_title > 0 ) return;
941 if( i_spu >= 0 && i_spu <= 0x1f )
943 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
945 ESNew( p_demux, 0xbd20 + i_spu );
947 /* be sure to unselect it (reset) */
948 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
949 (vlc_bool_t)VLC_FALSE );
952 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
956 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
958 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
961 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
962 (vlc_bool_t)VLC_FALSE );
968 /*****************************************************************************
969 * DemuxBlock: demux a given block
970 *****************************************************************************/
971 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
973 demux_sys_t *p_sys = p_demux->p_sys;
976 while( p < &pkt[i_pkt] )
978 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
986 p_pkt = block_New( p_demux, i_size );
987 memcpy( p_pkt->p_buffer, p, i_size);
989 /* Parse it and send it */
990 switch( 0x100 | p[3] )
998 msg_Warn( p_demux, "received a PSM packet" );
1000 else if( p[3] == 0xbb )
1002 msg_Warn( p_demux, "received a SYSTEM packet" );
1005 block_Release( p_pkt );
1012 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1014 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
1015 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1017 block_Release( p_pkt );
1022 int i_id = ps_pkt_id( p_pkt );
1025 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1029 ESNew( p_demux, i_id );
1031 if( tk->b_seen && tk->es &&
1032 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1034 es_out_Send( p_demux->out, tk->es, p_pkt );
1038 block_Release( p_pkt );
1043 block_Release( p_pkt );
1055 /*****************************************************************************
1056 * ESNew: register a new elementary stream
1057 *****************************************************************************/
1058 static void ESNew( demux_t *p_demux, int i_id )
1060 demux_sys_t *p_sys = p_demux->p_sys;
1061 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1062 vlc_bool_t b_select = VLC_FALSE;
1064 if( tk->b_seen ) return;
1066 if( ps_track_fill( tk, 0, i_id ) )
1068 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1073 if( tk->fmt.i_cat == VIDEO_ES )
1075 if( p_sys->i_aspect >= 0 )
1077 tk->fmt.video.i_aspect = p_sys->i_aspect;
1079 b_select = VLC_TRUE;
1081 else if( tk->fmt.i_cat == AUDIO_ES )
1084 /* find the audio number PLEASE find another way */
1085 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1087 i_audio = i_id&0x07;
1089 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1093 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1095 i_audio = i_id&0x1f;
1097 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1099 i_audio = i_id&0x1f;
1103 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1104 if( i_lang != 0xffff )
1106 tk->fmt.psz_language = malloc( 3 );
1107 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1108 tk->fmt.psz_language[1] = (i_lang )&0xff;
1109 tk->fmt.psz_language[2] = 0;
1111 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1113 b_select = VLC_TRUE;
1117 else if( tk->fmt.i_cat == SPU_ES )
1119 int32_t i_title, i_part;
1120 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1121 if( i_lang != 0xffff )
1123 tk->fmt.psz_language = malloc( 3 );
1124 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1125 tk->fmt.psz_language[1] = (i_lang )&0xff;
1126 tk->fmt.psz_language[2] = 0;
1130 tk->fmt.subs.spu.palette[0] = 0xBeef;
1131 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1132 16 * sizeof( uint32_t ) );
1134 /* We select only when we are not in the menu */
1135 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1137 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1139 b_select = VLC_TRUE;
1143 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1146 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1148 tk->b_seen = VLC_TRUE;
1150 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux );
1153 /*****************************************************************************
1154 * Event handler code
1155 *****************************************************************************/
1156 static int EventMouse( vlc_object_t *, char const *,
1157 vlc_value_t, vlc_value_t, void * );
1158 static int EventKey ( vlc_object_t *, char const *,
1159 vlc_value_t, vlc_value_t, void * );
1161 static int EventThread( vlc_object_t *p_this )
1163 event_thread_t *p_ev = (event_thread_t*)p_this;
1164 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1165 vlc_object_t *p_vout = NULL;
1167 vlc_mutex_init( p_ev, &p_ev->lock );
1168 p_ev->b_moved = VLC_FALSE;
1169 p_ev->b_clicked = VLC_FALSE;
1170 p_ev->b_key = VLC_FALSE;
1171 p_ev->b_still = VLC_FALSE;
1173 /* catch all key event */
1174 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1177 while( !p_ev->b_die )
1179 vlc_bool_t b_activated = VLC_FALSE;
1184 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1187 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1188 int i, i_action = -1;
1190 vlc_mutex_lock( &p_ev->lock );
1191 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1192 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1194 if( p_hotkeys[i].i_key == valk.i_int )
1196 i_action = p_hotkeys[i].i_action;
1202 case ACTIONID_NAV_LEFT:
1203 dvdnav_left_button_select( p_sys->dvdnav, pci );
1205 case ACTIONID_NAV_RIGHT:
1206 dvdnav_right_button_select( p_sys->dvdnav, pci );
1208 case ACTIONID_NAV_UP:
1209 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1211 case ACTIONID_NAV_DOWN:
1212 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1214 case ACTIONID_NAV_ACTIVATE:
1215 b_activated = VLC_TRUE;
1216 dvdnav_button_activate( p_sys->dvdnav, pci );
1221 p_ev->b_key = VLC_FALSE;
1222 vlc_mutex_unlock( &p_ev->lock );
1226 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1228 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1229 vlc_value_t valx, valy;
1231 vlc_mutex_lock( &p_ev->lock );
1232 var_Get( p_vout, "mouse-x", &valx );
1233 var_Get( p_vout, "mouse-y", &valy );
1237 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1240 if( p_ev->b_clicked )
1242 b_activated = VLC_TRUE;
1243 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1247 p_ev->b_moved = VLC_FALSE;
1248 p_ev->b_clicked = VLC_FALSE;
1249 vlc_mutex_unlock( &p_ev->lock );
1251 if( p_vout && p_vout->b_die )
1253 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1254 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1255 vlc_object_release( p_vout );
1258 if( p_vout == NULL )
1260 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1264 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1265 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1270 vlc_mutex_lock( &p_ev->lock );
1273 if( /* b_activated || // This breaks menus */
1274 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1276 p_ev->b_still = VLC_FALSE;
1277 dvdnav_still_skip( p_sys->dvdnav );
1280 vlc_mutex_unlock( &p_ev->lock );
1286 /* Release callback */
1289 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1290 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1291 vlc_object_release( p_vout );
1293 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1295 vlc_mutex_destroy( &p_ev->lock );
1300 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1301 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1303 event_thread_t *p_ev = p_data;
1304 vlc_mutex_lock( &p_ev->lock );
1305 if( psz_var[6] == 'c' )
1306 p_ev->b_clicked = VLC_TRUE;
1307 else if( psz_var[6] == 'm' )
1308 p_ev->b_moved = VLC_TRUE;
1309 vlc_mutex_unlock( &p_ev->lock );
1314 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1315 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1317 event_thread_t *p_ev = p_data;
1318 vlc_mutex_lock( &p_ev->lock );
1319 p_ev->b_key = VLC_TRUE;
1320 vlc_mutex_unlock( &p_ev->lock );
1325 /*****************************************************************************
1326 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1327 *****************************************************************************/
1328 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1330 #ifdef HAVE_SYS_STAT_H
1331 struct stat stat_info;
1332 uint8_t pi_anchor[2];
1333 uint16_t i_tag_id = 0;
1336 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1338 /* Let dvdnav_open() do the probing */
1342 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1344 /* Let dvdnav_open() do the probing */
1348 /* Try to find the anchor (2 bytes at LBA 256) */
1349 i_ret = VLC_SUCCESS;
1350 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1352 i_ret = VLC_EGENERIC;
1355 if( read( i_fd, pi_anchor, 2 ) == 2 )
1357 i_tag_id = GetWLE(pi_anchor);
1358 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1362 i_ret = VLC_EGENERIC;