]> git.sesse.net Git - vlc/blob - modules/access/dvdnav.c
bb48a8c6beff3691dbe6ee1318e4dfd1a66db612
[vlc] / modules / access / dvdnav.c
1 /*****************************************************************************
2  * dvdnav.c: DVD module using the dvdnav library.
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #ifdef HAVE_UNISTD_H
33 #   include <unistd.h>
34 #endif
35 #ifdef HAVE_SYS_TYPES_H
36 #   include <sys/types.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #   include <sys/stat.h>
40 #endif
41 #ifdef HAVE_FCNTL_H
42 #   include <fcntl.h>
43 #endif
44
45 #include "vlc_keys.h"
46 #include "iso_lang.h"
47
48 #include <dvdnav/dvdnav.h>
49
50 #include "../demux/ps.h"
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 #define CACHING_TEXT N_("Caching value in ms")
56 #define CACHING_LONGTEXT N_( \
57     "Allows you to modify the default caching value for DVDnav streams. This "\
58     "value should be set in millisecond units." )
59
60 static int  Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
62
63 vlc_module_begin();
64     set_description( _("DVDnav Input") );
65     add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
66         CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
67     set_capability( "access_demux", 5 );
68     add_shortcut( "dvd" );
69     add_shortcut( "dvdnav" );
70     set_callbacks( Open, Close );
71 vlc_module_end();
72
73 /* Shall we use libdvdnav's read ahead cache? */
74 #define DVD_READ_CACHE 1
75
76 /*****************************************************************************
77  * Local prototypes
78  *****************************************************************************/
79 static char *ParseCL( vlc_object_t *, char *, vlc_bool_t, int *, int *, int *);
80
81 typedef struct
82 {
83     VLC_COMMON_MEMBERS
84
85     demux_t        *p_demux;
86     vlc_mutex_t     lock;
87
88     vlc_bool_t      b_moved;
89     vlc_bool_t      b_clicked;
90     vlc_bool_t      b_key;
91
92     vlc_bool_t      b_still;
93     int64_t         i_still_end;
94
95 } event_thread_t;
96
97 static int EventThread( vlc_object_t * );
98
99 struct demux_sys_t
100 {
101     dvdnav_t    *dvdnav;
102
103     /* track */
104     ps_track_t  tk[PS_TK_COUNT];
105     int         i_mux_rate;
106
107     /* for spu variables */
108     input_thread_t *p_input;
109
110     /* event */
111     event_thread_t *p_ev;
112
113     /* FIXME */
114     uint8_t     alpha[4];
115     uint32_t    clut[16];
116
117     /* */
118     int i_aspect;
119
120     int           i_title;
121     input_title_t **title;
122 };
123
124 static int Control( demux_t *, int, va_list );
125 static int Demux( demux_t * );
126 static int DemuxBlock( demux_t *, uint8_t *, int );
127
128 static void DemuxTitles( demux_t * );
129 static void ESSubtitleUpdate( demux_t * );
130 static void ButtonUpdate( demux_t * );
131
132 static void ESNew( demux_t *, int );
133 static int ProbeDVD( demux_t *, char * );
134
135 /*****************************************************************************
136  * DemuxOpen:
137  *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
139 {
140     demux_t     *p_demux = (demux_t*)p_this;
141     demux_sys_t *p_sys;
142     dvdnav_t    *p_dvdnav;
143     int         i_title, i_chapter, i_angle;
144     char        *psz_name;
145
146     psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
147                         &i_title, &i_chapter, &i_angle );
148     if( !psz_name )
149     {
150         return VLC_EGENERIC;
151     }
152
153     /* Try some simple probing to avoid going through dvdnav_open too often */
154     if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
155     {
156         free( psz_name );
157         return VLC_EGENERIC;
158     }
159
160     msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
161              psz_name, i_title, i_chapter, i_angle );
162
163     /* Open dvdnav */
164     if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
165     {
166         msg_Warn( p_demux, "cannot open dvdnav" );
167         free( psz_name );
168         return VLC_EGENERIC;
169     }
170     free( psz_name );
171
172     /* Fill p_demux field */
173     p_demux->pf_demux = Demux;
174     p_demux->pf_control = Control;
175     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
176     memset( p_sys, 0, sizeof( demux_sys_t ) );
177     p_sys->dvdnav = p_dvdnav;
178
179     ps_track_init( p_sys->tk );
180     p_sys->i_aspect = -1;
181     p_sys->i_mux_rate = 0;
182
183     if( 1 )
184     {
185         // Hack for libdvdnav CVS.
186         // Without it dvdnav_get_number_of_titles() fails.
187         // Remove when fixed in libdvdnav CVS.
188         uint8_t buffer[DVD_VIDEO_LB_LEN];
189         int i_event, i_len;
190
191         if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
192               == DVDNAV_STATUS_ERR )
193         {
194             msg_Warn( p_demux, "dvdnav_get_next_block failed" );
195         }
196
197         dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
198     }
199
200     /* Configure dvdnav */
201     if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
202           DVDNAV_STATUS_OK )
203     {
204         msg_Warn( p_demux, "cannot set read-a-head flag" );
205     }
206
207     if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
208           DVDNAV_STATUS_OK )
209     {
210         msg_Warn( p_demux, "cannot set PGC positioning flag" );
211     }
212
213     if( dvdnav_menu_language_select (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
214         dvdnav_audio_language_select(p_sys->dvdnav,"en") != DVDNAV_STATUS_OK ||
215         dvdnav_spu_language_select  (p_sys->dvdnav,"en") != DVDNAV_STATUS_OK )
216     {
217         msg_Warn( p_demux, "something failed while setting en language (%s)",
218                   dvdnav_err_to_string( p_sys->dvdnav ) );
219     }
220
221     DemuxTitles( p_demux );
222
223     /* Set forced title/chapter */
224     if( i_title != 0 )
225     {
226         if( dvdnav_title_play( p_sys->dvdnav, i_title ) != DVDNAV_STATUS_OK )
227         {
228             msg_Warn( p_demux, "cannot set title" );
229             i_title = 0;
230         }
231         else
232         {
233             p_demux->info.i_update |= INPUT_UPDATE_TITLE;
234             p_demux->info.i_title = i_title;
235         }
236     }
237
238     if( i_chapter != 1 && i_title != 0 )
239     {
240         if( dvdnav_part_play( p_sys->dvdnav, i_title, i_chapter ) !=
241             DVDNAV_STATUS_OK )
242         {
243             msg_Warn( p_demux, "cannot set chapter" );
244             i_chapter = 1;
245         }
246         else
247         {
248             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
249             p_demux->info.i_seekpoint = i_chapter;
250         }
251     }
252
253     /* Update default_pts to a suitable value for dvdnav access */
254     var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
255
256     /* FIXME hack hack hack hack FIXME */
257     /* Get p_input and create variable */
258     p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
259     var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
260     var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
261     var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
262     var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
263     var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
264     var_Create( p_sys->p_input, "contrast", VLC_VAR_ADDRESS );
265     var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
266     var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
267
268     /* Now create our event thread catcher */
269     p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
270     p_sys->p_ev->p_demux = p_demux;
271     vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
272                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
273
274     return VLC_SUCCESS;
275 }
276
277 /*****************************************************************************
278  * Close:
279  *****************************************************************************/
280 static void Close( vlc_object_t *p_this )
281 {
282     demux_t     *p_demux = (demux_t*)p_this;
283     demux_sys_t *p_sys = p_demux->p_sys;
284     int i;
285
286     /* stop the event handler */
287     p_sys->p_ev->b_die = VLC_TRUE;
288     vlc_thread_join( p_sys->p_ev );
289     vlc_object_destroy( p_sys->p_ev );
290
291     var_Destroy( p_sys->p_input, "highlight-mutex" );
292     var_Destroy( p_sys->p_input, "highlight" );
293     var_Destroy( p_sys->p_input, "x-start" );
294     var_Destroy( p_sys->p_input, "x-end" );
295     var_Destroy( p_sys->p_input, "y-start" );
296     var_Destroy( p_sys->p_input, "y-end" );
297     var_Destroy( p_sys->p_input, "color" );
298     var_Destroy( p_sys->p_input, "contrast" );
299
300     vlc_object_release( p_sys->p_input );
301
302     for( i = 0; i < PS_TK_COUNT; i++ )
303     {
304         ps_track_t *tk = &p_sys->tk[i];
305         if( tk->b_seen )
306         {
307             es_format_Clean( &tk->fmt );
308             if( tk->es ) es_out_Del( p_demux->out, tk->es );
309         }
310     }
311
312     dvdnav_close( p_sys->dvdnav );
313     free( p_sys );
314 }
315
316 /*****************************************************************************
317  * Control:
318  *****************************************************************************/
319 static int Control( demux_t *p_demux, int i_query, va_list args )
320 {
321     demux_sys_t *p_sys = p_demux->p_sys;
322     double f, *pf;
323     vlc_bool_t *pb;
324     int64_t *pi64;
325     input_title_t ***ppp_title;
326     int          *pi_int;
327     int i;
328
329     switch( i_query )
330     {
331         case DEMUX_SET_POSITION:
332         case DEMUX_GET_POSITION:
333         case DEMUX_GET_TIME:
334         case DEMUX_GET_LENGTH:
335         {
336             uint32_t pos, len;
337             if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
338                   DVDNAV_STATUS_OK || len == 0 )
339             {
340                 return VLC_EGENERIC;
341             }
342
343             if( i_query == DEMUX_GET_POSITION )
344             {
345                 pf = (double*)va_arg( args, double* );
346                 *pf = (double)pos / (double)len;
347                 return VLC_SUCCESS;
348             }
349             else if( i_query == DEMUX_SET_POSITION )
350             {
351                 f = (double)va_arg( args, double );
352                 pos = f * len;
353                 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
354                       DVDNAV_STATUS_OK )
355                 {
356                     return VLC_SUCCESS;
357                 }
358             }
359             else if( i_query == DEMUX_GET_TIME )
360             {
361                 pi64 = (int64_t*)va_arg( args, int64_t * );
362                 if( p_sys->i_mux_rate > 0 )
363                 {
364                     *pi64 = (int64_t)1000000 * 2048 * pos / 50 /
365                         p_sys->i_mux_rate;
366                     return VLC_SUCCESS;
367                 }
368             }
369             else if( i_query == DEMUX_GET_LENGTH )
370             {
371                 pi64 = (int64_t*)va_arg( args, int64_t * );
372                 if( p_sys->i_mux_rate > 0 )
373                 {
374                     *pi64 = (int64_t)1000000 * len * 2048 / 50 /
375                         p_sys->i_mux_rate;
376                     return VLC_SUCCESS;
377                 }
378             }
379
380             return VLC_EGENERIC;
381         }
382
383         /* Special for access_demux */
384         case DEMUX_CAN_PAUSE:
385         case DEMUX_CAN_CONTROL_PACE:
386             /* TODO */
387             pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
388             *pb = VLC_TRUE;
389             return VLC_SUCCESS;
390
391         case DEMUX_SET_PAUSE_STATE:
392             return VLC_SUCCESS;
393
394         case DEMUX_GET_TITLE_INFO:
395             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
396             pi_int    = (int*)va_arg( args, int* );
397
398             /* Duplicate title infos */
399             *pi_int = p_sys->i_title;
400             *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
401             for( i = 0; i < p_sys->i_title; i++ )
402             {
403                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
404             }
405             return VLC_SUCCESS;
406
407         case DEMUX_SET_TITLE:
408             i = (int)va_arg( args, int );
409             if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
410                   != DVDNAV_STATUS_OK ) ||
411                 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
412                   != DVDNAV_STATUS_OK ) )
413             {
414                 msg_Warn( p_demux, "cannot set title/chapter" );
415                 return VLC_EGENERIC;
416             }
417             p_demux->info.i_update |=
418                 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
419             p_demux->info.i_title = i;
420             p_demux->info.i_seekpoint = 0;
421             return VLC_SUCCESS;
422
423         case DEMUX_SET_SEEKPOINT:
424             i = (int)va_arg( args, int );
425             if( p_demux->info.i_title == 0 )
426             {
427                 int i_ret;
428                 /* Special case */
429                 switch( i )
430                 {
431                 case 0:
432                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
433                     break;
434                 case 1:
435                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
436                     break;
437                 case 2:
438                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
439                     break;
440                 case 3:
441                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
442                     break;
443                 case 4:
444                     i_ret = dvdnav_menu_call( p_sys->dvdnav,
445                                               DVD_MENU_Subpicture );
446                     break;
447                 case 5:
448                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
449                     break;
450                 case 6:
451                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
452                     break;
453                 default:
454                     return VLC_EGENERIC;
455                 }
456
457                 if( i_ret != DVDNAV_STATUS_OK )
458                     return VLC_EGENERIC;
459             }
460             else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
461                                        i + 1 ) != DVDNAV_STATUS_OK )
462             {
463                 msg_Warn( p_demux, "cannot set title/chapter" );
464                 return VLC_EGENERIC;
465             }
466             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
467             p_demux->info.i_seekpoint = i;
468             return VLC_SUCCESS;
469
470         case DEMUX_GET_PTS_DELAY:
471             pi64 = (int64_t*)va_arg( args, int64_t * );
472             *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
473             return VLC_SUCCESS;
474
475         /* TODO implement others */
476         default:
477             return VLC_EGENERIC;
478     }
479 }
480
481 /*****************************************************************************
482  * Demux:
483  *****************************************************************************/
484 static int Demux( demux_t *p_demux )
485 {
486     demux_sys_t *p_sys = p_demux->p_sys;
487
488     uint8_t buffer[DVD_VIDEO_LB_LEN];
489     uint8_t *packet = buffer;
490     int i_event;
491     int i_len;
492
493 #if DVD_READ_CACHE
494     if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
495         == DVDNAV_STATUS_ERR )
496 #else
497     if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
498         == DVDNAV_STATUS_ERR )
499 #endif
500     {
501         msg_Warn( p_demux, "cannot get next block (%s)",
502                   dvdnav_err_to_string( p_sys->dvdnav ) );
503         return -1;
504     }
505
506     switch( i_event )
507     {
508     case DVDNAV_BLOCK_OK:   /* mpeg block */
509         DemuxBlock( p_demux, packet, i_len );
510         break;
511
512     case DVDNAV_NOP:    /* Nothing */
513         msg_Dbg( p_demux, "DVDNAV_NOP" );
514         break;
515
516     case DVDNAV_STILL_FRAME:
517     {
518         dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
519         vlc_mutex_lock( &p_sys->p_ev->lock );
520         if( !p_sys->p_ev->b_still )
521         {
522             msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
523             msg_Dbg( p_demux, "     - length=0x%x", event->length );
524             p_sys->p_ev->b_still = VLC_TRUE;
525             if( event->length == 0xff )
526             {
527                 p_sys->p_ev->i_still_end = 0;
528             }
529             else
530             {
531                 p_sys->p_ev->i_still_end = (int64_t)event->length *
532                     1000000 + mdate() + p_sys->p_input->i_pts_delay;
533             }
534         }
535         vlc_mutex_unlock( &p_sys->p_ev->lock );
536         msleep( 40000 );
537         break;
538     }
539     case DVDNAV_SPU_STREAM_CHANGE:
540     {
541         dvdnav_spu_stream_change_event_t *event =
542             (dvdnav_spu_stream_change_event_t*)packet;
543         msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
544         msg_Dbg( p_demux, "     - physical_wide=%d",
545                  event->physical_wide );
546         msg_Dbg( p_demux, "     - physical_letterbox=%d",
547                  event->physical_letterbox);
548         msg_Dbg( p_demux, "     - physical_pan_scan=%d",
549                  event->physical_pan_scan );
550
551         ESSubtitleUpdate( p_demux );
552         break;
553     }
554     case DVDNAV_AUDIO_STREAM_CHANGE:
555     {
556         dvdnav_audio_stream_change_event_t *event =
557             (dvdnav_audio_stream_change_event_t*)packet;
558         msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
559         msg_Dbg( p_demux, "     - physical=%d", event->physical );
560         /* TODO */
561         break;
562     }
563     case DVDNAV_VTS_CHANGE:
564     {
565         int32_t i_title = 0;
566         int32_t i_part  = 0;
567         int i;
568
569         dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
570         msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
571         msg_Dbg( p_demux, "     - vtsN=%d", event->new_vtsN );
572         msg_Dbg( p_demux, "     - domain=%d", event->new_domain );
573
574         /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
575         /* TODO check if we always have VTS and CELL */
576         p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
577
578         /* reset PCR */
579         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
580
581         for( i = 0; i < PS_TK_COUNT; i++ )
582         {
583             ps_track_t *tk = &p_sys->tk[i];
584             if( tk->b_seen )
585             {
586                 es_format_Clean( &tk->fmt );
587                 if( tk->es ) es_out_Del( p_demux->out, tk->es );
588             }
589             tk->b_seen = VLC_FALSE;
590         }
591
592         if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
593                                        &i_part ) == DVDNAV_STATUS_OK )
594         {
595             if( i_title >= 0 && i_title < p_sys->i_title &&
596                 p_demux->info.i_title != i_title )
597             {
598                 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
599                 p_demux->info.i_title = i_title;
600             }
601         }
602         break;
603     }
604     case DVDNAV_CELL_CHANGE:
605     {
606         int32_t i_title = 0;
607         int32_t i_part  = 0;
608
609         dvdnav_cell_change_event_t *event =
610             (dvdnav_cell_change_event_t*)packet;
611         msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
612         msg_Dbg( p_demux, "     - cellN=%d", event->cellN );
613         msg_Dbg( p_demux, "     - pgN=%d", event->pgN );
614         msg_Dbg( p_demux, "     - cell_length=%lld", event->cell_length );
615         msg_Dbg( p_demux, "     - pg_length=%lld", event->pg_length );
616         msg_Dbg( p_demux, "     - pgc_length=%lld", event->pgc_length );
617         msg_Dbg( p_demux, "     - cell_start=%lld", event->cell_start );
618         msg_Dbg( p_demux, "     - pg_start=%lld", event->pg_start );
619
620         /* FIXME is it correct or there is better way to know chapter change */
621         if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
622                                        &i_part ) == DVDNAV_STATUS_OK )
623         {
624             if( i_title >= 0 && i_title < p_sys->i_title &&
625                 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
626                 p_demux->info.i_seekpoint != i_part - 1 )
627             {
628                 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
629                 p_demux->info.i_seekpoint = i_part - 1;
630             }
631         }
632         break;
633     }
634
635     case DVDNAV_NAV_PACKET:
636     {
637 #ifdef DVDNAV_DEBUG
638         msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
639 #endif
640         /* A lot of thing to do here :
641          *  - handle packet
642          *  - fetch pts (for time display)
643          *  - ...
644          */
645         DemuxBlock( p_demux, packet, i_len );
646         break;
647     }
648     case DVDNAV_STOP:   /* EOF */
649         msg_Dbg( p_demux, "DVDNAV_STOP" );
650         return 0;
651
652     case DVDNAV_HIGHLIGHT:
653     {
654         dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
655         msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
656         msg_Dbg( p_demux, "     - display=%d", event->display );
657         msg_Dbg( p_demux, "     - buttonN=%d", event->buttonN );
658         ButtonUpdate( p_demux );
659         break;
660     }
661
662     case DVDNAV_SPU_CLUT_CHANGE:
663     {
664         int i;
665
666         msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
667         /* Update color lookup table (16 *uint32_t in packet) */
668         memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
669
670         /* HACK to get the SPU tracks registered in the right order */
671         for( i = 0; i < 0x1f; i++ )
672         {
673             if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
674                 ESNew( p_demux, 0xbd20 + i );
675         }
676         /* END HACK */
677         break;
678     }
679
680     case DVDNAV_HOP_CHANNEL:
681         msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
682         /* We should try to flush all our internal buffer */
683         break;
684
685     case DVDNAV_WAIT:
686         msg_Dbg( p_demux, "DVDNAV_WAIT" );
687
688         dvdnav_wait_skip( p_sys->dvdnav );
689         break;
690
691     default:
692         msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
693         break;
694     }
695
696 #if DVD_READ_CACHE
697     dvdnav_free_cache_block( p_sys->dvdnav, packet );
698 #endif
699
700     return 1;
701 }
702
703 /*****************************************************************************
704  * ParseCL: parse command line.
705  * Titles start from 0 (menu), chapters and angles start from 1.
706  *****************************************************************************/
707 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
708                       int *i_title, int *i_chapter, int *i_angle )
709 {
710     char *psz_parser, *psz_source, *psz_next;
711
712     psz_source = strdup( psz_name );
713     if( psz_source == NULL ) return NULL;
714
715     *i_title = 0;
716     *i_chapter = 1;
717     *i_angle = 1;
718
719     /* Start with the end, because you could have :
720      * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
721      * (yes, this is kludgy). */
722     for( psz_parser = psz_source + strlen(psz_source) - 1;
723          psz_parser >= psz_source && *psz_parser != '@';
724          psz_parser-- );
725
726     if( psz_parser >= psz_source && *psz_parser == '@' )
727     {
728         /* Found options */
729         *psz_parser = '\0';
730         ++psz_parser;
731
732         *i_title = (int)strtol( psz_parser, &psz_next, 10 );
733         if( *psz_next )
734         {
735             psz_parser = psz_next + 1;
736             *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
737             if( *psz_next )
738             {
739                 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
740             }
741         }
742     }
743
744     *i_title   = *i_title >= 0 ? *i_title : 0;
745     *i_chapter = *i_chapter > 0 ? *i_chapter : 1;
746     *i_angle   = *i_angle > 0 ? *i_angle : 1;
747
748     if( !*psz_source )
749     {
750         free( psz_source );
751         if( !b_force )
752         {
753             return NULL;
754         }
755         psz_source = config_GetPsz( p_this, "dvd" );
756         if( !psz_source ) return NULL;
757     }
758
759 #ifdef WIN32
760     if( psz_source[0] && psz_source[1] == ':' &&
761         psz_source[2] == '\\' && psz_source[3] == '\0' )
762     {
763         psz_source[2] = '\0';
764     }
765 #endif
766
767     return psz_source;
768 }
769
770 static void DemuxTitles( demux_t *p_demux )
771 {
772     demux_sys_t *p_sys = p_demux->p_sys;
773     input_title_t *t;
774     seekpoint_t *s;
775     int32_t i_titles;
776     int i;
777
778     /* Menu */
779     t = vlc_input_title_New();
780     t->b_menu = VLC_TRUE;
781     t->psz_name = strdup( "DVD Menu" );
782
783     s = vlc_seekpoint_New();
784     s->psz_name = strdup( "Resume" );
785     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
786
787     s = vlc_seekpoint_New();
788     s->psz_name = strdup( "Root" );
789     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
790
791     s = vlc_seekpoint_New();
792     s->psz_name = strdup( "Title" );
793     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
794
795     s = vlc_seekpoint_New();
796     s->psz_name = strdup( "Chapter" );
797     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
798
799     s = vlc_seekpoint_New();
800     s->psz_name = strdup( "Subtitle" );
801     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
802
803     s = vlc_seekpoint_New();
804     s->psz_name = strdup( "Audio" );
805     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
806
807     s = vlc_seekpoint_New();
808     s->psz_name = strdup( "Angle" );
809     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
810
811     TAB_APPEND( p_sys->i_title, p_sys->title, t );
812
813     /* Find out number of titles/chapters */
814     dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
815     for( i = 1; i <= i_titles; i++ )
816     {
817         int32_t i_chapters = 0;
818         int j;
819
820         dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
821
822         t = vlc_input_title_New();
823         for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
824         {
825             s = vlc_seekpoint_New();
826             s->psz_name = malloc( strlen( _("Chapter %i") ) + 20 );
827             sprintf( s->psz_name, _("Chapter %i"), j + 1 );
828             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
829         }
830
831         TAB_APPEND( p_sys->i_title, p_sys->title, t );
832     }
833 }
834
835 /*****************************************************************************
836  * Update functions:
837  *****************************************************************************/
838 static void ButtonUpdate( demux_t *p_demux )
839 {
840     demux_sys_t *p_sys = p_demux->p_sys;
841     vlc_value_t val;
842     int32_t i_title, i_part;
843
844     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
845
846     if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
847     {
848         vlc_mutex_t *p_mutex = val.p_address;
849         dvdnav_highlight_area_t hl;
850         int32_t i_button;
851
852         if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
853             != DVDNAV_STATUS_OK )
854         {
855             msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
856             return;
857         }
858
859         if( i_button > 0 && i_title ==  0 )
860         {
861             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
862
863             dvdnav_get_highlight_area( pci, i_button, 1, &hl );
864
865             /* I fear it is plain wrong */
866             //val.p_address = (void *)&hl.palette;
867             p_sys->alpha[0] = hl.palette&0x0f;
868             p_sys->alpha[1] = (hl.palette>>4)&0x0f;
869             p_sys->alpha[2] = (hl.palette>>8)&0x0f;
870             p_sys->alpha[3] = (hl.palette>>12)&0x0f;
871
872             vlc_mutex_lock( p_mutex );
873             val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
874             val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
875             val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
876             val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
877
878             val.p_address = (void *)p_sys->alpha;
879             var_Set( p_sys->p_input, "contrast", val );
880
881             val.b_bool = VLC_TRUE; var_Set( p_sys->p_input, "highlight", val );
882             vlc_mutex_unlock( p_mutex );
883
884             msg_Dbg( p_demux, "buttonUpdate %d", i_button );
885         }
886         else
887         {
888             msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
889                      i_button, i_title );
890
891             /* Show all */
892             vlc_mutex_lock( p_mutex );
893             val.b_bool = VLC_FALSE;
894             var_Set( p_sys->p_input, "highlight", val );
895             vlc_mutex_unlock( p_mutex );
896         }
897     }
898 }
899
900 static void ESSubtitleUpdate( demux_t *p_demux )
901 {
902     demux_sys_t *p_sys = p_demux->p_sys;
903     int         i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
904     int32_t i_title, i_part;
905
906     ButtonUpdate( p_demux );
907
908     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
909     if( i_title > 0 )
910     {
911         return;
912     }
913
914     if( i_spu >= 0 && i_spu <= 0x1f )
915     {
916         ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
917
918         if( !tk->b_seen )
919         {
920             ESNew( p_demux, 0xbd20 + i_spu);
921         }
922         /* be sure to unselect it (reset) */
923         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
924                         (vlc_bool_t)VLC_FALSE );
925
926         /* now select it */
927         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
928     }
929     else
930     {
931         for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
932         {
933             ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
934             if( tk->b_seen )
935             {
936                 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
937                                 (vlc_bool_t)VLC_FALSE );
938             }
939         }
940     }
941 }
942
943 /*****************************************************************************
944  * DemuxBlock: demux a given block
945  *****************************************************************************/
946 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
947 {
948     demux_sys_t *p_sys = p_demux->p_sys;
949     uint8_t     *p = pkt;
950
951     while( p < &pkt[i_pkt] )
952     {
953         int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
954         block_t *p_pkt;
955         if( i_size <= 0 )
956         {
957             break;
958         }
959
960         /* Create a block */
961         p_pkt = block_New( p_demux, i_size );
962         memcpy( p_pkt->p_buffer, p, i_size);
963
964         /* Parse it and send it */
965         switch( 0x100 | p[3] )
966         {
967         case 0x1b9:
968         case 0x1bb:
969         case 0x1bc:
970 #ifdef DVDNAV_DEBUG
971             if( p[3] == 0xbc )
972             {
973                 msg_Warn( p_demux, "received a PSM packet" );
974             }
975             else if( p[3] == 0xbb )
976             {
977                 msg_Warn( p_demux, "received a SYSTEM packet" );
978             }
979 #endif
980             block_Release( p_pkt );
981             break;
982
983         case 0x1ba:
984         {
985             int64_t i_scr;
986             int i_mux_rate;
987             if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
988             {
989                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
990                 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
991             }
992             block_Release( p_pkt );
993             break;
994         }
995         default:
996         {
997             int i_id = ps_pkt_id( p_pkt );
998             if( i_id >= 0xc0 )
999             {
1000                 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1001
1002                 if( !tk->b_seen )
1003                 {
1004                     ESNew( p_demux, i_id );
1005                 }
1006                 if( tk->b_seen && tk->es &&
1007                     !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1008                 {
1009                     es_out_Send( p_demux->out, tk->es, p_pkt );
1010                 }
1011                 else
1012                 {
1013                     block_Release( p_pkt );
1014                 }
1015             }
1016             else
1017             {
1018                 block_Release( p_pkt );
1019             }
1020             break;
1021         }
1022         }
1023
1024         p += i_size;
1025     }
1026
1027     return VLC_SUCCESS;
1028 }
1029
1030 /*****************************************************************************
1031  * ESNew: register a new elementary stream
1032  *****************************************************************************/
1033 static void ESNew( demux_t *p_demux, int i_id )
1034 {
1035     demux_sys_t *p_sys = p_demux->p_sys;
1036     ps_track_t  *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1037     vlc_bool_t  b_select = VLC_FALSE;
1038
1039     if( tk->b_seen )
1040     {
1041         return;
1042     }
1043
1044     if( ps_track_fill( tk, i_id ) )
1045     {
1046         msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1047         return;
1048     }
1049
1050     /* Add a new ES */
1051     if( tk->fmt.i_cat == VIDEO_ES )
1052     {
1053         if( p_sys->i_aspect >= 0 )
1054         {
1055             tk->fmt.video.i_aspect = p_sys->i_aspect;
1056         }
1057         b_select = VLC_TRUE;
1058     }
1059     else if( tk->fmt.i_cat == AUDIO_ES )
1060     {
1061         int i_audio = -1;
1062         /* find the audio number PLEASE find another way */
1063         if( (i_id&0xbdf8) == 0xbd88 )       /* dts */
1064         {
1065             i_audio = i_id&0x07;
1066         }
1067         else if( (i_id&0xbdf0) == 0xbd80 )  /* a52 */
1068         {
1069             i_audio = i_id&0xf;
1070         }
1071         else if( (i_id&0xbdf0) == 0xbda0 )  /* lpcm */
1072         {
1073             i_audio = i_id&0x1f;
1074         }
1075         else if( ( i_id&0xe0 ) == 0xc0 )    /* mpga */
1076         {
1077             i_audio = i_id&0x1f;
1078         }
1079         if( i_audio >= 0 )
1080         {
1081             int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1082             if( i_lang != 0xffff )
1083             {
1084                 tk->fmt.psz_language = malloc( 3 );
1085                 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1086                 tk->fmt.psz_language[1] = (i_lang     )&0xff;
1087                 tk->fmt.psz_language[2] = 0;
1088             }
1089             if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1090             {
1091                 b_select = VLC_TRUE;
1092             }
1093         }
1094     }
1095     else if( tk->fmt.i_cat == SPU_ES )
1096     {
1097         int32_t i_title, i_part;
1098         int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1099         if( i_lang != 0xffff )
1100         {
1101             tk->fmt.psz_language = malloc( 3 );
1102             tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1103             tk->fmt.psz_language[1] = (i_lang     )&0xff;
1104             tk->fmt.psz_language[2] = 0;
1105         }
1106
1107         /* Palette */
1108         tk->fmt.subs.spu.palette[0] = 0xBeef;
1109         memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1110                 16 * sizeof( uint32_t ) );
1111
1112         /* We select only when we are not in the menu */
1113         dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1114         if( i_title > 0 &&
1115             dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1116         {
1117             b_select = VLC_TRUE;
1118         }
1119     }
1120
1121     tk->es = es_out_Add( p_demux->out, &tk->fmt );
1122     if( b_select )
1123     {
1124         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1125     }
1126     tk->b_seen = VLC_TRUE;
1127 }
1128
1129 /*****************************************************************************
1130  * Event handler code
1131  *****************************************************************************/
1132 static int  EventMouse( vlc_object_t *, char const *,
1133                         vlc_value_t, vlc_value_t, void * );
1134 static int  EventKey  ( vlc_object_t *, char const *,
1135                         vlc_value_t, vlc_value_t, void * );
1136
1137 static int EventThread( vlc_object_t *p_this )
1138 {
1139     event_thread_t *p_ev = (event_thread_t*)p_this;
1140     demux_sys_t    *p_sys = p_ev->p_demux->p_sys;
1141     vlc_object_t   *p_vout = NULL;
1142
1143     vlc_mutex_init( p_ev, &p_ev->lock );
1144     p_ev->b_moved   = VLC_FALSE;
1145     p_ev->b_clicked = VLC_FALSE;
1146     p_ev->b_key     = VLC_FALSE;
1147     p_ev->b_still   = VLC_FALSE;
1148
1149     /* catch all key event */
1150     var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1151
1152     /* main loop */
1153     while( !p_ev->b_die )
1154     {
1155         vlc_bool_t b_activated = VLC_FALSE;
1156
1157         /* KEY part */
1158         if( p_ev->b_key )
1159         {
1160             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1161
1162             vlc_value_t valk;
1163             struct hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
1164             int i, i_action = -1;
1165
1166             vlc_mutex_lock( &p_ev->lock );
1167             var_Get( p_ev->p_vlc, "key-pressed", &valk );
1168             for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
1169             {
1170                 if( p_hotkeys[i].i_key == valk.i_int )
1171                 {
1172                     i_action = p_hotkeys[i].i_action;
1173                 }
1174             }
1175
1176             switch( i_action )
1177             {
1178             case ACTIONID_NAV_LEFT:
1179                 dvdnav_left_button_select( p_sys->dvdnav, pci );
1180                 break;
1181             case ACTIONID_NAV_RIGHT:
1182                 dvdnav_right_button_select( p_sys->dvdnav, pci );
1183                 break;
1184             case ACTIONID_NAV_UP:
1185                 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1186                 break;
1187             case ACTIONID_NAV_DOWN:
1188                 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1189                 break;
1190             case ACTIONID_NAV_ACTIVATE:
1191                 b_activated = VLC_TRUE;
1192                 dvdnav_button_activate( p_sys->dvdnav, pci );
1193                 break;
1194             default:
1195                 break;
1196             }
1197             p_ev->b_key = VLC_FALSE;
1198             vlc_mutex_unlock( &p_ev->lock );
1199         }
1200
1201         /* VOUT part */
1202         if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1203         {
1204             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1205             vlc_value_t valx, valy;
1206
1207             vlc_mutex_lock( &p_ev->lock );
1208             var_Get( p_vout, "mouse-x", &valx );
1209             var_Get( p_vout, "mouse-y", &valy );
1210
1211             if( p_ev->b_moved )
1212             {
1213                 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1214                                      valy.i_int );
1215             }
1216             if( p_ev->b_clicked )
1217             {
1218                 b_activated = VLC_TRUE;
1219                 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1220                                        valy.i_int );
1221             }
1222
1223             p_ev->b_moved = VLC_FALSE;
1224             p_ev->b_clicked = VLC_FALSE;
1225             vlc_mutex_unlock( &p_ev->lock );
1226         }
1227         if( p_vout && p_vout->b_die )
1228         {
1229             var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1230             var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1231             vlc_object_release( p_vout );
1232             p_vout = NULL;
1233         }
1234         if( p_vout == NULL )
1235         {
1236             p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1237                                       FIND_CHILD );
1238             if( p_vout)
1239             {
1240                 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1241                 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1242             }
1243         }
1244
1245         /* Still part */
1246         vlc_mutex_lock( &p_ev->lock );
1247         if( p_ev->b_still )
1248         {
1249             if( /* b_activated || // This breaks menus */
1250                 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1251             {
1252                 p_ev->b_still = VLC_FALSE;
1253                 dvdnav_still_skip( p_sys->dvdnav );
1254             }
1255         }
1256         vlc_mutex_unlock( &p_ev->lock );
1257
1258         /* Wait a bit */
1259         msleep( 10000 );
1260     }
1261
1262     /* Release callback */
1263     if( p_vout )
1264     {
1265         var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1266         var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1267         vlc_object_release( p_vout );
1268     }
1269     var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
1270
1271     vlc_mutex_destroy( &p_ev->lock );
1272
1273     return VLC_SUCCESS;
1274 }
1275
1276 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1277                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1278 {
1279     event_thread_t *p_ev = p_data;
1280     vlc_mutex_lock( &p_ev->lock );
1281     if( psz_var[6] == 'c' )
1282         p_ev->b_clicked = VLC_TRUE;
1283     else if( psz_var[6] == 'm' )
1284         p_ev->b_moved = VLC_TRUE;
1285     vlc_mutex_unlock( &p_ev->lock );
1286
1287     return VLC_SUCCESS;
1288 }
1289
1290 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1291                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1292 {
1293     event_thread_t *p_ev = p_data;
1294     vlc_mutex_lock( &p_ev->lock );
1295     p_ev->b_key = VLC_TRUE;
1296     vlc_mutex_unlock( &p_ev->lock );
1297
1298     return VLC_SUCCESS;
1299 }
1300
1301 /*****************************************************************************
1302  * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1303  *****************************************************************************/
1304 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1305 {
1306 #ifdef HAVE_SYS_STAT_H
1307     struct stat stat_info;
1308     uint8_t pi_anchor[2];
1309     uint16_t i_tag_id = 0;
1310     int i_fd, i_ret;
1311
1312     if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1313     {
1314         /* Let dvdnav_open() do the probing */
1315         return VLC_SUCCESS;
1316     }
1317
1318     if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1319     {
1320         /* Let dvdnav_open() do the probing */
1321         return VLC_SUCCESS;
1322     }
1323
1324     /* Try to find the anchor (2 bytes at LBA 256) */
1325     i_ret = VLC_SUCCESS;
1326     if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1327     {
1328         i_ret = VLC_EGENERIC;
1329     }
1330
1331     if( read( i_fd, pi_anchor, 2 ) == 2 )
1332     {
1333         i_tag_id = GetWLE(pi_anchor);
1334         if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1335     }
1336     else
1337     {
1338         i_ret = VLC_EGENERIC;
1339     }
1340
1341     close( i_fd );
1342
1343     return i_ret;
1344 #else
1345
1346     return VLC_SUCCESS;
1347 #endif
1348 }