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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_input.h>
35 #include <vlc_access.h>
36 #include <vlc_demux.h>
37 #include <vlc_charset.h>
39 #include <vlc_interface.h>
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
55 #include "vlc_iso_lang.h"
57 /* FIXME we should find a better way than including that */
58 #include "../../src/text/iso-639_def.h"
61 #include <dvdnav/dvdnav.h>
63 #include "../demux/ps.h"
65 /*****************************************************************************
67 *****************************************************************************/
68 #define ANGLE_TEXT N_("DVD angle")
69 #define ANGLE_LONGTEXT N_( \
70 "Default DVD angle." )
72 #define CACHING_TEXT N_("Caching value in ms")
73 #define CACHING_LONGTEXT N_( \
74 "Caching value for DVDs. This "\
75 "value should be set in milliseconds." )
76 #define MENU_TEXT N_("Start directly in menu")
77 #define MENU_LONGTEXT N_( \
78 "Start the DVD directly in the main menu. This "\
79 "will try to skip all the useless warning introductions." )
81 #define LANGUAGE_DEFAULT ("en")
83 static int Open ( vlc_object_t * );
84 static void Close( vlc_object_t * );
87 set_shortname( N_("DVD with menus") );
88 set_description( N_("DVDnav Input") );
89 set_category( CAT_INPUT );
90 set_subcategory( SUBCAT_INPUT_ACCESS );
91 add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
92 ANGLE_LONGTEXT, false );
93 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
94 CACHING_TEXT, CACHING_LONGTEXT, true );
95 add_bool( "dvdnav-menu", true, NULL,
96 MENU_TEXT, MENU_LONGTEXT, false );
97 set_capability( "access_demux", 5 );
98 add_shortcut( "dvd" );
99 add_shortcut( "dvdnav" );
100 set_callbacks( Open, Close );
103 /* Shall we use libdvdnav's read ahead cache? */
104 #define DVD_READ_CACHE 1
106 /*****************************************************************************
108 *****************************************************************************/
125 static int EventThread( vlc_object_t * );
132 ps_track_t tk[PS_TK_COUNT];
135 /* for spu variables */
136 input_thread_t *p_input;
139 event_thread_t *p_ev;
141 /* palette for menus */
143 uint8_t palette[4][4];
150 input_title_t **title;
152 /* lenght of program group chain */
153 mtime_t i_pgc_length;
156 static int Control( demux_t *, int, va_list );
157 static int Demux( demux_t * );
158 static int DemuxBlock( demux_t *, uint8_t *, int );
160 static void DemuxTitles( demux_t * );
161 static void ESSubtitleUpdate( demux_t * );
162 static void ButtonUpdate( demux_t *, bool );
164 static void ESNew( demux_t *, int );
165 static int ProbeDVD( demux_t *, char * );
167 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
169 /*****************************************************************************
171 *****************************************************************************/
172 static int Open( vlc_object_t *p_this )
174 demux_t *p_demux = (demux_t*)p_this;
182 if( !p_demux->psz_path || !*p_demux->psz_path )
184 /* Only when selected */
185 if( !p_this->b_force ) return VLC_EGENERIC;
187 psz_name = var_CreateGetString( p_this, "dvd" );
190 psz_name = strdup("");
194 psz_name = ToLocaleDup( p_demux->psz_path );
197 if( psz_name[0] && psz_name[1] == ':' &&
198 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
201 /* Try some simple probing to avoid going through dvdnav_open too often */
202 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
209 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
211 msg_Warn( p_demux, "cannot open dvdnav" );
217 /* Fill p_demux field */
218 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
219 p_sys->dvdnav = p_dvdnav;
221 ps_track_init( p_sys->tk );
222 p_sys->i_aspect = -1;
223 p_sys->i_mux_rate = 0;
224 p_sys->i_pgc_length = 0;
225 p_sys->b_spu_change = false;
229 // Hack for libdvdnav CVS.
230 // Without it dvdnav_get_number_of_titles() fails.
231 // Remove when fixed in libdvdnav CVS.
232 uint8_t buffer[DVD_VIDEO_LB_LEN];
235 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
236 == DVDNAV_STATUS_ERR )
238 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
241 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
244 /* Configure dvdnav */
245 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
248 msg_Warn( p_demux, "cannot set read-a-head flag" );
251 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
254 msg_Warn( p_demux, "cannot set PGC positioning flag" );
257 /* Set menu language ("en")
258 * XXX: maybe it would be better to set it like audio/spu
259 * or to create a --menu-language option */
260 if( dvdnav_menu_language_select( p_sys->dvdnav, LANGUAGE_DEFAULT ) !=
263 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
264 LANGUAGE_DEFAULT, dvdnav_err_to_string( p_sys->dvdnav ) );
267 /* Set audio language */
268 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
269 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
272 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
273 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
274 /* We try to fall back to 'en' */
275 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
276 dvdnav_audio_language_select( p_sys->dvdnav, LANGUAGE_DEFAULT );
280 /* Set spu language */
281 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
282 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
285 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
286 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
287 /* We try to fall back to 'en' */
288 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
289 dvdnav_spu_language_select(p_sys->dvdnav, LANGUAGE_DEFAULT );
293 DemuxTitles( p_demux );
295 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
296 var_Get( p_demux, "dvdnav-menu", &val );
299 msg_Dbg( p_demux, "trying to go to dvd menu" );
301 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
303 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
304 intf_UserFatal( p_demux, false, _("Playback failure"),
305 _("VLC cannot set the DVD's title. It possibly "
306 "cannot decrypt the entire disk.") );
307 dvdnav_close( p_sys->dvdnav );
312 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
315 /* Try going to menu root */
316 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
318 msg_Warn( p_demux, "cannot go to dvd menu" );
322 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
323 var_Get( p_demux, "dvdnav-angle", &val );
324 i_angle = val.i_int > 0 ? val.i_int : 1;
326 /* Update default_pts to a suitable value for dvdnav access */
327 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
329 /* FIXME hack hack hack hack FIXME */
330 /* Get p_input and create variable */
331 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
332 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
333 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
334 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
335 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
336 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
337 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
338 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
339 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
341 /* Now create our event thread catcher */
342 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
343 p_sys->p_ev->p_demux = p_demux;
344 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
345 VLC_THREAD_PRIORITY_LOW, false );
350 /*****************************************************************************
352 *****************************************************************************/
353 static void Close( vlc_object_t *p_this )
355 demux_t *p_demux = (demux_t*)p_this;
356 demux_sys_t *p_sys = p_demux->p_sys;
359 /* stop the event handler */
360 vlc_object_kill( p_sys->p_ev );
361 vlc_thread_join( p_sys->p_ev );
362 vlc_object_release( p_sys->p_ev );
364 var_Destroy( p_sys->p_input, "highlight-mutex" );
365 var_Destroy( p_sys->p_input, "highlight" );
366 var_Destroy( p_sys->p_input, "x-start" );
367 var_Destroy( p_sys->p_input, "x-end" );
368 var_Destroy( p_sys->p_input, "y-start" );
369 var_Destroy( p_sys->p_input, "y-end" );
370 var_Destroy( p_sys->p_input, "color" );
371 var_Destroy( p_sys->p_input, "menu-palette" );
373 vlc_object_release( p_sys->p_input );
375 for( i = 0; i < PS_TK_COUNT; i++ )
377 ps_track_t *tk = &p_sys->tk[i];
380 es_format_Clean( &tk->fmt );
381 if( tk->es ) es_out_Del( p_demux->out, tk->es );
385 dvdnav_close( p_sys->dvdnav );
389 /*****************************************************************************
391 *****************************************************************************/
392 static int Control( demux_t *p_demux, int i_query, va_list args )
394 demux_sys_t *p_sys = p_demux->p_sys;
398 input_title_t ***ppp_title;
404 case DEMUX_SET_POSITION:
405 case DEMUX_GET_POSITION:
407 case DEMUX_GET_LENGTH:
410 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
411 DVDNAV_STATUS_OK || len == 0 )
416 if( i_query == DEMUX_GET_POSITION )
418 pf = (double*)va_arg( args, double* );
419 *pf = (double)pos / (double)len;
422 else if( i_query == DEMUX_SET_POSITION )
424 f = (double)va_arg( args, double );
426 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
432 else if( i_query == DEMUX_GET_TIME )
434 pi64 = (int64_t*)va_arg( args, int64_t * );
435 if( p_sys->i_pgc_length > 0 )
437 *pi64 = p_sys->i_pgc_length * pos / len;
441 else if( i_query == DEMUX_GET_LENGTH )
443 pi64 = (int64_t*)va_arg( args, int64_t * );
444 if( p_sys->i_pgc_length > 0 )
446 *pi64 = (int64_t)p_sys->i_pgc_length;
454 /* Special for access_demux */
455 case DEMUX_CAN_PAUSE:
457 case DEMUX_CAN_CONTROL_PACE:
459 pb = (bool*)va_arg( args, bool * );
463 case DEMUX_SET_PAUSE_STATE:
466 case DEMUX_GET_TITLE_INFO:
467 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
468 pi_int = (int*)va_arg( args, int* );
469 *((int*)va_arg( args, int* )) = 0; /* Title offset */
470 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
472 /* Duplicate title infos */
473 *pi_int = p_sys->i_title;
474 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
475 for( i = 0; i < p_sys->i_title; i++ )
477 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
481 case DEMUX_SET_TITLE:
482 i = (int)va_arg( args, int );
483 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
484 != DVDNAV_STATUS_OK ) ||
485 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
486 != DVDNAV_STATUS_OK ) )
488 msg_Warn( p_demux, "cannot set title/chapter" );
491 p_demux->info.i_update |=
492 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
493 p_demux->info.i_title = i;
494 p_demux->info.i_seekpoint = 0;
497 case DEMUX_SET_SEEKPOINT:
498 i = (int)va_arg( args, int );
499 if( p_demux->info.i_title == 0 )
506 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
509 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
512 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
515 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
518 i_ret = dvdnav_menu_call( p_sys->dvdnav,
519 DVD_MENU_Subpicture );
522 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
525 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
531 if( i_ret != DVDNAV_STATUS_OK )
534 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
535 i + 1 ) != DVDNAV_STATUS_OK )
537 msg_Warn( p_demux, "cannot set title/chapter" );
540 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
541 p_demux->info.i_seekpoint = i;
544 case DEMUX_GET_PTS_DELAY:
545 pi64 = (int64_t*)va_arg( args, int64_t * );
546 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
551 const char *title_name = NULL;
553 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
554 if( (NULL != title_name) && ('\0' != title_name[0]) )
556 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
557 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
563 /* TODO implement others */
569 /*****************************************************************************
571 *****************************************************************************/
572 static int Demux( demux_t *p_demux )
574 demux_sys_t *p_sys = p_demux->p_sys;
576 uint8_t buffer[DVD_VIDEO_LB_LEN];
577 uint8_t *packet = buffer;
582 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
583 == DVDNAV_STATUS_ERR )
585 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
586 == DVDNAV_STATUS_ERR )
589 msg_Warn( p_demux, "cannot get next block (%s)",
590 dvdnav_err_to_string( p_sys->dvdnav ) );
596 case DVDNAV_BLOCK_OK: /* mpeg block */
597 DemuxBlock( p_demux, packet, i_len );
600 case DVDNAV_NOP: /* Nothing */
601 msg_Dbg( p_demux, "DVDNAV_NOP" );
604 case DVDNAV_STILL_FRAME:
606 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
607 vlc_mutex_lock( &p_sys->p_ev->lock );
608 if( !p_sys->p_ev->b_still )
610 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
611 msg_Dbg( p_demux, " - length=0x%x", event->length );
612 p_sys->p_ev->b_still = true;
613 if( event->length == 0xff )
615 p_sys->p_ev->i_still_end = 0;
619 p_sys->p_ev->i_still_end = (int64_t)event->length *
620 1000000 + mdate() + p_sys->p_input->i_pts_delay;
623 vlc_mutex_unlock( &p_sys->p_ev->lock );
628 case DVDNAV_SPU_CLUT_CHANGE:
632 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
633 /* Update color lookup table (16 *uint32_t in packet) */
634 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
636 /* HACK to get the SPU tracks registered in the right order */
637 for( i = 0; i < 0x1f; i++ )
639 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
640 ESNew( p_demux, 0xbd20 + i );
646 case DVDNAV_SPU_STREAM_CHANGE:
648 dvdnav_spu_stream_change_event_t *event =
649 (dvdnav_spu_stream_change_event_t*)packet;
652 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
653 msg_Dbg( p_demux, " - physical_wide=%d",
654 event->physical_wide );
655 msg_Dbg( p_demux, " - physical_letterbox=%d",
656 event->physical_letterbox);
657 msg_Dbg( p_demux, " - physical_pan_scan=%d",
658 event->physical_pan_scan );
660 ESSubtitleUpdate( p_demux );
661 p_sys->b_spu_change = true;
663 /* HACK to get the SPU tracks registered in the right order */
664 for( i = 0; i < 0x1f; i++ )
666 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
667 ESNew( p_demux, 0xbd20 + i );
673 case DVDNAV_AUDIO_STREAM_CHANGE:
675 dvdnav_audio_stream_change_event_t *event =
676 (dvdnav_audio_stream_change_event_t*)packet;
677 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
678 msg_Dbg( p_demux, " - physical=%d", event->physical );
683 case DVDNAV_VTS_CHANGE:
689 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
690 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
691 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
692 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
694 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
695 /* TODO check if we always have VTS and CELL */
696 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
699 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
701 for( i = 0; i < PS_TK_COUNT; i++ )
703 ps_track_t *tk = &p_sys->tk[i];
706 es_format_Clean( &tk->fmt );
707 if( tk->es ) es_out_Del( p_demux->out, tk->es );
712 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
713 &i_part ) == DVDNAV_STATUS_OK )
715 if( i_title >= 0 && i_title < p_sys->i_title &&
716 p_demux->info.i_title != i_title )
718 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
719 p_demux->info.i_title = i_title;
725 case DVDNAV_CELL_CHANGE:
730 dvdnav_cell_change_event_t *event =
731 (dvdnav_cell_change_event_t*)packet;
732 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
733 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
734 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
735 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
736 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
737 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
738 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
739 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
741 /* Store the lenght in time of the current PGC */
742 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
744 /* FIXME is it correct or there is better way to know chapter change */
745 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
746 &i_part ) == DVDNAV_STATUS_OK )
748 if( i_title >= 0 && i_title < p_sys->i_title &&
749 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
750 p_demux->info.i_seekpoint != i_part - 1 )
752 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
753 p_demux->info.i_seekpoint = i_part - 1;
759 case DVDNAV_NAV_PACKET:
762 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
764 /* A lot of thing to do here :
766 * - fetch pts (for time display)
769 DemuxBlock( p_demux, packet, i_len );
770 if( p_sys->b_spu_change )
772 ButtonUpdate( p_demux, false );
773 p_sys->b_spu_change = false;
778 case DVDNAV_STOP: /* EOF */
779 msg_Dbg( p_demux, "DVDNAV_STOP" );
782 dvdnav_free_cache_block( p_sys->dvdnav, packet );
786 case DVDNAV_HIGHLIGHT:
788 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
789 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
790 msg_Dbg( p_demux, " - display=%d", event->display );
791 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
792 ButtonUpdate( p_demux, false );
796 case DVDNAV_HOP_CHANNEL:
797 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
798 /* We should try to flush all our internal buffer */
802 msg_Dbg( p_demux, "DVDNAV_WAIT" );
805 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
806 dvdnav_wait_skip( p_sys->dvdnav );
810 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
815 dvdnav_free_cache_block( p_sys->dvdnav, packet );
822 * FIXME: partiallyy duplicated from src/input/es_out.c
824 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
826 const iso639_lang_t *pl;
830 psz_lang = var_CreateGetString( p_demux, psz_var );
831 /* XXX: we will use only the first value
832 * (and ignore other ones in case of a list) */
833 if( ( p = strchr( psz_lang, ',' ) ) ) *p = '\0';
835 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
837 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
838 !strcasecmp( pl->psz_native_name, psz_lang ) ||
839 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
840 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
841 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
847 if( pl->psz_iso639_1 != NULL )
848 return strdup( pl->psz_iso639_1 );
850 return strdup(LANGUAGE_DEFAULT);
853 static void DemuxTitles( demux_t *p_demux )
855 demux_sys_t *p_sys = p_demux->p_sys;
862 t = vlc_input_title_New();
864 t->psz_name = strdup( "DVD Menu" );
866 s = vlc_seekpoint_New();
867 s->psz_name = strdup( "Resume" );
868 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
870 s = vlc_seekpoint_New();
871 s->psz_name = strdup( "Root" );
872 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
874 s = vlc_seekpoint_New();
875 s->psz_name = strdup( "Title" );
876 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
878 s = vlc_seekpoint_New();
879 s->psz_name = strdup( "Chapter" );
880 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
882 s = vlc_seekpoint_New();
883 s->psz_name = strdup( "Subtitle" );
884 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
886 s = vlc_seekpoint_New();
887 s->psz_name = strdup( "Audio" );
888 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
890 s = vlc_seekpoint_New();
891 s->psz_name = strdup( "Angle" );
892 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
894 TAB_APPEND( p_sys->i_title, p_sys->title, t );
896 /* Find out number of titles/chapters */
897 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
898 for( i = 1; i <= i_titles; i++ )
900 int32_t i_chapters = 0;
903 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
905 t = vlc_input_title_New();
906 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
908 s = vlc_seekpoint_New();
909 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
912 TAB_APPEND( p_sys->i_title, p_sys->title, t );
916 /*****************************************************************************
918 *****************************************************************************/
919 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
921 demux_sys_t *p_sys = p_demux->p_sys;
923 int32_t i_title, i_part;
925 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
927 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
929 vlc_mutex_t *p_mutex = val.p_address;
930 dvdnav_highlight_area_t hl;
933 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
934 != DVDNAV_STATUS_OK )
936 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
940 if( i_button > 0 && i_title == 0 )
943 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
945 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
947 for( i = 0; i < 4; i++ )
949 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
950 uint8_t i_alpha = (hl.palette>>(i*4))&0x0f;
951 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
953 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
954 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
955 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
956 p_sys->palette[i][3] = i_alpha;
959 vlc_mutex_lock( p_mutex );
960 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
961 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
962 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
963 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
965 val.p_address = (void *)p_sys->palette;
966 var_Set( p_sys->p_input, "menu-palette", val );
968 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
969 vlc_mutex_unlock( p_mutex );
971 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
975 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
979 vlc_mutex_lock( p_mutex );
981 var_Set( p_sys->p_input, "highlight", val );
982 vlc_mutex_unlock( p_mutex );
987 static void ESSubtitleUpdate( demux_t *p_demux )
989 demux_sys_t *p_sys = p_demux->p_sys;
990 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
991 int32_t i_title, i_part;
993 ButtonUpdate( p_demux, false );
995 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
996 if( i_title > 0 ) return;
998 if( i_spu >= 0 && i_spu <= 0x1f )
1000 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1002 ESNew( p_demux, 0xbd20 + i_spu );
1004 /* be sure to unselect it (reset) */
1005 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1009 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1013 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1015 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1018 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1025 /*****************************************************************************
1026 * DemuxBlock: demux a given block
1027 *****************************************************************************/
1028 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
1030 demux_sys_t *p_sys = p_demux->p_sys;
1033 while( p < &pkt[i_pkt] )
1035 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1042 /* Create a block */
1043 p_pkt = block_New( p_demux, i_size );
1044 memcpy( p_pkt->p_buffer, p, i_size);
1046 /* Parse it and send it */
1047 switch( 0x100 | p[3] )
1055 msg_Warn( p_demux, "received a PSM packet" );
1057 else if( p[3] == 0xbb )
1059 msg_Warn( p_demux, "received a SYSTEM packet" );
1062 block_Release( p_pkt );
1069 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1071 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
1072 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1074 block_Release( p_pkt );
1079 int i_id = ps_pkt_id( p_pkt );
1082 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1086 ESNew( p_demux, i_id );
1088 if( tk->b_seen && tk->es &&
1089 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1091 es_out_Send( p_demux->out, tk->es, p_pkt );
1095 block_Release( p_pkt );
1100 block_Release( p_pkt );
1112 /*****************************************************************************
1113 * ESNew: register a new elementary stream
1114 *****************************************************************************/
1115 static void ESNew( demux_t *p_demux, int i_id )
1117 demux_sys_t *p_sys = p_demux->p_sys;
1118 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1119 bool b_select = false;
1121 if( tk->b_seen ) return;
1123 if( ps_track_fill( tk, 0, i_id ) )
1125 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1130 if( tk->fmt.i_cat == VIDEO_ES )
1132 switch( p_sys->i_aspect )
1134 /* TODO Any docs somewhere ? */
1136 tk->fmt.video.i_aspect = 0;
1141 else if( tk->fmt.i_cat == AUDIO_ES )
1144 /* find the audio number PLEASE find another way */
1145 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1147 i_audio = i_id&0x07;
1149 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1153 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1155 i_audio = i_id&0x1f;
1157 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1159 i_audio = i_id&0x1f;
1163 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1164 if( i_lang != 0xffff )
1166 tk->fmt.psz_language = malloc( 3 );
1167 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1168 tk->fmt.psz_language[1] = (i_lang )&0xff;
1169 tk->fmt.psz_language[2] = 0;
1171 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1177 else if( tk->fmt.i_cat == SPU_ES )
1179 int32_t i_title, i_part;
1180 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1181 if( i_lang != 0xffff )
1183 tk->fmt.psz_language = malloc( 3 );
1184 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1185 tk->fmt.psz_language[1] = (i_lang )&0xff;
1186 tk->fmt.psz_language[2] = 0;
1190 tk->fmt.subs.spu.palette[0] = 0xBeef;
1191 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1192 16 * sizeof( uint32_t ) );
1194 /* We select only when we are not in the menu */
1195 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1197 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1203 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1206 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1210 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1213 /*****************************************************************************
1214 * Event handler code
1215 *****************************************************************************/
1216 static int EventMouse( vlc_object_t *, char const *,
1217 vlc_value_t, vlc_value_t, void * );
1218 static int EventKey ( vlc_object_t *, char const *,
1219 vlc_value_t, vlc_value_t, void * );
1221 static int EventThread( vlc_object_t *p_this )
1223 event_thread_t *p_ev = (event_thread_t*)p_this;
1224 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1225 vlc_object_t *p_vout = NULL;
1227 vlc_mutex_init( &p_ev->lock );
1228 p_ev->b_moved = false;
1229 p_ev->b_clicked = false;
1230 p_ev->i_key_action = 0;
1231 p_ev->b_still = false;
1233 /* catch all key event */
1234 var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
1237 while( !p_ev->b_die )
1239 bool b_activated = false;
1242 if( p_ev->i_key_action != 0 )
1244 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1249 vlc_mutex_lock( &p_ev->lock );
1250 switch( p_ev->i_key_action )
1252 case ACTIONID_NAV_LEFT:
1253 dvdnav_left_button_select( p_sys->dvdnav, pci );
1255 case ACTIONID_NAV_RIGHT:
1256 dvdnav_right_button_select( p_sys->dvdnav, pci );
1258 case ACTIONID_NAV_UP:
1259 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1261 case ACTIONID_NAV_DOWN:
1262 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1264 case ACTIONID_NAV_ACTIVATE:
1266 dvdnav_button_activate( p_sys->dvdnav, pci );
1267 ButtonUpdate( p_ev->p_demux, true );
1272 p_ev->i_key_action = 0;
1273 vlc_mutex_unlock( &p_ev->lock );
1277 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1279 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1280 vlc_value_t valx, valy;
1282 vlc_mutex_lock( &p_ev->lock );
1283 var_Get( p_vout, "mouse-x", &valx );
1284 var_Get( p_vout, "mouse-y", &valy );
1288 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1291 if( p_ev->b_clicked )
1294 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1296 ButtonUpdate( p_ev->p_demux, true );
1299 p_ev->b_moved = false;
1300 p_ev->b_clicked = false;
1301 vlc_mutex_unlock( &p_ev->lock );
1303 if( p_vout && p_vout->b_die )
1305 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1306 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1307 vlc_object_release( p_vout );
1310 if( p_vout == NULL )
1312 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1316 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1317 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1322 vlc_mutex_lock( &p_ev->lock );
1325 if( /* b_activated || // This breaks menus */
1326 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1328 p_ev->b_still = false;
1329 dvdnav_still_skip( p_sys->dvdnav );
1332 vlc_mutex_unlock( &p_ev->lock );
1338 /* Release callback */
1341 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1342 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1343 vlc_object_release( p_vout );
1345 var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
1347 vlc_mutex_destroy( &p_ev->lock );
1352 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1353 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1355 event_thread_t *p_ev = p_data;
1356 vlc_mutex_lock( &p_ev->lock );
1357 if( psz_var[6] == 'c' )
1358 p_ev->b_clicked = true;
1359 else if( psz_var[6] == 'm' )
1360 p_ev->b_moved = true;
1361 vlc_mutex_unlock( &p_ev->lock );
1366 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1367 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1369 event_thread_t *p_ev = p_data;
1370 vlc_mutex_lock( &p_ev->lock );
1371 p_ev->i_key_action = newval.i_int;
1372 vlc_mutex_unlock( &p_ev->lock );
1377 /*****************************************************************************
1378 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1379 *****************************************************************************/
1380 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1382 #ifdef HAVE_SYS_STAT_H
1383 struct stat stat_info;
1384 uint8_t pi_anchor[2];
1385 uint16_t i_tag_id = 0;
1390 /* Triggers libdvdcss autodetection */
1394 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1396 /* Let dvdnav_open() do the probing */
1400 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1402 /* Let dvdnav_open() do the probing */
1406 /* Try to find the anchor (2 bytes at LBA 256) */
1407 i_ret = VLC_SUCCESS;
1408 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1410 i_ret = VLC_EGENERIC;
1413 if( read( i_fd, pi_anchor, 2 ) == 2 )
1415 i_tag_id = GetWLE(pi_anchor);
1416 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1420 i_ret = VLC_EGENERIC;