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;
142 /* lenght of program group chain */
143 mtime_t i_pgc_length;
146 static int Control( demux_t *, int, va_list );
147 static int Demux( demux_t * );
148 static int DemuxBlock( demux_t *, uint8_t *, int );
150 static void DemuxTitles( demux_t * );
151 static void ESSubtitleUpdate( demux_t * );
152 static void ButtonUpdate( demux_t * );
154 static void ESNew( demux_t *, int );
155 static int ProbeDVD( demux_t *, char * );
157 static char *DemuxGetLanguageCode( demux_t *p_demux, char *psz_var );
159 /*****************************************************************************
161 *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
164 demux_t *p_demux = (demux_t*)p_this;
172 if( !p_demux->psz_path || !*p_demux->psz_path )
174 /* Only when selected */
175 if( !p_this->b_force ) return VLC_EGENERIC;
177 psz_name = var_CreateGetString( p_this, "dvd" );
178 if( !psz_name || !*psz_name )
180 if( psz_name ) free( psz_name );
184 else psz_name = strdup( p_demux->psz_path );
187 if( psz_name[0] && psz_name[1] == ':' &&
188 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
191 /* Try some simple probing to avoid going through dvdnav_open too often */
192 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
199 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
201 msg_Warn( p_demux, "cannot open dvdnav" );
207 /* Fill p_demux field */
208 p_demux->pf_demux = Demux;
209 p_demux->pf_control = Control;
210 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
211 memset( p_sys, 0, sizeof( demux_sys_t ) );
212 p_sys->dvdnav = p_dvdnav;
214 ps_track_init( p_sys->tk );
215 p_sys->i_aspect = -1;
216 p_sys->i_mux_rate = 0;
217 p_sys->i_pgc_length = 0;
221 // Hack for libdvdnav CVS.
222 // Without it dvdnav_get_number_of_titles() fails.
223 // Remove when fixed in libdvdnav CVS.
224 uint8_t buffer[DVD_VIDEO_LB_LEN];
227 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
228 == DVDNAV_STATUS_ERR )
230 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
233 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
236 /* Configure dvdnav */
237 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
240 msg_Warn( p_demux, "cannot set read-a-head flag" );
243 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
246 msg_Warn( p_demux, "cannot set PGC positioning flag" );
249 /* Set menu language ("en")
250 * XXX: maybe it would be better to set it like audio/spu
251 * or to create a --menu-language option */
252 if( dvdnav_menu_language_select( p_sys->dvdnav,LANGUAGE_DEFAULT ) !=
255 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
256 LANGUAGE_DEFAULT, dvdnav_err_to_string( p_sys->dvdnav ) );
259 /* Set audio language */
260 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
261 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
264 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
265 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
266 /* We try to fall back to 'en' */
267 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
268 dvdnav_audio_language_select( p_sys->dvdnav, LANGUAGE_DEFAULT );
272 /* Set spu language */
273 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
274 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
277 msg_Warn( p_demux, "can't set spu 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_spu_language_select(p_sys->dvdnav, LANGUAGE_DEFAULT );
285 DemuxTitles( p_demux );
287 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
288 var_Get( p_demux, "dvdnav-menu", &val );
291 msg_Dbg( p_demux, "trying to go to dvd menu" );
293 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
295 msg_Warn( p_demux, "cannot set title" );
298 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
301 /* Try going to menu root */
302 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
304 msg_Warn( p_demux, "cannot go to dvd menu" );
308 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
309 var_Get( p_demux, "dvdnav-angle", &val );
310 i_angle = val.i_int > 0 ? val.i_int : 1;
312 /* Update default_pts to a suitable value for dvdnav access */
313 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
315 /* FIXME hack hack hack hack FIXME */
316 /* Get p_input and create variable */
317 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
318 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
319 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
320 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
321 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
322 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
323 var_Create( p_sys->p_input, "menu-contrast", VLC_VAR_ADDRESS );
324 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
325 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
327 /* Now create our event thread catcher */
328 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
329 p_sys->p_ev->p_demux = p_demux;
330 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
331 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
336 /*****************************************************************************
338 *****************************************************************************/
339 static void Close( vlc_object_t *p_this )
341 demux_t *p_demux = (demux_t*)p_this;
342 demux_sys_t *p_sys = p_demux->p_sys;
345 /* stop the event handler */
346 p_sys->p_ev->b_die = VLC_TRUE;
347 vlc_thread_join( p_sys->p_ev );
348 vlc_object_destroy( p_sys->p_ev );
350 var_Destroy( p_sys->p_input, "highlight-mutex" );
351 var_Destroy( p_sys->p_input, "highlight" );
352 var_Destroy( p_sys->p_input, "x-start" );
353 var_Destroy( p_sys->p_input, "x-end" );
354 var_Destroy( p_sys->p_input, "y-start" );
355 var_Destroy( p_sys->p_input, "y-end" );
356 var_Destroy( p_sys->p_input, "color" );
357 var_Destroy( p_sys->p_input, "menu-contrast" );
359 vlc_object_release( p_sys->p_input );
361 for( i = 0; i < PS_TK_COUNT; i++ )
363 ps_track_t *tk = &p_sys->tk[i];
366 es_format_Clean( &tk->fmt );
367 if( tk->es ) es_out_Del( p_demux->out, tk->es );
371 dvdnav_close( p_sys->dvdnav );
375 /*****************************************************************************
377 *****************************************************************************/
378 static int Control( demux_t *p_demux, int i_query, va_list args )
380 demux_sys_t *p_sys = p_demux->p_sys;
384 input_title_t ***ppp_title;
390 case DEMUX_SET_POSITION:
391 case DEMUX_GET_POSITION:
393 case DEMUX_GET_LENGTH:
396 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
397 DVDNAV_STATUS_OK || len == 0 )
402 if( i_query == DEMUX_GET_POSITION )
404 pf = (double*)va_arg( args, double* );
405 *pf = (double)pos / (double)len;
408 else if( i_query == DEMUX_SET_POSITION )
410 f = (double)va_arg( args, double );
412 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
418 else if( i_query == DEMUX_GET_TIME )
420 pi64 = (int64_t*)va_arg( args, int64_t * );
421 if( p_sys->i_pgc_length > 0 )
423 *pi64 = (int64_t) ( (double)p_sys->i_pgc_length / (double)len ) * (double) pos;
427 else if( i_query == DEMUX_GET_LENGTH )
429 pi64 = (int64_t*)va_arg( args, int64_t * );
430 if( p_sys->i_pgc_length > 0 )
432 *pi64 = (int64_t)p_sys->i_pgc_length;
440 /* Special for access_demux */
441 case DEMUX_CAN_PAUSE:
442 case DEMUX_CAN_CONTROL_PACE:
444 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
448 case DEMUX_SET_PAUSE_STATE:
451 case DEMUX_GET_TITLE_INFO:
452 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
453 pi_int = (int*)va_arg( args, int* );
454 *((int*)va_arg( args, int* )) = 0; /* Title offset */
455 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
457 /* Duplicate title infos */
458 *pi_int = p_sys->i_title;
459 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
460 for( i = 0; i < p_sys->i_title; i++ )
462 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
466 case DEMUX_SET_TITLE:
467 i = (int)va_arg( args, int );
468 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
469 != DVDNAV_STATUS_OK ) ||
470 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
471 != DVDNAV_STATUS_OK ) )
473 msg_Warn( p_demux, "cannot set title/chapter" );
476 p_demux->info.i_update |=
477 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
478 p_demux->info.i_title = i;
479 p_demux->info.i_seekpoint = 0;
482 case DEMUX_SET_SEEKPOINT:
483 i = (int)va_arg( args, int );
484 if( p_demux->info.i_title == 0 )
491 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
494 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
497 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
500 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
503 i_ret = dvdnav_menu_call( p_sys->dvdnav,
504 DVD_MENU_Subpicture );
507 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
510 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
516 if( i_ret != DVDNAV_STATUS_OK )
519 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
520 i + 1 ) != DVDNAV_STATUS_OK )
522 msg_Warn( p_demux, "cannot set title/chapter" );
525 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
526 p_demux->info.i_seekpoint = i;
529 case DEMUX_GET_PTS_DELAY:
530 pi64 = (int64_t*)va_arg( args, int64_t * );
531 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
534 /* TODO implement others */
540 /*****************************************************************************
542 *****************************************************************************/
543 static int Demux( demux_t *p_demux )
545 demux_sys_t *p_sys = p_demux->p_sys;
547 uint8_t buffer[DVD_VIDEO_LB_LEN];
548 uint8_t *packet = buffer;
553 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
554 == DVDNAV_STATUS_ERR )
556 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
557 == DVDNAV_STATUS_ERR )
560 msg_Warn( p_demux, "cannot get next block (%s)",
561 dvdnav_err_to_string( p_sys->dvdnav ) );
567 case DVDNAV_BLOCK_OK: /* mpeg block */
568 DemuxBlock( p_demux, packet, i_len );
571 case DVDNAV_NOP: /* Nothing */
572 msg_Dbg( p_demux, "DVDNAV_NOP" );
575 case DVDNAV_STILL_FRAME:
577 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
578 vlc_mutex_lock( &p_sys->p_ev->lock );
579 if( !p_sys->p_ev->b_still )
581 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
582 msg_Dbg( p_demux, " - length=0x%x", event->length );
583 p_sys->p_ev->b_still = VLC_TRUE;
584 if( event->length == 0xff )
586 p_sys->p_ev->i_still_end = 0;
590 p_sys->p_ev->i_still_end = (int64_t)event->length *
591 1000000 + mdate() + p_sys->p_input->i_pts_delay;
594 vlc_mutex_unlock( &p_sys->p_ev->lock );
599 case DVDNAV_SPU_CLUT_CHANGE:
603 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
604 /* Update color lookup table (16 *uint32_t in packet) */
605 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
607 /* HACK to get the SPU tracks registered in the right order */
608 for( i = 0; i < 0x1f; i++ )
610 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
611 ESNew( p_demux, 0xbd20 + i );
617 case DVDNAV_SPU_STREAM_CHANGE:
619 dvdnav_spu_stream_change_event_t *event =
620 (dvdnav_spu_stream_change_event_t*)packet;
623 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
624 msg_Dbg( p_demux, " - physical_wide=%d",
625 event->physical_wide );
626 msg_Dbg( p_demux, " - physical_letterbox=%d",
627 event->physical_letterbox);
628 msg_Dbg( p_demux, " - physical_pan_scan=%d",
629 event->physical_pan_scan );
631 ESSubtitleUpdate( p_demux );
633 /* HACK to get the SPU tracks registered in the right order */
634 for( i = 0; i < 0x1f; i++ )
636 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
637 ESNew( p_demux, 0xbd20 + i );
643 case DVDNAV_AUDIO_STREAM_CHANGE:
645 dvdnav_audio_stream_change_event_t *event =
646 (dvdnav_audio_stream_change_event_t*)packet;
647 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
648 msg_Dbg( p_demux, " - physical=%d", event->physical );
653 case DVDNAV_VTS_CHANGE:
659 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
660 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
661 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
662 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
664 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
665 /* TODO check if we always have VTS and CELL */
666 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
669 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
671 for( i = 0; i < PS_TK_COUNT; i++ )
673 ps_track_t *tk = &p_sys->tk[i];
676 es_format_Clean( &tk->fmt );
677 if( tk->es ) es_out_Del( p_demux->out, tk->es );
679 tk->b_seen = VLC_FALSE;
682 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
683 &i_part ) == DVDNAV_STATUS_OK )
685 if( i_title >= 0 && i_title < p_sys->i_title &&
686 p_demux->info.i_title != i_title )
688 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
689 p_demux->info.i_title = i_title;
695 case DVDNAV_CELL_CHANGE:
700 dvdnav_cell_change_event_t *event =
701 (dvdnav_cell_change_event_t*)packet;
702 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
703 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
704 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
705 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
706 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
707 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
708 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
709 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
711 /* Store the lenght in time of the current PGC */
712 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
714 /* FIXME is it correct or there is better way to know chapter change */
715 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
716 &i_part ) == DVDNAV_STATUS_OK )
718 if( i_title >= 0 && i_title < p_sys->i_title &&
719 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
720 p_demux->info.i_seekpoint != i_part - 1 )
722 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
723 p_demux->info.i_seekpoint = i_part - 1;
729 case DVDNAV_NAV_PACKET:
732 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
734 /* A lot of thing to do here :
736 * - fetch pts (for time display)
739 DemuxBlock( p_demux, packet, i_len );
743 case DVDNAV_STOP: /* EOF */
744 msg_Dbg( p_demux, "DVDNAV_STOP" );
747 dvdnav_free_cache_block( p_sys->dvdnav, packet );
751 case DVDNAV_HIGHLIGHT:
753 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
754 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
755 msg_Dbg( p_demux, " - display=%d", event->display );
756 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
757 ButtonUpdate( p_demux );
761 case DVDNAV_HOP_CHANNEL:
762 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
763 /* We should try to flush all our internal buffer */
767 msg_Dbg( p_demux, "DVDNAV_WAIT" );
769 dvdnav_wait_skip( p_sys->dvdnav );
773 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
778 dvdnav_free_cache_block( p_sys->dvdnav, packet );
785 * FIXME: partiallyy duplicated from src/input/es_out.c
787 static char *DemuxGetLanguageCode( demux_t *p_demux, char *psz_var )
789 const iso639_lang_t *pl;
793 psz_lang = var_CreateGetString( p_demux, psz_var );
794 /* XXX: we will use only the first value
795 * (and ignore other ones in case of a list) */
796 if( ( p = strchr( psz_lang, ',' ) ) ) *p = '\0';
798 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
800 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
801 !strcasecmp( pl->psz_native_name, psz_lang ) ||
802 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
803 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
804 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
810 if( pl->psz_iso639_1 != NULL )
811 return strdup( pl->psz_iso639_1 );
813 return strdup(LANGUAGE_DEFAULT);
816 static void DemuxTitles( demux_t *p_demux )
818 demux_sys_t *p_sys = p_demux->p_sys;
825 t = vlc_input_title_New();
826 t->b_menu = VLC_TRUE;
827 t->psz_name = strdup( "DVD Menu" );
829 s = vlc_seekpoint_New();
830 s->psz_name = strdup( "Resume" );
831 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
833 s = vlc_seekpoint_New();
834 s->psz_name = strdup( "Root" );
835 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
837 s = vlc_seekpoint_New();
838 s->psz_name = strdup( "Title" );
839 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
841 s = vlc_seekpoint_New();
842 s->psz_name = strdup( "Chapter" );
843 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
845 s = vlc_seekpoint_New();
846 s->psz_name = strdup( "Subtitle" );
847 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
849 s = vlc_seekpoint_New();
850 s->psz_name = strdup( "Audio" );
851 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
853 s = vlc_seekpoint_New();
854 s->psz_name = strdup( "Angle" );
855 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
857 TAB_APPEND( p_sys->i_title, p_sys->title, t );
859 /* Find out number of titles/chapters */
860 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
861 for( i = 1; i <= i_titles; i++ )
863 int32_t i_chapters = 0;
866 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
868 t = vlc_input_title_New();
869 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
871 s = vlc_seekpoint_New();
872 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
875 TAB_APPEND( p_sys->i_title, p_sys->title, t );
879 /*****************************************************************************
881 *****************************************************************************/
882 static void ButtonUpdate( demux_t *p_demux )
884 demux_sys_t *p_sys = p_demux->p_sys;
886 int32_t i_title, i_part;
888 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
890 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
892 vlc_mutex_t *p_mutex = val.p_address;
893 dvdnav_highlight_area_t hl;
896 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
897 != DVDNAV_STATUS_OK )
899 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
903 if( i_button > 0 && i_title == 0 )
905 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
907 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
909 /* I fear it is plain wrong */
910 p_sys->alpha[0] = hl.palette&0x0f;
911 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
912 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
913 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
915 vlc_mutex_lock( p_mutex );
916 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
917 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
918 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
919 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
921 val.p_address = (void *)p_sys->alpha;
922 var_Set( p_sys->p_input, "menu-contrast", val );
924 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
925 vlc_mutex_unlock( p_mutex );
927 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
931 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
935 vlc_mutex_lock( p_mutex );
936 val.b_bool = VLC_FALSE;
937 var_Set( p_sys->p_input, "highlight", val );
938 vlc_mutex_unlock( p_mutex );
943 static void ESSubtitleUpdate( demux_t *p_demux )
945 demux_sys_t *p_sys = p_demux->p_sys;
946 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
947 int32_t i_title, i_part;
949 ButtonUpdate( p_demux );
951 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
952 if( i_title > 0 ) return;
954 if( i_spu >= 0 && i_spu <= 0x1f )
956 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
958 ESNew( p_demux, 0xbd20 + i_spu );
960 /* be sure to unselect it (reset) */
961 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
962 (vlc_bool_t)VLC_FALSE );
965 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
969 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
971 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
974 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
975 (vlc_bool_t)VLC_FALSE );
981 /*****************************************************************************
982 * DemuxBlock: demux a given block
983 *****************************************************************************/
984 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
986 demux_sys_t *p_sys = p_demux->p_sys;
989 while( p < &pkt[i_pkt] )
991 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
999 p_pkt = block_New( p_demux, i_size );
1000 memcpy( p_pkt->p_buffer, p, i_size);
1002 /* Parse it and send it */
1003 switch( 0x100 | p[3] )
1011 msg_Warn( p_demux, "received a PSM packet" );
1013 else if( p[3] == 0xbb )
1015 msg_Warn( p_demux, "received a SYSTEM packet" );
1018 block_Release( p_pkt );
1025 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1027 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
1028 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1030 block_Release( p_pkt );
1035 int i_id = ps_pkt_id( p_pkt );
1038 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1042 ESNew( p_demux, i_id );
1044 if( tk->b_seen && tk->es &&
1045 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1047 es_out_Send( p_demux->out, tk->es, p_pkt );
1051 block_Release( p_pkt );
1056 block_Release( p_pkt );
1068 /*****************************************************************************
1069 * ESNew: register a new elementary stream
1070 *****************************************************************************/
1071 static void ESNew( demux_t *p_demux, int i_id )
1073 demux_sys_t *p_sys = p_demux->p_sys;
1074 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1075 vlc_bool_t b_select = VLC_FALSE;
1077 if( tk->b_seen ) return;
1079 if( ps_track_fill( tk, 0, i_id ) )
1081 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1086 if( tk->fmt.i_cat == VIDEO_ES )
1088 if( p_sys->i_aspect >= 0 )
1090 tk->fmt.video.i_aspect = p_sys->i_aspect;
1092 b_select = VLC_TRUE;
1094 else if( tk->fmt.i_cat == AUDIO_ES )
1097 /* find the audio number PLEASE find another way */
1098 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1100 i_audio = i_id&0x07;
1102 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1106 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1108 i_audio = i_id&0x1f;
1110 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1112 i_audio = i_id&0x1f;
1116 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1117 if( i_lang != 0xffff )
1119 tk->fmt.psz_language = malloc( 3 );
1120 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1121 tk->fmt.psz_language[1] = (i_lang )&0xff;
1122 tk->fmt.psz_language[2] = 0;
1124 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1126 b_select = VLC_TRUE;
1130 else if( tk->fmt.i_cat == SPU_ES )
1132 int32_t i_title, i_part;
1133 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1134 if( i_lang != 0xffff )
1136 tk->fmt.psz_language = malloc( 3 );
1137 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1138 tk->fmt.psz_language[1] = (i_lang )&0xff;
1139 tk->fmt.psz_language[2] = 0;
1143 tk->fmt.subs.spu.palette[0] = 0xBeef;
1144 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1145 16 * sizeof( uint32_t ) );
1147 /* We select only when we are not in the menu */
1148 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1150 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1152 b_select = VLC_TRUE;
1156 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1159 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1161 tk->b_seen = VLC_TRUE;
1163 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux );
1166 /*****************************************************************************
1167 * Event handler code
1168 *****************************************************************************/
1169 static int EventMouse( vlc_object_t *, char const *,
1170 vlc_value_t, vlc_value_t, void * );
1171 static int EventKey ( vlc_object_t *, char const *,
1172 vlc_value_t, vlc_value_t, void * );
1174 static int EventThread( vlc_object_t *p_this )
1176 event_thread_t *p_ev = (event_thread_t*)p_this;
1177 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1178 vlc_object_t *p_vout = NULL;
1180 vlc_mutex_init( p_ev, &p_ev->lock );
1181 p_ev->b_moved = VLC_FALSE;
1182 p_ev->b_clicked = VLC_FALSE;
1183 p_ev->b_key = VLC_FALSE;
1184 p_ev->b_still = VLC_FALSE;
1186 /* catch all key event */
1187 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1190 while( !p_ev->b_die )
1192 vlc_bool_t b_activated = VLC_FALSE;
1197 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1200 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1201 int i, i_action = -1;
1203 vlc_mutex_lock( &p_ev->lock );
1204 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1205 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1207 if( p_hotkeys[i].i_key == valk.i_int )
1209 i_action = p_hotkeys[i].i_action;
1215 case ACTIONID_NAV_LEFT:
1216 dvdnav_left_button_select( p_sys->dvdnav, pci );
1218 case ACTIONID_NAV_RIGHT:
1219 dvdnav_right_button_select( p_sys->dvdnav, pci );
1221 case ACTIONID_NAV_UP:
1222 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1224 case ACTIONID_NAV_DOWN:
1225 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1227 case ACTIONID_NAV_ACTIVATE:
1228 b_activated = VLC_TRUE;
1229 dvdnav_button_activate( p_sys->dvdnav, pci );
1234 p_ev->b_key = VLC_FALSE;
1235 vlc_mutex_unlock( &p_ev->lock );
1239 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1241 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1242 vlc_value_t valx, valy;
1244 vlc_mutex_lock( &p_ev->lock );
1245 var_Get( p_vout, "mouse-x", &valx );
1246 var_Get( p_vout, "mouse-y", &valy );
1250 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1253 if( p_ev->b_clicked )
1255 b_activated = VLC_TRUE;
1256 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1260 p_ev->b_moved = VLC_FALSE;
1261 p_ev->b_clicked = VLC_FALSE;
1262 vlc_mutex_unlock( &p_ev->lock );
1264 if( p_vout && p_vout->b_die )
1266 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1267 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1268 vlc_object_release( p_vout );
1271 if( p_vout == NULL )
1273 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1277 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1278 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1283 vlc_mutex_lock( &p_ev->lock );
1286 if( /* b_activated || // This breaks menus */
1287 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1289 p_ev->b_still = VLC_FALSE;
1290 dvdnav_still_skip( p_sys->dvdnav );
1293 vlc_mutex_unlock( &p_ev->lock );
1299 /* Release callback */
1302 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1303 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1304 vlc_object_release( p_vout );
1306 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1308 vlc_mutex_destroy( &p_ev->lock );
1313 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1314 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1316 event_thread_t *p_ev = p_data;
1317 vlc_mutex_lock( &p_ev->lock );
1318 if( psz_var[6] == 'c' )
1319 p_ev->b_clicked = VLC_TRUE;
1320 else if( psz_var[6] == 'm' )
1321 p_ev->b_moved = VLC_TRUE;
1322 vlc_mutex_unlock( &p_ev->lock );
1327 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1328 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1330 event_thread_t *p_ev = p_data;
1331 vlc_mutex_lock( &p_ev->lock );
1332 p_ev->b_key = VLC_TRUE;
1333 vlc_mutex_unlock( &p_ev->lock );
1338 /*****************************************************************************
1339 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1340 *****************************************************************************/
1341 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1343 #ifdef HAVE_SYS_STAT_H
1344 struct stat stat_info;
1345 uint8_t pi_anchor[2];
1346 uint16_t i_tag_id = 0;
1349 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1351 /* Let dvdnav_open() do the probing */
1355 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1357 /* Let dvdnav_open() do the probing */
1361 /* Try to find the anchor (2 bytes at LBA 256) */
1362 i_ret = VLC_SUCCESS;
1363 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1365 i_ret = VLC_EGENERIC;
1368 if( read( i_fd, pi_anchor, 2 ) == 2 )
1370 i_tag_id = GetWLE(pi_anchor);
1371 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1375 i_ret = VLC_EGENERIC;