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 /*****************************************************************************
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 &&
168 !strncmp( p_input->psz_access, "dvdnav", 6 );
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" );
184 dvdnav_close( dvdnav );
187 /* Fill p_input fields */
188 p_input->pf_read = AccessRead;
189 p_input->pf_set_program = input_SetProgram;
190 p_input->pf_set_area = NULL;
191 p_input->pf_seek = NULL;
193 vlc_mutex_lock( &p_input->stream.stream_lock );
194 p_input->stream.b_pace_control = VLC_TRUE;
195 p_input->stream.b_seekable = VLC_TRUE;
196 p_input->stream.p_selected_area->i_tell = 0;
197 p_input->stream.p_selected_area->i_size = 0;
198 p_input->stream.i_method = INPUT_METHOD_DVD;
199 vlc_mutex_unlock( &p_input->stream.stream_lock );
202 /* Force dvdnav demux */
203 if( p_input->psz_access && !strncmp(p_input->psz_access, "dvdnav", 6 ) )
204 p_input->psz_demux = strdup( p_input->psz_access );
206 p_input->psz_demux = strdup( "dvdnav" );
208 /* Update default_pts to a suitable value for udp access */
209 var_Create( p_input, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
210 var_Get( p_input, "dvdnav-caching", &val );
211 p_input->i_pts_delay = val.i_int * 1000;
216 /*****************************************************************************
217 * Close: free unused data structures
218 *****************************************************************************/
219 static void AccessClose( vlc_object_t *p_this )
223 /*****************************************************************************
224 * Read: Should not be called (ie not used by the dvdnav demuxer)
225 *****************************************************************************/
226 static ssize_t AccessRead( input_thread_t *p_input, byte_t *p_buffer,
229 memset( p_buffer, 0, i_len );
233 /*****************************************************************************
234 * ParseCL: parse command line
235 *****************************************************************************/
236 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
237 int *i_title, int *i_chapter, int *i_angle )
239 char *psz_parser, *psz_source, *psz_next;
241 psz_source = strdup( psz_name );
242 if( psz_source == NULL ) return NULL;
248 /* Start with the end, because you could have :
249 * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
250 * (yes, this is kludgy). */
251 for( psz_parser = psz_source + strlen(psz_source) - 1;
252 psz_parser >= psz_source && *psz_parser != '@';
255 if( psz_parser >= psz_source && *psz_parser == '@' )
261 *i_title = (int)strtol( psz_parser, &psz_next, 10 );
264 psz_parser = psz_next + 1;
265 *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
268 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
273 *i_title = *i_title >= 0 ? *i_title : 0;
274 *i_chapter = *i_chapter ? *i_chapter : 1;
275 *i_angle = *i_angle ? *i_angle : 1;
284 psz_source = config_GetPsz( p_this, "dvd" );
285 if( !psz_source ) return NULL;
288 msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
289 psz_source, *i_title, *i_chapter, *i_angle );
294 /*****************************************************************************
296 *****************************************************************************/
297 static int DemuxOpen( vlc_object_t *p_this )
299 demux_t *p_demux = (demux_t*)p_this;
301 vlc_value_t val, text;
302 int i_title, i_titles, i_chapter, i_chapters, i_angle, i;
305 if( strncmp( p_demux->psz_access, "dvdnav", 6 ) ||
306 strncmp( p_demux->psz_demux, "dvdnav", 6 ) )
308 msg_Warn( p_demux, "dvdnav module discarded" );
312 psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
313 &i_title, &i_chapter, &i_angle );
320 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
321 memset( p_sys, 0, sizeof( demux_sys_t ) );
324 strcmp( p_demux->psz_access, "dvdnavsimple" ) ? VLC_FALSE : VLC_TRUE;
325 if( p_sys->b_simple && i_title < 1 )
331 ps_track_init( p_sys->tk );
332 p_sys->i_aspect = -1;
333 p_sys->b_es_out_ok = VLC_FALSE;
336 if( dvdnav_open( &p_sys->dvdnav, psz_name ) != DVDNAV_STATUS_OK )
338 msg_Warn( p_demux, "cannot open dvdnav" );
344 /* Configure dvdnav */
345 if( dvdnav_set_readahead_flag( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
347 msg_Warn( p_demux, "cannot set read-a-head flag" );
350 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
353 msg_Warn( p_demux, "cannot set PGC positioning flag" );
356 if( dvdnav_menu_language_select ( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK||
357 dvdnav_audio_language_select( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK||
358 dvdnav_spu_language_select ( p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
360 msg_Warn( p_demux, "something failed while setting en language (%s)",
361 dvdnav_err_to_string( p_sys->dvdnav ) );
364 /* Find out number of titles/chapters */
365 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
366 for( i = 1; i <= i_titles; i++ )
369 dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
372 if( dvdnav_title_play( p_sys->dvdnav, i_title ) !=
375 msg_Warn( p_demux, "cannot set title/chapter" );
377 if( !p_sys->b_simple )
379 /* Get p_input and create variable */
381 vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_ANYWHERE );
382 var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
383 var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
384 var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
385 var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
386 var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
387 var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
388 var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
389 var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
391 /* Create a few object variables used for navigation in the interfaces */
392 var_Create( p_sys->p_input, "dvd_menus",
393 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
394 text.psz_string = _("DVD menus");
395 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_SETTEXT, &text, NULL );
396 var_AddCallback( p_sys->p_input, "dvd_menus", MenusCallback, p_demux );
397 val.i_int = DVD_MENU_Escape; text.psz_string = _("Resume");
398 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
399 val.i_int = DVD_MENU_Root; text.psz_string = _("Root");
400 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
401 val.i_int = DVD_MENU_Title; text.psz_string = _("Title");
402 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
403 val.i_int = DVD_MENU_Part; text.psz_string = _("Chapter");
404 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
405 val.i_int = DVD_MENU_Subpicture; text.psz_string = _("Subtitle");
406 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
407 val.i_int = DVD_MENU_Audio; text.psz_string = _("Audio");
408 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
409 val.i_int = DVD_MENU_Angle; text.psz_string = _("Angle");
410 var_Change( p_sys->p_input, "dvd_menus", VLC_VAR_ADDCHOICE, &val, &text );
412 /* Now create our event thread catcher */
413 p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
414 p_sys->p_ev->p_demux = p_demux;
415 vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
416 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
419 /* fill p_demux field */
420 p_demux->pf_control = DemuxControl;
421 p_demux->pf_demux = DemuxDemux;
426 /*****************************************************************************
428 *****************************************************************************/
429 static void DemuxClose( vlc_object_t *p_this )
431 demux_t *p_demux = (demux_t*)p_this;
432 demux_sys_t *p_sys = p_demux->p_sys;
434 if( !p_sys->b_simple )
436 /* stop the event handler */
437 p_sys->p_ev->b_die = VLC_TRUE;
438 vlc_thread_join( p_sys->p_ev );
439 vlc_object_destroy( p_sys->p_ev );
441 var_Destroy( p_sys->p_input, "highlight-mutex" );
442 var_Destroy( p_sys->p_input, "highlight" );
443 var_Destroy( p_sys->p_input, "x-start" );
444 var_Destroy( p_sys->p_input, "x-end" );
445 var_Destroy( p_sys->p_input, "y-start" );
446 var_Destroy( p_sys->p_input, "y-end" );
447 var_Destroy( p_sys->p_input, "color" );
448 var_Destroy( p_sys->p_input, "contrast" );
450 vlc_object_release( p_sys->p_input );
453 dvdnav_close( p_sys->dvdnav );
457 /*****************************************************************************
459 *****************************************************************************/
460 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
462 demux_sys_t *p_sys = p_demux->p_sys;
467 case DEMUX_GET_POSITION:
470 pf = (double*) va_arg( args, double* );
471 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) ==
472 DVDNAV_STATUS_OK && len > 0 )
474 *pf = (double)pos / (double)len;
482 case DEMUX_SET_POSITION:
485 f = (double) va_arg( args, double );
486 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) ==
487 DVDNAV_STATUS_OK && len > 0 )
490 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
499 /* TODO implement others */
505 /*****************************************************************************
507 *****************************************************************************/
508 static int DemuxDemux( demux_t *p_demux )
510 demux_sys_t *p_sys = p_demux->p_sys;
512 uint8_t packet[DVD_VIDEO_LB_LEN];
516 if( !p_sys->b_es_out_ok )
518 if( !p_sys->b_simple )
520 /* We do ourself the selection/unselection
521 * Problem: bypass --audio-channel and --spu-channel
522 * Solution: call ourself dvdnav_??set_channel -> TODO
524 es_out_Control( p_demux->out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
527 p_sys->b_es_out_ok = VLC_TRUE;
530 if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len ) ==
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 if( !p_sys->b_simple )
553 vlc_mutex_lock( &p_sys->p_ev->lock );
554 if( !p_sys->p_ev->b_still )
556 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
557 msg_Dbg( p_demux, " - length=0x%x", event->length );
558 p_sys->p_ev->b_still = VLC_TRUE;
559 if( event->length == 0xff )
561 p_sys->p_ev->i_still_end = 0;
565 p_sys->p_ev->i_still_end = (int64_t)event->length *
566 1000000 + mdate() + p_sys->p_input->i_pts_delay;
569 vlc_mutex_unlock( &p_sys->p_ev->lock );
574 dvdnav_still_skip( p_sys->dvdnav );
578 case DVDNAV_SPU_STREAM_CHANGE:
580 dvdnav_spu_stream_change_event_t *event =
581 (dvdnav_spu_stream_change_event_t*)packet;
582 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
583 msg_Dbg( p_demux, " - physical_wide=%d",
584 event->physical_wide );
585 msg_Dbg( p_demux, " - physical_letterbox=%d",
586 event->physical_letterbox);
587 msg_Dbg( p_demux, " - physical_pan_scan=%d",
588 event->physical_pan_scan );
590 if( !p_sys->b_simple )
592 ESSubtitleUpdate( p_demux );
596 case DVDNAV_AUDIO_STREAM_CHANGE:
598 dvdnav_audio_stream_change_event_t *event =
599 (dvdnav_audio_stream_change_event_t*)packet;
600 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
601 msg_Dbg( p_demux, " - physical=%d", event->physical );
605 case DVDNAV_VTS_CHANGE:
608 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
609 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
610 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
611 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
613 /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
614 /* TODO check if we alsways have VTS and CELL */
615 p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
618 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
620 for( i = 0; i < PS_TK_COUNT; i++ )
622 ps_track_t *tk = &p_sys->tk[i];
623 if( tk->b_seen && tk->es )
625 es_out_Del( p_demux->out, tk->es );
627 tk->b_seen = VLC_FALSE;
630 if( p_sys->b_simple )
634 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part ) == DVDNAV_STATUS_OK )
638 /* we have returned in menu, stop dvd */
639 /* FIXME is it the right way ? */
646 case DVDNAV_CELL_CHANGE:
648 dvdnav_cell_change_event_t *event =
649 (dvdnav_cell_change_event_t*)packet;
650 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
651 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
652 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
653 msg_Dbg( p_demux, " - cell_length=%lld", event->cell_length );
654 msg_Dbg( p_demux, " - pg_length=%lld", event->pg_length );
655 msg_Dbg( p_demux, " - pgc_length=%lld", event->pgc_length );
656 msg_Dbg( p_demux, " - cell_start=%lld", event->cell_start );
657 msg_Dbg( p_demux, " - pg_start=%lld", event->pg_start );
660 case DVDNAV_NAV_PACKET:
662 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
663 /* A lot of thing to do here :
665 * - fetch pts (for time display)
668 DemuxBlock( p_demux, packet, i_len );
671 case DVDNAV_STOP: /* EOF */
672 msg_Dbg( p_demux, "DVDNAV_STOP" );
675 case DVDNAV_HIGHLIGHT:
677 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
678 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
679 msg_Dbg( p_demux, " - display=%d", event->display );
680 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
681 if( !p_sys->b_simple )
683 ButtonUpdate( p_demux );
688 case DVDNAV_SPU_CLUT_CHANGE:
689 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
690 /* Update color lookup table (16 *uint32_t in packet) */
691 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
694 case DVDNAV_HOP_CHANNEL:
695 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
696 /* We should try to flush all our internal buffer */
700 msg_Dbg( p_demux, "DVDNAV_WAIT" );
702 dvdnav_wait_skip( p_sys->dvdnav );
706 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
712 /*****************************************************************************
714 *****************************************************************************/
715 static void ButtonUpdate( demux_t *p_demux )
717 demux_sys_t *p_sys = p_demux->p_sys;
719 int32_t i_title, i_part;
721 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
723 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
725 vlc_mutex_t *p_mutex = val.p_address;
726 dvdnav_highlight_area_t hl;
729 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button ) != DVDNAV_STATUS_OK )
731 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
735 if( i_button > 0 && i_title == 0 )
737 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
739 dvdnav_get_highlight_area( pci, i_button, 1, &hl );
741 /* I fear it is plain wrong */
742 //val.p_address = (void *)&hl.palette;
743 p_sys->alpha[0] = hl.palette&0x0f;
744 p_sys->alpha[1] = (hl.palette>>4)&0x0f;
745 p_sys->alpha[2] = (hl.palette>>8)&0x0f;
746 p_sys->alpha[3] = (hl.palette>>12)&0x0f;
748 vlc_mutex_lock( p_mutex );
749 val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
750 val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
751 val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
752 val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
754 val.p_address = (void *)p_sys->alpha;
755 var_Set( p_sys->p_input, "contrast", val );
757 val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
758 vlc_mutex_unlock( p_mutex );
760 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
764 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d", i_button, i_title );
767 vlc_mutex_lock( p_mutex );
768 val.b_bool = VLC_FALSE; var_Set( p_sys->p_input, "highlight", val );
769 vlc_mutex_unlock( p_mutex );
774 static void ESNew( demux_t *p_demux, int i_id );
776 static void ESSubtitleUpdate( demux_t *p_demux )
778 demux_sys_t *p_sys = p_demux->p_sys;
779 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
780 int32_t i_title, i_part;
782 ButtonUpdate( p_demux );
784 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
790 if( i_spu >= 0 && i_spu <= 0x1f )
792 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
796 ESNew( p_demux, 0xbd20 + i_spu);
798 /* be sure to unselect it (reset) */
799 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
800 (vlc_bool_t)VLC_FALSE );
803 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
807 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
809 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
812 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
813 (vlc_bool_t)VLC_FALSE );
820 /*****************************************************************************
821 * DemuxBlock: demux a given block
822 *****************************************************************************/
823 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
825 demux_sys_t *p_sys = p_demux->p_sys;
828 while( p < &pkt[i_pkt] )
830 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
838 p_pkt = block_New( p_demux, i_size );
839 memcpy( p_pkt->p_buffer, p, i_size);
841 /* Parse it and send it */
842 switch( 0x100 | p[3] )
849 msg_Warn( p_demux, "received a PSM packet" );
851 else if( p[3] == 0xbb )
853 msg_Warn( p_demux, "received a SYSTEM packet" );
855 block_Release( p_pkt );
862 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
864 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
866 block_Release( p_pkt );
871 int i_id = ps_pkt_id( p_pkt );
874 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
878 ESNew( p_demux, i_id );
880 if( tk->b_seen && tk->es &&
881 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
883 es_out_Send( p_demux->out, tk->es, p_pkt );
887 block_Release( p_pkt );
892 block_Release( p_pkt );
904 /*****************************************************************************
905 * ESNew: register a new elementary stream
906 *****************************************************************************/
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;
982 tk->fmt.subs.spu.palette[0] = 0xBeef;
983 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
984 16 * sizeof( uint32_t ) );
986 /* We select only when we are not in the menu */
987 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
989 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
995 tk->es = es_out_Add( p_demux->out, &tk->fmt );
998 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1000 tk->b_seen = VLC_TRUE;
1003 /*****************************************************************************
1004 * Event handler code
1005 *****************************************************************************/
1006 static int EventMouse( vlc_object_t *, char const *,
1007 vlc_value_t, vlc_value_t, void * );
1008 static int EventKey ( vlc_object_t *, char const *,
1009 vlc_value_t, vlc_value_t, void * );
1011 static int EventThread( vlc_object_t *p_this )
1013 event_thread_t *p_ev = (event_thread_t*)p_this;
1014 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
1015 vlc_object_t *p_vout = NULL;
1017 vlc_mutex_init( p_ev, &p_ev->lock );
1018 p_ev->b_moved = VLC_FALSE;
1019 p_ev->b_clicked = VLC_FALSE;
1020 p_ev->b_key = VLC_FALSE;
1022 p_ev->b_still = VLC_FALSE;
1024 /* catch all key event */
1025 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1028 while( !p_ev->b_die )
1030 vlc_bool_t b_activated = VLC_FALSE;
1035 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1038 struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1039 int i, i_action = -1;
1041 vlc_mutex_lock( &p_ev->lock );
1042 var_Get( p_ev->p_vlc, "key-pressed", &valk );
1043 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1045 if( p_hotkeys[i].i_key == valk.i_int )
1047 i_action = p_hotkeys[i].i_action;
1053 case ACTIONID_NAV_LEFT:
1054 dvdnav_left_button_select( p_sys->dvdnav, pci );
1056 case ACTIONID_NAV_RIGHT:
1057 dvdnav_right_button_select( p_sys->dvdnav, pci );
1059 case ACTIONID_NAV_UP:
1060 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1062 case ACTIONID_NAV_DOWN:
1063 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1065 case ACTIONID_NAV_ACTIVATE:
1066 b_activated = VLC_TRUE;
1067 dvdnav_button_activate( p_sys->dvdnav, pci );
1072 p_ev->b_key = VLC_FALSE;
1073 vlc_mutex_unlock( &p_ev->lock );
1077 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1079 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1080 vlc_value_t valx, valy;
1082 vlc_mutex_lock( &p_ev->lock );
1083 var_Get( p_vout, "mouse-x", &valx );
1084 var_Get( p_vout, "mouse-y", &valy );
1088 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1091 if( p_ev->b_clicked )
1093 b_activated = VLC_TRUE;
1094 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1098 p_ev->b_moved = VLC_FALSE;
1099 p_ev->b_clicked = VLC_FALSE;
1100 vlc_mutex_unlock( &p_ev->lock );
1102 if( p_vout && p_vout->b_die )
1104 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1105 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1106 vlc_object_release( p_vout );
1109 if( p_vout == NULL )
1111 p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1115 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1116 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1121 vlc_mutex_lock( &p_ev->lock );
1124 if( /* b_activated || // This breaks menus */
1125 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1127 p_ev->b_still = VLC_FALSE;
1128 dvdnav_still_skip( p_sys->dvdnav );
1131 vlc_mutex_unlock( &p_ev->lock );
1137 /* Release callback */
1140 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1141 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1142 vlc_object_release( p_vout );
1144 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1146 vlc_mutex_destroy( &p_ev->lock );
1151 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1152 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1154 event_thread_t *p_ev = p_data;
1155 vlc_mutex_lock( &p_ev->lock );
1156 if( psz_var[6] == 'c' )
1157 p_ev->b_clicked = VLC_TRUE;
1158 else if( psz_var[6] == 'm' )
1159 p_ev->b_moved = VLC_TRUE;
1160 vlc_mutex_unlock( &p_ev->lock );
1165 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1166 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1168 event_thread_t *p_ev = p_data;
1169 vlc_mutex_lock( &p_ev->lock );
1170 p_ev->b_key = VLC_TRUE;
1171 vlc_mutex_unlock( &p_ev->lock );
1176 static int MenusCallback( vlc_object_t *p_this, char const *psz_name,
1177 vlc_value_t oldval, vlc_value_t newval, void *p_arg )
1179 demux_t *p_demux = (demux_t*)p_arg;
1181 /* FIXME, not thread safe */
1182 dvdnav_menu_call( p_demux->p_sys->dvdnav, newval.i_int );