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