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 #include <dvdnav/dvdnav.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 #define CACHING_TEXT N_("caching value in ms")
43 #define CACHING_LONGTEXT N_( \
44 "Allows you to modify the default caching value for DVDnav streams. This "\
45 "value should be set in millisecond units." )
47 static int AccessOpen ( vlc_object_t * );
48 static void AccessClose( vlc_object_t * );
50 static int DemuxOpen ( vlc_object_t * );
51 static void DemuxClose( vlc_object_t * );
54 set_description( _("DVDnav Input") );
55 add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
56 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
57 set_capability( "access", 0 );
58 add_shortcut( "dvdnav" );
59 add_shortcut( "dvdnavsimple" );
60 set_callbacks( AccessOpen, AccessClose );
63 set_description( _("DVDnav Input (demux)") );
64 set_capability( "demux2", 0 );
65 add_shortcut( "dvdnav" );
66 add_shortcut( "dvdnavsimple" );
67 set_callbacks( DemuxOpen, DemuxClose );
71 * - use dvdnav_get_next_cache_block/dvdnav_free_cache_block
73 * - once done the new input API remove the pseudo access
77 /* Shall we use libdvdnav's read ahead cache? */
78 #define DVD_READ_CACHE 1
80 /*****************************************************************************
82 *****************************************************************************/
83 static ssize_t AccessRead( input_thread_t *, byte_t *, size_t ); /* dummy */
84 static char *ParseCL( vlc_object_t *, char *, vlc_bool_t, int *, int *, int *);
102 static int EventThread( vlc_object_t * );
109 ps_track_t tk[PS_TK_COUNT];
111 /* for spu variables */
112 input_thread_t *p_input;
115 event_thread_t *p_ev;
125 vlc_bool_t b_es_out_ok;
129 static int DemuxControl( demux_t *, int, va_list );
130 static int DemuxDemux ( demux_t * );
131 static int DemuxBlock ( demux_t *, uint8_t *pkt, int i_pkt );
135 AR_SQUARE_PICTURE = 1, /* square pixels */
136 AR_3_4_PICTURE = 2, /* 3:4 picture (TV) */
137 AR_16_9_PICTURE = 3, /* 16:9 picture (wide screen) */
138 AR_221_1_PICTURE = 4, /* 2.21:1 picture (movie) */
141 static int MenusCallback( vlc_object_t *, char const *,
142 vlc_value_t, vlc_value_t, void * );
144 static void ESSubtitleUpdate( demux_t * );
145 static void ButtonUpdate( demux_t * );
147 /*****************************************************************************
148 * Open: see if dvdnav can handle the input and if so force dvdnav demux
149 *****************************************************************************
150 * For now it has to be like that (ie open dvdnav twice) but will be fixed
151 * when a demux can work without an access module.
152 *****************************************************************************/
153 static int AccessOpen( vlc_object_t *p_this )
155 input_thread_t *p_input = (input_thread_t *)p_this;
157 int i_title, i_chapter, i_angle;
162 /* We try only if we are forced */
163 if( p_input->psz_demux && *p_input->psz_demux &&
164 strncmp( p_input->psz_demux, "dvdnav", 6 ) )
166 msg_Warn( p_input, "dvdnav access discarded (demuxer forced)" );
170 b_force = p_input->psz_access &&
171 !strncmp( p_input->psz_access, "dvdnav", 6 );
173 psz_name = ParseCL( VLC_OBJECT(p_input), p_input->psz_name, b_force,
174 &i_title, &i_chapter, &i_angle );
181 if( dvdnav_open( &dvdnav, psz_name ) != DVDNAV_STATUS_OK )
183 msg_Warn( p_input, "cannot open dvdnav" );
187 dvdnav_close( dvdnav );
190 /* Fill p_input fields */
191 p_input->pf_read = AccessRead;
192 p_input->pf_set_program = input_SetProgram;
193 p_input->pf_set_area = NULL;
194 p_input->pf_seek = NULL;
196 vlc_mutex_lock( &p_input->stream.stream_lock );
197 p_input->stream.b_pace_control = VLC_TRUE;
198 p_input->stream.b_seekable = VLC_TRUE;
199 p_input->stream.p_selected_area->i_tell = 0;
201 /* Bogus for demux1 compatibility */
202 p_input->stream.p_selected_area->i_size = 10000;
204 p_input->stream.i_method = INPUT_METHOD_DVD;
205 vlc_mutex_unlock( &p_input->stream.stream_lock );
208 /* Force dvdnav demux */
209 if( p_input->psz_access && !strncmp(p_input->psz_access, "dvdnav", 6 ) )
210 p_input->psz_demux = strdup( p_input->psz_access );
212 p_input->psz_demux = strdup( "dvdnav" );
214 /* Update default_pts to a suitable value for udp access */
215 var_Create( p_input, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
216 var_Get( p_input, "dvdnav-caching", &val );
217 p_input->i_pts_delay = val.i_int * 1000;
222 /*****************************************************************************
223 * Close: free unused data structures
224 *****************************************************************************/
225 static void AccessClose( vlc_object_t *p_this )
229 /*****************************************************************************
230 * Read: Should not be called (ie not used by the dvdnav demuxer)
231 *****************************************************************************/
232 static ssize_t AccessRead( input_thread_t *p_input, byte_t *p_buffer,
235 memset( p_buffer, 0, i_len );
239 /*****************************************************************************
240 * ParseCL: parse command line
241 *****************************************************************************/
242 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
243 int *i_title, int *i_chapter, int *i_angle )
245 char *psz_parser, *psz_source, *psz_next;
247 psz_source = strdup( psz_name );
248 if( psz_source == NULL ) return NULL;
254 /* Start with the end, because you could have :
255 * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
256 * (yes, this is kludgy). */
257 for( psz_parser = psz_source + strlen(psz_source) - 1;
258 psz_parser >= psz_source && *psz_parser != '@';
261 if( psz_parser >= psz_source && *psz_parser == '@' )
267 *i_title = (int)strtol( psz_parser, &psz_next, 10 );
270 psz_parser = psz_next + 1;
271 *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
274 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
279 *i_title = *i_title >= 0 ? *i_title : 0;
280 *i_chapter = *i_chapter ? *i_chapter : 1;
281 *i_angle = *i_angle ? *i_angle : 1;
290 psz_source = config_GetPsz( p_this, "dvd" );
291 if( !psz_source ) return NULL;
295 if( psz_source[0] && psz_source[1] == ':' &&
296 psz_source[2] == '\\' && psz_source[3] == '\0' )
298 psz_source[2] = '\0';
302 msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
303 psz_source, *i_title, *i_chapter, *i_angle );
308 /*****************************************************************************
310 *****************************************************************************/
311 static int DemuxOpen( vlc_object_t *p_this )
313 demux_t *p_demux = (demux_t*)p_this;
315 vlc_value_t val, text;
316 int i_title, i_titles, i_chapter, i_chapters, i_angle, i;
319 if( strncmp( p_demux->psz_access, "dvdnav", 6 ) ||
320 strncmp( p_demux->psz_demux, "dvdnav", 6 ) )
322 msg_Warn( p_demux, "dvdnav module discarded" );
326 psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
327 &i_title, &i_chapter, &i_angle );
334 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
335 memset( p_sys, 0, sizeof( demux_sys_t ) );
338 strcmp( p_demux->psz_access, "dvdnavsimple" ) ? VLC_FALSE : VLC_TRUE;
339 if( p_sys->b_simple && i_title < 1 )
345 ps_track_init( p_sys->tk );
346 p_sys->i_aspect = -1;
347 p_sys->b_es_out_ok = VLC_FALSE;
350 if( dvdnav_open( &p_sys->dvdnav, psz_name ) != DVDNAV_STATUS_OK )
352 msg_Warn( p_demux, "cannot open dvdnav" );
358 /* Configure dvdnav */
359 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
362 msg_Warn( p_demux, "cannot set read-a-head flag" );
365 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
368 msg_Warn( p_demux, "cannot set PGC positioning flag" );
371 if( dvdnav_menu_language_select ( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK||
372 dvdnav_audio_language_select( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK||
373 dvdnav_spu_language_select ( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
375 msg_Warn( p_demux, "something failed while setting en language (%s)",
376 dvdnav_err_to_string( p_sys->dvdnav ) );
379 #if 0 // FIXME: Doesn't work with libdvdnav CVS!
380 /* Find out number of titles/chapters */
381 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
382 for( i = 1; i <= i_titles; i++ )
385 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
389 if( dvdnav_title_play( p_sys->dvdnav, i_title ) !=
392 msg_Warn( p_demux, "cannot set title/chapter" );
395 /* fill p_demux field */
396 p_demux->pf_control = DemuxControl;
397 p_demux->pf_demux = DemuxDemux;
399 /* For simple mode (no menus), we're done */
400 if( p_sys->b_simple ) return VLC_SUCCESS;
402 /* Get p_input and create variable */
403 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
404 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
405 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
406 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
407 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
408 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
409 var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
410 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
411 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
413 /* Create a few object variables used for navigation in the interfaces */
414 var_Create( p_sys->p_input, "dvd_menus",
415 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
416 text.psz_string = _("DVD menus");
417 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_SETTEXT, &text, NULL );
418 var_AddCallback( p_sys->p_input, "dvd_menus", MenusCallback, p_demux );
419 val.i_int = DVD_MENU_Escape; text.psz_string = _("Resume");
420 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
421 val.i_int = DVD_MENU_Root; text.psz_string = _("Root");
422 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
423 val.i_int = DVD_MENU_Title; text.psz_string = _("Title");
424 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
425 val.i_int = DVD_MENU_Part; text.psz_string = _("Chapter");
426 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
427 val.i_int = DVD_MENU_Subpicture; text.psz_string = _("Subtitle");
428 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
429 val.i_int = DVD_MENU_Audio; text.psz_string = _("Audio");
430 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
431 val.i_int = DVD_MENU_Angle; text.psz_string = _("Angle");
432 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
434 /* Now create our event thread catcher */
435 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
436 p_sys->p_ev->p_demux = p_demux;
437 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
438 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
443 /*****************************************************************************
445 *****************************************************************************/
446 static void DemuxClose( vlc_object_t *p_this )
448 demux_t *p_demux = (demux_t*)p_this;
449 demux_sys_t *p_sys = p_demux->p_sys;
452 if( !p_sys->b_simple )
454 /* stop the event handler */
455 p_sys->p_ev->b_die = VLC_TRUE;
456 vlc_thread_join( p_sys->p_ev );
457 vlc_object_destroy( p_sys->p_ev );
459 var_Destroy( p_sys->p_input, "highlight-mutex" );
460 var_Destroy( p_sys->p_input, "highlight" );
461 var_Destroy( p_sys->p_input, "x-start" );
462 var_Destroy( p_sys->p_input, "x-end" );
463 var_Destroy( p_sys->p_input, "y-start" );
464 var_Destroy( p_sys->p_input, "y-end" );
465 var_Destroy( p_sys->p_input, "color" );
466 var_Destroy( p_sys->p_input, "contrast" );
468 vlc_object_release( p_sys->p_input );
471 for( i = 0; i < PS_TK_COUNT; i++ )
473 ps_track_t *tk = &p_sys->tk[i];
476 es_format_Clean( &tk->fmt );
477 if( tk->es ) es_out_Del( p_demux->out, tk->es );
481 dvdnav_close( p_sys->dvdnav );
485 /*****************************************************************************
487 *****************************************************************************/
488 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
490 demux_sys_t *p_sys = p_demux->p_sys;
495 case DEMUX_GET_POSITION:
498 pf = (double*) va_arg( args, double* );
499 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) ==
500 DVDNAV_STATUS_OK && len > 0 )
502 *pf = (double)pos / (double)len;
510 case DEMUX_SET_POSITION:
513 f = (double) va_arg( args, double );
514 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) ==
515 DVDNAV_STATUS_OK && len > 0 )
518 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
528 /* TODO implement others */
534 /*****************************************************************************
536 *****************************************************************************/
537 static int DemuxDemux( demux_t *p_demux )
539 demux_sys_t *p_sys = p_demux->p_sys;
541 uint8_t buffer[DVD_VIDEO_LB_LEN];
542 uint8_t *packet = buffer;
546 if( !p_sys->b_es_out_ok )
548 if( !p_sys->b_simple )
550 /* We do ourself the selection/unselection
551 * Problem: bypass --audio-channel and --spu-channel
552 * Solution: call ourself dvdnav_??set_channel -> TODO
554 es_out_Control( p_demux->out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
557 p_sys->b_es_out_ok = VLC_TRUE;
561 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
563 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
565 == DVDNAV_STATUS_ERR )
567 msg_Warn( p_demux, "cannot get next block (%s)",
568 dvdnav_err_to_string( p_sys->dvdnav ) );
574 case DVDNAV_BLOCK_OK: /* mpeg block */
575 DemuxBlock( p_demux, packet, i_len );
578 case DVDNAV_NOP: /* Nothing */
579 msg_Dbg( p_demux, "DVDNAV_NOP" );
582 case DVDNAV_STILL_FRAME:
584 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
585 if( !p_sys->b_simple )
587 vlc_mutex_lock( &p_sys->p_ev->lock );
588 if( !p_sys->p_ev->b_still )
590 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
591 msg_Dbg( p_demux, " - length=0x%x", event->length );
592 p_sys->p_ev->b_still = VLC_TRUE;
593 if( event->length == 0xff )
595 p_sys->p_ev->i_still_end = 0;
599 p_sys->p_ev->i_still_end = (int64_t)event->length *
600 1000000 + mdate() + p_sys->p_input->i_pts_delay;
603 vlc_mutex_unlock( &p_sys->p_ev->lock );
608 dvdnav_still_skip( p_sys->dvdnav );
612 case DVDNAV_SPU_STREAM_CHANGE:
614 dvdnav_spu_stream_change_event_t *event =
615 (dvdnav_spu_stream_change_event_t*)packet;
616 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
617 msg_Dbg( p_demux, " - physical_wide=%d",
618 event->physical_wide );
619 msg_Dbg( p_demux, " - physical_letterbox=%d",
620 event->physical_letterbox);
621 msg_Dbg( p_demux, " - physical_pan_scan=%d",
622 event->physical_pan_scan );
624 if( !p_sys->b_simple )
626 ESSubtitleUpdate( p_demux );
630 case DVDNAV_AUDIO_STREAM_CHANGE:
632 dvdnav_audio_stream_change_event_t *event =
633 (dvdnav_audio_stream_change_event_t*)packet;
634 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
635 msg_Dbg( p_demux, " - physical=%d", event->physical );
639 case DVDNAV_VTS_CHANGE:
642 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
643 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
644 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
645 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
647 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
648 /* TODO check if we alsways have VTS and CELL */
649 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
652 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
654 for( i = 0; i < PS_TK_COUNT; i++ )
656 ps_track_t *tk = &p_sys->tk[i];
659 es_format_Clean( &tk->fmt );
660 if( tk->es ) es_out_Del( p_demux->out, tk->es );
662 tk->b_seen = VLC_FALSE;
665 if( p_sys->b_simple )
669 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
670 &i_part ) == DVDNAV_STATUS_OK )
674 /* we have returned in menu, stop dvd */
675 /* FIXME is it the right way ? */
682 case DVDNAV_CELL_CHANGE:
684 dvdnav_cell_change_event_t *event =
685 (dvdnav_cell_change_event_t*)packet;
686 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
687 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
688 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
689 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
690 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
691 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
692 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
693 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
696 case DVDNAV_NAV_PACKET:
699 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
701 /* A lot of thing to do here :
703 * - fetch pts (for time display)
706 DemuxBlock( p_demux, packet, i_len );
709 case DVDNAV_STOP: /* EOF */
710 msg_Dbg( p_demux, "DVDNAV_STOP" );
713 case DVDNAV_HIGHLIGHT:
715 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
716 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
717 msg_Dbg( p_demux, " - display=%d", event->display );
718 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
719 if( !p_sys->b_simple )
721 ButtonUpdate( p_demux );
726 case DVDNAV_SPU_CLUT_CHANGE:
727 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
728 /* Update color lookup table (16 *uint32_t in packet) */
729 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
732 case DVDNAV_HOP_CHANNEL:
733 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
734 /* We should try to flush all our internal buffer */
738 msg_Dbg( p_demux, "DVDNAV_WAIT" );
740 dvdnav_wait_skip( p_sys->dvdnav );
744 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
749 dvdnav_free_cache_block( p_sys->dvdnav, packet );
754 /*****************************************************************************
756 *****************************************************************************/
757 static void ButtonUpdate( demux_t *p_demux )
759 demux_sys_t *p_sys = p_demux->p_sys;
761 int32_t i_title, i_part;
763 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
765 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
767 vlc_mutex_t *p_mutex = val.p_address;
768 dvdnav_highlight_area_t hl;
771 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
772 != DVDNAV_STATUS_OK )
774 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
778 if( i_button > 0 && i_title == 0 )
780 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
782 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
784 /* I fear it is plain wrong */
785 //val.p_address = (void *)&hl.palette;
786 p_sys->alpha[0] = hl.palette&0x0f;
787 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
788 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
789 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
791 vlc_mutex_lock( p_mutex );
792 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
793 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
794 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
795 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
797 val.p_address = (void *)p_sys->alpha;
798 var_Set( p_sys->p_input, "contrast", val );
800 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
801 vlc_mutex_unlock( p_mutex );
803 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
807 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
811 vlc_mutex_lock( p_mutex );
812 val.b_bool = VLC_FALSE;
813 var_Set( p_sys->p_input, "highlight", val );
814 vlc_mutex_unlock( p_mutex );
819 static void ESNew( demux_t *p_demux, int i_id );
821 static void ESSubtitleUpdate( demux_t *p_demux )
823 demux_sys_t *p_sys = p_demux->p_sys;
824 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
825 int32_t i_title, i_part;
827 ButtonUpdate( p_demux );
829 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
835 if( i_spu >= 0 && i_spu <= 0x1f )
837 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
841 ESNew( p_demux, 0xbd20 + i_spu);
843 /* be sure to unselect it (reset) */
844 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
845 (vlc_bool_t)VLC_FALSE );
848 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
852 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
854 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
857 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
858 (vlc_bool_t)VLC_FALSE );
865 /*****************************************************************************
866 * DemuxBlock: demux a given block
867 *****************************************************************************/
868 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
870 demux_sys_t *p_sys = p_demux->p_sys;
873 while( p < &pkt[i_pkt] )
875 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
883 p_pkt = block_New( p_demux, i_size );
884 memcpy( p_pkt->p_buffer, p, i_size);
886 /* Parse it and send it */
887 switch( 0x100 | p[3] )
895 msg_Warn( p_demux, "received a PSM packet" );
897 else if( p[3] == 0xbb )
899 msg_Warn( p_demux, "received a SYSTEM packet" );
902 block_Release( p_pkt );
909 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
911 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
913 block_Release( p_pkt );
918 int i_id = ps_pkt_id( p_pkt );
921 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
925 ESNew( p_demux, i_id );
927 if( tk->b_seen && tk->es &&
928 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
930 es_out_Send( p_demux->out, tk->es, p_pkt );
934 block_Release( p_pkt );
939 block_Release( p_pkt );
951 /*****************************************************************************
952 * ESNew: register a new elementary stream
953 *****************************************************************************/
954 static void ESNew( demux_t *p_demux, int i_id )
956 demux_sys_t *p_sys = p_demux->p_sys;
957 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
958 vlc_bool_t b_select = VLC_FALSE;
965 if( ps_track_fill( tk, i_id ) )
967 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
972 if( tk->fmt.i_cat == VIDEO_ES )
974 if( p_sys->i_aspect >= 0 )
976 tk->fmt.video.i_aspect = p_sys->i_aspect;
980 else if( tk->fmt.i_cat == AUDIO_ES )
983 /* find the audio number PLEASE find another way */
984 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
988 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
992 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
996 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1002 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1003 if( i_lang != 0xffff )
1005 tk->fmt.psz_language = malloc( 3 );
1006 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1007 tk->fmt.psz_language[1] = (i_lang )&0xff;
1008 tk->fmt.psz_language[2] = 0;
1010 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1012 b_select = VLC_TRUE;
1016 else if( tk->fmt.i_cat == SPU_ES )
1018 int32_t i_title, i_part;
1019 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1020 if( i_lang != 0xffff )
1022 tk->fmt.psz_language = malloc( 3 );
1023 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1024 tk->fmt.psz_language[1] = (i_lang )&0xff;
1025 tk->fmt.psz_language[2] = 0;
1029 tk->fmt.subs.spu.palette[0] = 0xBeef;
1030 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1031 16 * sizeof( uint32_t ) );
1033 /* We select only when we are not in the menu */
1034 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1036 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1038 b_select = VLC_TRUE;
1042 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1045 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1047 tk->b_seen = VLC_TRUE;
1050 /*****************************************************************************
1051 * Event handler code
1052 *****************************************************************************/
1053 static int EventMouse( vlc_object_t *, char const *,
1054 vlc_value_t, vlc_value_t, void * );
1055 static int EventKey ( vlc_object_t *, char const *,
1056 vlc_value_t, vlc_value_t, void * );
1058 static int EventThread( vlc_object_t *p_this )
1060 event_thread_t *p_ev = (event_thread_t*)p_this;
1061 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1062 vlc_object_t *p_vout = NULL;
1064 vlc_mutex_init( p_ev, &p_ev->lock );
1065 p_ev->b_moved = VLC_FALSE;
1066 p_ev->b_clicked = VLC_FALSE;
1067 p_ev->b_key = VLC_FALSE;
1069 p_ev->b_still = VLC_FALSE;
1071 /* catch all key event */
1072 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1075 while( !p_ev->b_die )
1077 vlc_bool_t b_activated = VLC_FALSE;
1082 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1085 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1086 int i, i_action = -1;
1088 vlc_mutex_lock( &p_ev->lock );
1089 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1090 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1092 if( p_hotkeys[i].i_key == valk.i_int )
1094 i_action = p_hotkeys[i].i_action;
1100 case ACTIONID_NAV_LEFT:
1101 dvdnav_left_button_select( p_sys->dvdnav, pci );
1103 case ACTIONID_NAV_RIGHT:
1104 dvdnav_right_button_select( p_sys->dvdnav, pci );
1106 case ACTIONID_NAV_UP:
1107 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1109 case ACTIONID_NAV_DOWN:
1110 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1112 case ACTIONID_NAV_ACTIVATE:
1113 b_activated = VLC_TRUE;
1114 dvdnav_button_activate( p_sys->dvdnav, pci );
1119 p_ev->b_key = VLC_FALSE;
1120 vlc_mutex_unlock( &p_ev->lock );
1124 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1126 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1127 vlc_value_t valx, valy;
1129 vlc_mutex_lock( &p_ev->lock );
1130 var_Get( p_vout, "mouse-x", &valx );
1131 var_Get( p_vout, "mouse-y", &valy );
1135 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1138 if( p_ev->b_clicked )
1140 b_activated = VLC_TRUE;
1141 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1145 p_ev->b_moved = VLC_FALSE;
1146 p_ev->b_clicked = VLC_FALSE;
1147 vlc_mutex_unlock( &p_ev->lock );
1149 if( p_vout && p_vout->b_die )
1151 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1152 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1153 vlc_object_release( p_vout );
1156 if( p_vout == NULL )
1158 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1162 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1163 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1168 vlc_mutex_lock( &p_ev->lock );
1171 if( /* b_activated || // This breaks menus */
1172 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1174 p_ev->b_still = VLC_FALSE;
1175 dvdnav_still_skip( p_sys->dvdnav );
1178 vlc_mutex_unlock( &p_ev->lock );
1184 /* Release callback */
1187 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1188 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1189 vlc_object_release( p_vout );
1191 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1193 vlc_mutex_destroy( &p_ev->lock );
1198 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1199 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1201 event_thread_t *p_ev = p_data;
1202 vlc_mutex_lock( &p_ev->lock );
1203 if( psz_var[6] == 'c' )
1204 p_ev->b_clicked = VLC_TRUE;
1205 else if( psz_var[6] == 'm' )
1206 p_ev->b_moved = VLC_TRUE;
1207 vlc_mutex_unlock( &p_ev->lock );
1212 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1213 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1215 event_thread_t *p_ev = p_data;
1216 vlc_mutex_lock( &p_ev->lock );
1217 p_ev->b_key = VLC_TRUE;
1218 vlc_mutex_unlock( &p_ev->lock );
1223 static int MenusCallback( vlc_object_t *p_this, char const *psz_name,
1224 vlc_value_t oldval, vlc_value_t newval, void *p_arg )
1226 demux_t *p_demux = (demux_t*)p_arg;
1228 /* FIXME, not thread safe */
1229 dvdnav_menu_call( p_demux->p_sys->dvdnav, newval.i_int );