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>
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
48 #include <dvdnav/dvdnav.h>
50 #include "../demux/ps.h"
52 /*****************************************************************************
54 *****************************************************************************/
55 #define CACHING_TEXT N_("Caching value in ms")
56 #define CACHING_LONGTEXT N_( \
57 "Allows you to modify the default caching value for DVDnav streams. This "\
58 "value should be set in millisecond units." )
59 #define MENU_TEXT N_("Start directly in menu")
60 #define MENU_LONGTEXT N_( \
61 "Allows you to start the DVD directly in the main menu. This "\
62 "will try to skip all the useless warnings introductions." )
64 static int Open ( vlc_object_t * );
65 static void Close( vlc_object_t * );
68 set_description( _("DVDnav Input") );
69 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
70 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
71 add_bool( "dvdnav-menu", VLC_TRUE, NULL,
72 MENU_TEXT, MENU_LONGTEXT, VLC_FALSE );
73 set_capability( "access_demux", 5 );
74 add_shortcut( "dvd" );
75 add_shortcut( "dvdnav" );
76 set_callbacks( Open, Close );
79 /* Shall we use libdvdnav's read ahead cache? */
80 #define DVD_READ_CACHE 1
82 /*****************************************************************************
84 *****************************************************************************/
85 static char *ParseCL( vlc_object_t *, char *, vlc_bool_t, int *, int *, int *);
103 static int EventThread( vlc_object_t * );
110 ps_track_t tk[PS_TK_COUNT];
113 /* for spu variables */
114 input_thread_t *p_input;
117 event_thread_t *p_ev;
127 input_title_t **title;
130 static int Control( demux_t *, int, va_list );
131 static int Demux( demux_t * );
132 static int DemuxBlock( demux_t *, uint8_t *, int );
134 static void DemuxTitles( demux_t * );
135 static void ESSubtitleUpdate( demux_t * );
136 static void ButtonUpdate( demux_t * );
138 static void ESNew( demux_t *, int );
139 static int ProbeDVD( demux_t *, char * );
141 /*****************************************************************************
143 *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
146 demux_t *p_demux = (demux_t*)p_this;
149 int i_title, i_chapter, i_angle;
153 psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
154 &i_title, &i_chapter, &i_angle );
160 /* Try some simple probing to avoid going through dvdnav_open too often */
161 if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
167 msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
168 psz_name, i_title, i_chapter, i_angle );
171 if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
173 msg_Warn( p_demux, "cannot open dvdnav" );
179 /* Fill p_demux field */
180 p_demux->pf_demux = Demux;
181 p_demux->pf_control = Control;
182 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
183 memset( p_sys, 0, sizeof( demux_sys_t ) );
184 p_sys->dvdnav = p_dvdnav;
186 ps_track_init( p_sys->tk );
187 p_sys->i_aspect = -1;
188 p_sys->i_mux_rate = 0;
192 // Hack for libdvdnav CVS.
193 // Without it dvdnav_get_number_of_titles() fails.
194 // Remove when fixed in libdvdnav CVS.
195 uint8_t buffer[DVD_VIDEO_LB_LEN];
198 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
199 == DVDNAV_STATUS_ERR )
201 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
204 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
207 /* Configure dvdnav */
208 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
211 msg_Warn( p_demux, "cannot set read-a-head flag" );
214 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
217 msg_Warn( p_demux, "cannot set PGC positioning flag" );
220 if( dvdnav_menu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
221 dvdnav_audio_language_select(p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
222 dvdnav_spu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
224 msg_Warn( p_demux, "something failed while setting en language (%s)",
225 dvdnav_err_to_string( p_sys->dvdnav ) );
228 DemuxTitles( p_demux );
230 /* Set forced title/chapter */
233 if( dvdnav_title_play( p_sys->dvdnav, i_title ) != DVDNAV_STATUS_OK )
235 msg_Warn( p_demux, "cannot set title" );
240 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
241 p_demux->info.i_title = i_title;
245 if( i_chapter > 1 && i_title > 0 )
247 if( dvdnav_part_play( p_sys->dvdnav, i_title, i_chapter ) !=
250 msg_Warn( p_demux, "cannot set chapter" );
255 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
256 p_demux->info.i_seekpoint = i_chapter;
260 var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
261 var_Get( p_demux, "dvdnav-menu", &val );
262 if( i_title == 0 || val.b_bool )
264 msg_Dbg( p_demux, "trying to go to dvd menu" );
266 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
268 msg_Warn( p_demux, "cannot set title" );
271 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
274 msg_Warn( p_demux, "cannot go to dvd menu" );
278 p_demux->info.i_update |=
279 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
280 p_demux->info.i_title = 0;
281 p_demux->info.i_seekpoint = 0;
285 /* Update default_pts to a suitable value for dvdnav access */
286 var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
288 /* FIXME hack hack hack hack FIXME */
289 /* Get p_input and create variable */
290 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
291 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
292 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
293 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
294 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
295 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
296 var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
297 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
298 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
300 /* Now create our event thread catcher */
301 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
302 p_sys->p_ev->p_demux = p_demux;
303 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
304 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
309 /*****************************************************************************
311 *****************************************************************************/
312 static void Close( vlc_object_t *p_this )
314 demux_t *p_demux = (demux_t*)p_this;
315 demux_sys_t *p_sys = p_demux->p_sys;
318 /* stop the event handler */
319 p_sys->p_ev->b_die = VLC_TRUE;
320 vlc_thread_join( p_sys->p_ev );
321 vlc_object_destroy( p_sys->p_ev );
323 var_Destroy( p_sys->p_input, "highlight-mutex" );
324 var_Destroy( p_sys->p_input, "highlight" );
325 var_Destroy( p_sys->p_input, "x-start" );
326 var_Destroy( p_sys->p_input, "x-end" );
327 var_Destroy( p_sys->p_input, "y-start" );
328 var_Destroy( p_sys->p_input, "y-end" );
329 var_Destroy( p_sys->p_input, "color" );
330 var_Destroy( p_sys->p_input, "contrast" );
332 vlc_object_release( p_sys->p_input );
334 for( i = 0; i < PS_TK_COUNT; i++ )
336 ps_track_t *tk = &p_sys->tk[i];
339 es_format_Clean( &tk->fmt );
340 if( tk->es ) es_out_Del( p_demux->out, tk->es );
344 dvdnav_close( p_sys->dvdnav );
348 /*****************************************************************************
350 *****************************************************************************/
351 static int Control( demux_t *p_demux, int i_query, va_list args )
353 demux_sys_t *p_sys = p_demux->p_sys;
357 input_title_t ***ppp_title;
363 case DEMUX_SET_POSITION:
364 case DEMUX_GET_POSITION:
366 case DEMUX_GET_LENGTH:
369 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
370 DVDNAV_STATUS_OK || len == 0 )
375 if( i_query == DEMUX_GET_POSITION )
377 pf = (double*)va_arg( args, double* );
378 *pf = (double)pos / (double)len;
381 else if( i_query == DEMUX_SET_POSITION )
383 f = (double)va_arg( args, double );
385 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
391 else if( i_query == DEMUX_GET_TIME )
393 pi64 = (int64_t*)va_arg( args, int64_t * );
394 if( p_sys->i_mux_rate > 0 )
396 *pi64 = (int64_t)1000000 * 2048 * pos / 50 /
401 else if( i_query == DEMUX_GET_LENGTH )
403 pi64 = (int64_t*)va_arg( args, int64_t * );
404 if( p_sys->i_mux_rate > 0 )
406 *pi64 = (int64_t)1000000 * len * 2048 / 50 /
415 /* Special for access_demux */
416 case DEMUX_CAN_PAUSE:
417 case DEMUX_CAN_CONTROL_PACE:
419 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
423 case DEMUX_SET_PAUSE_STATE:
426 case DEMUX_GET_TITLE_INFO:
427 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
428 pi_int = (int*)va_arg( args, int* );
430 /* Duplicate title infos */
431 *pi_int = p_sys->i_title;
432 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
433 for( i = 0; i < p_sys->i_title; i++ )
435 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
439 case DEMUX_SET_TITLE:
440 i = (int)va_arg( args, int );
441 if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
442 != DVDNAV_STATUS_OK ) ||
443 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
444 != DVDNAV_STATUS_OK ) )
446 msg_Warn( p_demux, "cannot set title/chapter" );
449 p_demux->info.i_update |=
450 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
451 p_demux->info.i_title = i;
452 p_demux->info.i_seekpoint = 0;
455 case DEMUX_SET_SEEKPOINT:
456 i = (int)va_arg( args, int );
457 if( p_demux->info.i_title == 0 )
464 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
467 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
470 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
473 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
476 i_ret = dvdnav_menu_call( p_sys->dvdnav,
477 DVD_MENU_Subpicture );
480 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
483 i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
489 if( i_ret != DVDNAV_STATUS_OK )
492 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
493 i + 1 ) != DVDNAV_STATUS_OK )
495 msg_Warn( p_demux, "cannot set title/chapter" );
498 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
499 p_demux->info.i_seekpoint = i;
502 case DEMUX_GET_PTS_DELAY:
503 pi64 = (int64_t*)va_arg( args, int64_t * );
504 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
507 /* TODO implement others */
513 /*****************************************************************************
515 *****************************************************************************/
516 static int Demux( demux_t *p_demux )
518 demux_sys_t *p_sys = p_demux->p_sys;
520 uint8_t buffer[DVD_VIDEO_LB_LEN];
521 uint8_t *packet = buffer;
526 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
527 == DVDNAV_STATUS_ERR )
529 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
530 == DVDNAV_STATUS_ERR )
533 msg_Warn( p_demux, "cannot get next block (%s)",
534 dvdnav_err_to_string( p_sys->dvdnav ) );
540 case DVDNAV_BLOCK_OK: /* mpeg block */
541 DemuxBlock( p_demux, packet, i_len );
544 case DVDNAV_NOP: /* Nothing */
545 msg_Dbg( p_demux, "DVDNAV_NOP" );
548 case DVDNAV_STILL_FRAME:
550 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
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 );
571 case DVDNAV_SPU_STREAM_CHANGE:
573 dvdnav_spu_stream_change_event_t *event =
574 (dvdnav_spu_stream_change_event_t*)packet;
575 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
576 msg_Dbg( p_demux, " - physical_wide=%d",
577 event->physical_wide );
578 msg_Dbg( p_demux, " - physical_letterbox=%d",
579 event->physical_letterbox);
580 msg_Dbg( p_demux, " - physical_pan_scan=%d",
581 event->physical_pan_scan );
583 ESSubtitleUpdate( p_demux );
586 case DVDNAV_AUDIO_STREAM_CHANGE:
588 dvdnav_audio_stream_change_event_t *event =
589 (dvdnav_audio_stream_change_event_t*)packet;
590 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
591 msg_Dbg( p_demux, " - physical=%d", event->physical );
595 case DVDNAV_VTS_CHANGE:
601 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
602 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
603 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
604 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
606 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
607 /* TODO check if we always have VTS and CELL */
608 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
611 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
613 for( i = 0; i < PS_TK_COUNT; i++ )
615 ps_track_t *tk = &p_sys->tk[i];
618 es_format_Clean( &tk->fmt );
619 if( tk->es ) es_out_Del( p_demux->out, tk->es );
621 tk->b_seen = VLC_FALSE;
624 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
625 &i_part ) == DVDNAV_STATUS_OK )
627 if( i_title >= 0 && i_title < p_sys->i_title &&
628 p_demux->info.i_title != i_title )
630 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
631 p_demux->info.i_title = i_title;
636 case DVDNAV_CELL_CHANGE:
641 dvdnav_cell_change_event_t *event =
642 (dvdnav_cell_change_event_t*)packet;
643 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
644 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
645 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
646 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
647 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
648 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
649 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
650 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
652 /* FIXME is it correct or there is better way to know chapter change */
653 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
654 &i_part ) == DVDNAV_STATUS_OK )
656 if( i_title >= 0 && i_title < p_sys->i_title &&
657 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
658 p_demux->info.i_seekpoint != i_part - 1 )
660 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
661 p_demux->info.i_seekpoint = i_part - 1;
667 case DVDNAV_NAV_PACKET:
670 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
672 /* A lot of thing to do here :
674 * - fetch pts (for time display)
677 DemuxBlock( p_demux, packet, i_len );
680 case DVDNAV_STOP: /* EOF */
681 msg_Dbg( p_demux, "DVDNAV_STOP" );
684 case DVDNAV_HIGHLIGHT:
686 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
687 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
688 msg_Dbg( p_demux, " - display=%d", event->display );
689 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
690 ButtonUpdate( p_demux );
694 case DVDNAV_SPU_CLUT_CHANGE:
698 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
699 /* Update color lookup table (16 *uint32_t in packet) */
700 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
702 /* HACK to get the SPU tracks registered in the right order */
703 for( i = 0; i < 0x1f; i++ )
705 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
706 ESNew( p_demux, 0xbd20 + i );
712 case DVDNAV_HOP_CHANNEL:
713 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
714 /* We should try to flush all our internal buffer */
718 msg_Dbg( p_demux, "DVDNAV_WAIT" );
720 dvdnav_wait_skip( p_sys->dvdnav );
724 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
729 dvdnav_free_cache_block( p_sys->dvdnav, packet );
735 /*****************************************************************************
736 * ParseCL: parse command line.
737 * Titles start from 0 (menu), chapters and angles start from 1.
738 *****************************************************************************/
739 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
740 int *i_title, int *i_chapter, int *i_angle )
742 char *psz_parser, *psz_source, *psz_next;
744 psz_source = strdup( psz_name );
745 if( psz_source == NULL ) return NULL;
751 /* Start with the end, because you could have :
752 * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
753 * (yes, this is kludgy). */
754 for( psz_parser = psz_source + strlen(psz_source) - 1;
755 psz_parser >= psz_source && *psz_parser != '@';
758 if( psz_parser >= psz_source && *psz_parser == '@' )
764 *i_title = (int)strtol( psz_parser, &psz_next, 10 );
767 psz_parser = psz_next + 1;
768 *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
771 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
776 *i_title = *i_title >= 0 ? *i_title : -1;
777 *i_chapter = *i_chapter > 0 ? *i_chapter : 1;
778 *i_angle = *i_angle > 0 ? *i_angle : 1;
787 psz_source = config_GetPsz( p_this, "dvd" );
788 if( !psz_source ) return NULL;
792 if( psz_source[0] && psz_source[1] == ':' &&
793 psz_source[2] == '\\' && psz_source[3] == '\0' )
795 psz_source[2] = '\0';
802 static void DemuxTitles( demux_t *p_demux )
804 demux_sys_t *p_sys = p_demux->p_sys;
811 t = vlc_input_title_New();
812 t->b_menu = VLC_TRUE;
813 t->psz_name = strdup( "DVD Menu" );
815 s = vlc_seekpoint_New();
816 s->psz_name = strdup( "Resume" );
817 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
819 s = vlc_seekpoint_New();
820 s->psz_name = strdup( "Root" );
821 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
823 s = vlc_seekpoint_New();
824 s->psz_name = strdup( "Title" );
825 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
827 s = vlc_seekpoint_New();
828 s->psz_name = strdup( "Chapter" );
829 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
831 s = vlc_seekpoint_New();
832 s->psz_name = strdup( "Subtitle" );
833 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
835 s = vlc_seekpoint_New();
836 s->psz_name = strdup( "Audio" );
837 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
839 s = vlc_seekpoint_New();
840 s->psz_name = strdup( "Angle" );
841 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
843 TAB_APPEND( p_sys->i_title, p_sys->title, t );
845 /* Find out number of titles/chapters */
846 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
847 for( i = 1; i <= i_titles; i++ )
849 int32_t i_chapters = 0;
852 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
854 t = vlc_input_title_New();
855 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
857 s = vlc_seekpoint_New();
858 s->psz_name = malloc( strlen( _("Chapter %i") ) + 20 );
859 sprintf( s->psz_name, _("Chapter %i"), j + 1 );
860 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
863 TAB_APPEND( p_sys->i_title, p_sys->title, t );
867 /*****************************************************************************
869 *****************************************************************************/
870 static void ButtonUpdate( demux_t *p_demux )
872 demux_sys_t *p_sys = p_demux->p_sys;
874 int32_t i_title, i_part;
876 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
878 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
880 vlc_mutex_t *p_mutex = val.p_address;
881 dvdnav_highlight_area_t hl;
884 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
885 != DVDNAV_STATUS_OK )
887 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
891 if( i_button > 0 && i_title == 0 )
893 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
895 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
897 /* I fear it is plain wrong */
898 //val.p_address = (void *)&hl.palette;
899 p_sys->alpha[0] = hl.palette&0x0f;
900 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
901 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
902 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
904 vlc_mutex_lock( p_mutex );
905 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
906 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
907 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
908 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
910 val.p_address = (void *)p_sys->alpha;
911 var_Set( p_sys->p_input, "contrast", val );
913 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
914 vlc_mutex_unlock( p_mutex );
916 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
920 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
924 vlc_mutex_lock( p_mutex );
925 val.b_bool = VLC_FALSE;
926 var_Set( p_sys->p_input, "highlight", val );
927 vlc_mutex_unlock( p_mutex );
932 static void ESSubtitleUpdate( demux_t *p_demux )
934 demux_sys_t *p_sys = p_demux->p_sys;
935 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
936 int32_t i_title, i_part;
938 ButtonUpdate( p_demux );
940 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
946 if( i_spu >= 0 && i_spu <= 0x1f )
948 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
952 ESNew( p_demux, 0xbd20 + i_spu);
954 /* be sure to unselect it (reset) */
955 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
956 (vlc_bool_t)VLC_FALSE );
959 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
963 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
965 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
968 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
969 (vlc_bool_t)VLC_FALSE );
975 /*****************************************************************************
976 * DemuxBlock: demux a given block
977 *****************************************************************************/
978 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
980 demux_sys_t *p_sys = p_demux->p_sys;
983 while( p < &pkt[i_pkt] )
985 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
993 p_pkt = block_New( p_demux, i_size );
994 memcpy( p_pkt->p_buffer, p, i_size);
996 /* Parse it and send it */
997 switch( 0x100 | p[3] )
1005 msg_Warn( p_demux, "received a PSM packet" );
1007 else if( p[3] == 0xbb )
1009 msg_Warn( p_demux, "received a SYSTEM packet" );
1012 block_Release( p_pkt );
1019 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1021 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
1022 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1024 block_Release( p_pkt );
1029 int i_id = ps_pkt_id( p_pkt );
1032 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1036 ESNew( p_demux, i_id );
1038 if( tk->b_seen && tk->es &&
1039 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1041 es_out_Send( p_demux->out, tk->es, p_pkt );
1045 block_Release( p_pkt );
1050 block_Release( p_pkt );
1062 /*****************************************************************************
1063 * ESNew: register a new elementary stream
1064 *****************************************************************************/
1065 static void ESNew( demux_t *p_demux, int i_id )
1067 demux_sys_t *p_sys = p_demux->p_sys;
1068 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1069 vlc_bool_t b_select = VLC_FALSE;
1076 if( ps_track_fill( tk, i_id ) )
1078 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1083 if( tk->fmt.i_cat == VIDEO_ES )
1085 if( p_sys->i_aspect >= 0 )
1087 tk->fmt.video.i_aspect = p_sys->i_aspect;
1089 b_select = VLC_TRUE;
1091 else if( tk->fmt.i_cat == AUDIO_ES )
1094 /* find the audio number PLEASE find another way */
1095 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1097 i_audio = i_id&0x07;
1099 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1103 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1105 i_audio = i_id&0x1f;
1107 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1109 i_audio = i_id&0x1f;
1113 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1114 if( i_lang != 0xffff )
1116 tk->fmt.psz_language = malloc( 3 );
1117 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1118 tk->fmt.psz_language[1] = (i_lang )&0xff;
1119 tk->fmt.psz_language[2] = 0;
1121 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1123 b_select = VLC_TRUE;
1127 else if( tk->fmt.i_cat == SPU_ES )
1129 int32_t i_title, i_part;
1130 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1131 if( i_lang != 0xffff )
1133 tk->fmt.psz_language = malloc( 3 );
1134 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1135 tk->fmt.psz_language[1] = (i_lang )&0xff;
1136 tk->fmt.psz_language[2] = 0;
1140 tk->fmt.subs.spu.palette[0] = 0xBeef;
1141 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1142 16 * sizeof( uint32_t ) );
1144 /* We select only when we are not in the menu */
1145 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1147 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1149 b_select = VLC_TRUE;
1153 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1156 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1158 tk->b_seen = VLC_TRUE;
1161 /*****************************************************************************
1162 * Event handler code
1163 *****************************************************************************/
1164 static int EventMouse( vlc_object_t *, char const *,
1165 vlc_value_t, vlc_value_t, void * );
1166 static int EventKey ( vlc_object_t *, char const *,
1167 vlc_value_t, vlc_value_t, void * );
1169 static int EventThread( vlc_object_t *p_this )
1171 event_thread_t *p_ev = (event_thread_t*)p_this;
1172 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1173 vlc_object_t *p_vout = NULL;
1175 vlc_mutex_init( p_ev, &p_ev->lock );
1176 p_ev->b_moved = VLC_FALSE;
1177 p_ev->b_clicked = VLC_FALSE;
1178 p_ev->b_key = VLC_FALSE;
1179 p_ev->b_still = VLC_FALSE;
1181 /* catch all key event */
1182 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1185 while( !p_ev->b_die )
1187 vlc_bool_t b_activated = VLC_FALSE;
1192 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1195 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1196 int i, i_action = -1;
1198 vlc_mutex_lock( &p_ev->lock );
1199 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1200 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1202 if( p_hotkeys[i].i_key == valk.i_int )
1204 i_action = p_hotkeys[i].i_action;
1210 case ACTIONID_NAV_LEFT:
1211 dvdnav_left_button_select( p_sys->dvdnav, pci );
1213 case ACTIONID_NAV_RIGHT:
1214 dvdnav_right_button_select( p_sys->dvdnav, pci );
1216 case ACTIONID_NAV_UP:
1217 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1219 case ACTIONID_NAV_DOWN:
1220 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1222 case ACTIONID_NAV_ACTIVATE:
1223 b_activated = VLC_TRUE;
1224 dvdnav_button_activate( p_sys->dvdnav, pci );
1229 p_ev->b_key = VLC_FALSE;
1230 vlc_mutex_unlock( &p_ev->lock );
1234 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1236 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1237 vlc_value_t valx, valy;
1239 vlc_mutex_lock( &p_ev->lock );
1240 var_Get( p_vout, "mouse-x", &valx );
1241 var_Get( p_vout, "mouse-y", &valy );
1245 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1248 if( p_ev->b_clicked )
1250 b_activated = VLC_TRUE;
1251 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1255 p_ev->b_moved = VLC_FALSE;
1256 p_ev->b_clicked = VLC_FALSE;
1257 vlc_mutex_unlock( &p_ev->lock );
1259 if( p_vout && p_vout->b_die )
1261 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1262 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1263 vlc_object_release( p_vout );
1266 if( p_vout == NULL )
1268 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1272 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1273 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1278 vlc_mutex_lock( &p_ev->lock );
1281 if( /* b_activated || // This breaks menus */
1282 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1284 p_ev->b_still = VLC_FALSE;
1285 dvdnav_still_skip( p_sys->dvdnav );
1288 vlc_mutex_unlock( &p_ev->lock );
1294 /* Release callback */
1297 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1298 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1299 vlc_object_release( p_vout );
1301 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1303 vlc_mutex_destroy( &p_ev->lock );
1308 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1309 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1311 event_thread_t *p_ev = p_data;
1312 vlc_mutex_lock( &p_ev->lock );
1313 if( psz_var[6] == 'c' )
1314 p_ev->b_clicked = VLC_TRUE;
1315 else if( psz_var[6] == 'm' )
1316 p_ev->b_moved = VLC_TRUE;
1317 vlc_mutex_unlock( &p_ev->lock );
1322 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1323 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1325 event_thread_t *p_ev = p_data;
1326 vlc_mutex_lock( &p_ev->lock );
1327 p_ev->b_key = VLC_TRUE;
1328 vlc_mutex_unlock( &p_ev->lock );
1333 /*****************************************************************************
1334 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1335 *****************************************************************************/
1336 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1338 #ifdef HAVE_SYS_STAT_H
1339 struct stat stat_info;
1340 uint8_t pi_anchor[2];
1341 uint16_t i_tag_id = 0;
1344 if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1346 /* Let dvdnav_open() do the probing */
1350 if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1352 /* Let dvdnav_open() do the probing */
1356 /* Try to find the anchor (2 bytes at LBA 256) */
1357 i_ret = VLC_SUCCESS;
1358 if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1360 i_ret = VLC_EGENERIC;
1363 if( read( i_fd, pi_anchor, 2 ) == 2 )
1365 i_tag_id = GetWLE(pi_anchor);
1366 if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1370 i_ret = VLC_EGENERIC;