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_name( _("DVD with menus (DVD with menu support)") );
73 set_description( _("DVDnav Input") );
74 set_category( CAT_INPUT );
75 set_subcategory( SUBCAT_INPUT_ACCESS );
76 add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
77 ANGLE_LONGTEXT, VLC_FALSE );
78 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
79 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
80 add_bool( "dvdnav-menu", VLC_TRUE, NULL,
81 MENU_TEXT, MENU_LONGTEXT, VLC_FALSE );
82 set_capability( "access_demux", 5 );
83 add_shortcut( "dvd" );
84 add_shortcut( "dvdnav" );
85 set_callbacks( Open, Close );
88 /* Shall we use libdvdnav's read ahead cache? */
89 #define DVD_READ_CACHE 1
91 /*****************************************************************************
93 *****************************************************************************/
102 vlc_bool_t b_clicked;
110 static int EventThread( vlc_object_t * );
117 ps_track_t tk[PS_TK_COUNT];
120 /* for spu variables */
121 input_thread_t *p_input;
124 event_thread_t *p_ev;
134 input_title_t **title;
137 static int Control( demux_t *, int, va_list );
138 static int Demux( demux_t * );
139 static int DemuxBlock( demux_t *, uint8_t *, int );
141 static void DemuxTitles( demux_t * );
142 static void ESSubtitleUpdate( demux_t * );
143 static void ButtonUpdate( demux_t * );
145 static void ESNew( demux_t *, int );
146 static int ProbeDVD( demux_t *, char * );
148 /*****************************************************************************
150 *****************************************************************************/
151 static int Open( vlc_object_t *p_this )
153 demux_t *p_demux = (demux_t*)p_this;
160 if( !p_demux->psz_path || !*p_demux->psz_path )
162 /* Only when selected */
163 if( !p_this->b_force ) return VLC_EGENERIC;
165 psz_name = var_CreateGetString( p_this, "dvd" );
166 if( !psz_name || !*psz_name )
168 if( psz_name ) free( psz_name );
172 else psz_name = strdup( p_demux->psz_path );
175 if( psz_name[0] && psz_name[1] == ':' &&
176 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
179 /* Try some simple probing to avoid going through dvdnav_open too often */
180 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
187 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
189 msg_Warn( p_demux, "cannot open dvdnav" );
195 /* Fill p_demux field */
196 p_demux->pf_demux = Demux;
197 p_demux->pf_control = Control;
198 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
199 memset( p_sys, 0, sizeof( demux_sys_t ) );
200 p_sys->dvdnav = p_dvdnav;
202 ps_track_init( p_sys->tk );
203 p_sys->i_aspect = -1;
204 p_sys->i_mux_rate = 0;
208 // Hack for libdvdnav CVS.
209 // Without it dvdnav_get_number_of_titles() fails.
210 // Remove when fixed in libdvdnav CVS.
211 uint8_t buffer[DVD_VIDEO_LB_LEN];
214 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
215 == DVDNAV_STATUS_ERR )
217 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
220 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
223 /* Configure dvdnav */
224 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
227 msg_Warn( p_demux, "cannot set read-a-head flag" );
230 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
233 msg_Warn( p_demux, "cannot set PGC positioning flag" );
236 if( dvdnav_menu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
237 dvdnav_audio_language_select(p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
238 dvdnav_spu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
240 msg_Warn( p_demux, "something failed while setting en language (%s)",
241 dvdnav_err_to_string( p_sys->dvdnav ) );
244 DemuxTitles( p_demux );
246 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
247 var_Get( p_demux, "dvdnav-menu", &val );
250 msg_Dbg( p_demux, "trying to go to dvd menu" );
252 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
254 msg_Warn( p_demux, "cannot set title" );
257 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
260 msg_Warn( p_demux, "cannot go to dvd menu" );
264 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
265 var_Get( p_demux, "dvdnav-angle", &val );
266 i_angle = val.i_int > 0 ? val.i_int : 1;
268 /* Update default_pts to a suitable value for dvdnav access */
269 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
271 /* FIXME hack hack hack hack FIXME */
272 /* Get p_input and create variable */
273 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
274 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
275 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
276 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
277 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
278 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
279 var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
280 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
281 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
283 /* Now create our event thread catcher */
284 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
285 p_sys->p_ev->p_demux = p_demux;
286 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
287 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
292 /*****************************************************************************
294 *****************************************************************************/
295 static void Close( vlc_object_t *p_this )
297 demux_t *p_demux = (demux_t*)p_this;
298 demux_sys_t *p_sys = p_demux->p_sys;
301 /* stop the event handler */
302 p_sys->p_ev->b_die = VLC_TRUE;
303 vlc_thread_join( p_sys->p_ev );
304 vlc_object_destroy( p_sys->p_ev );
306 var_Destroy( p_sys->p_input, "highlight-mutex" );
307 var_Destroy( p_sys->p_input, "highlight" );
308 var_Destroy( p_sys->p_input, "x-start" );
309 var_Destroy( p_sys->p_input, "x-end" );
310 var_Destroy( p_sys->p_input, "y-start" );
311 var_Destroy( p_sys->p_input, "y-end" );
312 var_Destroy( p_sys->p_input, "color" );
313 var_Destroy( p_sys->p_input, "contrast" );
315 vlc_object_release( p_sys->p_input );
317 for( i = 0; i < PS_TK_COUNT; i++ )
319 ps_track_t *tk = &p_sys->tk[i];
322 es_format_Clean( &tk->fmt );
323 if( tk->es ) es_out_Del( p_demux->out, tk->es );
327 dvdnav_close( p_sys->dvdnav );
331 /*****************************************************************************
333 *****************************************************************************/
334 static int Control( demux_t *p_demux, int i_query, va_list args )
336 demux_sys_t *p_sys = p_demux->p_sys;
340 input_title_t ***ppp_title;
346 case DEMUX_SET_POSITION:
347 case DEMUX_GET_POSITION:
349 case DEMUX_GET_LENGTH:
352 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
353 DVDNAV_STATUS_OK || len == 0 )
358 if( i_query == DEMUX_GET_POSITION )
360 pf = (double*)va_arg( args, double* );
361 *pf = (double)pos / (double)len;
364 else if( i_query == DEMUX_SET_POSITION )
366 f = (double)va_arg( args, double );
368 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
374 else if( i_query == DEMUX_GET_TIME )
376 pi64 = (int64_t*)va_arg( args, int64_t * );
377 if( p_sys->i_mux_rate > 0 )
379 *pi64 = (int64_t)1000000 * 2048 * pos / 50 /
384 else if( i_query == DEMUX_GET_LENGTH )
386 pi64 = (int64_t*)va_arg( args, int64_t * );
387 if( p_sys->i_mux_rate > 0 )
389 *pi64 = (int64_t)1000000 * len * 2048 / 50 /
398 /* Special for access_demux */
399 case DEMUX_CAN_PAUSE:
400 case DEMUX_CAN_CONTROL_PACE:
402 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
406 case DEMUX_SET_PAUSE_STATE:
409 case DEMUX_GET_TITLE_INFO:
410 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
411 pi_int = (int*)va_arg( args, int* );
412 *((int*)va_arg( args, int* )) = 0; /* Title offset */
413 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
415 /* Duplicate title infos */
416 *pi_int = p_sys->i_title;
417 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
418 for( i = 0; i < p_sys->i_title; i++ )
420 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
424 case DEMUX_SET_TITLE:
425 i = (int)va_arg( args, int );
426 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
427 != DVDNAV_STATUS_OK ) ||
428 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
429 != DVDNAV_STATUS_OK ) )
431 msg_Warn( p_demux, "cannot set title/chapter" );
434 p_demux->info.i_update |=
435 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
436 p_demux->info.i_title = i;
437 p_demux->info.i_seekpoint = 0;
440 case DEMUX_SET_SEEKPOINT:
441 i = (int)va_arg( args, int );
442 if( p_demux->info.i_title == 0 )
449 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
452 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
455 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
458 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
461 i_ret = dvdnav_menu_call( p_sys->dvdnav,
462 DVD_MENU_Subpicture );
465 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
468 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
474 if( i_ret != DVDNAV_STATUS_OK )
477 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
478 i + 1 ) != DVDNAV_STATUS_OK )
480 msg_Warn( p_demux, "cannot set title/chapter" );
483 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
484 p_demux->info.i_seekpoint = i;
487 case DEMUX_GET_PTS_DELAY:
488 pi64 = (int64_t*)va_arg( args, int64_t * );
489 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
492 /* TODO implement others */
498 /*****************************************************************************
500 *****************************************************************************/
501 static int Demux( demux_t *p_demux )
503 demux_sys_t *p_sys = p_demux->p_sys;
505 uint8_t buffer[DVD_VIDEO_LB_LEN];
506 uint8_t *packet = buffer;
511 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
512 == DVDNAV_STATUS_ERR )
514 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
515 == DVDNAV_STATUS_ERR )
518 msg_Warn( p_demux, "cannot get next block (%s)",
519 dvdnav_err_to_string( p_sys->dvdnav ) );
525 case DVDNAV_BLOCK_OK: /* mpeg block */
526 DemuxBlock( p_demux, packet, i_len );
529 case DVDNAV_NOP: /* Nothing */
530 msg_Dbg( p_demux, "DVDNAV_NOP" );
533 case DVDNAV_STILL_FRAME:
535 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
536 vlc_mutex_lock( &p_sys->p_ev->lock );
537 if( !p_sys->p_ev->b_still )
539 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
540 msg_Dbg( p_demux, " - length=0x%x", event->length );
541 p_sys->p_ev->b_still = VLC_TRUE;
542 if( event->length == 0xff )
544 p_sys->p_ev->i_still_end = 0;
548 p_sys->p_ev->i_still_end = (int64_t)event->length *
549 1000000 + mdate() + p_sys->p_input->i_pts_delay;
552 vlc_mutex_unlock( &p_sys->p_ev->lock );
557 case DVDNAV_SPU_CLUT_CHANGE:
561 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
562 /* Update color lookup table (16 *uint32_t in packet) */
563 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
565 /* HACK to get the SPU tracks registered in the right order */
566 for( i = 0; i < 0x1f; i++ )
568 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
569 ESNew( p_demux, 0xbd20 + i );
575 case DVDNAV_SPU_STREAM_CHANGE:
577 dvdnav_spu_stream_change_event_t *event =
578 (dvdnav_spu_stream_change_event_t*)packet;
581 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
582 msg_Dbg( p_demux, " - physical_wide=%d",
583 event->physical_wide );
584 msg_Dbg( p_demux, " - physical_letterbox=%d",
585 event->physical_letterbox);
586 msg_Dbg( p_demux, " - physical_pan_scan=%d",
587 event->physical_pan_scan );
589 ESSubtitleUpdate( p_demux );
591 /* HACK to get the SPU tracks registered in the right order */
592 for( i = 0; i < 0x1f; i++ )
594 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
595 ESNew( p_demux, 0xbd20 + i );
601 case DVDNAV_AUDIO_STREAM_CHANGE:
603 dvdnav_audio_stream_change_event_t *event =
604 (dvdnav_audio_stream_change_event_t*)packet;
605 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
606 msg_Dbg( p_demux, " - physical=%d", event->physical );
611 case DVDNAV_VTS_CHANGE:
617 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
618 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
619 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
620 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
622 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
623 /* TODO check if we always have VTS and CELL */
624 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
627 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
629 for( i = 0; i < PS_TK_COUNT; i++ )
631 ps_track_t *tk = &p_sys->tk[i];
634 es_format_Clean( &tk->fmt );
635 if( tk->es ) es_out_Del( p_demux->out, tk->es );
637 tk->b_seen = VLC_FALSE;
640 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
641 &i_part ) == DVDNAV_STATUS_OK )
643 if( i_title >= 0 && i_title < p_sys->i_title &&
644 p_demux->info.i_title != i_title )
646 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
647 p_demux->info.i_title = i_title;
653 case DVDNAV_CELL_CHANGE:
658 dvdnav_cell_change_event_t *event =
659 (dvdnav_cell_change_event_t*)packet;
660 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
661 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
662 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
663 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
664 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
665 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
666 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
667 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
669 /* FIXME is it correct or there is better way to know chapter change */
670 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
671 &i_part ) == DVDNAV_STATUS_OK )
673 if( i_title >= 0 && i_title < p_sys->i_title &&
674 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
675 p_demux->info.i_seekpoint != i_part - 1 )
677 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
678 p_demux->info.i_seekpoint = i_part - 1;
684 case DVDNAV_NAV_PACKET:
687 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
689 /* A lot of thing to do here :
691 * - fetch pts (for time display)
694 DemuxBlock( p_demux, packet, i_len );
698 case DVDNAV_STOP: /* EOF */
699 msg_Dbg( p_demux, "DVDNAV_STOP" );
702 case DVDNAV_HIGHLIGHT:
704 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
705 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
706 msg_Dbg( p_demux, " - display=%d", event->display );
707 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
708 ButtonUpdate( p_demux );
712 case DVDNAV_HOP_CHANNEL:
713 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
714 /* We should try to flush all our internal buffer */
718 msg_Dbg( p_demux, "DVDNAV_WAIT" );
720 dvdnav_wait_skip( p_sys->dvdnav );
724 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
729 dvdnav_free_cache_block( p_sys->dvdnav, packet );
735 static void DemuxTitles( demux_t *p_demux )
737 demux_sys_t *p_sys = p_demux->p_sys;
744 t = vlc_input_title_New();
745 t->b_menu = VLC_TRUE;
746 t->psz_name = strdup( "DVD Menu" );
748 s = vlc_seekpoint_New();
749 s->psz_name = strdup( "Resume" );
750 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
752 s = vlc_seekpoint_New();
753 s->psz_name = strdup( "Root" );
754 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
756 s = vlc_seekpoint_New();
757 s->psz_name = strdup( "Title" );
758 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
760 s = vlc_seekpoint_New();
761 s->psz_name = strdup( "Chapter" );
762 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
764 s = vlc_seekpoint_New();
765 s->psz_name = strdup( "Subtitle" );
766 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
768 s = vlc_seekpoint_New();
769 s->psz_name = strdup( "Audio" );
770 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
772 s = vlc_seekpoint_New();
773 s->psz_name = strdup( "Angle" );
774 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
776 TAB_APPEND( p_sys->i_title, p_sys->title, t );
778 /* Find out number of titles/chapters */
779 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
780 for( i = 1; i <= i_titles; i++ )
782 int32_t i_chapters = 0;
785 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
787 t = vlc_input_title_New();
788 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
790 s = vlc_seekpoint_New();
791 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
794 TAB_APPEND( p_sys->i_title, p_sys->title, t );
798 /*****************************************************************************
800 *****************************************************************************/
801 static void ButtonUpdate( demux_t *p_demux )
803 demux_sys_t *p_sys = p_demux->p_sys;
805 int32_t i_title, i_part;
807 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
809 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
811 vlc_mutex_t *p_mutex = val.p_address;
812 dvdnav_highlight_area_t hl;
815 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
816 != DVDNAV_STATUS_OK )
818 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
822 if( i_button > 0 && i_title == 0 )
824 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
826 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
828 /* I fear it is plain wrong */
829 p_sys->alpha[0] = hl.palette&0x0f;
830 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
831 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
832 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
834 vlc_mutex_lock( p_mutex );
835 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
836 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
837 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
838 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
840 val.p_address = (void *)p_sys->alpha;
841 var_Set( p_sys->p_input, "contrast", val );
843 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
844 vlc_mutex_unlock( p_mutex );
846 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
850 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
854 vlc_mutex_lock( p_mutex );
855 val.b_bool = VLC_FALSE;
856 var_Set( p_sys->p_input, "highlight", val );
857 vlc_mutex_unlock( p_mutex );
862 static void ESSubtitleUpdate( demux_t *p_demux )
864 demux_sys_t *p_sys = p_demux->p_sys;
865 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
866 int32_t i_title, i_part;
868 ButtonUpdate( p_demux );
870 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
871 if( i_title > 0 ) return;
873 if( i_spu >= 0 && i_spu <= 0x1f )
875 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
877 ESNew( p_demux, 0xbd20 + i_spu );
879 /* be sure to unselect it (reset) */
880 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
881 (vlc_bool_t)VLC_FALSE );
884 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
888 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
890 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
893 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
894 (vlc_bool_t)VLC_FALSE );
900 /*****************************************************************************
901 * DemuxBlock: demux a given block
902 *****************************************************************************/
903 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
905 demux_sys_t *p_sys = p_demux->p_sys;
908 while( p < &pkt[i_pkt] )
910 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
918 p_pkt = block_New( p_demux, i_size );
919 memcpy( p_pkt->p_buffer, p, i_size);
921 /* Parse it and send it */
922 switch( 0x100 | p[3] )
930 msg_Warn( p_demux, "received a PSM packet" );
932 else if( p[3] == 0xbb )
934 msg_Warn( p_demux, "received a SYSTEM packet" );
937 block_Release( p_pkt );
944 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
946 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
947 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
949 block_Release( p_pkt );
954 int i_id = ps_pkt_id( p_pkt );
957 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
961 ESNew( p_demux, i_id );
963 if( tk->b_seen && tk->es &&
964 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
966 es_out_Send( p_demux->out, tk->es, p_pkt );
970 block_Release( p_pkt );
975 block_Release( p_pkt );
987 /*****************************************************************************
988 * ESNew: register a new elementary stream
989 *****************************************************************************/
990 static void ESNew( demux_t *p_demux, int i_id )
992 demux_sys_t *p_sys = p_demux->p_sys;
993 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
994 vlc_bool_t b_select = VLC_FALSE;
996 if( tk->b_seen ) return;
998 if( ps_track_fill( tk, 0, i_id ) )
1000 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1005 if( tk->fmt.i_cat == VIDEO_ES )
1007 if( p_sys->i_aspect >= 0 )
1009 tk->fmt.video.i_aspect = p_sys->i_aspect;
1011 b_select = VLC_TRUE;
1013 else if( tk->fmt.i_cat == AUDIO_ES )
1016 /* find the audio number PLEASE find another way */
1017 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1019 i_audio = i_id&0x07;
1021 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1025 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1027 i_audio = i_id&0x1f;
1029 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1031 i_audio = i_id&0x1f;
1035 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1036 if( i_lang != 0xffff )
1038 tk->fmt.psz_language = malloc( 3 );
1039 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1040 tk->fmt.psz_language[1] = (i_lang )&0xff;
1041 tk->fmt.psz_language[2] = 0;
1043 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1045 b_select = VLC_TRUE;
1049 else if( tk->fmt.i_cat == SPU_ES )
1051 int32_t i_title, i_part;
1052 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1053 if( i_lang != 0xffff )
1055 tk->fmt.psz_language = malloc( 3 );
1056 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1057 tk->fmt.psz_language[1] = (i_lang )&0xff;
1058 tk->fmt.psz_language[2] = 0;
1062 tk->fmt.subs.spu.palette[0] = 0xBeef;
1063 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1064 16 * sizeof( uint32_t ) );
1066 /* We select only when we are not in the menu */
1067 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1069 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1071 b_select = VLC_TRUE;
1075 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1078 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1080 tk->b_seen = VLC_TRUE;
1082 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux );
1085 /*****************************************************************************
1086 * Event handler code
1087 *****************************************************************************/
1088 static int EventMouse( vlc_object_t *, char const *,
1089 vlc_value_t, vlc_value_t, void * );
1090 static int EventKey ( vlc_object_t *, char const *,
1091 vlc_value_t, vlc_value_t, void * );
1093 static int EventThread( vlc_object_t *p_this )
1095 event_thread_t *p_ev = (event_thread_t*)p_this;
1096 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1097 vlc_object_t *p_vout = NULL;
1099 vlc_mutex_init( p_ev, &p_ev->lock );
1100 p_ev->b_moved = VLC_FALSE;
1101 p_ev->b_clicked = VLC_FALSE;
1102 p_ev->b_key = VLC_FALSE;
1103 p_ev->b_still = VLC_FALSE;
1105 /* catch all key event */
1106 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1109 while( !p_ev->b_die )
1111 vlc_bool_t b_activated = VLC_FALSE;
1116 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1119 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1120 int i, i_action = -1;
1122 vlc_mutex_lock( &p_ev->lock );
1123 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1124 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1126 if( p_hotkeys[i].i_key == valk.i_int )
1128 i_action = p_hotkeys[i].i_action;
1134 case ACTIONID_NAV_LEFT:
1135 dvdnav_left_button_select( p_sys->dvdnav, pci );
1137 case ACTIONID_NAV_RIGHT:
1138 dvdnav_right_button_select( p_sys->dvdnav, pci );
1140 case ACTIONID_NAV_UP:
1141 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1143 case ACTIONID_NAV_DOWN:
1144 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1146 case ACTIONID_NAV_ACTIVATE:
1147 b_activated = VLC_TRUE;
1148 dvdnav_button_activate( p_sys->dvdnav, pci );
1153 p_ev->b_key = VLC_FALSE;
1154 vlc_mutex_unlock( &p_ev->lock );
1158 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1160 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1161 vlc_value_t valx, valy;
1163 vlc_mutex_lock( &p_ev->lock );
1164 var_Get( p_vout, "mouse-x", &valx );
1165 var_Get( p_vout, "mouse-y", &valy );
1169 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1172 if( p_ev->b_clicked )
1174 b_activated = VLC_TRUE;
1175 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1179 p_ev->b_moved = VLC_FALSE;
1180 p_ev->b_clicked = VLC_FALSE;
1181 vlc_mutex_unlock( &p_ev->lock );
1183 if( p_vout && p_vout->b_die )
1185 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1186 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1187 vlc_object_release( p_vout );
1190 if( p_vout == NULL )
1192 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1196 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1197 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1202 vlc_mutex_lock( &p_ev->lock );
1205 if( /* b_activated || // This breaks menus */
1206 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1208 p_ev->b_still = VLC_FALSE;
1209 dvdnav_still_skip( p_sys->dvdnav );
1212 vlc_mutex_unlock( &p_ev->lock );
1218 /* Release callback */
1221 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1222 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1223 vlc_object_release( p_vout );
1225 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1227 vlc_mutex_destroy( &p_ev->lock );
1232 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1233 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1235 event_thread_t *p_ev = p_data;
1236 vlc_mutex_lock( &p_ev->lock );
1237 if( psz_var[6] == 'c' )
1238 p_ev->b_clicked = VLC_TRUE;
1239 else if( psz_var[6] == 'm' )
1240 p_ev->b_moved = VLC_TRUE;
1241 vlc_mutex_unlock( &p_ev->lock );
1246 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1247 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1249 event_thread_t *p_ev = p_data;
1250 vlc_mutex_lock( &p_ev->lock );
1251 p_ev->b_key = VLC_TRUE;
1252 vlc_mutex_unlock( &p_ev->lock );
1257 /*****************************************************************************
1258 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1259 *****************************************************************************/
1260 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1262 #ifdef HAVE_SYS_STAT_H
1263 struct stat stat_info;
1264 uint8_t pi_anchor[2];
1265 uint16_t i_tag_id = 0;
1268 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1270 /* Let dvdnav_open() do the probing */
1274 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1276 /* Let dvdnav_open() do the probing */
1280 /* Try to find the anchor (2 bytes at LBA 256) */
1281 i_ret = VLC_SUCCESS;
1282 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1284 i_ret = VLC_EGENERIC;
1287 if( read( i_fd, pi_anchor, 2 ) == 2 )
1289 i_tag_id = GetWLE(pi_anchor);
1290 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1294 i_ret = VLC_EGENERIC;