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