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