1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: dvdnav.c,v 1.10 2004/01/30 14:27:48 fenrir Exp $
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 miliseconds 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 /*****************************************************************************
79 *****************************************************************************/
80 static ssize_t AccessRead( input_thread_t *, byte_t *, size_t ); /* dummy */
81 static char *ParseCL( vlc_object_t *, char *, vlc_bool_t, int *, int *, int *);
99 static int EventThread( vlc_object_t * );
106 ps_track_t tk[PS_TK_COUNT];
108 /* for spu variables */
109 input_thread_t *p_input;
112 event_thread_t *p_ev;
122 vlc_bool_t b_es_out_ok;
126 static int DemuxControl( demux_t *, int, va_list );
127 static int DemuxDemux ( demux_t * );
128 static int DemuxBlock ( demux_t *, uint8_t *pkt, int i_pkt );
132 AR_SQUARE_PICTURE = 1, /* square pixels */
133 AR_3_4_PICTURE = 2, /* 3:4 picture (TV) */
134 AR_16_9_PICTURE = 3, /* 16:9 picture (wide screen) */
135 AR_221_1_PICTURE = 4, /* 2.21:1 picture (movie) */
138 static int MenusCallback( vlc_object_t *, char const *,
139 vlc_value_t, vlc_value_t, void * );
141 static void ESSubtitleUpdate( demux_t * );
142 static void ButtonUpdate( demux_t * );
144 /*****************************************************************************
145 * Open: see if dvdnav can handle the input and if so force dvdnav demux
146 *****************************************************************************
147 * For now it has to be like that (ie open dvdnav twice) but will be fixed
148 * when a demux can work without an access module.
149 *****************************************************************************/
150 static int AccessOpen( vlc_object_t *p_this )
152 input_thread_t *p_input = (input_thread_t *)p_this;
154 int i_title, i_chapter, i_angle;
159 /* We try only if we are forced */
160 if( p_input->psz_demux && *p_input->psz_demux &&
161 strncmp( p_input->psz_demux, "dvdnav", 6 ) )
163 msg_Warn( p_input, "dvdnav access discarded (demuxer forced)" );
167 b_force = (p_input->psz_access && !strncmp(p_input->psz_access, "dvdnav", 6)) ?
168 VLC_TRUE : VLC_FALSE;
170 psz_name = ParseCL( VLC_OBJECT(p_input), p_input->psz_name, b_force,
171 &i_title, &i_chapter, &i_angle );
178 if( dvdnav_open( &dvdnav, psz_name ) != DVDNAV_STATUS_OK )
180 msg_Warn( p_input, "cannot open dvdnav" );
183 dvdnav_close( dvdnav );
186 /* Fill p_input fields */
187 p_input->pf_read = AccessRead;
188 p_input->pf_set_program = input_SetProgram;
189 p_input->pf_set_area = NULL;
190 p_input->pf_seek = NULL;
192 vlc_mutex_lock( &p_input->stream.stream_lock );
193 p_input->stream.b_pace_control = VLC_TRUE;
194 p_input->stream.b_seekable = VLC_TRUE;
195 p_input->stream.p_selected_area->i_tell = 0;
196 p_input->stream.p_selected_area->i_size = 1000;
197 p_input->stream.i_method = INPUT_METHOD_NETWORK;
198 vlc_mutex_unlock( &p_input->stream.stream_lock );
201 /* force dvdnav plugin */
202 if( p_input->psz_access && !strncmp(p_input->psz_access, "dvdnav", 6 ) )
204 p_input->psz_demux = strdup( p_input->psz_access );
208 p_input->psz_demux = strdup( "dvdnav" );
211 /* Update default_pts to a suitable value for udp access */
212 var_Create( p_input, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
213 var_Get( p_input, "dvdnav-caching", &val );
214 p_input->i_pts_delay = val.i_int * 1000;
219 /*****************************************************************************
220 * Close: free unused data structures
221 *****************************************************************************/
222 static void AccessClose( vlc_object_t *p_this )
226 /*****************************************************************************
227 * Read: Should not be called (ie not used by the dvdnav demuxer)
228 *****************************************************************************/
229 static ssize_t AccessRead( input_thread_t *p_input, byte_t *p_buffer,
232 memset( p_buffer, 0, i_len );
236 /*****************************************************************************
237 * ParseCL: parse command line
238 *****************************************************************************/
239 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
240 int *i_title, int *i_chapter, int *i_angle )
242 char *psz_parser, *psz_source, *psz_next;
244 psz_source = strdup( psz_name );
245 if( psz_source == NULL ) return NULL;
251 /* Start with the end, because you could have :
252 * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
253 * (yes, this is kludgy). */
254 for( psz_parser = psz_source + strlen(psz_source) - 1;
255 psz_parser >= psz_source && *psz_parser != '@';
258 if( psz_parser >= psz_source && *psz_parser == '@' )
264 *i_title = (int)strtol( psz_parser, &psz_next, 10 );
267 psz_parser = psz_next + 1;
268 *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
271 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
276 *i_title = *i_title >= 0 ? *i_title : 0;
277 *i_chapter = *i_chapter ? *i_chapter : 1;
278 *i_angle = *i_angle ? *i_angle : 1;
287 psz_source = config_GetPsz( p_this, "dvd" );
288 if( !psz_source ) return NULL;
291 msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
292 psz_source, *i_title, *i_chapter, *i_angle );
297 /*****************************************************************************
299 *****************************************************************************/
300 static int DemuxOpen( vlc_object_t *p_this )
302 demux_t *p_demux = (demux_t*)p_this;
304 vlc_value_t val, text;
305 int i_title, i_titles, i_chapter, i_chapters, i_angle, i;
308 if( strncmp( p_demux->psz_access, "dvdnav", 6 ) ||
309 strncmp( p_demux->psz_demux, "dvdnav", 6 ) )
311 msg_Warn( p_demux, "dvdnav module discarded" );
315 psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
316 &i_title, &i_chapter, &i_angle );
323 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
324 memset( p_sys, 0, sizeof( demux_sys_t ) );
326 p_sys->b_simple = strcmp( p_demux->psz_access, "dvdnavsimple" ) ? VLC_FALSE : VLC_TRUE;
327 if( p_sys->b_simple && i_title < 1 )
333 ps_track_init( p_sys->tk );
335 p_sys->i_aspect = -1;
337 p_sys->b_es_out_ok = VLC_FALSE;
340 if( dvdnav_open( &p_sys->dvdnav, psz_name ) != DVDNAV_STATUS_OK )
342 msg_Warn( p_demux, "cannot open dvdnav" );
347 /* Configure dvdnav */
348 if( dvdnav_set_readahead_flag( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
350 msg_Warn( p_demux, "cannot set read-a-head flag" );
353 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
356 msg_Warn( p_demux, "cannot set PGC positioning flag" );
359 if( dvdnav_menu_language_select ( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK||
360 dvdnav_audio_language_select( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK||
361 dvdnav_spu_language_select ( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
363 msg_Warn( p_demux, "something failed while setting en language (%s)",
364 dvdnav_err_to_string( p_sys->dvdnav ) );
367 /* Find out number of titles/chapters */
368 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
369 for( i = 1; i <= i_titles; i++ )
372 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
375 if( dvdnav_title_play( p_sys->dvdnav, i_title ) !=
378 msg_Warn( p_demux, "cannot set title/chapter" );
380 if( !p_sys->b_simple )
382 /* Get p_input and create variable */
384 vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_ANYWHERE );
385 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
386 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
387 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
388 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
389 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
390 var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
391 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
392 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
394 /* Create a few object variables used for navigation in the interfaces */
395 var_Create( p_sys->p_input, "dvd_menus",
396 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
397 text.psz_string = _("DVD menus");
398 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_SETTEXT, &text, NULL );
399 var_AddCallback( p_sys->p_input, "dvd_menus", MenusCallback, p_demux );
400 val.i_int = DVD_MENU_Escape; text.psz_string = _("Resume");
401 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
402 val.i_int = DVD_MENU_Root; text.psz_string = _("Root");
403 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
404 val.i_int = DVD_MENU_Title; text.psz_string = _("Title");
405 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
406 val.i_int = DVD_MENU_Part; text.psz_string = _("Chapter");
407 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
408 val.i_int = DVD_MENU_Subpicture; text.psz_string = _("Subtitle");
409 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
410 val.i_int = DVD_MENU_Audio; text.psz_string = _("Audio");
411 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
412 val.i_int = DVD_MENU_Angle; text.psz_string = _("Angle");
413 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
415 /* Now create our event thread catcher */
416 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
417 p_sys->p_ev->p_demux = p_demux;
418 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
419 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
422 /* fill p_demux field */
423 p_demux->pf_control = DemuxControl;
424 p_demux->pf_demux = DemuxDemux;
429 /*****************************************************************************
431 *****************************************************************************/
432 static void DemuxClose( vlc_object_t *p_this )
435 demux_t *p_demux = (demux_t*)p_this;
436 demux_sys_t *p_sys = p_demux->p_sys;
438 if( !p_sys->b_simple )
440 /* stop the event handler */
441 p_sys->p_ev->b_die = VLC_TRUE;
442 vlc_thread_join( p_sys->p_ev );
443 vlc_object_destroy( p_sys->p_ev );
445 var_Destroy( p_sys->p_input, "highlight-mutex" );
446 var_Destroy( p_sys->p_input, "highlight" );
447 var_Destroy( p_sys->p_input, "x-start" );
448 var_Destroy( p_sys->p_input, "x-end" );
449 var_Destroy( p_sys->p_input, "y-start" );
450 var_Destroy( p_sys->p_input, "y-end" );
451 var_Destroy( p_sys->p_input, "color" );
452 var_Destroy( p_sys->p_input, "contrast" );
454 vlc_object_release( p_sys->p_input );
457 dvdnav_close( p_sys->dvdnav );
461 /*****************************************************************************
463 *****************************************************************************/
464 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
466 demux_sys_t *p_sys = p_demux->p_sys;
471 case DEMUX_GET_POSITION:
474 pf = (double*) va_arg( args, double* );
475 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) ==
476 DVDNAV_STATUS_OK && len > 0 )
478 *pf = (double)pos / (double)len;
486 case DEMUX_SET_POSITION:
489 f = (double) va_arg( args, double );
490 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) ==
491 DVDNAV_STATUS_OK && len > 0 )
494 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
503 /* TODO implement others */
509 /*****************************************************************************
511 *****************************************************************************/
512 static int DemuxDemux( demux_t *p_demux )
514 demux_sys_t *p_sys = p_demux->p_sys;
516 uint8_t packet[DVD_VIDEO_LB_LEN];
520 if( !p_sys->b_es_out_ok )
522 if( !p_sys->b_simple )
524 /* We do ourself the selection/unselection
525 * Problem: bypass --audio-channel and --spu-channel
526 * Solution: call ourself dvdnav_??set_channel -> TODO
528 es_out_Control( p_demux->out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
531 p_sys->b_es_out_ok = VLC_TRUE;
534 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len ) ==
537 msg_Warn( p_demux, "cannot get next block (%s)",
538 dvdnav_err_to_string( p_sys->dvdnav ) );
544 case DVDNAV_BLOCK_OK: /* mpeg block */
545 DemuxBlock( p_demux, packet, i_len );
548 case DVDNAV_NOP: /* Nothing */
549 msg_Dbg( p_demux, "DVDNAV_NOP" );
552 case DVDNAV_STILL_FRAME:
554 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
555 if( !p_sys->b_simple )
557 vlc_mutex_lock( &p_sys->p_ev->lock );
558 if( !p_sys->p_ev->b_still )
560 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
561 msg_Dbg( p_demux, " - length=0x%x", event->length );
562 p_sys->p_ev->b_still = VLC_TRUE;
563 if( event->length == 0xff )
565 p_sys->p_ev->i_still_end = 0;
569 p_sys->p_ev->i_still_end = (int64_t)event->length *
570 1000000 + mdate() + p_sys->p_input->i_pts_delay;
573 vlc_mutex_unlock( &p_sys->p_ev->lock );
578 dvdnav_still_skip( p_sys->dvdnav );
582 case DVDNAV_SPU_STREAM_CHANGE:
584 dvdnav_spu_stream_change_event_t *event =
585 (dvdnav_spu_stream_change_event_t*)packet;
586 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
587 msg_Dbg( p_demux, " - physical_wide=%d",
588 event->physical_wide );
589 msg_Dbg( p_demux, " - physical_letterbox=%d",
590 event->physical_letterbox);
591 msg_Dbg( p_demux, " - physical_pan_scan=%d",
592 event->physical_pan_scan );
594 if( !p_sys->b_simple )
596 ESSubtitleUpdate( p_demux );
600 case DVDNAV_AUDIO_STREAM_CHANGE:
602 dvdnav_audio_stream_change_event_t *event =
603 (dvdnav_audio_stream_change_event_t*)packet;
604 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
605 msg_Dbg( p_demux, " - physical=%d", event->physical );
609 case DVDNAV_VTS_CHANGE:
612 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
613 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
614 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
615 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
617 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
618 /* TODO check if we alsways have VTS and CELL */
619 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
622 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
624 for( i = 0; i < PS_TK_COUNT; i++ )
626 ps_track_t *tk = &p_sys->tk[i];
627 if( tk->b_seen && tk->es )
629 es_out_Del( p_demux->out, tk->es );
631 tk->b_seen = VLC_FALSE;
634 if( p_sys->b_simple )
638 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part ) == DVDNAV_STATUS_OK )
642 /* we have returned in menu, stop dvd */
643 /* FIXME is it the right way ? */
650 case DVDNAV_CELL_CHANGE:
652 dvdnav_cell_change_event_t *event =
653 (dvdnav_cell_change_event_t*)packet;
654 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
655 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
656 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
657 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
658 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
659 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
660 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
661 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
664 case DVDNAV_NAV_PACKET:
666 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
667 /* A lot of thing to do here :
669 * - fetch pts (for time display)
672 DemuxBlock( p_demux, packet, i_len );
675 case DVDNAV_STOP: /* EOF */
676 msg_Dbg( p_demux, "DVDNAV_STOP" );
679 case DVDNAV_HIGHLIGHT:
681 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
682 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
683 msg_Dbg( p_demux, " - display=%d", event->display );
684 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
685 if( !p_sys->b_simple )
687 ButtonUpdate( p_demux );
692 case DVDNAV_SPU_CLUT_CHANGE:
693 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
694 /* Update color lookup table (16 *uint32_t in packet) */
695 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
698 case DVDNAV_HOP_CHANNEL:
699 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
700 /* We should try to flush all our internal buffer */
704 msg_Dbg( p_demux, "DVDNAV_WAIT" );
706 dvdnav_wait_skip( p_sys->dvdnav );
710 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
716 /*****************************************************************************
718 *****************************************************************************/
719 static void ButtonUpdate( demux_t *p_demux )
721 demux_sys_t *p_sys = p_demux->p_sys;
723 int32_t i_title, i_part;
725 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
727 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
729 vlc_mutex_t *p_mutex = val.p_address;
730 dvdnav_highlight_area_t hl;
733 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button ) != DVDNAV_STATUS_OK )
735 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
739 if( i_button > 0 && i_title == 0 )
741 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
743 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
745 /* I fear it is plain wrong */
746 //val.p_address = (void *)&hl.palette;
747 p_sys->alpha[0] = hl.palette&0x0f;
748 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
749 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
750 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
752 vlc_mutex_lock( p_mutex );
753 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
754 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
755 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
756 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
758 val.p_address = (void *)p_sys->alpha;
759 var_Set( p_sys->p_input, "contrast", val );
761 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
762 vlc_mutex_unlock( p_mutex );
764 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
768 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d", i_button, i_title );
771 vlc_mutex_lock( p_mutex );
772 val.b_bool = VLC_FALSE; var_Set( p_sys->p_input, "highlight", val );
773 vlc_mutex_unlock( p_mutex );
778 static void ESNew( demux_t *p_demux, int i_id );
780 static void ESSubtitleUpdate( demux_t *p_demux )
782 demux_sys_t *p_sys = p_demux->p_sys;
783 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
784 int32_t i_title, i_part;
786 ButtonUpdate( p_demux );
788 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
789 msg_Err( p_demux, "title=%d part=%d", i_title, i_part );
795 if( i_spu >= 0 && i_spu <= 0x1f )
797 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
801 ESNew( p_demux, 0xbd20 + i_spu);
803 /* be sure to unselect it (reset) */
804 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es, (vlc_bool_t)VLC_FALSE );
807 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
811 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
813 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
816 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es, (vlc_bool_t)VLC_FALSE );
823 /*****************************************************************************
824 * DemuxBlock: demux a given block
825 *****************************************************************************/
826 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
828 demux_sys_t *p_sys = p_demux->p_sys;
831 while( p < &pkt[i_pkt] )
833 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
841 p_pkt = block_New( p_demux, i_size );
842 memcpy( p_pkt->p_buffer, p, i_size);
844 /* Parse it and send it */
845 switch( 0x100 | p[3] )
852 msg_Warn( p_demux, "received a PSM packet" );
854 else if( p[3] == 0xbb )
856 msg_Warn( p_demux, "received a SYSTEM packet" );
858 block_Release( p_pkt );
865 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
867 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
869 block_Release( p_pkt );
874 int i_id = ps_pkt_id( p_pkt );
877 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
881 ESNew( p_demux, i_id );
883 if( tk->b_seen && tk->es &&
884 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
886 es_out_Send( p_demux->out, tk->es, p_pkt );
890 block_Release( p_pkt );
895 block_Release( p_pkt );
907 static void ESNew( demux_t *p_demux, int i_id )
909 demux_sys_t *p_sys = p_demux->p_sys;
910 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
911 vlc_bool_t b_select = VLC_FALSE;
918 if( ps_track_fill( tk, i_id ) )
920 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
925 if( tk->fmt.i_cat == VIDEO_ES )
927 if( p_sys->i_aspect >= 0 )
929 tk->fmt.video.i_aspect = p_sys->i_aspect;
933 else if( tk->fmt.i_cat == AUDIO_ES )
936 /* find the audio number PLEASE find another way */
937 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
941 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
945 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
949 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
955 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
956 if( i_lang != 0xffff )
958 tk->fmt.psz_language = malloc( 3 );
959 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
960 tk->fmt.psz_language[1] = (i_lang )&0xff;
961 tk->fmt.psz_language[2] = 0;
963 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
969 else if( tk->fmt.i_cat == SPU_ES )
971 int32_t i_title, i_part;
972 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
973 if( i_lang != 0xffff )
975 tk->fmt.psz_language = malloc( 3 );
976 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
977 tk->fmt.psz_language[1] = (i_lang )&0xff;
978 tk->fmt.psz_language[2] = 0;
981 tk->fmt.subs.spu.palette[0] = 0xBeef;
982 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut, 16 * sizeof( uint32_t ) );
984 /* We select only when we are not in the menu */
985 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
987 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
993 tk->es = es_out_Add( p_demux->out, &tk->fmt );
996 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
998 tk->b_seen = VLC_TRUE;
1001 /*****************************************************************************
1002 * Event handler code
1003 *****************************************************************************/
1004 static int EventMouse( vlc_object_t *, char const *,
1005 vlc_value_t, vlc_value_t, void * );
1006 static int EventKey ( vlc_object_t *, char const *,
1007 vlc_value_t, vlc_value_t, void * );
1009 static int EventThread( vlc_object_t *p_this )
1011 event_thread_t *p_ev = (event_thread_t*)p_this;
1012 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1013 vlc_object_t *p_vout = NULL;
1015 vlc_mutex_init( p_ev, &p_ev->lock );
1016 p_ev->b_moved = VLC_FALSE;
1017 p_ev->b_clicked = VLC_FALSE;
1018 p_ev->b_key = VLC_FALSE;
1020 p_ev->b_still = VLC_FALSE;
1022 /* catch all key event */
1023 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1026 while( !p_ev->b_die )
1028 vlc_bool_t b_activated = VLC_FALSE;
1033 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1036 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1037 int i, i_action = -1;
1039 vlc_mutex_lock( &p_ev->lock );
1040 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1041 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1043 if( p_hotkeys[i].i_key == valk.i_int )
1045 i_action = p_hotkeys[i].i_action;
1051 case ACTIONID_NAV_LEFT:
1052 dvdnav_left_button_select( p_sys->dvdnav, pci );
1054 case ACTIONID_NAV_RIGHT:
1055 dvdnav_right_button_select( p_sys->dvdnav, pci );
1057 case ACTIONID_NAV_UP:
1058 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1060 case ACTIONID_NAV_DOWN:
1061 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1063 case ACTIONID_NAV_ACTIVATE:
1064 b_activated = VLC_TRUE;
1065 dvdnav_button_activate( p_sys->dvdnav, pci );
1070 p_ev->b_key = VLC_FALSE;
1071 vlc_mutex_unlock( &p_ev->lock );
1075 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1077 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1078 vlc_value_t valx, valy;
1080 vlc_mutex_lock( &p_ev->lock );
1081 var_Get( p_vout, "mouse-x", &valx );
1082 var_Get( p_vout, "mouse-y", &valy );
1086 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1089 if( p_ev->b_clicked )
1091 b_activated = VLC_TRUE;
1092 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1096 p_ev->b_moved = VLC_FALSE;
1097 p_ev->b_clicked = VLC_FALSE;
1098 vlc_mutex_unlock( &p_ev->lock );
1100 if( p_vout && p_vout->b_die )
1102 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1103 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1104 vlc_object_release( p_vout );
1107 if( p_vout == NULL )
1109 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1113 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1114 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1119 vlc_mutex_lock( &p_ev->lock );
1122 if( /* b_activated || // This breaks menus */
1123 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1125 p_ev->b_still = VLC_FALSE;
1126 dvdnav_still_skip( p_sys->dvdnav );
1129 vlc_mutex_unlock( &p_ev->lock );
1135 /* Release callback */
1138 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1139 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1140 vlc_object_release( p_vout );
1142 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1144 vlc_mutex_destroy( &p_ev->lock );
1149 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1150 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1152 event_thread_t *p_ev = p_data;
1153 vlc_mutex_lock( &p_ev->lock );
1154 if( psz_var[6] == 'c' )
1155 p_ev->b_clicked = VLC_TRUE;
1156 else if( psz_var[6] == 'm' )
1157 p_ev->b_moved = VLC_TRUE;
1158 vlc_mutex_unlock( &p_ev->lock );
1163 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1164 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1166 event_thread_t *p_ev = p_data;
1167 vlc_mutex_lock( &p_ev->lock );
1168 p_ev->b_key = VLC_TRUE;
1169 vlc_mutex_unlock( &p_ev->lock );
1174 static int MenusCallback( vlc_object_t *p_this, char const *psz_name,
1175 vlc_value_t oldval, vlc_value_t newval, void *p_arg )
1177 demux_t *p_demux = (demux_t*)p_arg;
1179 /* FIXME, not thread safe */
1180 dvdnav_menu_call( p_demux->p_sys->dvdnav, newval.i_int );