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 );
553 case DVDNAV_SPU_STREAM_CHANGE:
555 dvdnav_spu_stream_change_event_t *event =
556 (dvdnav_spu_stream_change_event_t*)packet;
557 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
558 msg_Dbg( p_demux, " - physical_wide=%d",
559 event->physical_wide );
560 msg_Dbg( p_demux, " - physical_letterbox=%d",
561 event->physical_letterbox);
562 msg_Dbg( p_demux, " - physical_pan_scan=%d",
563 event->physical_pan_scan );
565 ESSubtitleUpdate( p_demux );
568 case DVDNAV_AUDIO_STREAM_CHANGE:
570 dvdnav_audio_stream_change_event_t *event =
571 (dvdnav_audio_stream_change_event_t*)packet;
572 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
573 msg_Dbg( p_demux, " - physical=%d", event->physical );
577 case DVDNAV_VTS_CHANGE:
583 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
584 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
585 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
586 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
588 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
589 /* TODO check if we always have VTS and CELL */
590 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
593 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
595 for( i = 0; i < PS_TK_COUNT; i++ )
597 ps_track_t *tk = &p_sys->tk[i];
600 es_format_Clean( &tk->fmt );
601 if( tk->es ) es_out_Del( p_demux->out, tk->es );
603 tk->b_seen = VLC_FALSE;
606 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
607 &i_part ) == DVDNAV_STATUS_OK )
609 if( i_title >= 0 && i_title < p_sys->i_title &&
610 p_demux->info.i_title != i_title )
612 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
613 p_demux->info.i_title = i_title;
618 case DVDNAV_CELL_CHANGE:
623 dvdnav_cell_change_event_t *event =
624 (dvdnav_cell_change_event_t*)packet;
625 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
626 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
627 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
628 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
629 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
630 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
631 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
632 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
634 /* FIXME is it correct or there is better way to know chapter change */
635 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
636 &i_part ) == DVDNAV_STATUS_OK )
638 if( i_title >= 0 && i_title < p_sys->i_title &&
639 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
640 p_demux->info.i_seekpoint != i_part - 1 )
642 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
643 p_demux->info.i_seekpoint = i_part - 1;
649 case DVDNAV_NAV_PACKET:
652 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
654 /* A lot of thing to do here :
656 * - fetch pts (for time display)
659 DemuxBlock( p_demux, packet, i_len );
662 case DVDNAV_STOP: /* EOF */
663 msg_Dbg( p_demux, "DVDNAV_STOP" );
666 case DVDNAV_HIGHLIGHT:
668 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
669 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
670 msg_Dbg( p_demux, " - display=%d", event->display );
671 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
672 ButtonUpdate( p_demux );
676 case DVDNAV_SPU_CLUT_CHANGE:
680 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
681 /* Update color lookup table (16 *uint32_t in packet) */
682 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
684 /* HACK to get the SPU tracks registered in the right order */
685 for( i = 0; i < 0x1f; i++ )
687 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
688 ESNew( p_demux, 0xbd20 + i );
694 case DVDNAV_HOP_CHANNEL:
695 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
696 /* We should try to flush all our internal buffer */
700 msg_Dbg( p_demux, "DVDNAV_WAIT" );
702 dvdnav_wait_skip( p_sys->dvdnav );
706 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
711 dvdnav_free_cache_block( p_sys->dvdnav, packet );
717 static void DemuxTitles( demux_t *p_demux )
719 demux_sys_t *p_sys = p_demux->p_sys;
726 t = vlc_input_title_New();
727 t->b_menu = VLC_TRUE;
728 t->psz_name = strdup( "DVD Menu" );
730 s = vlc_seekpoint_New();
731 s->psz_name = strdup( "Resume" );
732 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
734 s = vlc_seekpoint_New();
735 s->psz_name = strdup( "Root" );
736 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
738 s = vlc_seekpoint_New();
739 s->psz_name = strdup( "Title" );
740 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
742 s = vlc_seekpoint_New();
743 s->psz_name = strdup( "Chapter" );
744 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
746 s = vlc_seekpoint_New();
747 s->psz_name = strdup( "Subtitle" );
748 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
750 s = vlc_seekpoint_New();
751 s->psz_name = strdup( "Audio" );
752 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
754 s = vlc_seekpoint_New();
755 s->psz_name = strdup( "Angle" );
756 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
758 TAB_APPEND( p_sys->i_title, p_sys->title, t );
760 /* Find out number of titles/chapters */
761 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
762 for( i = 1; i <= i_titles; i++ )
764 int32_t i_chapters = 0;
767 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
769 t = vlc_input_title_New();
770 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
772 s = vlc_seekpoint_New();
773 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
776 TAB_APPEND( p_sys->i_title, p_sys->title, t );
780 /*****************************************************************************
782 *****************************************************************************/
783 static void ButtonUpdate( demux_t *p_demux )
785 demux_sys_t *p_sys = p_demux->p_sys;
787 int32_t i_title, i_part;
789 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
791 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
793 vlc_mutex_t *p_mutex = val.p_address;
794 dvdnav_highlight_area_t hl;
797 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
798 != DVDNAV_STATUS_OK )
800 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
804 if( i_button > 0 && i_title == 0 )
806 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
808 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
810 /* I fear it is plain wrong */
811 //val.p_address = (void *)&hl.palette;
812 p_sys->alpha[0] = hl.palette&0x0f;
813 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
814 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
815 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
817 vlc_mutex_lock( p_mutex );
818 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
819 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
820 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
821 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
823 val.p_address = (void *)p_sys->alpha;
824 var_Set( p_sys->p_input, "contrast", val );
826 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
827 vlc_mutex_unlock( p_mutex );
829 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
833 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
837 vlc_mutex_lock( p_mutex );
838 val.b_bool = VLC_FALSE;
839 var_Set( p_sys->p_input, "highlight", val );
840 vlc_mutex_unlock( p_mutex );
845 static void ESSubtitleUpdate( demux_t *p_demux )
847 demux_sys_t *p_sys = p_demux->p_sys;
848 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
849 int32_t i_title, i_part;
851 ButtonUpdate( p_demux );
853 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
859 if( i_spu >= 0 && i_spu <= 0x1f )
861 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
865 ESNew( p_demux, 0xbd20 + i_spu);
867 /* be sure to unselect it (reset) */
868 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
869 (vlc_bool_t)VLC_FALSE );
872 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
876 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
878 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
881 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
882 (vlc_bool_t)VLC_FALSE );
888 /*****************************************************************************
889 * DemuxBlock: demux a given block
890 *****************************************************************************/
891 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
893 demux_sys_t *p_sys = p_demux->p_sys;
896 while( p < &pkt[i_pkt] )
898 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
906 p_pkt = block_New( p_demux, i_size );
907 memcpy( p_pkt->p_buffer, p, i_size);
909 /* Parse it and send it */
910 switch( 0x100 | p[3] )
918 msg_Warn( p_demux, "received a PSM packet" );
920 else if( p[3] == 0xbb )
922 msg_Warn( p_demux, "received a SYSTEM packet" );
925 block_Release( p_pkt );
932 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
934 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
935 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
937 block_Release( p_pkt );
942 int i_id = ps_pkt_id( p_pkt );
945 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
949 ESNew( p_demux, i_id );
951 if( tk->b_seen && tk->es &&
952 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
954 es_out_Send( p_demux->out, tk->es, p_pkt );
958 block_Release( p_pkt );
963 block_Release( p_pkt );
975 /*****************************************************************************
976 * ESNew: register a new elementary stream
977 *****************************************************************************/
978 static void ESNew( demux_t *p_demux, int i_id )
980 demux_sys_t *p_sys = p_demux->p_sys;
981 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
982 vlc_bool_t b_select = VLC_FALSE;
989 if( ps_track_fill( tk, i_id ) )
991 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
996 if( tk->fmt.i_cat == VIDEO_ES )
998 if( p_sys->i_aspect >= 0 )
1000 tk->fmt.video.i_aspect = p_sys->i_aspect;
1002 b_select = VLC_TRUE;
1004 else if( tk->fmt.i_cat == AUDIO_ES )
1007 /* find the audio number PLEASE find another way */
1008 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1010 i_audio = i_id&0x07;
1012 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1016 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1018 i_audio = i_id&0x1f;
1020 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1022 i_audio = i_id&0x1f;
1026 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1027 if( i_lang != 0xffff )
1029 tk->fmt.psz_language = malloc( 3 );
1030 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1031 tk->fmt.psz_language[1] = (i_lang )&0xff;
1032 tk->fmt.psz_language[2] = 0;
1034 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1036 b_select = VLC_TRUE;
1040 else if( tk->fmt.i_cat == SPU_ES )
1042 int32_t i_title, i_part;
1043 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1044 if( i_lang != 0xffff )
1046 tk->fmt.psz_language = malloc( 3 );
1047 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1048 tk->fmt.psz_language[1] = (i_lang )&0xff;
1049 tk->fmt.psz_language[2] = 0;
1053 tk->fmt.subs.spu.palette[0] = 0xBeef;
1054 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1055 16 * sizeof( uint32_t ) );
1057 /* We select only when we are not in the menu */
1058 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1060 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1062 b_select = VLC_TRUE;
1066 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1069 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1071 tk->b_seen = VLC_TRUE;
1074 /*****************************************************************************
1075 * Event handler code
1076 *****************************************************************************/
1077 static int EventMouse( vlc_object_t *, char const *,
1078 vlc_value_t, vlc_value_t, void * );
1079 static int EventKey ( vlc_object_t *, char const *,
1080 vlc_value_t, vlc_value_t, void * );
1082 static int EventThread( vlc_object_t *p_this )
1084 event_thread_t *p_ev = (event_thread_t*)p_this;
1085 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1086 vlc_object_t *p_vout = NULL;
1088 vlc_mutex_init( p_ev, &p_ev->lock );
1089 p_ev->b_moved = VLC_FALSE;
1090 p_ev->b_clicked = VLC_FALSE;
1091 p_ev->b_key = VLC_FALSE;
1092 p_ev->b_still = VLC_FALSE;
1094 /* catch all key event */
1095 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1098 while( !p_ev->b_die )
1100 vlc_bool_t b_activated = VLC_FALSE;
1105 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1108 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1109 int i, i_action = -1;
1111 vlc_mutex_lock( &p_ev->lock );
1112 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1113 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1115 if( p_hotkeys[i].i_key == valk.i_int )
1117 i_action = p_hotkeys[i].i_action;
1123 case ACTIONID_NAV_LEFT:
1124 dvdnav_left_button_select( p_sys->dvdnav, pci );
1126 case ACTIONID_NAV_RIGHT:
1127 dvdnav_right_button_select( p_sys->dvdnav, pci );
1129 case ACTIONID_NAV_UP:
1130 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1132 case ACTIONID_NAV_DOWN:
1133 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1135 case ACTIONID_NAV_ACTIVATE:
1136 b_activated = VLC_TRUE;
1137 dvdnav_button_activate( p_sys->dvdnav, pci );
1142 p_ev->b_key = VLC_FALSE;
1143 vlc_mutex_unlock( &p_ev->lock );
1147 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1149 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1150 vlc_value_t valx, valy;
1152 vlc_mutex_lock( &p_ev->lock );
1153 var_Get( p_vout, "mouse-x", &valx );
1154 var_Get( p_vout, "mouse-y", &valy );
1158 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1161 if( p_ev->b_clicked )
1163 b_activated = VLC_TRUE;
1164 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1168 p_ev->b_moved = VLC_FALSE;
1169 p_ev->b_clicked = VLC_FALSE;
1170 vlc_mutex_unlock( &p_ev->lock );
1172 if( p_vout && p_vout->b_die )
1174 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1175 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1176 vlc_object_release( p_vout );
1179 if( p_vout == NULL )
1181 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1185 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1186 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1191 vlc_mutex_lock( &p_ev->lock );
1194 if( /* b_activated || // This breaks menus */
1195 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1197 p_ev->b_still = VLC_FALSE;
1198 dvdnav_still_skip( p_sys->dvdnav );
1201 vlc_mutex_unlock( &p_ev->lock );
1207 /* Release callback */
1210 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1211 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1212 vlc_object_release( p_vout );
1214 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1216 vlc_mutex_destroy( &p_ev->lock );
1221 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1222 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1224 event_thread_t *p_ev = p_data;
1225 vlc_mutex_lock( &p_ev->lock );
1226 if( psz_var[6] == 'c' )
1227 p_ev->b_clicked = VLC_TRUE;
1228 else if( psz_var[6] == 'm' )
1229 p_ev->b_moved = VLC_TRUE;
1230 vlc_mutex_unlock( &p_ev->lock );
1235 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1236 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1238 event_thread_t *p_ev = p_data;
1239 vlc_mutex_lock( &p_ev->lock );
1240 p_ev->b_key = VLC_TRUE;
1241 vlc_mutex_unlock( &p_ev->lock );
1246 /*****************************************************************************
1247 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1248 *****************************************************************************/
1249 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1251 #ifdef HAVE_SYS_STAT_H
1252 struct stat stat_info;
1253 uint8_t pi_anchor[2];
1254 uint16_t i_tag_id = 0;
1257 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1259 /* Let dvdnav_open() do the probing */
1263 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1265 /* Let dvdnav_open() do the probing */
1269 /* Try to find the anchor (2 bytes at LBA 256) */
1270 i_ret = VLC_SUCCESS;
1271 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1273 i_ret = VLC_EGENERIC;
1276 if( read( i_fd, pi_anchor, 2 ) == 2 )
1278 i_tag_id = GetWLE(pi_anchor);
1279 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1283 i_ret = VLC_EGENERIC;