]> git.sesse.net Git - vlc/blob - modules/access/dvdread/input.c
* src/misc/variables.c, ALL: improvements to the object variables api.
[vlc] / modules / access / dvdread / input.c
1 /*****************************************************************************
2  * input.c: DvdRead plugin.
3  *****************************************************************************
4  * This plugins should handle all the known specificities of the DVD format,
5  * especially the 2048 bytes logical block size.
6  * It depends on: libdvdread for ifo files and block reading.
7  *****************************************************************************
8  * Copyright (C) 2001, 2003 VideoLAN
9  * $Id: input.c,v 1.20 2003/05/04 22:42:14 gbazin Exp $
10  *
11  * Author: Stéphane Borel <stef@via.ecp.fr>
12  *
13  * Some code taken form the play_title.c by Billy Biggs <vektor@dumbterm.net>
14  * in libdvdread.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
29  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include <vlc/vlc.h>
38 #include <vlc/input.h>
39
40 #include "../../demux/mpeg/system.h"
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #endif
45
46 #include <fcntl.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <string.h>
50 #include <errno.h>
51 #include <assert.h>
52
53 #ifdef STRNCASECMP_IN_STRINGS_H
54 #   include <strings.h>
55 #endif
56
57 #if defined( WIN32 )
58 #   include <io.h>                                                 /* read() */
59 #endif
60
61 #include <dvdread/dvd_reader.h>
62 #include <dvdread/ifo_types.h>
63 #include <dvdread/ifo_read.h>
64 #include <dvdread/nav_read.h>
65 #include <dvdread/nav_print.h>
66
67 #include "input.h"
68
69 #include "iso_lang.h"
70
71 /* how many blocks DVDRead will read in each loop */
72 #define DVD_BLOCK_READ_ONCE 64
73
74 /*****************************************************************************
75  * Private structure
76  *****************************************************************************/
77 struct demux_sys_t
78 {
79     module_t *   p_module;
80     mpeg_demux_t mpeg;
81 };
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86 /* called from outside */
87 static int  DvdReadDemux    ( input_thread_t * );
88 static int  DvdReadRewind   ( input_thread_t * );
89
90 static int  DvdReadSetArea    ( input_thread_t *, input_area_t * );
91 static int  DvdReadSetProgram ( input_thread_t *, pgrm_descriptor_t * );
92 static int  DvdReadRead       ( input_thread_t *, byte_t *, size_t );
93 static void DvdReadSeek       ( input_thread_t *, off_t );
94
95 /* called only from here */
96 static void DvdReadLauchDecoders( input_thread_t * p_input );
97 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, uint8_t * p_data );
98 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
99
100 /*
101  * Data demux functions
102  */
103
104 /*****************************************************************************
105  * InitDVD: initialize DVD structures
106  *****************************************************************************/
107 int E_(InitDVD) ( vlc_object_t *p_this )
108 {
109     input_thread_t *p_input = (input_thread_t *)p_this;
110     demux_sys_t *   p_demux;
111
112     if( p_input->stream.i_method != INPUT_METHOD_DVD )
113     {
114         return VLC_EGENERIC;
115     }
116
117     p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t ) );
118     if( p_demux == NULL )
119     {
120         return VLC_ENOMEM;
121     }
122
123     p_input->p_private = (void*)&p_demux->mpeg;
124     p_demux->p_module = module_Need( p_input, "mpeg-system", NULL );
125     if( p_demux->p_module == NULL )
126     {
127         free( p_input->p_demux_data );
128         return VLC_ENOMOD;
129     }
130
131     p_input->pf_demux = DvdReadDemux;
132     p_input->pf_rewind = NULL;
133
134     vlc_mutex_lock( &p_input->stream.stream_lock );
135
136     DvdReadLauchDecoders( p_input );
137
138     vlc_mutex_unlock( &p_input->stream.stream_lock );
139
140     return VLC_SUCCESS;
141 }
142
143 /*****************************************************************************
144  * EndDVD: end DVD structures
145  *****************************************************************************/
146 void E_(EndDVD) ( vlc_object_t *p_this )
147 {
148     input_thread_t *p_input = (input_thread_t *)p_this;
149
150     module_Unneed( p_input, p_input->p_demux_data->p_module );
151     free( p_input->p_demux_data );
152 }
153
154 /*****************************************************************************
155  * DvdReadDemux
156  *****************************************************************************/
157 #define PEEK( SIZE )                                                        \
158     i_result = input_Peek( p_input, &p_peek, SIZE );                        \
159     if( i_result == -1 )                                                    \
160     {                                                                       \
161         return -1;                                                          \
162     }                                                                       \
163     else if( i_result < SIZE )                                              \
164     {                                                                       \
165         /* EOF */                                                           \
166         return 0;                                                           \
167     }
168
169 static int DvdReadDemux( input_thread_t * p_input )
170 {
171     int                 i;
172     byte_t *            p_peek;
173     data_packet_t *     p_data;
174     ssize_t             i_result;
175     int                 i_packet_size;
176
177
178     /* Read headers to compute payload length */
179     for( i = 0 ; i < DVD_BLOCK_READ_ONCE ; i++ )
180     {
181
182         /* Read what we believe to be a packet header. */
183         PEEK( 4 );
184
185         /* Default header */
186         if( U32_AT( p_peek ) != 0x1BA )
187         {
188             /* That's the case for all packets, except pack header. */
189             i_packet_size = U16_AT( p_peek + 4 );
190         }
191         else
192         {
193             /* MPEG-2 Pack header. */
194             i_packet_size = 8;
195         }
196
197         /* Fetch a packet of the appropriate size. */
198         i_result = input_SplitBuffer( p_input, &p_data, i_packet_size + 6 );
199         if( i_result <= 0 )
200         {
201             return( i_result );
202         }
203
204         /* In MPEG-2 pack headers we still have to read stuffing bytes. */
205         if( (p_data->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
206         {
207             ssize_t i_stuffing = (p_data->p_demux_start[13] & 0x7);
208             /* Force refill of the input buffer - though we don't care
209              * about p_peek. Please note that this is unoptimized. */
210             PEEK( i_stuffing );
211             p_input->p_current_data += i_stuffing;
212         }
213
214         p_input->p_demux_data->mpeg.pf_demux_ps( p_input, p_data );
215
216     }
217
218     return i;
219 }
220
221 /*****************************************************************************
222  * DVDRewind : reads a stream backward
223  *****************************************************************************/
224 static int DvdReadRewind( input_thread_t * p_input )
225 {
226     return -1;
227 }
228
229 /*
230  * Data access functions
231  */
232
233 /*****************************************************************************
234  * OpenDVD: open libdvdread
235  *****************************************************************************/
236 int E_(OpenDVD) ( vlc_object_t *p_this )
237 {
238     input_thread_t *        p_input = (input_thread_t *)p_this;
239     char *                  psz_parser;
240     char *                  psz_source;
241     char *                  psz_next;
242     struct stat             stat_info;
243     thread_dvd_data_t *     p_dvd;
244     dvd_reader_t *          p_dvdread;
245     input_area_t *          p_area;
246     unsigned int            i_title = 1;
247     unsigned int            i_chapter = 1;
248     unsigned int            i_angle = 1;
249     unsigned int            i;
250
251     psz_source = strdup( p_input->psz_name );
252     if( psz_source == NULL )
253     {
254         return VLC_ENOMEM;
255     }
256
257     p_input->pf_read = DvdReadRead;
258     p_input->pf_seek = DvdReadSeek;
259     p_input->pf_set_area = DvdReadSetArea;
260     p_input->pf_set_program = DvdReadSetProgram;
261
262     /* Start with the end, because you could have :
263      * dvdread:/Volumes/my@toto/VIDEO_TS@1,1
264      * (yes, this is kludgy). */
265     for ( psz_parser = psz_source + strlen(psz_source) - 1;
266           psz_parser >= psz_source && *psz_parser != '@';
267           psz_parser-- );
268
269     if( psz_parser >= psz_source && *psz_parser == '@' )
270     {
271         /* Found options */
272         *psz_parser = '\0';
273         ++psz_parser;
274
275         i_title = (int)strtol( psz_parser, &psz_next, 10 );
276         if( *psz_next )
277         {
278             psz_parser = psz_next + 1;
279             i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
280             if( *psz_next )
281             {
282                 i_angle = (int)strtol( psz_next + 1, NULL, 10 );
283             }
284         }
285
286         i_title = i_title ? i_title : 1;
287         i_chapter = i_chapter ? i_chapter : 1;
288         i_angle = i_angle ? i_angle : 1;
289     }
290
291     if( !*psz_source )
292     {
293         free( psz_source );
294         if( !p_input->psz_access )
295         {
296             return VLC_EGENERIC;
297         }
298         psz_source = config_GetPsz( p_input, "dvd" );
299         if( !psz_source ) return VLC_EGENERIC;
300     }
301
302     if( stat( psz_source, &stat_info ) == -1 )
303     {
304         msg_Warn( p_input, "cannot stat() source `%s' (%s)",
305                            psz_source, strerror(errno) );
306         free( psz_source );
307         return VLC_EGENERIC;
308     }
309     if( !S_ISBLK(stat_info.st_mode) &&
310         !S_ISCHR(stat_info.st_mode) &&
311         !S_ISDIR(stat_info.st_mode) )
312     {
313         msg_Warn( p_input, "dvdread module discarded (not a valid source)" );
314         free( psz_source );
315         return VLC_EGENERIC;
316     }
317
318     msg_Dbg( p_input, "dvdroot=%s title=%d chapter=%d angle=%d",
319                       psz_source, i_title, i_chapter, i_angle );
320
321
322     p_dvdread = DVDOpen( psz_source );
323
324     /* free allocated strings */
325     free( psz_source );
326
327     if( ! p_dvdread )
328     {
329         msg_Err( p_input, "libdvdcss cannot open source" );
330         return VLC_EGENERIC;
331     }
332
333     /* set up input  */
334     p_input->i_mtu = 0;
335
336     p_dvd = malloc( sizeof(thread_dvd_data_t) );
337     if( p_dvd == NULL )
338     {
339         msg_Err( p_input, "out of memory" );
340         return VLC_ENOMEM;
341     }
342
343     p_dvd->p_dvdread = p_dvdread;
344     p_dvd->p_title = NULL;
345     p_dvd->p_vts_file = NULL;
346
347
348     p_input->p_access_data = (void *)p_dvd;
349
350     /* Ifo allocation & initialisation */
351     if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
352     {
353         msg_Warn( p_input, "cannot open VMG info" );
354         free( p_dvd );
355         return VLC_EGENERIC;
356     }
357     msg_Dbg( p_input, "VMG opened" );
358
359     /* Set stream and area data */
360     vlc_mutex_lock( &p_input->stream.stream_lock );
361
362     p_input->stream.i_method = INPUT_METHOD_DVD;
363
364     /* If we are here we can control the pace... */
365     p_input->stream.b_pace_control = VLC_TRUE;
366     p_input->stream.b_seekable = VLC_TRUE;
367
368     p_input->stream.p_selected_area->i_size = 0;
369     p_input->stream.p_selected_area->i_tell = 0;
370
371     /* Initialize ES structures */
372     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
373
374     /* disc input method */
375     p_input->stream.i_method = INPUT_METHOD_DVD;
376
377 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
378     msg_Dbg( p_input, "number of titles: %d", tt_srpt->nr_of_srpts );
379
380 #define area p_input->stream.pp_areas
381     /* We start from 1 here since the default area 0
382      * is reserved for video_ts.vob */
383     for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
384     {
385         /* Titles are Program Chains */
386         input_AddArea( p_input, i, tt_srpt->title[i-1].nr_of_ptts );
387
388         /* Absolute start offset and size
389          * We can only set that with vts ifo, so we do it during the
390          * first call to DVDSetArea */
391         area[i]->i_start = 0;
392         area[i]->i_size = 0;
393
394         /* Default Chapter */
395         area[i]->i_part = 1;
396
397         area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
398     }
399 #undef area
400
401     p_dvd->i_title = i_title <= tt_srpt->nr_of_srpts ? i_title : 1;
402 #undef tt_srpt
403
404     p_area = p_input->stream.pp_areas[p_dvd->i_title];
405     p_dvd->i_chapter = i_chapter;
406
407     p_dvd->i_chapter = i_chapter < p_area->i_part_nb ? i_chapter : 1;
408     p_area->i_part = p_dvd->i_chapter;
409
410     p_dvd->i_angle = i_angle;
411
412     /* set title, chapter, audio and subpic */
413     if( DvdReadSetArea( p_input, p_area ) )
414     {
415         vlc_mutex_unlock( &p_input->stream.stream_lock );
416         return VLC_EGENERIC;
417     }
418
419     vlc_mutex_unlock( &p_input->stream.stream_lock );
420
421     if( !p_input->psz_demux || !*p_input->psz_demux )
422     {
423         p_input->psz_demux = "dvdread";
424     }
425
426     return VLC_SUCCESS;
427 }
428
429 /*****************************************************************************
430  * CloseDVD: close libdvdread
431  *****************************************************************************/
432 void E_(CloseDVD) ( vlc_object_t *p_this )
433 {
434     input_thread_t *    p_input = (input_thread_t *)p_this;
435     thread_dvd_data_t * p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
436
437     /* This is a very nasty side-effect in the DVD plug-in : language
438      * selection here influences language selection of other streams. So
439      * unset those variables (may not be what the user wants).
440      * FIXME FIXME FIXME FIXME FIXME FIXME FIXME --Meuuh */
441     config_PutInt( p_input, "audio-channel", -1 );
442     config_PutInt( p_input, "spu-channel", -1 );
443
444     /* close libdvdread */
445     DVDCloseFile( p_dvd->p_title );
446     ifoClose( p_dvd->p_vts_file );
447     ifoClose( p_dvd->p_vmg_file );
448
449     DVDClose( p_dvd->p_dvdread );
450     free( p_dvd );
451     p_input->p_access_data = NULL;
452
453 }
454
455 /*****************************************************************************
456  * DvdReadSetProgram: Does nothing, a DVD is mono-program
457  *****************************************************************************/
458 static int DvdReadSetProgram( input_thread_t * p_input,
459                               pgrm_descriptor_t * p_program )
460 {
461     if( p_input->stream.p_selected_program != p_program )
462     {
463         thread_dvd_data_t *  p_dvd;
464         vlc_value_t val;
465
466         p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
467         p_dvd->i_angle = p_program->i_number;
468
469         memcpy( p_program, p_input->stream.p_selected_program,
470                 sizeof(pgrm_descriptor_t) );
471         p_program->i_number = p_dvd->i_angle;
472         p_input->stream.p_selected_program = p_program;
473
474         msg_Dbg( p_input, "angle %d selected", p_dvd->i_angle );
475
476         /* Update the navigation variables without triggering a callback */
477         val.i_int = p_program->i_number;
478         var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
479     }
480
481     return VLC_SUCCESS;
482 }
483
484 #define p_pgc         p_dvd->p_cur_pgc
485
486 /*****************************************************************************
487  * DvdReadSetArea: initialize input data for title x, chapter y.
488  * It should be called for each user navigation request.
489  *****************************************************************************
490  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
491  * Note that you have to take the lock before entering here.
492  *****************************************************************************/
493 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
494 {
495     thread_dvd_data_t *  p_dvd;
496     int                  pgc_id = 0;
497     int                  pgn = 0;
498     vlc_value_t          val;
499
500     p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
501
502     /* we can't use the interface slider until initilization is complete */
503     p_input->stream.b_seekable = VLC_FALSE;
504
505     if( p_area != p_input->stream.p_selected_area )
506     {
507         es_descriptor_t *    p_es;
508         unsigned int         i_cell = 0;
509         unsigned int         i_audio_nb = 0;
510         unsigned int         i_spu_nb = 0;
511         unsigned int         i;
512
513 #define p_vmg         p_dvd->p_vmg_file
514 #define p_vts         p_dvd->p_vts_file
515         if( p_dvd->p_title != NULL )
516         {
517             DVDCloseFile( p_dvd->p_title );
518         }
519
520         if( p_vts != NULL )
521         {
522             ifoClose( p_vts );
523         }
524
525         /* Reset the Chapter position of the old title */
526         p_input->stream.p_selected_area->i_part = 1;
527
528         /*
529          *  We have to load all title information
530          */
531         /* Change the default area */
532         p_input->stream.p_selected_area = p_area;
533
534         msg_Dbg( p_input, "open VTS %d, for title %d",
535             p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
536             p_area->i_id );
537
538         /* ifo vts */
539         if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
540                 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
541         {
542             msg_Err( p_input, "fatal error in vts ifo" );
543             ifoClose( p_vmg );
544             DVDClose( p_dvd->p_dvdread );
545             return VLC_EGENERIC;
546         }
547
548         /* title position inside the selected vts */
549         p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
550
551         /*
552          * Set selected title start
553          */
554         pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
555         pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
556         p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
557         i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
558
559         p_area->i_start =
560             LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
561
562         msg_Dbg( p_input, "start %d vts_title %d pgc %d pgn %d",
563                   p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
564
565         /*
566          * Find title end
567          */
568         i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
569
570         p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
571         p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
572
573         msg_Dbg( p_input, "start "I64Fd" size "I64Fd" end %d",
574                   p_area->i_start , p_area->i_size, p_dvd->i_end_block );
575
576         /*
577          * Set properties for current chapter
578          */
579         /* Remeber current chapter */
580         p_dvd->i_chapter = p_area->i_part;
581         p_dvd->b_eoc = VLC_FALSE;
582
583         pgc_id = p_vts->vts_ptt_srpt->title[
584                     p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
585         pgn = p_vts->vts_ptt_srpt->title[
586                     p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
587
588         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
589         p_dvd->i_pack_len = 0;
590         p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
591         DvdReadFindCell( p_dvd );
592
593         p_dvd->i_next_vobu = p_dvd->i_cur_block =
594             p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
595
596         /*
597          * Angle management
598          */
599         p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
600
601         if( p_dvd->i_angle > p_dvd->i_angle_nb )
602         {
603             p_dvd->i_angle = 1;
604         }
605
606         /*
607          * We've got enough info, time to open the title set data.
608          */
609         if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
610             p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
611             DVD_READ_TITLE_VOBS ) ) )
612         {
613             msg_Err( p_input, "cannot open title (VTS_%02d_1.VOB)",
614                      p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
615             ifoClose( p_vts );
616             ifoClose( p_vmg );
617             DVDClose( p_dvd->p_dvdread );
618             return VLC_EGENERIC;
619         }
620
621 //        IfoPrintTitle( p_dvd );
622
623         /*
624          * Destroy obsolete ES by reinitializing program 0
625          * and find all ES in title with ifo data
626          */
627         if( p_input->stream.pp_programs != NULL )
628         {
629             /* We don't use input_EndStream here since
630              * we keep area structures */
631
632             while( p_input->stream.i_es_number )
633             {
634                 input_DelES( p_input, p_input->stream.pp_es[0] );
635             }
636
637             while( p_input->stream.i_pgrm_number )
638             {
639                 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
640             }
641
642             if( p_input->stream.pp_selected_es )
643             {
644                 free( p_input->stream.pp_selected_es );
645                 p_input->stream.pp_selected_es = NULL;
646             }
647             p_input->stream.i_selected_es_number = 0;
648         }
649
650         input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
651         p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
652
653         for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
654         {
655             input_AddProgram( p_input, i+1, 0 );
656         }
657
658         DvdReadSetProgram( p_input,
659                            p_input->stream.pp_programs[p_dvd->i_angle-1] );
660
661         /* No PSM to read in DVD mode, we already have all information */
662         p_input->stream.p_selected_program->b_is_ok = VLC_TRUE;
663
664         p_es = NULL;
665
666         /* ES 0 -> video MPEG2 */
667 //        IfoPrintVideo( p_dvd );
668
669         p_es = input_AddES( p_input, NULL, 0xe0, 0 );
670         p_es->i_stream_id = 0xe0;
671         p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
672         p_es->i_cat = VIDEO_ES;
673
674 #define audio_control \
675     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
676         /* Audio ES, in the order they appear in .ifo */
677         for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
678         {
679             int i_position = 0;
680             uint16_t i_id;
681
682 //            IfoPrintAudio( p_dvd, i );
683
684             /* audio channel is active if first byte is 0x80 */
685             if( audio_control & 0x8000 )
686             {
687                 i_audio_nb++;
688                 i_position = ( audio_control & 0x7F00 ) >> 8;
689
690             msg_Dbg( p_input, "audio position  %d", i_position );
691                 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
692                 {
693                 case 0x00:              /* A52 */
694                     i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
695                     p_es = input_AddES( p_input, NULL, i_id, 0 );
696                     p_es->i_stream_id = 0xbd;
697                     p_es->i_fourcc = VLC_FOURCC('a','5','2','b');
698                     p_es->i_cat = AUDIO_ES;
699                     strcpy( p_es->psz_desc, DecodeLanguage(
700                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
701                     strcat( p_es->psz_desc, " (A52)" );
702
703                     break;
704                 case 0x02:
705                 case 0x03:              /* MPEG audio */
706                     i_id = 0xc0 + i_position;
707                     p_es = input_AddES( p_input, NULL, i_id, 0 );
708                     p_es->i_stream_id = i_id;
709                     p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
710                     p_es->i_cat = AUDIO_ES;
711                     strcpy( p_es->psz_desc, DecodeLanguage(
712                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
713                     strcat( p_es->psz_desc, " (mpeg)" );
714
715                     break;
716                 case 0x04:              /* LPCM */
717
718                     i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
719                     p_es = input_AddES( p_input, NULL, i_id, 0 );
720                     p_es->i_stream_id = i_id;
721                     p_es->i_fourcc = VLC_FOURCC('l','p','c','b');
722                     p_es->i_cat = AUDIO_ES;
723                     strcpy( p_es->psz_desc, DecodeLanguage(
724                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) );
725                     strcat( p_es->psz_desc, " (lpcm)" );
726
727                     break;
728                 case 0x06:              /* DTS */
729                     i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
730                     msg_Err( p_input, "DTS audio not handled yet"
731                                       "(0x%x)", i_id );
732                     break;
733                 default:
734                     i_id = 0;
735                     msg_Err( p_input, "unknown audio type %.2x",
736                           p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
737                 }
738             }
739         }
740 #undef audio_control
741 #define spu_control \
742     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
743
744         /* Sub Picture ES */
745
746         for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
747         {
748             int i_position = 0;
749             uint16_t i_id;
750
751 //            IfoPrintSpu( p_dvd, i );
752             msg_Dbg( p_input, "spu %d 0x%02x", i, spu_control );
753
754             if( spu_control & 0x80000000 )
755             {
756                 i_spu_nb++;
757
758                 /*  there are several streams for one spu */
759                 if(  p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
760                 {
761                     /* 16:9 */
762                     switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
763                     {
764                     case 1:
765                         i_position = spu_control & 0xff;
766                         break;
767                     case 2:
768                         i_position = ( spu_control >> 8 ) & 0xff;
769                         break;
770                     default:
771                         i_position = ( spu_control >> 16 ) & 0xff;
772                         break;
773                     }
774                 }
775                 else
776                 {
777                     /* 4:3 */
778                     i_position = ( spu_control >> 24 ) & 0x7F;
779                 }
780
781                 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
782                 p_es = input_AddES( p_input, NULL, i_id, 0 );
783                 p_es->i_stream_id = 0xbd;
784                 p_es->i_fourcc = VLC_FOURCC('s','p','u','b');
785                 p_es->i_cat = SPU_ES;
786                 strcpy( p_es->psz_desc, DecodeLanguage(
787                     p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) );
788             }
789         }
790 #undef spu_control
791
792         /* FIXME: hack to check that the demuxer is ready, and set
793          * the decoders */
794         if( p_input->p_demux )
795         {
796             DvdReadLauchDecoders( p_input );
797         }
798
799         /* Update the navigation variables without triggering a callback */
800         val.i_int = p_area->i_id;
801         var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
802         var_Change( p_input, "chapter", VLC_VAR_CLEARCHOICES, NULL, NULL );
803         for( i = 1; i <= p_area->i_part_nb; i++ )
804         {
805             val.i_int = i;
806             var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val, NULL );
807         }
808
809     } /* i_title >= 0 */
810     else
811     {
812         p_area = p_input->stream.p_selected_area;
813     }
814
815     /*
816      * Chapter selection
817      */
818
819     if( p_area->i_part != p_dvd->i_chapter )
820     {
821         if( ( p_area->i_part > 0 ) &&
822             ( p_area->i_part <= p_area->i_part_nb ))
823         {
824             p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
825             pgc_id = p_vts->vts_ptt_srpt->title[
826                         p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
827             pgn = p_vts->vts_ptt_srpt->title[
828                         p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
829
830             p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
831
832             p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
833             p_dvd->i_chapter = p_area->i_part;
834             DvdReadFindCell( p_dvd );
835
836             p_dvd->i_pack_len = 0;
837             p_dvd->i_next_vobu = p_dvd->i_cur_block =
838                     p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
839         }
840         else
841         {
842             p_area->i_part = p_dvd->i_chapter;
843         }
844     }
845 #undef p_vts
846 #undef p_vmg
847
848     /* warn interface that something has changed */
849     p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
850     p_input->stream.b_seekable = VLC_TRUE;
851     p_input->stream.b_changed = VLC_TRUE;
852
853     /* Update the navigation variables without triggering a callback */
854     val.i_int = p_area->i_part;
855     var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
856
857     return VLC_SUCCESS;
858 }
859
860
861 /*****************************************************************************
862  * DvdReadRead: reads data packets.
863  *****************************************************************************
864  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
865  * bytes.
866  *****************************************************************************/
867 static int DvdReadRead( input_thread_t * p_input,
868                         byte_t * p_buffer, size_t i_count )
869 {
870     thread_dvd_data_t *     p_dvd;
871     byte_t *                p_buf;
872     unsigned int            i_blocks_once;
873     unsigned int            i_blocks;
874     int                     i_read;
875     int                     i_read_total;
876     vlc_bool_t              b_eot = VLC_FALSE;
877
878     p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
879     p_buf = p_buffer;
880
881     /*
882      * Playback by cell in this pgc, starting at the cell for our chapter.
883      */
884     i_blocks = OFF2LB( i_count );
885     i_read_total = 0;
886     i_read = 0;
887
888     while( i_blocks )
889     {
890         /*
891          * End of pack, we select the following one
892          */
893         if( ! p_dvd->i_pack_len )
894         {
895             /*
896              * Read NAV packet.
897              */
898             if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
899                            1, p_buf ) ) != 1 )
900             {
901                 msg_Err( p_input, "read failed for block %d",
902                                   p_dvd->i_next_vobu );
903                 return -1;
904             }
905
906             /* basic check to be sure we don't have a empty title
907              * go to next title if so */
908             //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
909
910             /*
911              * Parse the contained dsi packet.
912              */
913
914             DvdReadHandleDSI( p_dvd, p_buf );
915
916             /* End of title */
917             if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
918             {
919                 b_eot = 1;
920                 break;
921             }
922
923             assert( p_dvd->i_pack_len < 1024 );
924             /* FIXME: Ugly kludge: we send the pack block to the input for it
925              * sometimes has a zero scr and restart the sync */
926             p_dvd->i_cur_block ++;
927             //p_dvd->i_pack_len++;
928
929             i_read_total++;
930             p_buf += DVD_VIDEO_LB_LEN;
931             i_blocks--;
932         }
933
934         /*
935          * Compute the number of blocks to read
936          */
937         i_blocks_once = __MIN( p_dvd->i_pack_len, i_blocks );
938         p_dvd->i_pack_len -= i_blocks_once;
939
940         /* Reads from DVD */
941         i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
942                                 i_blocks_once, p_buf );
943         if( (unsigned int)i_read != i_blocks_once )
944         {
945             msg_Err( p_input, "read failed for %d/%d blocks at 0x%02x",
946                               i_read, i_blocks_once, p_dvd->i_cur_block );
947             return -1;
948         }
949
950         i_blocks -= i_read;
951         i_read_total += i_read;
952         p_dvd->i_cur_block += i_read;
953         p_buf += LB2OFF( i_read );
954
955     }
956 /*
957     msg_Dbg( p_input, "i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
958 */
959
960     vlc_mutex_lock( &p_input->stream.stream_lock );
961
962     if( p_dvd->b_eoc )
963     {
964         /* We modify i_part only at end of chapter not to erase
965          * some modification from the interface */
966         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
967         p_dvd->b_eoc = VLC_FALSE;
968     }
969
970     if( ( LB2OFF( p_dvd->i_cur_block )
971           - p_input->stream.p_selected_area->i_start )
972             >= p_input->stream.p_selected_area->i_size || b_eot )
973     {
974         if( ( p_input->stream.p_selected_area->i_id + 1 ) >=
975                         p_input->stream.i_area_nb )
976         {
977             /* EOF */
978             vlc_mutex_unlock( &p_input->stream.stream_lock );
979             return 0;
980         }
981
982         /* EOT */
983         msg_Dbg( p_input, "new title" );
984         DvdReadSetArea( p_input, p_input->stream.pp_areas[
985                         p_input->stream.p_selected_area->i_id+1] );
986     }
987
988     vlc_mutex_unlock( &p_input->stream.stream_lock );
989
990     return LB2OFF( i_read_total );
991 }
992 #undef p_pgc
993
994 /*****************************************************************************
995  * DvdReadSeek : Goes to a given position on the stream.
996  *****************************************************************************
997  * This one is used by the input and translate chronological position from
998  * input to logical position on the device.
999  * The lock should be taken before calling this function.
1000  *****************************************************************************/
1001 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
1002 {
1003     thread_dvd_data_t *     p_dvd;
1004     unsigned int            i_lb;
1005     unsigned int            i_tmp;
1006     unsigned int            i_chapter = 0;
1007     unsigned int            i_cell = 0;
1008     unsigned int            i_vobu = 0;
1009     unsigned int            i_sub_cell = 0;
1010
1011     vlc_mutex_lock( &p_input->stream.stream_lock );
1012     i_off += p_input->stream.p_selected_area->i_start;
1013     vlc_mutex_unlock( &p_input->stream.stream_lock );
1014
1015     i_lb = OFF2LB( i_off );
1016     p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
1017
1018     /* find cell */
1019     while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1020     {
1021         i_cell++;
1022     }
1023
1024     /* find chapter */
1025     do
1026     {
1027         pgc_t *     p_pgc;
1028         int         pgc_id, pgn;
1029
1030         i_chapter++;
1031         pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1032                     p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1033         pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1034                     p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1035
1036         p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1037         i_tmp = p_pgc->program_map[pgn-1];
1038
1039     } while( i_tmp <= i_cell );
1040
1041     /* find vobu */
1042     while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1043             <= i_lb )
1044     {
1045         i_vobu++;
1046     }
1047
1048     /* find sub_cell */
1049     while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1050             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1051     {
1052         i_sub_cell++;
1053     }
1054
1055 /*
1056     msg_Dbg( p_input, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1057             i_cell, i_sub_cell,i_chapter, i_vobu,
1058             p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1059             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1060             p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1061 */
1062     p_dvd->i_cur_block = i_lb;
1063     p_dvd->i_next_vobu =
1064         p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1065     p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1066     p_dvd->i_cur_cell = i_cell;
1067     p_dvd->i_chapter = i_chapter;
1068     DvdReadFindCell( p_dvd );
1069
1070     vlc_mutex_lock( &p_input->stream.stream_lock );
1071     p_input->stream.p_selected_area->i_tell =
1072         LB2OFF ( p_dvd->i_cur_block )
1073          - p_input->stream.p_selected_area->i_start;
1074     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1075     vlc_mutex_unlock( &p_input->stream.stream_lock );
1076
1077     return;
1078 }
1079
1080 /*****************************************************************************
1081  * DvdReadHandleDSI
1082  *****************************************************************************/
1083 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, uint8_t * p_data )
1084 {
1085     navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1086
1087     /*
1088      * Determine where we go next.  These values are the ones we mostly
1089      * care about.
1090      */
1091     p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1092
1093     /*
1094      * If we're not at the end of this cell, we can determine the next
1095      * VOBU to display using the VOBU_SRI information section of the
1096      * DSI.  Using this value correctly follows the current angle,
1097      * avoiding the doubled scenes in The Matrix, and makes our life
1098      * really happy.
1099      */
1100     if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1101     {
1102 #if 1
1103         switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1104         {
1105             case 0x4:
1106                 /* interleaved unit with no angle */
1107                 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != 0 )
1108                 {
1109                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1110                         p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1111                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1112                 }
1113                 else
1114                 {
1115                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1116                         p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1117                     p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1118                 }
1119                 break;
1120             case 0x5:
1121                 /* vobu is end of ilvu */
1122                 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1123                 {
1124                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1125                         p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1126                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1127
1128                     break;
1129                 }
1130             case 0x6:
1131                 /* vobu is beginning of ilvu */
1132             case 0x9:
1133                 /* next scr is 0 */
1134             case 0xa:
1135                 /* entering interleaved section */
1136             case 0x8:
1137                 /* non interleaved cells in interleaved section */
1138             default:
1139                 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1140                     ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1141                 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1142                 break;
1143         }
1144 #else
1145         p_dvd->i_next_vobu = p_dvd->i_cur_block +
1146             ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1147         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1148 #endif
1149     }
1150     else
1151     {
1152         p_dvd->i_cur_cell = p_dvd->i_next_cell;
1153         DvdReadFindCell( p_dvd );
1154
1155         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1156         p_dvd->i_next_vobu =
1157             p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1158     }
1159
1160 #if 0
1161     msg_Dbg( p_input, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1162              p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1163              p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1164              p_dvd->dsi_pack.dsi_gi.vobu_ea,
1165              p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1166              p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1167
1168     msg_Dbg( p_input, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1169              p_dvd->dsi_pack.sml_pbi.category,
1170              p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1171              p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1172              p_dvd->dsi_pack.sml_pbi.size );
1173
1174     msg_Dbg( p_input, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1175              p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1176              p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1177              p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1178 #endif
1179 }
1180
1181 /*****************************************************************************
1182  * DvdReadFindCell
1183  *****************************************************************************/
1184 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1185 {
1186     int         pgc_id, pgn;
1187     int         i = 0;
1188     pgc_t *     p_pgc;
1189 #define cell p_dvd->p_cur_pgc->cell_playback
1190     if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1191     {
1192 #if 0
1193         p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1194         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1195 #else
1196         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1197
1198         while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1199         {
1200             i++;
1201         }
1202         p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1203 #endif
1204     }
1205     else
1206     {
1207         p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1208     }
1209 #undef cell
1210     pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1211                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1212     pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1213                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1214     p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1215
1216     if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1217     {
1218         p_dvd->i_chapter++;
1219         p_dvd->b_eoc = VLC_TRUE;
1220     }
1221 }
1222
1223 /*****************************************************************************
1224  * DvdReadLaunchDecoders
1225  *****************************************************************************/
1226 static void DvdReadLauchDecoders( input_thread_t * p_input )
1227 {
1228     thread_dvd_data_t * p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
1229     int i_audio, i_spu;
1230
1231     input_SelectES( p_input, p_input->stream.pp_es[0] );
1232
1233     /* For audio: first one if none or a not existing one specified */
1234     i_audio = config_GetInt( p_input, "audio-channel" );
1235     if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1236     {
1237         config_PutInt( p_input, "audio-channel", 1 );
1238         i_audio = 1;
1239     }
1240     if( i_audio > 0/* && i_audio_nb > 0*/ )
1241     {
1242         if( config_GetInt( p_input, "audio-type" ) == REQUESTED_A52 )
1243         {
1244             int     i_a52 = i_audio;
1245             while( ( p_input->stream.pp_es[i_a52]->i_fourcc !=
1246                    VLC_FOURCC('a','5','2','b') ) && ( i_a52 <=
1247                    p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
1248             {
1249                 i_a52++;
1250             }
1251             if( p_input->stream.pp_es[i_a52]->i_fourcc
1252                  == VLC_FOURCC('a','5','2','b') )
1253             {
1254                 input_SelectES( p_input, p_input->stream.pp_es[i_a52] );
1255             }
1256         }
1257         else
1258         {
1259             input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
1260         }
1261     }
1262
1263     /* for spu, default is none */
1264     i_spu = config_GetInt( p_input, "spu-channel" );
1265     if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1266     {
1267         config_PutInt( p_input, "spu-channel", 0 );
1268         i_spu = 0;
1269     }
1270     if( i_spu > 0 /*&& i_spu_nb > 0*/ )
1271     {
1272         i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
1273         input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
1274     }
1275 }