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;
294 msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
295 psz_source, *i_title, *i_chapter, *i_angle );
300 /*****************************************************************************
302 *****************************************************************************/
303 static int DemuxOpen( vlc_object_t *p_this )
305 demux_t *p_demux = (demux_t*)p_this;
307 vlc_value_t val, text;
308 int i_title, i_titles, i_chapter, i_chapters, i_angle, i;
311 if( strncmp( p_demux->psz_access, "dvdnav", 6 ) ||
312 strncmp( p_demux->psz_demux, "dvdnav", 6 ) )
314 msg_Warn( p_demux, "dvdnav module discarded" );
318 psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
319 &i_title, &i_chapter, &i_angle );
326 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
327 memset( p_sys, 0, sizeof( demux_sys_t ) );
330 strcmp( p_demux->psz_access, "dvdnavsimple" ) ? VLC_FALSE : VLC_TRUE;
331 if( p_sys->b_simple && i_title < 1 )
337 ps_track_init( p_sys->tk );
338 p_sys->i_aspect = -1;
339 p_sys->b_es_out_ok = VLC_FALSE;
342 if( dvdnav_open( &p_sys->dvdnav, psz_name ) != DVDNAV_STATUS_OK )
344 msg_Warn( p_demux, "cannot open dvdnav" );
350 /* Configure dvdnav */
351 if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
354 msg_Warn( p_demux, "cannot set read-a-head flag" );
357 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
360 msg_Warn( p_demux, "cannot set PGC positioning flag" );
363 if( dvdnav_menu_language_select ( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK||
364 dvdnav_audio_language_select( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK||
365 dvdnav_spu_language_select ( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
367 msg_Warn( p_demux, "something failed while setting en language (%s)",
368 dvdnav_err_to_string( p_sys->dvdnav ) );
371 #if 0 // FIXME: Doesn't work with libdvdnav CVS!
372 /* Find out number of titles/chapters */
373 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
374 for( i = 1; i <= i_titles; i++ )
377 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
381 if( dvdnav_title_play( p_sys->dvdnav, i_title ) !=
384 msg_Warn( p_demux, "cannot set title/chapter" );
387 /* fill p_demux field */
388 p_demux->pf_control = DemuxControl;
389 p_demux->pf_demux = DemuxDemux;
391 /* For simple mode (no menus), we're done */
392 if( p_sys->b_simple ) return VLC_SUCCESS;
394 /* Get p_input and create variable */
395 p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
396 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
397 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
398 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
399 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
400 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
401 var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
402 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
403 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
405 /* Create a few object variables used for navigation in the interfaces */
406 var_Create( p_sys->p_input, "dvd_menus",
407 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
408 text.psz_string = _("DVD menus");
409 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_SETTEXT, &text, NULL );
410 var_AddCallback( p_sys->p_input, "dvd_menus", MenusCallback, p_demux );
411 val.i_int = DVD_MENU_Escape; text.psz_string = _("Resume");
412 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
413 val.i_int = DVD_MENU_Root; text.psz_string = _("Root");
414 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
415 val.i_int = DVD_MENU_Title; text.psz_string = _("Title");
416 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
417 val.i_int = DVD_MENU_Part; text.psz_string = _("Chapter");
418 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
419 val.i_int = DVD_MENU_Subpicture; text.psz_string = _("Subtitle");
420 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
421 val.i_int = DVD_MENU_Audio; text.psz_string = _("Audio");
422 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
423 val.i_int = DVD_MENU_Angle; text.psz_string = _("Angle");
424 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
426 /* Now create our event thread catcher */
427 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
428 p_sys->p_ev->p_demux = p_demux;
429 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
430 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
435 /*****************************************************************************
437 *****************************************************************************/
438 static void DemuxClose( vlc_object_t *p_this )
440 demux_t *p_demux = (demux_t*)p_this;
441 demux_sys_t *p_sys = p_demux->p_sys;
444 if( !p_sys->b_simple )
446 /* stop the event handler */
447 p_sys->p_ev->b_die = VLC_TRUE;
448 vlc_thread_join( p_sys->p_ev );
449 vlc_object_destroy( p_sys->p_ev );
451 var_Destroy( p_sys->p_input, "highlight-mutex" );
452 var_Destroy( p_sys->p_input, "highlight" );
453 var_Destroy( p_sys->p_input, "x-start" );
454 var_Destroy( p_sys->p_input, "x-end" );
455 var_Destroy( p_sys->p_input, "y-start" );
456 var_Destroy( p_sys->p_input, "y-end" );
457 var_Destroy( p_sys->p_input, "color" );
458 var_Destroy( p_sys->p_input, "contrast" );
460 vlc_object_release( p_sys->p_input );
463 for( i = 0; i < PS_TK_COUNT; i++ )
465 ps_track_t *tk = &p_sys->tk[i];
468 es_format_Clean( &tk->fmt );
469 if( tk->es ) es_out_Del( p_demux->out, tk->es );
473 dvdnav_close( p_sys->dvdnav );
477 /*****************************************************************************
479 *****************************************************************************/
480 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
482 demux_sys_t *p_sys = p_demux->p_sys;
487 case DEMUX_GET_POSITION:
490 pf = (double*) va_arg( args, double* );
491 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) ==
492 DVDNAV_STATUS_OK && len > 0 )
494 *pf = (double)pos / (double)len;
502 case DEMUX_SET_POSITION:
505 f = (double) va_arg( args, double );
506 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) ==
507 DVDNAV_STATUS_OK && len > 0 )
510 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
520 /* TODO implement others */
526 /*****************************************************************************
528 *****************************************************************************/
529 static int DemuxDemux( demux_t *p_demux )
531 demux_sys_t *p_sys = p_demux->p_sys;
533 uint8_t buffer[DVD_VIDEO_LB_LEN];
534 uint8_t *packet = buffer;
538 if( !p_sys->b_es_out_ok )
540 if( !p_sys->b_simple )
542 /* We do ourself the selection/unselection
543 * Problem: bypass --audio-channel and --spu-channel
544 * Solution: call ourself dvdnav_??set_channel -> TODO
546 es_out_Control( p_demux->out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
549 p_sys->b_es_out_ok = VLC_TRUE;
553 if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
555 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
557 == DVDNAV_STATUS_ERR )
559 msg_Warn( p_demux, "cannot get next block (%s)",
560 dvdnav_err_to_string( p_sys->dvdnav ) );
566 case DVDNAV_BLOCK_OK: /* mpeg block */
567 DemuxBlock( p_demux, packet, i_len );
570 case DVDNAV_NOP: /* Nothing */
571 msg_Dbg( p_demux, "DVDNAV_NOP" );
574 case DVDNAV_STILL_FRAME:
576 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
577 if( !p_sys->b_simple )
579 vlc_mutex_lock( &p_sys->p_ev->lock );
580 if( !p_sys->p_ev->b_still )
582 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
583 msg_Dbg( p_demux, " - length=0x%x", event->length );
584 p_sys->p_ev->b_still = VLC_TRUE;
585 if( event->length == 0xff )
587 p_sys->p_ev->i_still_end = 0;
591 p_sys->p_ev->i_still_end = (int64_t)event->length *
592 1000000 + mdate() + p_sys->p_input->i_pts_delay;
595 vlc_mutex_unlock( &p_sys->p_ev->lock );
600 dvdnav_still_skip( p_sys->dvdnav );
604 case DVDNAV_SPU_STREAM_CHANGE:
606 dvdnav_spu_stream_change_event_t *event =
607 (dvdnav_spu_stream_change_event_t*)packet;
608 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
609 msg_Dbg( p_demux, " - physical_wide=%d",
610 event->physical_wide );
611 msg_Dbg( p_demux, " - physical_letterbox=%d",
612 event->physical_letterbox);
613 msg_Dbg( p_demux, " - physical_pan_scan=%d",
614 event->physical_pan_scan );
616 if( !p_sys->b_simple )
618 ESSubtitleUpdate( p_demux );
622 case DVDNAV_AUDIO_STREAM_CHANGE:
624 dvdnav_audio_stream_change_event_t *event =
625 (dvdnav_audio_stream_change_event_t*)packet;
626 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
627 msg_Dbg( p_demux, " - physical=%d", event->physical );
631 case DVDNAV_VTS_CHANGE:
634 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
635 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
636 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
637 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
639 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
640 /* TODO check if we alsways have VTS and CELL */
641 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
644 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
646 for( i = 0; i < PS_TK_COUNT; i++ )
648 ps_track_t *tk = &p_sys->tk[i];
651 es_format_Clean( &tk->fmt );
652 if( tk->es ) es_out_Del( p_demux->out, tk->es );
654 tk->b_seen = VLC_FALSE;
657 if( p_sys->b_simple )
661 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
662 &i_part ) == DVDNAV_STATUS_OK )
666 /* we have returned in menu, stop dvd */
667 /* FIXME is it the right way ? */
674 case DVDNAV_CELL_CHANGE:
676 dvdnav_cell_change_event_t *event =
677 (dvdnav_cell_change_event_t*)packet;
678 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
679 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
680 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
681 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
682 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
683 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
684 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
685 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
688 case DVDNAV_NAV_PACKET:
691 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
693 /* A lot of thing to do here :
695 * - fetch pts (for time display)
698 DemuxBlock( p_demux, packet, i_len );
701 case DVDNAV_STOP: /* EOF */
702 msg_Dbg( p_demux, "DVDNAV_STOP" );
705 case DVDNAV_HIGHLIGHT:
707 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
708 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
709 msg_Dbg( p_demux, " - display=%d", event->display );
710 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
711 if( !p_sys->b_simple )
713 ButtonUpdate( p_demux );
718 case DVDNAV_SPU_CLUT_CHANGE:
719 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
720 /* Update color lookup table (16 *uint32_t in packet) */
721 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
724 case DVDNAV_HOP_CHANNEL:
725 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
726 /* We should try to flush all our internal buffer */
730 msg_Dbg( p_demux, "DVDNAV_WAIT" );
732 dvdnav_wait_skip( p_sys->dvdnav );
736 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
741 dvdnav_free_cache_block( p_sys->dvdnav, packet );
746 /*****************************************************************************
748 *****************************************************************************/
749 static void ButtonUpdate( demux_t *p_demux )
751 demux_sys_t *p_sys = p_demux->p_sys;
753 int32_t i_title, i_part;
755 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
757 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
759 vlc_mutex_t *p_mutex = val.p_address;
760 dvdnav_highlight_area_t hl;
763 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
764 != DVDNAV_STATUS_OK )
766 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
770 if( i_button > 0 && i_title == 0 )
772 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
774 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
776 /* I fear it is plain wrong */
777 //val.p_address = (void *)&hl.palette;
778 p_sys->alpha[0] = hl.palette&0x0f;
779 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
780 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
781 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
783 vlc_mutex_lock( p_mutex );
784 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
785 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
786 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
787 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
789 val.p_address = (void *)p_sys->alpha;
790 var_Set( p_sys->p_input, "contrast", val );
792 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
793 vlc_mutex_unlock( p_mutex );
795 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
799 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
803 vlc_mutex_lock( p_mutex );
804 val.b_bool = VLC_FALSE;
805 var_Set( p_sys->p_input, "highlight", val );
806 vlc_mutex_unlock( p_mutex );
811 static void ESNew( demux_t *p_demux, int i_id );
813 static void ESSubtitleUpdate( demux_t *p_demux )
815 demux_sys_t *p_sys = p_demux->p_sys;
816 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
817 int32_t i_title, i_part;
819 ButtonUpdate( p_demux );
821 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
827 if( i_spu >= 0 && i_spu <= 0x1f )
829 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
833 ESNew( p_demux, 0xbd20 + i_spu);
835 /* be sure to unselect it (reset) */
836 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
837 (vlc_bool_t)VLC_FALSE );
840 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
844 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
846 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
849 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
850 (vlc_bool_t)VLC_FALSE );
857 /*****************************************************************************
858 * DemuxBlock: demux a given block
859 *****************************************************************************/
860 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
862 demux_sys_t *p_sys = p_demux->p_sys;
865 while( p < &pkt[i_pkt] )
867 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
875 p_pkt = block_New( p_demux, i_size );
876 memcpy( p_pkt->p_buffer, p, i_size);
878 /* Parse it and send it */
879 switch( 0x100 | p[3] )
887 msg_Warn( p_demux, "received a PSM packet" );
889 else if( p[3] == 0xbb )
891 msg_Warn( p_demux, "received a SYSTEM packet" );
894 block_Release( p_pkt );
901 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
903 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
905 block_Release( p_pkt );
910 int i_id = ps_pkt_id( p_pkt );
913 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
917 ESNew( p_demux, i_id );
919 if( tk->b_seen && tk->es &&
920 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
922 es_out_Send( p_demux->out, tk->es, p_pkt );
926 block_Release( p_pkt );
931 block_Release( p_pkt );
943 /*****************************************************************************
944 * ESNew: register a new elementary stream
945 *****************************************************************************/
946 static void ESNew( demux_t *p_demux, int i_id )
948 demux_sys_t *p_sys = p_demux->p_sys;
949 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
950 vlc_bool_t b_select = VLC_FALSE;
957 if( ps_track_fill( tk, i_id ) )
959 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
964 if( tk->fmt.i_cat == VIDEO_ES )
966 if( p_sys->i_aspect >= 0 )
968 tk->fmt.video.i_aspect = p_sys->i_aspect;
972 else if( tk->fmt.i_cat == AUDIO_ES )
975 /* find the audio number PLEASE find another way */
976 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
980 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
984 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
988 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
994 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
995 if( i_lang != 0xffff )
997 tk->fmt.psz_language = malloc( 3 );
998 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
999 tk->fmt.psz_language[1] = (i_lang )&0xff;
1000 tk->fmt.psz_language[2] = 0;
1002 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1004 b_select = VLC_TRUE;
1008 else if( tk->fmt.i_cat == SPU_ES )
1010 int32_t i_title, i_part;
1011 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1012 if( i_lang != 0xffff )
1014 tk->fmt.psz_language = malloc( 3 );
1015 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1016 tk->fmt.psz_language[1] = (i_lang )&0xff;
1017 tk->fmt.psz_language[2] = 0;
1021 tk->fmt.subs.spu.palette[0] = 0xBeef;
1022 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1023 16 * sizeof( uint32_t ) );
1025 /* We select only when we are not in the menu */
1026 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1028 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1030 b_select = VLC_TRUE;
1034 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1037 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1039 tk->b_seen = VLC_TRUE;
1042 /*****************************************************************************
1043 * Event handler code
1044 *****************************************************************************/
1045 static int EventMouse( vlc_object_t *, char const *,
1046 vlc_value_t, vlc_value_t, void * );
1047 static int EventKey ( vlc_object_t *, char const *,
1048 vlc_value_t, vlc_value_t, void * );
1050 static int EventThread( vlc_object_t *p_this )
1052 event_thread_t *p_ev = (event_thread_t*)p_this;
1053 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1054 vlc_object_t *p_vout = NULL;
1056 vlc_mutex_init( p_ev, &p_ev->lock );
1057 p_ev->b_moved = VLC_FALSE;
1058 p_ev->b_clicked = VLC_FALSE;
1059 p_ev->b_key = VLC_FALSE;
1061 p_ev->b_still = VLC_FALSE;
1063 /* catch all key event */
1064 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1067 while( !p_ev->b_die )
1069 vlc_bool_t b_activated = VLC_FALSE;
1074 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1077 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1078 int i, i_action = -1;
1080 vlc_mutex_lock( &p_ev->lock );
1081 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1082 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1084 if( p_hotkeys[i].i_key == valk.i_int )
1086 i_action = p_hotkeys[i].i_action;
1092 case ACTIONID_NAV_LEFT:
1093 dvdnav_left_button_select( p_sys->dvdnav, pci );
1095 case ACTIONID_NAV_RIGHT:
1096 dvdnav_right_button_select( p_sys->dvdnav, pci );
1098 case ACTIONID_NAV_UP:
1099 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1101 case ACTIONID_NAV_DOWN:
1102 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1104 case ACTIONID_NAV_ACTIVATE:
1105 b_activated = VLC_TRUE;
1106 dvdnav_button_activate( p_sys->dvdnav, pci );
1111 p_ev->b_key = VLC_FALSE;
1112 vlc_mutex_unlock( &p_ev->lock );
1116 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1118 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1119 vlc_value_t valx, valy;
1121 vlc_mutex_lock( &p_ev->lock );
1122 var_Get( p_vout, "mouse-x", &valx );
1123 var_Get( p_vout, "mouse-y", &valy );
1127 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1130 if( p_ev->b_clicked )
1132 b_activated = VLC_TRUE;
1133 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1137 p_ev->b_moved = VLC_FALSE;
1138 p_ev->b_clicked = VLC_FALSE;
1139 vlc_mutex_unlock( &p_ev->lock );
1141 if( p_vout && p_vout->b_die )
1143 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1144 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1145 vlc_object_release( p_vout );
1148 if( p_vout == NULL )
1150 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1154 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1155 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1160 vlc_mutex_lock( &p_ev->lock );
1163 if( /* b_activated || // This breaks menus */
1164 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1166 p_ev->b_still = VLC_FALSE;
1167 dvdnav_still_skip( p_sys->dvdnav );
1170 vlc_mutex_unlock( &p_ev->lock );
1176 /* Release callback */
1179 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1180 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1181 vlc_object_release( p_vout );
1183 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1185 vlc_mutex_destroy( &p_ev->lock );
1190 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1191 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1193 event_thread_t *p_ev = p_data;
1194 vlc_mutex_lock( &p_ev->lock );
1195 if( psz_var[6] == 'c' )
1196 p_ev->b_clicked = VLC_TRUE;
1197 else if( psz_var[6] == 'm' )
1198 p_ev->b_moved = VLC_TRUE;
1199 vlc_mutex_unlock( &p_ev->lock );
1204 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1205 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1207 event_thread_t *p_ev = p_data;
1208 vlc_mutex_lock( &p_ev->lock );
1209 p_ev->b_key = VLC_TRUE;
1210 vlc_mutex_unlock( &p_ev->lock );
1215 static int MenusCallback( vlc_object_t *p_this, char const *psz_name,
1216 vlc_value_t oldval, vlc_value_t newval, void *p_arg )
1218 demux_t *p_demux = (demux_t*)p_arg;
1220 /* FIXME, not thread safe */
1221 dvdnav_menu_call( p_demux->p_sys->dvdnav, newval.i_int );