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 *****************************************************************************/
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 /* FIXME we should find a better way than including that */
49 #include "../../src/misc/iso-639_def.h"
52 #include <dvdnav/dvdnav.h>
54 #include "../demux/ps.h"
56 /*****************************************************************************
58 *****************************************************************************/
59 #define ANGLE_TEXT N_("DVD angle")
60 #define ANGLE_LONGTEXT N_( \
61 "Default DVD angle." )
63 #define CACHING_TEXT N_("Caching value in ms")
64 #define CACHING_LONGTEXT N_( \
65 "Caching value for DVDs. This "\
66 "value should be set in milliseconds." )
67 #define MENU_TEXT N_("Start directly in menu")
68 #define MENU_LONGTEXT N_( \
69 "Start the DVD directly in the main menu. This "\
70 "will try to skip all the useless warning introductions." )
72 #define LANGUAGE_DEFAULT ("en")
74 static int Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
78 set_shortname( _("DVD with menus") );
79 set_description( _("DVDnav Input") );
80 set_category( CAT_INPUT );
81 set_subcategory( SUBCAT_INPUT_ACCESS );
82 add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
83 ANGLE_LONGTEXT, VLC_FALSE );
84 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
85 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
86 add_bool( "dvdnav-menu", VLC_TRUE, NULL,
87 MENU_TEXT, MENU_LONGTEXT, VLC_FALSE );
88 set_capability( "access_demux", 5 );
89 add_shortcut( "dvd" );
90 add_shortcut( "dvdnav" );
91 set_callbacks( Open, Close );
94 /* Shall we use libdvdnav's read ahead cache? */
95 #define DVD_READ_CACHE 1
97 /*****************************************************************************
99 *****************************************************************************/
108 vlc_bool_t b_clicked;
116 static int EventThread( vlc_object_t * );
123 ps_track_t tk[PS_TK_COUNT];
126 /* for spu variables */
127 input_thread_t *p_input;
130 event_thread_t *p_ev;
132 /* palette for menus */
134 uint8_t palette[4][4];
135 vlc_bool_t b_spu_change;
141 input_title_t **title;
143 /* lenght of program group chain */
144 mtime_t i_pgc_length;
147 static int Control( demux_t *, int, va_list );
148 static int Demux( demux_t * );
149 static int DemuxBlock( demux_t *, uint8_t *, int );
151 static void DemuxTitles( demux_t * );
152 static void ESSubtitleUpdate( demux_t * );
153 static void ButtonUpdate( demux_t *, vlc_bool_t );
155 static void ESNew( demux_t *, int );
156 static int ProbeDVD( demux_t *, char * );
158 static char *DemuxGetLanguageCode( demux_t *p_demux, char *psz_var );
160 /*****************************************************************************
162 *****************************************************************************/
163 static int Open( vlc_object_t *p_this )
165 demux_t *p_demux = (demux_t*)p_this;
173 if( !p_demux->psz_path || !*p_demux->psz_path )
175 /* Only when selected */
176 if( !p_this->b_force ) return VLC_EGENERIC;
178 psz_name = var_CreateGetString( p_this, "dvd" );
181 psz_name = strdup("");
185 psz_name = strdup( p_demux->psz_path );
188 if( psz_name[0] && psz_name[1] == ':' &&
189 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
192 /* Try some simple probing to avoid going through dvdnav_open too often */
193 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
200 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
202 msg_Warn( p_demux, "cannot open dvdnav" );
208 /* Fill p_demux field */
209 p_demux->pf_demux = Demux;
210 p_demux->pf_control = Control;
211 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
212 memset( p_sys, 0, sizeof( demux_sys_t ) );
213 p_sys->dvdnav = p_dvdnav;
215 ps_track_init( p_sys->tk );
216 p_sys->i_aspect = -1;
217 p_sys->i_mux_rate = 0;
218 p_sys->i_pgc_length = 0;
219 p_sys->b_spu_change = VLC_FALSE;
223 // Hack for libdvdnav CVS.
224 // Without it dvdnav_get_number_of_titles() fails.
225 // Remove when fixed in libdvdnav CVS.
226 uint8_t buffer[DVD_VIDEO_LB_LEN];
229 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
230 == DVDNAV_STATUS_ERR )
232 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
235 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
238 /* Configure dvdnav */
239 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
242 msg_Warn( p_demux, "cannot set read-a-head flag" );
245 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
248 msg_Warn( p_demux, "cannot set PGC positioning flag" );
251 /* Set menu language ("en")
252 * XXX: maybe it would be better to set it like audio/spu
253 * or to create a --menu-language option */
254 if( dvdnav_menu_language_select( p_sys->dvdnav,LANGUAGE_DEFAULT ) !=
257 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
258 LANGUAGE_DEFAULT, dvdnav_err_to_string( p_sys->dvdnav ) );
261 /* Set audio language */
262 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
263 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
266 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
267 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
268 /* We try to fall back to 'en' */
269 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
270 dvdnav_audio_language_select( p_sys->dvdnav, LANGUAGE_DEFAULT );
274 /* Set spu language */
275 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
276 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
279 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
280 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
281 /* We try to fall back to 'en' */
282 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
283 dvdnav_spu_language_select(p_sys->dvdnav, LANGUAGE_DEFAULT );
287 DemuxTitles( p_demux );
289 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
290 var_Get( p_demux, "dvdnav-menu", &val );
293 msg_Dbg( p_demux, "trying to go to dvd menu" );
295 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
297 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
298 dvdnav_close( p_sys->dvdnav );
303 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
306 /* Try going to menu root */
307 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
309 msg_Warn( p_demux, "cannot go to dvd menu" );
313 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
314 var_Get( p_demux, "dvdnav-angle", &val );
315 i_angle = val.i_int > 0 ? val.i_int : 1;
317 /* Update default_pts to a suitable value for dvdnav access */
318 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
320 /* FIXME hack hack hack hack FIXME */
321 /* Get p_input and create variable */
322 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
323 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
324 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
325 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
326 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
327 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
328 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
329 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
330 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
332 /* Now create our event thread catcher */
333 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
334 p_sys->p_ev->p_demux = p_demux;
335 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
336 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
341 /*****************************************************************************
343 *****************************************************************************/
344 static void Close( vlc_object_t *p_this )
346 demux_t *p_demux = (demux_t*)p_this;
347 demux_sys_t *p_sys = p_demux->p_sys;
350 /* stop the event handler */
351 p_sys->p_ev->b_die = VLC_TRUE;
352 vlc_thread_join( p_sys->p_ev );
353 vlc_object_destroy( p_sys->p_ev );
355 var_Destroy( p_sys->p_input, "highlight-mutex" );
356 var_Destroy( p_sys->p_input, "highlight" );
357 var_Destroy( p_sys->p_input, "x-start" );
358 var_Destroy( p_sys->p_input, "x-end" );
359 var_Destroy( p_sys->p_input, "y-start" );
360 var_Destroy( p_sys->p_input, "y-end" );
361 var_Destroy( p_sys->p_input, "color" );
362 var_Destroy( p_sys->p_input, "menu-palette" );
364 vlc_object_release( p_sys->p_input );
366 for( i = 0; i < PS_TK_COUNT; i++ )
368 ps_track_t *tk = &p_sys->tk[i];
371 es_format_Clean( &tk->fmt );
372 if( tk->es ) es_out_Del( p_demux->out, tk->es );
376 dvdnav_close( p_sys->dvdnav );
380 /*****************************************************************************
382 *****************************************************************************/
383 static int Control( demux_t *p_demux, int i_query, va_list args )
385 demux_sys_t *p_sys = p_demux->p_sys;
389 input_title_t ***ppp_title;
395 case DEMUX_SET_POSITION:
396 case DEMUX_GET_POSITION:
398 case DEMUX_GET_LENGTH:
401 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
402 DVDNAV_STATUS_OK || len == 0 )
407 if( i_query == DEMUX_GET_POSITION )
409 pf = (double*)va_arg( args, double* );
410 *pf = (double)pos / (double)len;
413 else if( i_query == DEMUX_SET_POSITION )
415 f = (double)va_arg( args, double );
417 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
423 else if( i_query == DEMUX_GET_TIME )
425 pi64 = (int64_t*)va_arg( args, int64_t * );
426 if( p_sys->i_pgc_length > 0 )
428 *pi64 = p_sys->i_pgc_length * pos / len;
432 else if( i_query == DEMUX_GET_LENGTH )
434 pi64 = (int64_t*)va_arg( args, int64_t * );
435 if( p_sys->i_pgc_length > 0 )
437 *pi64 = (int64_t)p_sys->i_pgc_length;
445 /* Special for access_demux */
446 case DEMUX_CAN_PAUSE:
447 case DEMUX_CAN_CONTROL_PACE:
449 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
453 case DEMUX_SET_PAUSE_STATE:
456 case DEMUX_GET_TITLE_INFO:
457 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
458 pi_int = (int*)va_arg( args, int* );
459 *((int*)va_arg( args, int* )) = 0; /* Title offset */
460 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
462 /* Duplicate title infos */
463 *pi_int = p_sys->i_title;
464 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
465 for( i = 0; i < p_sys->i_title; i++ )
467 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
471 case DEMUX_SET_TITLE:
472 i = (int)va_arg( args, int );
473 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
474 != DVDNAV_STATUS_OK ) ||
475 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
476 != DVDNAV_STATUS_OK ) )
478 msg_Warn( p_demux, "cannot set title/chapter" );
481 p_demux->info.i_update |=
482 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
483 p_demux->info.i_title = i;
484 p_demux->info.i_seekpoint = 0;
487 case DEMUX_SET_SEEKPOINT:
488 i = (int)va_arg( args, int );
489 if( p_demux->info.i_title == 0 )
496 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
499 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
502 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
505 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
508 i_ret = dvdnav_menu_call( p_sys->dvdnav,
509 DVD_MENU_Subpicture );
512 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
515 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
521 if( i_ret != DVDNAV_STATUS_OK )
524 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
525 i + 1 ) != DVDNAV_STATUS_OK )
527 msg_Warn( p_demux, "cannot set title/chapter" );
530 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
531 p_demux->info.i_seekpoint = i;
534 case DEMUX_GET_PTS_DELAY:
535 pi64 = (int64_t*)va_arg( args, int64_t * );
536 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
541 const char *title_name = NULL;
543 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
544 if( (NULL != title_name) && ('\0' != title_name[0]) )
546 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
547 vlc_meta_SetTitle( p_meta, title_name );
553 /* TODO implement others */
559 /*****************************************************************************
561 *****************************************************************************/
562 static int Demux( demux_t *p_demux )
564 demux_sys_t *p_sys = p_demux->p_sys;
566 uint8_t buffer[DVD_VIDEO_LB_LEN];
567 uint8_t *packet = buffer;
572 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
573 == DVDNAV_STATUS_ERR )
575 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
576 == DVDNAV_STATUS_ERR )
579 msg_Warn( p_demux, "cannot get next block (%s)",
580 dvdnav_err_to_string( p_sys->dvdnav ) );
586 case DVDNAV_BLOCK_OK: /* mpeg block */
587 DemuxBlock( p_demux, packet, i_len );
590 case DVDNAV_NOP: /* Nothing */
591 msg_Dbg( p_demux, "DVDNAV_NOP" );
594 case DVDNAV_STILL_FRAME:
596 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
597 vlc_mutex_lock( &p_sys->p_ev->lock );
598 if( !p_sys->p_ev->b_still )
600 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
601 msg_Dbg( p_demux, " - length=0x%x", event->length );
602 p_sys->p_ev->b_still = VLC_TRUE;
603 if( event->length == 0xff )
605 p_sys->p_ev->i_still_end = 0;
609 p_sys->p_ev->i_still_end = (int64_t)event->length *
610 1000000 + mdate() + p_sys->p_input->i_pts_delay;
613 vlc_mutex_unlock( &p_sys->p_ev->lock );
618 case DVDNAV_SPU_CLUT_CHANGE:
622 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
623 /* Update color lookup table (16 *uint32_t in packet) */
624 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
626 /* HACK to get the SPU tracks registered in the right order */
627 for( i = 0; i < 0x1f; i++ )
629 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
630 ESNew( p_demux, 0xbd20 + i );
636 case DVDNAV_SPU_STREAM_CHANGE:
638 dvdnav_spu_stream_change_event_t *event =
639 (dvdnav_spu_stream_change_event_t*)packet;
642 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
643 msg_Dbg( p_demux, " - physical_wide=%d",
644 event->physical_wide );
645 msg_Dbg( p_demux, " - physical_letterbox=%d",
646 event->physical_letterbox);
647 msg_Dbg( p_demux, " - physical_pan_scan=%d",
648 event->physical_pan_scan );
650 ESSubtitleUpdate( p_demux );
651 p_sys->b_spu_change = VLC_TRUE;
653 /* HACK to get the SPU tracks registered in the right order */
654 for( i = 0; i < 0x1f; i++ )
656 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
657 ESNew( p_demux, 0xbd20 + i );
663 case DVDNAV_AUDIO_STREAM_CHANGE:
665 dvdnav_audio_stream_change_event_t *event =
666 (dvdnav_audio_stream_change_event_t*)packet;
667 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
668 msg_Dbg( p_demux, " - physical=%d", event->physical );
673 case DVDNAV_VTS_CHANGE:
679 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
680 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
681 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
682 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
684 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
685 /* TODO check if we always have VTS and CELL */
686 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
689 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
691 for( i = 0; i < PS_TK_COUNT; i++ )
693 ps_track_t *tk = &p_sys->tk[i];
696 es_format_Clean( &tk->fmt );
697 if( tk->es ) es_out_Del( p_demux->out, tk->es );
699 tk->b_seen = VLC_FALSE;
702 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
703 &i_part ) == DVDNAV_STATUS_OK )
705 if( i_title >= 0 && i_title < p_sys->i_title &&
706 p_demux->info.i_title != i_title )
708 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
709 p_demux->info.i_title = i_title;
715 case DVDNAV_CELL_CHANGE:
720 dvdnav_cell_change_event_t *event =
721 (dvdnav_cell_change_event_t*)packet;
722 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
723 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
724 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
725 msg_Dbg( p_demux, " - cell_length="I64Fd, event->cell_length );
726 msg_Dbg( p_demux, " - pg_length="I64Fd, event->pg_length );
727 msg_Dbg( p_demux, " - pgc_length="I64Fd, event->pgc_length );
728 msg_Dbg( p_demux, " - cell_start="I64Fd, event->cell_start );
729 msg_Dbg( p_demux, " - pg_start="I64Fd, event->pg_start );
731 /* Store the lenght in time of the current PGC */
732 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
734 /* FIXME is it correct or there is better way to know chapter change */
735 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
736 &i_part ) == DVDNAV_STATUS_OK )
738 if( i_title >= 0 && i_title < p_sys->i_title &&
739 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
740 p_demux->info.i_seekpoint != i_part - 1 )
742 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
743 p_demux->info.i_seekpoint = i_part - 1;
749 case DVDNAV_NAV_PACKET:
752 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
754 /* A lot of thing to do here :
756 * - fetch pts (for time display)
759 DemuxBlock( p_demux, packet, i_len );
760 if( p_sys->b_spu_change )
762 ButtonUpdate( p_demux, VLC_FALSE );
763 p_sys->b_spu_change = VLC_FALSE;
768 case DVDNAV_STOP: /* EOF */
769 msg_Dbg( p_demux, "DVDNAV_STOP" );
772 dvdnav_free_cache_block( p_sys->dvdnav, packet );
776 case DVDNAV_HIGHLIGHT:
778 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
779 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
780 msg_Dbg( p_demux, " - display=%d", event->display );
781 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
782 ButtonUpdate( p_demux, VLC_FALSE );
786 case DVDNAV_HOP_CHANNEL:
787 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
788 /* We should try to flush all our internal buffer */
792 msg_Dbg( p_demux, "DVDNAV_WAIT" );
795 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
796 dvdnav_wait_skip( p_sys->dvdnav );
800 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
805 dvdnav_free_cache_block( p_sys->dvdnav, packet );
812 * FIXME: partiallyy duplicated from src/input/es_out.c
814 static char *DemuxGetLanguageCode( demux_t *p_demux, char *psz_var )
816 const iso639_lang_t *pl;
820 psz_lang = var_CreateGetString( p_demux, psz_var );
821 /* XXX: we will use only the first value
822 * (and ignore other ones in case of a list) */
823 if( ( p = strchr( psz_lang, ',' ) ) ) *p = '\0';
825 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
827 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
828 !strcasecmp( pl->psz_native_name, psz_lang ) ||
829 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
830 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
831 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
837 if( pl->psz_iso639_1 != NULL )
838 return strdup( pl->psz_iso639_1 );
840 return strdup(LANGUAGE_DEFAULT);
843 static void DemuxTitles( demux_t *p_demux )
845 demux_sys_t *p_sys = p_demux->p_sys;
852 t = vlc_input_title_New();
853 t->b_menu = VLC_TRUE;
854 t->psz_name = strdup( "DVD Menu" );
856 s = vlc_seekpoint_New();
857 s->psz_name = strdup( "Resume" );
858 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
860 s = vlc_seekpoint_New();
861 s->psz_name = strdup( "Root" );
862 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
864 s = vlc_seekpoint_New();
865 s->psz_name = strdup( "Title" );
866 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
868 s = vlc_seekpoint_New();
869 s->psz_name = strdup( "Chapter" );
870 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
872 s = vlc_seekpoint_New();
873 s->psz_name = strdup( "Subtitle" );
874 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
876 s = vlc_seekpoint_New();
877 s->psz_name = strdup( "Audio" );
878 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
880 s = vlc_seekpoint_New();
881 s->psz_name = strdup( "Angle" );
882 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
884 TAB_APPEND( p_sys->i_title, p_sys->title, t );
886 /* Find out number of titles/chapters */
887 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
888 for( i = 1; i <= i_titles; i++ )
890 int32_t i_chapters = 0;
893 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
895 t = vlc_input_title_New();
896 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
898 s = vlc_seekpoint_New();
899 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
902 TAB_APPEND( p_sys->i_title, p_sys->title, t );
906 /*****************************************************************************
908 *****************************************************************************/
909 static void ButtonUpdate( demux_t *p_demux, vlc_bool_t b_mode )
911 demux_sys_t *p_sys = p_demux->p_sys;
913 int32_t i_title, i_part;
915 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
917 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
919 vlc_mutex_t *p_mutex = val.p_address;
920 dvdnav_highlight_area_t hl;
923 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
924 != DVDNAV_STATUS_OK )
926 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
930 if( i_button > 0 && i_title == 0 )
933 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
935 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
937 for( i = 0; i < 4; i++ )
939 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
940 uint8_t i_alpha = (hl.palette>>(i*4))&0x0f;
941 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
943 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
944 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
945 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
946 p_sys->palette[i][3] = i_alpha;
949 vlc_mutex_lock( p_mutex );
950 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
951 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
952 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
953 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
955 val.p_address = (void *)p_sys->palette;
956 var_Set( p_sys->p_input, "menu-palette", val );
958 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
959 vlc_mutex_unlock( p_mutex );
961 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
965 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
969 vlc_mutex_lock( p_mutex );
970 val.b_bool = VLC_FALSE;
971 var_Set( p_sys->p_input, "highlight", val );
972 vlc_mutex_unlock( p_mutex );
977 static void ESSubtitleUpdate( demux_t *p_demux )
979 demux_sys_t *p_sys = p_demux->p_sys;
980 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
981 int32_t i_title, i_part;
983 ButtonUpdate( p_demux, VLC_FALSE );
985 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
986 if( i_title > 0 ) return;
988 if( i_spu >= 0 && i_spu <= 0x1f )
990 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
992 ESNew( p_demux, 0xbd20 + i_spu );
994 /* be sure to unselect it (reset) */
995 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
996 (vlc_bool_t)VLC_FALSE );
999 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1003 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1005 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1008 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1009 (vlc_bool_t)VLC_FALSE );
1015 /*****************************************************************************
1016 * DemuxBlock: demux a given block
1017 *****************************************************************************/
1018 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
1020 demux_sys_t *p_sys = p_demux->p_sys;
1023 while( p < &pkt[i_pkt] )
1025 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1032 /* Create a block */
1033 p_pkt = block_New( p_demux, i_size );
1034 memcpy( p_pkt->p_buffer, p, i_size);
1036 /* Parse it and send it */
1037 switch( 0x100 | p[3] )
1045 msg_Warn( p_demux, "received a PSM packet" );
1047 else if( p[3] == 0xbb )
1049 msg_Warn( p_demux, "received a SYSTEM packet" );
1052 block_Release( p_pkt );
1059 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1061 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
1062 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1064 block_Release( p_pkt );
1069 int i_id = ps_pkt_id( p_pkt );
1072 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1076 ESNew( p_demux, i_id );
1078 if( tk->b_seen && tk->es &&
1079 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1081 es_out_Send( p_demux->out, tk->es, p_pkt );
1085 block_Release( p_pkt );
1090 block_Release( p_pkt );
1102 /*****************************************************************************
1103 * ESNew: register a new elementary stream
1104 *****************************************************************************/
1105 static void ESNew( demux_t *p_demux, int i_id )
1107 demux_sys_t *p_sys = p_demux->p_sys;
1108 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1109 vlc_bool_t b_select = VLC_FALSE;
1111 if( tk->b_seen ) return;
1113 if( ps_track_fill( tk, 0, i_id ) )
1115 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1120 if( tk->fmt.i_cat == VIDEO_ES )
1122 if( p_sys->i_aspect >= 0 )
1124 tk->fmt.video.i_aspect = p_sys->i_aspect;
1126 b_select = VLC_TRUE;
1128 else if( tk->fmt.i_cat == AUDIO_ES )
1131 /* find the audio number PLEASE find another way */
1132 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1134 i_audio = i_id&0x07;
1136 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1140 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1142 i_audio = i_id&0x1f;
1144 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1146 i_audio = i_id&0x1f;
1150 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1151 if( i_lang != 0xffff )
1153 tk->fmt.psz_language = malloc( 3 );
1154 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1155 tk->fmt.psz_language[1] = (i_lang )&0xff;
1156 tk->fmt.psz_language[2] = 0;
1158 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1160 b_select = VLC_TRUE;
1164 else if( tk->fmt.i_cat == SPU_ES )
1166 int32_t i_title, i_part;
1167 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1168 if( i_lang != 0xffff )
1170 tk->fmt.psz_language = malloc( 3 );
1171 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1172 tk->fmt.psz_language[1] = (i_lang )&0xff;
1173 tk->fmt.psz_language[2] = 0;
1177 tk->fmt.subs.spu.palette[0] = 0xBeef;
1178 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1179 16 * sizeof( uint32_t ) );
1181 /* We select only when we are not in the menu */
1182 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1184 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1186 b_select = VLC_TRUE;
1190 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1193 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1195 tk->b_seen = VLC_TRUE;
1197 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, VLC_FALSE );
1200 /*****************************************************************************
1201 * Event handler code
1202 *****************************************************************************/
1203 static int EventMouse( vlc_object_t *, char const *,
1204 vlc_value_t, vlc_value_t, void * );
1205 static int EventKey ( vlc_object_t *, char const *,
1206 vlc_value_t, vlc_value_t, void * );
1208 static int EventThread( vlc_object_t *p_this )
1210 event_thread_t *p_ev = (event_thread_t*)p_this;
1211 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1212 vlc_object_t *p_vout = NULL;
1214 vlc_mutex_init( p_ev, &p_ev->lock );
1215 p_ev->b_moved = VLC_FALSE;
1216 p_ev->b_clicked = VLC_FALSE;
1217 p_ev->b_key = VLC_FALSE;
1218 p_ev->b_still = VLC_FALSE;
1220 /* catch all key event */
1221 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1224 while( !p_ev->b_die )
1226 vlc_bool_t b_activated = VLC_FALSE;
1231 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1234 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1235 int i, i_action = -1;
1237 vlc_mutex_lock( &p_ev->lock );
1238 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1239 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1241 if( p_hotkeys[i].i_key == valk.i_int )
1243 i_action = p_hotkeys[i].i_action;
1249 case ACTIONID_NAV_LEFT:
1250 dvdnav_left_button_select( p_sys->dvdnav, pci );
1252 case ACTIONID_NAV_RIGHT:
1253 dvdnav_right_button_select( p_sys->dvdnav, pci );
1255 case ACTIONID_NAV_UP:
1256 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1258 case ACTIONID_NAV_DOWN:
1259 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1261 case ACTIONID_NAV_ACTIVATE:
1262 b_activated = VLC_TRUE;
1263 dvdnav_button_activate( p_sys->dvdnav, pci );
1264 ButtonUpdate( p_ev->p_demux, VLC_TRUE );
1269 p_ev->b_key = VLC_FALSE;
1270 vlc_mutex_unlock( &p_ev->lock );
1274 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1276 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1277 vlc_value_t valx, valy;
1279 vlc_mutex_lock( &p_ev->lock );
1280 var_Get( p_vout, "mouse-x", &valx );
1281 var_Get( p_vout, "mouse-y", &valy );
1285 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1288 if( p_ev->b_clicked )
1290 b_activated = VLC_TRUE;
1291 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1293 ButtonUpdate( p_ev->p_demux, VLC_TRUE );
1296 p_ev->b_moved = VLC_FALSE;
1297 p_ev->b_clicked = VLC_FALSE;
1298 vlc_mutex_unlock( &p_ev->lock );
1300 if( p_vout && p_vout->b_die )
1302 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1303 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1304 vlc_object_release( p_vout );
1307 if( p_vout == NULL )
1309 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1313 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1314 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1319 vlc_mutex_lock( &p_ev->lock );
1322 if( /* b_activated || // This breaks menus */
1323 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1325 p_ev->b_still = VLC_FALSE;
1326 dvdnav_still_skip( p_sys->dvdnav );
1329 vlc_mutex_unlock( &p_ev->lock );
1335 /* Release callback */
1338 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1339 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1340 vlc_object_release( p_vout );
1342 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1344 vlc_mutex_destroy( &p_ev->lock );
1349 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1350 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1352 event_thread_t *p_ev = p_data;
1353 vlc_mutex_lock( &p_ev->lock );
1354 if( psz_var[6] == 'c' )
1355 p_ev->b_clicked = VLC_TRUE;
1356 else if( psz_var[6] == 'm' )
1357 p_ev->b_moved = VLC_TRUE;
1358 vlc_mutex_unlock( &p_ev->lock );
1363 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1364 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1366 event_thread_t *p_ev = p_data;
1367 vlc_mutex_lock( &p_ev->lock );
1368 p_ev->b_key = VLC_TRUE;
1369 vlc_mutex_unlock( &p_ev->lock );
1374 /*****************************************************************************
1375 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1376 *****************************************************************************/
1377 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1379 #ifdef HAVE_SYS_STAT_H
1380 struct stat stat_info;
1381 uint8_t pi_anchor[2];
1382 uint16_t i_tag_id = 0;
1387 /* Triggers libdvdcss autodetection */
1391 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1393 /* Let dvdnav_open() do the probing */
1397 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1399 /* Let dvdnav_open() do the probing */
1403 /* Try to find the anchor (2 bytes at LBA 256) */
1404 i_ret = VLC_SUCCESS;
1405 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1407 i_ret = VLC_EGENERIC;
1410 if( read( i_fd, pi_anchor, 2 ) == 2 )
1412 i_tag_id = GetWLE(pi_anchor);
1413 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1417 i_ret = VLC_EGENERIC;