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 void* 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 *, const 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 static int ControlInternal( demux_t *, int, ... );
171 /*****************************************************************************
173 *****************************************************************************/
174 static int Open( vlc_object_t *p_this )
176 demux_t *p_demux = (demux_t*)p_this;
184 if( !p_demux->psz_path || !*p_demux->psz_path )
186 /* Only when selected */
187 if( !p_this->b_force ) return VLC_EGENERIC;
189 psz_name = var_CreateGetString( p_this, "dvd" );
192 psz_name = strdup("");
196 psz_name = ToLocaleDup( p_demux->psz_path );
199 if( psz_name[0] && psz_name[1] == ':' &&
200 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
203 /* Try some simple probing to avoid going through dvdnav_open too often */
204 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
211 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
213 msg_Warn( p_demux, "cannot open dvdnav" );
219 /* Fill p_demux field */
220 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
221 p_sys->dvdnav = p_dvdnav;
223 ps_track_init( p_sys->tk );
224 p_sys->i_aspect = -1;
225 p_sys->i_mux_rate = 0;
226 p_sys->i_pgc_length = 0;
227 p_sys->b_spu_change = false;
231 // Hack for libdvdnav CVS.
232 // Without it dvdnav_get_number_of_titles() fails.
233 // Remove when fixed in libdvdnav CVS.
234 uint8_t buffer[DVD_VIDEO_LB_LEN];
237 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
238 == DVDNAV_STATUS_ERR )
240 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
243 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
246 /* Configure dvdnav */
247 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
250 msg_Warn( p_demux, "cannot set read-a-head flag" );
253 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
256 msg_Warn( p_demux, "cannot set PGC positioning flag" );
260 * XXX A menu-language may be better than sub-language */
261 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
262 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
265 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
266 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
267 /* We try to fall back to 'en' */
268 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
269 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
273 /* Set audio language */
274 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
275 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
278 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
279 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
280 /* We try to fall back to 'en' */
281 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
282 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
286 /* Set spu language */
287 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
288 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
291 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
292 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
293 /* We try to fall back to 'en' */
294 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
295 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
299 DemuxTitles( p_demux );
301 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
302 var_Get( p_demux, "dvdnav-menu", &val );
305 msg_Dbg( p_demux, "trying to go to dvd menu" );
307 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
309 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
310 intf_UserFatal( p_demux, false, _("Playback failure"),
311 _("VLC cannot set the DVD's title. It possibly "
312 "cannot decrypt the entire disk.") );
313 dvdnav_close( p_sys->dvdnav );
318 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
321 /* Try going to menu root */
322 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
324 msg_Warn( p_demux, "cannot go to dvd menu" );
328 var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
329 var_Get( p_demux, "dvdnav-angle", &val );
330 i_angle = val.i_int > 0 ? val.i_int : 1;
332 /* Update default_pts to a suitable value for dvdnav access */
333 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
335 /* FIXME hack hack hack hack FIXME */
336 /* Get p_input and create variable */
337 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
338 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
339 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
340 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
341 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
342 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
343 var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
344 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
345 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
347 /* Now create our event thread catcher */
348 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
349 p_sys->p_ev->p_demux = p_demux;
350 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
351 VLC_THREAD_PRIORITY_LOW, false );
356 /*****************************************************************************
358 *****************************************************************************/
359 static void Close( vlc_object_t *p_this )
361 demux_t *p_demux = (demux_t*)p_this;
362 demux_sys_t *p_sys = p_demux->p_sys;
365 /* stop the event handler */
366 vlc_object_kill( p_sys->p_ev );
367 vlc_thread_join( p_sys->p_ev );
368 vlc_object_release( p_sys->p_ev );
370 var_Destroy( p_sys->p_input, "highlight-mutex" );
371 var_Destroy( p_sys->p_input, "highlight" );
372 var_Destroy( p_sys->p_input, "x-start" );
373 var_Destroy( p_sys->p_input, "x-end" );
374 var_Destroy( p_sys->p_input, "y-start" );
375 var_Destroy( p_sys->p_input, "y-end" );
376 var_Destroy( p_sys->p_input, "color" );
377 var_Destroy( p_sys->p_input, "menu-palette" );
379 vlc_object_release( p_sys->p_input );
381 for( i = 0; i < PS_TK_COUNT; i++ )
383 ps_track_t *tk = &p_sys->tk[i];
386 es_format_Clean( &tk->fmt );
387 if( tk->es ) es_out_Del( p_demux->out, tk->es );
391 dvdnav_close( p_sys->dvdnav );
395 /*****************************************************************************
397 *****************************************************************************/
398 static int Control( demux_t *p_demux, int i_query, va_list args )
400 demux_sys_t *p_sys = p_demux->p_sys;
404 input_title_t ***ppp_title;
410 case DEMUX_SET_POSITION:
411 case DEMUX_GET_POSITION:
413 case DEMUX_GET_LENGTH:
416 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
417 DVDNAV_STATUS_OK || len == 0 )
422 if( i_query == DEMUX_GET_POSITION )
424 pf = (double*)va_arg( args, double* );
425 *pf = (double)pos / (double)len;
428 else if( i_query == DEMUX_SET_POSITION )
430 f = (double)va_arg( args, double );
432 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
438 else if( i_query == DEMUX_GET_TIME )
440 pi64 = (int64_t*)va_arg( args, int64_t * );
441 if( p_sys->i_pgc_length > 0 )
443 *pi64 = p_sys->i_pgc_length * pos / len;
447 else if( i_query == DEMUX_GET_LENGTH )
449 pi64 = (int64_t*)va_arg( args, int64_t * );
450 if( p_sys->i_pgc_length > 0 )
452 *pi64 = (int64_t)p_sys->i_pgc_length;
460 /* Special for access_demux */
461 case DEMUX_CAN_PAUSE:
463 case DEMUX_CAN_CONTROL_PACE:
465 pb = (bool*)va_arg( args, bool * );
469 case DEMUX_SET_PAUSE_STATE:
472 case DEMUX_GET_TITLE_INFO:
473 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
474 pi_int = (int*)va_arg( args, int* );
475 *((int*)va_arg( args, int* )) = 0; /* Title offset */
476 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
478 /* Duplicate title infos */
479 *pi_int = p_sys->i_title;
480 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
481 for( i = 0; i < p_sys->i_title; i++ )
483 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
487 case DEMUX_SET_TITLE:
488 i = (int)va_arg( args, int );
489 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
490 != DVDNAV_STATUS_OK ) ||
491 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
492 != DVDNAV_STATUS_OK ) )
494 msg_Warn( p_demux, "cannot set title/chapter" );
497 p_demux->info.i_update |=
498 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
499 p_demux->info.i_title = i;
500 p_demux->info.i_seekpoint = 0;
503 case DEMUX_SET_SEEKPOINT:
504 i = (int)va_arg( args, int );
505 if( p_demux->info.i_title == 0 )
512 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
515 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
518 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
521 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
524 i_ret = dvdnav_menu_call( p_sys->dvdnav,
525 DVD_MENU_Subpicture );
528 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
531 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
537 if( i_ret != DVDNAV_STATUS_OK )
540 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
541 i + 1 ) != DVDNAV_STATUS_OK )
543 msg_Warn( p_demux, "cannot set title/chapter" );
546 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
547 p_demux->info.i_seekpoint = i;
550 case DEMUX_GET_PTS_DELAY:
551 pi64 = (int64_t*)va_arg( args, int64_t * );
552 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
557 const char *title_name = NULL;
559 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
560 if( (NULL != title_name) && ('\0' != title_name[0]) )
562 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
563 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
569 /* TODO implement others */
575 static int ControlInternal( demux_t *p_demux, int i_query, ... )
580 va_start( args, i_query );
581 i_result = Control( p_demux, i_query, args );
586 /*****************************************************************************
588 *****************************************************************************/
589 static int Demux( demux_t *p_demux )
591 demux_sys_t *p_sys = p_demux->p_sys;
593 uint8_t buffer[DVD_VIDEO_LB_LEN];
594 uint8_t *packet = buffer;
599 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
600 == DVDNAV_STATUS_ERR )
602 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
603 == DVDNAV_STATUS_ERR )
606 msg_Warn( p_demux, "cannot get next block (%s)",
607 dvdnav_err_to_string( p_sys->dvdnav ) );
608 if( p_demux->info.i_title == 0 )
610 msg_Dbg( p_demux, "jumping to first title" );
611 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
618 case DVDNAV_BLOCK_OK: /* mpeg block */
619 p_sys->p_ev->b_still = false;
620 DemuxBlock( p_demux, packet, i_len );
623 case DVDNAV_NOP: /* Nothing */
624 msg_Dbg( p_demux, "DVDNAV_NOP" );
627 case DVDNAV_STILL_FRAME:
629 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
630 vlc_mutex_lock( &p_sys->p_ev->lock );
631 if( !p_sys->p_ev->b_still )
633 /* We send a dummy mpeg2 end of sequence to force still frame display */
634 static const uint8_t buffer[] = {
635 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
637 0x00, 0x00, 0x01, 0xB7,
639 DemuxBlock( p_demux, buffer, sizeof(buffer) );
641 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
642 msg_Dbg( p_demux, " - length=0x%x", event->length );
643 p_sys->p_ev->b_still = true;
644 if( event->length == 0xff )
646 p_sys->p_ev->i_still_end = 0;
650 p_sys->p_ev->i_still_end = (int64_t)event->length *
651 1000000 + mdate() + p_sys->p_input->i_pts_delay;
654 vlc_mutex_unlock( &p_sys->p_ev->lock );
659 case DVDNAV_SPU_CLUT_CHANGE:
663 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
664 /* Update color lookup table (16 *uint32_t in packet) */
665 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
667 /* HACK to get the SPU tracks registered in the right order */
668 for( i = 0; i < 0x1f; i++ )
670 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
671 ESNew( p_demux, 0xbd20 + i );
677 case DVDNAV_SPU_STREAM_CHANGE:
679 dvdnav_spu_stream_change_event_t *event =
680 (dvdnav_spu_stream_change_event_t*)packet;
683 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
684 msg_Dbg( p_demux, " - physical_wide=%d",
685 event->physical_wide );
686 msg_Dbg( p_demux, " - physical_letterbox=%d",
687 event->physical_letterbox);
688 msg_Dbg( p_demux, " - physical_pan_scan=%d",
689 event->physical_pan_scan );
691 ESSubtitleUpdate( p_demux );
692 p_sys->b_spu_change = true;
694 /* HACK to get the SPU tracks registered in the right order */
695 for( i = 0; i < 0x1f; i++ )
697 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
698 ESNew( p_demux, 0xbd20 + i );
704 case DVDNAV_AUDIO_STREAM_CHANGE:
706 dvdnav_audio_stream_change_event_t *event =
707 (dvdnav_audio_stream_change_event_t*)packet;
708 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
709 msg_Dbg( p_demux, " - physical=%d", event->physical );
714 case DVDNAV_VTS_CHANGE:
720 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
721 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
722 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
723 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
725 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
726 /* TODO check if we always have VTS and CELL */
727 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
730 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
732 for( i = 0; i < PS_TK_COUNT; i++ )
734 ps_track_t *tk = &p_sys->tk[i];
737 es_format_Clean( &tk->fmt );
738 if( tk->es ) es_out_Del( p_demux->out, tk->es );
743 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
744 &i_part ) == DVDNAV_STATUS_OK )
746 if( i_title >= 0 && i_title < p_sys->i_title &&
747 p_demux->info.i_title != i_title )
749 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
750 p_demux->info.i_title = i_title;
756 case DVDNAV_CELL_CHANGE:
761 dvdnav_cell_change_event_t *event =
762 (dvdnav_cell_change_event_t*)packet;
763 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
764 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
765 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
766 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
767 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
768 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
769 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
770 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
772 /* Store the lenght in time of the current PGC */
773 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
775 /* FIXME is it correct or there is better way to know chapter change */
776 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
777 &i_part ) == DVDNAV_STATUS_OK )
779 if( i_title >= 0 && i_title < p_sys->i_title &&
780 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
781 p_demux->info.i_seekpoint != i_part - 1 )
783 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
784 p_demux->info.i_seekpoint = i_part - 1;
790 case DVDNAV_NAV_PACKET:
793 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
795 /* A lot of thing to do here :
797 * - fetch pts (for time display)
800 DemuxBlock( p_demux, packet, i_len );
801 if( p_sys->b_spu_change )
803 ButtonUpdate( p_demux, false );
804 p_sys->b_spu_change = false;
809 case DVDNAV_STOP: /* EOF */
810 msg_Dbg( p_demux, "DVDNAV_STOP" );
813 dvdnav_free_cache_block( p_sys->dvdnav, packet );
817 case DVDNAV_HIGHLIGHT:
819 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
820 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
821 msg_Dbg( p_demux, " - display=%d", event->display );
822 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
823 ButtonUpdate( p_demux, false );
827 case DVDNAV_HOP_CHANNEL:
828 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
829 /* We should try to flush all our internal buffer */
833 msg_Dbg( p_demux, "DVDNAV_WAIT" );
836 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
837 dvdnav_wait_skip( p_sys->dvdnav );
841 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
846 dvdnav_free_cache_block( p_sys->dvdnav, packet );
853 * FIXME: partiallyy duplicated from src/input/es_out.c
855 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
857 const iso639_lang_t *pl;
861 psz_lang = var_CreateGetString( p_demux, psz_var );
862 /* XXX: we will use only the first value
863 * (and ignore other ones in case of a list) */
864 if( ( p = strchr( psz_lang, ',' ) ) ) *p = '\0';
866 for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
868 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
869 !strcasecmp( pl->psz_native_name, psz_lang ) ||
870 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
871 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
872 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
878 if( pl->psz_iso639_1 != NULL )
879 return strdup( pl->psz_iso639_1 );
881 return strdup(LANGUAGE_DEFAULT);
884 static void DemuxTitles( demux_t *p_demux )
886 demux_sys_t *p_sys = p_demux->p_sys;
893 t = vlc_input_title_New();
895 t->psz_name = strdup( "DVD Menu" );
897 s = vlc_seekpoint_New();
898 s->psz_name = strdup( "Resume" );
899 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
901 s = vlc_seekpoint_New();
902 s->psz_name = strdup( "Root" );
903 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
905 s = vlc_seekpoint_New();
906 s->psz_name = strdup( "Title" );
907 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
909 s = vlc_seekpoint_New();
910 s->psz_name = strdup( "Chapter" );
911 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
913 s = vlc_seekpoint_New();
914 s->psz_name = strdup( "Subtitle" );
915 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
917 s = vlc_seekpoint_New();
918 s->psz_name = strdup( "Audio" );
919 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
921 s = vlc_seekpoint_New();
922 s->psz_name = strdup( "Angle" );
923 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
925 TAB_APPEND( p_sys->i_title, p_sys->title, t );
927 /* Find out number of titles/chapters */
928 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
929 for( i = 1; i <= i_titles; i++ )
931 int32_t i_chapters = 0;
934 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
936 t = vlc_input_title_New();
937 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
939 s = vlc_seekpoint_New();
940 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
943 TAB_APPEND( p_sys->i_title, p_sys->title, t );
947 /*****************************************************************************
949 *****************************************************************************/
950 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
952 demux_sys_t *p_sys = p_demux->p_sys;
954 int32_t i_title, i_part;
956 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
958 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
960 vlc_mutex_t *p_mutex = val.p_address;
961 dvdnav_highlight_area_t hl;
965 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
966 != DVDNAV_STATUS_OK )
968 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
973 if( i_button > 0 && i_title == 0 )
975 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
977 b_button_ok = dvdnav_get_highlight_area( pci, i_button, b_mode, &hl ) == DVDNAV_STATUS_OK;
982 for( i = 0; i < 4; i++ )
984 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
985 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
987 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
988 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
989 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
990 p_sys->palette[i][3] = i_alpha;
993 vlc_mutex_lock( p_mutex );
994 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
995 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
996 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
997 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
999 val.p_address = (void *)p_sys->palette;
1000 var_Set( p_sys->p_input, "menu-palette", val );
1002 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
1003 vlc_mutex_unlock( p_mutex );
1005 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1009 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1010 i_button, i_title );
1013 vlc_mutex_lock( p_mutex );
1015 var_Set( p_sys->p_input, "highlight", val );
1016 vlc_mutex_unlock( p_mutex );
1021 static void ESSubtitleUpdate( demux_t *p_demux )
1023 demux_sys_t *p_sys = p_demux->p_sys;
1024 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1025 int32_t i_title, i_part;
1027 ButtonUpdate( p_demux, false );
1029 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1030 if( i_title > 0 ) return;
1032 if( i_spu >= 0 && i_spu <= 0x1f )
1034 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1036 ESNew( p_demux, 0xbd20 + i_spu );
1038 /* be sure to unselect it (reset) */
1039 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1043 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1047 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1049 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1052 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1059 /*****************************************************************************
1060 * DemuxBlock: demux a given block
1061 *****************************************************************************/
1062 static int DemuxBlock( demux_t *p_demux, const uint8_t *pkt, int i_pkt )
1064 demux_sys_t *p_sys = p_demux->p_sys;
1065 const uint8_t *p = pkt;
1067 while( p < &pkt[i_pkt] )
1069 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1076 /* Create a block */
1077 p_pkt = block_New( p_demux, i_size );
1078 memcpy( p_pkt->p_buffer, p, i_size);
1080 /* Parse it and send it */
1081 switch( 0x100 | p[3] )
1089 msg_Warn( p_demux, "received a PSM packet" );
1091 else if( p[3] == 0xbb )
1093 msg_Warn( p_demux, "received a SYSTEM packet" );
1096 block_Release( p_pkt );
1103 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1105 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
1106 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1108 block_Release( p_pkt );
1113 int i_id = ps_pkt_id( p_pkt );
1116 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1120 ESNew( p_demux, i_id );
1122 if( tk->b_seen && tk->es &&
1123 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1125 es_out_Send( p_demux->out, tk->es, p_pkt );
1129 block_Release( p_pkt );
1134 block_Release( p_pkt );
1146 /*****************************************************************************
1147 * ESNew: register a new elementary stream
1148 *****************************************************************************/
1149 static void ESNew( demux_t *p_demux, int i_id )
1151 demux_sys_t *p_sys = p_demux->p_sys;
1152 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1153 bool b_select = false;
1155 if( tk->b_seen ) return;
1157 if( ps_track_fill( tk, 0, i_id ) )
1159 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1164 if( tk->fmt.i_cat == VIDEO_ES )
1166 switch( p_sys->i_aspect )
1168 case 1: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR; break;
1169 case 2: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3; break;
1170 case 3: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9; break;
1171 case 4: tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * 221 / 10; break;
1173 tk->fmt.video.i_aspect = 0;
1178 else if( tk->fmt.i_cat == AUDIO_ES )
1181 /* find the audio number PLEASE find another way */
1182 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1184 i_audio = i_id&0x07;
1186 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1190 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1192 i_audio = i_id&0x1f;
1194 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1196 i_audio = i_id&0x1f;
1200 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1201 if( i_lang != 0xffff )
1203 tk->fmt.psz_language = malloc( 3 );
1204 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1205 tk->fmt.psz_language[1] = (i_lang )&0xff;
1206 tk->fmt.psz_language[2] = 0;
1208 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1214 else if( tk->fmt.i_cat == SPU_ES )
1216 int32_t i_title, i_part;
1217 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1218 if( i_lang != 0xffff )
1220 tk->fmt.psz_language = malloc( 3 );
1221 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1222 tk->fmt.psz_language[1] = (i_lang )&0xff;
1223 tk->fmt.psz_language[2] = 0;
1227 tk->fmt.subs.spu.palette[0] = 0xBeef;
1228 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1229 16 * sizeof( uint32_t ) );
1231 /* We select only when we are not in the menu */
1232 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1234 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1240 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1243 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1247 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1250 /*****************************************************************************
1251 * Event handler code
1252 *****************************************************************************/
1253 static int EventMouse( vlc_object_t *, char const *,
1254 vlc_value_t, vlc_value_t, void * );
1255 static int EventKey ( vlc_object_t *, char const *,
1256 vlc_value_t, vlc_value_t, void * );
1258 static void* EventThread( vlc_object_t *p_this )
1260 event_thread_t *p_ev = (event_thread_t*)p_this;
1261 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1262 vlc_object_t *p_vout = NULL;
1264 vlc_mutex_init( &p_ev->lock );
1265 p_ev->b_moved = false;
1266 p_ev->b_clicked = false;
1267 p_ev->i_key_action = 0;
1268 p_ev->b_still = false;
1270 int canc = vlc_savecancel ();
1272 /* catch all key event */
1273 var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
1276 while( vlc_object_alive (p_ev) )
1278 bool b_activated = false;
1281 if( p_ev->i_key_action != 0 )
1283 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1285 vlc_mutex_lock( &p_ev->lock );
1286 switch( p_ev->i_key_action )
1288 case ACTIONID_NAV_LEFT:
1289 dvdnav_left_button_select( p_sys->dvdnav, pci );
1291 case ACTIONID_NAV_RIGHT:
1292 dvdnav_right_button_select( p_sys->dvdnav, pci );
1294 case ACTIONID_NAV_UP:
1295 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1297 case ACTIONID_NAV_DOWN:
1298 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1300 case ACTIONID_NAV_ACTIVATE:
1302 ButtonUpdate( p_ev->p_demux, true );
1303 dvdnav_button_activate( p_sys->dvdnav, pci );
1308 p_ev->i_key_action = 0;
1309 vlc_mutex_unlock( &p_ev->lock );
1313 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1315 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1316 vlc_value_t valx, valy;
1318 vlc_mutex_lock( &p_ev->lock );
1319 var_Get( p_vout, "mouse-x", &valx );
1320 var_Get( p_vout, "mouse-y", &valy );
1324 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1327 if( p_ev->b_clicked )
1330 ButtonUpdate( p_ev->p_demux, true );
1331 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1335 p_ev->b_moved = false;
1336 p_ev->b_clicked = false;
1337 vlc_mutex_unlock( &p_ev->lock );
1339 if( p_vout && !vlc_object_alive (p_vout) )
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 );
1346 if( p_vout == NULL )
1348 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1352 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1353 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1358 vlc_mutex_lock( &p_ev->lock );
1361 if( /* b_activated || // This breaks menus */
1362 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1364 p_ev->b_still = false;
1365 dvdnav_still_skip( p_sys->dvdnav );
1368 vlc_mutex_unlock( &p_ev->lock );
1374 /* Release callback */
1377 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1378 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1379 vlc_object_release( p_vout );
1381 var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
1382 vlc_restorecancel (canc);
1383 vlc_mutex_destroy( &p_ev->lock );
1388 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1389 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1391 (void)p_this; (void)oldval; (void)newval; (void)p_data;
1392 event_thread_t *p_ev = p_data;
1393 vlc_mutex_lock( &p_ev->lock );
1394 if( psz_var[6] == 'c' )
1395 p_ev->b_clicked = true;
1396 else if( psz_var[6] == 'm' )
1397 p_ev->b_moved = true;
1398 vlc_mutex_unlock( &p_ev->lock );
1403 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1404 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1406 (void)p_this; (void)psz_var; (void)oldval;
1407 event_thread_t *p_ev = p_data;
1408 vlc_mutex_lock( &p_ev->lock );
1409 p_ev->i_key_action = newval.i_int;
1410 vlc_mutex_unlock( &p_ev->lock );
1415 /*****************************************************************************
1416 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1417 *****************************************************************************/
1418 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1421 #ifdef HAVE_SYS_STAT_H
1422 struct stat stat_info;
1423 uint8_t pi_anchor[2];
1424 uint16_t i_tag_id = 0;
1429 /* Triggers libdvdcss autodetection */
1433 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1435 /* Let dvdnav_open() do the probing */
1439 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1441 /* Let dvdnav_open() do the probing */
1445 /* Try to find the anchor (2 bytes at LBA 256) */
1446 i_ret = VLC_SUCCESS;
1447 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1449 i_ret = VLC_EGENERIC;
1452 if( read( i_fd, pi_anchor, 2 ) == 2 )
1454 i_tag_id = GetWLE(pi_anchor);
1455 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1459 i_ret = VLC_EGENERIC;