1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc_input.h>
30 #include <vlc_access.h>
31 #include <vlc_demux.h>
33 #include <vlc_interface.h>
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
51 /* FIXME we should find a better way than including that */
52 #include "../../src/text/iso-639_def.h"
55 #include <dvdnav/dvdnav.h>
57 #include "../demux/ps.h"
59 /*****************************************************************************
61 *****************************************************************************/
62 #define ANGLE_TEXT N_("DVD angle")
63 #define ANGLE_LONGTEXT N_( \
64 "Default DVD angle." )
66 #define CACHING_TEXT N_("Caching value in ms")
67 #define CACHING_LONGTEXT N_( \
68 "Caching value for DVDs. This "\
69 "value should be set in milliseconds." )
70 #define MENU_TEXT N_("Start directly in menu")
71 #define MENU_LONGTEXT N_( \
72 "Start the DVD directly in the main menu. This "\
73 "will try to skip all the useless warning introductions." )
75 #define LANGUAGE_DEFAULT ("en")
77 static int Open ( vlc_object_t * );
78 static void Close( vlc_object_t * );
81 set_shortname( _("DVD with menus") );
82 set_description( _("DVDnav Input") );
83 set_category( CAT_INPUT );
84 set_subcategory( SUBCAT_INPUT_ACCESS );
85 add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
86 ANGLE_LONGTEXT, VLC_FALSE );
88 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
89 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
91 add_bool( "dvdnav-menu", VLC_TRUE, NULL,
92 MENU_TEXT, MENU_LONGTEXT, VLC_FALSE );
94 set_capability( "access_demux", 5 );
95 add_shortcut( "dvd" );
96 add_shortcut( "dvdnav" );
97 set_callbacks( Open, Close );
100 /* Shall we use libdvdnav's read ahead cache? */
101 #define DVD_READ_CACHE 1
103 /*****************************************************************************
105 *****************************************************************************/
114 vlc_bool_t b_clicked;
122 static int EventThread( vlc_object_t * );
129 ps_track_t tk[PS_TK_COUNT];
132 /* for spu variables */
133 input_thread_t *p_input;
136 event_thread_t *p_ev;
138 /* palette for menus */
140 uint8_t palette[4][4];
141 vlc_bool_t b_spu_change;
147 input_title_t **title;
149 /* lenght of program group chain */
150 mtime_t i_pgc_length;
153 static int Control( demux_t *, int, va_list );
154 static int Demux( demux_t * );
155 static int DemuxBlock( demux_t *, uint8_t *, int );
157 static void DemuxTitles( demux_t * );
158 static void ESSubtitleUpdate( demux_t * );
159 static void ButtonUpdate( demux_t *, vlc_bool_t );
161 static void ESNew( demux_t *, int );
162 static int ProbeDVD( demux_t *, char * );
164 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
166 /*****************************************************************************
168 *****************************************************************************/
169 static int Open( vlc_object_t *p_this )
171 demux_t *p_demux = (demux_t*)p_this;
179 if( !p_demux->psz_path || !*p_demux->psz_path )
181 /* Only when selected */
182 if( !p_this->b_force ) return VLC_EGENERIC;
184 psz_name = var_CreateGetString( p_this, "dvd" );
187 psz_name = strdup("");
191 psz_name = strdup( p_demux->psz_path );
194 if( psz_name[0] && psz_name[1] == ':' &&
195 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
198 /* Try some simple probing to avoid going through dvdnav_open too often */
199 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
206 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
208 msg_Warn( p_demux, "cannot open dvdnav" );
214 /* Fill p_demux field */
215 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
216 p_sys->dvdnav = p_dvdnav;
218 ps_track_init( p_sys->tk );
219 p_sys->i_aspect = -1;
220 p_sys->i_mux_rate = 0;
221 p_sys->i_pgc_length = 0;
222 p_sys->b_spu_change = VLC_FALSE;
226 // Hack for libdvdnav CVS.
227 // Without it dvdnav_get_number_of_titles() fails.
228 // Remove when fixed in libdvdnav CVS.
229 uint8_t buffer[DVD_VIDEO_LB_LEN];
232 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
233 == DVDNAV_STATUS_ERR )
235 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
238 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
241 /* Configure dvdnav */
242 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
245 msg_Warn( p_demux, "cannot set read-a-head flag" );
248 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
251 msg_Warn( p_demux, "cannot set PGC positioning flag" );
254 /* Set menu language ("en")
255 * XXX: maybe it would be better to set it like audio/spu
256 * or to create a --menu-language option */
257 if( dvdnav_menu_language_select( p_sys->dvdnav, LANGUAGE_DEFAULT ) !=
260 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
261 LANGUAGE_DEFAULT, dvdnav_err_to_string( p_sys->dvdnav ) );
264 /* Set audio language */
265 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
266 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
269 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
270 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
271 /* We try to fall back to 'en' */
272 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
273 dvdnav_audio_language_select( p_sys->dvdnav, LANGUAGE_DEFAULT );
277 /* Set spu language */
278 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
279 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
282 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
283 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
284 /* We try to fall back to 'en' */
285 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
286 dvdnav_spu_language_select(p_sys->dvdnav, LANGUAGE_DEFAULT );
290 DemuxTitles( p_demux );
292 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
293 var_Get( p_demux, "dvdnav-menu", &val );
296 msg_Dbg( p_demux, "trying to go to dvd menu" );
298 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
300 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
301 intf_UserFatal( p_demux, VLC_FALSE, _("Playback failure"),
302 _("VLC cannot set the DVD's title. It possibly "
303 "cannot decrypt the entire disk.") );
304 dvdnav_close( p_sys->dvdnav );
309 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
312 /* Try going to menu root */
313 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
315 msg_Warn( p_demux, "cannot go to dvd menu" );
319 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
320 var_Get( p_demux, "dvdnav-angle", &val );
321 i_angle = val.i_int > 0 ? val.i_int : 1;
323 /* Update default_pts to a suitable value for dvdnav access */
324 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
326 /* FIXME hack hack hack hack FIXME */
327 /* Get p_input and create variable */
328 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
329 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
330 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
331 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
332 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
333 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
334 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
335 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
336 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
338 /* Now create our event thread catcher */
339 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
340 p_sys->p_ev->p_demux = p_demux;
341 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
342 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
347 /*****************************************************************************
349 *****************************************************************************/
350 static void Close( vlc_object_t *p_this )
352 demux_t *p_demux = (demux_t*)p_this;
353 demux_sys_t *p_sys = p_demux->p_sys;
356 /* stop the event handler */
357 vlc_object_kill( p_sys->p_ev );
358 vlc_thread_join( p_sys->p_ev );
359 vlc_object_destroy( p_sys->p_ev );
361 var_Destroy( p_sys->p_input, "highlight-mutex" );
362 var_Destroy( p_sys->p_input, "highlight" );
363 var_Destroy( p_sys->p_input, "x-start" );
364 var_Destroy( p_sys->p_input, "x-end" );
365 var_Destroy( p_sys->p_input, "y-start" );
366 var_Destroy( p_sys->p_input, "y-end" );
367 var_Destroy( p_sys->p_input, "color" );
368 var_Destroy( p_sys->p_input, "menu-palette" );
370 vlc_object_release( p_sys->p_input );
372 for( i = 0; i < PS_TK_COUNT; i++ )
374 ps_track_t *tk = &p_sys->tk[i];
377 es_format_Clean( &tk->fmt );
378 if( tk->es ) es_out_Del( p_demux->out, tk->es );
382 dvdnav_close( p_sys->dvdnav );
386 /*****************************************************************************
388 *****************************************************************************/
389 static int Control( demux_t *p_demux, int i_query, va_list args )
391 demux_sys_t *p_sys = p_demux->p_sys;
395 input_title_t ***ppp_title;
401 case DEMUX_SET_POSITION:
402 case DEMUX_GET_POSITION:
404 case DEMUX_GET_LENGTH:
407 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
408 DVDNAV_STATUS_OK || len == 0 )
413 if( i_query == DEMUX_GET_POSITION )
415 pf = (double*)va_arg( args, double* );
416 *pf = (double)pos / (double)len;
419 else if( i_query == DEMUX_SET_POSITION )
421 f = (double)va_arg( args, double );
423 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
429 else if( i_query == DEMUX_GET_TIME )
431 pi64 = (int64_t*)va_arg( args, int64_t * );
432 if( p_sys->i_pgc_length > 0 )
434 *pi64 = p_sys->i_pgc_length * pos / len;
438 else if( i_query == DEMUX_GET_LENGTH )
440 pi64 = (int64_t*)va_arg( args, int64_t * );
441 if( p_sys->i_pgc_length > 0 )
443 *pi64 = (int64_t)p_sys->i_pgc_length;
451 /* Special for access_demux */
452 case DEMUX_CAN_PAUSE:
454 case DEMUX_CAN_CONTROL_PACE:
456 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
460 case DEMUX_SET_PAUSE_STATE:
463 case DEMUX_GET_TITLE_INFO:
464 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
465 pi_int = (int*)va_arg( args, int* );
466 *((int*)va_arg( args, int* )) = 0; /* Title offset */
467 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
469 /* Duplicate title infos */
470 *pi_int = p_sys->i_title;
471 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
472 for( i = 0; i < p_sys->i_title; i++ )
474 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
478 case DEMUX_SET_TITLE:
479 i = (int)va_arg( args, int );
480 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
481 != DVDNAV_STATUS_OK ) ||
482 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
483 != DVDNAV_STATUS_OK ) )
485 msg_Warn( p_demux, "cannot set title/chapter" );
488 p_demux->info.i_update |=
489 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
490 p_demux->info.i_title = i;
491 p_demux->info.i_seekpoint = 0;
494 case DEMUX_SET_SEEKPOINT:
495 i = (int)va_arg( args, int );
496 if( p_demux->info.i_title == 0 )
503 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
506 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
509 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
512 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
515 i_ret = dvdnav_menu_call( p_sys->dvdnav,
516 DVD_MENU_Subpicture );
519 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
522 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
528 if( i_ret != DVDNAV_STATUS_OK )
531 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
532 i + 1 ) != DVDNAV_STATUS_OK )
534 msg_Warn( p_demux, "cannot set title/chapter" );
537 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
538 p_demux->info.i_seekpoint = i;
541 case DEMUX_GET_PTS_DELAY:
542 pi64 = (int64_t*)va_arg( args, int64_t * );
543 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
548 const char *title_name = NULL;
550 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
551 if( (NULL != title_name) && ('\0' != title_name[0]) )
553 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
554 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
560 /* TODO implement others */
566 /*****************************************************************************
568 *****************************************************************************/
569 static int Demux( demux_t *p_demux )
571 demux_sys_t *p_sys = p_demux->p_sys;
573 uint8_t buffer[DVD_VIDEO_LB_LEN];
574 uint8_t *packet = buffer;
579 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
580 == DVDNAV_STATUS_ERR )
582 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
583 == DVDNAV_STATUS_ERR )
586 msg_Warn( p_demux, "cannot get next block (%s)",
587 dvdnav_err_to_string( p_sys->dvdnav ) );
593 case DVDNAV_BLOCK_OK: /* mpeg block */
594 DemuxBlock( p_demux, packet, i_len );
597 case DVDNAV_NOP: /* Nothing */
598 msg_Dbg( p_demux, "DVDNAV_NOP" );
601 case DVDNAV_STILL_FRAME:
603 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
604 vlc_mutex_lock( &p_sys->p_ev->lock );
605 if( !p_sys->p_ev->b_still )
607 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
608 msg_Dbg( p_demux, " - length=0x%x", event->length );
609 p_sys->p_ev->b_still = VLC_TRUE;
610 if( event->length == 0xff )
612 p_sys->p_ev->i_still_end = 0;
616 p_sys->p_ev->i_still_end = (int64_t)event->length *
617 1000000 + mdate() + p_sys->p_input->i_pts_delay;
620 vlc_mutex_unlock( &p_sys->p_ev->lock );
625 case DVDNAV_SPU_CLUT_CHANGE:
629 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
630 /* Update color lookup table (16 *uint32_t in packet) */
631 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
633 /* HACK to get the SPU tracks registered in the right order */
634 for( i = 0; i < 0x1f; i++ )
636 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
637 ESNew( p_demux, 0xbd20 + i );
643 case DVDNAV_SPU_STREAM_CHANGE:
645 dvdnav_spu_stream_change_event_t *event =
646 (dvdnav_spu_stream_change_event_t*)packet;
649 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
650 msg_Dbg( p_demux, " - physical_wide=%d",
651 event->physical_wide );
652 msg_Dbg( p_demux, " - physical_letterbox=%d",
653 event->physical_letterbox);
654 msg_Dbg( p_demux, " - physical_pan_scan=%d",
655 event->physical_pan_scan );
657 ESSubtitleUpdate( p_demux );
658 p_sys->b_spu_change = VLC_TRUE;
660 /* HACK to get the SPU tracks registered in the right order */
661 for( i = 0; i < 0x1f; i++ )
663 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
664 ESNew( p_demux, 0xbd20 + i );
670 case DVDNAV_AUDIO_STREAM_CHANGE:
672 dvdnav_audio_stream_change_event_t *event =
673 (dvdnav_audio_stream_change_event_t*)packet;
674 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
675 msg_Dbg( p_demux, " - physical=%d", event->physical );
680 case DVDNAV_VTS_CHANGE:
686 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
687 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
688 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
689 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
691 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
692 /* TODO check if we always have VTS and CELL */
693 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
696 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
698 for( i = 0; i < PS_TK_COUNT; i++ )
700 ps_track_t *tk = &p_sys->tk[i];
703 es_format_Clean( &tk->fmt );
704 if( tk->es ) es_out_Del( p_demux->out, tk->es );
706 tk->b_seen = VLC_FALSE;
709 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
710 &i_part ) == DVDNAV_STATUS_OK )
712 if( i_title >= 0 && i_title < p_sys->i_title &&
713 p_demux->info.i_title != i_title )
715 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
716 p_demux->info.i_title = i_title;
722 case DVDNAV_CELL_CHANGE:
727 dvdnav_cell_change_event_t *event =
728 (dvdnav_cell_change_event_t*)packet;
729 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
730 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
731 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
732 msg_Dbg( p_demux, " - cell_length="I64Fd, event->cell_length );
733 msg_Dbg( p_demux, " - pg_length="I64Fd, event->pg_length );
734 msg_Dbg( p_demux, " - pgc_length="I64Fd, event->pgc_length );
735 msg_Dbg( p_demux, " - cell_start="I64Fd, event->cell_start );
736 msg_Dbg( p_demux, " - pg_start="I64Fd, event->pg_start );
738 /* Store the lenght in time of the current PGC */
739 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
741 /* FIXME is it correct or there is better way to know chapter change */
742 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
743 &i_part ) == DVDNAV_STATUS_OK )
745 if( i_title >= 0 && i_title < p_sys->i_title &&
746 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
747 p_demux->info.i_seekpoint != i_part - 1 )
749 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
750 p_demux->info.i_seekpoint = i_part - 1;
756 case DVDNAV_NAV_PACKET:
759 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
761 /* A lot of thing to do here :
763 * - fetch pts (for time display)
766 DemuxBlock( p_demux, packet, i_len );
767 if( p_sys->b_spu_change )
769 ButtonUpdate( p_demux, VLC_FALSE );
770 p_sys->b_spu_change = VLC_FALSE;
775 case DVDNAV_STOP: /* EOF */
776 msg_Dbg( p_demux, "DVDNAV_STOP" );
779 dvdnav_free_cache_block( p_sys->dvdnav, packet );
783 case DVDNAV_HIGHLIGHT:
785 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
786 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
787 msg_Dbg( p_demux, " - display=%d", event->display );
788 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
789 ButtonUpdate( p_demux, VLC_FALSE );
793 case DVDNAV_HOP_CHANNEL:
794 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
795 /* We should try to flush all our internal buffer */
799 msg_Dbg( p_demux, "DVDNAV_WAIT" );
802 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
803 dvdnav_wait_skip( p_sys->dvdnav );
807 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
812 dvdnav_free_cache_block( p_sys->dvdnav, packet );
819 * FIXME: partiallyy duplicated from src/input/es_out.c
821 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
823 const iso639_lang_t *pl;
827 psz_lang = var_CreateGetString( p_demux, psz_var );
828 /* XXX: we will use only the first value
829 * (and ignore other ones in case of a list) */
830 if( ( p = strchr( psz_lang, ',' ) ) ) *p = '\0';
832 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
834 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
835 !strcasecmp( pl->psz_native_name, psz_lang ) ||
836 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
837 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
838 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
844 if( pl->psz_iso639_1 != NULL )
845 return strdup( pl->psz_iso639_1 );
847 return strdup(LANGUAGE_DEFAULT);
850 static void DemuxTitles( demux_t *p_demux )
852 demux_sys_t *p_sys = p_demux->p_sys;
859 t = vlc_input_title_New();
860 t->b_menu = VLC_TRUE;
861 t->psz_name = strdup( "DVD Menu" );
863 s = vlc_seekpoint_New();
864 s->psz_name = strdup( "Resume" );
865 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
867 s = vlc_seekpoint_New();
868 s->psz_name = strdup( "Root" );
869 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
871 s = vlc_seekpoint_New();
872 s->psz_name = strdup( "Title" );
873 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
875 s = vlc_seekpoint_New();
876 s->psz_name = strdup( "Chapter" );
877 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
879 s = vlc_seekpoint_New();
880 s->psz_name = strdup( "Subtitle" );
881 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
883 s = vlc_seekpoint_New();
884 s->psz_name = strdup( "Audio" );
885 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
887 s = vlc_seekpoint_New();
888 s->psz_name = strdup( "Angle" );
889 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
891 TAB_APPEND( p_sys->i_title, p_sys->title, t );
893 /* Find out number of titles/chapters */
894 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
895 for( i = 1; i <= i_titles; i++ )
897 int32_t i_chapters = 0;
900 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
902 t = vlc_input_title_New();
903 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
905 s = vlc_seekpoint_New();
906 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
909 TAB_APPEND( p_sys->i_title, p_sys->title, t );
913 /*****************************************************************************
915 *****************************************************************************/
916 static void ButtonUpdate( demux_t *p_demux, vlc_bool_t b_mode )
918 demux_sys_t *p_sys = p_demux->p_sys;
920 int32_t i_title, i_part;
922 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
924 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
926 vlc_mutex_t *p_mutex = val.p_address;
927 dvdnav_highlight_area_t hl;
930 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
931 != DVDNAV_STATUS_OK )
933 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
937 if( i_button > 0 && i_title == 0 )
940 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
942 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
944 for( i = 0; i < 4; i++ )
946 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
947 uint8_t i_alpha = (hl.palette>>(i*4))&0x0f;
948 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
950 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
951 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
952 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
953 p_sys->palette[i][3] = i_alpha;
956 vlc_mutex_lock( p_mutex );
957 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
958 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
959 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
960 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
962 val.p_address = (void *)p_sys->palette;
963 var_Set( p_sys->p_input, "menu-palette", val );
965 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
966 vlc_mutex_unlock( p_mutex );
968 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
972 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
976 vlc_mutex_lock( p_mutex );
977 val.b_bool = VLC_FALSE;
978 var_Set( p_sys->p_input, "highlight", val );
979 vlc_mutex_unlock( p_mutex );
984 static void ESSubtitleUpdate( demux_t *p_demux )
986 demux_sys_t *p_sys = p_demux->p_sys;
987 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
988 int32_t i_title, i_part;
990 ButtonUpdate( p_demux, VLC_FALSE );
992 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
993 if( i_title > 0 ) return;
995 if( i_spu >= 0 && i_spu <= 0x1f )
997 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
999 ESNew( p_demux, 0xbd20 + i_spu );
1001 /* be sure to unselect it (reset) */
1002 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1003 (vlc_bool_t)VLC_FALSE );
1006 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1010 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1012 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1015 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1016 (vlc_bool_t)VLC_FALSE );
1022 /*****************************************************************************
1023 * DemuxBlock: demux a given block
1024 *****************************************************************************/
1025 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
1027 demux_sys_t *p_sys = p_demux->p_sys;
1030 while( p < &pkt[i_pkt] )
1032 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1039 /* Create a block */
1040 p_pkt = block_New( p_demux, i_size );
1041 memcpy( p_pkt->p_buffer, p, i_size);
1043 /* Parse it and send it */
1044 switch( 0x100 | p[3] )
1052 msg_Warn( p_demux, "received a PSM packet" );
1054 else if( p[3] == 0xbb )
1056 msg_Warn( p_demux, "received a SYSTEM packet" );
1059 block_Release( p_pkt );
1066 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1068 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
1069 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1071 block_Release( p_pkt );
1076 int i_id = ps_pkt_id( p_pkt );
1079 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1083 ESNew( p_demux, i_id );
1085 if( tk->b_seen && tk->es &&
1086 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1088 es_out_Send( p_demux->out, tk->es, p_pkt );
1092 block_Release( p_pkt );
1097 block_Release( p_pkt );
1109 /*****************************************************************************
1110 * ESNew: register a new elementary stream
1111 *****************************************************************************/
1112 static void ESNew( demux_t *p_demux, int i_id )
1114 demux_sys_t *p_sys = p_demux->p_sys;
1115 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1116 vlc_bool_t b_select = VLC_FALSE;
1118 if( tk->b_seen ) return;
1120 if( ps_track_fill( tk, 0, i_id ) )
1122 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1127 if( tk->fmt.i_cat == VIDEO_ES )
1129 switch( p_sys->i_aspect )
1131 /* TODO Any docs somewhere ? */
1133 tk->fmt.video.i_aspect = 0;
1136 b_select = VLC_TRUE;
1138 else if( tk->fmt.i_cat == AUDIO_ES )
1141 /* find the audio number PLEASE find another way */
1142 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1144 i_audio = i_id&0x07;
1146 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1150 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1152 i_audio = i_id&0x1f;
1154 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1156 i_audio = i_id&0x1f;
1160 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1161 if( i_lang != 0xffff )
1163 tk->fmt.psz_language = malloc( 3 );
1164 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1165 tk->fmt.psz_language[1] = (i_lang )&0xff;
1166 tk->fmt.psz_language[2] = 0;
1168 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1170 b_select = VLC_TRUE;
1174 else if( tk->fmt.i_cat == SPU_ES )
1176 int32_t i_title, i_part;
1177 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1178 if( i_lang != 0xffff )
1180 tk->fmt.psz_language = malloc( 3 );
1181 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1182 tk->fmt.psz_language[1] = (i_lang )&0xff;
1183 tk->fmt.psz_language[2] = 0;
1187 tk->fmt.subs.spu.palette[0] = 0xBeef;
1188 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1189 16 * sizeof( uint32_t ) );
1191 /* We select only when we are not in the menu */
1192 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1194 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1196 b_select = VLC_TRUE;
1200 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1203 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1205 tk->b_seen = VLC_TRUE;
1207 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, VLC_FALSE );
1210 /*****************************************************************************
1211 * Event handler code
1212 *****************************************************************************/
1213 static int EventMouse( vlc_object_t *, char const *,
1214 vlc_value_t, vlc_value_t, void * );
1215 static int EventKey ( vlc_object_t *, char const *,
1216 vlc_value_t, vlc_value_t, void * );
1218 static int EventThread( vlc_object_t *p_this )
1220 event_thread_t *p_ev = (event_thread_t*)p_this;
1221 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1222 vlc_object_t *p_vout = NULL;
1224 vlc_mutex_init( p_ev, &p_ev->lock );
1225 p_ev->b_moved = VLC_FALSE;
1226 p_ev->b_clicked = VLC_FALSE;
1227 p_ev->b_key = VLC_FALSE;
1228 p_ev->b_still = VLC_FALSE;
1230 /* catch all key event */
1231 var_AddCallback( p_ev->p_libvlc, "key-pressed", EventKey, p_ev );
1234 while( !p_ev->b_die )
1236 vlc_bool_t b_activated = VLC_FALSE;
1241 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1244 struct hotkey *p_hotkeys = p_ev->p_libvlc->p_hotkeys;
1245 int i, i_action = -1;
1247 vlc_mutex_lock( &p_ev->lock );
1248 var_Get( p_ev->p_libvlc, "key-pressed", &valk );
1249 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1251 if( p_hotkeys[i].i_key == valk.i_int )
1253 i_action = p_hotkeys[i].i_action;
1259 case ACTIONID_NAV_LEFT:
1260 dvdnav_left_button_select( p_sys->dvdnav, pci );
1262 case ACTIONID_NAV_RIGHT:
1263 dvdnav_right_button_select( p_sys->dvdnav, pci );
1265 case ACTIONID_NAV_UP:
1266 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1268 case ACTIONID_NAV_DOWN:
1269 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1271 case ACTIONID_NAV_ACTIVATE:
1272 b_activated = VLC_TRUE;
1273 dvdnav_button_activate( p_sys->dvdnav, pci );
1274 ButtonUpdate( p_ev->p_demux, VLC_TRUE );
1279 p_ev->b_key = VLC_FALSE;
1280 vlc_mutex_unlock( &p_ev->lock );
1284 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1286 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1287 vlc_value_t valx, valy;
1289 vlc_mutex_lock( &p_ev->lock );
1290 var_Get( p_vout, "mouse-x", &valx );
1291 var_Get( p_vout, "mouse-y", &valy );
1295 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1298 if( p_ev->b_clicked )
1300 b_activated = VLC_TRUE;
1301 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1303 ButtonUpdate( p_ev->p_demux, VLC_TRUE );
1306 p_ev->b_moved = VLC_FALSE;
1307 p_ev->b_clicked = VLC_FALSE;
1308 vlc_mutex_unlock( &p_ev->lock );
1310 if( p_vout && p_vout->b_die )
1312 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1313 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1314 vlc_object_release( p_vout );
1317 if( p_vout == NULL )
1319 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1323 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1324 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1329 vlc_mutex_lock( &p_ev->lock );
1332 if( /* b_activated || // This breaks menus */
1333 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1335 p_ev->b_still = VLC_FALSE;
1336 dvdnav_still_skip( p_sys->dvdnav );
1339 vlc_mutex_unlock( &p_ev->lock );
1345 /* Release callback */
1348 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1349 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1350 vlc_object_release( p_vout );
1352 var_DelCallback( p_ev->p_libvlc, "key-pressed", EventKey, p_ev );
1354 vlc_mutex_destroy( &p_ev->lock );
1359 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1360 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1362 event_thread_t *p_ev = p_data;
1363 vlc_mutex_lock( &p_ev->lock );
1364 if( psz_var[6] == 'c' )
1365 p_ev->b_clicked = VLC_TRUE;
1366 else if( psz_var[6] == 'm' )
1367 p_ev->b_moved = VLC_TRUE;
1368 vlc_mutex_unlock( &p_ev->lock );
1373 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1374 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1376 event_thread_t *p_ev = p_data;
1377 vlc_mutex_lock( &p_ev->lock );
1378 p_ev->b_key = VLC_TRUE;
1379 vlc_mutex_unlock( &p_ev->lock );
1384 /*****************************************************************************
1385 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1386 *****************************************************************************/
1387 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1389 #ifdef HAVE_SYS_STAT_H
1390 struct stat stat_info;
1391 uint8_t pi_anchor[2];
1392 uint16_t i_tag_id = 0;
1397 /* Triggers libdvdcss autodetection */
1401 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1403 /* Let dvdnav_open() do the probing */
1407 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1409 /* Let dvdnav_open() do the probing */
1413 /* Try to find the anchor (2 bytes at LBA 256) */
1414 i_ret = VLC_SUCCESS;
1415 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1417 i_ret = VLC_EGENERIC;
1420 if( read( i_fd, pi_anchor, 2 ) == 2 )
1422 i_tag_id = GetWLE(pi_anchor);
1423 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1427 i_ret = VLC_EGENERIC;