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 #include <dvdnav/dvdnav.h>
50 #include "../demux/ps.h"
52 /*****************************************************************************
54 *****************************************************************************/
55 #define ANGLE_TEXT N_("DVD angle")
56 #define ANGLE_LONGTEXT N_( \
57 "Allows you to select the default DVD angle." )
59 #define CACHING_TEXT N_("Caching value in ms")
60 #define CACHING_LONGTEXT N_( \
61 "Allows you to modify the default caching value for DVDnav streams. This "\
62 "value should be set in millisecond units." )
63 #define MENU_TEXT N_("Start directly in menu")
64 #define MENU_LONGTEXT N_( \
65 "Allows you to start the DVD directly in the main menu. This "\
66 "will try to skip all the useless warnings introductions." )
68 static int Open ( vlc_object_t * );
69 static void Close( vlc_object_t * );
72 set_description( _("DVDnav Input") );
73 add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
74 ANGLE_LONGTEXT, VLC_FALSE );
75 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
76 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
77 add_bool( "dvdnav-menu", VLC_TRUE, NULL,
78 MENU_TEXT, MENU_LONGTEXT, VLC_FALSE );
79 set_capability( "access_demux", 5 );
80 add_shortcut( "dvd" );
81 add_shortcut( "dvdnav" );
82 set_callbacks( Open, Close );
85 /* Shall we use libdvdnav's read ahead cache? */
86 #define DVD_READ_CACHE 1
88 /*****************************************************************************
90 *****************************************************************************/
107 static int EventThread( vlc_object_t * );
114 ps_track_t tk[PS_TK_COUNT];
117 /* for spu variables */
118 input_thread_t *p_input;
121 event_thread_t *p_ev;
131 input_title_t **title;
134 static int Control( demux_t *, int, va_list );
135 static int Demux( demux_t * );
136 static int DemuxBlock( demux_t *, uint8_t *, int );
138 static void DemuxTitles( demux_t * );
139 static void ESSubtitleUpdate( demux_t * );
140 static void ButtonUpdate( demux_t * );
142 static void ESNew( demux_t *, int );
143 static int ProbeDVD( demux_t *, char * );
145 /*****************************************************************************
147 *****************************************************************************/
148 static int Open( vlc_object_t *p_this )
150 demux_t *p_demux = (demux_t*)p_this;
157 if( !p_demux->psz_path || !*p_demux->psz_path )
159 /* Only when selected */
160 if( !p_this->b_force ) return VLC_EGENERIC;
162 psz_name = var_CreateGetString( p_this, "dvd" );
163 if( !psz_name || !*psz_name )
165 if( psz_name ) free( psz_name );
169 else psz_name = strdup( p_demux->psz_path );
172 if( psz_name[0] && psz_name[1] == ':' &&
173 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
176 /* Try some simple probing to avoid going through dvdnav_open too often */
177 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
184 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
186 msg_Warn( p_demux, "cannot open dvdnav" );
192 /* Fill p_demux field */
193 p_demux->pf_demux = Demux;
194 p_demux->pf_control = Control;
195 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
196 memset( p_sys, 0, sizeof( demux_sys_t ) );
197 p_sys->dvdnav = p_dvdnav;
199 ps_track_init( p_sys->tk );
200 p_sys->i_aspect = -1;
201 p_sys->i_mux_rate = 0;
205 // Hack for libdvdnav CVS.
206 // Without it dvdnav_get_number_of_titles() fails.
207 // Remove when fixed in libdvdnav CVS.
208 uint8_t buffer[DVD_VIDEO_LB_LEN];
211 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
212 == DVDNAV_STATUS_ERR )
214 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
217 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
220 /* Configure dvdnav */
221 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
224 msg_Warn( p_demux, "cannot set read-a-head flag" );
227 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
230 msg_Warn( p_demux, "cannot set PGC positioning flag" );
233 if( dvdnav_menu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
234 dvdnav_audio_language_select(p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
235 dvdnav_spu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
237 msg_Warn( p_demux, "something failed while setting en language (%s)",
238 dvdnav_err_to_string( p_sys->dvdnav ) );
241 DemuxTitles( p_demux );
243 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
244 var_Get( p_demux, "dvdnav-menu", &val );
247 msg_Dbg( p_demux, "trying to go to dvd menu" );
249 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
251 msg_Warn( p_demux, "cannot set title" );
254 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
257 msg_Warn( p_demux, "cannot go to dvd menu" );
261 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
262 var_Get( p_demux, "dvdnav-angle", &val );
263 i_angle = val.i_int > 0 ? val.i_int : 1;
265 /* Update default_pts to a suitable value for dvdnav access */
266 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
268 /* FIXME hack hack hack hack FIXME */
269 /* Get p_input and create variable */
270 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
271 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
272 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
273 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
274 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
275 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
276 var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
277 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
278 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
280 /* Now create our event thread catcher */
281 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
282 p_sys->p_ev->p_demux = p_demux;
283 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
284 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
289 /*****************************************************************************
291 *****************************************************************************/
292 static void Close( vlc_object_t *p_this )
294 demux_t *p_demux = (demux_t*)p_this;
295 demux_sys_t *p_sys = p_demux->p_sys;
298 /* stop the event handler */
299 p_sys->p_ev->b_die = VLC_TRUE;
300 vlc_thread_join( p_sys->p_ev );
301 vlc_object_destroy( p_sys->p_ev );
303 var_Destroy( p_sys->p_input, "highlight-mutex" );
304 var_Destroy( p_sys->p_input, "highlight" );
305 var_Destroy( p_sys->p_input, "x-start" );
306 var_Destroy( p_sys->p_input, "x-end" );
307 var_Destroy( p_sys->p_input, "y-start" );
308 var_Destroy( p_sys->p_input, "y-end" );
309 var_Destroy( p_sys->p_input, "color" );
310 var_Destroy( p_sys->p_input, "contrast" );
312 vlc_object_release( p_sys->p_input );
314 for( i = 0; i < PS_TK_COUNT; i++ )
316 ps_track_t *tk = &p_sys->tk[i];
319 es_format_Clean( &tk->fmt );
320 if( tk->es ) es_out_Del( p_demux->out, tk->es );
324 dvdnav_close( p_sys->dvdnav );
328 /*****************************************************************************
330 *****************************************************************************/
331 static int Control( demux_t *p_demux, int i_query, va_list args )
333 demux_sys_t *p_sys = p_demux->p_sys;
337 input_title_t ***ppp_title;
343 case DEMUX_SET_POSITION:
344 case DEMUX_GET_POSITION:
346 case DEMUX_GET_LENGTH:
349 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
350 DVDNAV_STATUS_OK || len == 0 )
355 if( i_query == DEMUX_GET_POSITION )
357 pf = (double*)va_arg( args, double* );
358 *pf = (double)pos / (double)len;
361 else if( i_query == DEMUX_SET_POSITION )
363 f = (double)va_arg( args, double );
365 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
371 else if( i_query == DEMUX_GET_TIME )
373 pi64 = (int64_t*)va_arg( args, int64_t * );
374 if( p_sys->i_mux_rate > 0 )
376 *pi64 = (int64_t)1000000 * 2048 * pos / 50 /
381 else if( i_query == DEMUX_GET_LENGTH )
383 pi64 = (int64_t*)va_arg( args, int64_t * );
384 if( p_sys->i_mux_rate > 0 )
386 *pi64 = (int64_t)1000000 * len * 2048 / 50 /
395 /* Special for access_demux */
396 case DEMUX_CAN_PAUSE:
397 case DEMUX_CAN_CONTROL_PACE:
399 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
403 case DEMUX_SET_PAUSE_STATE:
406 case DEMUX_GET_TITLE_INFO:
407 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
408 pi_int = (int*)va_arg( args, int* );
409 *((int*)va_arg( args, int* )) = 0; /* Title offset */
410 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
412 /* Duplicate title infos */
413 *pi_int = p_sys->i_title;
414 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
415 for( i = 0; i < p_sys->i_title; i++ )
417 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
421 case DEMUX_SET_TITLE:
422 i = (int)va_arg( args, int );
423 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
424 != DVDNAV_STATUS_OK ) ||
425 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
426 != DVDNAV_STATUS_OK ) )
428 msg_Warn( p_demux, "cannot set title/chapter" );
431 p_demux->info.i_update |=
432 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
433 p_demux->info.i_title = i;
434 p_demux->info.i_seekpoint = 0;
437 case DEMUX_SET_SEEKPOINT:
438 i = (int)va_arg( args, int );
439 if( p_demux->info.i_title == 0 )
446 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
449 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
452 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
455 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
458 i_ret = dvdnav_menu_call( p_sys->dvdnav,
459 DVD_MENU_Subpicture );
462 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
465 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
471 if( i_ret != DVDNAV_STATUS_OK )
474 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
475 i + 1 ) != DVDNAV_STATUS_OK )
477 msg_Warn( p_demux, "cannot set title/chapter" );
480 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
481 p_demux->info.i_seekpoint = i;
484 case DEMUX_GET_PTS_DELAY:
485 pi64 = (int64_t*)va_arg( args, int64_t * );
486 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
489 /* TODO implement others */
495 /*****************************************************************************
497 *****************************************************************************/
498 static int Demux( demux_t *p_demux )
500 demux_sys_t *p_sys = p_demux->p_sys;
502 uint8_t buffer[DVD_VIDEO_LB_LEN];
503 uint8_t *packet = buffer;
508 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
509 == DVDNAV_STATUS_ERR )
511 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
512 == DVDNAV_STATUS_ERR )
515 msg_Warn( p_demux, "cannot get next block (%s)",
516 dvdnav_err_to_string( p_sys->dvdnav ) );
522 case DVDNAV_BLOCK_OK: /* mpeg block */
523 DemuxBlock( p_demux, packet, i_len );
526 case DVDNAV_NOP: /* Nothing */
527 msg_Dbg( p_demux, "DVDNAV_NOP" );
530 case DVDNAV_STILL_FRAME:
532 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
533 vlc_mutex_lock( &p_sys->p_ev->lock );
534 if( !p_sys->p_ev->b_still )
536 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
537 msg_Dbg( p_demux, " - length=0x%x", event->length );
538 p_sys->p_ev->b_still = VLC_TRUE;
539 if( event->length == 0xff )
541 p_sys->p_ev->i_still_end = 0;
545 p_sys->p_ev->i_still_end = (int64_t)event->length *
546 1000000 + mdate() + p_sys->p_input->i_pts_delay;
549 vlc_mutex_unlock( &p_sys->p_ev->lock );
554 case DVDNAV_SPU_CLUT_CHANGE:
558 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
559 /* Update color lookup table (16 *uint32_t in packet) */
560 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
562 /* HACK to get the SPU tracks registered in the right order */
563 for( i = 0; i < 0x1f; i++ )
565 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
566 ESNew( p_demux, 0xbd20 + i );
572 case DVDNAV_SPU_STREAM_CHANGE:
574 dvdnav_spu_stream_change_event_t *event =
575 (dvdnav_spu_stream_change_event_t*)packet;
578 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
579 msg_Dbg( p_demux, " - physical_wide=%d",
580 event->physical_wide );
581 msg_Dbg( p_demux, " - physical_letterbox=%d",
582 event->physical_letterbox);
583 msg_Dbg( p_demux, " - physical_pan_scan=%d",
584 event->physical_pan_scan );
586 ESSubtitleUpdate( p_demux );
588 /* HACK to get the SPU tracks registered in the right order */
589 for( i = 0; i < 0x1f; i++ )
591 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
592 ESNew( p_demux, 0xbd20 + i );
598 case DVDNAV_AUDIO_STREAM_CHANGE:
600 dvdnav_audio_stream_change_event_t *event =
601 (dvdnav_audio_stream_change_event_t*)packet;
602 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
603 msg_Dbg( p_demux, " - physical=%d", event->physical );
608 case DVDNAV_VTS_CHANGE:
614 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
615 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
616 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
617 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
619 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
620 /* TODO check if we always have VTS and CELL */
621 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
624 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
626 for( i = 0; i < PS_TK_COUNT; i++ )
628 ps_track_t *tk = &p_sys->tk[i];
631 es_format_Clean( &tk->fmt );
632 if( tk->es ) es_out_Del( p_demux->out, tk->es );
634 tk->b_seen = VLC_FALSE;
637 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
638 &i_part ) == DVDNAV_STATUS_OK )
640 if( i_title >= 0 && i_title < p_sys->i_title &&
641 p_demux->info.i_title != i_title )
643 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
644 p_demux->info.i_title = i_title;
650 case DVDNAV_CELL_CHANGE:
655 dvdnav_cell_change_event_t *event =
656 (dvdnav_cell_change_event_t*)packet;
657 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
658 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
659 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
660 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
661 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
662 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
663 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
664 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
666 /* FIXME is it correct or there is better way to know chapter change */
667 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
668 &i_part ) == DVDNAV_STATUS_OK )
670 if( i_title >= 0 && i_title < p_sys->i_title &&
671 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
672 p_demux->info.i_seekpoint != i_part - 1 )
674 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
675 p_demux->info.i_seekpoint = i_part - 1;
681 case DVDNAV_NAV_PACKET:
684 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
686 /* A lot of thing to do here :
688 * - fetch pts (for time display)
691 DemuxBlock( p_demux, packet, i_len );
695 case DVDNAV_STOP: /* EOF */
696 msg_Dbg( p_demux, "DVDNAV_STOP" );
699 case DVDNAV_HIGHLIGHT:
701 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
702 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
703 msg_Dbg( p_demux, " - display=%d", event->display );
704 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
705 ButtonUpdate( p_demux );
709 case DVDNAV_HOP_CHANNEL:
710 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
711 /* We should try to flush all our internal buffer */
715 msg_Dbg( p_demux, "DVDNAV_WAIT" );
717 dvdnav_wait_skip( p_sys->dvdnav );
721 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
726 dvdnav_free_cache_block( p_sys->dvdnav, packet );
732 static void DemuxTitles( demux_t *p_demux )
734 demux_sys_t *p_sys = p_demux->p_sys;
741 t = vlc_input_title_New();
742 t->b_menu = VLC_TRUE;
743 t->psz_name = strdup( "DVD Menu" );
745 s = vlc_seekpoint_New();
746 s->psz_name = strdup( "Resume" );
747 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
749 s = vlc_seekpoint_New();
750 s->psz_name = strdup( "Root" );
751 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
753 s = vlc_seekpoint_New();
754 s->psz_name = strdup( "Title" );
755 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
757 s = vlc_seekpoint_New();
758 s->psz_name = strdup( "Chapter" );
759 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
761 s = vlc_seekpoint_New();
762 s->psz_name = strdup( "Subtitle" );
763 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
765 s = vlc_seekpoint_New();
766 s->psz_name = strdup( "Audio" );
767 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
769 s = vlc_seekpoint_New();
770 s->psz_name = strdup( "Angle" );
771 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
773 TAB_APPEND( p_sys->i_title, p_sys->title, t );
775 /* Find out number of titles/chapters */
776 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
777 for( i = 1; i <= i_titles; i++ )
779 int32_t i_chapters = 0;
782 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
784 t = vlc_input_title_New();
785 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
787 s = vlc_seekpoint_New();
788 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
791 TAB_APPEND( p_sys->i_title, p_sys->title, t );
795 /*****************************************************************************
797 *****************************************************************************/
798 static void ButtonUpdate( demux_t *p_demux )
800 demux_sys_t *p_sys = p_demux->p_sys;
802 int32_t i_title, i_part;
804 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
806 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
808 vlc_mutex_t *p_mutex = val.p_address;
809 dvdnav_highlight_area_t hl;
812 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
813 != DVDNAV_STATUS_OK )
815 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
819 if( i_button > 0 && i_title == 0 )
821 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
823 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
825 /* I fear it is plain wrong */
826 p_sys->alpha[0] = hl.palette&0x0f;
827 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
828 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
829 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
831 vlc_mutex_lock( p_mutex );
832 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
833 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
834 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
835 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
837 val.p_address = (void *)p_sys->alpha;
838 var_Set( p_sys->p_input, "contrast", val );
840 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
841 vlc_mutex_unlock( p_mutex );
843 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
847 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
851 vlc_mutex_lock( p_mutex );
852 val.b_bool = VLC_FALSE;
853 var_Set( p_sys->p_input, "highlight", val );
854 vlc_mutex_unlock( p_mutex );
859 static void ESSubtitleUpdate( demux_t *p_demux )
861 demux_sys_t *p_sys = p_demux->p_sys;
862 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
863 int32_t i_title, i_part;
865 ButtonUpdate( p_demux );
867 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
868 if( i_title > 0 ) return;
870 if( i_spu >= 0 && i_spu <= 0x1f )
872 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
874 ESNew( p_demux, 0xbd20 + i_spu );
876 /* be sure to unselect it (reset) */
877 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
878 (vlc_bool_t)VLC_FALSE );
881 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
885 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
887 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
890 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
891 (vlc_bool_t)VLC_FALSE );
897 /*****************************************************************************
898 * DemuxBlock: demux a given block
899 *****************************************************************************/
900 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
902 demux_sys_t *p_sys = p_demux->p_sys;
905 while( p < &pkt[i_pkt] )
907 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
915 p_pkt = block_New( p_demux, i_size );
916 memcpy( p_pkt->p_buffer, p, i_size);
918 /* Parse it and send it */
919 switch( 0x100 | p[3] )
927 msg_Warn( p_demux, "received a PSM packet" );
929 else if( p[3] == 0xbb )
931 msg_Warn( p_demux, "received a SYSTEM packet" );
934 block_Release( p_pkt );
941 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
943 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
944 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
946 block_Release( p_pkt );
951 int i_id = ps_pkt_id( p_pkt );
954 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
958 ESNew( p_demux, i_id );
960 if( tk->b_seen && tk->es &&
961 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
963 es_out_Send( p_demux->out, tk->es, p_pkt );
967 block_Release( p_pkt );
972 block_Release( p_pkt );
984 /*****************************************************************************
985 * ESNew: register a new elementary stream
986 *****************************************************************************/
987 static void ESNew( demux_t *p_demux, int i_id )
989 demux_sys_t *p_sys = p_demux->p_sys;
990 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
991 vlc_bool_t b_select = VLC_FALSE;
993 if( tk->b_seen ) return;
995 if( ps_track_fill( tk, 0, i_id ) )
997 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1002 if( tk->fmt.i_cat == VIDEO_ES )
1004 if( p_sys->i_aspect >= 0 )
1006 tk->fmt.video.i_aspect = p_sys->i_aspect;
1008 b_select = VLC_TRUE;
1010 else if( tk->fmt.i_cat == AUDIO_ES )
1013 /* find the audio number PLEASE find another way */
1014 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1016 i_audio = i_id&0x07;
1018 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1022 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1024 i_audio = i_id&0x1f;
1026 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1028 i_audio = i_id&0x1f;
1032 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1033 if( i_lang != 0xffff )
1035 tk->fmt.psz_language = malloc( 3 );
1036 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1037 tk->fmt.psz_language[1] = (i_lang )&0xff;
1038 tk->fmt.psz_language[2] = 0;
1040 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1042 b_select = VLC_TRUE;
1046 else if( tk->fmt.i_cat == SPU_ES )
1048 int32_t i_title, i_part;
1049 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1050 if( i_lang != 0xffff )
1052 tk->fmt.psz_language = malloc( 3 );
1053 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1054 tk->fmt.psz_language[1] = (i_lang )&0xff;
1055 tk->fmt.psz_language[2] = 0;
1059 tk->fmt.subs.spu.palette[0] = 0xBeef;
1060 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1061 16 * sizeof( uint32_t ) );
1063 /* We select only when we are not in the menu */
1064 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1066 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1068 b_select = VLC_TRUE;
1072 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1075 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1077 tk->b_seen = VLC_TRUE;
1079 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux );
1082 /*****************************************************************************
1083 * Event handler code
1084 *****************************************************************************/
1085 static int EventMouse( vlc_object_t *, char const *,
1086 vlc_value_t, vlc_value_t, void * );
1087 static int EventKey ( vlc_object_t *, char const *,
1088 vlc_value_t, vlc_value_t, void * );
1090 static int EventThread( vlc_object_t *p_this )
1092 event_thread_t *p_ev = (event_thread_t*)p_this;
1093 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1094 vlc_object_t *p_vout = NULL;
1096 vlc_mutex_init( p_ev, &p_ev->lock );
1097 p_ev->b_moved = VLC_FALSE;
1098 p_ev->b_clicked = VLC_FALSE;
1099 p_ev->b_key = VLC_FALSE;
1100 p_ev->b_still = VLC_FALSE;
1102 /* catch all key event */
1103 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1106 while( !p_ev->b_die )
1108 vlc_bool_t b_activated = VLC_FALSE;
1113 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1116 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1117 int i, i_action = -1;
1119 vlc_mutex_lock( &p_ev->lock );
1120 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1121 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1123 if( p_hotkeys[i].i_key == valk.i_int )
1125 i_action = p_hotkeys[i].i_action;
1131 case ACTIONID_NAV_LEFT:
1132 dvdnav_left_button_select( p_sys->dvdnav, pci );
1134 case ACTIONID_NAV_RIGHT:
1135 dvdnav_right_button_select( p_sys->dvdnav, pci );
1137 case ACTIONID_NAV_UP:
1138 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1140 case ACTIONID_NAV_DOWN:
1141 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1143 case ACTIONID_NAV_ACTIVATE:
1144 b_activated = VLC_TRUE;
1145 dvdnav_button_activate( p_sys->dvdnav, pci );
1150 p_ev->b_key = VLC_FALSE;
1151 vlc_mutex_unlock( &p_ev->lock );
1155 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1157 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1158 vlc_value_t valx, valy;
1160 vlc_mutex_lock( &p_ev->lock );
1161 var_Get( p_vout, "mouse-x", &valx );
1162 var_Get( p_vout, "mouse-y", &valy );
1166 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1169 if( p_ev->b_clicked )
1171 b_activated = VLC_TRUE;
1172 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1176 p_ev->b_moved = VLC_FALSE;
1177 p_ev->b_clicked = VLC_FALSE;
1178 vlc_mutex_unlock( &p_ev->lock );
1180 if( p_vout && p_vout->b_die )
1182 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1183 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1184 vlc_object_release( p_vout );
1187 if( p_vout == NULL )
1189 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1193 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1194 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1199 vlc_mutex_lock( &p_ev->lock );
1202 if( /* b_activated || // This breaks menus */
1203 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1205 p_ev->b_still = VLC_FALSE;
1206 dvdnav_still_skip( p_sys->dvdnav );
1209 vlc_mutex_unlock( &p_ev->lock );
1215 /* Release callback */
1218 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1219 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1220 vlc_object_release( p_vout );
1222 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1224 vlc_mutex_destroy( &p_ev->lock );
1229 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1230 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1232 event_thread_t *p_ev = p_data;
1233 vlc_mutex_lock( &p_ev->lock );
1234 if( psz_var[6] == 'c' )
1235 p_ev->b_clicked = VLC_TRUE;
1236 else if( psz_var[6] == 'm' )
1237 p_ev->b_moved = VLC_TRUE;
1238 vlc_mutex_unlock( &p_ev->lock );
1243 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1244 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1246 event_thread_t *p_ev = p_data;
1247 vlc_mutex_lock( &p_ev->lock );
1248 p_ev->b_key = VLC_TRUE;
1249 vlc_mutex_unlock( &p_ev->lock );
1254 /*****************************************************************************
1255 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1256 *****************************************************************************/
1257 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1259 #ifdef HAVE_SYS_STAT_H
1260 struct stat stat_info;
1261 uint8_t pi_anchor[2];
1262 uint16_t i_tag_id = 0;
1265 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1267 /* Let dvdnav_open() do the probing */
1271 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1273 /* Let dvdnav_open() do the probing */
1277 /* Try to find the anchor (2 bytes at LBA 256) */
1278 i_ret = VLC_SUCCESS;
1279 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1281 i_ret = VLC_EGENERIC;
1284 if( read( i_fd, pi_anchor, 2 ) == 2 )
1286 i_tag_id = GetWLE(pi_anchor);
1287 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1291 i_ret = VLC_EGENERIC;