]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
3a99b1f002b8d46692f56d2a968da344fba12d42
[vlc] / plugins / dvd / input_dvd.c
1 /* input_dvd.c: DVD raw reading plugin.
2  *****************************************************************************
3  * This plugins should handle all the known specificities of the DVD format,
4  * especially the 2048 bytes logical block size.
5  * It depends on:
6  *  -libdvdcss for access and unscrambling
7  *  -dvd_ifo for ifo parsing and analyse
8  *  -dvd_udf to find files
9  *****************************************************************************
10  * Copyright (C) 1998-2001 VideoLAN
11  * $Id: input_dvd.c,v 1.127 2002/03/04 01:53:56 stef Exp $
12  *
13  * Author: Stéphane Borel <stef@via.ecp.fr>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  * 
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
28  *****************************************************************************/
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <videolan/vlc.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 #include <fcntl.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <string.h>
47 #include <errno.h>
48
49 #ifdef STRNCASECMP_IN_STRINGS_H
50 #   include <strings.h>
51 #endif
52
53 #if defined( WIN32 )
54 #   include <io.h>                                                 /* read() */
55 #endif
56
57 #ifdef GOD_DAMN_DMCA
58 #   include "dummy_dvdcss.h"
59 #else
60 #   include <videolan/dvdcss.h>
61 #endif
62
63 #include "stream_control.h"
64 #include "input_ext-intf.h"
65 #include "input_ext-dec.h"
66 #include "input_ext-plugins.h"
67
68 #include "input_dvd.h"
69 #include "dvd_ifo.h"
70 #include "dvd_summary.h"
71 #include "iso_lang.h"
72
73 #include "debug.h"
74
75 /* how many packets DVDDemux will read in each loop */
76 #define DVD_READ_ONCE 64
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81
82 /* called from outside */
83 static int  DVDOpen         ( struct input_thread_s * );
84 static void DVDClose        ( struct input_thread_s * );
85 static int  DVDSetArea      ( struct input_thread_s *, struct input_area_s * );
86 static int  DVDSetProgram   ( struct input_thread_s *, pgrm_descriptor_t * );
87 static int  DVDRead         ( struct input_thread_s *, byte_t *, size_t );
88 static void DVDSeek         ( struct input_thread_s *, off_t );
89
90 static int  DVDRewind       ( struct input_thread_s * );
91 static int  DVDDemux        ( struct input_thread_s * );
92 static int  DVDInit         ( struct input_thread_s * );
93 static void DVDEnd          ( struct input_thread_s * );
94
95 /* called only inside */
96 static void DVDLaunchDecoders( input_thread_t * p_input );
97 static int  DVDChooseAngle( thread_dvd_data_t * );
98 static int  DVDFindCell( thread_dvd_data_t * );
99 static int  DVDFindSector( thread_dvd_data_t * );
100 static int  DVDChapterSelect( thread_dvd_data_t *, int );
101
102 /*****************************************************************************
103  * Functions exported as capabilities. They are declared as static so that
104  * we don't pollute the namespace too much.
105  *****************************************************************************/
106 void _M( access_getfunctions)( function_list_t * p_function_list )
107 {
108 #define input p_function_list->functions.access
109     input.pf_open             = DVDOpen;
110     input.pf_close            = DVDClose;
111     input.pf_read             = DVDRead;
112     input.pf_set_area         = DVDSetArea;
113     input.pf_set_program      = DVDSetProgram;
114     input.pf_seek             = DVDSeek;
115 #undef input
116 }
117
118 void _M( demux_getfunctions)( function_list_t * p_function_list )
119 {
120 #define demux p_function_list->functions.demux
121     demux.pf_init             = DVDInit;
122     demux.pf_end              = DVDEnd;
123     demux.pf_demux            = DVDDemux;
124     demux.pf_rewind           = DVDRewind;
125 #undef demux
126 }
127
128 /*
129  * Data demux functions
130  */
131
132 /*****************************************************************************
133  * DVDInit: initializes DVD structures
134  *****************************************************************************/
135 static int DVDInit( input_thread_t * p_input )
136 {
137
138     if( strncmp( p_input->p_access_module->psz_name, "dvd", 3 ) )
139     {
140         return -1;
141     }
142
143     vlc_mutex_lock( &p_input->stream.stream_lock );
144     
145     DVDLaunchDecoders( p_input );
146     
147     vlc_mutex_unlock( &p_input->stream.stream_lock );
148
149     return 0;
150 }
151
152 /*****************************************************************************
153  * DVDEnd: frees unused data
154  *****************************************************************************/
155 static void DVDEnd( input_thread_t * p_input )
156 {
157 }
158
159 /*****************************************************************************
160  * DVDDemux
161  *****************************************************************************/
162 #define PEEK( SIZE )                                                        \
163     i_result = input_Peek( p_input, &p_peek, SIZE );                        \
164     if( i_result == -1 )                                                    \
165     {                                                                       \
166         return( -1 );                                                       \
167     }                                                                       \
168     else if( i_result < SIZE )                                              \
169     {                                                                       \
170         /* EOF */                                                           \
171         return( 0 );                                                        \
172     }
173
174 static int DVDDemux( input_thread_t * p_input )
175 {
176     int                 i;
177     byte_t *            p_peek;
178     data_packet_t *     p_data;
179     ssize_t             i_result;
180     int                 i_packet_size;
181             
182
183     /* Read headers to compute payload length */
184     for( i = 0 ; i < DVD_READ_ONCE ; i++ )
185     {
186
187         /* Read what we believe to be a packet header. */
188         PEEK( 4 );
189             
190         /* Default header */
191         if( U32_AT( p_peek ) != 0x1BA )
192         {
193             /* That's the case for all packets, except pack header. */
194             i_packet_size = U16_AT( p_peek + 4 );
195         }
196         else
197         {
198             /* MPEG-2 Pack header. */
199             i_packet_size = 8;
200         }
201
202         /* Fetch a packet of the appropriate size. */
203         i_result = input_SplitBuffer( p_input, &p_data, i_packet_size + 6 );
204         if( i_result <= 0 )
205         {
206             return( i_result );
207         }
208
209         /* In MPEG-2 pack headers we still have to read stuffing bytes. */
210         if( (p_data->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
211         {
212             size_t i_stuffing = (p_data->p_demux_start[13] & 0x7);
213             /* Force refill of the input buffer - though we don't care
214              * about p_peek. Please note that this is unoptimized. */
215             PEEK( i_stuffing );
216             p_input->p_current_data += i_stuffing;
217         }
218
219         input_DemuxPS( p_input, p_data );
220                         
221     }
222     
223     return i;
224 }
225
226 /*****************************************************************************
227  * DVDRewind : reads a stream backward
228  *****************************************************************************/
229 static int DVDRewind( input_thread_t * p_input )
230 {
231     return( -1 );
232 }
233
234
235
236 /*
237  * Data access functions
238  */
239
240 #define PARSE( chr, action )                            \
241     psz_parser = p_input->psz_name;                     \
242     while( *(psz_parser) && *(psz_parser) != (chr) )    \
243     {                                                   \
244         (psz_parser)++;                                 \
245     }                                                   \
246                                                         \
247     if( *(psz_parser) == (chr) )                        \
248     {                                                   \
249         *(psz_parser) = '\0';                           \
250         (action);                                       \
251     }
252
253 /*****************************************************************************
254  * DVDOpen: open dvd
255  *****************************************************************************/
256 static int DVDOpen( struct input_thread_s *p_input )
257 {
258     struct stat          stat_info;
259     char *               psz_parser = p_input->psz_name;
260     char *               psz_device = p_input->psz_name;
261     char *               psz_raw;
262     char *               psz_next;
263     dvdcss_handle        dvdhandle;
264     thread_dvd_data_t *  p_dvd;
265     input_area_t *       p_area;
266     boolean_t            b_need_free = 0;
267     boolean_t            b_options = 0;
268     int                  i_title = 1;
269     int                  i_chapter = 1;
270     int                  i_angle = 1;
271     int                  i;
272
273     /* Parse input string :
274      * [device][@rawdevice][@[title][,[chapter][,angle]]] */
275     while( *psz_parser && *psz_parser != '@' )
276     {
277         psz_parser++;
278     }
279
280     if( *psz_parser == '@' )
281     {
282         /* Found raw device */
283         *psz_parser = '\0';
284         psz_raw = ++psz_parser;
285     }
286     else
287     {
288         psz_raw = NULL;
289     }
290
291     if( !strtol( psz_parser, NULL, 10 ) )
292     {
293         while( *psz_parser && *psz_parser != '@' )
294         {
295             psz_parser++;
296         }
297         
298         if( *psz_parser == '@' )
299         {
300             *psz_parser = '\0';
301             ++psz_parser;
302             b_options = 1;
303         }
304         else
305         {
306             psz_parser = psz_raw + 1;
307             for( i=0 ; i<3 ; i++ )
308             {
309                 if( !*psz_parser )
310                 {
311                     break;
312                 }
313                 if( strtol( psz_parser, NULL, 10 ) )
314                 {
315                     psz_parser = psz_raw;
316                     psz_raw = NULL;
317                     b_options = 1;
318                     break;
319                 }
320                 psz_parser++;
321             }
322         }
323     }
324     else
325     {
326         psz_raw = NULL;
327         b_options = 1;
328     }
329
330     if( b_options )
331     {
332         /* Found options */
333         i_title = (int)strtol( psz_parser, &psz_next, 10 );
334         if( *psz_next )
335         {
336             psz_parser = psz_next + 1;
337             i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
338             if( *psz_next )
339             {
340                 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
341             }
342         }
343
344         i_title = i_title ? i_title : 1;
345         i_chapter = i_chapter ? i_chapter : 1;
346         i_angle = i_angle ? i_angle : 1;
347     }
348
349     if( !*psz_device )
350     {
351         psz_device = config_GetPszVariable( INPUT_DVD_DEVICE_VAR );
352         b_need_free = 1;
353     }
354
355     if( stat( psz_device, &stat_info ) == -1 )
356     {
357         intf_ErrMsg( "input error: cannot stat() device `%s' (%s)",
358                      psz_device, strerror(errno));
359         return( -1 );                    
360     }
361     
362 #ifndef WIN32    
363     if( !S_ISBLK(stat_info.st_mode) && !S_ISCHR(stat_info.st_mode) )
364     {
365         intf_WarnMsg( 3, "input: DVD plugin discarded"
366                          " (not a valid block device)" );
367         return -1;
368     }
369 #endif
370     
371     if( psz_raw )
372     {
373         if( *psz_raw )
374         {
375             setenv( "DVDCSS_RAW_DEVICE", psz_raw, 1 );
376         }
377         else
378         {
379             psz_raw = NULL;
380         }
381     }
382
383     intf_WarnMsg( 2, "input: dvd=%s raw=%s title=%d chapter=%d angle=%d",
384                   psz_device, psz_raw, i_title, i_chapter, i_angle );
385     
386     /* 
387      * set up input
388      */ 
389     p_input->i_mtu = 0;
390
391     vlc_mutex_lock( &p_input->stream.stream_lock );
392
393     p_input->stream.i_method = INPUT_METHOD_DVD;
394
395     /* If we are here we can control the pace... */
396     p_input->stream.b_pace_control = 1;
397
398     p_input->stream.b_seekable = 1;
399     p_input->stream.p_selected_area->i_size = 0;
400
401     p_input->stream.p_selected_area->i_tell = 0;
402
403     vlc_mutex_unlock( &p_input->stream.stream_lock );
404
405     /*
406      *  get plugin ready
407      */ 
408     dvdhandle = dvdcss_open( psz_device );
409     
410     if( b_need_free )
411     {
412         free( psz_device );
413     }
414
415     if( dvdhandle == NULL )
416     {
417         intf_ErrMsg( "dvd error: dvdcss can't open device" );
418         return -1;
419     }
420
421     p_dvd = malloc( sizeof(thread_dvd_data_t) );
422     if( p_dvd == NULL )
423     {
424         intf_ErrMsg( "dvd error: out of memory" );
425         return -1;
426     }
427
428     p_dvd->dvdhandle = (dvdcss_handle) dvdhandle;
429     p_input->p_access_data = (void *)p_dvd;
430
431     p_dvd->i_title = i_title;
432     p_dvd->i_chapter = i_chapter;
433     p_dvd->i_angle = i_angle;
434
435     if( dvdcss_seek( p_dvd->dvdhandle, 0, DVDCSS_NOFLAGS ) < 0 )
436     {
437         intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
438         return -1;
439     }
440
441     /* Ifo allocation & initialisation */
442     if( IfoCreate( p_dvd ) < 0 )
443     {
444         intf_ErrMsg( "dvd error: allcation error in ifo" );
445         free( p_dvd );
446         return -1;
447     }
448
449     if( IfoInit( p_dvd->p_ifo ) < 0 )
450     {
451         intf_ErrMsg( "dvd error: fatal failure in ifo" );
452         IfoDestroy( p_dvd->p_ifo );
453         free( p_dvd );
454         return -1;
455     }
456
457     /* Set stream and area data */
458     vlc_mutex_lock( &p_input->stream.stream_lock );
459
460     /* Initialize ES structures */
461     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
462
463 #define title_inf p_dvd->p_ifo->vmg.title_inf
464     intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
465
466 #define area p_input->stream.pp_areas
467     /* We start from 1 here since the default area 0
468      * is reserved for video_ts.vob */
469     for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
470     {
471         input_AddArea( p_input );
472
473         /* Titles are Program Chains */
474         area[i]->i_id = i;
475
476         /* Absolute start offset and size 
477          * We can only set that with vts ifo, so we do it during the
478          * first call to DVDSetArea */
479         area[i]->i_start = 0;
480         area[i]->i_size = 0;
481
482         /* Number of chapters */
483         area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
484         area[i]->i_part = 1;
485
486         /* Number of angles */
487         area[i]->i_angle_nb = 0;
488         area[i]->i_angle = 1;
489
490         /* Offset to vts_i_0.ifo */
491         area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
492                        title_inf.p_attr[i-1].i_start_sector;
493     }   
494 #undef area
495 #undef title_inf
496
497     p_area = p_input->stream.pp_areas[i_title];
498     p_area->i_part = i_chapter;
499
500     /* set title, chapter, audio and subpic */
501     if( DVDSetArea( p_input, p_area ) )
502     {
503         vlc_mutex_unlock( &p_input->stream.stream_lock );
504         return -1;
505     }
506
507     vlc_mutex_unlock( &p_input->stream.stream_lock );
508
509     return 0;
510
511 }
512
513 /*****************************************************************************
514  * DVDClose: close dvd
515  *****************************************************************************/
516 static void DVDClose( struct input_thread_s *p_input )
517 {
518     thread_dvd_data_t *     p_dvd;
519
520     p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
521
522     IfoDestroy( p_dvd->p_ifo );
523
524     p_input->p_access_data = (void *)(p_dvd->dvdhandle);
525     free( p_dvd );
526
527     /* Clean up libdvdcss */
528     dvdcss_close( (dvdcss_handle) p_input->p_access_data );
529 }
530
531 /*****************************************************************************
532  * DVDSetProgram: Does nothing, a DVD is mono-program
533  *****************************************************************************/
534 static int DVDSetProgram( input_thread_t * p_input, 
535             pgrm_descriptor_t * p_program ) 
536 {
537     return 0;
538 }
539
540 /*****************************************************************************
541  * DVDSetArea: initialize input data for title x, chapter y.
542  * It should be called for each user navigation request.
543  *****************************************************************************
544  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
545  * Note that you have to take the lock before entering here.
546  *****************************************************************************/
547 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
548 {
549     thread_dvd_data_t *  p_dvd;
550     es_descriptor_t *    p_es;
551     u16                  i_id;
552     int                  i_vts_title;
553     int                  i_audio_nb = 0;
554     int                  i_spu_nb = 0;
555     int                  i;
556
557     p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
558
559     /* we can't use the interface slider until initilization is complete */
560     p_input->stream.b_seekable = 0;
561
562     if( p_area != p_input->stream.p_selected_area )
563     {
564         /* Reset the Chapter position of the old title */
565         p_input->stream.p_selected_area->i_part = 0;
566
567         /*
568          *  We have to load all title information
569          */
570         /* Change the default area */
571         p_input->stream.p_selected_area =
572                     p_input->stream.pp_areas[p_area->i_id];
573
574         /* title number: it is not vts nb!,
575          * it is what appears in the interface list */
576         p_dvd->i_title = p_area->i_id;
577         p_dvd->p_ifo->i_title = p_dvd->i_title;
578
579         /* set number of chapters of current title */
580         p_dvd->i_chapter_nb = p_area->i_part_nb;
581
582         /* ifo vts */
583         if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
584         {
585             intf_ErrMsg( "dvd error: fatal error in vts ifo" );
586             free( p_dvd );
587             p_input->b_error = 1;
588             return -1;
589         }
590
591 #define vmg p_dvd->p_ifo->vmg
592 #define vts p_dvd->p_ifo->vts
593         /* title position inside the selected vts */
594         i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
595         p_dvd->i_title_id =
596             vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
597
598         intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
599                       p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
600
601         /*
602          * Angle management
603          */
604         p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
605         if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
606         {
607             p_dvd->i_angle = 1;
608         }
609     
610         /*
611          * Set selected title start and size
612          */
613         
614         /* title set offset XXX: convert to block values */
615         p_dvd->i_title_start =
616             vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
617
618         /* last video cell */
619         p_dvd->i_cell = 0;
620         p_dvd->i_prg_cell = -1 +
621             vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
622
623         if( DVDFindCell( p_dvd ) < 0 )
624         {
625             intf_ErrMsg( "dvd error: can't find title end" );
626             p_input->b_error = 1;
627             return -1;
628         }
629         
630         /* temporary hack to fix size in some dvds */
631         if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
632         {
633             p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
634         }
635
636         p_dvd->i_sector = 0;
637         p_dvd->i_size = vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector;
638
639         if( DVDChapterSelect( p_dvd, 1 ) < 0 )
640         {
641             intf_ErrMsg( "dvd error: can't find first chapter" );
642             p_input->b_error = 1;
643             return -1;
644         }
645         
646         /* Force libdvdcss to check its title key.
647          * It is only useful for title cracking method. Methods using the
648          * decrypted disc key are fast enough to check the key at each seek */
649
650         if( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_start,
651                             DVDCSS_SEEK_KEY ) < 0 )
652         {
653             intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
654             return -1;
655         }
656
657         p_dvd->i_size -= p_dvd->i_sector + 1;
658
659         IfoPrintTitle( p_dvd );
660
661         /* Area definition */
662         p_input->stream.p_selected_area->i_start = LB2OFF( p_dvd->i_start );
663         p_input->stream.p_selected_area->i_size = LB2OFF( p_dvd->i_size );
664         p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
665         p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
666
667 #if 0
668         /* start at the beginning of the title */
669         /* FIXME: create a conf option to select whether to restart
670          * title or not */
671         p_input->stream.p_selected_area->i_tell = 0;
672         p_input->stream.p_selected_area->i_part = 1;
673 #endif
674
675         /*
676          * Destroy obsolete ES by reinitializing program 0
677          * and find all ES in title with ifo data
678          */
679         if( p_input->stream.pp_programs != NULL )
680         {
681             /* We don't use input_EndStream here since
682              * we keep area structures */
683
684             for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
685             {
686                 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
687             }
688
689             free( p_input->stream.pp_selected_es );
690             input_DelProgram( p_input, p_input->stream.p_selected_program );
691
692             p_input->stream.pp_selected_es = NULL;
693             p_input->stream.i_selected_es_number = 0;
694         }
695
696         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
697         p_input->stream.p_selected_program = p_input->stream.pp_programs[0]; 
698 //        p_input->stream.p_new_program = p_input->stream.pp_programs[0]; 
699
700         /* No PSM to read in DVD mode, we already have all information */
701         p_input->stream.p_selected_program->b_is_ok = 1;
702
703         p_es = NULL;
704
705         /* ES 0 -> video MPEG2 */
706         IfoPrintVideo( p_dvd );
707
708         p_es = input_AddES( p_input, p_input->stream.p_selected_program, 
709                 0xe0, 0 );
710         p_es->i_stream_id = 0xe0;
711         p_es->i_type = MPEG2_VIDEO_ES;
712         p_es->i_cat = VIDEO_ES;
713         
714 #define audio_status \
715     vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
716         /* Audio ES, in the order they appear in .ifo */
717         for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
718         {
719             IfoPrintAudio( p_dvd, i );
720
721             /* audio channel is active if first byte is 0x80 */
722             if( audio_status.i_available )
723             {
724                 i_audio_nb++;
725
726                 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
727                 {
728                 case 0x00:              /* AC3 */
729                     i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
730                     p_es = input_AddES( p_input,
731                                p_input->stream.p_selected_program, i_id, 0 );
732                     p_es->i_stream_id = 0xbd;
733                     p_es->i_type = AC3_AUDIO_ES;
734                     p_es->b_audio = 1;
735                     p_es->i_cat = AUDIO_ES;
736                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
737                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
738                     strcat( p_es->psz_desc, " (ac3)" );
739     
740                     break;
741                 case 0x02:
742                 case 0x03:              /* MPEG audio */
743                     i_id = 0xc0 + audio_status.i_position;
744                     p_es = input_AddES( p_input,
745                                     p_input->stream.p_selected_program, i_id
746                                     , 0 );
747                     p_es->i_stream_id = i_id;
748                     p_es->i_type = MPEG2_AUDIO_ES;
749                     p_es->b_audio = 1;
750                     p_es->i_cat = AUDIO_ES;
751                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
752                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
753                     strcat( p_es->psz_desc, " (mpeg)" );
754     
755                     break;
756                 case 0x04:              /* LPCM */
757     
758                     i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
759                     p_es = input_AddES( p_input,
760                                     p_input->stream.p_selected_program,
761                                     i_id, 0 );
762                     p_es->i_stream_id = 0xbd;
763                     p_es->i_type = LPCM_AUDIO_ES;
764                     p_es->b_audio = 1;
765                     p_es->i_cat = AUDIO_ES;
766                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
767                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
768                     strcat( p_es->psz_desc, " (lpcm)" );
769     
770                     break;
771                 case 0x06:              /* DTS */
772                     i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
773                     intf_ErrMsg( "dvd warning: DTS audio not handled yet"
774                                  "(0x%x)", i_id );
775                     break;
776                 default:
777                     i_id = 0;
778                     intf_ErrMsg( "dvd warning: unknown audio type %.2x",
779                              vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
780                 }
781             }
782         }
783 #undef audio_status
784 #define spu_status \
785     vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
786
787         /* Sub Picture ES */
788            
789         for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
790         {
791             IfoPrintSpu( p_dvd, i );
792
793             if( spu_status.i_available )
794             {
795                 i_spu_nb++;
796
797                 /*  there are several streams for one spu */
798                 if(  vts.manager_inf.video_attr.i_ratio )
799                 {
800                     /* 16:9 */
801                     switch( vts.manager_inf.video_attr.i_perm_displ )
802                     {
803                     case 1:
804                         i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
805                                | 0xbd;
806                         break;
807                     case 2:
808                         i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
809                                | 0xbd;
810                         break;
811                     default:
812                         i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
813                                | 0xbd;
814                         break;
815                     }
816                 }
817                 else
818                 {
819                     /* 4:3 */
820                     i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
821                            | 0xbd;
822                 }
823                 p_es = input_AddES( p_input,
824                                     p_input->stream.p_selected_program,
825                                     i_id, 0 );
826                 p_es->i_stream_id = 0xbd;
827                 p_es->i_type = DVD_SPU_ES;
828                 p_es->i_cat = SPU_ES;
829                 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
830                     vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) ); 
831             }
832         }
833 #undef spu_status
834     
835         /* FIXME: hack to check that the demuxer is ready, and set
836         * the decoders */
837         if( p_input->p_demux_module )
838         {
839             DVDLaunchDecoders( p_input );
840         }
841
842     } /* i_title >= 0 */
843     else
844     {
845         p_area = p_input->stream.p_selected_area;
846     }
847 #undef vts
848 #undef vmg
849
850     /*
851      * Chapter selection
852      */
853
854     if( p_area->i_part != p_dvd->i_chapter )
855     {
856         if( ( p_area->i_part > 0 ) &&
857             ( p_area->i_part <= p_area->i_part_nb ))
858         {
859             if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
860             {
861                 intf_ErrMsg( "dvd error: can't set chapter in area" );
862                 p_input->b_error = 1;
863                 return -1;
864             }
865     
866             p_input->stream.p_selected_area->i_tell =
867                                    LB2OFF( p_dvd->i_start ) - p_area->i_start;
868             p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
869     
870             intf_WarnMsg( 4, "dvd info: chapter %d start at: %lld",
871                                         p_area->i_part, p_area->i_tell );
872         }
873         else
874         {
875             p_area->i_part = 1;
876             p_dvd->i_chapter = 1;
877         }
878     }
879
880 #define title \
881     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
882     if( p_area->i_angle != p_dvd->i_angle )
883     {
884         if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
885         {
886             if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
887             {
888                 p_dvd->i_cell = 0;
889             }
890             p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
891             p_dvd->i_angle = p_area->i_angle;
892     
893             DVDFindSector( p_dvd );
894             p_dvd->i_cell += p_dvd->i_angle_cell;
895         }
896         else
897         {
898             p_dvd->i_angle = p_area->i_angle;
899         }
900
901         intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
902     }
903
904     /* warn interface that something has changed */
905     p_input->stream.b_seekable = 1;
906     p_input->stream.b_changed = 1;
907
908     return 0;
909 }
910
911
912 /*****************************************************************************
913  * DVDRead: reads data packets.
914  *****************************************************************************
915  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
916  * bytes.
917  *****************************************************************************/
918 static int DVDRead( input_thread_t * p_input,
919                     byte_t * p_buffer, size_t i_count )
920 {
921     thread_dvd_data_t *     p_dvd;
922     int                     i_block_once;
923     int                     i_read_blocks;
924     int                     i_read_total;
925     int                     i_sector;
926     int                     i_blocks;
927     boolean_t               b_eoc;
928
929     p_dvd = (thread_dvd_data_t *)(p_input->p_access_data);
930
931     i_sector = 0;
932     i_read_total = 0;
933     i_read_blocks = 0;
934     b_eoc = 0;
935
936     i_blocks = OFF2LB(i_count);
937
938     while( i_blocks )
939     {
940         i_sector = p_dvd->i_title_start + p_dvd->i_sector;
941         i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
942
943         /* Get the position of the next cell if we're at cell end */
944         if( i_block_once <= 0 )
945         {
946             int     i_angle;
947
948             p_dvd->i_cell++;
949             p_dvd->i_angle_cell++;
950
951             /* Find cell index in adress map */
952             if( DVDFindSector( p_dvd ) < 0 )
953             {
954                 intf_ErrMsg( "dvd error: can't find next cell" );
955                 return 1;
956             }
957
958             /* Position the fd pointer on the right address */
959             if( ( i_sector = dvdcss_seek( p_dvd->dvdhandle,
960                                 p_dvd->i_title_start + p_dvd->i_sector,
961                                 DVDCSS_SEEK_MPEG ) ) < 0 )
962             {
963                 intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
964                 return -1;
965             }
966
967             /* update chapter : it will be easier when we have navigation
968              * ES support */
969             if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
970             {
971                 if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
972                 {
973                     i_angle = p_dvd->i_angle - 1;
974                 }
975                 else
976                 {
977                     i_angle = 0;
978                 }
979                 if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
980                     ( p_dvd->i_prg_cell - i_angle + 1 ) )
981                 {
982                     p_dvd->i_chapter++;
983                     b_eoc = 1;
984                 }
985             }
986
987             i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
988         }
989
990         /* The number of blocks read is the max between the requested
991          * value and the leaving block in the cell */
992         if( i_block_once > i_blocks )
993         {
994             i_block_once = i_blocks;
995         }
996     /*
997     intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
998     */
999
1000         /* Reads from DVD */
1001         i_read_blocks = dvdcss_read( p_dvd->dvdhandle, p_buffer,
1002                                      i_block_once, DVDCSS_READ_DECRYPT );
1003
1004         i_blocks -= i_read_blocks;
1005         p_buffer += LB2OFF( i_read_blocks );
1006         i_read_total += i_read_blocks;
1007
1008         /* Update global position */
1009         p_dvd->i_sector += i_read_blocks;
1010     }
1011
1012     vlc_mutex_lock( &p_input->stream.stream_lock );
1013
1014     p_input->stream.p_selected_area->i_tell =
1015         LB2OFF( i_sector + i_read_total ) -
1016         p_input->stream.p_selected_area->i_start;
1017     if( b_eoc )
1018     {
1019         /* We modify i_part only at end of chapter not to erase
1020          * some modification from the interface */
1021         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1022     }
1023
1024     if( p_input->stream.p_selected_area->i_tell
1025             >= p_input->stream.p_selected_area->i_size )
1026     {
1027         if( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb )
1028         {
1029             /* EOF */
1030             vlc_mutex_unlock( &p_input->stream.stream_lock );
1031             return 0;
1032         }
1033
1034         /* EOT */
1035         intf_WarnMsg( 4, "dvd info: new title" );
1036         p_dvd->i_title++;
1037         DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
1038         vlc_mutex_unlock( &p_input->stream.stream_lock );
1039         return LB2OFF( i_read_total );
1040     }
1041
1042     vlc_mutex_unlock( &p_input->stream.stream_lock );
1043 /*
1044     if( i_read_blocks != i_block_once )
1045     {
1046         return -1;
1047     }
1048 */
1049     return LB2OFF( i_read_total );
1050 }
1051
1052 /*****************************************************************************
1053  * DVDSeek : Goes to a given position on the stream.
1054  *****************************************************************************
1055  * This one is used by the input and translate chronological position from
1056  * input to logical position on the device.
1057  * The lock should be taken before calling this function.
1058  *****************************************************************************/
1059 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1060 {
1061     thread_dvd_data_t *     p_dvd;
1062     int                     i_block;
1063     int                     i_prg_cell;
1064     int                     i_cell;
1065     int                     i_chapter;
1066     int                     i_angle;
1067     
1068     p_dvd = ( thread_dvd_data_t * )(p_input->p_access_data);
1069
1070     vlc_mutex_lock( &p_input->stream.stream_lock );
1071     /* we have to take care of offset of beginning of title */
1072     p_dvd->i_sector = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
1073                        - p_dvd->i_title_start;
1074     vlc_mutex_unlock( &p_input->stream.stream_lock );
1075
1076     i_prg_cell = 0;
1077     i_chapter = 0;
1078
1079     /* parse vobu address map to find program cell */
1080     while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector  )
1081     {
1082         i_prg_cell++;
1083     }
1084
1085     p_dvd->i_prg_cell = i_prg_cell;
1086
1087     if( DVDChooseAngle( p_dvd ) < 0 )
1088     {
1089         p_input->b_error = 1;
1090         return;        
1091     }
1092
1093     p_dvd->i_cell = 0;
1094
1095     /* Find first title cell which is inside program cell */
1096     if( DVDFindCell( p_dvd ) < 0 )
1097     {
1098         /* no following cell : we're at eof */
1099         intf_ErrMsg( "dvd error: cell seeking failed" );
1100         p_input->b_error = 1;
1101         return;
1102     }
1103
1104     i_cell = p_dvd->i_cell;
1105
1106 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1107     /* parse cell address map to find title cell containing sector */
1108     while( cell.i_end_sector < p_dvd->i_sector )
1109     {
1110         i_cell++;
1111     }
1112
1113     p_dvd->i_cell = i_cell;
1114
1115     /* if we're inside a multi-angle zone, we have to choose i_sector
1116      * in the current angle ; we can't do it all the time since cells
1117      * can be very wide out of such zones */
1118     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1119     {
1120         p_dvd->i_sector = __MAX(
1121                 cell.i_start_sector,
1122                 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1123     }
1124
1125     p_dvd->i_end_sector = __MIN(
1126             cell.i_end_sector,
1127             title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1128 #undef cell
1129     /* update chapter */
1130     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1131     {
1132         i_angle = p_dvd->i_angle - 1;
1133     }
1134     else
1135     {
1136         i_angle = 0;
1137     }
1138     if( p_dvd->i_chapter_nb > 1 )
1139     {
1140         while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1141                     ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1142                ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1143         {
1144             i_chapter++;
1145         }
1146     }
1147     else
1148     {
1149         i_chapter = 1;
1150     }
1151
1152     p_dvd->i_chapter = i_chapter;
1153
1154     if( ( i_block = dvdcss_seek( p_dvd->dvdhandle,
1155                                  p_dvd->i_title_start + p_dvd->i_sector,
1156                                  DVDCSS_SEEK_MPEG ) ) < 0 )
1157     {
1158         intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1159         p_input->b_error = 1;
1160         return;
1161     }
1162
1163     vlc_mutex_lock( &p_input->stream.stream_lock );
1164     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1165     p_input->stream.p_selected_area->i_tell =
1166         LB2OFF ( i_block ) - p_input->stream.p_selected_area->i_start;
1167     vlc_mutex_unlock( &p_input->stream.stream_lock );
1168
1169     intf_WarnMsg( 7, "Program Cell: %d Cell: %d Chapter: %d",
1170                      p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1171
1172     return;
1173 }
1174
1175 #define cell  p_dvd->p_ifo->vts.cell_inf
1176
1177 /*****************************************************************************
1178  * DVDFindCell: adjust the title cell index with the program cell
1179  *****************************************************************************/
1180 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1181 {
1182     int                 i_cell;
1183     int                 i_index;
1184
1185     i_cell = p_dvd->i_cell;
1186     i_index = p_dvd->i_prg_cell;
1187
1188     if( i_cell >= cell.i_cell_nb )
1189     {
1190         return -1;
1191     }
1192
1193     while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1194                    cell.p_cell_map[i_cell].i_vob_id ) ||
1195       ( title.p_cell_pos[i_index].i_cell_id !=
1196                    cell.p_cell_map[i_cell].i_cell_id ) ) &&
1197            ( i_cell < cell.i_cell_nb - 1 ) )
1198     {
1199         i_cell++;
1200     }
1201
1202 /*
1203 intf_WarnMsg( 12, "FindCell: i_cell %d i_index %d found %d nb %d",
1204                     p_dvd->i_cell,
1205                     p_dvd->i_prg_cell,
1206                     i_cell,
1207                     cell.i_cell_nb );
1208 */
1209
1210     p_dvd->i_cell = i_cell;
1211
1212     return 0;    
1213 }
1214
1215 #undef cell
1216
1217 /*****************************************************************************
1218  * DVDFindSector: find cell index in adress map from index in
1219  * information table program map and give corresponding sectors.
1220  *****************************************************************************/
1221 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1222 {
1223
1224     if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1225     {
1226         p_dvd->i_prg_cell++;
1227
1228         if( DVDChooseAngle( p_dvd ) < 0 )
1229         {
1230             return -1;
1231         }
1232     }
1233
1234     if( DVDFindCell( p_dvd ) < 0 )
1235     {
1236         intf_ErrMsg( "dvd error: can't find sector" );
1237         return -1;
1238     }
1239     
1240     /* Find start and end sectors of new cell */
1241 #if 1
1242     p_dvd->i_sector = __MAX(
1243          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1244          title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1245     p_dvd->i_end_sector = __MIN(
1246          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1247          title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1248 #else
1249     p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1250     p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1251 #endif
1252
1253 /*
1254     intf_WarnMsg( 12, "cell: %d sector1: 0x%x end1: 0x%x\n"
1255                    "index: %d sector2: 0x%x end2: 0x%x\n"
1256                    "category: 0x%x ilvu end: 0x%x vobu start 0x%x", 
1257         p_dvd->i_cell,
1258         p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1259         p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1260         p_dvd->i_prg_cell,
1261         title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1262         title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1263         title.p_cell_play[p_dvd->i_prg_cell].i_category, 
1264         title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1265         title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1266 */
1267
1268     return 0;
1269 }
1270
1271 /*****************************************************************************
1272  * DVDChapterSelect: find the cell corresponding to requested chapter
1273  *****************************************************************************/
1274 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1275 {
1276
1277     /* Find cell index in Program chain for current chapter */
1278     p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1279     p_dvd->i_cell = 0;
1280     p_dvd->i_sector = 0;
1281
1282     DVDChooseAngle( p_dvd );
1283
1284     /* Search for cell_index in cell adress_table and initialize
1285      * start sector */
1286     if( DVDFindSector( p_dvd ) < 0 )
1287     {
1288         intf_ErrMsg( "dvd error: can't select chapter" );
1289         return -1;
1290     }
1291
1292     /* start is : beginning of vts vobs + offset to vob x */
1293     p_dvd->i_start = p_dvd->i_title_start + p_dvd->i_sector;
1294
1295     /* Position the fd pointer on the right address */
1296     if( ( p_dvd->i_start = dvdcss_seek( p_dvd->dvdhandle,
1297                                         p_dvd->i_start,
1298                                         DVDCSS_SEEK_MPEG ) ) < 0 )
1299     {
1300         intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1301         return -1;
1302     }
1303
1304     p_dvd->i_chapter = i_chapter;
1305     return 0;
1306 }
1307
1308 /*****************************************************************************
1309  * DVDChooseAngle: select the cell corresponding to the selected angle
1310  *****************************************************************************/
1311 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1312 {
1313     /* basic handling of angles */
1314     switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1315                     >> 12 ) )
1316     {
1317         /* we enter a muli-angle section */
1318         case 0x5:
1319             p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1320             p_dvd->i_angle_cell = 0;
1321             break;
1322         /* we exit a multi-angle section */
1323         case 0x9:
1324         case 0xd:
1325             p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;
1326             break;
1327     }
1328
1329     return 0;
1330 }
1331
1332 #undef title
1333 /*****************************************************************************
1334  * DVDLaunchDecoders
1335  *****************************************************************************/
1336 static void DVDLaunchDecoders( input_thread_t * p_input )
1337 {
1338     thread_dvd_data_t *  p_dvd;
1339     int                  i_audio;
1340     int                  i_spu;
1341
1342     p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
1343
1344     /* Select Video stream (always 0) */
1345     if( p_main->b_video )
1346     {
1347         input_SelectES( p_input, p_input->stream.pp_es[0] );
1348     }
1349
1350     /* Select audio stream */
1351     if( p_main->b_audio )
1352     {
1353         /* For audio: first one if none or a not existing one specified */
1354         i_audio = config_GetIntVariable( INPUT_CHANNEL_VAR );
1355         if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1356         {
1357             config_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
1358             i_audio = 1;
1359         }
1360         if( i_audio > 0 /*&& i_audio_nb > 0*/ )
1361         {
1362             if( config_GetIntVariable( AOUT_SPDIF_VAR ) ||
1363                 ( config_GetIntVariable( INPUT_AUDIO_VAR ) ==
1364                   REQUESTED_AC3 ) )
1365             {
1366                 int     i_ac3 = i_audio;
1367                 while( ( p_input->stream.pp_es[i_ac3]->i_type !=
1368                          AC3_AUDIO_ES ) && ( i_ac3 <=
1369                          p_dvd->p_ifo->vts.manager_inf.i_audio_nb ) )
1370                 {
1371                     i_ac3++;
1372                 }
1373                 if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
1374                 {
1375                     input_SelectES( p_input,
1376                                     p_input->stream.pp_es[i_ac3] );
1377                 }
1378             }
1379             else
1380             {
1381                 input_SelectES( p_input,
1382                                 p_input->stream.pp_es[i_audio] );
1383             }
1384         }
1385     }
1386
1387     /* Select subtitle */
1388     if( p_main->b_video )
1389     {
1390         /* for spu, default is none */
1391         i_spu = config_GetIntVariable( INPUT_SUBTITLE_VAR );
1392         if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1393         {
1394             config_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
1395             i_spu = 0;
1396         }
1397         if( i_spu > 0 /* && i_spu_nb > 0*/ )
1398         {
1399             i_spu += p_dvd->p_ifo->vts.manager_inf.i_audio_nb;
1400             input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
1401         }
1402     }
1403 }