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 *****************************************************************************/
33 #include <vlc_input.h>
34 #include <vlc_access.h>
35 #include <vlc_demux.h>
36 #include <vlc_charset.h>
38 #include <vlc_interface.h>
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
56 /* FIXME we should find a better way than including that */
57 #include "../../src/text/iso-639_def.h"
60 #include <dvdnav/dvdnav.h>
62 #include "../demux/ps.h"
64 /*****************************************************************************
66 *****************************************************************************/
67 #define ANGLE_TEXT N_("DVD angle")
68 #define ANGLE_LONGTEXT N_( \
69 "Default DVD angle." )
71 #define CACHING_TEXT N_("Caching value in ms")
72 #define CACHING_LONGTEXT N_( \
73 "Caching value for DVDs. This "\
74 "value should be set in milliseconds." )
75 #define MENU_TEXT N_("Start directly in menu")
76 #define MENU_LONGTEXT N_( \
77 "Start the DVD directly in the main menu. This "\
78 "will try to skip all the useless warning introductions." )
80 #define LANGUAGE_DEFAULT ("en")
82 static int Open ( vlc_object_t * );
83 static void Close( vlc_object_t * );
86 set_shortname( _("DVD with menus") );
87 set_description( _("DVDnav Input") );
88 set_category( CAT_INPUT );
89 set_subcategory( SUBCAT_INPUT_ACCESS );
90 add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
91 ANGLE_LONGTEXT, false );
92 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
93 CACHING_TEXT, CACHING_LONGTEXT, true );
94 add_bool( "dvdnav-menu", true, NULL,
95 MENU_TEXT, MENU_LONGTEXT, false );
96 set_capability( "access_demux", 5 );
97 add_shortcut( "dvd" );
98 add_shortcut( "dvdnav" );
99 set_callbacks( Open, Close );
102 /* Shall we use libdvdnav's read ahead cache? */
103 #define DVD_READ_CACHE 1
105 /*****************************************************************************
107 *****************************************************************************/
124 static int EventThread( vlc_object_t * );
131 ps_track_t tk[PS_TK_COUNT];
134 /* for spu variables */
135 input_thread_t *p_input;
138 event_thread_t *p_ev;
140 /* palette for menus */
142 uint8_t palette[4][4];
149 input_title_t **title;
151 /* lenght of program group chain */
152 mtime_t i_pgc_length;
155 static int Control( demux_t *, int, va_list );
156 static int Demux( demux_t * );
157 static int DemuxBlock( demux_t *, uint8_t *, int );
159 static void DemuxTitles( demux_t * );
160 static void ESSubtitleUpdate( demux_t * );
161 static void ButtonUpdate( demux_t *, bool );
163 static void ESNew( demux_t *, int );
164 static int ProbeDVD( demux_t *, char * );
166 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
168 /*****************************************************************************
170 *****************************************************************************/
171 static int Open( vlc_object_t *p_this )
173 demux_t *p_demux = (demux_t*)p_this;
181 if( !p_demux->psz_path || !*p_demux->psz_path )
183 /* Only when selected */
184 if( !p_this->b_force ) return VLC_EGENERIC;
186 psz_name = var_CreateGetString( p_this, "dvd" );
189 psz_name = strdup("");
193 psz_name = ToLocaleDup( p_demux->psz_path );
196 if( psz_name[0] && psz_name[1] == ':' &&
197 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
200 /* Try some simple probing to avoid going through dvdnav_open too often */
201 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
208 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
210 msg_Warn( p_demux, "cannot open dvdnav" );
216 /* Fill p_demux field */
217 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
218 p_sys->dvdnav = p_dvdnav;
220 ps_track_init( p_sys->tk );
221 p_sys->i_aspect = -1;
222 p_sys->i_mux_rate = 0;
223 p_sys->i_pgc_length = 0;
224 p_sys->b_spu_change = false;
228 // Hack for libdvdnav CVS.
229 // Without it dvdnav_get_number_of_titles() fails.
230 // Remove when fixed in libdvdnav CVS.
231 uint8_t buffer[DVD_VIDEO_LB_LEN];
234 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
235 == DVDNAV_STATUS_ERR )
237 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
240 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
243 /* Configure dvdnav */
244 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
247 msg_Warn( p_demux, "cannot set read-a-head flag" );
250 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
253 msg_Warn( p_demux, "cannot set PGC positioning flag" );
256 /* Set menu language ("en")
257 * XXX: maybe it would be better to set it like audio/spu
258 * or to create a --menu-language option */
259 if( dvdnav_menu_language_select( p_sys->dvdnav, LANGUAGE_DEFAULT ) !=
262 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
263 LANGUAGE_DEFAULT, dvdnav_err_to_string( p_sys->dvdnav ) );
266 /* Set audio language */
267 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
268 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
271 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
272 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
273 /* We try to fall back to 'en' */
274 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
275 dvdnav_audio_language_select( p_sys->dvdnav, LANGUAGE_DEFAULT );
279 /* Set spu language */
280 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
281 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
284 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
285 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
286 /* We try to fall back to 'en' */
287 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
288 dvdnav_spu_language_select(p_sys->dvdnav, LANGUAGE_DEFAULT );
292 DemuxTitles( p_demux );
294 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
295 var_Get( p_demux, "dvdnav-menu", &val );
298 msg_Dbg( p_demux, "trying to go to dvd menu" );
300 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
302 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
303 intf_UserFatal( p_demux, false, _("Playback failure"),
304 _("VLC cannot set the DVD's title. It possibly "
305 "cannot decrypt the entire disk.") );
306 dvdnav_close( p_sys->dvdnav );
311 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
314 /* Try going to menu root */
315 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
317 msg_Warn( p_demux, "cannot go to dvd menu" );
321 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
322 var_Get( p_demux, "dvdnav-angle", &val );
323 i_angle = val.i_int > 0 ? val.i_int : 1;
325 /* Update default_pts to a suitable value for dvdnav access */
326 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
328 /* FIXME hack hack hack hack FIXME */
329 /* Get p_input and create variable */
330 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
331 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
332 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
333 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
334 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
335 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
336 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
337 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
338 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
340 /* Now create our event thread catcher */
341 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
342 p_sys->p_ev->p_demux = p_demux;
343 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
344 VLC_THREAD_PRIORITY_LOW, false );
349 /*****************************************************************************
351 *****************************************************************************/
352 static void Close( vlc_object_t *p_this )
354 demux_t *p_demux = (demux_t*)p_this;
355 demux_sys_t *p_sys = p_demux->p_sys;
358 /* stop the event handler */
359 vlc_object_kill( p_sys->p_ev );
360 vlc_thread_join( p_sys->p_ev );
361 vlc_object_release( p_sys->p_ev );
363 var_Destroy( p_sys->p_input, "highlight-mutex" );
364 var_Destroy( p_sys->p_input, "highlight" );
365 var_Destroy( p_sys->p_input, "x-start" );
366 var_Destroy( p_sys->p_input, "x-end" );
367 var_Destroy( p_sys->p_input, "y-start" );
368 var_Destroy( p_sys->p_input, "y-end" );
369 var_Destroy( p_sys->p_input, "color" );
370 var_Destroy( p_sys->p_input, "menu-palette" );
372 vlc_object_release( p_sys->p_input );
374 for( i = 0; i < PS_TK_COUNT; i++ )
376 ps_track_t *tk = &p_sys->tk[i];
379 es_format_Clean( &tk->fmt );
380 if( tk->es ) es_out_Del( p_demux->out, tk->es );
384 dvdnav_close( p_sys->dvdnav );
388 /*****************************************************************************
390 *****************************************************************************/
391 static int Control( demux_t *p_demux, int i_query, va_list args )
393 demux_sys_t *p_sys = p_demux->p_sys;
397 input_title_t ***ppp_title;
403 case DEMUX_SET_POSITION:
404 case DEMUX_GET_POSITION:
406 case DEMUX_GET_LENGTH:
409 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
410 DVDNAV_STATUS_OK || len == 0 )
415 if( i_query == DEMUX_GET_POSITION )
417 pf = (double*)va_arg( args, double* );
418 *pf = (double)pos / (double)len;
421 else if( i_query == DEMUX_SET_POSITION )
423 f = (double)va_arg( args, double );
425 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
431 else if( i_query == DEMUX_GET_TIME )
433 pi64 = (int64_t*)va_arg( args, int64_t * );
434 if( p_sys->i_pgc_length > 0 )
436 *pi64 = p_sys->i_pgc_length * pos / len;
440 else if( i_query == DEMUX_GET_LENGTH )
442 pi64 = (int64_t*)va_arg( args, int64_t * );
443 if( p_sys->i_pgc_length > 0 )
445 *pi64 = (int64_t)p_sys->i_pgc_length;
453 /* Special for access_demux */
454 case DEMUX_CAN_PAUSE:
456 case DEMUX_CAN_CONTROL_PACE:
458 pb = (bool*)va_arg( args, bool * );
462 case DEMUX_SET_PAUSE_STATE:
465 case DEMUX_GET_TITLE_INFO:
466 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
467 pi_int = (int*)va_arg( args, int* );
468 *((int*)va_arg( args, int* )) = 0; /* Title offset */
469 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
471 /* Duplicate title infos */
472 *pi_int = p_sys->i_title;
473 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
474 for( i = 0; i < p_sys->i_title; i++ )
476 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
480 case DEMUX_SET_TITLE:
481 i = (int)va_arg( args, int );
482 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
483 != DVDNAV_STATUS_OK ) ||
484 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
485 != DVDNAV_STATUS_OK ) )
487 msg_Warn( p_demux, "cannot set title/chapter" );
490 p_demux->info.i_update |=
491 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
492 p_demux->info.i_title = i;
493 p_demux->info.i_seekpoint = 0;
496 case DEMUX_SET_SEEKPOINT:
497 i = (int)va_arg( args, int );
498 if( p_demux->info.i_title == 0 )
505 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
508 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
511 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
514 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
517 i_ret = dvdnav_menu_call( p_sys->dvdnav,
518 DVD_MENU_Subpicture );
521 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
524 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
530 if( i_ret != DVDNAV_STATUS_OK )
533 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
534 i + 1 ) != DVDNAV_STATUS_OK )
536 msg_Warn( p_demux, "cannot set title/chapter" );
539 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
540 p_demux->info.i_seekpoint = i;
543 case DEMUX_GET_PTS_DELAY:
544 pi64 = (int64_t*)va_arg( args, int64_t * );
545 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
550 const char *title_name = NULL;
552 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
553 if( (NULL != title_name) && ('\0' != title_name[0]) )
555 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
556 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
562 /* TODO implement others */
568 /*****************************************************************************
570 *****************************************************************************/
571 static int Demux( demux_t *p_demux )
573 demux_sys_t *p_sys = p_demux->p_sys;
575 uint8_t buffer[DVD_VIDEO_LB_LEN];
576 uint8_t *packet = buffer;
581 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
582 == DVDNAV_STATUS_ERR )
584 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
585 == DVDNAV_STATUS_ERR )
588 msg_Warn( p_demux, "cannot get next block (%s)",
589 dvdnav_err_to_string( p_sys->dvdnav ) );
595 case DVDNAV_BLOCK_OK: /* mpeg block */
596 DemuxBlock( p_demux, packet, i_len );
599 case DVDNAV_NOP: /* Nothing */
600 msg_Dbg( p_demux, "DVDNAV_NOP" );
603 case DVDNAV_STILL_FRAME:
605 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
606 vlc_mutex_lock( &p_sys->p_ev->lock );
607 if( !p_sys->p_ev->b_still )
609 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
610 msg_Dbg( p_demux, " - length=0x%x", event->length );
611 p_sys->p_ev->b_still = true;
612 if( event->length == 0xff )
614 p_sys->p_ev->i_still_end = 0;
618 p_sys->p_ev->i_still_end = (int64_t)event->length *
619 1000000 + mdate() + p_sys->p_input->i_pts_delay;
622 vlc_mutex_unlock( &p_sys->p_ev->lock );
627 case DVDNAV_SPU_CLUT_CHANGE:
631 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
632 /* Update color lookup table (16 *uint32_t in packet) */
633 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
635 /* HACK to get the SPU tracks registered in the right order */
636 for( i = 0; i < 0x1f; i++ )
638 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
639 ESNew( p_demux, 0xbd20 + i );
645 case DVDNAV_SPU_STREAM_CHANGE:
647 dvdnav_spu_stream_change_event_t *event =
648 (dvdnav_spu_stream_change_event_t*)packet;
651 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
652 msg_Dbg( p_demux, " - physical_wide=%d",
653 event->physical_wide );
654 msg_Dbg( p_demux, " - physical_letterbox=%d",
655 event->physical_letterbox);
656 msg_Dbg( p_demux, " - physical_pan_scan=%d",
657 event->physical_pan_scan );
659 ESSubtitleUpdate( p_demux );
660 p_sys->b_spu_change = true;
662 /* HACK to get the SPU tracks registered in the right order */
663 for( i = 0; i < 0x1f; i++ )
665 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
666 ESNew( p_demux, 0xbd20 + i );
672 case DVDNAV_AUDIO_STREAM_CHANGE:
674 dvdnav_audio_stream_change_event_t *event =
675 (dvdnav_audio_stream_change_event_t*)packet;
676 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
677 msg_Dbg( p_demux, " - physical=%d", event->physical );
682 case DVDNAV_VTS_CHANGE:
688 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
689 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
690 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
691 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
693 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
694 /* TODO check if we always have VTS and CELL */
695 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
698 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
700 for( i = 0; i < PS_TK_COUNT; i++ )
702 ps_track_t *tk = &p_sys->tk[i];
705 es_format_Clean( &tk->fmt );
706 if( tk->es ) es_out_Del( p_demux->out, tk->es );
711 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
712 &i_part ) == DVDNAV_STATUS_OK )
714 if( i_title >= 0 && i_title < p_sys->i_title &&
715 p_demux->info.i_title != i_title )
717 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
718 p_demux->info.i_title = i_title;
724 case DVDNAV_CELL_CHANGE:
729 dvdnav_cell_change_event_t *event =
730 (dvdnav_cell_change_event_t*)packet;
731 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
732 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
733 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
734 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
735 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
736 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
737 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
738 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
740 /* Store the lenght in time of the current PGC */
741 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
743 /* FIXME is it correct or there is better way to know chapter change */
744 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
745 &i_part ) == DVDNAV_STATUS_OK )
747 if( i_title >= 0 && i_title < p_sys->i_title &&
748 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
749 p_demux->info.i_seekpoint != i_part - 1 )
751 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
752 p_demux->info.i_seekpoint = i_part - 1;
758 case DVDNAV_NAV_PACKET:
761 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
763 /* A lot of thing to do here :
765 * - fetch pts (for time display)
768 DemuxBlock( p_demux, packet, i_len );
769 if( p_sys->b_spu_change )
771 ButtonUpdate( p_demux, false );
772 p_sys->b_spu_change = false;
777 case DVDNAV_STOP: /* EOF */
778 msg_Dbg( p_demux, "DVDNAV_STOP" );
781 dvdnav_free_cache_block( p_sys->dvdnav, packet );
785 case DVDNAV_HIGHLIGHT:
787 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
788 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
789 msg_Dbg( p_demux, " - display=%d", event->display );
790 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
791 ButtonUpdate( p_demux, false );
795 case DVDNAV_HOP_CHANNEL:
796 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
797 /* We should try to flush all our internal buffer */
801 msg_Dbg( p_demux, "DVDNAV_WAIT" );
804 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
805 dvdnav_wait_skip( p_sys->dvdnav );
809 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
814 dvdnav_free_cache_block( p_sys->dvdnav, packet );
821 * FIXME: partiallyy duplicated from src/input/es_out.c
823 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
825 const iso639_lang_t *pl;
829 psz_lang = var_CreateGetString( p_demux, psz_var );
830 /* XXX: we will use only the first value
831 * (and ignore other ones in case of a list) */
832 if( ( p = strchr( psz_lang, ',' ) ) ) *p = '\0';
834 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
836 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
837 !strcasecmp( pl->psz_native_name, psz_lang ) ||
838 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
839 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
840 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
846 if( pl->psz_iso639_1 != NULL )
847 return strdup( pl->psz_iso639_1 );
849 return strdup(LANGUAGE_DEFAULT);
852 static void DemuxTitles( demux_t *p_demux )
854 demux_sys_t *p_sys = p_demux->p_sys;
861 t = vlc_input_title_New();
863 t->psz_name = strdup( "DVD Menu" );
865 s = vlc_seekpoint_New();
866 s->psz_name = strdup( "Resume" );
867 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
869 s = vlc_seekpoint_New();
870 s->psz_name = strdup( "Root" );
871 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
873 s = vlc_seekpoint_New();
874 s->psz_name = strdup( "Title" );
875 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
877 s = vlc_seekpoint_New();
878 s->psz_name = strdup( "Chapter" );
879 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
881 s = vlc_seekpoint_New();
882 s->psz_name = strdup( "Subtitle" );
883 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
885 s = vlc_seekpoint_New();
886 s->psz_name = strdup( "Audio" );
887 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
889 s = vlc_seekpoint_New();
890 s->psz_name = strdup( "Angle" );
891 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
893 TAB_APPEND( p_sys->i_title, p_sys->title, t );
895 /* Find out number of titles/chapters */
896 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
897 for( i = 1; i <= i_titles; i++ )
899 int32_t i_chapters = 0;
902 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
904 t = vlc_input_title_New();
905 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
907 s = vlc_seekpoint_New();
908 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
911 TAB_APPEND( p_sys->i_title, p_sys->title, t );
915 /*****************************************************************************
917 *****************************************************************************/
918 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
920 demux_sys_t *p_sys = p_demux->p_sys;
922 int32_t i_title, i_part;
924 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
926 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
928 vlc_mutex_t *p_mutex = val.p_address;
929 dvdnav_highlight_area_t hl;
932 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
933 != DVDNAV_STATUS_OK )
935 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
939 if( i_button > 0 && i_title == 0 )
942 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
944 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
946 for( i = 0; i < 4; i++ )
948 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
949 uint8_t i_alpha = (hl.palette>>(i*4))&0x0f;
950 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
952 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
953 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
954 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
955 p_sys->palette[i][3] = i_alpha;
958 vlc_mutex_lock( p_mutex );
959 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
960 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
961 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
962 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
964 val.p_address = (void *)p_sys->palette;
965 var_Set( p_sys->p_input, "menu-palette", val );
967 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
968 vlc_mutex_unlock( p_mutex );
970 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
974 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
978 vlc_mutex_lock( p_mutex );
980 var_Set( p_sys->p_input, "highlight", val );
981 vlc_mutex_unlock( p_mutex );
986 static void ESSubtitleUpdate( demux_t *p_demux )
988 demux_sys_t *p_sys = p_demux->p_sys;
989 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
990 int32_t i_title, i_part;
992 ButtonUpdate( p_demux, false );
994 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
995 if( i_title > 0 ) return;
997 if( i_spu >= 0 && i_spu <= 0x1f )
999 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1001 ESNew( p_demux, 0xbd20 + i_spu );
1003 /* be sure to unselect it (reset) */
1004 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1008 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1012 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1014 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1017 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1024 /*****************************************************************************
1025 * DemuxBlock: demux a given block
1026 *****************************************************************************/
1027 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
1029 demux_sys_t *p_sys = p_demux->p_sys;
1032 while( p < &pkt[i_pkt] )
1034 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1041 /* Create a block */
1042 p_pkt = block_New( p_demux, i_size );
1043 memcpy( p_pkt->p_buffer, p, i_size);
1045 /* Parse it and send it */
1046 switch( 0x100 | p[3] )
1054 msg_Warn( p_demux, "received a PSM packet" );
1056 else if( p[3] == 0xbb )
1058 msg_Warn( p_demux, "received a SYSTEM packet" );
1061 block_Release( p_pkt );
1068 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1070 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
1071 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1073 block_Release( p_pkt );
1078 int i_id = ps_pkt_id( p_pkt );
1081 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1085 ESNew( p_demux, i_id );
1087 if( tk->b_seen && tk->es &&
1088 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1090 es_out_Send( p_demux->out, tk->es, p_pkt );
1094 block_Release( p_pkt );
1099 block_Release( p_pkt );
1111 /*****************************************************************************
1112 * ESNew: register a new elementary stream
1113 *****************************************************************************/
1114 static void ESNew( demux_t *p_demux, int i_id )
1116 demux_sys_t *p_sys = p_demux->p_sys;
1117 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1118 bool b_select = false;
1120 if( tk->b_seen ) return;
1122 if( ps_track_fill( tk, 0, i_id ) )
1124 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1129 if( tk->fmt.i_cat == VIDEO_ES )
1131 switch( p_sys->i_aspect )
1133 /* TODO Any docs somewhere ? */
1135 tk->fmt.video.i_aspect = 0;
1140 else if( tk->fmt.i_cat == AUDIO_ES )
1143 /* find the audio number PLEASE find another way */
1144 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1146 i_audio = i_id&0x07;
1148 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1152 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1154 i_audio = i_id&0x1f;
1156 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1158 i_audio = i_id&0x1f;
1162 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1163 if( i_lang != 0xffff )
1165 tk->fmt.psz_language = malloc( 3 );
1166 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1167 tk->fmt.psz_language[1] = (i_lang )&0xff;
1168 tk->fmt.psz_language[2] = 0;
1170 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1176 else if( tk->fmt.i_cat == SPU_ES )
1178 int32_t i_title, i_part;
1179 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1180 if( i_lang != 0xffff )
1182 tk->fmt.psz_language = malloc( 3 );
1183 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1184 tk->fmt.psz_language[1] = (i_lang )&0xff;
1185 tk->fmt.psz_language[2] = 0;
1189 tk->fmt.subs.spu.palette[0] = 0xBeef;
1190 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1191 16 * sizeof( uint32_t ) );
1193 /* We select only when we are not in the menu */
1194 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1196 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1202 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1205 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1209 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1212 /*****************************************************************************
1213 * Event handler code
1214 *****************************************************************************/
1215 static int EventMouse( vlc_object_t *, char const *,
1216 vlc_value_t, vlc_value_t, void * );
1217 static int EventKey ( vlc_object_t *, char const *,
1218 vlc_value_t, vlc_value_t, void * );
1220 static int EventThread( vlc_object_t *p_this )
1222 event_thread_t *p_ev = (event_thread_t*)p_this;
1223 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1224 vlc_object_t *p_vout = NULL;
1226 vlc_mutex_init( &p_ev->lock );
1227 p_ev->b_moved = false;
1228 p_ev->b_clicked = false;
1229 p_ev->i_key_action = 0;
1230 p_ev->b_still = false;
1232 /* catch all key event */
1233 var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
1236 while( !p_ev->b_die )
1238 bool b_activated = false;
1241 if( p_ev->i_key_action != 0 )
1243 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1248 vlc_mutex_lock( &p_ev->lock );
1249 switch( p_ev->i_key_action )
1251 case ACTIONID_NAV_LEFT:
1252 dvdnav_left_button_select( p_sys->dvdnav, pci );
1254 case ACTIONID_NAV_RIGHT:
1255 dvdnav_right_button_select( p_sys->dvdnav, pci );
1257 case ACTIONID_NAV_UP:
1258 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1260 case ACTIONID_NAV_DOWN:
1261 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1263 case ACTIONID_NAV_ACTIVATE:
1265 dvdnav_button_activate( p_sys->dvdnav, pci );
1266 ButtonUpdate( p_ev->p_demux, true );
1271 p_ev->i_key_action = 0;
1272 vlc_mutex_unlock( &p_ev->lock );
1276 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1278 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1279 vlc_value_t valx, valy;
1281 vlc_mutex_lock( &p_ev->lock );
1282 var_Get( p_vout, "mouse-x", &valx );
1283 var_Get( p_vout, "mouse-y", &valy );
1287 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1290 if( p_ev->b_clicked )
1293 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1295 ButtonUpdate( p_ev->p_demux, true );
1298 p_ev->b_moved = false;
1299 p_ev->b_clicked = false;
1300 vlc_mutex_unlock( &p_ev->lock );
1302 if( p_vout && p_vout->b_die )
1304 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1305 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1306 vlc_object_release( p_vout );
1309 if( p_vout == NULL )
1311 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1315 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1316 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1321 vlc_mutex_lock( &p_ev->lock );
1324 if( /* b_activated || // This breaks menus */
1325 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1327 p_ev->b_still = false;
1328 dvdnav_still_skip( p_sys->dvdnav );
1331 vlc_mutex_unlock( &p_ev->lock );
1337 /* Release callback */
1340 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1341 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1342 vlc_object_release( p_vout );
1344 var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
1346 vlc_mutex_destroy( &p_ev->lock );
1351 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1352 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1354 event_thread_t *p_ev = p_data;
1355 vlc_mutex_lock( &p_ev->lock );
1356 if( psz_var[6] == 'c' )
1357 p_ev->b_clicked = true;
1358 else if( psz_var[6] == 'm' )
1359 p_ev->b_moved = true;
1360 vlc_mutex_unlock( &p_ev->lock );
1365 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1366 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1368 event_thread_t *p_ev = p_data;
1369 vlc_mutex_lock( &p_ev->lock );
1370 p_ev->i_key_action = newval.i_int;
1371 vlc_mutex_unlock( &p_ev->lock );
1376 /*****************************************************************************
1377 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1378 *****************************************************************************/
1379 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1381 #ifdef HAVE_SYS_STAT_H
1382 struct stat stat_info;
1383 uint8_t pi_anchor[2];
1384 uint16_t i_tag_id = 0;
1389 /* Triggers libdvdcss autodetection */
1393 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1395 /* Let dvdnav_open() do the probing */
1399 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1401 /* Let dvdnav_open() do the probing */
1405 /* Try to find the anchor (2 bytes at LBA 256) */
1406 i_ret = VLC_SUCCESS;
1407 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1409 i_ret = VLC_EGENERIC;
1412 if( read( i_fd, pi_anchor, 2 ) == 2 )
1414 i_tag_id = GetWLE(pi_anchor);
1415 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1419 i_ret = VLC_EGENERIC;