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 CACHING_TEXT N_("Caching value in ms")
56 #define CACHING_LONGTEXT N_( \
57 "Allows you to modify the default caching value for DVDnav streams. This "\
58 "value should be set in millisecond units." )
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
64 set_description( _("DVDnav Input") );
65 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
66 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
67 set_capability( "access_demux", 5 );
68 add_shortcut( "dvd" );
69 add_shortcut( "dvdnav" );
70 set_callbacks( Open, Close );
73 /* Shall we use libdvdnav's read ahead cache? */
74 #define DVD_READ_CACHE 1
76 /*****************************************************************************
78 *****************************************************************************/
79 static char *ParseCL( vlc_object_t *, char *, vlc_bool_t, int *, int *, int *);
97 static int EventThread( vlc_object_t * );
104 ps_track_t tk[PS_TK_COUNT];
107 /* for spu variables */
108 input_thread_t *p_input;
111 event_thread_t *p_ev;
121 input_title_t **title;
124 static int Control( demux_t *, int, va_list );
125 static int Demux( demux_t * );
126 static int DemuxBlock( demux_t *, uint8_t *, int );
128 static void DemuxTitles( demux_t * );
129 static void ESSubtitleUpdate( demux_t * );
130 static void ButtonUpdate( demux_t * );
132 static void ESNew( demux_t *, int );
133 static int ProbeDVD( demux_t *, char * );
135 /*****************************************************************************
137 *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
140 demux_t *p_demux = (demux_t*)p_this;
143 int i_title, i_chapter, i_angle;
146 psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
147 &i_title, &i_chapter, &i_angle );
153 /* Try some simple probing to avoid going through dvdnav_open too often */
154 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
160 msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
161 psz_name, i_title, i_chapter, i_angle );
164 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
166 msg_Warn( p_demux, "cannot open dvdnav" );
172 /* Fill p_demux field */
173 p_demux->pf_demux = Demux;
174 p_demux->pf_control = Control;
175 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
176 memset( p_sys, 0, sizeof( demux_sys_t ) );
177 p_sys->dvdnav = p_dvdnav;
179 ps_track_init( p_sys->tk );
180 p_sys->i_aspect = -1;
181 p_sys->i_mux_rate = 0;
185 // Hack for libdvdnav CVS.
186 // Without it dvdnav_get_number_of_titles() fails.
187 // Remove when fixed in libdvdnav CVS.
188 uint8_t buffer[DVD_VIDEO_LB_LEN];
191 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
192 == DVDNAV_STATUS_ERR )
194 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
197 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
200 /* Configure dvdnav */
201 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
204 msg_Warn( p_demux, "cannot set read-a-head flag" );
207 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
210 msg_Warn( p_demux, "cannot set PGC positioning flag" );
213 if( dvdnav_menu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
214 dvdnav_audio_language_select(p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
215 dvdnav_spu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
217 msg_Warn( p_demux, "something failed while setting en language (%s)",
218 dvdnav_err_to_string( p_sys->dvdnav ) );
221 DemuxTitles( p_demux );
223 /* Set forced title/chapter */
226 if( dvdnav_title_play( p_sys->dvdnav, i_title ) != DVDNAV_STATUS_OK )
228 msg_Warn( p_demux, "cannot set title" );
233 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
234 p_demux->info.i_title = i_title;
238 if( i_chapter != 1 && i_title != 0 )
240 if( dvdnav_part_play( p_sys->dvdnav, i_title, i_chapter ) !=
243 msg_Warn( p_demux, "cannot set chapter" );
248 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
249 p_demux->info.i_seekpoint = i_chapter;
253 /* Update default_pts to a suitable value for dvdnav access */
254 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
256 /* FIXME hack hack hack hack FIXME */
257 /* Get p_input and create variable */
258 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
259 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
260 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
261 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
262 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
263 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
264 var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
265 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
266 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
268 /* Now create our event thread catcher */
269 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
270 p_sys->p_ev->p_demux = p_demux;
271 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
272 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
277 /*****************************************************************************
279 *****************************************************************************/
280 static void Close( vlc_object_t *p_this )
282 demux_t *p_demux = (demux_t*)p_this;
283 demux_sys_t *p_sys = p_demux->p_sys;
286 /* stop the event handler */
287 p_sys->p_ev->b_die = VLC_TRUE;
288 vlc_thread_join( p_sys->p_ev );
289 vlc_object_destroy( p_sys->p_ev );
291 var_Destroy( p_sys->p_input, "highlight-mutex" );
292 var_Destroy( p_sys->p_input, "highlight" );
293 var_Destroy( p_sys->p_input, "x-start" );
294 var_Destroy( p_sys->p_input, "x-end" );
295 var_Destroy( p_sys->p_input, "y-start" );
296 var_Destroy( p_sys->p_input, "y-end" );
297 var_Destroy( p_sys->p_input, "color" );
298 var_Destroy( p_sys->p_input, "contrast" );
300 vlc_object_release( p_sys->p_input );
302 for( i = 0; i < PS_TK_COUNT; i++ )
304 ps_track_t *tk = &p_sys->tk[i];
307 es_format_Clean( &tk->fmt );
308 if( tk->es ) es_out_Del( p_demux->out, tk->es );
312 dvdnav_close( p_sys->dvdnav );
316 /*****************************************************************************
318 *****************************************************************************/
319 static int Control( demux_t *p_demux, int i_query, va_list args )
321 demux_sys_t *p_sys = p_demux->p_sys;
325 input_title_t ***ppp_title;
331 case DEMUX_SET_POSITION:
332 case DEMUX_GET_POSITION:
334 case DEMUX_GET_LENGTH:
337 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
338 DVDNAV_STATUS_OK || len == 0 )
343 if( i_query == DEMUX_GET_POSITION )
345 pf = (double*)va_arg( args, double* );
346 *pf = (double)pos / (double)len;
349 else if( i_query == DEMUX_SET_POSITION )
351 f = (double)va_arg( args, double );
353 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
359 else if( i_query == DEMUX_GET_TIME )
361 pi64 = (int64_t*)va_arg( args, int64_t * );
362 if( p_sys->i_mux_rate > 0 )
364 *pi64 = (int64_t)1000000 * 2048 * pos / 50 /
369 else if( i_query == DEMUX_GET_LENGTH )
371 pi64 = (int64_t*)va_arg( args, int64_t * );
372 if( p_sys->i_mux_rate > 0 )
374 *pi64 = (int64_t)1000000 * len * 2048 / 50 /
383 /* Special for access_demux */
384 case DEMUX_CAN_PAUSE:
385 case DEMUX_CAN_CONTROL_PACE:
387 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
391 case DEMUX_SET_PAUSE_STATE:
394 case DEMUX_GET_TITLE_INFO:
395 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
396 pi_int = (int*)va_arg( args, int* );
398 /* Duplicate title infos */
399 *pi_int = p_sys->i_title;
400 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
401 for( i = 0; i < p_sys->i_title; i++ )
403 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
407 case DEMUX_SET_TITLE:
408 i = (int)va_arg( args, int );
409 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
410 != DVDNAV_STATUS_OK ) ||
411 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
412 != DVDNAV_STATUS_OK ) )
414 msg_Warn( p_demux, "cannot set title/chapter" );
417 p_demux->info.i_update |=
418 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
419 p_demux->info.i_title = i;
420 p_demux->info.i_seekpoint = 0;
423 case DEMUX_SET_SEEKPOINT:
424 i = (int)va_arg( args, int );
425 if( p_demux->info.i_title == 0 )
432 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
435 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
438 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
441 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
444 i_ret = dvdnav_menu_call( p_sys->dvdnav,
445 DVD_MENU_Subpicture );
448 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
451 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
457 if( i_ret != DVDNAV_STATUS_OK )
460 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
461 i + 1 ) != DVDNAV_STATUS_OK )
463 msg_Warn( p_demux, "cannot set title/chapter" );
466 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
467 p_demux->info.i_seekpoint = i;
470 case DEMUX_GET_PTS_DELAY:
471 pi64 = (int64_t*)va_arg( args, int64_t * );
472 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
475 /* TODO implement others */
481 /*****************************************************************************
483 *****************************************************************************/
484 static int Demux( demux_t *p_demux )
486 demux_sys_t *p_sys = p_demux->p_sys;
488 uint8_t buffer[DVD_VIDEO_LB_LEN];
489 uint8_t *packet = buffer;
494 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
495 == DVDNAV_STATUS_ERR )
497 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
498 == DVDNAV_STATUS_ERR )
501 msg_Warn( p_demux, "cannot get next block (%s)",
502 dvdnav_err_to_string( p_sys->dvdnav ) );
508 case DVDNAV_BLOCK_OK: /* mpeg block */
509 DemuxBlock( p_demux, packet, i_len );
512 case DVDNAV_NOP: /* Nothing */
513 msg_Dbg( p_demux, "DVDNAV_NOP" );
516 case DVDNAV_STILL_FRAME:
518 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
519 vlc_mutex_lock( &p_sys->p_ev->lock );
520 if( !p_sys->p_ev->b_still )
522 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
523 msg_Dbg( p_demux, " - length=0x%x", event->length );
524 p_sys->p_ev->b_still = VLC_TRUE;
525 if( event->length == 0xff )
527 p_sys->p_ev->i_still_end = 0;
531 p_sys->p_ev->i_still_end = (int64_t)event->length *
532 1000000 + mdate() + p_sys->p_input->i_pts_delay;
535 vlc_mutex_unlock( &p_sys->p_ev->lock );
539 case DVDNAV_SPU_STREAM_CHANGE:
541 dvdnav_spu_stream_change_event_t *event =
542 (dvdnav_spu_stream_change_event_t*)packet;
543 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
544 msg_Dbg( p_demux, " - physical_wide=%d",
545 event->physical_wide );
546 msg_Dbg( p_demux, " - physical_letterbox=%d",
547 event->physical_letterbox);
548 msg_Dbg( p_demux, " - physical_pan_scan=%d",
549 event->physical_pan_scan );
551 ESSubtitleUpdate( p_demux );
554 case DVDNAV_AUDIO_STREAM_CHANGE:
556 dvdnav_audio_stream_change_event_t *event =
557 (dvdnav_audio_stream_change_event_t*)packet;
558 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
559 msg_Dbg( p_demux, " - physical=%d", event->physical );
563 case DVDNAV_VTS_CHANGE:
569 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
570 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
571 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
572 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
574 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
575 /* TODO check if we always have VTS and CELL */
576 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
579 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
581 for( i = 0; i < PS_TK_COUNT; i++ )
583 ps_track_t *tk = &p_sys->tk[i];
586 es_format_Clean( &tk->fmt );
587 if( tk->es ) es_out_Del( p_demux->out, tk->es );
589 tk->b_seen = VLC_FALSE;
592 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
593 &i_part ) == DVDNAV_STATUS_OK )
595 if( i_title >= 0 && i_title < p_sys->i_title &&
596 p_demux->info.i_title != i_title )
598 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
599 p_demux->info.i_title = i_title;
604 case DVDNAV_CELL_CHANGE:
609 dvdnav_cell_change_event_t *event =
610 (dvdnav_cell_change_event_t*)packet;
611 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
612 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
613 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
614 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
615 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
616 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
617 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
618 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
620 /* FIXME is it correct or there is better way to know chapter change */
621 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
622 &i_part ) == DVDNAV_STATUS_OK )
624 if( i_title >= 0 && i_title < p_sys->i_title &&
625 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
626 p_demux->info.i_seekpoint != i_part - 1 )
628 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
629 p_demux->info.i_seekpoint = i_part - 1;
635 case DVDNAV_NAV_PACKET:
638 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
640 /* A lot of thing to do here :
642 * - fetch pts (for time display)
645 DemuxBlock( p_demux, packet, i_len );
648 case DVDNAV_STOP: /* EOF */
649 msg_Dbg( p_demux, "DVDNAV_STOP" );
652 case DVDNAV_HIGHLIGHT:
654 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
655 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
656 msg_Dbg( p_demux, " - display=%d", event->display );
657 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
658 ButtonUpdate( p_demux );
662 case DVDNAV_SPU_CLUT_CHANGE:
666 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
667 /* Update color lookup table (16 *uint32_t in packet) */
668 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
670 /* HACK to get the SPU tracks registered in the right order */
671 for( i = 0; i < 0x1f; i++ )
673 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
674 ESNew( p_demux, 0xbd20 + i );
680 case DVDNAV_HOP_CHANNEL:
681 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
682 /* We should try to flush all our internal buffer */
686 msg_Dbg( p_demux, "DVDNAV_WAIT" );
688 dvdnav_wait_skip( p_sys->dvdnav );
692 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
697 dvdnav_free_cache_block( p_sys->dvdnav, packet );
703 /*****************************************************************************
704 * ParseCL: parse command line.
705 * Titles start from 0 (menu), chapters and angles start from 1.
706 *****************************************************************************/
707 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
708 int *i_title, int *i_chapter, int *i_angle )
710 char *psz_parser, *psz_source, *psz_next;
712 psz_source = strdup( psz_name );
713 if( psz_source == NULL ) return NULL;
719 /* Start with the end, because you could have :
720 * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
721 * (yes, this is kludgy). */
722 for( psz_parser = psz_source + strlen(psz_source) - 1;
723 psz_parser >= psz_source && *psz_parser != '@';
726 if( psz_parser >= psz_source && *psz_parser == '@' )
732 *i_title = (int)strtol( psz_parser, &psz_next, 10 );
735 psz_parser = psz_next + 1;
736 *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
739 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
744 *i_title = *i_title >= 0 ? *i_title : 0;
745 *i_chapter = *i_chapter > 0 ? *i_chapter : 1;
746 *i_angle = *i_angle > 0 ? *i_angle : 1;
755 psz_source = config_GetPsz( p_this, "dvd" );
756 if( !psz_source ) return NULL;
760 if( psz_source[0] && psz_source[1] == ':' &&
761 psz_source[2] == '\\' && psz_source[3] == '\0' )
763 psz_source[2] = '\0';
770 static void DemuxTitles( demux_t *p_demux )
772 demux_sys_t *p_sys = p_demux->p_sys;
779 t = vlc_input_title_New();
780 t->b_menu = VLC_TRUE;
781 t->psz_name = strdup( "DVD Menu" );
783 s = vlc_seekpoint_New();
784 s->psz_name = strdup( "Resume" );
785 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
787 s = vlc_seekpoint_New();
788 s->psz_name = strdup( "Root" );
789 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
791 s = vlc_seekpoint_New();
792 s->psz_name = strdup( "Title" );
793 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
795 s = vlc_seekpoint_New();
796 s->psz_name = strdup( "Chapter" );
797 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
799 s = vlc_seekpoint_New();
800 s->psz_name = strdup( "Subtitle" );
801 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
803 s = vlc_seekpoint_New();
804 s->psz_name = strdup( "Audio" );
805 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
807 s = vlc_seekpoint_New();
808 s->psz_name = strdup( "Angle" );
809 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
811 TAB_APPEND( p_sys->i_title, p_sys->title, t );
813 /* Find out number of titles/chapters */
814 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
815 for( i = 1; i <= i_titles; i++ )
817 int32_t i_chapters = 0;
820 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
822 t = vlc_input_title_New();
823 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
825 s = vlc_seekpoint_New();
826 s->psz_name = malloc( strlen( _("Chapter %i") ) + 20 );
827 sprintf( s->psz_name, _("Chapter %i"), j + 1 );
828 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
831 TAB_APPEND( p_sys->i_title, p_sys->title, t );
835 /*****************************************************************************
837 *****************************************************************************/
838 static void ButtonUpdate( demux_t *p_demux )
840 demux_sys_t *p_sys = p_demux->p_sys;
842 int32_t i_title, i_part;
844 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
846 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
848 vlc_mutex_t *p_mutex = val.p_address;
849 dvdnav_highlight_area_t hl;
852 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
853 != DVDNAV_STATUS_OK )
855 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
859 if( i_button > 0 && i_title == 0 )
861 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
863 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
865 /* I fear it is plain wrong */
866 //val.p_address = (void *)&hl.palette;
867 p_sys->alpha[0] = hl.palette&0x0f;
868 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
869 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
870 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
872 vlc_mutex_lock( p_mutex );
873 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
874 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
875 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
876 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
878 val.p_address = (void *)p_sys->alpha;
879 var_Set( p_sys->p_input, "contrast", val );
881 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
882 vlc_mutex_unlock( p_mutex );
884 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
888 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
892 vlc_mutex_lock( p_mutex );
893 val.b_bool = VLC_FALSE;
894 var_Set( p_sys->p_input, "highlight", val );
895 vlc_mutex_unlock( p_mutex );
900 static void ESSubtitleUpdate( demux_t *p_demux )
902 demux_sys_t *p_sys = p_demux->p_sys;
903 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
904 int32_t i_title, i_part;
906 ButtonUpdate( p_demux );
908 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
914 if( i_spu >= 0 && i_spu <= 0x1f )
916 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
920 ESNew( p_demux, 0xbd20 + i_spu);
922 /* be sure to unselect it (reset) */
923 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
924 (vlc_bool_t)VLC_FALSE );
927 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
931 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
933 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
936 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
937 (vlc_bool_t)VLC_FALSE );
943 /*****************************************************************************
944 * DemuxBlock: demux a given block
945 *****************************************************************************/
946 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
948 demux_sys_t *p_sys = p_demux->p_sys;
951 while( p < &pkt[i_pkt] )
953 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
961 p_pkt = block_New( p_demux, i_size );
962 memcpy( p_pkt->p_buffer, p, i_size);
964 /* Parse it and send it */
965 switch( 0x100 | p[3] )
973 msg_Warn( p_demux, "received a PSM packet" );
975 else if( p[3] == 0xbb )
977 msg_Warn( p_demux, "received a SYSTEM packet" );
980 block_Release( p_pkt );
987 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
989 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
990 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
992 block_Release( p_pkt );
997 int i_id = ps_pkt_id( p_pkt );
1000 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1004 ESNew( p_demux, i_id );
1006 if( tk->b_seen && tk->es &&
1007 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1009 es_out_Send( p_demux->out, tk->es, p_pkt );
1013 block_Release( p_pkt );
1018 block_Release( p_pkt );
1030 /*****************************************************************************
1031 * ESNew: register a new elementary stream
1032 *****************************************************************************/
1033 static void ESNew( demux_t *p_demux, int i_id )
1035 demux_sys_t *p_sys = p_demux->p_sys;
1036 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1037 vlc_bool_t b_select = VLC_FALSE;
1044 if( ps_track_fill( tk, i_id ) )
1046 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1051 if( tk->fmt.i_cat == VIDEO_ES )
1053 if( p_sys->i_aspect >= 0 )
1055 tk->fmt.video.i_aspect = p_sys->i_aspect;
1057 b_select = VLC_TRUE;
1059 else if( tk->fmt.i_cat == AUDIO_ES )
1062 /* find the audio number PLEASE find another way */
1063 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1065 i_audio = i_id&0x07;
1067 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1071 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1073 i_audio = i_id&0x1f;
1075 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1077 i_audio = i_id&0x1f;
1081 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1082 if( i_lang != 0xffff )
1084 tk->fmt.psz_language = malloc( 3 );
1085 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1086 tk->fmt.psz_language[1] = (i_lang )&0xff;
1087 tk->fmt.psz_language[2] = 0;
1089 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1091 b_select = VLC_TRUE;
1095 else if( tk->fmt.i_cat == SPU_ES )
1097 int32_t i_title, i_part;
1098 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1099 if( i_lang != 0xffff )
1101 tk->fmt.psz_language = malloc( 3 );
1102 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1103 tk->fmt.psz_language[1] = (i_lang )&0xff;
1104 tk->fmt.psz_language[2] = 0;
1108 tk->fmt.subs.spu.palette[0] = 0xBeef;
1109 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1110 16 * sizeof( uint32_t ) );
1112 /* We select only when we are not in the menu */
1113 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1115 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1117 b_select = VLC_TRUE;
1121 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1124 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1126 tk->b_seen = VLC_TRUE;
1129 /*****************************************************************************
1130 * Event handler code
1131 *****************************************************************************/
1132 static int EventMouse( vlc_object_t *, char const *,
1133 vlc_value_t, vlc_value_t, void * );
1134 static int EventKey ( vlc_object_t *, char const *,
1135 vlc_value_t, vlc_value_t, void * );
1137 static int EventThread( vlc_object_t *p_this )
1139 event_thread_t *p_ev = (event_thread_t*)p_this;
1140 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1141 vlc_object_t *p_vout = NULL;
1143 vlc_mutex_init( p_ev, &p_ev->lock );
1144 p_ev->b_moved = VLC_FALSE;
1145 p_ev->b_clicked = VLC_FALSE;
1146 p_ev->b_key = VLC_FALSE;
1147 p_ev->b_still = VLC_FALSE;
1149 /* catch all key event */
1150 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1153 while( !p_ev->b_die )
1155 vlc_bool_t b_activated = VLC_FALSE;
1160 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1163 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1164 int i, i_action = -1;
1166 vlc_mutex_lock( &p_ev->lock );
1167 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1168 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1170 if( p_hotkeys[i].i_key == valk.i_int )
1172 i_action = p_hotkeys[i].i_action;
1178 case ACTIONID_NAV_LEFT:
1179 dvdnav_left_button_select( p_sys->dvdnav, pci );
1181 case ACTIONID_NAV_RIGHT:
1182 dvdnav_right_button_select( p_sys->dvdnav, pci );
1184 case ACTIONID_NAV_UP:
1185 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1187 case ACTIONID_NAV_DOWN:
1188 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1190 case ACTIONID_NAV_ACTIVATE:
1191 b_activated = VLC_TRUE;
1192 dvdnav_button_activate( p_sys->dvdnav, pci );
1197 p_ev->b_key = VLC_FALSE;
1198 vlc_mutex_unlock( &p_ev->lock );
1202 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1204 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1205 vlc_value_t valx, valy;
1207 vlc_mutex_lock( &p_ev->lock );
1208 var_Get( p_vout, "mouse-x", &valx );
1209 var_Get( p_vout, "mouse-y", &valy );
1213 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1216 if( p_ev->b_clicked )
1218 b_activated = VLC_TRUE;
1219 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1223 p_ev->b_moved = VLC_FALSE;
1224 p_ev->b_clicked = VLC_FALSE;
1225 vlc_mutex_unlock( &p_ev->lock );
1227 if( p_vout && p_vout->b_die )
1229 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1230 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1231 vlc_object_release( p_vout );
1234 if( p_vout == NULL )
1236 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1240 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1241 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1246 vlc_mutex_lock( &p_ev->lock );
1249 if( /* b_activated || // This breaks menus */
1250 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1252 p_ev->b_still = VLC_FALSE;
1253 dvdnav_still_skip( p_sys->dvdnav );
1256 vlc_mutex_unlock( &p_ev->lock );
1262 /* Release callback */
1265 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1266 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1267 vlc_object_release( p_vout );
1269 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1271 vlc_mutex_destroy( &p_ev->lock );
1276 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1277 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1279 event_thread_t *p_ev = p_data;
1280 vlc_mutex_lock( &p_ev->lock );
1281 if( psz_var[6] == 'c' )
1282 p_ev->b_clicked = VLC_TRUE;
1283 else if( psz_var[6] == 'm' )
1284 p_ev->b_moved = VLC_TRUE;
1285 vlc_mutex_unlock( &p_ev->lock );
1290 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1291 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1293 event_thread_t *p_ev = p_data;
1294 vlc_mutex_lock( &p_ev->lock );
1295 p_ev->b_key = VLC_TRUE;
1296 vlc_mutex_unlock( &p_ev->lock );
1301 /*****************************************************************************
1302 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1303 *****************************************************************************/
1304 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1306 #ifdef HAVE_SYS_STAT_H
1307 struct stat stat_info;
1308 uint8_t pi_anchor[2];
1309 uint16_t i_tag_id = 0;
1312 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1314 /* Let dvdnav_open() do the probing */
1318 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1320 /* Let dvdnav_open() do the probing */
1324 /* Try to find the anchor (2 bytes at LBA 256) */
1325 i_ret = VLC_SUCCESS;
1326 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1328 i_ret = VLC_EGENERIC;
1331 if( read( i_fd, pi_anchor, 2 ) == 2 )
1333 i_tag_id = GetWLE(pi_anchor);
1334 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1338 i_ret = VLC_EGENERIC;