demux: mkv: handle WAVE_FORMAT_MPEG_ADTS_AAC
[vlc.git] / modules / access / dvdnav.c
blobada8713904624326d22895f717e41ee2b3f2ade9
1 /*****************************************************************************
2 * dvdnav.c: DVD module using the dvdnav library.
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * NOTA BENE: this module requires the linking against a library which is
26 * known to require licensing under the GNU General Public License version 2
27 * (or later). Therefore, the result of compiling this module will normally
28 * be subject to the terms of that later license.
29 *****************************************************************************/
32 /*****************************************************************************
33 * Preamble
34 *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
40 #include <assert.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <unistd.h> /* close() */
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
48 #include <vlc_input.h>
49 #include <vlc_access.h>
50 #include <vlc_demux.h>
51 #include <vlc_charset.h>
52 #include <vlc_fs.h>
53 #include <vlc_vout.h>
54 #include <vlc_dialog.h>
55 #include <vlc_iso_lang.h>
57 /* FIXME we should find a better way than including that */
58 #include "../../src/text/iso-639_def.h"
61 #include <dvdnav/dvdnav.h>
63 #include "../demux/mpeg/pes.h"
64 #include "../demux/mpeg/ps.h"
66 /*****************************************************************************
67 * Module descriptor
68 *****************************************************************************/
69 #define ANGLE_TEXT N_("DVD angle")
70 #define ANGLE_LONGTEXT N_( \
71 "Default DVD angle." )
73 #define MENU_TEXT N_("Start directly in menu")
74 #define MENU_LONGTEXT N_( \
75 "Start the DVD directly in the main menu. This "\
76 "will try to skip all the useless warning introductions." )
78 #define LANGUAGE_DEFAULT ("en")
80 static int AccessDemuxOpen ( vlc_object_t * );
81 static void Close( vlc_object_t * );
83 #if DVDREAD_VERSION >= 50300 && defined( HAVE_STREAM_CB_IN_DVDNAV_H )
84 #define HAVE_DVDNAV_DEMUX
85 static int DemuxOpen ( vlc_object_t * );
86 #endif
88 vlc_module_begin ()
89 set_shortname( N_("DVD with menus") )
90 set_description( N_("DVDnav Input") )
91 set_category( CAT_INPUT )
92 set_subcategory( SUBCAT_INPUT_ACCESS )
93 add_integer( "dvdnav-angle", 1, ANGLE_TEXT,
94 ANGLE_LONGTEXT, false )
95 add_bool( "dvdnav-menu", true,
96 MENU_TEXT, MENU_LONGTEXT, false )
97 set_capability( "access_demux", 5 )
98 add_shortcut( "dvd", "dvdnav", "file" )
99 set_callbacks( AccessDemuxOpen, Close )
100 #ifdef HAVE_DVDNAV_DEMUX
101 add_submodule()
102 set_description( N_("DVDnav demuxer") )
103 set_category( CAT_INPUT )
104 set_subcategory( SUBCAT_INPUT_DEMUX )
105 set_capability( "demux", 5 )
106 set_callbacks( DemuxOpen, Close )
107 add_shortcut( "dvd", "iso" )
108 #endif
109 vlc_module_end ()
111 /* Shall we use libdvdnav's read ahead cache? */
112 #ifdef __OS2__
113 #define DVD_READ_CACHE 0
114 #else
115 #define DVD_READ_CACHE 1
116 #endif
118 /*****************************************************************************
119 * Local prototypes
120 *****************************************************************************/
121 struct demux_sys_t
123 dvdnav_t *dvdnav;
125 /* */
126 bool b_reset_pcr;
127 bool b_readahead;
129 struct
131 bool b_created;
132 bool b_enabled;
133 vlc_mutex_t lock;
134 vlc_timer_t timer;
135 } still;
137 /* track */
138 ps_track_t tk[PS_TK_COUNT];
139 int i_mux_rate;
141 /* event */
142 vout_thread_t *p_vout;
144 /* palette for menus */
145 uint32_t clut[16];
146 uint8_t palette[4][4];
147 bool b_spu_change;
149 /* Aspect ration */
150 struct {
151 unsigned i_num;
152 unsigned i_den;
153 } sar;
155 /* */
156 int i_title;
157 input_title_t **title;
159 /* length of program group chain */
160 mtime_t i_pgc_length;
161 int i_vobu_index;
162 int i_vobu_flush;
165 static int Control( demux_t *, int, va_list );
166 static int Demux( demux_t * );
167 static int DemuxBlock( demux_t *, const uint8_t *, int );
168 static void DemuxForceStill( demux_t * );
170 static void DemuxTitles( demux_t * );
171 static void ESSubtitleUpdate( demux_t * );
172 static void ButtonUpdate( demux_t *, bool );
174 static void ESNew( demux_t *, int );
175 static int ProbeDVD( const char * );
177 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
179 static int ControlInternal( demux_t *, int, ... );
181 static void StillTimer( void * );
183 static int EventMouse( vlc_object_t *, char const *,
184 vlc_value_t, vlc_value_t, void * );
185 static int EventIntf( vlc_object_t *, char const *,
186 vlc_value_t, vlc_value_t, void * );
188 /*****************************************************************************
189 * CommonOpen:
190 *****************************************************************************/
191 static int CommonOpen( vlc_object_t *p_this,
192 dvdnav_t *p_dvdnav, bool b_readahead )
194 demux_t *p_demux = (demux_t*)p_this;
195 demux_sys_t *p_sys;
196 int i_angle;
197 char *psz_code;
199 assert( p_dvdnav );
201 /* Fill p_demux field */
202 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
203 p_sys->dvdnav = p_dvdnav;
204 p_sys->b_reset_pcr = false;
206 ps_track_init( p_sys->tk );
207 p_sys->sar.i_num = 0;
208 p_sys->sar.i_den = 0;
209 p_sys->i_mux_rate = 0;
210 p_sys->i_pgc_length = 0;
211 p_sys->b_spu_change = false;
212 p_sys->i_vobu_index = 0;
213 p_sys->i_vobu_flush = 0;
214 p_sys->b_readahead = b_readahead;
216 if( 1 )
218 // Hack for libdvdnav CVS.
219 // Without it dvdnav_get_number_of_titles() fails.
220 // Remove when fixed in libdvdnav CVS.
221 uint8_t buffer[DVD_VIDEO_LB_LEN];
222 int i_event, i_len;
224 if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
225 == DVDNAV_STATUS_ERR )
227 msg_Warn( p_demux, "dvdnav_get_next_block failed" );
230 dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
233 /* Configure dvdnav */
234 if( dvdnav_set_readahead_flag( p_sys->dvdnav, p_sys->b_readahead ) !=
235 DVDNAV_STATUS_OK )
237 msg_Warn( p_demux, "cannot set read-a-head flag" );
240 if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
241 DVDNAV_STATUS_OK )
243 msg_Warn( p_demux, "cannot set PGC positioning flag" );
246 /* Set menu language */
247 psz_code = DemuxGetLanguageCode( p_demux, "menu-language" );
248 if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
249 DVDNAV_STATUS_OK )
251 msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
252 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
253 /* We try to fall back to 'en' */
254 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
255 dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
257 free( psz_code );
259 /* Set audio language */
260 psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
261 if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
262 DVDNAV_STATUS_OK )
264 msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
265 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
266 /* We try to fall back to 'en' */
267 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
268 dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
270 free( psz_code );
272 /* Set spu language */
273 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
274 if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
275 DVDNAV_STATUS_OK )
277 msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
278 psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
279 /* We try to fall back to 'en' */
280 if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
281 dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
283 free( psz_code );
285 DemuxTitles( p_demux );
287 if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
289 msg_Dbg( p_demux, "trying to go to dvd menu" );
291 if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
293 msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
294 vlc_dialog_display_error( p_demux, _("Playback failure"), "%s",
295 _("VLC cannot set the DVD's title. It possibly "
296 "cannot decrypt the entire disc.") );
297 free( p_sys );
298 return VLC_EGENERIC;
301 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
302 DVDNAV_STATUS_OK )
304 /* Try going to menu root */
305 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
306 DVDNAV_STATUS_OK )
307 msg_Warn( p_demux, "cannot go to dvd menu" );
311 i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
312 if( i_angle <= 0 ) i_angle = 1;
314 /* FIXME hack hack hack hack FIXME */
315 /* Get p_input and create variable */
316 var_Create( p_demux->p_input, "x-start", VLC_VAR_INTEGER );
317 var_Create( p_demux->p_input, "y-start", VLC_VAR_INTEGER );
318 var_Create( p_demux->p_input, "x-end", VLC_VAR_INTEGER );
319 var_Create( p_demux->p_input, "y-end", VLC_VAR_INTEGER );
320 var_Create( p_demux->p_input, "color", VLC_VAR_ADDRESS );
321 var_Create( p_demux->p_input, "menu-palette", VLC_VAR_ADDRESS );
322 var_Create( p_demux->p_input, "highlight", VLC_VAR_BOOL );
324 /* catch vout creation event */
325 var_AddCallback( p_demux->p_input, "intf-event", EventIntf, p_demux );
327 p_sys->still.b_enabled = false;
328 vlc_mutex_init( &p_sys->still.lock );
329 if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
330 p_sys->still.b_created = true;
332 return VLC_SUCCESS;
335 /*****************************************************************************
336 * AccessDemuxOpen:
337 *****************************************************************************/
338 static int AccessDemuxOpen ( vlc_object_t *p_this )
340 demux_t *p_demux = (demux_t*)p_this;
341 dvdnav_t *p_dvdnav = NULL;
342 char *psz_file = NULL;
343 const char *psz_path = NULL;
344 int i_ret = VLC_EGENERIC;
345 bool forced = false;
347 if( !strncmp(p_demux->psz_access, "dvd", 3) )
348 forced = true;
350 if( !p_demux->psz_file || !*p_demux->psz_file )
352 /* Only when selected */
353 if( !forced )
354 return VLC_EGENERIC;
356 psz_file = var_InheritString( p_this, "dvd" );
358 else
359 psz_file = strdup( p_demux->psz_file );
361 #if defined( _WIN32 ) || defined( __OS2__ )
362 if( psz_file != NULL )
364 /* Remove trailing backslash, otherwise dvdnav_open will fail */
365 size_t flen = strlen( psz_file );
366 if( flen > 0 && psz_file[flen - 1] == '\\' )
367 psz_file[flen - 1] = '\0';
369 else
370 psz_file = strdup("");
371 #endif
373 if( unlikely(psz_file == NULL) )
374 return VLC_EGENERIC;
376 /* Try some simple probing to avoid going through dvdnav_open too often */
377 if( !forced && ProbeDVD( psz_file ) != VLC_SUCCESS )
378 goto bailout;
380 /* Open dvdnav */
381 psz_path = ToLocale( psz_file );
382 if( dvdnav_open( &p_dvdnav, psz_path ) != DVDNAV_STATUS_OK )
384 msg_Warn( p_demux, "cannot open DVD (%s)", psz_file);
385 goto bailout;
388 i_ret = CommonOpen( p_this, p_dvdnav, !!DVD_READ_CACHE );
389 if( i_ret != VLC_SUCCESS )
390 dvdnav_close( p_dvdnav );
392 bailout:
393 free( psz_file );
394 if( psz_path )
395 LocaleFree( psz_path );
396 return i_ret;
399 #ifdef HAVE_DVDNAV_DEMUX
400 /*****************************************************************************
401 * StreamProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
402 *****************************************************************************/
403 static int StreamProbeDVD( stream_t *s )
405 /* first sector should be filled with zeros */
406 ssize_t i_peek;
407 const uint8_t *p_peek;
408 i_peek = vlc_stream_Peek( s, &p_peek, 2048 );
409 if( i_peek < 512 ) {
410 return VLC_EGENERIC;
412 while (i_peek > 0) {
413 if (p_peek[ --i_peek ]) {
414 return VLC_EGENERIC;
418 /* ISO 9660 volume descriptor */
419 char iso_dsc[6];
420 if( vlc_stream_Seek( s, 0x8000 + 1 ) != VLC_SUCCESS
421 || vlc_stream_Read( s, iso_dsc, sizeof (iso_dsc) ) < (int)sizeof (iso_dsc)
422 || memcmp( iso_dsc, "CD001\x01", 6 ) )
423 return VLC_EGENERIC;
425 /* Try to find the anchor (2 bytes at LBA 256) */
426 uint16_t anchor;
428 if( vlc_stream_Seek( s, 256 * DVD_VIDEO_LB_LEN ) == VLC_SUCCESS
429 && vlc_stream_Read( s, &anchor, 2 ) == 2
430 && GetWLE( &anchor ) == 2 )
431 return VLC_SUCCESS;
432 else
433 return VLC_EGENERIC;
436 /*****************************************************************************
437 * dvdnav stream callbacks
438 *****************************************************************************/
439 static int stream_cb_seek( void *s, uint64_t pos )
441 return vlc_stream_Seek( (stream_t *)s, pos );
444 static int stream_cb_read( void *s, void* buffer, int size )
446 return vlc_stream_Read( (stream_t *)s, buffer, size );
449 /*****************************************************************************
450 * DemuxOpen:
451 *****************************************************************************/
452 static int DemuxOpen ( vlc_object_t *p_this )
454 demux_t *p_demux = (demux_t*)p_this;
455 dvdnav_t *p_dvdnav = NULL;
456 int i_ret = VLC_EGENERIC;
457 int64_t i_init_pos;
458 bool forced = false, b_seekable = false;
460 if( p_demux->psz_demux != NULL
461 && !strncmp(p_demux->psz_demux, "dvd", 3) )
462 forced = true;
464 /* StreamProbeDVD need FASTSEEK, but if dvd is forced, we don't probe thus
465 * don't need fastseek */
466 vlc_stream_Control( p_demux->s, forced ? STREAM_CAN_SEEK : STREAM_CAN_FASTSEEK,
467 &b_seekable );
468 if( !b_seekable )
469 return VLC_EGENERIC;
471 i_init_pos = vlc_stream_Tell( p_demux->s );
473 /* Try some simple probing to avoid going through dvdnav_open too often */
474 if( !forced && StreamProbeDVD( p_demux->s ) != VLC_SUCCESS )
475 goto bailout;
477 static dvdnav_stream_cb stream_cb =
479 .pf_seek = stream_cb_seek,
480 .pf_read = stream_cb_read,
481 .pf_readv = NULL,
484 /* Open dvdnav with stream callbacks */
485 if( dvdnav_open_stream( &p_dvdnav, p_demux->s,
486 &stream_cb ) != DVDNAV_STATUS_OK )
488 msg_Warn( p_demux, "cannot open DVD with open_stream" );
489 goto bailout;
492 i_ret = CommonOpen( p_this, p_dvdnav, false );
493 if( i_ret != VLC_SUCCESS )
494 dvdnav_close( p_dvdnav );
496 bailout:
497 if( i_ret != VLC_SUCCESS )
498 vlc_stream_Seek( p_demux->s, i_init_pos );
499 return i_ret;
501 #endif
503 /*****************************************************************************
504 * Close:
505 *****************************************************************************/
506 static void Close( vlc_object_t *p_this )
508 demux_t *p_demux = (demux_t*)p_this;
509 demux_sys_t *p_sys = p_demux->p_sys;
511 /* Stop vout event handler */
512 var_DelCallback( p_demux->p_input, "intf-event", EventIntf, p_demux );
513 if( p_sys->p_vout != NULL )
514 { /* Should not happen, but better be safe than sorry. */
515 msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
516 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
517 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
520 /* Stop still image handler */
521 if( p_sys->still.b_created )
522 vlc_timer_destroy( p_sys->still.timer );
523 vlc_mutex_destroy( &p_sys->still.lock );
525 var_Destroy( p_demux->p_input, "highlight" );
526 var_Destroy( p_demux->p_input, "x-start" );
527 var_Destroy( p_demux->p_input, "x-end" );
528 var_Destroy( p_demux->p_input, "y-start" );
529 var_Destroy( p_demux->p_input, "y-end" );
530 var_Destroy( p_demux->p_input, "color" );
531 var_Destroy( p_demux->p_input, "menu-palette" );
533 for( int i = 0; i < PS_TK_COUNT; i++ )
535 ps_track_t *tk = &p_sys->tk[i];
536 if( tk->b_configured )
538 es_format_Clean( &tk->fmt );
539 if( tk->es ) es_out_Del( p_demux->out, tk->es );
543 /* Free the array of titles */
544 for( int i = 0; i < p_sys->i_title; i++ )
545 vlc_input_title_Delete( p_sys->title[i] );
546 TAB_CLEAN( p_sys->i_title, p_sys->title );
548 dvdnav_close( p_sys->dvdnav );
549 free( p_sys );
552 /*****************************************************************************
553 * Control:
554 *****************************************************************************/
555 static int Control( demux_t *p_demux, int i_query, va_list args )
557 demux_sys_t *p_sys = p_demux->p_sys;
558 input_title_t ***ppp_title;
559 int i;
561 switch( i_query )
563 case DEMUX_SET_POSITION:
564 case DEMUX_GET_POSITION:
565 case DEMUX_GET_TIME:
566 case DEMUX_GET_LENGTH:
568 uint32_t pos, len;
569 if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
570 DVDNAV_STATUS_OK || len == 0 )
572 return VLC_EGENERIC;
575 switch( i_query )
577 case DEMUX_GET_POSITION:
578 *va_arg( args, double* ) = (double)pos / (double)len;
579 return VLC_SUCCESS;
581 case DEMUX_SET_POSITION:
582 pos = va_arg( args, double ) * len;
583 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
584 DVDNAV_STATUS_OK )
586 return VLC_SUCCESS;
588 break;
590 case DEMUX_GET_TIME:
591 if( p_sys->i_pgc_length > 0 )
593 *va_arg( args, int64_t * ) = p_sys->i_pgc_length*pos/len;
594 return VLC_SUCCESS;
596 break;
598 case DEMUX_GET_LENGTH:
599 if( p_sys->i_pgc_length > 0 )
601 *va_arg( args, int64_t * ) = (int64_t)p_sys->i_pgc_length;
602 return VLC_SUCCESS;
604 break;
606 return VLC_EGENERIC;
609 /* Special for access_demux */
610 case DEMUX_CAN_PAUSE:
611 case DEMUX_CAN_SEEK:
612 case DEMUX_CAN_CONTROL_PACE:
613 /* TODO */
614 *va_arg( args, bool * ) = true;
615 return VLC_SUCCESS;
617 case DEMUX_SET_PAUSE_STATE:
618 return VLC_SUCCESS;
620 case DEMUX_GET_TITLE_INFO:
621 ppp_title = va_arg( args, input_title_t*** );
622 *va_arg( args, int* ) = p_sys->i_title;
623 *va_arg( args, int* ) = 0; /* Title offset */
624 *va_arg( args, int* ) = 1; /* Chapter offset */
626 /* Duplicate title infos */
627 *ppp_title = malloc( p_sys->i_title * sizeof( input_title_t * ) );
628 for( i = 0; i < p_sys->i_title; i++ )
630 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
632 return VLC_SUCCESS;
634 case DEMUX_SET_TITLE:
635 i = va_arg( args, int );
636 if( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
637 != DVDNAV_STATUS_OK )
639 msg_Warn( p_demux, "cannot set title/chapter" );
640 return VLC_EGENERIC;
643 if( i != 0 )
645 dvdnav_still_skip( p_sys->dvdnav );
646 if( dvdnav_title_play( p_sys->dvdnav, i ) != DVDNAV_STATUS_OK )
648 msg_Warn( p_demux, "cannot set title/chapter" );
649 return VLC_EGENERIC;
653 p_demux->info.i_update |=
654 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
655 p_demux->info.i_title = i;
656 p_demux->info.i_seekpoint = 0;
657 return VLC_SUCCESS;
659 case DEMUX_SET_SEEKPOINT:
660 i = va_arg( args, int );
661 if( p_demux->info.i_title == 0 )
663 static const int argtab[] = {
664 DVD_MENU_Escape,
665 DVD_MENU_Root,
666 DVD_MENU_Title,
667 DVD_MENU_Part,
668 DVD_MENU_Subpicture,
669 DVD_MENU_Audio,
670 DVD_MENU_Angle
672 enum { numargs = sizeof(argtab)/sizeof(int) };
673 if( (unsigned)i >= numargs || DVDNAV_STATUS_OK !=
674 dvdnav_menu_call(p_sys->dvdnav,argtab[i]) )
675 return VLC_EGENERIC;
677 else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
678 i + 1 ) != DVDNAV_STATUS_OK )
680 msg_Warn( p_demux, "cannot set title/chapter" );
681 return VLC_EGENERIC;
683 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
684 p_demux->info.i_seekpoint = i;
685 return VLC_SUCCESS;
687 case DEMUX_GET_PTS_DELAY:
688 *va_arg( args, int64_t * ) =
689 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
690 return VLC_SUCCESS;
692 case DEMUX_GET_META:
694 const char *title_name = NULL;
696 dvdnav_get_title_string(p_sys->dvdnav, &title_name);
697 if( (NULL != title_name) && ('\0' != title_name[0]) )
699 vlc_meta_t *p_meta = va_arg( args, vlc_meta_t* );
700 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
701 return VLC_SUCCESS;
703 return VLC_EGENERIC;
706 case DEMUX_NAV_ACTIVATE:
708 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
710 ButtonUpdate( p_demux, true );
711 dvdnav_button_activate( p_sys->dvdnav, pci );
712 break;
715 case DEMUX_NAV_UP:
717 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
719 dvdnav_upper_button_select( p_sys->dvdnav, pci );
720 break;
723 case DEMUX_NAV_DOWN:
725 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
727 dvdnav_lower_button_select( p_sys->dvdnav, pci );
728 break;
731 case DEMUX_NAV_LEFT:
733 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
735 dvdnav_left_button_select( p_sys->dvdnav, pci );
736 break;
739 case DEMUX_NAV_RIGHT:
741 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
743 dvdnav_right_button_select( p_sys->dvdnav, pci );
744 break;
747 case DEMUX_NAV_MENU:
749 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title )
750 != DVDNAV_STATUS_OK )
752 msg_Warn( p_demux, "cannot select Title menu" );
753 if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
754 != DVDNAV_STATUS_OK )
756 msg_Warn( p_demux, "cannot select Root menu" );
757 return VLC_EGENERIC;
760 p_demux->info.i_update |=
761 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
762 p_demux->info.i_title = 0;
763 p_demux->info.i_seekpoint = 2;
764 break;
767 /* TODO implement others */
768 default:
769 return VLC_EGENERIC;
772 return VLC_SUCCESS;
775 static int ControlInternal( demux_t *p_demux, int i_query, ... )
777 va_list args;
778 int i_result;
780 va_start( args, i_query );
781 i_result = Control( p_demux, i_query, args );
782 va_end( args );
784 return i_result;
786 /*****************************************************************************
787 * Demux:
788 *****************************************************************************/
789 static int Demux( demux_t *p_demux )
791 demux_sys_t *p_sys = p_demux->p_sys;
793 uint8_t buffer[DVD_VIDEO_LB_LEN];
794 uint8_t *packet = buffer;
795 int i_event;
796 int i_len;
797 dvdnav_status_t status;
799 if( p_sys->b_readahead )
800 status = dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event,
801 &i_len );
802 else
803 status = dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event,
804 &i_len );
805 if( status == DVDNAV_STATUS_ERR )
807 msg_Warn( p_demux, "cannot get next block (%s)",
808 dvdnav_err_to_string( p_sys->dvdnav ) );
809 if( p_demux->info.i_title == 0 )
811 msg_Dbg( p_demux, "jumping to first title" );
812 return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
814 return -1;
817 switch( i_event )
819 case DVDNAV_BLOCK_OK: /* mpeg block */
820 vlc_mutex_lock( &p_sys->still.lock );
821 vlc_timer_schedule( p_sys->still.timer, false, 0, 0 );
822 p_sys->still.b_enabled = false;
823 vlc_mutex_unlock( &p_sys->still.lock );
824 if( p_sys->b_reset_pcr )
826 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
827 p_sys->b_reset_pcr = false;
829 DemuxBlock( p_demux, packet, i_len );
830 if( p_sys->i_vobu_index > 0 )
832 if( p_sys->i_vobu_flush == p_sys->i_vobu_index )
833 DemuxForceStill( p_demux );
834 p_sys->i_vobu_index++;
836 break;
838 case DVDNAV_NOP: /* Nothing */
839 msg_Dbg( p_demux, "DVDNAV_NOP" );
840 break;
842 case DVDNAV_STILL_FRAME:
844 dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
845 bool b_still_init = false;
847 vlc_mutex_lock( &p_sys->still.lock );
848 if( !p_sys->still.b_enabled )
850 msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
851 msg_Dbg( p_demux, " - length=0x%x", event->length );
852 p_sys->still.b_enabled = true;
854 if( event->length != 0xff && p_sys->still.b_created )
856 mtime_t delay = event->length * CLOCK_FREQ;
857 vlc_timer_schedule( p_sys->still.timer, false, delay, 0 );
860 b_still_init = true;
862 vlc_mutex_unlock( &p_sys->still.lock );
864 if( b_still_init )
866 DemuxForceStill( p_demux );
867 p_sys->b_reset_pcr = true;
869 msleep( 40000 );
870 break;
873 case DVDNAV_SPU_CLUT_CHANGE:
875 int i;
877 msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
878 /* Update color lookup table (16 *uint32_t in packet) */
879 memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
881 /* HACK to get the SPU tracks registered in the right order */
882 for( i = 0; i < 0x1f; i++ )
884 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
885 ESNew( p_demux, 0xbd20 + i );
887 /* END HACK */
888 break;
891 case DVDNAV_SPU_STREAM_CHANGE:
893 dvdnav_spu_stream_change_event_t *event =
894 (dvdnav_spu_stream_change_event_t*)packet;
895 int i;
897 msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
898 msg_Dbg( p_demux, " - physical_wide=%d",
899 event->physical_wide );
900 msg_Dbg( p_demux, " - physical_letterbox=%d",
901 event->physical_letterbox);
902 msg_Dbg( p_demux, " - physical_pan_scan=%d",
903 event->physical_pan_scan );
905 ESSubtitleUpdate( p_demux );
906 p_sys->b_spu_change = true;
908 /* HACK to get the SPU tracks registered in the right order */
909 for( i = 0; i < 0x1f; i++ )
911 if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
912 ESNew( p_demux, 0xbd20 + i );
914 /* END HACK */
915 break;
918 case DVDNAV_AUDIO_STREAM_CHANGE:
920 dvdnav_audio_stream_change_event_t *event =
921 (dvdnav_audio_stream_change_event_t*)packet;
922 msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
923 msg_Dbg( p_demux, " - physical=%d", event->physical );
924 /* TODO */
925 break;
928 case DVDNAV_VTS_CHANGE:
930 int32_t i_title = 0;
931 int32_t i_part = 0;
933 dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
934 msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
935 msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
936 msg_Dbg( p_demux, " - domain=%d", event->new_domain );
938 /* reset PCR */
939 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
941 for( int i = 0; i < PS_TK_COUNT; i++ )
943 ps_track_t *tk = &p_sys->tk[i];
944 if( tk->b_configured )
946 es_format_Clean( &tk->fmt );
947 if( tk->es ) es_out_Del( p_demux->out, tk->es );
949 tk->b_configured = false;
952 uint32_t i_width, i_height;
953 if( dvdnav_get_video_resolution( p_sys->dvdnav,
954 &i_width, &i_height ) )
955 i_width = i_height = 0;
956 switch( dvdnav_get_video_aspect( p_sys->dvdnav ) )
958 case 0:
959 p_sys->sar.i_num = 4 * i_height;
960 p_sys->sar.i_den = 3 * i_width;
961 break;
962 case 3:
963 p_sys->sar.i_num = 16 * i_height;
964 p_sys->sar.i_den = 9 * i_width;
965 break;
966 default:
967 p_sys->sar.i_num = 0;
968 p_sys->sar.i_den = 0;
969 break;
972 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
973 &i_part ) == DVDNAV_STATUS_OK )
975 if( i_title >= 0 && i_title < p_sys->i_title &&
976 p_demux->info.i_title != i_title )
978 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
979 p_demux->info.i_title = i_title;
982 break;
985 case DVDNAV_CELL_CHANGE:
987 int32_t i_title = 0;
988 int32_t i_part = 0;
990 dvdnav_cell_change_event_t *event =
991 (dvdnav_cell_change_event_t*)packet;
992 msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
993 msg_Dbg( p_demux, " - cellN=%d", event->cellN );
994 msg_Dbg( p_demux, " - pgN=%d", event->pgN );
995 msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
996 msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
997 msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
998 msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
999 msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
1001 /* Store the length in time of the current PGC */
1002 p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
1003 p_sys->i_vobu_index = 0;
1004 p_sys->i_vobu_flush = 0;
1006 /* FIXME is it correct or there is better way to know chapter change */
1007 if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
1008 &i_part ) == DVDNAV_STATUS_OK )
1010 if( i_title >= 0 && i_title < p_sys->i_title )
1012 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
1013 p_demux->info.i_title = i_title;
1015 if( i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint )
1017 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1018 p_demux->info.i_seekpoint = i_part - 1;
1022 break;
1025 case DVDNAV_NAV_PACKET:
1027 p_sys->i_vobu_index = 1;
1028 p_sys->i_vobu_flush = 0;
1030 /* Look if we have need to force a flush (and when) */
1031 const pci_gi_t *p_pci_gi = &dvdnav_get_current_nav_pci( p_sys->dvdnav )->pci_gi;
1032 if( p_pci_gi->vobu_se_e_ptm != 0 && p_pci_gi->vobu_se_e_ptm < p_pci_gi->vobu_e_ptm )
1034 const dsi_gi_t *p_dsi_gi = &dvdnav_get_current_nav_dsi( p_sys->dvdnav )->dsi_gi;
1035 if( p_dsi_gi->vobu_3rdref_ea != 0 )
1036 p_sys->i_vobu_flush = p_dsi_gi->vobu_3rdref_ea;
1037 else if( p_dsi_gi->vobu_2ndref_ea != 0 )
1038 p_sys->i_vobu_flush = p_dsi_gi->vobu_2ndref_ea;
1039 else if( p_dsi_gi->vobu_1stref_ea != 0 )
1040 p_sys->i_vobu_flush = p_dsi_gi->vobu_1stref_ea;
1043 #ifdef DVDNAV_DEBUG
1044 msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
1045 #endif
1046 /* A lot of thing to do here :
1047 * - handle packet
1048 * - fetch pts (for time display)
1049 * - ...
1051 DemuxBlock( p_demux, packet, i_len );
1052 if( p_sys->b_spu_change )
1054 ButtonUpdate( p_demux, false );
1055 p_sys->b_spu_change = false;
1057 break;
1060 case DVDNAV_STOP: /* EOF */
1061 msg_Dbg( p_demux, "DVDNAV_STOP" );
1063 if( p_sys->b_readahead )
1064 dvdnav_free_cache_block( p_sys->dvdnav, packet );
1065 return 0;
1067 case DVDNAV_HIGHLIGHT:
1069 dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
1070 msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
1071 msg_Dbg( p_demux, " - display=%d", event->display );
1072 msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
1073 ButtonUpdate( p_demux, false );
1074 break;
1077 case DVDNAV_HOP_CHANNEL:
1078 msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
1079 p_sys->i_vobu_index = 0;
1080 p_sys->i_vobu_flush = 0;
1081 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1082 break;
1084 case DVDNAV_WAIT:
1085 msg_Dbg( p_demux, "DVDNAV_WAIT" );
1087 bool b_empty;
1088 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
1089 if( !b_empty )
1091 msleep( 40*1000 );
1093 else
1095 dvdnav_wait_skip( p_sys->dvdnav );
1096 p_sys->b_reset_pcr = true;
1098 break;
1100 default:
1101 msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
1102 break;
1105 if( p_sys->b_readahead )
1106 dvdnav_free_cache_block( p_sys->dvdnav, packet );
1108 return 1;
1111 /* Get a 2 char code
1112 * FIXME: partiallyy duplicated from src/input/es_out.c
1114 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
1116 const iso639_lang_t *pl;
1117 char *psz_lang;
1118 char *p;
1120 psz_lang = var_CreateGetString( p_demux, psz_var );
1121 if( !psz_lang )
1122 return strdup(LANGUAGE_DEFAULT);
1124 /* XXX: we will use only the first value
1125 * (and ignore other ones in case of a list) */
1126 if( ( p = strchr( psz_lang, ',' ) ) )
1127 *p = '\0';
1129 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
1131 if( *psz_lang == '\0' )
1132 continue;
1133 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
1134 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
1135 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
1136 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
1137 break;
1140 free( psz_lang );
1142 if( pl->psz_eng_name != NULL )
1143 return strdup( pl->psz_iso639_1 );
1145 return strdup(LANGUAGE_DEFAULT);
1148 static void DemuxTitles( demux_t *p_demux )
1150 demux_sys_t *p_sys = p_demux->p_sys;
1151 input_title_t *t;
1152 seekpoint_t *s;
1153 int32_t i_titles;
1155 /* Menu */
1156 t = vlc_input_title_New();
1157 t->i_flags = INPUT_TITLE_MENU | INPUT_TITLE_INTERACTIVE;
1158 t->psz_name = strdup( "DVD Menu" );
1160 s = vlc_seekpoint_New();
1161 s->psz_name = strdup( "Resume" );
1162 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1164 s = vlc_seekpoint_New();
1165 s->psz_name = strdup( "Root" );
1166 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1168 s = vlc_seekpoint_New();
1169 s->psz_name = strdup( "Title" );
1170 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1172 s = vlc_seekpoint_New();
1173 s->psz_name = strdup( "Chapter" );
1174 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1176 s = vlc_seekpoint_New();
1177 s->psz_name = strdup( "Subtitle" );
1178 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1180 s = vlc_seekpoint_New();
1181 s->psz_name = strdup( "Audio" );
1182 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1184 s = vlc_seekpoint_New();
1185 s->psz_name = strdup( "Angle" );
1186 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1188 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1190 /* Find out number of titles/chapters */
1191 dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
1193 if( i_titles > 90 )
1194 msg_Err( p_demux, "This is probably an Arccos Protected DVD. This could take time..." );
1196 for( int i = 1; i <= i_titles; i++ )
1198 uint64_t i_title_length;
1199 uint64_t *p_chapters_time;
1201 int32_t i_chapters = dvdnav_describe_title_chapters( p_sys->dvdnav, i,
1202 &p_chapters_time,
1203 &i_title_length );
1204 if( i_chapters < 1 )
1206 i_title_length = 0;
1207 p_chapters_time = NULL;
1209 t = vlc_input_title_New();
1210 t->i_length = i_title_length * 1000 / 90;
1211 for( int j = 0; j < __MAX( i_chapters, 1 ); j++ )
1213 s = vlc_seekpoint_New();
1214 if( p_chapters_time )
1215 s->i_time_offset = p_chapters_time[j] * 1000 / 90;
1216 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1218 free( p_chapters_time );
1219 TAB_APPEND( p_sys->i_title, p_sys->title, t );
1223 /*****************************************************************************
1224 * Update functions:
1225 *****************************************************************************/
1226 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
1228 demux_sys_t *p_sys = p_demux->p_sys;
1229 int32_t i_title, i_part;
1231 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1233 dvdnav_highlight_area_t hl;
1234 int32_t i_button;
1235 bool b_button_ok;
1237 if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
1238 != DVDNAV_STATUS_OK )
1240 msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
1241 return;
1244 b_button_ok = false;
1245 if( i_button > 0 && i_title == 0 )
1247 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1249 b_button_ok = DVDNAV_STATUS_OK ==
1250 dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
1253 if( b_button_ok )
1255 for( unsigned i = 0; i < 4; i++ )
1257 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
1258 uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
1260 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
1261 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
1262 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
1263 p_sys->palette[i][3] = i_alpha;
1266 vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1267 var_SetInteger( p_demux->p_input, "x-start", hl.sx );
1268 var_SetInteger( p_demux->p_input, "x-end", hl.ex );
1269 var_SetInteger( p_demux->p_input, "y-start", hl.sy );
1270 var_SetInteger( p_demux->p_input, "y-end", hl.ey );
1272 var_SetAddress( p_demux->p_input, "menu-palette", p_sys->palette );
1273 var_SetBool( p_demux->p_input, "highlight", true );
1275 msg_Dbg( p_demux, "buttonUpdate %d", i_button );
1277 else
1279 msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
1280 i_button, i_title );
1282 /* Show all */
1283 vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
1284 var_SetBool( p_demux->p_input, "highlight", false );
1286 vlc_global_unlock( VLC_HIGHLIGHT_MUTEX );
1289 static void ESSubtitleUpdate( demux_t *p_demux )
1291 demux_sys_t *p_sys = p_demux->p_sys;
1292 int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
1293 int32_t i_title, i_part;
1295 ButtonUpdate( p_demux, false );
1297 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1298 if( i_title > 0 ) return;
1300 /* dvdnav_get_active_spu_stream sets (in)visibility flag as 0xF0 */
1301 if( i_spu >= 0 && i_spu <= 0x1f )
1303 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1305 ESNew( p_demux, 0xbd20 + i_spu );
1307 /* be sure to unselect it (reset) */
1308 if( tk->es )
1310 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1311 (bool)false );
1313 /* now select it */
1314 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1317 else
1319 for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1321 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1322 if( tk->es )
1324 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1325 (bool)false );
1331 /*****************************************************************************
1332 * DemuxBlock: demux a given block
1333 *****************************************************************************/
1334 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
1336 demux_sys_t *p_sys = p_demux->p_sys;
1338 while( len > 0 )
1340 int i_size = ps_pkt_size( p, len );
1341 if( i_size <= 0 || i_size > len )
1343 break;
1346 /* Create a block */
1347 block_t *p_pkt = block_Alloc( i_size );
1348 memcpy( p_pkt->p_buffer, p, i_size);
1350 /* Parse it and send it */
1351 switch( 0x100 | p[3] )
1353 case 0x1b9:
1354 case 0x1bb:
1355 case 0x1bc:
1356 #ifdef DVDNAV_DEBUG
1357 if( p[3] == 0xbc )
1359 msg_Warn( p_demux, "received a PSM packet" );
1361 else if( p[3] == 0xbb )
1363 msg_Warn( p_demux, "received a SYSTEM packet" );
1365 #endif
1366 block_Release( p_pkt );
1367 break;
1369 case 0x1ba:
1371 int64_t i_scr;
1372 int i_mux_rate;
1373 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1375 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
1376 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1378 block_Release( p_pkt );
1379 break;
1381 default:
1383 int i_id = ps_pkt_id( p_pkt );
1384 if( i_id >= 0xc0 )
1386 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1388 if( !tk->b_configured )
1390 ESNew( p_demux, i_id );
1392 if( tk->es &&
1393 !ps_pkt_parse_pes( VLC_OBJECT(p_demux), p_pkt, tk->i_skip ) )
1395 es_out_Send( p_demux->out, tk->es, p_pkt );
1397 else
1399 block_Release( p_pkt );
1402 else
1404 block_Release( p_pkt );
1406 break;
1410 p += i_size;
1411 len -= i_size;
1414 return VLC_SUCCESS;
1417 /*****************************************************************************
1418 * Force still images to be displayed by sending EOS and stopping buffering.
1419 *****************************************************************************/
1420 static void DemuxForceStill( demux_t *p_demux )
1422 static const uint8_t buffer[] = {
1423 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
1424 0x80, 0x00, 0x00,
1425 0x00, 0x00, 0x01, 0xB7,
1427 DemuxBlock( p_demux, buffer, sizeof(buffer) );
1429 bool b_empty;
1430 es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
1433 /*****************************************************************************
1434 * ESNew: register a new elementary stream
1435 *****************************************************************************/
1436 static void ESNew( demux_t *p_demux, int i_id )
1438 demux_sys_t *p_sys = p_demux->p_sys;
1439 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1440 bool b_select = false;
1442 if( tk->b_configured ) return;
1444 if( ps_track_fill( tk, 0, i_id, NULL, true ) )
1446 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1447 return;
1450 /* Add a new ES */
1451 if( tk->fmt.i_cat == VIDEO_ES )
1453 tk->fmt.video.i_sar_num = p_sys->sar.i_num;
1454 tk->fmt.video.i_sar_den = p_sys->sar.i_den;
1455 b_select = true;
1457 else if( tk->fmt.i_cat == AUDIO_ES )
1459 int i_audio = -1;
1460 /* find the audio number PLEASE find another way */
1461 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
1463 i_audio = i_id&0x07;
1465 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
1467 i_audio = i_id&0xf;
1469 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
1471 i_audio = i_id&0x1f;
1473 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
1475 i_audio = i_id&0x1f;
1477 if( i_audio >= 0 )
1479 int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1480 if( i_lang != 0xffff )
1482 tk->fmt.psz_language = malloc( 3 );
1483 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1484 tk->fmt.psz_language[1] = (i_lang )&0xff;
1485 tk->fmt.psz_language[2] = 0;
1487 if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1489 b_select = true;
1493 else if( tk->fmt.i_cat == SPU_ES )
1495 int32_t i_title, i_part;
1496 int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1497 if( i_lang != 0xffff )
1499 tk->fmt.psz_language = malloc( 3 );
1500 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1501 tk->fmt.psz_language[1] = (i_lang )&0xff;
1502 tk->fmt.psz_language[2] = 0;
1505 /* Palette */
1506 tk->fmt.subs.spu.palette[0] = 0xBeef;
1507 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1508 16 * sizeof( uint32_t ) );
1510 /* We select only when we are not in the menu */
1511 dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1512 if( i_title > 0 &&
1513 dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1515 b_select = true;
1519 tk->fmt.i_id = i_id;
1520 tk->es = es_out_Add( p_demux->out, &tk->fmt );
1521 if( b_select && tk->es )
1523 es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1525 tk->b_configured = true;
1527 if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1530 /*****************************************************************************
1531 * Still image end
1532 *****************************************************************************/
1533 static void StillTimer( void *p_data )
1535 demux_sys_t *p_sys = p_data;
1537 vlc_mutex_lock( &p_sys->still.lock );
1538 if( likely(p_sys->still.b_enabled) )
1540 p_sys->still.b_enabled = false;
1541 dvdnav_still_skip( p_sys->dvdnav );
1543 vlc_mutex_unlock( &p_sys->still.lock );
1546 static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
1547 vlc_value_t oldval, vlc_value_t val, void *p_data )
1549 demux_t *p_demux = p_data;
1550 demux_sys_t *p_sys = p_demux->p_sys;
1552 /* FIXME? PCI usage thread safe? */
1553 pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1554 int x = val.coords.x;
1555 int y = val.coords.y;
1557 if( psz_var[6] == 'm' ) /* mouse-moved */
1558 dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
1559 else
1561 assert( psz_var[6] == 'c' ); /* mouse-clicked */
1563 ButtonUpdate( p_demux, true );
1564 dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
1566 (void)p_vout;
1567 (void)oldval;
1568 return VLC_SUCCESS;
1571 static int EventIntf( vlc_object_t *p_input, char const *psz_var,
1572 vlc_value_t oldval, vlc_value_t val, void *p_data )
1574 demux_t *p_demux = p_data;
1575 demux_sys_t *p_sys = p_demux->p_sys;
1577 if (val.i_int == INPUT_EVENT_VOUT)
1579 if( p_sys->p_vout != NULL )
1581 var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
1582 var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
1583 vlc_object_release( p_sys->p_vout );
1586 p_sys->p_vout = input_GetVout( (input_thread_t *)p_input );
1587 if( p_sys->p_vout != NULL )
1589 var_AddCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
1590 var_AddCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
1593 (void) psz_var; (void) oldval;
1594 return VLC_SUCCESS;
1597 /*****************************************************************************
1598 * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1599 *****************************************************************************/
1600 static int ProbeDVD( const char *psz_name )
1602 if( !*psz_name )
1603 /* Triggers libdvdcss autodetection */
1604 return VLC_SUCCESS;
1606 int fd = vlc_open( psz_name, O_RDONLY | O_NONBLOCK );
1607 if( fd == -1 )
1608 #ifdef HAVE_FDOPENDIR
1609 return VLC_EGENERIC;
1610 #else
1611 return (errno == ENOENT) ? VLC_EGENERIC : VLC_SUCCESS;
1612 #endif
1614 int ret = VLC_EGENERIC;
1615 struct stat stat_info;
1617 if( fstat( fd, &stat_info ) == -1 )
1618 goto bailout;
1619 if( !S_ISREG( stat_info.st_mode ) )
1621 if( S_ISDIR( stat_info.st_mode ) || S_ISBLK( stat_info.st_mode ) )
1622 ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
1623 goto bailout;
1626 /* ISO 9660 volume descriptor */
1627 char iso_dsc[6];
1628 if( lseek( fd, 0x8000 + 1, SEEK_SET ) == -1
1629 || read( fd, iso_dsc, sizeof (iso_dsc) ) < (int)sizeof (iso_dsc)
1630 || memcmp( iso_dsc, "CD001\x01", 6 ) )
1631 goto bailout;
1633 /* Try to find the anchor (2 bytes at LBA 256) */
1634 uint16_t anchor;
1636 if( lseek( fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
1637 && read( fd, &anchor, 2 ) == 2
1638 && GetWLE( &anchor ) == 2 )
1639 ret = VLC_SUCCESS; /* Found a potential anchor */
1640 bailout:
1641 vlc_close( fd );
1642 return ret;