1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
45 #include <dvdnav/dvdnav.h>
47 #include "../demux/ps.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 #define CACHING_TEXT N_("Caching value in ms")
53 #define CACHING_LONGTEXT N_( \
54 "Allows you to modify the default caching value for DVDnav streams. This "\
55 "value should be set in millisecond units." )
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
61 set_description( _("DVDnav Input") );
62 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
63 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
64 set_capability( "access_demux", 5 );
65 add_shortcut( "dvd" );
66 add_shortcut( "dvdnav" );
67 add_shortcut( "dvdnavsimple" );
68 set_callbacks( Open, Close );
71 /* Shall we use libdvdnav's read ahead cache? */
72 #define DVD_READ_CACHE 1
74 /*****************************************************************************
76 *****************************************************************************/
77 static char *ParseCL( vlc_object_t *, char *, vlc_bool_t, int *, int *, int *);
95 static int EventThread( vlc_object_t * );
102 ps_track_t tk[PS_TK_COUNT];
105 /* for spu variables */
106 input_thread_t *p_input;
109 event_thread_t *p_ev;
119 vlc_bool_t b_es_out_ok;
123 input_title_t **title;
126 static int Control( demux_t *, int, va_list );
127 static int Demux ( demux_t * );
128 static int DemuxBlock ( demux_t *, uint8_t *pkt, int i_pkt );
130 static void DemuxTitles( demux_t *p_demux );
131 static void ESSubtitleUpdate( demux_t * );
132 static void ButtonUpdate( demux_t * );
134 static int ProbeDVD( demux_t *, char * );
136 /*****************************************************************************
138 *****************************************************************************/
139 static int Open( vlc_object_t *p_this )
141 demux_t *p_demux = (demux_t*)p_this;
144 int i_title, i_chapter, i_angle;
147 psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
148 &i_title, &i_chapter, &i_angle );
154 /* Try some simple probing to avoid going through dvdnav_open too often */
155 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
161 msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
162 psz_name, i_title, i_chapter, i_angle );
165 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
167 msg_Warn( p_demux, "cannot open dvdnav" );
173 /* Fill p_demux field */
174 p_demux->pf_demux = Demux;
175 p_demux->pf_control = Control;
176 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
177 memset( p_sys, 0, sizeof( demux_sys_t ) );
178 p_sys->dvdnav = p_dvdnav;
181 strcmp( p_demux->psz_access, "dvdnavsimple" ) ? VLC_FALSE : VLC_TRUE;
182 if( p_sys->b_simple && i_title < 1 )
188 ps_track_init( p_sys->tk );
189 p_sys->i_aspect = -1;
190 p_sys->b_es_out_ok = VLC_FALSE;
191 p_sys->i_mux_rate = 0;
195 // Hack for libdvdnav CVS.
196 // Without it dvdnav_get_number_of_titles() fails.
197 // Remove when fixed in libdvdnav CVS.
198 uint8_t buffer[DVD_VIDEO_LB_LEN];
201 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
202 == DVDNAV_STATUS_ERR )
204 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
207 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
210 /* Configure dvdnav */
211 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
214 msg_Warn( p_demux, "cannot set read-a-head flag" );
217 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
220 msg_Warn( p_demux, "cannot set PGC positioning flag" );
223 if( dvdnav_menu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
224 dvdnav_audio_language_select(p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
225 dvdnav_spu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
227 msg_Warn( p_demux, "something failed while setting en language (%s)",
228 dvdnav_err_to_string( p_sys->dvdnav ) );
231 DemuxTitles( p_demux );
233 /* Set forced title/chapter */
236 if( dvdnav_title_play( p_sys->dvdnav, i_title ) != DVDNAV_STATUS_OK )
238 msg_Warn( p_demux, "cannot set title" );
243 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
244 p_demux->info.i_title = i_title;
248 if( i_chapter != 1 && i_title != 0 )
250 if( dvdnav_part_play( p_sys->dvdnav, i_title, i_chapter ) !=
253 msg_Warn( p_demux, "cannot set chapter" );
258 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
259 p_demux->info.i_seekpoint = i_chapter;
263 /* Update default_pts to a suitable value for dvdnav access */
264 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
266 /* For simple mode (no menus), we're done */
267 if( p_sys->b_simple ) return VLC_SUCCESS;
269 /* FIXME hack hack hack hack FIXME */
270 /* Get p_input and create variable */
271 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
272 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
273 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
274 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
275 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
276 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
277 var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
278 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
279 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
281 /* Now create our event thread catcher */
282 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
283 p_sys->p_ev->p_demux = p_demux;
284 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
285 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
290 /*****************************************************************************
292 *****************************************************************************/
293 static void Close( vlc_object_t *p_this )
295 demux_t *p_demux = (demux_t*)p_this;
296 demux_sys_t *p_sys = p_demux->p_sys;
299 if( !p_sys->b_simple )
301 /* stop the event handler */
302 p_sys->p_ev->b_die = VLC_TRUE;
303 vlc_thread_join( p_sys->p_ev );
304 vlc_object_destroy( p_sys->p_ev );
306 var_Destroy( p_sys->p_input, "highlight-mutex" );
307 var_Destroy( p_sys->p_input, "highlight" );
308 var_Destroy( p_sys->p_input, "x-start" );
309 var_Destroy( p_sys->p_input, "x-end" );
310 var_Destroy( p_sys->p_input, "y-start" );
311 var_Destroy( p_sys->p_input, "y-end" );
312 var_Destroy( p_sys->p_input, "color" );
313 var_Destroy( p_sys->p_input, "contrast" );
315 vlc_object_release( p_sys->p_input );
318 for( i = 0; i < PS_TK_COUNT; i++ )
320 ps_track_t *tk = &p_sys->tk[i];
323 es_format_Clean( &tk->fmt );
324 if( tk->es ) es_out_Del( p_demux->out, tk->es );
328 dvdnav_close( p_sys->dvdnav );
332 /*****************************************************************************
334 *****************************************************************************/
335 static int Control( demux_t *p_demux, int i_query, va_list args )
337 demux_sys_t *p_sys = p_demux->p_sys;
341 input_title_t ***ppp_title;
347 case DEMUX_SET_POSITION:
348 case DEMUX_GET_POSITION:
350 case DEMUX_GET_LENGTH:
353 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
354 DVDNAV_STATUS_OK || len == 0 )
359 if( i_query == DEMUX_GET_POSITION )
361 pf = (double*)va_arg( args, double* );
362 *pf = (double)pos / (double)len;
365 else if( i_query == DEMUX_SET_POSITION )
367 f = (double)va_arg( args, double );
369 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
375 else if( i_query == DEMUX_GET_TIME )
377 pi64 = (int64_t*)va_arg( args, int64_t * );
378 if( p_sys->i_mux_rate > 0 )
380 *pi64 = (int64_t)1000000 * 2048 * pos / 50 /
385 else if( i_query == DEMUX_GET_LENGTH )
387 pi64 = (int64_t*)va_arg( args, int64_t * );
388 if( p_sys->i_mux_rate > 0 )
390 *pi64 = (int64_t)1000000 * len * 2048 / 50 /
399 /* Special for access_demux */
400 case DEMUX_CAN_PAUSE:
401 case DEMUX_CAN_CONTROL_PACE:
403 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
407 case DEMUX_SET_PAUSE_STATE:
410 case DEMUX_GET_TITLE_INFO:
411 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
412 pi_int = (int*)va_arg( args, int* );
414 /* Duplicate title infos */
415 *pi_int = p_sys->i_title;
416 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
417 for( i = 0; i < p_sys->i_title; i++ )
419 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
423 case DEMUX_SET_TITLE:
424 i = (int)va_arg( args, int );
425 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
426 != DVDNAV_STATUS_OK ) ||
427 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
428 != DVDNAV_STATUS_OK ) )
430 msg_Warn( p_demux, "cannot set title/chapter" );
433 p_demux->info.i_update |=
434 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
435 p_demux->info.i_title = i;
436 p_demux->info.i_seekpoint = 0;
439 case DEMUX_SET_SEEKPOINT:
440 i = (int)va_arg( args, int );
441 if( p_demux->info.i_title == 0 )
448 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
451 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
454 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
457 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
460 i_ret = dvdnav_menu_call( p_sys->dvdnav,
461 DVD_MENU_Subpicture );
464 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
467 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
473 if( i_ret != DVDNAV_STATUS_OK )
476 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
477 i + 1 ) != DVDNAV_STATUS_OK )
479 msg_Warn( p_demux, "cannot set title/chapter" );
482 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
483 p_demux->info.i_seekpoint = i;
486 case DEMUX_GET_PTS_DELAY:
487 pi64 = (int64_t*)va_arg( args, int64_t * );
488 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
491 /* TODO implement others */
497 /*****************************************************************************
499 *****************************************************************************/
500 static int Demux( demux_t *p_demux )
502 demux_sys_t *p_sys = p_demux->p_sys;
504 uint8_t buffer[DVD_VIDEO_LB_LEN];
505 uint8_t *packet = buffer;
509 if( !p_sys->b_es_out_ok )
511 if( !p_sys->b_simple )
513 /* We do ourself the selection/unselection
514 * Problem: bypass --audio-channel and --spu-channel
515 * Solution: call ourself dvdnav_??set_channel -> TODO
517 es_out_Control( p_demux->out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
520 p_sys->b_es_out_ok = VLC_TRUE;
524 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
525 == DVDNAV_STATUS_ERR )
527 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
528 == DVDNAV_STATUS_ERR )
531 msg_Warn( p_demux, "cannot get next block (%s)",
532 dvdnav_err_to_string( p_sys->dvdnav ) );
538 case DVDNAV_BLOCK_OK: /* mpeg block */
539 DemuxBlock( p_demux, packet, i_len );
542 case DVDNAV_NOP: /* Nothing */
543 msg_Dbg( p_demux, "DVDNAV_NOP" );
546 case DVDNAV_STILL_FRAME:
548 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
549 if( !p_sys->b_simple )
551 vlc_mutex_lock( &p_sys->p_ev->lock );
552 if( !p_sys->p_ev->b_still )
554 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
555 msg_Dbg( p_demux, " - length=0x%x", event->length );
556 p_sys->p_ev->b_still = VLC_TRUE;
557 if( event->length == 0xff )
559 p_sys->p_ev->i_still_end = 0;
563 p_sys->p_ev->i_still_end = (int64_t)event->length *
564 1000000 + mdate() + p_sys->p_input->i_pts_delay;
567 vlc_mutex_unlock( &p_sys->p_ev->lock );
572 dvdnav_still_skip( p_sys->dvdnav );
576 case DVDNAV_SPU_STREAM_CHANGE:
578 dvdnav_spu_stream_change_event_t *event =
579 (dvdnav_spu_stream_change_event_t*)packet;
580 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
581 msg_Dbg( p_demux, " - physical_wide=%d",
582 event->physical_wide );
583 msg_Dbg( p_demux, " - physical_letterbox=%d",
584 event->physical_letterbox);
585 msg_Dbg( p_demux, " - physical_pan_scan=%d",
586 event->physical_pan_scan );
588 if( !p_sys->b_simple )
590 ESSubtitleUpdate( p_demux );
594 case DVDNAV_AUDIO_STREAM_CHANGE:
596 dvdnav_audio_stream_change_event_t *event =
597 (dvdnav_audio_stream_change_event_t*)packet;
598 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
599 msg_Dbg( p_demux, " - physical=%d", event->physical );
603 case DVDNAV_VTS_CHANGE:
609 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
610 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
611 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
612 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
614 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
615 /* TODO check if we always have VTS and CELL */
616 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
619 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
621 for( i = 0; i < PS_TK_COUNT; i++ )
623 ps_track_t *tk = &p_sys->tk[i];
626 es_format_Clean( &tk->fmt );
627 if( tk->es ) es_out_Del( p_demux->out, tk->es );
629 tk->b_seen = VLC_FALSE;
632 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
633 &i_part ) == DVDNAV_STATUS_OK )
635 if( p_sys->b_simple && i_title == 0 )
637 /* we have returned in menu, stop dvd */
638 /* FIXME is it the right way ? */
641 if( i_title >= 0 && i_title < p_sys->i_title &&
642 p_demux->info.i_title != i_title )
644 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
645 p_demux->info.i_title = i_title;
650 case DVDNAV_CELL_CHANGE:
655 dvdnav_cell_change_event_t *event =
656 (dvdnav_cell_change_event_t*)packet;
657 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
658 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
659 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
660 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
661 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
662 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
663 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
664 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
666 /* FIXME is it correct or there is better way to know chapter change */
667 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
668 &i_part ) == DVDNAV_STATUS_OK )
670 if( i_title >= 0 && i_title < p_sys->i_title &&
671 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
672 p_demux->info.i_seekpoint != i_part - 1 )
674 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
675 p_demux->info.i_seekpoint = i_part - 1;
681 case DVDNAV_NAV_PACKET:
684 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
686 /* A lot of thing to do here :
688 * - fetch pts (for time display)
691 DemuxBlock( p_demux, packet, i_len );
694 case DVDNAV_STOP: /* EOF */
695 msg_Dbg( p_demux, "DVDNAV_STOP" );
698 case DVDNAV_HIGHLIGHT:
700 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
701 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
702 msg_Dbg( p_demux, " - display=%d", event->display );
703 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
704 if( !p_sys->b_simple )
706 ButtonUpdate( p_demux );
711 case DVDNAV_SPU_CLUT_CHANGE:
712 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
713 /* Update color lookup table (16 *uint32_t in packet) */
714 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
717 case DVDNAV_HOP_CHANNEL:
718 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
719 /* We should try to flush all our internal buffer */
723 msg_Dbg( p_demux, "DVDNAV_WAIT" );
725 dvdnav_wait_skip( p_sys->dvdnav );
729 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
734 dvdnav_free_cache_block( p_sys->dvdnav, packet );
740 /*****************************************************************************
741 * ParseCL: parse command line.
742 * Titles start from 0 (menu), chapters and angles start from 1.
743 *****************************************************************************/
744 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
745 int *i_title, int *i_chapter, int *i_angle )
747 char *psz_parser, *psz_source, *psz_next;
749 psz_source = strdup( psz_name );
750 if( psz_source == NULL ) return NULL;
756 /* Start with the end, because you could have :
757 * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
758 * (yes, this is kludgy). */
759 for( psz_parser = psz_source + strlen(psz_source) - 1;
760 psz_parser >= psz_source && *psz_parser != '@';
763 if( psz_parser >= psz_source && *psz_parser == '@' )
769 *i_title = (int)strtol( psz_parser, &psz_next, 10 );
772 psz_parser = psz_next + 1;
773 *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
776 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
781 *i_title = *i_title >= 0 ? *i_title : 0;
782 *i_chapter = *i_chapter > 0 ? *i_chapter : 1;
783 *i_angle = *i_angle > 0 ? *i_angle : 1;
792 psz_source = config_GetPsz( p_this, "dvd" );
793 if( !psz_source ) return NULL;
797 if( psz_source[0] && psz_source[1] == ':' &&
798 psz_source[2] == '\\' && psz_source[3] == '\0' )
800 psz_source[2] = '\0';
807 static void DemuxTitles( demux_t *p_demux )
809 demux_sys_t *p_sys = p_demux->p_sys;
816 t = vlc_input_title_New();
817 t->b_menu = VLC_TRUE;
818 t->psz_name = strdup( "DVD Menu" );
820 s = vlc_seekpoint_New();
821 s->psz_name = strdup( "Resume" );
822 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
824 s = vlc_seekpoint_New();
825 s->psz_name = strdup( "Root" );
826 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
828 s = vlc_seekpoint_New();
829 s->psz_name = strdup( "Title" );
830 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
832 s = vlc_seekpoint_New();
833 s->psz_name = strdup( "Chapter" );
834 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
836 s = vlc_seekpoint_New();
837 s->psz_name = strdup( "Subtitle" );
838 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
840 s = vlc_seekpoint_New();
841 s->psz_name = strdup( "Audio" );
842 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
844 s = vlc_seekpoint_New();
845 s->psz_name = strdup( "Angle" );
846 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
848 TAB_APPEND( p_sys->i_title, p_sys->title, t );
850 /* Find out number of titles/chapters */
851 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
852 for( i = 1; i <= i_titles; i++ )
854 int32_t i_chapters = 0;
857 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
859 t = vlc_input_title_New();
860 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
862 s = vlc_seekpoint_New();
863 s->psz_name = malloc( strlen( _("Chapter %i") ) + 20 );
864 sprintf( s->psz_name, _("Chapter %i"), j + 1 );
865 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
868 TAB_APPEND( p_sys->i_title, p_sys->title, t );
872 /*****************************************************************************
874 *****************************************************************************/
875 static void ButtonUpdate( demux_t *p_demux )
877 demux_sys_t *p_sys = p_demux->p_sys;
879 int32_t i_title, i_part;
881 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
883 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
885 vlc_mutex_t *p_mutex = val.p_address;
886 dvdnav_highlight_area_t hl;
889 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
890 != DVDNAV_STATUS_OK )
892 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
896 if( i_button > 0 && i_title == 0 )
898 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
900 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
902 /* I fear it is plain wrong */
903 //val.p_address = (void *)&hl.palette;
904 p_sys->alpha[0] = hl.palette&0x0f;
905 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
906 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
907 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
909 vlc_mutex_lock( p_mutex );
910 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
911 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
912 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
913 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
915 val.p_address = (void *)p_sys->alpha;
916 var_Set( p_sys->p_input, "contrast", val );
918 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
919 vlc_mutex_unlock( p_mutex );
921 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
925 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
929 vlc_mutex_lock( p_mutex );
930 val.b_bool = VLC_FALSE;
931 var_Set( p_sys->p_input, "highlight", val );
932 vlc_mutex_unlock( p_mutex );
937 static void ESNew( demux_t *p_demux, int i_id );
939 static void ESSubtitleUpdate( demux_t *p_demux )
941 demux_sys_t *p_sys = p_demux->p_sys;
942 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
943 int32_t i_title, i_part;
945 ButtonUpdate( p_demux );
947 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
953 if( i_spu >= 0 && i_spu <= 0x1f )
955 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
959 ESNew( p_demux, 0xbd20 + i_spu);
961 /* be sure to unselect it (reset) */
962 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
963 (vlc_bool_t)VLC_FALSE );
966 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
970 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
972 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
975 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
976 (vlc_bool_t)VLC_FALSE );
982 /*****************************************************************************
983 * DemuxBlock: demux a given block
984 *****************************************************************************/
985 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
987 demux_sys_t *p_sys = p_demux->p_sys;
990 while( p < &pkt[i_pkt] )
992 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1000 p_pkt = block_New( p_demux, i_size );
1001 memcpy( p_pkt->p_buffer, p, i_size);
1003 /* Parse it and send it */
1004 switch( 0x100 | p[3] )
1012 msg_Warn( p_demux, "received a PSM packet" );
1014 else if( p[3] == 0xbb )
1016 msg_Warn( p_demux, "received a SYSTEM packet" );
1019 block_Release( p_pkt );
1026 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1028 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
1029 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1031 block_Release( p_pkt );
1036 int i_id = ps_pkt_id( p_pkt );
1039 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1043 ESNew( p_demux, i_id );
1045 if( tk->b_seen && tk->es &&
1046 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1048 es_out_Send( p_demux->out, tk->es, p_pkt );
1052 block_Release( p_pkt );
1057 block_Release( p_pkt );
1069 /*****************************************************************************
1070 * ESNew: register a new elementary stream
1071 *****************************************************************************/
1072 static void ESNew( demux_t *p_demux, int i_id )
1074 demux_sys_t *p_sys = p_demux->p_sys;
1075 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1076 vlc_bool_t b_select = VLC_FALSE;
1083 if( ps_track_fill( tk, i_id ) )
1085 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1090 if( tk->fmt.i_cat == VIDEO_ES )
1092 if( p_sys->i_aspect >= 0 )
1094 tk->fmt.video.i_aspect = p_sys->i_aspect;
1096 b_select = VLC_TRUE;
1098 else if( tk->fmt.i_cat == AUDIO_ES )
1101 /* find the audio number PLEASE find another way */
1102 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1104 i_audio = i_id&0x07;
1106 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1110 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1112 i_audio = i_id&0x1f;
1114 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1116 i_audio = i_id&0x1f;
1120 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1121 if( i_lang != 0xffff )
1123 tk->fmt.psz_language = malloc( 3 );
1124 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1125 tk->fmt.psz_language[1] = (i_lang )&0xff;
1126 tk->fmt.psz_language[2] = 0;
1128 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1130 b_select = VLC_TRUE;
1134 else if( tk->fmt.i_cat == SPU_ES )
1136 int32_t i_title, i_part;
1137 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1138 if( i_lang != 0xffff )
1140 tk->fmt.psz_language = malloc( 3 );
1141 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1142 tk->fmt.psz_language[1] = (i_lang )&0xff;
1143 tk->fmt.psz_language[2] = 0;
1147 tk->fmt.subs.spu.palette[0] = 0xBeef;
1148 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1149 16 * sizeof( uint32_t ) );
1151 /* We select only when we are not in the menu */
1152 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1154 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1156 b_select = VLC_TRUE;
1160 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1163 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1165 tk->b_seen = VLC_TRUE;
1168 /*****************************************************************************
1169 * Event handler code
1170 *****************************************************************************/
1171 static int EventMouse( vlc_object_t *, char const *,
1172 vlc_value_t, vlc_value_t, void * );
1173 static int EventKey ( vlc_object_t *, char const *,
1174 vlc_value_t, vlc_value_t, void * );
1176 static int EventThread( vlc_object_t *p_this )
1178 event_thread_t *p_ev = (event_thread_t*)p_this;
1179 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1180 vlc_object_t *p_vout = NULL;
1182 vlc_mutex_init( p_ev, &p_ev->lock );
1183 p_ev->b_moved = VLC_FALSE;
1184 p_ev->b_clicked = VLC_FALSE;
1185 p_ev->b_key = VLC_FALSE;
1186 p_ev->b_still = VLC_FALSE;
1188 /* catch all key event */
1189 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1192 while( !p_ev->b_die )
1194 vlc_bool_t b_activated = VLC_FALSE;
1199 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1202 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1203 int i, i_action = -1;
1205 vlc_mutex_lock( &p_ev->lock );
1206 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1207 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1209 if( p_hotkeys[i].i_key == valk.i_int )
1211 i_action = p_hotkeys[i].i_action;
1217 case ACTIONID_NAV_LEFT:
1218 dvdnav_left_button_select( p_sys->dvdnav, pci );
1220 case ACTIONID_NAV_RIGHT:
1221 dvdnav_right_button_select( p_sys->dvdnav, pci );
1223 case ACTIONID_NAV_UP:
1224 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1226 case ACTIONID_NAV_DOWN:
1227 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1229 case ACTIONID_NAV_ACTIVATE:
1230 b_activated = VLC_TRUE;
1231 dvdnav_button_activate( p_sys->dvdnav, pci );
1236 p_ev->b_key = VLC_FALSE;
1237 vlc_mutex_unlock( &p_ev->lock );
1241 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1243 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1244 vlc_value_t valx, valy;
1246 vlc_mutex_lock( &p_ev->lock );
1247 var_Get( p_vout, "mouse-x", &valx );
1248 var_Get( p_vout, "mouse-y", &valy );
1252 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1255 if( p_ev->b_clicked )
1257 b_activated = VLC_TRUE;
1258 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1262 p_ev->b_moved = VLC_FALSE;
1263 p_ev->b_clicked = VLC_FALSE;
1264 vlc_mutex_unlock( &p_ev->lock );
1266 if( p_vout && p_vout->b_die )
1268 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1269 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1270 vlc_object_release( p_vout );
1273 if( p_vout == NULL )
1275 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1279 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1280 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1285 vlc_mutex_lock( &p_ev->lock );
1288 if( /* b_activated || // This breaks menus */
1289 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1291 p_ev->b_still = VLC_FALSE;
1292 dvdnav_still_skip( p_sys->dvdnav );
1295 vlc_mutex_unlock( &p_ev->lock );
1301 /* Release callback */
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 );
1308 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1310 vlc_mutex_destroy( &p_ev->lock );
1315 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1316 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1318 event_thread_t *p_ev = p_data;
1319 vlc_mutex_lock( &p_ev->lock );
1320 if( psz_var[6] == 'c' )
1321 p_ev->b_clicked = VLC_TRUE;
1322 else if( psz_var[6] == 'm' )
1323 p_ev->b_moved = VLC_TRUE;
1324 vlc_mutex_unlock( &p_ev->lock );
1329 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1330 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1332 event_thread_t *p_ev = p_data;
1333 vlc_mutex_lock( &p_ev->lock );
1334 p_ev->b_key = VLC_TRUE;
1335 vlc_mutex_unlock( &p_ev->lock );
1340 /*****************************************************************************
1341 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1342 *****************************************************************************/
1343 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1345 #ifdef HAVE_SYS_STAT_H
1346 struct stat stat_info;
1347 uint8_t pi_anchor[2];
1348 uint16_t i_tag_id = 0;
1351 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1353 /* Let dvdnav_open() do the probing */
1357 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1359 /* Let dvdnav_open() do the probing */
1363 /* Try to find the anchor (2 bytes at LBA 256) */
1364 i_ret = VLC_SUCCESS;
1365 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1367 i_ret = VLC_EGENERIC;
1370 if( read( i_fd, pi_anchor, 2 ) == 2 )
1372 i_tag_id = GetWLE(pi_anchor);
1373 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1377 i_ret = VLC_EGENERIC;